The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { |
| 2 | ProviderUsageLimitsUpdate, |
| 3 | ServerProviderUsageLimits, |
| 4 | ServerProviderUsageWindow, |
| 5 | } from "@t3tools/contracts"; |
| 6 | |
| 7 | const WINDOW_KIND_ORDER: Record<ServerProviderUsageWindow["kind"], number> = { |
| 8 | session: 0, |
| 9 | weekly: 1, |
| 10 | monthly: 2, |
| 11 | other: 3, |
| 12 | }; |
| 13 | |
| 14 | export function clampPercent(value: number): number { |
| 15 | return Number.isFinite(value) ? Math.max(0, Math.min(100, value)) : 0; |
| 16 | } |
| 17 | |
| 18 | function sortWindows( |
| 19 | windows: Iterable<ServerProviderUsageWindow>, |
| 20 | ): ReadonlyArray<ServerProviderUsageWindow> { |
| 21 | return [...windows].toSorted( |
| 22 | (left, right) => |
| 23 | WINDOW_KIND_ORDER[left.kind] - WINDOW_KIND_ORDER[right.kind] || |
| 24 | left.id.localeCompare(right.id), |
| 25 | ); |
| 26 | } |
| 27 | |
| 28 | export function makeUsageLimits(input: { |
| 29 | readonly checkedAt: string; |
| 30 | readonly windows: Iterable<ServerProviderUsageWindow>; |
| 31 | }): ServerProviderUsageLimits { |
| 32 | return { checkedAt: input.checkedAt, windows: sortWindows(input.windows) }; |
| 33 | } |
| 34 | |
| 35 | export function makeUnavailableUsageLimits(input: { |
| 36 | readonly checkedAt: string; |
| 37 | readonly reason: "unsupported" | "probeFailed"; |
| 38 | readonly message?: string; |
| 39 | }): ServerProviderUsageLimits { |
| 40 | return { |
| 41 | checkedAt: input.checkedAt, |
| 42 | windows: [], |
| 43 | unavailable: { |
| 44 | reason: input.reason, |
| 45 | ...(input.message ? { message: input.message } : {}), |
| 46 | }, |
| 47 | }; |
| 48 | } |
| 49 | |
| 50 | export function applyUsageLimitsUpdate(input: { |
| 51 | readonly previous: ServerProviderUsageLimits | undefined; |
| 52 | readonly update: ProviderUsageLimitsUpdate; |
| 53 | readonly checkedAt: string; |
| 54 | }): ServerProviderUsageLimits | undefined { |
| 55 | const { previous, update } = input; |
| 56 | if (update.windows.length === 0 || previous?.unavailable?.reason === "unsupported") { |
| 57 | return previous; |
| 58 | } |
| 59 | const merged = new Map(previous?.windows.map((window) => [window.id, window] as const)); |
| 60 | // Codex repeats unchanged limits on token ticks; avoid allocating snapshots for no-ops. |
| 61 | let changed = false; |
| 62 | for (const window of update.windows) { |
| 63 | const existing = merged.get(window.id); |
| 64 | const next: ServerProviderUsageWindow = { |
| 65 | ...window, |
| 66 | usedPercent: clampPercent(window.usedPercent), |
| 67 | ...(window.resetsAt === undefined && existing?.resetsAt !== undefined |
| 68 | ? { resetsAt: existing.resetsAt } |
| 69 | : {}), |
| 70 | ...(window.windowDurationMins === undefined && existing?.windowDurationMins !== undefined |
| 71 | ? { windowDurationMins: existing.windowDurationMins } |
| 72 | : {}), |
| 73 | }; |
| 74 | if (existing === undefined || !usageWindowEquals(existing, next)) { |
| 75 | merged.set(window.id, next); |
| 76 | changed = true; |
| 77 | } |
| 78 | } |
| 79 | if (!changed && previous !== undefined) { |
| 80 | return previous; |
| 81 | } |
| 82 | const unavailable = |
| 83 | previous === undefined ? { reason: "probeFailed" as const } : previous.unavailable; |
| 84 | return { |
| 85 | ...makeUsageLimits({ checkedAt: input.checkedAt, windows: merged.values() }), |
| 86 | ...(previous?.resetCredits !== undefined ? { resetCredits: previous.resetCredits } : {}), |
| 87 | ...(previous?.zeroSpendCap ? { zeroSpendCap: true } : {}), |
| 88 | ...(unavailable !== undefined ? { unavailable } : {}), |
| 89 | }; |
| 90 | } |
| 91 | |
| 92 | function usageWindowEquals(a: ServerProviderUsageWindow, b: ServerProviderUsageWindow): boolean { |
| 93 | return ( |
| 94 | a.id === b.id && |
| 95 | a.kind === b.kind && |
| 96 | a.label === b.label && |
| 97 | a.usedPercent === b.usedPercent && |
| 98 | (a.modelIds === b.modelIds || |
| 99 | (a.modelIds !== undefined && |
| 100 | b.modelIds !== undefined && |
| 101 | a.modelIds.length === b.modelIds.length && |
| 102 | a.modelIds.every((id, index) => id === b.modelIds?.[index]))) && |
| 103 | (a.providerIds === b.providerIds || |
| 104 | (a.providerIds !== undefined && |
| 105 | b.providerIds !== undefined && |
| 106 | a.providerIds.length === b.providerIds.length && |
| 107 | a.providerIds.every((id, index) => id === b.providerIds?.[index]))) && |
| 108 | a.resetsAt === b.resetsAt && |
| 109 | a.windowDurationMins === b.windowDurationMins |
| 110 | ); |
| 111 | } |
| 112 | |
| 113 | export function resolveUsageLimitsAfterProbe(input: { |
| 114 | readonly published: ServerProviderUsageLimits | undefined; |
| 115 | readonly probed: ServerProviderUsageLimits | undefined; |
| 116 | }): ServerProviderUsageLimits | undefined { |
| 117 | const { published, probed } = input; |
| 118 | if (probed?.unavailable?.reason === "probeFailed" && published && !published.unavailable) { |
| 119 | return published; |
| 120 | } |
| 121 | if ( |
| 122 | published && |
| 123 | probed && |
| 124 | !published.unavailable && |
| 125 | !probed.unavailable && |
| 126 | Date.parse(published.checkedAt) > Date.parse(probed.checkedAt) |
| 127 | ) { |
| 128 | return { ...probed, checkedAt: published.checkedAt, windows: published.windows }; |
| 129 | } |
| 130 | return probed; |
| 131 | } |