Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

usageLimits.ts

110 lines3.6 KB

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;
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 );
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) };
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 };
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 && previous.unavailable === undefined) {
82 return previous;
83 }
84 return {
85 ...makeUsageLimits({ checkedAt: input.checkedAt, windows: merged.values() }),
86 ...(previous?.resetCredits !== undefined ? { resetCredits: previous.resetCredits } : {}),
87 };
89​
90function usageWindowEquals(a: ServerProviderUsageWindow, b: ServerProviderUsageWindow): boolean {
91 return (
92 a.id === b.id &&
93 a.kind === b.kind &&
94 a.label === b.label &&
95 a.usedPercent === b.usedPercent &&
96 a.resetsAt === b.resetsAt &&
97 a.windowDurationMins === b.windowDurationMins
98 );
100​
101export function resolveUsageLimitsAfterProbe(input: {
102 readonly published: ServerProviderUsageLimits | undefined;
103 readonly probed: ServerProviderUsageLimits | undefined;
104}): ServerProviderUsageLimits | undefined {
105 const { published, probed } = input;
106 if (probed?.unavailable?.reason === "probeFailed" && published && !published.unavailable) {
107 return published;
108 }
109 return probed;