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调用性能

lbyc13943bcb790
diff --git a/jiuwenswarm/agents/harness/common/tools/web_search/paid.py b/jiuwenswarm/agents/harness/common/tools/web_search/paid.py
index bca4856dee..2ec6216f75 100644
--- a/jiuwenswarm/agents/harness/common/tools/web_search/paid.py
+++ b/jiuwenswarm/agents/harness/common/tools/web_search/paid.py
@@ -66,7 +66,8 @@ def _petal_format_answer_from_records(records: list[dict[str, str]]) -> str:
for rec in records:
title = (rec.get("title") or "").strip()
url = (rec.get("url") or "").strip()
- summary = (rec.get("summary") or "").strip()
+ # 优先用完整 content;content 缺失时回退 summary(petal 的 summary 可能被截断)
+ summary = (rec.get("content") or "").strip() or (rec.get("summary") or "").strip()
if not title and not url and not summary:
continue
n += 1
@@ -75,7 +76,10 @@ def _petal_format_answer_from_records(records: list[dict[str, str]]) -> str:
if url:
lines.append(f" URL: {url}")
if summary:
- lines.append(f" Summary: {summary}")
+ label = "Content" if (rec.get("content") or "").strip() else "Summary"
+ if len(summary) > _PETAL_MAX_SUMMARY_LEN:
+ summary = summary[:_PETAL_MAX_SUMMARY_LEN] + " ...(body truncated)"
+ lines.append(f" {label}: {summary}")
return "\n".join(lines)
diff --git a/jiuwenswarm/agents/harness/common/tools/web_search/response.py b/jiuwenswarm/agents/harness/common/tools/web_search/response.py
index 8ef1b27b7c..6e3cb80717 100644
--- a/jiuwenswarm/agents/harness/common/tools/web_search/response.py
+++ b/jiuwenswarm/agents/harness/common/tools/web_search/response.py
@@ -4,6 +4,9 @@ from __future__ import annotations
from jiuwenswarm.agents.harness.common.tools.web_search.types import WebSearchRecord
+# 内联正文展示上限,避免超大网页正文撑爆模型上下文
+_MAX_DISPLAY_BODY = 6000
+
def _dedupe_records(records: list[WebSearchRecord]) -> list[WebSearchRecord]:
seen: set[str] = set()
@@ -30,14 +33,19 @@ def _format_records_block(
if not deduped and not include_answer.strip():
lines.append("(no structured results)")
return lines
- # 提示模型按需调用 fetch_webpage(正文已在内存缓存,命中快)
- lines.append("Hint: 仅展示摘要与 URL。如需正文,请调用 fetch_webpage 工具。")
+ # petal 已将完整正文(content)内联返回,模型通常无需再 fetch
+ lines.append("Hint: 结果已内联展示正文(Content);仅在正文缺失时才需 fetch_webpage。")
for idx, rec in enumerate(deduped, 1):
lines.append(f"{idx}. {rec.title}")
if rec.url:
lines.append(f" URL: {rec.url}")
- if rec.snippet:
- lines.append(f" Snippet: {rec.snippet}")
+ # 优先展示完整 content;content 缺失(非 petal 源)时回退 snippet(summary)
+ body = (rec.content or "").strip() or (rec.snippet or "").strip()
+ if body:
+ label = "Content" if (rec.content and rec.content.strip()) else "Snippet"
+ if len(body) > _MAX_DISPLAY_BODY:
+ body = body[:_MAX_DISPLAY_BODY] + " ...(body truncated)"
+ lines.append(f" {label}: {body}")
lines.append(f" Source: {rec.source}")
return lines
diff --git a/jiuwenswarm/common/mcp_config.py b/jiuwenswarm/common/mcp_config.py
index e4323857a2..3fbae2160a 100644
--- a/jiuwenswarm/common/mcp_config.py
+++ b/jiuwenswarm/common/mcp_config.py
@@ -596,7 +596,7 @@ def create_mcp_tool(config_str: str) -> McpServerConfig:
env = tool_config.get("env")
cwd = tool_config.get("cwd")
# timeout_s 兼容两种下发形状:顶层(config.yaml 风格)与嵌套 params.timeout_s
- # (relay buildMcpRequestFields 下发的 {"params": {"timeout_s": N}})。
+ # (前端下发的 {"params": {"timeout_s": N}})。
timeout_s = tool_config.get("timeout_s")
if timeout_s is None:
nested_params = tool_config.get("params")
@@ -609,7 +609,7 @@ def create_mcp_tool(config_str: str) -> McpServerConfig:
url = _pick_mcp_url(tool_config)
client_type = _normalize_mcp_client_type(tool_config.get("type"))
params = {}
- # 前端(Relay/officeAce)下发的单连接器调用超时(秒):透传到 params,
+ # 前端下发的单连接器调用超时(秒):透传到 params,
# 供 _run_mcp_worker 按 call_tool 超时使用;非法值忽略,回落到默认 300s。
if isinstance(timeout_s, (int, float)) and not isinstance(timeout_s, bool) and timeout_s > 0:
params["timeout_s"] = timeout_s
@@ -906,7 +906,7 @@ async def _run_mcp_worker(
"""
client_type = str(params.get("_mcp_client_type") or "").lower() or "stdio"
- # 单连接器调用超时:前端(Relay/officeAce)下发的 timeout_s 经 create_mcp_tool
+ # 单连接器调用超时:前端下发的 timeout_s 经 create_mcp_tool
# 透传进 params;未下发或非法时回落到 300s 默认值。
_timeout_raw = params.get("timeout_s")
call_timeout_s = (
@@ -1044,7 +1044,7 @@ async def _enter_remote_mcp_session(
"""sse/streamable-http transport:复用 openjiuwen 高层 client,长连接复用。
connect 时 ``Runner.callback_framework.trigger(TOOL_AUTH)`` 注入 auth_headers
- (relay 下发的 ``Authorization: Bearer xxx`` 经 HeaderQueryAuthStrategy 加到请求头)。
+ (前端下发的 ``Authorization: Bearer xxx`` 经 HeaderQueryAuthStrategy 加到请求头)。
把 disconnect 注册进 stack,使 worker 退出时统一关连接。
"""
client_cls = _remote_mcp_client_cls(client_type)
@@ -1585,7 +1585,7 @@ def extract_office_claw_mcp(params: Any) -> dict[str, Any] | None:
def extract_request_mcp_servers(params: Any) -> dict[str, dict[str, Any]] | None:
"""取 ``request_mcp_servers.mcpServers`` 的用户连接器 map。
- Relay 的 buildMcpRequestFields 把用户配置的连接器放这里,值仅含启动配置
+ 前端 的 buildMcpRequestFields 把用户配置的连接器放这里,值仅含启动配置
(stdio: {command,args,cwd,env?};remote: {type,url,auth_headers?}),无 tool schema,
需由 list_request_mcp_server_tools 发现。无该字段返回 None。
"""
@@ -1834,7 +1834,7 @@ async def _list_remote_mcp_connector_tools(
``SseClient`` / ``StreamableHttpClient`` 自带 owner-task/cancel-scope/超时/重连,
且 connect 时经 ``Runner.callback_framework.trigger(TOOL_AUTH)`` 注入 auth_headers
(由 ``auth_callback`` 的 ``HeaderQueryAuthStrategy`` 转 ``AuthHeaderAndQueryProvider``,
- 把 relay 下发的 ``Authorization: Bearer xxx`` 加到请求头)。import 链自动注册 handler。
+ 把 前端 下发的 ``Authorization: Bearer xxx`` 加到请求头)。import 链自动注册 handler。
"""
client_cls = _remote_mcp_client_cls(client_type)
if client_cls is None: