From 44cab88546c6511483f495a36c9cad2499699421 Mon Sep 17 00:00:00 2001 From: butubb <1422726308@qq.com> Date: Fri, 4 Sep 2026 13:23:04 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20Agent=20=E6=AD=A5=E9=AA=A4=E4=B8=8A?= =?UTF-8?q?=E9=99=90=E4=BC=98=E5=8C=96=E2=80=94=E2=80=94max=5Fsteps=2020?= =?UTF-8?q?=E2=86=9240=EF=BC=88=E5=A4=9A=E6=AD=A5=E4=BB=BB=E5=8A=A1?= =?UTF-8?q?=E5=A6=82=E6=89=93=E5=BC=80=E6=8A=96=E9=9F=B3=E6=90=9C=E7=B4=A2?= =?UTF-8?q?=EF=BC=89=EF=BC=8Csystem=20prompt=20=E5=8A=A0=E6=94=B6=E6=95=9B?= =?UTF-8?q?=E8=A7=84=E8=8C=83=EF=BC=88=E6=97=A0=E8=BF=9B=E5=B1=95=206=20?= =?UTF-8?q?=E6=AD=A5=E4=B8=BB=E5=8A=A8=E5=81=9C=E6=AD=A2=E6=80=BB=E7=BB=93?= =?UTF-8?q?=EF=BC=89=EF=BC=8C=E8=B6=85=E9=99=90=E6=97=B6=E6=97=A0=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E6=94=B6=E5=B0=BE=E8=AF=B7=E6=B1=82=E8=AE=A9=E6=A8=A1?= =?UTF-8?q?=E5=9E=8B=E7=BB=99=E6=9C=89=E6=84=8F=E4=B9=89=E6=80=BB=E7=BB=93?= =?UTF-8?q?=EF=BC=88=E4=B8=8D=E5=86=8D=E6=9C=BA=E6=A2=B0=E6=8F=90=E7=A4=BA?= =?UTF-8?q?=EF=BC=89=EF=BC=9Bweb=20=E8=B6=85=E6=97=B6=20600=E2=86=92900s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mcp_agent/agent.py | 25 ++++++++++++++++++++++++- mcp_agent/config.py | 2 +- web/agent_api.py | 2 +- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/mcp_agent/agent.py b/mcp_agent/agent.py index 41f4781..15bca89 100644 --- a/mcp_agent/agent.py +++ b/mcp_agent/agent.py @@ -32,6 +32,8 @@ SYSTEM_PROMPT = """你是手机自动化控制助手。你通过工具实时操 5. 每次关键操作后再次 de_screenshot 验证结果,直到完成用户目标 6. 完成或失败时用中文总结:做了什么、当前状态、需要用户注意的事项 7. 设备不可用/操作失败时如实报告错误,不要臆测成功 +8. 效率:界面未变化时不要重复截图/点击同一位置;每步都要推进目标; + 若连续 6 步无进展(截图内容未变/操作无效),停止并总结原因,不要空转 可用工具清单将由系统提供。""" @@ -214,7 +216,28 @@ class Agent: # 无工具调用:本轮即最终回答 return full_content - return "(达到最大步骤数未完成,请检查操作是否卡在循环)" + # 步骤超限:不带工具让模型做最终总结(避免机械提示,给用户有意义的结论) + try: + _log.warning("达到最大步骤数,请求模型收尾总结") + saved_tools = self.tools_schema + self.tools_schema = [] + self.messages.append({"role": "user", + "content": "已达最大操作步骤数,请立即用中文总结:" + "已完成的部分、当前设备状态、未能完成的原因与下一步建议。" + "不要调用任何工具。"}) + parts = [] + async for chunk in self._chat_stream(): + delta = (chunk.get("choices") or [{}])[0].get("delta") or {} + text = delta.get("content") + if text: + parts.append(text) + if on_delta: + on_delta(text, "content") + self.tools_schema = saved_tools + summary = "".join(parts) + return summary or "(已达步骤上限,模型未能生成总结)" + except Exception as e: + return f"(已达最大步骤数,且收尾总结失败: {e})" # ---------- 非流式(CLI) ---------- async def run(self, prompt: str, serial: str = "") -> str: diff --git a/mcp_agent/config.py b/mcp_agent/config.py index 6c6c49c..74ed891 100644 --- a/mcp_agent/config.py +++ b/mcp_agent/config.py @@ -23,7 +23,7 @@ class AgentSettings: default_serial = _env("AGENT_DEFAULT_SERIAL", "") # Agent 循环上限与请求超时 - max_steps = int(_env("AGENT_MAX_STEPS", "20")) + max_steps = int(_env("AGENT_MAX_STEPS", "40")) request_timeout = float(_env("AGENT_TIMEOUT", "120")) # system prompt 语言 diff --git a/web/agent_api.py b/web/agent_api.py index 6d2db18..d1e1759 100644 --- a/web/agent_api.py +++ b/web/agent_api.py @@ -201,7 +201,7 @@ def _agent_thread(run_id, prompt, serial, cfg): on_delta=on_delta, on_tool=on_tool) # 整体超时保护:卡死时结束,释放单实例 - answer = asyncio.run(asyncio.wait_for(_execute(), timeout=600)) + answer = asyncio.run(asyncio.wait_for(_execute(), timeout=900)) with _lock: _run["state"] = "done" _run["answer"] = answer