wire.py 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. """GitHub Copilot wire quirks: per-model api_mode, editor/initiator/vision headers, max-tokens param.
  2. Fills the generic ProviderProfile wire seams (``providers/base.py`` L1 block). Every hook here is
  3. pure and answers from the arguments alone, except ``api_mode_for_model`` with an ``api_key``, which
  4. may read the account's cached live catalog to normalize the model id first.
  5. """
  6. from __future__ import annotations
  7. import re
  8. from typing import Any, Optional
  9. from .helpers import base_url_host_matches, base_url_hostname
  10. # Copilot's own API host family (individual, business and enterprise subdomains).
  11. _COPILOT_HOST = "githubcopilot.com"
  12. # Hosts whose first call of a user turn carries ``x-initiator: user`` (Hermes core's set). Every other host,
  13. # including the plan hosts the token exchange hands out (api.individual./api.business./api.enterprise.),
  14. # keeps the client default ``agent`` by design; see README § Premium requests.
  15. _INITIATOR_HOSTS = ("api.githubcopilot.com", "models.github.ai")
  16. def is_copilot_host(base_url: str) -> bool:
  17. return base_url_host_matches(str(base_url or ""), _COPILOT_HOST)
  18. def requires_responses_api(model_id: str) -> bool:
  19. """opencode's ``shouldUseCopilotResponsesApi``: GPT-5+ uses the Responses API except
  20. ``gpt-5-mini``; non-GPT models (Claude, Gemini, ...) use Chat Completions."""
  21. match = re.match(r"^gpt-(\d+)", model_id)
  22. return bool(match) and int(match.group(1)) >= 5 and not model_id.startswith("gpt-5-mini")
  23. def model_api_mode(
  24. model_id: Optional[str], *, catalog: Optional[list[dict[str, Any]]] = None,
  25. api_key: Optional[str] = None) -> str:
  26. """API mode for a Copilot model from the id pattern (opencode's approach). Copilot's Claude models
  27. go through its OpenAI-compatible chat endpoint, not the native Anthropic adapter: the catalog may
  28. advertise /v1/messages but the Copilot token/header scheme lives in the OpenAI client path."""
  29. from .catalog import fetch_github_model_catalog, normalize_copilot_model_id
  30. if catalog is None and api_key: # fetch once so normalize + endpoint check share it
  31. catalog = fetch_github_model_catalog(api_key=api_key)
  32. normalized = normalize_copilot_model_id(model_id, catalog=catalog, api_key=api_key)
  33. if normalized and requires_responses_api(normalized):
  34. return "codex_responses"
  35. return "chat_completions"
  36. def editor_headers(*, is_vision: bool = False) -> dict[str, str]:
  37. from .credentials import copilot_request_headers
  38. return copilot_request_headers(is_agent_turn=True, is_vision=is_vision)
  39. class CopilotWireMixin:
  40. """ProviderProfile wire hooks for the Copilot API."""
  41. def api_mode_for_model(self, model: str, *, api_key: str | None = None) -> str | None:
  42. if api_key is None:
  43. return "codex_responses" if requires_responses_api(model) else "chat_completions"
  44. return model_api_mode(model, api_key=api_key)
  45. def client_headers(
  46. self, *, base_url: str, api_key: str = "", has_images: bool = False,
  47. ) -> dict[str, str] | None:
  48. return editor_headers(is_vision=has_images) if is_copilot_host(base_url) else None
  49. def request_headers(self, *, base_url: str, model: str = "", user_initiated: bool = False) -> dict[str, str]:
  50. # First call of a user turn says ``user``; follow-ups keep the client default ``x-initiator: agent``.
  51. if user_initiated and any(base_url_host_matches(base_url, h) for h in _INITIATOR_HOSTS):
  52. return {"x-initiator": "user"}
  53. return {}
  54. def max_tokens_param(self, *, base_url: str, model: str = "") -> str | None:
  55. host = base_url_hostname(str(base_url or "")) or ""
  56. return "max_completion_tokens" if host.endswith("." + _COPILOT_HOST) else None