Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

15.9 KB
Raw Blame
import { FORMATS } from "../../translator/formats.ts";
import { isVerifiedNativeCodexRequest } from "../../config/codexIdentity.ts";
import { isClaudeCodeCompatibleProvider } from "../../services/claudeCodeCompatible.ts";
import { downgradeRestoredClaudeThinking } from "../../services/claudeThinkingStore.ts";
import { isResponsesEndpointPath } from "../../utils/responsesEndpoint.ts";
import { mergeClientAnthropicBeta } from "../../config/anthropicHeaders.ts";
import { getHeaderValueCaseInsensitive } from "./headers.ts";

export { isResponsesEndpointPath };

export const XAI_API_PROVIDERS = new Set(["xai", "xai-oauth", "xao"]);

const SAFEGUARDS_PAIRED_BETA = "dangerous-tool-use-2026-09-03";

/**
 * Top-level fields Claude Code sends that Anthropic accepts only next to their
 * paired beta. `safeguards` is the auto mode classifier request
 * (https://code.claude.com/docs/en/auto-mode-classifier-billing); without
 * `dangerous-tool-use-2026-09-03` on the outbound request, Anthropic rejects
 * the whole request:
 *
 *   400 safeguards: Extra inputs are not permitted
 *
 * The executor forwards a client beta only through mergeClientAnthropicBeta, so
 * the same merge decides here: keep the field when its beta travels with it,
 * strip it otherwise (the client then falls back to its own classifier).
 */
export function unpairedClaudeClientFields(clientAnthropicBeta: string | null | undefined) {
  const forwarded = mergeClientAnthropicBeta("", clientAnthropicBeta).toLowerCase().split(",");
  return forwarded.includes(SAFEGUARDS_PAIRED_BETA) ? [] : ["safeguards"];
}

/**
 * Drop the top-level fields for which Anthropic's Messages API rejects the
 * whole request on the native `claude` passthrough, which forwards the client
 * body verbatim. Third-party Claude-shape gateways are left untouched.
 */
export function stripClaudeRejectedTopLevelFields(
  body: Record<string, unknown>,
  clientHeaders: Headers | Record<string, unknown> | null | undefined
): void {
  // VS Code Claude extension and similar clients send both; Anthropic rejects the pair.
  if (body.temperature !== undefined && body.top_p !== undefined) delete body.top_p;
  const clientBeta = getHeaderValueCaseInsensitive(clientHeaders, "anthropic-beta");
  for (const field of unpairedClaudeClientFields(clientBeta)) delete body[field];
}

export function shouldUseNativeCodexPassthrough({
  provider,
  sourceFormat,
  endpointPath,
  body,
  headers,
}: {
  provider?: string | null;
  sourceFormat?: string | null;
  endpointPath?: string | null;
  body?: unknown;
  headers?: Headers | Record<string, unknown> | null;
}): boolean {
  if (provider !== "codex" && provider !== "chatgpt-web-codex") return false;
  if (sourceFormat !== FORMATS.OPENAI_RESPONSES) return false;
  let normalizedEndpoint = String(endpointPath || "");
  while (normalizedEndpoint.endsWith("/")) normalizedEndpoint = normalizedEndpoint.slice(0, -1);
  const segments = normalizedEndpoint.split("/");
  if (!segments.includes("responses")) return false;
  return provider === "codex" || isVerifiedNativeCodexRequest(body, headers);
}

export function shouldUseNativeXaiResponsesPassthrough({
  provider,
  sourceFormat,
  endpointPath,
}: {
  provider?: string | null;
  sourceFormat?: string | null;
  endpointPath?: string | null;
}): boolean {
  if (!provider || !XAI_API_PROVIDERS.has(provider)) return false;
  if (sourceFormat !== FORMATS.OPENAI_RESPONSES) return false;
  return isResponsesEndpointPath(endpointPath);
}

export function stampNativeResponsesPassthroughBody(
  body: Record<string, unknown>,
  mode: "codex" | "xai" | "openai-compatible"
): Record<string, unknown> {
  if (mode === "codex") return { ...body, _nativeCodexPassthrough: true };
  if (mode === "xai") return { ...body, _nativeXaiResponsesPassthrough: true };
  return { ...body, _nativeOpenAICompatibleResponsesPassthrough: true };
}

