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