修复三个问题:1. 子图执行后的无限循环 2. llm_call没有输出 3. 思考打印两次

- 子图执行后直接进入finalize,避免回到react_reason循环
- llm_call节点检查是否已有final_result,避免重复调用LLM
- 直接在react_reason_node中通过adispatch_custom_event发送推理事件,避免通过state传递导致重复
This commit is contained in:
2026-05-02 09:00:34 +08:00
parent bd2c20d927
commit 2893accbc4
5 changed files with 139 additions and 86 deletions

View File

@@ -146,7 +146,7 @@ class AIAgentService:
"current_action": CurrentAction.NONE
}
# ========== 意图识别(保留用于日志) ==========
# ========== 意图识别(保留用于日志)==========
intent_result = await self.intent_classifier.classify(message)
info(f"🧠 意图识别: {intent_result.intent_type} (置信度: {intent_result.confidence:.2f})")
info(f"📝 推理: {intent_result.reasoning}")
@@ -265,34 +265,9 @@ class AIAgentService:
updates_data = chunk["data"]
serialized_data = self._serialize_value(updates_data)
# 检查是否有最新的推理内容(来自 react_reason_node
if isinstance(serialized_data, dict):
# 遍历所有节点的数据
for node_name, node_data in serialized_data.items():
if isinstance(node_data, dict) and "debug_info" in node_data:
debug_info = node_data["debug_info"]
latest_reasoning = debug_info.get("latest_reasoning")
if latest_reasoning and not latest_reasoning.get("sent"):
# 发送推理过程到前端
step = latest_reasoning.get("step", 1)
action = latest_reasoning.get("action", "unknown")
confidence = latest_reasoning.get("confidence", 0)
reasoning = latest_reasoning.get("reasoning", "")
info(f"[Agent Service] 发送推理过程 #{step}: {action}")
# 发送推理事件
yield {
"type": "react_reasoning",
"step": step,
"action": action,
"confidence": confidence,
"reasoning": reasoning
}
# 标记为已发送(避免重复发送)
latest_reasoning["sent"] = True
# 关键修复:不再从 updates 中读取 latest_reasoning避免重复
# 因为我们现在直接通过 custom 事件发送推理结果了
# 检查是否有人工审核请求
if "review_pending" in serialized_data and serialized_data["review_pending"]:
review_id = serialized_data.get("review_id", "")
@@ -302,7 +277,7 @@ class AIAgentService:
"review_id": review_id,
"content": content_to_review
}
# 检查是否有工具结果
if "messages" in serialized_data:
for msg in serialized_data["messages"]:
@@ -311,7 +286,7 @@ class AIAgentService:
tool_call_id = msg.get("tool_call_id", "")
tool_name = msg.get("name", "")
tool_output = msg.get("content", "")
if tool_call_id in tool_calls_in_progress:
yield {
"type": "tool_call_end",
@@ -320,19 +295,33 @@ class AIAgentService:
"result": tool_output
}
del tool_calls_in_progress[tool_call_id]
processed_event = {
"type": "state_update",
"data": serialized_data
}
elif chunk_type == "custom":
info(f"🎯 处理custom chunk")
serialized_data = self._serialize_value(chunk["data"])
processed_event = {
"type": "custom",
"data": serialized_data
}
info(f"🎯 处理custom chunk: {chunk['data']}")
custom_data = chunk["data"]
# 关键修复:处理我们从 react_reason_node 发送的自定义推理事件
if isinstance(custom_data, dict) and custom_data.get("type") == "react_reasoning":
info(f"[Agent Service] 收到自定义推理事件")
yield {
"type": "react_reasoning",
"step": custom_data.get("step", 1),
"action": custom_data.get("action", "unknown"),
"confidence": custom_data.get("confidence", 0),
"reasoning": custom_data.get("reasoning", "")
}
else:
# 处理其他自定义事件
serialized_data = self._serialize_value(custom_data)
processed_event = {
"type": "custom",
"data": serialized_data
}
if processed_event:
yield processed_event