setup.py 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. """GitHub Copilot ACP setup and Accounts-card surfaces: the ``hermes model`` flow and the
  2. Desktop card that points at the Copilot CLI's own login. Reached through ``SETUP_FIELDS``."""
  3. from __future__ import annotations
  4. import contextlib
  5. from typing import Any
  6. def _cli_status() -> tuple[dict[str, Any], Exception | None]:
  7. """Hermes' launch details for the Copilot CLI (command, base_url), or the reason it can't run."""
  8. from hermes_cli.auth import resolve_external_process_provider_credentials
  9. try:
  10. return resolve_external_process_provider_credentials("copilot-acp"), None
  11. except Exception as exc: # health: allow BLE001 -- missing CLI (AuthError) is a status, not a failure
  12. return {}, exc
  13. def account_status() -> dict[str, Any]:
  14. """Status for copilot-acp. ``logged_in`` only on positive evidence (a supported env token); the CLI
  15. keeps its own login, which Hermes does not read (catalog rule 11), so the unverified state reads
  16. "managed by the Copilot CLI" — never signed out."""
  17. from ..copilot.credentials import copilot_cli_auth_evidence
  18. creds, error = _cli_status()
  19. verified, auth_source = copilot_cli_auth_evidence()
  20. configured = error is None
  21. if verified:
  22. source_label = auth_source or "Copilot credentials detected"
  23. elif configured:
  24. source_label = f"Managed by the GitHub Copilot CLI ({creds.get('command') or 'copilot'})"
  25. else:
  26. source_label = "GitHub Copilot CLI not found on PATH"
  27. return {
  28. "logged_in": verified, "source": "copilot_cli", "source_label": source_label, "token_preview": None,
  29. "expires_at": None, "has_refresh_token": False, "configured": configured,
  30. }
  31. def model_flow_copilot_acp(config, current_model="", args=None):
  32. """GitHub Copilot ACP flow using the local Copilot CLI (a user-started Copilot flow: the picker may
  33. read the account catalog)."""
  34. from ..copilot.activation import copilot_user_flow
  35. del config, args
  36. with copilot_user_flow():
  37. _model_flow_copilot_acp(current_model)
  38. def _model_flow_copilot_acp(current_model):
  39. from hermes_cli.model_setup import persist_model_selection
  40. from ..copilot import setup as copilot_setup
  41. from ..copilot.helpers import say
  42. provider_id = "copilot-acp"
  43. creds, error = _cli_status()
  44. resolved_command = creds.get("command") or "copilot"
  45. effective_base = creds.get("base_url") or "acp://copilot"
  46. say(" GitHub Copilot ACP delegates Hermes turns to `copilot --acp`.",
  47. " Hermes currently starts its own ACP subprocess for each request.",
  48. " Hermes uses your selected model as a hint for the Copilot ACP session.",
  49. f" Command: {resolved_command}", f" Backend marker: {effective_base}", "")
  50. if error is not None:
  51. say(f" ⚠ {error}", " Set HERMES_COPILOT_ACP_COMMAND or COPILOT_CLI_PATH if Copilot CLI is installed elsewhere.")
  52. return
  53. catalog_api_key = ""
  54. with contextlib.suppress(Exception):
  55. catalog_api_key = copilot_setup.copilot_api_key()[0]
  56. _catalog, catalog_ids, _normalize = copilot_setup._copilot_catalog(catalog_api_key)
  57. selected = copilot_setup.pick_model_or_prompt(
  58. copilot_setup._copilot_model_list(catalog_ids), "Model name: ", current_model=_normalize(current_model),
  59. confirm_provider=provider_id, confirm_base_url=effective_base, confirm_api_key=catalog_api_key)
  60. if not selected:
  61. print("No change.")
  62. return
  63. selected = _normalize(selected)
  64. persist_model_selection(selected, provider_id, base_url=effective_base, api_mode="chat_completions")
  65. print(f"Default model set to: {selected} (via GitHub Copilot ACP)")
  66. SETUP_FIELDS: dict[str, Any] = {
  67. "model_setup_flow": model_flow_copilot_acp,
  68. # `copilot login` is the non-interactive subcommand; `copilot /login` is not valid
  69. # (slash-commands only exist inside an interactive session).
  70. "account_card": {"name": "GitHub Copilot (ACP)", "flow": "external", "cli_command": "copilot login",
  71. "docs_url": "https://docs.github.com/en/copilot"},
  72. "account_status": account_status,
  73. }