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.4 KB
Raw Blame
/**
 * chatCore rate-limit connection guard (#14573).
 *
 * Decides whether a 429/quota-exhausted failure may keep the provider
 * connection active because sibling extra API keys remain eligible. A single
 * hot key must not take the other ~200 down: the per-key cooldown recorded at
 * the execution sites cools only the offending key, and as long as the rotator
 * can still produce an eligible key the connection stays up.
 *
 * Mirrors the ACCOUNT_DEACTIVATED extra-keys guard in chatCore.ts — same
 * shape, extended from the 401-family to the 429/rate-limit path.
 */
import {
  connectionHasExtraKeys,
  hasEligibleKey,
  type KeyHealth,
} from "../../services/apiKeyRotator.ts";

export function shouldKeepConnectionActiveOnRateLimit(
  credentials: unknown,
  errorConnectionId: string | null | undefined
): boolean {
  if (!errorConnectionId) return false;
  const creds = credentials as Record<string, unknown> | null | undefined;
  const psd = creds?.providerSpecificData as Record<string, unknown> | undefined;
  const extraKeys = (psd?.extraApiKeys as string[] | undefined) ?? [];
  const health = psd?.apiKeyHealth as Record<string, KeyHealth> | undefined;
  const primaryKey = typeof creds?.apiKey === "string" ? creds.apiKey : "";
  return (
    connectionHasExtraKeys(errorConnectionId, extraKeys) &&
    hasEligibleKey(errorConnectionId, primaryKey, extraKeys, health)
  );
}