hermes_web.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. """System-owned web tools over Hermes's native providers and safety pipeline.
  2. Called under hermes_service's configuration lock. Bot homes never execute these
  3. operations; Yaovi stores the returned full text as a session-scoped tool result.
  4. """
  5. import json
  6. import asyncio
  7. import os
  8. import stat
  9. from pathlib import Path
  10. def _run(value):
  11. return asyncio.run(value) if asyncio.iscoroutine(value) else value
  12. def _system():
  13. if os.environ.get("YAOVI_HERMES_ROOT"):
  14. raise ValueError("搜索与网页配置仅支持系统层")
  15. def _extract_backend():
  16. from tools import web_tools
  17. from tools.tool_backend_helpers import read_web_capability_selection
  18. backend = web_tools._get_extract_backend()
  19. if read_web_capability_selection("extract") is None:
  20. # Native shared autodetection can choose search-only ddgs/Brave. For
  21. # an unpinned extract capability use Hermes's capability-aware registry.
  22. web_tools._ensure_web_plugins_loaded()
  23. from agent.web_search_registry import get_provider, get_active_extract_provider
  24. candidate = get_provider(backend)
  25. if candidate is not None and not candidate.supports_extract():
  26. candidate = get_active_extract_provider()
  27. if candidate is not None:
  28. backend = candidate.name
  29. return backend
  30. def config():
  31. _system()
  32. from hermes_cli.web_routers.tools import get_toolset_config
  33. from hermes_cli.config import read_raw_config
  34. result = _run(get_toolset_config("web"))
  35. web = (read_raw_config() or {}).get("web") or {}
  36. from tools.tool_backend_helpers import read_selection
  37. result["selection"] = {cap: web.get(f"{cap}_backend") or read_selection("web")
  38. for cap in ("search", "extract")}
  39. result["active_extract_backend"] = _extract_backend()
  40. return result
  41. def provider(args):
  42. _system()
  43. from hermes_cli.config import read_raw_config, save_config
  44. from hermes_cli.web_routers.tools import select_toolset_provider
  45. from hermes_cli.web_models import ToolsetProviderSelect
  46. capability = args["capability"]
  47. if capability not in ("search", "extract"):
  48. raise ValueError("未知网页工具能力")
  49. if args.get("provider") is None:
  50. # Restore this capability to native autodetection. A legacy shared pin
  51. # must not keep it pinned; preserve the other capability explicitly.
  52. raw = read_raw_config() or {}
  53. web = raw.setdefault("web", {})
  54. from tools.tool_backend_helpers import read_selection
  55. shared = read_selection("web")
  56. web.pop("backend", None)
  57. web.pop("use_gateway", None)
  58. other = "extract" if capability == "search" else "search"
  59. if shared and not web.get(f"{other}_backend"):
  60. web[f"{other}_backend"] = shared
  61. web.pop(f"{capability}_backend", None)
  62. save_config(raw)
  63. else:
  64. _run(select_toolset_provider("web", ToolsetProviderSelect(
  65. provider=args["provider"], capability=capability)))
  66. return config()
  67. def env(args):
  68. _system()
  69. from hermes_cli.web_routers.tools import save_toolset_env
  70. from hermes_cli.web_models import ToolsetEnvUpdate
  71. _run(save_toolset_env("web", ToolsetEnvUpdate(env=args["env"])))
  72. return config()
  73. def _public_result(result):
  74. # Vendor error envelopes are successful RPC replies; redact them too.
  75. from hermes_cli.config import load_env, OPTIONAL_ENV_VARS
  76. secrets = [v for k, v in (load_env() or {}).items()
  77. if len(v) >= 8 and OPTIONAL_ENV_VARS.get(k, {}).get("password", True)]
  78. def clean(value):
  79. if isinstance(value, str):
  80. for secret in secrets:
  81. value = value.replace(secret, "[已隐藏授权]")
  82. return value
  83. if isinstance(value, list):
  84. return [clean(v) for v in value]
  85. if isinstance(value, dict):
  86. return {k: clean(v) for k, v in value.items()}
  87. return value
  88. result = clean(result)
  89. failed = result.get("success") is False or bool(result.get("error")) or (
  90. "results" in result and not any(r.get("content") and not r.get("error") for r in result["results"]))
  91. if failed:
  92. result["isError"] = True
  93. result["configuration_hint"] = "请在我的设置 → 模型服务 → 搜索与网页中检查供应商、凭据和连接。"
  94. return result
  95. def search(args):
  96. _system()
  97. from tools.web_tools import web_search_tool, _get_search_backend
  98. result = json.loads(web_search_tool(args["query"], limit=args.get("limit", 5)))
  99. return _public_result({**result, "provider": _get_search_backend()})
  100. def extract(args):
  101. _system()
  102. from hermes_constants import get_hermes_dir
  103. from tools import web_tools_truncate as truncation
  104. from tools import web_tools
  105. created = {}
  106. backend = _extract_backend()
  107. original_backend = web_tools._get_extract_backend
  108. original = truncation._store_full_text
  109. def record(url, content):
  110. path = original(url, content)
  111. if path:
  112. created[url] = Path(path)
  113. return path
  114. # Record only paths generated by this native call, never parse a path from
  115. # provider text or accept one from the model. Restore even on failure.
  116. truncation._store_full_text = record
  117. web_tools._get_extract_backend = lambda: backend
  118. try:
  119. result = json.loads(_run(web_tools.web_extract_tool(args["urls"], char_limit=args.get("char_limit"))))
  120. finally:
  121. truncation._store_full_text = original
  122. web_tools._get_extract_backend = original_backend
  123. cache = get_hermes_dir("cache/web", "web_cache").resolve()
  124. for row in result.get("results", []):
  125. path = created.get(row.get("url"))
  126. if path is None:
  127. continue
  128. try:
  129. if path.parent.resolve() != cache:
  130. raise ValueError("网页缓存目录无效")
  131. fd = os.open(path, os.O_RDONLY | os.O_NOFOLLOW)
  132. with os.fdopen(fd, "r", encoding="utf-8") as stream:
  133. info = os.fstat(stream.fileno())
  134. if not stat.S_ISREG(info.st_mode) or info.st_size > 8 * 1024 * 1024:
  135. raise ValueError("网页缓存超过读取上限")
  136. row["content"] = stream.read(2 * 1024 * 1024)
  137. except (OSError, ValueError):
  138. # Do not expose the native read_file footer if storage/read failed.
  139. row["content"] = row.get("content", "").split("──────── [TRUNCATED]", 1)[0]
  140. row["note"] = "全文缓存无法读取,可提高 char_limit 重新提取。"
  141. return _public_result({**result, "provider": backend})
  142. def test(args):
  143. result = search({"query": args.get("query") or "Hermes web search", "limit": 1}) if args["capability"] == "search" else extract({"urls": [args.get("url") or "https://example.com"], "char_limit": 2000})
  144. return {"ok": not result.get("isError", False), "provider": result.get("provider"), "result": result}