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 { noteResilienceAction } from "@/lib/usage/resilienceActionsContext.ts";

/**
 * resilience effective served-account changes (effective-change counting): the first served dispatch is 0,
 * each later dispatch served by a distinct account is +1. Abort and
 * shared-egress exclusion arms never reach the call site, so they never
 * count. Best-effort notes only.
 */
export function createServedAccountTracker(): (fingerprint: string) => void {
  let lastServedFingerprint: string | null = null;
  return (fingerprint: string): void => {
    if (lastServedFingerprint !== null && lastServedFingerprint !== fingerprint) {
      noteResilienceAction({ rotations: 1 });
    }
    lastServedFingerprint = fingerprint;
  };
}

/** resilience park outcome notes (local monotone wait + replayed/stored flags). */
export function noteParkWait(elapsedMs: number): void {
  noteResilienceAction({ parked: true, parkMs: Math.max(0, Math.round(elapsedMs)) });
}

export function noteReplayed(): void {
  noteResilienceAction({ replayed: true });
}

export function noteStoredFallback(): void {
  // The stored 429 fallback is served without a new send.
  noteResilienceAction({ replayed: false, stored429: true });
}