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(flash): web_flash 支持显式搜索引擎选择

qwqssqwq7b70bdc0f7cb
diff --git a/jiuwenswarm/agents/harness/flash/tools/web_flash.py b/jiuwenswarm/agents/harness/flash/tools/web_flash.py
index bd48c60ec0..c9f003cc49 100644
--- a/jiuwenswarm/agents/harness/flash/tools/web_flash.py
+++ b/jiuwenswarm/agents/harness/flash/tools/web_flash.py
@@ -13,6 +13,9 @@ from jiuwenswarm.agents.harness.common.tools.harness_named_web_tools import (
from jiuwenswarm.agents.harness.common.tools.web_search.harness import (
JiuwenHarnessWebSearchTool,
)
+from jiuwenswarm.agents.harness.common.tools.web_search.constants import (
+ KNOWN_PAID_PROVIDERS,
+)
def _description(language: str) -> str:
@@ -25,18 +28,33 @@ def _description(language: str) -> str:
return (
"Flash 模式联网工具。只填一个操作对象:search 返回 URL 和摘要;"
"fetch 抓取一个或多个网页的正文。通常先搜索,再抓取相关结果页。"
+ "用户明确指定搜索引擎时,请在 search 中填写 search_source。"
)
def _input_params(language: str) -> dict[str, Any]:
if language == "en":
- search_desc = "Search the web. The system selects the available provider."
+ search_desc = (
+ "Search the web. Set search_source when the user explicitly requests "
+ "a specific search engine; otherwise omit it for automatic selection."
+ )
query_desc = "Search query."
+ search_source_desc = (
+ "Optional explicit search engine, e.g. bocha, petal, tavily. "
+ "Omit when the user does not request a specific engine."
+ )
fetch_desc = "Fetch full text from one or more webpage URLs."
urls_desc = "One or more webpage URLs."
else:
- search_desc = "联网搜索;可用搜索服务由系统自动选择。"
+ search_desc = (
+ "联网搜索;用户明确指定搜索引擎时填写 search_source,"
+ "未指定时省略并由系统自动选择。"
+ )
query_desc = "搜索查询。查询当前信息时应使用当前年份或日期。"
+ search_source_desc = (
+ "可选。用户明确指定的搜索引擎,如 bocha、petal、tavily;"
+ "未指定时省略,由系统自动选择。"
+ )
fetch_desc = "抓取一个或多个网页的正文。"
urls_desc = "一个或多个网页 URL。"
@@ -47,7 +65,16 @@ def _input_params(language: str) -> dict[str, Any]:
"type": "object",
"description": search_desc,
"properties": {
- "query": {"type": "string", "minLength": 1, "description": query_desc},
+ "query": {
+ "type": "string",
+ "minLength": 1,
+ "description": query_desc,
+ },
+ "search_source": {
+ "type": "string",
+ "enum": sorted(KNOWN_PAID_PROVIDERS),
+ "description": search_source_desc,
+ },
"max_results": {
"type": "integer",
"minimum": 1,
@@ -119,9 +146,7 @@ def build_web_flash_tool(
async def _invoke(**kwargs: Any) -> Any:
actions = [
- name
- for name in ("search", "fetch")
- if isinstance(kwargs.get(name), dict)
+ name for name in ("search", "fetch") if isinstance(kwargs.get(name), dict)
]
if len(actions) != 1:
raise ValueError("fill exactly one web action object: search or fetch")
diff --git a/tests/unit_tests/agentserver/test_flash_merged_web_tool.py b/tests/unit_tests/agentserver/test_flash_merged_web_tool.py
index c6a7027a41..1065b68cf8 100644
--- a/tests/unit_tests/agentserver/test_flash_merged_web_tool.py
+++ b/tests/unit_tests/agentserver/test_flash_merged_web_tool.py
@@ -22,6 +22,7 @@ def test_web_flash_schema_requires_exactly_one_action() -> None:
validate = Draft202012Validator(schema).validate
validate({"search": {"query": "current news"}})
+ validate({"search": {"query": "current news", "search_source": "bocha"}})
validate({"fetch": {"url": ["https://example.com"]}})
invalid = [
@@ -31,6 +32,7 @@ def test_web_flash_schema_requires_exactly_one_action() -> None:
{"fetch": {"url": []}},
{"fetch": {"url": "https://example.com"}},
{"search": {"query": "q", "provider": "paid"}},
+ {"search": {"query": "q", "search_source": "not-a-provider"}},
]
for inputs in invalid:
with pytest.raises(ValidationError):
@@ -63,6 +65,97 @@ async def test_web_flash_dispatches_to_mainline_backends(monkeypatch) -> None:
assert [name for name, _inputs in calls] == ["search", "fetch"]
+async def test_web_flash_passes_explicit_search_source_to_mainline_backend(
+ monkeypatch,
+) -> None:
+ calls: list[dict] = []
+
+ class _SearchBackend:
+ async def invoke(self, inputs: dict):
+ calls.append(inputs)
+ return {"ok": True}
+
+ monkeypatch.setattr(
+ web_flash,
+ "JiuwenHarnessWebSearchTool",
+ lambda **_kwargs: _SearchBackend(),
+ )
+ monkeypatch.setattr(
+ web_flash,
+ "JiuwenHarnessFetchWebpageTool",
+ lambda **_kwargs: _ChildTool("fetch", []),
+ )
+ tool = web_flash.build_web_flash_tool(agent_id="agent")
+
+ await tool.invoke(
+ {
+ "search": {
+ "query": "华为技术有限公司 工商登记信息",
+ "search_source": "bocha",
+ }
+ }
+ )
+
+ assert calls == [
+ {
+ "query": "华为技术有限公司 工商登记信息",
+ "search_source": "bocha",
+ }
+ ]
+
+
+async def test_web_flash_explicit_search_source_reaches_web_search_impl(
+ monkeypatch,
+) -> None:
+ calls: list[dict] = []
+
+ async def fake_run(
+ query,
+ *,
+ search_mode="default",
+ search_source=None,
+ max_results=None,
+ cache=None,
+ ):
+ calls.append(
+ {
+ "query": query,
+ "search_mode": search_mode,
+ "search_source": search_source,
+ "max_results": max_results,
+ }
+ )
+ return "ok"
+
+ monkeypatch.setattr(
+ "jiuwenswarm.agents.harness.common.tools.web_search.tool.run_web_search",
+ fake_run,
+ )
+ tool = web_flash.build_web_flash_tool(agent_id="agent", language="cn")
+
+ result = await tool.invoke(
+ {
+ "search": {
+ "query": "华为技术有限公司 工商登记信息",
+ "search_source": "bocha",
+ "max_results": 10,
+ }
+ }
+ )
+
+ assert result == "ok"
+ assert calls == [
+ {
+ "query": "华为技术有限公司 工商登记信息",
+ "search_mode": "default",
+ "search_source": "bocha",
+ "max_results": 10,
+ }
+ ]
+
+
@pytest.mark.asyncio
async def test_web_flash_runtime_rejects_multiple_actions(monkeypatch) -> None:
calls: list[tuple[str, dict]] = []