// A body only qualifies for the native-Responses passthrough fast path when it is
// actually shaped like a Responses API request (`input`, no `messages`). Endpoint
// path alone is not sufficient: an internally-synthesized Chat Completions-shaped
// body (e.g. the context-handoff summary request) can be dispatched through a
// closure that still carries the original client request's `/responses` endpoint,
// which otherwise makes `sourceFormat` resolve to "openai-responses" even though
// the body itself was never translated. See issue #12129.
function isResponsesShapedBody(body: unknown): boolean {
  if (!body || typeof body !== "object") return false;
  const candidate = body as Record<string, unknown>;
  return candidate.input !== undefined && candidate.messages === undefined;
}

export function shouldUseNativeOpenAICompatibleResponsesPassthrough({
  provider,
  sourceFormat,
  endpointPath,
  providerSpecificData,
  body,
}: {
  provider?: string | null;
  sourceFormat?: string | null;
  endpointPath?: string | null;
  providerSpecificData?: unknown;
  body?: unknown;
}): boolean {
  if (!provider?.startsWith("openai-compatible-")) return false;
  if (sourceFormat !== FORMATS.OPENAI_RESPONSES) return false;
  if (body !== undefined && !isResponsesShapedBody(body)) return false;
  if (providerSpecificData && typeof providerSpecificData === "object") {
    const psd = providerSpecificData as Record<string, unknown>;
    if (psd.apiType === "responses" || psd._omnirouteForceResponsesUpstream === true) {
      return true;
    }
  }
  return false;
}

/**
 * Pass `thinking` / `redacted_thinking` blocks through UNCHANGED.
 *
 * This used to rewrite every assistant thinking block to `redacted_thinking`
 * carrying a synthetic signature, on the assumption that a thinking signature is
 * bound to the auth token that produced it and would be rejected after a token /
 * model switch with 400 "Invalid signature in thinking block" (issue #2454).
 *
 * That rewrite is the actual cause of a different, far more common failure on the
 * Anthropic-native Claude OAuth passthrough:
 *
 *   400 messages.N.content.M: `thinking` or `redacted_thinking` blocks in the
 *   latest assistant message cannot be modified. These blocks must remain as
 *   they were in the original response.
 *
 * The Messages API validates submitted thinking blocks against the original
 * response and rejects ANY modification — so converting them to
 * `redacted_thinking` makes every multi-turn request with thinking fail (most
 * visible on long Claude Code tool-loops). The thinking-block signature is
 * validated server-side by Anthropic and stays valid when the blocks are replayed,
 * including under a different OAuth token — verified by preserving the blocks
 * across a mid-conversation account switch with zero "Invalid signature"
 * responses. The redaction is therefore both unnecessary and the cause of the
 * regression, so the blocks are now returned verbatim. The `signature` parameter
 * is kept for call-site compatibility.
 *
 * The one exception is a block that carries no signature at all (see
 * {@link dropUnsignedPassthroughThinkingBlocks}): it was never issued by Anthropic,
 * so it is dropped instead of forwarded.
 */
export function redactPassthroughThinkingSignatures(
  messages: unknown,
  _signature: string
): unknown {
  // Signed blocks stay verbatim; only blocks Anthropic never issued are dropped (#12917).
  return dropUnsignedPassthroughThinkingBlocks(messages);
}

type MessageLike = {
  role?: unknown;
  content?: unknown;
};

/**
 * True for an assistant `thinking` / `redacted_thinking` block that carries no
 * signature (`thinking`) or no payload (`redacted_thinking`). Such a block was
 * never issued by Anthropic — typically an OpenAI-compatible leg's reasoning that
 * the response translator relayed as `thinking` — so a real Anthropic upstream can
 * only answer 400 "Invalid signature in thinking block" when it is replayed.
 */
