Skip to content

Open source CapCut alternative.

TypeScript

feat(site): chat grounds render claims in the live job store and waits via wait_for_renders [rebuild]

David Le2673e5113329
diff --git a/site/src/cut/lib/aiContext.ts b/site/src/cut/lib/aiContext.ts
index 01d59628..d86dc77a 100644
--- a/site/src/cut/lib/aiContext.ts
+++ b/site/src/cut/lib/aiContext.ts
@@ -1,5 +1,6 @@
"use client";
+import { useGenerate } from "./generate";
import { getClipSpans, overlayLayers, totalDuration, useEditor } from "./store";
import { laneCues, subtitleLaneCount } from "./subtitles";
import { isCrossStyle, rectOf, regionLabel, type ClipSpan, type VideoClip } from "./types";
@@ -107,6 +108,23 @@ export function buildAiContext(opts?: { fullCues?: boolean }) {
...(a.origin ? { origin: a.origin } : {}),
})),
mediaTruncated: s.assets.length > cueCap,
+ // AI video renders for this project, live from the job store — what
+ // "rendering" claims must be grounded in. A done render names the asset
+ // it landed as (already in `media`); a failed one carries its error.
+ renders: useGenerate
+ .getState()
+ .jobs.filter((j) => j.kind === "video" && j.projectId === s.projectId)
+ .slice(0, 8)
+ .map((j) => ({
+ jobId: j.id,
+ prompt: j.prompt.length > 80 ? `${j.prompt.slice(0, 77)}…` : j.prompt,
+ status: j.status,
+ ...(j.status === "running"
+ ? { elapsedSec: Math.round((Date.now() - j.startedAt) / 1000) }
+ : {}),
+ ...(j.status === "done" && j.assetId ? { assetId: j.assetId } : {}),
+ ...(j.status === "error" && j.error ? { error: j.error } : {}),
+ })),
videoTrack: spans.map((sp, index) => ({
index,
id: sp.clip.id,
diff --git a/site/src/cut/lib/aiTools.ts b/site/src/cut/lib/aiTools.ts
index f789812d..9faff134 100644
--- a/site/src/cut/lib/aiTools.ts
+++ b/site/src/cut/lib/aiTools.ts
@@ -681,6 +681,49 @@ export async function runAiTool(
return launchVideoJob(projectId, input, [{ prompt, opts: { ...baseOpts } }]);
}
+ case "wait_for_renders": {
+ const projectId = s.projectId;
+ if (!projectId) throw new ToolError("No project open.");
+ const watched = useGenerate
+ .getState()
+ .jobs.filter(
+ (j) => j.kind === "video" && j.status === "running" && j.projectId === projectId
+ )
+ .map((j) => j.id);
+ if (watched.length === 0) {
+ return { renders: [], note: "No renders in flight — check `renders` in the state for recent outcomes." };
+ }
+ // Poll the job store rather than the settlement promises: it also tracks
+ // renders another tab owns. Stay under the tool bridge's 2-minute cap;
+ // whatever hasn't settled reports as still rendering.
+ const deadline = Date.now() + 100_000;
+ while (Date.now() < deadline) {
+ const jobs = useGenerate.getState().jobs;
+ if (!watched.some((id) => jobs.find((j) => j.id === id)?.status === "running")) break;
+ await new Promise((r) => setTimeout(r, 1000));
+ }
+ const jobs = useGenerate.getState().jobs;
+ const cur = useEditor.getState();
+ const renders = watched.map((id) => {
+ const j = jobs.find((x) => x.id === id);
+ if (!j) return { jobId: id, status: "dismissed" as const };
+ const asset = j.assetId ? cur.assets.find((a) => a.id === j.assetId) : undefined;
+ return {
+ jobId: id,
+ status: j.status,
+ ...(asset ? { assetId: asset.id, name: asset.name, duration: asset.duration } : {}),
+ ...(j.status === "error" && j.error ? { error: j.error } : {}),
+ };
+ });
+ const stillRunning = renders.filter((x) => x.status === "running").length;
+ return {
+ renders,
+ ...(stillRunning > 0
+ ? { note: `${stillRunning} still rendering — call wait_for_renders again to keep waiting.` }
+ : {}),
+ };
+ }
+
case "generate_character_video": {
const projectId = s.projectId;
if (!projectId) throw new ToolError("No project open.");
diff --git a/site/src/cut/server/ai/catalog.ts b/site/src/cut/server/ai/catalog.ts
index f31950c2..7f5c52e5 100644
--- a/site/src/cut/server/ai/catalog.ts
+++ b/site/src/cut/server/ai/catalog.ts
@@ -308,6 +308,12 @@ export const AI_TOOLS: AiToolDef[] = [
index: num("Insert position on the video track (passing it implies add_to_timeline)"),
}, ["character_id", "line"]),
},
+ {
+ name: "wait_for_renders",
+ description:
+ "Block until this project's in-flight video renders settle (up to ~100s), then report each one: landed (with its asset id, ready to place) or failed (with the error). Call it whenever the user's ask depends on a render that `renders` in the state shows as running — \"add it when it's done\", \"assemble the clips\" — and then finish the job in the same turn; never tell the user to come back and report when a card appears. If some renders are still running when it returns, say how long they've been going and call it again on the user's go-ahead.",
+ inputSchema: obj({}),
+ },
{
name: "generate_scene",
description:
@@ -731,6 +737,7 @@ Stock: stock_search browses the bundled catalogs — footage clips and images in
Generation:
- generate_image(prompt, aspect?, resolution?, reference_asset_ids?, add_to_timeline?, index?): the hosted image model renders the prompt at 16:9, 9:16, or 1:1 (default: project aspect) and 1K/2K/4K. The still previews in the chat; placed (add_to_timeline:true or an index) it rides video track 0 as a still clip (8s default, stretchable). Great for a cover/hook frame (index 0), a background, or a b-roll still.
- generate_video(prompt, aspect?, reference_asset_id?, add_to_timeline?, index?): the hosted video model renders a short clip with audio in one pass — 720p, up to ~10s, the model picks the length (no duration knob; the user trims the clip on the timeline). With reference_asset_id it stages: the image model designs the opening frame from the reference first (that still previews in the chat), then the video model animates that exact frame. The render takes a minute or two, so the tool returns once it's started and the clip previews in the chat when it finishes (on the timeline only when asked) — tell the user it's rendering. Don't call it again for the same shot while one is in flight.
+- Every render's live status is \`renders\` in editor_state (running with elapsed seconds, done with its asset id, failed with the error) — report render progress from there, never from memory. When the user's ask depends on a running render ("assemble the clips", "add it when it's done"), call wait_for_renders and finish the job in the same turn — never hand the waiting to the user.
- generate_character_video(character_id, line, …): a stock talking character delivers a line to camera, same async render as generate_video. Characters come from stock_search kind:"character" — each has a persona; you write the line (chat deliverable rules apply: asked for "a script", write it in chat first).
References: users attach images/clips to their message or the generate panels; project asset ids (see \`media\` in editor_state, including attachments — OS drops become project assets) pass through reference_asset_ids / reference_asset_id. Images draw from any number of references; video stages one reference into a designed opening frame and animates it. When the user says "use this clip/image", pass the reference — don't just describe it in the prompt.
If generation fails with a sign-in or credits message, relay that plainly — generation needs a Donkey sign-in with credits. Write vivid, specific prompts (subject, style, lighting, motion); the user's request is usually shorthand, so flesh it out. A narrated multi-shot production is generate_scene — read the scene-productions skill before planning one, especially from an audio file or in a named style.`,