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.2 KB
Raw Blame
/**
 * xAI Reasoning / Thinking Patcher
 *
 * Source of truth: router-for-me/CLIProxyAPI internal/thinking/provider/xai/apply.go
 *
 * Maps the various inbound reasoning/thinking spec shapes (OpenAI Chat,
 * OpenAI Responses, Anthropic Messages, Gemini) onto the xAI Responses
 * `reasoning` field. Single source of truth for budget mapping.
 *
 * Defaults policy mirrors CLIProxyAPI:
 *   - never proactively enable reasoning when the caller omits it
 *   - honor explicit caller intent verbatim
 */

const VALID_EFFORTS = new Set(["minimal", "low", "medium", "high", "xhigh"]);

export type ReasoningEffort = "minimal" | "low" | "medium" | "high" | "xhigh";

export function normalizeXaiReasoningEffort(effort: unknown): ReasoningEffort | undefined {
  if (typeof effort !== "string") return undefined;
  const normalized = effort.toLowerCase();
  // "max" is not an xAI tier; "xhigh" is real on grok-4.6+ and xAI itself
  // degrades it to "high" on older models, so passing it through is always safe.
  if (normalized === "max") return "high";
  return VALID_EFFORTS.has(normalized) ? (normalized as ReasoningEffort) : undefined;
}

/**
 * Map a numeric token budget to a discrete effort tier.
 *   <=0       → undefined (disabled)
 *   1..3999   → "low"
 *   4000..15999 → "medium"
 *   >=16000   → "high"
 */
export function budgetToEffort(budget: number): ReasoningEffort | undefined {
  if (typeof budget !== "number" || !Number.isFinite(budget) || budget <= 0) return undefined;
  if (budget >= 16000) return "high";
  if (budget >= 4000) return "medium";
  return "low";
}

interface AnthropicThinking {
  type?: string;
  budget_tokens?: number;
}

interface GeminiThinkingConfig {
  thinkingBudget?: number;
  includeThoughts?: boolean;
}

interface XaiReasoning {
  effort: ReasoningEffort;
}

interface ThinkingRequest {
  reasoning?: XaiReasoning | Record<string, unknown>;
  reasoning_effort?: string;
  thinking?: AnthropicThinking;
  thinkingConfig?: GeminiThinkingConfig;
  [key: string]: unknown;
}

interface ApplyThinkingOptions {
  defaultEffort?: ReasoningEffort;
}

/**
 * Apply reasoning/thinking patch to an xAI request body.
 *
 * Returns a new object — caller's request is not mutated.
 *
 * Recognized inbound shapes:
 *   - request.reasoning_effort: "minimal"|"low"|"medium"|"high"  (OpenAI Chat)
 *   - request.reasoning: { effort: ... }                          (OpenAI Responses)
 *   - request.thinking: { type: "enabled", budget_tokens: N }     (Anthropic)
 *   - request.thinkingConfig: { thinkingBudget: N, includeThoughts } (Gemini)
 */
export function applyThinking(
  request: ThinkingRequest,
  options: ApplyThinkingOptions = {}
): ThinkingRequest {
  if (!request || typeof request !== "object") return request;
  const out: ThinkingRequest = { ...request };

  // 1) Already xAI-native? Honor and stop.
  if (out.reasoning && typeof out.reasoning === "object") {
    const reasoning = out.reasoning as Record<string, unknown>;
    const normalizedEffort = normalizeXaiReasoningEffort(reasoning.effort);
    if (normalizedEffort) {
      if (reasoning.effort !== normalizedEffort) {
        out.reasoning = { ...reasoning, effort: normalizedEffort };
      }
      return out;
    }
  }

  // 2) OpenAI Chat reasoning_effort
  const reasoningEffort = normalizeXaiReasoningEffort(out.reasoning_effort);
  if (reasoningEffort) {
    out.reasoning = { effort: reasoningEffort };
    delete out.reasoning_effort;
    return out;
  }

  // 3) Anthropic-style thinking
  if (out.thinking && typeof out.thinking === "object") {
    if (out.thinking.type === "enabled") {
      const eff = budgetToEffort(out.thinking.budget_tokens ?? 0) ?? "medium";
      out.reasoning = { effort: eff };
    }
    delete out.thinking;
    return out;
  }

  // 4) Gemini-style thinkingConfig
  if (out.thinkingConfig && typeof out.thinkingConfig === "object") {
    const eff = budgetToEffort(out.thinkingConfig.thinkingBudget ?? 0);
    if (eff) out.reasoning = { effort: eff };
    delete out.thinkingConfig;
    return out;
  }

  // 5) Default — leave untouched, optionally apply defaultEffort
  if (options.defaultEffort && VALID_EFFORTS.has(options.defaultEffort)) {
    out.reasoning = { effort: options.defaultEffort };
  }
  return out;
}