auth.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. """ACP auth helpers — detect and advertise Hermes authentication methods."""
  2. from __future__ import annotations
  3. from typing import Any, Optional
  4. TERMINAL_SETUP_AUTH_METHOD_ID = "hermes-setup"
  5. def detect_provider() -> Optional[str]:
  6. """Resolve the active Hermes runtime provider, or None if unavailable.
  7. A callable ``api_key`` (Azure Foundry Entra ID bearer-token provider, see
  8. :mod:`agent.azure_identity_adapter`) counts as a valid credential; otherwise
  9. Entra-configured Foundry deployments would default to ``"openrouter"`` and
  10. the ACP auth handshake would reject the legitimate provider."""
  11. try:
  12. from hermes_cli.runtime_provider import resolve_runtime_provider
  13. runtime = resolve_runtime_provider()
  14. api_key, provider = runtime.get("api_key"), runtime.get("provider")
  15. if isinstance(provider, str) and provider.strip() and (
  16. (isinstance(api_key, str) and api_key.strip()) or callable(api_key)):
  17. return provider.strip().lower()
  18. except Exception:
  19. pass
  20. return None
  21. def build_auth_methods() -> list[Any]:
  22. """Return registry-compatible ACP auth methods for Hermes.
  23. The ACP registry requires at least one usable auth method in the initial
  24. handshake. A fresh Zed install may have no Hermes credentials yet, so the
  25. terminal setup method is always advertised; when credentials resolve, the
  26. provider is also advertised as the default agent-managed runtime method."""
  27. from acp.schema import AuthMethodAgent, TerminalAuthMethod
  28. methods: list[Any] = []
  29. provider = detect_provider()
  30. if provider:
  31. methods.append(AuthMethodAgent(
  32. id=provider, name=f"{provider} runtime credentials",
  33. description=f"Authenticate Hermes using the currently configured {provider} runtime credentials.",
  34. ))
  35. methods.append(TerminalAuthMethod(
  36. id=TERMINAL_SETUP_AUTH_METHOD_ID, name="Configure Hermes provider", type="terminal", args=["--setup"],
  37. description=("Open Hermes' interactive model/provider setup in a terminal. "
  38. "Use this when Hermes has not been configured on this machine yet."),
  39. ))
  40. return methods