TypeScript
fix(server): preserve Codex hub plan metadata (#21413)
Julius Marminge253f139b1529
diff --git a/apps/server/src/usage/cliproxyApi.test.ts b/apps/server/src/usage/cliproxyApi.test.tsindex 82521ac8c64..f41a1d7058a 100644--- a/apps/server/src/usage/cliproxyApi.test.ts+++ b/apps/server/src/usage/cliproxyApi.test.ts@@ -49,7 +49,12 @@ const encodeJson = Schema.encodeSync(Schema.fromJsonString(Schema.Unknown));function fixture(options: {- accounts?: Array<(typeof accounts)[number] & { disabled?: boolean }>;+ accounts?: Array<+ (typeof accounts)[number] & {+ disabled?: boolean;+ id_token: { chatgpt_account_id: string; plan_type?: string; chatgpt_plan_type?: string };+ }+ >;upstream?: (request: RequestBody) => { status: number; body: unknown };cooldownStatus?: number;} = {},@@ -254,6 +259,46 @@ describe("CLIProxyAPI built-in management API", () => {}),);+ it.effect("resolves a missing usage plan from current, then legacy, token plans", () =>+ Effect.gen(function* () {+ const test = fixture({+ accounts: [+ { ...accounts[0]!, id_token: { chatgpt_account_id: "account-a", plan_type: "free" } },+ {+ ...accounts[1]!,+ id_token: {+ chatgpt_account_id: "account-b",+ plan_type: "go",+ chatgpt_plan_type: "plus",+ },+ },+ {+ ...accounts[1]!,+ id: "third.json",+ auth_index: "c",+ id_token: { chatgpt_account_id: "account-c", chatgpt_plan_type: "free" },+ },+ ],+ upstream: (request) => ({+ status: 200,+ body: {+ ...(request.auth_index === "a" ? { plan_type: "plus" } : {}),+ rate_limit: { primary_window: { used_percent: 12 } },+ },+ }),+ });+ const api = yield* test.api;+ const result = yield* api.readAccounts(config);+ expect(result.map((account) => [account.plan, account.usageLimits.windows[0]?.kind])).toEqual(+ [+ ["ChatGPT Plus Subscription", "session"],+ ["ChatGPT Go Subscription", "monthly"],+ ["ChatGPT Free Subscription", "monthly"],+ ],+ );+ }),+ );+it.effect("keeps the path of a hub served under a prefix", () =>Effect.gen(function* () {const test = fixture();diff --git a/apps/server/src/usage/cliproxyApi.ts b/apps/server/src/usage/cliproxyApi.tsindex d386b7e9bd8..f097aa7a358 100644--- a/apps/server/src/usage/cliproxyApi.ts+++ b/apps/server/src/usage/cliproxyApi.ts@@ -28,6 +28,7 @@ const AuthFile = Schema.Struct({Schema.Struct({chatgpt_account_id: Schema.optional(Schema.String),chatgpt_plan_type: Schema.optional(Schema.String),+ plan_type: Schema.optional(Schema.String),}),),});@@ -305,14 +306,16 @@ export const makeCliproxyApi = Effect.gen(function* () {: null;const available = yield* credits(config, account).pipe(Effect.orElseSucceed(() => undefined));const next = available?.[0];+ const planType =+ usage.plan_type ?? account.id_token?.plan_type ?? account.id_token?.chatgpt_plan_type;return {...base,- plan: codexPlanLabel(usage.plan_type ?? account.id_token?.chatgpt_plan_type),+ plan: codexPlanLabel(planType),usageLimits: {...codexRateLimitsToLimits({checkedAt,snapshot: {- planType: usage.plan_type ?? null,+ planType: planType ?? null,primary: toWindow(usage.rate_limit?.primary_window),secondary: toWindow(usage.rate_limit?.secondary_window),},