function isUnsignedThinkingBlock(block: unknown): boolean {
  if (!block || typeof block !== "object") return false;
  const { type, signature, data } = block as {
    type?: unknown;
    signature?: unknown;
    data?: unknown;
  };
  const hasText = (value: unknown) => typeof value === "string" && value.trim().length > 0;
  if (type === "thinking") return !hasText(signature);
  if (type === "redacted_thinking") return !hasText(data);
  return false;
}

/**
 * Drop assistant thinking blocks that no Anthropic upstream could have signed
 * (see {@link isUnsignedThinkingBlock}) before the history is replayed to a
 * genuine Anthropic endpoint. Signed blocks are never touched — Anthropic rejects
 * any modification of a valid one — and an assistant turn left with no content is
 * removed (consecutive same-role turns are accepted by the Messages API).
 *
 * This is the proactive complement of the exact-error one-shot recovery
 * (`executeWithAnthropicThinkingSignatureRecovery`), which cannot help when the
 * unsigned block sits in the still-open tool-use cycle. Returns the original
 * reference when nothing needs dropping; never mutates its input.
 */
export function dropUnsignedPassthroughThinkingBlocks(messages: unknown): unknown {
  if (!Array.isArray(messages)) return messages;

  let changed = false;
  const kept: unknown[] = [];
  for (const message of messages as MessageLike[]) {
    if (
      !message ||
      message.role !== "assistant" ||
      !Array.isArray(message.content) ||
      !message.content.some(isUnsignedThinkingBlock)
    ) {
      kept.push(message);
      continue;
    }
    changed = true;
    const content = message.content.filter((block) => !isUnsignedThinkingBlock(block));
    if (content.length > 0) kept.push({ ...message, content });
  }
  return changed ? kept : messages;
}

type ThinkingSignatureError = {
  provider?: string | null;
  status?: number | null;
  message?: string | null;
};

function isThinkingBlock(block: unknown): boolean {
  if (!block || typeof block !== "object") return false;
  const type = (block as { type?: unknown }).type;
  return type === "thinking" || type === "redacted_thinking";
}

function hasBlock(message: MessageLike | null | undefined, type: string): boolean {
  return (
    !!message &&
    Array.isArray(message.content) &&
    message.content.some(
      (block) => !!block && typeof block === "object" && (block as { type?: unknown }).type === type
    )
  );
}

/**
 * Match only the Anthropic validation failure this recovery path understands.
 * Generic 400s and the separate "latest assistant message cannot be modified"
 * validation error must continue through the normal error path unchanged.
 */
