Open History to see the last commit
1import type {
2 ProviderUsageLimitsUpdate,
3 ServerProviderUsageLimits,
4 ServerProviderUsageWindow,
5} from "@t3tools/contracts";
6
7const WINDOW_KIND_ORDER: Record<ServerProviderUsageWindow["kind"], number> = {
8 session: 0,
9 weekly: 1,
10 monthly: 2,
11 other: 3,
12};
13
14export function clampPercent(value: number): number {
15 return Number.isFinite(value) ? Math.max(0, Math.min(100, value)) : 0;
16}
17
18function 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
28export 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
35export 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
50export 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 sends this notification beside every token-usage tick, almost
61 // always with unchanged numbers. Decide "nothing changed" per window on
62 // the way through so the no-op case never allocates a new snapshot.
63 let changed = false;
64 for (const window of update.windows) {
65 const existing = merged.get(window.id);
66 const next: ServerProviderUsageWindow = {
67 ...window,
68 usedPercent: clampPercent(window.usedPercent),
69 ...(window.resetsAt === undefined && existing?.resetsAt !== undefined
70 ? { resetsAt: existing.resetsAt }
71 : {}),
72 ...(window.windowDurationMins === undefined && existing?.windowDurationMins !== undefined
73 ? { windowDurationMins: existing.windowDurationMins }
74 : {}),
75 };
76 if (existing === undefined || !usageWindowEquals(existing, next)) {
77 merged.set(window.id, next);
78 changed = true;
79 }
80 }
81 if (!changed && previous !== undefined) {
82 return previous;
83 }
84 const unavailable =
85 previous === undefined ? { reason: "probeFailed" as const } : previous.unavailable;
86 return {
87 ...makeUsageLimits({ checkedAt: input.checkedAt, windows: merged.values() }),
88 ...(previous?.resetCredits !== undefined ? { resetCredits: previous.resetCredits } : {}),
89 ...(unavailable !== undefined ? { unavailable } : {}),
90 };
91}
92
93function usageWindowEquals(a: ServerProviderUsageWindow, b: ServerProviderUsageWindow): boolean {
94 return (
95 a.id === b.id &&
96 a.kind === b.kind &&
97 a.label === b.label &&
98 a.usedPercent === b.usedPercent &&
99 a.resetsAt === b.resetsAt &&
100 a.windowDurationMins === b.windowDurationMins
101 );
102}
103
104export function resolveUsageLimitsAfterProbe(input: {
105 readonly published: ServerProviderUsageLimits | undefined;
106 readonly probed: ServerProviderUsageLimits | undefined;
107}): ServerProviderUsageLimits | undefined {
108 const { published, probed } = input;
109 if (probed?.unavailable?.reason === "probeFailed" && published && !published.unavailable) {
110 return published;
111 }
112 if (
113 published &&
114 probed &&
115 !published.unavailable &&
116 !probed.unavailable &&
117 Date.parse(published.checkedAt) > Date.parse(probed.checkedAt)
118 ) {
119 return { ...probed, checkedAt: published.checkedAt, windows: published.windows };
120 }
121 return probed;
122}