from deepagents.middleware.patch_tool_calls import PatchToolCallsMiddleware
from langchain.agents import create_agent
from langchain.agents.middleware import ModelRetryMiddleware, TodoListMiddleware

from yuxi.agents import BaseAgent, load_chat_model
from yuxi.agents.backends import create_agent_filesystem_middleware
from yuxi.agents.context import prepare_agent_runtime_context
from yuxi.agents.middlewares import (
    create_summary_middleware,
    save_attachments_to_fs,
)
from yuxi.agents.middlewares.knowledge_base import KnowledgeBaseMiddleware
from yuxi.agents.middlewares.skills import SkillsMiddleware
from yuxi.agents.middlewares.subagent_task import create_subagent_task_middleware
from yuxi.agents.toolkits.service import resolve_configured_runtime_tools

from .context import ChatBotContext
from .prompt import TODO_MID_PROMPT, build_prompt_with_context
from .state import ChatBotState


async def _build_middlewares(context):
    """构建中间件列表"""
    # summary middleware
    # 主 Agent 上下文优化：默认 100k tokens 触发压缩，保留最近 50%
    summary_trigger_tokens = getattr(context, "summary_threshold", 100) * 1024
    summary_middleware = create_summary_middleware(
        model=load_chat_model(fully_specified_name=context.model),
        trigger=("tokens", summary_trigger_tokens),
        keep=("tokens", summary_trigger_tokens // 2),
        trim_tokens_to_summarize=4000,
    )

    middlewares = [
        create_agent_filesystem_middleware(
            getattr(context, "tool_token_limit", 20) * 1024,
            context=context,
        ),
        save_attachments_to_fs,
        KnowledgeBaseMiddleware(),
        SkillsMiddleware(),
    ]
    subagent_middleware = await create_subagent_task_middleware(context)
    if subagent_middleware:
        middlewares.append(subagent_middleware)
    middlewares.extend(
        [
            summary_middleware,
            TodoListMiddleware(system_prompt=TODO_MID_PROMPT),
            PatchToolCallsMiddleware(),
            ModelRetryMiddleware(max_retries=getattr(context, "model_retry_times", 2)),
        ]
    )
    return middlewares


class ChatbotAgent(BaseAgent):
    name = "智能助手"
    description = "基础的对话机器人，可以回答问题，可在配置中启用需要的工具。"
    capabilities = ["file_upload", "files"]  # 支持文件上传功能
    context_schema = ChatBotContext

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    async def get_graph(self, context=None, **kwargs):

        context = await prepare_agent_runtime_context(
            context or self.context_schema(),
            context_schema=self.context_schema,
        )

        # 使用 create_agent 创建智能体
        graph = create_agent(
            model=load_chat_model(fully_specified_name=context.model),
            tools=await resolve_configured_runtime_tools(context),
            system_prompt=build_prompt_with_context(context),
            middleware=await _build_middlewares(context),
            state_schema=ChatBotState,
            checkpointer=await self._get_checkpointer(),
        )

        return graph


def main():
    pass


if __name__ == "__main__":
    main()
    # asyncio.run(main())
