fix: auto-inject context-aware tool, works on any turn

This commit is contained in:
ymq 2026-08-10 17:57:53 +08:00
parent 23a04d2b94
commit 91dabd7b4c

View File

@ -136,20 +136,26 @@ class AgentExecutor:
action_type = act.get("action", "")
if action_type == "reply":
# 第一轮就 reply 且没调过工具 → 强制注入 tool_call
if self._tool_call_count == 0 and turn == 0:
# 任何轮次,只要没成功调过工具就 reply → 强制注入工具调用
if self._tool_call_count == 0:
# 根据用户输入推断合适的工具
hint_tool = "diagnose_project"
if any(kw in user_input.lower() for kw in ["任务", "task", "list"]):
hint_tool = "list_tasks"
elif any(kw in user_input.lower() for kw in ["问题", "question"]):
hint_tool = "list_questions"
yield json.dumps({
"type": "auto_tool", "message": "未调工具,自动注入 diagnose_project",
"type": "auto_tool", "message": f"未调工具,自动注入 {hint_tool}",
}, ensure_ascii=False) + "\n"
self._msgs.append({
"role": "assistant",
"content": "已调用 diagnose_project",
"content": f"已调用 {hint_tool}",
})
self._msgs.append({
"role": "user",
"content": "请调用 diagnose_project 查看项目状态",
"content": f"请调用 {hint_tool} 工具,只输出 {{\"action\":\"tool_call\",\"tool\":\"{hint_tool}\",\"params\":{{}}}}",
})
continue # 不退出,继续下一轮
continue
final_reply = act.get("message", "")
yield json.dumps({