From a195bdc101eb28258306b792dcc0fb97be6d2af8 Mon Sep 17 00:00:00 2001 From: Liang Jiaqing Date: Thu, 9 Jul 2026 13:45:01 +0800 Subject: [PATCH] Handle Claude refusal without retry --- assets/supergrok_proxy.py | 2 +- frontends/stapp.py | 1 + llmcore.py | 5 +++++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/assets/supergrok_proxy.py b/assets/supergrok_proxy.py index 41e411be..10559545 100644 --- a/assets/supergrok_proxy.py +++ b/assets/supergrok_proxy.py @@ -6,7 +6,7 @@ SuperGrok Local Proxy - CLI python assets/supergrok_proxy.py login python assets/supergrok_proxy.py serve --port 15433 python assets/supergrok_proxy.py models - python assets/supergrok_proxy.py test --model grok-4.3 + python assets/supergrok_proxy.py test --model grok-4.5 GenericAgent/mykey 配置示例: native_oai_config_supergrok_proxy = { diff --git a/frontends/stapp.py b/frontends/stapp.py index 731e53ae..0d336752 100644 --- a/frontends/stapp.py +++ b/frontends/stapp.py @@ -88,6 +88,7 @@ def get_controller(): threading.Thread(target=ag.run, daemon=True).start() while True: b['ev'].wait(); b['ev'].clear() + if ag.llm_no != agent.llm_no: ag.next_llm(agent.llm_no) dq = ag.put_task(build_prompt(b['obj']), source="controller") while 'done' not in (it := dq.get()): pass ms = re.findall(r'(.*?)', it['done'], re.S) diff --git a/llmcore.py b/llmcore.py index 5eaa016d..c9a976db 100644 --- a/llmcore.py +++ b/llmcore.py @@ -117,6 +117,10 @@ def auto_make_url(base, path): return f"{b}/{p}" if re.search(r'/v\d+(/|$)', b) else f"{b}/v1/{p}" def _parse_claude_json(data): + if data.get("stop_reason") == "refusal": + err = "[Error: Claude refusal]" + yield err + return [{"type": "text", "text": err}] content_blocks = data.get("content", []) _record_usage(data.get("usage", {}), "messages") for b in content_blocks: @@ -182,6 +186,7 @@ def _parse_claude_sse(resp_lines): if not warn: if not got_message_stop and not stop_reason: warn = "\n\n[!!! 流异常中断,未收到完整响应 !!!]" elif stop_reason == "max_tokens": warn = "\n\n[!!! Response truncated: max_tokens !!!]" + elif stop_reason == "refusal": warn = "\n\n[Error: Claude refusal]" if current_block: if current_block["type"] == "tool_use": try: current_block["input"] = json.loads(tool_json_buf) if tool_json_buf else {}