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

10.3 KB
Raw Blame
import { getDbInstance, rowToCamel } from "./core";
import type { QuotaSnapshotRow, ProviderUtilizationPoint } from "@/shared/types/utilization";
import {
  hasCodexScopeCooldown,
  liftCodexScopeCooldownOnHeadroom,
} from "./providers/codexAccountState";
import { isCodexSparkQuotaKey } from "@omniroute/open-sse/config/codexQuotaScopes";

type JsonRecord = Record<string, unknown>;

interface StatementLike<TRow = unknown> {
  all: (...params: unknown[]) => TRow[];
  get: (...params: unknown[]) => TRow | undefined;
  run: (...params: unknown[]) => { changes: number };
}

interface DbLike {
  prepare: <TRow = unknown>(sql: string) => StatementLike<TRow>;
}

let lastCleanupAt = 0;

export function saveQuotaSnapshot(snapshot: Omit<QuotaSnapshotRow, "id" | "created_at">): void {
  const db = getDbInstance() as unknown as DbLike;
  const now = new Date().toISOString();

  try {
    db.prepare(
      `INSERT INTO quota_snapshots
       (provider, connection_id, window_key, remaining_percentage, is_exhausted,
        next_reset_at, window_duration_ms, raw_data, created_at)
       VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`
    ).run(
      snapshot.provider,
      snapshot.connection_id,
      snapshot.window_key,
      snapshot.remaining_percentage,
      snapshot.is_exhausted,
      snapshot.next_reset_at,
      snapshot.window_duration_ms,
      snapshot.raw_data,
      now
    );
  } catch (err: any) {
    if (err?.message?.includes("no such table")) {
      console.warn(
        "[QuotaSnapshots] Skipping save: quota_snapshots table not found. Awaiting migration."
      );
      return;
    }
    throw err;
  }

  // #12860: When a new snapshot demonstrates headroom on a Codex connection,
  // lift any fallback-sourced scope cooldown parked by quota preflight.
  maybeLiftCodexCooldownOnHeadroom(snapshot);
}

/**
 * `rowToCamel` leaves snake_case keys intact on some driver paths, so accept
 * either casing rather than trusting one shape.
 */
type SnapshotShape = Partial<QuotaSnapshotRow> & {
  windowKey?: string;
  remainingPercentage?: number;
  isExhausted?: number;
};

function snapshotHasHeadroom(s: SnapshotShape): boolean {
  const pct = s.remainingPercentage ?? s.remaining_percentage ?? 0;
  const exhausted = s.isExhausted ?? s.is_exhausted ?? 0;
  return pct > 0 && exhausted !== 1;
}

function maybeLiftCodexCooldownOnHeadroom(
  snapshot: Omit<QuotaSnapshotRow, "id" | "created_at">
): void {
  if (
    snapshot.provider?.toLowerCase() !== "codex" ||
    typeof snapshot.connection_id !== "string" ||
    snapshot.connection_id.length === 0 ||
    (snapshot.remaining_percentage ?? 0) <= 0 ||
    snapshot.is_exhausted === 1
  ) {
    return;
  }

  try {
    const scope = isCodexSparkQuotaKey(snapshot.window_key) ? "spark" : "codex";
    // The scope-wide read is only worth paying for when a cooldown is
    // actually parked on this connection+scope; the common case is clean.
    if (!hasCodexScopeCooldown(snapshot.connection_id, scope)) return;

    const scopeWindows = getLatestQuotaSnapshotsForConnection(snapshot.connection_id).filter(
      (s: SnapshotShape) => {
        const key = s.windowKey ?? s.window_key;
        return scope === "spark" ? isCodexSparkQuotaKey(key) : !isCodexSparkQuotaKey(key);
      }
    );
    // Every window in the scope must be healthy: one exhausted window still
    // justifies the cooldown even when a sibling reports full headroom.
    if (scopeWindows.length > 0 && scopeWindows.every(snapshotHasHeadroom)) {
      liftCodexScopeCooldownOnHeadroom(snapshot.connection_id, scope);
    }
  } catch (err) {
    console.debug("[QuotaSnapshots] Headroom evaluation skipped:", err);
  }
}

/**
 * #10095 — a window the upstream did not report a fraction for is persisted as
 * `remaining_percentage = 0` with `raw_data = {"fractionReported":false}`. That
 * 0 is a placeholder, not a reading: consumers that rank or average remaining
 * quota must skip these rows. Accepts raw and `rowToCamel` shapes.
 */
export function isUnreportedQuotaSnapshot(snapshot: QuotaSnapshotRow): boolean {
  const view = snapshot as unknown as { rawData?: unknown; raw_data?: unknown };
  const rawData = view.rawData ?? view.raw_data;
  if (rawData == null) return false;
  try {
    const metadata = typeof rawData === "string" ? JSON.parse(rawData) : rawData;
    return (metadata as { fractionReported?: unknown } | null)?.fractionReported === false;
  } catch {
    return false;
  }
}

export function getQuotaSnapshots(opts: {
  provider?: string;
  connectionId?: string;
  since: string;
  until?: string;
}): QuotaSnapshotRow[] {
  const db = getDbInstance() as unknown as DbLike;
  const conditions: string[] = ["created_at >= ?"];
  const params: unknown[] = [opts.since];

  if (opts.provider) {
    conditions.push("provider = ?");
    params.push(opts.provider);
  }

  if (opts.connectionId) {
    conditions.push("connection_id = ?");
    params.push(opts.connectionId);
  }

  if (opts.until) {
    conditions.push("created_at <= ?");
    params.push(opts.until);
  }

  try {
    const sql = `SELECT * FROM quota_snapshots WHERE ${conditions.join(" AND ")} ORDER BY created_at ASC`;
    const rows = db.prepare(sql).all(...params);
    return rows.map((r) => rowToCamel(r) as unknown as QuotaSnapshotRow);
  } catch (err: any) {
    if (err?.message?.includes("no such table")) {
      return [];
    }
    throw err;
  }
}

