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

4.8 KB
Raw Blame
/**
 * chatCore Claude effort-variant normalizer (Quality Gate v2 / Fase 9 — chatCore god-file
 * decomposition, #3501).
 *
 * The Claude / Claude-Code model picker (e.g. VS Code's "Effort" slider) advertises
 * claude-...-{low,medium,high,xhigh,max}. Anthropic has no such model, so the suffixed id 404s
 * upstream. This strips it back to the real base id and surfaces the level as reasoning_effort so
 * the OpenAI→Claude translator / Claude-Code bridge can turn it into Claude thinking/effort config.
 * An explicit client-supplied effort always wins; native Claude passthrough (sourceFormat === claude)
 * is left untouched (it carries its own `thinking`). The body is mutated in place (model +
 * reasoning_effort), byte-identical to the previous inline block; the new effectiveModel and an
 * optional log line are returned for the handler to apply.
 */

import { splitClaudeEffortSuffix } from "../../config/providerModels.ts";
import { resolveProviderModelAlias } from "../../services/modelCanonicalization.ts";
import { isClaudeCodeCompatibleProvider } from "../../services/claudeCodeCompatible.ts";
import { FORMATS } from "../../translator/formats.ts";
import { isKnownClaudeEffortBaseModel } from "../../utils/claudeEffortVariants.ts";
import { isAntigravityLiteralTierModelId } from "../../utils/antigravityLiteralModelIds.ts";
import { isDevinLiteralModelIdProvider } from "../../utils/devinLiteralModelIds.ts";

/**
 * True when the client already supplied an explicit reasoning effort (top-level reasoning_effort,
 * reasoning.effort, or output_config.effort) — in which case the stripped suffix must not overwrite
 * it. A blank string counts as "not supplied".
 */
function hasExplicitClaudeEffort(claudeBody: Record<string, unknown>): boolean {
  const explicitEffort =
    claudeBody.reasoning_effort ??
    (claudeBody.reasoning as Record<string, unknown> | undefined)?.effort ??
    (claudeBody.output_config as Record<string, unknown> | undefined)?.effort;
  return !(explicitEffort === undefined || explicitEffort === null || explicitEffort === "");
}

export function applyClaudeEffortVariant(opts: {
  provider: string | null | undefined;
  effectiveModel: string;
  /** Mutated in place (model + reasoning_effort) when an effort suffix is stripped. */
  body: unknown;
  sourceFormat: string;
}): { effectiveModel: string; log: string | null } {
  const { provider, body, sourceFormat } = opts;
  // Cursor advertises native effort-suffixed ids. Its executor resolves exact
  // live-catalog ids before applying its own suffix fallback; stripping here
  // destroys that information before the executor can see it.
  if (
    provider === "cursor" ||
    provider === "cu" ||
    provider === "cursor-api" ||
    provider === "cua"
  ) {
    return { effectiveModel: opts.effectiveModel, log: null };
  }
  let effectiveModel = opts.effectiveModel;
  let log: string | null = null;

  // Devin CLI catalogs embed the effort tier in the model id itself
  // (`claude-opus-5-low` is a distinct upstream model). Stripping the suffix
  // would dispatch a base id that does not exist upstream, so keep the id
  // literal for these providers regardless of the Claude-family name.
  if (isDevinLiteralModelIdProvider(provider)) {
    return { effectiveModel, log: null };
  }

  // Antigravity Claude 5.x tiers are distinct upstream ids (`claude-opus-5-5-high`);
  // the bare base id does not exist on the Cloud Code backend.
  if (isAntigravityLiteralTierModelId(provider, effectiveModel)) {
    return { effectiveModel, log: null };
  }

  if (typeof effectiveModel === "string") {
    const { baseModel, effort } = splitClaudeEffortSuffix(effectiveModel);
    const isDirectClaudeLane = provider === "claude" || isClaudeCodeCompatibleProvider(provider);
    // Anthropic's dotted catalog aliases must resolve before checking the known base.
    const knownBase =
      provider === "anthropic" ? resolveProviderModelAlias(provider, baseModel) : baseModel;
    if (effort && (isDirectClaudeLane || isKnownClaudeEffortBaseModel(knownBase))) {
      // The suffixed id was resolved before it was split, so the provider alias (for example the
      // dotted `claude-haiku-4.5` that Anthropic only accepts as `claude-haiku-4-5`) has to be
      // applied to the base id here too.
      const resolvedBase = resolveProviderModelAlias(provider, baseModel);
      effectiveModel = resolvedBase;
      if (body && typeof body === "object" && !Array.isArray(body)) {
        const claudeBody = body as Record<string, unknown>;
        claudeBody.model = resolvedBase;
        if (sourceFormat !== FORMATS.CLAUDE && !hasExplicitClaudeEffort(claudeBody)) {
          claudeBody.reasoning_effort = effort;
        }
      }
      log = `Claude effort variant: stripped "-${effort}" → ${resolvedBase} (reasoning_effort=${effort})`;
    }
  }

  return { effectiveModel, log };
}