AI media harness for planning, generating, and editing locally
TypeScript
Add LAN generation worker and client routing
Bitti5be2216869d5
diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lockindex ccf61a5..38ecc1a 100644--- a/src-tauri/Cargo.lock+++ b/src-tauri/Cargo.lock@@ -56,6 +56,12 @@ dependencies = ["derive_arbitrary",]+[[package]]+name = "ascii"+version = "1.1.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "d92bec98840b8f03a5ff5413de5293bfcd8bf96467cf5452609f939ec6f5de16"+[[package]]name = "atk"version = "0.18.2"@@ -353,6 +359,12 @@ dependencies = ["windows-link 0.2.1",]+[[package]]+name = "chunked_transfer"+version = "1.5.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "6e4de3bc4ea267985becf712dc6d9eed8b04c953b3fcfb339ebc87acd9804901"+[[package]]name = "combine"version = "4.6.7"@@ -872,6 +884,17 @@ dependencies = ["miniz_oxide",]+[[package]]+name = "flume"+version = "0.12.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "5e139bc46ca777eb5efaf62df0ab8cc5fd400866427e56c68b22e414e53bd3be"+dependencies = [+ "futures-core",+ "futures-sink",+ "spin",+]+[[package]]name = "fnv"version = "1.0.7"@@ -1367,6 +1390,12 @@ version = "1.10.1"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87"+[[package]]+name = "httpdate"+version = "1.0.3"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9"+[[package]]name = "hyper"version = "1.11.0"@@ -1572,6 +1601,16 @@ dependencies = ["icu_properties",]+[[package]]+name = "if-addrs"+version = "0.15.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "c0a05c691e1fae256cf7013d99dad472dc52d5543322761f83ec8d47eab40d2b"+dependencies = [+ "libc",+ "windows-sys 0.61.2",+]+[[package]]name = "image"version = "0.25.8"@@ -1943,6 +1982,21 @@ dependencies = ["web_atoms",]+[[package]]+name = "mdns-sd"+version = "0.21.4"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "1067efe2aadc6967f84c95977dca910e98f3edfd6403efc8fa8508d289ce012d"+dependencies = [+ "fastrand",+ "flume",+ "if-addrs",+ "log",+ "mio",+ "socket-pktinfo",+ "socket2",+]+[[package]]name = "memchr"version = "2.8.3"@@ -1987,6 +2041,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "30d65c71f1ce40ab09135ce117d742b9f8a19ff91a41a8b57ed50bc2de59c427"dependencies = ["libc",+ "log","wasi","windows-sys 0.61.2",]@@ -3366,6 +3421,7 @@ dependencies = ["image","jpeg-encoder","libloading 0.8.9",+ "mdns-sd","reqwest 0.12.28","serde","serde_json",@@ -3376,6 +3432,7 @@ dependencies = ["tauri-plugin-updater","tauri-plugin-window-state","tempfile",+ "tiny_http","tokio","webview2-com","windows",@@ -3388,6 +3445,17 @@ version = "1.15.2"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "8ed6a63f02c8539c91a8685a86f4099661ba3da017932f6ebbea6de3f0fa7c90"+[[package]]+name = "socket-pktinfo"+version = "0.4.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "612942246d0cc239cfd83af1dfd39be47f649208a3524e5e9da651910128e0ac"+dependencies = [+ "libc",+ "socket2",+ "windows-sys 0.61.2",+]+[[package]]name = "socket2"version = "0.6.5"@@ -3446,6 +3514,15 @@ dependencies = ["system-deps",]+[[package]]+name = "spin"+version = "0.9.9"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "3763264f6b73151db08c50ff20d7d8a0b8796e021cdea7ceedad07b80155fa0e"+dependencies = [+ "lock_api",+]+[[package]]name = "stable_deref_trait"version = "1.2.1"@@ -4074,6 +4151,18 @@ dependencies = ["time-core",]+[[package]]+name = "tiny_http"+version = "0.12.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "389915df6413a2e74fb181895f933386023c71110878cd0825588928e64cdc82"+dependencies = [+ "ascii",+ "chunked_transfer",+ "httpdate",+ "log",+]+[[package]]name = "tinystr"version = "0.8.4"diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.tomlindex e83de67..d4c99ad 100644--- a/src-tauri/Cargo.toml+++ b/src-tauri/Cargo.toml@@ -31,6 +31,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"+mdns-sd = "0.21"[target.'cfg(windows)'.dependencies]# Must stay on the same major as wry's, or with_webview hands back a controllerdiff --git a/src-tauri/src/app.rs b/src-tauri/src/app.rsindex 4e4c15d..a6b901d 100644--- a/src-tauri/src/app.rs+++ b/src-tauri/src/app.rs@@ -1,7 +1,7 @@use crate::window::{CloseDecision, ExitGuard};use crate::{agent, app_paths, app_settings, commands, cuda_support, diagnostics, export, native_shell,- slopfab, weights, window,+ slopfab, weights, window, worker,};use std::io;/// Portable distributions carry a marker because Windows updates launch an@@ -90,6 +90,7 @@ pub fn run() {),Err(error) => eprintln!("Could not initialize diagnostic logging: {error}"),}+ app.state::<worker::Workers>().start(app.handle(), &app_paths::data_directory(app.handle()));window::apply_theme(&window);window.show()?;let _ = window.set_focus();@@ -100,6 +101,7 @@ pub fn run() {.manage(agent::AgentRuntime::default()).manage(slopfab::SlopfabRuntime::default()).manage(weights::WeightDownloads::default())+ .manage(worker::Workers::default()).manage(ExitGuard::default()).manage(native_shell::Backdrop::default()).manage(native_shell::SystemAccent::default())@@ -215,6 +217,11 @@ pub fn run() {commands::generation::resolve_slopfab_plan,commands::generation::enqueue_slopfab_generation,commands::generation::cancel_slopfab_generation,+ commands::workers::list_workers,+ commands::workers::select_worker,+ commands::workers::add_worker,+ commands::workers::remove_worker,+ commands::workers::set_worker_token,window::set_generation_active,window::answer_app_close,native_shell::system_accent_colors,diff --git a/src-tauri/src/bin/slopus-worker.rs b/src-tauri/src/bin/slopus-worker.rsnew file mode 100644index 0000000..ffa16b9--- /dev/null+++ b/src-tauri/src/bin/slopus-worker.rs@@ -0,0 +1,51 @@+//! Headless generation worker for Slopus on the local network.+//!+//! 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 std::path::PathBuf;++const USAGE: &str = r"Usage: slopus-worker [options]++ --port <number> TCP port to listen on (default 47321)+ --name <text> Name shown in Slopus (default: computer name)+ --token <text> Require this access token from clients+ --data <folder> Uploads, job files and logs (default: %LOCALAPPDATA%\Slopus Worker)+ --weights <folder> Extra weights folder to search and download into; repeatable+ --vulkan Use the Vulkan backend instead of CUDA+ --no-mdns Do not announce on the network; clients add it by address+ --help Show this help";++fn parse() -> Result<WorkerOptions, String> {+ let mut options = WorkerOptions { port: DEFAULT_WORKER_PORT, ..Default::default() };+ let mut args = std::env::args().skip(1);+ while let Some(arg) = args.next() {+ let mut value = |name: &str| args.next().ok_or_else(|| format!("{name} needs a value."));+ match arg.as_str() {+ "--port" => options.port = value("--port")?.parse().map_err(|_| "--port needs a number.")?,+ "--name" => options.name = Some(value("--name")?),+ "--token" => options.token = Some(value("--token")?),+ "--data" => options.data = Some(PathBuf::from(value("--data")?)),+ "--weights" => {+ let folder = std::path::absolute(value("--weights")?).map_err(|error| error.to_string())?;+ options.weights.push(folder);+ }+ "--vulkan" => options.vulkan = true,+ "--no-mdns" => options.advertise = false,+ "--help" | "-h" => {+ println!("{USAGE}");+ std::process::exit(0);+ }+ other => return Err(format!("Unknown option {other}.\n\n{USAGE}")),+ }+ }+ Ok(options)+}++fn main() {+ if let Err(error) = parse().and_then(run_worker) {+ eprintln!("{error}");+ std::process::exit(1);+ }+}diff --git a/src-tauri/src/commands/artifacts.rs b/src-tauri/src/commands/artifacts.rsindex 7bcd5f1..6875bf8 100644--- a/src-tauri/src/commands/artifacts.rs+++ b/src-tauri/src/commands/artifacts.rs@@ -391,7 +391,8 @@ pub(crate) fn save_builtin_reference_icon(/// One frame of a finished render, as RGBA the webview can build a `VideoFrame`/// from. A frame at a time because a whole render is hundreds of megabytes and/// the encoder only ever wants the next one.-#[tauri::command]+// Off the main thread: a worker render fetches its frames over the network.+#[tauri::command(async)]pub(crate) fn generated_frame(job_id: String, index: u32) -> Result<tauri::ipc::Response, String> {rendered::frame(&job_id, index)?.map(tauri::ipc::Response::new)@@ -402,7 +403,8 @@ pub(crate) fn generated_frame(job_id: String, index: u32) -> Result<tauri::ipc::/// The render's soundtrack, interleaved little-endian f32 — one read, because/// even fifteen seconds of stereo is a couple of megabytes.-#[tauri::command]+// Off the main thread: a worker render fetches its frames over the network.+#[tauri::command(async)]pub(crate) fn generated_audio(job_id: String) -> Result<tauri::ipc::Response, String> {rendered::audio(&job_id).map(tauri::ipc::Response::new)diff --git a/src-tauri/src/commands/generation.rs b/src-tauri/src/commands/generation.rsindex 709b8fe..17b808f 100644--- a/src-tauri/src/commands/generation.rs+++ b/src-tauri/src/commands/generation.rs@@ -1,17 +1,38 @@use crate::media::artifacts::*;use crate::project::{validation::validate_and_normalize_config, *};-use crate::{diagnostics, slopfab};+use crate::{diagnostics, slopfab, worker::Workers};use tauri::{AppHandle, Emitter};+/// Generation commands run on the native queue here, or on the LAN worker+/// selected in Settings → Workers. The webview cannot tell the difference.+async fn blocking<T: Send + 'static>(+ work: impl FnOnce() -> Result<T, String> + Send + 'static,+) -> Result<T, String> {+ tauri::async_runtime::spawn_blocking(work)+ .await+ .map_err(|error| error.to_string())?+}+#[tauri::command]-pub(crate) fn create_reference_video(+pub(crate) async fn create_reference_video(state: tauri::State<'_, slopfab::SlopfabRuntime>,+ workers: tauri::State<'_, Workers>,duration_seconds: f64,config: ProjectConfig,) -> Result<String, String> {- let config = validate_and_normalize_config(config)?;- state- .references- .create_reference_video(duration_seconds, &config.provider_settings)+ let (runtime, workers) = (state.inner().clone(), workers.inner().clone());+ blocking(move || {+ let config = validate_and_normalize_config(config)?;+ match workers.active()? {+ Some(worker) => worker.post(+ "/v1/reference-videos",+ &serde_json::json!({ "durationSeconds": duration_seconds }),+ ),+ None => runtime+ .references+ .create_reference_video(duration_seconds, &config.provider_settings),+ }+ })+ .await}pub(crate) fn reference_video_header<'a>(@@ -26,132 +47,186 @@ pub(crate) fn reference_video_header<'a>(}#[tauri::command]-pub(crate) fn append_reference_video(+pub(crate) async fn append_reference_video(state: tauri::State<'_, slopfab::SlopfabRuntime>,+ workers: tauri::State<'_, Workers>,request: tauri::ipc::Request<'_>,) -> Result<(), String> {let tauri::ipc::InvokeBody::Raw(bytes) = request.body() else {return Err("Reference frames require a binary body.".into());};- let id = reference_video_header(&request, "x-reference-id")?;- let width = reference_video_header(&request, "x-reference-width")?+ let id = reference_video_header(&request, "x-reference-id")?.to_string();+ let width: i32 = reference_video_header(&request, "x-reference-width")?.parse().map_err(|_| "Invalid frame width.")?;- let height = reference_video_header(&request, "x-reference-height")?+ let height: i32 = reference_video_header(&request, "x-reference-height")?.parse().map_err(|_| "Invalid frame height.")?;- let timestamp = reference_video_header(&request, "x-reference-time")?+ let timestamp: f64 = reference_video_header(&request, "x-reference-time")?.parse().map_err(|_| "Invalid frame timestamp.")?;- state- .references- .append_reference_video(id, bytes, width, height, timestamp)+ let bytes = bytes.clone();+ let (runtime, workers) = (state.inner().clone(), workers.inner().clone());+ blocking(move || match workers.active()? {+ Some(worker) => worker.post_bytes(+ &format!("/v1/reference-videos/{id}/frames"),+ &[+ ("x-reference-width", width.to_string()),+ ("x-reference-height", height.to_string()),+ ("x-reference-time", timestamp.to_string()),+ ],+ bytes,+ ),+ None => runtime+ .references+ .append_reference_video(&id, &bytes, width, height, timestamp),+ })+ .await}#[tauri::command]-pub(crate) fn set_reference_video_audio(+pub(crate) async fn set_reference_video_audio(state: tauri::State<'_, slopfab::SlopfabRuntime>,+ workers: tauri::State<'_, Workers>,request: tauri::ipc::Request<'_>,) -> Result<(), String> {let tauri::ipc::InvokeBody::Raw(bytes) = request.body() else {return Err("Reference audio requires a binary body.".into());};- let id = reference_video_header(&request, "x-reference-id")?;- let channels = reference_video_header(&request, "x-reference-channels")?+ let id = reference_video_header(&request, "x-reference-id")?.to_string();+ let channels: i32 = reference_video_header(&request, "x-reference-channels")?.parse().map_err(|_| "Invalid audio channels.")?;- let rate = reference_video_header(&request, "x-reference-rate")?+ let rate: i32 = reference_video_header(&request, "x-reference-rate")?.parse().map_err(|_| "Invalid sample rate.")?;- state- .references- .set_reference_video_audio(id, bytes, channels, rate)+ let bytes = bytes.clone();+ let (runtime, workers) = (state.inner().clone(), workers.inner().clone());+ blocking(move || match workers.active()? {+ Some(worker) => worker.post_bytes(+ &format!("/v1/reference-videos/{id}/audio"),+ &[+ ("x-reference-channels", channels.to_string()),+ ("x-reference-rate", rate.to_string()),+ ],+ bytes,+ ),+ None => runtime+ .references+ .set_reference_video_audio(&id, &bytes, channels, rate),+ })+ .await}#[tauri::command]-pub(crate) fn release_reference_videos(+pub(crate) async fn release_reference_videos(state: tauri::State<'_, slopfab::SlopfabRuntime>,+ workers: tauri::State<'_, Workers>,ids: Vec<String>,) -> Result<(), String> {- state.references.release_reference_videos(&ids)+ let (runtime, workers) = (state.inner().clone(), workers.inner().clone());+ blocking(move || match workers.active()? {+ Some(worker) => worker+ .post::<bool>("/v1/reference-videos/release", &ids)+ .map(|_| ()),+ None => runtime.references.release_reference_videos(&ids),+ })+ .await}#[tauri::command]-pub(crate) fn resolve_slopfab_plan(+pub(crate) async fn resolve_slopfab_plan(state: tauri::State<'_, slopfab::SlopfabRuntime>,+ workers: tauri::State<'_, Workers>,mut request: slopfab::GenerationRequest,config: ProjectConfig,folder_path: Option<String>,-) -> Result<slopfab::ResolvedPlan, String> {- prepare_continuation_path(&mut request, folder_path.as_deref())?;- prepare_image_edit_path(&mut request, folder_path.as_deref())?;- let context = serde_json::json!({- "jobId": request.job_id, "frames": request.frames, "steps": request.steps,- "canvasWidth": request.canvas_width, "canvasHeight": request.canvas_height,- "referenceCount": request.reference_count(), "refmodCount": request.refmod_count(), "inputChars": request.prompt.chars().count(),- });- diagnostics::info(- "slopfab",- "plan.requested",- "Generation plan requested.",- context.clone(),- );- let result = validate_and_normalize_config(config).and_then(|config| {- slopfab::resolve_plan(&request, &config.provider_settings, &state.references)- });- match &result {- Ok(plan) => diagnostics::info(+) -> Result<serde_json::Value, String> {+ let (runtime, workers) = (state.inner().clone(), workers.inner().clone());+ blocking(move || {+ prepare_continuation_path(&mut request, folder_path.as_deref())?;+ prepare_image_edit_path(&mut request, folder_path.as_deref())?;+ let context = serde_json::json!({+ "jobId": request.job_id, "frames": request.frames, "steps": request.steps,+ "canvasWidth": request.canvas_width, "canvasHeight": request.canvas_height,+ "referenceCount": request.reference_count(), "refmodCount": request.refmod_count(), "inputChars": request.prompt.chars().count(),+ });+ diagnostics::info("slopfab",- "plan.resolved",- "Generation plan resolved.",- serde_json::json!({- "jobId": request.job_id, "frames": plan.aligned_frames,- "canvasWidth": plan.canvas_width, "canvasHeight": plan.canvas_height,- "durationSeconds": plan.duration_seconds, "modelEvaluations": plan.model_evaluations,- }),- ),- Err(error) => diagnostics::error("slopfab", "plan.failed", error, context),- }- result+ "plan.requested",+ "Generation plan requested.",+ context.clone(),+ );+ let result = validate_and_normalize_config(config).and_then(|config| {+ match workers.active()? {+ Some(worker) => workers.plan(&worker, &request, &config.provider_settings),+ None => slopfab::resolve_plan(&request, &config.provider_settings, &runtime.references)+ .and_then(|plan| serde_json::to_value(plan).map_err(|error| error.to_string())),+ }+ });+ match &result {+ Ok(plan) => diagnostics::info(+ "slopfab",+ "plan.resolved",+ "Generation plan resolved.",+ serde_json::json!({+ "jobId": request.job_id, "frames": plan["alignedFrames"],+ "canvasWidth": plan["canvasWidth"], "canvasHeight": plan["canvasHeight"],+ "durationSeconds": plan["durationSeconds"], "modelEvaluations": plan["modelEvaluations"],+ }),+ ),+ Err(error) => diagnostics::error("slopfab", "plan.failed", error, context),+ }+ result+ })+ .await}#[tauri::command]-pub(crate) fn enqueue_slopfab_generation(+pub(crate) async fn enqueue_slopfab_generation(app: AppHandle,state: tauri::State<'_, slopfab::SlopfabRuntime>,+ workers: tauri::State<'_, Workers>,mut request: slopfab::GenerationRequest,config: ProjectConfig,folder_path: String,) -> Result<(), String> {- prepare_continuation_path(&mut request, Some(&folder_path))?;- prepare_image_edit_path(&mut request, Some(&folder_path))?;- if !request.still_image {- request.save_latents_path = Some(generated_latent_destination(&folder_path, &request.job_id)?);- }- let job_id = request.job_id.clone();- let result = validate_and_normalize_config(config).and_then(|config| {- state.enqueue(- std::sync::Arc::new(move |event| match event {- slopfab::NativeEvent::Job(value) => {- let _ = app.emit("slopfab-job", value);- }- slopfab::NativeEvent::Progress(value) => {- let _ = app.emit("slopfab-progress", value);- }- }),- request,- &config.provider_settings,- )- });- if let Err(error) = &result {- diagnostics::error(- "slopfab",- "generation.enqueue_failed",- error,- serde_json::json!({ "jobId": job_id }),- );- }- result+ let (runtime, workers) = (state.inner().clone(), workers.inner().clone());+ blocking(move || {+ prepare_continuation_path(&mut request, Some(&folder_path))?;+ prepare_image_edit_path(&mut request, Some(&folder_path))?;+ if !request.still_image {+ request.save_latents_path = Some(generated_latent_destination(&folder_path, &request.job_id)?);+ }+ let job_id = request.job_id.clone();+ let result = validate_and_normalize_config(config).and_then(|config| {+ match workers.active()? {+ Some(worker) => workers.enqueue(worker, request, &config.provider_settings),+ None => runtime.enqueue(+ std::sync::Arc::new(move |event| match event {+ slopfab::NativeEvent::Job(value) => {+ let _ = app.emit("slopfab-job", value);+ }+ slopfab::NativeEvent::Progress(value) => {+ let _ = app.emit("slopfab-progress", value);+ }+ }),+ request,+ &config.provider_settings,+ ),+ }+ });+ if let Err(error) = &result {+ diagnostics::error(+ "slopfab",+ "generation.enqueue_failed",+ error,+ serde_json::json!({ "jobId": job_id }),+ );+ }+ result+ })+ .await}fn prepare_image_edit_path(request: &mut slopfab::GenerationRequest, folder_path: Option<&str>) -> Result<(), String> {@@ -168,9 +243,12 @@ fn prepare_image_edit_path(request: &mut slopfab::GenerationRequest, folder_path}#[tauri::command]-pub(crate) fn cancel_slopfab_generation(+pub(crate) async fn cancel_slopfab_generation(state: tauri::State<'_, slopfab::SlopfabRuntime>,+ workers: tauri::State<'_, Workers>,job_id: String,-) -> bool {- state.cancel(&job_id)+) -> Result<bool, String> {+ let (runtime, workers) = (state.inner().clone(), workers.inner().clone());+ // A job sent to a worker is cancelled there, whichever worker is selected now.+ blocking(move || Ok(workers.cancel(&job_id) || runtime.cancel(&job_id))).await}diff --git a/src-tauri/src/commands/mod.rs b/src-tauri/src/commands/mod.rsindex e557842..d6a4934 100644--- a/src-tauri/src/commands/mod.rs+++ b/src-tauri/src/commands/mod.rs@@ -6,3 +6,4 @@ pub(crate) mod generation;pub(crate) mod media;pub(crate) mod project;pub(crate) mod runtime;+pub(crate) mod workers;diff --git a/src-tauri/src/commands/runtime.rs b/src-tauri/src/commands/runtime.rsindex 4575416..d987966 100644--- a/src-tauri/src/commands/runtime.rs+++ b/src-tauri/src/commands/runtime.rs@@ -1,5 +1,5 @@use crate::project::{paths::*, *};-use crate::{agent, diagnostics, slopfab};+use crate::{agent, diagnostics, slopfab, worker::Workers};use serde::Serialize;use std::collections::BTreeMap;use tauri::AppHandle;@@ -14,11 +14,29 @@ pub(crate) struct RuntimeStatus {/// Probes the video engine from provider settings alone. The settings screen/// has no project in hand — engine paths belong to the machine, not to a/// project file — so it cannot go through `runtime_status`.+/// The selected worker's engine when one is chosen in Settings → Workers.+fn engine_status(workers: &Workers, settings: &BTreeMap<String, ProviderSetting>) -> slopfab::SlopfabStatus {+ match workers.active() {+ Ok(Some(worker)) => workers.status(&worker, settings),+ Ok(None) => slopfab::status(settings),+ Err(error) => {+ let mut status = slopfab::status(settings);+ status.state = "runtimeMissing";+ status.detail = error;+ status+ }+ }+}+#[tauri::command]-pub(crate) fn slopfab_status(+pub(crate) async fn slopfab_status(+ workers: tauri::State<'_, Workers>,settings: BTreeMap<String, ProviderSetting>,-) -> slopfab::SlopfabStatus {- let status = slopfab::status(&settings);+) -> Result<slopfab::SlopfabStatus, String> {+ let workers = workers.inner().clone();+ let status = tauri::async_runtime::spawn_blocking(move || engine_status(&workers, &settings))+ .await+ .map_err(|error| error.to_string())?;diagnostics::info("runtime","slopfab.probed",@@ -32,7 +50,7 @@ pub(crate) fn slopfab_status(})).collect::<Vec<_>>(),}),);- status+ Ok(status)}/// One OS picker for one engine path. `directory` picks a folder (the@@ -76,12 +94,14 @@ pub(crate) fn choose_engine_path(#[tauri::command]pub(crate) async fn runtime_status(discovery: tauri::State<'_, agent::ProviderDiscovery>,+ workers: tauri::State<'_, Workers>,settings: BTreeMap<String, ProviderSetting>,) -> Result<RuntimeStatus, String> {let discovery = discovery.inner().clone();+ let workers = workers.inner().clone();let result = tauri::async_runtime::spawn_blocking(move || RuntimeStatus {providers: agent::provider_statuses(&discovery, &settings),- slopfab: slopfab::status(&settings),+ slopfab: engine_status(&workers, &settings),}).await.map_err(|error| format!("Could not probe this computer: {error}"));diff --git a/src-tauri/src/commands/workers.rs b/src-tauri/src/commands/workers.rsnew file mode 100644index 0000000..c7fd650--- /dev/null+++ b/src-tauri/src/commands/workers.rs@@ -0,0 +1,51 @@+//! Settings → Workers: the LAN workers this computer can generate on.+use crate::worker::{WorkerList, Workers};++async fn run<T: Send + 'static>(+ workers: &Workers,+ work: impl FnOnce(&Workers) -> Result<T, String> + Send + 'static,+) -> Result<T, String> {+ let workers = workers.clone();+ tauri::async_runtime::spawn_blocking(move || work(&workers))+ .await+ .map_err(|error| error.to_string())?+}++#[tauri::command]+pub(crate) async fn list_workers(workers: tauri::State<'_, Workers>) -> Result<WorkerList, String> {+ run(&workers, |workers| Ok(workers.list())).await+}++/// `None` generates on this computer.+#[tauri::command]+pub(crate) async fn select_worker(+ workers: tauri::State<'_, Workers>,+ id: Option<String>,+) -> Result<WorkerList, String> {+ run(&workers, move |workers| workers.select(id)).await+}++#[tauri::command]+pub(crate) async fn add_worker(+ workers: tauri::State<'_, Workers>,+ address: String,+) -> Result<WorkerList, String> {+ run(&workers, move |workers| workers.add(&address)).await+}++#[tauri::command]+pub(crate) async fn remove_worker(+ workers: tauri::State<'_, Workers>,+ key: String,+) -> Result<WorkerList, String> {+ run(&workers, move |workers| Ok(workers.remove(&key))).await+}++#[tauri::command]+pub(crate) async fn set_worker_token(+ workers: tauri::State<'_, Workers>,+ id: String,+ token: String,+) -> Result<WorkerList, String> {+ run(&workers, move |workers| Ok(workers.set_token(&id, &token))).await+}diff --git a/src-tauri/src/diagnostics.rs b/src-tauri/src/diagnostics.rsindex 565055a..16cc410 100644--- a/src-tauri/src/diagnostics.rs+++ b/src-tauri/src/diagnostics.rs@@ -290,8 +290,13 @@ pub fn directory(app: &AppHandle) -> PathBuf {}pub fn initialize(app: &AppHandle) -> Result<DiagnosticLogInfo, String> {- let directory = directory(app);- let writer = LogWriter::new(&directory, MAX_BYTES)?;+ 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))diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rsindex 659d631..e865f26 100644--- a/src-tauri/src/lib.rs+++ b/src-tauri/src/lib.rs@@ -17,8 +17,10 @@ mod slopfab;mod storage;mod weights;mod window;+mod worker;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,};diff --git a/src-tauri/src/rendered.rs b/src-tauri/src/rendered.rsindex 1fac346..bdcb54c 100644--- a/src-tauri/src/rendered.rs+++ b/src-tauri/src/rendered.rs@@ -337,7 +337,7 @@ pub fn audio(job_id: &str) -> Option<Vec<u8>> {/// The event is a notification — it can be missed, it can arrive at a window/// that is reloading — and the only authority on what can still be encoded is/// what is still held.-#[derive(Debug, Clone, PartialEq, serde::Serialize)]+#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]#[serde(rename_all = "camelCase")]pub struct RenderedSummary {pub job_id: String,diff --git a/src-tauri/src/slopfab/mod.rs b/src-tauri/src/slopfab/mod.rsindex 04bd21c..d9a0a5a 100644--- a/src-tauri/src/slopfab/mod.rs+++ b/src-tauri/src/slopfab/mod.rs@@ -28,7 +28,7 @@ pub use types::*;pub use events::NativeEvent;pub use icons::{generate_reference_icon_batch, ReferenceIconBatchConfig, ReferenceIconSpec};pub use planning::resolve_plan;-pub use status::status;+pub use status::{status, worker_status};const DLL_FILE_NAME: &str = "slopfab.dll";/// Tauri stages the repository's runtime resources in development and release/// builds. Installers and portable folders use the same relative layout.diff --git a/src-tauri/src/slopfab/status.rs b/src-tauri/src/slopfab/status.rsindex 0bb035f..f2155a3 100644--- a/src-tauri/src/slopfab/status.rs+++ b/src-tauri/src/slopfab/status.rs@@ -2,8 +2,7 @@ use super::{config::Configuration, ffi, platform::*, types::*};use crate::project::ProviderSetting;use std::{collections::BTreeMap, path::Path};-pub fn status(settings: &BTreeMap<String, ProviderSetting>) -> SlopfabStatus {- let configuration = Configuration::from_settings(settings);+fn model_statuses(configuration: &Configuration, available: impl Fn(&Path) -> bool) -> Vec<ModelStatus> {let mut models = configuration.models.iter()@@ -11,7 +10,7 @@ pub fn status(settings: &BTreeMap<String, ProviderSetting>) -> SlopfabStatus {.map(|(_, id, path)| ModelStatus {id,configured: path.is_some(),- available: path.as_deref().is_some_and(Path::is_file),+ available: path.as_deref().is_some_and(&available),path: path.as_ref().map(|value| value.to_string_lossy().into_owned()),@@ -24,13 +23,19 @@ pub fn status(settings: &BTreeMap<String, ProviderSetting>) -> SlopfabStatus {available: configuration.prompt_embedding.as_deref()- .is_some_and(Path::is_file),+ .is_some_and(&available),path: configuration.prompt_embedding.as_ref().map(|path| path.to_string_lossy().into_owned()),});}+ models+}++pub fn status(settings: &BTreeMap<String, ProviderSetting>) -> SlopfabStatus {+ let configuration = Configuration::from_settings(settings);+ let models = model_statuses(&configuration, Path::is_file);let dll_path = configuration.dll_path.to_string_lossy().into_owned();match ffi::Api::load(&configuration.dll_path) {Ok(api) => match api.version() {@@ -83,3 +88,39 @@ pub fn status(settings: &BTreeMap<String, ProviderSetting>) -> SlopfabStatus {},}}++/// Status for generation on a LAN worker. The worker reports its own engine;+/// a model counts as available when this computer can upload it or the+/// worker can download it.+pub fn worker_status(+ settings: &BTreeMap<String, ProviderSetting>,+ runtime: &crate::worker::WorkerRuntime,+ location: String,+) -> SlopfabStatus {+ let configuration = Configuration::from_settings(settings);+ let models = model_statuses(&configuration, |path| {+ let value = path.to_string_lossy();+ value.starts_with("https://") || value.starts_with("http://") || path.exists()+ });+ let missing = models.iter().filter(|model| !model.available && model.id != "tokenizer").count();+ let state = match runtime.state.as_str() {+ "ready" if missing == 0 => "ready",+ "ready" => "modelsMissing",+ "incompatible" => "incompatible",+ _ => "runtimeMissing",+ };+ SlopfabStatus {+ state,+ dll_path: location,+ version: runtime.version.clone(),+ platform: ["CUDA 13", "CUDA 12", "Vulkan"].into_iter().find(|label| runtime.platform.as_deref() == Some(*label)),+ cuda_available: runtime.cuda_available,+ cuda_device_names: runtime.cuda_device_names.clone(),+ detail: match state {+ "ready" => "The worker is ready. Missing weights download on the worker; local files are sent to it.".into(),+ "modelsMissing" => format!("The worker is ready; {missing} required model paths are not set or not found."),+ _ => runtime.detail.clone(),+ },+ models,+ }+}diff --git a/src-tauri/src/slopfab/types.rs b/src-tauri/src/slopfab/types.rsindex be3e2c3..2e58b7e 100644--- a/src-tauri/src/slopfab/types.rs+++ b/src-tauri/src/slopfab/types.rs@@ -24,7 +24,7 @@ pub struct ModelStatus {pub path: Option<String>,}-#[derive(Debug, Clone, Serialize)]+#[derive(Debug, Clone, Serialize, Deserialize)]#[serde(rename_all = "camelCase")]pub struct GpuDevice {pub name: String,@@ -35,7 +35,9 @@ pub fn gpu_devices() -> Vec<GpuDevice> {ffi::gpu_devices()}-#[derive(Debug, Clone, Default, Deserialize)]+// Serialize exists for LAN workers. Skipped fields stay on this side: the+// worker resolves its own continuation, edit source and latent paths.+#[derive(Debug, Clone, Default, Deserialize, Serialize)]#[serde(rename_all = "camelCase")]pub struct GenerationRequest {pub job_id: String,@@ -71,13 +73,13 @@ pub struct GenerationRequest {pub save_latents_path: Option<PathBuf>,}-#[derive(Debug, Clone, Deserialize)]+#[derive(Debug, Clone, Deserialize, Serialize)]#[serde(rename_all = "camelCase")]pub struct ImageEditRequest {pub source_relative_path: String,pub edits: Vec<ImageEditStep>,}-#[derive(Debug, Clone, Deserialize)]+#[derive(Debug, Clone, Deserialize, Serialize)]#[serde(rename_all = "camelCase")]pub struct ImageEditStep {pub prompt: String,diff --git a/src-tauri/src/weights.rs b/src-tauri/src/weights.rsindex 96db79a..b309c81 100644--- a/src-tauri/src/weights.rs+++ b/src-tauri/src/weights.rs@@ -111,6 +111,32 @@ fn find_downloaded(urls: Vec<String>, roots: &[PathBuf]) -> HashMap<String, Stri}).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 || {@@ -215,7 +241,7 @@ 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())}-fn hardware_devices() -> Vec<crate::slopfab::GpuDevice> {+pub(crate) fn hardware_devices() -> Vec<crate::slopfab::GpuDevice> {#[cfg(windows)]{use windows::Win32::Graphics::Dxgi::{CreateDXGIFactory1, IDXGIFactory1, DXGI_ADAPTER_FLAG_SOFTWARE};diff --git a/src-tauri/src/weights/lora.rs b/src-tauri/src/weights/lora.rsindex 687a985..5b7193b 100644--- a/src-tauri/src/weights/lora.rs+++ b/src-tauri/src/weights/lora.rs@@ -10,6 +10,12 @@ fn prepare(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");diff --git a/src-tauri/src/worker/client.rs b/src-tauri/src/worker/client.rsnew file mode 100644index 0000000..a4db75f--- /dev/null+++ b/src-tauri/src/worker/client.rs@@ -0,0 +1,895 @@+//! 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::{+ discovery::{self, Announced},+ protocol::*,+};+use crate::{+ diagnostics,+ project::ProviderSetting,+ rendered,+ slopfab::{self, GenerationRequest, GpuDevice},+ weights,+};+use reqwest::blocking::{Body, Client, Response};+use serde::{de::DeserializeOwned, Deserialize, Serialize};+use std::{+ collections::{BTreeMap, HashMap},+ fs::{self, File},+ io::{self, Read},+ path::{Path, PathBuf},+ sync::{+ atomic::{AtomicBool, Ordering},+ Arc, Mutex, OnceLock,+ },+ time::{Duration, Instant, UNIX_EPOCH},+};+use tauri::{AppHandle, Emitter};++#[derive(Default, Clone, Serialize, Deserialize)]+#[serde(rename_all = "camelCase")]+struct Saved {+ /// The selected worker's id. None runs generation on this computer.+ selected: Option<String>,+ /// Addresses added by hand, for networks where mDNS does not reach.+ manual: Vec<String>,+ /// Where each worker was last reached, so a selection survives a restart+ /// before mDNS has answered.+ known: BTreeMap<String, KnownWorker>,+ tokens: BTreeMap<String, String>,+}++#[derive(Clone, Serialize, Deserialize)]+#[serde(rename_all = "camelCase")]+struct KnownWorker {+ name: String,+ address: String,+}++#[derive(Clone)]+struct Probe {+ info: Result<WorkerInfo, String>,+ at: Instant,+}++#[derive(Default)]+struct Inner {+ app: OnceLock<AppHandle>,+ file: OnceLock<PathBuf>,+ saved: Mutex<Saved>,+ announced: Mutex<Vec<Announced>>,+ probes: Mutex<HashMap<String, Probe>>,+ jobs: Mutex<HashMap<String, Arc<AtomicBool>>>,+ daemon: Mutex<Option<mdns_sd::ServiceDaemon>>,+}++#[derive(Clone, Default)]+pub struct Workers(Arc<Inner>);++#[derive(Debug, Clone, Serialize)]+#[serde(rename_all = "camelCase")]+pub struct WorkerView {+ /// The worker id, or the address of a manual worker never reached.+ key: String,+ id: Option<String>,+ name: String,+ address: Option<String>,+ online: bool,+ compatible: bool,+ discovered: bool,+ manual: bool,+ selected: bool,+ version: Option<String>,+ requires_token: bool,+ has_token: bool,+ gpus: Vec<GpuDevice>,+ runtime: Option<WorkerRuntime>,+ error: Option<String>,+}++#[derive(Debug, Clone, Serialize)]+#[serde(rename_all = "camelCase")]+pub struct WorkerList {+ selected_id: Option<String>,+ workers: Vec<WorkerView>,+}++/// A selected worker that answered. Cheap to clone; one per remote call.+#[derive(Clone)]+pub(crate) struct Connection {+ base: String,+ token: Option<String>,+ pub(crate) name: String,+ pub(crate) address: String,+ pub(crate) info: WorkerInfo,+ http: Client,+}++/// A worker the list should show, before anyone has asked it anything.+struct Candidate {+ id: Option<String>,+ name: String,+ addresses: Vec<String>,+ discovered: bool,+ manual: bool,+}++const PROBE_FRESH: Duration = Duration::from_secs(10);++fn normalize_address(address: &str) -> Result<String, String> {+ let trimmed = address.trim().trim_start_matches("http://").trim_end_matches('/');+ if trimmed.is_empty() || trimmed.contains(['/', '?', '#', '@', ' ']) {+ return Err("Enter a worker address like 192.168.1.20 or worker-pc:47321.".into());+ }+ let has_port = trimmed.rsplit_once(':').is_some_and(|(host, port)| !host.is_empty() && !host.ends_with(':') && port.parse::<u16>().is_ok());+ Ok(if has_port { trimmed.to_string() } else { format!("{trimmed}:{DEFAULT_PORT}") })+}++fn socket_address(address: &std::net::SocketAddr) -> String {+ match address {+ std::net::SocketAddr::V6(v6) => format!("[{}]:{}", v6.ip(), v6.port()),+ std::net::SocketAddr::V4(v4) => v4.to_string(),+ }+}++fn http_client(timeout: Option<Duration>) -> Client {+ Client::builder()+ .connect_timeout(Duration::from_secs(4))+ .timeout(timeout)+ .build()+ .unwrap_or_default()+}++fn worker_error(name: &str, response: Response) -> String {+ let status = response.status();+ #[derive(Deserialize)]+ struct Failure { error: String }+ match response.json::<Failure>() {+ Ok(failure) => format!("Worker {name}: {}", failure.error),+ Err(_) => format!("Worker {name} answered {status}."),+ }+}++impl Connection {+ fn request(&self, method: reqwest::Method, path: &str) -> reqwest::blocking::RequestBuilder {+ let builder = self.http.request(method, format!("{}{path}", self.base));+ match &self.token {+ Some(token) => builder.header(TOKEN_HEADER, token),+ None => builder,+ }+ }++ fn send(&self, builder: reqwest::blocking::RequestBuilder) -> Result<Response, String> {+ let response = builder.send().map_err(|error| format!("Could not reach worker {}: {error}", self.name))?;+ if response.status().is_success() { Ok(response) } else { Err(worker_error(&self.name, response)) }+ }++ pub(crate) fn get<T: DeserializeOwned>(&self, path: &str) -> Result<T, String> {+ self.send(self.request(reqwest::Method::GET, path))?.json().map_err(|error| format!("Worker {}: {error}", self.name))+ }++ pub(crate) fn post<T: DeserializeOwned>(&self, path: &str, body: &impl Serialize) -> Result<T, String> {+ self.send(self.request(reqwest::Method::POST, path).json(body))?.json().map_err(|error| format!("Worker {}: {error}", self.name))+ }++ pub(crate) fn post_bytes(&self, path: &str, headers: &[(&str, String)], bytes: Vec<u8>) -> Result<(), String> {+ let mut builder = self.request(reqwest::Method::POST, path).body(bytes);+ for (name, value) in headers {+ builder = builder.header(*name, value);+ }+ self.send(builder).map(|_| ())+ }++ fn bytes(&self, path: &str) -> Result<Vec<u8>, String> {+ let response = self.send(self.request(reqwest::Method::GET, path))?;+ response.bytes().map(|bytes| bytes.to_vec()).map_err(|error| format!("Worker {}: {error}", self.name))+ }++ fn release(&self, job_id: &str) {+ let _ = self.send(self.request(reqwest::Method::DELETE, &format!("/v1/jobs/{job_id}")));+ }+}++fn probe(address: &str, token: Option<&str>) -> Result<WorkerInfo, String> {+ let mut builder = http_client(Some(Duration::from_secs(3))).get(format!("http://{address}/v1/info"));+ if let Some(token) = token {+ builder = builder.header(TOKEN_HEADER, token);+ }+ let response = builder.send().map_err(|error| format!("Not reachable: {error}"))?;+ if !response.status().is_success() {+ return Err(format!("Answered {}.", response.status()));+ }+ let info: WorkerInfo = response.json().map_err(|_| "This address is not a Slopus worker.".to_string())?;+ if !valid_key(&info.id) {+ return Err("This address is not a Slopus worker.".into());+ }+ Ok(info)+}++impl Workers {+ /// Loads the saved selection and starts listening for workers.+ pub fn start(&self, app: &AppHandle, data_directory: &Path) {+ let _ = self.0.app.set(app.clone());+ let file = data_directory.join("workers.json");+ if let Some(saved) = fs::read(&file).ok().and_then(|bytes| serde_json::from_slice::<Saved>(&bytes).ok()) {+ if let Ok(mut current) = self.0.saved.lock() {+ *current = saved;+ }+ }+ let _ = self.0.file.set(file);+ let inner = Arc::downgrade(&self.0);+ match discovery::browse(move |announced| {+ let Some(inner) = inner.upgrade() else { return };+ if let Ok(mut current) = inner.announced.lock() {+ *current = announced;+ }+ if let Some(app) = inner.app.get() {+ let _ = app.emit("workers-changed", ());+ }+ }) {+ Ok(daemon) => {+ if let Ok(mut current) = self.0.daemon.lock() {+ *current = Some(daemon);+ }+ }+ Err(error) => diagnostics::warn("workers", "discovery.failed", &error, serde_json::json!({})),+ }+ }++ fn save(&self, saved: &Saved) {+ if let Some(file) = self.0.file.get() {+ if let Ok(bytes) = serde_json::to_vec_pretty(saved) {+ if let Err(error) = crate::storage::atomic::write_atomically(file, &bytes) {+ diagnostics::warn("workers", "save.failed", &error, serde_json::json!({}));+ }+ }+ }+ }++ fn saved(&self) -> Saved {+ self.0.saved.lock().map(|saved| saved.clone()).unwrap_or_default()+ }++ fn update(&self, change: impl FnOnce(&mut Saved)) -> Saved {+ let saved = match self.0.saved.lock() {+ Ok(mut saved) => {+ change(&mut saved);+ saved.clone()+ }+ Err(_) => return Saved::default(),+ };+ self.save(&saved);+ if let Some(app) = self.0.app.get() {+ let _ = app.emit("workers-changed", ());+ }+ saved+ }++ fn probe_cached(&self, address: &str, token: Option<&str>, fresh: Duration) -> Result<WorkerInfo, String> {+ if let Some(probe) = self.0.probes.lock().ok().and_then(|probes| probes.get(address).cloned()) {+ if probe.at.elapsed() < fresh {+ return probe.info;+ }+ }+ let info = probe(address, token);+ if let Ok(mut probes) = self.0.probes.lock() {+ probes.insert(address.to_string(), Probe { info: info.clone(), at: Instant::now() });+ }+ info+ }++ /// Every address a worker id might answer on, best first.+ fn addresses_for(&self, id: &str, saved: &Saved) -> Vec<String> {+ let mut addresses: Vec<String> = self.0.announced.lock().map(|announced| announced.iter()+ .filter(|worker| worker.id == id)+ .flat_map(|worker| worker.addresses.iter().map(socket_address))+ .collect()).unwrap_or_default();+ if let Some(known) = saved.known.get(id) {+ addresses.push(known.address.clone());+ }+ addresses.dedup();+ addresses+ }++ /// The selected worker, or None to generate on this computer. A selected+ /// worker that does not answer is an error rather than a silent fallback:+ /// the user chose where their GPU time is spent.+ pub(crate) fn active(&self) -> Result<Option<Connection>, String> {+ let saved = self.saved();+ let Some(id) = saved.selected.clone() else { return Ok(None) };+ let name = saved.known.get(&id).map(|known| known.name.clone()).unwrap_or_else(|| "worker".into());+ let token = saved.tokens.get(&id).cloned();+ let mut last_error = None;+ for address in self.addresses_for(&id, &saved) {+ match self.probe_cached(&address, token.as_deref(), PROBE_FRESH) {+ Ok(info) if info.id == id => {+ if info.protocol != PROTOCOL_VERSION {+ return Err(format!("Worker {} uses a different Slopus version ({}). Update it to match this app.", info.name, info.version));+ }+ if info.requires_token && token.is_none() {+ return Err(format!("Worker {} requires an access token. Enter it in Settings → Workers.", info.name));+ }+ if saved.known.get(&id).is_none_or(|known| known.address != address || known.name != info.name) {+ let (worker_name, worker_address) = (info.name.clone(), address.clone());+ self.update(|saved| { saved.known.insert(id.clone(), KnownWorker { name: worker_name, address: worker_address }); });+ }+ return Ok(Some(Connection {+ base: format!("http://{address}"),+ token,+ name: info.name.clone(),+ address,+ info,+ http: http_client(None),+ }));+ }+ Ok(_) => last_error = Some("Another worker now answers at its last address.".to_string()),+ Err(error) => last_error = Some(error),+ }+ }+ Err(format!(+ "The selected worker {name} is offline{}. Start it, or choose This computer in Settings → Workers.",+ last_error.map(|error| format!(" ({error})")).unwrap_or_default()+ ))+ }++ pub fn list(&self) -> WorkerList {+ let saved = self.saved();+ let announced = self.0.announced.lock().map(|announced| announced.clone()).unwrap_or_default();+ let mut candidates: Vec<Candidate> = announced.iter()+ .map(|worker| Candidate { id: Some(worker.id.clone()), name: worker.name.clone(), addresses: worker.addresses.iter().map(socket_address).collect(), discovered: true, manual: false })+ .collect();+ for address in &saved.manual {+ candidates.push(Candidate { id: None, name: address.clone(), addresses: vec![address.clone()], discovered: false, manual: true });+ }+ if let Some(id) = &saved.selected {+ if let Some(known) = saved.known.get(id) {+ candidates.push(Candidate { id: Some(id.clone()), name: known.name.clone(), addresses: vec![known.address.clone()], discovered: false, manual: false });+ }+ }+ let probes: Vec<_> = std::thread::scope(|scope| {+ let handles: Vec<_> = candidates.iter().map(|Candidate { id, addresses, .. }| {+ let token = id.as_ref().and_then(|id| saved.tokens.get(id)).cloned();+ scope.spawn(move || {+ let mut last = Err("No address.".to_string());+ for address in addresses {+ last = self.probe_cached(address, token.as_deref(), Duration::from_secs(3)).map(|info| (info, address.clone()));+ if last.is_ok() { break; }+ }+ last+ })+ }).collect();+ handles.into_iter().map(|handle| handle.join().unwrap_or_else(|_| Err("Probe failed.".into()))).collect()+ });+ let mut views: Vec<WorkerView> = Vec::new();+ for (Candidate { id, name, addresses, discovered, manual }, probe) in candidates.into_iter().zip(probes) {+ let (id, view) = match probe {+ Ok((info, address)) => (Some(info.id.clone()), WorkerView {+ key: info.id.clone(), id: Some(info.id.clone()), name: info.name.clone(), address: Some(address),+ online: true, compatible: info.protocol == PROTOCOL_VERSION, discovered, manual,+ selected: saved.selected.as_deref() == Some(info.id.as_str()), version: Some(info.version.clone()),+ requires_token: info.requires_token, has_token: saved.tokens.contains_key(&info.id),+ gpus: info.gpus.clone(), runtime: Some(info.runtime.clone()), error: None,+ }),+ Err(error) => (id.clone(), WorkerView {+ key: id.clone().unwrap_or_else(|| addresses.first().cloned().unwrap_or_default()),+ selected: id.is_some() && saved.selected == id, has_token: id.as_ref().is_some_and(|id| saved.tokens.contains_key(id)),+ id, name, address: addresses.first().cloned(), online: false, compatible: false, discovered, manual,+ version: None, requires_token: false, gpus: Vec::new(), runtime: None, error: Some(error),+ }),+ };+ match views.iter_mut().find(|view| id.is_some() && view.id == id) {+ Some(existing) => {+ existing.discovered |= view.discovered;+ existing.manual |= view.manual;+ if !existing.online && view.online {+ let (discovered, manual) = (existing.discovered, existing.manual);+ *existing = WorkerView { discovered, manual, ..view };+ }+ }+ None => views.push(view),+ }+ }+ views.sort_by(|a, b| b.online.cmp(&a.online).then_with(|| a.name.to_lowercase().cmp(&b.name.to_lowercase())));+ WorkerList { selected_id: saved.selected, workers: views }+ }++ pub fn select(&self, id: Option<String>) -> Result<WorkerList, String> {+ if let Some(id) = &id {+ let list = self.list();+ let worker = list.workers.iter().find(|worker| worker.id.as_ref() == Some(id))+ .ok_or("This worker is not available.")?;+ if !worker.online {+ return Err(format!("Worker {} is offline.", worker.name));+ }+ if !worker.compatible {+ return Err(format!("Worker {} runs a different Slopus version. Update it to match this app.", worker.name));+ }+ let known = KnownWorker { name: worker.name.clone(), address: worker.address.clone().unwrap_or_default() };+ self.update(|saved| {+ saved.known.insert(id.clone(), known);+ saved.selected = Some(id.clone());+ });+ } else {+ self.update(|saved| saved.selected = None);+ }+ diagnostics::info("workers", "selected", "Generation worker selected.", serde_json::json!({ "remote": id.is_some() }));+ Ok(self.list())+ }++ pub fn add(&self, address: &str) -> Result<WorkerList, String> {+ let address = normalize_address(address)?;+ let info = probe(&address, None).map_err(|error| format!("{address}: {error}"))?;+ if let Ok(mut probes) = self.0.probes.lock() {+ probes.insert(address.clone(), Probe { info: Ok(info.clone()), at: Instant::now() });+ }+ self.update(|saved| {+ if !saved.manual.contains(&address) {+ saved.manual.push(address.clone());+ }+ saved.known.insert(info.id.clone(), KnownWorker { name: info.name.clone(), address: address.clone() });+ });+ Ok(self.list())+ }++ pub fn remove(&self, key: &str) -> WorkerList {+ self.update(|saved| {+ let address = saved.known.get(key).map(|known| known.address.clone());+ saved.manual.retain(|manual| manual != key && Some(manual) != address.as_ref());+ saved.known.remove(key);+ saved.tokens.remove(key);+ if saved.selected.as_deref() == Some(key) {+ saved.selected = None;+ }+ });+ self.list()+ }++ pub fn set_token(&self, id: &str, token: &str) -> WorkerList {+ let token = token.trim().to_string();+ self.update(|saved| {+ if token.is_empty() { saved.tokens.remove(id); } else { saved.tokens.insert(id.to_string(), token.clone()); }+ });+ if let Ok(mut probes) = self.0.probes.lock() {+ probes.clear();+ }+ self.list()+ }++ fn emit(&self, event: &str, payload: serde_json::Value) {+ if let Some(app) = self.0.app.get() {+ let _ = app.emit(event, payload);+ }+ }++ fn register(&self, job_id: &str) -> Result<Arc<AtomicBool>, String> {+ let mut jobs = self.0.jobs.lock().map_err(|_| "Worker job lock failed.")?;+ if jobs.contains_key(job_id) {+ return Err("This generation job is already queued.".into());+ }+ let cancel = Arc::new(AtomicBool::new(false));+ jobs.insert(job_id.to_string(), cancel.clone());+ Ok(cancel)+ }++ fn unregister(&self, job_id: &str) {+ if let Ok(mut jobs) = self.0.jobs.lock() {+ jobs.remove(job_id);+ }+ }++ /// Cancels a job this computer sent to a worker. False when it sent none.+ pub(crate) fn cancel(&self, job_id: &str) -> bool {+ let Some(flag) = self.0.jobs.lock().ok().and_then(|jobs| jobs.get(job_id).cloned()) else { return false };+ flag.store(true, Ordering::Release);+ if let Ok(Some(connection)) = self.active() {+ let job_id = job_id.to_string();+ std::thread::spawn(move || {+ let _ = connection.post::<serde_json::Value>(&format!("/v1/jobs/{job_id}/cancel"), &());+ let _ = connection.post::<serde_json::Value>(&format!("/v1/prepare/{job_id}/cancel"), &());+ });+ }+ true+ }++ /// Uploads what only this computer has, then lets the worker download the+ /// rest. Both report progress as queue events on the job.+ fn send_files(&self, connection: &Connection, job: &RemoteJob, sources: &HashMap<String, PathBuf>, cancel: &Arc<AtomicBool>) -> Result<(), String> {+ let request = &job.request;+ let missing: MissingUploads = connection.post("/v1/uploads/missing", &job.files)?;+ let entries: Vec<(&String, &UploadEntry, bool)> = job.files.iter().flat_map(|file| match file {+ FileRef::Upload { key, entries, directory, .. } => entries.iter()+ .filter(|entry| missing.missing.iter().any(|(k, path)| k == key && *path == entry.path))+ .map(|entry| (key, entry, *directory)).collect(),+ FileRef::Url { .. } => Vec::new(),+ }).collect();+ let total: u64 = entries.iter().map(|(_, entry, _)| entry.bytes).sum();+ let sent = Arc::new(Mutex::new((0u64, Instant::now())));+ for (key, entry, directory) in entries {+ if cancel.load(Ordering::Acquire) {+ return Err("Generation was cancelled before it started.".into());+ }+ let root = sources.get(key).ok_or("Upload source missing.")?;+ let local = if directory { root.join(safe_relative_path(&entry.path)?) } else { root.clone() };+ let file = File::open(&local).map_err(|error| format!("Could not read {}: {error}", local.display()))?;+ if file.metadata().map(|meta| meta.len()).ok() != Some(entry.bytes) {+ return Err(format!("{} changed while it was being sent. Try again.", local.display()));+ }+ let reader = Progress { inner: file, cancel: cancel.clone(), sent: sent.clone(), report: {+ let workers = self.clone();+ let request = request.clone();+ let name = entry.path.clone();+ Box::new(move |done| workers.emit("slopfab-progress", transfer_event(&request, "workerUpload", format!("Sending {name} to the worker"), done, Some(total))))+ } };+ let encoded = entry.path.split('/').map(encode_segment).collect::<Vec<_>>().join("/");+ connection.send(connection.request(reqwest::Method::PUT, &format!("/v1/uploads/{key}/{encoded}"))+ .body(Body::sized(reader, entry.bytes)))+ .map_err(|error| if cancel.load(Ordering::Acquire) { "Generation was cancelled before it started.".into() } else { error })?;+ }+ if !job.files.iter().any(|file| matches!(file, FileRef::Url { .. })) {+ return Ok(());+ }+ let id = &request.job_id;+ connection.post::<bool>(&format!("/v1/prepare/{id}"), &job.files)?;+ loop {+ std::thread::sleep(Duration::from_millis(400));+ if cancel.load(Ordering::Acquire) {+ let _ = connection.post::<bool>(&format!("/v1/prepare/{id}/cancel"), &());+ return Err("Generation was cancelled before it started.".into());+ }+ let status: FilePreparation = connection.get(&format!("/v1/prepare/{id}"))?;+ match status.state.as_str() {+ "done" => return Ok(()),+ "failed" => return Err(format!("Worker {} could not get the weights: {}", connection.name, status.error.unwrap_or_default())),+ "cancelled" => return Err("Generation was cancelled before it started.".into()),+ _ => {+ let file = status.file.as_deref().and_then(|url| url.rsplit('/').next()).unwrap_or("weights");+ let detail = format!("Worker downloading {file} ({} of {})", (status.completed + 1).min(status.files), status.files);+ self.emit("slopfab-progress", transfer_event(request, "workerDownload", detail, status.downloaded, status.total));+ }+ }+ }+ }++ pub(crate) fn plan(&self, connection: &Connection, request: &GenerationRequest, settings: &BTreeMap<String, ProviderSetting>) -> Result<serde_json::Value, String> {+ let (job, sources) = build_job(request, settings)?;+ let cancel = self.register(&request.job_id)?;+ let result = self.send_files(connection, &job, &sources, &cancel)+ .and_then(|_| connection.post::<serde_json::Value>("/v1/plan", &job));+ self.unregister(&request.job_id);+ result+ }++ /// Starts a job on the worker and follows it on a background thread. The+ /// webview receives the same queued/progress/framesReady events, and the+ /// frames wait in this process's render store like a local render.+ pub(crate) fn enqueue(&self, connection: Connection, request: GenerationRequest, settings: &BTreeMap<String, ProviderSetting>) -> Result<(), String> {+ crate::media::artifacts::generated_file_stem(&request.job_id)?;+ let (job, sources) = build_job(&request, settings)?;+ let cancel = self.register(&request.job_id)?;+ let workers = self.clone();+ let latents = request.save_latents_path.clone();+ diagnostics::info("workers", "job.sending", "Generation is being sent to a worker.", serde_json::json!({+ "jobId": request.job_id, "files": job.files.len(),+ }));+ std::thread::Builder::new().name("worker-job".into()).spawn(move || {+ let id = request.job_id.clone();+ let result = workers.send_files(&connection, &job, &sources, &cancel)+ .and_then(|_| connection.post::<bool>(&format!("/v1/jobs/{id}"), &StartJob { job, save_latents: latents.is_some() }))+ .and_then(|_| workers.follow(&connection, &id, latents.as_deref()));+ if let Err(error) = result {+ let state = if cancel.load(Ordering::Acquire) { "cancelled" } else { "failed" };+ diagnostics::warn("workers", "job.failed", &error, serde_json::json!({ "jobId": id, "state": state }));+ workers.emit("slopfab-job", serde_json::json!({ "jobId": id, "state": state, "detail": error, "output": null }));+ connection.release(&id);+ }+ workers.unregister(&id);+ }).map_err(|error| error.to_string())?;+ Ok(())+ }++ fn follow(&self, connection: &Connection, id: &str, latents: Option<&Path>) -> Result<(), String> {+ let mut after = 0;+ let mut failures = 0;+ loop {+ let events: Vec<WireEvent> = match connection.get(&format!("/v1/jobs/{id}/events?after={after}")) {+ Ok(events) => { failures = 0; events }+ Err(error) => {+ failures += 1;+ if failures >= 15 {+ return Err(format!("Lost the connection to worker {}: {error}", connection.name));+ }+ std::thread::sleep(Duration::from_secs(2));+ continue;+ }+ };+ for event in events {+ after = event.seq;+ if event.kind == "progress" {+ self.emit("slopfab-progress", event.payload);+ continue;+ }+ let mut payload = event.payload;+ match payload.get("state").and_then(|state| state.as_str()) {+ Some("framesReady") => {+ let evicted = self.collect(connection, id, latents)?;+ if let Some(job) = evicted.filter(|job| job != id) {+ payload["detail"] = format!("Frames and audio are ready to be encoded. The frames of {job} were dropped to make room and that scene would have to be rendered again.").into();+ }+ self.emit("slopfab-job", payload);+ return Ok(());+ }+ Some("failed") | Some("cancelled") => {+ self.emit("slopfab-job", payload);+ connection.release(id);+ return Ok(());+ }+ _ => self.emit("slopfab-job", payload),+ }+ }+ }+ }++ /// Brings a finished job home: latents into the project now, frames on+ /// demand as the webview encodes them.+ fn collect(&self, connection: &Connection, id: &str, latents: Option<&Path>) -> Result<Option<String>, String> {+ if let Some(destination) = latents {+ let mut response = connection.send(connection.request(reqwest::Method::GET, &format!("/v1/jobs/{id}/latents")))?;+ let temporary = destination.with_extension("safetensors.part");+ let copied = File::create(&temporary)+ .and_then(|mut file| io::copy(&mut response, &mut file).and_then(|bytes| file.sync_all().map(|_| bytes)))+ .map_err(|error| format!("Could not save latents from the worker: {error}"));+ match copied {+ Ok(_) => fs::rename(&temporary, destination).map_err(|error| error.to_string())?,+ Err(error) => {+ let _ = fs::remove_file(&temporary);+ return Err(error);+ }+ }+ }+ let summary: rendered::RenderedSummary = connection.get(&format!("/v1/jobs/{id}/summary"))?;+ let audio: Vec<f32> = connection.bytes(&format!("/v1/jobs/{id}/audio"))?+ .chunks_exact(4).map(|chunk| f32::from_le_bytes(chunk.try_into().unwrap())).collect();+ let values = summary.frame_count as usize * summary.width as usize * summary.height as usize * 3;+ let video = rendered::from_source(+ id,+ Box::new(RemoteFrames { connection: connection.clone(), job_id: id.to_string() }),+ audio,+ summary.frame_count,+ summary.width,+ summary.height,+ 3,+ values,+ summary.fps,+ summary.audio_channels,+ summary.audio_sample_rate,+ )?;+ diagnostics::info("workers", "job.collected", "Worker generation is ready to encode.", serde_json::json!({+ "jobId": id, "frames": summary.frame_count, "latents": latents.is_some(),+ }));+ Ok(rendered::keep(video))+ }++ pub(crate) fn status(&self, connection: &Connection, settings: &BTreeMap<String, ProviderSetting>) -> slopfab::SlopfabStatus {+ slopfab::worker_status(settings, &connection.info.runtime, format!("Worker {} ({})", connection.name, connection.address))+ }+}++/// Frames stay on the worker until the encoder asks for them, and the+/// worker's copy is released with this one.+struct RemoteFrames {+ connection: Connection,+ job_id: String,+}++impl rendered::FrameSource for RemoteFrames {+ fn frame_rgba(&self, index: u32, width: u32, height: u32) -> Result<Vec<u8>, String> {+ let frame = self.connection.bytes(&format!("/v1/jobs/{}/frames/{index}", self.job_id))?;+ if frame.len() != width as usize * height as usize * 4 {+ return Err(format!("Worker {} sent an incomplete frame {index}.", self.connection.name));+ }+ Ok(frame)+ }+}++impl Drop for RemoteFrames {+ fn drop(&mut self) {+ let (connection, job_id) = (self.connection.clone(), self.job_id.clone());+ std::thread::spawn(move || connection.release(&job_id));+ }+}++struct Progress {+ inner: File,+ cancel: Arc<AtomicBool>,+ sent: Arc<Mutex<(u64, Instant)>>,+ report: Box<dyn Fn(u64) + Send>,+}++impl Read for Progress {+ fn read(&mut self, buffer: &mut [u8]) -> io::Result<usize> {+ if self.cancel.load(Ordering::Acquire) {+ return Err(io::Error::new(io::ErrorKind::Interrupted, "cancelled"));+ }+ let count = self.inner.read(buffer)?;+ if let Ok(mut sent) = self.sent.lock() {+ sent.0 += count as u64;+ if sent.1.elapsed() >= Duration::from_millis(250) {+ sent.1 = Instant::now();+ (self.report)(sent.0);+ }+ }+ Ok(count)+ }+}++fn encode_segment(segment: &str) -> String {+ segment.bytes().map(|byte| match byte {+ b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => (byte as char).to_string(),+ _ => format!("%{byte:02X}"),+ }).collect()+}++fn transfer_event(request: &GenerationRequest, stage: &str, detail: String, done: u64, total: Option<u64>) -> serde_json::Value {+ serde_json::json!({+ "jobId": request.job_id, "stage": stage, "step": 0, "totalSteps": 0, "plannedSteps": 0,+ "elapsedSeconds": 0.0, "frames": request.frames, "canvasWidth": request.canvas_width,+ "canvasHeight": request.canvas_height, "referenceCount": request.reference_count(),+ "timingProfile": "", "detail": detail, "transferred": done, "transferTotal": total,+ })+}++fn is_url(value: &str) -> bool {+ let lower = value.trim().to_ascii_lowercase();+ lower.starts_with("https://") || lower.starts_with("http://")+}++fn modified(meta: &fs::Metadata) -> u128 {+ meta.modified().ok().and_then(|time| time.duration_since(UNIX_EPOCH).ok()).map_or(0, |time| time.as_nanos())+}++/// Identifies upload content by name, size and modification time, so an+/// unchanged file is sent once and an edited one is sent again.+fn upload_key(name: &str, files: &[(String, u64, u128)]) -> String {+ let mut hash = 0xcbf29ce484222325u64;+ let mut feed = |bytes: &[u8]| for byte in bytes { hash = (hash ^ *byte as u64).wrapping_mul(0x100000001b3); };+ feed(name.as_bytes());+ for (path, bytes, modified) in files {+ feed(path.as_bytes());+ feed(&bytes.to_le_bytes());+ feed(&modified.to_le_bytes());+ }+ format!("{hash:016x}")+}++fn walk(root: &Path, relative: &str, out: &mut Vec<(String, u64, u128)>) -> Result<(), String> {+ let directory = if relative.is_empty() { root.to_path_buf() } else { root.join(relative) };+ let mut entries = fs::read_dir(&directory).map_err(|error| format!("Could not read {}: {error}", directory.display()))?+ .collect::<Result<Vec<_>, _>>().map_err(|error| error.to_string())?;+ entries.sort_by_key(|entry| entry.file_name());+ for entry in entries {+ let name = entry.file_name().to_string_lossy().into_owned();+ let path = if relative.is_empty() { name } else { format!("{relative}/{name}") };+ let meta = entry.metadata().map_err(|error| error.to_string())?;+ if meta.is_dir() {+ walk(root, &path, out)?;+ } else if meta.is_file() {+ out.push((path, meta.len(), modified(&meta)));+ }+ if out.len() > 10_000 {+ return Err(format!("{} has too many files to send to a worker.", root.display()));+ }+ }+ Ok(())+}++fn file_ref(kind: PathKind, value: &str, sources: &mut HashMap<String, PathBuf>) -> Result<FileRef, String> {+ let lora = kind == PathKind::Lora;+ if is_url(value) {+ return Ok(FileRef::Url { url: value.trim().to_string(), lora });+ }+ let path = PathBuf::from(value);+ if kind != PathKind::Input {+ if let Some(url) = weights::recorded_download_url(&path) {+ return Ok(FileRef::Url { url, lora });+ }+ }+ let meta = fs::metadata(&path).map_err(|_| format!("{} was not found on this computer.", path.display()))?;+ let name = path.file_name().map(|name| name.to_string_lossy().into_owned()).unwrap_or_else(|| "file".into());+ safe_relative_path(&name)?;+ let (directory, files) = if meta.is_dir() {+ if kind != PathKind::Folder {+ return Err(format!("{} is a folder, not a file.", path.display()));+ }+ let mut files = Vec::new();+ walk(&path, "", &mut files)?;+ (true, files)+ } else {+ (false, vec![(name.clone(), meta.len(), modified(&meta))])+ };+ let key = upload_key(&name, &files);+ sources.insert(key.clone(), path);+ Ok(FileRef::Upload {+ key, name, directory, lora,+ entries: files.into_iter().map(|(path, bytes, _)| UploadEntry { path, bytes }).collect(),+ })+}++/// Replaces every path of a resolved local request with a token for the worker.+pub(crate) fn build_job(request: &GenerationRequest, settings: &BTreeMap<String, ProviderSetting>) -> Result<(RemoteJob, HashMap<String, PathBuf>), String> {+ let mut request = request.clone();+ let mut settings: BTreeMap<String, ProviderSetting> = settings.iter()+ .filter(|(name, _)| name.as_str() == "slopfab").map(|(name, value)| (name.clone(), value.clone())).collect();+ let mut files = Vec::new();+ let mut indices: HashMap<String, usize> = HashMap::new();+ let mut sources = HashMap::new();+ let mut add = |kind: PathKind, value: &str, files: &mut Vec<FileRef>| -> Result<usize, String> {+ if let Some(index) = indices.get(value) {+ return Ok(*index);+ }+ files.push(file_ref(kind, value, &mut sources)?);+ indices.insert(value.to_string(), files.len() - 1);+ Ok(files.len() - 1)+ };+ visit_paths(&mut request, &mut settings, |kind, value| {+ *value = file_token(add(kind, value, &mut files)?);+ Ok(())+ })?;+ let continuation = request.continuation_path.take()+ .map(|path| add(PathKind::Input, &path.to_string_lossy(), &mut files)).transpose()?;+ let image_edit_source = request.image_edit_path.take()+ .map(|path| add(PathKind::Input, &path.to_string_lossy(), &mut files)).transpose()?;+ request.save_latents_path = None;+ request.image_edit_pixels = None;+ Ok((RemoteJob { request, settings, files, continuation, image_edit_source }, sources))+}++#[cfg(test)]+mod tests {+ use super::*;+ use crate::project::ProviderOption;++ #[test]+ fn addresses_get_the_default_port_and_reject_urls_with_paths() {+ assert_eq!(normalize_address("192.168.1.20").unwrap(), format!("192.168.1.20:{DEFAULT_PORT}"));+ assert_eq!(normalize_address(" http://pc:9000/ ").unwrap(), "pc:9000");+ assert!(normalize_address("pc/v1/info").is_err());+ assert!(normalize_address("").is_err());+ }++ #[test]+ fn jobs_send_urls_for_downloads_and_uploads_for_local_files() {+ let folder = tempfile::tempdir().unwrap();+ let image = folder.path().join("ref one.png");+ fs::write(&image, b"png").unwrap();+ let tokenizer = folder.path().join("tokenizer");+ fs::create_dir_all(tokenizer.join("nested")).unwrap();+ fs::write(tokenizer.join("vocab.json"), b"{}").unwrap();+ fs::write(tokenizer.join("nested/merges.txt"), b"m").unwrap();+ let options = BTreeMap::from([+ ("transformer".to_string(), ProviderOption::String("https://example.com/t.safetensors".into())),+ ("tokenizer".to_string(), ProviderOption::String(tokenizer.to_string_lossy().into_owned())),+ ("attention".to_string(), ProviderOption::String("sage2".into())),+ ]);+ let settings = BTreeMap::from([+ ("slopfab".to_string(), ProviderSetting { enabled: true, model: None, options }),+ ("openrouter".to_string(), ProviderSetting { enabled: true, model: None, options: BTreeMap::from([("apiKey".to_string(), ProviderOption::String("secret".into()))]) }),+ ]);+ let request = GenerationRequest {+ job_id: "work-1".into(),+ reference_paths: vec![image.to_string_lossy().into_owned(), image.to_string_lossy().into_owned()],+ continuation_path: Some(image.clone()),+ ..Default::default()+ };+ let (job, sources) = build_job(&request, &settings).unwrap();+ assert!(!job.settings.contains_key("openrouter"), "credentials never leave this computer");+ assert_eq!(job.files.len(), 3, "a repeated path is sent once");+ assert_eq!(job.files[0], FileRef::Url { url: "https://example.com/t.safetensors".into(), lora: false });+ let FileRef::Upload { directory: true, entries, .. } = &job.files[1] else { panic!("tokenizer folder") };+ assert_eq!(entries.iter().map(|entry| entry.path.as_str()).collect::<Vec<_>>(), vec!["nested/merges.txt", "vocab.json"]);+ assert_eq!(job.request.reference_paths, vec![file_token(2), file_token(2)]);+ assert_eq!(job.continuation, Some(2));+ assert_eq!(sources.len(), 2);+ assert!(build_job(&GenerationRequest { reference_paths: vec![folder.path().join("missing.png").to_string_lossy().into_owned()], ..Default::default() }, &BTreeMap::new()).is_err());+ }+}diff --git a/src-tauri/src/worker/discovery.rs b/src-tauri/src/worker/discovery.rsnew file mode 100644index 0000000..cc8fb40--- /dev/null+++ b/src-tauri/src/worker/discovery.rs@@ -0,0 +1,88 @@+//! mDNS: workers announce themselves on the local network and Slopus listens.+use super::protocol::{WorkerInfo, PROTOCOL_VERSION, SERVICE_TYPE};+use mdns_sd::{ServiceDaemon, ServiceEvent, ServiceInfo};+use std::{+ collections::HashMap,+ net::{IpAddr, SocketAddr},+};++/// 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>,+}++fn host_label(name: &str) -> String {+ let label: String = name+ .chars()+ .map(|c| if c.is_ascii_alphanumeric() { c.to_ascii_lowercase() } else { '-' })+ .collect::<String>()+ .trim_matches('-')+ .chars()+ .take(50)+ .collect();+ if label.is_empty() { "slopus-worker".into() } else { label }+}++/// Keeps announcing this worker for as long as the returned daemon lives.+pub(crate) 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();+ let properties = [+ ("id", info.id.as_str()),+ ("name", info.name.as_str()),+ ("version", info.version.as_str()),+ ("protocol", protocol.as_str()),+ ];+ let service = ServiceInfo::new(SERVICE_TYPE, &info.id, &host, "", port, &properties[..])+ .map_err(|error| error.to_string())?+ .enable_addr_auto();+ daemon.register(service).map_err(|error| error.to_string())?;+ Ok(daemon)+}++/// 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> {+ 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()+ .name("worker-discovery".into())+ .spawn(move || {+ let mut seen: HashMap<String, Announced> = HashMap::new();+ while let Ok(event) = events.recv() {+ let before = seen.clone();+ match event {+ ServiceEvent::ServiceResolved(service) => {+ let Some(id) = service.get_property_val_str("id").map(str::to_string) else { continue };+ let mut addresses: Vec<SocketAddr> = service+ .get_addresses()+ .iter()+ .map(|ip| ip.to_ip_addr())+ .filter(|ip| !ip.is_loopback() || service.get_addresses().len() == 1)+ .map(|ip| SocketAddr::new(ip, service.get_port()))+ .collect();+ // IPv4 first: link-local IPv6 needs a scope that URLs drop.+ addresses.sort_by_key(|address| matches!(address.ip(), IpAddr::V6(_)));+ seen.insert(service.get_fullname().to_string(), Announced {+ name: service.get_property_val_str("name").unwrap_or(&id).to_string(),+ id,+ addresses,+ });+ }+ ServiceEvent::ServiceRemoved(_, fullname) => {+ seen.remove(&fullname);+ }+ _ => continue,+ }+ if seen != before {+ changed(seen.values().cloned().collect());+ }+ }+ })+ .map_err(|error| error.to_string())?;+ Ok(daemon)+}diff --git a/src-tauri/src/worker/mod.rs b/src-tauri/src/worker/mod.rsnew file mode 100644index 0000000..e167cf7--- /dev/null+++ b/src-tauri/src/worker/mod.rs@@ -0,0 +1,14 @@+//! 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.+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/src/worker/protocol.rs b/src-tauri/src/worker/protocol.rsnew file mode 100644index 0000000..acddc12--- /dev/null+++ b/src-tauri/src/worker/protocol.rs@@ -0,0 +1,258 @@+//! 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},+ slopfab::{GenerationRequest, GpuDevice},+};+use serde::{Deserialize, Serialize};+use std::collections::BTreeMap;++pub const SERVICE_TYPE: &str = "_slopus-worker._tcp.local.";+/// Bumped on any incompatible change to the routes or bodies below.+pub const PROTOCOL_VERSION: u32 = 1;+pub const DEFAULT_PORT: u16 = 47321;+pub const TOKEN_HEADER: &str = "x-slopus-token";++/// A request can only name a worker file through one of these tokens. Raw+/// paths are refused, so a client cannot make the worker read its own disk.+const FILE_TOKEN: &str = "slopus-file:";++#[derive(Debug, Clone, Serialize, Deserialize)]+#[serde(rename_all = "camelCase")]+pub struct WorkerInfo {+ pub id: String,+ pub name: String,+ pub version: String,+ pub protocol: u32,+ pub requires_token: bool,+ pub gpus: Vec<GpuDevice>,+ pub runtime: WorkerRuntime,+}++/// The worker's engine, without any model paths: those come from the client.+#[derive(Debug, Clone, Serialize, Deserialize)]+#[serde(rename_all = "camelCase")]+pub struct WorkerRuntime {+ /// "ready" when the engine library loaded, else the engine's own state.+ pub state: String,+ pub version: Option<String>,+ pub platform: Option<String>,+ pub cuda_available: bool,+ pub cuda_device_names: Vec<String>,+ pub detail: String,+}++/// Where the worker gets one model file or folder.+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]+#[serde(tag = "kind", rename_all = "camelCase")]+pub enum FileRef {+ /// Downloaded on the worker, into its weights folders, unless already there.+ #[serde(rename_all = "camelCase")]+ Url { url: String, lora: bool },+ /// Sent by the client. `key` identifies the content; entries are relative+ /// to the uploaded folder, or a single entry named like the file.+ #[serde(rename_all = "camelCase")]+ Upload { key: String, name: String, directory: bool, entries: Vec<UploadEntry>, lora: bool },+}++#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]+#[serde(rename_all = "camelCase")]+pub struct UploadEntry {+ pub path: String,+ pub bytes: u64,+}++/// A generation with every path replaced by a token into `files`.+#[derive(Debug, Clone, Serialize, Deserialize)]+#[serde(rename_all = "camelCase")]+pub struct RemoteJob {+ pub request: GenerationRequest,+ pub settings: BTreeMap<String, ProviderSetting>,+ pub files: Vec<FileRef>,+ pub continuation: Option<usize>,+ pub image_edit_source: Option<usize>,+}++#[derive(Debug, Clone, Serialize, Deserialize)]+#[serde(rename_all = "camelCase")]+pub struct StartJob {+ pub job: RemoteJob,+ /// Video jobs save latents on the worker for the client to collect.+ pub save_latents: bool,+}++#[derive(Debug, Clone, Serialize, Deserialize)]+#[serde(rename_all = "camelCase")]+pub struct WireEvent {+ pub seq: u64,+ /// "job" or "progress": the desktop event the payload is re-emitted as.+ pub kind: String,+ pub payload: serde_json::Value,+}++#[derive(Debug, Clone, Default, Serialize, Deserialize)]+#[serde(rename_all = "camelCase")]+pub struct FilePreparation {+ /// "running", "done", "failed" or "cancelled".+ pub state: String,+ pub file: Option<String>,+ pub completed: usize,+ pub files: usize,+ pub downloaded: u64,+ pub total: Option<u64>,+ pub error: Option<String>,+}++#[derive(Debug, Clone, Serialize, Deserialize)]+#[serde(rename_all = "camelCase")]+pub struct MissingUploads {+ /// (key, entry path) pairs the worker does not hold yet.+ pub missing: Vec<(String, String)>,+}++pub fn file_token(index: usize) -> String {+ format!("{FILE_TOKEN}{index}")+}++pub fn parse_file_token(value: &str) -> Option<usize> {+ value.strip_prefix(FILE_TOKEN)?.parse().ok()+}++/// What a path in a generation is for. Uploading a folder and preparing a+/// LoRA after download both depend on it.+#[derive(Debug, Clone, Copy, PartialEq, Eq)]+pub enum PathKind {+ Model,+ Folder,+ Lora,+ Input,+}++const MODEL_OPTIONS: [&str; 5] = ["transformer", "textEncoder", "videoVae", "audioVae", "promptEmbedding"];++/// Visits every file path a generation names, in a fixed order. The client+/// swaps local paths for tokens with it, and the worker swaps tokens back+/// for its own paths. Anything not listed here never crosses the network.+pub fn visit_paths(+ request: &mut GenerationRequest,+ settings: &mut BTreeMap<String, ProviderSetting>,+ mut visit: impl FnMut(PathKind, &mut String) -> Result<(), String>,+) -> Result<(), String> {+ if let Some(slopfab) = settings.get_mut("slopfab") {+ // The engine library always comes from the machine that runs it.+ slopfab.options.remove("dllPath");+ for name in MODEL_OPTIONS {+ if let Some(ProviderOption::String(value)) = slopfab.options.get_mut(name) {+ if !value.trim().is_empty() {+ visit(PathKind::Model, value)?;+ }+ }+ }+ if let Some(ProviderOption::String(value)) = slopfab.options.get_mut("tokenizer") {+ if !value.trim().is_empty() {+ visit(PathKind::Folder, value)?;+ }+ }+ if let Some(ProviderOption::String(value)) = slopfab.options.get_mut("loras") {+ let mut loras: Vec<serde_json::Map<String, serde_json::Value>> = serde_json::from_str(value)+ .map_err(|error| format!("Invalid LoRA settings: {error}"))?;+ for lora in &mut loras {+ if let Some(serde_json::Value::String(path)) = lora.get_mut("path") {+ visit(PathKind::Lora, path)?;+ }+ }+ *value = serde_json::to_string(&loras).map_err(|error| error.to_string())?;+ }+ }+ for path in &mut request.reference_paths {+ visit(PathKind::Input, path)?;+ }+ for refmod in &mut request.refmods {+ visit(PathKind::Input, &mut refmod.path)?;+ }+ if let Some(edit) = &mut request.image_edit {+ for step in &mut edit.edits {+ for path in step.reference_paths.iter_mut().flatten() {+ visit(PathKind::Input, path)?;+ }+ for refmod in step.refmods.iter_mut().flatten() {+ visit(PathKind::Input, &mut refmod.path)?;+ }+ }+ }+ Ok(())+}++/// A relative upload path that stays inside its folder on any platform.+pub fn safe_relative_path(path: &str) -> Result<std::path::PathBuf, String> {+ let mut safe = std::path::PathBuf::new();+ for part in path.split('/') {+ let valid = !part.is_empty()+ && part != "."+ && part != ".."+ && part.len() <= 200+ && !part.chars().any(|c| c.is_control() || matches!(c, '\\' | ':' | '*' | '?' | '"' | '<' | '>' | '|'));+ if !valid {+ return Err(format!("'{path}' is not a valid upload path."));+ }+ safe.push(part);+ }+ if safe.as_os_str().is_empty() {+ return Err("An upload path cannot be empty.".into());+ }+ Ok(safe)+}++/// Upload keys become folder names on the worker.+pub fn valid_key(key: &str) -> bool {+ (8..=64).contains(&key.len()) && key.chars().all(|c| c.is_ascii_hexdigit())+}++#[cfg(test)]+mod tests {+ use super::*;+ use crate::slopfab::RefmodInput;++ fn settings(options: &[(&str, &str)]) -> BTreeMap<String, ProviderSetting> {+ let options = options.iter().map(|(name, value)| (name.to_string(), ProviderOption::String(value.to_string()))).collect();+ BTreeMap::from([("slopfab".to_string(), ProviderSetting { enabled: true, model: None, options })])+ }++ #[test]+ fn visits_every_path_and_drops_the_engine_library() {+ let mut request = GenerationRequest {+ reference_paths: vec!["C:/project/a.png".into()],+ refmods: vec![RefmodInput { path: "C:/refmod.safetensors".into(), strength: 1.0, copies: 1 }],+ ..Default::default()+ };+ let mut settings = settings(&[+ ("transformer", "C:/w/t.safetensors"), ("tokenizer", "C:/w/tok"), ("dllPath", "C:/evil.dll"),+ ("loras", r#"[{"path":"https://example.com/l.safetensors","strength":0.5}]"#),+ ]);+ let mut seen = Vec::new();+ visit_paths(&mut request, &mut settings, |kind, path| {+ seen.push((kind, path.clone()));+ *path = file_token(seen.len() - 1);+ Ok(())+ }).unwrap();+ assert_eq!(seen.iter().map(|(kind, _)| *kind).collect::<Vec<_>>(),+ vec![PathKind::Model, PathKind::Folder, PathKind::Lora, PathKind::Input, PathKind::Input]);+ let options = &settings["slopfab"].options;+ assert!(!options.contains_key("dllPath"));+ assert_eq!(options["transformer"], ProviderOption::String(file_token(0)));+ assert_eq!(options["loras"], ProviderOption::String(r#"[{"path":"slopus-file:2","strength":0.5}]"#.into()));+ assert_eq!(request.refmods[0].path, file_token(4));+ assert_eq!(parse_file_token(&request.reference_paths[0]), Some(3));+ assert_eq!(parse_file_token("C:/project/a.png"), None);+ }++ #[test]+ fn upload_paths_cannot_leave_their_folder() {+ assert_eq!(safe_relative_path("tokenizer/vocab.json").unwrap(), std::path::Path::new("tokenizer").join("vocab.json"));+ for path in ["", "../x", "a/../../x", "/abs", "C:/x", "a\\..\\b", "a//b", "./a"] {+ assert!(safe_relative_path(path).is_err(), "{path}");+ }+ assert!(valid_key("0123456789abcdef"));+ assert!(!valid_key("../../etc"));+ }+}diff --git a/src-tauri/src/worker/server.rs b/src-tauri/src/worker/server.rsnew file mode 100644index 0000000..54b0210--- /dev/null+++ b/src-tauri/src/worker/server.rs@@ -0,0 +1,528 @@+//! The headless worker: the desktop app's generation queue behind a small+//! HTTP API. Every route runs on its own thread; generation itself stays on+//! the one serial native queue.+use super::{+ discovery,+ protocol::*,+ store::FileStore,+};+use crate::{+ diagnostics,+ media::artifacts::generated_file_stem,+ project::{ProviderOption, ProviderSetting},+ rendered, slopfab,+};+use serde::{de::DeserializeOwned, Deserialize, Serialize};+use std::{+ collections::{BTreeMap, HashMap},+ fs,+ io::Read,+ path::PathBuf,+ sync::{+ atomic::{AtomicBool, Ordering},+ Arc, Condvar, Mutex,+ },+ time::{Duration, Instant},+};+use tiny_http::{Header, Method, Request, Response, Server};++#[derive(Debug, Clone)]+pub struct WorkerOptions {+ pub port: u16,+ pub name: Option<String>,+ pub token: Option<String>,+ pub data: Option<PathBuf>,+ pub weights: Vec<PathBuf>,+ pub vulkan: bool,+ pub advertise: bool,+}++impl Default for WorkerOptions {+ fn default() -> Self {+ Self { port: DEFAULT_PORT, name: None, token: None, data: None, weights: Vec::new(), vulkan: false, advertise: true }+ }+}++struct JobLog {+ events: Mutex<Vec<WireEvent>>,+ changed: Condvar,+ latents: Option<PathBuf>,+ created: Instant,+}++impl JobLog {+ fn push(&self, kind: &str, payload: serde_json::Value) {+ if let Ok(mut events) = self.events.lock() {+ let seq = events.len() as u64 + 1;+ events.push(WireEvent { seq, kind: kind.into(), payload });+ }+ self.changed.notify_all();+ }++ /// Long poll: returns as soon as anything after `after` exists.+ fn after(&self, after: u64, wait: Duration) -> Vec<WireEvent> {+ let Ok(events) = self.events.lock() else { return Vec::new() };+ let (events, _) = self+ .changed+ .wait_timeout_while(events, wait, |events| events.len() as u64 <= after)+ .unwrap_or_else(|poisoned| poisoned.into_inner());+ events.iter().filter(|event| event.seq > after).cloned().collect()+ }+}++struct Preparation {+ status: Mutex<FilePreparation>,+ cancel: Arc<AtomicBool>,+}++struct Worker {+ id: String,+ name: String,+ token: Option<String>,+ vulkan: bool,+ data: PathBuf,+ store: Arc<FileStore>,+ runtime: slopfab::SlopfabRuntime,+ jobs: Mutex<HashMap<String, Arc<JobLog>>>,+ preparations: Mutex<HashMap<String, Arc<Preparation>>>,+}++struct HttpError(u16, String);++impl From<String> for HttpError {+ fn from(message: String) -> Self {+ Self(400, message)+ }+}+impl From<&str> for HttpError {+ fn from(message: &str) -> Self {+ Self(400, message.into())+ }+}++type Reply = Result<Response<Box<dyn Read + Send>>, HttpError>;++fn json<T: Serialize>(value: &T) -> Reply {+ let body = serde_json::to_vec(value).map_err(|error| HttpError(500, error.to_string()))?;+ Ok(bytes(body, "application/json"))+}++fn bytes(body: Vec<u8>, kind: &str) -> Response<Box<dyn Read + Send>> {+ let length = body.len();+ Response::new(+ 200.into(),+ vec![Header::from_bytes("Content-Type", kind).unwrap()],+ Box::new(std::io::Cursor::new(body)) as Box<dyn Read + Send>,+ Some(length),+ None,+ )+}++fn body<T: DeserializeOwned>(request: &mut Request) -> Result<T, HttpError> {+ let mut text = Vec::new();+ request+ .as_reader()+ .take(64 * 1024 * 1024)+ .read_to_end(&mut text)+ .map_err(|error| HttpError(400, error.to_string()))?;+ serde_json::from_slice(&text).map_err(|error| HttpError(400, format!("Invalid request body: {error}")))+}++fn header<'a>(request: &'a Request, name: &str) -> Option<&'a str> {+ request+ .headers()+ .iter()+ .find(|header| header.field.as_str().as_str().eq_ignore_ascii_case(name))+ .map(|header| header.value.as_str())+}++fn parsed_header<T: std::str::FromStr>(request: &Request, name: &str) -> Result<T, HttpError> {+ header(request, name)+ .and_then(|value| value.parse().ok())+ .ok_or_else(|| HttpError(400, format!("Missing or invalid {name} header.")))+}++fn data_directory(options: &WorkerOptions) -> PathBuf {+ options.data.clone().unwrap_or_else(|| {+ std::env::var_os("LOCALAPPDATA")+ .map(PathBuf::from)+ .or_else(|| std::env::var_os("HOME").map(|home| PathBuf::from(home).join(".local/share")))+ .unwrap_or_else(std::env::temp_dir)+ .join("Slopus Worker")+ })+}++fn worker_id(data: &std::path::Path) -> Result<String, String> {+ let path = data.join("worker-id");+ if let Ok(id) = fs::read_to_string(&path) {+ if valid_key(id.trim()) {+ return Ok(id.trim().to_string());+ }+ }+ let mut random = [0u8; 8];+ getrandom::fill(&mut random).map_err(|error| error.to_string())?;+ let id = format!("{:016x}", u64::from_le_bytes(random));+ fs::write(&path, &id).map_err(|error| format!("Could not write {}: {error}", path.display()))?;+ Ok(id)+}++pub fn computer_name() -> String {+ std::env::var("COMPUTERNAME")+ .or_else(|_| std::env::var("HOSTNAME"))+ .ok()+ .filter(|name| !name.trim().is_empty())+ .unwrap_or_else(|| "Slopus worker".into())+}++/// Runs the worker until the process exits.+pub fn run(options: WorkerOptions) -> Result<(), String> {+ let data = data_directory(&options);+ fs::create_dir_all(&data).map_err(|error| format!("Could not create {}: {error}", data.display()))?;+ if let Err(error) = diagnostics::initialize_in(&data.join("logs")) {+ eprintln!("Could not initialize diagnostic logging: {error}");+ }+ let roots = crate::weights::worker_weight_roots(&options.weights, &data)?;+ let worker = Arc::new(Worker {+ id: worker_id(&data)?,+ name: options.name.clone().unwrap_or_else(computer_name),+ token: options.token.clone().filter(|token| !token.is_empty()),+ vulkan: options.vulkan,+ store: Arc::new(FileStore::new(&data, roots.clone())?),+ data,+ runtime: slopfab::SlopfabRuntime::default(),+ jobs: Mutex::default(),+ preparations: Mutex::default(),+ });+ let server = Server::http(("0.0.0.0", options.port)).map_err(|error| format!("Could not listen on port {}: {error}", options.port))?;+ let info = worker.info();+ println!("Slopus worker '{}' ({}) listening on port {}.", info.name, info.id, options.port);+ println!("Engine: {} {}", info.runtime.state, info.runtime.version.as_deref().unwrap_or(""));+ println!("Weights: {}", roots.iter().map(|root| root.display().to_string()).collect::<Vec<_>>().join("; "));+ println!("Data: {}", worker.data.display());+ if worker.token.is_some() {+ println!("Clients must enter this worker's access token.");+ }+ diagnostics::info("worker", "started", "Slopus worker started.", serde_json::json!({ "port": options.port, "id": info.id }));+ let _advertisement = if options.advertise {+ match discovery::advertise(&info, options.port) {+ Ok(daemon) => Some(daemon),+ Err(error) => {+ eprintln!("mDNS advertisement failed; clients must add this worker by address: {error}");+ None+ }+ }+ } else {+ None+ };+ for request in server.incoming_requests() {+ let worker = worker.clone();+ std::thread::spawn(move || worker.handle(request));+ }+ Ok(())+}++impl Worker {+ fn info(&self) -> WorkerInfo {+ let status = slopfab::status(&BTreeMap::new());+ let loaded = matches!(status.state, "ready" | "modelsMissing");+ WorkerInfo {+ id: self.id.clone(),+ name: self.name.clone(),+ version: env!("CARGO_PKG_VERSION").into(),+ protocol: PROTOCOL_VERSION,+ requires_token: self.token.is_some(),+ gpus: crate::weights::hardware_devices(),+ runtime: WorkerRuntime {+ state: if loaded { "ready".into() } else { status.state.into() },+ version: status.version,+ platform: if self.vulkan && loaded { Some("Vulkan".into()) } else { status.platform.map(Into::into) },+ cuda_available: status.cuda_available,+ cuda_device_names: status.cuda_device_names,+ detail: if loaded { format!("Engine loaded from {}.", status.dll_path) } else { status.detail },+ },+ }+ }++ fn handle(&self, mut request: Request) {+ let started = Instant::now();+ let method = request.method().clone();+ let url = request.url().to_string();+ let reply = self.route(&mut request, &method, &url);+ let response = match reply {+ Ok(response) => response,+ Err(HttpError(status, message)) => {+ if status >= 500 || !url.ends_with("/events") {+ diagnostics::warn("worker", "request.failed", &message, serde_json::json!({ "url": url, "status": status }));+ }+ let body = serde_json::to_vec(&serde_json::json!({ "error": message })).unwrap_or_default();+ bytes(body, "application/json").with_status_code(status)+ }+ };+ let _ = request.respond(response);+ if started.elapsed() > Duration::from_secs(30) && !url.contains("/events") {+ diagnostics::debug("worker", "request.slow", "Slow worker request.", serde_json::json!({ "url": url, "ms": started.elapsed().as_millis() as u64 }));+ }+ }++ fn route(&self, request: &mut Request, method: &Method, url: &str) -> Reply {+ let (path, query) = url.split_once('?').unwrap_or((url, ""));+ let parts: Vec<&str> = path.trim_matches('/').split('/').collect();+ if parts.first() != Some(&"v1") {+ return Err(HttpError(404, "Unknown worker route.".into()));+ }+ if !matches!(parts.as_slice(), ["v1", "info"]) {+ if let Some(token) = &self.token {+ if header(request, TOKEN_HEADER) != Some(token.as_str()) {+ return Err(HttpError(401, "This worker requires an access token. Enter it in Settings → Workers.".into()));+ }+ }+ }+ match (method, &parts[1..]) {+ (Method::Get, ["info"]) => json(&self.info()),+ (Method::Post, ["uploads", "missing"]) => {+ let files: Vec<FileRef> = body(request)?;+ json(&MissingUploads { missing: self.store.missing(&files)? })+ }+ (Method::Put, ["uploads", key, entry @ ..]) => {+ let entry = entry.iter().map(|part| percent_decode(part)).collect::<Result<Vec<_>, _>>()?.join("/");+ let bytes = request.body_length().ok_or("Uploads need a Content-Length.")? as u64;+ let key = key.to_string();+ self.store.receive(&key, &entry, bytes, request.as_reader())?;+ json(&true)+ }+ (Method::Post, ["prepare", id]) => {+ let files: Vec<FileRef> = body(request)?;+ self.start_preparation(id, files)?;+ json(&true)+ }+ (Method::Get, ["prepare", id]) => {+ let preparation = self.preparations.lock().map_err(|_| "Lock failed.")?.get(*id).cloned()+ .ok_or(HttpError(404, "No preparation with this id.".into()))?;+ let status = preparation.status.lock().map_err(|_| "Lock failed.")?.clone();+ json(&status)+ }+ (Method::Post, ["prepare", id, "cancel"]) => {+ if let Some(preparation) = self.preparations.lock().map_err(|_| "Lock failed.")?.get(*id) {+ preparation.cancel.store(true, Ordering::Relaxed);+ }+ json(&true)+ }+ (Method::Post, ["plan"]) => {+ let job: RemoteJob = body(request)?;+ let (request, settings) = self.materialize(job)?;+ json(&slopfab::resolve_plan(&request, &settings, &self.runtime.references)?)+ }+ (Method::Post, ["jobs", id]) => {+ let start: StartJob = body(request)?;+ self.start_job(id, start)?;+ json(&true)+ }+ (Method::Get, ["jobs", id, "events"]) => {+ let after = query.split('&').find_map(|pair| pair.strip_prefix("after=")).and_then(|value| value.parse().ok()).unwrap_or(0);+ let log = self.job(id)?;+ json(&log.after(after, Duration::from_secs(20)))+ }+ (Method::Post, ["jobs", id, "cancel"]) => json(&self.runtime.cancel(id)),+ (Method::Get, ["jobs", id, "summary"]) => {+ json(&rendered::summary(id).ok_or(HttpError(404, "The frames of this job are not in memory.".into()))?)+ }+ (Method::Get, ["jobs", id, "frames", index]) => {+ let index: u32 = index.parse().map_err(|_| "Invalid frame index.")?;+ let frame = rendered::frame(id, index)?.ok_or(HttpError(404, format!("Frame {index} of {id} is not in memory.")))?;+ Ok(bytes(frame, "application/octet-stream"))+ }+ (Method::Get, ["jobs", id, "audio"]) => {+ Ok(bytes(rendered::audio(id).ok_or(HttpError(404, "The audio of this job is not in memory.".into()))?, "application/octet-stream"))+ }+ (Method::Get, ["jobs", id, "latents"]) => {+ let log = self.job(id)?;+ let path = log.latents.clone().ok_or(HttpError(404, "This job saves no latents.".into()))?;+ let file = fs::File::open(&path).map_err(|error| HttpError(404, format!("Latents are unavailable: {error}")))?;+ let length = file.metadata().map(|meta| meta.len() as usize).ok();+ Ok(Response::new(200.into(), vec![Header::from_bytes("Content-Type", "application/octet-stream").unwrap()],+ Box::new(file) as Box<dyn Read + Send>, length, None))+ }+ (Method::Delete, ["jobs", id]) => {+ self.release_job(id);+ json(&true)+ }+ (Method::Post, ["reference-videos"]) => {+ #[derive(Deserialize)]+ #[serde(rename_all = "camelCase")]+ struct Create { duration_seconds: f64 }+ let create: Create = body(request)?;+ json(&self.runtime.references.create_reference_video(create.duration_seconds, &BTreeMap::new())?)+ }+ (Method::Post, ["reference-videos", "release"]) => {+ let ids: Vec<String> = body(request)?;+ self.runtime.references.release_reference_videos(&ids)?;+ json(&true)+ }+ (Method::Post, ["reference-videos", id, "frames"]) => {+ let width = parsed_header(request, "x-reference-width")?;+ let height = parsed_header(request, "x-reference-height")?;+ let time = parsed_header(request, "x-reference-time")?;+ let mut pixels = Vec::new();+ request.as_reader().take(512 * 1024 * 1024).read_to_end(&mut pixels).map_err(|error| error.to_string())?;+ self.runtime.references.append_reference_video(id, &pixels, width, height, time)?;+ json(&true)+ }+ (Method::Post, ["reference-videos", id, "audio"]) => {+ let channels = parsed_header(request, "x-reference-channels")?;+ let rate = parsed_header(request, "x-reference-rate")?;+ let mut samples = Vec::new();+ request.as_reader().take(1024 * 1024 * 1024).read_to_end(&mut samples).map_err(|error| error.to_string())?;+ self.runtime.references.set_reference_video_audio(id, &samples, channels, rate)?;+ json(&true)+ }+ _ => Err(HttpError(404, "Unknown worker route.".into())),+ }+ }++ fn job(&self, id: &str) -> Result<Arc<JobLog>, HttpError> {+ self.jobs.lock().map_err(|_| "Lock failed.")?.get(id).cloned().ok_or(HttpError(404, "No job with this id.".into()))+ }++ fn start_preparation(&self, id: &str, files: Vec<FileRef>) -> Result<(), String> {+ generated_file_stem(id)?;+ let preparation = Arc::new(Preparation {+ status: Mutex::new(FilePreparation { state: "running".into(), ..Default::default() }),+ cancel: Arc::new(AtomicBool::new(false)),+ });+ {+ let mut preparations = self.preparations.lock().map_err(|_| "Lock failed.")?;+ // Finished entries are only read once; keep the map small.+ preparations.retain(|_, entry| entry.status.lock().is_ok_and(|status| status.state == "running"));+ if preparations.contains_key(id) {+ return Err("Files for this job are already being prepared.".into());+ }+ preparations.insert(id.to_string(), preparation.clone());+ }+ let store = self.store.clone();+ let shared = preparation;+ std::thread::spawn(move || {+ let result = store.prepare(&files, &shared.cancel, |status| {+ if let Ok(mut current) = shared.status.lock() {+ *current = status.clone();+ }+ });+ if let Ok(mut status) = shared.status.lock() {+ match result {+ Ok(()) => status.state = "done".into(),+ Err(_) if shared.cancel.load(Ordering::Relaxed) => status.state = "cancelled".into(),+ Err(error) => {+ status.state = "failed".into();+ status.error = Some(error);+ }+ }+ }+ });+ Ok(())+ }++ /// Turns a client's job into one this machine can run: tokens become this+ /// worker's own files, and machine choices are this worker's.+ fn materialize(&self, job: RemoteJob) -> Result<(slopfab::GenerationRequest, BTreeMap<String, ProviderSetting>), String> {+ let paths = job.files.iter().map(|file| self.store.resolve(file)).collect::<Result<Vec<_>, _>>()?;+ let mut request = job.request;+ let mut settings = job.settings;+ settings.retain(|name, _| name == "slopfab");+ visit_paths(&mut request, &mut settings, |_, value| {+ let index = parse_file_token(value).ok_or("A worker job can only name files sent with it.")?;+ *value = paths.get(index).ok_or("A worker job names a file it did not send.")?.to_string_lossy().into_owned();+ Ok(())+ })?;+ if let Some(slopfab) = settings.get_mut("slopfab") {+ // The backend is a property of the computer that runs the job.+ slopfab.options.remove("inferenceBackend");+ if self.vulkan {+ slopfab.options.insert("inferenceBackend".into(), ProviderOption::String("vulkan".into()));+ }+ }+ let file = |index: Option<usize>| index.map(|index| paths.get(index).cloned().ok_or("A worker job names a file it did not send.")).transpose();+ request.continuation_path = file(job.continuation)?;+ request.image_edit_path = file(job.image_edit_source)?;+ if request.image_edit.is_some() && request.image_edit_path.is_none() {+ return Err("Image edits require their source image.".into());+ }+ request.image_edit_pixels = None;+ request.save_latents_path = None;+ Ok((request, settings))+ }++ fn start_job(&self, id: &str, start: StartJob) -> Result<(), String> {+ generated_file_stem(id)?;+ if start.job.request.job_id != id {+ return Err("The job id does not match its request.".into());+ }+ self.prune_jobs();+ let (mut request, settings) = self.materialize(start.job)?;+ let latents = if start.save_latents && !request.still_image {+ let directory = self.data.join("jobs").join(id);+ let _ = fs::remove_dir_all(&directory);+ fs::create_dir_all(&directory).map_err(|error| error.to_string())?;+ Some(directory.join("latents.safetensors"))+ } else {+ None+ };+ request.save_latents_path = latents.clone();+ let log = Arc::new(JobLog { events: Mutex::default(), changed: Condvar::new(), latents, created: Instant::now() });+ {+ let mut jobs = self.jobs.lock().map_err(|_| "Lock failed.")?;+ if jobs.contains_key(id) {+ return Err("This job was already started on the worker.".into());+ }+ jobs.insert(id.to_string(), log.clone());+ }+ let sink = log.clone();+ let result = self.runtime.enqueue(+ Arc::new(move |event| match event {+ slopfab::NativeEvent::Job(value) => sink.push("job", serde_json::to_value(value).unwrap_or_default()),+ slopfab::NativeEvent::Progress(value) => sink.push("progress", serde_json::to_value(value).unwrap_or_default()),+ }),+ request,+ &settings,+ );+ if result.is_err() {+ self.release_job(id);+ }+ result+ }++ fn release_job(&self, id: &str) {+ rendered::release(id);+ if let Ok(mut jobs) = self.jobs.lock() {+ jobs.remove(id);+ }+ if generated_file_stem(id).is_ok() {+ let _ = fs::remove_dir_all(self.data.join("jobs").join(id));+ }+ }++ /// Clients release their jobs; this catches the ones that vanished.+ fn prune_jobs(&self) {+ let stale: Vec<String> = self.jobs.lock().map(|jobs| jobs.iter()+ .filter(|(_, log)| log.created.elapsed() > Duration::from_secs(12 * 60 * 60))+ .map(|(id, _)| id.clone()).collect()).unwrap_or_default();+ for id in stale {+ self.release_job(&id);+ }+ }+}++fn percent_decode(part: &str) -> Result<String, String> {+ let bytes = part.as_bytes();+ let mut decoded = Vec::with_capacity(bytes.len());+ let mut index = 0;+ while index < bytes.len() {+ if bytes[index] == b'%' {+ let hex = part.get(index + 1..index + 3).ok_or("Invalid escape in upload path.")?;+ decoded.push(u8::from_str_radix(hex, 16).map_err(|_| "Invalid escape in upload path.")?);+ index += 3;+ } else {+ decoded.push(bytes[index]);+ index += 1;+ }+ }+ String::from_utf8(decoded).map_err(|_| "Upload paths must be UTF-8.".into())+}diff --git a/src-tauri/src/worker/store.rs b/src-tauri/src/worker/store.rsnew file mode 100644index 0000000..8c00c90--- /dev/null+++ b/src-tauri/src/worker/store.rs@@ -0,0 +1,213 @@+//! Files a worker holds for its clients: weights it downloaded itself, using+//! the same downloader and folders as the desktop app, and files clients+//! uploaded because they exist only on the client's disk.+use super::protocol::{safe_relative_path, valid_key, FilePreparation, FileRef};+use crate::weights;+use std::{+ fs::{self, File},+ io::{self, Read, Write},+ path::{Path, PathBuf},+ sync::{atomic::AtomicBool, Mutex},+};++pub(crate) struct FileStore {+ uploads: PathBuf,+ weight_roots: Vec<PathBuf>,+ /// One download at a time, like the desktop app; parallel clients that+ /// need the same weights then find the finished file instead.+ downloads: Mutex<()>,+}++impl FileStore {+ pub(crate) 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(()) })+ }++ fn upload_root(&self, key: &str) -> Result<PathBuf, String> {+ if !valid_key(key) {+ return Err("Invalid upload key.".into());+ }+ Ok(self.uploads.join(key))+ }++ fn entry_path(&self, key: &str, entry: &str) -> Result<PathBuf, String> {+ Ok(self.upload_root(key)?.join(safe_relative_path(entry)?))+ }++ /// Upload entries this worker does not hold yet.+ pub(crate) 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 {+ for entry in entries {+ let path = self.entry_path(key, &entry.path)?;+ if !fs::metadata(&path).is_ok_and(|meta| meta.is_file() && meta.len() == entry.bytes) {+ missing.push((key.clone(), entry.path.clone()));+ }+ }+ }+ }+ Ok(missing)+ }++ /// 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> {+ 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())?;+ let mut random = [0u8; 8];+ getrandom::fill(&mut random).map_err(|error| error.to_string())?;+ let temporary = directory.join(format!(".upload-{:x}.part", u64::from_le_bytes(random)));+ let result = (|| {+ let mut file = File::create(&temporary).map_err(|error| error.to_string())?;+ let copied = io::copy(&mut body.take(bytes), &mut file).map_err(|error| format!("Upload interrupted: {error}"))?;+ if copied != bytes {+ return Err(format!("Upload incomplete: received {copied} of {bytes} bytes."));+ }+ file.flush().map_err(|error| error.to_string())?;+ drop(file);+ if destination.exists() {+ fs::remove_file(&destination).map_err(|error| error.to_string())?;+ }+ fs::rename(&temporary, &destination).map_err(|error| error.to_string())+ })();+ let _ = fs::remove_file(&temporary);+ result+ }++ /// 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> {+ match file {+ FileRef::Url { url, lora } => {+ let path = weights::cached_weight(url, &self.weight_roots)+ .ok_or_else(|| format!("The worker has not downloaded {url} yet."))?;+ if *lora && !prepared(&path) {+ return Err(format!("The worker has not prepared the LoRA {url} yet."));+ }+ Ok(path)+ }+ FileRef::Upload { key, name, directory, entries, .. } => {+ if !self.missing(std::slice::from_ref(file))?.is_empty() {+ return Err(format!("{name} has not been uploaded to the worker."));+ }+ if *directory {+ self.upload_root(key)+ } else {+ match entries.as_slice() {+ [entry] if entry.path == *name => self.entry_path(key, name),+ _ => Err(format!("{name} must be uploaded as one file.")),+ }+ }+ }+ }+ }++ /// Downloads and prepares everything `files` names that is not here yet.+ pub(crate) fn prepare(+ &self,+ files: &[FileRef],+ cancelled: &AtomicBool,+ mut report: impl FnMut(&FilePreparation),+ ) -> Result<(), String> {+ let downloads: Vec<_> = files.iter().filter_map(|file| match file {+ FileRef::Url { url, lora } => Some((url, *lora)),+ FileRef::Upload { .. } => None,+ }).collect();+ let mut status = FilePreparation { state: "running".into(), files: downloads.len(), ..Default::default() };+ report(&status);+ for (url, lora) in downloads {+ status.file = Some(url.clone());+ status.downloaded = 0;+ status.total = None;+ report(&status);+ let path = match weights::cached_weight(url, &self.weight_roots) {+ Some(path) => path,+ None => {+ let _one_at_a_time = self.downloads.lock().map_err(|_| "Download lock failed.")?;+ weights::fetch_weight(url, &self.weight_roots, cancelled, |downloaded, total| {+ status.downloaded = downloaded;+ status.total = total;+ report(&status);+ })?+ }+ };+ if lora && !prepared(&path) {+ // Same preparation the desktop app runs after a LoRA download.+ // Wait for running jobs rather than failing like the UI does.+ let _models = crate::slopfab::MODEL_ACCESS.write().map_err(|_| "Model access lock failed.")?;+ weights::lora::prepare_exclusive(&path, 2688, true)?;+ mark_prepared(&path)?;+ }+ status.completed += 1;+ report(&status);+ }+ Ok(())+ }+}++fn prepared_marker(path: &Path) -> PathBuf {+ path.with_extension("worker-prepared")+}++fn prepared(path: &Path) -> bool {+ let size = fs::metadata(path).map(|meta| meta.len()).ok();+ fs::read_to_string(prepared_marker(path)).ok().and_then(|text| text.trim().parse().ok()) == size+}++fn mark_prepared(path: &Path) -> Result<(), String> {+ let size = fs::metadata(path).map_err(|error| error.to_string())?.len();+ fs::write(prepared_marker(path), size.to_string()).map_err(|error| error.to_string())+}++#[cfg(test)]+mod tests {+ use super::*;+ use crate::worker::protocol::UploadEntry;++ fn upload(name: &str, directory: bool, entries: &[(&str, u64)]) -> FileRef {+ FileRef::Upload {+ key: "00112233aabbccdd".into(), name: name.into(), directory, lora: false,+ entries: entries.iter().map(|(path, bytes)| UploadEntry { path: path.to_string(), bytes: *bytes }).collect(),+ }+ }++ #[test]+ fn uploads_resolve_only_once_every_byte_has_arrived() {+ let folder = tempfile::tempdir().unwrap();+ let store = FileStore::new(folder.path(), vec![folder.path().join("weights")]).unwrap();+ let file = upload("a.png", false, &[("a.png", 4)]);+ assert_eq!(store.missing(std::slice::from_ref(&file)).unwrap().len(), 1);+ assert!(store.resolve(&file).is_err());+ assert!(store.receive("00112233aabbccdd", "a.png", 4, &mut &b"da"[..]).is_err());+ assert!(store.resolve(&file).is_err());+ store.receive("00112233aabbccdd", "a.png", 4, &mut &b"data"[..]).unwrap();+ assert!(store.missing(std::slice::from_ref(&file)).unwrap().is_empty());+ assert_eq!(fs::read(store.resolve(&file).unwrap()).unwrap(), b"data");+ }++ #[test]+ fn folders_resolve_to_their_root_and_refuse_escaping_entries() {+ let folder = tempfile::tempdir().unwrap();+ let store = FileStore::new(folder.path(), Vec::new()).unwrap();+ let tokenizer = upload("tokenizer", true, &[("vocab.json", 2), ("nested/merges.txt", 1)]);+ store.receive("00112233aabbccdd", "vocab.json", 2, &mut &b"{}"[..]).unwrap();+ store.receive("00112233aabbccdd", "nested/merges.txt", 1, &mut &b"m"[..]).unwrap();+ let root = store.resolve(&tokenizer).unwrap();+ assert!(root.join("nested").join("merges.txt").is_file());+ assert!(store.receive("00112233aabbccdd", "../escape", 1, &mut &b"x"[..]).is_err());+ assert!(store.receive("../../x", "a", 1, &mut &b"x"[..]).is_err());+ assert!(!folder.path().join("escape").exists());+ }++ #[test]+ fn urls_must_be_downloaded_before_they_resolve() {+ let folder = tempfile::tempdir().unwrap();+ let store = FileStore::new(folder.path(), vec![folder.path().join("weights")]).unwrap();+ let file = FileRef::Url { url: "https://example.com/model.safetensors".into(), lora: false };+ assert!(store.resolve(&file).unwrap_err().contains("not downloaded"));+ }+}