Repository navigation
Expand file tree
/
Copy pathcodex-catalog.ts
More file actions
433 lines (398 loc) · 14.8 KB
/
Copy pathcodex-catalog.ts
File metadata and controls
433 lines (398 loc) · 14.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
// Codex model catalog: official ChatGPT rows stay in the main picker; everything wired in ~/.codex is
// tagged `custom`. `codex app-server` thread/start takes `model` and `modelProvider` separately.
import { readdirSync, readFileSync } from "node:fs";
import { homedir } from "node:os";
import { basename, join } from "node:path";
import type { ModelCatalog } from "../contracts.ts";
import { qualifiedModelLabel } from "../contracts.ts";
import { killCliTree, spawnCli } from "../procs.ts";
import { codexHome } from "./codex-identity.ts";
import { mergeLocalInject } from "./local-inject.ts";
export const STATIC_CODEX_MODELS: ModelCatalog = {
default: "gpt-5.6-sol",
options: [
// Only used when live discovery fails; these rows do not establish account access.
{ id: "gpt-6-astra", label: "GPT-6 Astra (availability unverified)" },
{ id: "gpt-6-sol", label: "GPT-6 Sol (availability unverified)" },
{ id: "gpt-6-luna", label: "GPT-6 Luna (availability unverified)" },
{ id: "gpt-5.6-sol", label: "GPT-5.6 Sol" },
{ id: "gpt-5.6-terra", label: "GPT-5.6 Terra" },
{ id: "gpt-5.6-luna", label: "GPT-5.6 Luna" },
{ id: "gpt-5.5", label: "GPT-5.5" },
{ id: "gpt-5.4", label: "GPT-5.4" },
{ id: "gpt-5.4-mini", label: "GPT-5.4 Mini" },
{ id: "gpt-5.3-codex-spark", label: "GPT-5.3 Codex Spark" },
],
};
/** Official rows force this provider, so a local `model_provider = "omlx"` cannot swallow GPT-5.6. */
export const OFFICIAL_CODEX_PROVIDER = "openai";
const SEP = "::";
const MODEL_ID = /^[\w][\w./:+-]*$/;
const PROVIDER_ID = /^[a-z][a-z0-9_-]*$/i;
export function encodeCodexSelection(provider: string, model: string): string {
return `${provider}${SEP}${model}`;
}
export function decodeCodexSelection(id: string | null | undefined): {
model: string | null;
modelProvider: string | null;
} {
if (!id) return { model: null, modelProvider: null };
const sep = id.indexOf(SEP);
if (sep > 0) {
return { model: id.slice(sep + SEP.length), modelProvider: id.slice(0, sep) };
}
// A bare id is official: a new cloud model must not fall through to the user's local provider.
return { model: id, modelProvider: MODEL_ID.test(id) ? OFFICIAL_CODEX_PROVIDER : null };
}
interface CodexAppServerModel {
id?: unknown;
displayName?: unknown;
hidden?: unknown;
isDefault?: unknown;
}
/** The installed Codex CLI's own ChatGPT catalog, every page: it changes more often than our releases. */
export function readCodexAppServerModelCatalog(
cli: string,
env: Record<string, string | undefined>,
timeoutMs = 8_000,
): Promise<ModelCatalog | null> {
return new Promise((resolve) => {
const child = spawnCli(cli, ["app-server"], {
cwd: homedir(),
env,
stdio: ["pipe", "pipe", "pipe"],
});
let settled = false;
let buffer = "";
let nextId = 1;
const models: CodexAppServerModel[] = [];
const cursors = new Set<string>();
const pending = new Map<number, "initialize" | "models">();
const finish = (catalog: ModelCatalog | null) => {
if (settled) return;
settled = true;
clearTimeout(timer);
killCliTree(child);
resolve(catalog);
};
const request = (method: string, params: unknown, kind: "initialize" | "models") => {
const id = nextId++;
pending.set(id, kind);
try {
child.stdin.write(`${JSON.stringify({ jsonrpc: "2.0", id, method, params })}\n`);
} catch {
finish(null);
}
};
const requestModels = (cursor: string | null) => {
request("model/list", { cursor, limit: 100 }, "models");
};
const timer = setTimeout(() => finish(null), timeoutMs);
timer.unref?.();
child.stdout.setEncoding("utf8");
child.stdout.on("data", (chunk) => {
buffer += chunk;
let newline;
while ((newline = buffer.indexOf("\n")) !== -1) {
const line = buffer.slice(0, newline);
buffer = buffer.slice(newline + 1);
if (!line.trim()) continue;
let message: any;
try {
message = JSON.parse(line);
} catch {
continue;
}
const kind = pending.get(message.id);
if (!kind) continue;
pending.delete(message.id);
if (message.error) {
finish(null);
return;
}
if (kind === "initialize") {
try {
child.stdin.write(`${JSON.stringify({ jsonrpc: "2.0", method: "initialized", params: {} })}\n`);
} catch {
finish(null);
return;
}
requestModels(null);
continue;
}
const page = message.result;
if (Array.isArray(page?.data)) models.push(...page.data);
const cursor = typeof page?.nextCursor === "string" && page.nextCursor ? page.nextCursor : null;
if (cursor && !cursors.has(cursor)) {
cursors.add(cursor);
requestModels(cursor);
continue;
}
const options: ModelCatalog["options"] = [];
const seen = new Set<string>();
let defaultModel: string | null = null;
for (const row of models) {
if (row.hidden === true || typeof row.id !== "string" || !MODEL_ID.test(row.id) || seen.has(row.id)) continue;
seen.add(row.id);
options.push({
id: row.id,
label: typeof row.displayName === "string" && row.displayName.trim() ? row.displayName : row.id,
});
if (row.isDefault === true) defaultModel = row.id;
}
if (!options.length) {
finish(null);
return;
}
finish({
default: defaultModel && seen.has(defaultModel) ? defaultModel : options[0].id,
options,
});
}
});
child.on("error", () => finish(null));
child.on("close", () => finish(null));
request("initialize", { clientInfo: { name: "openmausbot", version: "1" } }, "initialize");
});
}
function unquote(raw: string): string {
let value = raw.trim();
const hash = value.indexOf(" #");
if (hash !== -1 && !(value.startsWith('"') || value.startsWith("'"))) {
value = value.slice(0, hash).trim();
}
if (value.length >= 2) {
const quote = value[0];
if ((quote === '"' || quote === "'") && value.endsWith(quote)) {
return value.slice(1, -1).replace(/\\"/g, '"');
}
}
return value;
}
interface CodexProvider {
id: string;
name?: string;
baseUrl?: string;
envKey?: string;
}
interface CodexToml {
model?: string;
modelProvider?: string;
providers: CodexProvider[];
}
function parseCodexToml(text: string): CodexToml {
const result: CodexToml = { providers: [] };
const byId = new Map<string, CodexProvider>();
let section: "root" | "other" | CodexProvider = "root";
const providerFor = (id: string): CodexProvider => {
let provider = byId.get(id);
if (!provider) {
provider = { id };
byId.set(id, provider);
result.providers.push(provider);
}
return provider;
};
for (const line of text.split(/\r?\n/)) {
const stripped = line.trim();
if (!stripped || stripped.startsWith("#")) continue;
if (stripped.startsWith("[") && stripped.endsWith("]")) {
const inner = stripped.slice(1, -1);
const match = /^model_providers\.(.+)$/.exec(inner);
if (match) {
let id = match[1];
if (id.startsWith('"') && id.endsWith('"')) id = id.slice(1, -1);
section = PROVIDER_ID.test(id) ? providerFor(id) : "other";
} else {
section = "other";
}
continue;
}
const eq = stripped.indexOf("=");
if (eq < 0) continue;
const key = stripped.slice(0, eq).trim();
const value = unquote(stripped.slice(eq + 1));
if (!value) continue;
if (section === "root") {
if (key === "model") result.model = value;
if (key === "model_provider") result.modelProvider = value;
continue;
}
if (section === "other") continue;
if (key === "name") section.name = value;
if (key === "base_url") section.baseUrl = value;
if (key === "env_key") section.envKey = value;
}
return result;
}
function readText(path: string): string | null {
try {
return readFileSync(path, "utf8");
} catch {
return null;
}
}
function listDir(path: string): string[] {
try {
return readdirSync(path);
} catch {
return [];
}
}
function providerName(provider: string, known: Map<string, CodexProvider>): string {
return known.get(provider)?.name || provider;
}
function niceLabel(model: string, provider: string, known: Map<string, CodexProvider>, named: Map<string, string>): string {
const encoded = encodeCodexSelection(provider, model);
if (named.has(encoded)) return named.get(encoded)!;
const host = providerName(provider, known);
return qualifiedModelLabel(model, host, {
redundantWhen: () => host === provider,
decorate: (facet) => ` (${facet})`,
});
}
function collectCatalogNames(home: string): Map<string, string> {
const named = new Map<string, string>();
for (const file of listDir(join(home, "model-catalogs"))) {
if (!file.endsWith(".json")) continue;
const provider = basename(file, ".json").replace(/-models$/, "");
if (!PROVIDER_ID.test(provider)) continue;
const raw = readText(join(home, "model-catalogs", file));
if (!raw) continue;
let parsed: unknown;
try {
parsed = JSON.parse(raw);
} catch {
continue;
}
const records = Array.isArray(parsed)
? parsed
: parsed && typeof parsed === "object" && Array.isArray((parsed as { models?: unknown }).models)
? (parsed as { models: unknown[] }).models
: parsed && typeof parsed === "object" && Array.isArray((parsed as { data?: unknown }).data)
? (parsed as { data: unknown[] }).data
: [];
for (const record of records) {
if (!record || typeof record !== "object") continue;
const row = record as { slug?: unknown; id?: unknown; display_name?: unknown; name?: unknown };
const slug = typeof row.slug === "string" ? row.slug : typeof row.id === "string" ? row.id : "";
if (!MODEL_ID.test(slug)) continue;
const label = typeof row.display_name === "string" ? row.display_name : typeof row.name === "string" ? row.name : "";
if (label) named.set(encodeCodexSelection(provider, slug), label);
}
}
return named;
}
function idsFromModelsPayload(payload: unknown): string[] {
const records = Array.isArray(payload)
? payload
: payload && typeof payload === "object" && Array.isArray((payload as { data?: unknown }).data)
? (payload as { data: unknown[] }).data
: payload && typeof payload === "object" && Array.isArray((payload as { models?: unknown }).models)
? (payload as { models: unknown[] }).models
: [];
return records.flatMap((record) => {
if (typeof record === "string") return MODEL_ID.test(record) ? [record] : [];
if (!record || typeof record !== "object") return [];
const id = (record as { id?: unknown; slug?: unknown }).id ?? (record as { slug?: unknown }).slug;
return typeof id === "string" && MODEL_ID.test(id) ? [id] : [];
});
}
async function probeProviderModels(
provider: CodexProvider,
env: Record<string, string | undefined>,
fetchImpl: typeof fetch,
): Promise<string[]> {
if (!provider.baseUrl) return [];
const url = `${provider.baseUrl.replace(/\/$/, "")}/models`;
const headers: Record<string, string> = {};
if (provider.envKey && env[provider.envKey]) {
headers.Authorization = `Bearer ${env[provider.envKey]}`;
}
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), 1000);
timer.unref?.();
try {
const response = await fetchImpl(url, { signal: controller.signal, headers });
if (!response.ok) return [];
return idsFromModelsPayload(await response.json());
} catch {
return [];
} finally {
clearTimeout(timer);
}
}
export async function readCodexModelCatalog(
env: Record<string, string | undefined> = process.env,
fetchImpl: typeof fetch = fetch,
cli?: string,
): Promise<ModelCatalog> {
const official = (cli ? await readCodexAppServerModelCatalog(cli, env) : null) ?? STATIC_CODEX_MODELS;
// A relative CODEX_HOME or user home must not load a catalog from another directory.
const home = codexHome(env);
if (!home) return mergeLocalInject(official, env, fetchImpl);
const mainText = readText(join(home, "config.toml"));
if (!mainText) return mergeLocalInject(official, env, fetchImpl);
const main = parseCodexToml(mainText);
const known = new Map(main.providers.map((provider) => [provider.id, provider]));
const named = collectCatalogNames(home);
const extras: Array<{ provider: string; model: string }> = [];
const remember = (provider: string | undefined, model: string | undefined) => {
if (!provider || !model) return;
if (!PROVIDER_ID.test(provider) || !MODEL_ID.test(model)) return;
// A local provider may serve an official slug: keep its row provider-qualified.
if (
provider === OFFICIAL_CODEX_PROVIDER &&
official.options.some((option) => option.id === model)
) return;
extras.push({ provider, model });
};
remember(main.modelProvider, main.model);
for (const file of listDir(home)) {
if (!file.endsWith(".config.toml")) continue;
const profile = parseCodexToml(readText(join(home, file)) ?? "");
for (const provider of profile.providers) {
if (!known.has(provider.id)) known.set(provider.id, provider);
}
remember(profile.modelProvider ?? main.modelProvider, profile.model);
}
for (const [encoded, _label] of named) {
const decoded = decodeCodexSelection(encoded);
if (decoded.model && decoded.modelProvider) remember(decoded.modelProvider, decoded.model);
}
const live = await Promise.all(
[...known.values()].map(async (provider) => {
const ids = await probeProviderModels(provider, env, fetchImpl);
return ids.map((model) => ({ provider: provider.id, model }));
}),
);
for (const row of live.flat()) remember(row.provider, row.model);
const options = official.options.map((option) => ({ ...option }));
const seen = new Set(options.map((option) => option.id));
for (const extra of extras) {
const id = encodeCodexSelection(extra.provider, extra.model);
if (seen.has(id)) continue;
seen.add(id);
options.push({
id,
label: niceLabel(extra.model, extra.provider, known, named),
custom: true,
});
}
const configured = main.model && main.modelProvider
? main.modelProvider === OFFICIAL_CODEX_PROVIDER &&
official.options.some((option) => option.id === main.model)
? main.model
: encodeCodexSelection(main.modelProvider, main.model)
: main.model && seen.has(main.model)
? main.model
: null;
return mergeLocalInject(
{
default: configured && seen.has(configured) ? configured : official.default,
options,
},
env,
fetchImpl,
);
}