From 1860179a39fb9d208cd61f5e1ae832df4d15f2c2 Mon Sep 17 00:00:00 2001 From: ymq Date: Sat, 15 Aug 2026 08:19:03 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=88=A0auto-inject=E7=A1=AC?= =?UTF-8?q?=E7=BC=96=E7=A0=81=E5=85=B3=E9=94=AE=E8=AF=8D=E5=BC=BA=E5=88=B6?= =?UTF-8?q?=E8=B7=AF=E7=94=B1=EF=BC=8Creply/ask=5Fuser=E4=B8=BA=E5=90=88?= =?UTF-8?q?=E6=B3=95=E7=BB=88=E6=AD=A2=E5=8A=A8=E4=BD=9C=EF=BC=8C=E6=81=A2?= =?UTF-8?q?=E5=A4=8Dagent=E8=AF=9A=E5=AE=9E=E9=99=8D=E7=BA=A7=E8=83=BD?= =?UTF-8?q?=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pipeline_service/agent_loop_v2.py | 49 ++++++++----------------------- 1 file changed, 12 insertions(+), 37 deletions(-) diff --git a/pipeline_service/agent_loop_v2.py b/pipeline_service/agent_loop_v2.py index d2a56fb..71eeb53 100644 --- a/pipeline_service/agent_loop_v2.py +++ b/pipeline_service/agent_loop_v2.py @@ -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", {})