AI media harness for planning, generating, and editing locally
TypeScript
import { effectiveLoras, loraSnapshot } from "./loras";
import { completeSceneGeneration, sceneInput } from "./sceneHistory";
import { generatedFilePaths } from "./generationCleanup";
import { albumCoverPrompt, musicSongSchema, musicSettingsForModel, newSongVariation, updateSong, type MusicSong } from "./music";
import { albumCoverGenerator } from "./referenceIconSettings";
import { templateSupportsMusic } from "./settings";
import { imageCfgScale } from "./generatorModels";
import { activeGenerationEffects } from "./generationEffects";
import { imageSettings, imageGenerationDimensions, imageGenerationSizeError } from "./imageSettings";
import { listen } from "@tauri-apps/api/event";
import { invoke } from "@tauri-apps/api/core";
import { createImageEditScene, createImageScene, imageScenePrompt } from "./imageScene";
import { compileImageEdits, imageEditDebugPrompt, imageEditCanvasSize, imageEditRegionSize } from "./imageEditing";
import { compileImagePrompt } from "./imagePrompt";
import { compileExtendImage, type ExtendOptions } from "./extendImage";
import { CHARACTER_SHEET_ORDER, CHARACTER_SHEET_VIEWS, characterSheetDimensions, characterSheetViewIndices, characterSheetViews, compileCharacterSheet, type CharacterSheetOptions } from "./characterSheet";
import { completeImageDraft, createTemplateImageDraft, imageFamilyRoot, imageGenerationSnapshot, saveImageDraft } from "./imageHistory";
import type { ImageGenerationSnapshot, ProjectAsset } from "./project";
import { projectItemPath, referenceDefinition, referenceImages, referenceRefmodInputs } from "./project";
import { generationSigmaShiftsWithLoras, engineProviderSetting, generatorModel, templateSupportsEditing, templateSupportsOutpainting, templateSupportsImages, templateSupportsVideo, defaultGeneratorTemplate, loadGeneratorTemplateSettings, type GeneratorTemplate } from "./settings";
import { describeDiagnosticError, writeDiagnostic } from "./diagnostics";
import { GenerationTimingEstimator, type CompletedGenerationTiming, type GenerationTimingProgress } from "./generationTiming";
import { validGenerationPreview, type GenerationPreview } from "./generationPreview";
import { loadGenerationPreviewsEnabled, subscribeGenerationPreviews } from "./generationPreviewSettings";
import { releaseRendered, saveGeneratedScene, saveGenerationPreview } from "./generatedVideo";
import { isTauri, saveProject } from "./persistence";
import { continuationBlocker, generationAssetId, generationLatentPath, GENERATION_FRAME_RATE, sceneSiblings, sceneOutputFrames, sceneGenerationSnapshot, type SceneInput, type GenerationJob, type ProjectRecord, type ProjectConfig } from "./project";
import { ProjectSession, type ProjectWriter } from "./projectSession";
import { cancelSlopfabGeneration, enqueueSlopfabGeneration, resolveSlopfabPlan, type SlopfabGenerationRequest } from "./runtime";
import { generationStepsWithLoras, withEngineSettings } from "./settings";
import { remoteWorkerSelected } from "./workers";
import { purgeTimelineThumbnails } from "./timelineThumbnails";
import { ReferenceIconWork } from "./referenceIconWork";
import { prepareReferenceVideos, releaseReferenceVideos } from "./referenceVideo";
import { prepareReferenceAudios, releaseReferenceAudios } from "./referenceAudio";
import { joinedSceneSegments, sceneArchiveSegments } from "./continuationMedia";
import { bridgedSceneSegments, longShotBlocker, longShotContinuationInputs, longShotInputs, longShotPosition } from "./longShot";
import type { SceneMediaSegment } from "./project";
import { exportReferenceRefmod, refmodExportBlocker, refmodExportRequest, type RefmodExportTarget } from "./refmodExport";
export type WorkStatus = "queued" | "preparing" | "generating" | "encoding" | "completed" | "failed" | "cancelled";
export interface GenerationSubmission { job: GenerationJob; request: SlopfabGenerationRequest; snapshot: string; template?: GeneratorTemplate; upscaleSource?: import("./project").SceneGenerationVersion }
export const MAX_IMAGE_BATCH_COUNT = 100;
export const MAX_MUSIC_BATCH_COUNT = 100;
function imageGenerationSeed(seed: number): number {
if (seed !== -1) return seed;
// Resolve before submission so history stores exactly what the engine uses.
// Keep all 53 bits exactly representable in JSON and the editable seed field.
const [high, low] = crypto.getRandomValues(new Uint32Array(2));
return (high & 0x1fffff) * 0x100000000 + low;
}
export interface WorkItem {
/** Exact source runs captured at submission, independent of later draft edits. */
sourceWorkIds?: string[];
imageAssetId?: string | null;
imageDraftId?: string;
kind?: "reference-icons" | "image" | "refmod" | "music" | "album-cover";
id: string;
projectKey: string;
folderPath: string;
projectName: string;
sceneId: string;
title: string;
submittedAt: string;
status: WorkStatus;
progress: number;
detail: string;
error: string | null;
completionAt: number | null;
cancelling: boolean;
needsSave: boolean;
preview?: GenerationPreview;
settings: { frames: number; steps: number; audioSteps?: number; videoSigmaShift?: number; audioSigmaShift?: number; latentUpscale?: boolean; seed: number; canvasWidth: number; canvasHeight: number };
}
interface PendingWork {
lastPreview?: GenerationPreview;
cancellation?: Promise<void>;
generatorName?: string;
albumCoverSongId?: string;
upscaleSource?: import("./project").SceneGenerationVersion;
music?: MusicSong;
sceneInput?: SceneInput;
extend?: { sourceId: string; name: string; options: ExtendOptions };
characterSheet?: {
views: ReturnType<typeof compileCharacterSheet>; sourceId: string; name: string; index: number;
dimensions: ReturnType<typeof characterSheetDimensions>;
order: number[];
ready?: { resolve: () => void; reject: (reason: Error) => void };
};
outputFrames?: number;
continuationSegments?: SceneMediaSegment[];
bridgeSegments?: SceneMediaSegment[];
imageDraftId?: string;
/** The family a regenerated image joins. */
imageParentId?: string;
image?: boolean;
imageGeneration?: ImageGenerationSnapshot;
refmod?: RefmodExportTarget;
id: string;
session: ProjectSession;
sceneId: string;
request: SlopfabGenerationRequest;
config: ProjectConfig;
snapshot: string;
cancelled: boolean;
submitted: boolean;
done: Promise<void>;
finish: () => void;
}
interface JobEvent { jobId: string; state: string; detail: string; output?: CompletedGenerationTiming | null }
export const isWorkActive = (work: WorkItem) => !["completed", "failed", "cancelled"].includes(work.status);
export const projectQueueKey = (record: ProjectRecord) => {
const path = record.folderPath.replaceAll("\\", "/").replace(/\/$/, "");
const normalized = /^[a-z]:\//i.test(path) || path.startsWith("//") ? path.toLowerCase() : path;
return `${normalized}::${record.config.id}`;
};
/** App-owned FIFO. A render includes encoding and saving before the next one
* starts, so native frame buffers cannot evict work still being encoded. */
export class WorkQueue {
private projects = new Map<string, ProjectSession>();
private work = new Map<string, PendingWork>();
private items: readonly WorkItem[] = [];
private listeners = new Set<() => void>();
private pumping = false;
private timing = new GenerationTimingEstimator();
private connections = 0;
private disconnect: (() => void) | null = null;
private icons = new ReferenceIconWork((item) => {
this.items = this.items.some((current) => current.id === item.id)
? this.items.map((current) => current.id === item.id ? item : current)
: [...this.items, item];
this.publish();
}, () => { void this.pump(); });
ready: Promise<void> = Promise.resolve();
constructor(private writer: ProjectWriter = saveProject) {}
getSnapshot = () => this.items;
projectRecord(key: string): ProjectRecord | undefined {
const session = this.projects.get(key);
return session ? { ...session.record, config: session.getSnapshot().config } : undefined;
}
updateBlockReason = (): string | null => {
if (this.items.some(isWorkActive) || this.icons.confirmationCount() > 0) {
return "Finish or cancel queued work before installing an update.";
}
if (this.items.some((item) => item.needsSave) || [...this.projects.values()].some((session) => {
const state = session.getSnapshot();
return state.dirty || state.saving || state.saveError;
})) return "Save your project changes and generated videos before installing an update.";
return null;
};
getIconConfirmationCount = () => this.icons.confirmationCount();
answerIconConfirmation = (confirmed: boolean, remember: boolean) => this.icons.answerConfirmation(confirmed, remember);
subscribe = (listener: () => void) => { this.listeners.add(listener); return () => { this.listeners.delete(listener); }; };
private publish() { this.listeners.forEach((listener) => listener()); }
private patch(id: string, patch: Partial<WorkItem>) {
if (patch.cancelling || (patch.status && ["completed", "failed", "cancelled"].includes(patch.status))) patch = { ...patch, preview: undefined };
this.items = this.items.map((item) => item.id === id ? { ...item, ...patch } : item);
this.publish();
}
project(record: ProjectRecord): ProjectSession {
const key = projectQueueKey(record);
let session = this.projects.get(key);
if (!session) {
// The queue belongs to this app run. A persisted in-flight status from a
// previous run has no worker behind it and must be restartable.
const config = isTauri() ? { ...record.config, musicSongs: (record.config.musicSongs ?? []).map((song) =>
["queued", "generating"].includes(song.status) ? { ...song, status: "failed" as const, error: "Generation was interrupted when Slopus closed. Generate again to restart." } : song), generationJobs: record.config.generationJobs.map((job) =>
["queued", "generating", "ready"].includes(job.status) ? { ...job, status: "failed" as const, stage: "failed" as const, error: "Generation was interrupted when Slopus closed. Generate again to restart." } : job) } : record.config;
session = new ProjectSession({ ...record, config }, this.writer, () => {
const paths = new Set<string>();
for (const work of this.work.values()) {
if (projectQueueKey(work.session.record) === key && this.items.some(item => item.id === work.id && isWorkActive(item))) generatedFilePaths(work.config, paths, record.folderPath);
}
return [...paths];
});
this.projects.set(key, session);
}
this.icons.watch(session);
return session;
}
hasActiveProject(record: ProjectRecord) {
const session = this.projects.get(projectQueueKey(record));
return Boolean(session && this.icons.hasActiveProject(session)) || this.items.some((item) => item.projectKey === projectQueueKey(record) && isWorkActive(item));
}
isReferenceIconPending(session: ProjectSession, referenceId: string) {
return this.icons.isPending(session, referenceId);
}
regenerateReferenceIcon(session: ProjectSession, referenceId: string) {
if (!isTauri()) throw new Error("Icon generation is available in the desktop app.");
this.icons.regenerate(session, referenceId);
}
generateBuiltinReferenceIcons(session: ProjectSession) {
if (!isTauri()) throw new Error("Icon generation is available in the desktop app.");
this.icons.enqueueBuiltins(session);
}
regenerateBuiltinReferenceIcon(session: ProjectSession, presetId: string) {
if (!isTauri()) throw new Error("Icon generation is available in the desktop app.");
this.icons.regenerateBuiltin(session, presetId);
}
pendingBuiltinIconIds() { return this.icons.pendingBuiltinIds(); }
forgetProject(record: ProjectRecord) {
if (this.hasActiveProject(record)) throw new Error("Cancel or finish this project's work before deleting it.");
const session = this.projects.get(projectQueueKey(record));
if (session) this.icons.forget(session);
this.projects.delete(projectQueueKey(record));
}
clearFinished() {
const cleared = this.items.filter((item) => !isWorkActive(item) && !item.needsSave);
cleared.forEach((item) => this.work.delete(item.id));
cleared.forEach((item) => this.icons.clearFinished(item.id));
this.items = this.items.filter((item) => isWorkActive(item) || item.needsSave);
this.publish();
}
start = () => {
this.connections += 1;
this.projects.forEach((session) => this.icons.watch(session));
if (this.connections === 1 && isTauri()) {
let disposed = false;
const stops: (() => void)[] = [];
stops.push(subscribeGenerationPreviews(() => {
if (!loadGenerationPreviewsEnabled()) {
this.items = this.items.map((item) => item.preview ? { ...item, preview: undefined } : item);
this.publish();
}
}));
const subscribe = async <T,>(name: string, handler: (payload: T) => void) => {
const stop = await listen<T>(name, ({ payload }) => { if (!disposed) handler(payload); });
if (disposed) stop(); else stops.push(stop);
};
this.ready = Promise.all([
subscribe<GenerationTimingProgress>("slopfab-progress", (event) => this.progress(event)),
subscribe<GenerationPreview>("slopfab-preview", (event) => this.preview(event)),
subscribe<JobEvent>("slopfab-job", (event) => this.event(event)),
]).then(() => undefined);
void this.ready.catch((reason) => writeDiagnostic("error", "work-queue", "subscription.failed", describeDiagnosticError(reason), {}));
this.disconnect = () => { disposed = true; stops.forEach((stop) => stop()); };
}
return () => {
this.connections -= 1;
if (this.connections === 0) { this.disconnect?.(); this.disconnect = null; this.icons.stop(); }
};
};
enqueue(session: ProjectSession, submissions: GenerationSubmission[], template?: GeneratorTemplate): string[] {
if (template && !templateSupportsVideo(template)) throw new Error("Choose a generator that supports video generation.");
const projectKey = projectQueueKey(session.record);
// Take every setting before the first await, including the selected engine
// template. Planning and native submission consume this same private copy.
const current = session.getSnapshot().config;
const ids: string[] = [];
for (const submission of submissions) {
if (this.items.some((item) => item.projectKey === projectKey && item.sceneId === submission.job.id && isWorkActive(item))) continue;
const chosen = template ?? submission.template ?? loadGeneratorTemplateSettings().templates.find(t => t.id === submission.job.generatorTemplateId) ?? defaultGeneratorTemplate(undefined, "video");
if (!templateSupportsVideo(chosen)) throw new Error("Choose a generator that supports video generation.");
const config = structuredClone({ ...current, providerSettings: { ...current.providerSettings,
slopfab: engineProviderSetting(chosen.paths, current.providerSettings.slopfab, chosen.attention, chosen.loras ?? [], chosen.mode ?? "prompt", chosen.additionalSafetensors ?? [], chosen.motionCache ?? false, chosen.sources ?? {}, chosen, submission.job.loraOverrides ?? []),
} });
const captured = submission.upscaleSource ? submission.job : { ...submission.job, generatorTemplateId: chosen.id || undefined, steps: submission.request.steps, seed: submission.request.seed };
config.generationJobs = config.generationJobs.map(j => j.id === captured.id ? structuredClone(captured) : j);
const id = `work-${crypto.randomUUID()}`;
ids.push(id);
let finish!: () => void;
const done = new Promise<void>((resolve) => { finish = resolve; });
const request = structuredClone({ ...submission.request, jobId: id, seed: submission.upscaleSource ? submission.request.seed : imageGenerationSeed(submission.request.seed), loraSnapshot: loraSnapshot(effectiveLoras(chosen.loras, submission.job.loraOverrides ?? [], chosen.modelType)), ...(!submission.upscaleSource ? { steps: generationStepsWithLoras(submission.request.steps, config),
...generationSigmaShiftsWithLoras(submission.request, config) } : {}) });
const snapshot = !submission.upscaleSource && (request.seed !== submission.request.seed || request.loraSnapshot.length || submission.request.loraSnapshot?.length)
? sceneGenerationSnapshot(captured, request) : submission.snapshot;
this.work.set(id, { id, generatorName: submission.upscaleSource ? submission.upscaleSource.generatorName : chosen.name, session, sceneId: submission.job.id, sceneInput: structuredClone(submission.upscaleSource?.scene ?? sceneInput(captured)), upscaleSource: submission.upscaleSource && structuredClone(submission.upscaleSource), request, config, snapshot, cancelled: false, submitted: false, done, finish });
this.items = [...this.items, {
id, projectKey, folderPath: session.record.folderPath, projectName: config.name,
sceneId: submission.job.id, title: submission.job.title, submittedAt: new Date().toISOString(),
status: "queued", progress: 0, detail: submission.upscaleSource ? "Waiting to upscale" : "Waiting to generate", error: null, completionAt: null,
cancelling: false, needsSave: false,
settings: { frames: request.frames, steps: request.steps, audioSteps: request.audioSteps, videoSigmaShift: request.videoSigmaShift, audioSigmaShift: request.audioSigmaShift, latentUpscale: request.latentUpscale, seed: request.seed, canvasWidth: request.canvasWidth, canvasHeight: request.canvasHeight },
}];
this.updateScene(this.work.get(id)!, { status: "queued", stage: "queued", progress: 0, error: null,
...(!submission.upscaleSource ? { generationSnapshot: snapshot, generatorTemplateId: captured.generatorTemplateId, steps: captured.steps, seed: captured.seed } : {}) });
}
// Capture directed dependencies after the whole batch is present, including
// sources submitted in earlier calls. Run IDs survive draft edits and keep
// cancellation from spreading to ancestors or another run of the same scene.
for (const id of ids) {
const work = this.work.get(id)!;
const job = work.config.generationJobs.find((job) => job.id === work.sceneId)!;
if (work.upscaleSource) continue;
const sources = new Set([work.request.previousSceneId, work.request.contextSceneId, work.request.latentBridge?.leftSceneId,
work.request.latentBridge?.rightSceneId, longShotPosition(job, sceneSiblings(job, work.config))?.previousBridge?.id]);
const sourceWorkIds = this.items.filter((item) => item.id !== id && item.projectKey === projectKey && !item.kind && isWorkActive(item)
&& sources.has(item.sceneId)).map((item) => item.id);
this.items = this.items.map((item) => item.id === id ? { ...item, sourceWorkIds } : item);
}
this.publish();
if (this.items.some((item) => item.status === "queued" && item.kind !== "reference-icons")) this.icons.yieldToVideo();
void this.pump();
return ids;
}
/** Wait for this exact run, including encoding and the project save. A saved
* scene status alone could belong to an older render of the same scene. */
waitFor(id: string, signal: AbortSignal, onProgress: (item: WorkItem) => void = () => {}): Promise<WorkItem> {
const work = this.work.get(id);
let latest = this.items.find((item) => item.id === id);
if (!latest) return Promise.reject(new Error("Generation is no longer in the work queue."));
if (signal.aborted) return Promise.reject(signal.reason ?? new Error("Agent request cancelled."));
if (!work && !isWorkActive(latest)) { onProgress(latest); return Promise.resolve(latest); }
if (!work) return Promise.reject(new Error("Generation is no longer in the work queue."));
return new Promise((resolve, reject) => {
const stop = this.subscribe(() => {
const item = this.items.find((candidate) => candidate.id === id);
if (item && item !== latest) { latest = item; onProgress(item); }
});
const cleanup = () => { stop(); signal.removeEventListener("abort", abort); };
const abort = () => { cleanup(); reject(signal.reason ?? new Error("Agent request cancelled.")); };
signal.addEventListener("abort", abort, { once: true });
if (signal.aborted) { abort(); return; }
onProgress(latest!);
void work.done.then(() => { cleanup(); resolve(latest!); });
});
}
enqueueMusic(session: ProjectSession, songId: string, template: GeneratorTemplate, count = 1) {
if (!Number.isInteger(count) || count < 1 || count > MAX_MUSIC_BATCH_COUNT) throw new Error(`Count must be a whole number from 1 to ${MAX_MUSIC_BATCH_COUNT}.`);
if (!isTauri()) throw new Error("Music generation requires the desktop app and model weights.");
if (!templateSupportsMusic(template)) throw new Error("Choose a music generator.");
const current = session.getSnapshot().config;
const source = musicSongSchema.parse((current.musicSongs ?? []).find((item) => item.id === songId));
if (!source.style.trim()) throw new Error("Describe the song's style before generating.");
const projectKey = projectQueueKey(session.record);
if (this.items.some((item) => item.projectKey === projectKey && item.kind === "music" && item.sceneId === songId && isWorkActive(item))) return songId;
const songs = [...(current.musicSongs ?? [])];
const entries: MusicSong[] = [];
for (let index = 0; index < count; index++) {
const song = index > 0 || source.assetId || source.generation || source.status === "completed" ? newSongVariation(source, songs) : source;
entries.push(song);
if (song.id !== source.id) songs.push(song);
}
let seed = imageGenerationSeed(source.seed);
const items: WorkItem[] = [];
for (const song of entries) {
const songId = song.id;
const id = `music-${crypto.randomUUID()}`;
const captured = { ...structuredClone(song), generatorId: template.id, seed, settings: musicSettingsForModel(song.settings, template.modelType) };
const config = structuredClone({ ...current, providerSettings: { ...current.providerSettings,
slopfab: engineProviderSetting(template.paths, current.providerSettings.slopfab, "flash2", [], "prompt", [], false, template.sources, { modelType: template.modelType }) } });
const request: SlopfabGenerationRequest = { jobId: id, prompt: song.style, steps: song.steps, seed: captured.seed,
music: captured.settings, frames: 0, canvasWidth: 0, canvasHeight: 0, referencePaths: [] };
let finish!: () => void;
const done = new Promise<void>((resolve) => { finish = resolve; });
this.work.set(id, { id, music: captured, session, sceneId: songId, request, config, snapshot: JSON.stringify(captured), cancelled: false, submitted: false, done, finish });
items.push({ id, kind: "music", projectKey, folderPath: session.record.folderPath, projectName: current.name,
sceneId: songId, title: song.title, submittedAt: new Date().toISOString(), status: "queued", progress: 0,
detail: "Waiting to generate music", error: null, completionAt: null, cancelling: false, needsSave: false,
settings: { frames: 0, steps: song.steps, seed: captured.seed, canvasWidth: 0, canvasHeight: 0 } });
seed = seed === Number.MAX_SAFE_INTEGER ? 0 : seed + 1;
}
const queuedIds = new Set(entries.map((song) => song.id));
session.update((value) => ({ ...value, musicSongs: [...(value.musicSongs ?? []), ...entries.filter((song) => song.id !== source.id)]
.map((song) => queuedIds.has(song.id) ? { ...song, status: "queued", error: null, generatorId: template.id } : song) }));
this.items = [...this.items, ...items];
this.publish(); void this.pump();
return entries[0].id;
}
enqueueAlbumCover(session: ProjectSession, songId: string) {
if (!isTauri()) throw new Error("Album cover generation requires the desktop app and model weights.");
const current = session.getSnapshot().config;
const song = current.musicSongs?.find((item) => item.id === songId);
if (!song) throw new Error("This song no longer exists.");
const projectKey = projectQueueKey(session.record);
if (this.items.some((item) => item.projectKey === projectKey && item.kind === "album-cover" && item.sceneId === songId && isWorkActive(item))) return;
const template = albumCoverGenerator();
if (!template) throw new Error("Choose an available text-prompt generator for album covers in Settings → Generators → Icons.");
const id = `cover-${crypto.randomUUID()}`;
const config = structuredClone({ ...current, providerSettings: { ...current.providerSettings,
slopfab: engineProviderSetting(template.paths, current.providerSettings.slopfab, template.attention, template.loras ?? [], "prompt", template.additionalSafetensors ?? [], false, template.sources, template) } });
const request: SlopfabGenerationRequest = { jobId: id, prompt: albumCoverPrompt(song, template.modelType), stillImage: true,
frames: 1, canvasWidth: 1024, canvasHeight: 1024, steps: generationStepsWithLoras(template.defaultSteps, config),
seed: imageGenerationSeed(-1), guidanceScale: imageCfgScale(template.modelType, undefined, template.kreaVariant), referencePaths: [] };
let finish!: () => void;
const done = new Promise<void>((resolve) => { finish = resolve; });
this.work.set(id, { id, image: true, albumCoverSongId: songId, session, sceneId: songId, request, config,
snapshot: "", cancelled: false, submitted: false, done, finish });
this.items = [...this.items, { id, kind: "album-cover", projectKey, folderPath: session.record.folderPath, projectName: current.name,
sceneId: songId, title: `Album cover · ${song.title}`, submittedAt: new Date().toISOString(), status: "queued", progress: 0,
detail: "Waiting to generate album cover", error: null, completionAt: null, cancelling: false, needsSave: false,
settings: { frames: 1, steps: request.steps, seed: request.seed, canvasWidth: 1024, canvasHeight: 1024 } }];
this.publish(); this.icons.yieldToVideo(); void this.pump();
return id;
}
enqueueImage(session: ProjectSession, template: GeneratorTemplate, count = 1) {
if (!Number.isInteger(count) || count < 1 || count > MAX_IMAGE_BATCH_COUNT) throw new Error(`Count must be a whole number from 1 to ${MAX_IMAGE_BATCH_COUNT}.`);
if (!isTauri()) throw new Error("Image generation requires the desktop app and downloaded model weights.");
if (!templateSupportsImages(template)) throw new Error("Choose a prompt generator for images.");
let current = session.getSnapshot().config;
const scene = current.imageScene ?? createImageScene(current.brief.prompt);
if (!templateSupportsEditing(template) && scene.rootType === "image") throw new Error(`${generatorModel(template.modelType).label} does not support image editing. Choose an editing generator.`);
if (activeGenerationEffects(scene.generationEffects) && !generatorModel(template.modelType).generationEffects) throw new Error("This model does not support generation effects. Bypass or remove the effects, or choose another generator.");
const edit = scene.rootType === "image" ? compileImageEdits(current, { modelType: template.modelType }) : null;
if (!imageScenePrompt(scene)) throw new Error("Describe the image or add an object before generating.");
const projectKey = projectQueueKey(session.record);
if (this.items.some((item) => item.projectKey === projectKey && item.kind === "image" && (item.imageDraftId ?? item.imageAssetId) === scene.outputAssetId && isWorkActive(item))) return;
const { prompt, references } = edit ? edit.edits[0] : compileImagePrompt(current, { modelType: template.modelType });
const { width, height } = edit ? imageEditCanvasSize(scene) : imageGenerationDimensions(imageSettings(current).resolution, imageSettings(current).aspectRatio, template.modelType);
const sizeError = !edit && imageGenerationSizeError({ width, height }, template.modelType);
if (sizeError) throw new Error(sizeError);
const id = `image-${crypto.randomUUID()}`;
const negativePrompt = generatorModel(template.modelType).negativePrompt ? scene.negativePrompts?.[template.modelType] : undefined;
const guidanceScale = imageCfgScale(template.modelType, scene.cfgScales, template.kreaVariant);
// Publish and select the job's own entry before generation starts. The
// finished source stays untouched, and an existing empty draft is reused.
session.update((config) => saveImageDraft({ ...config, imageScene: {
...(config.imageScene ?? scene),
...(guidanceScale !== undefined ? { cfgScales: { ...(config.imageScene ?? scene).cfgScales, [template.modelType]: guidanceScale } } : {}),
} }, template.id, true, id));
current = session.getSnapshot().config;
const firstDraft = current.assets.find((asset) => asset.id === current.imageScene!.outputAssetId)!;
const entries = [{ id, draft: firstDraft }, ...Array.from({ length: count - 1 }, () => {
const nextId = `image-${crypto.randomUUID()}`;
return { id: nextId, draft: { ...structuredClone(firstDraft), id: nextId, parentAssetId: firstDraft.parentAssetId ?? firstDraft.id } };
})];
const siblings = entries.slice(1).map((entry) => entry.draft);
if (siblings.length) {
session.update((config) => ({ ...config, assets: [...config.assets, ...siblings.filter((draft) => !config.assets.some((asset) => asset.id === draft.id))] }));
current = session.getSnapshot().config;
}
const referencePaths = (selected: typeof references) => [
...(edit && template.modelType === "minimax-h3" ? [projectItemPath(session.record.folderPath, edit.source)!] : []),
...selected.flatMap((reference) => referenceImages(reference).map((image) => projectItemPath(session.record.folderPath, image)!)),
];
let seed = imageGenerationSeed(scene.seed);
const items: WorkItem[] = [];
for (const entry of entries) {
const imageDraftId = entry.draft.id;
const config = structuredClone({ ...current, imageScene: { ...current.imageScene!, outputAssetId: imageDraftId }, providerSettings: { ...current.providerSettings,
slopfab: engineProviderSetting(template.paths, current.providerSettings.slopfab, template.attention, template.loras, "prompt", template.additionalSafetensors, false, template.sources, template) } });
let finish!: () => void;
const done = new Promise<void>((resolve) => { finish = resolve; });
const request: SlopfabGenerationRequest = { ...(negativePrompt ? { negativePrompt } : {}), ...(guidanceScale !== undefined ? { guidanceScale } : {}), generationEffects: activeGenerationEffects(scene.generationEffects), jobId: entry.id, stillImage: true, frames: 1, prompt: edit ? edit.edits[0].prompt : prompt,
...(edit ? { imageEdit: { sourceRelativePath: edit.source.relativePath, regionSize: imageEditRegionSize(scene), edits: edit.edits.map(({ references, ...step }) => ({
...step, referencePaths: referencePaths(references), refmods: referenceRefmodInputs(session.record.folderPath, references),
})) } } : {}),
canvasWidth: width, canvasHeight: height, steps: generationStepsWithLoras(scene.steps, config), seed,
referencePaths: referencePaths(references),
refmods: referenceRefmodInputs(session.record.folderPath, references),
};
this.work.set(entry.id, { id: entry.id, image: true, imageDraftId, imageGeneration: { ...imageGenerationSnapshot(config, edit ? imageEditDebugPrompt(edit.edits) : prompt, template.id), usedSeed: request.seed }, session, sceneId: scene.nodes.find((node) => node.kind === "root")!.id, config, snapshot: JSON.stringify(config.imageScene), request, submitted: false, cancelled: false, done, finish });
items.push({ id: entry.id, kind: "image", imageAssetId: imageDraftId, imageDraftId, projectKey, folderPath: session.record.folderPath, projectName: config.name, sceneId: scene.nodes.find((node) => node.kind === "root")!.id,
title: "Image · " + config.name, submittedAt: new Date().toISOString(), status: "queued", progress: 0, detail: "Waiting to generate image", error: null, completionAt: null, cancelling: false, needsSave: false,
settings: { frames: 1, steps: request.steps, seed: request.seed, canvasWidth: width, canvasHeight: height } });
// Wrap without exceeding JSON's exactly representable integer range.
seed = seed === Number.MAX_SAFE_INTEGER ? 0 : seed + 1;
}
this.items = [...this.items, ...items];
this.publish(); this.icons.yieldToVideo(); void this.pump();
return id;
}
enqueueExtendImage(session: ProjectSession, template: GeneratorTemplate, sourceId: string, options: ExtendOptions) {
if (!isTauri()) throw new Error("Zoom requires the desktop app.");
if (!templateSupportsOutpainting(template)) throw new Error("Choose a generator that supports image editing for Zoom.");
if (template.mode === "animate") throw new Error("Choose a prompt generator for Zoom.");
const current = session.getSnapshot().config;
// Editing drafts retain saved source pixels that the Zoom canvas displays.
const source = current.assets.find((asset) => asset.id === sourceId && asset.kind === "image");
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, template) } });
const id = `extend-${crypto.randomUUID()}`;
const sourceRelativePath = `cache/extend-images/${id}/canvas.png`;
const compiled = compileExtendImage(config, source, sourceRelativePath, options, template.modelType);
// Outpainting uses the positioned canvas as context. Crop regeneration
// also supplies the prepared crop as its composition reference.
const referencePaths = compiled.references.flatMap((reference) => referenceImages(reference).map((image) => projectItemPath(session.record.folderPath, image)!));
const regenerate = compiled.layout.mode === "regenerate";
if (regenerate && template.modelType === "minimax-h3") referencePaths.unshift(projectItemPath(session.record.folderPath, { relativePath: sourceRelativePath })!);
const resultName = `${regenerate ? "Regenerated" : "Extended"} - ${source.name}`;
const request: SlopfabGenerationRequest = { jobId: id, stillImage: true, frames: 1, prompt: compiled.prompt,
canvasWidth: compiled.layout.output.width, canvasHeight: compiled.layout.output.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; });
const imageGeneration: ImageGenerationSnapshot = {
...imageGenerationSnapshot({ ...config, imageScene: { ...createImageScene(options.prompt), steps: request.steps, seed: options.seed } }, compiled.prompt, template.id),
usedSeed: request.seed,
template: { kind: "extend", sourceId, sourceName: source.name, generatorName: template.name, ...structuredClone(options), steps: request.steps },
};
this.work.set(id, { id, image: true, imageDraftId: id, extend: { sourceId, name: resultName, options: structuredClone(options) }, imageParentId: sourceId,
imageGeneration,
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, imageDraftId: id, projectKey, folderPath: session.record.folderPath, projectName: config.name,
sceneId: this.work.get(id)!.sceneId, title: `Zoom · ${source.name}`, submittedAt: new Date().toISOString(), status: "queued", progress: 0,
detail: regenerate ? "Waiting to regenerate selection" : "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 } }];
session.update((current) => createTemplateImageDraft(current, id, resultName, imageGeneration, compiled.layout.output));
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 (!generatorModel(template.modelType).characterSheets) throw new Error("Choose a generator that supports image references for character sheets.");
if (template.mode === "animate") throw new Error("Choose a prompt generator for the character sheet.");
const current = session.getSnapshot().config;
const source = current.assets.find((asset) => asset.id === sourceId && asset.kind === "image");
if (!source || (!source.relativePath && !source.sourcePath)) throw new Error("Select an image with a saved file first.");
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, template) } });
const id = `character-sheet-${crypto.randomUUID()}`;
const frontRelativePath = `cache/character-sheets/${id}/1.png`;
const views = compileCharacterSheet(config, source, frontRelativePath, options, template.modelType);
config.imageScene = { ...(config.imageScene ?? createImageScene()),
steps: generationStepsWithLoras(options?.steps ?? current.imageScene?.steps ?? template.defaultSteps, config),
seed: options?.seed ?? current.imageScene?.seed ?? -1 };
const dimensions = characterSheetDimensions(imageSettings(config).resolution, options?.height, template.modelType);
const indices = characterSheetViewIndices(options);
const order = CHARACTER_SHEET_ORDER.filter((index) => indices.includes(index));
const firstView = order[0];
const { width, height } = dimensions[firstView];
let finish!: () => void;
const done = new Promise<void>((resolve) => { finish = resolve; });
const request: SlopfabGenerationRequest = { jobId: `${id}-view-${firstView + 1}`, stillImage: true, frames: 1, prompt: views[firstView].prompt,
canvasWidth: width, canvasHeight: height, steps: config.imageScene.steps,
seed: imageGenerationSeed(config.imageScene.seed),
referencePaths: views[firstView].references.flatMap((reference) => referenceImages(reference).map((image) => projectItemPath(session.record.folderPath, image)!)),
refmods: referenceRefmodInputs(session.record.folderPath, views[firstView].references) };
const imageGeneration: ImageGenerationSnapshot = {
...imageGenerationSnapshot({ ...config, imageScene: { ...createImageScene(options?.prompt ?? ""), steps: request.steps, seed: config.imageScene.seed } }, indices.map((index) => views[index].prompt).join("\n\n"), template.id),
usedSeed: request.seed,
references: [...new Map(indices.flatMap((index) => views[index].references.slice(1)).map((reference) => [reference.id, reference])).values()],
template: { kind: "character-sheet", sourceId, sourceName: source.name, generatorName: template.name, prompt: options?.prompt ?? "",
height, steps: request.steps, seed: config.imageScene.seed, views: characterSheetViews(options) },
};
this.work.set(id, { id, image: true, imageDraftId: id, characterSheet: { views, dimensions, order, sourceId, name: `Character sheet - ${source.name}`, index: 0 },
imageGeneration,
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, imageDraftId: id, projectKey, folderPath: session.record.folderPath, projectName: config.name,
sceneId: this.work.get(id)!.sceneId, title: `Character sheet · ${source.name}`, submittedAt: new Date().toISOString(),
status: "queued", progress: 0, detail: "Waiting to generate character sheet", error: null, completionAt: null, cancelling: false, needsSave: false,
settings: { frames: order.length, steps: request.steps, seed: request.seed, canvasWidth: width, canvasHeight: height } }];
session.update((current) => createTemplateImageDraft(current, id, `Character sheet - ${source.name}`, imageGeneration,
{ width: indices.reduce((total, index) => total + dimensions[index].width, 0), height }));
this.publish(); this.icons.yieldToVideo(); void this.pump();
}
/** Queues encoding one reference's images, video or sound into a refmod file. */
exportReferenceRefmod(session: ProjectSession, referenceId: string, outputPath: string) {
if (!isTauri()) throw new Error("Refmod export is available in the desktop app.");
const config = structuredClone(withEngineSettings(session.getSnapshot().config));
const reference = config.references.find((candidate) => candidate.id === referenceId);
if (!reference) throw new Error("The reference no longer exists.");
const blocker = refmodExportBlocker(reference);
if (blocker) throw new Error(blocker);
const projectKey = projectQueueKey(session.record);
if (this.items.some((item) => item.projectKey === projectKey && item.kind === "refmod" && item.sceneId === referenceId && isWorkActive(item))) {
throw new Error("This reference is already being exported.");
}
const id = `refmod-${crypto.randomUUID()}`;
let finish!: () => void;
const done = new Promise<void>((resolve) => { finish = resolve; });
const request = refmodExportRequest(session.record.folderPath, reference, id);
this.work.set(id, { id, session, sceneId: referenceId, request, config, snapshot: "", cancelled: false, submitted: false, done, finish,
refmod: { outputPath, name: reference.name, description: referenceDefinition(reference) } });
this.items = [...this.items, { id, kind: "refmod", projectKey, folderPath: session.record.folderPath, projectName: config.name, sceneId: referenceId,
title: "Refmod · " + reference.name, submittedAt: new Date().toISOString(), status: "queued", progress: 0, detail: "Waiting to export refmod", error: null, completionAt: null, cancelling: false, needsSave: false,
settings: { frames: 0, steps: 0, seed: 0, canvasWidth: 0, canvasHeight: 0 } }];
this.publish(); this.icons.yieldToVideo(); void this.pump();
}
private updateScene(work: PendingWork, patch: Partial<GenerationJob>, dirty = true) {
if (work.music) {
const id = work.sceneId;
const status = patch.status === "ready" ? "generating" : patch.status;
work.session.update((current) => updateSong(current, id, { ...(status ? { status } : {}), ...(patch.error !== undefined ? { error: patch.error } : {}) }), dirty);
return;
}
if (work.image || work.refmod) return;
// The undo replay log retains this callback. Do not retain the submitted
// project snapshot with every progress update.
work.session.update(sceneUpdate(work.sceneId, patch), dirty);
}
private async pump() {
if (this.pumping) return;
this.pumping = true;
try {
while (true) {
const next = this.items.find((item) => item.status === "queued" && item.kind !== "reference-icons");
if (!next) {
if (!this.icons.hasRunnable()) break;
await this.icons.runNext(this.ready, () => this.items.some((item) => item.status === "queued" && item.kind !== "reference-icons"));
continue;
}
const work = this.work.get(next.id)!;
this.patch(work.id, { status: "preparing", detail: work.refmod ? "Preparing refmod" : work.upscaleSource ? "Preparing upscale" : "Preparing generation" });
try {
await this.ready;
if (work.cancelled) continue;
if (work.refmod) { await this.exportRefmod(work, next.folderPath); continue; }
// Newly created scenes/references must exist on disk before any
// background result can be attached to this project.
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,
output: { width: work.request.canvasWidth, height: work.request.canvasHeight } });
if (work.cancelled) continue;
}
if (work.request.latentBridge) {
const live = work.session.getSnapshot().config;
const captured = work.config.generationJobs.find((job) => job.id === work.sceneId)!;
const current = live.generationJobs.find((job) => job.id === work.sceneId);
const inputs = current && longShotInputs({ ...current, bridgeLeftMargin: captured.bridgeLeftMargin, bridgeRightMargin: captured.bridgeRightMargin }, live);
if (!inputs || inputs.bridge.leftSceneId !== work.request.latentBridge.leftSceneId || inputs.bridge.rightSceneId !== work.request.latentBridge.rightSceneId)
throw new Error("The Long Shot neighbors changed while queued. Queue this scene again.");
const blocker = longShotBlocker({ ...captured, latentUpscale: work.request.latentUpscale }, live);
if (blocker) throw new Error(blocker);
work.request.latentBridge = inputs.bridge;
work.bridgeSegments = inputs.leftSegments;
work.snapshot = sceneGenerationSnapshot(captured, work.request);
this.updateScene(work, { generationSnapshot: work.snapshot });
}
if (work.request.previousSceneId) {
const live = work.session.getSnapshot().config;
const previous = live.generationJobs.find((job) => job.id === work.request.previousSceneId);
if (!previous?.latentRelativePath || previous.status !== "completed") {
throw new Error("Generate the selected source scene successfully to save its latents before continuing it. You can also use Generate All.");
}
const captured = work.config.generationJobs.find((job) => job.id === work.sceneId)!;
if (captured.sceneType === "single-shot") {
const blocker = continuationBlocker(captured, live.generationJobs);
if (blocker) throw new Error(blocker);
const sourceInput = JSON.parse(previous.generationSnapshot ?? "null");
const divisor = sourceInput?.latentUpscale ? 2 : 1;
if (sourceInput && (Math.ceil(sourceInput.canvasWidth / divisor / 32) * 32 !== work.request.canvasWidth || Math.ceil(sourceInput.canvasHeight / divisor / 32) * 32 !== work.request.canvasHeight))
throw new Error("Use the resolution of the previous video's original latents.");
}
if (captured.sceneType === "long-shot") {
const current = live.generationJobs.find((job) => job.id === work.sceneId);
const inputs = current && longShotContinuationInputs(current, live);
if (!inputs || inputs.request.previousSceneId !== work.request.previousSceneId)
throw new Error("The Long Shot neighbors changed while queued. Queue this scene again.");
const blocker = longShotBlocker({ ...captured, latentUpscale: work.request.latentUpscale }, live);
if (blocker) throw new Error(blocker);
Object.assign(work.request, inputs.request);
work.continuationSegments = inputs.segments;
} else {
work.request.continuationRelativePath = previous.latentRelativePath;
work.request.continuationSourceFrames = sceneOutputFrames(previous, live);
work.continuationSegments = sceneArchiveSegments(live, previous);
}
work.snapshot = sceneGenerationSnapshot(captured, work.request);
this.updateScene(work, { generationSnapshot: work.snapshot });
}
if (work.request.contextSceneId) {
const live = work.session.getSnapshot().config;
const context = live.generationJobs.find((job) => job.id === work.request.contextSceneId);
if (!context?.latentRelativePath || context.status !== "completed")
throw new Error("Generate the Context video successfully to save its latents first, or use Generate all.");
work.request.keystoneRelativePath = context.latentRelativePath;
const captured = work.config.generationJobs.find((job) => job.id === work.sceneId)!;
work.snapshot = sceneGenerationSnapshot(captured, work.request);
this.updateScene(work, { generationSnapshot: work.snapshot });
}
if (!work.image && !work.music) await purgeTimelineThumbnails(next.folderPath, work.sceneId).catch(() => undefined);
if (work.cancelled) continue;
await this.prepareReferences(work, next.folderPath);
if (work.cancelled) continue;
const plan = await resolveSlopfabPlan(work.request, work.config, next.folderPath);
if (work.cancelled) continue;
work.outputFrames = plan.alignedFrames;
this.patch(work.id, { status: "generating", detail: work.upscaleSource ? `Upscaling to ${plan.canvasWidth} × ${plan.canvasHeight}` : work.music ? "Generating music" : `Generating ${work.image ? "image" : `${plan.alignedFrames} frames`} at ${plan.canvasWidth} × ${plan.canvasHeight}`,
...(work.upscaleSource ? { settings: { ...next.settings, frames: plan.alignedFrames, canvasWidth: plan.canvasWidth, canvasHeight: plan.canvasHeight } } : {}) });
this.updateScene(work, { status: "generating", stage: "preparing" }, false);
await enqueueSlopfabGeneration(work.request, work.config, next.folderPath);
work.submitted = true;
if (work.cancelled) await this.requestNativeCancellation(work);
await work.done;
} catch (reason) { await 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;
await releaseReferenceAudios(work.request.referenceAudioIds ?? []).catch((reason) =>
writeDiagnostic("error", "work-queue", "references.release_failed", describeDiagnosticError(reason), { workId: work.id }));
delete work.request.referenceAudioIds;
// Queue history only needs WorkItem; finished runs must not keep a
// deep copy of the entire project and its image history alive.
this.work.delete(work.id);
await work.session.flushGeneratedCleanup().catch(() => undefined);
}
}
} finally { this.pumping = false; }
}
private async prepareReferences(work: PendingWork, folderPath: string) {
const report = (detail: string) => this.patch(work.id, { detail });
if (work.request.referenceVideos?.length) {
work.request.referenceVideoIds = await prepareReferenceVideos(folderPath, work.request, work.config, () => work.cancelled, report);
}
if (work.request.referenceAudios?.length) {
work.request.referenceAudioIds = await prepareReferenceAudios(folderPath, work.request, () => work.cancelled, report);
}
}
private async exportRefmod(work: PendingWork, folderPath: string) {
await this.prepareReferences(work, folderPath);
if (work.cancelled) return;
// The runtime's export call is synchronous, so it cannot be cancelled.
this.patch(work.id, { status: "encoding", progress: 0.5, detail: "Encoding refmod" });
await exportReferenceRefmod(work.request, work.config, work.refmod!);
this.patch(work.id, { status: "completed", progress: 1, detail: "Refmod saved" });
work.finish();
}
async cancel(id: string): Promise<void> {
if (this.icons.owns(id)) { await this.icons.cancel(); return; }
const item = this.items.find((candidate) => candidate.id === id);
if (!item || !isWorkActive(item)) return;
const active = this.items.filter((candidate) => candidate.projectKey === item.projectKey && isWorkActive(candidate));
const cancelledIds = new Set([id]);
// Follow only source -> dependent edges, including indirect descendants.
// Iterate to a fixed point so batch submission order does not matter.
let previousSize: number;
do {
previousSize = cancelledIds.size;
for (const candidate of active) {
if (candidate.sourceWorkIds?.some((sourceId) => cancelledIds.has(sourceId))) cancelledIds.add(candidate.id);
}
} while (cancelledIds.size !== previousSize);
await Promise.all([...cancelledIds].map((cancelledId) => this.cancelOne(cancelledId)));
}
private async cancelOne(id: string): Promise<void> {
const item = this.items.find((candidate) => candidate.id === id);
const work = this.work.get(id);
if (!item || !work || !isWorkActive(item) || (item.status === "encoding" && item.kind) || item.cancelling) return;
work.cancelled = true;
if (item.status === "encoding") {
// Finish the in-flight file write safely, but do not start linked work.
this.patch(id, { cancelling: true, detail: "Cancelling after the current save" });
return;
}
if (item.status === "queued" || item.status === "preparing") {
await this.cancelled(work);
if (item.status === "queued") this.work.delete(id);
// Stops files that are still being sent to a LAN worker for this job.
if (item.status === "preparing" && remoteWorkerSelected()) void cancelSlopfabGeneration(work.request.jobId).catch(() => undefined);
return;
}
this.patch(id, { cancelling: true, detail: "Cancelling generation" });
if (work.submitted) await this.requestNativeCancellation(work);
}
private async requestNativeCancellation(work: PendingWork) {
try { await cancelSlopfabGeneration(work.request.jobId); }
catch (reason) {
work.cancelled = false;
this.patch(work.id, { cancelling: false, detail: work.music ? "Generating music" : work.image ? "Generating image" : "Generating video", error: `Could not cancel: ${describeDiagnosticError(reason)}` });
}
}
cancelScenes(session: ProjectSession, sceneIds: string[]) {
return Promise.all(this.items.filter((item) => item.projectKey === projectQueueKey(session.record) && sceneIds.includes(item.sceneId) && isWorkActive(item)).map((item) => this.cancel(item.id))).then(() => undefined);
}
private cancelled(work: PendingWork, detail = "Generation cancelled before it started."): Promise<void> {
// Native cancellation, a late result and an enqueue error can race.
// Finalize once and keep the queue occupied until the preview is saved.
return work.cancellation ??= this.finishCancellation(work, detail);
}
private async finishCancellation(work: PendingWork, detail: string) {
this.timing.clear(work.id);
work.cancelled = true;
const preview = work.lastPreview;
work.lastPreview = undefined;
if (preview) {
this.patch(work.id, { status: "encoding", detail: "Saving cancelled generation preview", cancelling: true, completionAt: null });
this.updateScene(work, { status: "ready", stage: "encoding", error: null }, false);
try {
const saved = await saveGenerationPreview(work.session.record.folderPath, work.id, preview);
const createdAt = new Date().toISOString();
work.session.update(sceneResultUpdate(work.sceneId, {
id: work.id, createdAt, scene: work.sceneInput!, generatorName: work.generatorName,
generationSnapshot: work.snapshot, latentRelativePath: null,
media: { id: generationAssetId(work.sceneId), name: work.sceneInput!.title, kind: "generated", createdAt,
relativePath: saved.relativePath, sourcePath: null, mimeType: "video/mp4", hasAudio: false,
width: saved.width, height: saved.height, durationMs: saved.durationMs },
}));
try {
await work.session.save();
this.patch(work.id, { status: "cancelled", detail: "Cancelled; preview saved as Take", cancelling: false, progress: 1, error: null });
} catch (reason) {
this.patch(work.id, { status: "cancelled", detail: "Preview saved; project save failed", cancelling: false,
progress: 1, needsSave: true, error: describeDiagnosticError(reason) });
}
return;
} catch (reason) {
detail = `Generation cancelled; preview could not be saved: ${describeDiagnosticError(reason)}`;
writeDiagnostic("error", "work-queue", "preview.save_failed", detail, { workId: work.id, sceneId: work.sceneId });
} finally { work.finish(); }
}
this.patch(work.id, { status: "cancelled", detail, cancelling: false, completionAt: null });
this.updateScene(work, work.upscaleSource ? { status: "completed", stage: "completed", progress: 1, error: detail } : { status: "cancelled", stage: "failed", error: detail });
void work.session.save().catch(() => undefined);
work.finish();
}
private async fail(work: PendingWork, reason: unknown) {
if (work.cancelled) {
await this.cancelled(work);
return;
}
const detail = describeDiagnosticError(reason);
this.timing.clear(work.id);
this.patch(work.id, { status: "failed", detail: work.refmod ? "Refmod export failed" : "Generation failed", error: detail, completionAt: null, cancelling: false });
this.updateScene(work, work.upscaleSource ? { status: "completed", stage: "completed", progress: 1, error: detail } : { status: "failed", stage: "failed", error: detail });
writeDiagnostic("error", "work-queue", "generation.failed", detail, { workId: work.id, sceneId: work.sceneId, folderPath: work.session.record.folderPath });
void work.session.save().catch(() => undefined);
work.finish();
}
private preview(event: GenerationPreview) {
if (!loadGenerationPreviewsEnabled() || !validGenerationPreview(event)) return;
const work = this.nativeWork(event.jobId);
const item = this.items.find((candidate) => candidate.id === work?.id);
if (!work || !item || work.cancelled || item.cancelling || (item.kind && item.kind !== "image") || work.upscaleSource
|| (work.config.providerSettings.slopfab?.options.modelType ?? "minimax-h3") !== "minimax-h3"
|| (work.image && event.frames.length !== 1)
|| !["preparing", "generating"].includes(item.status)) return;
if (!item.kind) work.lastPreview = event;
this.patch(work.id, { preview: event });
}
private progress(event: GenerationTimingProgress) {
if (this.icons.progressEvent(event)) return;
const work = this.nativeWork(event.jobId);
const item = this.items.find((candidate) => candidate.id === work?.id);
// A refmod export sends its files to a LAN worker while it is encoding.
if (!work || !item || !(["preparing", "generating"].includes(item.status) || (work.refmod && item.status === "encoding" && isWorkerTransfer(event)))) return;
if (isWorkerTransfer(event)) {
// Files moving to a LAN worker: say what is happening without moving
// the generation bar or the time estimate.
if (!item.cancelling) this.patch(work.id, { detail: workerTransferDetail(event) });
return;
}
if (event.stage === "calibrating") {
this.patch(work.id, { status: "generating", completionAt: null, detail: item.cancelling ? "Cancelling generation" : event.totalSteps > 0 ? `Preparing generation effects (${event.step}/${event.totalSteps})` : "Checking generation effects calibration" });
return;
}
if (work.characterSheet) {
const sheet = work.characterSheet;
const fraction = event.totalSteps > 0 ? Math.max(0, Math.min(1, event.step / event.totalSteps)) : 0;
this.patch(work.id, { progress: Math.max(item.progress, (sheet.index + fraction) / sheet.order.length * 0.9),
detail: item.cancelling ? "Cancelling generation" : `Character sheet ${sheet.index + 1}/${sheet.order.length} · ${CHARACTER_SHEET_VIEWS[sheet.order[sheet.index]].label}` });
return;
}
const progress = Math.max(item.progress, event.totalSteps > 0 ? Math.min(0.88, 0.12 + Math.max(0, event.step) / event.totalSteps * 0.76) : event.stage === "delivering" ? 0.88 : 0.08);
this.patch(work.id, { status: "generating", progress, completionAt: this.timing.update(event), detail: item.cancelling ? "Cancelling generation" : event.stage === "singleShotEnding" ? "Generating ending (1 of 2)" : event.stage === "singleShotFull" ? "Generating full shot (2 of 2)" : event.stage === "upscaling" ? "Upscaling video latents" : work.request.imageEdit ? `Applying image edits (${Math.min(work.request.imageEdit.edits.length, Math.floor(event.step / Math.max(1, event.totalSteps) * work.request.imageEdit.edits.length) + 1)} of ${work.request.imageEdit.edits.length})` : work.music ? "Generating music" : event.stage === "starting" || event.stage === "transformerLoad" ? "Loading MiniMax H3" : work.image ? "Generating image" : "Generating video" });
this.updateScene(work, { status: "generating", stage: event.stage === "starting" || event.stage === "transformerLoad" ? "preparing" : "generating", progress }, false);
}
private event(event: JobEvent) {
if (this.icons.event(event)) return;
const work = this.nativeWork(event.jobId);
const item = this.items.find((candidate) => candidate.id === work?.id);
if (!work || !item) {
if (event.state === "framesReady" && this.items.some((candidate) => candidate.id === event.jobId && !isWorkActive(candidate))) void releaseRendered(event.jobId);
return;
}
if (!isWorkActive(item)) { if (event.state === "framesReady") void releaseRendered(event.jobId); return; }
if (item.status === "encoding") {
if (work.cancellation && event.state === "framesReady") void releaseRendered(event.jobId);
return;
}
if (work.characterSheet) {
if (event.state === "framesReady") work.characterSheet.ready?.resolve();
else if (event.state === "failed" || event.state === "cancelled") {
if (event.state === "cancelled") work.cancelled = true;
work.characterSheet.ready?.reject(new Error(event.detail));
}
return;
}
if (event.state === "framesReady") {
if (event.output) this.timing.record(event.output);
this.timing.clear(work.id);
if (work.cancelled) { void releaseRendered(work.id).then(() => this.cancelled(work)); return; }
void this.encode(work);
} else if (event.state === "failed") void this.fail(work, event.detail);
else if (event.state === "cancelled") void this.cancelled(work, event.detail);
// A queued acknowledgement can arrive after progress. It must not rewind it.
}
private nativeWork(jobId: string) {
return [...this.work.values()].find((work) => work.request.jobId === jobId);
}
private async generateCharacterSheet(work: PendingWork) {
const sheet = work.characterSheet!;
const folderPath = work.session.record.folderPath;
const dimensions = sheet.dimensions;
try {
for (let index = 0; index < sheet.order.length; index++) {
if (work.cancelled) return;
sheet.index = index;
const viewIndex = sheet.order[index];
const { width, height } = dimensions[viewIndex];
work.submitted = false;
const view = sheet.views[viewIndex];
work.request = { ...work.request, jobId: `${work.id}-view-${viewIndex + 1}`, prompt: view.prompt,
canvasWidth: width, canvasHeight: height,
referencePaths: view.references.flatMap((reference) => referenceImages(reference).map((image) => projectItemPath(folderPath, image)!)),
refmods: referenceRefmodInputs(folderPath, view.references) };
this.patch(work.id, { status: "preparing", detail: `Preparing character sheet view ${index + 1}/${sheet.order.length}` });
await resolveSlopfabPlan(work.request, work.config, folderPath);
if (work.cancelled) return;
const ready = new Promise<void>((resolve, reject) => { sheet.ready = { resolve, reject }; });
// Events can arrive before native submission returns.
void ready.catch(() => undefined);
this.patch(work.id, { status: "generating", detail: `Character sheet ${index + 1}/${sheet.order.length} · ${CHARACTER_SHEET_VIEWS[viewIndex].label}` });
await enqueueSlopfabGeneration(work.request, work.config, folderPath);
work.submitted = true;
if (work.cancelled) await this.requestNativeCancellation(work);
await ready;
sheet.ready = undefined;
if (work.cancelled) { await this.cancelled(work); return; }
this.patch(work.id, { status: "encoding", detail: `Saving character sheet view ${index + 1}/${sheet.order.length}` });
await invoke("save_character_sheet_view", { folderPath, sheetId: work.id, jobId: work.request.jobId, index: viewIndex });
await releaseRendered(work.request.jobId);
}
this.patch(work.id, { status: "encoding", progress: 0.95, detail: "Combining character sheet", completionAt: null });
const saved = await invoke<{ relativePath: string; width: number; height: number }>("combine_character_sheet", { folderPath, sheetId: work.id, indices: [...sheet.order].sort((a, b) => a - b) });
const scene = createImageEditScene({ ...saved, name: sheet.name }, work.config.imageScene ?? undefined);
scene.referenceIds = [];
scene.generationEffects = work.request.generationEffects;
const generation = { ...work.imageGeneration!, scene };
work.session.update(derivedImageUpdate(work.id, sheet.sourceId, { id: work.id, kind: "image", name: sheet.name,
...saved, mimeType: "image/png", imageGeneration: generation, createdAt: new Date().toISOString() }));
try { await work.session.save(); this.patch(work.id, { status: "completed", progress: 1, detail: "Character sheet saved" }); }
catch (reason) { this.patch(work.id, { status: "failed", progress: 1, needsSave: true, detail: "Character sheet created; project save failed", error: describeDiagnosticError(reason) }); }
} finally {
sheet.ready = undefined;
await releaseRendered(work.request.jobId).catch(() => undefined);
await invoke("discard_character_sheet_views", { folderPath, sheetId: work.id }).catch((reason) =>
writeDiagnostic("error", "work-queue", "character-sheet.cleanup_failed", describeDiagnosticError(reason), { workId: work.id }));
work.finish();
}
}
private async encode(work: PendingWork) {
if (work.music) { await this.saveMusic(work); return; }
if (work.image) { await this.saveImage(work); return; }
this.patch(work.id, { status: "encoding", progress: 0.9, detail: "Saving video", completionAt: null });
this.updateScene(work, { status: "ready", stage: "encoding", progress: 0.9, error: null }, false);
try {
const saved = await saveGeneratedScene({
folderPath: work.session.record.folderPath, jobId: work.id, thumbnailJobId: work.sceneId,
...(work.request.latentUpscale ? { outputSize: { width: work.request.canvasWidth, height: work.request.canvasHeight } } : {}),
onProgress: (encoded, total) => {
if (total <= 0) return;
const progress = Math.min(0.99, 0.9 + encoded / total * 0.1);
this.patch(work.id, { progress });
this.updateScene(work, { progress }, false);
},
});
if (work.cancelled) { await this.cancelled(work); return; }
const assetId = generationAssetId(work.sceneId);
const totalFrames = saved.durationMs ? Math.round(saved.durationMs * GENERATION_FRAME_RATE / 1000) : work.outputFrames ?? work.request.frames;
const sceneSegments = work.upscaleSource ? work.upscaleSource.media.sceneSegments : work.request.latentBridge
? bridgedSceneSegments(work.sceneId, generationLatentPath(work.id), totalFrames, work.request.frames, work.request.latentBridge, work.bridgeSegments)
: joinedSceneSegments(work.sceneId, generationLatentPath(work.id), totalFrames,
Math.ceil(work.request.frames / 17) * 17, work.request.previousSceneId ? {
sceneId: work.request.previousSceneId, latentRelativePath: work.request.continuationRelativePath!,
frameCount: work.request.continuationSourceFrames ?? totalFrames - Math.ceil(work.request.frames / 17) * 17,
segments: work.continuationSegments, from: work.request.continuationFrom,
} : undefined);
const media = { kind: "generated" as const, relativePath: saved.relativePath, sourcePath: null, mimeType: "video/mp4", hasAudio: saved.hasAudio ?? null, width: saved.width ?? work.request.canvasWidth, height: saved.height ?? work.request.canvasHeight, durationMs: saved.durationMs ?? Math.round(totalFrames / GENERATION_FRAME_RATE * 1000), sceneSegments };
const version = { id: work.id, createdAt: new Date().toISOString(), scene: work.sceneInput!,
generatorName: work.generatorName,
media: { ...media, id: assetId, name: work.sceneInput!.title, createdAt: new Date().toISOString() },
generationSnapshot: work.snapshot, latentRelativePath: generationLatentPath(work.id),
...(work.upscaleSource ? { originalLatentRelativePath: work.upscaleSource.originalLatentRelativePath ?? work.upscaleSource.latentRelativePath,
upscaleFactor: work.request.latentUpscaleSource!.scale } : {}) };
work.session.update(sceneResultUpdate(work.sceneId, version, saved.note));
try {
await work.session.save();
if (work.cancelled) { await this.cancelled(work); return; }
this.patch(work.id, { status: "completed", progress: 1, detail: "Video saved", error: saved.note });
} catch (reason) {
this.patch(work.id, { status: "failed", progress: 1, needsSave: true, detail: "Video created; project save failed", error: describeDiagnosticError(reason) });
}
} catch (reason) { await this.fail(work, reason); }
finally { await releaseRendered(work.id); work.finish(); }
}
async retrySave(id: string) {
if (this.icons.owns(id)) { await this.icons.retrySave(); return; }
const item = this.items.find((candidate) => candidate.id === id);
const session = item && this.projects.get(item.projectKey);
if (!item?.needsSave || !session) return;
try { await session.save(); this.patch(id, { status: item.status === "cancelled" ? "cancelled" : "completed", needsSave: false, error: null, detail: item.status === "cancelled" ? "Cancelled; preview saved as Take" : item.kind === "music" ? "Music saved" : item.kind === "album-cover" ? "Album cover saved" : item.kind === "image" ? "Image saved" : "Video saved" }); }
catch (reason) { this.patch(id, { error: describeDiagnosticError(reason) }); }
}
private async saveMusic(work: PendingWork) {
this.patch(work.id, { status: "encoding", progress: 0.9, detail: "Saving music", completionAt: null });
try {
const saved = await invoke<{ relativePath: string; durationMs: number }>("save_generated_music", { folderPath: work.session.record.folderPath, jobId: work.id });
const song = work.music!, id = work.id;
const asset: ProjectAsset = { id, kind: "audio", name: song.title, ...saved, mimeType: "audio/wav", hasAudio: true, createdAt: new Date().toISOString() };
work.session.update((current) => ({ ...updateSong(current, song.id, { status: "completed", assetId: id, error: null,
generation: { generatorId: song.generatorId, style: work.request.prompt, settings: work.request.music!, steps: work.request.steps, seed: work.request.seed } }), assets: [...current.assets.filter((item) => item.id !== id), asset] }));
try {
await work.session.save();
this.patch(id, { status: "completed", progress: 1, detail: "Music saved" });
} catch (reason) {
this.patch(id, { status: "failed", progress: 1, needsSave: true, detail: "Music created; project save failed", error: describeDiagnosticError(reason) });
}
} catch (reason) { await this.fail(work, reason); }
finally { await releaseRendered(work.id); work.finish(); }
}
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 }>(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.albumCoverSongId) {
const songId = work.albumCoverSongId;
work.session.update((current) => updateSong(current, songId, { coverRelativePath: saved.relativePath }));
} else if (work.extend) {
const scene = createImageEditScene({ ...saved, name: work.extend.name }, { ...work.config.imageScene!, steps: work.request.steps, seed: work.request.seed, generationEffects: work.request.generationEffects });
work.imageGeneration = { ...work.imageGeneration!, scene };
work.session.update(derivedImageUpdate(work.id, work.extend.sourceId, { id: work.id, kind: "image", name: work.extend.name,
...saved, mimeType: "image/png", imageGeneration: work.imageGeneration, createdAt: new Date().toISOString() }));
} else {
work.session.update(imageResultUpdate({ id: work.id, imageDraftId: work.imageDraftId, imageParentId: work.imageParentId,
imageGeneration: work.imageGeneration, outputAssetId: work.config.imageScene?.outputAssetId,
imageEdit: Boolean(work.request.imageEdit), saved }));
}
try { await work.session.save(); this.patch(work.id, { status: "completed", progress: 1, detail: work.albumCoverSongId ? "Album cover saved" : "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) { await this.fail(work, reason); }
finally {
try { await releaseRendered(work.id); }
finally { work.finish(); }
}
}
}
// These factories live outside the running job's closure scope: undo replay
// retains only the result fields, never its full submitted project snapshot.
function sceneUpdate(sceneId: string, patch: Partial<GenerationJob>) {
return (current: ProjectConfig): ProjectConfig => ({ ...current,
generationJobs: current.generationJobs.map((job) => job.id === sceneId ? { ...job, ...patch, updatedAt: new Date().toISOString() } : job),
});
}
function derivedImageUpdate(id: string, sourceId: string, asset: ProjectAsset) {
return (current: ProjectConfig) => completeImageDraft(current, id, {
...asset, parentAssetId: imageFamilyRoot(current.assets, sourceId),
});
}
function sceneResultUpdate(sceneId: string, version: Parameters<typeof completeSceneGeneration>[2], note?: string | null) {
return (current: ProjectConfig) => completeSceneGeneration(current, sceneId, version, note);
}
function imageResultUpdate({ id, imageDraftId, imageParentId, imageGeneration, outputAssetId, imageEdit, saved }: {
id: string; imageDraftId?: string; imageParentId?: string; imageGeneration?: ImageGenerationSnapshot;
outputAssetId?: string | null; imageEdit: boolean; saved: { relativePath: string; width: number; height: number };
}) {
const mimeType = imageEdit ? "image/png" : "image/jpeg";
return (current: ProjectConfig): ProjectConfig => imageDraftId ? completeImageDraft(current, imageDraftId, {
id: imageDraftId, kind: "image", name: current.assets.find((asset) => asset.id === imageDraftId && asset.name !== "New image")?.name
?? `Image ${current.assets.filter((asset) => asset.kind === "image" && !asset.imageDraft).length + 1}`,
...saved, imageGeneration, mimeType, createdAt: new Date().toISOString(),
}) : ({ ...current,
...(current.imageScene?.outputAssetId === outputAssetId ? {
thumbnail: saved.relativePath,
imageScene: { ...(current.imageScene ?? createImageScene()), outputAssetId: id },
} : {}),
assets: [...current.assets, { id, kind: "image", name: `Image ${current.assets.filter((asset) => asset.kind === "image").length + 1}`, ...saved, imageGeneration, mimeType, ...(imageParentId ? { parentAssetId: imageFamilyRoot(current.assets, imageParentId) } : {}), createdAt: new Date().toISOString() }],
});
}
type WorkerTransfer = GenerationTimingProgress & { detail?: string; transferred?: number; transferTotal?: number | null };
function isWorkerTransfer(event: GenerationTimingProgress): event is WorkerTransfer {
return event.stage === "workerUpload" || event.stage === "workerDownload";
}
function workerTransferDetail(event: WorkerTransfer): string {
const gb = (bytes: number) => `${(bytes / 1024 ** 3).toFixed(2)} GB`;
const amount = event.transferTotal ? ` · ${Math.floor(100 * (event.transferred ?? 0) / event.transferTotal)}% of ${gb(event.transferTotal)}`
: event.transferred ? ` · ${gb(event.transferred)}` : "";
return `${event.detail ?? (event.stage === "workerUpload" ? "Sending files to the worker" : "Worker downloading weights")}${amount}`;
}