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

providerUsageLimits.ts

167 lines6.6 KB

1import * as Schema from "effect/Schema";
2​
3import {
4 ForwardCompatibleArray,
5 IsoDateTime,
6 NonNegativeInt,
7 TrimmedNonEmptyString,
8} from "./baseSchemas.ts";
9import { ProviderDriverKind, ProviderInstanceId } from "./providerInstance.ts";
10import { UsageLimitSourceId } from "./usageLimitSourceId.ts";
11​
12/**
13 * One rolling quota window a subscription provider reports for the signed-in
14 * account, e.g. Claude's five-hour session or Codex's weekly allowance.
15 *
16 * `id` is stable per provider (`five_hour`, `seven_day_opus`, `primary`) so a
17 * sparse turn-driven update lands on the same row a full probe produced.
18 * `kind` only orders and labels the bar.
19 */
20export const ServerProviderUsageWindow = Schema.Struct({
21 id: TrimmedNonEmptyString,
22 kind: Schema.Literals(["session", "weekly", "monthly", "other"]),
23 label: TrimmedNonEmptyString,
24 usedPercent: Schema.Number.check(Schema.isBetween({ minimum: 0, maximum: 100 })),
25 resetsAt: Schema.optional(IsoDateTime),
26 windowDurationMins: Schema.optional(NonNegativeInt),
27});
28export type ServerProviderUsageWindow = typeof ServerProviderUsageWindow.Type;
29​
30/**
31 * Reset credits a provider banks on the account. Codex grants these when it
32 * has rate-limited the user unfairly; redeeming one clears the current
33 * windows. Only present when the provider reports them at all.
34 */
35export const ServerProviderResetCredits = Schema.Struct({
36 availableCount: NonNegativeInt,
37 nextExpiresAt: Schema.optional(IsoDateTime),
38 /** Pins hub redemption to the displayed credit, including retries from another client. */
39 nextCreditId: Schema.optional(TrimmedNonEmptyString),
40});
41export type ServerProviderResetCredits = typeof ServerProviderResetCredits.Type;
42​
43/**
44 * Subscription usage the provider knows about the signed-in account.
45 *
46 * `unavailable` distinguishes an account that can never report windows (API
47 * key, Bedrock) from a probe that failed this time, so clients can keep the
48 * last good bars for the latter and clear them for the former.
49 */
50export const ServerProviderUsageLimits = Schema.Struct({
51 checkedAt: IsoDateTime,
52 windows: ForwardCompatibleArray(ServerProviderUsageWindow),
53 resetCredits: Schema.optional(ServerProviderResetCredits),
54 unavailable: Schema.optional(
55 Schema.Struct({
56 reason: Schema.Literals(["unsupported", "probeFailed"]),
57 message: Schema.optional(TrimmedNonEmptyString),
58 }),
59 ),
60});
61export type ServerProviderUsageLimits = typeof ServerProviderUsageLimits.Type;
62​
63/**
64 * What an adapter reports when its runtime pushes a rate-limit update during
65 * a turn. Sparse by contract: Claude's `rate_limit_event` names one window at
66 * a time and Codex documents its notification as a partial. Windows merge by
67 * `id` onto the instance's published snapshot; omitted windows are unchanged.
68 */
69export const ProviderUsageLimitsUpdate = Schema.Struct({
70 windows: Schema.Array(ServerProviderUsageWindow),
71});
72export type ProviderUsageLimitsUpdate = typeof ProviderUsageLimitsUpdate.Type;
73​
74/**
75 * One account a usage-limit source reports on. `driver` is the provider the
76 * account belongs to, for the icon and colour clients already have; the
77 * account itself is not something this environment can run turns on.
78 */
79export const UsageLimitSourceAccount = Schema.Struct({
80 id: TrimmedNonEmptyString,
81 driver: ProviderDriverKind,
82 /** The signed-in address, when the source names one; clients blur it like provider auth. */
83 email: Schema.optional(TrimmedNonEmptyString),
84 /** Plan as the matching provider would label it (`ChatGPT Pro 20x Subscription`). */
85 plan: Schema.optional(TrimmedNonEmptyString),
86 usageLimits: ServerProviderUsageLimits,
87});
88export type UsageLimitSourceAccount = typeof UsageLimitSourceAccount.Type;
89​
90/**
91 * The published state of one configured `usageLimitSources` entry. A source
92 * that could not be read keeps `error` beside an empty account list rather
93 * than vanishing, so the user can see it is configured but failing.
94 */
95export const UsageLimitSourceSnapshot = Schema.Struct({
96 id: UsageLimitSourceId,
97 kind: Schema.Literal("cliproxy"),
98 label: TrimmedNonEmptyString,
99 checkedAt: IsoDateTime,
100 accounts: ForwardCompatibleArray(UsageLimitSourceAccount),
101 error: Schema.optional(TrimmedNonEmptyString),
102});
103export type UsageLimitSourceSnapshot = typeof UsageLimitSourceSnapshot.Type;
104​
105export const UsageLimitSourceSnapshots = ForwardCompatibleArray(UsageLimitSourceSnapshot);
106export type UsageLimitSourceSnapshots = typeof UsageLimitSourceSnapshots.Type;
107​
108export const UsageLimitSourceConsumeResetCreditInput = Schema.Struct({
109 sourceId: UsageLimitSourceId,
110 accountId: TrimmedNonEmptyString,
111 creditId: TrimmedNonEmptyString,
112});
113export type UsageLimitSourceConsumeResetCreditInput =
114 typeof UsageLimitSourceConsumeResetCreditInput.Type;
115​
116export const ProviderConsumeResetCreditInput = Schema.Union([
117 Schema.Struct({ instanceId: ProviderInstanceId }),
118 UsageLimitSourceConsumeResetCreditInput,
119]);
120export type ProviderConsumeResetCreditInput = typeof ProviderConsumeResetCreditInput.Type;
121​
122export class UsageLimitSourceError extends Schema.TaggedError<UsageLimitSourceError>()(
123 "UsageLimitSourceError",
124 { detail: Schema.String },
125) {
126 override get message(): string {
127 return this.detail;
128 }
130​
131/** Mirrors Codex's own outcome set; other providers map onto it. */
132export const ProviderConsumeResetCreditOutcome = Schema.Literals([
133 "reset",
134 "nothingToReset",
135 "noCredit",
136 "alreadyRedeemed",
137]);
138export type ProviderConsumeResetCreditOutcome = typeof ProviderConsumeResetCreditOutcome.Type;
139​
140export const ProviderConsumeResetCreditResult = Schema.Struct({
141 outcome: ProviderConsumeResetCreditOutcome,
142 /** Redemption succeeded, but a follow-up such as clearing the hub cooldown failed. */
143 warning: Schema.optional(TrimmedNonEmptyString),
144});
145export type ProviderConsumeResetCreditResult = typeof ProviderConsumeResetCreditResult.Type;
146​
147/** A point-in-time view of one provider's limits, built for the /usage-limits panel. */
148export const UsageLimitsReport = Schema.Struct({
149 createdAt: IsoDateTime,
150 accounts: Schema.Array(
151 Schema.Struct({
152 id: TrimmedNonEmptyString,
153 driver: ProviderDriverKind,
154 label: TrimmedNonEmptyString,
155 plan: Schema.optional(TrimmedNonEmptyString),
156 email: Schema.optional(TrimmedNonEmptyString),
157 sourceLabel: Schema.optional(TrimmedNonEmptyString),
158 instanceId: Schema.optional(ProviderInstanceId),
159 resetCreditInput: Schema.optional(ProviderConsumeResetCreditInput),
160 displayName: Schema.optional(Schema.String),
161 accentColor: Schema.optional(Schema.String),
162 limits: ServerProviderUsageLimits,
163 }),
164 ),
165 notices: Schema.Array(Schema.String),
166});
167export type UsageLimitsReport = typeof UsageLimitsReport.Type;