__init__.py 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. """GitHub Copilot ACP provider profile.
  2. copilot-acp does not speak OpenAI-over-HTTP: it drives an external ACP subprocess over
  3. stdio, so the profile supplies its own client via :meth:`ProviderProfile.create_client`.
  4. Registered by the plugin's top-level ``__init__`` next to the ``copilot`` profile.
  5. """
  6. from typing import Any
  7. from providers.base import ProviderProfile
  8. from .auth_rows import AUTH_ROW_FIELDS
  9. from .catalog import acp_picker_catalog, clear_session_memo
  10. from .picker import PICKER_FIELDS
  11. from .setup import SETUP_FIELDS
  12. class CopilotACPProfile(ProviderProfile):
  13. """GitHub Copilot ACP — external process, no REST models endpoint."""
  14. def external_auth_evidence(self) -> tuple[bool, str | None] | None:
  15. """Sign-in evidence without spawning the CLI: a supported env token only; the CLI's own token
  16. files are never read (rules owned by the sibling ``copilot`` credentials module)."""
  17. from ..copilot.credentials import copilot_cli_auth_evidence
  18. return copilot_cli_auth_evidence()
  19. def create_client(self, **client_kwargs: Any) -> Any:
  20. """Build the ACP stdio shim rather than an HTTP client."""
  21. from .client import CopilotACPClient
  22. return CopilotACPClient(**client_kwargs)
  23. def fetch_models(
  24. self, *, api_key: str | None = None, base_url: str | None = None, timeout: float = 15.0
  25. ) -> list[str] | None:
  26. """Enabled models advertised by a short-lived signed-in ACP session (``session/new``).
  27. The CLI may keep its login in an OS credential store with no token Hermes can reuse, so
  28. the session is the only source that reflects the account's enablement. ``api_key`` /
  29. ``base_url`` are ignored: the subprocess owns auth. None when the CLI is missing, refuses
  30. ``--acp``, or the probe fails/times out — callers fall back to their next source.
  31. """
  32. from hermes_cli.auth import resolve_external_process_provider_credentials
  33. try:
  34. creds = resolve_external_process_provider_credentials(self.name)
  35. if not str(creds.get("base_url") or "").startswith("acp://"):
  36. return None
  37. client = self.create_client(
  38. api_key=creds.get("api_key"), base_url=creds.get("base_url"),
  39. command=creds.get("command"), args=creds.get("args"))
  40. return client.list_models(timeout_seconds=timeout) or None
  41. except Exception:
  42. # Missing CLI (AuthError), refused --acp / failed spawn (RuntimeError), probe
  43. # timeout — the base fetch_models contract is "None if the fetch failed".
  44. return None
  45. def live_context_length(self, model: str, *, api_key: str = "", base_url: str = "") -> int | None:
  46. """Same account catalog as the HTTP ``copilot`` provider (lane L4 seam)."""
  47. from providers import get_provider_profile
  48. copilot = get_provider_profile("copilot")
  49. return copilot.live_context_length(model, api_key=api_key, base_url=base_url) if copilot else None
  50. def invalidate_catalog_cache(self) -> None:
  51. clear_session_memo()
  52. def normalize_model_id(self, model: str, *, api_key: str = "") -> str | None:
  53. """Copilot id space; the ACP marker key never reaches the HTTP catalog."""
  54. from providers import get_provider_profile
  55. copilot = get_provider_profile("copilot")
  56. return copilot.normalize_model_id(model) if copilot else None
  57. copilot_acp = CopilotACPProfile(
  58. name="copilot-acp", aliases=("github-copilot-acp", "copilot-acp-agent"),
  59. api_mode="chat_completions", # ACP subprocess uses chat_completions routing
  60. env_vars=(), # Managed by ACP subprocess
  61. base_url="acp://copilot", # ACP internal scheme
  62. auth_type="external_process",
  63. # How to launch the CLI; env var names predate this profile (formerly hardcoded in
  64. # hermes_cli/auth.py), so existing setups keep working.
  65. process_command="copilot",
  66. process_args=("--acp", "--stdio"),
  67. process_command_env_vars=("HERMES_COPILOT_ACP_COMMAND", "COPILOT_CLI_PATH"),
  68. process_args_env_var="HERMES_COPILOT_ACP_ARGS",
  69. picker_catalog=acp_picker_catalog, static_models=("copilot-acp",),
  70. **SETUP_FIELDS, **PICKER_FIELDS, **AUTH_ROW_FIELDS,
  71. )