Skip to content

AI media harness for planning, generating, and editing locally

TypeScript

Export references as refmod files from References

Bitti440d88450c92
diff --git a/src/components/ProjectWorkspace.tsx b/src/components/ProjectWorkspace.tsx
index 08b8f43..fcf40a8 100644
--- a/src/components/ProjectWorkspace.tsx
+++ b/src/components/ProjectWorkspace.tsx
@@ -289,7 +289,7 @@ export function ProjectWorkspace({ project, initialView, runtime = null, onBack,
{view === "editor" && <ImageEditor config={config} folderPath={project.folderPath} onChange={changeConfig} onGenerate={(template) => queue.enqueueImage(session, template)} onCancel={(id) => queue.cancel(id)} workItems={projectItems.filter((item) => item.kind === "image")} onOpenGenerator={(jobId) => { setSelectedGenerationJobId(jobId); setView("generator"); }} onOpenReferences={() => setView("references")} />}
{view === "timeline" && <TimelineView config={config} openSceneId={selectedGenerationJobId} folderPath={project.folderPath} generationCompletionTimes={generationCompletionTimes} onChange={changeConfig} onMeasured={recordMeasurement} onOpenGenerator={(jobId) => { setSelectedGenerationJobId(jobId); setView("generator"); }} />}
{view === "generator" && <GeneratorView onGenerate={(submissions) => queue.enqueue(session, submissions)} onCancelGeneration={(ids) => queue.cancelScenes(session, ids)} cancellingJobIds={cancellingJobIds} config={config} folderPath={project.folderPath} generationCompletionTimes={generationCompletionTimes} runtime={runtime?.slopfab ?? null} onRuntimeChange={onGeneratorRuntimeChange} onChange={changeConfig} selectedJobId={selectedGenerationJobId} onSelectedJobChange={setSelectedGenerationJobId} onOpenTimeline={() => setView("timeline")} />}
- {view === "references" && <ReferencesView config={config} folderPath={project.folderPath} onChange={changeConfig} onRegenerateIcon={(id) => queue.regenerateReferenceIcon(session, id)} onGenerateBuiltinIcons={() => queue.generateBuiltinReferenceIcons(session)} onRegenerateBuiltinIcon={(id) => queue.regenerateBuiltinReferenceIcon(session, id)} pendingBuiltinIconIds={queue.pendingBuiltinIconIds()} pendingIconIds={new Set(config.references.filter((reference) => queue.isReferenceIconPending(session, reference.id)).map((reference) => reference.id))} onOpenGenerator={(jobId) => { setSelectedGenerationJobId(jobId); setView("generator"); }} />}
+ {view === "references" && <ReferencesView config={config} folderPath={project.folderPath} onChange={changeConfig} onRegenerateIcon={(id) => queue.regenerateReferenceIcon(session, id)} onExportRefmod={(id, outputPath) => queue.exportReferenceRefmod(session, id, outputPath)} onGenerateBuiltinIcons={() => queue.generateBuiltinReferenceIcons(session)} onRegenerateBuiltinIcon={(id) => queue.regenerateBuiltinReferenceIcon(session, id)} pendingBuiltinIconIds={queue.pendingBuiltinIconIds()} pendingIconIds={new Set(config.references.filter((reference) => queue.isReferenceIconPending(session, reference.id)).map((reference) => reference.id))} onOpenGenerator={(jobId) => { setSelectedGenerationJobId(jobId); setView("generator"); }} />}
{view === "export" && <div className="project-export">
<SelectorBar aria-label="Export type" className="project-export__types" value={mediaMode} onChange={setMediaMode} items={[{ value: "video", label: "Video" }, { value: "image", label: "Image" }]} />
{mediaMode === "image"
diff --git a/src/components/WorkQueuePanel.tsx b/src/components/WorkQueuePanel.tsx
index 8275cef..86bd5fc 100644
--- a/src/components/WorkQueuePanel.tsx
+++ b/src/components/WorkQueuePanel.tsx
@@ -14,7 +14,7 @@ import { Flyout, ProgressBar, ProgressRing } from "./ui";
it runs, a 3px bar. The render settings a row was queued with sit in its
tooltip rather than on a third line. */
-const settingsLine = (item: WorkItem) => `${item.settings.canvasWidth} × ${item.settings.canvasHeight} · ${item.kind === "reference-icons" ? "JPG icons" : item.kind === "image" ? "JPG image" : `${(item.settings.frames / GENERATION_FRAME_RATE).toFixed(1)}s`} · ${item.settings.steps} steps · ${item.settings.seed === -1 ? "Random seed" : `Seed ${item.settings.seed}`}`;
+const settingsLine = (item: WorkItem) => item.kind === "refmod" ? "Safetensors refmod" : `${item.settings.canvasWidth} × ${item.settings.canvasHeight} · ${item.kind === "reference-icons" ? "JPG icons" : item.kind === "image" ? "JPG image" : `${(item.settings.frames / GENERATION_FRAME_RATE).toFixed(1)}s`} · ${item.settings.steps} steps · ${item.settings.seed === -1 ? "Random seed" : `Seed ${item.settings.seed}`}`;
function StateGlyph({ item }: { item: WorkItem }) {
if (item.status === "completed") return <Check16 />;
diff --git a/src/components/ui/icons.tsx b/src/components/ui/icons.tsx
index 9a01494..40b7aa1 100644
--- a/src/components/ui/icons.tsx
+++ b/src/components/ui/icons.tsx
@@ -16,6 +16,7 @@ export { Add16Regular as Add16, Add20Regular as Add20 } from "@fluentui/react-ic
export { ArrowCounterclockwise12Regular as Reset12, ArrowCounterclockwise16Regular as Reset16, ArrowCounterclockwise32Regular as Reset32 } from "@fluentui/react-icons/headless/svg/arrow-counterclockwise";
export { ArrowDown12Regular as ArrowDown12, ArrowDown16Regular as ArrowDown16 } from "@fluentui/react-icons/headless/svg/arrow-down";
export { ArrowDownload16Regular as Download16, ArrowDownload16Filled as Download16Filled, ArrowDownload20Regular as Download20, ArrowDownload32Regular as Download32 } from "@fluentui/react-icons/headless/svg/arrow-download";
+export { ArrowExportUp16Regular as Export16 } from "@fluentui/react-icons/headless/svg/arrow-export-up";
export { ArrowLeft16Regular as Back16 } from "@fluentui/react-icons/headless/svg/arrow-left";
export { ArrowRedo16Regular as Redo16 } from "@fluentui/react-icons/headless/svg/arrow-redo";
export { ArrowSync16Regular as Refresh16, ArrowSync16Filled as Refresh16Filled, ArrowSync20Regular as Refresh20 } from "@fluentui/react-icons/headless/svg/arrow-sync";
diff --git a/src/components/workspace/ReferencesView.test.tsx b/src/components/workspace/ReferencesView.test.tsx
index 8568c94..f97e866 100644
--- a/src/components/workspace/ReferencesView.test.tsx
+++ b/src/components/workspace/ReferencesView.test.tsx
@@ -457,6 +457,28 @@ describe("Reference library views and selection", () => {
expect(within(usage).getByRole("button", { name: initial.generationJobs[0].title })).toBeInTheDocument();
});
+ it("exports a reference with media as a refmod and reports why it could not", async () => {
+ Object.assign(window, { __TAURI_INTERNALS__: {} });
+ const initial = project();
+ initial.references.push({ ...initial.references[0], id: "ref-face", name: "Face", images: [{ id: "front", name: "front", relativePath: "references/front.png" }] });
+ const onExportRefmod = vi.fn();
+ vi.mocked(invoke).mockImplementation(async (command) => command === "choose_refmod_export_path" ? "D:\\out\\Face.safetensors" : new Uint8Array());
+ render(<ReferencesView config={initial} folderPath="C:\\project" onChange={vi.fn()} onExportRefmod={onExportRefmod} />);
+ // The selected text reference has nothing to encode.
+ expect(screen.getByRole("button", { name: "Export refmod" })).toBeDisabled();
+ const exportFace = () => {
+ fireEvent.contextMenu(screen.getByRole("option", { name: /^Face/ }));
+ fireEvent.click(screen.getByRole("menuitem", { name: "Export as refmod…" }));
+ };
+ exportFace();
+ await waitFor(() => expect(onExportRefmod).toHaveBeenCalledWith("ref-face", "D:\\out\\Face.safetensors"));
+ expect(invoke).toHaveBeenCalledWith("choose_refmod_export_path", { name: "Face" });
+ expect(screen.getByRole("button", { name: "Export refmod" })).toBeEnabled();
+ onExportRefmod.mockImplementation(() => { throw new Error("This reference is already being exported."); });
+ exportFace();
+ expect(await screen.findByText("This reference is already being exported.")).toBeInTheDocument();
+ });
+
it("does not count stale scene bindings as usage", () => {
const initial = project();
initial.generationJobs[0].referenceIds = ["ref-hero"];
diff --git a/src/components/workspace/ReferencesView.tsx b/src/components/workspace/ReferencesView.tsx
index 0f58756..ef10c54 100644
--- a/src/components/workspace/ReferencesView.tsx
+++ b/src/components/workspace/ReferencesView.tsx
@@ -1,4 +1,4 @@
-import { Add16, Audio22, ArrowDown12, ArrowUp12, ChevronLeft16, ChevronRight16, Copy16, CursorClick32, Delete14, Delete16, FolderOpen16, GridView16, GridView16Filled, ImageAdd14, ImageAdd16, Images32, ListView16, ListView16Filled, OpenExternal16, Refresh16, Refresh20, Rename16, Search16, TextFile14, TextFile16, TextFile24 } from "../ui/icons";
+import { Add16, Audio22, ArrowDown12, ArrowUp12, ChevronLeft16, ChevronRight16, Copy16, CursorClick32, Delete14, Delete16, Export16, FolderOpen16, GridView16, GridView16Filled, ImageAdd14, ImageAdd16, Images32, ListView16, ListView16Filled, OpenExternal16, Refresh16, Refresh20, Rename16, Search16, TextFile14, TextFile16, TextFile24 } from "../ui/icons";
import { invoke } from "@tauri-apps/api/core";
import { useEffect, useMemo, useRef, useState, useSyncExternalStore, type KeyboardEvent, type MouseEvent } from "react";
import { isReferenceDescribed, projectItemPath, referenceImages, sceneFrameInputs, type ProjectConfig, type ProjectReference, type ProjectReferenceImage } from "../../lib/project";
@@ -35,6 +35,7 @@ import { ProjectStatus } from "./ProjectStatus";
import { MediaThumbnail } from "./MediaThumbnail";
import { inspectReferenceVideo } from "../../lib/referenceVideo";
import { inspectReferenceAudio } from "../../lib/referenceAudio";
+import { chooseRefmodExportPath, refmodExportBlocker } from "../../lib/refmodExport";
import { DebugPromptDialog } from "./DebugPromptDialog";
import { referenceIconPrompt } from "../../lib/referenceIcons";
import { loadDebugOptionsEnabled, subscribeDebugOptions } from "../../lib/settings";
@@ -72,12 +73,14 @@ const readView = (): LibraryView => {
try { return localStorage.getItem(VIEW_KEY) === "details" ? "details" : "icons"; } catch { return "icons"; }
};
-export function ReferencesView({ config, folderPath, onChange, onRegenerateIcon, onGenerateBuiltinIcons, onRegenerateBuiltinIcon, pendingBuiltinIconIds = new Set<string>(), pendingIconIds = new Set<string>(), onOpenGenerator }: {
+export function ReferencesView({ config, folderPath, onChange, onRegenerateIcon, onExportRefmod, onGenerateBuiltinIcons, onRegenerateBuiltinIcon, pendingBuiltinIconIds = new Set<string>(), pendingIconIds = new Set<string>(), onOpenGenerator }: {
config: ProjectConfig;
folderPath: string;
/** `key` groups rapid edits of one reference into one undo step. */
onChange: (next: ProjectConfig, key?: string) => void;
onRegenerateIcon?: (referenceId: string) => void;
+ /** Queues the export; throws when the reference cannot be exported. */
+ onExportRefmod?: (referenceId: string, outputPath: string) => void;
onGenerateBuiltinIcons?: () => void;
onRegenerateBuiltinIcon?: (presetId: string) => void;
pendingBuiltinIconIds?: ReadonlySet<string>;
@@ -100,6 +103,7 @@ export function ReferencesView({ config, folderPath, onChange, onRegenerateIcon,
const [importError, setImportError] = useState<string | null>(null);
const [importingFiles, setImportingFiles] = useState(false);
const [iconError, setIconError] = useState<string | null>(null);
+ const [exportError, setExportError] = useState<string | null>(null);
const [presetDialog, setPresetDialog] = useState(false);
const catalogVisit = useRef(0);
useEffect(() => () => { catalogVisit.current += 1; }, []);
@@ -334,6 +338,15 @@ export function ReferencesView({ config, folderPath, onChange, onRegenerateIcon,
} catch (reason) { setImportError(reason instanceof Error ? reason.message : String(reason)); }
finally { setImportingFiles(false); }
};
+ const exportBlocker = (reference: ProjectReference | undefined) =>
+ !reference ? "Select a reference to export." : !onExportRefmod || !isTauri() ? "Refmod export is available in the desktop app." : refmodExportBlocker(reference);
+ const exportRefmod = async (reference: ProjectReference) => {
+ setExportError(null);
+ try {
+ const outputPath = await chooseRefmodExportPath(reference.name);
+ if (outputPath) onExportRefmod?.(reference.id, outputPath);
+ } catch (reason) { setExportError(reason instanceof Error ? reason.message : String(reason)); }
+ };
const createEmptyReference = () => {
addTextReference(`New ${referenceTypeLabel(pickerType).toLocaleLowerCase()}`, "", pickerType, pickerSubcategory === "all" ? "" : pickerSubcategory);
setPresetDialog(false);
@@ -423,6 +436,7 @@ export function ReferencesView({ config, folderPath, onChange, onRegenerateIcon,
{ id: "rename", label: "Rename", icon: <Rename16 />, shortcut: "F2", disabled: many, onSelect: () => rename(reference.id) },
{ id: "add-file", label: "Add file…", icon: <ImageAdd16 />, disabled: many || Boolean(reference.refmods?.length) || importingFiles, onSelect: () => { selectOnly(reference.id); void addFiles(reference.id); } },
{ id: "reveal", label: `Show in ${fileManagerName()}`, icon: <FolderOpen16 />, disabled: many || !file || !isTauri(), onSelect: () => { if (file) void revealInExplorer(file).catch((reason) => setImportError(String(reason))); } },
+ { id: "export-refmod", label: "Export as refmod…", icon: <Export16 />, disabled: many || Boolean(exportBlocker(reference)), onSelect: () => void exportRefmod(reference) },
{ separator: true },
...(users.length
? users.map((job): MenuEntry => ({ id: `open-${job.id}`, label: `Open ${job.title}`, icon: <OpenExternal16 />, disabled: !onOpenGenerator, onSelect: () => onOpenGenerator?.(job.id) }))
@@ -495,10 +509,12 @@ export function ReferencesView({ config, folderPath, onChange, onRegenerateIcon,
<CommandBarButton icon={<TextFile16 />} label="Add Empty" showLabel onClick={() => addTextReference("Uncategorized", "", "custom")} />
<CommandBarSeparator />
<CommandBarButton icon={<ImageAdd16 />} label="Add file" showLabel disabled={!selected || importingFiles || hasRefmods || selection.length > 1} onClick={() => void addFiles()} />
+ <CommandBarButton icon={<Export16 />} label="Export refmod" tooltip={exportBlocker(selected) ?? "Export as a refmod .safetensors file"} showLabel disabled={selection.length > 1 || Boolean(exportBlocker(selected))} onClick={() => { if (selected) void exportRefmod(selected); }} />
<CommandBarButton icon={<Delete16 />} label="Delete" shortcut="Delete" disabled={!selection.length} onClick={() => remove()} />
</CommandBar>
{importError && <InfoBar severity="error" title="Couldn’t add reference" message={importError} onClose={() => setImportError(null)} />}
{iconError && <InfoBar severity="error" title="Couldn’t regenerate icon" message={iconError} onClose={() => setIconError(null)} />}
+ {exportError && <InfoBar severity="error" title="Couldn’t export refmod" message={exportError} onClose={() => setExportError(null)} />}
<section ref={library} className="reference-library" aria-labelledby="reference-library-heading">
<h2 className="sr-only" id="reference-library-heading">Reference library</h2>
{config.references.length === 0
diff --git a/src/lib/refmodExport.test.ts b/src/lib/refmodExport.test.ts
new file mode 100644
index 0000000..284cd62
--- /dev/null
+++ b/src/lib/refmodExport.test.ts
@@ -0,0 +1,41 @@
+import { describe, expect, it, vi } from "vitest";
+import type { ProjectReference } from "./project";
+import { refmodExportBlocker, refmodExportRequest } from "./refmodExport";
+
+vi.mock("@tauri-apps/api/core", () => ({ invoke: vi.fn() }));
+
+const reference = (patch: Partial<ProjectReference> = {}): ProjectReference => ({
+ id: "ref-hero", kind: "text", name: "Hero", description: "A traveler", intendedUse: [], createdAt: "2026-01-01T00:00:00.000Z", ...patch,
+});
+const images = (count: number) => Array.from({ length: count }, (_, index) => ({ id: `image-${index}`, name: `Image ${index}`, relativePath: `references/${index}.png` }));
+
+describe("refmod export", () => {
+ it("needs raw media, at most nine images and no attached refmods", () => {
+ expect(refmodExportBlocker(reference())).toBe("Add an image, video or sound to export a refmod.");
+ expect(refmodExportBlocker(reference({ kind: "video", video: { startSeconds: 0, includeAudio: true } }))).toContain("Add an image");
+ expect(refmodExportBlocker(reference({ images: images(10) }))).toBe("Refmod export supports up to nine images.");
+ expect(refmodExportBlocker(reference({ images: images(1), refmods: [{ id: "mod", name: "Mod", sourcePath: "D:/mod.safetensors", strength: 1, copies: 1 }] })))
+ .toBe("This reference already uses refmods.");
+ expect(refmodExportBlocker(reference({ images: images(9) }))).toBeNull();
+ expect(refmodExportBlocker(reference({ kind: "video", sourcePath: "D:/walk.mp4" }))).toBeNull();
+ expect(refmodExportBlocker(reference({ kind: "audio", sourcePath: "D:/voice.wav" }))).toBeNull();
+ });
+
+ it("sends images first, then the trimmed clip or the sound", () => {
+ const clip = refmodExportRequest("C:/project", reference({
+ kind: "video", sourcePath: "D:/walk.mp4", images: images(2), video: { startSeconds: 1.5, durationSeconds: 4, includeAudio: false },
+ }), "refmod-1");
+ expect(clip).toMatchObject({
+ jobId: "refmod-1", referencePaths: ["C:/project/references/0.png", "C:/project/references/1.png"],
+ referenceVideos: [{ name: "Hero", sourcePath: "D:/walk.mp4", startSeconds: 1.5, durationSeconds: 4, includeAudio: false }],
+ });
+ expect(clip.referenceAudios).toBeUndefined();
+ const sound = refmodExportRequest("C:/project", reference({ kind: "audio", sourcePath: "D:/voice.wav", audio: { startSeconds: 2, durationSeconds: 6 } }), "refmod-2");
+ expect(sound).toMatchObject({ referencePaths: [], referenceAudios: [{ sourcePath: "D:/voice.wav", startSeconds: 2, durationSeconds: 6 }] });
+ expect(sound.referenceVideos).toBeUndefined();
+ // Frames mode exports the selected stills rather than the clip.
+ const frames = refmodExportRequest("C:/project", reference({ kind: "video", sourcePath: "D:/walk.mp4", video: { startSeconds: 0, includeAudio: true, mode: "frames", frames: images(1) } }), "refmod-3");
+ expect(frames.referencePaths).toEqual(["C:/project/references/0.png"]);
+ expect(frames.referenceVideos).toBeUndefined();
+ });
+});
diff --git a/src/lib/refmodExport.ts b/src/lib/refmodExport.ts
new file mode 100644
index 0000000..50c35ef
--- /dev/null
+++ b/src/lib/refmodExport.ts
@@ -0,0 +1,40 @@
+import { invoke } from "@tauri-apps/api/core";
+import { isAudioReference, isVideoReference, projectItemPath, referenceImages, type ProjectConfig, type ProjectReference } from "./project";
+import type { SlopfabGenerationRequest } from "./runtime";
+
+export interface RefmodExportTarget { outputPath: string; name: string; description: string }
+
+const MAX_REFMOD_IMAGES = 9;
+const hasFile = (reference: ProjectReference) => Boolean(reference.sourcePath || reference.relativePath);
+
+/** Why a reference cannot be exported as a refmod, or null when it can. */
+export function refmodExportBlocker(reference: ProjectReference): string | null {
+ if (reference.refmods?.length) return "This reference already uses refmods.";
+ const images = referenceImages(reference).length;
+ if (images > MAX_REFMOD_IMAGES) return "Refmod export supports up to nine images.";
+ if (!images && !(isVideoReference(reference) && hasFile(reference)) && !isAudioReference(reference)) {
+ return "Add an image, video or sound to export a refmod.";
+ }
+ return null;
+}
+
+/** The reference's own media in SlopFab's order: images, then its clip or sound. */
+export function refmodExportRequest(folderPath: string, reference: ProjectReference, jobId: string): SlopfabGenerationRequest {
+ const file = { name: reference.name, relativePath: reference.relativePath, sourcePath: reference.sourcePath };
+ return {
+ jobId, prompt: "", frames: 0, steps: 0, seed: 0, canvasWidth: 0, canvasHeight: 0,
+ referencePaths: referenceImages(reference).map((image) => projectItemPath(folderPath, image)!),
+ ...(isVideoReference(reference) && hasFile(reference) ? { referenceVideos: [{
+ ...file, startSeconds: reference.video?.startSeconds ?? 0, durationSeconds: reference.video?.durationSeconds ?? 2, includeAudio: reference.video?.includeAudio ?? true,
+ }] } : {}),
+ ...(isAudioReference(reference) ? { referenceAudios: [{
+ ...file, startSeconds: reference.audio?.startSeconds ?? 0, durationSeconds: reference.audio?.durationSeconds ?? 15,
+ }] } : {}),
+ };
+}
+
+/** Asks where to save the refmod. Null when the user cancels. */
+export const chooseRefmodExportPath = (name: string) => invoke<string | null>("choose_refmod_export_path", { name });
+
+export const exportReferenceRefmod = (request: SlopfabGenerationRequest, config: ProjectConfig, target: RefmodExportTarget) =>
+ invoke<void>("export_reference_refmod", { request, config, ...target });
diff --git a/src/lib/workQueue.test.ts b/src/lib/workQueue.test.ts
index aa90752..0995888 100644
--- a/src/lib/workQueue.test.ts
+++ b/src/lib/workQueue.test.ts
@@ -623,6 +623,47 @@ describe("application work queue", () => {
expect(releaseRendered).toHaveBeenCalledWith(id);
expect(saveGeneratedScene).not.toHaveBeenCalled();
});
+ it("exports a reference as a refmod after preparing its clip, without planning a generation", async () => {
+ const { queue, first } = setup();
+ first.update((config) => ({ ...config, references: [{
+ id: "ref-walk", kind: "video", name: "Walk", description: "A brisk walk", intendedUse: [], createdAt: config.createdAt,
+ sourcePath: "D:/clips/walk.mp4", video: { startSeconds: 1, durationSeconds: 4, includeAudio: true },
+ images: [{ id: "front", name: "front", relativePath: "references/front.png" }],
+ }] }));
+ let exported: Record<string, unknown> | undefined;
+ vi.mocked(invoke).mockImplementation(async (command, payload) => {
+ if (command === "export_reference_refmod") exported = structuredClone(payload as Record<string, unknown>);
+ });
+ queue.exportReferenceRefmod(first, "ref-walk", "D:/out/walk.safetensors");
+ const item = () => queue.getSnapshot().find((candidate) => candidate.kind === "refmod")!;
+ expect(item()).toMatchObject({ title: "Refmod · Walk", sceneId: "ref-walk" });
+ await waitFor(() => expect(item()).toMatchObject({ status: "completed", detail: "Refmod saved" }));
+ expect(exported).toMatchObject({
+ outputPath: "D:/out/walk.safetensors", name: "Walk", description: "A brisk walk",
+ request: { jobId: item().id, referencePaths: ["C:/First/references/front.png"], referenceVideoIds: ["video-1"] },
+ });
+ expect(resolveSlopfabPlan).not.toHaveBeenCalled();
+ expect(enqueueSlopfabGeneration).not.toHaveBeenCalled();
+ await waitFor(() => expect(releaseReferenceVideos).toHaveBeenCalledWith(["video-1"]));
+ });
+ it("refuses refmod exports without media and reports a failed export", async () => {
+ const { queue, first } = setup();
+ first.update((config) => ({ ...config, references: [
+ { id: "ref-text", kind: "text", name: "Mood", description: "Warm light", intendedUse: [], createdAt: config.createdAt },
+ { id: "ref-face", kind: "text", name: "Face", description: "", intendedUse: [], createdAt: config.createdAt, images: [{ id: "front", name: "front", relativePath: "references/front.png" }] },
+ ] }));
+ expect(() => queue.exportReferenceRefmod(first, "ref-gone", "D:/out/gone.safetensors")).toThrow("no longer exists");
+ expect(() => queue.exportReferenceRefmod(first, "ref-text", "D:/out/mood.safetensors")).toThrow("Add an image, video or sound");
+ vi.mocked(invoke).mockImplementation(async (command) => {
+ if (command === "export_reference_refmod") throw new Error("refmod export: required video/audio VAE path is missing");
+ });
+ queue.exportReferenceRefmod(first, "ref-face", "D:/out/face.safetensors");
+ expect(() => queue.exportReferenceRefmod(first, "ref-face", "D:/out/again.safetensors")).toThrow("already being exported");
+ const item = () => queue.getSnapshot().find((candidate) => candidate.kind === "refmod")!;
+ await waitFor(() => expect(item()).toMatchObject({ status: "failed", detail: "Refmod export failed" }));
+ expect(item().error).toContain("VAE path is missing");
+ expect(prepareReferenceVideos).not.toHaveBeenCalled();
+ });
it("makes interrupted scenes restartable after the application has closed", () => {
const record = project("Interrupted");
record.config.generationJobs[0].status = "generating";
diff --git a/src/lib/workQueue.ts b/src/lib/workQueue.ts
index 08c9879..8940e1b 100644
--- a/src/lib/workQueue.ts
+++ b/src/lib/workQueue.ts
@@ -6,7 +6,7 @@ import { compileImagePrompt } from "./imagePrompt";
import { completeImageDraft, imageFamilyRoot, imageGenerationSnapshot, saveImageDraft } from "./imageHistory";
import type { ImageGenerationSnapshot } from "./project";
import { outputDimensions } from "./export";
-import { projectItemPath, referenceImages, referenceRefmodInputs } from "./project";
+import { projectItemPath, referenceDefinition, referenceImages, referenceRefmodInputs } from "./project";
import { engineProviderSetting, type GeneratorTemplate } from "./settings";
import { describeDiagnosticError, writeDiagnostic } from "./diagnostics";
import { GenerationTimingEstimator, type CompletedGenerationTiming, type GenerationTimingProgress } from "./generationTiming";
@@ -21,6 +21,7 @@ import { purgeTimelineThumbnails } from "./timelineThumbnails";
import { ReferenceIconWork } from "./referenceIconWork";
import { prepareReferenceVideos, releaseReferenceVideos } from "./referenceVideo";
import { prepareReferenceAudios, releaseReferenceAudios } from "./referenceAudio";
+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 }
@@ -35,7 +36,7 @@ function imageGenerationSeed(seed: number): number {
export interface WorkItem {
imageAssetId?: string | null;
imageDraftId?: string;
- kind?: "reference-icons" | "image";
+ kind?: "reference-icons" | "image" | "refmod";
id: string;
projectKey: string;
folderPath: string;
@@ -58,6 +59,7 @@ interface PendingWork {
imageParentId?: string;
image?: boolean;
imageGeneration?: ImageGenerationSnapshot;
+ refmod?: RefmodExportTarget;
id: string;
session: ProjectSession;
sceneId: string;
@@ -251,8 +253,31 @@ export class WorkQueue {
settings: { frames: 1, steps: request.steps, seed: request.seed, canvasWidth: width, canvasHeight: 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.image) return;
+ if (work.image || work.refmod) return;
work.session.update((current) => ({
...current, generationJobs: current.generationJobs.map((job) => job.id === work.sceneId ? { ...job, ...patch, updatedAt: new Date().toISOString() } : job),
}), dirty);
@@ -269,10 +294,11 @@ export class WorkQueue {
continue;
}
const work = this.work.get(next.id)!;
- this.patch(work.id, { status: "preparing", detail: "Preparing generation" });
+ this.patch(work.id, { status: "preparing", detail: work.refmod ? "Preparing refmod" : "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();
@@ -290,14 +316,7 @@ export class WorkQueue {
}
if (!work.image) await purgeTimelineThumbnails(next.folderPath, work.sceneId).catch(() => undefined);
if (work.cancelled) continue;
- if (work.request.referenceVideos?.length) {
- work.request.referenceVideoIds = await prepareReferenceVideos(next.folderPath, work.request, work.config,
- () => work.cancelled, (detail) => this.patch(work.id, { detail }));
- }
- if (work.request.referenceAudios?.length) {
- work.request.referenceAudioIds = await prepareReferenceAudios(next.folderPath, work.request,
- () => work.cancelled, (detail) => this.patch(work.id, { detail }));
- }
+ await this.prepareReferences(work, next.folderPath);
if (work.cancelled) continue;
const plan = await resolveSlopfabPlan(work.request, work.config, next.folderPath);
if (work.cancelled) continue;
@@ -319,6 +338,24 @@ export class WorkQueue {
}
} 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);
@@ -359,7 +396,7 @@ export class WorkQueue {
}
const detail = describeDiagnosticError(reason);
this.timing.clear(work.id);
- this.patch(work.id, { status: "failed", detail: "Generation failed", error: detail, completionAt: null, cancelling: false });
+ this.patch(work.id, { status: "failed", detail: work.refmod ? "Refmod export failed" : "Generation failed", error: detail, completionAt: null, cancelling: false });
this.updateScene(work, { 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);
@@ -369,7 +406,8 @@ export class WorkQueue {
if (this.icons.progressEvent(event)) return;
const work = this.work.get(event.jobId);
const item = this.items.find((candidate) => candidate.id === event.jobId);
- if (!work || !item || !["preparing", "generating"].includes(item.status)) return;
+ // 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.