Skip to content

AI media harness for planning, generating, and editing locally

TypeScript

Add scene ending frames and continuity

Bittib9ad09e3ae81
diff --git a/src-tauri/src/agent.rs b/src-tauri/src/agent.rs
index af58a98..75e92b3 100644
--- a/src-tauri/src/agent.rs
+++ b/src-tauri/src/agent.rs
@@ -40,8 +40,8 @@ For edits, return JSONL only: one compact JSON object per line, followed by one
- {"op":"ref.add","id":string,"name":string,"text":string,"use":["character"|"animal"|"product"|"location"|"style"|"audio",...]}
- {"op":"ref.set","id":string,"name"?:string,"text"?:string,"use"?:string[]}
- {"op":"ref.remove","id":string}; update every scene that uses it first
-- {"op":"scene.add","id":string,"title":string,"seconds":number,"steps"?:integer,"seed"?:integer,"sound"?:string,"music"?:string,"startFrame"?:reference-id}
-- {"op":"scene.set","id":string,"title"?:string,"seconds"?:number,"steps"?:integer,"seed"?:integer,"sound"?:string|null,"music"?:string|null,"startFrame"?:reference-id|null}
+- {"op":"scene.add","id":string,"title":string,"seconds":number,"steps"?:integer,"seed"?:integer,"sound"?:string,"music"?:string,"startFrame"?:reference-id,"endFrame"?:reference-id,"usePreviousSceneLastFrame"?:boolean}
+- {"op":"scene.set","id":string,"title"?:string,"seconds"?:number,"steps"?:integer,"seed"?:integer,"sound"?:string|null,"music"?:string|null,"startFrame"?:reference-id|null,"endFrame"?:reference-id|null,"usePreviousSceneLastFrame"?:boolean}
- {"op":"scene.remove","id":string}
- {"op":"scene.move","id":string,"before":scene-id|null}; null moves it to the end
- {"op":"shot.add","scene":scene-id,"id":string,"at":number,"action":string,"name"?:string,"speech"?:string,"language"?:string,"settings"?:object}
diff --git a/src-tauri/src/agent_commands.rs b/src-tauri/src/agent_commands.rs
index d71c210..6fb1ad6 100644
--- a/src-tauri/src/agent_commands.rs
+++ b/src-tauri/src/agent_commands.rs
@@ -86,6 +86,14 @@ pub enum ProjectCommand {
skip_serializing_if = "Option::is_none"
)]
start_frame_reference_id: Option<String>,
+ #[serde(default, rename = "endFrame", skip_serializing_if = "Option::is_none")]
+ end_frame_reference_id: Option<String>,
+ #[serde(
+ default,
+ rename = "usePreviousSceneLastFrame",
+ skip_serializing_if = "Option::is_none"
+ )]
+ use_previous_scene_last_frame: Option<bool>,
},
#[serde(rename = "scene.set")]
SceneSet {
@@ -117,6 +125,19 @@ pub enum ProjectCommand {
deserialize_with = "deserialize_nullable_patch"
)]
start_frame_reference_id: Option<Option<String>>,
+ #[serde(
+ default,
+ rename = "endFrame",
+ skip_serializing_if = "Option::is_none",
+ deserialize_with = "deserialize_nullable_patch"
+ )]
+ end_frame_reference_id: Option<Option<String>>,
+ #[serde(
+ default,
+ rename = "usePreviousSceneLastFrame",
+ skip_serializing_if = "Option::is_none"
+ )]
+ use_previous_scene_last_frame: Option<bool>,
},
#[serde(rename = "scene.remove")]
SceneRemove { id: String },
@@ -480,6 +501,7 @@ fn apply_command(
let token = format!("@[ref:{id}]");
if project.generation_jobs.iter().any(|job| {
job.start_frame_reference_id.as_deref() == Some(id)
+ || job.end_frame_reference_id.as_deref() == Some(id)
|| job
.shots
.as_ref()
@@ -503,6 +525,8 @@ fn apply_command(
sound,
music,
start_frame_reference_id,
+ end_frame_reference_id,
+ use_previous_scene_last_frame,
} => {
if project.generation_jobs.iter().any(|job| job.id == *id) {
return Err(format!("scene '{id}' already exists"));
@@ -521,6 +545,8 @@ fn apply_command(
generation_snapshot: None,
reference_ids: Vec::new(),
start_frame_reference_id: start_frame_reference_id.clone(),
+ end_frame_reference_id: end_frame_reference_id.clone(),
+ use_previous_scene_last_frame: *use_previous_scene_last_frame,
shot_tags: None,
shots: Some(Vec::new()),
duration_seconds: Some(*duration_seconds),
@@ -544,6 +570,8 @@ fn apply_command(
sound,
music,
start_frame_reference_id,
+ end_frame_reference_id,
+ use_previous_scene_last_frame,
} => {
ensure_any(
[
@@ -554,6 +582,8 @@ fn apply_command(
sound.is_some(),
music.is_some(),
start_frame_reference_id.is_some(),
+ end_frame_reference_id.is_some(),
+ use_previous_scene_last_frame.is_some(),
],
"has no fields to change",
)?;
@@ -579,6 +609,16 @@ fn apply_command(
}
if let Some(value) = start_frame_reference_id {
job.start_frame_reference_id = value.clone();
+ job.use_previous_scene_last_frame = None;
+ }
+ if let Some(value) = end_frame_reference_id {
+ job.end_frame_reference_id = value.clone();
+ }
+ if let Some(value) = use_previous_scene_last_frame {
+ job.use_previous_scene_last_frame = Some(*value);
+ if *value {
+ job.start_frame_reference_id = None;
+ }
}
job.updated_at = updated_at;
}
@@ -1109,6 +1149,47 @@ mod tests {
));
}
+ #[test]
+ fn scene_frame_commands_accept_continuity_and_clear_the_last_frame() {
+ let config = fixture();
+ let id = &config.generation_jobs[0].id;
+ let image = &config
+ .references
+ .iter()
+ .find(|reference| reference.kind == "image")
+ .unwrap()
+ .id;
+ let command: ProjectCommand = serde_json::from_value(serde_json::json!({
+ "op": "scene.set", "id": id, "endFrame": image, "usePreviousSceneLastFrame": true
+ }))
+ .unwrap();
+ let updated = execute_commands_at(&config, &[command], "2026-09-12T00:00:00.000Z").unwrap();
+ assert_eq!(
+ updated.generation_jobs[0].end_frame_reference_id.as_deref(),
+ Some(image.as_str())
+ );
+ assert_eq!(
+ updated.generation_jobs[0].use_previous_scene_last_frame,
+ Some(true)
+ );
+ let clear: ProjectCommand = serde_json::from_value(serde_json::json!({
+ "op": "scene.set", "id": id, "endFrame": null, "startFrame": image
+ }))
+ .unwrap();
+ let updated = execute_commands_at(&updated, &[clear], "2026-09-12T00:00:00.000Z").unwrap();
+ assert!(updated.generation_jobs[0].end_frame_reference_id.is_none());
+ assert_ne!(
+ updated.generation_jobs[0].use_previous_scene_last_frame,
+ Some(true)
+ );
+ assert_eq!(
+ updated.generation_jobs[0]
+ .start_frame_reference_id
+ .as_deref(),
+ Some(image.as_str())
+ );
+ }
+
#[test]
fn requires_a_final_non_empty_commit() {
assert!(
diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs
index 0f7c8fb..82960e6 100644
--- a/src-tauri/src/lib.rs
+++ b/src-tauri/src/lib.rs
@@ -294,6 +294,10 @@ struct GenerationJob {
/// Optional image reference that initializes the scene's first frame.
#[serde(default, skip_serializing_if = "Option::is_none")]
start_frame_reference_id: Option<String>,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ end_frame_reference_id: Option<String>,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ use_previous_scene_last_frame: Option<bool>,
/// The H3 vocabulary tags this shot was built from: group id -> option ids.
/// zod spells it `.nullish()` (src/lib/project.ts `shotTagSelectionSchema`),
/// so `null` IS readable on the frontend — but the key is still skipped
@@ -1222,22 +1226,27 @@ fn validate_and_normalize_config(mut config: ProjectConfig) -> Result<ProjectCon
job.id, reference_id
));
}
- if let Some(start_frame_id) = &job.start_frame_reference_id {
- let Some(reference) = config
- .references
- .iter()
- .find(|reference| reference.id == *start_frame_id)
- else {
- return Err(format!(
- "Generation job '{}' uses unknown start-frame reference '{}'.",
- job.id, start_frame_id
- ));
- };
- if reference.kind != "image" && reference.images.is_empty() {
- return Err(format!(
- "Generation job '{}' start-frame reference '{}' is not an image.",
- job.id, start_frame_id
- ));
+ for (edge, frame_id) in [
+ ("start", &job.start_frame_reference_id),
+ ("end", &job.end_frame_reference_id),
+ ] {
+ if let Some(start_frame_id) = frame_id {
+ let Some(reference) = config
+ .references
+ .iter()
+ .find(|reference| reference.id == *start_frame_id)
+ else {
+ return Err(format!(
+ "Generation job '{}' uses unknown {edge}-frame reference '{}'.",
+ job.id, start_frame_id
+ ));
+ };
+ if reference.kind != "image" && reference.images.is_empty() {
+ return Err(format!(
+ "Generation job '{}' {edge}-frame reference '{}' is not an image.",
+ job.id, start_frame_id
+ ));
+ }
}
}
}
@@ -2501,6 +2510,38 @@ fn write_generated_video(request: Request<'_>) -> Result<GeneratedVideoFile, Str
result
}
+#[tauri::command]
+fn write_scene_last_frame(request: Request<'_>) -> Result<(), String> {
+ let InvokeBody::Raw(bytes) = request.body() else {
+ return Err("The last frame must be sent as raw PNG bytes.".into());
+ };
+ if !bytes.starts_with(b"\x89PNG\r\n\x1a\n") {
+ return Err("The last frame is not a PNG image.".into());
+ }
+ let header = |name: &str| -> Result<String, String> {
+ let value = request
+ .headers()
+ .get(name)
+ .ok_or_else(|| format!("Missing {name} header."))?;
+ export::percent_decode(
+ value
+ .to_str()
+ .map_err(|_| format!("Invalid {name} header."))?,
+ )
+ };
+ let root = project_root(&header(GENERATED_FOLDER_HEADER)?)?;
+ let stem = generated_file_stem(&header(GENERATED_JOB_HEADER)?)?;
+ let directory = root.join("cache/scene-last");
+ fs::create_dir_all(&directory).map_err(|error| error.to_string())?;
+ let directory = directory
+ .canonicalize()
+ .map_err(|error| error.to_string())?;
+ if !directory.starts_with(&root) {
+ return Err("Scene frame images must stay inside the project folder.".into());
+ }
+ export::write_atomically(&directory.join(format!("{stem}.png")), bytes)
+}
+
/** Execute an already provider-validated command batch against the latest
* config held by the editor. The provider can take minutes to answer; applying
* here, after it returns, keeps edits made while it was thinking instead of
@@ -2835,6 +2876,7 @@ pub fn run() {
set_generation_active,
answer_app_close,
write_generated_video,
+ write_scene_last_frame,
write_timeline_thumbnail,
purge_timeline_thumbnails,
generated_summary,
@@ -4244,6 +4286,49 @@ mod tests {
);
}
+ #[test]
+ fn scene_frame_settings_survive_save_and_reopen_and_validate_images() {
+ let mut config = fixture();
+ let image_id = config
+ .references
+ .iter()
+ .find(|reference| reference.kind == "image")
+ .unwrap()
+ .id
+ .clone();
+ config.generation_jobs[0].end_frame_reference_id = Some(image_id.clone());
+ config.generation_jobs[0].use_previous_scene_last_frame = Some(true);
+ let folder = tempfile::tempdir().unwrap();
+ write_project(folder.path(), &config).unwrap();
+ let restored = read_project(folder.path()).unwrap().config;
+ assert_eq!(
+ restored.generation_jobs[0]
+ .end_frame_reference_id
+ .as_deref(),
+ Some(image_id.as_str())
+ );
+ assert_eq!(
+ restored.generation_jobs[0].use_previous_scene_last_frame,
+ Some(true)
+ );
+ config.generation_jobs[0].end_frame_reference_id = Some("missing".into());
+ assert!(validate_and_normalize_config(config.clone())
+ .unwrap_err()
+ .contains("unknown end-frame"));
+ config.generation_jobs[0].end_frame_reference_id = Some(
+ config
+ .references
+ .iter()
+ .find(|reference| reference.kind == "text")
+ .unwrap()
+ .id
+ .clone(),
+ );
+ assert!(validate_and_normalize_config(config)
+ .unwrap_err()
+ .contains("is not an image"));
+ }
+
#[test]
fn reference_images_are_attachments_and_derived_state_is_not_serialized() {
let mut config = fixture();
diff --git a/src/components/workspace/GeneratorView.test.tsx b/src/components/workspace/GeneratorView.test.tsx
index 6126107..f0cd3e4 100644
--- a/src/components/workspace/GeneratorView.test.tsx
+++ b/src/components/workspace/GeneratorView.test.tsx
@@ -184,6 +184,63 @@ describe("Generator scene controls", () => {
expect(snapshot.prompt).toContain("<Picture 1> is the first frame of the video.");
});
+ it("sends a last-frame image and clears it independently of the start frame", () => {
+ const initial = project();
+ initial.references = [{ id: "closing", kind: "image", name: "Closing still", description: "", relativePath: "references/closing.png", intendedUse: [], createdAt: initial.createdAt }];
+ const state = setup(initial);
+ fireEvent.change(screen.getByRole("combobox", { name: "Last frame for this scene" }), { target: { value: "closing" } });
+ expect(state.latest().generationJobs[0].endFrameReferenceId).toBe("closing");
+ fireEvent.click(within(screen.getByRole("region", { name: "First scene" })).getByRole("button", { name: "Generate" }));
+ const snapshot = JSON.parse(state.latest().generationJobs[0].generationSnapshot!);
+ expect(snapshot.prompt).toContain("<Picture 1> is the last frame of the video.");
+ expect(snapshot.referencePaths[0]).toContain("closing.png");
+ fireEvent.change(screen.getByRole("combobox", { name: "Last frame for this scene" }), { target: { value: "" } });
+ expect(state.latest().generationJobs[0].endFrameReferenceId).toBeUndefined();
+ });
+
+ it("marks a linked scene yellow when its previous scene regenerates and clears it after regeneration", () => {
+ const initial = project();
+ initial.generationJobs[0] = { ...initial.generationJobs[0], status: "completed", outputRelativePath: "media/generated/work-original.mp4" };
+ const state = setup(initial);
+ expect(screen.getByRole("option", { name: "Previous scene's last frame" })).toBeDisabled();
+ fireEvent.click(screen.getByRole("button", { name: "Select scene Second scene" }));
+ fireEvent.change(screen.getByRole("combobox", { name: "Start frame for this scene" }), { target: { value: "previous-scene" } });
+ expect(state.latest().generationJobs[1].usePreviousSceneLastFrame).toBe(true);
+ const generateSecond = () => fireEvent.click(within(screen.getByRole("region", { name: "Second scene" })).getByRole("button", { name: "Generate" }));
+ const finishSecond = () => state.replace({ ...state.latest(), generationJobs: state.latest().generationJobs.map((job, index) => index === 1 ? { ...job, status: "completed", outputRelativePath: "media/generated/work-second.mp4" } : job) });
+ generateSecond();
+ const snapshot = JSON.parse(state.latest().generationJobs[1].generationSnapshot!);
+ expect(snapshot.previousSceneId).toBe("scene-first");
+ expect(snapshot.referencePaths[0]).toContain("work-original.png");
+ finishSecond();
+ const second = () => screen.getByRole("region", { name: "Second scene" });
+ expect(within(second()).getByText("FINISHED")).toBeInTheDocument();
+ state.replace({ ...state.latest(), generationJobs: state.latest().generationJobs.map((job, index) => index === 0 ? { ...job, status: "generating" } : job) });
+ expect(within(second()).getByText("CHANGED")).toBeInTheDocument();
+ expect(second().querySelector(".scene-rule__status--changed")).not.toBeNull();
+ state.replace({ ...state.latest(), generationJobs: state.latest().generationJobs.map((job, index) => index === 0 ? { ...job, status: "completed", outputRelativePath: "media/generated/work-new.mp4" } : job) });
+ expect(within(second()).getByText("CHANGED")).toBeInTheDocument();
+ // Opening a saved project retains the dependency's recorded renderer inputs.
+ state.replace(parseProjectConfig(JSON.parse(JSON.stringify(state.latest()))));
+ expect(within(second()).getByText("CHANGED")).toBeInTheDocument();
+ generateSecond();
+ expect(JSON.parse(state.latest().generationJobs[1].generationSnapshot!).referencePaths[0]).toContain("work-new.png");
+ finishSecond();
+ expect(within(second()).getByText("FINISHED")).toBeInTheDocument();
+ });
+
+ it("includes fixed-seed linked scenes when Generate All regenerates their source", () => {
+ const initial = project();
+ initial.generationJobs = initial.generationJobs.map((job, index) => ({ ...job, seed: 42, outputRelativePath: `media/generated/work-${index}.mp4`, usePreviousSceneLastFrame: index === 1 }));
+ const state = setup(initial);
+ fireEvent.click(screen.getByRole("button", { name: "Generate All" }));
+ state.replace({ ...state.latest(), generationJobs: state.latest().generationJobs.map((job) => ({ ...job, status: "completed" })) });
+ expect(screen.getByRole("button", { name: "Generate All" })).toHaveAttribute("title", "Every fixed-seed scene is already up to date.");
+ fireEvent.change(screen.getByRole("textbox", { name: "The sound of this scene" }), { target: { value: "Rain" } });
+ fireEvent.click(screen.getByRole("button", { name: "Generate All" }));
+ expect(state.latest().generationJobs.map((job) => job.status)).toEqual(["queued", "queued"]);
+ });
+
it("shows scene generation controls and sends their values in the render snapshot", async () => {
const initial = project();
initial.settings = { ...initial.settings, resolution: "416p", frameRate: 60 };
diff --git a/src/components/workspace/GeneratorView.tsx b/src/components/workspace/GeneratorView.tsx
index 82a2e46..e5f92f0 100644
--- a/src/components/workspace/GeneratorView.tsx
+++ b/src/components/workspace/GeneratorView.tsx
@@ -2,7 +2,7 @@ import type { GenerationSubmission } from "../../lib/workQueue";
import { ChevronDown, Plus, Square, Trash2, WandSparkles } from "lucide-react";
import { invoke } from "@tauri-apps/api/core";
import { useEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
-import { actionReferenceIds, compileGenerationJobPrompt, compileGenerationJobSegments, createDraftGenerationJob, danglingReferenceTokens, GENERATION_FRAME_RATE, RANDOM_GENERATION_SEED, sceneDurationSeconds, sceneGenerationReferences, sceneGenerationSeed, sceneGenerationSnapshot, sceneGenerationSteps, sceneShots, SCENE_MAX_SECONDS, SCENE_MIN_SECONDS, projectItemPath, usableReferenceImages, type GenerationJob, type ProjectConfig, type ProjectReference, type SceneShot } from "../../lib/project";
+import { actionReferenceIds, compileGenerationJobPrompt, compileGenerationJobSegments, createDraftGenerationJob, danglingReferenceTokens, GENERATION_FRAME_RATE, RANDOM_GENERATION_SEED, sceneDurationSeconds, sceneFrameInputs, sceneGenerationSeed, sceneGenerationSnapshot, sceneGenerationSteps, sceneShots, SCENE_MAX_SECONDS, SCENE_MIN_SECONDS, projectItemPath, usableReferenceImages, type GenerationJob, type ProjectConfig, type ProjectReference, type SceneShot } from "../../lib/project";
import { generationDimensions } from "../../lib/export";
import { isTauri } from "../../lib/persistence";
import { getEngineStatus, type SlopfabGenerationRequest, type SlopfabStatus } from "../../lib/runtime";
@@ -122,7 +122,8 @@ export function GeneratorView({ config, folderPath, runtime = null, generationCo
const active = jobs.filter((job) => job.status === "generating" || job.status === "ready");
const queued = jobs.filter((job) => job.status === "queued");
const cancellable = jobs.filter((job) => job.status === "queued" || job.status === "generating");
- const boundRefs = useMemo(() => selected ? sceneGenerationReferences(selected, config.references) : [], [config.references, selected]);
+ const frameInputs = useMemo(() => selected ? sceneFrameInputs(selected, config) : null, [config, selected]);
+ const boundRefs = frameInputs?.references ?? [];
const runtimeReady = Boolean(selectedTemplate.id) && generatorRuntime?.state === "ready";
const updateJob = (id: string, updates: Partial<GenerationJob>) => {
@@ -153,7 +154,7 @@ export function GeneratorView({ config, folderPath, runtime = null, generationCo
const updateScene = (job: GenerationJob, updates: Partial<GenerationJob>) =>
updateJob(job.id, mergeSceneUpdates(job, updates));
- const addStartFrame = async (job: GenerationJob) => {
+ const addFrame = async (job: GenerationJob, edge: "start" | "end") => {
if (!isTauri()) return;
setStartFrameError(null);
try {
@@ -174,7 +175,7 @@ export function GeneratorView({ config, folderPath, runtime = null, generationCo
...current,
references: [reference, ...current.references],
generationJobs: current.generationJobs.map((candidate) => candidate.id === job.id
- ? { ...candidate, startFrameReferenceId: id, updatedAt: new Date().toISOString() }
+ ? { ...candidate, ...(edge === "start" ? { startFrameReferenceId: id, usePreviousSceneLastFrame: undefined } : { endFrameReferenceId: id }), updatedAt: new Date().toISOString() }
: candidate),
});
} catch (reason) {
@@ -285,7 +286,8 @@ export function GeneratorView({ config, folderPath, runtime = null, generationCo
// ordered list drives the <Subject N> / <Picture N> numbering inside the
// compiled prompt, because slopfab.rs adds reference_paths sequentially — so
// array index 0 must be the asset the prompt calls <Picture 1>.
- const bound = sceneGenerationReferences(job, configRef.current.references);
+ const inputs = sceneFrameInputs(job, configRef.current);
+ const bound = inputs.references;
const canvas = generationDimensions(config.settings.resolution, config.settings.aspectRatio);
return {
jobId: job.id,
@@ -293,7 +295,8 @@ export function GeneratorView({ config, folderPath, runtime = null, generationCo
// retimed shot reach the engine, rather than sending a prompt frozen at
// draft-creation time. This is the ONE string slopfab is given, and it is
// the same string the compiled-prompt panel shows.
- prompt: compileGenerationJobPrompt(job, bound),
+ prompt: compileGenerationJobPrompt(inputs.job, bound),
+ ...(inputs.previousSceneId ? { previousSceneId: inputs.previousSceneId } : {}),
// The scene's own length, not a fixed six seconds.
frames: Math.round(sceneDurationSeconds(job) * GENERATION_FRAME_RATE),
steps: sceneGenerationSteps(job, defaultGenerationSteps),
@@ -363,28 +366,34 @@ export function GeneratorView({ config, folderPath, runtime = null, generationCo
/** Generate All is an idempotent update for fixed seeds, but a deliberate
* reroll for random ones. A completed fixed-seed scene is current only when
* both its output and the exact renderer-input snapshot still exist. */
- const batchScenesReady = jobs.filter((job) => {
- if (job.status === "queued" || job.status === "generating" || job.status === "ready") return false;
- if (sendBlocker(job, config.references)) return false;
- if (sceneGenerationSeed(job) === RANDOM_GENERATION_SEED) return true;
- return job.status !== "completed"
+ const batchScenesReady: GenerationJob[] = [];
+ for (const [index, job] of jobs.entries()) {
+ if (job.status === "queued" || job.status === "generating" || job.status === "ready") continue;
+ if (job.usePreviousSceneLastFrame && index === 0) continue;
+ if (sendBlocker(job, config.references)) continue;
+ const previous = jobs[index - 1];
+ const previousWillRender = job.usePreviousSceneLastFrame && previous &&
+ (batchScenesReady.includes(previous) || ["queued", "generating", "ready"].includes(previous.status));
+ if (previousWillRender || sceneGenerationSeed(job) === RANDOM_GENERATION_SEED || job.status !== "completed"
|| !job.outputRelativePath
|| !job.generationSnapshot
- || job.generationSnapshot !== snapshotFor(job);
- });
+ || job.generationSnapshot !== snapshotFor(job)) batchScenesReady.push(job);
+ }
const generateAll = () => submit(batchScenesReady);
const generationBlocker = (job: GenerationJob): string | null => {
if (!runtimeReady) return runtimeError ?? generatorRuntime?.detail ?? "The video generator is not ready.";
if (job.status === "ready") return "This scene is being saved now.";
+ if (job.usePreviousSceneLastFrame && jobs[0]?.id === job.id) return "This scene needs a previous scene to supply its first frame.";
return sendBlocker(job, config.references);
};
const changedJobIds = new Set(jobs
- .filter((job) => job.status === "completed"
+ .filter((job, index) => job.status === "completed"
&& Boolean(job.generationSnapshot)
- && job.generationSnapshot !== snapshotFor(job))
+ && (job.generationSnapshot !== snapshotFor(job)
+ || (job.usePreviousSceneLastFrame && ["queued", "generating", "ready"].includes(jobs[index - 1]?.status))))
.map((job) => job.id));
const selectedIndicator = selected && changedJobIds.has(selected.id) ? "changed" : selected?.status;
@@ -540,7 +549,9 @@ export function GeneratorView({ config, folderPath, runtime = null, generationCo
references={config.references}
importAvailable={isTauri()}
importError={startFrameError}
- onAddStartFrame={() => void addStartFrame(selected)}
+ previousScene={jobs[jobs.findIndex((job) => job.id === selected.id) - 1]}
+ onAddStartFrame={() => void addFrame(selected, "start")}
+ onAddEndFrame={() => void addFrame(selected, "end")}
onChange={(updates) => updateScene(selected, updates)}
onShots={(next) => setShots(selected, next)}
/>
@@ -560,7 +571,7 @@ export function GeneratorView({ config, folderPath, runtime = null, generationCo
{debugEnabled && showDebugPrompt && selected && !openShot && <DebugPromptDialog
sceneTitle={selected.title}
- segments={compileGenerationJobSegments(selected, boundRefs)}
+ segments={compileGenerationJobSegments(frameInputs?.job ?? selected, boundRefs)}
onClose={() => setShowDebugPrompt(false)}
/>}
diff --git a/src/components/workspace/ReferencesView.tsx b/src/components/workspace/ReferencesView.tsx
index 6f8e8d3..0b37c74 100644
--- a/src/components/workspace/ReferencesView.tsx
+++ b/src/components/workspace/ReferencesView.tsx
@@ -102,6 +102,7 @@ export function ReferencesView({ config, folderPath, onChange, onRegenerateIcon,
...job,
referenceIds: job.referenceIds.filter((id) => id !== selected.id),
startFrameReferenceId: job.startFrameReferenceId === selected.id ? undefined : job.startFrameReferenceId,
+ endFrameReferenceId: job.endFrameReferenceId === selected.id ? undefined : job.endFrameReferenceId,
})) });
setSelectedId(config.references.find((ref) => ref.id !== selected.id)?.id);
};
diff --git a/src/components/workspace/SceneEditor.tsx b/src/components/workspace/SceneEditor.tsx
index 1218280..191ee40 100644
--- a/src/components/workspace/SceneEditor.tsx
+++ b/src/components/workspace/SceneEditor.tsx
@@ -266,15 +266,17 @@ export function ShotInspector({ job, shots, shot, index, endsAt, duration, refer
/** Scene-wide render controls, the look the description opens with (base guide
* §4.1), and the two sound fields defined per prompt (§4.6, §4.7). Length
* lives in the scene header where it stays visible. */
-export function SceneInspector({ job, shots, references, defaultSteps, disabled, importAvailable, importError, onAddStartFrame, onChange, onShots }: {
+export function SceneInspector({ job, shots, references, previousScene, defaultSteps, disabled, importAvailable, importError, onAddStartFrame, onAddEndFrame, onChange, onShots }: {
job: GenerationJob;
shots: SceneShot[];
references: ProjectReference[];
+ previousScene?: GenerationJob;
defaultSteps: number;
disabled: boolean;
importAvailable: boolean;
importError: string | null;
onAddStartFrame: () => void;
+ onAddEndFrame: () => void;
onChange: (updates: Partial<GenerationJob>) => void;
onShots: (next: SceneShot[]) => void;
}) {
@@ -314,12 +316,16 @@ export function SceneInspector({ job, shots, references, defaultSteps, disabled,
<span>Start frame</span>
<div className="scene-settings__start-frame">
<select
- value={job.startFrameReferenceId ?? ""}
+ value={job.usePreviousSceneLastFrame ? "previous-scene" : job.startFrameReferenceId ?? ""}
disabled={disabled}
aria-label="Start frame for this scene"
- onChange={(event) => onChange({ startFrameReferenceId: event.target.value || undefined })}
+ onChange={(event) => onChange({
+ usePreviousSceneLastFrame: event.target.value === "previous-scene" || undefined,
+ startFrameReferenceId: event.target.value === "previous-scene" ? undefined : event.target.value || undefined,
+ })}
>
<option value="">None</option>
+ <option value="previous-scene" disabled={!previousScene}>Previous scene's last frame</option>
{imageReferences.map((reference) => <option key={reference.id} value={reference.id}>{reference.name}</option>)}
</select>
<button
@@ -330,9 +336,25 @@ export function SceneInspector({ job, shots, references, defaultSteps, disabled,
onClick={onAddStartFrame}
><ImagePlus size={15} /> Add image</button>
</div>
- <small>The selected image is sent to SlopFab as Picture 1 and anchors the opening frame.</small>
- {importError && <small className="scene-settings__error" role="alert">{importError}</small>}
+ <small>{job.usePreviousSceneLastFrame
+ ? previousScene ? `Starts from the last rendered frame of “${previousScene.title}”. Generate that scene first, or use Generate All.` : "Move this scene after another scene to use its last frame."
+ : "The selected image anchors the opening frame."}</small>
</div>
+ <div className="scene-settings__field">
+ <span>Last frame</span>
+ <div className="scene-settings__start-frame">
+ <select value={job.endFrameReferenceId ?? ""} disabled={disabled} aria-label="Last frame for this scene"
+ onChange={(event) => onChange({ endFrameReferenceId: event.target.value || undefined })}>
+ <option value="">None</option>
+ {imageReferences.map((reference) => <option key={reference.id} value={reference.id}>{reference.name}</option>)}
+ </select>
+ <button type="button" className="secondary-button" disabled={disabled || !importAvailable}
+ title={importAvailable ? "Import an image and use it as this scene's last frame" : "Image import is available in the desktop app"}
+ onClick={onAddEndFrame}><ImagePlus size={15} /> Add image</button>
+ </div>
+ <small>The selected image anchors the closing frame.</small>
+ </div>
+ {importError && <small className="scene-settings__error" role="alert">{importError}</small>}
<label className="scene-settings__field">
<span>Sound</span>
<textarea
diff --git a/src/lib/project.test.ts b/src/lib/project.test.ts
index 69f9765..11014a6 100644
--- a/src/lib/project.test.ts
+++ b/src/lib/project.test.ts
@@ -42,6 +42,7 @@ import {
sceneBriefText,
sceneDurationSeconds,
sceneGenerationReferences,
+ sceneFrameInputs,
sceneShots,
splitActionText,
type ProjectConfig,
@@ -492,6 +493,45 @@ describe("project schema", () => {
expect(compiled).not.toContain("<Subject 2>");
});
+ it("numbers opening, closing and subject pictures together, sending one image per frame anchor", () => {
+ const image = (id: string): ProjectReference => projectReferenceSchema.parse({
+ id, kind: "text", name: id, description: "", intendedUse: [], createdAt: createdFixture.createdAt,
+ images: [1, 2].map((n) => ({ id: `${id}-${n}`, name: id, relativePath: `references/${id}-${n}.png` })),
+ });
+ const references = [image("subject"), image("end"), image("start")];
+ const job = createDraftGenerationJob("The subject turns.", { startFrameReferenceId: "start", endFrameReferenceId: "end", referenceIds: ["subject", "start", "end"] });
+ const ordered = sceneGenerationReferences(job, references);
+ expect(usableReferenceImages(ordered).map((image) => image.relativePath)).toEqual([
+ "references/start-1.png", "references/end-1.png", "references/subject-1.png", "references/subject-2.png",
+ ]);
+ const prompt = compileGenerationJobPrompt(job, ordered);
+ expect(prompt).toContain("<Picture 1> is the first frame of the video.");
+ expect(prompt).toContain("<Picture 2> is the last frame of the video.");
+ expect(prompt).toContain("<Subject 1> is the content shown in <Picture 3> and <Picture 4>.");
+ expect(prompt).toContain("<Picture 2>: last_frame - used as the closing frame.");
+ expect(prompt).toContain("The closing frame matches <Picture 2>.");
+ const endOnly = { ...job, startFrameReferenceId: undefined, referenceIds: [] };
+ expect(compileGenerationJobPrompt(endOnly, sceneGenerationReferences(endOnly, references))).toContain("<Picture 1> is the last frame of the video.");
+ const same = { ...job, endFrameReferenceId: "start", referenceIds: [] };
+ expect(usableReferenceImages(sceneGenerationReferences(same, references))).toHaveLength(1);
+ expect(compileGenerationJobPrompt(same, sceneGenerationReferences(same, references))).toContain("<Picture 1> is the last frame of the video.");
+ });
+
+ it("resolves scene continuity from board order and the current rendered file", () => {
+ const config = parseProjectConfig(createdFixture);
+ const first = { ...config.generationJobs[0], outputRelativePath: "media/generated/work-first.mp4" };
+ const second = createDraftGenerationJob("Continue", { usePreviousSceneLastFrame: true });
+ config.generationJobs = [first, second];
+ const inputs = sceneFrameInputs(second, config);
+ expect(inputs.previousSceneId).toBe(first.id);
+ expect(usableReferenceImages(inputs.references)[0].relativePath).toBe("cache/scene-last/work-first.png");
+ expect(compileGenerationJobPrompt(inputs.job, inputs.references)).toContain("<Picture 1> is the first frame of the video.");
+ first.outputRelativePath = "media/generated/work-rerendered.mp4";
+ expect(usableReferenceImages(sceneFrameInputs(second, config).references)[0].relativePath).toBe("cache/scene-last/work-rerendered.png");
+ config.generationJobs.reverse();
+ expect(sceneFrameInputs(second, config).previousSceneId).toBeUndefined();
+ });
+
it("allows a generated scene asset to wait for its render without relaxing imported media", () => {
const waiting = structuredClone(createdFixture) as any;
waiting.assets = [{
diff --git a/src/lib/project.ts b/src/lib/project.ts
index 14f64de..4b0190e 100644
--- a/src/lib/project.ts
+++ b/src/lib/project.ts
@@ -416,6 +416,8 @@ export const generationJobSchema = z.object({
/** An image reference used as the scene's opening frame. It is separate from
* subject citations because a frame is composition, not a named subject. */
startFrameReferenceId: idSchema.nullish(),
+ endFrameReferenceId: idSchema.nullish(),
+ usePreviousSceneLastFrame: z.boolean().nullish(),
// `.nullish()` because Rust holds it as an Option — see the note on
// projectAssetSchema.durationMs. Every project written before shot tags
// existed has no key here at all, and must keep opening.
@@ -739,13 +741,34 @@ export function usableReferenceImages(references: ProjectReference[]): ProjectRe
return usableImageReferences(references).flatMap(referenceImages);
}
-/** References in the exact order both the prompt compiler and SlopFab consume.
- * A start frame must be Picture 1; ordinary bound references retain project
- * order after it and a frame that is also bound is still sent only once. */
+/** Opening and closing pictures come first, then bound references in project
+ * order. Frame anchors use their first image, even on a multi-image reference;
+ * the same anchor selected for both ends is sent only once. */
export function sceneGenerationReferences(job: GenerationJob, references: ProjectReference[]): ProjectReference[] {
const start = references.find((reference) => reference.id === job.startFrameReferenceId && referenceImages(reference).length > 0 && isReferenceUsable(reference));
- const bound = references.filter((reference) => job.referenceIds.includes(reference.id) && reference.id !== start?.id);
- return start ? [start, ...bound] : bound;
+ const end = references.find((reference) => reference.id === job.endFrameReferenceId && referenceImages(reference).length > 0 && isReferenceUsable(reference));
+ const anchors = [start, end].filter((reference, index, all): reference is ProjectReference => Boolean(reference) && all.indexOf(reference) === index)
+ .map((reference) => ({ ...reference, kind: "text" as const, relativePath: null, sourcePath: null, intendedUse: [], images: referenceImages(reference).slice(0, 1) }));
+ return [...anchors, ...references.filter((reference) => job.referenceIds.includes(reference.id) && !anchors.some((anchor) => anchor.id === reference.id))];
+}
+
+/** Resolve the adjacent scene when preparing a request; queued work captures
+ * its id so subsequent board edits cannot change the source of that run. */
+export function sceneFrameInputs(job: GenerationJob, config: ProjectConfig) {
+ if (!job.usePreviousSceneLastFrame) return { job, references: sceneGenerationReferences(job, config.references) };
+ const previous = config.generationJobs[config.generationJobs.findIndex((candidate) => candidate.id === job.id) - 1];
+ const id = `previous-frame-${job.id}`;
+ const resolved = { ...job, startFrameReferenceId: id };
+ const reference: ProjectReference = {
+ id, kind: "image", name: "Previous scene's last frame", description: "", intendedUse: [], createdAt: job.createdAt,
+ relativePath: previous?.outputRelativePath ? sceneLastFramePath(previous.outputRelativePath) : `cache/scene-last/pending-${job.id}.png`,
+ };
+ return { job: resolved, references: sceneGenerationReferences(resolved, [reference, ...config.references]), previousSceneId: previous?.id };
+}
+
+export function sceneLastFramePath(outputRelativePath: string): string {
+ const stem = outputRelativePath.replaceAll("\\", "/").split("/").pop()!.replace(/\.[^.]+$/, "");
+ return `cache/scene-last/${stem}.png`;
}
/* Who wrote each piece of the compiled prompt.
@@ -777,6 +800,7 @@ const addedStop = (text: string): PromptSegment[] => (endSentence(text) === text
export interface ScenePrompt {
shots: readonly SceneShot[];
startFrameReferenceId?: string | null;
+ endFrameReferenceId?: string | null;
/** Base guide §4.6 / §4.7. Blank or absent falls back to the two default
* lines below, which are marked as Slopus's own writing. */
soundscape?: string | null;
@@ -830,6 +854,7 @@ export interface SceneGenerationInput {
canvasWidth: number;
canvasHeight: number;
referencePaths: readonly string[];
+ previousSceneId?: string;
}
/** A stable record of everything sent to the renderer. Shot ids are retained
@@ -849,6 +874,7 @@ export function sceneGenerationSnapshot(job: GenerationJob, input: SceneGenerati
canvasWidth: input.canvasWidth,
canvasHeight: input.canvasHeight,
referencePaths: input.referencePaths,
+ ...(input.previousSceneId ? { previousSceneId: input.previousSceneId } : {}),
shots,
});
}
@@ -883,7 +909,7 @@ export function compileMiniMaxH3PromptSegments(
}
export function compileGenerationJobSegments(job: GenerationJob, references: ProjectReference[] = []): PromptSegment[] {
- return compileScenePromptSegments({ shots: sceneShots(job), startFrameReferenceId: job.startFrameReferenceId, soundscape: job.soundscape, music: job.music }, references);
+ return compileScenePromptSegments({ shots: sceneShots(job), startFrameReferenceId: job.startFrameReferenceId, endFrameReferenceId: job.endFrameReferenceId, soundscape: job.soundscape, music: job.music }, references);
}
export function compileGenerationJobPrompt(job: GenerationJob, references: ProjectReference[] = []): string {
@@ -913,7 +939,8 @@ export function compileScenePromptSegments(scene: ScenePrompt, references: Proje
// audio-only-tagged reference is dropped here (ref guide §2.1), so a scene
// whose only reference is audio-tagged correctly falls back to T2VA.
const startFrame = references.find((reference) => reference.id === scene.startFrameReferenceId && referenceImages(reference).length > 0 && isReferenceUsable(reference)) ?? null;
- const usable = references.filter(isReferenceUsable).filter(isVisualReference).filter((reference) => reference.id !== startFrame?.id);
+ const endFrame = references.find((reference) => reference.id === scene.endFrameReferenceId && referenceImages(reference).length > 0 && isReferenceUsable(reference)) ?? null;
+ const usable = references.filter(isReferenceUsable).filter(isVisualReference).filter((reference) => reference.id !== startFrame?.id && reference.id !== endFrame?.id);
const subjectNumber = new Map(usable.map((reference, index) => [reference.id, index + 1]));
const compiled: CompiledShot[] = shots.map((shot, index) => {
@@ -990,7 +1017,7 @@ export function compileScenePromptSegments(scene: ScenePrompt, references: Proje
const soundSegment = sound ? own(sound) : frame(DEFAULT_SOUNDSCAPE);
const musicSegment = music ? own(music) : frame(DEFAULT_MUSIC);
- if (usable.length === 0 && !startFrame) {
+ if (usable.length === 0 && !startFrame && !endFrame) {
// T2VA — base guide §2.2 field list and order.
return [
frame("integrated_multimodal_description: "),
@@ -1048,9 +1075,12 @@ export function compileScenePromptSegments(scene: ScenePrompt, references: Proje
];
});
const startPicture = startFrame ? pictureNumbers.get(startFrame.id)?.[0] ?? null : null;
+ const endPicture = endFrame ? pictureNumbers.get(endFrame.id)?.[0] ?? null : null;
const startDefinition = startPicture
- ? [frame(`<Picture ${startPicture}> is the first frame of the video.${definitions.length > 0 ? "\n" : ""}`)]
+ ? [frame(`<Picture ${startPicture}> is the first frame of the video.${definitions.length > 0 || endPicture ? "\n" : ""}`)]
: [];
+ const endDefinition = endPicture
+ ? [frame(`<Picture ${endPicture}> is the last frame of the video.${definitions.length > 0 ? "\n" : ""}`)] : [];
const labels = usable.map((reference, index) => referenceLabel(reference, index));
const labelList = listOf(labels);
@@ -1076,6 +1106,7 @@ export function compileScenePromptSegments(scene: ScenePrompt, references: Proje
...addedStop(shot.text),
]),
...(startPicture ? [frame(` The video begins with <Picture ${startPicture}>.`)] : []),
+ ...(endPicture ? [frame(` The video ends with <Picture ${endPicture}>.`)] : []),
...(labels.length > 0 ? [frame(` ${labelList} ${labels.length === 1 ? "provides" : "provide"} generation guidance for the ${compiled.length === 1 ? "single shot" : `${compiled.length} shots`} described below.`)] : []),
];
@@ -1096,6 +1127,7 @@ export function compileScenePromptSegments(scene: ScenePrompt, references: Proje
return `${referenceLabel(reference, index)}${where}: fully_preserved - the referenced characteristics are retained.`;
});
if (startPicture) retention.unshift(`<Picture ${startPicture}>: first_frame - used as the opening frame.`);
+ if (endPicture) retention.push(`<Picture ${endPicture}>: last_frame - used as the closing frame.`);
// §5.2: in full-reference mode the style opening comes BEFORE [Shot 1], not
// after it. §5.3: cite each <Subject N> where it appears in the shot.
@@ -1116,12 +1148,14 @@ export function compileScenePromptSegments(scene: ScenePrompt, references: Proje
...tail(shot),
...dialogue(shot, shot.text.length > 0 || featured[shot.index].length > 0 || shot.tags.clauses.length > 0),
...(shot.index === 0 && startPicture ? [frame(` The opening frame matches <Picture ${startPicture}>.`)] : []),
+ ...(shot.index === compiled.length - 1 && endPicture ? [frame(` The closing frame matches <Picture ${endPicture}>.`)] : []),
]),
];
return [
frame("subject_definitions:\n"),
...startDefinition,
+ ...endDefinition,
...definitions,
frame("\n\nsummary:\n"),
...summary,
@@ -1162,6 +1196,8 @@ export function createDraftGenerationJob(
title?: string;
referenceIds?: string[];
startFrameReferenceId?: string | null;
+ endFrameReferenceId?: string | null;
+ usePreviousSceneLastFrame?: boolean;
references?: ProjectReference[];
shotTags?: ShotTagSelection | null;
/** The shots to open the scene with. Omitted means one shot holding
@@ -1197,6 +1233,8 @@ export function createDraftGenerationJob(
creativeBrief: text,
referenceIds: options.referenceIds ?? [],
startFrameReferenceId: options.startFrameReferenceId ?? undefined,
+ endFrameReferenceId: options.endFrameReferenceId ?? undefined,
+ usePreviousSceneLastFrame: options.usePreviousSceneLastFrame ?? undefined,
shots,
durationSeconds,
steps: options.steps ?? DEFAULT_GENERATION_STEPS,
diff --git a/src/lib/runtime.ts b/src/lib/runtime.ts
index e59ad55..e96ad25 100644
--- a/src/lib/runtime.ts
+++ b/src/lib/runtime.ts
@@ -43,8 +43,8 @@ export type ProjectCommand =
| { op: "ref.add"; id: string; name: string; text: string; use: string[] }
| { op: "ref.set"; id: string; name?: string; text?: string; use?: string[] }
| { op: "ref.remove"; id: string }
- | { op: "scene.add"; id: string; title: string; seconds: number; steps?: number; seed?: number; sound?: string; music?: string; startFrame?: string }
- | { op: "scene.set"; id: string; title?: string; seconds?: number; steps?: number; seed?: number; sound?: string | null; music?: string | null; startFrame?: string | null }
+ | { op: "scene.add"; id: string; title: string; seconds: number; steps?: number; seed?: number; sound?: string; music?: string; startFrame?: string; endFrame?: string; usePreviousSceneLastFrame?: boolean }
+ | { op: "scene.set"; id: string; title?: string; seconds?: number; steps?: number; seed?: number; sound?: string | null; music?: string | null; startFrame?: string | null; endFrame?: string | null; usePreviousSceneLastFrame?: boolean }
| { op: "scene.remove"; id: string }
| { op: "scene.move"; id: string; before?: string | null }
| { op: "shot.add"; scene: string; id: string; at: number; action: string; name?: string; speech?: string; language?: string; settings?: Record<string, string[]> }
@@ -88,6 +88,8 @@ export interface SlopfabGenerationRequest {
canvasWidth: number;
canvasHeight: number;
referencePaths: string[];
+ /** Resolved by the app queue before native submission. */
+ previousSceneId?: string;
}
export interface ResolvedPlan {
@@ -313,7 +315,7 @@ function executeDemoCommands(config: ProjectConfig, commands: ProjectCommand[]):
}
case "ref.remove": {
const token = `@[ref:${command.id}]`;
- if (next.generationJobs.some((job) => job.startFrameReferenceId === command.id || job.shots?.some((shot) => shot.action.includes(token)))) {
+ if (next.generationJobs.some((job) => job.startFrameReferenceId === command.id || job.endFrameReferenceId === command.id || job.shots?.some((shot) => shot.action.includes(token)))) {
throw new Error(`Reference '${command.id}' is still used by a scene.`);
}
const at = next.references.findIndex((reference) => reference.id === command.id);
@@ -331,6 +333,8 @@ function executeDemoCommands(config: ProjectConfig, commands: ProjectCommand[]):
job.soundscape = command.sound;
job.music = command.music;
job.startFrameReferenceId = command.startFrame;
+ job.endFrameReferenceId = command.endFrame;
+ job.usePreviousSceneLastFrame = command.usePreviousSceneLastFrame;
next.generationJobs.push(job);
break;
}
@@ -342,7 +346,15 @@ function executeDemoCommands(config: ProjectConfig, commands: ProjectCommand[]):
if (command.seed !== undefined) job.seed = command.seed;
if ("sound" in command) job.soundscape = command.sound;
if ("music" in command) job.music = command.music;
- if ("startFrame" in command) job.startFrameReferenceId = command.startFrame;
+ if ("startFrame" in command) {
+ job.startFrameReferenceId = command.startFrame;
+ job.usePreviousSceneLastFrame = false;
+ }
+ if ("endFrame" in command) job.endFrameReferenceId = command.endFrame;
+ if ("usePreviousSceneLastFrame" in command) {
+ job.usePreviousSceneLastFrame = command.usePreviousSceneLastFrame;
+ if (command.usePreviousSceneLastFrame) job.startFrameReferenceId = undefined;
+ }
job.updatedAt = now;
break;
}
diff --git a/src/lib/sceneLastFrame.test.ts b/src/lib/sceneLastFrame.test.ts
new file mode 100644
index 0000000..a664b08
--- /dev/null
+++ b/src/lib/sceneLastFrame.test.ts
@@ -0,0 +1,53 @@
+import { afterEach, expect, it, vi } from "vitest";
+import { invoke } from "@tauri-apps/api/core";
+import { demux } from "./exportPipeline";
+import { saveSceneLastFrame } from "./sceneLastFrame";
+
+vi.mock("@tauri-apps/api/core", () => ({ invoke: vi.fn() }));
+vi.mock("./persistence", () => ({ readMediaFileBytes: vi.fn(async () => new ArrayBuffer(0)) }));
+vi.mock("./exportPipeline", () => ({ demux: vi.fn() }));
+afterEach(() => { vi.unstubAllGlobals(); vi.clearAllMocks(); });
+
+it("decodes the final GOP, captures the last presentation frame at full size and releases every frame", async () => {
+ vi.mocked(demux).mockResolvedValue({ config: { codec: "avc1" }, samples: [0, 100, 200, 400, 300].map((timestampUs) => ({
+ timestampUs, key: timestampUs === 0 || timestampUs === 200, durationUs: 100, data: new Uint8Array(1),
+ })) });
+ const frames: Array<{ timestamp: number; displayWidth: number; displayHeight: number; close: ReturnType<typeof vi.fn> }> = [];
+ const close = vi.fn();
+ const draw = vi.fn();
+ const canvas = vi.fn();
+ vi.stubGlobal("EncodedVideoChunk", class { constructor(public init: EncodedVideoChunkInit) {} });
+ vi.stubGlobal("VideoDecoder", class {
+ state = "configured";
+ decodeQueueSize = 0;
+ constructor(private init: VideoDecoderInit) {}
+ configure() {}
+ decode(chunk: { init: EncodedVideoChunkInit }) {
+ const frame = { timestamp: chunk.init.timestamp, displayWidth: 1920, displayHeight: 1080, close: vi.fn() };
+ frames.push(frame);
+ this.init.output(frame as unknown as VideoFrame);
+ }
+ async flush() {}
+ close = close;
+ });
+ vi.stubGlobal("OffscreenCanvas", class {
+ constructor(width: number, height: number) { canvas(width, height); }
+ getContext() { return { drawImage: draw }; }
+ async convertToBlob() { return { arrayBuffer: async () => new Uint8Array([137, 80, 78, 71]).buffer }; }
+ });
+ expect(await saveSceneLastFrame("C:/project", "media/generated/work-source.mp4")).toBe("cache/scene-last/work-source.png");
+ expect(frames.map((frame) => frame.timestamp)).toEqual([200, 400, 300]);
+ expect(draw).toHaveBeenCalledWith(frames[1], 0, 0);
+ expect(canvas).toHaveBeenCalledWith(1920, 1080);
+ expect(frames.every((frame) => frame.close.mock.calls.length === 1)).toBe(true);
+ expect(close).toHaveBeenCalledOnce();
+ expect(invoke).toHaveBeenCalledWith("write_scene_last_frame", expect.any(Uint8Array), { headers: {
+ "x-generated-folder": "C%3A%2Fproject", "x-generated-job": "work-source",
+ } });
+});
+
+it("rejects a video with no decoded samples before creating an image", async () => {
+ vi.mocked(demux).mockResolvedValue({ config: { codec: "avc1" }, samples: [] });
+ await expect(saveSceneLastFrame("C:/project", "media/generated/work-source.mp4")).rejects.toThrow("no video frames");
+ expect(invoke).not.toHaveBeenCalled();
+});
diff --git a/src/lib/sceneLastFrame.ts b/src/lib/sceneLastFrame.ts
new file mode 100644
index 0000000..a53529d
--- /dev/null
+++ b/src/lib/sceneLastFrame.ts
@@ -0,0 +1,49 @@
+import { invoke } from "@tauri-apps/api/core";
+import { demux } from "./exportPipeline";
+import { readMediaFileBytes } from "./persistence";
+import { sceneLastFramePath } from "./project";
+
+/** Decode the final keyframe group into a full-resolution conditioning image. */
+export async function saveSceneLastFrame(folderPath: string, outputRelativePath: string): Promise<string> {
+ const relativePath = sceneLastFramePath(outputRelativePath);
+ const source = await demux(await readMediaFileBytes(folderPath, { relativePath: outputRelativePath }), "Previous scene");
+ if (!source.samples.length) throw new Error("The previous scene contains no video frames.");
+ let start = source.samples.length - 1;
+ while (start > 0 && !source.samples[start].key) start -= 1;
+ const state: { frame: VideoFrame | null; error: Error | null } = { frame: null, error: null };
+ const decoder = new VideoDecoder({
+ output: (frame) => {
+ if (!state.frame || frame.timestamp >= state.frame.timestamp) {
+ state.frame?.close();
+ state.frame = frame;
+ } else frame.close();
+ },
+ error: (error) => { state.error = error; },
+ });
+ try {
+ decoder.configure({ ...source.config, hardwareAcceleration: "prefer-hardware" });
+ for (let index = start; index < source.samples.length; index += 1) {
+ if (state.error) throw state.error;
+ const sample = source.samples[index];
+ decoder.decode(new EncodedVideoChunk({ type: sample.key ? "key" : "delta", timestamp: sample.timestampUs, duration: sample.durationUs, data: sample.data }));
+ while (decoder.decodeQueueSize > 8 && !state.error) await new Promise((resolve) => setTimeout(resolve, 0));
+ }
+ await decoder.flush();
+ if (state.error) throw state.error;
+ const frame = state.frame;
+ if (!frame) throw new Error("Could not decode the previous scene's last frame.");
+ const canvas = new OffscreenCanvas(frame.displayWidth, frame.displayHeight);
+ const context = canvas.getContext("2d");
+ if (!context) throw new Error("Could not create the previous scene's last-frame image.");
+ context.drawImage(frame, 0, 0);
+ const bytes = new Uint8Array(await (await canvas.convertToBlob({ type: "image/png" })).arrayBuffer());
+ await invoke("write_scene_last_frame", bytes, { headers: {
+ "x-generated-folder": encodeURIComponent(folderPath),
+ "x-generated-job": encodeURIComponent(relativePath.split("/").pop()!.replace(/\.png$/, "")),
+ } });
+ return relativePath;
+ } finally {
+ state.frame?.close();
+ if (decoder.state !== "closed") decoder.close();
+ }
+}
diff --git a/src/lib/workQueue.test.ts b/src/lib/workQueue.test.ts
index c61cf34..f86a722 100644
--- a/src/lib/workQueue.test.ts
+++ b/src/lib/workQueue.test.ts
@@ -9,12 +9,14 @@ import { ProjectSession } from "./projectSession";
import { cancelSlopfabGeneration, enqueueSlopfabGeneration, resolveSlopfabPlan } from "./runtime";
import { saveEngineSettings, EMPTY_ENGINE_SETTINGS } from "./settings";
import { WorkQueue, type GenerationSubmission } from "./workQueue";
+import { saveSceneLastFrame } from "./sceneLastFrame";
import { downloadTemplateWeights, getWeightDownloadState } from "./weightDownloads";
vi.mock("@tauri-apps/api/core", () => ({ invoke: vi.fn() }));
vi.mock("@tauri-apps/api/event", () => ({ listen: vi.fn() }));
vi.mock("./generatedVideo", () => ({ saveGeneratedScene: vi.fn(), releaseRendered: vi.fn(async () => true) }));
vi.mock("./timelineThumbnails", () => ({ purgeTimelineThumbnails: vi.fn(async () => undefined) }));
+vi.mock("./sceneLastFrame", () => ({ saveSceneLastFrame: vi.fn(async () => "cache/scene-last/work-new.png") }));
vi.mock("./runtime", () => ({ cancelSlopfabGeneration: vi.fn(), enqueueSlopfabGeneration: vi.fn(), resolveSlopfabPlan: vi.fn() }));
const handlers = new Map<string, (event: { payload: unknown }) => void>();
const stops: (() => void)[] = [];
@@ -49,6 +51,37 @@ const finish = async (queue: WorkQueue, id: string) => {
};
describe("application work queue", () => {
+ it("resolves a linked first frame after the previous scene finishes encoding and saving", async () => {
+ const { queue, first } = setup();
+ first.update((config) => ({ ...config, generationJobs: [config.generationJobs[0], { ...config.generationJobs[0], id: "linked", usePreviousSceneLastFrame: true }] }));
+ const source = submission(first);
+ const linked = { ...submission(first), job: first.getSnapshot().config.generationJobs[1], request: { ...source.request, previousSceneId: source.job.id, referencePaths: ["pending.png", "subject.png"] } };
+ queue.enqueue(first, [source, linked]);
+ await waitFor(() => expect(enqueueSlopfabGeneration).toHaveBeenCalledTimes(1));
+ expect(saveSceneLastFrame).not.toHaveBeenCalled();
+ await finish(queue, queue.getSnapshot()[0].id);
+ await waitFor(() => expect(enqueueSlopfabGeneration).toHaveBeenCalledTimes(2));
+ expect(saveSceneLastFrame).toHaveBeenCalledWith("C:/First", "media/generated/result.mp4");
+ expect(vi.mocked(enqueueSlopfabGeneration).mock.calls[1][0].referencePaths).toEqual(["C:/First/cache/scene-last/work-new.png", "subject.png"]);
+ await finish(queue, queue.getSnapshot()[1].id);
+ const snapshot = JSON.parse(first.getSnapshot().config.generationJobs[1].generationSnapshot!);
+ expect(snapshot.previousSceneId).toBe(source.job.id);
+ expect(snapshot.referencePaths[0]).toBe("C:/First/cache/scene-last/work-new.png");
+ });
+
+ it("fails a dependent scene if the previous generation fails, even when an older video exists", async () => {
+ const { queue, first } = setup();
+ first.update((config) => ({ ...config, generationJobs: [{ ...config.generationJobs[0], outputRelativePath: "media/generated/old.mp4" }, { ...config.generationJobs[0], id: "linked", usePreviousSceneLastFrame: true }] }));
+ const source = submission(first);
+ queue.enqueue(first, [source, { ...source, job: first.getSnapshot().config.generationJobs[1], request: { ...source.request, previousSceneId: source.job.id } }]);
+ await waitFor(() => expect(enqueueSlopfabGeneration).toHaveBeenCalledTimes(1));
+ emit("slopfab-job", { jobId: queue.getSnapshot()[0].id, state: "failed", detail: "Render failed" });
+ await waitFor(() => expect(queue.getSnapshot()[1].status).toBe("failed"));
+ expect(queue.getSnapshot()[1].error).toContain("previous scene");
+ expect(saveSceneLastFrame).not.toHaveBeenCalled();
+ expect(enqueueSlopfabGeneration).toHaveBeenCalledTimes(1);
+ });
+
it("starts and advances generations while a weight download remains in progress", async () => {
let finishDownload: () => void = () => undefined;
vi.mocked(invoke).mockImplementation(async (command) => {
diff --git a/src/lib/workQueue.ts b/src/lib/workQueue.ts
index 0cc73b2..e106aa0 100644
--- a/src/lib/workQueue.ts
+++ b/src/lib/workQueue.ts
@@ -3,7 +3,8 @@ import { describeDiagnosticError, writeDiagnostic } from "./diagnostics";
import { GenerationTimingEstimator, type CompletedGenerationTiming, type GenerationTimingProgress } from "./generationTiming";
import { releaseRendered, saveGeneratedScene } from "./generatedVideo";
import { isTauri, saveProject } from "./persistence";
-import { generationAssetId, GENERATION_FRAME_RATE, type GenerationJob, type ProjectRecord, type ProjectConfig } from "./project";
+import { generationAssetId, GENERATION_FRAME_RATE, projectItemPath, sceneGenerationSnapshot, type GenerationJob, type ProjectRecord, type ProjectConfig } from "./project";
+import { saveSceneLastFrame } from "./sceneLastFrame";
import { ProjectSession, type ProjectWriter } from "./projectSession";
import { cancelSlopfabGeneration, enqueueSlopfabGeneration, resolveSlopfabPlan, type SlopfabGenerationRequest } from "./runtime";
import { withEngineSettings } from "./settings";
@@ -200,6 +201,18 @@ export class WorkQueue {
// background result can be attached to this project.
await work.session.save();
if (work.cancelled) continue;
+ if (work.request.previousSceneId) {
+ const previous = work.session.getSnapshot().config.generationJobs.find((job) => job.id === work.request.previousSceneId);
+ if (!previous?.outputRelativePath || previous.status !== "completed") {
+ throw new Error("Generate the previous scene successfully before using its last frame.");
+ }
+ const relativePath = await saveSceneLastFrame(next.folderPath, previous.outputRelativePath);
+ if (work.cancelled) continue;
+ work.request.referencePaths[0] = projectItemPath(next.folderPath, { relativePath })!;
+ const captured = work.config.generationJobs.find((job) => job.id === work.sceneId)!;
+ work.snapshot = sceneGenerationSnapshot(captured, work.request);
+ this.updateScene(work, { generationSnapshot: work.snapshot });
+ }
await purgeTimelineThumbnails(next.folderPath, work.sceneId).catch(() => undefined);
if (work.cancelled) continue;
const plan = await resolveSlopfabPlan(work.request, work.config);