Open History to see the last commit
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});
39export type ServerProviderResetCredits = typeof ServerProviderResetCredits.Type;
40
41/**
42 * Subscription usage the provider knows about the signed-in account.
43 *
44 * `unavailable` distinguishes an account that can never report windows (API
45 * key, Bedrock) from a probe that failed this time, so clients can keep the
46 * last good bars for the latter and clear them for the former.
47 */
48export const ServerProviderUsageLimits = Schema.Struct({
49 checkedAt: IsoDateTime,
50 windows: ForwardCompatibleArray(ServerProviderUsageWindow),
51 resetCredits: Schema.optional(ServerProviderResetCredits),
52 unavailable: Schema.optional(
53 Schema.Struct({
54 reason: Schema.Literals(["unsupported", "probeFailed"]),
55 message: Schema.optional(TrimmedNonEmptyString),
56 }),
57 ),
58});
59export type ServerProviderUsageLimits = typeof ServerProviderUsageLimits.Type;
60
61/**
62 * What an adapter reports when its runtime pushes a rate-limit update during
63 * a turn. Sparse by contract: Claude's `rate_limit_event` names one window at
64 * a time and Codex documents its notification as a partial. Windows merge by
65 * `id` onto the instance's published snapshot; omitted windows are unchanged.
66 */
67export const ProviderUsageLimitsUpdate = Schema.Struct({
68 windows: Schema.Array(ServerProviderUsageWindow),
69});
70export type ProviderUsageLimitsUpdate = typeof ProviderUsageLimitsUpdate.Type;
71
72/**
73 * One account a usage-limit source reports on. `driver` is the provider the
74 * account belongs to, for the icon and colour clients already have; the
75 * account itself is not something this environment can run turns on.
76 */
77export const UsageLimitSourceAccount = Schema.Struct({
78 id: TrimmedNonEmptyString,
79 driver: ProviderDriverKind,
80 /** The signed-in address, when the source names one; clients blur it like provider auth. */
81 email: Schema.optional(TrimmedNonEmptyString),
82 /** Plan as the matching provider would label it (`ChatGPT Pro 20x Subscription`). */
83 plan: Schema.optional(TrimmedNonEmptyString),
84 usageLimits: ServerProviderUsageLimits,
85});
86export type UsageLimitSourceAccount = typeof UsageLimitSourceAccount.Type;
87
88/**
89 * The published state of one configured `usageLimitSources` entry. A source
90 * that could not be read keeps `error` beside an empty account list rather
91 * than vanishing, so the user can see it is configured but failing.
92 */
93export const UsageLimitSourceSnapshot = Schema.Struct({
94 id: UsageLimitSourceId,
95 kind: Schema.Literal("cliproxy"),
96 label: TrimmedNonEmptyString,
97 checkedAt: IsoDateTime,
98 accounts: ForwardCompatibleArray(UsageLimitSourceAccount),
99 error: Schema.optional(TrimmedNonEmptyString),
100});
101export type UsageLimitSourceSnapshot = typeof UsageLimitSourceSnapshot.Type;
102
103export const UsageLimitSourceSnapshots = ForwardCompatibleArray(UsageLimitSourceSnapshot);
104export type UsageLimitSourceSnapshots = typeof UsageLimitSourceSnapshots.Type;
105
106export const ProviderConsumeResetCreditInput = Schema.Struct({
107 instanceId: ProviderInstanceId,
108});
109export type ProviderConsumeResetCreditInput = typeof ProviderConsumeResetCreditInput.Type;
110
111/** Mirrors Codex's own outcome set; other providers map onto it. */
112export const ProviderConsumeResetCreditOutcome = Schema.Literals([
113 "reset",
114 "nothingToReset",
115 "noCredit",
116 "alreadyRedeemed",
117]);
118export type ProviderConsumeResetCreditOutcome = typeof ProviderConsumeResetCreditOutcome.Type;
119
120export const ProviderConsumeResetCreditResult = Schema.Struct({
121 outcome: ProviderConsumeResetCreditOutcome,
122});
123export type ProviderConsumeResetCreditResult = typeof ProviderConsumeResetCreditResult.Type;