/**
 * Returns the single latest snapshot row for each distinct `window_key`
 * ever observed for this connection.
 *
 * Deliberately NOT a "most recent N rows across all windows" query: a
 * connection with many quota windows where only a subset actively churn
 * (frequent writes as they drain) and the rest stay idle/healthy (a single
 * old row each, thanks to the #4438 no-op-write dedup) would otherwise have
 * its recent-rows slice flooded entirely by the hot windows, silently
 * evicting the idle windows from rehydration (#8431). Scoping "latest" PER
 * window_key via a window function keeps every window visible regardless of
 * how skewed the write frequency is across windows.
 */
export function getLatestQuotaSnapshotsForConnection(connectionId: string): QuotaSnapshotRow[] {
  const db = getDbInstance() as unknown as DbLike;

  try {
    const rows = db
      .prepare(
        `SELECT * FROM (
           SELECT *, ROW_NUMBER() OVER (
             PARTITION BY window_key ORDER BY created_at DESC, id DESC
           ) AS rn
           FROM quota_snapshots
           WHERE connection_id = ?
         )
         WHERE rn = 1`
      )
      .all(connectionId);

    return rows.map((row) => rowToCamel(row) as unknown as QuotaSnapshotRow);
  } catch (err: any) {
    if (err?.message?.includes("no such table")) {
      return [];
    }
    throw err;
  }
}

/**
 * Utilization series for the dashboard: ONE point per (bucket, provider-or-connection).
 *
 * A provider can report several quota windows at once (a 5h session plus a weekly
 * window, per-model pools, ...). The point therefore carries the TIGHTEST window of the
 * bucket (lowest remaining percentage, `windowKey` names it) and is exhausted when ANY
 * window is. Returning one row per window made consumers pick an arbitrary one, so a
 * provider whose weekly window was exhausted could be drawn from its half-empty session
 * window instead. Samples of the same window inside a bucket are still averaged first.
 */
export function getAggregatedSnapshots(opts: {
  provider?: string;
  since: string;
  until?: string;
  bucketMinutes: number;
  aggregateBy?: "provider" | "connection";
}): ProviderUtilizationPoint[] {
  const db = getDbInstance() as unknown as DbLike;
  const conditions: string[] = ["created_at >= ?"];
  const params: unknown[] = [opts.since];

  if (opts.provider) {
    conditions.push("provider = ?");
    params.push(opts.provider);
  }

  if (opts.until) {
    conditions.push("created_at <= ?");
    params.push(opts.until);
  }

  const bucketSeconds = Number(opts.bucketMinutes) * 60;
  if (!Number.isFinite(bucketSeconds) || bucketSeconds <= 0) {
    throw new Error("Invalid bucket size");
  }

  const groupFields =
    opts.aggregateBy === "connection"
      ? "bucket, provider, connection_id, window_key"
      : "bucket, provider, window_key";
  const selectKey =
    opts.aggregateBy === "connection" ? "provider || ':' || connection_id as provider" : "provider";

  try {
    const sql = `
      WITH per_window AS (
        SELECT
          datetime((strftime('%s', created_at) / ${bucketSeconds}) * ${bucketSeconds}, 'unixepoch') as bucket,
          ${selectKey},
          AVG(remaining_percentage) as remainingPct,
          MAX(is_exhausted) as isExhausted,
          window_key
        FROM quota_snapshots
        WHERE ${conditions.join(" AND ")}
        GROUP BY ${groupFields}
      )
      SELECT bucket, provider, remainingPct, isExhausted, window_key AS windowKey
      FROM (
        SELECT
          bucket,
          provider,
          remainingPct,
          MAX(isExhausted) OVER (PARTITION BY bucket, provider) AS isExhausted,
          window_key,
          ROW_NUMBER() OVER (
            PARTITION BY bucket, provider
            ORDER BY remainingPct IS NULL, remainingPct ASC, window_key ASC
          ) AS rn
        FROM per_window
      )
      WHERE rn = 1
      ORDER BY bucket ASC
    `;

    const rows = db.prepare(sql).all(...params) as Array<{
      bucket: string;
      provider: string;
      remainingPct: number | null;
      isExhausted: number;
      windowKey: string;
    }>;

    return rows.map((r) => ({
      timestamp: r.bucket,
      provider: r.provider,
      remainingPct: r.remainingPct ?? 0,
      isExhausted: r.isExhausted === 1,
      windowKey: r.windowKey,
    }));
  } catch (err: any) {
    if (err?.message?.includes("no such table")) {
      return [];
    }
    throw err;
  }
}

export function cleanupOldSnapshots(retentionDays = 90): number {
  const now = Date.now();
  const cleanupThresholdMs = 6 * 60 * 60 * 1000;

  if (now - lastCleanupAt < cleanupThresholdMs) {
    return 0;
  }

  const db = getDbInstance() as unknown as DbLike;
  const cutoffDate = new Date(Date.now() - retentionDays * 24 * 60 * 60 * 1000).toISOString();

  try {
    const result = db.prepare("DELETE FROM quota_snapshots WHERE created_at < ?").run(cutoffDate);
    lastCleanupAt = now;
    return result.changes;
  } catch (err: any) {
    if (err?.message?.includes("no such table")) {
      return 0;
    }
    throw err;
  }
}