Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Send a model_catalog_json listing to the provider that serves it
nguyenngocthien1605b385217306af
diff --git a/server/drivers/codex-catalog.test.ts b/server/drivers/codex-catalog.test.tsindex ff31e73178..6e7bac9cd5 100644--- a/server/drivers/codex-catalog.test.ts+++ b/server/drivers/codex-catalog.test.ts@@ -88,6 +88,49 @@ describe("readCodexModelCatalog", () => {});});+ it("sends a model_catalog_json listing to the provider that serves it", async () => {+ chmodSync(FAKE_CLI, 0o755);+ const home = scratchHome({+ "config.toml": [+ 'model = "gpt-fake-default"',+ 'model_provider = "gw"',+ "model_catalog_json = 'gw-models.json'",+ "",+ "[model_providers.gw]",+ 'name = "My gateway"',+ 'base_url = "http://127.0.0.1:9/v1"',+ "",+ ].join("\n"),+ });++ const catalog = await readCodexModelCatalog({ HOME: home, PATH: process.env.PATH, FAKE_CODEX_TIERS: "1" }, fetch, FAKE_CLI);++ expect(catalog.default).toBe("gw::gpt-fake-default");+ expect(catalog.options.map((option) => option.id)).toEqual(["gw::gpt-fake-default", "gw::gpt-page-two"]);+ expect(catalog.options.every((option) => option.custom)).toBe(true);+ expect(catalog.options.map((option) => decodeCodexSelection(option.id).modelProvider)).toEqual(["gw", "gw"]);+ });++ it("keeps a model_catalog_json listing on OpenAI when OpenAI serves it", async () => {+ chmodSync(FAKE_CLI, 0o755);+ const home = scratchHome({ "config.toml": "model_catalog_json = 'models.json'\n" });++ const catalog = await readCodexModelCatalog({ HOME: home, PATH: process.env.PATH }, fetch, FAKE_CLI);++ expect(catalog.options.map((option) => option.id)).toEqual(["gpt-fake-default", "gpt-page-two"]);+ });++ it("keeps OpenAI's listing beside a local provider that has no model_catalog_json", async () => {+ chmodSync(FAKE_CLI, 0o755);+ const home = scratchHome({+ "config.toml": 'model = "local-model"\nmodel_provider = "gw"\n\n[model_providers.gw]\nname = "My gateway"\nbase_url = "http://127.0.0.1:9/v1"\n',+ });++ const catalog = await readCodexModelCatalog({ HOME: home, PATH: process.env.PATH }, fetch, FAKE_CLI);++ expect(catalog.options.map((option) => option.id)).toEqual(["gpt-fake-default", "gpt-page-two", "gw::local-model"]);+ });+it("adds a Fast row after each model that offers Fast and does not already run on it", async () => {chmodSync(FAKE_CLI, 0o755);const catalog = await readCodexModelCatalog(diff --git a/server/drivers/codex-catalog.ts b/server/drivers/codex-catalog.tsindex e5360c02ea..e2bd4e6d5b 100644--- a/server/drivers/codex-catalog.ts+++ b/server/drivers/codex-catalog.ts@@ -219,6 +219,7 @@ interface CodexProvider {interface CodexToml {model?: string;modelProvider?: string;+ modelCatalogJson?: string;providers: CodexProvider[];}@@ -260,6 +261,7 @@ function parseCodexToml(text: string): CodexToml {if (section === "root") {if (key === "model") result.model = value;if (key === "model_provider") result.modelProvider = value;+ if (key === "model_catalog_json") result.modelCatalogJson = value;continue;}if (section === "other") continue;@@ -380,13 +382,21 @@ export async function readCodexModelCatalog(cli?: string,): Promise<ModelCatalog> {const official = cli ? await readCodexAppServerModelCatalog(cli, env) : null;- const catalog = await catalogAround(official ?? STATIC_CODEX_MODELS, env, fetchImpl);+ const catalog = await catalogAround(official ?? STATIC_CODEX_MODELS, official !== null, env, fetchImpl);if (cli && !official) unconfirmedModelCatalog(catalog);return catalog;}+/** codex-cli 0.171.0 lists the models of `model_catalog_json` in place of OpenAI's and sends them to the configured provider. */+function catalogFileProvider(config: CodexToml): string | null {+ const provider = config.modelProvider;+ if (!config.modelCatalogJson || !provider || provider === OFFICIAL_CODEX_PROVIDER || !PROVIDER_ID.test(provider)) return null;+ return provider;+}+async function catalogAround(official: ModelCatalog,+ listedByCli: boolean,env: Record<string, string | undefined>,fetchImpl: typeof fetch,): Promise<ModelCatalog> {@@ -397,6 +407,9 @@ async function catalogAround(if (!mainText) return mergeLocalInject(official, env, fetchImpl);const main = parseCodexToml(mainText);+ const listingProvider = listedByCli ? catalogFileProvider(main) : null;+ const listed = listingProvider ? official.options.filter((option) => codexTierOf(option.id) === null) : [];+ if (listingProvider) official = { default: encodeCodexSelection(listingProvider, official.default), options: [] };const known = new Map(main.providers.map((provider) => [provider.id, provider]));const named = collectCatalogNames(home);const extras: Array<{ provider: string; model: string }> = [];@@ -413,6 +426,7 @@ async function catalogAround(};remember(main.modelProvider, main.model);+ if (listingProvider) for (const option of listed) remember(listingProvider, option.id);for (const file of listDir(home)) {if (!file.endsWith(".config.toml")) continue;