export function isAnthropicThinkingSignatureError({
  provider,
  status,
  message,
}: ThinkingSignatureError): boolean {
  const isAnthropicTarget =
    provider === "claude" ||
    (typeof provider === "string" && provider.startsWith("anthropic-compatible-"));
  if (!isAnthropicTarget || status !== 400 || typeof message !== "string") return false;

  return /invalid\s+[`'\"]?signature[`'\"]?\s+in\s+[`'\"]?thinking[`'\"]?\s+block/i.test(message);
}

/**
 * Build a one-shot recovery body after Anthropic has explicitly rejected a
 * thinking signature. Historical thinking blocks are omitted, but the complete
 * active tool-use cycle is preserved verbatim: when the request ends in one or
 * more `user[tool_result]` turns, every paired assistant `tool_use` turn in that
 * still-open cycle keeps its thinking blocks. A trailing unresolved assistant
 * `tool_use` turn is protected as well. The one exception inside that cycle is a
 * block OmniRoute itself restored from claudeThinkingStore (the client never sent
 * it): it is swapped back to the #5312 `redacted_thinking` placeholder and
 * forgotten, so a rejected restored signature is still recoverable.
 *
 * This helper is intentionally NOT used eagerly. Normal same-model requests must
 * retain their thinking history, cache shape, and current-model semantics.
 * Returns the original body reference when no safe recovery change is possible.
 */
export function stripHistoricalThinkingForSignatureRecovery<T>(body: T): T {
  if (!body || typeof body !== "object" || Array.isArray(body)) return body;

  const record = body as Record<string, unknown>;
  if (!Array.isArray(record.messages)) return body;

  const messages = record.messages as MessageLike[];
  const protectedAssistantIndexes = new Set<number>();
  let cursor = messages.length - 1;

  // Some internal callers can resume from an unresolved assistant tool_use.
  if (
    cursor >= 0 &&
    messages[cursor]?.role === "assistant" &&
    hasBlock(messages[cursor], "tool_use")
  ) {
    protectedAssistantIndexes.add(cursor);
    cursor -= 1;
  }

  // Walk the complete trailing tool-result chain. Interleaved thinking can span
  // several assistant/tool_result pairs, so protecting only the latest assistant
  // message is insufficient.
  while (
    cursor >= 0 &&
    messages[cursor]?.role === "user" &&
    hasBlock(messages[cursor], "tool_result")
  ) {
    cursor -= 1;
    while (cursor >= 0 && messages[cursor]?.role !== "assistant") cursor -= 1;
    if (cursor < 0 || !hasBlock(messages[cursor], "tool_use")) break;
    protectedAssistantIndexes.add(cursor);
    cursor -= 1;
  }

  let changed = false;
  const recoveredMessages = messages.map((message, index) => {
    if (!message || message.role !== "assistant" || !Array.isArray(message.content)) {
      return message;
    }
    if (protectedAssistantIndexes.has(index)) {
      const downgraded = downgradeRestoredClaudeThinking(message.content);
      if (!downgraded) return message;
      changed = true;
      return { ...message, content: downgraded as typeof message.content };
    }

    const content = message.content.filter((block) => !isThinkingBlock(block));
    if (content.length === message.content.length) return message;
    changed = true;
    return { ...message, content };
  });

  if (!changed) return body;
  return { ...record, messages: recoveredMessages } as T;
}

type SignatureRecoveryExecution<T> = {
  result: T;
  retried: boolean;
  recoveryBody: unknown | null;
};

/** Execute the normal body once, then perform at most one exact-error recovery. */
export async function executeWithAnthropicThinkingSignatureRecovery<T>(args: {
  provider?: string | null;
  body: unknown;
  execute: (body: unknown) => Promise<T>;
  getError: (
    result: T
  ) =>
    | { status?: number | null; message?: string | null }
    | null
    | Promise<{ status?: number | null; message?: string | null } | null>;
}): Promise<SignatureRecoveryExecution<T>> {
  const first = await args.execute(args.body);
  const failure = await args.getError(first);
  if (
    !failure ||
    !isAnthropicThinkingSignatureError({
      provider: args.provider,
      status: failure.status,
      message: failure.message,
    })
  ) {
    return { result: first, retried: false, recoveryBody: null };
  }

  const recoveryBody = stripHistoricalThinkingForSignatureRecovery(args.body);
  if (recoveryBody === args.body) {
    return { result: first, retried: false, recoveryBody: null };
  }

  return {
    result: await args.execute(recoveryBody),
    retried: true,
    recoveryBody,
  };
}

export function isClaudeCodeSemanticPassthroughRequest({
  provider,
  sourceFormat,
  targetFormat,
  headers,
  userAgent,
}: {
  provider?: string | null;
  sourceFormat?: string | null;
  targetFormat?: string | null;
  headers?: Record<string, unknown> | Headers | null;
  userAgent?: string | null;
}): boolean {
  const isDirectClaudeCodeProvider =
    provider === "claude" || isClaudeCodeCompatibleProvider(provider);
  if (!isDirectClaudeCodeProvider) return false;
  if (sourceFormat !== FORMATS.CLAUDE) return false;
  if (targetFormat !== FORMATS.CLAUDE) return false;

  const headerUserAgent = getHeaderValueCaseInsensitive(headers, "user-agent");
  const ua = `${userAgent || ""} ${headerUserAgent || ""}`.toLowerCase();
  if (ua.includes("claude-code") || ua.includes("claude-cli")) return true;

  const appHeader = getHeaderValueCaseInsensitive(headers, "x-app");
  if (typeof appHeader === "string" && appHeader.trim().toLowerCase() === "cli") return true;

  const sessionId = getHeaderValueCaseInsensitive(headers, "x-claude-code-session-id");
  return typeof sessionId === "string" && sessionId.trim().length > 0;
}