__init__.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. """GitHub Copilot model providers: ``copilot`` (Copilot API) and ``copilot-acp`` (Copilot CLI over ACP).
  2. Hermes imports this file through provider discovery (``kind: model-provider``), never through
  3. ``register(ctx)``. Both profiles register here, once, and only on a core that no longer bundles
  4. Copilot and carries the generic provider seams the profiles fill (hermes-agent#144788). On an
  5. older core this plugin registers nothing and logs one warning: that core still serves Copilot
  6. itself, and shadowing its profiles with ones its call sites cannot drive would break them.
  7. """
  8. from __future__ import annotations
  9. import logging
  10. from pathlib import Path
  11. logger = logging.getLogger(__name__)
  12. PROVIDER_NAMES = ("copilot", "copilot-acp")
  13. # ProviderProfile members the profiles override or set. A core without them drives Copilot
  14. # through its own name-keyed branches, so these profiles would be half-wired there.
  15. _REQUIRED_SEAMS = (
  16. "api_mode_for_model", "client_headers", "request_headers", "max_tokens_param",
  17. "seed_pool_credentials", "refresh_runtime_credentials", "external_auth_evidence",
  18. "live_context_length", "replay_message_carriers", "start_device_login", "picker_catalog",
  19. )
  20. def _bundled_copilot_dirs() -> list[Path]:
  21. """Copilot plugin dirs the running core ships, found the way core's discovery finds them
  22. (``providers/__init__.py``: ``<core>/plugins/model-providers/<name>/__init__.py``). Anchored on
  23. the ``providers`` module actually imported, so an editable install of another checkout can't
  24. answer for it; a leftover dir holding only ``__pycache__`` does not count."""
  25. import providers
  26. root = Path(providers.__file__).resolve().parent.parent / "plugins" / "model-providers"
  27. return [root / name for name in PROVIDER_NAMES if (root / name / "__init__.py").is_file()]
  28. def inert_reason() -> str | None:
  29. """Why this core must not get these profiles, or None when it should."""
  30. from providers.base import ProviderProfile
  31. if bundled := _bundled_copilot_dirs():
  32. return f"this Hermes core still bundles GitHub Copilot ({bundled[0]})"
  33. if missing := [name for name in _REQUIRED_SEAMS if not hasattr(ProviderProfile, name)]:
  34. return f"this Hermes core lacks the provider seams the plugin needs ({', '.join(missing)})"
  35. return None
  36. def _register() -> bool:
  37. reason = inert_reason()
  38. if reason:
  39. logger.warning("github-copilot plugin is inactive: %s. Update Hermes; until then the core's "
  40. "own Copilot support is used.", reason)
  41. return False
  42. from providers import register_provider
  43. from .copilot import copilot
  44. from .copilot_acp import copilot_acp
  45. register_provider(copilot)
  46. register_provider(copilot_acp)
  47. return True
  48. # pytest imports this file bare (no parent package) while collecting tests/; registering then
  49. # would need the relative imports above, and the suite loads the package itself (tests/conftest.py).
  50. ACTIVE = _register() if __package__ else False