xai.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. """xAI Grok OAuth upstream adapter."""
  2. from __future__ import annotations
  3. import logging
  4. import threading
  5. from typing import FrozenSet, Optional
  6. from agent.credential_pool import CredentialPool, PooledCredential, load_pool
  7. from hermes_cli.auth import DEFAULT_XAI_OAUTH_BASE_URL
  8. from hermes_cli.proxy.adapters.base import UpstreamAdapter, UpstreamCredential
  9. logger = logging.getLogger(__name__)
  10. _POOL_PROVIDER = "xai-oauth"
  11. # OpenAI-compatible endpoints; ``/responses`` because the native xAI runtime uses codex_responses.
  12. _ALLOWED_PATHS: frozenset[str] = frozenset(
  13. {"/responses", "/chat/completions", "/completions", "/embeddings", "/models"}
  14. )
  15. class XAIGrokAdapter(UpstreamAdapter):
  16. """Proxy upstream for xAI Grok via Hermes-managed OAuth credentials."""
  17. auth_hint = "hermes auth add xai-oauth --type oauth"
  18. def __init__(self) -> None:
  19. self._lock = threading.Lock()
  20. self._pool: Optional[CredentialPool] = None
  21. @property
  22. def name(self) -> str:
  23. return "xai"
  24. @property
  25. def display_name(self) -> str:
  26. return "xAI Grok OAuth"
  27. @property
  28. def allowed_paths(self) -> frozenset[str]:
  29. return _ALLOWED_PATHS
  30. def is_authenticated(self) -> bool:
  31. pool = self._load_pool()
  32. return bool(pool and pool.has_available())
  33. def get_credential(self) -> UpstreamCredential:
  34. with self._lock:
  35. pool = self._load_pool()
  36. if pool is None or not pool.has_credentials():
  37. raise RuntimeError(
  38. "No xAI OAuth credentials found. Run `hermes auth add xai-oauth --type oauth` first."
  39. )
  40. entry = pool.select()
  41. if entry is None:
  42. raise RuntimeError(
  43. "No available xAI OAuth credentials found. Run "
  44. "`hermes auth reset xai-oauth` or re-authenticate with `hermes auth add xai-oauth --type oauth`."
  45. )
  46. self._pool = pool
  47. return self._credential_from_entry(entry)
  48. def get_retry_credential(
  49. self, *, failed_credential: UpstreamCredential, status_code: int
  50. ) -> Optional[UpstreamCredential]:
  51. if status_code not in {401, 429}:
  52. return None
  53. with self._lock:
  54. pool = self._pool or self._load_pool()
  55. if pool is None:
  56. return None
  57. # 401: refresh the current key first. 429: never refresh — cooldown the rate-limited
  58. # key and rotate. None when the pool has nothing else → the status flows to the client.
  59. refreshed = pool.try_refresh_current() if status_code == 401 else None
  60. if refreshed is None:
  61. refreshed = pool.mark_exhausted_and_rotate(status_code=status_code)
  62. if refreshed is None:
  63. return None
  64. retry_cred = self._credential_from_entry(refreshed)
  65. if retry_cred.bearer == failed_credential.bearer:
  66. return None
  67. logger.info("proxy: xAI upstream returned %s; retrying with rotated pool credential", status_code)
  68. return retry_cred
  69. def _load_pool(self) -> Optional[CredentialPool]:
  70. try:
  71. return load_pool(_POOL_PROVIDER)
  72. except Exception as exc:
  73. logger.warning("proxy: failed to load xAI OAuth credential pool: %s", exc)
  74. return None
  75. def _credential_from_entry(self, entry: PooledCredential) -> UpstreamCredential:
  76. bearer = str(getattr(entry, "runtime_api_key", None) or entry.access_token or "").strip()
  77. if not bearer:
  78. raise RuntimeError(
  79. "xAI OAuth credential pool entry did not contain an access token. "
  80. "Re-authenticate with `hermes auth add xai-oauth --type oauth`."
  81. )
  82. base_url = str(
  83. getattr(entry, "runtime_base_url", None) or entry.base_url or DEFAULT_XAI_OAUTH_BASE_URL
  84. ).strip().rstrip("/")
  85. return UpstreamCredential(
  86. bearer=bearer, base_url=base_url or DEFAULT_XAI_OAUTH_BASE_URL, expires_at=entry.expires_at
  87. )
  88. __all__ = ["XAIGrokAdapter"]