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
type JsonRecord = Record<string, unknown>;

function asRecord(value: unknown): JsonRecord | null {
  return value && typeof value === "object" && !Array.isArray(value) ? (value as JsonRecord) : null;
}

function remaining(value: JsonRecord | null): number | null {
  if (!value) return null;
  const candidate = value.remaining ?? value.remainingPercentage;
  const parsed = typeof candidate === "number" ? candidate : Number(candidate);
  return Number.isFinite(parsed) ? parsed : null;
}

/**
 * Kimi uses the same 403 wording for two different conditions:
 * a depleted weekly subscription and a temporary request window. The latter
 * must stay recoverable, otherwise a healthy subscription is marked terminal.
 */
export function getKimiTemporaryRateLimitResetAt(
  usage: unknown,
  nowMs = Date.now()
): string | null {
  const quotas = asRecord(asRecord(usage)?.quotas);
  const rateLimit = asRecord(quotas?.Ratelimit);
  const weekly = asRecord(quotas?.Weekly);
  const rateLimitRemaining = remaining(rateLimit);
  const weeklyRemaining = remaining(weekly);
  const resetAt = typeof rateLimit?.resetAt === "string" ? rateLimit.resetAt : null;
  const resetMs = resetAt ? new Date(resetAt).getTime() : NaN;

  if (
    rateLimitRemaining !== 0 ||
    weeklyRemaining === null ||
    weeklyRemaining <= 0 ||
    !Number.isFinite(resetMs) ||
    resetMs <= nowMs
  ) {
    return null;
  }

  return resetAt;
}