__init__.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. """Solstice provider profile: a user's own per-user-quota Gemini subscription, signed in with OAuth.
  2. Pre-release (``hidden=True``): no picker, setup list or accounts tab offers it until the user signs in
  3. by name (``hermes auth add solstice``); after that it lists like any signed-in provider. Login and
  4. token refresh go through the generic PKCE plugin flow with a Nous-portal broker as the token endpoint
  5. (``auth.py``); inference goes straight to Google on the per-user-quota methods (``transport.py``).
  6. """
  7. from __future__ import annotations
  8. from dataclasses import replace
  9. from typing import Any
  10. from hermes_cli.auth_oauth_pkce_plugin import OAuthPKCEConfig, pkce_auth_handler, pkce_refresh_credential
  11. from providers import register_provider
  12. from providers.base import ProviderProfile
  13. from .auth import broker_token_request, discover_client
  14. # Lives here, not in transport.py: discovery imports this module from interpreters without the app's
  15. # HTTP stack (PM's runtime reads config, which lists providers), so the transport loads on first use.
  16. INFERENCE_BASE_URL = "https://generativelanguage.googleapis.com/v1alpha"
  17. # Hardcoded because the per-user-quota endpoint has no model listing a user token may read (its
  18. # /models answers 403 ACCESS_TOKEN_SCOPE_INSUFFICIENT). Quota is per model, so lite stays last as the
  19. # reserve once the larger models are exhausted.
  20. FALLBACK_MODELS = ("gemini-3.1-pro", "gemini-4-argon", "gemini-3.8-flash", "gemini-3.5-flash-lite")
  21. # The broker owns client_id/scope (discovered per login); refresh needs neither, the broker adds them.
  22. _BASE_CFG = OAuthPKCEConfig(
  23. client_id="", authorize_url="", token_url="", label="Solstice",
  24. redirect_path="/gemini-auth/callback", token_request=broker_token_request,
  25. # A refresh token is issued only for offline access, and Google omits it on a repeat consent.
  26. extra_authorize_params={"access_type": "offline", "prompt": "consent"},
  27. )
  28. def _auth_handler(action: str, args: Any) -> bool:
  29. cfg = _BASE_CFG
  30. if action == "add":
  31. client = discover_client()
  32. cfg = replace(_BASE_CFG, client_id=client["client_id"], authorize_url=client["authorize_url"],
  33. scopes=tuple(client["scope"].split()))
  34. return pkce_auth_handler(cfg)(action, args)
  35. class SolsticeProfile(ProviderProfile):
  36. def create_client(self, **client_kwargs: Any) -> Any:
  37. from .transport import SolsticeClient
  38. allowed = {"api_key", "base_url", "default_headers", "timeout", "http_client"}
  39. return SolsticeClient(**{k: v for k, v in client_kwargs.items() if k in allowed})
  40. def get_max_tokens(self, model: str | None) -> int | None:
  41. return None # output caps are the backend's; a Hermes default would truncate long answers
  42. register_provider(SolsticeProfile(
  43. name="solstice", aliases=("solstice-oauth",), display_name="Solstice",
  44. description="Solstice (your own per-user quota, OAuth sign-in)",
  45. auth_type="oauth_external", base_url=INFERENCE_BASE_URL, hidden=True,
  46. supports_health_check=False, supports_model_listing=False, supports_vision=True,
  47. fallback_models=FALLBACK_MODELS, default_aux_model="gemini-flash-lite-latest",
  48. auth_handler=_auth_handler, refresh_credential=pkce_refresh_credential(_BASE_CFG),
  49. ))