Skip to content

Open source CapCut alternative.

TypeScript

fix(site): claim a video render is underway only after its submit is accepted

David Le8ed2efa12d0f
diff --git a/site/src/cut/lib/aiTools.ts b/site/src/cut/lib/aiTools.ts
index bd0b07db..265d55fd 100644
--- a/site/src/cut/lib/aiTools.ts
+++ b/site/src/cut/lib/aiTools.ts
@@ -631,14 +631,17 @@ export async function runAiTool(
// the render untouched — no image-model redesign in between, and no
// text-only rung (a render without the image isn't what was asked).
if (refs.length > 0 && input.animate_reference === true) {
- return {
- ...launchVideoJob(projectId, input, [
+ return launchVideoJob(
+ projectId,
+ input,
+ [
{ prompt, opts: { ...baseOpts, refs: [refs[0]], composeRefs: false } },
asReference(refs[0]),
- ]),
- note:
- "Animating the referenced image as the literal opening frame — the clip previews in this chat when it lands, in a minute or two.",
- };
+ ],
+ {
+ note: "Animating the referenced image as the literal opening frame — the clip previews in this chat when it lands, in a minute or two.",
+ }
+ );
}
// Staged, like the scene pipeline: reference work happens in the image
// model first — it holds identity far better than any video model — and
@@ -660,16 +663,19 @@ export async function runAiTool(
? useEditor.getState().assets.find((a) => a.id === still.assetId)
: undefined;
if (stillAsset) {
- return {
- ...launchVideoJob(projectId, input, [
+ return launchVideoJob(
+ projectId,
+ input,
+ [
{ prompt, opts: { ...baseOpts, refs: [refFromAsset(stillAsset)], composeRefs: false } },
asReference(refs[0]),
textOnly,
- ]),
- stillAssetId: stillAsset.id,
- note:
- "Designed the opening frame from the reference first (the image card above the render), then started the video from that exact frame — it previews in this chat when it lands, in a minute or two.",
- };
+ ],
+ {
+ extra: { stillAssetId: stillAsset.id },
+ note: "Designed the opening frame from the reference first (the image card above the render), then started the video from that exact frame — it previews in this chat when it lands, in a minute or two.",
+ }
+ );
}
return launchVideoJob(projectId, input, [
{ prompt, opts: { ...baseOpts, refs } },
@@ -736,8 +742,10 @@ export async function runAiTool(
const gen = useGenerate.getState();
const signedIn = gen.signedIn ?? (await gen.probeNow());
if (!signedIn) throw new ToolError("Sign in to Donkey to generate video.");
- return {
- ...launchVideoJob(projectId, input, [
+ return launchVideoJob(
+ projectId,
+ input,
+ [
{
prompt: characterPrompt(character.persona!, line),
opts: {
@@ -749,9 +757,9 @@ export async function runAiTool(
composeRefs: false,
},
},
- ]),
- character: character.id,
- };
+ ],
+ { extra: { character: character.id } }
+ );
}
case "generate_scene": {
@@ -1700,11 +1708,20 @@ function addVideoTrackClip(
/** Start a video render — the shared shape of generate_video and
* generate_character_video: one job (and one chat card) spans the ladder's
* fallback rungs, the landed asset files under the asking chat, and it goes
- * onto the timeline only when the model asked. Returns the tool output. */
-function launchVideoJob(
+ * onto the timeline only when the model asked.
+ *
+ * Awaits the submission, not the render: it returns started:true only once the
+ * backend actually accepted the render, so the model can say it is underway. An
+ * instant reject — bad credentials, no credits, a bad request — settles the
+ * submit in well under a second and comes back started:false with the reason,
+ * so the model reports the failure instead of narrating a clip that never
+ * started. `success.note` overrides the default "rendering" line; `success.extra`
+ * merges caller facts (the designed still, the character) into either outcome. */
+async function launchVideoJob(
projectId: string,
input: Record<string, unknown>,
- attempts: VideoAttempt[]
+ attempts: VideoAttempt[],
+ success?: { note?: string; extra?: Record<string, unknown> }
) {
const addToTimeline = wantsTimeline(input, "index");
const index = promptedIndex(input);
@@ -1713,22 +1730,36 @@ function launchVideoJob(
// creation, so neither the job row nor the landed asset ever touches the
// Video panel — including a render that errors and lands no asset.
const chatId = chatOwner();
- const { jobId } = useGenerate.getState().generateVideoLadder(projectId, attempts, {
+ const { jobId, submitted } = useGenerate.getState().generateVideoLadder(projectId, attempts, {
...(chatId ? { chatId } : {}),
onDone: (asset) => {
if (addToTimeline) addVideoTrackClip(asset.id, index);
},
});
+ const extra = success?.extra ?? {};
+ const outcome = await submitted;
+ if (!outcome.ok) {
+ return {
+ kind: "video",
+ started: false,
+ jobId,
+ ...extra,
+ error: outcome.error,
+ note: `The render didn't start — ${outcome.error} Tell the user it failed; don't say it's rendering.`,
+ };
+ }
return {
kind: "video",
started: true,
jobId,
addToTimeline,
+ ...extra,
note:
- "Rendering — it previews in this chat when it lands, in a minute or two" +
- (addToTimeline
- ? ", and goes onto the timeline."
- : ". It stays in the chat until the user places it."),
+ success?.note ??
+ ("Rendering — it previews in this chat when it lands, in a minute or two" +
+ (addToTimeline
+ ? ", and goes onto the timeline."
+ : ". It stays in the chat until the user places it.")),
};
}
diff --git a/site/src/cut/lib/generate.ts b/site/src/cut/lib/generate.ts
index ff158b63..a8d68779 100644
--- a/site/src/cut/lib/generate.ts
+++ b/site/src/cut/lib/generate.ts
@@ -62,6 +62,13 @@ export interface GenerateJob {
};
}
+/** A render's initial submission outcome, distinct from its settlement: `ok`
+ * once the backend accepts the render (it is genuinely in flight, still minutes
+ * from landing), or the reason when the submit is rejected outright — auth,
+ * credits, a bad request — before any render started. Lets a caller claim the
+ * render is underway only when it actually is, never on an instant reject. */
+export type VideoSubmitOutcome = { ok: true } | { ok: false; error: string };
+
interface GenerateState {
/** Whether a Donkey session exists; null = not probed yet. */
signedIn: boolean | null;
@@ -97,7 +104,7 @@ interface GenerateState {
projectId: string,
prompt: string,
opts?: VideoGenOptions
- ) => { jobId: string; settled: Promise<GenerateJob> };
+ ) => { jobId: string; settled: Promise<GenerateJob>; submitted: Promise<VideoSubmitOutcome> };
/** Kick off a video render that walks an identity ladder: ONE job spans the
* attempts in order, and a failed rung submits the next instead of failing
* the job — so a safety-blocked or rejected anchor degrades the render
@@ -114,7 +121,7 @@ interface GenerateState {
/** Called as each rung starts (0-based) — progress narration. */
onAttempt?: (rung: number) => void;
}
- ) => { jobId: string; settled: Promise<GenerateJob> };
+ ) => { jobId: string; settled: Promise<GenerateJob>; submitted: Promise<VideoSubmitOutcome> };
dismiss: (id: string) => void;
/** Re-attach the poll loop of every persisted running render (reload
* recovery) — called once when the app boots. Idempotent. */
@@ -601,6 +608,22 @@ export const useGenerate = create<GenerateState>((set, get) => {
set((s) => ({ jobs: [job, ...s.jobs] }));
claimJobLease(job.id); // this tab started it, this tab polls it
+ // Resolves the moment the backend accepts a rung's submit (a real render
+ // is in flight) or, if every rung is rejected before acceptance, with the
+ // reason. A caller awaits this to distinguish "rendering" from a render
+ // that never started (auth, credits, a bad request reject in well under a
+ // second) instead of claiming success on a fire-and-forget kickoff.
+ let resolveSubmitted!: (outcome: VideoSubmitOutcome) => void;
+ const submitted = new Promise<VideoSubmitOutcome>((resolve) => {
+ resolveSubmitted = resolve;
+ });
+ let submitReported = false;
+ const reportSubmit = (outcome: VideoSubmitOutcome) => {
+ if (submitReported) return;
+ submitReported = true;
+ resolveSubmitted(outcome);
+ };
+
/** Compose and submit one rung's render request; resolves the provider's
* first response (usually in_progress) or throws the readable error. */
const submitRung = async ({ prompt, opts }: VideoAttempt): Promise<GenerationResponse> => {
@@ -653,6 +676,10 @@ export const useGenerate = create<GenerateState>((set, get) => {
// know which rung (anchored or not) produced the take.
update(job.id, { rung });
const gen = await submitRung(attempt);
+ // The backend took the submit — a render is genuinely in flight
+ // now (instant rejects throw above and never reach here). A later
+ // polling failure still fails the job, but it did start.
+ reportSubmit({ ok: true });
await finishVideo(job.id, gen, jobOpts?.onDone, Date.now());
},
{
@@ -667,11 +694,16 @@ export const useGenerate = create<GenerateState>((set, get) => {
if (!outcome.ok) fail(job.id, new Error(outcome.error));
} finally {
releaseJobLease(job.id);
+ // No rung was ever accepted: the render never started, so report the
+ // submit as failed with the job's recorded reason. A no-op once an
+ // accepted rung already reported success.
+ const errored = get().jobs.find((x) => x.id === job.id);
+ reportSubmit({ ok: false, error: errored?.error ?? "Video generation failed." });
}
return settled(job.id, job);
})();
settlements.set(job.id, settledRun);
- return { jobId: job.id, settled: settledRun };
+ return { jobId: job.id, settled: settledRun, submitted };
},
resumeRunningJobs: () => {