Skip to content

Open source CapCut alternative.

TypeScript

fix(site): the page names a model role and the route resolves it

David Lea1c0d8b9918b
diff --git a/docs/guides/backend-apis.md b/docs/guides/backend-apis.md
index 562e38c5..91ee34ff 100644
--- a/docs/guides/backend-apis.md
+++ b/docs/guides/backend-apis.md
@@ -78,6 +78,13 @@ Every inference route requires the `x-donkey-client-id` header. Provider request
mapping lives behind the provider registry, so handlers import the registry and
the neutral schemas, never an individual adapter.
+A caller that ships separately from the site — the editor page, the turn
+worker — names a model role (`chat`, `review`, `fastDecision`) and the
+responses route resolves it to the registry's current id. An id baked into
+their bundles would outlive a registry bump and fail pricing the moment the
+site deploys; a role never goes stale. A lint rule keeps the ids out of the
+page and worker source.
+
**State stays on the client.** The backend can create or refresh a provider job and
hand back job IDs, generation IDs, polling URLs, and output references, but it
never persists prompts, generation records, provider output references, or
diff --git a/site/eslint.config.mjs b/site/eslint.config.mjs
index a31d9c42..d217dc62 100644
--- a/site/eslint.config.mjs
+++ b/site/eslint.config.mjs
@@ -34,6 +34,33 @@ const eslintConfig = defineConfig([
],
},
},
+ {
+ // The page, the chat loop and the turn worker ship separately from the
+ // site's routes, so a model id baked into them outlives a registry bump.
+ // They name a role and the responses route resolves it.
+ files: ["src/cut/lib/**/*.ts", "src/cut/lib/**/*.tsx", "src/cut/components/**/*.tsx", "src/cut/components/**/*.ts", "src/cut/hooks/**/*.ts", "src/cut/worker/**/*.ts"],
+ rules: {
+ "no-restricted-imports": [
+ "error",
+ {
+ paths: [
+ {
+ name: "@/lib/inference/gemini-models",
+ importNames: [
+ "geminiModels",
+ "geminiModelRoles",
+ "geminiTtsModels",
+ "geminiOmniModels",
+ "geminiTranscribeModels",
+ "geminiMusicModels",
+ ],
+ message: "Name a model role (geminiModelRoleNames); the responses route resolves the id.",
+ },
+ ],
+ },
+ ],
+ },
+ },
]);
export default eslintConfig;
diff --git a/site/src/app/api/inference/responses/route.ts b/site/src/app/api/inference/responses/route.ts
index 5d918741..f1509e28 100644
--- a/site/src/app/api/inference/responses/route.ts
+++ b/site/src/app/api/inference/responses/route.ts
@@ -12,6 +12,7 @@ import {
} from "@/lib/credits/inference";
import { createProviderRegistry } from "@/lib/inference/router";
import { resolveInferenceBlobs } from "@/lib/inference/blobs";
+import { resolveGeminiModel } from "@/lib/inference/gemini-models";
import { toJsonObject } from "@/lib/inference/json";
import {
inferenceErrorCode,
@@ -38,10 +39,13 @@ export const POST = withDonkeyAuth(async (request) => {
return validationErrorResponse(parsed.error);
}
- const requestedModel =
+ // The body names a model role or an id; a role resolves here, so the page
+ // never carries an id that a registry bump can retire under it.
+ const resolvedModel =
typeof parsed.data.body.model === "string" && parsed.data.body.model.trim()
- ? parsed.data.body.model.trim()
- : "unknown";
+ ? resolveGeminiModel(parsed.data.body.model.trim())
+ : undefined;
+ const requestedModel = resolvedModel ?? "unknown";
const bypassCredits = shouldBypassDonkeyInferenceCredits(request.donkey);
if (!bypassCredits) {
const credits = await requireInferenceCredits({
@@ -63,7 +67,10 @@ export const POST = withDonkeyAuth(async (request) => {
const data = {
...parsed.data,
body: toJsonObject(
- await resolveInferenceBlobs(parsed.data.body, request.donkey.userId),
+ await resolveInferenceBlobs(
+ resolvedModel === undefined ? parsed.data.body : { ...parsed.data.body, model: resolvedModel },
+ request.donkey.userId,
+ ),
),
};
const registry = createProviderRegistry();
diff --git a/site/src/cut/components/AiPanel.tsx b/site/src/cut/components/AiPanel.tsx
index dbceb9eb..cbf46753 100644
--- a/site/src/cut/components/AiPanel.tsx
+++ b/site/src/cut/components/AiPanel.tsx
@@ -309,6 +309,7 @@ const SUGGESTIONS = [
/** Chat provider bucket for a model id. */
const provider = (id: string): string =>
+ (AI_MODELS.find((m) => m.id === id)?.provider ?? "") ||
id.startsWith("claude")
? "claude"
: id.startsWith("gemini")
diff --git a/site/src/cut/lib/aiModels.ts b/site/src/cut/lib/aiModels.ts
index ae73d60c..c64584be 100644
--- a/site/src/cut/lib/aiModels.ts
+++ b/site/src/cut/lib/aiModels.ts
@@ -1,4 +1,4 @@
-import { geminiModels } from "@/lib/inference/gemini-models";
+import { geminiModelRoleNames } from "@/lib/inference/gemini-models";
export interface AiModel {
id: string;
@@ -22,7 +22,7 @@ export const AI_MODELS: AiModel[] = [
{ id: "gpt-5.6-luna", label: "GPT-5.6 Luna", provider: "codex" },
{ id: "gpt-5.5", label: "GPT-5.5", provider: "codex" },
{ id: "gpt-5.4-mini", label: "GPT-5.4 Mini", provider: "codex" },
- { id: geminiModels.flash, label: "Gemini Flash", provider: "gemini" },
+ { id: geminiModelRoleNames.chat, label: "Gemini Flash", provider: "gemini" },
// Hermetic test provider for e2e runs — hidden unless enabled in the UI.
{ id: "cut-test", label: "Test model", provider: "test", hidden: true },
];
diff --git a/site/src/cut/lib/composeGen.ts b/site/src/cut/lib/composeGen.ts
index a4ba14d2..2e316c9f 100644
--- a/site/src/cut/lib/composeGen.ts
+++ b/site/src/cut/lib/composeGen.ts
@@ -1,6 +1,6 @@
"use client";
-import { geminiModelRoles } from "@/lib/inference/gemini-models";
+import { geminiModelRoleNames } from "@/lib/inference/gemini-models";
import type { AssetRef } from "./assetRef";
import { hostedPost } from "./hosted";
import { readRefText, refsToParts, type InlineImage } from "./refMedia";
@@ -55,7 +55,7 @@ export async function composeGenPrompt(
if (parts.length === 0) return null;
const res = await hostedPost("/api/inference/responses", {
donkeyProvider: "gemini",
- model: geminiModelRoles.chat,
+ model: geminiModelRoleNames.chat,
instructions: target === "video" ? VIDEO_INSTRUCTIONS : IMAGE_INSTRUCTIONS,
response_format: { type: "json_object" },
input: [{ role: "user", content: [...parts, { text: `Request: ${prompt}` }] }],
@@ -92,7 +92,7 @@ export async function composeMusicPrompt(
if (parts.length === 0) return null;
const res = await hostedPost("/api/inference/responses", {
donkeyProvider: "gemini",
- model: geminiModelRoles.chat,
+ model: geminiModelRoleNames.chat,
instructions: MUSIC_INSTRUCTIONS,
response_format: { type: "json_object" },
input: [{ role: "user", content: [...parts, { text: `Request: ${prompt}` }] }],
diff --git a/site/src/cut/lib/cutout.ts b/site/src/cut/lib/cutout.ts
index 1b8bee96..9295faed 100644
--- a/site/src/cut/lib/cutout.ts
+++ b/site/src/cut/lib/cutout.ts
@@ -22,7 +22,7 @@ import {
} from "@donkeycut/effects-kit";
import { hostedPost } from "./hosted";
import { decodeRasterImage, rasterCanvasToDataUrl, rasterCanvasToPng } from "./raster";
-import { geminiModelRoles } from "@/lib/inference/gemini-models";
+import { geminiModelRoleNames } from "@/lib/inference/gemini-models";
const WASM_BASE = "/mediapipe/wasm";
const PERSON_MODEL = "/mediapipe/selfie_segmenter.tflite";
@@ -197,7 +197,7 @@ export async function hostedCutout(blob: Blob): Promise<HTMLCanvasElement | null
const dataUrl = await rasterCanvasToDataUrl(small, "image/jpeg", 0.85);
const res = await hostedPost("/api/inference/responses", {
donkeyProvider: "gemini",
- model: geminiModelRoles.chat,
+ model: geminiModelRoleNames.chat,
instructions:
"You segment images. Output a JSON object {\"masks\": [...]} where each entry has \"box_2d\" ([y0, x0, y1, x1], normalized 0-1000), \"mask\" (a data: URI PNG probability mask for that box), and \"label\". Segment only the single main foreground subject.",
response_format: { type: "json_object" },
diff --git a/site/src/cut/lib/genvideo/adapters/review.ts b/site/src/cut/lib/genvideo/adapters/review.ts
index 4fa8a446..2b61f52d 100644
--- a/site/src/cut/lib/genvideo/adapters/review.ts
+++ b/site/src/cut/lib/genvideo/adapters/review.ts
@@ -12,7 +12,7 @@
* structured call.
*/
-import { geminiModelRoles } from "@/lib/inference/gemini-models";
+import { geminiModelRoleNames } from "@/lib/inference/gemini-models";
import { hostedPost } from "../../hosted";
import { captureVideoFrames } from "../../visualFrames";
import { findRunAsset } from "../docWriter";
@@ -97,7 +97,7 @@ The benchmark sheet — the artist to match:`,
];
const res = await hostedPost("/api/inference/responses", {
donkeyProvider: "gemini",
- model: geminiModelRoles.review,
+ model: geminiModelRoleNames.review,
instructions: FRAME_INSTRUCTIONS,
response_format: { type: "json_object" },
input: [{ role: "user", content }],
@@ -141,7 +141,7 @@ The look: ${input.style || "(none)"}`,
if (seen.length < 2) return allOk();
const res = await hostedPost("/api/inference/responses", {
donkeyProvider: "gemini",
- model: geminiModelRoles.review,
+ model: geminiModelRoleNames.review,
instructions: STORYBOARD_INSTRUCTIONS,
response_format: { type: "json_object" },
input: [{ role: "user", content }],
@@ -220,7 +220,7 @@ The timeline slot needs ${input.slotSec.toFixed(1)}s of this take.`,
}
const res = await hostedPost("/api/inference/responses", {
donkeyProvider: "gemini",
- model: geminiModelRoles.review,
+ model: geminiModelRoleNames.review,
instructions: REVIEW_INSTRUCTIONS,
response_format: { type: "json_object" },
input: [{ role: "user", content }],
@@ -276,7 +276,7 @@ The canonical design sheet for "${sheet.name}":`,
}
const idRes = await hostedPost("/api/inference/responses", {
donkeyProvider: "gemini",
- model: geminiModelRoles.review,
+ model: geminiModelRoleNames.review,
instructions: IDENTITY_INSTRUCTIONS,
response_format: { type: "json_object" },
input: [{ role: "user", content: idContent }],
diff --git a/site/src/cut/lib/genvideo/adapters/text.ts b/site/src/cut/lib/genvideo/adapters/text.ts
index 1a3c5dc6..1915d44b 100644
--- a/site/src/cut/lib/genvideo/adapters/text.ts
+++ b/site/src/cut/lib/genvideo/adapters/text.ts
@@ -14,7 +14,7 @@
* construction, not by hoping two independent calls agree.
*/
-import { geminiModelRoles } from "@/lib/inference/gemini-models";
+import { geminiModelRoleNames } from "@/lib/inference/gemini-models";
import { hostedPost } from "../../hosted";
import { NO_CREDITS_MESSAGE } from "../../credits";
import { secToFrame, type RawShot } from "../coverage";
@@ -46,7 +46,7 @@ async function llmJson<T>(
try {
res = await hostedPost("/api/inference/responses", {
donkeyProvider: "gemini",
- model: geminiModelRoles.chat,
+ model: geminiModelRoleNames.chat,
instructions,
response_format: { type: "json_object" },
input: [
diff --git a/site/src/cut/lib/pi/prodDeps.ts b/site/src/cut/lib/pi/prodDeps.ts
index 129d67b3..5faa6bd7 100644
--- a/site/src/cut/lib/pi/prodDeps.ts
+++ b/site/src/cut/lib/pi/prodDeps.ts
@@ -1,6 +1,6 @@
"use client";
-import { geminiModelRoles } from "@/lib/inference/gemini-models";
+import { geminiModelRoleNames } from "@/lib/inference/gemini-models";
import { AI_SKILL_INDEX, AI_SKILLS } from "@/cut/server/ai/catalog";
import { buildAiContext } from "../aiContext";
import { runAiTool } from "../aiTools";
@@ -30,9 +30,9 @@ export function productionDeps(): CutAgentDeps {
return runAiTool(name, args);
},
models: {
- simple: geminiModelRoles.chatSimple,
- complex: geminiModelRoles.chat,
- gate: geminiModelRoles.fastDecision,
+ simple: geminiModelRoleNames.chatSimple,
+ complex: geminiModelRoleNames.chat,
+ gate: geminiModelRoleNames.fastDecision,
},
buildContext: () => buildAiContext(),
resolveRefs: async (meta) => {
diff --git a/site/src/cut/lib/pi/serverDeps.ts b/site/src/cut/lib/pi/serverDeps.ts
index 0987dbc8..b664c171 100644
--- a/site/src/cut/lib/pi/serverDeps.ts
+++ b/site/src/cut/lib/pi/serverDeps.ts
@@ -1,4 +1,4 @@
-import { geminiModelRoles } from "@/lib/inference/gemini-models";
+import { geminiModelRoleNames } from "@/lib/inference/gemini-models";
import { AI_SKILL_INDEX, AI_SKILLS } from "@/cut/server/ai/catalog";
import { buildAiContext } from "../aiContext";
import { MEDIA_RUNTIME_TOOLS, runAiTool, UI_TOOLS } from "../aiTools";
@@ -45,9 +45,9 @@ export function headlessDeps(session: HeadlessSession): CutAgentDeps {
return runAiTool(name, args);
},
models: {
- simple: geminiModelRoles.chatSimple,
- complex: geminiModelRoles.chat,
- gate: geminiModelRoles.fastDecision,
+ simple: geminiModelRoleNames.chatSimple,
+ complex: geminiModelRoleNames.chat,
+ gate: geminiModelRoleNames.fastDecision,
},
buildContext: () => buildAiContext(),
resolveRefs: async (meta) => {
diff --git a/site/src/cut/lib/tts.ts b/site/src/cut/lib/tts.ts
index ed0a1451..6ff45623 100644
--- a/site/src/cut/lib/tts.ts
+++ b/site/src/cut/lib/tts.ts
@@ -1,6 +1,6 @@
"use client";
-import { geminiModels } from "@/lib/inference/gemini-models";
+import { geminiModelRoleNames } from "@/lib/inference/gemini-models";
import { bytesFromBase64 } from "./bytes";
import { NO_CREDITS_MESSAGE } from "./credits";
import { hostedPost } from "./hosted";
@@ -135,7 +135,7 @@ export async function planVoiceover(
try {
const res = await hostedPost("/api/inference/responses", {
donkeyProvider: "gemini",
- model: geminiModels.flash,
+ model: geminiModelRoleNames.chat,
instructions: PLAN_INSTRUCTIONS,
input: [{ role: "user", content: [{ text: JSON.stringify({ direction, lines: texts }) }] }],
});
diff --git a/site/src/cut/worker/turnJob.ts b/site/src/cut/worker/turnJob.ts
index 3be6afd7..95d6d443 100644
--- a/site/src/cut/worker/turnJob.ts
+++ b/site/src/cut/worker/turnJob.ts
@@ -1,5 +1,5 @@
import type { UIMessage } from "ai";
-import { geminiModelRoles } from "@/lib/inference/gemini-models";
+import { geminiModelRoleNames } from "@/lib/inference/gemini-models";
import { openCloudProject, pushCloudProject } from "../lib/headless/docSession";
import { dropPiSession, streamCutChat } from "../lib/pi/cutAgent";
import { headlessDeps, type HeadlessSession } from "../lib/pi/serverDeps";
@@ -131,7 +131,7 @@ export async function runTurnJob(
}, 1000);
const reader = streamCutChat({
threadId: piKey,
- model: spec.model ?? geminiModelRoles.chat,
+ model: spec.model ?? geminiModelRoleNames.chat,
messages: spec.messages as UIMessage[],
abortSignal: abort.signal,
deps,
diff --git a/site/src/lib/inference/gemini-models.test.ts b/site/src/lib/inference/gemini-models.test.ts
new file mode 100644
index 00000000..587e43e1
--- /dev/null
+++ b/site/src/lib/inference/gemini-models.test.ts
@@ -0,0 +1,20 @@
+import { describe, expect, test } from "bun:test";
+import { geminiModelRoleNames, geminiModelRoles, resolveGeminiModel } from "./gemini-models";
+
+// The page names a role; the route turns it into the registry's current id.
+describe("model roles", () => {
+ test("every role name resolves to its registry id", () => {
+ for (const role of Object.values(geminiModelRoleNames)) {
+ expect(resolveGeminiModel(role)).toBe(geminiModelRoles[role]);
+ }
+ });
+
+ test("an id passes through unchanged", () => {
+ expect(resolveGeminiModel(geminiModelRoles.chat)).toBe(geminiModelRoles.chat);
+ expect(resolveGeminiModel("gemini-3.7-flash")).toBe("gemini-3.7-flash");
+ });
+
+ test("an object prototype key is not a role", () => {
+ expect(resolveGeminiModel("toString")).toBe("toString");
+ });
+});
diff --git a/site/src/lib/inference/gemini-models.ts b/site/src/lib/inference/gemini-models.ts
index 238d513d..5fa343cd 100644
--- a/site/src/lib/inference/gemini-models.ts
+++ b/site/src/lib/inference/gemini-models.ts
@@ -116,3 +116,19 @@ export const geminiModelRoles = {
// Hosted speech-to-text: the recorded mix in, word-timed cues out.
transcription: geminiTranscribeModels.file,
} as const;
+
+/** The role names, for a caller that says what it needs and lets the server
+ * pick the id. The page and the turn worker ship separately from the site, so
+ * an id baked into their bundles outlives a registry bump; a role name never
+ * goes stale. */
+export type GeminiModelRole = keyof typeof geminiModelRoles;
+export const geminiModelRoleNames = Object.fromEntries(
+ Object.keys(geminiModelRoles).map((role) => [role, role]),
+) as { [K in GeminiModelRole]: K };
+
+/** A role name becomes its registry id; anything else is an id already. */
+export function resolveGeminiModel(value: string): string {
+ return Object.hasOwn(geminiModelRoles, value)
+ ? geminiModelRoles[value as GeminiModelRole]
+ : value;
+}