Skip to content

Open source CapCut alternative.

TypeScript

fix(cut): keep the Codex catalog off the models path [rebuild]

David Le40782d63f9b2
diff --git a/site/src/cut/server/ai/codexModels.test.ts b/site/src/cut/server/ai/codexModels.test.ts
index 51d53468..134b2976 100644
--- a/site/src/cut/server/ai/codexModels.test.ts
+++ b/site/src/cut/server/ai/codexModels.test.ts
@@ -1,5 +1,5 @@
import { describe, expect, test } from "bun:test";
-import { codexAccountModels } from "./codexModels";
+import { CODEX_CATALOG_TTL_MS, codexAccountModels, createCodexCatalog } from "./codexModels";
describe("codexAccountModels", () => {
test("reads the slugs from `codex debug models`", () => {
@@ -18,3 +18,37 @@ describe("codexAccountModels", () => {
expect(codexAccountModels(JSON.stringify({ models: "x" }))).toBeUndefined();
});
});
+
+describe("createCodexCatalog", () => {
+ const flush = () => new Promise((r) => setTimeout(r, 0));
+
+ test("never waits on a read and serves the last catalog while one runs", async () => {
+ let clock = 0;
+ const reads: ((models: string[] | undefined) => void)[] = [];
+ const catalog = createCodexCatalog(() => new Promise((r) => reads.push(r)), () => clock);
+
+ // The first ask starts a read and answers at once with nothing known.
+ expect(catalog.current()).toBeUndefined();
+ expect(reads.length).toBe(1);
+
+ // Asks while the read runs start no second one.
+ expect(catalog.current()).toBeUndefined();
+ expect(reads.length).toBe(1);
+
+ reads[0](["gpt-6-luna"]);
+ await flush();
+ expect(catalog.current()).toEqual(["gpt-6-luna"]);
+ expect(reads.length).toBe(1);
+
+ // A stale catalog still answers while its refresh runs.
+ clock += CODEX_CATALOG_TTL_MS;
+ expect(catalog.current()).toEqual(["gpt-6-luna"]);
+ expect(reads.length).toBe(2);
+
+ // A failed refresh keeps the last catalog.
+ reads[1](undefined);
+ await flush();
+ expect(catalog.current()).toEqual(["gpt-6-luna"]);
+ expect(reads.length).toBe(2);
+ });
+});
diff --git a/site/src/cut/server/ai/codexModels.ts b/site/src/cut/server/ai/codexModels.ts
index 5422b6e4..d50e3a1c 100644
--- a/site/src/cut/server/ai/codexModels.ts
+++ b/site/src/cut/server/ai/codexModels.ts
@@ -13,3 +13,35 @@ export function codexAccountModels(stdout: string): string[] | undefined {
return undefined;
}
}
+
+/** How long a read catalog stands before the next ask refreshes it. */
+export const CODEX_CATALOG_TTL_MS = 10 * 60_000;
+
+/** The account catalog, read in the background. `current` never waits on
+ * `codex debug models` (which can take seconds): it answers with the last
+ * catalog read and starts a refresh when that one is stale. A failed read
+ * keeps the last catalog. */
+export function createCodexCatalog(read: () => Promise<string[] | undefined>, now: () => number = Date.now) {
+ let models: string[] | undefined;
+ let readAt = -Infinity;
+ let reading = false;
+
+ return {
+ current(): string[] | undefined {
+ if (reading || now() - readAt < CODEX_CATALOG_TTL_MS) {
+ return models;
+ }
+
+ // One read at a time; the stale catalog answers until it lands.
+ reading = true;
+ void read().then((next) => {
+ if (next) {
+ models = next;
+ }
+ readAt = now();
+ reading = false;
+ });
+ return models;
+ },
+ };
+}
diff --git a/site/src/cut/server/http/ai.ts b/site/src/cut/server/http/ai.ts
index 6f7849b2..5d7e5a2f 100644
--- a/site/src/cut/server/http/ai.ts
+++ b/site/src/cut/server/http/ai.ts
@@ -41,7 +41,7 @@ import {
import { STEP_BUDGET, stopText, turnClose, type TurnEnd } from "../../lib/turnBudget";
import { pendingAsk } from "../../lib/chatResume";
import { codexCommand } from "../tool-path";
-import { codexAccountModels } from "../ai/codexModels";
+import { codexAccountModels, createCodexCatalog } from "../ai/codexModels";
import { errorMessage } from "../util";
interface ChatBody {
@@ -524,8 +524,12 @@ function codexModels(cmd: string): Promise<string[] | undefined> {
});
}
+// The account catalog refreshes behind the probes, so the picker never
+// waits on `codex debug models`.
+const codexCatalog = createCodexCatalog(async () => codexModels(await codexCommand()));
+
// Providers rarely change mid-session; cache probes for a minute.
-type ProviderStatus = { ok: boolean; note: string; installed: boolean; models?: string[] };
+type ProviderStatus = { ok: boolean; note: string; installed: boolean };
let aiProbe: {
at: number;
value: { claude: ProviderStatus; codex: ProviderStatus };
@@ -684,29 +688,31 @@ export const aiApi = {
let value = aiProbe && Date.now() - aiProbe.at < 60_000 ? aiProbe.value : null;
if (!value) {
const codexCmd = await codexCommand();
- const [claude, codexLogin, codexAccount] = await Promise.all([
+ const [claude, codexLogin] = await Promise.all([
probe("claude", ["--version"]),
probe(codexCmd, ["login", "status"]),
- codexModels(codexCmd),
]);
// The login probe running at all means codex is installed; only its
// ENOENT failure (carried through in codexLogin) marks it missing.
- // A ChatGPT login runs only the models its account catalog lists.
const codex: ProviderStatus = codexLogin.ok
? /logged in/i.test(codexLogin.note)
- ? { ok: true, note: codexLogin.note, installed: true, models: codexAccount }
+ ? { ok: true, note: codexLogin.note, installed: true }
: { ok: false, note: "Not signed in — run: codex login", installed: true }
: codexLogin;
value = { claude, codex };
aiProbe = { at: Date.now(), value };
}
+
+ // A ChatGPT login runs only the models its account catalog lists; the
+ // catalog is read only once the login probe says someone is signed in.
+ const codexAccount = value.codex.ok ? codexCatalog.current() : undefined;
// The model catalog lives in the page (src/cut/lib/aiModels.ts); the
// engine reports which provider CLIs are usable on this Mac and which
// models the Codex account runs.
return Response.json({
providers: {
claude: { available: value.claude.ok, note: value.claude.note, installed: value.claude.installed },
- codex: { available: value.codex.ok, note: value.codex.note, installed: value.codex.installed, models: value.codex.models },
+ codex: { available: value.codex.ok, note: value.codex.note, installed: value.codex.installed, models: codexAccount },
// Gemini chats run from the page through Donkey's hosted inference;
// the browser overlays the real availability from its sign-in probe.
gemini: { available: true, note: "runs on your Donkey account", installed: true },