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

3.3 KB
Raw Blame
import type { ProviderLimitsCacheEntry } from "@/lib/db/providerLimits";
import { sanitizeProviderBillingStatus } from "@/shared/utils/providerBilling";
import { GROK_BUILD_ADDITIONAL_CREDITS_URL } from "@/shared/utils/grokBilling";
import { parseCodexPaidCredits } from "@/lib/providers/codexPaidCredits";

const GROK_CLI_PROVIDER = "grok-cli";
const GLM_RESET_CARD_PROVIDERS = new Set(["glm", "glm-cn", "glmt", "zai"]);

type JsonRecord = Record<string, unknown>;

function isRecord(value: unknown): value is JsonRecord {
  return Boolean(value && typeof value === "object" && !Array.isArray(value));
}

function hasUsableCachedData(cache: ProviderLimitsCacheEntry | null | undefined): boolean {
  return Boolean(
    cache?.billing ||
    (cache?.quotas && Object.keys(cache.quotas).length > 0) ||
    (cache?.modelQuotas && Object.keys(cache.modelQuotas).length > 0)
  );
}

export function toProviderLimitsCacheEntry(
  usage: JsonRecord,
  source: string,
  // A fetcher that serves its last known quota (e.g. Claude during a 429 cooldown) flags it
  // `_stale` with the original read time — keep that time instead of stamping it as fresh.
  fetchedAt = usage._stale === true && typeof usage._staleSince === "string"
    ? usage._staleSince
    : new Date().toISOString()
): ProviderLimitsCacheEntry {
  const bankedResetCredits = Number(usage.bankedResetCredits);
  return {
    quotas: isRecord(usage.quotas) ? usage.quotas : null,
    ...(isRecord(usage.modelQuotas) ? { modelQuotas: usage.modelQuotas } : {}),
    plan: usage.plan ?? null,
    message: typeof usage.message === "string" ? usage.message : null,
    fetchedAt,
    source,
    bankedResetCredits: Number.isFinite(bankedResetCredits) ? bankedResetCredits : undefined,
    paidCredits: parseCodexPaidCredits(usage.paidCredits),
    billing: sanitizeProviderBillingStatus(usage.billing),
    quotaGroups: Array.isArray(usage.quotaGroups)
      ? (usage.quotaGroups as Array<Record<string, unknown>>)
      : undefined,
  };
}

export function mergeProviderLimitsCacheEntry(
  provider: string,
  next: ProviderLimitsCacheEntry,
  previous: ProviderLimitsCacheEntry | null | undefined
): ProviderLimitsCacheEntry {
  if (!previous) return next;

  if (!next.quotas && next.message && hasUsableCachedData(previous)) {
    return previous;
  }

  // A GLM quota refresh omits `bankedResetCredits` when the auxiliary reset-card
  // list request failed (fetchGlmResetCardCount → null). That "unknown" must not
  // erase a previously known count; an explicit number (including an
  // authoritative 0) always wins.
  if (
    GLM_RESET_CARD_PROVIDERS.has(provider) &&
    next.bankedResetCredits === undefined &&
    previous.bankedResetCredits !== undefined
  ) {
    return { ...next, bankedResetCredits: previous.bankedResetCredits };
  }

  if (provider !== GROK_CLI_PROVIDER) return next;

  const nextBilling = next.billing;
  const previousBilling = previous.billing;
  if (
    !nextBilling ||
    nextBilling.additionalCreditsUrl !== GROK_BUILD_ADDITIONAL_CREDITS_URL ||
    nextBilling.autoTopUp.available ||
    !previousBilling ||
    previousBilling.additionalCreditsUrl !== GROK_BUILD_ADDITIONAL_CREDITS_URL
  )
    return next;

  return {
    ...next,
    billing: {
      ...nextBilling,
      autoTopUp: previousBilling.autoTopUp,
    },
  };
}