codex_models.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. """Codex model discovery from API, local cache, and config."""
  2. from __future__ import annotations
  3. import json
  4. import logging
  5. import os
  6. from pathlib import Path
  7. from typing import List, Optional
  8. from hermes_cli.route_identity import normalize_route_base_url
  9. logger = logging.getLogger(__name__)
  10. # Curated offline fallback (first-run, transient API failure). Only slugs the ChatGPT Codex
  11. # OAuth backend actually accepts: the public API's "-pro" variants and the retired
  12. # gpt-5.3-codex / gpt-5.2-codex / gpt-5.1-codex-max / gpt-5.1-codex-mini return HTTP 400 there
  13. # ("not supported when using Codex with a ChatGPT account"), so listing them leaked dead picker
  14. # choices (#52492). If OpenAI re-enables any, live discovery (_fetch_models_from_api) picks them
  15. # up automatically.
  16. DEFAULT_CODEX_MODELS: list[str] = [
  17. "gpt-6-sol",
  18. "gpt-6-luna",
  19. "gpt-5.6-sol",
  20. "gpt-5.6-terra",
  21. "gpt-5.6-luna",
  22. "gpt-5.5",
  23. "gpt-5.4-mini",
  24. "gpt-5.4",
  25. # Research preview exposed ONLY via the Codex OAuth backend for ChatGPT Pro subscribers —
  26. # not in the public API, so it stays out of the "openai" catalog in hermes_cli/models.py.
  27. # The backend reports ``supported_in_api: false`` for it; that flag describes API
  28. # availability, not Codex availability, so fetch/cache paths must not filter on it.
  29. "gpt-5.3-codex-spark"]
  30. # gpt-5.3-codex-spark is in research preview and is exposed *only* via the Codex CLI / OAuth backend
  31. # (chatgpt.com/backend-api/codex/models) for ChatGPT Pro subscribers. It is NOT available in the public
  32. # OpenAI API, so it intentionally stays out of the "openai" provider catalog in hermes_cli/models.py — only
  33. # the openai-codex (OAuth) provider surfaces it. The Codex backend reports ``supported_in_api: false`` for
  34. # this slug; that flag describes API availability, not Codex backend availability, so the fetch/cache code
  35. # paths below intentionally do not filter on it. PR #12994 removed this entry on the assumption it was
  36. # unsupported — that was wrong; restored here. Keep it in the curated fallback so Pro users still see Spark
  37. # in `/model` when live discovery is unavailable (offline first run, transient API failure).
  38. _FORWARD_COMPAT_TEMPLATE_MODELS: list[tuple[str, tuple[str, ...]]] = [
  39. ("gpt-6-sol", ("gpt-5.6-sol", "gpt-5.5")),
  40. ("gpt-6-luna", ("gpt-5.6-luna", "gpt-5.5")),
  41. ("gpt-5.6-sol", ("gpt-5.5", "gpt-5.4")),
  42. ("gpt-5.6-terra", ("gpt-5.5", "gpt-5.4")),
  43. ("gpt-5.6-luna", ("gpt-5.5", "gpt-5.4")),
  44. ("gpt-5.5", ("gpt-5.4", "gpt-5.4-mini")),
  45. # Spark surfaces whenever a compatible template is present; the backend (not Hermes)
  46. # gates real availability by ChatGPT Pro entitlement.
  47. ("gpt-5.3-codex-spark", ("gpt-5.4", "gpt-5.5"))]
  48. def _dedupe(model_ids) -> list[str]:
  49. """Order-preserving dedupe."""
  50. return list(dict.fromkeys(model_ids))
  51. def _add_forward_compat_models(model_ids: list[str]) -> list[str]:
  52. """Surface newer Codex slugs missing from live discovery when an older compatible template is
  53. present (Clawdbot-style synthetic forward-compat catalog)."""
  54. ordered = _dedupe(model_ids)
  55. seen = set(ordered)
  56. for synthetic_model, template_models in _FORWARD_COMPAT_TEMPLATE_MODELS:
  57. if synthetic_model not in seen and any(template in seen for template in template_models):
  58. ordered.append(synthetic_model)
  59. seen.add(synthetic_model)
  60. return ordered
  61. def _add_context_variants(model_ids: list[str]) -> list[str]:
  62. """Insert ``<slug>-900k`` large-context picker variants after eligible base slugs.
  63. Base slugs keep the cheaper advertised 272K limit; the variant opts into the large window.
  64. The suffix is Hermes-side only — stripped before the id hits the wire (agent/transports/codex.py,
  65. agent/auxiliary_client.py).
  66. """
  67. from agent.model_metadata import CODEX_CONTEXT_VARIANT_SUFFIX, has_codex_context_variant
  68. out: list[str] = []
  69. present = set(model_ids)
  70. for model_id in model_ids:
  71. out.append(model_id)
  72. variant = model_id + CODEX_CONTEXT_VARIANT_SUFFIX
  73. if variant in present or variant in out:
  74. continue
  75. if has_codex_context_variant(model_id):
  76. out.append(variant)
  77. return out
  78. def _finalize_codex_models(model_ids: list[str]) -> list[str]:
  79. """Forward-compat synthesis + large-context variant synthesis."""
  80. return _add_context_variants(_add_forward_compat_models(model_ids))
  81. def _drop_undiscovered_astra(model_ids: list[str]) -> list[str]:
  82. """Astra is account-gated: only the live account-scoped catalog may advertise it. A stale
  83. ``models_cache.json`` or a ``config.toml`` default is a compatibility hint, not entitlement."""
  84. from agent.reasoning_effort import is_astra_model
  85. return [model for model in model_ids if not is_astra_model(model)]
  86. def codex_catalog_credential_identity() -> str:
  87. """Identity of the credential live discovery would use right now, for the catalog cache key.
  88. Access/refresh tokens rotate in place while the account-scoped catalog stays authoritative for
  89. the same ChatGPT principal and route, so the key includes the principal and resolved base URL,
  90. not the rotating token. An expired token is its own state (per route): ``_codex_catalog`` serves
  91. the static fallback for it, and that fallback must not outlive the refresh under the healthy
  92. principal's key. Opaque non-JWT tokens
  93. fall back to the token itself (the caller hashes every part before anything is persisted).
  94. """
  95. from hermes_cli.auth import _codex_access_token_is_expiring, resolve_codex_runtime_credentials
  96. try:
  97. creds = resolve_codex_runtime_credentials(read_only=True)
  98. token = str(creds.get("api_key") or "")
  99. route = normalize_route_base_url(str(creds.get("base_url") or "").strip())
  100. except Exception: # AuthError (no/exhausted creds) or the pytest seat belt: no live catalog either way
  101. token = ""
  102. if not token:
  103. return "missing"
  104. if _codex_access_token_is_expiring(token, 0):
  105. return "expired\n" + route
  106. from agent.credential_pool import _codex_principal_identity
  107. principal = _codex_principal_identity(token)
  108. return ("/".join(principal) if principal else token) + "\n" + route
  109. def _ranked_slugs(entries: object) -> list[str]:
  110. """Visible slugs from a Codex catalog ``models`` list, sorted by (priority, slug), deduped.
  111. Does not filter on ``supported_in_api``: that flag describes the public OpenAI API, while the
  112. OAuth-backed Codex backend still accepts slugs marked false there (gpt-5.3-codex-spark).
  113. """
  114. sortable = []
  115. for item in entries:
  116. if not isinstance(item, dict):
  117. continue
  118. slug = item.get("slug")
  119. if not isinstance(slug, str) or not slug.strip():
  120. continue
  121. visibility = item.get("visibility")
  122. if isinstance(visibility, str) and visibility.strip().lower() in {"hide", "hidden"}:
  123. continue
  124. priority = item.get("priority")
  125. rank = int(priority) if isinstance(priority, (int, float)) else 10_000
  126. sortable.append((rank, slug.strip()))
  127. sortable.sort()
  128. return _dedupe(slug for _, slug in sortable)
  129. def _fetch_models_from_api(access_token: str, base_url: Optional[str] = None) -> list[str]:
  130. """Fetch available models from the Codex API. Returns visible models sorted by priority.
  131. ``base_url`` is the host the credential is routed to (resolved together with it); the
  132. catalog is fetched there, never from a host the credential does not belong to (#121486).
  133. """
  134. try:
  135. from agent.model_metadata import _codex_catalog_probe_allowed
  136. from hermes_cli.auth_codex import _codex_base_url
  137. catalog_base = (base_url or "").strip().rstrip("/") or _codex_base_url()
  138. if not _codex_catalog_probe_allowed(access_token, catalog_base):
  139. return []
  140. import httpx
  141. # The per-account catalog needs ChatGPT-Account-ID (else ``{"models":[]}`` with HTTP 200
  142. # masquerades as "no models") and, for residency-enforced workspaces, the residency header.
  143. from agent.codex_headers import codex_account_headers
  144. headers = {"Authorization": f"Bearer {access_token}", **codex_account_headers(access_token)}
  145. from agent.model_metadata import fetch_codex_catalog_entries
  146. entries, _status = fetch_codex_catalog_entries(
  147. lambda url: httpx.get(url, headers=headers, timeout=10), base_url=catalog_base)
  148. except Exception as exc:
  149. logger.debug("Failed to fetch Codex models from API: %s", exc)
  150. return []
  151. return _finalize_codex_models(_ranked_slugs(entries))
  152. def _read_default_model(codex_home: Path) -> Optional[str]:
  153. config_path = codex_home / "config.toml"
  154. if not config_path.exists():
  155. return None
  156. try:
  157. import tomllib
  158. import tomllib
  159. payload = tomllib.loads(config_path.read_text(encoding="utf-8-sig"))
  160. except Exception:
  161. return None
  162. model = payload.get("model") if isinstance(payload, dict) else None
  163. return model.strip() if isinstance(model, str) and model.strip() else None
  164. def _read_cache_models(codex_home: Path) -> list[str]:
  165. cache_path = codex_home / "models_cache.json"
  166. if not cache_path.exists():
  167. return []
  168. try:
  169. raw = json.loads(cache_path.read_text(encoding="utf-8-sig"))
  170. except Exception:
  171. return []
  172. entries = raw.get("models") if isinstance(raw, dict) else None
  173. return _ranked_slugs(entries if isinstance(entries, list) else [])
  174. def get_codex_model_ids(access_token: Optional[str] = None, base_url: Optional[str] = None) -> list[str]:
  175. """Available Codex model IDs: live API (if token) > config.toml default > local cache > defaults.
  176. Pass the ``base_url`` resolved together with ``access_token`` (runtime/pool route) so live
  177. discovery asks the credential's own host. Without a live answer the result is
  178. ``CuratedFallbackModels``: a cache placeholder that never replaces a verified catalog, since
  179. account-gated rows such as Astra are absent from the offline hints."""
  180. codex_home = Path(os.getenv("CODEX_HOME", "").strip() or str(Path.home() / ".codex")).expanduser()
  181. if access_token:
  182. api_models = _fetch_models_from_api(access_token, base_url=base_url)
  183. if api_models:
  184. return _finalize_codex_models(api_models)
  185. # Late: models_catalog_static builds its codex table from this module at import time.
  186. from hermes_cli.models_catalog_static import CuratedFallbackModels
  187. default_model = _read_default_model(codex_home)
  188. return CuratedFallbackModels(_finalize_codex_models(_drop_undiscovered_astra(_dedupe([
  189. *([default_model] if default_model else []), *_read_cache_models(codex_home),
  190. *DEFAULT_CODEX_MODELS]))))