Skip to content

AI media harness for planning, generating, and editing locally

TypeScript

Add imported references to image bar

Bitti7757ae1806a7
diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md
index fde2f39..28e6084 100644
--- a/RELEASE_NOTES.md
+++ b/RELEASE_NOTES.md
@@ -4,6 +4,7 @@ This alpha update adds DMAD LoRA support, resumable model downloads, and improve
## What's new
+- Images imported through Timeline or References, including selected video reference frames, appear in the image bar and open for editing.
- Duplicate references from their context menu, including multiple selected references with their prompts, attachments, and media settings.
- Create a video scene with an image as its first frame, or create an image reference, directly from the Image tab's image bar context menu.
- Every project now includes Video and Image tabs. Older video and image projects keep their saved content, and Export supports both output types.
diff --git a/src/components/ImportedImages.integration.test.tsx b/src/components/ImportedImages.integration.test.tsx
new file mode 100644
index 0000000..c53b719
--- /dev/null
+++ b/src/components/ImportedImages.integration.test.tsx
@@ -0,0 +1,81 @@
+// @vitest-environment jsdom
+import "@testing-library/jest-dom/vitest";
+import { cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";
+import { useState, useSyncExternalStore } from "react";
+import { afterEach, beforeEach, expect, it, vi } from "vitest";
+import { invoke } from "@tauri-apps/api/core";
+import { createProjectConfig } from "../lib/project";
+import { createProject } from "../lib/persistence";
+import { ProjectSession } from "../lib/projectSession";
+import { captureReferenceVideoFrame } from "../lib/referenceVideoFrame";
+import { ReferencesView } from "./workspace/ReferencesView";
+import { TimelineView } from "./workspace/TimelineView";
+import { ImageEditor } from "./workspace/ImageEditor";
+
+vi.mock("@tauri-apps/api/core", () => ({ invoke: vi.fn() }));
+vi.mock("@tauri-apps/api/event", () => ({ listen: vi.fn(async () => () => undefined) }));
+vi.mock("../lib/persistence", async (original) => ({ ...await original<typeof import("../lib/persistence")>(), readMediaFileUrl: vi.fn(async () => "blob:image") }));
+vi.mock("../lib/referenceVideoFrame", () => ({ captureReferenceVideoFrame: vi.fn() }));
+vi.mock("./workspace/MediaThumbnail", () => ({ MediaThumbnail: () => null }));
+
+const input = { name: "Imports", prompt: "", aspectRatio: "16:9" as const, resolution: "416p" as const, targetDurationSeconds: 10 };
+beforeEach(() => {
+ Object.assign(window, { __TAURI_INTERNALS__: {} });
+ vi.spyOn(HTMLMediaElement.prototype, "pause").mockImplementation(() => {});
+ vi.spyOn(HTMLMediaElement.prototype, "load").mockImplementation(() => {});
+ vi.stubGlobal("URL", class extends URL { static revokeObjectURL = vi.fn(); });
+});
+afterEach(() => { cleanup(); localStorage.clear(); vi.restoreAllMocks(); vi.resetAllMocks(); vi.unstubAllGlobals(); delete (window as unknown as Record<string, unknown>).__TAURI_INTERNALS__; });
+
+function setup(view: "references" | "timeline", video = false) {
+ const config = createProjectConfig(input);
+ config.references = [{ id: "reference", name: "Subject", kind: video ? "video" : "text", description: "", intendedUse: [], createdAt: config.createdAt,
+ ...(video ? { sourcePath: "D:/clip.mp4", video: { startSeconds: 0, durationSeconds: 5, includeAudio: false } } : {}) }];
+ const session = new ProjectSession({ config, folderPath: "D:/Imports" }, vi.fn());
+ function Harness() {
+ const { config } = useSyncExternalStore(session.subscribe, session.getSnapshot);
+ const [editor, setEditor] = useState(false);
+ return <><button onClick={() => setEditor(true)}>Open image editor</button>{editor
+ ? <ImageEditor config={config} folderPath="D:/Imports" onChange={session.edit} onGenerate={vi.fn()} onCancel={vi.fn()} />
+ : view === "references" ? <ReferencesView config={config} folderPath="D:/Imports" onChange={session.edit} />
+ : <TimelineView config={config} folderPath="D:/Imports" onChange={session.edit} onOpenGenerator={vi.fn()} />}</>;
+ }
+ render(<Harness />);
+ return session;
+}
+
+it.each(["references", "timeline"] as const)("puts images imported from %s in the image bar", async (view) => {
+ vi.mocked(invoke).mockImplementation(async (command) => command === "choose_reference_files" || command === "import_media_files"
+ ? [{ kind: "image", name: "Photo", relativePath: "references/photo.png", sourcePath: null, mimeType: "image/png" }] : null);
+ const session = setup(view);
+ fireEvent.click(screen.getAllByRole("button", { name: view === "references" ? "Add file" : /Import media/ })[0]);
+ await waitFor(() => expect(session.getSnapshot().config.assets).toHaveLength(1));
+ fireEvent.click(screen.getByRole("button", { name: "Open image editor" }));
+ expect(screen.getByRole("button", { name: "View Photo" })).toBeInTheDocument();
+});
+
+it("puts a selected video frame in the image bar", async () => {
+ const session = setup("references", true);
+ fireEvent.click(screen.getByRole("button", { name: "Edit video clip" }));
+ const preview = await screen.findByLabelText("Subject video preview");
+ Object.defineProperties(preview, { duration: { value: 20 }, readyState: { value: 2 } });
+ fireEvent.loadedMetadata(preview);
+ fireEvent.click(screen.getByRole("tab", { name: "Frames" }));
+ fireEvent.seeked(preview);
+ vi.mocked(captureReferenceVideoFrame).mockResolvedValue({ id: "picked", name: "Subject frame", relativePath: "references/frames/picked.png", timeSeconds: 0 });
+ fireEvent.click(screen.getByRole("button", { name: "Add current frame" }));
+ await waitFor(() => expect(session.getSnapshot().config.assets).toHaveLength(1));
+ fireEvent.click(screen.getByRole("button", { name: "Done" }));
+ fireEvent.click(screen.getByRole("button", { name: "Open image editor" }));
+ expect(screen.getByRole("button", { name: "View Subject frame" })).toBeInTheDocument();
+});
+
+it("saves initial project reference imports as image assets", async () => {
+ const config = createProjectConfig(input);
+ config.references = [{ id: "initial", kind: "image", name: "Initial photo", relativePath: "references/initial.jpg", description: "", intendedUse: [], createdAt: config.createdAt }];
+ vi.mocked(invoke).mockImplementation(async (command) => command === "create_project" ? { config, folderPath: "D:/Imports" } : undefined);
+ const record = await createProject({ ...input, referenceImages: [{ name: "Initial photo", sourcePath: "D:/initial.jpg" }] });
+ expect(record!.config.assets).toHaveLength(1);
+ expect(record!.config.assets[0]).toMatchObject({ kind: "image", name: "Initial photo", relativePath: "references/initial.jpg" });
+ expect(invoke).toHaveBeenCalledWith("save_project", { folderPath: "D:/Imports", config: record!.config });
+});
diff --git a/src/components/workspace/ImageEditor.test.tsx b/src/components/workspace/ImageEditor.test.tsx
index 2695cd5..030a5f4 100644
--- a/src/components/workspace/ImageEditor.test.tsx
+++ b/src/components/workspace/ImageEditor.test.tsx
@@ -32,6 +32,38 @@ function setup(initial = parseProjectConfig(fixture)) {
/* Undo lives in the title bar; inside the editor it is Ctrl+Z. */
const undoImageEdit = () => fireEvent.keyDown(screen.getByRole("tree", { name: "Image nodes" }), { key: "z", code: "KeyZ", ctrlKey: true });
+it.each(["media/photo.png", "references/photo.webp", "references/frames/frame.png"])("opens %s for editing after measuring its preview and preserves the original", async (relativePath) => {
+ vi.spyOn(persistence, "readMediaFileUrl").mockResolvedValue("blob:imported");
+ vi.stubGlobal("URL", class extends URL { static revokeObjectURL = vi.fn(); });
+ const initial = parseProjectConfig(fixture);
+ initial.assets = [{ id: "imported", name: "Imported", kind: "image", relativePath, mimeType: "image/png", createdAt: initial.createdAt }];
+ const current = setup(initial);
+ fireEvent.click(screen.getByRole("button", { name: "View Imported" }));
+ const preview = await waitFor(() => {
+ const element = document.querySelector<HTMLImageElement>(".image-frame img");
+ expect(element).not.toBeNull();
+ return element!;
+ });
+ Object.defineProperties(preview, { naturalWidth: { value: 640 }, naturalHeight: { value: 480 } });
+ fireEvent.load(preview);
+ expect(current().imageScene).toMatchObject({ rootType: "image", outputAssetId: "imported", sourceImage: { relativePath, width: 640, height: 480 } });
+ expect(current().assets).toHaveLength(1);
+ expect(promptValue(screen.getByLabelText("Edit prompt"))).toBe("");
+ typePrompt(screen.getByLabelText("Edit prompt"), "Make the sky pink");
+ expect(compileImageEdits(current()).edits[0]).toMatchObject({ width: 640, height: 480 });
+ expect(current().assets[0]).toMatchObject({ id: "imported", relativePath, width: 640, height: 480 });
+ expect(current().assets[0].imageGeneration).toBeUndefined();
+ expect(current().assets[1]).toMatchObject({ imageDraft: true, parentAssetId: "imported" });
+ const draftId = current().assets[1].id;
+ fireEvent.click(screen.getByRole("button", { name: "View Imported" }));
+ expect(current().imageScene?.outputAssetId).toBe("imported");
+ expect(promptValue(screen.getByLabelText("Edit prompt"))).toBe("");
+ fireEvent.click(screen.getByRole("button", { name: "View Editing Imported" }));
+ expect(current().imageScene?.outputAssetId).toBe(draftId);
+ expect(promptValue(screen.getByLabelText("Edit prompt"))).toBe("Make the sky pink");
+ expect(parseProjectConfig(JSON.parse(JSON.stringify(current()))).imageScene).toEqual(current().imageScene);
+});
+
it.each(["Create Scene", "Create Reference"])("%s uses the right-clicked image without changing the viewed image", (action) => {
const initial = parseProjectConfig(fixture);
initial.assets = ["Viewed", "Chosen"].map((name) => ({ id: name, name, kind: "image", relativePath: `media/generated/${name}.png`, mimeType: "image/png", createdAt: initial.createdAt }));
diff --git a/src/components/workspace/ImageEditor.tsx b/src/components/workspace/ImageEditor.tsx
index 027e01f..05fa7b6 100644
--- a/src/components/workspace/ImageEditor.tsx
+++ b/src/components/workspace/ImageEditor.tsx
@@ -193,8 +193,16 @@ export function ImageEditor({ config, folderPath, onChange: changeConfig, onGene
};
const selectImage = (id: string) => {
resetImageEditing(id);
- onChange((current) => restoreGeneratedImage(current, id));
+ attempt(() => onChange((current) => restoreGeneratedImage(current, id)));
};
+ const measureImportedImage = (id: string, size: { width: number; height: number }) => attempt(() => {
+ onChange((current) => {
+ const asset = current.assets.find((item) => item.id === id);
+ if (!asset?.relativePath || asset.imageGeneration || current.imageScene?.outputAssetId !== id || current.imageScene.rootType === "image") return current;
+ const next = { ...current, assets: current.assets.map((item) => item.id === id ? { ...item, ...size } : item) };
+ return restoreGeneratedImage(next, id);
+ });
+ });
const createFromImage = (id: string, asScene: boolean) => {
const asset = config.assets.find((candidate) => candidate.id === id && candidate.kind === "image");
if (!asset || (!asset.relativePath && !asset.sourcePath)) return;
@@ -507,7 +515,7 @@ export function ImageEditor({ config, folderPath, onChange: changeConfig, onGene
if (source) replaceScene(createImageEditScene(source, scene));
}) },
...(imageMenu.id ? [
- { label: "Edit", icon: <Edit16 />, separator: true, action: () => attempt(() => config.assets.find((asset) => asset.id === imageMenu.id)?.imageDraft ? selectImage(imageMenu.id!) : replaceScene(editGeneratedImage(config, imageMenu.id!).imageScene!)) },
+ { label: "Edit", icon: <Edit16 />, separator: true, action: () => attempt(() => menuImage?.imageDraft || !menuImage?.width || !menuImage?.height ? selectImage(imageMenu.id!) : replaceScene(editGeneratedImage(config, imageMenu.id!).imageScene!)) },
{ label: "Create Scene", icon: <Film16 />, separator: true, disabled: !canUseMenuImage, action: () => attempt(() => createFromImage(imageMenu.id!, true)) },
{ label: "Create Reference", icon: <References16 />, disabled: !canUseMenuImage, action: () => attempt(() => createFromImage(imageMenu.id!, false)) },
{ label: "Remove", icon: <Delete16 />, danger: true,
@@ -589,7 +597,7 @@ export function ImageEditor({ config, folderPath, onChange: changeConfig, onGene
onPointerUpCapture={endPan} onPointerCancelCapture={endPan} onLostPointerCapture={endPan}
onAuxClick={(event) => { if (event.button === 1) event.preventDefault(); }}>
<div className="image-frame" style={{ "--image-ratio": width / height, "--image-zoom": view.zoom, "--image-pan-x": `${view.x}px`, "--image-pan-y": `${view.y}px` } as CSSProperties}>
- {imageRoot && scene.sourceImage ? <ReferenceImage folderPath={folderPath} relativePath={scene.sourceImage.relativePath} alt={scene.sourceImage.name} /> : output && !imageRoot && (output.relativePath || output.sourcePath) ? <ReferenceImage folderPath={folderPath} relativePath={output.relativePath} sourcePath={output.sourcePath} alt={output.name} /> : <div className="image-empty"><Image32 aria-hidden="true" /><strong>{imageRoot ? "Add an image to edit" : "Compose your image"}</strong><span>{imageRoot ? "Choose Add image file in the image bar menu, then enter an edit prompt." : "Add objects, text and groups, then describe them in the inspector."}</span></div>}
+ {imageRoot && scene.sourceImage ? <ReferenceImage folderPath={folderPath} relativePath={scene.sourceImage.relativePath} alt={scene.sourceImage.name} /> : output && !imageRoot && (output.relativePath || output.sourcePath) ? <ReferenceImage key={output.id} folderPath={folderPath} relativePath={output.relativePath} sourcePath={output.sourcePath} alt={output.name} onMeasured={!output.imageGeneration ? (size) => measureImportedImage(output.id, size) : undefined} /> : <div className="image-empty"><Image32 aria-hidden="true" /><strong>{imageRoot ? "Add an image to edit" : "Compose your image"}</strong><span>{imageRoot ? "Choose Add image file in the image bar menu, then enter an edit prompt." : "Add objects, text and groups, then describe them in the inspector."}</span></div>}
<svg className={`image-overlay ${drawKind ? "drawing" : ""}`} viewBox="0 0 1000 1000" preserveAspectRatio="none" aria-label="Image placement canvas" onPointerDown={pointerDown} onPointerMove={pointerMove} onPointerUp={pointerUp} onPointerCancel={() => { pointer.current = null; setDraftBox(null); }}>
{boxes && scene.nodes.filter((node) => node.box).map((node) => {
const box = pointer.current?.id === node.id && draftBox ? draftBox : node.box!;
diff --git a/src/components/workspace/ReferenceImage.tsx b/src/components/workspace/ReferenceImage.tsx
index f4a8291..457aa24 100644
--- a/src/components/workspace/ReferenceImage.tsx
+++ b/src/components/workspace/ReferenceImage.tsx
@@ -22,7 +22,7 @@ const mimeFor = (path: string) => {
}
};
-export function ReferenceImage({ folderPath, relativePath, sourcePath = null, alt, className = "" }: {
+export function ReferenceImage({ folderPath, relativePath, sourcePath = null, alt, className = "", onMeasured }: {
folderPath: string;
/** Set for a file copied into the project. */
relativePath?: string | null;
@@ -30,6 +30,7 @@ export function ReferenceImage({ folderPath, relativePath, sourcePath = null, al
sourcePath?: string | null;
alt: string;
className?: string;
+ onMeasured?: (size: { width: number; height: number }) => void;
}) {
const [url, setUrl] = useState<string | null>(null);
const [failed, setFailed] = useState(false);
@@ -59,7 +60,10 @@ export function ReferenceImage({ folderPath, relativePath, sourcePath = null, al
}, [folderPath, relativePath, sourcePath]);
if (url) {
- return <img className={className} src={url} alt={alt} loading="lazy" onError={() => setFailed(true)} />;
+ return <img className={className} src={url} alt={alt} loading="lazy" onError={() => setFailed(true)} onLoad={(event) => {
+ const { naturalWidth: width, naturalHeight: height } = event.currentTarget;
+ if (width && height) onMeasured?.({ width, height });
+ }} />;
}
/* Two different states, said plainly rather than with one ambiguous
placeholder: still loading, or the file is not where the project says. */
diff --git a/src/lib/imageHistory.ts b/src/lib/imageHistory.ts
index 8071c5d..0849d57 100644
--- a/src/lib/imageHistory.ts
+++ b/src/lib/imageHistory.ts
@@ -1,4 +1,4 @@
-import { createImageEditScene, createImageScene, imageSceneReferenceIds, imageScenePromptText } from "./imageScene";
+import { createImageEditScene, createImageScene, imageSceneReferenceIds, imageScenePromptText, imageSourceSchema } from "./imageScene";
import { actionReferenceIds, type ImageGenerationSnapshot, type ProjectAsset, type ProjectConfig } from "./project";
export function imageGenerationSnapshot(config: ProjectConfig, prompt: string, generatorTemplateId: string): ImageGenerationSnapshot {
@@ -92,6 +92,11 @@ export function restoreGeneratedImage(config: ProjectConfig, id: string): Projec
const asset = config.assets.find((candidate) => candidate.id === id && candidate.kind === "image");
if (!asset) return config;
const snapshot = asset.imageGeneration;
+ const source = !snapshot && imageSourceSchema.safeParse(asset);
+ if (source && source.success) {
+ return { ...config, thumbnail: asset.relativePath ?? null,
+ imageScene: { ...createImageEditScene(source.data, config.imageScene ?? undefined), outputAssetId: id } };
+ }
if (!snapshot) return { ...config, thumbnail: asset.relativePath ?? null,
imageScene: { ...(config.imageScene?.rootType === "image" ? createImageScene() : config.imageScene ?? createImageScene(config.brief.prompt)), outputAssetId: id } };
const restored = structuredClone(snapshot);
diff --git a/src/lib/persistence.ts b/src/lib/persistence.ts
index ebaa2cb..e686faa 100644
--- a/src/lib/persistence.ts
+++ b/src/lib/persistence.ts
@@ -1,4 +1,5 @@
import { invoke } from "@tauri-apps/api/core";
+import { addReferenceImageAssets } from "./referenceImageAssets";
import {
createProjectConfig,
parseProjectConfig,
@@ -245,6 +246,8 @@ export async function createProject(input: CreateProjectInput): Promise<ProjectR
});
if (!value) return null;
const record = { ...value, config: parseProjectConfig(value.config) };
+ const withImages = addReferenceImageAssets(record.config);
+ if (withImages !== record.config) return saveProject({ ...record, config: withImages });
rememberPath(record.folderPath);
return record;
}
diff --git a/src/lib/projectSession.ts b/src/lib/projectSession.ts
index e6a2133..2653667 100644
--- a/src/lib/projectSession.ts
+++ b/src/lib/projectSession.ts
@@ -1,6 +1,7 @@
import { describeDiagnosticError, writeDiagnostic } from "./diagnostics";
import type { ProjectConfig, ProjectRecord } from "./project";
import { saveImageDraft } from "./imageHistory";
+import { addReferenceImageAssets } from "./referenceImageAssets";
import { createUndoStack, type UndoStack } from "./undo";
export type ProjectUpdate = ProjectConfig | ((current: ProjectConfig) => ProjectConfig);
@@ -102,7 +103,7 @@ export class ProjectSession {
/** A change the person made: recorded for undo. Same `key` within 500ms = one step. */
edit = (next: ProjectUpdate, key = "edit") => {
- const config = saveImageDraft(typeof next === "function" ? next(this.state.config) : next);
+ const config = saveImageDraft(addReferenceImageAssets(typeof next === "function" ? next(this.state.config) : next, this.state.config.references));
if (config === this.state.config) return;
this.history.record({ config: this.state.config, mark: this.backgroundSeq }, key);
this.apply(config, true);
diff --git a/src/lib/referenceImageAssets.test.ts b/src/lib/referenceImageAssets.test.ts
new file mode 100644
index 0000000..09114a7
--- /dev/null
+++ b/src/lib/referenceImageAssets.test.ts
@@ -0,0 +1,57 @@
+import { describe, expect, it, vi } from "vitest";
+import { createProjectConfig, parseProjectConfig } from "./project";
+import { ProjectSession } from "./projectSession";
+import { addReferenceImageAssets } from "./referenceImageAssets";
+
+const project = () => createProjectConfig({ name: "Imported images", prompt: "", aspectRatio: "16:9", resolution: "416p", targetDurationSeconds: 10 });
+
+describe("reference images in the image bar", () => {
+ it("registers legacy images, attachments and saved frames, but not icons, video, audio or refmods", () => {
+ const config = project();
+ const base = { description: "", intendedUse: [], createdAt: config.createdAt };
+ config.references = [
+ { ...base, id: "legacy", kind: "image", name: "Legacy", relativePath: "references/legacy.jpg" },
+ { ...base, id: "text", kind: "text", name: "Mood", iconRelativePath: "references/icons/mood.png",
+ images: [{ id: "picture", name: "Mood board", relativePath: "references/mood.webp" }],
+ refmods: [{ id: "mod", name: "Style", sourcePath: "D:/style.refmod", strength: 1, copies: 1 }] },
+ { ...base, id: "video", kind: "video", name: "Clip", sourcePath: "D:/clip.mp4",
+ video: { mode: "video", startSeconds: 0, durationSeconds: 5, includeAudio: false,
+ frames: [{ id: "frame", name: "Clip at 0:02.0", timeSeconds: 2, relativePath: "references/frames/frame.png" }] } },
+ { ...base, id: "audio", kind: "audio", name: "Sound", sourcePath: "D:/sound.wav" },
+ ];
+ const next = addReferenceImageAssets(config);
+ expect(next.assets.map((asset) => [asset.name, asset.mimeType])).toEqual([
+ ["Legacy", "image/jpeg"], ["Mood board", "image/webp"], ["Clip at 0:02.0", "image/png"],
+ ]);
+ expect(next.imageScene).toBe(config.imageScene);
+ expect(addReferenceImageAssets(next)).toBe(next);
+ expect(parseProjectConfig(JSON.parse(JSON.stringify(next))).assets).toEqual(next.assets);
+ });
+
+ it("adds images in the import's undo step and preserves removal across edits and reopening", () => {
+ const config = project();
+ const session = new ProjectSession({ config, folderPath: "D:/Images" }, vi.fn());
+ session.edit((current) => ({ ...current, references: [{ id: "reference", name: "Photo", kind: "text", description: "", intendedUse: [], createdAt: config.createdAt,
+ images: [{ id: "photo", name: "Photo", relativePath: "references/photo.png" }] }] }));
+ const imported = session.getSnapshot().config;
+ expect(imported.assets).toHaveLength(1);
+ session.undo();
+ expect(session.getSnapshot().config.assets).toEqual([]);
+ expect(session.getSnapshot().config.references).toEqual([]);
+ session.redo();
+ expect(session.getSnapshot().config.assets).toEqual(imported.assets);
+ session.edit((current) => ({ ...current, references: [...current.references, { ...current.references[0], id: "copy" }] }));
+ expect(session.getSnapshot().config.assets).toHaveLength(1);
+ session.edit((current) => ({ ...current, assets: [] }));
+ session.edit((current) => ({ ...current, references: current.references.map((reference) => ({ ...reference, name: "Renamed" })) }));
+ const reopened = new ProjectSession({ config: parseProjectConfig(JSON.parse(JSON.stringify(session.getSnapshot().config))), folderPath: "D:/Images" }, vi.fn());
+ expect(reopened.getSnapshot().config.assets).toEqual([]);
+ });
+
+ it("reuses an existing image asset when it is made into a reference", () => {
+ const config = project();
+ config.assets = [{ id: "image", kind: "image", name: "Photo", relativePath: "media/photo.png", mimeType: "image/png", createdAt: config.createdAt }];
+ config.references = [{ id: "reference", name: "Photo", kind: "image", relativePath: "media/photo.png", description: "", intendedUse: [], createdAt: config.createdAt }];
+ expect(addReferenceImageAssets(config)).toBe(config);
+ });
+});
diff --git a/src/lib/referenceImageAssets.ts b/src/lib/referenceImageAssets.ts
new file mode 100644
index 0000000..edec6f5
--- /dev/null
+++ b/src/lib/referenceImageAssets.ts
@@ -0,0 +1,30 @@
+import { referenceImages, type ProjectAsset, type ProjectConfig, type ProjectReference, type ProjectReferenceImage, type StoredLocation } from "./project";
+
+const locationKey = (image: StoredLocation) => image.sourcePath ? `external:${image.sourcePath}` : image.relativePath ? `project:${image.relativePath}` : null;
+const pictures = (reference: ProjectReference): ProjectReferenceImage[] => [
+ ...referenceImages({ ...reference, video: undefined }),
+ ...(reference.video?.frames ?? []),
+];
+
+/** Register newly attached pictures once. Comparing against the previous references
+ * keeps an image removed from the bar from returning on the next reference edit. */
+export function addReferenceImageAssets(config: ProjectConfig, previous: readonly ProjectReference[] = []): ProjectConfig {
+ if (config.references === previous) return config;
+ const known = new Set([
+ ...config.assets.filter((asset) => asset.kind === "image").map(locationKey),
+ ...previous.flatMap(pictures).map(locationKey),
+ ]);
+ const added: ProjectAsset[] = [];
+ for (const reference of config.references) {
+ for (const image of pictures(reference)) {
+ const key = locationKey(image);
+ if (!key || known.has(key)) continue;
+ known.add(key);
+ const extension = (image.sourcePath ?? image.relativePath ?? "").split(".").pop()?.toLowerCase();
+ const mimeType = extension === "png" ? "image/png" : extension === "webp" ? "image/webp" : extension === "gif" ? "image/gif" : "image/jpeg";
+ added.push({ id: `asset-${crypto.randomUUID()}`, kind: "image", name: image.name,
+ relativePath: image.relativePath, sourcePath: image.sourcePath, mimeType, createdAt: new Date().toISOString() });
+ }
+ }
+ return added.length ? { ...config, assets: [...config.assets, ...added] } : config;
+}