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

1.2 KB
Raw Blame
import { parseApiKeyCodexServiceMode } from "../../shared/constants/codexServiceMode";
import { sharedSymbol } from "../../shared/utils/processSingleton";

// Request-local metadata, never accepted from a client body or persisted credentials.
const SERVICE_TIER_OVERRIDE = sharedSymbol("apiKeyCodexServiceTier");

export function applyApiKeyCodexServiceMode<T>(provider: string, body: T, mode: unknown): T {
  const tier = parseApiKeyCodexServiceMode(mode);
  if (provider !== "codex" || tier === "inherit" || !body || typeof body !== "object") {
    return body;
  }
  return { ...body, service_tier: tier };
}

export function withApiKeyCodexServiceMode<T>(provider: string, credentials: T, mode: unknown): T {
  const tier = parseApiKeyCodexServiceMode(mode);
  if (provider !== "codex" || tier === "inherit" || !credentials) return credentials;
  return { ...credentials, [SERVICE_TIER_OVERRIDE]: tier };
}

export function getApiKeyCodexServiceTier(
  credentials: unknown
): "default" | "priority" | "flex" | null {
  if (!credentials || typeof credentials !== "object") return null;
  const tier = parseApiKeyCodexServiceMode(
    (credentials as Record<symbol, unknown>)[SERVICE_TIER_OVERRIDE]
  );
  return tier === "inherit" ? null : tier;
}