provider.py 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. """OpenAI native web search — declares the Responses API server-side ``web_search`` built-in.
  2. Config: ``web.search_backend: openai-native`` (or ``web.backend``).
  3. Auth: openai-codex OAuth (``hermes auth add openai-codex``); no API key of its own.
  4. Unlike every other provider here, this one never executes a search itself. Selecting it
  5. tells the Codex Responses transport to declare the provider-executed ``web_search`` tool
  6. (``{"type": "web_search"}``) in place of the client-side ``web_search`` function, so the
  7. model drives search server-side. The transport performs that swap; this class exists so
  8. ``web.search_backend`` has a real provider name to point at.
  9. Auth gating lives here rather than in the transport because the transport must stay
  10. reachable for a user who configured this backend but has not signed in yet — they get the
  11. clear "sign in" error from :meth:`search` rather than a silently different backend.
  12. """
  13. from __future__ import annotations
  14. import json
  15. from typing import Any, Dict
  16. from plugins.web._common import BaseWebSearchProvider, search_fail
  17. _UNSUPPORTED_MSG = (
  18. "openai-native declares OpenAI's server-side web_search tool; it cannot run as a "
  19. "client-side search and requires the Codex Responses transport (provider "
  20. "openai-codex). For client-side search use firecrawl (default) or another backend."
  21. )
  22. def _dget(obj: Any, key: str) -> Any:
  23. return obj.get(key) if isinstance(obj, dict) else None
  24. def has_codex_credentials() -> bool:
  25. """Cheap probe: True when openai-codex OAuth tokens are *likely* usable.
  26. Mirrors ``tools/xai_http.has_xai_credentials`` — deliberately avoids
  27. ``resolve_codex_runtime_credentials`` (disk locks, OAuth network refresh), because
  28. this runs on every ``hermes tools`` repaint. Checks, fast-to-slow:
  29. ``providers.openai-codex.tokens.access_token`` in ``auth.json``, then any
  30. ``credential_pool.openai-codex`` entry carrying an ``access_token`` (pool-only
  31. multi-account grants never write the providers singleton). Returns False on any
  32. exception so a corrupted auth store cannot block other availability scans.
  33. """
  34. try:
  35. from hermes_constants import get_hermes_home
  36. auth_path = get_hermes_home() / "auth.json"
  37. if not auth_path.exists():
  38. return False
  39. store = json.loads(auth_path.read_text(encoding="utf-8-sig"))
  40. tokens = _dget(_dget(_dget(store, "providers"), "openai-codex"), "tokens")
  41. if str(_dget(tokens, "access_token") or "").strip():
  42. return True
  43. entries = _dget(_dget(store, "credential_pool"), "openai-codex")
  44. return isinstance(entries, list) and any(
  45. isinstance(e, dict) and str(e.get("access_token", "") or "").strip() for e in entries
  46. )
  47. except Exception:
  48. return False
  49. class OpenAINativeWebSearchProvider(BaseWebSearchProvider):
  50. """Marker provider: the Codex Responses transport swaps the client ``web_search``
  51. function for the server-executed built-in when this backend is active."""
  52. NAME = "openai-native"
  53. DISPLAY_NAME = "OpenAI Native Web Search (Codex Responses)"
  54. def is_available(self) -> bool:
  55. return has_codex_credentials()
  56. def search(self, query: str, limit: int = 5) -> dict[str, Any]:
  57. """Never called on a successful native turn — the transport replaces the tool
  58. before the request goes out. Reached only when the active transport cannot host
  59. the built-in, so fail loudly instead of returning an empty result set."""
  60. return search_fail(_UNSUPPORTED_MSG)
  61. def get_setup_schema(self) -> dict[str, Any]:
  62. from plugins.web._common import setup_schema
  63. return setup_schema(
  64. self.DISPLAY_NAME,
  65. "native",
  66. "Search runs on the provider side (needs the Codex Responses transport + an openai-codex login); search only, extraction still uses another backend",
  67. "",
  68. )