From 6589f7f4815d68c8167892150dc1a481ec8d06d3 Mon Sep 17 00:00:00 2001 From: Sakura-RanChen <1908198662@qq.com> Date: Wed, 4 Nov 2026 11:26:20 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E4=B8=BAcode=E5=88=A4?= =?UTF-8?q?=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugins_func/functions/web_search.py | 16 ++++++++++++---- .../plugins_func/test_web_search_outcrawl.py | 15 +++++++++------ 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/main/xiaozhi-server/plugins_func/functions/web_search.py b/main/xiaozhi-server/plugins_func/functions/web_search.py index fab57ffe4..623fc5f66 100644 --- a/main/xiaozhi-server/plugins_func/functions/web_search.py +++ b/main/xiaozhi-server/plugins_func/functions/web_search.py @@ -166,13 +166,21 @@ async def _search_outcrawl(api_key: str, query: str, max_results: int) -> str: logger.bind(tag=TAG).debug(f"Outcrawl搜索响应 | status: {response.status_code}") if response.status_code != 200: + # 错误信封为 {"code": "...", "message": "...", "details": {}}, + # code 是官方承诺的封闭枚举 + code = data.get("code", "") if isinstance(data, dict) else "" detail = data.get("message", "") if isinstance(data, dict) else "" logger.bind(tag=TAG).error( - f"Outcrawl搜索失败 | status: {response.status_code} | detail: {detail}" + f"Outcrawl搜索失败 | status: {response.status_code} | code: {code} | detail: {detail}" ) - if response.status_code == 402: - return "联网搜索额度已用完,请检查Outcrawl账户余额。" - return "联网搜索请求失败,请检查API Key是否正确。" + if code == "quota_exceeded": + # 该code覆盖两种情况:每分钟请求数限流,或月度credits用尽 + return "联网搜索被限流或额度已用完,请稍后重试或检查Outcrawl账户余额。" + if code == "unauthorized": + return "联网搜索请求失败,请检查API Key是否正确。" + # 其余code(bad_request、no_capacity、search_refused、internal等) + # 多为输入问题或服务端暂时性故障,统一引导重试 + return "联网搜索请求失败,请稍后重试。" hits = data.get("hits", []) if not hits: diff --git a/main/xiaozhi-server/tests/plugins_func/test_web_search_outcrawl.py b/main/xiaozhi-server/tests/plugins_func/test_web_search_outcrawl.py index 31006aa36..12b9f8d12 100644 --- a/main/xiaozhi-server/tests/plugins_func/test_web_search_outcrawl.py +++ b/main/xiaozhi-server/tests/plugins_func/test_web_search_outcrawl.py @@ -95,15 +95,18 @@ async def test_outcrawl_empty_hits(fake_http): @pytest.mark.parametrize( - ("status_code", "message"), + ("code", "message"), [ - (401, "联网搜索请求失败,请检查API Key是否正确。"), - (402, "联网搜索额度已用完,请检查Outcrawl账户余额。"), - (429, "联网搜索请求失败,请检查API Key是否正确。"), + ("unauthorized", "联网搜索请求失败,请检查API Key是否正确。"), + ("quota_exceeded", "联网搜索被限流或额度已用完,请稍后重试或检查Outcrawl账户余额。"), + ("bad_request", "联网搜索请求失败,请稍后重试。"), + ("internal", "联网搜索请求失败,请稍后重试。"), ], ) -async def test_outcrawl_error_status(fake_http, status_code, message): - fake_http.response = FakeResponse(status_code=status_code, payload={"message": "denied"}) +async def test_outcrawl_error_code(fake_http, code, message): + fake_http.response = FakeResponse( + status_code=429, payload={"code": code, "message": "denied", "details": {}} + ) assert await web_search_module._search_outcrawl("oc-key", "q", 3) == message