Skip to content

Open source CapCut alternative.

TypeScript

feat(site): Gemini chat hands off at the step limit instead of erroring [rebuild]

David Le71012363d5fb
diff --git a/docs/guides/cut/ai-assistant.md b/docs/guides/cut/ai-assistant.md
index 4de61b62..167de29e 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> snapshot
On 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 16 rounds). Gemini's thought signatures are replayed exactly with each call, and an empty round surfaces as an error rather than a blank bubble.
+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.
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.
@@ -86,7 +86,7 @@ A thread saves one session slot per provider, so switching models mid-thread kee
| Limit | Value |
| --- | --- |
| Provider turns per request (Claude) | 30 |
-| Tool rounds per turn (Gemini) | 16 |
+| Tool rounds per turn (Gemini) | 24 |
| Browser tool execution | 120s |
| One watch-video call | 600s of source, 4 sheets (36 frames); the result says where coverage stopped |
| Snapshot caps | 60 media items, 60 cues |
diff --git a/site/src/cut/lib/geminiChat.ts b/site/src/cut/lib/geminiChat.ts
index 4a790857..d24f6ea6 100644
--- a/site/src/cut/lib/geminiChat.ts
+++ b/site/src/cut/lib/geminiChat.ts
@@ -16,7 +16,58 @@ import { refsToParts } from "./refMedia";
// and their results go back until the model settles on a reply. The whole
// conversation is replayed per turn, so no provider session is kept.
-const MAX_TOOL_ROUNDS = 16;
+const MAX_TOOL_ROUNDS = 24;
+
+const STEP_LIMIT_FALLBACK =
+ 'I got partway through and hit this turn’s step limit — say “keep going” and I’ll finish the rest.';
+
+/** The loop ran out of tool rounds. One last call — tools withheld so it can't
+ * keep looping — asks the model to hand back what it finished and what's left,
+ * in its own voice, so the turn ends on a readable summary instead of a raw
+ * error. A static line covers a failed or empty summary. */
+async function emitStepLimitSummary({
+ model,
+ input,
+ emit,
+ textId,
+ abortSignal,
+}: {
+ model: string;
+ input: Item[];
+ emit: (chunk: Record<string, unknown>) => void;
+ textId: string;
+ abortSignal?: AbortSignal;
+}): Promise<void> {
+ let text = STEP_LIMIT_FALLBACK;
+ try {
+ const summaryInput: Item[] = [
+ ...input,
+ {
+ role: "user",
+ content: [
+ {
+ text: 'You’ve reached this turn’s tool-step limit and can’t call more tools now. In one or two short sentences, in your normal voice, tell me what you got done and what still needs doing, and that I can say “keep going” to finish the rest.',
+ },
+ ],
+ },
+ ];
+ const res = await hostedPost(
+ "/api/inference/responses",
+ { donkeyProvider: "gemini", model, instructions: systemPrompt(), input: summaryInput },
+ abortSignal
+ );
+ if (res.ok) {
+ const body = (await res.json()) as ResponseBody;
+ const summary = (body.output_text ?? "").trim();
+ if (summary) text = summary;
+ }
+ } catch {
+ // A failed summary falls back to the static handoff below.
+ }
+ emit({ type: "text-start", id: textId });
+ emit({ type: "text-delta", id: textId, delta: text });
+ emit({ type: "text-end", id: textId });
+}
type Item = Record<string, unknown>;
@@ -252,8 +303,14 @@ export function streamGeminiChat({
input.push({ role: "user", content: responseParts });
}
- if (!settled) {
- emit({ type: "error", errorText: "Gemini stopped after too many tool rounds." });
+ if (!settled && !abortSignal?.aborted) {
+ await emitStepLimitSummary({
+ model,
+ input,
+ emit,
+ textId: `t${textCount + 1}`,
+ abortSignal,
+ });
}
} catch (err) {
if (!abortSignal?.aborted) {
diff --git a/site/src/cut/server/ai/catalog.ts b/site/src/cut/server/ai/catalog.ts
index 6fdc81a2..9985b340 100644
--- a/site/src/cut/server/ai/catalog.ts
+++ b/site/src/cut/server/ai/catalog.ts
@@ -697,7 +697,7 @@ export function systemPrompt(): string {
Rules:
- First decide what the user wants handed back: an edit to the project, or words in chat. "Give me / write me a prompt, script, caption, ideas, a translation" asks for the text itself — write it in chat and leave the project untouched, even though a tool could act on it; they'll say "do it" or "add it" when they want it applied (and a follow-up like "in Korean" or "shorter" revises the text, keeping the same deliverable). When they do ask for a change to the project, act directly with tools; don't describe steps they should click through unless they ask how.
-- Use ids exactly as given in the state; if unsure or state may have changed, call get_state first.
+- Use ids exactly as given in the state, and act on what your tools return — each reports what it changed, so batch independent edits into one step and call get_state only when the snapshot is stale or a result surprised you.
- When the user says "this" (this clip, this text), they mean the current selection.
- Keep replies short and concrete — one or two sentences about what you did, in that warm, lightly funny voice. You have no name and never name the app; greet with a short "How can I help?" / "What would you like to do?". No headings, no fluff.
- Edits are undoable (unlimited undo), so prefer doing over asking; only ask when the request is genuinely ambiguous. Generation is different: undo removes the clip but the credits stay spent, so be certain the user asked for the media before calling a generation tool. Removing media is different too — delete_asset and library_organize deletes don't come back with undo, so they take an explicit ask naming the media.