Skip to content

JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.

Python

fix(web_search): paid模式全部不可用时返回带详情错误阻止重试指定源

caozhenhua000002d4f287be13
diff --git a/jiuwenswarm/agents/harness/common/tools/web_search/orchestrator.py b/jiuwenswarm/agents/harness/common/tools/web_search/orchestrator.py
index 69944f9f5f..14008020e3 100644
--- a/jiuwenswarm/agents/harness/common/tools/web_search/orchestrator.py
+++ b/jiuwenswarm/agents/harness/common/tools/web_search/orchestrator.py
@@ -6,6 +6,7 @@ import logging
import re
from jiuwenswarm.agents.harness.common.tools.web_search.constants import KNOWN_PAID_PROVIDERS
+from jiuwenswarm.agents.harness.common.tools.web_search.free import _free_search_engines
from jiuwenswarm.agents.harness.common.tools.web_search.providers import (
any_paid_provider_available,
paid_provider_available,
@@ -181,7 +182,12 @@ async def run_web_search(
reason=f"requested source {preferred_source} unavailable: {reason}",
detail=paid_availability_report(settings.paid_provider_order),
)
- return f"[ERROR]: requested paid source '{preferred_source}' unavailable ({reason})."
+ return (
+ f"[ERROR]: requested paid source '{preferred_source}' unavailable ({reason}). "
+ "You may retry with search_mode=free."
+ if _free_search_engines()
+ else f"[ERROR]: requested paid source '{preferred_source}' unavailable ({reason})."
+ )
logger.debug(
"[web_search] start query=%r search_mode=%s search_source=%s %s",
truncate_query(query),
@@ -234,13 +240,16 @@ async def run_web_search(
paid_run, tried = await run_paid_chain(query, settings, preferred_provider=preferred_source)
else:
if not any_paid_provider_available(settings.paid_provider_order):
+ availability = paid_availability_report(settings.paid_provider_order)
_log_failed(
query=query,
mode=mode,
reason="paid unavailable",
- detail=paid_availability_report(settings.paid_provider_order),
+ detail=availability,
)
- return "[ERROR]: paid search unavailable."
+ if _free_search_engines():
+ return "[ERROR]: paid search unavailable. Use search_mode=free instead."
+ return "[ERROR]: paid search unavailable. No search source is available."
paid_run, tried = await run_paid_chain(query, settings)
if paid_run and paid_run.quality_passed:
_log_done(
@@ -267,7 +276,9 @@ async def run_web_search(
providers_tried=tried,
detail=provider_run_summary(paid_run),
)
- return "[ERROR]: paid search failed."
+ if _free_search_engines():
+ return "[ERROR]: paid search failed. You may retry with search_mode=free."
+ return "[ERROR]: paid search failed. No search source is available."
paid_run, tried = await run_paid_chain(query, settings, preferred_provider=preferred_source)
if paid_run and paid_run.quality_passed:
@@ -308,7 +319,11 @@ async def run_web_search(
providers_tried=tried,
detail=provider_run_summary(free_run),
)
- return f"[ERROR]: free search failed: {free_run.error}"
+ return (
+ "[ERROR]: web search failed: all sources exhausted (configuration issue, not query-related). "
+ "Do not retry with any search_mode or a different query; "
+ "inform the user that web search is currently unavailable."
+ )
if not _has_usable_results(free_run):
_log_failed(
query=query,
@@ -317,7 +332,11 @@ async def run_web_search(
providers_tried=tried,
detail=provider_run_summary(free_run),
)
- return "[ERROR]: free search failed: no results"
+ return (
+ "[ERROR]: web search failed: no results from any source. "
+ "Do not retry with any search_mode or a different query; "
+ "inform the user that web search returned no results."
+ )
tried.append(f"{free_run.provider}({_provider_status(free_run)})")
_log_done(
query=query,
diff --git a/jiuwenswarm/agents/harness/common/tools/web_search/tool.py b/jiuwenswarm/agents/harness/common/tools/web_search/tool.py
index 7043c08b99..1622585a02 100644
--- a/jiuwenswarm/agents/harness/common/tools/web_search/tool.py
+++ b/jiuwenswarm/agents/harness/common/tools/web_search/tool.py
@@ -51,7 +51,6 @@ def _has_enabled_free_engines() -> bool:
def _build_web_search_description() -> str:
- providers = _configured_providers_summary()
has_free = _has_enabled_free_engines()
if has_free:
return (
@@ -59,22 +58,24 @@ def _build_web_search_description() -> str:
"search_mode=default(默认)先付费后免费;"
"search_mode=paid 仅付费,失败报错;"
"search_mode=free 仅免费且不调用付费。"
- f"search_source 可选,指定付费源名称(如 {providers}),"
"配合 search_mode=paid 使用时优先使用指定源,不可用时返回明确错误。"
"max_results 可选,限制单个 query 的最大返回条数。"
"若用户指定使用某搜索引擎(如 bing、duckduckgo),请在 query 开头包含该引擎名称(如 'bing 今天的天气'),"
"以便系统识别并在引擎不可用时向用户说明。"
+ "default 模式已尝试所有可用搜索源(付费与免费),任何失败(all sources exhausted 或 no results)都不要再单独用 search_mode=paid 或 free 重试,"
+ "也不要换 query 重试,直接告知用户搜索失败。"
)
else:
return (
"网页搜索统一入口。"
"search_mode=default(默认)使用付费源;"
"search_mode=paid 仅付费,失败报错。"
- f"search_source 可选,指定付费源名称(如 {providers}),"
"配合 search_mode=paid 使用时优先使用指定源,不可用时返回明确错误。"
"max_results 可选,限制单个 query 的最大返回条数。"
"若用户指定使用某搜索引擎(如 bing、duckduckgo),请在 query 开头包含该引擎名称(如 'bing 今天的天气'),"
"以便系统识别并在引擎不可用时向用户说明。"
+ "default 模式已尝试所有可用搜索源(付费),任何失败(all sources exhausted 或 no results)都不要再单独用 search_mode=paid 重试,"
+ "也不要换 query 重试,直接告知用户搜索失败。"
)
@@ -146,7 +147,6 @@ def _import_harness_tool_metadata():
def _fallback_web_search_input_params(language: str) -> dict[str, Any]:
- providers = _configured_providers_summary()
has_free = _has_enabled_free_engines()
search_mode_desc = "default | paid | free" if has_free else "default | paid"
try:
@@ -162,11 +162,6 @@ def _fallback_web_search_input_params(language: str) -> dict[str, Any]:
"description": search_mode_desc,
"default": "default",
}
- props["search_source"] = {
- "type": "string",
- "description": f"指定付费源名称:{providers}。配合 search_mode=paid 使用。",
- "default": None,
- }
if "max_results" in props:
props["max_results"]["description"] = (
"Optional result count; defaults to web_search.max_results in config."
@@ -186,7 +181,6 @@ def _fallback_web_search_input_params(language: str) -> dict[str, Any]:
"default": "default",
"description": search_mode_desc,
},
- "search_source": {"type": "string", "description": f"指定付费源:{providers}"},
"max_results": {"type": "integer", "description": "Optional."},
},
"required": ["query"],
diff --git a/tests/unit_tests/agents/harness/test_web_search.py b/tests/unit_tests/agents/harness/test_web_search.py
index 96e476b6b8..0027c1c598 100644
--- a/tests/unit_tests/agents/harness/test_web_search.py
+++ b/tests/unit_tests/agents/harness/test_web_search.py
@@ -90,11 +90,41 @@ def test_run_web_search_paid_unavailable(monkeypatch):
"jiuwenswarm.agents.harness.common.tools.web_search.providers.any_paid_provider_available",
lambda _order: False,
)
+ monkeypatch.setattr(
+ "jiuwenswarm.agents.harness.common.tools.web_search.orchestrator._free_search_engines",
+ lambda: [("duckduckgo", object)],
+ )
+
+ import asyncio
+
+ result = asyncio.run(run_web_search("test", search_mode="paid"))
+ assert result.startswith("[ERROR]: paid search unavailable")
+ assert "Use search_mode=free instead" in result
+ assert "petal" not in result
+ assert "tavily" not in result
+ assert "bocha" not in result
+ assert "availability" not in result
+ assert "search_source" not in result
+
+
+def test_run_web_search_paid_unavailable_no_free(monkeypatch):
+ monkeypatch.setattr(
+ "jiuwenswarm.agents.harness.common.tools.web_search.providers.any_paid_provider_available",
+ lambda _order: False,
+ )
+ monkeypatch.setattr(
+ "jiuwenswarm.agents.harness.common.tools.web_search.orchestrator._free_search_engines",
+ lambda: [],
+ )
import asyncio
result = asyncio.run(run_web_search("test", search_mode="paid"))
- assert result == "[ERROR]: paid search unavailable."
+ assert result.startswith("[ERROR]: paid search unavailable")
+ assert "No search source is available" in result
+ assert "Use search_mode=free" not in result
+ assert "petal" not in result
+ assert "search_source" not in result
def test_run_web_search_free_does_not_call_paid(monkeypatch):
@@ -484,11 +514,9 @@ def test_build_web_search_description_contains_configured_providers(monkeypatch)
from jiuwenswarm.agents.harness.common.tools.web_search.tool import _build_web_search_description
desc = _build_web_search_description()
- assert "petal, bocha" in desc
- assert "tavily" not in desc
- assert "serper" not in desc
- assert "jina" not in desc
- assert "perplexity" not in desc
+ for provider in ("petal", "bocha", "tavily", "serper", "jina", "perplexity"):
+ assert provider not in desc
+ assert "search_source" not in desc
def test_build_web_search_description_hides_free_mode_when_no_free_engines(monkeypatch):