From 9d4cf15c96fdcfd759e9cf3ef32a38ccd748b119 Mon Sep 17 00:00:00 2001 From: root <953994191@qq.com> Date: Fri, 1 May 2026 00:13:13 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E7=BB=9F=E4=B8=80=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E6=96=B0=E7=89=88=20React=20=E6=A8=A1=E5=BC=8F?= =?UTF-8?q?=E5=9B=BE=EF=BC=8C=E7=A7=BB=E9=99=A4=E6=97=A7=E7=89=88=20GraphB?= =?UTF-8?q?uilder=20=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/agent/service.py | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/backend/app/agent/service.py b/backend/app/agent/service.py index 31e6893..5f6776a 100644 --- a/backend/app/agent/service.py +++ b/backend/app/agent/service.py @@ -7,7 +7,7 @@ import json import asyncio # 本地模块 -from app.main_graph.graph_builder import GraphBuilder, GraphContext +from app.main_graph.utils.subgraph_builder import build_react_main_graph from app.main_graph.tools.graph_tools import AVAILABLE_TOOLS, TOOLS_BY_NAME from app.main_graph.config import set_stream_writer from ..model_services.chat_services import get_all_chat_services, LocalVLLMChatProvider @@ -36,13 +36,12 @@ class AIAgentService: self.tools.append(rag_tool) self.tools_by_name[rag_tool.name] = rag_tool - # 2. 构建各模型的 Graph + # 2. 构建各模型的 Graph(使用新版 React 模式) chat_services = get_all_chat_services() for name, llm in chat_services.items(): try: info(f"🔄 初始化模型 '{name}'...") - builder = GraphBuilder(llm, self.tools, self.tools_by_name).build() - graph = builder.compile(checkpointer=self.checkpointer) + graph = build_react_main_graph().compile(checkpointer=self.checkpointer) self.graphs[name] = graph info(f"✅ 模型 '{name}' 初始化成功") except Exception as e: @@ -67,14 +66,22 @@ class AIAgentService: "configurable": {"thread_id": thread_id}, "metadata": {"user_id": user_id} } - input_state = {"messages": [{"role": "user", "content": message}]} - context = GraphContext(user_id=user_id) + # 新版状态输入:传入完整的 MainGraphState,关键是 user_query + from app.main_graph.state import MainGraphState, CurrentAction + input_state = { + "user_query": message, + "messages": [{"role": "user", "content": message}], + "user_id": user_id, + "current_action": CurrentAction.NONE + } - result = await graph.ainvoke(input_state, config=config, context=context) + result = await graph.ainvoke(input_state, config=config) - reply = result["messages"][-1].content - token_usage = result.get("last_token_usage", {}) - elapsed_time = result.get("last_elapsed_time", 0.0) + reply = result.get("final_result", "") + if not reply and result.get("messages"): + reply = result["messages"][-1].content + token_usage = result.get("debug_info", {}).get("token_usage", {}) + elapsed_time = result.get("debug_info", {}).get("elapsed_time", 0.0) return { "reply": reply,