Skip to content

armgit / fuobe

Public Imported from GitHub
0

The open creative studio on ComfyUI: images, video, 3D and audio on open weights, on your own GPU or the cloud. Camera motions, effects, apps, Cinema, Layers, Image to 3D and an AI agent.

Cinema: a finished World turn stays finished when its words have stray spaces, and Redo from here holds after a reload

Claude292554790958
diff --git a/src/components/cinema/world-panel.tsx b/src/components/cinema/world-panel.tsx
index 458498a2..aa7d2721 100644
--- a/src/components/cinema/world-panel.tsx
+++ b/src/components/cinema/world-panel.tsx
@@ -103,22 +103,25 @@ export function WorldPanel(props: WorldPanelProps) {
}, [script, projectId]);
const w = script ?? newWorld(0);
- const { clips, states } = useMemo(() => clipsFromTakes(w, takes, redoAt), [w, takes, redoAt]);
- const next = nextTurn(w, clips);
- const exported = exportClips(w, clips);
- const target = next >= 0 ? next : w.turns.length;
- const input = (k: number, join?: string[]): Omit<CinemaGenerateInput, "brief"> => ({ engine, mode: "video", action: "world", prompt: "", settings, world: { script: w, turn: k, ...(join ? { join } : {}) } });
- const plan = useMemo(() => planCinema({ ...input(target), brief }, context), [w, target, engine, settings, brief, context]); // eslint-disable-line react-hooks/exhaustive-deps
+ // The script as it runs (words trimmed, lengths on the half second): what is sent, keyed and checked. `w` is what the
+ // fields show while they are edited.
+ const tidy = useMemo(() => normalizeWorld(w), [w]);
+ const { clips, states } = useMemo(() => clipsFromTakes(tidy, takes, redoAt), [tidy, takes, redoAt]);
+ const next = nextTurn(tidy, clips);
+ const exported = exportClips(tidy, clips);
+ const target = next >= 0 ? next : tidy.turns.length;
+ const input = (k: number, join?: string[]): Omit<CinemaGenerateInput, "brief"> => ({ engine, mode: "video", action: "world", prompt: "", settings, world: { script: tidy, turn: k, ...(join ? { join } : {}) } });
+ const plan = useMemo(() => planCinema({ ...input(target), brief }, context), [tidy, target, engine, settings, brief, context]); // eslint-disable-line react-hooks/exhaustive-deps
const price = demo ? "Placeholder · free" : "Free";
- const total = worldSeconds(w);
- const sketch = useMemo(() => pathSketch(allTurns(w).map((t) => ({ move: t.move, seconds: t.seconds }))), [w]);
+ const total = worldSeconds(tidy);
+ const sketch = useMemo(() => pathSketch(allTurns(tidy).map((t) => ({ move: t.move, seconds: t.seconds }))), [tidy]);
const set = (fn: (s: WorldScript) => WorldScript) => setScript((s) => fn(s ?? newWorld()));
const setTurn = (k: number, patch: Partial<WorldTurn>) =>
set((s) => (k === 0 ? { ...s, opening: { ...s.opening, ...patch } } : { ...s, turns: s.turns.map((t, i) => (i === k - 1 ? { ...t, ...patch } : t)) }));
const run = async (k: number) => {
- const problem = engine !== "open" ? "World mode runs on your GPU (open source)" : worldProblem(w, k);
+ const problem = engine !== "open" ? "World mode runs on your GPU (open source)" : worldProblem(tidy, k);
if (problem) return toast.error(problem);
setBusy(k);
try {
@@ -139,7 +142,7 @@ export function WorldPanel(props: WorldPanelProps) {
if (!exported) return;
setBusy("export");
try {
- await generate.mutateAsync({ input: input(w.turns.length, exported) as CinemaGenerateInput, folderId });
+ await generate.mutateAsync({ input: input(tidy.turns.length, exported) as CinemaGenerateInput, folderId });
toast.success("Joining the turns into one film — it lands in this scene as a new take", {
duration: 8000,
description: "Hy-Nomas makes no sound: add it afterwards.",
@@ -155,7 +158,7 @@ export function WorldPanel(props: WorldPanelProps) {
const turnCard = (t: WorldTurn, k: number) => {
const clip = clips[k];
const state = states[k];
- const runnable = canRun(w, clips, k);
+ const runnable = canRun(tidy, clips, k);
const opening = k === 0;
return (
<li key={k} className="grid gap-2 rounded-xl border p-3" style={{ borderLeft: `4px solid ${hue(k)}` }}>
@@ -237,7 +240,7 @@ export function WorldPanel(props: WorldPanelProps) {
</Button>
)}
{(clip || state === "stale") && (
- <Button size="sm" variant="outline" onClick={() => redo(k)} disabled={!canRun(w, clips, k) || busy !== null} title="Films this turn again and the ones after it; the turns before it stay. The same words and move give the same turn: change them for a different one">
+ <Button size="sm" variant="outline" onClick={() => redo(k)} disabled={!runnable || busy !== null} title="Films this turn again and the ones after it; the turns before it stay. The same words and move give the same turn: change them for a different one">
<RotateCcw className="size-3.5" /> Redo from here
</Button>
)}
@@ -277,7 +280,7 @@ export function WorldPanel(props: WorldPanelProps) {
</span>
</div>
<div className="flex h-2 overflow-hidden rounded-full bg-muted" aria-hidden>
- {allTurns(w).map((t, k) => (
+ {allTurns(tidy).map((t, k) => (
<div key={k} style={{ width: `${(t.seconds / WORLD_LIMITS.maxTotal) * 100}%`, background: hue(k), opacity: states[k] === "done" ? 1 : 0.35 }} />
))}
</div>
@@ -330,14 +333,14 @@ export function WorldPanel(props: WorldPanelProps) {
<div className="flex flex-wrap items-center gap-2">
<p className="min-w-0 flex-1 text-[12.5px]">
<span className="font-medium">{price}</span>
- <span className="text-muted-foreground"> · Hy-Nomas on your GPU · about {clipSeconds(allTurns(w)[target] ?? w.opening)} s a turn</span>
+ <span className="text-muted-foreground"> · Hy-Nomas on your GPU · about {clipSeconds(allTurns(tidy)[target] ?? tidy.opening)} s a turn</span>
{plan.blocker && <span className="block truncate text-[12px] text-muted-foreground">{plan.blocker}</span>}
</p>
<Button variant="outline" onClick={exportFilm} disabled={!exported || exported.length < 2 || busy !== null || engine !== "open"}>
{busy === "export" ? <Loader2 className="size-4 animate-spin" /> : <Film className="size-4" />} Export
</Button>
{next >= 0 && (
- <Button onClick={() => run(next)} disabled={!!plan.blocker || !canRun(w, clips, next) || busy !== null}>
+ <Button onClick={() => run(next)} disabled={!!plan.blocker || !canRun(tidy, clips, next) || busy !== null}>
{busy === next ? <Loader2 className="size-4 animate-spin" /> : <Clapperboard className="size-4" />} {next === 0 ? "Run the opening" : `Run turn ${next}`}
</Button>
)}
diff --git a/src/lib/world.ts b/src/lib/world.ts
index 2e1231cf..b207cdb0 100644
--- a/src/lib/world.ts
+++ b/src/lib/world.ts
@@ -169,10 +169,15 @@ export interface WorldClip {
made: string;
}
-/** The part of the script that turn k's clip depends on (everything up to k, and the seed). */
+/**
+ * The part of the script that turn k's clip depends on (everything up to k, and the seed), read from the script as the
+ * planner runs it (normalizeWorld): words typed with spaces round them, or a length off the half second, key the same
+ * as the take the server filed for them.
+ */
export function scriptKey(script: WorldScript, k: number): string {
- const o = script.opening;
- return JSON.stringify([script.seed, o.subject ?? "", o.details ?? "", o.perspective ?? "", o.style ?? "", allTurns(script).slice(0, k + 1).map((t) => [t.words, t.action ?? "", t.move, t.seconds])]);
+ const tidy = normalizeWorld(script);
+ const o = tidy.opening;
+ return JSON.stringify([tidy.seed, o.subject ?? "", o.details ?? "", o.perspective ?? "", o.style ?? "", allTurns(tidy).slice(0, k + 1).map((t) => [t.words, t.action ?? "", t.move, t.seconds])]);
}
/** Whether turn k's clip still matches the script (nothing up to k has changed since it was made). */
@@ -227,22 +232,26 @@ export type TurnState = "done" | "running" | "failed" | "stale" | "none";
/**
* The clips of the script's turns from the project's takes: per turn, the newest take made from the script as it is now
- * (nothing up to that turn changed since), made after the turn's last Redo from here (`redoAt[k]`, ms). A take that
- * failed or was cancelled is not a clip. Also each turn's state, for the timeline.
+ * (nothing up to that turn changed since), made after the turn's last Redo from here (`redoAt[k]`, ms), and made after
+ * the clips of the turns before it (the world continues from them, so a take older than one of them was filmed from an
+ * earlier chain: this is what keeps a Redo from here after a reload, when `redoAt` is gone). A take that failed or was
+ * cancelled is not a clip. Also each turn's state, for the timeline.
*/
export function clipsFromTakes(script: WorldScript, takes: readonly WorldTakeLike[], redoAt: Readonly<Record<number, number>> = {}): { clips: (WorldClip | undefined)[]; states: TurnState[] } {
const n = script.turns.length + 1;
const clips: (WorldClip | undefined)[] = [];
const states: TurnState[] = [];
const sorted = [...takes].filter((t) => t.take?.world && !t.take.world.joined).sort((a, b) => b.createdAt - a.createdAt);
+ let floor = 0;
for (let k = 0; k < n; k++) {
const key = scriptKey(script, k);
- const since = Math.max(0, ...Object.entries(redoAt).filter(([i]) => Number(i) <= k).map(([, at]) => at));
+ const since = Math.max(floor, ...Object.entries(redoAt).filter(([i]) => Number(i) <= k).map(([, at]) => at));
const mine = sorted.filter((t) => t.take!.world!.turn === k && t.createdAt >= since);
const current = mine.find((t) => t.take!.world!.made === key && t.status !== "failed" && t.status !== "canceled");
if (current) {
const url = current.status === "succeeded" ? current.outputs?.find((o) => !o.kind || o.kind === "video")?.url : undefined;
clips.push({ generationId: current.id, made: key, ...(url ? { url } : {}) });
+ floor = Math.max(floor, current.createdAt);
states.push(url ? "done" : current.status === "succeeded" ? "failed" : "running");
continue;
}
diff --git a/tests/e2e/cinema-world.mjs b/tests/e2e/cinema-world.mjs
index 04ed395b..95c83ef0 100644
--- a/tests/e2e/cinema-world.mjs
+++ b/tests/e2e/cinema-world.mjs
@@ -1,7 +1,8 @@
// World mode (package nm10-hy-nomas-world) in a real browser (the app must be running in demo mode).
// Usage: node tests/e2e/cinema-world.mjs [appUrl] [--shots=dir]. Exits 1 on the first failure.
//
-// 1. 1440 px, light, open engine: /cinema/<id>?world=1 opens the World panel; the opening is written, "Run the opening"
+// 1. 1440 px, light, open engine: /cinema/<id>?world=1 opens the World panel; the opening is written (with stray spaces,
+// which must not leave a finished turn looking changed), "Run the opening"
// queues a take that remembers its turn; once its clip lands a turn is added with a camera move, "Preview the path"
// draws the camera from above, turn 1 runs, and Export joins the two turns into one film. "Free" and the sound links
// show; Redo from here is offered on a finished turn.
@@ -70,8 +71,8 @@ try {
const ctx = await browser.newContext({ ...DESKTOP, colorScheme: "light" });
const page = await open(ctx, "open");
const dlg = page.getByRole("dialog", { name: "World" });
- await dlg.getByLabel("The world, in words").fill("A stone path through a luminous forest at dusk, blue fireflies between mossy trees, mist on the ground.");
- await dlg.getByLabel("Who the camera follows").fill("a small orange fox");
+ await dlg.getByLabel("The world, in words").fill("A stone path through a luminous forest at dusk, blue fireflies between mossy trees, mist on the ground. \n");
+ await dlg.getByLabel("Who the camera follows").fill(" a small orange fox ");
await dlg.getByLabel("What happens first").fill("the fox walks slowly along the path");
await dlg.getByLabel("Opening camera move").selectOption("zoom_in");
assert.match(await dlg.innerText(), /free/i);
@@ -84,9 +85,10 @@ try {
await until(async () => (await items()).find((g) => g.id === first.id && g.status === "succeeded"), "the opening's clip");
await dlg.getByText("Ready").first().waitFor({ timeout: 30_000 });
await dlg.getByRole("button", { name: "Redo from here" }).first().waitFor();
+ assert.equal(await dlg.getByText("Changed: run it again").count(), 0, "a finished turn typed with stray spaces stays finished");
await dlg.getByRole("button", { name: "Add a turn" }).click();
- await dlg.getByLabel("Turn 1: what changes").fill("a wooden bridge crosses a glowing stream ahead");
+ await dlg.getByLabel("Turn 1: what changes").fill("a wooden bridge crosses a glowing stream ahead ");
await dlg.getByLabel("Turn 1: camera move").selectOption("orbit_90_ccw");
await dlg.getByRole("button", { name: "Preview the path" }).click();
await dlg.getByRole("img", { name: /camera's path from above/ }).waitFor();
diff --git a/tests/world-mode.test.ts b/tests/world-mode.test.ts
index ea8a4635..d28dc8d1 100644
--- a/tests/world-mode.test.ts
+++ b/tests/world-mode.test.ts
@@ -25,7 +25,7 @@ const { prepareWorkflow } = await import("@/server/providers/comfyui");
const { STUDIO_ROWS } = await import("@/server/gpu/tools");
const { getModel, getPreset } = await import("@/lib/catalog");
const { planGeneration } = await import("@/lib/catalog/plan");
-const { planCinema, WORLD_OPEN_ONLY, WORLD_SOUND } = await import("@/lib/catalog/cinema");
+const { planCinema, worldTake, WORLD_OPEN_ONLY, WORLD_SOUND } = await import("@/lib/catalog/cinema");
const { NODE_PACKS } = await import("@/lib/licences");
const W = await import("@/lib/world");
const { parseScript } = await import("@/lib/world-path");
@@ -247,6 +247,34 @@ describe("the turn state", () => {
expect(again.clips.map((c) => c?.generationId)).toEqual(["a", "d", undefined]);
});
+ it("matches a take filed for the tidied script while the fields still hold spaces or an odd length", () => {
+ const typed = { ...s, opening: { ...s.opening, words: `${s.opening.words} \n`, subject: ` ${s.opening.subject ?? "a fox"} ` }, turns: [{ ...s.turns[0], words: ` ${s.turns[0].words} `, seconds: 4.3 }, s.turns[1]] };
+ const tidy = W.normalizeWorld(typed);
+ expect(tidy.turns[0].seconds).toBe(4.5);
+ // The server keys the take from the tidied script (worldTake); the browser keys the script as typed.
+ const filed = worldTake({ world: { script: typed, turn: 1 } })!;
+ const takes = [take("a", 0, worldTake({ world: { script: typed, turn: 0 } })!.made), take("b", 1, filed.made)];
+ const r = W.clipsFromTakes(typed, takes);
+ expect(r.states).toEqual(["done", "done", "none"]);
+ expect(W.nextTurn(typed, r.clips)).toBe(2);
+ expect(W.canRun(typed, r.clips, 2)).toBe(true);
+ expect(W.scriptKey(typed, 1)).toBe(W.scriptKey(tidy, 1));
+ });
+
+ it("keeps a Redo from here after a reload: a later turn's take older than the redone turn's isn't current", () => {
+ const takes = [take("a", 0, W.scriptKey(s, 0), "succeeded", 1000), take("b", 1, W.scriptKey(s, 1), "succeeded", 2000), take("c", 2, W.scriptKey(s, 2), "succeeded", 3000)];
+ expect(W.clipsFromTakes(s, takes).clips.map((c) => c?.generationId)).toEqual(["a", "b", "c"]);
+ // Turn 1 filmed again (no redoAt: the panel was closed or the page reloaded): turn 2's old take no longer counts.
+ const redone = [...takes, take("d", 1, W.scriptKey(s, 1), "running", 4000)];
+ const r = W.clipsFromTakes(s, redone);
+ expect(r.clips.map((c) => c?.generationId)).toEqual(["a", "d", undefined]);
+ expect(r.states).toEqual(["done", "running", "none"]);
+ expect(W.exportClips(s, r.clips)).toBeNull();
+ // A redo that failed leaves the earlier film standing.
+ const failed = W.clipsFromTakes(s, [...takes, take("e", 1, W.scriptKey(s, 1), "failed", 4000)]);
+ expect(failed.clips.map((c) => c?.generationId)).toEqual(["a", "b", "c"]);
+ });
+
it("exports only when every turn has a finished, current clip", () => {
const done = [0, 1, 2].map((k) => take(`t${k}`, k, W.scriptKey(s, k)));
expect(W.exportClips(s, W.clipsFromTakes(s, done).clips)).toEqual(["/api/files/t0.mp4", "/api/files/t1.mp4", "/api/files/t2.mp4"]);