auxiliary_plugin_oauth.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. """Auxiliary client for OAuth-shaped model-provider plugins (``auth_type`` oauth_external / oauth_device_code).
  2. Built-in OAuth routes (nous, openai-codex, xai-oauth) have their own branches in ``auxiliary_client``.
  3. A plugin OAuth provider reaches this one: its credential is the pooled row ``hermes auth add <name>``
  4. wrote, and its transport comes from ``ProviderProfile.create_client``. ``CredentialPool.select()``
  5. rotates an expiring row before leasing it, so compression and titling never send a dead bearer.
  6. """
  7. from __future__ import annotations
  8. from typing import Any
  9. def _configured_endpoint(provider: str) -> str:
  10. """The endpoint the main runtime would use: ``model.base_url`` when ``model.provider`` is this
  11. provider (a relay / proxy override), else the registered profile's own."""
  12. from hermes_cli.auth import PROVIDER_REGISTRY
  13. from hermes_cli.runtime_provider import _config_base_url_for_provider, _get_model_config
  14. pconfig = PROVIDER_REGISTRY.get(provider)
  15. return _config_base_url_for_provider(_get_model_config(), provider) or (pconfig.inference_base_url if pconfig else "")
  16. def resolve_plugin_oauth_client(req: Any) -> tuple[Any, Any]:
  17. """``(client, model)`` for ``req.provider``, or ``(None, None)`` when signed out or transport-less.
  18. An explicit key from the main runtime wins, so aux shares the session's bearer.
  19. """
  20. from agent import auxiliary_client as aux
  21. provider = req.provider
  22. api_key = aux._normalize_api_key(req.explicit_api_key)
  23. entry = None
  24. if not api_key:
  25. _exists, entry = aux._select_pool_entry(provider)
  26. api_key = str(getattr(entry, "runtime_api_key", "") or "") if entry is not None else ""
  27. base_url = (req.explicit_base_url or str(getattr(entry, "runtime_base_url", "") or "")
  28. or _configured_endpoint(provider)).strip().rstrip("/")
  29. client = aux._api_key_profile_supplied_client(provider, api_key=api_key, base_url=base_url) if api_key else None
  30. if client is None:
  31. aux._log_once_debug(aux._LOGGED_UNSUPPORTED_OAUTH_KEYS, provider,
  32. "resolve_provider_client: OAuth provider %s has no signed-in credential or "
  33. "plugin transport, try 'auto'", provider)
  34. return None, None
  35. model = aux._normalize_resolved_model(req.model or aux._get_aux_model_for_provider(provider), provider)
  36. return aux._route_client(req, client, model)