nous_portal.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. """Nous Portal upstream adapter."""
  2. from __future__ import annotations
  3. import logging
  4. import threading
  5. from typing import Any, Dict, FrozenSet, Optional
  6. from hermes_cli.anon_challenge import background_caller
  7. from hermes_cli.auth import (
  8. AuthError,
  9. DEFAULT_NOUS_INFERENCE_URL,
  10. _load_auth_store,
  11. _auth_store_lock,
  12. _is_terminal_nous_refresh_error,
  13. _nous_inference_env_override,
  14. _quarantine_nous_oauth_state,
  15. _quarantine_nous_pool_entries,
  16. _save_auth_store,
  17. _validate_nous_inference_url_from_network,
  18. _write_shared_nous_state,
  19. resolve_nous_runtime_credentials,
  20. )
  21. from hermes_cli.proxy.adapters.base import UpstreamAdapter, UpstreamCredential
  22. logger = logging.getLogger(__name__)
  23. # Endpoints inference-api.nousresearch.com actually serves; anything else is a 404 so stray
  24. # clients cannot leak odd requests upstream.
  25. _ALLOWED_PATHS: frozenset[str] = frozenset({"/chat/completions", "/completions", "/embeddings", "/models"})
  26. class NousPortalAdapter(UpstreamAdapter):
  27. """Proxy upstream for the Nous Portal inference API."""
  28. def __init__(self) -> None:
  29. # In-process serialization; cross-process refresh/persistence is resolve_nous_runtime_credentials().
  30. self._lock = threading.Lock()
  31. @property
  32. def name(self) -> str:
  33. return "nous"
  34. @property
  35. def display_name(self) -> str:
  36. return "Nous Portal"
  37. @property
  38. def allowed_paths(self) -> frozenset[str]:
  39. return _ALLOWED_PATHS
  40. def is_authenticated(self) -> bool:
  41. # Usable inference JWT, OR refresh_token + access_token to recover via the refresh helper.
  42. state = self._read_state() or {}
  43. return bool(state.get("agent_key") or (state.get("refresh_token") and state.get("access_token")))
  44. def get_credential(self) -> UpstreamCredential:
  45. return self._get_credential()
  46. def get_retry_credential(
  47. self, *, failed_credential: UpstreamCredential, status_code: int
  48. ) -> Optional[UpstreamCredential]:
  49. if status_code != 401:
  50. return None
  51. logger.info("proxy: Nous upstream rejected bearer; force-refreshing invoke JWT")
  52. return self._get_credential(force_refresh=True, stale_access_token=failed_credential.bearer)
  53. def _get_credential(
  54. self, *, force_refresh: bool = False, stale_access_token: Optional[str] = None
  55. ) -> UpstreamCredential:
  56. with self._lock:
  57. state = self._read_state()
  58. if state is None:
  59. raise RuntimeError("Not logged into Nous Portal. Run `hermes auth add nous` first.")
  60. try:
  61. # Every proxied request queues on self._lock: a free-tier browser challenge is
  62. # announced and raised, never waited on while holding it.
  63. with background_caller():
  64. refreshed = resolve_nous_runtime_credentials(
  65. force_refresh=force_refresh, stale_access_token=stale_access_token or None
  66. )
  67. except Exception as exc:
  68. if isinstance(exc, AuthError) and _is_terminal_nous_refresh_error(exc):
  69. _quarantine_nous_oauth_state(state, exc, reason="proxy_refresh_failure")
  70. self._save_state(state, quarantine_error=exc, quarantine_reason="proxy_refresh_failure")
  71. raise RuntimeError(f"Failed to refresh Nous Portal credentials: {exc}") from exc
  72. runtime_key = refreshed.get("api_key")
  73. if not runtime_key:
  74. raise RuntimeError(
  75. "Nous Portal refresh did not return a usable inference JWT. "
  76. "Try `hermes auth add nous` to re-authenticate."
  77. )
  78. # The returned base_url already honors the NOUS_INFERENCE_BASE_URL override (documented
  79. # dev/staging hatch); validating it against the prod allowlist would reject a legit
  80. # staging URL. So: env override wins, else network-validate the returned URL, else the
  81. # production default (defense-in-depth against a future source-layer bypass).
  82. base_url = (
  83. _nous_inference_env_override()
  84. or _validate_nous_inference_url_from_network(refreshed.get("base_url"))
  85. or DEFAULT_NOUS_INFERENCE_URL
  86. ).rstrip("/")
  87. return UpstreamCredential(bearer=runtime_key, base_url=base_url, expires_at=refreshed.get("expires_at"))
  88. # auth.json access — kept local so hermes_cli.auth's public surface does not grow.
  89. def _read_state(self) -> Optional[dict[str, Any]]:
  90. try:
  91. with _auth_store_lock():
  92. store = _load_auth_store()
  93. except Exception as exc:
  94. logger.warning("proxy: failed to load auth store: %s", exc)
  95. return None
  96. state = (store.get("providers") or {}).get("nous")
  97. return dict(state) if isinstance(state, dict) else None
  98. def _save_state(
  99. self,
  100. state: dict[str, Any],
  101. *,
  102. quarantine_error: Optional[AuthError] = None,
  103. quarantine_reason: Optional[str] = None,
  104. ) -> None:
  105. try:
  106. with _auth_store_lock():
  107. store = _load_auth_store()
  108. if quarantine_error is not None and quarantine_reason:
  109. _quarantine_nous_pool_entries(store, quarantine_error, reason=quarantine_reason)
  110. providers = store.setdefault("providers", {})
  111. providers["nous"] = state
  112. _save_auth_store(store)
  113. _write_shared_nous_state(state, renewal=True)
  114. except Exception as exc:
  115. logger.warning("proxy: failed to persist Nous quarantine state: %s", exc)
  116. __all__ = ["NousPortalAdapter"]