From f166d1e504b0236ecc668107c63518317759b2c5 Mon Sep 17 00:00:00 2001 From: yumao Date: Thu, 23 Jul 2026 17:58:09 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20LLM=20=E5=AE=A2=E6=88=B7=E7=AB=AF?= =?UTF-8?q?=E8=87=AA=E5=BB=BA=E5=A4=A7=E8=B6=85=E6=97=B6=20Client=EF=BC=8C?= =?UTF-8?q?=E7=BB=95=E5=BC=80=20SDK=20=E9=BB=98=E8=AE=A4=2060s=20unary=20?= =?UTF-8?q?=E8=B6=85=E6=97=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CURSOR_MODEL=auto 时后端首 token 常超过 SDK 默认 unary_timeout, 自建带 DAILY_CURSOR_UNARY_TIMEOUT(300s)/DAILY_CURSOR_STREAM_TIMEOUT(900s) 的 Client 并显式传入 Agent.prompt,finally 中关闭释放资源。 Co-Authored-By: Claude Opus 4.8 (1M context) --- daily/llm_client.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/daily/llm_client.py b/daily/llm_client.py index eb3e709..1be3fd5 100644 --- a/daily/llm_client.py +++ b/daily/llm_client.py @@ -74,7 +74,7 @@ def _cursor_chat(system: str, user: str) -> str: api_key = (env("CURSOR_API_KEY") or "").strip() if not api_key: return "" - from cursor_sdk import Agent, AgentOptions, CursorAgentError, LocalAgentOptions + from cursor_sdk import Agent, AgentOptions, Client, CursorAgentError, LocalAgentOptions from daily.bridge_manager import warm_cursor_bridge @@ -83,6 +83,17 @@ def _cursor_chat(system: str, user: str) -> str: warm_cursor_bridge() model = env("CURSOR_MODEL") or "composer-2.5" prompt = f"{system}\n\n{user}" + + # SDK 默认 unary_timeout 只有 60s,CURSOR_MODEL=auto 时后端首 token 常超时; + # 自建带大超时的 Client 绕开 _default_client(),unary/stream 都放大。 + unary_timeout = env_int("DAILY_CURSOR_UNARY_TIMEOUT", 300) + stream_timeout = env_int("DAILY_CURSOR_STREAM_TIMEOUT", 900) + client = Client( + base_url=os.environ["CURSOR_SDK_BRIDGE_URL"], + auth_token=os.environ["CURSOR_SDK_BRIDGE_TOKEN"], + unary_timeout=unary_timeout, + stream_timeout=stream_timeout, + ) try: result = Agent.prompt( prompt, @@ -91,9 +102,12 @@ def _cursor_chat(system: str, user: str) -> str: model=model, local=LocalAgentOptions(cwd=cwd), ), + client=client, ) except CursorAgentError as exc: raise RuntimeError(f"LLM 调用失败:{exc.message}") from exc + finally: + client.close() if result.status == "error": raise RuntimeError(f"LLM 调用失败:{result.result or '未知错误'}") return (result.result or "").strip()