refactor: 删auto-inject硬编码关键词强制路由,reply/ask_user为合法终止动作,恢复agent诚实降级能力

This commit is contained in:
ymq 2026-08-15 08:19:03 +08:00
parent 099c283562
commit 1860179a39

View File

@ -184,43 +184,9 @@ class AgentExecutor:
action_type = act.get("action", "")
if action_type == "reply":
# 任何轮次,只要没成功调过工具就 reply → 强制注入工具调用
if self._tool_call_count == 0:
# 意图识别:新需求描述(长文本+需求特征词)→ create_task 推进
req_keywords = ["实现", "开发", "建设", "系统", "功能", "需求", "模块",
"支持", "切换", "监控", "备份", "同步", "要做", "设计", "平台"]
is_requirement = (len(user_input) > 40 and
any(kw in user_input for kw in req_keywords))
if is_requirement:
hint_tool = "create_task"
elif 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"
else:
hint_tool = "diagnose_project"
yield json.dumps({
"type": "auto_tool", "message": f"未调工具,自动注入 {hint_tool}",
}, ensure_ascii=False) + "\n"
self._msgs.append({"role": "assistant", "content": f"已调用 {hint_tool}"})
if hint_tool == "create_task":
self._msgs.append({"role": "user", "content":
"用户描述了新需求,请用 create_task 创建任务推进(可先 role=requirement 做需求分析,再拆分到 develop填好 title/role/description 参数,输出 tool_call JSON。"})
else:
self._msgs.append({"role": "user", "content": f"请调用 {hint_tool} 工具,只输出 {{\"action\":\"tool_call\",\"tool\":\"{hint_tool}\",\"params\":{{}}}}"})
continue
# 调了工具但太少1-2次LLM 想停 → 推它继续深入最多3次
if self._tool_call_count <= 2 and self._auto_push_count < 3:
self._auto_push_count += 1
hint = "请继续:查看失败任务的详情(task_detail),查看待回答问题(list_questions)或启动agent(start_agents)"
yield json.dumps({
"type": "auto_tool", "message": f"已调{self._tool_call_count}次工具,继续深入({self._auto_push_count}/3)",
}, ensure_ascii=False) + "\n"
self._msgs.append({"role": "assistant", "content": "继续分析"})
self._msgs.append({"role": "user", "content": hint})
continue
# reply 是合法终止动作,直接输出,不再强制注入工具。
# (历史版本会在 tool_call_count==0 时用硬编码关键词强制路由,
# 那会剥夺 LLM 诚实降级/能力自省的能力,把 agent 变成路由器。)
final_reply = act.get("message", "")
yield json.dumps({
"type": "reply", "message": final_reply,
@ -228,6 +194,15 @@ class AgentExecutor:
await self._save_turn(user_input, final_reply)
return
elif action_type == "ask_user":
# ask_user 是合法终止动作:把问题抛给用户,等待回答。
question = act.get("question", "")
yield json.dumps({
"type": "ask_user", "message": question,
}, ensure_ascii=False) + "\n"
await self._save_turn(user_input, f"[提问] {question}")
return
elif action_type == "tool_call":
tool_name = act.get("tool", "")
tool_params = act.get("params", {})