Skip to content

AI media harness for planning, generating, and editing locally

TypeScript

Offer shared built-in reference icons

Bittid827f193d542
diff --git a/docs/reference-icons.md b/docs/reference-icons.md
new file mode 100644
index 0000000..d0ca517
--- /dev/null
+++ b/docs/reference-icons.md
@@ -0,0 +1,18 @@
+# Built-in reference icons
+
+Opening **Add a reference** for the first time offers to generate any missing
+built-in icons. **Don't ask again** remembers either answer across app launches:
+Start enables generation of missing icons on later visits; Cancel disables it.
+Without that option, the question appears only once per app session.
+
+Generating the catalog can take a long time. It uses the current generator's
+model settings, runs in the Work Queue, and yields to normal video generation.
+An interrupted built-in icon is retried after the videos finish. The Work Queue
+can also cancel the batch. An engine or storage failure stops the remaining
+catalog so the same failure is not repeated for every reference.
+
+Built-in icons are shared across projects. They are saved as 256×256 JPEGs in
+`reference-icons/` inside the same directory as `slopus.log`, using stable preset
+IDs as filenames. They load as their cards come into view and are never sent to
+the video engine as reference-image attachments. Existing project-specific
+icons remain in their project folders.
diff --git a/src-tauri/src/diagnostics.rs b/src-tauri/src/diagnostics.rs
index 98ecfb6..33c4e92 100644
--- a/src-tauri/src/diagnostics.rs
+++ b/src-tauri/src/diagnostics.rs
@@ -285,11 +285,15 @@ fn redact(value: Value, depth: usize) -> Value {
}
}
-pub fn initialize(app: &AppHandle) -> Result<DiagnosticLogInfo, String> {
- let directory = app
+pub fn directory(app: &AppHandle) -> PathBuf {
+ app
.path()
.app_log_dir()
- .unwrap_or_else(|_| std::env::temp_dir().join("slopus").join("logs"));
+ .unwrap_or_else(|_| std::env::temp_dir().join("slopus").join("logs"))
+}
+
+pub fn initialize(app: &AppHandle) -> Result<DiagnosticLogInfo, String> {
+ let directory = directory(app);
let writer = LogWriter::new(&directory, MAX_BYTES)?;
let info = writer.info();
LOGGER
diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs
index 30b200d..7bcbd61 100644
--- a/src-tauri/src/lib.rs
+++ b/src-tauri/src/lib.rs
@@ -2682,12 +2682,30 @@ fn generated_summary(job_id: String) -> Option<rendered::RenderedSummary> {
/// buffer. Icon artwork is stored separately from conditioning attachments.
#[tauri::command]
fn save_reference_icon(folder_path: String, job_id: String) -> Result<String, String> {
+ write_reference_icon_frame(&folder_path, &job_id, &reference_icon_pixels(&job_id)?)
+}
+
+fn reference_icon_pixels(job_id: &str) -> Result<Vec<u8>, String> {
let summary = rendered::summary(&job_id).ok_or("The reference icon is no longer available.")?;
if summary.width != reference_icons::RENDER_SIZE || summary.height != reference_icons::RENDER_SIZE || summary.frame_count != 1 {
return Err("A reference icon must be a single 768x768 frame.".into());
}
- let rgba = rendered::frame(&job_id, 0)?.ok_or("The reference icon has no image.")?;
- write_reference_icon_frame(&folder_path, &job_id, &rgba)
+ rendered::frame(job_id, 0)?.ok_or_else(|| "The reference icon has no image.".into())
+}
+
+#[tauri::command]
+fn list_builtin_reference_icons(app: AppHandle) -> Result<Vec<String>, String> {
+ reference_icons::list_builtin(&diagnostics::directory(&app))
+}
+
+#[tauri::command]
+fn read_builtin_reference_icon(app: AppHandle, preset_id: String) -> Result<tauri::ipc::Response, String> {
+ reference_icons::read_builtin(&diagnostics::directory(&app), &preset_id).map(tauri::ipc::Response::new)
+}
+
+#[tauri::command]
+fn save_builtin_reference_icon(app: AppHandle, preset_id: String, job_id: String) -> Result<(), String> {
+ reference_icons::save_builtin(&diagnostics::directory(&app), &preset_id, &reference_icon_pixels(&job_id)?)
}
fn write_reference_icon_frame(folder_path: &str, job_id: &str, rgba: &[u8]) -> Result<String, String> {
@@ -2949,6 +2967,9 @@ pub fn run() {
purge_timeline_thumbnails,
generated_summary,
save_reference_icon,
+ list_builtin_reference_icons,
+ read_builtin_reference_icon,
+ save_builtin_reference_icon,
generated_frame,
generated_audio,
release_generated_frames,
diff --git a/src-tauri/src/reference_icons.rs b/src-tauri/src/reference_icons.rs
index 82959d5..7bf490e 100644
--- a/src-tauri/src/reference_icons.rs
+++ b/src-tauri/src/reference_icons.rs
@@ -1,8 +1,55 @@
+use std::{fs, path::{Path, PathBuf}};
+
pub const RENDER_SIZE: u32 = 768;
pub const ICON_SIZE: u16 = 256;
pub const STEPS: i32 = 20;
const JPEG_QUALITY: u8 = 95;
+fn builtin_directory(log_directory: &Path) -> Result<PathBuf, String> {
+ fs::create_dir_all(log_directory).map_err(|error| error.to_string())?;
+ let root = log_directory.canonicalize().map_err(|error| error.to_string())?;
+ let directory = root.join("reference-icons");
+ fs::create_dir_all(&directory).map_err(|error| error.to_string())?;
+ let directory = directory.canonicalize().map_err(|error| error.to_string())?;
+ if !directory.starts_with(&root) { return Err("Built-in icons must stay inside the log directory.".into()); }
+ Ok(directory)
+}
+
+fn builtin_path(log_directory: &Path, id: &str) -> Result<PathBuf, String> {
+ if id.is_empty() || id.len() > 160 || !id.bytes().all(|byte| byte.is_ascii_alphanumeric() || byte == b'-' || byte == b'_') {
+ return Err("Invalid built-in reference icon ID.".into());
+ }
+ Ok(builtin_directory(log_directory)?.join(format!("{id}.jpg")))
+}
+
+pub fn save_builtin(log_directory: &Path, id: &str, rgba: &[u8]) -> Result<(), String> {
+ let path = builtin_path(log_directory, id)?;
+ crate::export::write_atomically(&path, &encode_jpeg(rgba)?)
+}
+
+pub fn read_builtin(log_directory: &Path, id: &str) -> Result<Vec<u8>, String> {
+ let path = builtin_path(log_directory, id)?;
+ let resolved = path.canonicalize().map_err(|error| error.to_string())?;
+ if !resolved.starts_with(path.parent().unwrap()) { return Err("Built-in icon points outside its directory.".into()); }
+ fs::read(resolved).map_err(|error| error.to_string())
+}
+
+pub fn list_builtin(log_directory: &Path) -> Result<Vec<String>, String> {
+ let directory = builtin_directory(log_directory)?;
+ let mut ids = Vec::new();
+ for entry in fs::read_dir(&directory).map_err(|error| error.to_string())? {
+ let entry = entry.map_err(|error| error.to_string())?;
+ if !entry.file_type().map_err(|error| error.to_string())?.is_file() { continue; }
+ let path = entry.path();
+ if path.extension().and_then(|value| value.to_str()) != Some("jpg") { continue; }
+ if let Some(id) = path.file_stem().and_then(|value| value.to_str()) {
+ if builtin_path(log_directory, id).is_ok() { ids.push(id.to_string()); }
+ }
+ }
+ ids.sort();
+ Ok(ids)
+}
+
fn downscale_rgb(rgba: &[u8]) -> Result<Vec<u8>, String> {
let render_size = RENDER_SIZE as usize;
let icon_size = ICON_SIZE as usize;
@@ -44,6 +91,20 @@ pub fn encode_jpeg(rgba: &[u8]) -> Result<Vec<u8>, String> {
mod tests {
use super::*;
+ #[test]
+ fn builtins_are_shared_in_a_subfolder_of_the_log_directory() {
+ let root = tempfile::tempdir().unwrap();
+ let logs = root.path().join("logs");
+ let rgba = vec![127; RENDER_SIZE as usize * RENDER_SIZE as usize * 4];
+ save_builtin(&logs, "character-alice", &rgba).unwrap();
+ assert!(logs.join("reference-icons/character-alice.jpg").is_file());
+ assert_eq!(list_builtin(&logs).unwrap(), ["character-alice"]);
+ assert!(read_builtin(&logs, "character-alice").unwrap().starts_with(&[0xff, 0xd8]));
+ assert!(save_builtin(&logs, "../escape", &rgba).is_err());
+ assert!(read_builtin(&logs, "../escape").is_err());
+ assert!(save_builtin(&logs, "invalid", &[0; 4]).is_err());
+ }
+
#[test]
fn downscale_averages_each_three_by_three_block() {
let mut rgba = vec![0; RENDER_SIZE as usize * RENDER_SIZE as usize * 4];
diff --git a/src/components/ProjectWorkspace.tsx b/src/components/ProjectWorkspace.tsx
index 03f3745..810adff 100644
--- a/src/components/ProjectWorkspace.tsx
+++ b/src/components/ProjectWorkspace.tsx
@@ -127,7 +127,7 @@ export function ProjectWorkspace({ project, initialView = "timeline", runtime =
<div className={`project-content project-content--${view}`}>
{view === "timeline" && <TimelineView config={config} 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} onOpenTimeline={() => setView("timeline")} />}
- {view === "references" && <ReferencesView config={config} folderPath={project.folderPath} onChange={changeConfig} onRegenerateIcon={(id) => queue.regenerateReferenceIcon(session, id)} 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)} onGenerateBuiltinIcons={() => queue.generateBuiltinReferenceIcons(session)} pendingIconIds={new Set(config.references.filter((reference) => queue.isReferenceIconPending(session, reference.id)).map((reference) => reference.id))} onOpenGenerator={(jobId) => { setSelectedGenerationJobId(jobId); setView("generator"); }} />}
{view === "export" && <ExportView config={config} folderPath={project.folderPath} />}
</div>
<footer className={`project-agent-row${view === "agent" ? " project-agent-row--page" : ""}`}><AgentDock
diff --git a/src/components/ReferenceIconGenerationDialog.tsx b/src/components/ReferenceIconGenerationDialog.tsx
index 04ce386..1bb3123 100644
--- a/src/components/ReferenceIconGenerationDialog.tsx
+++ b/src/components/ReferenceIconGenerationDialog.tsx
@@ -1,8 +1,9 @@
import { ImagePlus } from "lucide-react";
import { useEffect, useRef, useState } from "react";
-export function ReferenceIconGenerationDialog({ count, onAnswer }: {
+export function ReferenceIconGenerationDialog({ count, onAnswer, builtins = false }: {
count: number;
+ builtins?: boolean;
onAnswer: (confirmed: boolean, remember: boolean) => void;
}) {
const [remember, setRemember] = useState(false);
@@ -43,15 +44,21 @@ export function ReferenceIconGenerationDialog({ count, onAnswer }: {
<div className="exit-guard" ref={dialog} role="alertdialog" aria-modal="true" aria-labelledby="reference-icon-generation-title" aria-describedby="reference-icon-generation-description">
<div className="exit-guard__head">
<span className="exit-guard__mark" aria-hidden="true"><ImagePlus size={20} /></span>
- <h2 id="reference-icon-generation-title">Generate reference icons?</h2>
+ <h2 id="reference-icon-generation-title">{builtins ? "Generate built-in reference icons?" : "Generate reference icons?"}</h2>
</div>
<div className="exit-guard__body" id="reference-icon-generation-description">
- <p>{count} {count === 1 ? "reference needs an icon" : "references need icons"}. Generate them now using the video engine? This uses your GPU; waiting videos take priority.</p>
- <p>Cancel skips this batch. You can change automatic generation in Settings → Generator.</p>
+ {builtins ? <>
+ <p>Generate icons for {count} built-in references to make them easier to browse?</p>
+ <p>This can take a long time. Icon generation runs in the background and pauses for normal video generation, so you can keep creating videos.</p>
+ <p>The icons are shared across projects and saved in a reference-icons subfolder beside the log files.</p>
+ </> : <>
+ <p>{count} {count === 1 ? "reference needs an icon" : "references need icons"}. Generate them now using the video engine? This uses your GPU; waiting videos take priority.</p>
+ <p>Cancel skips this batch.</p>
+ </>}
</div>
<label className="reference-icon-option">
<input type="checkbox" checked={remember} onChange={(event) => setRemember(event.target.checked)} />
- <span><b>Don't ask again</b><small>Remember Start as automatic generation, or Cancel as turning it off.</small></span>
+ <span><b>Don't ask again</b><small>{builtins ? "Remember whether to generate missing built-in icons when you open Add a reference." : "Remember Start as automatic generation, or Cancel as turning it off."}</small></span>
</label>
<div className="exit-guard__actions">
<button className="secondary-button" type="button" ref={cancel} onClick={() => onAnswer(false, remember)}>Cancel</button>
diff --git a/src/components/workspace/PresetIcon.tsx b/src/components/workspace/PresetIcon.tsx
new file mode 100644
index 0000000..01ca9ef
--- /dev/null
+++ b/src/components/workspace/PresetIcon.tsx
@@ -0,0 +1,27 @@
+import { useEffect, useRef, useState, useSyncExternalStore } from "react";
+import { builtinIconRevision, builtinIconUrl, hasBuiltinIcon, loadBuiltinIcon, subscribeBuiltinIcons } from "../../lib/builtinReferenceIcons";
+import type { ReferencePreset } from "../../lib/reference-presets";
+
+/** Shared disk artwork is loaded only when its card enters the viewport. */
+export function PresetIcon({ preset, className, alt = "" }: { preset: ReferencePreset; className?: string; alt?: string }) {
+ const revision = useSyncExternalStore(subscribeBuiltinIcons, builtinIconRevision);
+ const marker = useRef<HTMLSpanElement>(null);
+ const [error, setError] = useState(false);
+ const [url, setUrl] = useState(() => builtinIconUrl(preset.id) ?? preset.icon);
+ const available = hasBuiltinIcon(preset.id);
+ useEffect(() => {
+ setError(false);
+ setUrl(builtinIconUrl(preset.id) ?? preset.icon);
+ if (!available || builtinIconUrl(preset.id)) return;
+ let disposed = false;
+ const load = () => { void loadBuiltinIcon(preset.id).then((value) => { if (!disposed) setUrl(value); }).catch(() => { if (!disposed) setError(true); }); };
+ if (typeof IntersectionObserver === "undefined" || !marker.current) { load(); return () => { disposed = true; }; }
+ const observer = new IntersectionObserver((entries) => {
+ if (entries.some((entry) => entry.isIntersecting)) { observer.disconnect(); load(); }
+ }, { rootMargin: "200px" });
+ observer.observe(marker.current);
+ return () => { disposed = true; observer.disconnect(); };
+ }, [preset.id, preset.icon, available, revision]);
+ if (url) return <img className={className} src={url} alt={alt} loading="lazy" />;
+ return <span ref={marker} className={className} role={alt ? "img" : undefined} aria-label={alt || undefined} title={error ? "Could not load icon" : undefined} />;
+}
diff --git a/src/components/workspace/ReferencesView.builtins.test.tsx b/src/components/workspace/ReferencesView.builtins.test.tsx
new file mode 100644
index 0000000..efbbfce
--- /dev/null
+++ b/src/components/workspace/ReferencesView.builtins.test.tsx
@@ -0,0 +1,62 @@
+// @vitest-environment jsdom
+import "@testing-library/jest-dom/vitest";
+import { cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";
+import { invoke } from "@tauri-apps/api/core";
+import { afterEach, beforeEach, expect, it, vi } from "vitest";
+import { createProjectConfig } from "../../lib/project";
+import { builtinIconVisitAction } from "../../lib/referenceIconSettings";
+import { ReferencesView } from "./ReferencesView";
+
+vi.mock("@tauri-apps/api/core", () => ({ invoke: vi.fn() }));
+beforeEach(() => {
+ localStorage.clear(); sessionStorage.clear(); vi.clearAllMocks();
+ Object.assign(window, { __TAURI_INTERNALS__: {} });
+ vi.mocked(invoke).mockResolvedValue([]);
+});
+afterEach(() => { cleanup(); delete (window as unknown as Record<string, unknown>).__TAURI_INTERNALS__; });
+const open = (generate = vi.fn()) => {
+ const config = createProjectConfig({ name: "Icons", prompt: "", aspectRatio: "16:9", resolution: "416p", targetDurationSeconds: 10 });
+ return { generate, ...render(<ReferencesView config={config} folderPath="C:/project" onChange={vi.fn()} onGenerateBuiltinIcons={generate} />) };
+};
+
+it("asks on the first Add reference visit, explains video priority, and remembers Start", async () => {
+ const view = open();
+ expect(screen.queryByRole("alertdialog")).toBeNull();
+ fireEvent.click(screen.getByRole("button", { name: /^Add a reference/ }));
+ const prompt = await screen.findByRole("alertdialog", { name: "Generate built-in reference icons?" });
+ expect(prompt).toHaveTextContent("take a long time");
+ expect(prompt).toHaveTextContent("pauses for normal video generation");
+ expect(view.generate).not.toHaveBeenCalled();
+ fireEvent.click(screen.getByRole("checkbox", { name: /Don't ask again/ }));
+ fireEvent.click(screen.getByRole("button", { name: "Start generation" }));
+ expect(view.generate).toHaveBeenCalledOnce();
+ expect(screen.getByRole("dialog", { name: "Add a reference" })).toBeInTheDocument();
+ view.unmount(); sessionStorage.clear();
+ open(view.generate);
+ fireEvent.click(screen.getByRole("button", { name: /^Add a reference/ }));
+ await waitFor(() => expect(view.generate).toHaveBeenCalledTimes(2));
+ expect(screen.queryByRole("alertdialog")).toBeNull();
+});
+
+it("remembers Cancel across sessions without starting generation", async () => {
+ const view = open();
+ fireEvent.click(screen.getByRole("button", { name: /^Add a reference/ }));
+ await screen.findByRole("alertdialog");
+ fireEvent.click(screen.getByRole("checkbox", { name: /Don't ask again/ }));
+ fireEvent.click(screen.getByRole("button", { name: "Cancel" }));
+ sessionStorage.clear();
+ expect(builtinIconVisitAction()).toBe("skip");
+ expect(view.generate).not.toHaveBeenCalled();
+});
+
+it("does not ask again on another visit in the same session when Cancel is not remembered", async () => {
+ open();
+ fireEvent.click(screen.getByRole("button", { name: /^Add a reference/ }));
+ await screen.findByRole("alertdialog");
+ fireEvent.click(screen.getByRole("button", { name: "Cancel" }));
+ fireEvent.click(screen.getByRole("button", { name: "Close type picker" }));
+ fireEvent.click(screen.getByRole("button", { name: /^Add a reference/ }));
+ await waitFor(() => expect(invoke).toHaveBeenCalledWith("list_builtin_reference_icons"));
+ expect(screen.queryByRole("alertdialog")).toBeNull();
+ expect(localStorage.getItem("slopus.builtin-reference-icons.v1")).toBeNull();
+});
diff --git a/src/components/workspace/ReferencesView.test.tsx b/src/components/workspace/ReferencesView.test.tsx
index 90ee008..d5c928a 100644
--- a/src/components/workspace/ReferencesView.test.tsx
+++ b/src/components/workspace/ReferencesView.test.tsx
@@ -9,15 +9,20 @@ import { createProjectConfig, parseProjectConfig, type ProjectConfig } from "../
import { ReferencesView } from "./ReferencesView";
import { referenceIconPrompt } from "../../lib/referenceIcons";
import { saveDebugOptionsEnabled } from "../../lib/settings";
+import { REFERENCE_PRESETS } from "../../lib/reference-presets";
vi.mock("@tauri-apps/api/core", () => ({ invoke: vi.fn() }));
+const originalPresetIcons = new Map(REFERENCE_PRESETS.map((preset) => [preset.id, preset.icon]));
+
beforeEach(() => localStorage.clear());
afterEach(() => {
+ REFERENCE_PRESETS.forEach((preset) => { preset.icon = originalPresetIcons.get(preset.id); });
cleanup();
delete (window as unknown as Record<string, unknown>).__TAURI_INTERNALS__;
vi.mocked(invoke).mockReset();
+ vi.restoreAllMocks();
});
const project = (): ProjectConfig => {
@@ -225,6 +230,7 @@ describe("Reference type presets", () => {
});
it("shows a bundled MiniMax icon for an illustrated character preset", () => {
+ REFERENCE_PRESETS.find((preset) => preset.name === "Abby Sciuto")!.icon = "/test-preset.jpg";
setup();
fireEvent.click(screen.getByRole("button", { name: /Add a reference/ }));
const dialog = screen.getByRole("dialog", { name: "Add a reference" });
diff --git a/src/components/workspace/ReferencesView.tsx b/src/components/workspace/ReferencesView.tsx
index 62310b5..7a91946 100644
--- a/src/components/workspace/ReferencesView.tsx
+++ b/src/components/workspace/ReferencesView.tsx
@@ -10,6 +10,7 @@ import {
referenceType,
referenceTypeLabel,
referenceSubcategories,
+ hasPresetIcon,
selectedReferencePreset,
type ReferencePreset,
type PresetReferenceType,
@@ -17,6 +18,10 @@ import {
import { LOCATION_SETTING_GROUPS } from "../../lib/expanded-reference-options";
import { isTauri } from "../../lib/persistence";
import { ReferenceImage } from "./ReferenceImage";
+import { PresetIcon } from "./PresetIcon";
+import { ReferenceIconGenerationDialog } from "../ReferenceIconGenerationDialog";
+import { builtinIconRevision, refreshBuiltinIcons, subscribeBuiltinIcons } from "../../lib/builtinReferenceIcons";
+import { builtinIconVisitAction, saveBuiltinIconChoice } from "../../lib/referenceIconSettings";
import { ReferenceVideo } from "./ReferenceVideo";
import { inspectReferenceVideo } from "../../lib/referenceVideo";
import { DebugPromptDialog } from "./DebugPromptDialog";
@@ -47,11 +52,12 @@ function ClipSecondsInput({ label, value, min, max = Number.MAX_SAFE_INTEGER, on
}} /></label>;
}
-export function ReferencesView({ config, folderPath, onChange, onRegenerateIcon, pendingIconIds = new Set<string>(), onOpenGenerator }: {
+export function ReferencesView({ config, folderPath, onChange, onRegenerateIcon, onGenerateBuiltinIcons, pendingIconIds = new Set<string>(), onOpenGenerator }: {
config: ProjectConfig;
folderPath: string;
onChange: (next: ProjectConfig) => void;
onRegenerateIcon?: (referenceId: string) => void;
+ onGenerateBuiltinIcons?: () => void;
pendingIconIds?: ReadonlySet<string>;
onOpenGenerator?: (jobId: string) => void;
}) {
@@ -65,6 +71,13 @@ export function ReferencesView({ config, folderPath, onChange, onRegenerateIcon,
const [importingVideo, setImportingVideo] = useState(false);
const [iconError, setIconError] = useState<string | null>(null);
const [presetDialog, setPresetDialog] = useState(false);
+ const catalogVisit = useRef(0);
+ useEffect(() => () => { catalogVisit.current += 1; }, []);
+ const [builtinConfirmationCount, setBuiltinConfirmationCount] = useState(0);
+ useSyncExternalStore(subscribeBuiltinIcons, builtinIconRevision);
+ useEffect(() => {
+ if (onGenerateBuiltinIcons && isTauri()) void refreshBuiltinIcons().catch((reason) => setIconError(String(reason)));
+ }, [folderPath, Boolean(onGenerateBuiltinIcons)]);
const [pickerType, setPickerType] = useState<PresetReferenceType>("character");
const [pickerSubcategory, setPickerSubcategory] = useState("all");
const [presetSearch, setPresetSearch] = useState("");
@@ -76,7 +89,7 @@ export function ReferencesView({ config, folderPath, onChange, onRegenerateIcon,
const selectedLocation = useMemo(() => selected && referenceType(selected) === "location"
? locationSelectionFromPrompt(selected.description)
: undefined, [selected]);
- const selectedPresetIcon = selectedImages.length === 0 ? selectedPreset?.icon : undefined;
+ const selectedPresetIcon = selectedImages.length === 0 && hasPresetIcon(selectedPreset) ? selectedPreset : undefined;
const jobs = useMemo(() => config.generationJobs.filter((job) => job.referenceIds.includes(selectedId ?? "")), [config.generationJobs, selectedId]);
const update = (id: string, patch: Partial<ProjectReference>) => {
const current = configRef.current;
@@ -93,10 +106,19 @@ export function ReferencesView({ config, folderPath, onChange, onRegenerateIcon,
return id;
};
const openNewReference = () => {
+ const visit = ++catalogVisit.current;
setPickerType("character");
setPickerSubcategory("all");
setPresetSearch("");
setPresetDialog(true);
+ if (onGenerateBuiltinIcons && isTauri()) void refreshBuiltinIcons().then(() => {
+ if (visit !== catalogVisit.current) return;
+ const missing = REFERENCE_PRESETS.filter((preset) => !hasPresetIcon(preset)).length;
+ if (!missing) return;
+ const action = builtinIconVisitAction();
+ if (action === "ask") setBuiltinConfirmationCount(missing);
+ else if (action === "generate") onGenerateBuiltinIcons();
+ }).catch((reason) => setIconError(String(reason)));
};
const addImages = async () => {
if (!selected) return;
@@ -187,14 +209,15 @@ export function ReferencesView({ config, folderPath, onChange, onRegenerateIcon,
{config.references.map((ref) => {
const images = referenceImages(ref);
const cover = images[0];
- const presetIcon = selectedReferencePreset(ref)?.icon;
+ const matchedPreset = selectedReferencePreset(ref);
+ const presetIcon = hasPresetIcon(matchedPreset) ? matchedPreset : undefined;
return <button key={ref.id} className={selectedId === ref.id ? "selected" : ""} onClick={() => setSelectedId(ref.id)}>
{cover
? <span className="reference-art reference-art--photo"><ReferenceImage folderPath={folderPath} relativePath={cover.relativePath} sourcePath={cover.sourcePath} alt={cover.name} /></span>
: ref.iconRelativePath
? <span className="reference-art reference-art--photo"><ReferenceImage folderPath={folderPath} relativePath={ref.iconRelativePath} alt={`${ref.name} icon`} /></span>
: presetIcon
- ? <span className="reference-art reference-art--photo"><img src={presetIcon} alt="" /></span>
+ ? <span className="reference-art reference-art--photo"><PresetIcon preset={presetIcon} /></span>
: <span className="reference-copy-art">{ref.kind === "video" ? <Video size={26} /> : <FileText size={26} />}</span>}
<span className="reference-card__body"><span><b>{ref.name}</b>{(images.length > 0 || ref.kind === "video" || ref.kind === "audio") && <small>{referenceKindLabel(ref)}</small>}</span>{isReferenceDescribed(ref)
? <p>{ref.description}</p>
@@ -238,7 +261,7 @@ export function ReferencesView({ config, folderPath, onChange, onRegenerateIcon,
: selected.iconRelativePath
? <ReferenceImage className="reference-detail-preset-icon" folderPath={folderPath} relativePath={selected.iconRelativePath} alt={`${selected.name} reference icon`} />
: selectedPresetIcon
- ? <img className="reference-detail-preset-icon" src={selectedPresetIcon} alt={`${selected.name} reference icon`} />
+ ? <PresetIcon className="reference-detail-preset-icon" preset={selectedPresetIcon} alt={`${selected.name} reference icon`} />
: <span><Users size={30} /></span>}
{/* A caption only where there is no picture to look at. It used
to print the file's path over the thumbnail, which is neither
@@ -303,10 +326,18 @@ export function ReferencesView({ config, folderPath, onChange, onRegenerateIcon,
segments={[{ kind: "brief", value: referenceIconPrompt(selected) }]}
onClose={() => setShowDebugIconPrompt(false)}
/>}
- {presetDialog && <div className="reference-dialog-backdrop"><div className="reference-preset-dialog" role="dialog" aria-modal="true" aria-labelledby="reference-preset-title">
+ {builtinConfirmationCount > 0 && <ReferenceIconGenerationDialog builtins count={builtinConfirmationCount} onAnswer={(confirmed, remember) => {
+ setBuiltinConfirmationCount(0);
+ if (remember) saveBuiltinIconChoice(confirmed);
+ if (confirmed) {
+ try { onGenerateBuiltinIcons?.(); }
+ catch (reason) { setIconError(String(reason)); }
+ }
+ }} />}
+ {presetDialog && builtinConfirmationCount === 0 && <div className="reference-dialog-backdrop"><div className="reference-preset-dialog" role="dialog" aria-modal="true" aria-labelledby="reference-preset-title">
<header>
<div><h2 id="reference-preset-title">Add a reference</h2><p>Choose a preset or create a new reference, then add a prompt and images.</p></div>
- <button onClick={() => setPresetDialog(false)} aria-label="Close type picker"><X size={18} /></button>
+ <button onClick={() => { catalogVisit.current += 1; setPresetDialog(false); }} aria-label="Close type picker"><X size={18} /></button>
</header>
<div className="reference-preset-dialog__body">
<nav aria-label="Reference types">
@@ -322,8 +353,8 @@ export function ReferencesView({ config, folderPath, onChange, onRegenerateIcon,
{subcategories.map((subcategory) => <button key={subcategory} className={pickerSubcategory === subcategory ? "active" : ""} aria-pressed={pickerSubcategory === subcategory} onClick={() => setPickerSubcategory(subcategory)}>{subcategory}</button>)}
</div>
<div className="reference-preset-grid">
- {visiblePresets.map((preset) => <button key={preset.id} className={preset.icon ? "reference-preset-card--with-icon" : undefined} onClick={() => choosePreset(preset)}>
- {preset.icon && <img className="reference-preset-icon" src={preset.icon} alt="" loading="lazy" />}
+ {visiblePresets.map((preset) => <button key={preset.id} className={hasPresetIcon(preset) ? "reference-preset-card--with-icon" : undefined} onClick={() => choosePreset(preset)}>
+ {hasPresetIcon(preset) && <PresetIcon className="reference-preset-icon" preset={preset} />}
<small>{preset.subcategory}</small><b>{preset.name}</b><span>{preset.prompt}</span>
</button>)}
{visiblePresets.length === 0 && <p>No options match that search.</p>}
diff --git a/src/lib/builtinIconWork.test.ts b/src/lib/builtinIconWork.test.ts
new file mode 100644
index 0000000..00a5f97
--- /dev/null
+++ b/src/lib/builtinIconWork.test.ts
@@ -0,0 +1,74 @@
+// @vitest-environment jsdom
+import { waitFor } from "@testing-library/react";
+import { beforeEach, expect, it, vi } from "vitest";
+import { ReferenceIconWork } from "./referenceIconWork";
+import { ProjectSession } from "./projectSession";
+import { createProjectConfig } from "./project";
+import { cancelSlopfabGeneration, enqueueSlopfabGeneration } from "./runtime";
+import { releaseRendered } from "./generatedVideo";
+import { hasBuiltinIcon, saveBuiltinIcon } from "./builtinReferenceIcons";
+import type { ReferencePreset } from "./reference-presets";
+
+vi.mock("./runtime", () => ({ enqueueSlopfabGeneration: vi.fn(), cancelSlopfabGeneration: vi.fn(), saveReferenceIcon: vi.fn() }));
+vi.mock("./generatedVideo", () => ({ releaseRendered: vi.fn(async () => true) }));
+vi.mock("./builtinReferenceIcons", () => ({ hasBuiltinIcon: vi.fn(() => false), saveBuiltinIcon: vi.fn(async () => undefined) }));
+beforeEach(() => { localStorage.clear(); vi.clearAllMocks(); });
+const presets: ReferencePreset[] = [
+ { id: "test-alice", type: "character", name: "Alice", prompt: "Alice in a blue dress.", subcategory: "Stories" },
+ { id: "test-forest", type: "location", name: "Forest", prompt: "A green forest.", subcategory: "Nature" },
+];
+const setup = () => {
+ const writer = vi.fn(async () => undefined);
+ const session = new ProjectSession({ folderPath: "C:/project", config: createProjectConfig({ name: "Test", prompt: "", aspectRatio: "16:9", resolution: "416p", targetDurationSeconds: 10 }) }, writer);
+ const changed = vi.fn();
+ const work = new ReferenceIconWork(changed, vi.fn());
+ work.enqueueBuiltins(session, presets);
+ return { work, session, writer, changed };
+};
+
+it("interrupts a built-in icon for video work, retries it, and saves shared artwork without modifying the project", async () => {
+ const { work, session, writer } = setup();
+ const before = session.getSnapshot().config;
+ const first = work.runNext(Promise.resolve(), () => false);
+ await waitFor(() => expect(enqueueSlopfabGeneration).toHaveBeenCalledOnce());
+ const id = vi.mocked(enqueueSlopfabGeneration).mock.calls[0][0].jobId;
+ work.yieldToVideo();
+ expect(cancelSlopfabGeneration).toHaveBeenCalledWith(id);
+ work.event({ jobId: id, state: "cancelled", detail: "Cancelled" });
+ await first;
+ expect(work.hasRunnable()).toBe(true);
+ await work.runNext(Promise.resolve(), () => true);
+ expect(enqueueSlopfabGeneration).toHaveBeenCalledOnce();
+ const retry = work.runNext(Promise.resolve(), () => false);
+ await waitFor(() => expect(enqueueSlopfabGeneration).toHaveBeenCalledTimes(2));
+ const retryId = vi.mocked(enqueueSlopfabGeneration).mock.calls[1][0].jobId;
+ expect(retryId).not.toBe(id);
+ work.event({ jobId: retryId, state: "framesReady", detail: "Ready" });
+ await retry;
+ expect(saveBuiltinIcon).toHaveBeenCalledWith("test-alice", retryId);
+ expect(releaseRendered).toHaveBeenCalledWith(retryId);
+ expect(session.getSnapshot().config).toBe(before);
+ expect(writer).not.toHaveBeenCalled();
+ expect(work.confirmationCount()).toBe(0);
+});
+
+it("skips saved built-ins and deduplicates the shared batch across projects", async () => {
+ vi.mocked(hasBuiltinIcon).mockImplementation((id) => id === "test-alice");
+ const { work, session } = setup();
+ work.enqueueBuiltins(session, presets);
+ const pending = work.runNext(Promise.resolve(), () => false);
+ await waitFor(() => expect(enqueueSlopfabGeneration).toHaveBeenCalledOnce());
+ const request = vi.mocked(enqueueSlopfabGeneration).mock.calls[0][0];
+ expect(request.prompt).toContain("A green forest.");
+ work.event({ jobId: request.jobId, state: "framesReady", detail: "Ready" });
+ await pending;
+ expect(work.hasRunnable()).toBe(false);
+ vi.mocked(hasBuiltinIcon).mockReturnValue(false);
+});
+
+it("stops the remaining catalog after an engine failure instead of repeating it for every preset", async () => {
+ const { work } = setup();
+ vi.mocked(enqueueSlopfabGeneration).mockRejectedValueOnce(new Error("Missing model weights"));
+ await work.runNext(Promise.resolve(), () => false);
+ expect(work.hasRunnable()).toBe(false);
+});
diff --git a/src/lib/builtinReferenceIcons.test.ts b/src/lib/builtinReferenceIcons.test.ts
new file mode 100644
index 0000000..9a8962f
--- /dev/null
+++ b/src/lib/builtinReferenceIcons.test.ts
@@ -0,0 +1,39 @@
+// @vitest-environment jsdom
+import { invoke } from "@tauri-apps/api/core";
+import { afterEach, beforeEach, expect, it, vi } from "vitest";
+import { builtinIconUrl, hasBuiltinIcon, loadBuiltinIcon, refreshBuiltinIcons, saveBuiltinIcon } from "./builtinReferenceIcons";
+import { builtinIconVisitAction, saveBuiltinIconChoice } from "./referenceIconSettings";
+
+vi.mock("@tauri-apps/api/core", () => ({ invoke: vi.fn() }));
+beforeEach(() => { localStorage.clear(); sessionStorage.clear(); vi.clearAllMocks(); Object.assign(window, { __TAURI_INTERNALS__: {} }); });
+afterEach(() => { delete (window as unknown as Record<string, unknown>).__TAURI_INTERNALS__; vi.unstubAllGlobals(); });
+
+it("asks once per session and remembers both affirmative and negative choices", () => {
+ expect(builtinIconVisitAction()).toBe("ask");
+ expect(builtinIconVisitAction()).toBe("skip");
+ sessionStorage.clear();
+ expect(builtinIconVisitAction()).toBe("ask");
+ saveBuiltinIconChoice(false);
+ sessionStorage.clear();
+ expect(builtinIconVisitAction()).toBe("skip");
+ saveBuiltinIconChoice(true);
+ sessionStorage.clear();
+ expect(builtinIconVisitAction()).toBe("generate");
+});
+
+it("discovers saved icons and lazily reads each shared JPEG only once", async () => {
+ const create = vi.fn(() => "blob:builtin");
+ const revoke = vi.fn();
+ vi.stubGlobal("URL", { createObjectURL: create, revokeObjectURL: revoke });
+ vi.mocked(invoke).mockImplementation(async (command) => command === "list_builtin_reference_icons" ? ["test-preset"] : new Uint8Array([255, 216]).buffer);
+ await refreshBuiltinIcons();
+ expect(hasBuiltinIcon("test-preset")).toBe(true);
+ expect(builtinIconUrl("test-preset")).toBeUndefined();
+ expect(await Promise.all([loadBuiltinIcon("test-preset"), loadBuiltinIcon("test-preset")])).toEqual(["blob:builtin", "blob:builtin"]);
+ expect(vi.mocked(invoke).mock.calls.filter(([command]) => command === "read_builtin_reference_icon")).toHaveLength(1);
+ expect(builtinIconUrl("test-preset")).toBe("blob:builtin");
+ await saveBuiltinIcon("test-preset", "icon-job");
+ expect(invoke).toHaveBeenCalledWith("save_builtin_reference_icon", { presetId: "test-preset", jobId: "icon-job" });
+ expect(revoke).toHaveBeenCalledWith("blob:builtin");
+ expect(hasBuiltinIcon("test-preset")).toBe(true);
+});
diff --git a/src/lib/builtinReferenceIcons.ts b/src/lib/builtinReferenceIcons.ts
new file mode 100644
index 0000000..d989ef6
--- /dev/null
+++ b/src/lib/builtinReferenceIcons.ts
@@ -0,0 +1,45 @@
+import { invoke } from "@tauri-apps/api/core";
+import { isTauri } from "./persistence";
+
+let ids = new Set<string>();
+const urls = new Map<string, string>();
+const pending = new Map<string, Promise<string>>();
+const listeners = new Set<() => void>();
+let revision = 0;
+let refreshing: Promise<void> | undefined;
+const changed = () => { revision += 1; listeners.forEach((listener) => listener()); };
+export const subscribeBuiltinIcons = (listener: () => void) => { listeners.add(listener); return () => { listeners.delete(listener); }; };
+export const builtinIconRevision = () => revision;
+export const hasBuiltinIcon = (id: string) => ids.has(id);
+export const builtinIconUrl = (id: string) => urls.get(id);
+
+export function refreshBuiltinIcons(): Promise<void> {
+ if (!isTauri()) return Promise.resolve();
+ return refreshing ??= invoke<string[]>("list_builtin_reference_icons").then((found) => {
+ ids = new Set(found);
+ for (const [id, url] of urls) if (!ids.has(id)) { URL.revokeObjectURL(url); urls.delete(id); }
+ changed();
+ }).finally(() => { refreshing = undefined; });
+}
+
+export function loadBuiltinIcon(id: string): Promise<string> {
+ const cached = urls.get(id);
+ if (cached) return Promise.resolve(cached);
+ const existing = pending.get(id);
+ if (existing) return existing;
+ const request = invoke<ArrayBuffer>("read_builtin_reference_icon", { presetId: id }).then((bytes) => {
+ const url = URL.createObjectURL(new Blob([bytes], { type: "image/jpeg" }));
+ urls.set(id, url);
+ return url;
+ }).finally(() => pending.delete(id));
+ pending.set(id, request);
+ return request;
+}
+
+export async function saveBuiltinIcon(presetId: string, jobId: string) {
+ await invoke("save_builtin_reference_icon", { presetId, jobId });
+ const previous = urls.get(presetId);
+ if (previous) { URL.revokeObjectURL(previous); urls.delete(presetId); }
+ ids.add(presetId);
+ changed();
+}
diff --git a/src/lib/reference-presets.test.ts b/src/lib/reference-presets.test.ts
index c021281..b538758 100644
--- a/src/lib/reference-presets.test.ts
+++ b/src/lib/reference-presets.test.ts
@@ -1,7 +1,7 @@
import { describe, expect, it } from "vitest";
import { KNOWN_CHARACTERS } from "./known-characters";
import { LOCATION_GROUPS } from "./expanded-reference-options";
-import { composeLocationPrompt, locationSelectionFromPrompt, REFERENCE_PRESETS, type PresetReferenceType } from "./reference-presets";
+import { composeLocationPrompt, hasPresetIcon, locationSelectionFromPrompt, REFERENCE_PRESETS, type PresetReferenceType } from "./reference-presets";
const TYPES: PresetReferenceType[] = ["animal", "character", "product", "location", "style"];
@@ -39,10 +39,13 @@ describe("reference preset catalog", () => {
expect(Math.max(...REFERENCE_PRESETS.map((preset) => preset.prompt.length))).toBeLessThanOrEqual(100);
});
- it("bundles icons for the existing character, product, location and style presets without treating it as a generation reference", () => {
+ it("keeps the catalog usable before icons are generated and accepts optional bundled artwork", () => {
const visual = REFERENCE_PRESETS.filter((preset) => preset.type === "character" || preset.type === "product" || preset.type === "location" || preset.type === "style");
expect(visual).toHaveLength(810);
- expect(visual.every((preset) => typeof preset.icon === "string" && preset.icon.length > 0)).toBe(true);
+ const preset = { ...visual[0], icon: undefined };
+ expect(hasPresetIcon(preset)).toBe(false);
+ expect(hasPresetIcon({ ...preset, icon: "/bundled-icon.jpg" })).toBe(true);
+ expect(REFERENCE_PRESETS.every((item) => /^[a-zA-Z0-9_-]{1,160}$/.test(item.id))).toBe(true);
});
it("does not multiply products or styles by adjectives", () => {
diff --git a/src/lib/reference-presets.ts b/src/lib/reference-presets.ts
index b31bd84..6505c8b 100644
--- a/src/lib/reference-presets.ts
+++ b/src/lib/reference-presets.ts
@@ -1,6 +1,7 @@
import { KNOWN_CHARACTERS } from "./known-characters";
import { ANIMAL_GROUPS, LOCATION_GROUPS, LOCATION_SETTING_GROUPS, PRODUCT_GROUPS, STYLE_GROUPS, type OptionGroup } from "./expanded-reference-options";
import type { ProjectReference } from "./project";
+import { hasBuiltinIcon } from "./builtinReferenceIcons";
export type ReferenceType = "custom" | "character" | "animal" | "product" | "location" | "style";
export type PresetReferenceType = Exclude<ReferenceType, "custom">;
@@ -12,8 +13,8 @@ export interface ReferencePreset {
name: string;
prompt: string;
searchTerms?: string;
- /** A build-time MiniMax render bundled by Vite. It is presentation only and
- * is never silently attached to a generation as a reference image. */
+ /** Optional bundled artwork, used when no shared generated icon is available.
+ * Presentation only; never attached to generation as a reference image. */
icon?: string;
}
@@ -28,6 +29,8 @@ const iconForPreset = (id: string): string | undefined =>
const withIcon = (preset: ReferencePreset): ReferencePreset => ({ ...preset, icon: iconForPreset(preset.id) });
+export const hasPresetIcon = (preset: ReferencePreset | undefined): boolean => Boolean(preset && (preset.icon || hasBuiltinIcon(preset.id)));
+
export const REFERENCE_TYPES: ReadonlyArray<{ id: PresetReferenceType; label: string }> = [
{ id: "character", label: "Character" },
{ id: "animal", label: "Animal" },
diff --git a/src/lib/referenceIconSettings.ts b/src/lib/referenceIconSettings.ts
index ddaf06a..39fb0ef 100644
--- a/src/lib/referenceIconSettings.ts
+++ b/src/lib/referenceIconSettings.ts
@@ -1,5 +1,32 @@
export type ReferenceIconAutomation = "ask" | "enabled" | "disabled";
+const BUILTIN_KEY = "slopus.builtin-reference-icons.v1";
+const BUILTIN_VISITED_KEY = "slopus.builtin-reference-icons.visited";
+let builtinOverride: ReferenceIconAutomation | undefined;
+let visitedWithoutStorage = false;
+
+export function saveBuiltinIconChoice(confirmed: boolean) {
+ const choice = confirmed ? "enabled" : "disabled";
+ try { localStorage.setItem(BUILTIN_KEY, choice); builtinOverride = undefined; }
+ catch { builtinOverride = choice; }
+}
+
+/** Ask once per app session until the user remembers either answer. */
+export function builtinIconVisitAction(): "ask" | "generate" | "skip" {
+ let choice: string | null | undefined = builtinOverride;
+ try { choice ??= localStorage.getItem(BUILTIN_KEY); } catch { /* Session fallback. */ }
+ if (choice === "enabled") return "generate";
+ if (choice === "disabled") return "skip";
+ try {
+ if (sessionStorage.getItem(BUILTIN_VISITED_KEY)) return "skip";
+ sessionStorage.setItem(BUILTIN_VISITED_KEY, "true");
+ } catch {
+ if (visitedWithoutStorage) return "skip";
+ visitedWithoutStorage = true;
+ }
+ return "ask";
+}
+
const STORAGE_KEY = "slopus.reference-icon-automation.v1";
const CHANGE_EVENT = "slopus:reference-icon-automation-changed";
let sessionOverride: ReferenceIconAutomation | undefined;
diff --git a/src/lib/referenceIconWork.test.ts b/src/lib/referenceIconWork.test.ts
index 804a352..92cf3e5 100644
--- a/src/lib/referenceIconWork.test.ts
+++ b/src/lib/referenceIconWork.test.ts
@@ -40,6 +40,8 @@ const video = (queue: WorkQueue, session: ReturnType<WorkQueue["project"]>) => q
},
}]);
+const originalPresetIcons = new Map(REFERENCE_PRESETS.map((preset) => [preset.id, preset.icon]));
+
beforeEach(() => {
vi.useFakeTimers(); vi.clearAllMocks(); localStorage.clear(); handlers.clear();
saveReferenceIconAutomation("enabled");
@@ -51,7 +53,8 @@ beforeEach(() => {
vi.mocked(saveReferenceIcon).mockImplementation(async (_folder, id) => `references/icons/${id}.jpg`);
vi.mocked(cancelSlopfabGeneration).mockResolvedValue(true);
});
-afterEach(() => { stop?.(); vi.useRealTimers(); delete (window as any).__TAURI_INTERNALS__; });
+afterEach(() => {
+ REFERENCE_PRESETS.forEach((preset) => { preset.icon = originalPresetIcons.get(preset.id); }); stop?.(); vi.restoreAllMocks(); vi.useRealTimers(); delete (window as any).__TAURI_INTERNALS__; });
it("waits for batch confirmation without blocking video generation", async () => {
saveReferenceIconAutomation("ask");
@@ -184,7 +187,8 @@ it("groups icons across projects and gives waiting videos priority between icons
});
it("waits for text and skips references that already have artwork", async () => {
- const preset = REFERENCE_PRESETS.find((preset) => preset.icon)!;
+ const preset = REFERENCE_PRESETS[0];
+ preset.icon = "/test-preset.jpg";
const { session } = setup([
{ ...reference("blank"), description: "" },
{ ...reference("existing"), iconRelativePath: "references/icons/existing.jpg" },
@@ -285,7 +289,8 @@ it("uses a distinct composition for every reference type and preserves the user'
});
it("forces replacement of bundled and generated icons in one batch behind videos", async () => {
- const preset = REFERENCE_PRESETS.find((preset) => preset.icon)!;
+ const preset = REFERENCE_PRESETS[0];
+ preset.icon = "/test-preset.jpg";
const existing = { ...reference("existing", "animal"), iconRelativePath: "references/icons/old.jpg" };
const { queue, session } = setup([existing, { ...reference("preset", preset.type), description: preset.prompt }]);
await vi.advanceTimersByTimeAsync(1001);
diff --git a/src/lib/referenceIconWork.ts b/src/lib/referenceIconWork.ts
index 7a74117..68be7a3 100644
--- a/src/lib/referenceIconWork.ts
+++ b/src/lib/referenceIconWork.ts
@@ -8,11 +8,15 @@ import { cancelSlopfabGeneration, enqueueSlopfabGeneration, saveReferenceIcon }
import { withEngineSettings } from "./settings";
import { loadReferenceIconAutomation, saveReferenceIconAutomation, subscribeReferenceIconAutomation } from "./referenceIconSettings";
import type { WorkItem } from "./workQueue";
+import { hasPresetIcon, REFERENCE_PRESETS, type ReferencePreset } from "./reference-presets";
+import { saveBuiltinIcon } from "./builtinReferenceIcons";
type TaskStatus = "queued" | "generating" | "saving" | "completed" | "failed" | "cancelled" | "skipped";
interface IconTask {
key: string;
session: ProjectSession;
+ preset?: ReferencePreset;
+ yielded?: boolean;
referenceId: string;
name: string;
prompt: string;
@@ -68,7 +72,7 @@ export class ReferenceIconWork {
this.timers.delete(session);
this.watches.get(session)?.();
this.watches.delete(session);
- for (const [key, task] of this.tasks) if (task.session === session) this.tasks.delete(key);
+ for (const [key, task] of this.tasks) if (task.session === session && !task.preset) this.tasks.delete(key);
this.publish();
}
@@ -84,7 +88,7 @@ export class ReferenceIconWork {
scan(session: ProjectSession) {
const references = session.getSnapshot().config.references;
for (const task of this.tasks.values()) {
- if (task.session !== session || task.status !== "queued") continue;
+ if (task.preset || task.session !== session || task.status !== "queued") continue;
const reference = references.find((reference) => reference.id === task.referenceId);
if (!reference || !this.shouldGenerate(reference, task) || (!task.force && loadReferenceIconAutomation() === "disabled")) task.status = "skipped";
}
@@ -104,6 +108,39 @@ export class ReferenceIconWork {
if (added) this.wake();
}
+ enqueueBuiltins(session: ProjectSession, presets: readonly ReferencePreset[] = REFERENCE_PRESETS) {
+ for (const preset of presets) {
+ const key = `builtin::${preset.id}`;
+ const previous = this.tasks.get(key);
+ if (hasPresetIcon(preset) || (previous && ["queued", "generating", "saving"].includes(previous.status))) continue;
+ this.tasks.set(key, { key, session, preset, referenceId: preset.id, name: preset.name, prompt: "", status: "queued", submitted: false, cancelled: false, force: true });
+ }
+ this.publish();
+ this.wake();
+ }
+
+ private referenceFor(task: IconTask): ProjectReference | undefined {
+ if (!task.preset) return task.session.getSnapshot().config.references.find((reference) => reference.id === task.referenceId);
+ const preset = task.preset;
+ return { id: preset.id, kind: "text", name: preset.name, description: preset.prompt,
+ intendedUse: [preset.type], subcategory: preset.subcategory, createdAt: task.session.record.config.createdAt };
+ }
+
+ /** Interrupt a built-in icon so normal video work gets the GPU immediately
+ * after native cancellation. The interrupted icon remains queued to retry. */
+ yieldToVideo() {
+ const task = this.active;
+ if (!task?.preset || task.status !== "generating" || task.cancelled || task.yielded) return;
+ task.yielded = true;
+ if (task.submitted) void this.cancelForVideo(task);
+ this.publish();
+ }
+
+ private async cancelForVideo(task: IconTask) {
+ try { await cancelSlopfabGeneration(task.nativeId!); }
+ catch (reason) { task.yielded = false; task.error = describeDiagnosticError(reason); this.publish(); }
+ }
+
private shouldGenerate(reference: ProjectReference, task: { force?: boolean }) {
return task.force ? Boolean(reference.description.trim()) && referenceImages(reference).length === 0 : needsReferenceIcon(reference);
}
@@ -143,7 +180,7 @@ export class ReferenceIconWork {
this.wake();
}
hasActiveProject(session: ProjectSession) {
- return [...this.tasks.values()].some((task) => task.session === session && ["queued", "generating", "saving"].includes(task.status));
+ return [...this.tasks.values()].some((task) => !task.preset && task.session === session && ["queued", "generating", "saving"].includes(task.status));
}
owns(id: string) { return this.item?.id === id; }
clearFinished(id: string) { if (this.owns(id)) this.item = undefined; }
@@ -166,10 +203,10 @@ export class ReferenceIconWork {
const projects = new Set(tasks.map((task) => task.session));
this.item = {
...this.item,
- projectName: projects.size === 1 ? [...projects][0].getSnapshot().config.name : `${projects.size} projects`,
+ projectName: tasks.every((task) => task.preset) ? "Built-in references" : projects.size === 1 ? [...projects][0].getSnapshot().config.name : `${projects.size} projects`,
status: this.active ? (this.active.status === "saving" ? "encoding" : "generating") : queued ? "queued" : failed.length ? "failed" : tasks.some((task) => task.status === "cancelled") ? "cancelled" : "completed",
progress: tasks.length ? Math.min(1, (settled + (this.active ? this.progress : 0)) / tasks.length) : 1,
- detail: this.active ? `${this.active.cancelled ? "Cancelling" : this.active.status === "saving" ? "Saving" : "Generating"} ${this.active.name} · ${completed}/${tasks.length} icons saved`
+ detail: this.active ? `${this.active.yielded ? "Pausing for video" : this.active.cancelled ? "Cancelling" : this.active.status === "saving" ? "Saving" : "Generating"} ${this.active.name} · ${completed}/${tasks.length} icons saved`
: this.confirmationCount() > 0 ? `${this.confirmationCount()} icons awaiting confirmation`
: queued ? `${completed}/${tasks.length} icons saved · waiting behind videos` : `${completed}/${tasks.length} icons saved`,
error: failed.length ? failed.map((task) => `${task.name}: ${task.error}`).join("\n") : null,
@@ -183,18 +220,21 @@ export class ReferenceIconWork {
const task = [...this.tasks.values()].find((task) => this.canRun(task));
if (!task) return;
this.active = task;
+ task.submitted = false;
+ task.yielded = false;
task.status = "generating";
this.progress = 0;
this.publish();
try {
await ready;
if (task.cancelled) return;
- await task.session.save();
+ if (!task.preset) await task.session.save();
if (task.cancelled) return;
if (videoWaiting()) { task.status = "queued"; return; }
if (!task.force && loadReferenceIconAutomation() === "disabled") { task.status = "skipped"; return; }
if (!task.force && !task.approved && loadReferenceIconAutomation() === "ask") { task.status = "queued"; return; }
- const reference = task.session.getSnapshot().config.references.find((reference) => reference.id === task.referenceId);
+ if (task.preset && hasPresetIcon(task.preset)) { task.status = "skipped"; return; }
+ const reference = this.referenceFor(task);
if (!reference || !this.shouldGenerate(reference, task)) { task.status = "skipped"; return; }
task.prompt = referenceIconPrompt(reference);
task.name = reference.name;
@@ -206,15 +246,23 @@ export class ReferenceIconWork {
}, withEngineSettings(task.session.getSnapshot().config));
task.submitted = true;
if (task.cancelled) await this.cancelNative(task);
+ else if (task.yielded) await this.cancelForVideo(task);
await done;
} catch (reason) {
task.status = task.cancelled ? "cancelled" : "failed";
task.error = describeDiagnosticError(reason);
} finally {
if (task.cancelled) task.status = "cancelled";
+ else if (task.yielded && task.status === "cancelled") { task.status = "queued"; task.error = undefined; }
+ task.yielded = false;
+ // Stop a built-in batch on an engine/storage failure instead of sending
+ // the same failing setup through the entire catalog.
+ if (task.preset && task.status === "failed") {
+ for (const waiting of this.tasks.values()) if (waiting.preset && waiting.status === "queued") waiting.status = "cancelled";
+ }
this.active = undefined;
this.publish();
- this.scan(task.session);
+ if (!task.preset) this.scan(task.session);
}
}
@@ -243,9 +291,14 @@ export class ReferenceIconWork {
private async save(task: IconTask) {
try {
- const reference = task.session.getSnapshot().config.references.find((reference) => reference.id === task.referenceId);
+ const reference = this.referenceFor(task);
if (task.cancelled) { task.status = "cancelled"; return; }
if (!reference || !this.shouldGenerate(reference, task) || referenceIconPrompt(reference) !== task.prompt) { task.status = "skipped"; return; }
+ if (task.preset) {
+ await saveBuiltinIcon(task.preset.id, task.nativeId!);
+ task.status = "completed";
+ return;
+ }
const iconRelativePath = await saveReferenceIcon(task.session.record.folderPath, task.nativeId!);
let attached = false;
task.session.update((current) => ({ ...current, references: current.references.map((reference) => {
@@ -262,8 +315,9 @@ export class ReferenceIconWork {
task.status = "failed";
task.error = describeDiagnosticError(reason);
} finally {
- await releaseRendered(task.nativeId!);
- task.finish?.();
+ try { await releaseRendered(task.nativeId!); }
+ catch (reason) { task.status = "failed"; task.error = describeDiagnosticError(reason); }
+ finally { task.finish?.(); }
}
}
diff --git a/src/lib/referenceIcons.ts b/src/lib/referenceIcons.ts
index c847ebb..d3d7b08 100644
--- a/src/lib/referenceIcons.ts
+++ b/src/lib/referenceIcons.ts
@@ -1,5 +1,5 @@
import { deriveH3Style, referenceImages, type ProjectReference } from "./project";
-import { referenceType, selectedReferencePreset } from "./reference-presets";
+import { hasPresetIcon, referenceType, selectedReferencePreset } from "./reference-presets";
export const REFERENCE_ICON_SIZE = 256;
export const REFERENCE_ICON_RENDER_SIZE = 768;
@@ -36,5 +36,5 @@ export function referenceIconPrompt(reference: ProjectReference): string {
export function needsReferenceIcon(reference: ProjectReference): boolean {
return Boolean(reference.description.trim()) && !reference.iconRelativePath
- && referenceImages(reference).length === 0 && !selectedReferencePreset(reference)?.icon;
+ && referenceImages(reference).length === 0 && !hasPresetIcon(selectedReferencePreset(reference));
}
diff --git a/src/lib/workQueue.ts b/src/lib/workQueue.ts
index dac793e..f6f1c6b 100644
--- a/src/lib/workQueue.ts
+++ b/src/lib/workQueue.ts
@@ -117,6 +117,10 @@ export class WorkQueue {
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);
+ }
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));
@@ -175,6 +179,7 @@ export class WorkQueue {
this.updateScene(this.work.get(id)!, { status: "queued", stage: "queued", progress: 0, error: null, generationSnapshot: submission.snapshot });
}
this.publish();
+ if (this.items.some((item) => item.status === "queued" && item.kind !== "reference-icons")) this.icons.yieldToVideo();
void this.pump();
}
private updateScene(work: PendingWork, patch: Partial<GenerationJob>, dirty = true) {