mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-25 16:42:16 +03:00
Merged — locally validated (30/30 focused tests: chatcore-codex-account-pool, codex-account-cooldown-write, codex-account-pool, providers-route-codex-account-pool, resilience-explain-codex-account, sse-auth-codex-account-pool; typecheck:core clean; file-size/complexity/cognitive-complexity/changelog gates all green). Merges clean against the current release tip with zero conflicts. Great refactor — extracting persistCodexQuotaState out of chatCore.ts into a proper codexAccount/ module with virtual quota pool isolation is a solid improvement. Thanks!
72 lines
2.3 KiB
TypeScript
72 lines
2.3 KiB
TypeScript
import {
|
|
getCodexDualWindowCooldownMs,
|
|
getCodexModelScope,
|
|
parseCodexQuotaHeaders,
|
|
} from "../../executors/codex.ts";
|
|
import { updateCodexScopedQuotaState } from "@/lib/db/providers";
|
|
import type { CodexQuotaScope } from "../../config/codexQuotaScopes.ts";
|
|
|
|
export interface PersistCodexChildQuotaResult {
|
|
readonly scope: CodexQuotaScope;
|
|
readonly providerSpecificData: Record<string, unknown>;
|
|
readonly exhaustionLog: string | null;
|
|
}
|
|
|
|
/** Parse and atomically persist one virtual child's quota response evidence. */
|
|
export async function persistCodexChildQuotaResponse(params: {
|
|
connectionId: string;
|
|
model: string;
|
|
headers: Record<string, string>;
|
|
status: number;
|
|
fallbackRateLimitedUntil?: string | null;
|
|
}): Promise<PersistCodexChildQuotaResult | null> {
|
|
if (params.model.trim().length === 0) return null;
|
|
const quota = parseCodexQuotaHeaders(params.headers);
|
|
if (!quota) return null;
|
|
|
|
const scope = getCodexModelScope(params.model);
|
|
const quotaState = {
|
|
usage5h: quota.usage5h,
|
|
limit5h: quota.limit5h,
|
|
resetAt5h: quota.resetAt5h,
|
|
usage7d: quota.usage7d,
|
|
limit7d: quota.limit7d,
|
|
resetAt7d: quota.resetAt7d,
|
|
observedAt: new Date().toISOString(),
|
|
};
|
|
let exhaustedWindow: "5h" | "7d" | undefined;
|
|
let rateLimitedUntil: string | undefined;
|
|
|
|
if (params.status === 429) {
|
|
const exhausted = getCodexDualWindowCooldownMs(quota);
|
|
if (exhausted.cooldownMs > 0 && exhausted.window !== "none") {
|
|
exhaustedWindow = exhausted.window;
|
|
rateLimitedUntil =
|
|
exhausted.window === "7d" ? (quota.resetAt7d ?? undefined) : (quota.resetAt5h ?? undefined);
|
|
} else if (params.fallbackRateLimitedUntil) {
|
|
rateLimitedUntil = params.fallbackRateLimitedUntil;
|
|
}
|
|
}
|
|
|
|
const providerSpecificData = await updateCodexScopedQuotaState(params.connectionId, scope, {
|
|
quotaState,
|
|
exhaustedWindow: exhaustedWindow ?? null,
|
|
...(rateLimitedUntil
|
|
? {
|
|
rateLimitedUntil,
|
|
rateLimitSource: exhaustedWindow ? ("quota_reset" as const) : ("fallback" as const),
|
|
}
|
|
: {}),
|
|
});
|
|
if (!providerSpecificData) return null;
|
|
|
|
return {
|
|
scope,
|
|
providerSpecificData,
|
|
exhaustionLog:
|
|
exhaustedWindow && rateLimitedUntil
|
|
? `Quota exhaustion on ${exhaustedWindow} window, cooldown until ${rateLimitedUntil}`
|
|
: null,
|
|
};
|
|
}
|