codex_service_tiers.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. """Account-scoped speed and Daybreak metadata from the existing authenticated Codex catalogue.
  2. Unknown discovery preserves the requested tier. Only an explicit advertised tier
  3. list permits a downgrade; response echoes never drive selection or retries.
  4. Daybreak is the opposite: it is requested only when the catalog explicitly lists it.
  5. """
  6. from __future__ import annotations
  7. import logging
  8. import time
  9. from typing import Any
  10. logger = logging.getLogger(__name__)
  11. # Token fingerprints include the origin; token refresh/account changes cannot reuse
  12. # an old entitlement. No raw credentials or identities are retained in this cache.
  13. # Value: (speed tiers per model, fetched-at, ``available_access_programs.cyber`` per model). One
  14. # record, published by one assignment, so a reader never pairs a fresh timestamp with stale programs.
  15. _catalogs: dict[str, tuple[dict[str, frozenset[str]], float, dict[str, frozenset[str]]]] = {}
  16. _TTL = 3600
  17. _MAX_CATALOGS = 128
  18. DAYBREAK_PROGRAM = "daybreak_blue"
  19. def _advertised_tiers(entry: dict[str, Any]) -> frozenset[str] | None:
  20. tiers = entry.get("service_tiers")
  21. if isinstance(tiers, list):
  22. if any(not isinstance(item, dict) or not isinstance(item.get("id"), str) for item in tiers):
  23. return None
  24. if tiers:
  25. return frozenset(item["id"] for item in tiers)
  26. speeds = entry.get("additional_speed_tiers")
  27. if isinstance(speeds, list) and all(isinstance(item, str) for item in speeds):
  28. return frozenset({"fast": "priority"}.get(item, item) for item in speeds)
  29. return frozenset() if tiers == [] else None
  30. def _advertised_cyber_programs(entry: dict[str, Any]) -> frozenset[str] | None:
  31. programs = entry.get("available_access_programs")
  32. cyber = programs.get("cyber") if isinstance(programs, dict) else None
  33. if isinstance(cyber, list) and all(isinstance(item, str) for item in cyber):
  34. return frozenset(cyber)
  35. return None
  36. def remember_catalog(cache_key: str, entries: list[Any]) -> None:
  37. """Retain only speed and access-program metadata, even when a model omits its context window."""
  38. models, programs = {}, {}
  39. for entry in entries:
  40. if not isinstance(entry, dict) or not isinstance(entry.get("slug"), str):
  41. continue
  42. slug = entry["slug"].strip()
  43. tiers = _advertised_tiers(entry)
  44. if tiers is not None:
  45. models[slug] = tiers
  46. cyber = _advertised_cyber_programs(entry)
  47. if cyber is not None:
  48. programs[slug] = cyber
  49. if cache_key not in _catalogs and len(_catalogs) >= _MAX_CATALOGS:
  50. del _catalogs[min(_catalogs, key=lambda key: _catalogs[key][1])]
  51. _catalogs[cache_key] = (models, time.time(), programs)
  52. def _fresh_catalog(access_token: str, base_url: str) -> tuple | None:
  53. """This credential's catalog record once it is fresh, else None."""
  54. from agent import model_metadata as metadata
  55. key = metadata._codex_oauth_token_fingerprint(access_token, base_url)
  56. cached = _catalogs.get(key)
  57. if cached is None or time.time() - cached[1] >= _TTL:
  58. # Uses the established route/auth guards, headers, positive and negative
  59. # TTLs. A failed refresh must never consume expired entitlement metadata.
  60. metadata._fetch_codex_oauth_context_lengths_with_source(access_token, base_url)
  61. cached = _catalogs.get(key)
  62. if cached is None or time.time() - cached[1] >= _TTL:
  63. return None
  64. return cached
  65. def _tiers_for_model(access_token: str, base_url: str, model: str) -> frozenset[str] | None:
  66. from agent import model_metadata as metadata
  67. cached = _fresh_catalog(access_token, base_url)
  68. return None if cached is None else cached[0].get(metadata.strip_codex_context_variant_suffix(model))
  69. def _cyber_programs_for_model(access_token: str, base_url: str, model: str) -> frozenset[str] | None:
  70. from agent import model_metadata as metadata
  71. cached = _fresh_catalog(access_token, base_url)
  72. return None if cached is None else cached[2].get(metadata.strip_codex_context_variant_suffix(model))
  73. def _account_catalog_token(access_token: Any) -> str | None:
  74. """The ChatGPT OAuth JWT that reads this account's Codex catalog, or None."""
  75. from hermes_cli.auth_constants import AuthError, _decode_jwt_claims
  76. # Only a ChatGPT OAuth JWT reads this catalog; a caller's pre-init key (an OpenAI API key on a
  77. # profile that also has one) is not this account's credential, so use the stored Codex login.
  78. token = access_token if isinstance(access_token, str) and _decode_jwt_claims(access_token) else None
  79. if not token:
  80. try:
  81. from hermes_cli.auth_codex import resolve_codex_runtime_credentials
  82. token = resolve_codex_runtime_credentials(read_only=True).get("api_key")
  83. except AuthError: # no Codex login on this profile: unknown, never a refusal
  84. return None
  85. return token if isinstance(token, str) and token else None
  86. def codex_account_offers_tier(
  87. model: Any, tier: str, *, base_url: Any = None, access_token: Any = None,
  88. ) -> bool | None:
  89. """Whether the signed-in ChatGPT account's Codex catalog lists ``tier`` for ``model``.
  90. ``None`` when unknown (no usable token, catalog unreachable, model absent) so callers never refuse
  91. on a failed lookup. ``access_token`` defaults to the profile's stored Codex OAuth token."""
  92. from agent.codex_headers import CODEX_AUX_BASE_URL, is_official_codex_base_url
  93. base = str(base_url or "").strip() or CODEX_AUX_BASE_URL
  94. if not is_official_codex_base_url(base):
  95. return None
  96. token = _account_catalog_token(access_token)
  97. if token is None:
  98. return None
  99. from agent.model_metadata import _bare_codex_slug
  100. supported = _tiers_for_model(token, base, _bare_codex_slug(str(model or "")))
  101. return None if supported is None else tier in supported
  102. def daybreak_offered(model: Any, *, provider: Any = None, base_url: Any = None, access_token: Any = None) -> bool:
  103. """Whether the Daybreak switch is offered for ``model``: the ChatGPT/Codex subscription route, and
  104. this account's own Codex catalog lists ``daybreak_blue`` for the model. Unknown is False, so the
  105. switch stays hidden rather than offering something the account may not have."""
  106. from agent.codex_headers import CODEX_AUX_BASE_URL, is_official_codex_base_url
  107. from hermes_cli.models import normalize_provider
  108. if not provider or normalize_provider(provider) != "openai-codex":
  109. return False
  110. base = str(base_url or "").strip() or CODEX_AUX_BASE_URL
  111. if not is_official_codex_base_url(base):
  112. return False
  113. token = _account_catalog_token(access_token)
  114. if token is None:
  115. return False
  116. from agent.model_metadata import _bare_codex_slug
  117. programs = _cyber_programs_for_model(token, base, _bare_codex_slug(str(model or "")))
  118. return bool(programs) and DAYBREAK_PROGRAM in programs
  119. def apply_daybreak(agent: Any, overrides: dict[str, Any]) -> None:
  120. """Add ``access_programs.cyber: daybreak_blue`` when the user turned Daybreak on and this request's
  121. credential and model are listed for it. Checked per request, so a model switch, credential rotation
  122. or expired catalog never carries the program to a model or account without it."""
  123. from agent.codex_headers import is_official_codex_base_url
  124. base_url = str(getattr(agent, "base_url", "") or "")
  125. if (getattr(agent, "daybreak", False) is not True
  126. or getattr(agent, "api_mode", None) != "codex_responses"
  127. or getattr(agent, "provider", None) != "openai-codex"
  128. or not is_official_codex_base_url(base_url)):
  129. return
  130. token = getattr(agent, "api_key", None)
  131. if not isinstance(token, str) or not token:
  132. return
  133. programs = _cyber_programs_for_model(token, base_url, overrides.get("model", getattr(agent, "model", "")))
  134. if not programs or DAYBREAK_PROGRAM not in programs:
  135. return
  136. extra_body = dict(overrides.get("extra_body") or {})
  137. extra_body["access_programs"] = {**dict(extra_body.get("access_programs") or {}), "cyber": DAYBREAK_PROGRAM}
  138. overrides["extra_body"] = extra_body
  139. def ultrafast_offered(model: Any, *, provider: Any = None, base_url: Any = None, access_token: Any = None) -> bool:
  140. """Whether ``/fast ultrafast`` (and the Ultrafast picker row) is offered on this route: the route
  141. gate plus, on the ChatGPT/Codex subscription, the account's own Codex catalog. That backend accepts
  142. ``service_tier: "ultrafast"`` from a plan without the entitlement and silently serves the default
  143. tier, so the catalog the official client reads is the authority. An unreadable catalog never
  144. blocks (request-time negotiation still clamps per credential)."""
  145. from urllib.parse import urlparse
  146. from hermes_cli.models import normalize_provider, resolve_fast_mode_overrides
  147. if resolve_fast_mode_overrides(model, provider=provider, base_url=base_url, tier="ultrafast") is None:
  148. return False
  149. on_codex_sub = (provider and normalize_provider(provider) == "openai-codex") or (
  150. (urlparse(str(base_url or "")).hostname or "").lower() == "chatgpt.com")
  151. return not on_codex_sub or codex_account_offers_tier(
  152. model, "ultrafast", base_url=base_url, access_token=access_token) is not False
  153. def ultrafast_offered_for(cli: Any, model: Any) -> bool:
  154. """:func:`ultrafast_offered` on a CLI shell's resolved route (attributes absent before setup)."""
  155. return ultrafast_offered(model, provider=getattr(cli, "provider", None), base_url=getattr(cli, "base_url", None),
  156. access_token=getattr(cli, "api_key", None))
  157. def negotiate_request_tier(agent: Any, overrides: dict[str, Any]) -> None:
  158. """Resolve the current credential on each build without mutating user preference."""
  159. from agent.codex_headers import is_official_codex_base_url
  160. requested = overrides.get("service_tier")
  161. base_url = str(getattr(agent, "base_url", "") or "")
  162. if (requested not in {"ultrafast", "priority"}
  163. or getattr(agent, "api_mode", None) != "codex_responses"
  164. or getattr(agent, "provider", None) != "openai-codex"
  165. or not is_official_codex_base_url(base_url)):
  166. return
  167. token = getattr(agent, "api_key", None)
  168. if not isinstance(token, str) or not token:
  169. return
  170. model = overrides.get("model", getattr(agent, "model", ""))
  171. supported = _tiers_for_model(token, base_url, model)
  172. if supported is None:
  173. return
  174. candidates = ("ultrafast", "priority") if requested == "ultrafast" else ("priority",)
  175. effective = next((tier for tier in candidates if tier in supported), "default")
  176. if effective == "default":
  177. overrides.pop("service_tier", None) # Codex represents Standard by omission.
  178. else:
  179. overrides["service_tier"] = effective
  180. if effective != requested:
  181. logger.debug("Codex service tier %s -> %s: not advertised for selected credential", requested, effective)