__init__.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. """OpenRouter provider profile."""
  2. import logging
  3. from typing import Any
  4. from agent.portal_tags import get_affinity_scope, get_conversation_context
  5. from agent.prompt_cache_scope import GROK_AGGREGATOR_MODEL_PREFIXES, is_fork_cache_scope
  6. from agent.reasoning_effort import codex_supported_efforts
  7. from agent.transports.codex import _cache_scope_from_session_id
  8. from providers import register_provider
  9. from providers.base import ProviderProfile
  10. logger = logging.getLogger(__name__)
  11. _CACHE: list[str] | None = None
  12. # Legacy allowlist of Anthropic models that still accept an explicit "disable
  13. # thinking" request. Claude 4.6+ and newer named models mandate reasoning and
  14. # 400 on any disable form, so *unknown* Anthropic models default to "cannot
  15. # disable" (mirrors agent/anthropic_adapter._get_anthropic_max_output).
  16. _ANTHROPIC_REASONING_OPTIONAL_SUBSTRINGS = (
  17. "claude-3", # 3, 3.5, 3.7
  18. "claude-opus-4-0", "claude-opus-4.0", "claude-opus-4-1", "claude-opus-4.1",
  19. "claude-sonnet-4-0", "claude-sonnet-4.0",
  20. "claude-opus-4-2025", "claude-sonnet-4-2025", # date-stamped 4.0 IDs
  21. "claude-opus-4-5", "claude-opus-4.5",
  22. "claude-sonnet-4-5", "claude-sonnet-4.5",
  23. "claude-haiku-4-5", "claude-haiku-4.5",
  24. )
  25. def _anthropic_reasoning_is_mandatory(model: str | None) -> bool:
  26. """True for Anthropic models that reject any disable-thinking form (unknown -> True)."""
  27. m = (model or "").lower()
  28. if not m.startswith(("anthropic/", "claude")) and "claude" not in m:
  29. return False
  30. return not any(sub in m for sub in _ANTHROPIC_REASONING_OPTIONAL_SUBSTRINGS)
  31. def _sticky_key(session_id: str | None) -> str | None:
  32. """Declared routing scope, then ambient conversation, then explicit session_id.
  33. Aux call sites (compression, titles, vision, MoA…) pass no ``session_id``,
  34. so the ambient lineage ROOT keeps them pinned to their conversation."""
  35. return _cache_scope_from_session_id(get_affinity_scope() or get_conversation_context() or session_id)
  36. # OpenAI speed tiers. Nous Portal serves them as distinct slugs (``-fast``/``-flex``); OpenRouter
  37. # serves them as ENDPOINTS of the base model (tags ``openai/fast``, ``openai/flex``) and silently
  38. # routes an unknown suffix to the standard tier at standard price. So the picker carries the Nous
  39. # slugs for both providers, and here the wire model becomes the base slug with ``provider.only``
  40. # pinned to that tier's endpoints; the base slug is pinned to the standard endpoints so default
  41. # routing never lands on flex/fast.
  42. _SPEED_TIER_ENDPOINTS = {"": ("openai", "azure", "azure/us"), "-fast": ("openai/fast",), "-flex": ("openai/flex",)}
  43. _SPEED_TIERED_BASES = ("openai/gpt-6-astra", "openai/gpt-6-astra-pro")
  44. OPENROUTER_ENDPOINT_PINS: dict[str, tuple[str, tuple[str, ...]]] = {
  45. base + suffix: (base, tags) for base in _SPEED_TIERED_BASES for suffix, tags in _SPEED_TIER_ENDPOINTS.items()
  46. }
  47. class OpenRouterProfile(ProviderProfile):
  48. """OpenRouter aggregator — provider preferences, reasoning config passthrough."""
  49. @staticmethod
  50. def _clamp_reasoning_to_catalog(cfg: dict[str, Any], model: str | None) -> dict[str, Any] | None:
  51. """Clamp ``cfg["effort"]`` to the model's catalog-advertised levels.
  52. Returns None when the config is a disable and the catalog marks the
  53. route reasoning-mandatory (the caller omits the field).
  54. OpenRouter's /v1/models entries publish ``reasoning.supported_efforts``
  55. per model (ported from PrimeIntellect-ai/prime-agent#1258). Sending an
  56. unsupported effort (e.g. ``ultra`` to a route that stops at ``high``)
  57. yields provider 4xx errors; clamp to the nearest LOWER supported level
  58. instead. No-op when the catalog is unreachable, the model is unlisted,
  59. or no supported_efforts list is published (None = all levels accepted).
  60. """
  61. effort = cfg.get("effort")
  62. disabled = cfg.get("enabled") is False or effort == "none"
  63. if not effort and not disabled:
  64. return cfg
  65. try:
  66. from hermes_cli.models import clamp_reasoning_effort_to_supported
  67. from hermes_cli.models_reasoning_caps import openrouter_model_reasoning_capabilities
  68. caps = openrouter_model_reasoning_capabilities(model)
  69. if not caps or not caps.get("supports_reasoning"):
  70. return cfg
  71. # A reasoning-mandatory route 400s on a disable ("Reasoning is
  72. # mandatory for this endpoint and cannot be disabled") — omit
  73. # the field and let the model think, same as the Nous profile.
  74. # OpenRouter's catalog lists ``none`` for openai/gpt-6.1-sol, but upstream 400s on it
  75. # (live 2026-09-29), so the OpenAI ladder in agent.reasoning_effort wins over the catalog.
  76. if disabled:
  77. no_disable = (model or "").startswith("openai/") and "none" not in codex_supported_efforts(model)
  78. return None if caps.get("mandatory") or no_disable else cfg
  79. clamped = clamp_reasoning_effort_to_supported(
  80. effort, caps.get("supported_efforts")
  81. )
  82. except Exception:
  83. return cfg
  84. if clamped and clamped != effort:
  85. logger.debug(
  86. "openrouter: clamped reasoning effort %r → %r for %s (catalog supported_efforts=%s)",
  87. effort, clamped, model, caps.get("supported_efforts"),
  88. )
  89. cfg = {**cfg, "effort": clamped}
  90. return cfg
  91. def fetch_models(
  92. self, *, api_key: str | None = None, base_url: str | None = None, timeout: float = 8.0
  93. ) -> list[str] | None:
  94. """Public OpenRouter catalog (no auth), cached per process. Tool-call
  95. filtering happens in hermes_cli/models.py, which the picker reaches first."""
  96. global _CACHE
  97. if _CACHE is not None:
  98. return _CACHE
  99. try:
  100. result = super().fetch_models(api_key=None, base_url=base_url, timeout=timeout)
  101. except Exception as exc:
  102. logger.debug("fetch_models(openrouter): %s", exc)
  103. return None
  104. if result is not None:
  105. _CACHE = result
  106. return result
  107. def build_extra_body(self, *, session_id: str | None = None, **context: Any) -> dict[str, Any]:
  108. body: dict[str, Any] = {}
  109. # Top-level session_id is OpenRouter's sticky routing key (used directly,
  110. # not hashed from the opening messages; active from the first request).
  111. sticky_key = _sticky_key(session_id)
  112. if sticky_key:
  113. body["session_id"] = sticky_key
  114. prefs = context.get("provider_preferences")
  115. pin = OPENROUTER_ENDPOINT_PINS.get(context.get("model") or "")
  116. # The tier pin owns ``only`` (ignore/sort/... still apply) — except on the BASE slug, where the pin
  117. # merely keeps default routing off flex/fast and an explicit user ``only`` is the stronger intent.
  118. if pin and not (pin[0] == context.get("model") and (prefs or {}).get("only")):
  119. prefs = {**(prefs or {}), "only": list(pin[1])}
  120. if prefs:
  121. body["provider"] = prefs
  122. # Pareto Code router plugin is only meaningful for openrouter/pareto-code.
  123. score = context.get("openrouter_min_coding_score")
  124. if (context.get("model") or "") == "openrouter/pareto-code" and score is not None and score != "":
  125. try:
  126. score_f = float(score)
  127. except (TypeError, ValueError):
  128. score_f = None
  129. if score_f is not None and 0.0 <= score_f <= 1.0:
  130. body["plugins"] = [{"id": "pareto-router", "min_coding_score": score_f}]
  131. return body
  132. def build_api_kwargs_extras(
  133. self, *, reasoning_config: dict | None = None, supports_reasoning: bool = False,
  134. model: str | None = None, session_id: str | None = None, **context: Any,
  135. ) -> tuple[dict[str, Any], dict[str, Any]]:
  136. """Pass reasoning_config as extra_body.reasoning; pin Grok's cache via x-grok-conv-id;
  137. rewrite speed-tier slugs to their OpenRouter base model."""
  138. extra_body: dict[str, Any] = {}
  139. top_level: dict[str, Any] = {}
  140. pin = OPENROUTER_ENDPOINT_PINS.get(model or "")
  141. if pin and pin[0] != model:
  142. top_level["model"] = pin[0]
  143. if supports_reasoning:
  144. # Reasoning-mandatory Anthropic models use adaptive thinking: any
  145. # ``reasoning`` field (disable, or an enabled form on a tool-continuation
  146. # turn without a replayed thinking block) makes OpenRouter emit
  147. # ``thinking: {type: "disabled"}`` -> 400. Omit it; the user's effort
  148. # still reaches Anthropic's output_config.effort via top-level ``verbosity``.
  149. # Reasoning-mandatory Anthropic models (Claude 4.6+ / fable / future named models) use
  150. # *adaptive* thinking: the model decides how much to think, and OpenRouter ignores
  151. # ``reasoning.effort`` for them entirely. Sending any ``reasoning`` field is therefore both
  152. # pointless and actively harmful: - any enabled form, on a tool-continuation turn whose prior
  153. # assistant tool_call carries no thinking block (chat_completions never replays signed thinking
  154. # blocks), ALSO makes OpenRouter emit ``thinking: {type: "disabled"}`` → the same 400 on every
  155. # turn after the first tool call. See hermes-agent#42991 (disable case) and the tool-replay
  156. # follow-up. ``reasoning.effort`` being ignored does NOT mean these models have no effort lever
  157. # — OpenRouter honors the requested effort on the top-level ``verbosity`` field instead (it maps
  158. # to Anthropic's ``output_config.effort``; ``reasoning.effort`` is accepted but ignored —
  159. # confirmed by OpenRouter's Claude migration docs and a live token-spend probe in
  160. # hermes-agent#43432). Route the existing ``reasoning_config["effort"]`` (sourced from
  161. # ``agent.reasoning_effort``) onto ``verbosity`` so the knob the user already sets keeps working
  162. # for these models.
  163. if _anthropic_reasoning_is_mandatory(model):
  164. cfg = reasoning_config or {}
  165. effort = cfg.get("effort")
  166. if cfg.get("enabled", True) is not False and effort and effort != "none":
  167. top_level["verbosity"] = effort
  168. elif reasoning_config is not None:
  169. clamped = self._clamp_reasoning_to_catalog(
  170. dict(reasoning_config), model
  171. )
  172. if clamped is not None:
  173. extra_body["reasoning"] = clamped
  174. else:
  175. extra_body["reasoning"] = {"enabled": True, "effort": "medium"}
  176. # xAI's prompt cache is pinned per backend server via this header.
  177. grok_conv_id = _sticky_key(session_id)
  178. # A cache-parity fork carries the parent's ambient scope; on Grok that key would evict
  179. # the parent's server slot, so honour the fork-derived scope (agent/prompt_cache_scope.py).
  180. if is_fork_cache_scope(context.get("cache_scope_id")):
  181. grok_conv_id = context["cache_scope_id"]
  182. if grok_conv_id and model and model.startswith(GROK_AGGREGATOR_MODEL_PREFIXES):
  183. top_level["extra_headers"] = {"x-grok-conv-id": grok_conv_id}
  184. return extra_body, top_level
  185. openrouter = OpenRouterProfile(
  186. name="openrouter", aliases=("or",), env_vars=("OPENROUTER_API_KEY",), display_name="OpenRouter",
  187. description="OpenRouter — unified API for 200+ models", signup_url="https://openrouter.ai/keys",
  188. base_url="https://openrouter.ai/api/v1", models_url="https://openrouter.ai/api/v1/models",
  189. fallback_models=(
  190. "anthropic/claude-sonnet-4.6", "openai/gpt-5.4", "deepseek/deepseek-chat", "google/gemini-3.8-flash",
  191. "google/gemini-3.7-flash", "qwen/qwen3-plus",
  192. ),
  193. )
  194. register_provider(openrouter)