xai_http.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. """Shared helpers for direct xAI HTTP integrations."""
  2. from __future__ import annotations
  3. import datetime
  4. import json
  5. import os
  6. import uuid
  7. from typing import Any, Dict, Optional
  8. DEFAULT_XAI_BASE_URL = "https://api.x.ai/v1"
  9. MAX_XAI_STORAGE_EXPIRES_AFTER_SECONDS = 30 * 24 * 60 * 60
  10. SAFE_XAI_STORAGE_EXPIRES_AFTER_SECONDS = 2 * 24 * 60 * 60
  11. def _dict_get(obj: Any, key: str) -> Any:
  12. return obj.get(key) if isinstance(obj, dict) else None
  13. def has_xai_credentials() -> bool:
  14. """Cheap probe: True when xAI credentials are *likely* usable.
  15. Deliberately avoids :func:`resolve_xai_http_credentials` (disk locks, OAuth network
  16. refresh) — hot-paint callers and ``WebSearchProvider.is_available()`` must not do network
  17. I/O. Checks, fast-to-slow: ``XAI_API_KEY``; ``providers.xai-oauth.tokens.access_token`` in
  18. ``auth.json``; any ``credential_pool.xai-oauth`` entry with an ``access_token`` (pool-only
  19. multi-account grants never write the providers singleton). Returns False on any exception
  20. so a corrupted auth store can't block other availability scans.
  21. """
  22. from agent.secret_scope import get_secret
  23. if (get_secret("XAI_API_KEY", "") or "").strip():
  24. return True
  25. try:
  26. from hermes_constants import get_hermes_home
  27. auth_path = get_hermes_home() / "auth.json"
  28. if not auth_path.exists():
  29. return False
  30. store = json.loads(auth_path.read_text(encoding="utf-8-sig"))
  31. tokens = _dict_get(_dict_get(_dict_get(store, "providers"), "xai-oauth"), "tokens")
  32. if str(_dict_get(tokens, "access_token") or "").strip():
  33. return True
  34. entries = _dict_get(_dict_get(store, "credential_pool"), "xai-oauth")
  35. return isinstance(entries, list) and any(
  36. isinstance(e, dict) and str(e.get("access_token", "") or "").strip() for e in entries
  37. )
  38. except Exception:
  39. return False
  40. def hermes_xai_user_agent() -> str:
  41. """Return a stable Hermes-specific User-Agent for xAI HTTP calls."""
  42. try:
  43. from hermes_cli.version_info import get_version_info
  44. return f"Hermes-Agent/{get_version_info().base_version}"
  45. except Exception:
  46. return "Hermes-Agent/unknown"
  47. def hermes_xai_default_headers() -> dict[str, str]:
  48. """Default headers for OpenAI-SDK and raw HTTP clients talking to xAI (replaces the SDK User-Agent)."""
  49. return {"User-Agent": hermes_xai_user_agent()}
  50. _TRUE_WORDS = {"1", "true", "yes", "on", "enabled"}
  51. _FALSE_WORDS = {"0", "false", "no", "off", "disabled"}
  52. def _coerce_bool(value: Any, default: bool) -> bool:
  53. if isinstance(value, bool):
  54. return value
  55. if isinstance(value, str) and (normalized := value.strip().lower()) in _TRUE_WORDS | _FALSE_WORDS:
  56. return normalized in _TRUE_WORDS
  57. return default
  58. def _coerce_expires_after(value: Any) -> Optional[int]:
  59. """Normalize an xAI storage TTL: int seconds, or None for permanent storage (omit on the wire)."""
  60. if value is None:
  61. return None
  62. if isinstance(value, str):
  63. value = value.strip().lower()
  64. if value in {"", "default", "none", "null", "never", "permanent", "forever", "0"}:
  65. return None
  66. try:
  67. value = int(value)
  68. except ValueError:
  69. return SAFE_XAI_STORAGE_EXPIRES_AFTER_SECONDS
  70. if not isinstance(value, (int, float)):
  71. return SAFE_XAI_STORAGE_EXPIRES_AFTER_SECONDS
  72. return None if int(value) <= 0 else min(int(value), MAX_XAI_STORAGE_EXPIRES_AFTER_SECONDS)
  73. def read_xai_imagine_storage_config(section_name: str) -> dict[str, Any]:
  74. """Read ``<section_name>.xai.storage`` (``image_gen``/``video_gen``) -> {enabled, public_url, expires_after}.
  75. On by default so xAI returns permanent public URLs, not short-lived CDN ones; null TTL = permanent."""
  76. try:
  77. from hermes_cli.config import load_config
  78. storage = _dict_get(_dict_get(_dict_get(load_config(), section_name), "xai"), "storage")
  79. except Exception:
  80. storage = None
  81. storage = storage if isinstance(storage, dict) else {}
  82. return {
  83. "enabled": _coerce_bool(storage.get("enabled"), True),
  84. "public_url": _coerce_bool(storage.get("public_url"), True),
  85. "expires_after": _coerce_expires_after(storage.get("expires_after")),
  86. }
  87. def build_xai_storage_options(
  88. section_name: str, *, filename_prefix: str, extension: str,
  89. ) -> Optional[dict[str, Any]]:
  90. """Return an xAI ``storage_options`` payload, or None when disabled."""
  91. cfg = read_xai_imagine_storage_config(section_name)
  92. if not cfg["enabled"]:
  93. return None
  94. ts = datetime.datetime.now(datetime.UTC).strftime("%Y%m%d-%H%M%S")
  95. filename = f"{filename_prefix}-{ts}-{uuid.uuid4().hex[:8]}.{extension.lstrip('.') or 'bin'}"
  96. payload: dict[str, Any] = {"filename": filename, "public_url": bool(cfg["public_url"])}
  97. if cfg["expires_after"] is not None:
  98. payload["expires_after"] = cfg["expires_after"]
  99. return payload
  100. def xai_storage_notice_text(section_name: str) -> str:
  101. """User-facing notice for first xAI Imagine storage use."""
  102. cfg = read_xai_imagine_storage_config(section_name)
  103. if not cfg["enabled"]:
  104. return ""
  105. retention = "without an automatic expiry"
  106. if cfg["expires_after"] is not None:
  107. days = cfg["expires_after"] / (24 * 60 * 60)
  108. retention = f"for about {days:g} day{'s' if days != 1 else ''}"
  109. return (
  110. "xAI Imagine storage is enabled so generated media gets a reusable "
  111. f"public URL {retention}. xAI may bill for stored files and public URL "
  112. f"hosting. Disable this with `{section_name}.xai.storage.enabled: false` "
  113. "or set `expires_after` to change the retention."
  114. )
  115. def maybe_mark_xai_storage_notice_seen(section_name: str) -> Optional[str]:
  116. """Return the storage notice once per Hermes home, then mark it seen."""
  117. notice = xai_storage_notice_text(section_name)
  118. if not notice:
  119. return None
  120. try:
  121. from hermes_constants import get_hermes_home
  122. marker_dir = get_hermes_home() / "state"
  123. marker_dir.mkdir(parents=True, exist_ok=True)
  124. marker = marker_dir / f"{section_name}_xai_storage_notice_seen"
  125. if marker.exists():
  126. return None
  127. marker.write_text(datetime.datetime.now(datetime.UTC).isoformat() + "\n", encoding="utf-8")
  128. return notice
  129. except Exception:
  130. return notice
  131. def _resolve_explicit_xai_api_key() -> str:
  132. """Read ``XAI_API_KEY`` via ``resolve_provider_secret`` (config → profile scope → env/.env → pool).
  133. Both the preferred-key and the no-OAuth fallback paths go through here so scope policy
  134. (incl. failing closed in a multiplexed gateway turn) is never re-implemented per caller.
  135. """
  136. from tools.tool_backend_helpers import resolve_provider_secret
  137. return resolve_provider_secret("XAI_API_KEY", "xai")
  138. def _xai_base_url_override() -> str:
  139. """``HERMES_XAI_BASE_URL`` then ``XAI_BASE_URL``, stripped; '' when unset."""
  140. from hermes_cli.config import get_env_value
  141. return str(get_env_value("HERMES_XAI_BASE_URL") or get_env_value("XAI_BASE_URL") or "").strip().rstrip("/")
  142. def resolve_xai_http_credentials(
  143. *, force_refresh: bool = False, api_key_hint: Optional[str] = None, prefer_api_key: bool = False,
  144. ) -> dict[str, str]:
  145. """Resolve bearer credentials for direct xAI HTTP endpoints.
  146. Default order: Hermes-managed xAI OAuth, then ``XAI_API_KEY`` (via ``get_env_value`` so
  147. ``~/.hermes/.env`` keys count). ``prefer_api_key=True`` inverts that for API-metered
  148. endpoints where the subscription OAuth bearer authorizes but misbehaves (x_search answers
  149. without citations, TTS 403s). Both branches honor ``HERMES_XAI_BASE_URL``/``XAI_BASE_URL``
  150. behind the same origin-pinning validation. ``force_refresh=True`` forces an OAuth refresh;
  151. pass the rejected bearer as ``api_key_hint`` so a multi-account pool refreshes the issuing
  152. entry, not whichever its strategy selects first.
  153. Prefers Hermes-managed xAI OAuth credentials when available, then falls back to ``XAI_API_KEY`` resolved
  154. via ``hermes_cli.config.get_env_value`` so keys stored in ``~/.hermes/.env`` (the standard Hermes
  155. location) are honored — not just ones already exported into ``os.environ``. This keeps direct xAI
  156. endpoints (images, TTS, STT, etc.) aligned with the main runtime auth model and preserves the regression
  157. contract from PR #17140 / #17163.
  158. The key is read through :func:`tools.tool_backend_helpers.resolve_provider_secret` so profile secret
  159. scoping is identical to the fallback branch, and the base URL honors ``HERMES_XAI_BASE_URL`` /
  160. ``XAI_BASE_URL`` behind the same origin-pinning validation as the OAuth branch. See #87045, #88040.
  161. """
  162. import hermes_cli.auth as auth_mod
  163. if prefer_api_key and (explicit_key := str(_resolve_explicit_xai_api_key() or "").strip()):
  164. # Origin-pinned so a tampered env override can't exfiltrate the bearer; rejection -> default URL.
  165. override = _xai_base_url_override()
  166. base_url = auth_mod._xai_validate_inference_base_url(override, fallback=DEFAULT_XAI_BASE_URL)
  167. return {"provider": "xai", "api_key": explicit_key, "base_url": base_url}
  168. try:
  169. from agent.credential_pool import load_pool
  170. pool = load_pool("xai-oauth")
  171. entry = pool.try_refresh_matching(api_key_hint) if force_refresh else pool.select()
  172. if force_refresh and entry is None:
  173. # A rejected refresh may quarantine the issuing entry; continue with
  174. # the next healthy account rather than resurrecting the stale row.
  175. entry = pool.select()
  176. access_token = getattr(entry, "runtime_api_key", None) or getattr(entry, "access_token", "")
  177. fallback_base_url = str(
  178. getattr(entry, "runtime_base_url", None)
  179. or getattr(entry, "base_url", "")
  180. or auth_mod.DEFAULT_XAI_OAUTH_BASE_URL
  181. ).strip().rstrip("/")
  182. base_url = auth_mod._xai_validate_inference_base_url(_xai_base_url_override(), fallback=fallback_base_url)
  183. if str(access_token).strip():
  184. return {"provider": "xai-oauth", "api_key": str(access_token).strip(), "base_url": base_url}
  185. except Exception:
  186. pass
  187. from hermes_cli.config import get_env_value
  188. api_key = _resolve_explicit_xai_api_key()
  189. base_url = str(get_env_value("XAI_BASE_URL") or DEFAULT_XAI_BASE_URL).strip().rstrip("/")
  190. return {"provider": "xai", "api_key": api_key, "base_url": base_url}