Open History to see the last commit
1import type { ServerProviderModel } from "@workbench/contracts";
2
3export type CodexPlanType =
4 | "free"
5 | "go"
6 | "plus"
7 | "pro"
8 | "team"
9 | "business"
10 | "enterprise"
11 | "edu"
12 | "unknown";
13
14export interface CodexAccountSnapshot {
15 readonly type: "apiKey" | "chatgpt" | "unknown";
16 readonly planType: CodexPlanType | null;
17 readonly sparkEnabled: boolean;
18}
19
20export const CODEX_DEFAULT_MODEL = "gpt-5.3-codex";
21export const CODEX_SPARK_MODEL = "gpt-5.3-codex-spark";
22const CODEX_SPARK_ENABLED_PLAN_TYPES = new Set<CodexPlanType>(["pro"]);
23
24function asObject(value: unknown): Record<string, unknown> | undefined {
25 if (!value || typeof value !== "object") {
26 return undefined;
27 }
28 return value as Record<string, unknown>;
29}
30
31function asString(value: unknown): string | undefined {
32 return typeof value === "string" ? value : undefined;
33}
34
35export function readCodexAccountSnapshot(response: unknown): CodexAccountSnapshot {
36 const record = asObject(response);
37 const account = asObject(record?.account) ?? record;
38 const accountType = asString(account?.type);
39
40 if (accountType === "apiKey") {
41 return {
42 type: "apiKey",
43 planType: null,
44 sparkEnabled: false,
45 };
46 }
47
48 if (accountType === "chatgpt") {
49 const planType = (account?.planType as CodexPlanType | null) ?? "unknown";
50 return {
51 type: "chatgpt",
52 planType,
53 sparkEnabled: CODEX_SPARK_ENABLED_PLAN_TYPES.has(planType),
54 };
55 }
56
57 return {
58 type: "unknown",
59 planType: null,
60 sparkEnabled: false,
61 };
62}
63
64export function codexAuthSubType(account: CodexAccountSnapshot | undefined): string | undefined {
65 if (account?.type === "apiKey") {
66 return "apiKey";
67 }
68
69 if (account?.type !== "chatgpt") {
70 return undefined;
71 }
72
73 return account.planType && account.planType !== "unknown" ? account.planType : "chatgpt";
74}
75
76export function codexAuthSubLabel(account: CodexAccountSnapshot | undefined): string | undefined {
77 switch (codexAuthSubType(account)) {
78 case "apiKey":
79 return "OpenAI API Key";
80 case "chatgpt":
81 return "ChatGPT Subscription";
82 case "free":
83 return "ChatGPT Free Subscription";
84 case "go":
85 return "ChatGPT Go Subscription";
86 case "plus":
87 return "ChatGPT Plus Subscription";
88 case "pro":
89 return "ChatGPT Pro Subscription";
90 case "team":
91 return "ChatGPT Team Subscription";
92 case "business":
93 return "ChatGPT Business Subscription";
94 case "enterprise":
95 return "ChatGPT Enterprise Subscription";
96 case "edu":
97 return "ChatGPT Edu Subscription";
98 default:
99 return undefined;
100 }
101}
102
103export function adjustCodexModelsForAccount(
104 baseModels: ReadonlyArray<ServerProviderModel>,
105 account: CodexAccountSnapshot | undefined,
106): ReadonlyArray<ServerProviderModel> {
107 if (account?.sparkEnabled !== false) {
108 return baseModels;
109 }
110
111 return baseModels.filter((model) => model.isCustom || model.slug !== CODEX_SPARK_MODEL);
112}
113
114export function resolveCodexModelForAccount(
115 model: string | undefined,
116 account: CodexAccountSnapshot,
117): string | undefined {
118 if (model !== CODEX_SPARK_MODEL || account.sparkEnabled) {
119 return model;
120 }
121
122 return CODEX_DEFAULT_MODEL;
123}