Open source CapCut alternative.
TypeScript
feat(site): route chat turns by complexity
David Leaec42bec61c1
diff --git a/docs/guides/cut/ai-assistant.md b/docs/guides/cut/ai-assistant.mdindex bbe58675..1ba48f31 100644--- a/docs/guides/cut/ai-assistant.md+++ b/docs/guides/cut/ai-assistant.md@@ -27,7 +27,7 @@ user message + @attachments + fresh <editor_state> snapshotOn the engine path, the chat route holds one streaming response open per turn. The provider sees a single MCP server named `cut`: a small stdio proxy the provider harness spawns, which forwards every tool listing and call to the engine over HTTP. The engine writes each call into the open chat stream; the tab executes it on the editor store and posts the result back; the engine hands it to the waiting provider. A call the tab never answers times out after two minutes.-The Gemini path skips the engine entirely — the same carve-out as AI media generation. The page posts the conversation to Donkey's hosted responses route with the user's session, executes any function calls directly against the store, and repeats until the model settles on a reply (at most 24 rounds). Gemini's thought signatures are replayed exactly with each call, and an empty round surfaces as an error rather than a blank bubble. A turn that exhausts the round budget ends with one tools-off call that summarizes what landed and what's left, so a long edit hands off cleanly ("say keep going") instead of dead-ending on a raw error.+The Gemini path skips the engine entirely — the same carve-out as AI media generation. Before the first round, one light-model call judges the newest message three ways and the harness acts on the verdict: a turn that requests nothing runs with every tool declaration withheld, one self-contained ask runs the loop on the light chat model, and a composed job — or any classifier doubt — runs on the full one. The page then posts the conversation to Donkey's hosted responses route with the user's session, executes any function calls directly against the store, and repeats until the model settles on a reply (at most 24 rounds). Gemini's thought signatures are replayed exactly with each call, and an empty round surfaces as an error rather than a blank bubble. A turn that exhausts the round budget ends with one tools-off call that summarizes what landed and what's left, so a long edit hands off cleanly ("say keep going") instead of dead-ending on a raw error.Which path runs is chosen by the model id alone. The picker lives in the page so a site deploy updates it for everyone; the engine only reports which CLIs are installed and signed in (probed and cached for a minute), and the page overlays Gemini availability from its own sign-in probe.diff --git a/site/src/cut/lib/geminiChat.ts b/site/src/cut/lib/geminiChat.tsindex 14b89108..0288653e 100644--- a/site/src/cut/lib/geminiChat.ts+++ b/site/src/cut/lib/geminiChat.ts@@ -176,18 +176,20 @@ async function emitStepLimitSummary({type Item = Record<string, unknown>;-/** The turn's tool gate: judge the newest message before the first round, and- * withhold every tool declaration when it asks for nothing. A message carrying- * attachments is work by construction (the user brought media to act on), so- * it skips the model call. Single attempt, fails open to "work" — a classifier- * hiccup must never block a real request. */+/** The turn's tool gate and router: judge the newest message before the first+ * round — a "chat" verdict withholds every tool declaration, a "simple"+ * verdict runs the turn on the light chat model, "complex" keeps the full+ * model. A message carrying attachments is complex by construction (the user+ * brought media to act on), so it skips the model call. Single attempt, fails+ * open to "complex" — a classifier hiccup must never block or downgrade a+ * real request. */async function classifyTurnIntent(messages: UIMessage[],abortSignal?: AbortSignal): Promise<TurnIntent> {const lastUser = messages.findLast((m) => m.role === "user");const attached = (lastUser?.metadata as { attachments?: unknown[] } | undefined)?.attachments;- if (Array.isArray(attached) && attached.length > 0) return "work";+ if (Array.isArray(attached) && attached.length > 0) return "complex";const turns = messages.map((m) => ({role: m.role === "user" ? ("user" as const) : ("assistant" as const),text: m.parts@@ -206,11 +208,11 @@ async function classifyTurnIntent(},abortSignal);- if (!res.ok) return "work";+ if (!res.ok) return "complex";const body = (await res.json()) as ResponseBody;return parseTurnIntent(body.output_text);} catch {- return "work";+ return "complex";}}@@ -401,10 +403,14 @@ export function streamGeminiChat({try {// The gate classifies while the input assembles; a "chat" verdict// (nothing asked) drops the tool declarations from the whole turn, so- // the model can only answer in words.+ // the model can only answer in words. The verdict also routes the+ // turn's model: one self-contained ask runs on the light chat model,+ // and a composed job — or any doubt — stays on the picked model.const intentPromise = classifyTurnIntent(messages, abortSignal);const input = await inputFromMessages(messages);- const tools = (await intentPromise) === "work" ? toolDeclarations() : undefined;+ const intent = await intentPromise;+ const tools = intent === "chat" ? undefined : toolDeclarations();+ const roundModel = intent === "simple" ? geminiModelRoles.chatSimple : model;let textCount = 0;let settled = false;let emptyRounds = 0;@@ -425,7 +431,7 @@ export function streamGeminiChat({let body: ResponseBody;try {body = await postRound(- { donkeyProvider: "gemini", model, instructions: systemPrompt(), input, ...(tools ? { tools } : {}) },+ { donkeyProvider: "gemini", model: roundModel, instructions: systemPrompt(), input, ...(tools ? { tools } : {}) },abortSignal,(delta) => {if (!textId) {@@ -532,7 +538,7 @@ export function streamGeminiChat({if (!settled && !abortSignal?.aborted) {await emitStepLimitSummary({- model,+ model: roundModel,input,emit,textId: `t${textCount + 1}`,diff --git a/site/src/cut/lib/turnIntent.ts b/site/src/cut/lib/turnIntent.tsindex 867ce709..ec3cfd23 100644--- a/site/src/cut/lib/turnIntent.ts+++ b/site/src/cut/lib/turnIntent.ts@@ -1,21 +1,22 @@// The chat harness's understanding boundary for one composer turn: a fast-// structured model call judges whether the newest user message asks the-// assistant for anything, and the harness gates deterministically on the-// verdict. A "chat" turn (greeting, thanks — social filler that requests-// nothing) runs with no tool declarations at all, so unsolicited edits are-// impossible by construction rather than discouraged by prompt.+// structured model call judges the newest user message — does it ask for+// anything, and how big is the job — and the harness gates and routes+// deterministically on the verdict. A "chat" turn (greeting, thanks — social+// filler that requests nothing) runs with no tool declarations at all, so+// unsolicited edits are impossible by construction. A "simple" turn (one+// self-contained ask) runs on the light chat model; a "complex" turn (a+// composed job) runs on the full one.-export type TurnIntent = "work" | "chat";+export type TurnIntent = "chat" | "simple" | "complex";-export const TURN_INTENT_PROMPT = `You gate the tools of an AI assistant built into a video editor. Read the conversation and judge only its newest user message: does it ask the assistant for anything — an edit, an answer, media, information, an opinion, or any other work — or is it pure social filler (a greeting, thanks, a sign-off, an acknowledgement) that requests nothing?--Terse follow-ups ("yes", "do it", "the second one") refer to the earlier turns and DO ask for something.+export const TURN_INTENT_PROMPT = `You gate and route the tools of an AI assistant built into a video editor. Read the conversation and judge only its newest user message.Reply with exactly one word:-work — the message asks for something, however vague or implicit.-chat — the message requests nothing.+chat — pure social filler (a greeting, thanks, a sign-off, an acknowledgement) that requests nothing.+simple — one self-contained ask: a single edit, a single generation, or a question to answer. Terse follow-ups ("yes", "do it", "the second one") that confirm one pending action are simple.+complex — a composed job: several edits across the timeline, cutting or reorganizing many clips, assembling media into a cut, or anything needing a plan across steps.-If unsure, reply work.`;+If unsure between chat and the others, reply simple. If unsure between simple and complex, reply complex.`;/** The conversation as plain text turns for the classifier — recent turns ride* along so follow-ups keep their referent, and each is capped so a pasted wall@@ -29,8 +30,13 @@ export function turnIntentInput(.map((t) => ({ role: t.role, content: [{ text: t.text.slice(0, 2000) }] }));}-/** Deterministic read of the classifier's one-word verdict; anything but a- * clear "chat" counts as work, so a garbled reply never blocks a request. */+/** Deterministic read of the classifier's one-word verdict. Only a clear+ * "chat" withholds tools, and only a clear "simple" takes the light model —+ * a garbled reply runs the full model with tools, so a classifier hiccup+ * never blocks or downgrades a real request. */export function parseTurnIntent(outputText: string | undefined): TurnIntent {- return /^\W*chat\b/i.test((outputText ?? "").trim()) ? "chat" : "work";+ const t = (outputText ?? "").trim();+ if (/^\W*chat\b/i.test(t)) return "chat";+ if (/^\W*simple\b/i.test(t)) return "simple";+ return "complex";}diff --git a/site/src/lib/inference/gemini-models.ts b/site/src/lib/inference/gemini-models.tsindex 4bf611f1..2578405a 100644--- a/site/src/lib/inference/gemini-models.ts+++ b/site/src/lib/inference/gemini-models.ts@@ -62,6 +62,9 @@ export type GeminiMusicModel = (typeof geminiMusicModels)[keyof typeof geminiMusexport const geminiModelRoles = {// General chat and non-decision Responses calls — the latest full flash.chat: geminiModels.flash,+ // Chat turns the gate judges simple — one self-contained ask runs on the+ // light chat model; complex turns stay on `chat`.+ chatSimple: geminiModels.flashLite,// Fast structured task-intent and follow-up decisions.fastDecision: geminiModels.flashLite,// Computer Use tool calls (browser and macOS desktop environments). Computer