Skip to content

AI media harness for planning, generating, and editing locally

TypeScript

Split shared runtime into slopus-core and worker crates

Bittica69978d9419
diff --git a/docs/rust-architecture.md b/docs/rust-architecture.md
index d66c47d..932eb3f 100644
--- a/docs/rust-architecture.md
+++ b/docs/rust-architecture.md
@@ -1,25 +1,41 @@
# Rust architecture
-The four former files over 1,000 lines (`lib.rs`, `slopfab.rs`, `agent.rs`, and
-`agent_commands.rs`) are split by responsibility. Existing IPC names, project
-JSON, JSONL commands, event payloads, and reference import policies are preserved.
+`src-tauri` is a Cargo workspace of three crates:
-| Area | Responsibilities |
-| --- | --- |
-| `app.rs`, `window.rs`, `commands/` | Tauri setup, window lifecycle, dialogs, IPC and event delivery |
-| `project/` | Document types, compatibility migrations, ordered validation, persistence and lifecycle |
-| `project/commands/` | Restricted command vocabulary, JSONL parsing, transactional batches and domain handlers |
-| `media/`, `storage/` | Format catalog, session access, confined project artifacts and atomic replacement |
-| `agent/` | Turn protocol, correction budget, semantic policy, cancellation and provider discovery |
-| `agent/providers/` | Claude/Codex CLI adapters and compatible HTTP endpoints behind one turn interface |
-| `generation/models/` | Model defaults, capabilities, authoring instructions and shot-setting validation |
-| `slopfab/` | Native runtime configuration, planning, serial generation queue, icons and references |
-| `slopfab/ffi/` | Private ABI bindings, library ownership, handles, callbacks and output access |
+| Crate | Path | Contents |
+| --- | --- | --- |
+| `slopus-core` | `src-tauri/core` | Generation runtime, weights, render store, diagnostics and the LAN worker protocol and server. No UI framework. |
+| `slopus` | `src-tauri` | The desktop app: Tauri setup, IPC commands, projects, agents and the worker client. Depends on `slopus-core`. |
+| `slopus-worker` | `src-tauri/worker` | The headless LAN worker binary. Depends only on `slopus-core`. |
+
+The app re-exports core modules at its crate root (`crate::slopfab`,
+`crate::rendered`, ...), so app code uses the same paths as before. Code that the
+worker needs must live in `slopus-core` and must not depend on Tauri. Existing IPC
+names, project JSON, JSONL commands, event payloads, and reference import
+policies are preserved.
+
+| Area | Crate | Responsibilities |
+| --- | --- | --- |
+| `app.rs`, `window.rs`, `commands/` | app | Tauri setup, window lifecycle, dialogs, IPC and event delivery |
+| `project/` | app | Document types, compatibility migrations, ordered validation, persistence and lifecycle |
+| `project/commands/` | app | Restricted command vocabulary, JSONL parsing, transactional batches and domain handlers |
+| `media/` | app | Format catalog, session access and confined project artifacts |
+| `agent/` | app | Turn protocol, correction budget, semantic policy, cancellation and provider discovery |
+| `agent/providers/` | app | Claude/Codex CLI adapters and compatible HTTP endpoints behind one turn interface |
+| `generation/models/` | app | Model defaults, authoring instructions and shot-setting validation |
+| `weights.rs`, `worker/` | app | Weight download commands, and the client that sends generations to a LAN worker |
+| `models.rs`, `settings.rs` | core | Model generation limits; provider settings passed to the engine |
+| `slopfab/` | core | Native runtime configuration, planning, serial generation queue, icons and references |
+| `slopfab/ffi/` | core | Private ABI bindings, library ownership, handles, callbacks and output access |
+| `rendered.rs`, `weights.rs`, `storage/` | core | Finished-render store, weight downloader, atomic replacement |
+| `diagnostics.rs` | core | Log files for both the app and the worker |
+| `worker/` | core | Worker protocol, HTTP server, upload/download store and mDNS discovery |
## Adding a generation model
1. Define a `ModelDefinition` under `generation/models/` and register its persisted
- ID in `find`. Specify scene defaults, generation limits, authoring instructions,
+ ID in `find`. Generation limits (`ModelCapabilities`) live in
+ `slopus-core`'s `models.rs`. Specify scene defaults, generation limits, authoring instructions,
vocabulary and shot-setting validation. Unknown IDs remain readable data and
do not acquire H3 authoring instructions; unregistered documents retain the
existing v1 validation limits.
@@ -62,7 +78,7 @@ removed from model context; credentials remain in the selected transport session
## Verification
-Run `cargo test --manifest-path src-tauri/Cargo.toml`, `npm test`, and `npm run build`.
+Run `cargo test --workspace --manifest-path src-tauri/Cargo.toml`, `npm test`, and `npm run build`.
Native tests exercise the bundled DLL's ABI, planning, reference snapshots and
handle ownership without loading model weights. They do not verify a full GPU
render; that requires installed weights and suitable hardware.
diff --git a/package.cmd b/package.cmd
index 0eb6a22..737d992 100644
--- a/package.cmd
+++ b/package.cmd
@@ -78,8 +78,8 @@ if not exist "%RELEASE_EXE%" (
goto :fail
)
-rem The LAN worker is headless and embeds no frontend, so plain cargo is right.
-"%CARGO_EXE%" build --release --manifest-path "%ROOT_DIR%\src-tauri\Cargo.toml" --bin slopus-worker || goto :fail
+rem The LAN worker is its own crate on slopus-core, without Tauri, so plain cargo is right.
+"%CARGO_EXE%" build --release --manifest-path "%ROOT_DIR%\src-tauri\Cargo.toml" -p slopus-worker || goto :fail
rem Guard against silently shipping the dev-mode binary again: a production
rem build embeds the hashed frontend assets, a dev-mode one does not.
diff --git a/release.cmd b/release.cmd
index 206be97..2471127 100644
--- a/release.cmd
+++ b/release.cmd
@@ -102,8 +102,8 @@ if not exist "%RELEASE_EXE%" (
goto :fail
)
-rem The LAN worker is headless and embeds no frontend, so plain cargo is right.
-"%CARGO_EXE%" build --release --manifest-path "%ROOT_DIR%\src-tauri\Cargo.toml" --bin slopus-worker || goto :fail
+rem The LAN worker is its own crate on slopus-core, without Tauri, so plain cargo is right.
+"%CARGO_EXE%" build --release --manifest-path "%ROOT_DIR%\src-tauri\Cargo.toml" -p slopus-worker || goto :fail
rem Guard against silently shipping the dev-mode binary again: a production
rem build embeds the hashed frontend assets, a dev-mode one does not.
diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock
index 38ecc1a..ee15376 100644
--- a/src-tauri/Cargo.lock
+++ b/src-tauri/Cargo.lock
@@ -3425,6 +3425,7 @@ dependencies = [
"reqwest 0.12.28",
"serde",
"serde_json",
+ "slopus-core",
"tauri",
"tauri-build",
"tauri-plugin-dialog",
@@ -3439,6 +3440,32 @@ dependencies = [
"windows-sys 0.59.0",
]
+[[package]]
+name = "slopus-core"
+version = "0.2.0"
+dependencies = [
+ "getrandom 0.3.4",
+ "image",
+ "jpeg-encoder",
+ "libloading 0.8.9",
+ "mdns-sd",
+ "reqwest 0.12.28",
+ "serde",
+ "serde_json",
+ "tempfile",
+ "tiny_http",
+ "windows",
+ "windows-sys 0.59.0",
+]
+
+[[package]]
+name = "slopus-worker"
+version = "0.2.0"
+dependencies = [
+ "slopus-core",
+ "tauri-winres",
+]
+
[[package]]
name = "smallvec"
version = "1.15.2"
diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml
index d4c99ad..be3883f 100644
--- a/src-tauri/Cargo.toml
+++ b/src-tauri/Cargo.toml
@@ -1,6 +1,14 @@
+# One version for the app and the worker. scripts/updater-manifest.mjs checks
+# it against package.json and tauri.conf.json.
+[workspace.package]
+version = "0.2.0"
+
+[workspace]
+members = ["core", "worker"]
+
[package]
name = "slopus"
-version = "0.2.0"
+version.workspace = true
description = "An AI media harness for planning, generating, and editing media locally."
authors = ["Slopus"]
edition = "2021"
@@ -31,8 +39,9 @@ jpeg-encoder = "0.6"
image = { version = "=0.25.8", default-features = false, features = ["jpeg", "png"] }
tokio = { version = "1", features = ["rt", "time", "net", "macros"] }
tauri-plugin-window-state = "2"
-# LAN generation workers: the worker serves HTTP and both sides use mDNS.
-tiny_http = "0.12"
+# The generation runtime and LAN worker protocol, shared with slopus-worker.
+slopus-core = { path = "core" }
+# The worker client browses for workers with mDNS.
mdns-sd = "0.21"
[target.'cfg(windows)'.dependencies]
@@ -57,3 +66,4 @@ windows-sys = { version = "0.59", features = ["Win32_Foundation", "Win32_Storage
[dev-dependencies]
tempfile = "3"
+tiny_http = "0.12"
diff --git a/src-tauri/core/Cargo.toml b/src-tauri/core/Cargo.toml
new file mode 100644
index 0000000..8c0c67e
--- /dev/null
+++ b/src-tauri/core/Cargo.toml
@@ -0,0 +1,30 @@
+[package]
+name = "slopus-core"
+version.workspace = true
+description = "Slopus generation runtime shared by the desktop app and the LAN worker. No UI framework."
+authors = ["Slopus"]
+edition = "2021"
+rust-version = "1.87"
+
+[lib]
+name = "slopus_core"
+
+[dependencies]
+serde = { version = "1", features = ["derive"] }
+serde_json = "1"
+libloading = "0.8"
+getrandom = "0.3"
+reqwest = { version = "0.12", default-features = false, features = ["blocking", "json", "rustls-tls"] }
+jpeg-encoder = "0.6"
+# Keep image decoding compatible with the package's Rust 1.87 minimum.
+image = { version = "=0.25.8", default-features = false, features = ["jpeg", "png"] }
+tiny_http = "0.12"
+mdns-sd = "0.21"
+
+[target.'cfg(windows)'.dependencies]
+# DXGI reads GPU memory for weight selection without a CUDA toolkit.
+windows = { version = "0.61", features = ["Win32_Graphics_Dxgi", "Win32_Foundation"] }
+windows-sys = { version = "0.59", features = ["Win32_Foundation", "Win32_Storage_FileSystem"] }
+
+[dev-dependencies]
+tempfile = "3"
diff --git a/src-tauri/core/build.rs b/src-tauri/core/build.rs
new file mode 100644
index 0000000..89a9cae
--- /dev/null
+++ b/src-tauri/core/build.rs
@@ -0,0 +1,16 @@
+fn main() {
+ // Stage the SlopFab runtime beside this build's executables (the worker,
+ // and test binaries, which look one folder up from deps/). The desktop
+ // app stages it through Tauri resources as well; the copy is identical.
+ let source = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../lib/slopfab/slopfab.dll");
+ println!("cargo:rerun-if-changed={}", source.display());
+ let out = std::path::PathBuf::from(std::env::var("OUT_DIR").unwrap());
+ // OUT_DIR is target/<profile>/build/<crate>-<hash>/out.
+ if let Some(profile) = out.ancestors().nth(3) {
+ let destination = profile.join("slopfab.dll");
+ let stale = std::fs::metadata(&destination).ok().map(|meta| meta.len()) != std::fs::metadata(&source).ok().map(|meta| meta.len());
+ if source.is_file() && stale {
+ let _ = std::fs::copy(&source, destination);
+ }
+ }
+}
diff --git a/src-tauri/src/conditioning.rs b/src-tauri/core/src/conditioning.rs
similarity index 99%
rename from src-tauri/src/conditioning.rs
rename to src-tauri/core/src/conditioning.rs
index 120fc2f..fe99b45 100644
--- a/src-tauri/src/conditioning.rs
+++ b/src-tauri/core/src/conditioning.rs
@@ -89,7 +89,7 @@ pub fn prepare(source: &Path) -> Result<PathBuf, String> {
}
#[cfg(test)]
-pub(crate) fn fixture(path: &Path) {
+pub fn fixture(path: &Path) {
let header = serde_json::to_vec(&json!({
"prompt_embeds": { "dtype": "BF16", "shape": [1, TOKENS, 5120], "data_offsets": [0, VALUES * 2] },
"text_token_tags": { "dtype": "I64", "shape": [TOKENS], "data_offsets": [VALUES * 2, VALUES * 2 + TOKENS * 8] },
diff --git a/src-tauri/core/src/diagnostics.rs b/src-tauri/core/src/diagnostics.rs
new file mode 100644
index 0000000..26669aa
--- /dev/null
+++ b/src-tauri/core/src/diagnostics.rs
@@ -0,0 +1,485 @@
+//! Persistent diagnostics shared by the native runtime and the webview.
+//!
+//! One human-readable record is written per line so a damaged final write
+//! never makes earlier records unreadable. The active file rotates at startup
+//! and at 5 MiB, and only one previous file is retained. Prompt bodies and credentials are
+//! deliberately excluded by callers and redacted again here at the final
+//! write boundary.
+
+use serde::{Deserialize, Serialize};
+use serde_json::{Map, Value};
+use std::{
+ fs::{self, File, OpenOptions},
+ io::{BufWriter, Write},
+ path::{Path, PathBuf},
+ process::Command,
+ sync::{Mutex, Once, OnceLock},
+ time::{SystemTime, UNIX_EPOCH},
+};
+
+const ACTIVE_FILE: &str = "slopus.log";
+const PREVIOUS_FILE: &str = "slopus-prev.log";
+const MAX_BYTES: u64 = 5 * 1024 * 1024;
+const MAX_TEXT_CHARS: usize = 8 * 1024;
+
+static LOGGER: OnceLock<Mutex<LogWriter>> = OnceLock::new();
+static PANIC_HOOK: Once = Once::new();
+
+#[derive(Debug, Deserialize)]
+#[serde(rename_all = "camelCase")]
+pub struct FrontendLogEvent {
+ pub level: String,
+ pub target: String,
+ pub event: String,
+ pub message: String,
+ #[serde(default)]
+ pub context: Option<Value>,
+}
+
+#[derive(Debug, Clone, Serialize)]
+#[serde(rename_all = "camelCase")]
+pub struct DiagnosticLogInfo {
+ pub path: String,
+ pub previous_path: Option<String>,
+ pub session_id: String,
+ pub max_file_bytes: u64,
+}
+
+struct LogWriter {
+ active_path: PathBuf,
+ previous_path: PathBuf,
+ session_id: String,
+ max_bytes: u64,
+ bytes: u64,
+ file: Option<BufWriter<File>>,
+}
+
+impl LogWriter {
+ fn new(directory: &Path, max_bytes: u64) -> Result<Self, String> {
+ fs::create_dir_all(directory).map_err(|error| {
+ format!(
+ "Could not create diagnostic log directory '{}': {error}",
+ directory.display()
+ )
+ })?;
+ let active_path = directory.join(ACTIVE_FILE);
+ let previous_path = directory.join(PREVIOUS_FILE);
+ let mut writer = Self {
+ active_path,
+ previous_path,
+ session_id: format!("{}-{}", std::process::id(), timestamp_ms()),
+ max_bytes,
+ bytes: 0,
+ file: None,
+ };
+ writer.rotate()?;
+ Ok(writer)
+ }
+
+ fn write(
+ &mut self,
+ level: &str,
+ target: &str,
+ event: &str,
+ message: &str,
+ context: Value,
+ ) -> Result<(), String> {
+ let context = format_context(redact(context, 0));
+ let mut fields = format!(
+ "sessionId={}",
+ serde_json::to_string(&self.session_id).unwrap_or_else(|_| "\"[unprintable]\"".into())
+ );
+ if !context.is_empty() {
+ fields.push_str(", ");
+ fields.push_str(&context);
+ }
+ let line = format!(
+ "{} [{}] {} {} - {}{}\n",
+ format_timestamp(timestamp_ms()),
+ normalized_level(level).to_ascii_uppercase(),
+ single_line(&bounded_text(target)),
+ single_line(&bounded_text(event)),
+ single_line(&bounded_text(message)),
+ format!(" | {fields}"),
+ )
+ .into_bytes();
+ if self.bytes > 0 && self.bytes.saturating_add(line.len() as u64) > self.max_bytes {
+ self.rotate()?;
+ }
+ let file = self
+ .file
+ .as_mut()
+ .ok_or_else(|| "Diagnostic log is not open.".to_string())?;
+ file.write_all(&line)
+ .map_err(|error| format!("Could not write diagnostic log: {error}"))?;
+ file.flush()
+ .map_err(|error| format!("Could not flush diagnostic log: {error}"))?;
+ self.bytes = self.bytes.saturating_add(line.len() as u64);
+ Ok(())
+ }
+
+ fn rotate(&mut self) -> Result<(), String> {
+ if let Some(mut file) = self.file.take() {
+ let _ = file.flush();
+ }
+ if self.active_path.exists() {
+ if self.previous_path.exists() {
+ fs::remove_file(&self.previous_path).map_err(|error| {
+ format!("Could not replace previous diagnostic log: {error}")
+ })?;
+ }
+ fs::rename(&self.active_path, &self.previous_path)
+ .map_err(|error| format!("Could not rotate diagnostic log: {error}"))?;
+ }
+ self.file = Some(open_append(&self.active_path)?);
+ self.bytes = 0;
+ Ok(())
+ }
+
+ fn info(&self) -> DiagnosticLogInfo {
+ DiagnosticLogInfo {
+ path: self.active_path.to_string_lossy().into_owned(),
+ previous_path: self
+ .previous_path
+ .is_file()
+ .then(|| self.previous_path.to_string_lossy().into_owned()),
+ session_id: self.session_id.clone(),
+ max_file_bytes: self.max_bytes,
+ }
+ }
+}
+
+fn open_append(path: &Path) -> Result<BufWriter<File>, String> {
+ OpenOptions::new()
+ .create(true)
+ .append(true)
+ .open(path)
+ .map(BufWriter::new)
+ .map_err(|error| {
+ format!(
+ "Could not open diagnostic log '{}': {error}",
+ path.display()
+ )
+ })
+}
+
+fn timestamp_ms() -> u64 {
+ SystemTime::now()
+ .duration_since(UNIX_EPOCH)
+ .unwrap_or_default()
+ .as_millis()
+ .min(u64::MAX as u128) as u64
+}
+
+fn format_timestamp(timestamp_ms: u64) -> String {
+ let total_seconds = timestamp_ms / 1_000;
+ let milliseconds = timestamp_ms % 1_000;
+ let days = (total_seconds / 86_400) as i64;
+ let seconds_in_day = total_seconds % 86_400;
+ let hour = seconds_in_day / 3_600;
+ let minute = seconds_in_day % 3_600 / 60;
+ let second = seconds_in_day % 60;
+
+ // Gregorian civil date conversion from days since the Unix epoch.
+ let shifted_days = days + 719_468;
+ let era = if shifted_days >= 0 {
+ shifted_days
+ } else {
+ shifted_days - 146_096
+ } / 146_097;
+ let day_of_era = shifted_days - era * 146_097;
+ let year_of_era =
+ (day_of_era - day_of_era / 1_460 + day_of_era / 36_524 - day_of_era / 146_096) / 365;
+ let mut year = year_of_era + era * 400;
+ let day_of_year = day_of_era - (365 * year_of_era + year_of_era / 4 - year_of_era / 100);
+ let month_prime = (5 * day_of_year + 2) / 153;
+ let day = day_of_year - (153 * month_prime + 2) / 5 + 1;
+ let month = month_prime + if month_prime < 10 { 3 } else { -9 };
+ if month <= 2 {
+ year += 1;
+ }
+
+ format!("{year:04}-{month:02}-{day:02}T{hour:02}:{minute:02}:{second:02}.{milliseconds:03}Z")
+}
+
+fn single_line(value: &str) -> String {
+ value
+ .replace('\r', "\\r")
+ .replace('\n', "\\n")
+ .replace('\t', "\\t")
+}
+
+fn format_context(value: Value) -> String {
+ let format_value =
+ |value: &Value| serde_json::to_string(value).unwrap_or_else(|_| "\"[unprintable]\"".into());
+ match value {
+ Value::Null => String::new(),
+ Value::Object(values) if values.is_empty() => String::new(),
+ Value::Object(values) => values
+ .iter()
+ .map(|(key, value)| format!("{}={}", single_line(key), format_value(value)))
+ .collect::<Vec<_>>()
+ .join(", "),
+ other => format!("context={}", format_value(&other)),
+ }
+}
+
+fn bounded_text(value: &str) -> String {
+ value.chars().take(MAX_TEXT_CHARS).collect()
+}
+
+fn normalized_level(value: &str) -> &'static str {
+ match value.to_ascii_lowercase().as_str() {
+ "trace" => "trace",
+ "debug" => "debug",
+ "warn" | "warning" => "warn",
+ "error" => "error",
+ _ => "info",
+ }
+}
+
+fn sensitive_key(key: &str) -> bool {
+ matches!(
+ key.to_ascii_lowercase().replace(['_', '-'], "").as_str(),
+ "apikey"
+ | "authorization"
+ | "password"
+ | "secret"
+ | "token"
+ | "accesstoken"
+ | "refreshtoken"
+ | "prompt"
+ | "content"
+ )
+}
+
+fn redact(value: Value, depth: usize) -> Value {
+ if depth >= 6 {
+ return Value::String("[truncated]".into());
+ }
+ match value {
+ Value::Object(values) => Value::Object(
+ values
+ .into_iter()
+ .take(64)
+ .map(|(key, value)| {
+ let value = if sensitive_key(&key) {
+ Value::String("[redacted]".into())
+ } else {
+ redact(value, depth + 1)
+ };
+ (key, value)
+ })
+ .collect::<Map<_, _>>(),
+ ),
+ Value::Array(values) => Value::Array(
+ values
+ .into_iter()
+ .take(64)
+ .map(|value| redact(value, depth + 1))
+ .collect(),
+ ),
+ Value::String(value) => Value::String(bounded_text(&value)),
+ other => other,
+ }
+}
+
+/// Starts logging into `directory`. The desktop app passes its data folder
+/// and the headless worker its own.
+pub fn initialize_in(directory: &Path) -> Result<DiagnosticLogInfo, String> {
+ let writer = LogWriter::new(directory, MAX_BYTES)?;
+ let info = writer.info();
+ LOGGER
+ .set(Mutex::new(writer))
+ .map_err(|_| "Diagnostic logging was initialized twice.".to_string())?;
+ install_panic_hook();
+ Ok(info)
+}
+
+fn install_panic_hook() {
+ PANIC_HOOK.call_once(|| {
+ let previous = std::panic::take_hook();
+ std::panic::set_hook(Box::new(move |panic| {
+ let location = panic
+ .location()
+ .map(|value| {
+ serde_json::json!({
+ "file": value.file(), "line": value.line(), "column": value.column(),
+ })
+ })
+ .unwrap_or(Value::Null);
+ let message = panic
+ .payload()
+ .downcast_ref::<&str>()
+ .copied()
+ .or_else(|| panic.payload().downcast_ref::<String>().map(String::as_str))
+ .unwrap_or("Rust panicked without a string message.");
+ error(
+ "native",
+ "panic",
+ message,
+ serde_json::json!({ "location": location }),
+ );
+ previous(panic);
+ }));
+ });
+}
+
+fn write(
+ level: &str,
+ target: &str,
+ event: &str,
+ message: &str,
+ context: Value,
+) -> Result<(), String> {
+ let logger = LOGGER
+ .get()
+ .ok_or_else(|| "Diagnostic logging is not initialized.".to_string())?;
+ logger
+ .lock()
+ .map_err(|_| "Diagnostic log lock failed.".to_string())?
+ .write(level, target, event, message, context)
+}
+
+pub fn info(target: &str, event: &str, message: &str, context: Value) {
+ let _ = write("info", target, event, message, context);
+}
+
+pub fn debug(target: &str, event: &str, message: &str, context: Value) {
+ let _ = write("debug", target, event, message, context);
+}
+
+pub fn warn(target: &str, event: &str, message: &str, context: Value) {
+ let _ = write("warn", target, event, message, context);
+}
+
+pub fn error(target: &str, event: &str, message: &str, context: Value) {
+ let _ = write("error", target, event, message, context);
+}
+
+pub fn write_frontend(entry: FrontendLogEvent) -> Result<(), String> {
+ write(
+ &entry.level,
+ &format!("frontend.{}", entry.target),
+ &entry.event,
+ &entry.message,
+ entry.context.unwrap_or_else(|| Value::Object(Map::new())),
+ )
+}
+
+pub fn log_info() -> Result<DiagnosticLogInfo, String> {
+ let logger = LOGGER
+ .get()
+ .ok_or_else(|| "Diagnostic logging is not initialized.".to_string())?;
+ Ok(logger
+ .lock()
+ .map_err(|_| "Diagnostic log lock failed.".to_string())?
+ .info())
+}
+
+pub fn reveal_log() -> Result<DiagnosticLogInfo, String> {
+ let info = log_info()?;
+ let path = PathBuf::from(&info.path);
+ #[cfg(target_os = "windows")]
+ Command::new("explorer.exe")
+ .arg("/select,")
+ .arg(&path)
+ .spawn()
+ .map_err(|error| format!("Could not open the diagnostic log folder: {error}"))?;
+ #[cfg(target_os = "macos")]
+ Command::new("open")
+ .arg("-R")
+ .arg(&path)
+ .spawn()
+ .map_err(|error| format!("Could not reveal the diagnostic log: {error}"))?;
+ #[cfg(all(unix, not(target_os = "macos")))]
+ Command::new("xdg-open")
+ .arg(path.parent().unwrap_or(Path::new(".")))
+ .spawn()
+ .map_err(|error| format!("Could not open the diagnostic log folder: {error}"))?;
+ Ok(info)
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn text_lines_are_readable_and_redact_credentials_and_prompt_content() {
+ let folder = tempfile::tempdir().unwrap();
+ let mut writer = LogWriter::new(folder.path(), 1024 * 1024).unwrap();
+ writer.write("error", "generation", "failed", "encoder failed", serde_json::json!({
+ "jobId": "job-1", "apiKey": "secret-key", "prompt": "private scene", "nested": { "password": "secret" }
+ })).unwrap();
+ drop(writer);
+ let text = fs::read_to_string(folder.path().join(ACTIVE_FILE)).unwrap();
+ assert!(text.contains("[ERROR] generation failed - encoder failed"));
+ assert!(text.contains("sessionId="));
+ assert!(text.contains("jobId=\"job-1\""));
+ assert!(text.contains("apiKey=\"[redacted]\""));
+ assert!(text.contains("prompt=\"[redacted]\""));
+ assert!(text.contains("nested={\"password\":\"[redacted]\"}"));
+ assert!(!text.contains("private scene"));
+ assert!(!text.contains("secret-key"));
+ }
+
+ #[test]
+ fn timestamp_is_utc_and_human_readable() {
+ assert_eq!(format_timestamp(0), "1970-01-01T00:00:00.000Z");
+ assert_eq!(
+ format_timestamp(1_788_449_322_122),
+ "2026-09-03T15:28:42.122Z"
+ );
+ }
+
+ #[test]
+ fn startup_rotates_the_old_log_and_replaces_the_previous_session() {
+ let folder = tempfile::tempdir().unwrap();
+ let active = folder.path().join(ACTIVE_FILE);
+ let previous = folder.path().join("slopus-prev.log");
+ fs::write(&active, "last session").unwrap();
+ fs::write(&previous, "older session").unwrap();
+
+ let mut writer = LogWriter::new(folder.path(), MAX_BYTES).unwrap();
+ assert_eq!(fs::read_to_string(&previous).unwrap(), "last session");
+ assert_eq!(fs::read_to_string(&active).unwrap(), "");
+ assert_eq!(writer.bytes, 0);
+ assert_eq!(
+ writer.info().previous_path,
+ Some(previous.to_string_lossy().into_owned())
+ );
+ writer
+ .write("info", "test", "startup", "new session", Value::Null)
+ .unwrap();
+ drop(writer);
+
+ let current = fs::read_to_string(&active).unwrap();
+ assert!(current.contains("new session"));
+ assert!(!current.contains("last session"));
+ let writer = LogWriter::new(folder.path(), MAX_BYTES).unwrap();
+ assert_eq!(fs::read_to_string(&previous).unwrap(), current);
+ assert_eq!(fs::read_to_string(&active).unwrap(), "");
+ drop(writer);
+ }
+
+ #[test]
+ fn rotation_keeps_one_previous_file() {
+ let folder = tempfile::tempdir().unwrap();
+ let mut writer = LogWriter::new(folder.path(), 300).unwrap();
+ for index in 0..12 {
+ writer
+ .write(
+ "info",
+ "test",
+ "line",
+ &format!("record {index} {}", "x".repeat(80)),
+ Value::Null,
+ )
+ .unwrap();
+ }
+ drop(writer);
+ assert!(folder.path().join(ACTIVE_FILE).is_file());
+ assert!(folder.path().join(PREVIOUS_FILE).is_file());
+ assert_eq!(fs::read_dir(folder.path()).unwrap().count(), 2);
+ }
+}
diff --git a/src-tauri/core/src/lib.rs b/src-tauri/core/src/lib.rs
new file mode 100644
index 0000000..d0ea370
--- /dev/null
+++ b/src-tauri/core/src/lib.rs
@@ -0,0 +1,16 @@
+//! The Slopus generation runtime, shared by the desktop app and the headless
+//! LAN worker. Nothing here depends on a UI framework: the app adds Tauri
+//! commands on top, and `slopus-worker` serves the same runtime over HTTP.
+pub mod conditioning;
+pub mod diagnostics;
+pub mod models;
+pub mod names;
+pub mod reference_icons;
+pub mod rendered;
+pub mod settings;
+pub mod slopfab;
+pub mod storage;
+pub mod weights;
+pub mod worker;
+
+pub use slopfab::{default_dll_path, generate_reference_icon_batch, ReferenceIconBatchConfig, ReferenceIconSpec};
diff --git a/src-tauri/core/src/models.rs b/src-tauri/core/src/models.rs
new file mode 100644
index 0000000..d34a8c6
--- /dev/null
+++ b/src-tauri/core/src/models.rs
@@ -0,0 +1,23 @@
+//! What each generation model can do. The desktop app adds authoring rules
+//! (shot vocabulary, agent instructions) on top of these limits.
+
+#[derive(Debug, Clone, Copy)]
+pub struct ModelCapabilities {
+ pub max_scene_seconds: f64,
+ pub min_steps: i32,
+ pub max_image_references: usize,
+ pub max_video_references: usize,
+ pub max_references: usize,
+ pub continuation_overlap: i32,
+ pub frame_stride: i32,
+}
+
+pub const H3_CAPABILITIES: ModelCapabilities = ModelCapabilities {
+ max_scene_seconds: 15.0,
+ min_steps: 2,
+ max_image_references: 9,
+ max_video_references: 3,
+ max_references: 12,
+ continuation_overlap: 22,
+ frame_stride: 17,
+};
diff --git a/src-tauri/core/src/names.rs b/src-tauri/core/src/names.rs
new file mode 100644
index 0000000..f8ac037
--- /dev/null
+++ b/src-tauri/core/src/names.rs
@@ -0,0 +1,25 @@
+//! Caller-named things that become file names.
+
+/// A scene id, as a file name.
+///
+/// The webview names the job, so this is caller-controlled text about to become
+/// a path — the exact shape of a traversal. Rather than sanitising it, the id
+/// is REFUSED unless it is already only letters, digits, dash and underscore,
+/// which is what every id Slopus generates looks like. There is nothing to
+/// strip and nothing to get wrong: no dots (so no `..` and no second
+/// extension), no separators, no length worth arguing about.
+pub fn generated_file_stem(job_id: &str) -> Result<String, String> {
+ let trimmed = job_id.trim();
+ if trimmed.is_empty() || trimmed.len() > 64 {
+ return Err("A scene id must be between 1 and 64 characters.".into());
+ }
+ if !trimmed
+ .chars()
+ .all(|character| character.is_ascii_alphanumeric() || matches!(character, '-' | '_'))
+ {
+ return Err(format!(
+ "'{job_id}' is not a scene id: only letters, digits, dashes and underscores name a rendered file."
+ ));
+ }
+ Ok(trimmed.to_string())
+}
diff --git a/src-tauri/src/reference_icons.rs b/src-tauri/core/src/reference_icons.rs
similarity index 98%
rename from src-tauri/src/reference_icons.rs
rename to src-tauri/core/src/reference_icons.rs
index 4f39bce..7f32b93 100644
--- a/src-tauri/src/reference_icons.rs
+++ b/src-tauri/core/src/reference_icons.rs
@@ -24,7 +24,7 @@ fn builtin_path(data_directory: &Path, id: &str) -> Result<PathBuf, String> {
pub fn save_builtin(data_directory: &Path, id: &str, rgba: &[u8]) -> Result<(), String> {
let path = builtin_path(data_directory, id)?;
- crate::export::write_atomically(&path, &encode_jpeg(rgba)?)
+ crate::storage::atomic::write_atomically(&path, &encode_jpeg(rgba)?)
}
pub fn read_builtin(data_directory: &Path, id: &str) -> Result<Vec<u8>, String> {
diff --git a/src-tauri/src/rendered.rs b/src-tauri/core/src/rendered.rs
similarity index 100%
rename from src-tauri/src/rendered.rs
rename to src-tauri/core/src/rendered.rs
diff --git a/src-tauri/core/src/settings.rs b/src-tauri/core/src/settings.rs
new file mode 100644
index 0000000..a04258c
--- /dev/null
+++ b/src-tauri/core/src/settings.rs
@@ -0,0 +1,20 @@
+//! Provider settings as projects store them and generation commands receive
+//! them. The `slopfab` entry carries the model paths and engine options.
+use serde::{Deserialize, Serialize};
+use std::collections::BTreeMap;
+
+#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
+pub struct ProviderSetting {
+ pub enabled: bool,
+ pub model: Option<String>,
+ #[serde(default)]
+ pub options: BTreeMap<String, ProviderOption>,
+}
+
+#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
+#[serde(untagged)]
+pub enum ProviderOption {
+ String(String),
+ Number(f64),
+ Boolean(bool),
+}
diff --git a/src-tauri/src/slopfab/config.rs b/src-tauri/core/src/slopfab/config.rs
similarity index 99%
rename from src-tauri/src/slopfab/config.rs
rename to src-tauri/core/src/slopfab/config.rs
index f00d25b..30a4eed 100644
--- a/src-tauri/src/slopfab/config.rs
+++ b/src-tauri/core/src/slopfab/config.rs
@@ -1,5 +1,5 @@
use super::{default_dll_path, platform::ComputePlatform, types::GenerationRequest};
-use crate::project::{ProviderOption, ProviderSetting};
+use crate::settings::{ProviderOption, ProviderSetting};
use serde::Deserialize;
use std::{collections::BTreeMap, path::PathBuf};
diff --git a/src-tauri/src/slopfab/events.rs b/src-tauri/core/src/slopfab/events.rs
similarity index 100%
rename from src-tauri/src/slopfab/events.rs
rename to src-tauri/core/src/slopfab/events.rs
diff --git a/src-tauri/src/slopfab/ffi/handles.rs b/src-tauri/core/src/slopfab/ffi/handles.rs
similarity index 100%
rename from src-tauri/src/slopfab/ffi/handles.rs
rename to src-tauri/core/src/slopfab/ffi/handles.rs
diff --git a/src-tauri/src/slopfab/ffi/mod.rs b/src-tauri/core/src/slopfab/ffi/mod.rs
similarity index 100%
rename from src-tauri/src/slopfab/ffi/mod.rs
rename to src-tauri/core/src/slopfab/ffi/mod.rs
diff --git a/src-tauri/src/slopfab/ffi/raw/device.rs b/src-tauri/core/src/slopfab/ffi/raw/device.rs
similarity index 100%
rename from src-tauri/src/slopfab/ffi/raw/device.rs
rename to src-tauri/core/src/slopfab/ffi/raw/device.rs
diff --git a/src-tauri/src/slopfab/ffi/raw/mod.rs b/src-tauri/core/src/slopfab/ffi/raw/mod.rs
similarity index 100%
rename from src-tauri/src/slopfab/ffi/raw/mod.rs
rename to src-tauri/core/src/slopfab/ffi/raw/mod.rs
diff --git a/src-tauri/src/slopfab/ffi/raw/reference_video.rs b/src-tauri/core/src/slopfab/ffi/raw/reference_video.rs
similarity index 100%
rename from src-tauri/src/slopfab/ffi/raw/reference_video.rs
rename to src-tauri/core/src/slopfab/ffi/raw/reference_video.rs
diff --git a/src-tauri/src/slopfab/ffi/tests.rs b/src-tauri/core/src/slopfab/ffi/tests.rs
similarity index 100%
rename from src-tauri/src/slopfab/ffi/tests.rs
rename to src-tauri/core/src/slopfab/ffi/tests.rs
diff --git a/src-tauri/src/slopfab/generation.rs b/src-tauri/core/src/slopfab/generation.rs
similarity index 100%
rename from src-tauri/src/slopfab/generation.rs
rename to src-tauri/core/src/slopfab/generation.rs
diff --git a/src-tauri/src/slopfab/generation_tests.rs b/src-tauri/core/src/slopfab/generation_tests.rs
similarity index 100%
rename from src-tauri/src/slopfab/generation_tests.rs
rename to src-tauri/core/src/slopfab/generation_tests.rs
diff --git a/src-tauri/src/slopfab/h3.rs b/src-tauri/core/src/slopfab/h3.rs
similarity index 97%
rename from src-tauri/src/slopfab/h3.rs
rename to src-tauri/core/src/slopfab/h3.rs
index 83dbfcd..d5c63dd 100644
--- a/src-tauri/src/slopfab/h3.rs
+++ b/src-tauri/core/src/slopfab/h3.rs
@@ -77,9 +77,7 @@ pub(super) fn configure_request(
api.set_continuation_file(
handle,
path,
- crate::generation::models::h3::MODEL
- .capabilities
- .continuation_overlap,
+ crate::models::H3_CAPABILITIES.continuation_overlap,
)?;
}
if purpose == RequestPurpose::Generate {
@@ -153,6 +151,6 @@ pub(super) fn configure_request(
// https://huggingface.co/Viggle/Viggle-Animate/raw/main/assets/fixed_prompt.txt
pub(super) const ANIMATE_PROMPT: &str =
- include_str!("../../assets/viggle-animate-fixed-prompt.txt");
+ include_str!("../../../assets/viggle-animate-fixed-prompt.txt");
// SlopFab rounds up to 17*k + 5 frames but rejects Animate plans over 360.
pub(super) const ANIMATE_MAX_ALIGNED_FRAMES: i32 = (360 - 5) / 17 * 17 + 5;
diff --git a/src-tauri/src/slopfab/icons.rs b/src-tauri/core/src/slopfab/icons.rs
similarity index 100%
rename from src-tauri/src/slopfab/icons.rs
rename to src-tauri/core/src/slopfab/icons.rs
diff --git a/src-tauri/src/slopfab/mod.rs b/src-tauri/core/src/slopfab/mod.rs
similarity index 93%
rename from src-tauri/src/slopfab/mod.rs
rename to src-tauri/core/src/slopfab/mod.rs
index d9a0a5a..5820ee1 100644
--- a/src-tauri/src/slopfab/mod.rs
+++ b/src-tauri/core/src/slopfab/mod.rs
@@ -1,9 +1,9 @@
//! Native runtime adapter. Raw ABI and resource ownership are confined to ffi.
use std::path::{Path, PathBuf};
// Preparation atomically replaces adapters; model readers must be closed first.
-pub(crate) static MODEL_ACCESS: std::sync::RwLock<()> = std::sync::RwLock::new(());
+pub static MODEL_ACCESS: std::sync::RwLock<()> = std::sync::RwLock::new(());
-pub(crate) fn prepare_lora_grid(
+pub fn prepare_lora_grid(
path: &Path,
width: i32,
allow_download: bool,
diff --git a/src-tauri/src/slopfab/planning.rs b/src-tauri/core/src/slopfab/planning.rs
similarity index 97%
rename from src-tauri/src/slopfab/planning.rs
rename to src-tauri/core/src/slopfab/planning.rs
index 9ed16a5..7866e0b 100644
--- a/src-tauri/src/slopfab/planning.rs
+++ b/src-tauri/core/src/slopfab/planning.rs
@@ -11,7 +11,7 @@ use super::{
references::ReferenceVideos,
types::*,
};
-use crate::project::ProviderSetting;
+use crate::settings::ProviderSetting;
use getrandom::fill as fill_random;
use std::collections::BTreeMap;
@@ -66,7 +66,7 @@ pub(super) fn validate_generation_controls(request: &GenerationRequest) -> Resul
return Err("Image edits require one source image and nonempty boxes inside its dimensions.".into());
}
}
- let limits = crate::generation::models::h3::MODEL.capabilities;
+ let limits = crate::models::H3_CAPABILITIES;
if request.reference_paths.len() > limits.max_image_references
|| request.reference_video_ids.len() > limits.max_video_references
|| request.reference_count() > limits.max_references
@@ -114,7 +114,7 @@ pub(super) fn scene_frame_window(
if request.frames <= 0 {
return Err("Continuation needs a positive frame count.".into());
}
- let limits = crate::generation::models::h3::MODEL.capabilities;
+ let limits = crate::models::H3_CAPABILITIES;
let frames = request
.frames
.checked_add(limits.frame_stride - 1)
diff --git a/src-tauri/src/slopfab/platform.rs b/src-tauri/core/src/slopfab/platform.rs
similarity index 100%
rename from src-tauri/src/slopfab/platform.rs
rename to src-tauri/core/src/slopfab/platform.rs
diff --git a/src-tauri/src/slopfab/references.rs b/src-tauri/core/src/slopfab/references.rs
similarity index 98%
rename from src-tauri/src/slopfab/references.rs
rename to src-tauri/core/src/slopfab/references.rs
index 6b17bea..50dd054 100644
--- a/src-tauri/src/slopfab/references.rs
+++ b/src-tauri/core/src/slopfab/references.rs
@@ -1,5 +1,5 @@
use super::{config::Configuration, ffi};
-use crate::project::ProviderSetting;
+use crate::settings::ProviderSetting;
use std::{
collections::{BTreeMap, HashMap},
sync::{atomic::Ordering, Arc, Mutex},
diff --git a/src-tauri/src/slopfab/runtime.rs b/src-tauri/core/src/slopfab/runtime.rs
similarity index 99%
rename from src-tauri/src/slopfab/runtime.rs
rename to src-tauri/core/src/slopfab/runtime.rs
index 458ddcb..fe3d88c 100644
--- a/src-tauri/src/slopfab/runtime.rs
+++ b/src-tauri/core/src/slopfab/runtime.rs
@@ -7,7 +7,7 @@ use super::{
references::ReferenceVideos,
types::*,
};
-use crate::{diagnostics, project::ProviderSetting, rendered};
+use crate::{diagnostics, settings::ProviderSetting, rendered};
use std::{
collections::{BTreeMap, HashMap},
sync::{
diff --git a/src-tauri/src/slopfab/status.rs b/src-tauri/core/src/slopfab/status.rs
similarity index 99%
rename from src-tauri/src/slopfab/status.rs
rename to src-tauri/core/src/slopfab/status.rs
index f2155a3..5f937de 100644
--- a/src-tauri/src/slopfab/status.rs
+++ b/src-tauri/core/src/slopfab/status.rs
@@ -1,5 +1,5 @@
use super::{config::Configuration, ffi, platform::*, types::*};
-use crate::project::ProviderSetting;
+use crate::settings::ProviderSetting;
use std::{collections::BTreeMap, path::Path};
fn model_statuses(configuration: &Configuration, available: impl Fn(&Path) -> bool) -> Vec<ModelStatus> {
diff --git a/src-tauri/src/slopfab/tests.rs b/src-tauri/core/src/slopfab/tests.rs
similarity index 99%
rename from src-tauri/src/slopfab/tests.rs
rename to src-tauri/core/src/slopfab/tests.rs
index 32bf424..a6f11ff 100644
--- a/src-tauri/src/slopfab/tests.rs
+++ b/src-tauri/core/src/slopfab/tests.rs
@@ -2,7 +2,7 @@ use super::{
config::*, events::stage_name, ffi::RequestHandle, h3::*, planning::*, platform::*,
references::ReferenceVideos, *,
};
-use crate::project::{ProviderOption, ProviderSetting};
+use crate::settings::{ProviderOption, ProviderSetting};
use std::collections::BTreeMap;
#[test]
diff --git a/src-tauri/src/slopfab/types.rs b/src-tauri/core/src/slopfab/types.rs
similarity index 100%
rename from src-tauri/src/slopfab/types.rs
rename to src-tauri/core/src/slopfab/types.rs
diff --git a/src-tauri/src/storage/atomic.rs b/src-tauri/core/src/storage/atomic.rs
similarity index 90%
rename from src-tauri/src/storage/atomic.rs
rename to src-tauri/core/src/storage/atomic.rs
index 6d44946..808d156 100644
--- a/src-tauri/src/storage/atomic.rs
+++ b/src-tauri/core/src/storage/atomic.rs
@@ -3,13 +3,13 @@ use std::{
path::{Path, PathBuf},
};
#[cfg(not(windows))]
-pub(crate) fn atomic_replace(temporary: &Path, destination: &Path) -> io::Result<()> {
+pub fn atomic_replace(temporary: &Path, destination: &Path) -> io::Result<()> {
fs::rename(temporary, destination)?;
fs::File::open(destination.parent().expect("project file has a parent"))?.sync_all()
}
#[cfg(windows)]
-pub(crate) fn atomic_replace(temporary: &Path, destination: &Path) -> io::Result<()> {
+pub fn atomic_replace(temporary: &Path, destination: &Path) -> io::Result<()> {
use std::os::windows::ffi::OsStrExt;
use windows_sys::Win32::{
Foundation::GetLastError,
@@ -59,7 +59,7 @@ impl Drop for TemporaryFile {
}
}
-pub(crate) fn write_with_replacer<F>(
+pub fn write_with_replacer<F>(
destination: &Path,
bytes: &[u8],
replacer: F,
@@ -99,7 +99,7 @@ where
replacer(&temporary.0, destination)
}
-pub(crate) fn write_atomically(destination: &Path, bytes: &[u8]) -> Result<(), String> {
+pub fn write_atomically(destination: &Path, bytes: &[u8]) -> Result<(), String> {
write_with_replacer(destination, bytes, atomic_replace)
.map_err(|error| format!("Could not save {}: {error}", destination.display()))
}
diff --git a/src-tauri/core/src/storage/mod.rs b/src-tauri/core/src/storage/mod.rs
new file mode 100644
index 0000000..652223f
--- /dev/null
+++ b/src-tauri/core/src/storage/mod.rs
@@ -0,0 +1 @@
+pub mod atomic;
diff --git a/src-tauri/core/src/weights.rs b/src-tauri/core/src/weights.rs
new file mode 100644
index 0000000..ea58bca
--- /dev/null
+++ b/src-tauri/core/src/weights.rs
@@ -0,0 +1,374 @@
+//! Weight downloads, shared by the desktop app and the LAN worker: one
+//! downloader, one folder layout and one completion record format.
+use reqwest::{blocking::Client, Url};
+use serde::{Deserialize, Serialize};
+use std::{
+ collections::HashMap,
+ fs::{self, OpenOptions},
+ io::{Read, Write},
+ path::{Path, PathBuf},
+ sync::atomic::{AtomicBool, Ordering},
+ time::{Duration, Instant},
+};
+
+#[derive(Serialize, Deserialize)]
+pub struct CompletedFile { pub url: String, pub bytes: u64 }
+
+pub mod lora;
+
+pub fn download_url(value: &str) -> Result<Url, String> {
+ let mut url = Url::parse(value.trim()).map_err(|_| "Enter an HTTP or HTTPS download URL.".to_string())?;
+ if !matches!(url.scheme(), "http" | "https") || url.host_str().is_none() || !url.username().is_empty() || url.password().is_some() {
+ return Err("Enter an HTTP or HTTPS download URL without embedded credentials.".into());
+ }
+ if url.host_str() == Some("huggingface.co") {
+ let path = url.path().replacen("/blob/", "/resolve/", 1);
+ url.set_path(&path);
+ }
+ url.set_fragment(None);
+ Ok(url)
+}
+
+pub fn destination_name(url: &Url) -> String {
+ // A stable URL prefix prevents different repositories' model.safetensors
+ // files from overwriting each other. Completion metadata also verifies URL identity.
+ let hash = url.as_str().bytes().fold(0xcbf29ce484222325u64, |hash, byte| (hash ^ byte as u64).wrapping_mul(0x100000001b3));
+ let filename: String = url.path_segments().and_then(|mut parts| parts.next_back()).unwrap_or("weights.bin")
+ .chars().take(150).map(|c| if c.is_ascii_alphanumeric() || matches!(c, '.' | '-' | '_') { c } else { '_' }).collect();
+ format!("{hash:016x}-{}", if filename.is_empty() { "weights.bin" } else { &filename })
+}
+
+pub fn writable_weights(directory: &Path) -> Result<PathBuf, String> {
+ fs::create_dir_all(directory).map_err(|error| error.to_string())?;
+ let mut random = [0u8; 16];
+ getrandom::fill(&mut random).map_err(|error| error.to_string())?;
+ let probe = directory.join(format!(".write-check-{:x}", u128::from_le_bytes(random)));
+ let file = OpenOptions::new().write(true).create_new(true).open(&probe).map_err(|error| error.to_string())?;
+ drop(file);
+ fs::remove_file(probe).map_err(|error| error.to_string())?;
+ Ok(directory.to_path_buf())
+}
+
+pub fn weight_roots(custom: &[PathBuf], primary: PathBuf, fallback: PathBuf) -> Vec<PathBuf> {
+ let mut roots = Vec::new();
+ for root in custom.iter().cloned().chain([primary, fallback]) {
+ if !roots.contains(&root) { roots.push(root); }
+ }
+ roots
+}
+
+pub fn weights_directory(roots: &[PathBuf]) -> Result<PathBuf, String> {
+ let mut errors = Vec::new();
+ for root in roots {
+ match writable_weights(root) {
+ Ok(directory) => return Ok(directory),
+ Err(error) => errors.push(format!("{}: {error}", root.display())),
+ }
+ }
+ Err(format!("Could not create a writable weights folder: {}", errors.join("; ")))
+}
+
+pub fn cached_file(path: &Path, url: &Url) -> bool {
+ let metadata = fs::read(path.with_extension("complete.json")).ok()
+ .and_then(|bytes| serde_json::from_slice::<CompletedFile>(&bytes).ok());
+ metadata.is_some_and(|info| info.url == url.as_str() && info.bytes > 0
+ && fs::metadata(path).is_ok_and(|file| file.is_file() && file.len() == info.bytes))
+}
+
+pub 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.
+ if let Some(path) = find_cached_weight(url, roots) { return Ok(path); }
+ transfer(url, &weights_directory(roots)?, cancelled, progress)
+}
+
+pub 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())
+}
+
+pub 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()
+}
+
+/// Weight folders for a process without the desktop settings file: the LAN
+/// worker passes its own `--weights` folders and data directory.
+pub fn worker_weight_roots(custom: &[PathBuf], data_directory: &Path) -> Result<Vec<PathBuf>, String> {
+ let executable = std::env::current_exe().map_err(|error| error.to_string())?;
+ let primary = executable.parent().ok_or("Could not locate the worker executable.")?.join("weights");
+ Ok(weight_roots(custom, primary, data_directory.join("weights")))
+}
+
+/// A completed download of `url` in any root, without touching the network.
+pub fn cached_weight(url: &str, roots: &[PathBuf]) -> Option<PathBuf> {
+ find_cached_weight(&download_url(url).ok()?, roots).map(PathBuf::from)
+}
+
+/// Finds or downloads one weight URL, as the desktop download command does.
+pub fn fetch_weight(url: &str, roots: &[PathBuf], cancelled: &AtomicBool, progress: impl FnMut(u64, Option<u64>)) -> Result<PathBuf, String> {
+ transfer_from_roots(&download_url(url)?, roots, cancelled, progress).map(PathBuf::from)
+}
+
+/// The URL a complete Slopus download came from. A LAN worker is sent this
+/// instead of the file, so it can fetch the weights itself.
+pub fn recorded_download_url(path: &Path) -> Option<String> {
+ let record: CompletedFile = serde_json::from_slice(&fs::read(path.with_extension("complete.json")).ok()?).ok()?;
+ let url = download_url(&record.url).ok()?;
+ (path.file_name()?.to_str()? == destination_name(&url) && cached_file(path, &url)).then_some(record.url)
+}
+
+pub 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()); }
+ let mut random = [0u8; 16];
+ getrandom::fill(&mut random).map_err(|error| error.to_string())?;
+ let temporary = directory.join(format!(".download-{:x}.part", u128::from_le_bytes(random)));
+ let result = (|| {
+ let client = Client::builder().connect_timeout(Duration::from_secs(30))
+ .timeout(Duration::from_secs(24 * 60 * 60)).build().map_err(|error| error.to_string())?;
+ let mut response = client.get(url.clone()).header("Accept-Encoding", "identity").send()
+ .and_then(|response| response.error_for_status()).map_err(|error| format!("Download failed: {error}"))?;
+ if response.status() != reqwest::StatusCode::OK { return Err("The server did not return a complete file.".into()); }
+ if response.headers().get(reqwest::header::CONTENT_TYPE).and_then(|v| v.to_str().ok())
+ .is_some_and(|value| value.starts_with("text/html")) {
+ return Err("The URL returned a web page instead of a weight file. Use a direct download link.".into());
+ }
+ let total = response.content_length();
+ let mut file = OpenOptions::new().write(true).create_new(true).open(&temporary).map_err(|error| error.to_string())?;
+ let mut downloaded = 0u64;
+ let mut buffer = vec![0u8; 1024 * 1024];
+ let mut last_progress = Instant::now();
+ progress(0, total);
+ loop {
+ if cancelled.load(Ordering::Relaxed) { return Err("Download cancelled.".into()); }
+ let count = response.read(&mut buffer).map_err(|error| format!("Download interrupted: {error}"))?;
+ if count == 0 { break; }
+ file.write_all(&buffer[..count]).map_err(|error| format!("Could not write weights: {error}"))?;
+ downloaded += count as u64;
+ if last_progress.elapsed() >= Duration::from_millis(200) {
+ progress(downloaded, total);
+ last_progress = Instant::now();
+ }
+ }
+ if downloaded == 0 || total.is_some_and(|total| downloaded != total) {
+ return Err("The download is incomplete. Try downloading again.".into());
+ }
+ if cancelled.load(Ordering::Relaxed) { return Err("Download cancelled.".into()); }
+ file.sync_all().map_err(|error| error.to_string())?;
+ drop(file);
+ // Only a successful, complete transfer receives the final filename.
+ if destination.exists() {
+ if cached_file(&destination, url) { return Ok(destination.to_string_lossy().into_owned()); }
+ fs::remove_file(&destination).map_err(|error| error.to_string())?;
+ }
+ fs::rename(&temporary, &destination).map_err(|error| error.to_string())?;
+ let record = CompletedFile { url: url.to_string(), bytes: downloaded };
+ fs::write(destination.with_extension("complete.json"), serde_json::to_vec(&record).map_err(|error| error.to_string())?)
+ .map_err(|error| error.to_string())?;
+ progress(downloaded, Some(downloaded));
+ Ok(destination.to_string_lossy().into_owned())
+ })();
+ let _ = fs::remove_file(&temporary);
+ result
+}
+
+pub fn hardware_devices() -> Vec<crate::slopfab::GpuDevice> {
+ #[cfg(windows)]
+ {
+ use windows::Win32::Graphics::Dxgi::{CreateDXGIFactory1, IDXGIFactory1, DXGI_ADAPTER_FLAG_SOFTWARE};
+ // DXGI reports dedicated VRAM for NVIDIA, AMD and Intel independently
+ // of CUDA installation. COM interface wrappers release their references.
+ let mut devices = Vec::new();
+ unsafe {
+ if let Ok(factory) = CreateDXGIFactory1::<IDXGIFactory1>() {
+ let mut index = 0;
+ while let Ok(adapter) = factory.EnumAdapters1(index) {
+ index += 1;
+ if let Ok(description) = adapter.GetDesc1() {
+ if description.Flags & DXGI_ADAPTER_FLAG_SOFTWARE.0 as u32 != 0 { continue; }
+ let end = description.Description.iter().position(|value| *value == 0).unwrap_or(description.Description.len());
+ devices.push(crate::slopfab::GpuDevice {
+ name: String::from_utf16_lossy(&description.Description[..end]),
+ memory_bytes: description.DedicatedVideoMemory as u64,
+ });
+ }
+ }
+ }
+ }
+ if !devices.is_empty() { return devices; }
+ }
+ crate::slopfab::gpu_devices()
+}
+
+pub fn validate_removal(path: &Path, roots: &[PathBuf]) -> Result<(), String> {
+ if !path.exists() { return Ok(()); }
+ let canonical = path.canonicalize().map_err(|error| error.to_string())?;
+ let allowed = roots.iter().filter_map(|root| root.canonicalize().ok())
+ .any(|root| canonical.parent() == Some(root.as_path()));
+ if !allowed { return Err("Only weights downloaded into Slopus's weights folders can be removed.".into()); }
+ let record: CompletedFile = serde_json::from_slice(&fs::read(path.with_extension("complete.json"))
+ .map_err(|_| "The downloaded weight's completion record is missing.".to_string())?)
+ .map_err(|_| "The downloaded weight's completion record is invalid.".to_string())?;
+ let url = download_url(&record.url)?;
+ if path.file_name().and_then(|name| name.to_str()) != Some(destination_name(&url).as_str()) {
+ return Err("The weight file does not match its download record.".into());
+ }
+ Ok(())
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use std::net::TcpListener;
+
+ #[test]
+ fn hardware_probe_reads_device_descriptions_without_a_toolkit() {
+ let devices = hardware_devices();
+ for device in &devices { assert!(!device.name.trim().is_empty()); }
+ println!("Detected weight-selection GPUs: {devices:?}");
+ }
+
+ fn server(response: &'static [u8]) -> Url {
+ let listener = TcpListener::bind("127.0.0.1:0").unwrap();
+ let address = listener.local_addr().unwrap();
+ std::thread::spawn(move || {
+ let (mut connection, _) = listener.accept().unwrap();
+ let mut request = [0u8; 8192];
+ let _ = connection.read(&mut request);
+ let _ = connection.write_all(response);
+ });
+ Url::parse(&format!("http://{address}/model.safetensors")).unwrap()
+ }
+
+ #[test]
+ fn converts_hugging_face_pages_and_rejects_non_download_schemes() {
+ assert_eq!(download_url("https://huggingface.co/org/repo/blob/main/vae/model.safetensors").unwrap().as_str(), "https://huggingface.co/org/repo/resolve/main/vae/model.safetensors");
+ for url in ["file:///C:/secret", "ftp://example.com/model", "https://user:[email protected]/model"] { assert!(download_url(url).is_err()); }
+ let url = download_url("https://example.com/../../model?file=../../oops").unwrap();
+ assert!(!destination_name(&url).contains('/'));
+ }
+
+ #[test]
+ fn falls_back_when_the_executable_directory_is_not_writable() {
+ let folder = tempfile::tempdir().unwrap();
+ let blocked = folder.path().join("blocked");
+ fs::write(&blocked, b"file, not a directory").unwrap();
+ assert_eq!(weights_directory(&[blocked.join("weights"), folder.path().join("weights")]).unwrap(), folder.path().join("weights"));
+ }
+
+ #[test]
+ fn custom_folders_are_used_directly_in_order_with_default_fallbacks() {
+ let folder = tempfile::tempdir().unwrap();
+ let blocked = folder.path().join("blocked");
+ fs::write(&blocked, b"not a directory").unwrap();
+ let custom = folder.path().join("custom models");
+ let primary = folder.path().join("exe/weights");
+ let fallback = folder.path().join("logs/weights");
+ let roots = weight_roots(&[blocked.clone(), custom.clone(), custom.clone()], primary.clone(), fallback.clone());
+ assert_eq!(roots, vec![blocked, custom.clone(), primary.clone(), fallback.clone()]);
+ let url = server(b"HTTP/1.1 200 OK\r\nContent-Length: 4\r\nConnection: close\r\n\r\ndata");
+ let downloaded = transfer_from_roots(&url, &roots, &AtomicBool::new(false), |_, _| {}).unwrap();
+ assert_eq!(Path::new(&downloaded).parent(), Some(custom.as_path()));
+ assert!(!custom.join("weights").exists());
+ assert!(!primary.exists());
+ assert!(validate_removal(Path::new(&downloaded), &roots).is_ok());
+ assert!(validate_removal(Path::new(&downloaded), &[primary.clone(), fallback.clone()]).is_err());
+ assert_eq!(weight_roots(&[], primary.clone(), fallback.clone()), vec![primary, fallback]);
+ }
+
+ #[test]
+ fn searches_all_folders_for_completed_files_before_creating_a_download_destination() {
+ let folder = tempfile::tempdir().unwrap();
+ let custom = folder.path().join("custom");
+ let primary = folder.path().join("exe/weights");
+ let fallback = folder.path().join("logs/weights");
+ fs::create_dir_all(&fallback).unwrap();
+ let roots = weight_roots(&[custom.clone()], primary.clone(), fallback.clone());
+ let url = server(b"HTTP/1.1 200 OK\r\nContent-Length: 4\r\nConnection: close\r\n\r\ndata");
+ let downloaded = transfer(&url, &fallback, &AtomicBool::new(false), |_, _| {}).unwrap();
+ // The server accepts only one request; reuse must work without network access.
+ assert_eq!(transfer_from_roots(&url, &roots, &AtomicBool::new(false), |_, _| {}).unwrap(), downloaded);
+ assert!(!custom.exists());
+ assert!(!primary.exists());
+ fs::create_dir_all(&custom).unwrap();
+ let moved = custom.join(destination_name(&url));
+ fs::rename(&downloaded, &moved).unwrap();
+ fs::rename(Path::new(&downloaded).with_extension("complete.json"), moved.with_extension("complete.json")).unwrap();
+ assert_eq!(transfer_from_roots(&url, &roots, &AtomicBool::new(false), |_, _| {}).unwrap(), moved.to_string_lossy());
+ 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();
+ let url = server(b"HTTP/1.1 200 OK\r\nContent-Length: 4\r\nConnection: close\r\n\r\ndata");
+ let path = transfer(&url, folder.path(), &AtomicBool::new(false), |_, _| {}).unwrap();
+ assert_eq!(fs::read(&path).unwrap(), b"data");
+ assert!(cached_file(Path::new(&path), &url));
+ assert_eq!(transfer(&url, folder.path(), &AtomicBool::new(false), |_, _| {}).unwrap(), path);
+ fs::remove_file(&path).unwrap();
+ assert!(!cached_file(Path::new(&path), &url));
+ }
+
+ #[test]
+ fn rejects_truncated_html_failed_and_cancelled_downloads_without_publishing_files() {
+ for (response, cancelled) in [
+ (&b"HTTP/1.1 200 OK\r\nContent-Length: 10\r\nConnection: close\r\n\r\ndata"[..], false),
+ (&b"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: 4\r\nConnection: close\r\n\r\ndata"[..], false),
+ (&b"HTTP/1.1 403 Forbidden\r\nContent-Length: 0\r\nConnection: close\r\n\r\n"[..], false),
+ (&b"HTTP/1.1 200 OK\r\nContent-Length: 4\r\nConnection: close\r\n\r\ndata"[..], true),
+ ] {
+ let folder = tempfile::tempdir().unwrap();
+ let url = server(response);
+ assert!(transfer(&url, folder.path(), &AtomicBool::new(cancelled), |_, _| {}).is_err());
+ assert_eq!(fs::read_dir(folder.path()).unwrap().count(), 0);
+ }
+ }
+
+ #[test]
+ fn removal_is_limited_to_files_created_by_the_downloader() {
+ let folder = tempfile::tempdir().unwrap();
+ let weights = folder.path().join("weights");
+ fs::create_dir(&weights).unwrap();
+ let external = folder.path().join("personal.safetensors");
+ fs::write(&external, b"data").unwrap();
+ assert!(validate_removal(&external, &[weights.clone()]).is_err());
+ let url = server(b"HTTP/1.1 200 OK\r\nContent-Length: 4\r\nConnection: close\r\n\r\ndata");
+ let downloaded = transfer(&url, &weights, &AtomicBool::new(false), |_, _| {}).unwrap();
+ assert!(validate_removal(Path::new(&downloaded), &[weights.clone()]).is_ok());
+ let unmanaged = weights.join("personal.safetensors");
+ fs::write(&unmanaged, b"data").unwrap();
+ assert!(validate_removal(&unmanaged, &[weights]).is_err());
+ assert!(external.is_file());
+ }
+}
diff --git a/src-tauri/core/src/weights/lora.rs b/src-tauri/core/src/weights/lora.rs
new file mode 100644
index 0000000..350752e
--- /dev/null
+++ b/src-tauri/core/src/weights/lora.rs
@@ -0,0 +1,102 @@
+use super::*;
+
+/// Runs before inference, while no model reader can hold the adapter open.
+pub fn prepare(
+ path: &Path,
+ width: i32,
+ allow_download: bool,
+ model_access: &std::sync::RwLock<()>,
+) -> Result<(), String> {
+ let _models = model_access
+ .try_write()
+ .map_err(|_| "Model files are in use. Prepare the LoRA after generation finishes.")?;
+ prepare_exclusive(path, width, allow_download)
+}
+
+/// Preparation for a caller that already holds the model write lock. The LAN
+/// worker waits for that lock instead of failing while a job is running.
+pub fn prepare_exclusive(path: &Path, width: i32, allow_download: bool) -> Result<(), String> {
+ // Slopfab owns format validation, asset selection and atomic embedding.
+ // Preserve the URL record even on an idempotent retry after a record-write error.
+ let record_path = path.with_extension("complete.json");
+ let record = fs::read(&record_path)
+ .ok()
+ .and_then(|bytes| serde_json::from_slice::<CompletedFile>(&bytes).ok())
+ .filter(|record| {
+ download_url(&record.url).is_ok_and(|url| {
+ path.file_name().and_then(|name| name.to_str())
+ == Some(destination_name(&url).as_str())
+ })
+ });
+ crate::slopfab::prepare_lora_grid(path, width, allow_download)?;
+ if let Some(mut record) = record {
+ record.bytes = fs::metadata(path).map_err(|error| error.to_string())?.len();
+ fs::write(
+ record_path,
+ serde_json::to_vec(&record).map_err(|error| error.to_string())?,
+ )
+ .map_err(|error| {
+ format!("LoRA prepared, but its download record could not be updated: {error}")
+ })?;
+ }
+ Ok(())
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ fn fixture(path: &Path, tensor: &str, shape: &[usize]) {
+ let bytes = shape.iter().product::<usize>() * 4;
+ let header = serde_json::json!({ tensor: { "dtype": "F32", "shape": shape, "data_offsets": [0, bytes] } }).to_string();
+ let mut data = (header.len() as u64).to_le_bytes().to_vec();
+ data.extend_from_slice(header.as_bytes());
+ data.resize(data.len() + bytes, 0);
+ fs::write(path, data).unwrap();
+ }
+
+ #[test]
+ fn preparation_preserves_download_discovery_and_is_idempotent() {
+ let model_access = std::sync::RwLock::new(());
+ let folder = tempfile::tempdir().unwrap();
+ let url = download_url("https://example.com/adapter.safetensors").unwrap();
+ let path = folder.path().join(destination_name(&url));
+ fixture(&path, "weight", &[1]);
+ let record = CompletedFile {
+ url: url.to_string(),
+ bytes: fs::metadata(&path).unwrap().len(),
+ };
+ fs::write(
+ path.with_extension("complete.json"),
+ serde_json::to_vec(&record).unwrap(),
+ )
+ .unwrap();
+ let original = fs::read(&path).unwrap();
+ assert!(prepare(&path, 2, false, &model_access).is_err());
+ assert_eq!(fs::read(&path).unwrap(), original);
+ assert!(cached_file(&path, &url));
+ let companion = folder.path().join("h3_silu_temb_grid.safetensors");
+ fixture(&companion, "silu_t_emb_grid", &[1025, 2]);
+ prepare(&path, 2, false, &model_access).unwrap();
+ let embedded = fs::read(&path).unwrap();
+ assert!(embedded.len() > original.len());
+ assert!(cached_file(&path, &url));
+ assert_eq!(
+ find_cached_weight(&url, &[folder.path().to_owned()]),
+ Some(path.to_string_lossy().into_owned())
+ );
+ fs::remove_file(companion).unwrap();
+ prepare(&path, 2, false, &model_access).unwrap();
+ assert_eq!(fs::read(&path).unwrap(), embedded);
+ assert!(validate_removal(&path, &[folder.path().to_owned()]).is_ok());
+ }
+
+ #[test]
+ fn preparation_rejects_busy_model_files() {
+ let model_access = std::sync::RwLock::new(());
+ let _models = model_access.read().unwrap();
+ assert!(prepare(Path::new("unused"), 2, false, &model_access)
+ .unwrap_err()
+ .contains("in use"));
+ }
+}
diff --git a/src-tauri/src/worker/discovery.rs b/src-tauri/core/src/worker/discovery.rs
similarity index 94%
rename from src-tauri/src/worker/discovery.rs
rename to src-tauri/core/src/worker/discovery.rs
index c18ea26..2a24501 100644
--- a/src-tauri/src/worker/discovery.rs
+++ b/src-tauri/core/src/worker/discovery.rs
@@ -8,10 +8,10 @@ use std::{
/// A worker seen on the network. Its HTTP info is fetched separately.
#[derive(Debug, Clone, PartialEq)]
-pub(crate) struct Announced {
- pub(crate) id: String,
- pub(crate) name: String,
- pub(crate) addresses: Vec<SocketAddr>,
+pub struct Announced {
+ pub id: String,
+ pub name: String,
+ pub addresses: Vec<SocketAddr>,
}
fn host_label(name: &str) -> String {
@@ -40,7 +40,7 @@ fn address_rank(address: &SocketAddr) -> (u8, SocketAddr) {
}
/// Keeps announcing this worker for as long as the returned daemon lives.
-pub(crate) fn advertise(info: &WorkerInfo, port: u16) -> Result<ServiceDaemon, String> {
+pub fn advertise(info: &WorkerInfo, port: u16) -> Result<ServiceDaemon, String> {
let daemon = ServiceDaemon::new().map_err(|error| error.to_string())?;
let host = format!("{}-{}.local.", host_label(&super::server::computer_name()), &info.id[..6.min(info.id.len())]);
let protocol = PROTOCOL_VERSION.to_string();
@@ -59,7 +59,7 @@ pub(crate) fn advertise(info: &WorkerInfo, port: u16) -> Result<ServiceDaemon, S
/// Browses in the background and calls `changed` with every worker currently
/// announced whenever that set changes.
-pub(crate) fn browse(changed: impl Fn(Vec<Announced>) + Send + 'static) -> Result<ServiceDaemon, String> {
+pub fn browse(changed: impl Fn(Vec<Announced>) + Send + 'static) -> Result<ServiceDaemon, String> {
let daemon = ServiceDaemon::new().map_err(|error| error.to_string())?;
let events = daemon.browse(SERVICE_TYPE).map_err(|error| error.to_string())?;
std::thread::Builder::new()
diff --git a/src-tauri/core/src/worker/mod.rs b/src-tauri/core/src/worker/mod.rs
new file mode 100644
index 0000000..7b3f890
--- /dev/null
+++ b/src-tauri/core/src/worker/mod.rs
@@ -0,0 +1,9 @@
+//! LAN generation workers: the wire protocol both sides compile, and the
+//! headless server `slopus-worker` runs. The desktop client lives in the app.
+pub mod discovery;
+pub mod protocol;
+pub mod server;
+pub mod store;
+
+pub use protocol::{WorkerRuntime, DEFAULT_PORT};
+pub use server::{run, WorkerOptions};
diff --git a/src-tauri/src/worker/protocol.rs b/src-tauri/core/src/worker/protocol.rs
similarity index 99%
rename from src-tauri/src/worker/protocol.rs
rename to src-tauri/core/src/worker/protocol.rs
index acddc12..bac0071 100644
--- a/src-tauri/src/worker/protocol.rs
+++ b/src-tauri/core/src/worker/protocol.rs
@@ -1,7 +1,7 @@
//! The wire contract between Slopus and a LAN worker. Both sides compile this
//! file, so a field renamed here is renamed for both.
use crate::{
- project::{ProviderOption, ProviderSetting},
+ settings::{ProviderOption, ProviderSetting},
slopfab::{GenerationRequest, GpuDevice},
};
use serde::{Deserialize, Serialize};
diff --git a/src-tauri/src/worker/server.rs b/src-tauri/core/src/worker/server.rs
similarity index 99%
rename from src-tauri/src/worker/server.rs
rename to src-tauri/core/src/worker/server.rs
index abd61c6..120454b 100644
--- a/src-tauri/src/worker/server.rs
+++ b/src-tauri/core/src/worker/server.rs
@@ -8,8 +8,8 @@ use super::{
};
use crate::{
diagnostics,
- media::artifacts::generated_file_stem,
- project::{ProviderOption, ProviderSetting},
+ names::generated_file_stem,
+ settings::{ProviderOption, ProviderSetting},
rendered, slopfab,
};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
diff --git a/src-tauri/src/worker/store.rs b/src-tauri/core/src/worker/store.rs
similarity index 95%
rename from src-tauri/src/worker/store.rs
rename to src-tauri/core/src/worker/store.rs
index 8c00c90..f2e94ad 100644
--- a/src-tauri/src/worker/store.rs
+++ b/src-tauri/core/src/worker/store.rs
@@ -10,7 +10,7 @@ use std::{
sync::{atomic::AtomicBool, Mutex},
};
-pub(crate) struct FileStore {
+pub struct FileStore {
uploads: PathBuf,
weight_roots: Vec<PathBuf>,
/// One download at a time, like the desktop app; parallel clients that
@@ -19,7 +19,7 @@ pub(crate) struct FileStore {
}
impl FileStore {
- pub(crate) fn new(data: &Path, weight_roots: Vec<PathBuf>) -> Result<Self, String> {
+ pub fn new(data: &Path, weight_roots: Vec<PathBuf>) -> Result<Self, String> {
let uploads = data.join("uploads");
fs::create_dir_all(&uploads).map_err(|error| format!("Could not create {}: {error}", uploads.display()))?;
Ok(Self { uploads, weight_roots, downloads: Mutex::new(()) })
@@ -37,7 +37,7 @@ impl FileStore {
}
/// Upload entries this worker does not hold yet.
- pub(crate) fn missing(&self, files: &[FileRef]) -> Result<Vec<(String, String)>, String> {
+ pub fn missing(&self, files: &[FileRef]) -> Result<Vec<(String, String)>, String> {
let mut missing = Vec::new();
for file in files {
if let FileRef::Upload { key, entries, .. } = file {
@@ -54,7 +54,7 @@ impl FileStore {
/// Streams one uploaded entry to disk. It only takes its final name once
/// every announced byte has arrived.
- pub(crate) fn receive(&self, key: &str, entry: &str, bytes: u64, body: &mut dyn Read) -> Result<(), String> {
+ pub fn receive(&self, key: &str, entry: &str, bytes: u64, body: &mut dyn Read) -> Result<(), String> {
let destination = self.entry_path(key, entry)?;
let directory = destination.parent().ok_or("Invalid upload path.")?;
fs::create_dir_all(directory).map_err(|error| error.to_string())?;
@@ -80,7 +80,7 @@ impl FileStore {
/// The local path for one file, without network access. Fails when the
/// file still has to be uploaded or downloaded.
- pub(crate) fn resolve(&self, file: &FileRef) -> Result<PathBuf, String> {
+ pub fn resolve(&self, file: &FileRef) -> Result<PathBuf, String> {
match file {
FileRef::Url { url, lora } => {
let path = weights::cached_weight(url, &self.weight_roots)
@@ -107,7 +107,7 @@ impl FileStore {
}
/// Downloads and prepares everything `files` names that is not here yet.
- pub(crate) fn prepare(
+ pub fn prepare(
&self,
files: &[FileRef],
cancelled: &AtomicBool,
diff --git a/src-tauri/src/diagnostics.rs b/src-tauri/src/diagnostics.rs
index 16cc410..76d88e6 100644
--- a/src-tauri/src/diagnostics.rs
+++ b/src-tauri/src/diagnostics.rs
@@ -1,290 +1,8 @@
-//! Persistent diagnostics shared by the native runtime and the webview.
-//!
-//! One human-readable record is written per line so a damaged final write
-//! never makes earlier records unreadable. The active file rotates at startup
-//! and at 5 MiB, and only one previous file is retained. Prompt bodies and credentials are
-//! deliberately excluded by callers and redacted again here at the final
-//! write boundary.
-
-use serde::{Deserialize, Serialize};
-use serde_json::{Map, Value};
-use std::{
- fs::{self, File, OpenOptions},
- io::{BufWriter, Write},
- path::{Path, PathBuf},
- process::Command,
- sync::{Mutex, Once, OnceLock},
- time::{SystemTime, UNIX_EPOCH},
-};
+//! Diagnostics are written by slopus-core; this adds where the app keeps them.
+pub use slopus_core::diagnostics::*;
+use std::path::PathBuf;
use tauri::AppHandle;
-const ACTIVE_FILE: &str = "slopus.log";
-const PREVIOUS_FILE: &str = "slopus-prev.log";
-const MAX_BYTES: u64 = 5 * 1024 * 1024;
-const MAX_TEXT_CHARS: usize = 8 * 1024;
-
-static LOGGER: OnceLock<Mutex<LogWriter>> = OnceLock::new();
-static PANIC_HOOK: Once = Once::new();
-
-#[derive(Debug, Deserialize)]
-#[serde(rename_all = "camelCase")]
-pub struct FrontendLogEvent {
- pub level: String,
- pub target: String,
- pub event: String,
- pub message: String,
- #[serde(default)]
- pub context: Option<Value>,
-}
-
-#[derive(Debug, Clone, Serialize)]
-#[serde(rename_all = "camelCase")]
-pub struct DiagnosticLogInfo {
- pub path: String,
- pub previous_path: Option<String>,
- pub session_id: String,
- pub max_file_bytes: u64,
-}
-
-struct LogWriter {
- active_path: PathBuf,
- previous_path: PathBuf,
- session_id: String,
- max_bytes: u64,
- bytes: u64,
- file: Option<BufWriter<File>>,
-}
-
-impl LogWriter {
- fn new(directory: &Path, max_bytes: u64) -> Result<Self, String> {
- fs::create_dir_all(directory).map_err(|error| {
- format!(
- "Could not create diagnostic log directory '{}': {error}",
- directory.display()
- )
- })?;
- let active_path = directory.join(ACTIVE_FILE);
- let previous_path = directory.join(PREVIOUS_FILE);
- let mut writer = Self {
- active_path,
- previous_path,
- session_id: format!("{}-{}", std::process::id(), timestamp_ms()),
- max_bytes,
- bytes: 0,
- file: None,
- };
- writer.rotate()?;
- Ok(writer)
- }
-
- fn write(
- &mut self,
- level: &str,
- target: &str,
- event: &str,
- message: &str,
- context: Value,
- ) -> Result<(), String> {
- let context = format_context(redact(context, 0));
- let mut fields = format!(
- "sessionId={}",
- serde_json::to_string(&self.session_id).unwrap_or_else(|_| "\"[unprintable]\"".into())
- );
- if !context.is_empty() {
- fields.push_str(", ");
- fields.push_str(&context);
- }
- let line = format!(
- "{} [{}] {} {} - {}{}\n",
- format_timestamp(timestamp_ms()),
- normalized_level(level).to_ascii_uppercase(),
- single_line(&bounded_text(target)),
- single_line(&bounded_text(event)),
- single_line(&bounded_text(message)),
- format!(" | {fields}"),
- )
- .into_bytes();
- if self.bytes > 0 && self.bytes.saturating_add(line.len() as u64) > self.max_bytes {
- self.rotate()?;
- }
- let file = self
- .file
- .as_mut()
- .ok_or_else(|| "Diagnostic log is not open.".to_string())?;
- file.write_all(&line)
- .map_err(|error| format!("Could not write diagnostic log: {error}"))?;
- file.flush()
- .map_err(|error| format!("Could not flush diagnostic log: {error}"))?;
- self.bytes = self.bytes.saturating_add(line.len() as u64);
- Ok(())
- }
-
- fn rotate(&mut self) -> Result<(), String> {
- if let Some(mut file) = self.file.take() {
- let _ = file.flush();
- }
- if self.active_path.exists() {
- if self.previous_path.exists() {
- fs::remove_file(&self.previous_path).map_err(|error| {
- format!("Could not replace previous diagnostic log: {error}")
- })?;
- }
- fs::rename(&self.active_path, &self.previous_path)
- .map_err(|error| format!("Could not rotate diagnostic log: {error}"))?;
- }
- self.file = Some(open_append(&self.active_path)?);
- self.bytes = 0;
- Ok(())
- }
-
- fn info(&self) -> DiagnosticLogInfo {
- DiagnosticLogInfo {
- path: self.active_path.to_string_lossy().into_owned(),
- previous_path: self
- .previous_path
- .is_file()
- .then(|| self.previous_path.to_string_lossy().into_owned()),
- session_id: self.session_id.clone(),
- max_file_bytes: self.max_bytes,
- }
- }
-}
-
-fn open_append(path: &Path) -> Result<BufWriter<File>, String> {
- OpenOptions::new()
- .create(true)
- .append(true)
- .open(path)
- .map(BufWriter::new)
- .map_err(|error| {
- format!(
- "Could not open diagnostic log '{}': {error}",
- path.display()
- )
- })
-}
-
-fn timestamp_ms() -> u64 {
- SystemTime::now()
- .duration_since(UNIX_EPOCH)
- .unwrap_or_default()
- .as_millis()
- .min(u64::MAX as u128) as u64
-}
-
-fn format_timestamp(timestamp_ms: u64) -> String {
- let total_seconds = timestamp_ms / 1_000;
- let milliseconds = timestamp_ms % 1_000;
- let days = (total_seconds / 86_400) as i64;
- let seconds_in_day = total_seconds % 86_400;
- let hour = seconds_in_day / 3_600;
- let minute = seconds_in_day % 3_600 / 60;
- let second = seconds_in_day % 60;
-
- // Gregorian civil date conversion from days since the Unix epoch.
- let shifted_days = days + 719_468;
- let era = if shifted_days >= 0 {
- shifted_days
- } else {
- shifted_days - 146_096
- } / 146_097;
- let day_of_era = shifted_days - era * 146_097;
- let year_of_era =
- (day_of_era - day_of_era / 1_460 + day_of_era / 36_524 - day_of_era / 146_096) / 365;
- let mut year = year_of_era + era * 400;
- let day_of_year = day_of_era - (365 * year_of_era + year_of_era / 4 - year_of_era / 100);
- let month_prime = (5 * day_of_year + 2) / 153;
- let day = day_of_year - (153 * month_prime + 2) / 5 + 1;
- let month = month_prime + if month_prime < 10 { 3 } else { -9 };
- if month <= 2 {
- year += 1;
- }
-
- format!("{year:04}-{month:02}-{day:02}T{hour:02}:{minute:02}:{second:02}.{milliseconds:03}Z")
-}
-
-fn single_line(value: &str) -> String {
- value
- .replace('\r', "\\r")
- .replace('\n', "\\n")
- .replace('\t', "\\t")
-}
-
-fn format_context(value: Value) -> String {
- let format_value =
- |value: &Value| serde_json::to_string(value).unwrap_or_else(|_| "\"[unprintable]\"".into());
- match value {
- Value::Null => String::new(),
- Value::Object(values) if values.is_empty() => String::new(),
- Value::Object(values) => values
- .iter()
- .map(|(key, value)| format!("{}={}", single_line(key), format_value(value)))
- .collect::<Vec<_>>()
- .join(", "),
- other => format!("context={}", format_value(&other)),
- }
-}
-
-fn bounded_text(value: &str) -> String {
- value.chars().take(MAX_TEXT_CHARS).collect()
-}
-
-fn normalized_level(value: &str) -> &'static str {
- match value.to_ascii_lowercase().as_str() {
- "trace" => "trace",
- "debug" => "debug",
- "warn" | "warning" => "warn",
- "error" => "error",
- _ => "info",
- }
-}
-
-fn sensitive_key(key: &str) -> bool {
- matches!(
- key.to_ascii_lowercase().replace(['_', '-'], "").as_str(),
- "apikey"
- | "authorization"
- | "password"
- | "secret"
- | "token"
- | "accesstoken"
- | "refreshtoken"
- | "prompt"
- | "content"
- )
-}
-
-fn redact(value: Value, depth: usize) -> Value {
- if depth >= 6 {
- return Value::String("[truncated]".into());
- }
- match value {
- Value::Object(values) => Value::Object(
- values
- .into_iter()
- .take(64)
- .map(|(key, value)| {
- let value = if sensitive_key(&key) {
- Value::String("[redacted]".into())
- } else {
- redact(value, depth + 1)
- };
- (key, value)
- })
- .collect::<Map<_, _>>(),
- ),
- Value::Array(values) => Value::Array(
- values
- .into_iter()
- .take(64)
- .map(|value| redact(value, depth + 1))
- .collect(),
- ),
- Value::String(value) => Value::String(bounded_text(&value)),
- other => other,
- }
-}
-
pub fn directory(app: &AppHandle) -> PathBuf {
crate::app_paths::data_directory(app).join("logs")
}
@@ -292,203 +10,3 @@ pub fn directory(app: &AppHandle) -> PathBuf {
pub fn initialize(app: &AppHandle) -> Result<DiagnosticLogInfo, String> {
initialize_in(&directory(app))
}
-
-/// Starts logging into an explicit folder. The headless worker has no Tauri
-/// app handle, but writes the same records.
-pub fn initialize_in(directory: &Path) -> Result<DiagnosticLogInfo, String> {
- let writer = LogWriter::new(directory, MAX_BYTES)?;
- let info = writer.info();
- LOGGER
- .set(Mutex::new(writer))
- .map_err(|_| "Diagnostic logging was initialized twice.".to_string())?;
- install_panic_hook();
- Ok(info)
-}
-
-fn install_panic_hook() {
- PANIC_HOOK.call_once(|| {
- let previous = std::panic::take_hook();
- std::panic::set_hook(Box::new(move |panic| {
- let location = panic
- .location()
- .map(|value| {
- serde_json::json!({
- "file": value.file(), "line": value.line(), "column": value.column(),
- })
- })
- .unwrap_or(Value::Null);
- let message = panic
- .payload()
- .downcast_ref::<&str>()
- .copied()
- .or_else(|| panic.payload().downcast_ref::<String>().map(String::as_str))
- .unwrap_or("Rust panicked without a string message.");
- error(
- "native",
- "panic",
- message,
- serde_json::json!({ "location": location }),
- );
- previous(panic);
- }));
- });
-}
-
-fn write(
- level: &str,
- target: &str,
- event: &str,
- message: &str,
- context: Value,
-) -> Result<(), String> {
- let logger = LOGGER
- .get()
- .ok_or_else(|| "Diagnostic logging is not initialized.".to_string())?;
- logger
- .lock()
- .map_err(|_| "Diagnostic log lock failed.".to_string())?
- .write(level, target, event, message, context)
-}
-
-pub fn info(target: &str, event: &str, message: &str, context: Value) {
- let _ = write("info", target, event, message, context);
-}
-
-pub fn debug(target: &str, event: &str, message: &str, context: Value) {
- let _ = write("debug", target, event, message, context);
-}
-
-pub fn warn(target: &str, event: &str, message: &str, context: Value) {
- let _ = write("warn", target, event, message, context);
-}
-
-pub fn error(target: &str, event: &str, message: &str, context: Value) {
- let _ = write("error", target, event, message, context);
-}
-
-pub fn write_frontend(entry: FrontendLogEvent) -> Result<(), String> {
- write(
- &entry.level,
- &format!("frontend.{}", entry.target),
- &entry.event,
- &entry.message,
- entry.context.unwrap_or_else(|| Value::Object(Map::new())),
- )
-}
-
-pub fn log_info() -> Result<DiagnosticLogInfo, String> {
- let logger = LOGGER
- .get()
- .ok_or_else(|| "Diagnostic logging is not initialized.".to_string())?;
- Ok(logger
- .lock()
- .map_err(|_| "Diagnostic log lock failed.".to_string())?
- .info())
-}
-
-pub fn reveal_log() -> Result<DiagnosticLogInfo, String> {
- let info = log_info()?;
- let path = PathBuf::from(&info.path);
- #[cfg(target_os = "windows")]
- Command::new("explorer.exe")
- .arg("/select,")
- .arg(&path)
- .spawn()
- .map_err(|error| format!("Could not open the diagnostic log folder: {error}"))?;
- #[cfg(target_os = "macos")]
- Command::new("open")
- .arg("-R")
- .arg(&path)
- .spawn()
- .map_err(|error| format!("Could not reveal the diagnostic log: {error}"))?;
- #[cfg(all(unix, not(target_os = "macos")))]
- Command::new("xdg-open")
- .arg(path.parent().unwrap_or(Path::new(".")))
- .spawn()
- .map_err(|error| format!("Could not open the diagnostic log folder: {error}"))?;
- Ok(info)
-}
-
-#[cfg(test)]
-mod tests {
- use super::*;
-
- #[test]
- fn text_lines_are_readable_and_redact_credentials_and_prompt_content() {
- let folder = tempfile::tempdir().unwrap();
- let mut writer = LogWriter::new(folder.path(), 1024 * 1024).unwrap();
- writer.write("error", "generation", "failed", "encoder failed", serde_json::json!({
- "jobId": "job-1", "apiKey": "secret-key", "prompt": "private scene", "nested": { "password": "secret" }
- })).unwrap();
- drop(writer);
- let text = fs::read_to_string(folder.path().join(ACTIVE_FILE)).unwrap();
- assert!(text.contains("[ERROR] generation failed - encoder failed"));
- assert!(text.contains("sessionId="));
- assert!(text.contains("jobId=\"job-1\""));
- assert!(text.contains("apiKey=\"[redacted]\""));
- assert!(text.contains("prompt=\"[redacted]\""));
- assert!(text.contains("nested={\"password\":\"[redacted]\"}"));
- assert!(!text.contains("private scene"));
- assert!(!text.contains("secret-key"));
- }
-
- #[test]
- fn timestamp_is_utc_and_human_readable() {
- assert_eq!(format_timestamp(0), "1970-01-01T00:00:00.000Z");
- assert_eq!(
- format_timestamp(1_788_449_322_122),
- "2026-09-03T15:28:42.122Z"
- );
- }
-
- #[test]
- fn startup_rotates_the_old_log_and_replaces_the_previous_session() {
- let folder = tempfile::tempdir().unwrap();
- let active = folder.path().join(ACTIVE_FILE);
- let previous = folder.path().join("slopus-prev.log");
- fs::write(&active, "last session").unwrap();
- fs::write(&previous, "older session").unwrap();
-
- let mut writer = LogWriter::new(folder.path(), MAX_BYTES).unwrap();
- assert_eq!(fs::read_to_string(&previous).unwrap(), "last session");
- assert_eq!(fs::read_to_string(&active).unwrap(), "");
- assert_eq!(writer.bytes, 0);
- assert_eq!(
- writer.info().previous_path,
- Some(previous.to_string_lossy().into_owned())
- );
- writer
- .write("info", "test", "startup", "new session", Value::Null)
- .unwrap();
- drop(writer);
-
- let current = fs::read_to_string(&active).unwrap();
- assert!(current.contains("new session"));
- assert!(!current.contains("last session"));
- let writer = LogWriter::new(folder.path(), MAX_BYTES).unwrap();
- assert_eq!(fs::read_to_string(&previous).unwrap(), current);
- assert_eq!(fs::read_to_string(&active).unwrap(), "");
- drop(writer);
- }
-
- #[test]
- fn rotation_keeps_one_previous_file() {
- let folder = tempfile::tempdir().unwrap();
- let mut writer = LogWriter::new(folder.path(), 300).unwrap();
- for index in 0..12 {
- writer
- .write(
- "info",
- "test",
- "line",
- &format!("record {index} {}", "x".repeat(80)),
- Value::Null,
- )
- .unwrap();
- }
- drop(writer);
- assert!(folder.path().join(ACTIVE_FILE).is_file());
- assert!(folder.path().join(PREVIOUS_FILE).is_file());
- assert_eq!(fs::read_dir(folder.path()).unwrap().count(), 2);
- }
-}
diff --git a/src-tauri/src/generation/models/h3.rs b/src-tauri/src/generation/models/h3.rs
index 4c27d84..1e8829e 100644
--- a/src-tauri/src/generation/models/h3.rs
+++ b/src-tauri/src/generation/models/h3.rs
@@ -1,4 +1,4 @@
-use super::{ModelCapabilities, ModelDefinition, SceneDefaults};
+use super::{ModelDefinition, SceneDefaults};
use crate::project::SceneShot;
use std::sync::LazyLock;
@@ -7,15 +7,7 @@ pub(crate) static MODEL: ModelDefinition = ModelDefinition {
provider_id: "minimax-h3",
duration_seconds: 6.0,
},
- capabilities: ModelCapabilities {
- max_scene_seconds: 15.0,
- min_steps: 2,
- max_image_references: 9,
- max_video_references: 3,
- max_references: 12,
- continuation_overlap: 22,
- frame_stride: 17,
- },
+ capabilities: slopus_core::models::H3_CAPABILITIES,
shot_catalog,
validate_shot_settings: validate_agent_shot_settings,
agent_instructions: include_str!("../../../assets/h3-agent.md"),
diff --git a/src-tauri/src/generation/models/mod.rs b/src-tauri/src/generation/models/mod.rs
index 4a8f477..dca9e46 100644
--- a/src-tauri/src/generation/models/mod.rs
+++ b/src-tauri/src/generation/models/mod.rs
@@ -6,16 +6,7 @@ pub(crate) struct SceneDefaults {
pub duration_seconds: f64,
}
-#[derive(Debug, Clone, Copy)]
-pub(crate) struct ModelCapabilities {
- pub max_scene_seconds: f64,
- pub min_steps: i32,
- pub max_image_references: usize,
- pub max_video_references: usize,
- pub max_references: usize,
- pub continuation_overlap: i32,
- pub frame_stride: i32,
-}
+pub(crate) use slopus_core::models::ModelCapabilities;
pub(crate) struct ModelDefinition {
pub defaults: SceneDefaults,
diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs
index e865f26..da47c18 100644
--- a/src-tauri/src/lib.rs
+++ b/src-tauri/src/lib.rs
@@ -3,7 +3,6 @@ mod app;
mod app_paths;
mod app_settings;
mod commands;
-mod conditioning;
mod cuda_support;
mod diagnostics;
mod export;
@@ -11,19 +10,15 @@ mod generation;
mod media;
mod native_shell;
mod project;
-mod reference_icons;
-mod rendered;
-mod slopfab;
-mod storage;
mod weights;
mod window;
mod worker;
+// The generation runtime is shared with the LAN worker through slopus-core.
+use slopus_core::{conditioning, reference_icons, rendered, slopfab, storage};
+
pub use app::run;
-pub use worker::{run as run_worker, WorkerOptions, DEFAULT_PORT as DEFAULT_WORKER_PORT};
-pub use slopfab::{
- default_dll_path, generate_reference_icon_batch, ReferenceIconBatchConfig, ReferenceIconSpec,
-};
+pub use slopus_core::{default_dll_path, generate_reference_icon_batch, ReferenceIconBatchConfig, ReferenceIconSpec};
#[cfg(test)]
mod tests;
diff --git a/src-tauri/src/media/artifacts.rs b/src-tauri/src/media/artifacts.rs
index e0529f9..3413e1c 100644
--- a/src-tauri/src/media/artifacts.rs
+++ b/src-tauri/src/media/artifacts.rs
@@ -7,29 +7,7 @@ use std::path::PathBuf;
/// `create_project` along with the rest of the folder layout.
pub(crate) const GENERATED_DIRECTORY: &str = "media/generated";
-/// A scene id, as a file name.
-///
-/// The webview names the job, so this is caller-controlled text about to become
-/// a path — the exact shape of a traversal. Rather than sanitising it, the id
-/// is REFUSED unless it is already only letters, digits, dash and underscore,
-/// which is what every id Slopus generates looks like. There is nothing to
-/// strip and nothing to get wrong: no dots (so no `..` and no second
-/// extension), no separators, no length worth arguing about.
-pub(crate) fn generated_file_stem(job_id: &str) -> Result<String, String> {
- let trimmed = job_id.trim();
- if trimmed.is_empty() || trimmed.len() > 64 {
- return Err("A scene id must be between 1 and 64 characters.".into());
- }
- if !trimmed
- .chars()
- .all(|character| character.is_ascii_alphanumeric() || matches!(character, '-' | '_'))
- {
- return Err(format!(
- "'{job_id}' is not a scene id: only letters, digits, dashes and underscores name a rendered file."
- ));
- }
- Ok(trimmed.to_string())
-}
+pub(crate) use slopus_core::names::generated_file_stem;
/// The single path a generated video may be written to, and what the project
/// will record. Nothing about it comes from the caller except the scene id,
diff --git a/src-tauri/src/project/model.rs b/src-tauri/src/project/model.rs
index 2c31919..ebb4c75 100644
--- a/src-tauri/src/project/model.rs
+++ b/src-tauri/src/project/model.rs
@@ -118,21 +118,8 @@ pub(crate) struct AgentMessage {
pub(crate) created_at: String,
}
-#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
-pub(crate) struct ProviderSetting {
- pub(crate) enabled: bool,
- pub(crate) model: Option<String>,
- #[serde(default)]
- pub(crate) options: BTreeMap<String, ProviderOption>,
-}
-
-#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
-#[serde(untagged)]
-pub(crate) enum ProviderOption {
- String(String),
- Number(f64),
- Boolean(bool),
-}
+// Shared with the LAN worker, which receives provider settings with each job.
+pub(crate) use slopus_core::settings::{ProviderOption, ProviderSetting};
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
diff --git a/src-tauri/src/storage/mod.rs b/src-tauri/src/storage/mod.rs
deleted file mode 100644
index d8ba4fb..0000000
--- a/src-tauri/src/storage/mod.rs
+++ /dev/null
@@ -1 +0,0 @@
-pub(crate) mod atomic;
diff --git a/src-tauri/src/weights.rs b/src-tauri/src/weights.rs
index b309c81..5a8d69b 100644
--- a/src-tauri/src/weights.rs
+++ b/src-tauri/src/weights.rs
@@ -1,13 +1,13 @@
+//! Weight download commands. The downloader itself lives in slopus-core,
+//! shared with the LAN worker; this adds the app's folders and progress events.
use crate::diagnostics;
-use reqwest::{blocking::Client, Url};
-use serde::{Deserialize, Serialize};
+use serde::Serialize;
+use slopus_core::weights::*;
use std::{
collections::HashMap,
- fs::{self, OpenOptions},
- io::{Read, Write},
+ fs,
path::{Path, PathBuf},
sync::{atomic::{AtomicBool, Ordering}, Arc, Mutex},
- time::{Duration, Instant},
};
use tauri::{AppHandle, Emitter, State};
@@ -18,52 +18,8 @@ pub struct WeightDownloads(Mutex<HashMap<String, Arc<AtomicBool>>>);
#[serde(rename_all = "camelCase")]
struct Progress { request_id: String, downloaded: u64, total: Option<u64> }
-#[derive(Serialize, Deserialize)]
-struct CompletedFile { url: String, bytes: u64 }
-
pub(crate) mod lora;
-fn download_url(value: &str) -> Result<Url, String> {
- let mut url = Url::parse(value.trim()).map_err(|_| "Enter an HTTP or HTTPS download URL.".to_string())?;
- if !matches!(url.scheme(), "http" | "https") || url.host_str().is_none() || !url.username().is_empty() || url.password().is_some() {
- return Err("Enter an HTTP or HTTPS download URL without embedded credentials.".into());
- }
- if url.host_str() == Some("huggingface.co") {
- let path = url.path().replacen("/blob/", "/resolve/", 1);
- url.set_path(&path);
- }
- url.set_fragment(None);
- Ok(url)
-}
-
-fn destination_name(url: &Url) -> String {
- // A stable URL prefix prevents different repositories' model.safetensors
- // files from overwriting each other. Completion metadata also verifies URL identity.
- let hash = url.as_str().bytes().fold(0xcbf29ce484222325u64, |hash, byte| (hash ^ byte as u64).wrapping_mul(0x100000001b3));
- let filename: String = url.path_segments().and_then(|mut parts| parts.next_back()).unwrap_or("weights.bin")
- .chars().take(150).map(|c| if c.is_ascii_alphanumeric() || matches!(c, '.' | '-' | '_') { c } else { '_' }).collect();
- format!("{hash:016x}-{}", if filename.is_empty() { "weights.bin" } else { &filename })
-}
-
-fn writable_weights(directory: &Path) -> Result<PathBuf, String> {
- fs::create_dir_all(directory).map_err(|error| error.to_string())?;
- let mut random = [0u8; 16];
- getrandom::fill(&mut random).map_err(|error| error.to_string())?;
- let probe = directory.join(format!(".write-check-{:x}", u128::from_le_bytes(random)));
- let file = OpenOptions::new().write(true).create_new(true).open(&probe).map_err(|error| error.to_string())?;
- drop(file);
- fs::remove_file(probe).map_err(|error| error.to_string())?;
- Ok(directory.to_path_buf())
-}
-
-fn weight_roots(custom: &[PathBuf], primary: PathBuf, fallback: PathBuf) -> Vec<PathBuf> {
- let mut roots = Vec::new();
- for root in custom.iter().cloned().chain([primary, fallback]) {
- if !roots.contains(&root) { roots.push(root); }
- }
- roots
-}
-
fn configured_weight_roots(app: &AppHandle) -> Result<Vec<PathBuf>, String> {
let data_directory = crate::app_paths::data_directory(app);
let settings = crate::app_settings::load(&data_directory)?;
@@ -74,69 +30,6 @@ fn configured_weight_roots(app: &AppHandle) -> Result<Vec<PathBuf>, String> {
Ok(weight_roots(&settings.weight_folders, primary, fallback))
}
-fn weights_directory(roots: &[PathBuf]) -> Result<PathBuf, String> {
- let mut errors = Vec::new();
- for root in roots {
- match writable_weights(root) {
- Ok(directory) => return Ok(directory),
- Err(error) => errors.push(format!("{}: {error}", root.display())),
- }
- }
- Err(format!("Could not create a writable weights folder: {}", errors.join("; ")))
-}
-
-fn cached_file(path: &Path, url: &Url) -> bool {
- let metadata = fs::read(path.with_extension("complete.json")).ok()
- .and_then(|bytes| serde_json::from_slice::<CompletedFile>(&bytes).ok());
- metadata.is_some_and(|info| info.url == url.as_str() && info.bytes > 0
- && fs::metadata(path).is_ok_and(|file| file.is_file() && file.len() == info.bytes))
-}
-
-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.
- 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()
-}
-
-/// Weight folders for a process without the desktop settings file: the LAN
-/// worker passes its own `--weights` folders and data directory.
-pub(crate) fn worker_weight_roots(custom: &[PathBuf], data_directory: &Path) -> Result<Vec<PathBuf>, String> {
- let executable = std::env::current_exe().map_err(|error| error.to_string())?;
- let primary = executable.parent().ok_or("Could not locate the worker executable.")?.join("weights");
- Ok(weight_roots(custom, primary, data_directory.join("weights")))
-}
-
-/// A completed download of `url` in any root, without touching the network.
-pub(crate) fn cached_weight(url: &str, roots: &[PathBuf]) -> Option<PathBuf> {
- find_cached_weight(&download_url(url).ok()?, roots).map(PathBuf::from)
-}
-
-/// Finds or downloads one weight URL, as the desktop download command does.
-pub(crate) fn fetch_weight(url: &str, roots: &[PathBuf], cancelled: &AtomicBool, progress: impl FnMut(u64, Option<u64>)) -> Result<PathBuf, String> {
- transfer_from_roots(&download_url(url)?, roots, cancelled, progress).map(PathBuf::from)
-}
-
-/// The URL a complete Slopus download came from. A LAN worker is sent this
-/// instead of the file, so it can fetch the weights itself.
-pub(crate) fn recorded_download_url(path: &Path) -> Option<String> {
- let record: CompletedFile = serde_json::from_slice(&fs::read(path.with_extension("complete.json")).ok()?).ok()?;
- let url = download_url(&record.url).ok()?;
- (path.file_name()?.to_str()? == destination_name(&url) && cached_file(path, &url)).then_some(record.url)
-}
-
#[tauri::command]
pub async fn find_downloaded_weights(app: AppHandle, urls: Vec<String>) -> Result<HashMap<String, String>, String> {
tauri::async_runtime::spawn_blocking(move || {
@@ -144,61 +37,6 @@ pub async fn find_downloaded_weights(app: AppHandle, urls: Vec<String>) -> Resul
}).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()); }
- let mut random = [0u8; 16];
- getrandom::fill(&mut random).map_err(|error| error.to_string())?;
- let temporary = directory.join(format!(".download-{:x}.part", u128::from_le_bytes(random)));
- let result = (|| {
- let client = Client::builder().connect_timeout(Duration::from_secs(30))
- .timeout(Duration::from_secs(24 * 60 * 60)).build().map_err(|error| error.to_string())?;
- let mut response = client.get(url.clone()).header("Accept-Encoding", "identity").send()
- .and_then(|response| response.error_for_status()).map_err(|error| format!("Download failed: {error}"))?;
- if response.status() != reqwest::StatusCode::OK { return Err("The server did not return a complete file.".into()); }
- if response.headers().get(reqwest::header::CONTENT_TYPE).and_then(|v| v.to_str().ok())
- .is_some_and(|value| value.starts_with("text/html")) {
- return Err("The URL returned a web page instead of a weight file. Use a direct download link.".into());
- }
- let total = response.content_length();
- let mut file = OpenOptions::new().write(true).create_new(true).open(&temporary).map_err(|error| error.to_string())?;
- let mut downloaded = 0u64;
- let mut buffer = vec![0u8; 1024 * 1024];
- let mut last_progress = Instant::now();
- progress(0, total);
- loop {
- if cancelled.load(Ordering::Relaxed) { return Err("Download cancelled.".into()); }
- let count = response.read(&mut buffer).map_err(|error| format!("Download interrupted: {error}"))?;
- if count == 0 { break; }
- file.write_all(&buffer[..count]).map_err(|error| format!("Could not write weights: {error}"))?;
- downloaded += count as u64;
- if last_progress.elapsed() >= Duration::from_millis(200) {
- progress(downloaded, total);
- last_progress = Instant::now();
- }
- }
- if downloaded == 0 || total.is_some_and(|total| downloaded != total) {
- return Err("The download is incomplete. Try downloading again.".into());
- }
- if cancelled.load(Ordering::Relaxed) { return Err("Download cancelled.".into()); }
- file.sync_all().map_err(|error| error.to_string())?;
- drop(file);
- // Only a successful, complete transfer receives the final filename.
- if destination.exists() {
- if cached_file(&destination, url) { return Ok(destination.to_string_lossy().into_owned()); }
- fs::remove_file(&destination).map_err(|error| error.to_string())?;
- }
- fs::rename(&temporary, &destination).map_err(|error| error.to_string())?;
- let record = CompletedFile { url: url.to_string(), bytes: downloaded };
- fs::write(destination.with_extension("complete.json"), serde_json::to_vec(&record).map_err(|error| error.to_string())?)
- .map_err(|error| error.to_string())?;
- progress(downloaded, Some(downloaded));
- Ok(destination.to_string_lossy().into_owned())
- })();
- let _ = fs::remove_file(&temporary);
- result
-}
-
#[tauri::command]
pub async fn download_weight(app: AppHandle, downloads: State<'_, WeightDownloads>, request_id: String, url: String) -> Result<String, String> {
let url = download_url(&url)?;
@@ -241,50 +79,6 @@ pub async fn weight_download_hardware() -> Result<Vec<crate::slopfab::GpuDevice>
tauri::async_runtime::spawn_blocking(hardware_devices).await.map_err(|error| error.to_string())
}
-pub(crate) fn hardware_devices() -> Vec<crate::slopfab::GpuDevice> {
- #[cfg(windows)]
- {
- use windows::Win32::Graphics::Dxgi::{CreateDXGIFactory1, IDXGIFactory1, DXGI_ADAPTER_FLAG_SOFTWARE};
- // DXGI reports dedicated VRAM for NVIDIA, AMD and Intel independently
- // of CUDA installation. COM interface wrappers release their references.
- let mut devices = Vec::new();
- unsafe {
- if let Ok(factory) = CreateDXGIFactory1::<IDXGIFactory1>() {
- let mut index = 0;
- while let Ok(adapter) = factory.EnumAdapters1(index) {
- index += 1;
- if let Ok(description) = adapter.GetDesc1() {
- if description.Flags & DXGI_ADAPTER_FLAG_SOFTWARE.0 as u32 != 0 { continue; }
- let end = description.Description.iter().position(|value| *value == 0).unwrap_or(description.Description.len());
- devices.push(crate::slopfab::GpuDevice {
- name: String::from_utf16_lossy(&description.Description[..end]),
- memory_bytes: description.DedicatedVideoMemory as u64,
- });
- }
- }
- }
- }
- if !devices.is_empty() { return devices; }
- }
- crate::slopfab::gpu_devices()
-}
-
-fn validate_removal(path: &Path, roots: &[PathBuf]) -> Result<(), String> {
- if !path.exists() { return Ok(()); }
- let canonical = path.canonicalize().map_err(|error| error.to_string())?;
- let allowed = roots.iter().filter_map(|root| root.canonicalize().ok())
- .any(|root| canonical.parent() == Some(root.as_path()));
- if !allowed { return Err("Only weights downloaded into Slopus's weights folders can be removed.".into()); }
- let record: CompletedFile = serde_json::from_slice(&fs::read(path.with_extension("complete.json"))
- .map_err(|_| "The downloaded weight's completion record is missing.".to_string())?)
- .map_err(|_| "The downloaded weight's completion record is invalid.".to_string())?;
- let url = download_url(&record.url)?;
- if path.file_name().and_then(|name| name.to_str()) != Some(destination_name(&url).as_str()) {
- return Err("The weight file does not match its download record.".into());
- }
- Ok(())
-}
-
#[tauri::command]
pub async fn remove_downloaded_weights(app: AppHandle, paths: Vec<String>) -> Result<(), String> {
tauri::async_runtime::spawn_blocking(move || {
@@ -303,157 +97,3 @@ pub async fn remove_downloaded_weights(app: AppHandle, paths: Vec<String>) -> Re
Ok(())
}).await.map_err(|error| error.to_string())?
}
-
-#[cfg(test)]
-mod tests {
- use super::*;
- use std::net::TcpListener;
-
- #[test]
- fn hardware_probe_reads_device_descriptions_without_a_toolkit() {
- let devices = hardware_devices();
- for device in &devices { assert!(!device.name.trim().is_empty()); }
- println!("Detected weight-selection GPUs: {devices:?}");
- }
-
- fn server(response: &'static [u8]) -> Url {
- let listener = TcpListener::bind("127.0.0.1:0").unwrap();
- let address = listener.local_addr().unwrap();
- std::thread::spawn(move || {
- let (mut connection, _) = listener.accept().unwrap();
- let mut request = [0u8; 8192];
- let _ = connection.read(&mut request);
- let _ = connection.write_all(response);
- });
- Url::parse(&format!("http://{address}/model.safetensors")).unwrap()
- }
-
- #[test]
- fn converts_hugging_face_pages_and_rejects_non_download_schemes() {
- assert_eq!(download_url("https://huggingface.co/org/repo/blob/main/vae/model.safetensors").unwrap().as_str(), "https://huggingface.co/org/repo/resolve/main/vae/model.safetensors");
- for url in ["file:///C:/secret", "ftp://example.com/model", "https://user:[email protected]/model"] { assert!(download_url(url).is_err()); }
- let url = download_url("https://example.com/../../model?file=../../oops").unwrap();
- assert!(!destination_name(&url).contains('/'));
- }
-
- #[test]
- fn falls_back_when_the_executable_directory_is_not_writable() {
- let folder = tempfile::tempdir().unwrap();
- let blocked = folder.path().join("blocked");
- fs::write(&blocked, b"file, not a directory").unwrap();
- assert_eq!(weights_directory(&[blocked.join("weights"), folder.path().join("weights")]).unwrap(), folder.path().join("weights"));
- }
-
- #[test]
- fn custom_folders_are_used_directly_in_order_with_default_fallbacks() {
- let folder = tempfile::tempdir().unwrap();
- let blocked = folder.path().join("blocked");
- fs::write(&blocked, b"not a directory").unwrap();
- let custom = folder.path().join("custom models");
- let primary = folder.path().join("exe/weights");
- let fallback = folder.path().join("logs/weights");
- let roots = weight_roots(&[blocked.clone(), custom.clone(), custom.clone()], primary.clone(), fallback.clone());
- assert_eq!(roots, vec![blocked, custom.clone(), primary.clone(), fallback.clone()]);
- let url = server(b"HTTP/1.1 200 OK\r\nContent-Length: 4\r\nConnection: close\r\n\r\ndata");
- let downloaded = transfer_from_roots(&url, &roots, &AtomicBool::new(false), |_, _| {}).unwrap();
- assert_eq!(Path::new(&downloaded).parent(), Some(custom.as_path()));
- assert!(!custom.join("weights").exists());
- assert!(!primary.exists());
- assert!(validate_removal(Path::new(&downloaded), &roots).is_ok());
- assert!(validate_removal(Path::new(&downloaded), &[primary.clone(), fallback.clone()]).is_err());
- assert_eq!(weight_roots(&[], primary.clone(), fallback.clone()), vec![primary, fallback]);
- }
-
- #[test]
- fn searches_all_folders_for_completed_files_before_creating_a_download_destination() {
- let folder = tempfile::tempdir().unwrap();
- let custom = folder.path().join("custom");
- let primary = folder.path().join("exe/weights");
- let fallback = folder.path().join("logs/weights");
- fs::create_dir_all(&fallback).unwrap();
- let roots = weight_roots(&[custom.clone()], primary.clone(), fallback.clone());
- let url = server(b"HTTP/1.1 200 OK\r\nContent-Length: 4\r\nConnection: close\r\n\r\ndata");
- let downloaded = transfer(&url, &fallback, &AtomicBool::new(false), |_, _| {}).unwrap();
- // The server accepts only one request; reuse must work without network access.
- assert_eq!(transfer_from_roots(&url, &roots, &AtomicBool::new(false), |_, _| {}).unwrap(), downloaded);
- assert!(!custom.exists());
- assert!(!primary.exists());
- fs::create_dir_all(&custom).unwrap();
- let moved = custom.join(destination_name(&url));
- fs::rename(&downloaded, &moved).unwrap();
- fs::rename(Path::new(&downloaded).with_extension("complete.json"), moved.with_extension("complete.json")).unwrap();
- assert_eq!(transfer_from_roots(&url, &roots, &AtomicBool::new(false), |_, _| {}).unwrap(), moved.to_string_lossy());
- 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();
- let url = server(b"HTTP/1.1 200 OK\r\nContent-Length: 4\r\nConnection: close\r\n\r\ndata");
- let path = transfer(&url, folder.path(), &AtomicBool::new(false), |_, _| {}).unwrap();
- assert_eq!(fs::read(&path).unwrap(), b"data");
- assert!(cached_file(Path::new(&path), &url));
- assert_eq!(transfer(&url, folder.path(), &AtomicBool::new(false), |_, _| {}).unwrap(), path);
- fs::remove_file(&path).unwrap();
- assert!(!cached_file(Path::new(&path), &url));
- }
-
- #[test]
- fn rejects_truncated_html_failed_and_cancelled_downloads_without_publishing_files() {
- for (response, cancelled) in [
- (&b"HTTP/1.1 200 OK\r\nContent-Length: 10\r\nConnection: close\r\n\r\ndata"[..], false),
- (&b"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: 4\r\nConnection: close\r\n\r\ndata"[..], false),
- (&b"HTTP/1.1 403 Forbidden\r\nContent-Length: 0\r\nConnection: close\r\n\r\n"[..], false),
- (&b"HTTP/1.1 200 OK\r\nContent-Length: 4\r\nConnection: close\r\n\r\ndata"[..], true),
- ] {
- let folder = tempfile::tempdir().unwrap();
- let url = server(response);
- assert!(transfer(&url, folder.path(), &AtomicBool::new(cancelled), |_, _| {}).is_err());
- assert_eq!(fs::read_dir(folder.path()).unwrap().count(), 0);
- }
- }
-
- #[test]
- fn removal_is_limited_to_files_created_by_the_downloader() {
- let folder = tempfile::tempdir().unwrap();
- let weights = folder.path().join("weights");
- fs::create_dir(&weights).unwrap();
- let external = folder.path().join("personal.safetensors");
- fs::write(&external, b"data").unwrap();
- assert!(validate_removal(&external, &[weights.clone()]).is_err());
- let url = server(b"HTTP/1.1 200 OK\r\nContent-Length: 4\r\nConnection: close\r\n\r\ndata");
- let downloaded = transfer(&url, &weights, &AtomicBool::new(false), |_, _| {}).unwrap();
- assert!(validate_removal(Path::new(&downloaded), &[weights.clone()]).is_ok());
- let unmanaged = weights.join("personal.safetensors");
- fs::write(&unmanaged, b"data").unwrap();
- assert!(validate_removal(&unmanaged, &[weights]).is_err());
- assert!(external.is_file());
- }
-}
diff --git a/src-tauri/src/weights/lora.rs b/src-tauri/src/weights/lora.rs
index 5b7193b..e436d28 100644
--- a/src-tauri/src/weights/lora.rs
+++ b/src-tauri/src/weights/lora.rs
@@ -1,46 +1,4 @@
-use super::*;
-
-/// Runs before inference, while no model reader can hold the adapter open.
-fn prepare(
- path: &Path,
- width: i32,
- allow_download: bool,
- model_access: &std::sync::RwLock<()>,
-) -> Result<(), String> {
- let _models = model_access
- .try_write()
- .map_err(|_| "Model files are in use. Prepare the LoRA after generation finishes.")?;
- prepare_exclusive(path, width, allow_download)
-}
-
-/// Preparation for a caller that already holds the model write lock. The LAN
-/// worker waits for that lock instead of failing while a job is running.
-pub(crate) fn prepare_exclusive(path: &Path, width: i32, allow_download: bool) -> Result<(), String> {
- // Slopfab owns format validation, asset selection and atomic embedding.
- // Preserve the URL record even on an idempotent retry after a record-write error.
- let record_path = path.with_extension("complete.json");
- let record = fs::read(&record_path)
- .ok()
- .and_then(|bytes| serde_json::from_slice::<CompletedFile>(&bytes).ok())
- .filter(|record| {
- download_url(&record.url).is_ok_and(|url| {
- path.file_name().and_then(|name| name.to_str())
- == Some(destination_name(&url).as_str())
- })
- });
- crate::slopfab::prepare_lora_grid(path, width, allow_download)?;
- if let Some(mut record) = record {
- record.bytes = fs::metadata(path).map_err(|error| error.to_string())?.len();
- fs::write(
- record_path,
- serde_json::to_vec(&record).map_err(|error| error.to_string())?,
- )
- .map_err(|error| {
- format!("LoRA prepared, but its download record could not be updated: {error}")
- })?;
- }
- Ok(())
-}
+use std::path::PathBuf;
#[tauri::command]
pub async fn prepare_lora(path: String, allow_download: bool) -> Result<(), String> {
@@ -50,67 +8,8 @@ pub async fn prepare_lora(path: String, allow_download: bool) -> Result<(), Stri
}
// All current Slopus generator profiles use the H3 2688-wide adapter grid.
tauri::async_runtime::spawn_blocking(move || {
- prepare(&path, 2688, allow_download, &crate::slopfab::MODEL_ACCESS)
+ slopus_core::weights::lora::prepare(&path, 2688, allow_download, &crate::slopfab::MODEL_ACCESS)
})
.await
.map_err(|error| error.to_string())?
}
-
-#[cfg(test)]
-mod tests {
- use super::*;
-
- fn fixture(path: &Path, tensor: &str, shape: &[usize]) {
- let bytes = shape.iter().product::<usize>() * 4;
- let header = serde_json::json!({ tensor: { "dtype": "F32", "shape": shape, "data_offsets": [0, bytes] } }).to_string();
- let mut data = (header.len() as u64).to_le_bytes().to_vec();
- data.extend_from_slice(header.as_bytes());
- data.resize(data.len() + bytes, 0);
- fs::write(path, data).unwrap();
- }
-
- #[test]
- fn preparation_preserves_download_discovery_and_is_idempotent() {
- let model_access = std::sync::RwLock::new(());
- let folder = tempfile::tempdir().unwrap();
- let url = download_url("https://example.com/adapter.safetensors").unwrap();
- let path = folder.path().join(destination_name(&url));
- fixture(&path, "weight", &[1]);
- let record = CompletedFile {
- url: url.to_string(),
- bytes: fs::metadata(&path).unwrap().len(),
- };
- fs::write(
- path.with_extension("complete.json"),
- serde_json::to_vec(&record).unwrap(),
- )
- .unwrap();
- let original = fs::read(&path).unwrap();
- assert!(prepare(&path, 2, false, &model_access).is_err());
- assert_eq!(fs::read(&path).unwrap(), original);
- assert!(cached_file(&path, &url));
- let companion = folder.path().join("h3_silu_temb_grid.safetensors");
- fixture(&companion, "silu_t_emb_grid", &[1025, 2]);
- prepare(&path, 2, false, &model_access).unwrap();
- let embedded = fs::read(&path).unwrap();
- assert!(embedded.len() > original.len());
- assert!(cached_file(&path, &url));
- assert_eq!(
- find_cached_weight(&url, &[folder.path().to_owned()]),
- Some(path.to_string_lossy().into_owned())
- );
- fs::remove_file(companion).unwrap();
- prepare(&path, 2, false, &model_access).unwrap();
- assert_eq!(fs::read(&path).unwrap(), embedded);
- assert!(validate_removal(&path, &[folder.path().to_owned()]).is_ok());
- }
-
- #[test]
- fn preparation_rejects_busy_model_files() {
- let model_access = std::sync::RwLock::new(());
- let _models = model_access.read().unwrap();
- assert!(prepare(Path::new("unused"), 2, false, &model_access)
- .unwrap_err()
- .contains("in use"));
- }
-}
diff --git a/src-tauri/src/worker/client.rs b/src-tauri/src/worker/client.rs
index 17968d4..34a786a 100644
--- a/src-tauri/src/worker/client.rs
+++ b/src-tauri/src/worker/client.rs
@@ -1,7 +1,7 @@
//! The desktop side of LAN workers: which worker is selected, how to reach it,
//! and running a generation there as if the native queue were local. The
//! webview keeps using the same commands and events either way.
-use super::{
+use slopus_core::worker::{
discovery::{self, Announced},
protocol::*,
};
@@ -10,8 +10,8 @@ use crate::{
project::ProviderSetting,
rendered,
slopfab::{self, GenerationRequest, GpuDevice},
- weights,
};
+use slopus_core::weights;
use reqwest::blocking::{Body, Client, Response};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use std::{
diff --git a/src-tauri/src/worker/mod.rs b/src-tauri/src/worker/mod.rs
index e167cf7..ef518f4 100644
--- a/src-tauri/src/worker/mod.rs
+++ b/src-tauri/src/worker/mod.rs
@@ -1,14 +1,5 @@
-//! LAN generation workers. A worker is this crate's generation queue run
-//! headless on another computer (`slopus-worker`); Slopus finds it with mDNS
-//! and sends it jobs. Weights with download URLs are fetched by the worker
-//! itself, local-only files are uploaded, and finished frames and latents
-//! come back to the project as if the job had run here.
+//! The desktop side of LAN workers. The protocol and the worker itself are in
+//! slopus-core, which `slopus-worker` uses without any UI framework.
mod client;
-mod discovery;
-mod protocol;
-mod server;
-mod store;
pub use client::{WorkerList, Workers};
-pub use protocol::{WorkerRuntime, DEFAULT_PORT};
-pub use server::{run, WorkerOptions};
diff --git a/src-tauri/worker/Cargo.toml b/src-tauri/worker/Cargo.toml
new file mode 100644
index 0000000..098db28
--- /dev/null
+++ b/src-tauri/worker/Cargo.toml
@@ -0,0 +1,15 @@
+[package]
+name = "slopus-worker"
+version.workspace = true
+description = "Headless Slopus generation worker for the local network."
+authors = ["Slopus"]
+edition = "2021"
+rust-version = "1.87"
+
+[dependencies]
+# Only the shared runtime: no UI framework at build or run time.
+slopus-core = { path = "../core" }
+
+[target.'cfg(windows)'.build-dependencies]
+# Windows file version and product name, matching Slopus.exe.
+tauri-winres = "0.3"
diff --git a/src-tauri/worker/build.rs b/src-tauri/worker/build.rs
new file mode 100644
index 0000000..771bac8
--- /dev/null
+++ b/src-tauri/worker/build.rs
@@ -0,0 +1,13 @@
+fn main() {
+ // Same version and product name as Slopus.exe, from the shared workspace version.
+ #[cfg(windows)]
+ {
+ let mut resource = tauri_winres::WindowsResource::new();
+ resource.set("ProductName", "Slopus");
+ resource.set("FileDescription", "Slopus LAN worker");
+ resource.set_icon("../icons/icon.ico");
+ if let Err(error) = resource.compile() {
+ panic!("Could not embed the worker's Windows resources: {error}");
+ }
+ }
+}
diff --git a/src-tauri/src/bin/slopus-worker.rs b/src-tauri/worker/src/main.rs
similarity index 95%
rename from src-tauri/src/bin/slopus-worker.rs
rename to src-tauri/worker/src/main.rs
index ffa16b9..bf2f66d 100644
--- a/src-tauri/src/bin/slopus-worker.rs
+++ b/src-tauri/worker/src/main.rs
@@ -3,7 +3,7 @@
//! Runs the same generation code as the desktop app and announces itself with
//! mDNS, so Slopus lists it under Settings → Workers. Place it beside
//! slopfab.dll, like Slopus.exe.
-use slopus_lib::{run_worker, WorkerOptions, DEFAULT_WORKER_PORT};
+use slopus_core::worker::{run as run_worker, WorkerOptions, DEFAULT_PORT as DEFAULT_WORKER_PORT};
use std::path::PathBuf;
const USAGE: &str = r"Usage: slopus-worker [options]
diff --git a/src/lib/settings.ts b/src/lib/settings.ts
index 9513fac..13c3a18 100644
--- a/src/lib/settings.ts
+++ b/src/lib/settings.ts
@@ -9,7 +9,7 @@
* disk layout into a file the user is invited to move, copy, and share.
*
* The option names below are the contract with `Configuration::from_settings`
- * in src-tauri/src/slopfab/config.rs. Renaming one here without renaming it there
+ * in src-tauri/core/src/slopfab/config.rs. Renaming one here without renaming it there
* silently drops that path.
*/
import { DEFAULT_GENERATION_STEPS, MAX_GENERATION_STEPS, type ProjectConfig, type ProviderSetting } from "./project";