AI media harness for planning, generating, and editing locally
TypeScript
Add image extension generation
Bitti01b9bc16e13f
diff --git a/src-tauri/src/app.rs b/src-tauri/src/app.rsindex 00815ed..6cc15e5 100644--- a/src-tauri/src/app.rs+++ b/src-tauri/src/app.rs@@ -189,6 +189,9 @@ pub fn run() {commands::artifacts::save_generated_image,commands::character_sheet::save_character_sheet_view,commands::character_sheet::combine_character_sheet,+ commands::extend_image::prepare_extend_image,+ commands::extend_image::save_extended_image,+ commands::extend_image::discard_extend_image,commands::character_sheet::discard_character_sheet_views,commands::artifacts::open_image_source,commands::artifacts::export_generated_image,diff --git a/src-tauri/src/commands/extend_image.rs b/src-tauri/src/commands/extend_image.rsnew file mode 100644index 0000000..7776ebd--- /dev/null+++ b/src-tauri/src/commands/extend_image.rs@@ -0,0 +1,389 @@+use crate::media::artifacts::generated_file_stem;+use crate::project::{paths::ProjectRoot, storage::read_project};+use image::{ImageEncoder, RgbaImage};+use serde::{Deserialize, Serialize};++#[derive(Clone, Copy, Deserialize, Serialize)]+pub(crate) struct ExtendBounds {+ x: i32,+ y: i32,+ width: u32,+ height: u32,+}++impl ExtendBounds {+ fn validate(self, width: u32, height: u32) -> Result<(), String> {+ if width == 0+ || height == 0+ || width > 8192+ || height > 8192+ || self.width == 0+ || self.height == 0+ || self.width > 8192+ || self.height > 8192+ || self.x.unsigned_abs() > 8192+ || self.y.unsigned_abs() > 8192+ {+ return Err("Invalid Extend image dimensions.".into());+ }+ let right = self.x + self.width as i32;+ let bottom = self.y + self.height as i32;+ if self.x >= width as i32 || self.y >= height as i32 || right <= 0 || bottom <= 0 {+ return Err("The Extend box must overlap the original image.".into());+ }+ if self.x >= 0 && self.y >= 0 && right <= width as i32 && bottom <= height as i32 {+ return Err("The Extend box must extend beyond the original image.".into());+ }+ Ok(())+ }+}++fn directory(job_id: &str) -> Result<String, String> {+ Ok(format!(+ "cache/extend-images/{}",+ generated_file_stem(job_id)?+ ))+}+fn write_png(path: &std::path::Path, image: &RgbaImage) -> Result<(), String> {+ let mut bytes = Vec::new();+ image::codecs::png::PngEncoder::new(&mut bytes)+ .write_image(+ image.as_raw(),+ image.width(),+ image.height(),+ image::ExtendedColorType::Rgba8,+ )+ .map_err(|e| e.to_string())?;+ crate::storage::atomic::write_atomically(path, &bytes)+}++fn padded_source(source: &RgbaImage, bounds: ExtendBounds) -> RgbaImage {+ RgbaImage::from_fn(bounds.width, bounds.height, |x, y| {+ // Edge replication gives inpainting color/texture context instead of black borders.+ *source.get_pixel(+ (x as i32 + bounds.x).clamp(0, source.width() as i32 - 1) as u32,+ (y as i32 + bounds.y).clamp(0, source.height() as i32 - 1) as u32,+ )+ })+}++#[tauri::command]+pub(crate) fn prepare_extend_image(+ folder_path: String,+ job_id: String,+ source_id: String,+ bounds: ExtendBounds,+) -> Result<(), String> {+ let root = ProjectRoot::open(&folder_path)?;+ let project = read_project(root.path())?;+ let asset = project+ .config+ .assets+ .iter()+ .find(|asset| asset.id == source_id && asset.kind == "image")+ .ok_or("The Extend source image no longer exists.")?;+ let source_path = if let Some(path) = &asset.relative_path {+ root.existing(path)?+ } else if let Some(path) = &asset.source_path {+ std::path::PathBuf::from(path)+ } else {+ return Err("The Extend source has no saved file.".into());+ };+ let (width, height) = image::image_dimensions(&source_path).map_err(|e| e.to_string())?;+ bounds.validate(width, height)?;+ if asset.width != Some(width) || asset.height != Some(height) {+ return Err(+ "The source image dimensions changed. Reopen the image before extending it.".into(),+ );+ }+ let source = image::open(source_path)+ .map_err(|e| e.to_string())?+ .to_rgba8();+ let cache = root.directory(&directory(&job_id)?)?;+ // Snapshot the original so file changes during generation cannot alter preservation.+ write_png(&cache.join("original.png"), &source)?;+ write_png(&cache.join("canvas.png"), &padded_source(&source, bounds))?;+ crate::storage::atomic::write_atomically(+ &cache.join("bounds.json"),+ &serde_json::to_vec(&bounds).map_err(|e| e.to_string())?,+ )+}++fn restore_original(result: &mut RgbaImage, source: &RgbaImage, bounds: ExtendBounds) {+ let left = bounds.x.max(0) as u32;+ let top = bounds.y.max(0) as u32;+ let right = (bounds.x + bounds.width as i32).min(source.width() as i32) as u32;+ let bottom = (bounds.y + bounds.height as i32).min(source.height() as i32) as u32;+ for y in top..bottom {+ for x in left..right {+ result.put_pixel(+ (x as i32 - bounds.x) as u32,+ (y as i32 - bounds.y) as u32,+ *source.get_pixel(x, y),+ );+ }+ }+}++#[tauri::command]+pub(crate) fn save_extended_image(+ folder_path: String,+ job_id: String,+) -> Result<super::artifacts::GeneratedImageFile, String> {+ let root = ProjectRoot::open(&folder_path)?;+ let cache = directory(&job_id)?;+ let bounds: ExtendBounds = serde_json::from_slice(+ &std::fs::read(root.existing(&format!("{cache}/bounds.json"))?)+ .map_err(|e| e.to_string())?,+ )+ .map_err(|e| e.to_string())?;+ let source = image::open(root.existing(&format!("{cache}/original.png"))?)+ .map_err(|e| e.to_string())?+ .to_rgba8();+ bounds.validate(source.width(), source.height())?;+ let summary =+ crate::rendered::summary(&job_id).ok_or("The extended image is no longer in memory.")?;+ if summary.frame_count != 1 || summary.width != bounds.width || summary.height != bounds.height+ {+ return Err("The generated image does not match the Extend box.".into());+ }+ let pixels = crate::rendered::frame(&job_id, 0)?.ok_or("The extended image has no pixels.")?;+ let mut result = RgbaImage::from_raw(bounds.width, bounds.height, pixels)+ .ok_or("Invalid generated image pixels.")?;+ restore_original(&mut result, &source, bounds);+ super::artifacts::write_generated_png_frame(+ &folder_path,+ &job_id,+ bounds.width,+ bounds.height,+ result.as_raw(),+ )+}++#[tauri::command]+pub(crate) fn discard_extend_image(folder_path: String, job_id: String) -> Result<(), String> {+ let root = ProjectRoot::open(&folder_path)?;+ let cache = directory(&job_id)?;+ if root.path().join(&cache).exists() {+ std::fs::remove_dir_all(root.existing(&cache)?).map_err(|e| e.to_string())?;+ }+ Ok(())+}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn extend_preserves_original_pixels_including_alpha_and_cropped_intersections() {+ let source = RgbaImage::from_fn(7, 5, |x, y| {+ image::Rgba([x as u8, y as u8, 80, (x * 25) as u8])+ });+ for bounds in [+ ExtendBounds {+ x: -3,+ y: -2,+ width: 12,+ height: 10,+ },+ ExtendBounds {+ x: 2,+ y: 1,+ width: 9,+ height: 7,+ },+ ExtendBounds {+ x: -2,+ y: -1,+ width: 5,+ height: 4,+ },+ ] {+ bounds.validate(source.width(), source.height()).unwrap();+ let mut result = RgbaImage::from_pixel(+ bounds.width,+ bounds.height,+ image::Rgba([200, 201, 202, 255]),+ );+ restore_original(&mut result, &source, bounds);+ for y in 0..bounds.height {+ for x in 0..bounds.width {+ let sx = x as i32 + bounds.x;+ let sy = y as i32 + bounds.y;+ let expected = if sx >= 0 && sy >= 0 && sx < 7 && sy < 5 {+ source.get_pixel(sx as u32, sy as u32).0+ } else {+ [200, 201, 202, 255]+ };+ assert_eq!(result.get_pixel(x, y).0, expected);+ }+ }+ let padded = padded_source(&source, bounds);+ assert_eq!(+ padded.get_pixel(0, 0),+ source.get_pixel(bounds.x.clamp(0, 6) as u32, bounds.y.clamp(0, 4) as u32)+ );+ }+ }++ #[test]+ fn extend_snapshots_source_validates_bounds_and_cleans_only_its_cache() {+ let folder = tempfile::tempdir().unwrap();+ let mut config = crate::tests::created_fixture();+ config.assets.push(serde_json::from_value(serde_json::json!({+ "id":"extend-source", "kind":"image", "name":"Source", "relativePath":"media/source.png", "mimeType":"image/png",+ "width":7, "height":5, "createdAt":config.created_at+ })).unwrap());+ crate::project::storage::write_project(folder.path(), &config).unwrap();+ let path = folder.path().to_string_lossy().into_owned();+ let root = ProjectRoot::open(&path).unwrap();+ let source = RgbaImage::from_pixel(7, 5, image::Rgba([30, 60, 90, 123]));+ write_png(+ &root.directory("media").unwrap().join("source.png"),+ &source,+ )+ .unwrap();+ let bounds = ExtendBounds {+ x: -3,+ y: 0,+ width: 10,+ height: 5,+ };+ prepare_extend_image(+ path.clone(),+ "extend-test".into(),+ "extend-source".into(),+ bounds,+ )+ .unwrap();+ prepare_extend_image(+ path.clone(),+ "extend-other".into(),+ "extend-source".into(),+ bounds,+ )+ .unwrap();+ let original_path = root+ .existing("cache/extend-images/extend-test/original.png")+ .unwrap();+ assert_eq!(image::open(&original_path).unwrap().to_rgba8(), source);+ assert_eq!(+ image::image_dimensions(+ root.existing("cache/extend-images/extend-test/canvas.png")+ .unwrap()+ )+ .unwrap(),+ (10, 5)+ );+ write_png(+ &root.existing("media/source.png").unwrap(),+ &RgbaImage::new(7, 5),+ )+ .unwrap();+ assert_eq!(image::open(original_path).unwrap().to_rgba8(), source);+ for invalid in [+ ExtendBounds { x: 7, ..bounds },+ ExtendBounds {+ x: 0,+ width: 7,+ ..bounds+ },+ ExtendBounds {+ width: 8193,+ ..bounds+ },+ ExtendBounds {+ x: i32::MIN,+ ..bounds+ },+ ] {+ assert!(prepare_extend_image(+ path.clone(),+ "invalid".into(),+ "extend-source".into(),+ invalid+ )+ .is_err());+ }+ assert!(prepare_extend_image(+ path.clone(),+ "../outside".into(),+ "extend-source".into(),+ bounds+ )+ .is_err());+ assert!(discard_extend_image(path.clone(), "../outside".into()).is_err());+ discard_extend_image(path.clone(), "extend-test".into()).unwrap();+ assert!(!root.path().join("cache/extend-images/extend-test").exists());+ assert!(root+ .existing("cache/extend-images/extend-other/original.png")+ .is_ok());+ assert!(root.existing("media/source.png").is_ok());+ discard_extend_image(path, "extend-test".into()).unwrap();+ }++ #[test]+ fn extend_finalizer_writes_a_lossless_png_using_the_source_snapshot() {+ struct GeneratedPixels;+ impl crate::rendered::FrameSource for GeneratedPixels {+ fn frame_rgba(&self, _: u32, width: u32, height: u32) -> Result<Vec<u8>, String> {+ Ok([210, 180, 150, 255].repeat(width as usize * height as usize))+ }+ }+ let folder = tempfile::tempdir().unwrap();+ crate::project::storage::write_project(folder.path(), &crate::tests::created_fixture())+ .unwrap();+ let path = folder.path().to_string_lossy().into_owned();+ let root = ProjectRoot::open(&path).unwrap();+ let job = "extend-finalizer-test";+ let cache = root.directory(&directory(job).unwrap()).unwrap();+ let bounds = ExtendBounds {+ x: -2,+ y: 1,+ width: 9,+ height: 6,+ };+ let source = RgbaImage::from_fn(7, 5, |x, y| image::Rgba([x as u8, y as u8, 80, 123]));+ write_png(&cache.join("original.png"), &source).unwrap();+ std::fs::write(+ cache.join("bounds.json"),+ serde_json::to_vec(&bounds).unwrap(),+ )+ .unwrap();+ crate::rendered::keep(+ crate::rendered::from_source(+ job,+ Box::new(GeneratedPixels),+ vec![],+ 1,+ 9,+ 6,+ 3,+ 9 * 6 * 3,+ 24.0,+ 0,+ 0,+ )+ .unwrap(),+ );+ let saved = save_extended_image(path.clone(), job.into()).unwrap();+ let image = image::open(root.existing(&saved.relative_path).unwrap())+ .unwrap()+ .to_rgba8();+ for y in 0..6 {+ for x in 0..9 {+ assert_eq!(+ image.get_pixel(x, y).0,+ if x >= 2 && y < 4 {+ source.get_pixel(x - 2, y + 1).0+ } else {+ [210, 180, 150, 255]+ }+ );+ }+ }+ crate::rendered::release(job);+ discard_extend_image(path, job.into()).unwrap();+ assert!(root.existing(&saved.relative_path).is_ok());+ }+}diff --git a/src-tauri/src/commands/mod.rs b/src-tauri/src/commands/mod.rsindex 3b18b4a..1909810 100644--- a/src-tauri/src/commands/mod.rs+++ b/src-tauri/src/commands/mod.rs@@ -2,6 +2,7 @@ pub(crate) mod agent;pub(crate) mod artifacts;pub(crate) mod binary;pub(crate) mod character_sheet;+pub(crate) mod extend_image;pub(crate) mod diagnostics;pub(crate) mod generation;pub(crate) mod media;diff --git a/src/lib/extendImage.test.ts b/src/lib/extendImage.test.tsnew file mode 100644index 0000000..1c18652--- /dev/null+++ b/src/lib/extendImage.test.ts@@ -0,0 +1,24 @@+import { expect, it } from "vitest";+import { extendRegions, initialExtendBounds } from "./extendImage";++it("partitions only new pixels for every combination of extension directions and crops", () => {+ for (const x of [-3, 0, 2]) for (const y of [-2, 0, 1]) for (const width of [5, 10]) for (const height of [4, 8]) {+ if (x >= 0 && y >= 0 && x + width <= 7 && y + height <= 5) continue;+ const { preserved, regions } = extendRegions({ width: 7, height: 5 }, { x, y, width, height });+ for (let py = 0; py < height; py++) for (let px = 0; px < width; px++) {+ const inside = px + x >= 0 && px + x < 7 && py + y >= 0 && py + y < 5;+ const contains = (r: typeof preserved) => px >= r.x && py >= r.y && px < r.x + r.width && py < r.y + r.height;+ expect(contains(preserved)).toBe(inside);+ expect(regions.filter(contains)).toHaveLength(inside ? 0 : 1);+ }+ }+});++it("rejects boxes with no original context, no extension, fractional or excessive dimensions", () => {+ for (const bounds of [+ { x: 100, y: 0, width: 20, height: 20 }, { x: -20, y: 0, width: 20, height: 20 },+ { x: 0, y: 0, width: 20, height: 20 }, { x: -1.5, y: 0, width: 20, height: 20 },+ { x: -20, y: 0, width: 8193, height: 20 }, { x: NaN, y: 0, width: 20, height: 20 },+ ]) expect(() => extendRegions({ width: 100, height: 100 }, bounds)).toThrow();+ expect(initialExtendBounds(8192, 400)).toEqual({ x: -0, y: -100, width: 8192, height: 600 });+});diff --git a/src/lib/extendImage.ts b/src/lib/extendImage.tsnew file mode 100644index 0000000..9a5b984--- /dev/null+++ b/src/lib/extendImage.ts@@ -0,0 +1,52 @@+import { createImageEditScene } from "./imageScene";+import { compileImagePrompt } from "./imagePrompt";+import type { ProjectAsset, ProjectConfig } from "./project";++/** Output bounds in original image pixels; negative coordinates extend left/up. */+export interface ExtendBounds { x: number; y: number; width: number; height: number }+export interface ExtendOptions { bounds: ExtendBounds; prompt: string; steps: number; seed: number }+export function initialExtendBounds(width: number, height: number): ExtendBounds {+ const dx = Math.min(Math.round(width / 4), Math.floor((8192 - width) / 2));+ const dy = Math.min(Math.round(height / 4), Math.floor((8192 - height) / 2));+ return { x: -dx, y: -dy, width: width + dx * 2, height: height + dy * 2 };+}++export function extendRegions(source: { width: number; height: number }, bounds: ExtendBounds) {+ const { x, y, width, height } = bounds;+ if (![source.width, source.height, x, y, width, height].every(Number.isSafeInteger)+ || source.width < 1 || source.height < 1 || source.width > 8192 || source.height > 8192+ || width < 1 || height < 1 || width > 8192 || height > 8192 || Math.abs(x) > 8192 || Math.abs(y) > 8192) {+ throw new Error("Use whole-pixel bounds and dimensions from 1 to 8192 pixels.");+ }+ const left = Math.max(0, -x), top = Math.max(0, -y);+ const right = Math.min(width, source.width - x), bottom = Math.min(height, source.height - y);+ if (right <= left || bottom <= top) throw new Error("The box must overlap the original image.");+ const regions = [+ { x: 0, y: 0, width, height: top },+ { x: 0, y: bottom, width, height: height - bottom },+ { x: 0, y: top, width: left, height: bottom - top },+ { x: right, y: top, width: width - right, height: bottom - top },+ ].filter((region) => region.width > 0 && region.height > 0);+ if (!regions.length) throw new Error("Extend the box beyond at least one edge of the original image.");+ return { preserved: { x: left, y: top, width: right - left, height: bottom - top }, regions };+}++export function compileExtendImage(config: ProjectConfig, source: ProjectAsset, sourceRelativePath: string, options: ExtendOptions) {+ if (!source.width || !source.height) throw new Error("Wait for the source image dimensions to load.");+ const { regions } = extendRegions({ width: source.width, height: source.height }, options.bounds);+ if (!Number.isInteger(options.steps) || options.steps < 2 || options.steps > 1000) throw new Error("Steps must be a whole number from 2 to 1000.");+ if (!Number.isSafeInteger(options.seed) || options.seed < -1) throw new Error("Seed must be -1 for random or a non-negative safe integer.");+ const scene = createImageEditScene({ relativePath: sourceRelativePath, name: source.name, width: options.bounds.width, height: options.bounds.height });+ scene.steps = options.steps; scene.seed = options.seed;+ scene.nodes[0].description = "Extend the scene naturally beyond its original framing. Continue the environment, objects, textures, perspective and lighting into the new space. Preserve the original scene and visual style. Replace the stretched edge placeholders with coherent new content. " + options.prompt.trim();+ const compiled = compileImagePrompt({ ...config, settings: { ...config.settings, defaultLook: null }, imageScene: scene });+ // The rectangular editor needs a little original context at each seam.+ // The native finalizer restores all original pixels after every region is generated.+ const overlap = 32;+ const edits = regions.map((region) => {+ const x = Math.max(0, region.x - overlap), y = Math.max(0, region.y - overlap);+ return { x, y, width: Math.min(options.bounds.width, region.x + region.width + overlap) - x,+ height: Math.min(options.bounds.height, region.y + region.height + overlap) - y, prompt: compiled.prompt };+ });+ return { ...compiled, scene, edits };+}diff --git a/src/lib/extendWork.test.ts b/src/lib/extendWork.test.tsnew file mode 100644index 0000000..fb7e387--- /dev/null+++ b/src/lib/extendWork.test.ts@@ -0,0 +1,134 @@+// @vitest-environment jsdom+import { waitFor } from "@testing-library/react";+import { invoke } from "@tauri-apps/api/core";+import { listen } from "@tauri-apps/api/event";+import { afterEach, beforeEach, expect, it, vi } from "vitest";+import { releaseRendered } from "./generatedVideo";+import { createProjectConfig, parseProjectConfig, type ProjectRecord } from "./project";+import { cancelSlopfabGeneration, enqueueSlopfabGeneration, resolveSlopfabPlan } from "./runtime";+import { EMPTY_ENGINE_SETTINGS, type GeneratorTemplate } from "./settings";+import { WorkQueue } from "./workQueue";+import type { ExtendOptions } from "./extendImage";++vi.mock("@tauri-apps/api/core", () => ({ invoke: vi.fn() }));+vi.mock("@tauri-apps/api/event", () => ({ listen: vi.fn() }));+vi.mock("./generatedVideo", () => ({ releaseRendered: vi.fn(), saveGeneratedScene: vi.fn() }));+vi.mock("./runtime", () => ({ cancelSlopfabGeneration: vi.fn(), enqueueSlopfabGeneration: vi.fn(), resolveSlopfabPlan: vi.fn() }));+const handlers = new Map<string, (event: { payload: unknown }) => void>();+const template: GeneratorTemplate = { id: "h3", name: "H3", modelType: "minimax-h3", defaultSteps: 20, attention: "sage2", paths: { ...EMPTY_ENGINE_SETTINGS, transformer: "C:/h3.safetensors" } };+const options = (): ExtendOptions => ({ bounds: { x: -64, y: -32, width: 256, height: 160 }, prompt: "Continue the forest", steps: 25, seed: 0 });+const result = { relativePath: "media/generated/extended.png", width: 256, height: 160 };+let stop: (() => void) | undefined;+beforeEach(() => {+ localStorage.clear(); vi.resetAllMocks(); handlers.clear();+ Object.assign(window, { __TAURI_INTERNALS__: {} });+ vi.mocked(listen).mockImplementation((async (name: string, handler: (event: { payload: unknown }) => void) => { handlers.set(name, handler); return () => {}; }) as typeof listen);+ vi.mocked(resolveSlopfabPlan).mockImplementation(async (request) => ({ alignedFrames: 1, canvasWidth: request.canvasWidth, canvasHeight: request.canvasHeight } as Awaited<ReturnType<typeof resolveSlopfabPlan>>));+ vi.mocked(enqueueSlopfabGeneration).mockResolvedValue(undefined);+ vi.mocked(cancelSlopfabGeneration).mockResolvedValue(true);+ vi.mocked(releaseRendered).mockResolvedValue(true);+ vi.mocked(invoke).mockImplementation(async (command) => command === "save_extended_image" ? result : undefined);+});+afterEach(() => { stop?.(); delete (window as unknown as Record<string, unknown>).__TAURI_INTERNALS__; });+function setup() {+ const config = createProjectConfig({ name: "Extend", prompt: "Forest", generationType: "image", aspectRatio: "1:1", resolution: "768p", targetDurationSeconds: 60 });+ config.assets = ["root", "source"].map((id) => ({ id, kind: "image", name: id, relativePath: `media/${id}.png`, mimeType: "image/png", width: 128, height: 96, createdAt: config.createdAt, ...(id === "source" ? { parentAssetId: "root" } : {}) }));+ config.imageScene!.outputAssetId = "source";+ const writer = vi.fn(async (record: ProjectRecord) => record);+ const queue = new WorkQueue(writer); stop = queue.start();+ return { queue, session: queue.project({ folderPath: "C:/Extend", config }), writer };+}+const emit = (state: string, jobId: string) => handlers.get("slopfab-job")?.({ payload: { state, jobId, detail: state } });+async function submitted() {+ await waitFor(() => expect(enqueueSlopfabGeneration).toHaveBeenCalledTimes(1));+ return vi.mocked(enqueueSlopfabGeneration).mock.calls[0][0];+}++it.each([false, true])("extends a frozen source and saves a child with a durable snapshot (scene changed: %s)", async (changed) => {+ const { queue, session } = setup();+ const initial = structuredClone(session.getSnapshot().config);+ const input = options();+ queue.enqueueExtendImage(session, template, "source", input);+ queue.enqueueExtendImage(session, template, "source", input);+ input.bounds.width = 999; input.prompt = "Changed";+ expect(queue.getSnapshot()).toHaveLength(1);+ const request = await submitted();+ const { jobId } = request;+ expect(request).toMatchObject({ stillImage: true, frames: 1, seed: 0, steps: 25, canvasWidth: 256, canvasHeight: 160,+ imageEdit: { sourceRelativePath: `cache/extend-images/${jobId}/canvas.png` }, referencePaths: [`C:/Extend/cache/extend-images/${jobId}/canvas.png`] });+ expect(request.imageEdit!.edits).toHaveLength(4);+ expect(request.prompt).toContain("Continue the forest");+ expect(request.prompt).not.toContain("Changed");+ expect(invoke).toHaveBeenCalledWith("prepare_extend_image", { folderPath: "C:/Extend", jobId, sourceId: "source", bounds: options().bounds });+ if (changed) session.update((config) => ({ ...config, imageScene: { ...config.imageScene!, nodes: config.imageScene!.nodes.map((node) => ({ ...node, description: "New prompt" })) } }));+ emit("framesReady", jobId);+ await waitFor(() => expect(queue.getSnapshot()[0].status).toBe("completed"));+ const reopened = parseProjectConfig(JSON.parse(JSON.stringify(session.getSnapshot().config)));+ const asset = reopened.assets.at(-1)!;+ expect(asset).toMatchObject({ id: jobId, name: "Extended - source", parentAssetId: "root", mimeType: "image/png", ...result,+ imageGeneration: { usedSeed: 0, scene: { steps: 25, seed: 0, sourceImage: { ...result } } } });+ expect(reopened.assets.slice(0, 2)).toEqual(initial.assets);+ expect(reopened.imageScene!.outputAssetId).toBe(changed ? "source" : jobId);+ expect(JSON.stringify(asset)).not.toContain("cache/extend-images");+ expect(invoke).toHaveBeenCalledWith("save_extended_image", { folderPath: "C:/Extend", jobId });+ await waitFor(() => expect(invoke).toHaveBeenCalledWith("discard_extend_image", { folderPath: "C:/Extend", jobId }));+ expect(releaseRendered).toHaveBeenCalledWith(jobId);+});++it("passes prompt reference images and refmods to the extension", async () => {+ const { queue, session } = setup();+ session.update((config) => ({ ...config, references: [{ id: "trees", name: "Trees", kind: "image", description: "Tall pines", relativePath: "references/trees.png", intendedUse: [], createdAt: config.createdAt,+ refmods: [{ id: "forest", name: "Forest", relativePath: "references/forest.safetensors", strength: 1, copies: 1 }] }] }));+ queue.enqueueExtendImage(session, template, "source", { ...options(), prompt: "More @[ref:trees]" });+ const request = await submitted();+ expect(request.referencePaths).toHaveLength(2);+ expect(request.referencePaths[1]).toBe("C:/Extend/references/trees.png");+ expect(request.refmods).toEqual([{ path: "C:/Extend/references/forest.safetensors", strength: 1, copies: 1 }]);+ expect(request.prompt).not.toContain("@[ref:");+ emit("framesReady", request.jobId);+ await waitFor(() => expect(queue.getSnapshot()[0].status).toBe("completed"));+ expect(session.getSnapshot().config.assets.at(-1)!.imageGeneration!.references[0].id).toBe("trees");+});++it.each(["failed", "cancelled"])("does not publish a result after generation is %s", async (state) => {+ const { queue, session } = setup();+ queue.enqueueExtendImage(session, template, "source", options());+ const request = await submitted();+ if (state === "cancelled") { await queue.cancel(request.jobId); expect(cancelSlopfabGeneration).toHaveBeenCalledWith(request.jobId); }+ emit(state, request.jobId);+ await waitFor(() => expect(queue.getSnapshot()[0].status).toBe(state));+ await waitFor(() => expect(invoke).toHaveBeenCalledWith("discard_extend_image", { folderPath: "C:/Extend", jobId: request.jobId }));+ expect(session.getSnapshot().config.assets).toHaveLength(2);+ expect(vi.mocked(invoke).mock.calls.some(([command]) => command === "save_extended_image")).toBe(false);+});++it("cleans preparation failures and lets the next extension proceed", async () => {+ const { queue, session } = setup();+ vi.mocked(invoke).mockRejectedValueOnce(new Error("Source missing"));+ queue.enqueueExtendImage(session, template, "source", options());+ await waitFor(() => expect(queue.getSnapshot()[0].status).toBe("failed"));+ expect(enqueueSlopfabGeneration).not.toHaveBeenCalled();+ queue.enqueueExtendImage(session, template, "source", options());+ const request = await submitted(); emit("framesReady", request.jobId);+ await waitFor(() => expect(queue.getSnapshot()[1].status).toBe("completed"));+});++it("retains the finished image when saving the project fails and retries without regenerating", async () => {+ const { queue, session, writer } = setup();+ queue.enqueueExtendImage(session, template, "source", options());+ const request = await submitted();+ writer.mockRejectedValueOnce(new Error("Disk full")); emit("framesReady", request.jobId);+ await waitFor(() => expect(queue.getSnapshot()[0]).toMatchObject({ status: "failed", needsSave: true }));+ expect(session.getSnapshot().config.assets).toHaveLength(3);+ await queue.retrySave(request.jobId);+ expect(queue.getSnapshot()[0]).toMatchObject({ status: "completed", needsSave: false });+ expect(enqueueSlopfabGeneration).toHaveBeenCalledTimes(1);+});++it("rejects invalid bounds, seeds and missing references before queueing", () => {+ const { queue, session } = setup();+ for (const input of [{ ...options(), bounds: { x: 0, y: 0, width: 128, height: 96 } }, { ...options(), seed: -2 }, { ...options(), prompt: "@[ref:missing]" }]) {+ expect(() => queue.enqueueExtendImage(session, template, "source", input)).toThrow();+ }+ expect(queue.getSnapshot()).toHaveLength(0);+});diff --git a/src/lib/workQueue.ts b/src/lib/workQueue.tsindex b7ee740..9907903 100644--- a/src/lib/workQueue.ts+++ b/src/lib/workQueue.ts@@ -3,6 +3,7 @@ import { invoke } from "@tauri-apps/api/core";import { createImageEditScene, createImageScene, imageScenePrompt } from "./imageScene";import { compileImageEdits, imageEditDebugPrompt } from "./imageEditing";import { compileImagePrompt } from "./imagePrompt";+import { compileExtendImage, type ExtendOptions } from "./extendImage";import { CHARACTER_SHEET_ORDER, CHARACTER_SHEET_VIEWS, characterSheetDimensions, compileCharacterSheet, type CharacterSheetOptions } from "./characterSheet";import { completeImageDraft, imageFamilyRoot, imageGenerationSnapshot, saveImageDraft } from "./imageHistory";import type { ImageGenerationSnapshot } from "./project";@@ -57,6 +58,7 @@ export interface WorkItem {settings: { frames: number; steps: number; seed: number; canvasWidth: number; canvasHeight: number };}interface PendingWork {+ extend?: { sourceId: string; name: string; options: ExtendOptions };characterSheet?: {views: ReturnType<typeof compileCharacterSheet>; sourceId: string; name: string; index: number;dimensions: ReturnType<typeof characterSheetDimensions>;@@ -263,6 +265,36 @@ export class WorkQueue {settings: { frames: 1, steps: request.steps, seed: request.seed, canvasWidth: width, canvasHeight: height } }];this.publish(); this.icons.yieldToVideo(); void this.pump();}+ enqueueExtendImage(session: ProjectSession, template: GeneratorTemplate, sourceId: string, options: ExtendOptions) {+ if (!isTauri()) throw new Error("Extend requires the desktop app.");+ if (template.mode === "animate") throw new Error("Choose a prompt generator for Extend.");+ const current = session.getSnapshot().config;+ const source = current.assets.find((asset) => asset.id === sourceId && asset.kind === "image" && !asset.imageDraft);+ if (!source || (!source.relativePath && !source.sourcePath)) throw new Error("Select a saved image to extend.");+ const projectKey = projectQueueKey(session.record);+ if (this.items.some((item) => item.projectKey === projectKey && item.kind === "image" && item.imageAssetId === sourceId && isWorkActive(item))) return;+ const config = structuredClone({ ...current, providerSettings: { ...current.providerSettings,+ slopfab: engineProviderSetting(template.paths, current.providerSettings.slopfab, template.attention, template.loras, "prompt", template.additionalSafetensors, false, template.sources) } });+ const id = `extend-${crypto.randomUUID()}`;+ const sourceRelativePath = `cache/extend-images/${id}/canvas.png`;+ const compiled = compileExtendImage(config, source, sourceRelativePath, options);+ const referencePaths = [projectItemPath(session.record.folderPath, { relativePath: sourceRelativePath })!,+ ...compiled.references.flatMap((reference) => referenceImages(reference).map((image) => projectItemPath(session.record.folderPath, image)!))];+ const request: SlopfabGenerationRequest = { jobId: id, stillImage: true, frames: 1, prompt: compiled.prompt,+ canvasWidth: options.bounds.width, canvasHeight: options.bounds.height, steps: generationStepsWithLoras(options.steps, config), seed: imageGenerationSeed(options.seed),+ referencePaths, refmods: referenceRefmodInputs(session.record.folderPath, compiled.references),+ imageEdit: { sourceRelativePath, edits: compiled.edits } };+ let finish!: () => void;+ const done = new Promise<void>((resolve) => { finish = resolve; });+ this.work.set(id, { id, image: true, extend: { sourceId, name: `Extended - ${source.name}`, options: structuredClone(options) }, imageParentId: sourceId,+ imageGeneration: { ...imageGenerationSnapshot({ ...config, imageScene: compiled.scene }, compiled.prompt, template.id), usedSeed: request.seed },+ session, sceneId: current.imageScene?.nodes[0].id ?? "image-root", config, snapshot: JSON.stringify(current.imageScene), request, submitted: false, cancelled: false, done, finish });+ this.items = [...this.items, { id, kind: "image", imageAssetId: sourceId, projectKey, folderPath: session.record.folderPath, projectName: config.name,+ sceneId: this.work.get(id)!.sceneId, title: `Extend · ${source.name}`, submittedAt: new Date().toISOString(), status: "queued", progress: 0,+ detail: "Waiting to extend image", error: null, completionAt: null, cancelling: false, needsSave: false,+ settings: { frames: 1, steps: request.steps, seed: request.seed, canvasWidth: request.canvasWidth, canvasHeight: request.canvasHeight } }];+ this.publish(); this.icons.yieldToVideo(); void this.pump();+ }enqueueCharacterSheet(session: ProjectSession, template: GeneratorTemplate, sourceId: string, options?: CharacterSheetOptions) {if (!isTauri()) throw new Error("Character sheet generation requires the desktop app.");if (template.mode === "animate") throw new Error("Choose a prompt generator for the character sheet.");@@ -351,6 +383,10 @@ export class WorkQueue {await work.session.save();if (work.cancelled) continue;if (work.characterSheet) { await this.generateCharacterSheet(work); continue; }+ if (work.extend) {+ await invoke("prepare_extend_image", { folderPath: next.folderPath, jobId: work.id, sourceId: work.extend.sourceId, bounds: work.extend.options.bounds });+ if (work.cancelled) continue;+ }if (work.request.previousSceneId) {const previous = work.session.getSnapshot().config.generationJobs.find((job) => job.id === work.request.previousSceneId);if (!previous?.latentRelativePath || previous.status !== "completed") {@@ -378,6 +414,8 @@ export class WorkQueue {await work.done;} catch (reason) { this.fail(work, reason); }finally {+ if (work.extend) await invoke("discard_extend_image", { folderPath: next.folderPath, jobId: work.id }).catch((reason) =>+ writeDiagnostic("error", "work-queue", "extend.cleanup_failed", describeDiagnosticError(reason), { workId: work.id }));await releaseReferenceVideos(work.request.referenceVideoIds ?? []).catch((reason) =>writeDiagnostic("error", "work-queue", "references.release_failed", describeDiagnosticError(reason), { workId: work.id }));delete work.request.referenceVideoIds;@@ -608,17 +646,28 @@ export class WorkQueue {private async saveImage(work: PendingWork) {this.patch(work.id, { status: "encoding", progress: 0.9, detail: "Saving image", completionAt: null });try {- const saved = await invoke<{ relativePath: string; width: number; height: number }>("save_generated_image", { folderPath: work.session.record.folderPath, jobId: work.id, ...(work.request.imageEdit ? { format: "png" } : {}) });- work.session.update((current) => work.imageDraftId ? completeImageDraft(current, work.imageDraftId, {- id: work.imageDraftId, kind: "image", name: `Image ${current.assets.filter((asset) => asset.kind === "image" && !asset.imageDraft).length + 1}`,- ...saved, imageGeneration: work.imageGeneration, mimeType: work.request.imageEdit ? "image/png" : "image/jpeg", createdAt: new Date().toISOString(),- }) : ({ ...current,- ...(current.imageScene?.outputAssetId === work.config.imageScene?.outputAssetId ? {- thumbnail: saved.relativePath,- imageScene: { ...(work.request.imageEdit && JSON.stringify(current.imageScene) === work.snapshot ? createImageEditScene({ ...saved, name: "Edited image" }, current.imageScene ?? undefined) : current.imageScene ?? createImageScene()), outputAssetId: work.id },- } : {}),- assets: [...current.assets, { id: work.id, kind: "image", name: `Image ${current.assets.filter((asset) => asset.kind === "image").length + 1}`, ...saved, imageGeneration: work.imageGeneration, mimeType: work.request.imageEdit ? "image/png" : "image/jpeg", ...(work.imageParentId ? { parentAssetId: imageFamilyRoot(current.assets, work.imageParentId) } : {}), createdAt: new Date().toISOString() }],- }));+ const saved = await invoke<{ relativePath: string; width: number; height: number }>(work.extend ? "save_extended_image" : "save_generated_image", { folderPath: work.session.record.folderPath, jobId: work.id, ...(!work.extend && work.request.imageEdit ? { format: "png" } : {}) });+ if (work.extend) {+ const scene = createImageEditScene({ ...saved, name: work.extend.name }, { ...work.config.imageScene!, steps: work.request.steps, seed: work.request.seed });+ work.imageGeneration = { ...work.imageGeneration!, scene };+ work.session.update((current) => ({ ...current,+ ...(current.imageScene?.outputAssetId === work.extend!.sourceId && JSON.stringify(current.imageScene) === work.snapshot+ ? { thumbnail: saved.relativePath, imageScene: { ...scene, outputAssetId: work.id } } : {}),+ assets: [...current.assets, { id: work.id, kind: "image", name: work.extend!.name, ...saved, mimeType: "image/png",+ parentAssetId: imageFamilyRoot(current.assets, work.extend!.sourceId), imageGeneration: work.imageGeneration, createdAt: new Date().toISOString() }],+ }));+ } else {+ work.session.update((current) => work.imageDraftId ? completeImageDraft(current, work.imageDraftId, {+ id: work.imageDraftId, kind: "image", name: `Image ${current.assets.filter((asset) => asset.kind === "image" && !asset.imageDraft).length + 1}`,+ ...saved, imageGeneration: work.imageGeneration, mimeType: work.request.imageEdit ? "image/png" : "image/jpeg", createdAt: new Date().toISOString(),+ }) : ({ ...current,+ ...(current.imageScene?.outputAssetId === work.config.imageScene?.outputAssetId ? {+ thumbnail: saved.relativePath,+ imageScene: { ...(work.request.imageEdit && JSON.stringify(current.imageScene) === work.snapshot ? createImageEditScene({ ...saved, name: "Edited image" }, current.imageScene ?? undefined) : current.imageScene ?? createImageScene()), outputAssetId: work.id },+ } : {}),+ assets: [...current.assets, { id: work.id, kind: "image", name: `Image ${current.assets.filter((asset) => asset.kind === "image").length + 1}`, ...saved, imageGeneration: work.imageGeneration, mimeType: work.request.imageEdit ? "image/png" : "image/jpeg", ...(work.imageParentId ? { parentAssetId: imageFamilyRoot(current.assets, work.imageParentId) } : {}), createdAt: new Date().toISOString() }],+ }));+ }try { await work.session.save(); this.patch(work.id, { status: "completed", progress: 1, detail: "Image saved" }); }catch (reason) { this.patch(work.id, { status: "failed", progress: 1, needsSave: true, detail: "Image created; project save failed", error: describeDiagnosticError(reason) }); }} catch (reason) { this.fail(work, reason); }