UNPKG

@bitkyc08/opencodex

Version:

Universal provider proxy for OpenAI Codex & Claude Code — use any LLM with Codex CLI/App/SDK and Claude Code

38 lines (37 loc) • 2.43 kB
/** Raw advisory projection; compatibility display clamping/rounding remains unchanged. */ import type { DecisionQuotaWindow } from "../quota-decision-snapshot"; import { normalizeResetAt, toFiniteNumber } from "../quota-wire"; import { anthropicExtraUsageMonthlyPercent, anthropicUsageBuckets } from "./anthropic-usage-windows"; /** Normalize a reset value; an explicit invalid reset becomes NaN so it cannot read as an undated valid measurement. */ function reset(value: unknown): number | undefined { // Explicit invalid reset must not become an apparently undated valid measurement. return value === undefined || value === null ? undefined : normalizeResetAt(value) ?? NaN; } /** Project plan windows and the optional monthly fallback, preserving invalid plan resets. */ export function rawAnthropicUsageWindows(body: Record<string, unknown>, observedAt: number): DecisionQuotaWindow[] { const rows: DecisionQuotaWindow[] = []; const windows = anthropicUsageBuckets(body); const { buckets } = windows; for (const { window, row, percentField } of buckets) { const percent = toFiniteNumber(row[percentField]); if (percent === undefined) continue; const resetAt = reset(row.resets_at); rows.push({ window, percent, observedAt, ...(resetAt !== undefined ? { resetAt } : {}) }); } const monthlyPercent = anthropicExtraUsageMonthlyPercent(body, windows); if (monthlyPercent !== undefined) rows.push({ window: "monthly", percent: monthlyPercent, observedAt }); return rows; } /** Extract decision windows from Anthropic unified rate-limit response headers, converting utilization fractions to percentages. */ export function rawAnthropicHeaderWindows(headers: Headers, observedAt: number): DecisionQuotaWindow[] { const rows: DecisionQuotaWindow[] = []; for (const [window, wire] of [["5h", "5h"], ["weekly", "7d"], ["fable", "7d_oi"]] as const) { const fraction = toFiniteNumber(headers.get(`anthropic-ratelimit-unified-${wire}-utilization`)); if (fraction === undefined || fraction < 0 || fraction > 1) continue; const rawReset = headers.get(`anthropic-ratelimit-unified-${wire}-reset`); const seconds = toFiniteNumber(rawReset); const resetAt = rawReset === null ? undefined : seconds !== undefined && seconds > 0 ? seconds * 1000 : NaN; rows.push({ window, percent: fraction * 100, observedAt, ...(resetAt !== undefined ? { resetAt } : {}) }); } return rows; }