diff --git a/backend/app/main_graph/nodes/agent.py b/backend/app/main_graph/nodes/agent.py index 225468f..7248269 100644 --- a/backend/app/main_graph/nodes/agent.py +++ b/backend/app/main_graph/nodes/agent.py @@ -68,22 +68,36 @@ def create_agent_node(llm_with_tools, llm): """ info(f"[Agent] 第 {state.current_step} 步推理") - # 组装完整消息:系统提示 + 历史消息 - full_messages = [SystemMessage(content=SYSTEM_PROMPT)] + state.messages + try: + # 组装完整消息:系统提示 + 历史消息 + full_messages = [SystemMessage(content=SYSTEM_PROMPT)] + state.messages + + info(f"[Agent] 消息数量: {len(full_messages)}, 最后一条: {type(full_messages[-1]).__name__}") - # 判断是否达到步数上限 - if state.current_step >= state.max_steps: - info(f"[Agent] 达到步数上限 {state.max_steps},强制结束,不绑定工具") - llm_no_tools = llm.bind_tools([]) - response = await llm_no_tools.ainvoke(full_messages) - else: - response = await llm_with_tools.ainvoke(full_messages) + # 判断是否达到步数上限 + if state.current_step >= state.max_steps: + info(f"[Agent] 达到步数上限 {state.max_steps},强制结束,不绑定工具") + llm_no_tools = llm.bind_tools([]) + response = await llm_no_tools.ainvoke(full_messages) + else: + info(f"[Agent] 调用带工具的 LLM...") + response = await llm_with_tools.ainvoke(full_messages) + + info(f"[Agent] LLM 调用成功!响应类型: {type(response).__name__}") + if hasattr(response, 'tool_calls') and response.tool_calls: + info(f"[Agent] 检测到工具调用: {[tc['name'] for tc in response.tool_calls]}") - # 返回状态更新(注意:不原地修改 state,返回字典让 LangGraph 处理 - return { - "messages": [response], - "current_step": state.current_step + 1, - "llm_calls": state.llm_calls + 1 - } + # 返回状态更新(注意:不原地修改 state,返回字典让 LangGraph 处理 + return { + "messages": [response], + "current_step": state.current_step + 1, + "llm_calls": state.llm_calls + 1 + } + + except Exception as e: + error(f"[Agent] ❌ 第 {state.current_step} 步推理出错: {e}") + import traceback + error(f"[Agent] 堆栈: {traceback.format_exc()}") + raise return agent_node