Skip to content

Commit 253f139

Browse files
fix(server): preserve Codex hub plan metadata (#21413)
Co-authored-by: Kalven Schraut <[email protected]>
1 parent 8c04786 commit 253f139

2 files changed

Lines changed: 51 additions & 3 deletions

File tree

‎apps/server/src/usage/cliproxyApi.test.ts‎

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,12 @@ const encodeJson = Schema.encodeSync(Schema.fromJsonString(Schema.Unknown));
4949

5050
function fixture(
5151
options: {
52-
accounts?: Array<(typeof accounts)[number] & { disabled?: boolean }>;
52+
accounts?: Array<
53+
(typeof accounts)[number] & {
54+
disabled?: boolean;
55+
id_token: { chatgpt_account_id: string; plan_type?: string; chatgpt_plan_type?: string };
56+
}
57+
>;
5358
upstream?: (request: RequestBody) => { status: number; body: unknown };
5459
cooldownStatus?: number;
5560
} = {},
@@ -254,6 +259,46 @@ describe("CLIProxyAPI built-in management API", () => {
254259
}),
255260
);
256261

262+
it.effect("resolves a missing usage plan from current, then legacy, token plans", () =>
263+
Effect.gen(function* () {
264+
const test = fixture({
265+
accounts: [
266+
{ ...accounts[0]!, id_token: { chatgpt_account_id: "account-a", plan_type: "free" } },
267+
{
268+
...accounts[1]!,
269+
id_token: {
270+
chatgpt_account_id: "account-b",
271+
plan_type: "go",
272+
chatgpt_plan_type: "plus",
273+
},
274+
},
275+
{
276+
...accounts[1]!,
277+
id: "third.json",
278+
auth_index: "c",
279+
id_token: { chatgpt_account_id: "account-c", chatgpt_plan_type: "free" },
280+
},
281+
],
282+
upstream: (request) => ({
283+
status: 200,
284+
body: {
285+
...(request.auth_index === "a" ? { plan_type: "plus" } : {}),
286+
rate_limit: { primary_window: { used_percent: 12 } },
287+
},
288+
}),
289+
});
290+
const api = yield* test.api;
291+
const result = yield* api.readAccounts(config);
292+
expect(result.map((account) => [account.plan, account.usageLimits.windows[0]?.kind])).toEqual(
293+
[
294+
["ChatGPT Plus Subscription", "session"],
295+
["ChatGPT Go Subscription", "monthly"],
296+
["ChatGPT Free Subscription", "monthly"],
297+
],
298+
);
299+
}),
300+
);
301+
257302
it.effect("keeps the path of a hub served under a prefix", () =>
258303
Effect.gen(function* () {
259304
const test = fixture();

‎apps/server/src/usage/cliproxyApi.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const AuthFile = Schema.Struct({
2828
Schema.Struct({
2929
chatgpt_account_id: Schema.optional(Schema.String),
3030
chatgpt_plan_type: Schema.optional(Schema.String),
31+
plan_type: Schema.optional(Schema.String),
3132
}),
3233
),
3334
});
@@ -305,14 +306,16 @@ export const makeCliproxyApi = Effect.gen(function* () {
305306
: null;
306307
const available = yield* credits(config, account).pipe(Effect.orElseSucceed(() => undefined));
307308
const next = available?.[0];
309+
const planType =
310+
usage.plan_type ?? account.id_token?.plan_type ?? account.id_token?.chatgpt_plan_type;
308311
return {
309312
...base,
310-
plan: codexPlanLabel(usage.plan_type ?? account.id_token?.chatgpt_plan_type),
313+
plan: codexPlanLabel(planType),
311314
usageLimits: {
312315
...codexRateLimitsToLimits({
313316
checkedAt,
314317
snapshot: {
315-
planType: usage.plan_type ?? null,
318+
planType: planType ?? null,
316319
primary: toWindow(usage.rate_limit?.primary_window),
317320
secondary: toWindow(usage.rate_limit?.secondary_window),
318321
},

0 commit comments

Comments
 (0)