catalog.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. """Copilot catalog and model metadata: hosts, models.dev id, the live ``/models`` catalog (picker,
  2. context windows, id normalization, reasoning efforts) and the curated fallback list.
  3. Fills the generic ProviderProfile catalog seams (``providers/base.py`` L4 + I2-cat blocks).
  4. """
  5. from __future__ import annotations
  6. import copy
  7. import logging
  8. import time
  9. from typing import Any, Optional
  10. from .helpers import ASTRA_EFFORTS, base_url_host_matches, clamp_effort, get_json, is_astra_model
  11. logger = logging.getLogger(__name__)
  12. # Copilot business/enterprise accounts are served from ``api.<plan>.githubcopilot.com``; GitHub Models
  13. # (models.github.ai) speaks the same wire (Responses quirks, reasoning payloads, x-initiator billing).
  14. # Per-site differences (carriers, editor headers: githubcopilot.com only) live in the hooks, keyed on base_url.
  15. HOSTNAMES = ("githubcopilot.com", "models.github.ai")
  16. # The ~8K-capped GitHub Models free tier shares the Copilot catalog only (model metadata lookups).
  17. METADATA_HOSTNAMES = ("models.inference.ai.azure.com",)
  18. MODELS_DEV_ID = "github-copilot"
  19. COPILOT_BASE_URL = "https://api.githubcopilot.com"
  20. COPILOT_MODELS_URL = f"{COPILOT_BASE_URL}/models"
  21. # Hosts (and subdomains) whose ``/models`` probe is answered from the Copilot catalog.
  22. _CATALOG_HOSTS = HOSTNAMES + METADATA_HOSTNAMES
  23. REASONING_EFFORTS_GPT5 = ["minimal", "low", "medium", "high"]
  24. REASONING_EFFORTS_O_SERIES = ["low", "medium", "high"]
  25. # Curated list (core's static catalog row): served when the live catalog is unreachable.
  26. STATIC_MODELS = (
  27. "gpt-5.4", "gpt-5.4-mini", "gpt-5-mini", "gpt-5.3-codex", "gpt-5.2-codex", "gpt-4.1", "gpt-4o", "gpt-4o-mini",
  28. "claude-sonnet-4.6", "claude-sonnet-5", "claude-sonnet-4", "claude-sonnet-4.5", "claude-haiku-4.5",
  29. "gemini-3.1-pro-preview", "gemini-3-pro-preview", "gemini-3-flash-preview", "gemini-2.5-pro",
  30. )
  31. # OpenRouter-style ids -> Copilot ids. Dash-notation Claude ids are accepted too: Hermes' default
  32. # Claude IDs use hyphens (Anthropic native) but Copilot's API only accepts dot-notation, so a
  33. # copilot + hyphenated default would otherwise hit HTTP 400 "model_not_supported".
  34. MODEL_ALIASES: dict[str, str] = dict((
  35. ("openai/gpt-5", "gpt-5-mini"), ("openai/gpt-5-chat", "gpt-5-mini"), ("openai/gpt-5-mini", "gpt-5-mini"),
  36. ("openai/gpt-5-nano", "gpt-5-mini"), ("openai/gpt-4.1", "gpt-4.1"), ("openai/gpt-4.1-mini", "gpt-4.1"),
  37. ("openai/gpt-4.1-nano", "gpt-4.1"), ("openai/gpt-4o", "gpt-4o"), ("openai/gpt-4o-mini", "gpt-4o-mini"),
  38. ("openai/o1", "gpt-5.2"), ("openai/o1-mini", "gpt-5-mini"), ("openai/o1-preview", "gpt-5.2"),
  39. ("openai/o3", "gpt-5.3-codex"), ("openai/o3-mini", "gpt-5-mini"), ("openai/o4-mini", "gpt-5-mini"),
  40. ("anthropic/claude-opus-4.6", "claude-opus-4.6"), ("anthropic/claude-sonnet-5", "claude-sonnet-5"),
  41. ("anthropic/claude-sonnet-4.6", "claude-sonnet-4.6"), ("anthropic/claude-sonnet-4", "claude-sonnet-4"),
  42. ("anthropic/claude-sonnet-4.5", "claude-sonnet-4.5"), ("anthropic/claude-haiku-4.5", "claude-haiku-4.5"),
  43. ("claude-sonnet-5", "claude-sonnet-5"), ("claude-opus-4-6", "claude-opus-4.6"),
  44. ("claude-sonnet-4-6", "claude-sonnet-4.6"), ("claude-sonnet-4-0", "claude-sonnet-4"),
  45. ("claude-sonnet-4-5", "claude-sonnet-4.5"), ("claude-haiku-4-5", "claude-haiku-4.5"),
  46. ("anthropic/claude-opus-4-6", "claude-opus-4.6"), ("anthropic/claude-sonnet-4-6", "claude-sonnet-4.6"),
  47. ("anthropic/claude-sonnet-4-0", "claude-sonnet-4"), ("anthropic/claude-sonnet-4-5", "claude-sonnet-4.5"),
  48. ("anthropic/claude-haiku-4-5", "claude-haiku-4.5"),
  49. ))
  50. # ─── catalog credential (best effort) ───
  51. def _first_exchangeable_copilot_token(raw_tokens) -> str:
  52. """Exchange stored GitHub tokens in order; the first that validates AND exchanges wins (every
  53. entry is tried so a later valid token survives an earlier malformed one)."""
  54. from .credentials import exchange_copilot_token, validate_copilot_token
  55. for raw in raw_tokens:
  56. raw = str(raw or "").strip()
  57. if not raw or not validate_copilot_token(raw)[0]:
  58. continue
  59. try:
  60. api_token = exchange_copilot_token(raw)[0] # (api_token, expires_at, base_url)
  61. except Exception:
  62. continue
  63. if api_token:
  64. return api_token
  65. return ""
  66. def _resolve_copilot_catalog_api_key() -> str:
  67. """Best-effort Copilot API token for the catalog: the exchanged env token (COPILOT_GITHUB_TOKEN,
  68. GH_TOKEN, GITHUB_TOKEN), then ``auth.json`` ``credential_pool.copilot[]`` (a ``hermes auth add
  69. copilot`` login). Without the pool, keyless users see the picker fall back to the stale curated
  70. list on a silent 401."""
  71. def _env_token() -> str:
  72. from .credentials import get_copilot_api_token, resolve_copilot_token
  73. raw, _source = resolve_copilot_token()
  74. return get_copilot_api_token(raw)[0] if raw else ""
  75. def _pool_token() -> str:
  76. from agent.credential_pool import read_credential_pool
  77. return _first_exchangeable_copilot_token(
  78. entry.get("access_token") for entry in read_credential_pool("copilot") if isinstance(entry, dict))
  79. sources = (_env_token, _pool_token)
  80. for source in sources:
  81. try:
  82. token = source()
  83. except Exception:
  84. continue
  85. if token:
  86. return token
  87. return ""
  88. # ─── live /models catalog ───
  89. def copilot_default_headers(*, is_agent_turn: bool = True) -> dict[str, str]:
  90. """Standard headers for Copilot API requests."""
  91. from .credentials import copilot_request_headers
  92. return copilot_request_headers(is_agent_turn=is_agent_turn)
  93. _COPILOT_CHAT_ENDPOINTS = {"/chat/completions", "/responses", "/v1/messages"}
  94. def _payload_items(payload: Any) -> list[dict[str, Any]]:
  95. data = payload.get("data", []) if isinstance(payload, dict) else payload
  96. return [item for item in data if isinstance(item, dict)] if isinstance(data, list) else []
  97. def _copilot_catalog_item_is_text_model(
  98. item: dict[str, Any], *, ignore_picker_flag: bool = False) -> bool:
  99. if not str(item.get("id") or "").strip():
  100. return False
  101. if not ignore_picker_flag and item.get("model_picker_enabled") is False:
  102. return False
  103. capabilities = item.get("capabilities")
  104. if isinstance(capabilities, dict):
  105. model_type = str(capabilities.get("type") or "").strip().lower()
  106. if model_type and model_type != "chat":
  107. return False
  108. supported_endpoints = item.get("supported_endpoints")
  109. if isinstance(supported_endpoints, list):
  110. endpoints = {e for endpoint in supported_endpoints if (e := str(endpoint).strip())}
  111. if endpoints and not endpoints & _COPILOT_CHAT_ENDPOINTS:
  112. return False
  113. return True
  114. def _copilot_text_models(items: list[dict[str, Any]], *, ignore_picker_flag: bool = False) -> list[dict[str, Any]]:
  115. """Chat-capable catalog rows, deduped by id, in catalog order."""
  116. models: list[dict[str, Any]] = []
  117. seen_ids: set[str] = set()
  118. for item in items:
  119. model_id = str(item.get("id") or "").strip()
  120. if model_id in seen_ids:
  121. continue
  122. if not _copilot_catalog_item_is_text_model(item, ignore_picker_flag=ignore_picker_flag):
  123. continue
  124. seen_ids.add(model_id)
  125. models.append(item)
  126. return models
  127. # Short-TTL cache of the filtered GitHub Copilot /models catalog (picker + context/normalize helpers
  128. # share it). Keyed by the api_key of the successful fetch so a credential swap never serves the
  129. # previous account's catalog; monotonic clock; lock-free (a race at worst duplicates one fetch).
  130. _github_model_catalog_cache: Optional[list[dict[str, Any]]] = None
  131. _github_model_catalog_cache_key: Optional[str] = None
  132. _github_model_catalog_cache_time: float = 0.0
  133. _GITHUB_MODEL_CATALOG_CACHE_TTL = 300 # 5 minutes
  134. def fetch_github_model_catalog(
  135. api_key: Optional[str] = None, timeout: float = 5.0) -> Optional[list[dict[str, Any]]]:
  136. """Fetch the live GitHub Copilot model catalog for this account."""
  137. global _github_model_catalog_cache, _github_model_catalog_cache_key
  138. global _github_model_catalog_cache_time
  139. if (
  140. _github_model_catalog_cache is not None
  141. and _github_model_catalog_cache_key == api_key
  142. and (time.monotonic() - _github_model_catalog_cache_time) < _GITHUB_MODEL_CATALOG_CACHE_TTL
  143. ):
  144. return copy.deepcopy(_github_model_catalog_cache) # deep: callers must not mutate cached dicts
  145. attempts: list[dict[str, str]] = []
  146. if api_key:
  147. attempts.append({**copilot_default_headers(), "Authorization": f"Bearer {api_key}"})
  148. attempts.append(copilot_default_headers())
  149. for headers in attempts:
  150. try:
  151. items = _payload_items(get_json(COPILOT_MODELS_URL, timeout=timeout, headers=headers))
  152. except Exception:
  153. continue
  154. models = _copilot_text_models(items)
  155. if not models and items:
  156. # GitHub has been observed returning ``model_picker_enabled: false`` for EVERY model on
  157. # some accounts, which would strand the picker on the stale curated fallback. The flag
  158. # is a display hint, not an availability contract — retry without it (chat/endpoint
  159. # checks still apply).
  160. models = _copilot_text_models(items, ignore_picker_flag=True)
  161. if models:
  162. _github_model_catalog_cache = copy.deepcopy(models)
  163. _github_model_catalog_cache_key = api_key
  164. _github_model_catalog_cache_time = time.monotonic()
  165. return models
  166. return None
  167. def _fetch_github_models(api_key: Optional[str] = None, timeout: float = 5.0) -> Optional[list[str]]:
  168. catalog = fetch_github_model_catalog(api_key=api_key, timeout=timeout)
  169. return [item.get("id", "") for item in catalog if item.get("id")] if catalog else None
  170. def copilot_picker_catalog(force_refresh: bool = False) -> Optional[list[str]]:
  171. """The account's live catalog ids, else None: core then serves ``STATIC_MODELS`` (the profile's
  172. ``static_models``) marked as a placeholder, never cached as the account's catalog."""
  173. from .activation import github_calls_allowed
  174. try:
  175. # Provider listings (``hermes status``, pickers, Desktop) warm every listed provider's
  176. # catalog; a home that never chose Copilot keeps the curated list and GitHub is not called.
  177. live = _fetch_github_models(_resolve_copilot_catalog_api_key()) if github_calls_allowed() else None
  178. if live:
  179. return live
  180. except Exception: # the picker degrades to the curated list on any catalog failure
  181. logger.debug("copilot live catalog fetch failed", exc_info=True)
  182. return None
  183. def _is_github_models_base_url(base_url: Optional[str]) -> bool:
  184. # Host match (never a URL prefix): the plan hosts api.<plan>.githubcopilot.com share the account's
  185. # catalog, and ``https://api.githubcopilot.com.example`` is not GitHub.
  186. return any(base_url_host_matches(str(base_url or "").strip(), host) for host in _CATALOG_HOSTS)
  187. # ─── context windows ───
  188. # Module-level cache: {model_id: max_prompt_tokens}
  189. _copilot_context_cache: dict[str, int] = {}
  190. _copilot_context_cache_time: float = 0.0
  191. _copilot_context_cache_key: Optional[str] = None # fingerprint of the api_key the entry was fetched with
  192. _COPILOT_CONTEXT_CACHE_TTL = 3600 # 1 hour
  193. def get_copilot_model_context(model_id: str, api_key: Optional[str] = None) -> Optional[int]:
  194. """``max_prompt_tokens`` for a Copilot model from the live /models API (cached in-process 1h; a
  195. miss on a fresh cache does not re-fetch), or None."""
  196. global _copilot_context_cache, _copilot_context_cache_time, _copilot_context_cache_key
  197. # Keyed on the credential like fetch_github_model_catalog: the catalog (and its limits) is
  198. # per-account, so another profile's token must not be served this entry.
  199. from .credentials import _token_fingerprint
  200. key_fp = _token_fingerprint(api_key) if api_key else None
  201. if (_copilot_context_cache and _copilot_context_cache_key == key_fp
  202. and (time.time() - _copilot_context_cache_time < _COPILOT_CONTEXT_CACHE_TTL)):
  203. return _copilot_context_cache.get(model_id)
  204. catalog = fetch_github_model_catalog(api_key=api_key)
  205. if not catalog:
  206. return None
  207. cache: dict[str, int] = {}
  208. for item in catalog:
  209. mid = str(item.get("id") or "").strip()
  210. max_prompt = ((item.get("capabilities") or {}).get("limits") or {}).get("max_prompt_tokens")
  211. if mid and isinstance(max_prompt, int) and max_prompt > 0:
  212. cache[mid] = max_prompt
  213. _copilot_context_cache = cache
  214. _copilot_context_cache_time = time.time()
  215. _copilot_context_cache_key = key_fp
  216. return cache.get(model_id)
  217. # ─── id normalization and reasoning efforts ───
  218. def _copilot_catalog_ids(
  219. catalog: Optional[list[dict[str, Any]]] = None, api_key: Optional[str] = None) -> set[str]:
  220. if catalog is None and api_key:
  221. catalog = fetch_github_model_catalog(api_key=api_key)
  222. return {mid for item in (catalog or []) if (mid := str(item.get("id") or "").strip())}
  223. def normalize_copilot_model_id(
  224. model_id: Optional[str], *, catalog: Optional[list[dict[str, Any]]] = None,
  225. api_key: Optional[str] = None) -> str:
  226. raw = str(model_id or "").strip()
  227. if not raw:
  228. return ""
  229. catalog_ids = _copilot_catalog_ids(catalog=catalog, api_key=api_key)
  230. alias = MODEL_ALIASES.get(raw)
  231. if alias:
  232. return alias
  233. candidates = [raw]
  234. if "/" in raw:
  235. candidates.append(raw.split("/", 1)[1].strip())
  236. if raw.endswith(("-mini", "-nano", "-chat")):
  237. candidates.append(raw[:-5])
  238. seen: set[str] = set()
  239. for candidate in candidates:
  240. if not candidate or candidate in seen:
  241. continue
  242. seen.add(candidate)
  243. if candidate in MODEL_ALIASES:
  244. return MODEL_ALIASES[candidate]
  245. if candidate in catalog_ids:
  246. return candidate
  247. if "/" in raw:
  248. stripped = raw.split("/", 1)[1].strip()
  249. # Enterprise BYOK custom models expose ``owner/sub/model`` ids (two
  250. # slashes). A strip guess that still contains "/" cannot be a Copilot
  251. # id, so pass the input through untouched instead of corrupting it.
  252. if stripped and "/" not in stripped:
  253. return stripped
  254. return raw
  255. def clamp_github_reasoning_effort(effort: Any, supported: list[str]) -> str:
  256. """Copilot/GitHub Models effort for a non-empty *supported* list: the level itself when listed,
  257. else the nearest WEAKER listed level; bespoke names the ladder can't place fall to ``medium``
  258. (or the first listed level)."""
  259. effort = str(effort or "medium").strip().lower()
  260. if effort not in supported:
  261. effort = clamp_effort(effort, supported)
  262. if effort not in supported:
  263. effort = "medium" if "medium" in supported else supported[0]
  264. return effort
  265. def _github_reasoning_efforts_for_model_id(model_id: str) -> list[str]:
  266. raw = (model_id or "").strip().lower()
  267. if raw.startswith(("openai/o1", "openai/o3", "openai/o4", "o1", "o3", "o4")):
  268. return list(REASONING_EFFORTS_O_SERIES)
  269. normalized = normalize_copilot_model_id(model_id).lower()
  270. if is_astra_model(normalized):
  271. return list(ASTRA_EFFORTS)
  272. if normalized.startswith("gpt-5"):
  273. return list(REASONING_EFFORTS_GPT5)
  274. return []
  275. def github_model_reasoning_efforts(
  276. model_id: Optional[str], *, catalog: Optional[list[dict[str, Any]]] = None,
  277. api_key: Optional[str] = None) -> list[str]:
  278. """Return supported reasoning-effort levels for a Copilot-visible model."""
  279. normalized = normalize_copilot_model_id(model_id, catalog=catalog, api_key=api_key)
  280. if not normalized:
  281. return []
  282. if catalog is None and api_key:
  283. catalog = fetch_github_model_catalog(api_key=api_key)
  284. catalog_entry = next((item for item in catalog if item.get("id") == normalized), None) if catalog else None
  285. if catalog_entry is not None:
  286. capabilities = catalog_entry.get("capabilities")
  287. if isinstance(capabilities, dict):
  288. # Structured catalog: the advertised list is authoritative (empty when absent).
  289. supports = capabilities.get("supports")
  290. efforts = supports.get("reasoning_effort") if isinstance(supports, dict) else None
  291. if not isinstance(efforts, list):
  292. return []
  293. return list(dict.fromkeys(e for effort in efforts if (e := str(effort).strip().lower())))
  294. # Legacy list-shaped capabilities: only a "reasoning" tag unlocks the pattern defaults.
  295. if "reasoning" not in {str(c).strip().lower() for c in catalog_entry.get("capabilities", [])}:
  296. return []
  297. return _github_reasoning_efforts_for_model_id(str(model_id or normalized))
  298. # ProviderProfile kwargs filling the I2-cat catalog seams.
  299. CATALOG_FIELDS = dict(picker_catalog=copilot_picker_catalog, static_models=STATIC_MODELS)
  300. class CopilotCatalogMixin:
  301. """Catalog/metadata hooks for the Copilot profiles."""
  302. def live_context_length(self, model: str, *, api_key: str = "", base_url: str = "") -> int | None:
  303. return get_copilot_model_context(model, api_key=api_key)
  304. def normalize_model_id(self, model: str, *, api_key: str = "") -> str | None:
  305. return normalize_copilot_model_id(model, api_key=api_key or None)
  306. def probe_models(
  307. self, *, base_url: str, api_key: str | None = None, timeout: float = 5.0,
  308. ) -> tuple[list[str] | None, str, str] | None:
  309. if not _is_github_models_base_url(base_url):
  310. return None
  311. return _fetch_github_models(api_key=api_key, timeout=timeout), COPILOT_MODELS_URL, COPILOT_BASE_URL