anthropic_endpoints.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. """Endpoint-family detection for Anthropic-compatible base URLs.
  2. A dozen services speak the Anthropic Messages API but differ in auth style, accepted beta
  3. headers, and request quirks (MiniMax, Kimi/Moonshot, DeepSeek, OpenCode, Azure AI Foundry, Nous
  4. Portal, Bedrock). Every such difference is decided from the configured base URL, so the
  5. predicates live together here as pure functions (no I/O, SDK or credentials) that both
  6. ``agent/anthropic_adapter.py`` and ``agent/anthropic_message_convert.py`` can import without a
  7. cycle.
  8. """
  9. from urllib.parse import urlparse
  10. from utils import base_url_host_matches, base_url_hostname
  11. _MINIMAX_ANTHROPIC_PREFIXES = ("https://api.minimax.io/anthropic", "https://api.minimaxi.com/anthropic")
  12. def _normalize_base_url_text(base_url) -> str:
  13. """Coerce a base URL (str or ``httpx.URL``) to a stripped string; "" when falsy."""
  14. return str(base_url).strip() if base_url else ""
  15. def _normalized_lower(base_url) -> str:
  16. """``_normalize_base_url_text`` + rstrip("/") + lower(), the shape most predicates match on."""
  17. return _normalize_base_url_text(base_url).rstrip("/").lower()
  18. def _is_third_party_anthropic_endpoint(base_url: str | None) -> bool:
  19. """Any non-anthropic.com endpoint (own x-api-key keys; skip OAuth detection). No base_url =
  20. direct Anthropic API."""
  21. normalized = _normalized_lower(base_url)
  22. return bool(normalized) and "anthropic.com" not in normalized
  23. def _is_kimi_coding_endpoint(base_url: str | None) -> bool:
  24. """Kimi's /coding endpoint, which requires a claude-code User-Agent."""
  25. return _normalized_lower(base_url).startswith("https://api.kimi.com/coding")
  26. def _is_opencode_endpoint(base_url: str | None) -> bool:
  27. """OpenCode's Zen/Go relay (opencode.ai)."""
  28. return base_url_host_matches(base_url or "", "opencode.ai")
  29. # Kimi / Moonshot family model-name prefixes: official slugs (``kimi-k2.5``, ``kimi_thinking``,
  30. # ``moonshot-v1-8k``) and release lines (``k1.5-…``, ``k2-thinking``, ``k25-…``, ``k3.x``/``k3-…``).
  31. # Matched case-insensitively after stripping any ``vendor/`` prefix.
  32. _KIMI_FAMILY_MODEL_PREFIXES = (
  33. "kimi-", "kimi_", "moonshot-", "moonshot_", "k1.", "k1-", "k2.", "k2-", "k25", "k2.5", "k3.", "k3-",
  34. )
  35. # Bare release slugs with no separator suffix (Kimi Coding Plan serves K3 as exactly ``k3``);
  36. # exact-match so unrelated names sharing the prefix don't match.
  37. _KIMI_FAMILY_EXACT_SLUGS = frozenset({"k3"})
  38. def _model_name_is_kimi_family(model: str | None) -> bool:
  39. if not isinstance(model, str):
  40. return False
  41. m = model.strip().lower().rsplit("/", 1)[-1] # ``moonshotai/kimi-k2.5`` -> ``kimi-k2.5``
  42. return bool(m) and (m in _KIMI_FAMILY_EXACT_SLUGS or m.startswith(_KIMI_FAMILY_MODEL_PREFIXES))
  43. def _is_kimi_family_endpoint(base_url: str | None, model: str | None = None) -> bool:
  44. """Any Kimi / Moonshot Anthropic-Messages endpoint: the /coding endpoint, any api.kimi.com /
  45. moonshot.ai / moonshot.cn host, or any endpoint (e.g. a private gateway) whose *model* is in
  46. the Kimi family — the upstream enforces Kimi's thinking semantics regardless of hostname.
  47. Decides whether unsigned reasoning_content-derived thinking blocks are preserved on replay."""
  48. return (
  49. _is_kimi_coding_endpoint(base_url)
  50. or any(base_url_host_matches(base_url or "", d) for d in ("api.kimi.com", "moonshot.ai", "moonshot.cn"))
  51. or _model_name_is_kimi_family(model)
  52. )
  53. _DEEPSEEK_THINKING_MODEL_PREFIXES = (
  54. "deepseek-r", "deepseek-v4", "deepseek_v4", "deepseek-pro",
  55. "deepseek_pro", "deepseek-flash", "deepseek_flash",
  56. )
  57. def _model_name_is_deepseek_thinking(model: str | None) -> bool:
  58. """Known DeepSeek thinking families behind an Anthropic-compatible relay.
  59. Strip vendor namespaces, but do not treat arbitrary DeepSeek chat/distill
  60. names as evidence of the thinking replay contract.
  61. """
  62. if not isinstance(model, str):
  63. return False
  64. name = model.strip().lower().rsplit("/", 1)[-1]
  65. return bool(name) and name.startswith(_DEEPSEEK_THINKING_MODEL_PREFIXES)
  66. def _is_deepseek_anthropic_endpoint(base_url: str | None) -> bool:
  67. """DeepSeek's ``/anthropic`` route. In thinking mode DeepSeek requires prior-turn ``thinking``
  68. blocks to round-trip while the generic third-party path strips them; its blocks are unsigned,
  69. so it gets the same strip-signed / keep-unsigned policy as Kimi. Pinned to the ``/anthropic``
  70. path so the OpenAI-compatible base URL is not misclassified.
  71. Per DeepSeek's published compatibility matrix the blocks are unsigned (no Anthropic-proprietary
  72. signature, no ``redacted_thinking`` support), so this endpoint is handled with the same strip-signed /
  73. keep-unsigned policy used for Kimi's ``/coding`` endpoint. See hermes-agent#16748.
  74. """
  75. return base_url_host_matches(base_url or "", "api.deepseek.com") and "/anthropic" in _normalized_lower(base_url)
  76. def _is_nous_portal_endpoint(base_url: str | None) -> bool:
  77. """Nous Portal's Anthropic Messages route (Bearer JWT, verbatim catalog ids, native
  78. thinking-signature replay). Trusted hosts only: prod ``inference-api.nousresearch.com`` or the
  79. operator-set ``NOUS_INFERENCE_BASE_URL`` host (exact hostname equality, so neither lookalike
  80. domains nor sibling hosts of the override match)."""
  81. if base_url_host_matches(base_url or "", "inference-api.nousresearch.com"):
  82. return True
  83. try:
  84. from hermes_cli.auth import _nous_inference_env_override
  85. override = _nous_inference_env_override()
  86. except Exception:
  87. return False
  88. override_host = base_url_hostname(override) if override else ""
  89. return bool(override_host) and base_url_hostname(base_url or "") == override_host
  90. def _requires_bearer_auth(base_url: str | None) -> bool:
  91. """Providers needing ``Authorization: Bearer`` instead of ``x-api-key``: MiniMax, Azure AI
  92. Foundry, Palantir Foundry's LLM proxy, CommandCode, Nous Portal. Palantir/CommandCode use
  93. hostname matching (not substring) so ``evil.com/palantirfoundry`` paths don't trigger it."""
  94. normalized = _normalized_lower(base_url)
  95. return (
  96. _is_nous_portal_endpoint(base_url)
  97. or normalized.startswith(_MINIMAX_ANTHROPIC_PREFIXES)
  98. or "azure.com" in normalized
  99. or base_url_host_matches(normalized, "palantirfoundry.com")
  100. or base_url_host_matches(normalized, "api.commandcode.ai")
  101. )
  102. def _base_url_needs_context_1m_beta(base_url: str | None) -> bool:
  103. """Endpoints that still gate 1M context behind a beta (Azure)."""
  104. return "azure.com" in _normalize_base_url_text(base_url).lower()
  105. def _is_minimax_anthropic_endpoint(base_url: str | None) -> bool:
  106. """MiniMax's Anthropic-compatible endpoints, which reject the fine-grained-tool-streaming and
  107. context-1m betas (stripped even though MiniMax also uses Bearer auth)."""
  108. return _normalized_lower(base_url).startswith(_MINIMAX_ANTHROPIC_PREFIXES)
  109. def _is_azure_anthropic_endpoint(base_url: str | None) -> bool:
  110. """Azure-hosted Anthropic Messages endpoints serving ``/anthropic``: modern Foundry
  111. (``*.services.ai.azure.*``) and legacy Azure OpenAI (``*.openai.azure.*``) hosts; opts them
  112. into ``api-version`` query plumbing. Deliberately no finite TLD allow-list, so
  113. sovereign/private clouds work."""
  114. normalized = _normalize_base_url_text(base_url)
  115. if not normalized:
  116. return False
  117. parsed = urlparse(normalized)
  118. host_padded = f".{(parsed.hostname or '').lower().rstrip('.')}."
  119. is_azure_host = ".services.ai.azure." in host_padded or ".openai.azure." in host_padded
  120. return is_azure_host and "/anthropic" in (parsed.path or "").lower()
  121. def _is_bedrock_runtime_endpoint(base_url: str | None) -> bool:
  122. """Amazon Bedrock's runtime host (``bedrock-runtime[-fips].<region>.amazonaws.com[.cn]``, and VPC
  123. endpoint names carrying the same label), which the AnthropicBedrock SDK path binds as its base URL."""
  124. host = base_url_hostname(base_url or "")
  125. return any(label.startswith("bedrock-runtime") for label in host.split(".")) and (
  126. base_url_host_matches(host, "amazonaws.com") or base_url_host_matches(host, "amazonaws.com.cn")
  127. )
  128. def _is_vertex_ai_endpoint(base_url: str | None) -> bool:
  129. """Vertex AI hosts: global ``aiplatform.googleapis.com`` and regional ``<region>-aiplatform.googleapis.com``."""
  130. host = base_url_hostname(base_url or "")
  131. return host == "aiplatform.googleapis.com" or host.endswith("-aiplatform.googleapis.com")
  132. def _is_claude_platform_endpoint(base_url: str | None) -> bool:
  133. """Cloud platforms serving Claude under Anthropic's own thinking signatures: Bedrock, Vertex AI and
  134. Azure AI Foundry ``/anthropic``. Anthropic documents ``signature`` values as compatible across the
  135. Claude API, Bedrock and Google Cloud, so these replay signed thinking like the direct API."""
  136. return (
  137. _is_bedrock_runtime_endpoint(base_url)
  138. or _is_vertex_ai_endpoint(base_url)
  139. or _is_azure_anthropic_endpoint(base_url)
  140. )