Skip to content

AI media harness for planning, generating, and editing locally

TypeScript

Discover existing weights during availability checks

Bitti24baafb3dbf0
diff --git a/docs/generator-weights.md b/docs/generator-weights.md
index 940e2c6..2b6fe8a 100644
--- a/docs/generator-weights.md
+++ b/docs/generator-weights.md
@@ -88,11 +88,13 @@ folders in list order, then `weights` beside the executable, then the log folder
`weights` directory for an existing completed download. New downloads go to the
first writable folder in that same order. These locations apply to both model
weights and LoRAs. Changes take effect on the next download or removal without
-restarting. An empty list keeps the default locations.
+restarting. Startup and the regular availability checks also discover completed
+model and LoRA downloads in these folders, even when no local paths were saved.
+An empty list keeps the default locations.
To move previously downloaded weights into a custom folder, move their matching
`.complete.json` records with them so Slopus can recognize and reuse the files
-when you click Download. Only files with valid download records can be removed
+automatically. Only files with valid download records can be removed
through the app. Other app preferences continue to use WebView2 localStorage.
Downloads continue in the background when Settings is closed. Reopening Settings
diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs
index f033e65..9ca2274 100644
--- a/src-tauri/src/lib.rs
+++ b/src-tauri/src/lib.rs
@@ -3017,6 +3017,7 @@ pub fn run() {
weights::download_weight,
weights::cancel_weight_download,
weights::check_weight_files,
+ weights::find_downloaded_weights,
weights::remove_downloaded_weights,
weights::weight_download_hardware,
cuda_support::open_cuda_download,
diff --git a/src-tauri/src/weights.rs b/src-tauri/src/weights.rs
index 6600e11..71c6b15 100644
--- a/src-tauri/src/weights.rs
+++ b/src-tauri/src/weights.rs
@@ -93,13 +93,29 @@ fn cached_file(path: &Path, url: &Url) -> bool {
fn transfer_from_roots(url: &Url, roots: &[PathBuf], cancelled: &AtomicBool, progress: impl FnMut(u64, Option<u64>)) -> Result<String, String> {
if cancelled.load(Ordering::Relaxed) { return Err("Download cancelled.".into()); }
// Search every location before choosing a destination, including read-only caches.
- for root in roots {
- let path = root.join(destination_name(url));
- if cached_file(&path, url) { return Ok(path.to_string_lossy().into_owned()); }
- }
+ if let Some(path) = find_cached_weight(url, roots) { return Ok(path); }
transfer(url, &weights_directory(roots)?, cancelled, progress)
}
+fn find_cached_weight(url: &Url, roots: &[PathBuf]) -> Option<String> {
+ roots.iter().map(|root| root.join(destination_name(url)))
+ .find(|path| cached_file(path, url)).map(|path| path.to_string_lossy().into_owned())
+}
+
+fn find_downloaded(urls: Vec<String>, roots: &[PathBuf]) -> HashMap<String, String> {
+ urls.into_iter().filter_map(|original| {
+ let url = download_url(&original).ok()?;
+ find_cached_weight(&url, roots).map(|path| (original, path))
+ }).collect()
+}
+
+#[tauri::command]
+pub async fn find_downloaded_weights(app: AppHandle, urls: Vec<String>) -> Result<HashMap<String, String>, String> {
+ tauri::async_runtime::spawn_blocking(move || {
+ Ok(find_downloaded(urls, &configured_weight_roots(&app)?))
+ }).await.map_err(|error| error.to_string())?
+}
+
fn transfer(url: &Url, directory: &Path, cancelled: &AtomicBool, mut progress: impl FnMut(u64, Option<u64>)) -> Result<String, String> {
let destination = directory.join(destination_name(url));
if cached_file(&destination, url) { return Ok(destination.to_string_lossy().into_owned()); }
@@ -337,6 +353,33 @@ mod tests {
assert!(transfer_from_roots(&url, &roots, &AtomicBool::new(true), |_, _| {}).is_err());
}
+ #[test]
+ fn discovery_normalizes_urls_and_ignores_missing_incomplete_or_unrecorded_files() {
+ let folder = tempfile::tempdir().unwrap();
+ let custom = folder.path().join("custom");
+ let fallback = folder.path().join("fallback");
+ fs::create_dir_all(&custom).unwrap();
+ fs::create_dir_all(&fallback).unwrap();
+ let blob = "https://huggingface.co/org/repo/blob/main/model.safetensors";
+ let url = download_url(blob).unwrap();
+ let destination = fallback.join(destination_name(&url));
+ fs::write(&destination, b"data").unwrap();
+ let roots = [custom.clone(), fallback];
+ assert!(find_downloaded(vec![blob.into()], &roots).is_empty());
+ let record = CompletedFile { url: url.to_string(), bytes: 4 };
+ fs::write(destination.with_extension("complete.json"), serde_json::to_vec(&record).unwrap()).unwrap();
+ // A truncated copy in the preferred folder must not hide a complete fallback.
+ let incomplete = custom.join(destination_name(&url));
+ fs::write(&incomplete, b"bad").unwrap();
+ fs::write(incomplete.with_extension("complete.json"), serde_json::to_vec(&record).unwrap()).unwrap();
+ let found = find_downloaded(vec![blob.into(), url.to_string(), "https://".into()], &roots);
+ assert_eq!(found.len(), 2);
+ assert_eq!(found[blob], destination.to_string_lossy());
+ assert_eq!(found[url.as_str()], destination.to_string_lossy());
+ fs::remove_file(&destination).unwrap();
+ assert!(find_downloaded(vec![blob.into()], &roots).is_empty());
+ }
+
#[test]
fn publishes_only_complete_files_and_reuses_completed_downloads() {
let folder = tempfile::tempdir().unwrap();
diff --git a/src/components/SettingsView.downloads.test.tsx b/src/components/SettingsView.downloads.test.tsx
index 25b60dc..5f2e453 100644
--- a/src/components/SettingsView.downloads.test.tsx
+++ b/src/components/SettingsView.downloads.test.tsx
@@ -5,7 +5,7 @@ import { invoke } from "@tauri-apps/api/core";
import { listen } from "@tauri-apps/api/event";
import { afterEach, beforeEach, expect, it, vi } from "vitest";
import { SettingsView } from "./SettingsView";
-import { loadGeneratorTemplateSettings } from "../lib/settings";
+import { loadGeneratorTemplateSettings, minimaxSingularityTemplate } from "../lib/settings";
import { getWeightDownloadState } from "../lib/weightDownloads";
import { WorkQueuePanel } from "./WorkQueuePanel";
import { WorkQueue } from "../lib/workQueue";
@@ -20,6 +20,7 @@ beforeEach(() => {
Object.defineProperty(window, "__TAURI_INTERNALS__", { configurable: true, value: {} });
vi.mocked(invoke).mockImplementation(async (command, args) => {
if (command === "weight_download_hardware") return [{ name: "RTX 5090", memoryBytes: 32 * 1024 ** 3 }];
+ if (command === "find_downloaded_weights") return {};
if (command === "check_weight_files") return Object.fromEntries((args as { paths: string[] }).paths.map((path) => [path, files.has(path)]));
if (command === "remove_downloaded_weights") { for (const path of (args as { paths: string[] }).paths) files.delete(path); return; }
if (command === "download_weight") {
@@ -31,6 +32,24 @@ beforeEach(() => {
});
afterEach(() => { cleanup(); delete (window as Window & { __TAURI_INTERNALS__?: unknown }).__TAURI_INTERNALS__; });
+it("discovers Singularity's existing weights on opening Settings and downloads only missing Turbo", async () => {
+ const normal = vi.mocked(invoke).getMockImplementation()!;
+ const cached = Object.fromEntries(Object.values(minimaxSingularityTemplate().paths).filter(Boolean)
+ .map((url) => [url, `D:/Projects/weights/${url.split('/').at(-1)}`]));
+ Object.values(cached).forEach((path) => files.add(path));
+ vi.mocked(invoke).mockImplementation(async (command, args) => command === "find_downloaded_weights"
+ ? Object.fromEntries((args as { urls: string[] }).urls.filter((url) => cached[url]).map((url) => [url, cached[url]])) : normal(command, args));
+ render(<SettingsView onClose={() => undefined} />);
+ await waitFor(() => expect(loadGeneratorTemplateSettings().templates.find(({ id }) => id === "minimax-h3-singularity")?.paths.transformer).toMatch(/^D:\/Projects\/weights\//));
+ expect(screen.getByRole("button", { name: "Download generator Singularity" })).toBeEnabled();
+ expect(invoke).not.toHaveBeenCalledWith("download_weight", expect.anything());
+ fireEvent.click(screen.getByRole("button", { name: "Download generator Singularity" }));
+ expect(await screen.findByRole("radio", { name: "Use Singularity as the default generator" })).toBeEnabled();
+ expect(vi.mocked(invoke).mock.calls.filter(([command]) => command === "download_weight")).toEqual([
+ ["download_weight", { requestId: expect.any(String), url: TURBO_LORA.url }],
+ ]);
+});
+
it("selects an undownloaded LoRA in a generator and downloads it from that generator", async () => {
render(<SettingsView onClose={() => undefined} />);
fireEvent.click(screen.getByRole("button", { name: "Edit Default generator" }));
diff --git a/src/lib/weightDownloads.test.ts b/src/lib/weightDownloads.test.ts
index 995ae4f..6822cb1 100644
--- a/src/lib/weightDownloads.test.ts
+++ b/src/lib/weightDownloads.test.ts
@@ -4,7 +4,7 @@ import { cancelWeightDownload, downloadLora, removeLora, refreshDownloadedLoras,
import { loadLoras, saveLoras, TAOMATE_LORA, TURBO_LORA } from "./loras";
import { beforeEach, expect, it, vi } from "vitest";
import { chooseWeightSource, downloadTemplateWeights, getWeightDownloadState, refreshDownloadedWeights, removeTemplateWeights, updateWeightPath, weightDownloadProgress, type DownloadState } from "./weightDownloads";
-import { createGeneratorTemplate, defaultGeneratorTemplate, isDownloadUrl, loadGeneratorTemplateSettings, minimaxOriginalTemplate, saveGeneratorTemplateSettings, templateNeedsDownload, type WeightSource } from "./settings";
+import { createGeneratorTemplate, defaultGeneratorTemplate, isDownloadUrl, loadGeneratorTemplateSettings, minimaxOriginalTemplate, minimaxSingularityTemplate, saveGeneratorTemplateSettings, templateNeedsDownload, type WeightSource } from "./settings";
vi.mock("@tauri-apps/api/core", () => ({ invoke: vi.fn() }));
vi.mock("@tauri-apps/api/event", () => ({ listen: vi.fn(async () => () => undefined) }));
@@ -62,6 +62,7 @@ beforeEach(() => {
localStorage.clear(); files.clear(); vi.clearAllMocks();
vi.mocked(invoke).mockImplementation(async (command, args) => {
if (command === "weight_download_hardware") return [{ name: "NVIDIA GeForce RTX 5090", memoryBytes: 32 * 1024 ** 3 }];
+ if (command === "find_downloaded_weights") return {};
if (command === "check_weight_files") return Object.fromEntries((args as { paths: string[] }).paths.map((path) => [path, files.has(path)]));
if (command === "remove_downloaded_weights") { for (const path of (args as { paths: string[] }).paths) files.delete(path); return; }
if (command === "download_weight") {
@@ -71,6 +72,45 @@ beforeEach(() => {
});
});
+it("discovers model weights and LoRAs without saved paths or starting downloads", async () => {
+ const normal = vi.mocked(invoke).getMockImplementation()!;
+ const template = minimaxSingularityTemplate();
+ const cached = Object.fromEntries([...Object.values(template.paths).filter(Boolean), TURBO_LORA.url!]
+ .map((url) => [url, `D:/Projects/weights/${url.split('/').at(-1)}`]));
+ vi.mocked(invoke).mockImplementation(async (command, args) => command === "find_downloaded_weights"
+ ? Object.fromEntries((args as { urls: string[] }).urls.filter((url) => cached[url]).map((url) => [url, cached[url]])) : normal(command, args));
+ await refreshDownloadedWeights();
+ const discovered = loadGeneratorTemplateSettings().templates.find(({ id }) => id === template.id)!;
+ expect(discovered.paths.transformer).toBe(cached[template.paths.transformer]);
+ expect(discovered.sources!.transformer![0].downloadedPath).toBe(cached[template.paths.transformer]);
+ expect(loadLoras().find(({ id }) => id === TURBO_LORA.id)?.path).toBe(cached[TURBO_LORA.url!]);
+ expect(templateNeedsDownload(discovered)).toBe(false);
+ expect(invoke).not.toHaveBeenCalledWith("download_weight", expect.anything());
+});
+
+it("finds moved weights and preserves manual paths and GPU variant selection", async () => {
+ const template = minimaxOriginalTemplate();
+ const movedUrl = template.paths.videoVae;
+ template.paths.videoVae = "C:/old/video.safetensors";
+ template.sources!.videoVae![0].downloadedPath = template.paths.videoVae;
+ template.paths.audioVae = "D:/personal/audio.safetensors";
+ saveGeneratorTemplateSettings({ templates: [template], defaultTemplateId: "", catalogVersion: 6 });
+ const normal = vi.mocked(invoke).getMockImplementation()!;
+ vi.mocked(invoke).mockImplementation(async (command, args) => {
+ if (command === "find_downloaded_weights") return {
+ [movedUrl]: "D:/Projects/weights/video.safetensors",
+ [template.sources!.audioVae![0].url]: "D:/Projects/weights/audio.safetensors",
+ [template.sources!.transformer![1].url]: "D:/Projects/weights/small.safetensors",
+ };
+ return normal(command, args);
+ });
+ await refreshDownloadedWeights();
+ expect(saved().paths.videoVae).toBe("D:/Projects/weights/video.safetensors");
+ expect(saved().paths.audioVae).toBe("D:/personal/audio.safetensors");
+ expect(saved().paths.transformer).toBe(template.paths.transformer);
+ expect(saved().sources!.transformer![1].downloadedPath).toBe("D:/Projects/weights/small.safetensors");
+});
+
it("chooses a matching GPU variant and the largest VRAM tier that fits", () => {
const generic = source("https://example.com/small"), large = source("https://example.com/large", "", 24);
const specific = source("https://example.com/5090", "RTX 5090", 32);
diff --git a/src/lib/weightDownloads.ts b/src/lib/weightDownloads.ts
index ed19a7c..c7a4cab 100644
--- a/src/lib/weightDownloads.ts
+++ b/src/lib/weightDownloads.ts
@@ -67,8 +67,13 @@ export function refreshDownloadedWeights(): Promise<void> {
await refreshDownloadedLoras();
const before = loadGeneratorTemplateSettings();
const paths = [...new Set(before.templates.flatMap((template) => Object.values(template.sources ?? {}).flatMap((sources) => sources.flatMap((source) => source.downloadedPath ? [source.downloadedPath] : []))))];
- if (!paths.length) return;
- const exists = await invoke<Record<string, boolean>>("check_weight_files", { paths });
+ const urls = [...new Set(before.templates.flatMap((template) => Object.values(template.sources ?? {}).flatMap((sources) => sources.map((source) => source.url))))];
+ if (!paths.length && !urls.length) return;
+ const [exists, found, devices] = await Promise.all([
+ paths.length ? invoke<Record<string, boolean>>("check_weight_files", { paths }) : Promise.resolve({} as Record<string, boolean>),
+ invoke<Record<string, string>>("find_downloaded_weights", { urls }),
+ invoke<WeightGpu[]>("weight_download_hardware"),
+ ]);
const current = loadGeneratorTemplateSettings();
let changed = false;
const templates = current.templates.map((template) => {
@@ -77,12 +82,19 @@ export function refreshDownloadedWeights(): Promise<void> {
const sources = template.sources?.[id];
if (!sources) continue;
restored.sources[id] = sources.map((source) => {
- if (!source.downloadedPath || exists?.[source.downloadedPath] !== false) return source;
+ const missing = source.downloadedPath && exists?.[source.downloadedPath] === false;
+ const discovered = found?.[source.url];
+ const downloadedPath = missing || !source.downloadedPath ? discovered : source.downloadedPath;
+ if (downloadedPath === source.downloadedPath) return source;
changed = true;
- if (restored.paths[id] === source.downloadedPath) restored.paths[id] = source.url;
+ if (restored.paths[id] === source.downloadedPath) restored.paths[id] = downloadedPath || source.url;
const { downloadedPath: _removed, ...remaining } = source;
- return remaining;
+ return downloadedPath ? { ...remaining, downloadedPath } : remaining;
});
+ if (!restored.paths[id].trim() || isDownloadUrl(restored.paths[id])) {
+ const selected = chooseWeightSource(restored.sources[id]!, devices ?? []);
+ if (selected?.downloadedPath) { restored.paths[id] = selected.downloadedPath; changed = true; }
+ }
}
return restored;
});
@@ -189,18 +201,31 @@ export function retryWeightDownload(): Promise<void> {
return state?.loraId ? downloadLora(state.loraId) : state ? downloadTemplateWeights(state.templateId) : Promise.resolve();
}
-export async function refreshDownloadedLoras(): Promise<void> {
- if (!isTauri()) return;
- const paths = loadLoras().filter((entry) => entry.url && entry.path).map(({ path }) => path);
- if (!paths.length) return;
- const exists = await invoke<Record<string, boolean>>("check_weight_files", { paths });
- let changed = false;
- const loras = loadLoras().map((entry) => {
- if (!entry.url || !paths.includes(entry.path) || exists?.[entry.path] !== false) return entry;
- changed = true;
- return { ...entry, path: "" };
- });
- if (changed) saveLoras(loras);
+let loraRefreshPending: Promise<void> | null = null;
+export function refreshDownloadedLoras(): Promise<void> {
+ if (!isTauri()) return Promise.resolve();
+ if (loraRefreshPending) return loraRefreshPending;
+ loraRefreshPending = (async () => {
+ const before = loadLoras().filter((entry) => entry.url);
+ const paths = before.filter((entry) => entry.path && !isDownloadUrl(entry.path)).map(({ path }) => path);
+ const urls = [...new Set(before.map((entry) => entry.url!))];
+ if (!urls.length) return;
+ const [exists, found] = await Promise.all([
+ paths.length ? invoke<Record<string, boolean>>("check_weight_files", { paths }) : Promise.resolve({} as Record<string, boolean>),
+ invoke<Record<string, string>>("find_downloaded_weights", { urls }),
+ ]);
+ let changed = false;
+ const loras = loadLoras().map((entry) => {
+ if (!entry.url || !before.some((old) => old.id === entry.id && old.url === entry.url && old.path === entry.path)) return entry;
+ if (entry.path.trim() && !isDownloadUrl(entry.path) && exists?.[entry.path] !== false) return entry;
+ const path = found?.[entry.url] || "";
+ if (entry.path === path) return entry;
+ changed = true;
+ return { ...entry, path };
+ });
+ if (changed) saveLoras(loras);
+ })().finally(() => { loraRefreshPending = null; });
+ return loraRefreshPending;
}
export async function removeLora(loraId: string): Promise<void> {