Unrestricted Open-source alternative to AI video platforms — Free AI image & video generation studio with 600+ models (Flux, Midjourney, Kling, Sora, Veo). No content filters. Self-hosted, MIT licensed.
JavaScript ai-art-generatorai-image-generationai-video-generationfal-ai-alternativegenerative-aiimage-to-videoinvideo-alternativekling-ailipsynclitellm-alternativemidjourney-alternativemuapiopen-sourceopenrouter-alternativeseedance2sora-alternativetext-to-imagetext-to-videouncensoredwan-video
feat(video): run local Wan2GP models from Video Studio
hakim16098b6c04e779a0
diff --git a/README.md b/README.mdindex 65439090..ebed7465 100644--- a/README.md+++ b/README.md@@ -185,7 +185,7 @@ The desktop app supports **two independent local engines**. Pick whichever fits| Engine | What it is | Best for ||---|---|---|| **sd.cpp** (bundled) | C++ engine from [stable-diffusion.cpp](https://github.com/leejet/stable-diffusion.cpp), runs on the same machine as the app. Metal GPU on Apple Silicon, CUDA/Vulkan/ROCm on Linux/Windows. | Image-only models. Works on Mac M-series. |-| **Wan2GP** (BYO server) | HTTP client to a user-run [Wan2GP](https://github.com/deepbeepmeep/Wan2GP) server. The server runs Python + PyTorch on a CUDA/ROCm GPU; the desktop app only sends prompts and receives results. | Video models (Wan 2.2, Hunyuan, LTX) and large image models (Flux, Qwen-Image). NVIDIA/AMD GPU required on the *server*; the desktop app itself can run on a Mac. |+| **Wan2GP** (BYO install) | A user-installed [Wan2GP](https://github.com/deepbeepmeep/Wan2GP): either its folder on this machine (the app runs `wgp.py` itself) or its MCP server on another machine. Python + PyTorch on a CUDA/ROCm GPU. | Video models (Wan 2.1/2.2, Hunyuan, LTX-2) in **Video Studio** and large image models (Flux, Qwen-Image). NVIDIA/AMD GPU required on the Wan2GP machine; the desktop app itself can run on a Mac. |Both engines share the same UI: open **Settings → Local Models** to configure each.@@ -225,29 +225,38 @@ inside that directory, and **Settings -> Local Models** shows the resolved modelLocal engine output and download errors are written to the app process console, so launchfrom Terminal or PowerShell when you need troubleshooting logs.-### Engine 2 — Wan2GP (remote Gradio server)+### Engine 2 — Wan2GP (your own install)-The app does **not** bundle Python or model weights for Wan2GP. You run Wan2GP yourself on a machine with a CUDA or ROCm GPU and point the desktop app at its URL.+The app does **not** bundle Python or model weights for Wan2GP. Install Wan2GP yourself on a machine with a CUDA or ROCm GPU:```bash-# On your GPU machinegit clone https://github.com/deepbeepmeep/Wan2GPcd Wan2GP./install.sh # or install.bat on Windows-python wgp.py --listen --server-name 0.0.0.0 # binds to all interfaces```-Then in the desktop app: **Settings → Local Models → Wan2GP server**, paste the URL (e.g. `http://192.168.1.42:7860`), click **Test**, then **Save**. The Wan2GP models become available — image models in **Image Studio**, video models reachable via the same generation API (Image Studio rejects video output explicitly; full Video Studio wiring is on the roadmap).+**Same machine (recommended).** In the desktop app open **Settings → Local Models → Wan2GP**, set the Wan2GP folder (Python defaults to `<folder>/venv/bin/python`), click **Save**, then **Check** — it runs `wgp.py --dry-run` on a test task. Each generation runs Wan2GP's headless CLI (`wgp.py --process settings.json`); weights download automatically the first time a model is used, and the download shows up in the progress bar.-| Model | Type | Notes |-|---|---|---|-| **Flux.1 Dev** | Image | 1024px, 28 steps |-| **Qwen Image** | Image | 1024px, 30 steps |-| **Wan 2.2 (T2V / I2V)** | Video | Slow on consumer GPUs |-| **Hunyuan Video** | Video | High-quality T2V |-| **LTX Video** | Video | Fastest video option |+**Another machine.** Start WanGP's MCP server there and put its URL (e.g. `http://192.168.1.42:7866/mcp`) in the same settings panel:++```bash+python wgp.py --mcp --mcp-api-version 1 --mcp-transport streamable-http --mcp-host 0.0.0.0 --mcp-port 7866+```++Use `--mcp-host 0.0.0.0` only on a trusted network (see Wan2GP's `docs/AUTHENTICATION.md` for OAuth/HTTPS). The Gradio web UI port (7860) is **not** an API and cannot be used here.++Then switch **Video Studio** to **⚡ Local** and pick a model. Models your Wan2GP version doesn't have are listed with the reason; models whose weights aren't downloaded yet are marked.++| Model | WanGP `model_type` | Type | Notes |+|---|---|---|---|+| **Wan 2.1 1.3B** | `t2v_1.3B` | Video | Lightest option, 480p, ~8 GB VRAM |+| **Wan 2.2 14B T2V / I2V** | `t2v_2_2` / `i2v_2_2` | Video | 480p, slow on consumer GPUs; I2V needs a start frame |+| **Hunyuan Video 1.5** | `hunyuan_1_5_480_t2v` | Video | 480p T2V |+| **LTX-2 Distilled** | `ltx2_22B_distilled` | Video | 8 steps, 720p |+| **Flux.1 Dev** | `flux` | Image | Image Studio |+| **Qwen Image** | `qwen_image_20B` | Image | Image Studio |-> **Why a separate server?** Wan2GP's runtime (Sage attention, flash-attn, AWQ/GGUF kernels) is CUDA-only — there is no MPS / Apple Silicon path. Treating it as a remote server lets a Mac-only user keep the desktop app while offloading inference to a Linux/Windows GPU box, a gaming PC on the LAN, or a rented RunPod/vast.ai instance.+> **Why the remote option?** Wan2GP's runtime (Sage attention, flash-attn, AWQ/GGUF kernels) is CUDA/ROCm-only — there is no MPS / Apple Silicon path. The MCP server lets a Mac-only user keep the desktop app while offloading inference to a Linux/Windows GPU box, a gaming PC on the LAN, or a rented RunPod/vast.ai instance.> **Local inference is only available in the desktop app.** The hosted web version always uses cloud APIs.@@ -289,7 +298,7 @@ A healthy run on Apple Silicon prints `total params memory size = 1969.78MB (VRA## ✨ Features- **Image Studio** — Generate images from text prompts (50+ text-to-image models) or transform existing images (55+ image-to-image models). Switches model set automatically based on whether a reference image is provided. Quality and resolution controls visible for models that support them.-- **Local Inference** — Two engines: **sd.cpp** (bundled, runs on Mac/Win/Linux with Metal/CUDA/Vulkan/ROCm) for SD 1.5, SDXL, and Z-Image; and **Wan2GP** (BYO Gradio server) for Flux, Qwen-Image, and video models (Wan 2.2, Hunyuan, LTX). Configure both in Settings → Local Models.+- **Local Inference** — Two engines: **sd.cpp** (bundled, runs on Mac/Win/Linux with Metal/CUDA/Vulkan/ROCm) for SD 1.5, SDXL, and Z-Image; and **Wan2GP** (your own install — local folder or remote MCP server) for Flux, Qwen-Image, and video models in Video Studio (Wan 2.1/2.2, Hunyuan, LTX-2). Configure both in Settings → Local Models.- **Multi-Image Input** — Upload up to 14 reference images for compatible edit models (Nano Banana 2 Edit, Flux Kontext Dev, GPT-4o Edit, and more). Multi-select picker with order badges, batch upload, and a "Use Selected" confirmation flow.- **Video Studio** — Generate videos from text prompts (40+ text-to-video models) or animate a start-frame image (60+ image-to-video models). Same intelligent mode switching as Image Studio.- **Audio Studio** — Generate and edit AI audio/music from text prompts.diff --git a/electron/lib/wan2gpCatalog.js b/electron/lib/wan2gpCatalog.jsnew file mode 100644index 00000000..1283105d--- /dev/null+++ b/electron/lib/wan2gpCatalog.js@@ -0,0 +1,139 @@+// Wan2GP model catalog + settings builder.+// `modelType` is the real WanGP model_type: the basename of <Wan2GP>/defaults/<modelType>.json.+// Pure module (no electron import) so it can be unit-tested with node:test.++const RES_480P = { '16:9': '832x480', '9:16': '480x832', '1:1': '720x720', '4:3': '832x624', '3:4': '624x832' };+const RES_720P = { '16:9': '1280x720', '9:16': '720x1280', '1:1': '960x960', '4:3': '1104x832', '3:4': '832x1104' };+const RES_IMAGE = { '1:1': '1024x1024', '16:9': '1280x720', '9:16': '720x1280', '4:3': '1104x832', '3:4': '832x1104' };++const WAN2GP_CATALOG = [+ {+ id: 'wan2gp:flux-dev',+ name: 'Flux.1 Dev (Wan2GP)',+ description: 'Image — FLUX.1 dev (12B) served by Wan2GP.',+ type: 'image',+ family: 'flux',+ provider: 'wan2gp',+ modelType: 'flux',+ resolutions: RES_IMAGE,+ aspectRatios: ['1:1', '4:3', '3:4', '16:9', '9:16'],+ tags: ['image', 'flux'],+ },+ {+ id: 'wan2gp:qwen-image',+ name: 'Qwen Image (Wan2GP)',+ description: 'Image — Qwen-Image 20B text-to-image served by Wan2GP.',+ type: 'image',+ family: 'qwen',+ provider: 'wan2gp',+ modelType: 'qwen_image_20B',+ resolutions: RES_IMAGE,+ aspectRatios: ['1:1', '4:3', '3:4', '16:9', '9:16'],+ tags: ['image', 'qwen'],+ },+ {+ id: 'wan2gp:wan21-t2v-1.3b',+ name: 'Wan 2.1 1.3B (Text-to-Video)',+ description: 'Video — lightest Wan model, 480p. Runs on ~8 GB VRAM; a 5 s clip takes a few minutes.',+ type: 'video',+ family: 'wan',+ provider: 'wan2gp',+ modelType: 't2v_1.3B',+ resolutions: RES_480P,+ aspectRatios: ['16:9', '9:16', '1:1'],+ tags: ['video', 'wan', 'text-to-video', 'fast'],+ },+ {+ id: 'wan2gp:wan22-t2v',+ name: 'Wan 2.2 14B (Text-to-Video)',+ description: 'Video — Wan 2.2 text-to-video, 480p. Slow on consumer GPUs.',+ type: 'video',+ family: 'wan',+ provider: 'wan2gp',+ modelType: 't2v_2_2',+ resolutions: RES_480P,+ aspectRatios: ['16:9', '9:16', '1:1'],+ tags: ['video', 'wan', 'text-to-video'],+ },+ {+ id: 'wan2gp:wan22-i2v',+ name: 'Wan 2.2 14B (Image-to-Video)',+ description: 'Video — Wan 2.2 image-to-video, 480p. Provide a start frame.',+ type: 'video',+ family: 'wan',+ provider: 'wan2gp',+ modelType: 'i2v_2_2',+ needsImage: true,+ resolutions: RES_480P,+ aspectRatios: ['16:9', '9:16', '1:1'],+ tags: ['video', 'wan', 'image-to-video'],+ },+ {+ id: 'wan2gp:hunyuan-video',+ name: 'Hunyuan Video 1.5 (Wan2GP)',+ description: 'Video — Hunyuan Video 1.5 text-to-video, 480p.',+ type: 'video',+ family: 'hunyuan',+ provider: 'wan2gp',+ modelType: 'hunyuan_1_5_480_t2v',+ resolutions: RES_480P,+ aspectRatios: ['16:9', '9:16', '1:1'],+ tags: ['video', 'hunyuan'],+ },+ {+ id: 'wan2gp:ltx-video',+ name: 'LTX-2 Distilled (Wan2GP)',+ description: 'Video — LTX-2 distilled, 8 steps, 720p. Fastest large video model in Wan2GP.',+ type: 'video',+ family: 'ltx',+ provider: 'wan2gp',+ modelType: 'ltx2_22B_distilled',+ resolutions: RES_720P,+ aspectRatios: ['16:9', '9:16', '1:1'],+ tags: ['video', 'ltx', 'fast'],+ },+];++function getModelById(id) {+ return WAN2GP_CATALOG.find(m => m.id === id) || null;+}++function toInt(value) {+ if (value === null || value === undefined || value === '') return null;+ const n = Number(value);+ return Number.isFinite(n) ? Math.round(n) : null;+}++// Build the settings object passed to `wgp.py --process` (local) or `wangp_generate` (MCP).+// Only fields the user actually chose are set; everything else falls back to the+// model's own defaults in Wan2GP, which are tuned per model (distilled models use+// few steps, LTX has its own frame count, etc.).+function buildWan2gpSettings(model, params = {}, { imageStart } = {}) {+ if (!model?.modelType) throw new Error('Unknown Wan2GP model');+ if (model.needsImage && !imageStart) {+ throw new Error(`${model.name} requires a start-frame image — upload one first.`);+ }++ const settings = {+ model_type: model.modelType,+ prompt: params.prompt || '',+ };+ if (params.negative_prompt) settings.negative_prompt = params.negative_prompt;++ const resolution = model.resolutions?.[params.aspect_ratio] || model.resolutions?.[model.aspectRatios?.[0]];+ if (resolution) settings.resolution = resolution;++ const steps = toInt(params.steps);+ if (steps && steps > 0) settings.num_inference_steps = steps;++ const frames = toInt(params.video_length);+ if (model.type === 'video' && frames && frames > 0) settings.video_length = frames;++ const seed = toInt(params.seed);+ settings.seed = seed !== null && seed >= 0 ? seed : Math.floor(Math.random() * 2147483647);++ if (imageStart) settings.image_start = imageStart;+ return settings;+}++module.exports = { WAN2GP_CATALOG, getModelById, buildWan2gpSettings };diff --git a/electron/lib/wan2gpLocal.js b/electron/lib/wan2gpLocal.jsnew file mode 100644index 00000000..5019bb13--- /dev/null+++ b/electron/lib/wan2gpLocal.js@@ -0,0 +1,206 @@+// Local Wan2GP engine: runs `wgp.py --process settings.json` from the user's+// Wan2GP folder. No electron import — the provider passes paths in.++const fs = require('fs');+const path = require('path');+const { spawn } = require('child_process');+const { createProgressParser, summarizeRun, tailLines } = require('./wan2gpOutput');++const MEDIA_EXTENSIONS = new Set(['.mp4', '.mkv', '.mov', '.webm', '.png', '.jpg', '.jpeg', '.webp']);+const OUTPUT_CAP = 2 * 1024 * 1024; // keep the last 2 MB of console output++function defaultPythonPath(dir, platform = process.platform) {+ return platform === 'win32'+ ? path.join(dir, 'venv', 'Scripts', 'python.exe')+ : path.join(dir, 'venv', 'bin', 'python');+}++function resolveInstall({ dir, python } = {}, platform = process.platform) {+ const root = (dir || '').trim();+ if (!root) return null;+ return {+ dir: root,+ python: (python || '').trim() || defaultPythonPath(root, platform),+ wgpPath: path.join(root, 'wgp.py'),+ defaultsDir: path.join(root, 'defaults'),+ ckptsDir: path.join(root, 'ckpts'),+ };+}++function checkInstall(install) {+ if (!install) return { ok: false, error: 'Wan2GP folder not set' };+ if (!fs.existsSync(install.dir)) return { ok: false, error: `Folder not found: ${install.dir}` };+ if (!fs.existsSync(install.wgpPath)) return { ok: false, error: `wgp.py not found in ${install.dir} — is this the Wan2GP folder?` };+ if (!fs.existsSync(install.python)) return { ok: false, error: `Python not found: ${install.python}` };+ return { ok: true };+}++// Status of each requested model type in the installed Wan2GP:+// 'available' — <dir>/defaults/<type>.json exists and one of its weight files is in ckpts/+// 'missing' — the model is defined but its weights are not downloaded yet+// 'installed' — defined, weight files could not be determined+// (absent) — no defaults/<type>.json: this Wan2GP version lacks the model+function localModelStatus(install, modelTypes) {+ const statuses = new Map();+ for (const modelType of modelTypes) {+ const defPath = path.join(install.defaultsDir, `${modelType}.json`);+ if (!fs.existsSync(defPath)) continue;+ let urls = null;+ try { urls = JSON.parse(fs.readFileSync(defPath, 'utf-8'))?.model?.URLs; } catch { /* unreadable def */ }+ if (!Array.isArray(urls) || urls.length === 0) {+ statuses.set(modelType, 'installed');+ continue;+ }+ const onDisk = urls.some(u => fs.existsSync(path.join(install.ckptsDir, path.basename(String(u)))));+ statuses.set(modelType, onDisk ? 'available' : 'missing');+ }+ return statuses;+}++function dirSize(dir) {+ let total = 0;+ let entries;+ try { entries = fs.readdirSync(dir, { withFileTypes: true }); } catch { return 0; }+ for (const entry of entries) {+ const full = path.join(dir, entry.name);+ if (entry.isDirectory()) total += dirSize(full);+ else if (entry.isFile()) {+ try { total += fs.statSync(full).size; } catch { /* file moved mid-scan */ }+ }+ }+ return total;+}++function newestMediaFile(dir) {+ let best = null;+ let entries;+ try { entries = fs.readdirSync(dir); } catch { return null; }+ for (const name of entries) {+ if (!MEDIA_EXTENSIONS.has(path.extname(name).toLowerCase())) continue;+ const full = path.join(dir, name);+ const mtime = fs.statSync(full).mtimeMs;+ if (!best || mtime > best.mtime) best = { full, mtime };+ }+ return best?.full || null;+}++// Kill wgp.py together with any children it spawned (ffmpeg, compile workers).+function killTree(child) {+ if (!child || child.exitCode !== null || child.signalCode !== null) return;+ if (process.platform === 'win32') {+ spawn('taskkill', ['/pid', String(child.pid), '/T', '/F'], { stdio: 'ignore' });+ return;+ }+ try { process.kill(-child.pid, 'SIGTERM'); } catch { /* already gone */ }+ setTimeout(() => {+ if (child.exitCode === null && child.signalCode === null) {+ try { process.kill(-child.pid, 'SIGKILL'); } catch { /* already gone */ }+ }+ }, 5000).unref();+}++function spawnWgp(install, args) {+ return spawn(install.python, [install.wgpPath, ...args], {+ cwd: install.dir,+ env: { ...process.env, PYTHONUNBUFFERED: '1' },+ // Own process group on POSIX so cancellation can signal the whole tree.+ detached: process.platform !== 'win32',+ });+}++function collectOutput(child, onChunk) {+ let output = '';+ const handle = (data) => {+ const text = data.toString();+ output = (output + text).slice(-OUTPUT_CAP);+ onChunk?.(text);+ };+ child.stdout.on('data', handle);+ child.stderr.on('data', handle);+ return () => output;+}++// Starts a generation. Returns { child, done } where `done` resolves to the+// media path or rejects with an Error carrying the log tail.+function runGeneration({ install, settings, runDir, onProgress }) {+ const outDir = path.join(runDir, 'out');+ fs.mkdirSync(outDir, { recursive: true });+ const settingsPath = path.join(runDir, 'settings.json');+ fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 2));++ const child = spawnWgp(install, ['--process', settingsPath, '--output-dir', outDir]);+ const parse = createProgressParser();+ let modelReady = false;+ const getOutput = collectOutput(child, (text) => {+ for (const event of parse(text)) {+ if (event.status !== 'loading') modelReady = true;+ onProgress?.(event);+ }+ });++ // WanGP prints nothing while it downloads weights in CLI mode, so report+ // download progress from the growth of <Wan2GP>/ckpts until the model loads.+ const baseline = dirSize(install.ckptsDir);+ const downloadTimer = setInterval(() => {+ if (modelReady) return;+ const downloaded = dirSize(install.ckptsDir) - baseline;+ if (downloaded > 0) onProgress?.({ status: 'downloading', downloadedBytes: downloaded, progress: 0.02 });+ }, 2000);++ const done = new Promise((resolve, reject) => {+ child.on('error', (err) => {+ clearInterval(downloadTimer);+ reject(new Error(`Could not start Wan2GP (${install.python}): ${err.message}`));+ });+ child.on('close', (exitCode, signal) => {+ clearInterval(downloadTimer);+ const output = getOutput();+ fs.writeFileSync(path.join(runDir, 'wgp.log'), output);+ const result = summarizeRun({ exitCode, signal, output });+ if (!result.ok) {+ reject(new Error(result.error));+ return;+ }+ const mediaPath = (result.savedPath && fs.existsSync(result.savedPath)) ? result.savedPath : newestMediaFile(outDir);+ if (!mediaPath) {+ reject(new Error(`Wan2GP reported success but no media file was found in ${outDir}.\n\n${tailLines(output)}`));+ return;+ }+ resolve(mediaPath);+ });+ });++ return { child, done };+}++// `wgp.py --process <settings> --dry-run`: validates the install and settings+// without generating. Resolves { ok, message }.+function dryRun({ install, settings, runDir }) {+ fs.mkdirSync(runDir, { recursive: true });+ const settingsPath = path.join(runDir, 'dry-run.json');+ fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 2));+ const child = spawnWgp(install, ['--process', settingsPath, '--dry-run']);+ const getOutput = collectOutput(child);+ return new Promise((resolve) => {+ child.on('error', (err) => resolve({ ok: false, message: `Could not start ${install.python}: ${err.message}` }));+ child.on('close', (exitCode) => {+ const output = getOutput();+ const m = output.match(/Validation complete\.\s*(\d+)\/(\d+) task/);+ if (exitCode === 0 && m && m[1] === m[2] && Number(m[2]) > 0) {+ resolve({ ok: true, message: 'Wan2GP validated a test task.' });+ } else {+ resolve({ ok: false, message: tailLines(output, 8) || `Exited with code ${exitCode}` });+ }+ });+ });+}++module.exports = {+ checkInstall,+ defaultPythonPath,+ dryRun,+ killTree,+ localModelStatus,+ resolveInstall,+ runGeneration,+};diff --git a/electron/lib/wan2gpMcp.js b/electron/lib/wan2gpMcp.jsnew file mode 100644index 00000000..0a87c6fc--- /dev/null+++ b/electron/lib/wan2gpMcp.js@@ -0,0 +1,193 @@+// Remote Wan2GP engine: WanGP's MCP server (API v1, Streamable HTTP), started with+// python wgp.py --mcp --mcp-api-version 1 --mcp-transport streamable-http --mcp-host 0.0.0.0 --mcp-port 7866+// and reached at http://<host>:7866/mcp. Gradio's UI endpoints are not an API —+// this is the only remote surface WanGP documents (docs/API.md → "MCP Server").++const PROTOCOL_VERSION = '2025-06-18';+const POLL_INTERVAL_MS = 2000;++function normalizeMcpUrl(url) {+ const trimmed = (url || '').trim().replace(/\/+$/, '');+ if (!trimmed) return '';+ return /\/mcp$/.test(trimmed) ? trimmed : `${trimmed}/mcp`;+}++function parseRpcResponse(text, contentType, id) {+ if ((contentType || '').includes('text/event-stream')) {+ const messages = text.split('\n')+ .filter(l => l.startsWith('data:'))+ .map(l => l.slice(5).trim())+ .filter(Boolean)+ .map(d => JSON.parse(d));+ return messages.find(m => m.id === id) || null;+ }+ return text ? JSON.parse(text) : null;+}++// WanGP phase names (shared/api.py `_normalize_phase`) → app progress status.+// Unknown phases fall back to 'working' so the UI shows WanGP's own status text.+const PHASE_STATUS = {+ loading_model: 'loading',+ encoding_text: 'encoding',+ inference: 'generating',+ inference_stage_1: 'generating',+ inference_stage_2: 'generating',+ inference_stage_3: 'generating',+ decoding: 'decoding',+ downloading_output: 'saving',+};++// Map a WanGP job progress event onto the app's progress shape.+function toProgressEvent(event) {+ const data = event?.data || {};+ if (event?.kind !== 'progress' || typeof data.progress !== 'number') return null;+ const status = PHASE_STATUS[String(data.phase || '')] || 'working';+ return {+ status,+ progress: Math.max(0, Math.min(1, data.progress / 100)),+ step: data.current_step ?? undefined,+ totalSteps: data.total_steps ?? undefined,+ message: data.status || undefined,+ };+}++function createMcpClient(url, { fetchImpl = fetch } = {}) {+ const endpoint = normalizeMcpUrl(url);+ let sessionId = null;+ let nextId = 1;+ let initialized = null;++ async function rpc(method, params, { notify = false, signal } = {}) {+ const body = notify ? { jsonrpc: '2.0', method, params } : { jsonrpc: '2.0', id: nextId++, method, params };+ const res = await fetchImpl(endpoint, {+ method: 'POST',+ headers: {+ 'Content-Type': 'application/json',+ Accept: 'application/json, text/event-stream',+ 'MCP-Protocol-Version': PROTOCOL_VERSION,+ ...(sessionId ? { 'Mcp-Session-Id': sessionId } : {}),+ },+ body: JSON.stringify(body),+ signal,+ });+ const sid = res.headers.get('mcp-session-id');+ if (sid) sessionId = sid;+ const text = await res.text();+ if (!res.ok) throw new Error(`Wan2GP MCP HTTP ${res.status}: ${text.slice(0, 200)}`);+ if (notify) return null;+ const msg = parseRpcResponse(text, res.headers.get('content-type'), body.id);+ if (!msg) throw new Error(`Wan2GP MCP returned no response to ${method}`);+ if (msg.error) throw new Error(`Wan2GP MCP error: ${msg.error.message || JSON.stringify(msg.error)}`);+ return msg.result;+ }++ function ensureInitialized() {+ if (!initialized) {+ initialized = (async () => {+ const result = await rpc('initialize', {+ protocolVersion: PROTOCOL_VERSION,+ capabilities: {},+ clientInfo: { name: 'open-generative-ai', version: '2' },+ });+ await rpc('notifications/initialized', {}, { notify: true });+ return result;+ })().catch((err) => { initialized = null; throw err; });+ }+ return initialized;+ }++ async function callTool(name, args, { signal } = {}) {+ await ensureInitialized();+ const result = await rpc('tools/call', { name, arguments: args }, { signal });+ const payload = result.structuredContent ?? (result.content || []).map(c => c.text).join('\n');+ if (result.isError) throw new Error(typeof payload === 'string' ? payload : JSON.stringify(payload));+ return payload;+ }++ async function probe() {+ const info = await ensureInitialized();+ const tools = await rpc('tools/list', {});+ const names = new Set((tools.tools || []).map(t => t.name));+ if (!names.has('wangp_generate') || !names.has('wangp_get_job')) {+ throw new Error('This MCP server does not expose WanGP generation tools (start it with --mcp-api-version 1).');+ }+ return { server: info.serverInfo?.name || 'WanGP', version: info.serverInfo?.version || 'unknown' };+ }++ // status: 'available' (weights on disk) | 'missing' (downloads on first use) | 'unknown-model'+ async function modelAvailability(modelType) {+ try {+ const r = await callTool('wangp_get_model_availability', { model_type: modelType });+ return r.status || (r.available ? 'available' : 'missing');+ } catch (err) {+ if (/Unknown model_type/i.test(err.message)) return 'unknown-model';+ throw err;+ }+ }++ function transferUrl(relative) {+ return new URL(relative, endpoint).toString();+ }++ async function uploadImage({ name, bytes, mime }) {+ const slot = await callTool('wangp_create_gallery_upload', { filename: name || 'start.png' });+ const res = await fetchImpl(transferUrl(slot.upload_url), {+ method: 'PUT',+ headers: { 'Content-Type': mime || 'application/octet-stream' },+ body: bytes,+ });+ const body = await res.json().catch(() => ({}));+ if (!res.ok || !body.media_id) throw new Error(`Wan2GP upload failed: HTTP ${res.status} ${body.detail || ''}`.trim());+ return body.media_id;+ }++ async function download(mediaId) {+ const slot = await callTool('wangp_create_gallery_download', { media_id: mediaId });+ const res = await fetchImpl(transferUrl(slot.download_url));+ if (!res.ok) throw new Error(`Wan2GP download failed: HTTP ${res.status}`);+ return { filename: slot.filename, bytes: Buffer.from(await res.arrayBuffer()) };+ }++ // Submit and poll until the job is done. Resolves the first generated+ // gallery item ({ media_id, media_type, filename }).+ async function generate(settings, { onProgress, onJobId, signal } = {}) {+ const job = await callTool('wangp_generate', { source: settings, wait: false, event_limit: 0 }, { signal });+ const jobId = job.job_id;+ if (!jobId) throw new Error('Wan2GP did not return a job id');+ onJobId?.(jobId);++ let snapshot = job;+ let lastKey = '';+ let maxProgress = 0;+ while (!snapshot.done) {+ if (signal?.aborted) throw new Error('Generation cancelled');+ await new Promise(r => setTimeout(r, POLL_INTERVAL_MS));+ snapshot = await callTool('wangp_get_job', { job_id: jobId, event_limit: 20 });+ const latest = (snapshot.events || []).map(toProgressEvent).filter(Boolean).pop();+ // Post-processing phases restart their own percentage; keep the bar monotonic.+ if (latest && latest.progress >= maxProgress) {+ maxProgress = latest.progress;+ const key = `${latest.status}:${latest.step}:${Math.round(latest.progress * 100)}`;+ if (key !== lastKey) { lastKey = key; onProgress?.(latest); }+ }+ }++ const result = snapshot.result || {};+ if (result.cancelled) throw new Error('Generation cancelled');+ if (!result.success) {+ const errors = (result.errors || []).map(e => e.message || JSON.stringify(e)).join('\n');+ throw new Error(`Wan2GP generation failed${errors ? `:\n${errors}` : ''}`);+ }+ const item = (result.gallery_items || [])[0];+ if (!item?.media_id) throw new Error('Wan2GP finished without a gallery item to download');+ return item;+ }++ async function cancel(jobId) {+ if (jobId) await callTool('wangp_cancel_job', { job_id: jobId });+ }++ return { endpoint, probe, modelAvailability, uploadImage, download, generate, cancel };+}++module.exports = { createMcpClient, normalizeMcpUrl, toProgressEvent };diff --git a/electron/lib/wan2gpModelAvailability.js b/electron/lib/wan2gpModelAvailability.jsindex 295cd9a7..0cd07034 100644--- a/electron/lib/wan2gpModelAvailability.js+++ b/electron/lib/wan2gpModelAvailability.js@@ -1,32 +1,29 @@-function withWan2gpAvailability(model, probeResult, cachedResolution) {- if (!probeResult?.ok) {- return {- ...model,- ready: false,- unavailableReason: probeResult?.error || 'Wan2GP probe failed',- };+// Availability of a Wan2GP catalog model for the configured engine.+//+// engine = { ok, error, mode: 'local' | 'remote', modelStatus: Map<modelType, status> }+// local: from <Wan2GP>/defaults/<modelType>.json + ckpts/ (see wan2gpLocal.localModelStatus)+// remote: from WanGP's MCP `wangp_get_model_availability`+// both: 'available' | 'missing' | 'installed'; anything else means the model is unknown+// Weights that are not on disk yet are still usable — Wan2GP downloads them on+// first use — so that case is ready with a note, not unavailable.+function withWan2gpAvailability(model, engine) {+ if (!engine?.ok) {+ return { ...model, ready: false, unavailableReason: engine?.error || 'Wan2GP is not configured' };}- const apiNames = Array.isArray(cachedResolution?.apiNames) ? cachedResolution.apiNames : [];- const realFn = cachedResolution?.resolved?.get?.(model.id) || null;-- if (realFn) {- return { ...model, ready: true, fn: realFn };+ const status = engine.modelStatus?.get(model.modelType);+ if (status === 'installed' || status === 'available') {+ return { ...model, ready: true };}-- if (apiNames.length === 0) {- return {- ...model,- ready: true,- fn: model.fn,- availabilityNote: 'Wan2GP did not expose endpoint metadata; using the default api_name.',- };+ if (status === 'missing') {+ return { ...model, ready: true, availabilityNote: 'Weights not downloaded yet — the first run downloads them (several GB).' };}+ const where = engine.mode === 'remote' ? 'The Wan2GP server' : 'This Wan2GP install';return {...model,ready: false,- unavailableReason: `Wan2GP server has no api_name matching "${model.fn}". Check Wan2GP version or load this model in its UI.`,+ unavailableReason: `${where} has no model "${model.modelType}" — update Wan2GP.`,};}diff --git a/electron/lib/wan2gpOutput.js b/electron/lib/wan2gpOutput.jsnew file mode 100644index 00000000..e220e9f5--- /dev/null+++ b/electron/lib/wan2gpOutput.js@@ -0,0 +1,122 @@+// Parsing of `wgp.py --process` console output: progress phases, the saved+// media path and failure detection. Pure module — unit-tested in tests/.++// Overall progress budget per phase (0–1).+const PHASES = {+ loading: [0.02, 0.1],+ encoding: [0.1, 0.12],+ generating: [0.12, 0.9],+ decoding: [0.9, 0.99],+};++function stripAnsi(text) {+ return String(text).replace(/\u001b\[[0-9;?]*[ -/]*[@-~]/g, '');+}++function classifyPhase(label) {+ if (/^Denoising/i.test(label)) return 'generating';+ if (/^VAE Decoding/i.test(label)) return 'decoding';+ if (/^Encoding/i.test(label)) return 'encoding';+ if (/^Loading/i.test(label)) return 'loading';+ return null;+}++function phaseProgress(status, step, total) {+ const [from, to] = PHASES[status];+ const ratio = total > 0 ? Math.min(1, step / total) : 0;+ return from + (to - from) * ratio;+}++// Stateful parser: feed it raw stdout/stderr chunks, get back progress events+// ({ status, progress, step, totalSteps }) only when something changed.+// Loading has sub-phases with their own counters (3/3 then 0/12), so overall+// progress is kept monotonic.+function createProgressParser() {+ let tail = '';+ let last = '';+ let maxProgress = 0;+ return function parse(chunk) {+ const text = tail + stripAnsi(chunk).replace(/\r/g, '\n');+ const lines = text.split('\n');+ tail = lines.pop();+ const events = [];+ for (const line of lines) {+ let event = null;+ const m = line.match(/\[(\d+)\/(\d+)\]\s+([A-Za-z][^|\[]*)/);+ const status = m ? classifyPhase(m[3].trim()) : null;+ if (status) {+ const step = Number(m[1]);+ const totalSteps = Number(m[2]);+ event = { status, step, totalSteps, progress: phaseProgress(status, step, totalSteps) };+ } else if (/^\s*Loading Model /.test(line)) {+ event = { status: 'loading', progress: PHASES.loading[0] };+ } else if (/Saving File/.test(line)) {+ event = { status: 'saving', progress: 0.99 };+ }+ if (!event) continue;+ const key = `${event.status}:${event.step ?? ''}/${event.totalSteps ?? ''}`;+ if (key === last) continue;+ last = key;+ maxProgress = Math.max(maxProgress, event.progress);+ events.push({ ...event, progress: maxProgress });+ }+ return events;+ };+}++function nonEmptyLines(output) {+ return stripAnsi(output).replace(/\r/g, '\n').split('\n').map(l => l.trimEnd()).filter(l => l.trim());+}++function tailLines(output, count = 15) {+ return nonEmptyLines(output).slice(-count).join('\n');+}++// The media path WanGP reports, e.g. "Video file saved to Path: /out/x.mp4".+// Post-processing (remux, upsampling) prints a later line; the last one wins.+function findSavedPath(output) {+ const matches = [...stripAnsi(output).matchAll(/saved to Path:\s*(.+?)\s*$/gm)];+ return matches.length ? matches[matches.length - 1][1] : null;+}++function describeFailure(output) {+ const text = stripAnsi(output);+ if (/out of memory|OutOfMemoryError/i.test(text)) {+ return 'GPU ran out of memory (VRAM). Try a lighter model, a lower resolution or fewer frames.';+ }+ const missing = text.match(/File not found for '([^']+)':\s*(.+)$/m);+ if (missing) return `Input file for "${missing[1]}" not found: ${missing[2].trim()}`;+ const errorLine = nonEmptyLines(text).reverse().find(l => /^\[ERROR\]/.test(l.trim()));+ if (errorLine) return errorLine.trim().replace(/^\[ERROR\]\s*/, '');+ if (/Traceback \(most recent call last\)/.test(text)) {+ const exceptionLine = nonEmptyLines(text).reverse().find(l => /^[A-Za-z_.]+(Error|Exception)\b/.test(l.trim()));+ if (exceptionLine) return exceptionLine.trim();+ }+ return null;+}++// Decide whether a finished `wgp.py --process` run produced media.+// Returns { ok: true, savedPath } or { ok: false, error } (error includes the log tail).+// savedPath can be null on success — the caller then scans the run's output dir.+function summarizeRun({ exitCode, signal, output }) {+ const summary = stripAnsi(output).match(/Queue completed:\s*(\d+)\/(\d+) tasks/);+ const completed = summary ? Number(summary[1]) : 0;+ const total = summary ? Number(summary[2]) : 0;++ if (!signal && exitCode === 0 && total > 0 && completed === total) {+ return { ok: true, savedPath: findSavedPath(output) };+ }++ const reason = signal+ ? `Wan2GP was stopped (${signal}).`+ : describeFailure(output)+ || (summary ? `Wan2GP finished ${completed}/${total} tasks.` : `Wan2GP exited with code ${exitCode}.`);+ return { ok: false, error: `${reason}\n\n${tailLines(output)}` };+}++module.exports = {+ createProgressParser,+ findSavedPath,+ summarizeRun,+ tailLines,+};diff --git a/electron/lib/wan2gpProvider.js b/electron/lib/wan2gpProvider.jsindex 68f6c83d..784e0ff0 100644--- a/electron/lib/wan2gpProvider.js+++ b/electron/lib/wan2gpProvider.js@@ -1,441 +1,230 @@-// Wan2GP HTTP provider — alternate local engine alongside sd.cpp.-// User runs Wan2GP themselves (https://github.com/deepbeepmeep/Wan2GP) and-// points this app at its Gradio server. We never bundle Python or weights.-// Useful when sd.cpp can't run a model (e.g. video) or the user has a-// dedicated CUDA box and only wants this Mac as the UI.--const { ipcMain, app, BrowserWindow } = require('electron');+// Wan2GP provider — video (and large image) models from https://github.com/deepbeepmeep/Wan2GP.+// We never bundle Python or weights; the user installs Wan2GP and picks one engine:+//+// local — the Wan2GP folder on this machine. Each generation runs+// `wgp.py --process settings.json --output-dir <run>` (WanGP's headless+// CLI mode, docs/CLI.md) and we parse its console for progress.+// remote — WanGP's MCP server on another machine (docs/API.md → "MCP Server"),+// `wgp.py --mcp --mcp-api-version 1 --mcp-transport streamable-http`.+//+// WanGP's Gradio UI is not an API: its ~700 endpoints are internal UI callbacks+// bound to browser state, so pointing the app at the Gradio port cannot work.++const { ipcMain, app, BrowserWindow, dialog } = require('electron');const path = require('path');const fs = require('fs');-const http = require('http');-const https = require('https');+const { pathToFileURL, fileURLToPath } = require('url');+const { resolveLocalAiPaths } = require('./localInferencePaths');const { withWan2gpAvailability } = require('./wan2gpModelAvailability');+const { WAN2GP_CATALOG, getModelById, buildWan2gpSettings } = require('./wan2gpCatalog');+const local = require('./wan2gpLocal');+const { createMcpClient, normalizeMcpUrl } = require('./wan2gpMcp');-const DATA_DIR = path.join(app.getPath('userData'), 'local-ai');-const CONFIG_FILE = path.join(DATA_DIR, 'wan2gp.json');-fs.mkdirSync(DATA_DIR, { recursive: true });--// ─── Catalog ──────────────────────────────────────────────────────────────────-// `fn` is the *preferred* Gradio api_name Wan2GP exposes via /gradio_api/call/<fn>.-// Wan2GP builds rename these between versions (and Pinokio packages drop them-// entirely on some endpoints), so at probe time we pull /info from the server-// and remap each catalog entry's `fn` to whatever is actually registered. The-// `fnAliases` list lets us match newer/older variants; `family` is the final-// fallback for fuzzy matching. See resolveFnNames() below.-const WAN2GP_CATALOG = [- {- id: 'wan2gp:flux-dev',- name: 'Flux.1 Dev (Wan2GP)',- description: 'Image — FLUX.1 dev served by Wan2GP. 1024px output.',- type: 'image',- family: 'flux',- provider: 'wan2gp',- fn: 'flux',- fnAliases: ['flux_dev', 'flux_1_dev', 'flux1_dev', 'flux_image'],- aspectRatios: ['1:1', '4:3', '3:4', '16:9', '9:16'],- defaultSteps: 28,- defaultGuidance: 3.5,- tags: ['image', 'flux', 'remote'],- },- {- id: 'wan2gp:qwen-image',- name: 'Qwen Image (Wan2GP)',- description: 'Image — Qwen-Image text-to-image served by Wan2GP.',- type: 'image',- family: 'qwen',- provider: 'wan2gp',- fn: 'qwen_image',- fnAliases: ['qwen', 'qwen_t2i', 'qwen_image_t2i'],- aspectRatios: ['1:1', '4:3', '3:4', '16:9', '9:16'],- defaultSteps: 30,- defaultGuidance: 4.0,- tags: ['image', 'qwen', 'remote'],- },- {- id: 'wan2gp:wan22-t2v',- name: 'Wan 2.2 (Text-to-Video)',- description: 'Video — Wan 2.2 text-to-video. Slow on consumer GPUs.',- type: 'video',- family: 'wan',- provider: 'wan2gp',- fn: 'wan22_t2v',- fnAliases: ['wan_2_2_t2v', 'wan22_text2video', 'wan_t2v', 'wan2_2_t2v', 't2v'],- aspectRatios: ['16:9', '1:1', '9:16'],- defaultSteps: 25,- defaultGuidance: 5.0,- tags: ['video', 'wan', 'text-to-video'],- },- {- id: 'wan2gp:wan22-i2v',- name: 'Wan 2.2 (Image-to-Video)',- description: 'Video — Wan 2.2 image-to-video. Provide a start frame.',- type: 'video',- family: 'wan',- provider: 'wan2gp',- fn: 'wan22_i2v',- fnAliases: ['wan_2_2_i2v', 'wan22_image2video', 'wan_i2v', 'wan2_2_i2v', 'i2v'],- needsImage: true,- aspectRatios: ['16:9', '1:1', '9:16'],- defaultSteps: 25,- defaultGuidance: 5.0,- tags: ['video', 'wan', 'image-to-video'],- },- {- id: 'wan2gp:hunyuan-video',- name: 'Hunyuan Video (Wan2GP)',- description: 'Video — Hunyuan text-to-video via Wan2GP.',- type: 'video',- family: 'hunyuan',- provider: 'wan2gp',- fn: 'hunyuan_video',- fnAliases: ['hunyuan', 'hunyuan_t2v', 'hyvideo', 'hy_video'],- aspectRatios: ['16:9', '1:1', '9:16'],- defaultSteps: 30,- defaultGuidance: 6.0,- tags: ['video', 'hunyuan'],- },- {- id: 'wan2gp:ltx-video',- name: 'LTX Video (Wan2GP)',- description: 'Video — LTX text-to-video. Fastest video option in Wan2GP.',- type: 'video',- family: 'ltx',- provider: 'wan2gp',- fn: 'ltx_video',- fnAliases: ['ltx', 'ltx_t2v', 'ltxv', 'ltx_v', 'ltx_2', 'ltx2'],- aspectRatios: ['16:9', '1:1', '9:16'],- defaultSteps: 20,- defaultGuidance: 3.0,- tags: ['video', 'ltx', 'fast'],- },-];+const CONFIG_DIR = path.join(app.getPath('userData'), 'local-ai');+const CONFIG_FILE = path.join(CONFIG_DIR, 'wan2gp.json');+const WORK_DIR = path.join(resolveLocalAiPaths({ userDataPath: app.getPath('userData') }).dataDir, 'wan2gp');+const INPUTS_DIR = path.join(WORK_DIR, 'inputs');+const RUNS_DIR = path.join(WORK_DIR, 'runs');+fs.mkdirSync(CONFIG_DIR, { recursive: true });-function getModelById(id) { return WAN2GP_CATALOG.find(m => m.id === id) || null; }+const VIDEO_EXTENSIONS = new Set(['.mp4', '.mkv', '.mov', '.webm']);+// Smallest model in the catalog — used for the settings "Check" dry run.+const CHECK_MODEL = getModelById('wan2gp:wan21-t2v-1.3b');// ─── Config ───────────────────────────────────────────────────────────────────+// { dir, python, url } — a Wan2GP folder takes precedence over a remote URL.function readConfig() {- if (!fs.existsSync(CONFIG_FILE)) return { url: '' };- try { return JSON.parse(fs.readFileSync(CONFIG_FILE, 'utf-8')); }- catch { return { url: '' }; }+ try { return { dir: '', python: '', url: '', ...JSON.parse(fs.readFileSync(CONFIG_FILE, 'utf-8')) }; }+ catch { return { dir: '', python: '', url: '' }; }}-function writeConfig(cfg) { fs.writeFileSync(CONFIG_FILE, JSON.stringify(cfg, null, 2)); }-function normalizeUrl(url) { return (url || '').trim().replace(/\/+$/, ''); }--// ─── State ────────────────────────────────────────────────────────────────────-let activeAbort = null;--// Map of uploaded source URL → { path, url, orig_name } so generate() can-// rehydrate the Gradio file descriptor when the renderer passes the URL back.-const uploadedFiles = new Map();--// Per-base cache of resolved api_names. Populated by probe(); consumed by-// listModels() and generate(). Without this we'd hit FnIndexInferError on-// every call because Wan2GP's api_name strings drift between releases.-// Shape: Map<baseUrl, { apiNames: string[], resolved: Map<modelId, string|null> }>-const fnResolutionCache = new Map();--// ─── HTTP helpers ─────────────────────────────────────────────────────────────-function httpJson(urlStr, { method = 'GET', body = null, timeoutMs = 5000 } = {}) {- return new Promise((resolve, reject) => {- const u = new URL(urlStr);- const mod = u.protocol === 'https:' ? https : http;- const headers = body ? { 'Content-Type': 'application/json' } : {};- const req = mod.request({- hostname: u.hostname, port: u.port, path: u.pathname + u.search,- method, headers,- }, (res) => {- let buf = '';- res.on('data', (d) => { buf += d; });- res.on('end', () => resolve({ status: res.statusCode, body: buf }));- res.on('error', reject);- });- req.on('error', reject);- req.setTimeout(timeoutMs, () => req.destroy(new Error('Request timed out')));- if (body) req.write(body);- req.end();- });+function writeConfig(cfg) {+ const next = {+ dir: (cfg.dir || '').trim(),+ python: (cfg.python || '').trim(),+ url: (cfg.url || '').trim().replace(/\/+$/, ''),+ };+ fs.writeFileSync(CONFIG_FILE, JSON.stringify(next, null, 2));+ return next;+}+function engineMode(cfg) {+ if (cfg.dir) return 'local';+ if (cfg.url) return 'remote';+ return null;}-// Pull the list of registered api_names from Gradio's /info endpoint.-// Gradio v4 exposes { named_endpoints: { "/api_name": {...}, ... } }; older-// builds use the legacy /api or a flat dependencies array. We try /info first,-// fall back to /api, and finally to /config['dependencies']. Returns an array-// of bare api_name strings (no leading slash). Empty array means we couldn't-// discover any — the server is reachable but doesn't expose a name index.-async function fetchApiNames(base) {- const candidates = [`${base}/info`, `${base}/api`, `${base}/gradio_api/info`];- for (const candidate of candidates) {- try {- const res = await httpJson(candidate, { timeoutMs: 8000 });- if (res.status !== 200) continue;- const parsed = JSON.parse(res.body);- const named = parsed.named_endpoints || parsed.unnamed_endpoints || parsed;- if (named && typeof named === 'object') {- const keys = Object.keys(named).filter(k => k.startsWith('/'));- if (keys.length) return keys.map(k => k.replace(/^\/+/, ''));- }- } catch { /* try next */ }- }- // Legacy fallback — /config exposes dependencies[] with api_name fields.- try {- const res = await httpJson(`${base}/config`, { timeoutMs: 5000 });- if (res.status === 200) {- const cfg = JSON.parse(res.body);- const deps = Array.isArray(cfg.dependencies) ? cfg.dependencies : [];- return deps.map(d => d.api_name).filter(n => typeof n === 'string' && n && n !== 'false');- }- } catch { /* ignore */ }- return [];+// ─── Remote (MCP) ─────────────────────────────────────────────────────────────+const mcpClients = new Map();+function mcpClientFor(url) {+ const endpoint = normalizeMcpUrl(url);+ if (!mcpClients.has(endpoint)) mcpClients.set(endpoint, createMcpClient(endpoint));+ return mcpClients.get(endpoint);}-// Map each catalog model to a real api_name on this server. Strategy:-// 1. exact match on `fn`-// 2. exact match on any `fnAliases` entry-// 3. fuzzy: api_name contains the model's `family` substring-// (e.g. catalog wants `ltx_video`; server registered `ltx_2_t2v` → match)-// Returns { resolved: Map<modelId, string|null>, apiNames: string[] }.-function resolveFnNames(apiNames) {- const set = new Set(apiNames);- const resolved = new Map();- for (const m of WAN2GP_CATALOG) {- let hit = null;- if (set.has(m.fn)) hit = m.fn;- if (!hit && Array.isArray(m.fnAliases)) {- for (const a of m.fnAliases) { if (set.has(a)) { hit = a; break; } }- }- if (!hit && m.family) {- // Prefer names that also match the type (image vs video keyword).- const typeHint = m.type === 'video' ? /(video|t2v|i2v|v2v)/i : /(image|t2i|txt2img)/i;- const fuzzy = apiNames.find(n => n.toLowerCase().includes(m.family) && typeHint.test(n))- || apiNames.find(n => n.toLowerCase().includes(m.family));- if (fuzzy) hit = fuzzy;+// Explain the most common misconfiguration: the URL of WanGP's Gradio UI.+async function explainRemoteFailure(url, err) {+ try {+ const res = await fetch(`${url.replace(/\/mcp$/, '')}/config`, { signal: AbortSignal.timeout(4000) });+ if (res.ok && (await res.json()).mode === 'blocks') {+ return 'This is the Wan2GP web UI (Gradio), which has no generation API. Start Wan2GP with '+ + '"python wgp.py --mcp --mcp-api-version 1 --mcp-transport streamable-http --mcp-host 0.0.0.0 --mcp-port 7866" '+ + 'and use http://<host>:7866/mcp — or set a local Wan2GP folder instead.';}- resolved.set(m.id, hit);- }- return { resolved, apiNames };+ } catch { /* not Gradio either */ }+ return `Wan2GP MCP server not reachable at ${normalizeMcpUrl(url)}: ${err.message}`;}-async function probe(url) {- const base = normalizeUrl(url);- if (!base) return { ok: false, error: 'URL is empty' };+async function probeRemote(url) {+ if (!url) return { ok: false, error: 'URL is empty' };try {- const res = await httpJson(`${base}/config`, { timeoutMs: 5000 });- if (res.status !== 200) return { ok: false, error: `HTTP ${res.status} from /config — is this a Gradio server?` };- const cfg = JSON.parse(res.body);- const apiNames = await fetchApiNames(base);- const { resolved } = resolveFnNames(apiNames);- fnResolutionCache.set(base, { apiNames, resolved });- const matched = [...resolved.values()].filter(Boolean).length;- return {- ok: true,- version: cfg.version || 'unknown',- apiNames,- matchedModels: matched,- totalModels: WAN2GP_CATALOG.length,- };- } catch (e) {- return { ok: false, error: e.message };+ const info = await mcpClientFor(url).probe();+ return { ok: true, mode: 'remote', message: `Connected to ${info.server} MCP ${info.version}` };+ } catch (err) {+ mcpClients.delete(normalizeMcpUrl(url));+ return { ok: false, mode: 'remote', error: await explainRemoteFailure(url, err) };}}-// ─── Upload (Gradio v4 /upload) ───────────────────────────────────────────────-// Renderer hands us { name, type, bytes:Uint8Array }. We POST as multipart to-// <base>/upload?upload_id=<id>; Gradio replies with an array of server paths.-// We expose those as a stable HTTP URL the renderer can preview AND stash the-// raw path for generate() to feed back into Gradio's file descriptor.-async function uploadFile({ name, type, bytes }) {- const { url } = readConfig();- if (!url) throw new Error('Wan2GP server URL not set. Open Settings → Local Models to configure.');- const base = normalizeUrl(url);-- if (!bytes || !bytes.length) throw new Error('Empty file payload');- const safeName = name || 'upload.bin';- const mime = type || 'application/octet-stream';-- const blob = new Blob([new Uint8Array(bytes)], { type: mime });- const form = new FormData();- form.append('files', blob, safeName);-- const uploadId = Math.random().toString(36).slice(2, 12);- const res = await fetch(`${base}/upload?upload_id=${uploadId}`, { method: 'POST', body: form });- if (!res.ok) throw new Error(`Wan2GP upload failed: HTTP ${res.status}`);-- const paths = await res.json();- const path = Array.isArray(paths) ? paths[0] : paths;- if (!path || typeof path !== 'string') throw new Error('Wan2GP upload returned no path');-- const fileUrl = `${base}/file=${path.replace(/^\/+/, '')}`;- uploadedFiles.set(fileUrl, { path, url: fileUrl, orig_name: safeName, mime_type: mime });- return { url: fileUrl, path };+// ─── Engine status ────────────────────────────────────────────────────────────+async function engineStatus(cfg = readConfig()) {+ const mode = engineMode(cfg);+ const modelTypes = WAN2GP_CATALOG.map(m => m.modelType);+ if (mode === 'local') {+ const install = local.resolveInstall(cfg);+ const check = local.checkInstall(install);+ if (!check.ok) return { ok: false, mode, error: check.error };+ return { ok: true, mode, modelStatus: local.localModelStatus(install, modelTypes) };+ }+ if (mode === 'remote') {+ const probe = await probeRemote(cfg.url);+ if (!probe.ok) return { ok: false, mode, error: probe.error };+ const client = mcpClientFor(cfg.url);+ const modelStatus = new Map();+ for (const modelType of modelTypes) {+ try { modelStatus.set(modelType, await client.modelAvailability(modelType)); }+ catch { /* leave unknown */ }+ }+ return { ok: true, mode, modelStatus };+ }+ return { ok: false, mode: null, error: 'Wan2GP is not configured — set its folder in Settings → Local Models.' };}async function listModels() {- const { url } = readConfig();- if (!url) return WAN2GP_CATALOG.map(m => ({ ...m, ready: false, unavailableReason: 'Wan2GP URL not set' }));- const base = normalizeUrl(url);- const probeRes = await probe(url); // populates fnResolutionCache- const cached = fnResolutionCache.get(base);- return WAN2GP_CATALOG.map(m => withWan2gpAvailability(m, probeRes, cached));+ const engine = await engineStatus();+ return WAN2GP_CATALOG.map(m => withWan2gpAvailability(m, engine));}-// ─── Generate ─────────────────────────────────────────────────────────────────-function arToDimensions(ar) {- const base = 1024;- const map = {- '1:1': [base, base],- '16:9': [Math.round(base * 16 / 9 / 64) * 64, base],- '9:16': [base, Math.round(base * 16 / 9 / 64) * 64],- '4:3': [Math.round(base * 4 / 3 / 64) * 64, base],- '3:4': [base, Math.round(base * 4 / 3 / 64) * 64],- };- return map[ar] || [base, base];+// Settings "Check": validates the folder + python with a `--dry-run`, or probes the MCP server.+// `quick` skips the dry run (it starts Python and imports torch — seconds, not milliseconds).+async function check(cfg) {+ const mode = engineMode(cfg);+ if (mode === 'remote') return probeRemote(cfg.url);+ if (mode !== 'local') return { ok: false, error: 'Set the Wan2GP folder or a server URL first.' };++ const install = local.resolveInstall(cfg);+ const installCheck = local.checkInstall(install);+ if (!installCheck.ok) return { ok: false, mode, error: installCheck.error };+ if (cfg.quick) return { ok: true, mode, message: `Wan2GP folder found · python ${install.python}` };+ const settings = buildWan2gpSettings(CHECK_MODEL, { prompt: 'dry run', aspect_ratio: '16:9', seed: 1 });+ const result = await local.dryRun({ install, settings, runDir: path.join(WORK_DIR, 'check') });+ return result.ok+ ? { ok: true, mode, message: `Wan2GP found · python ${install.python}` }+ : { ok: false, mode, error: result.message };}-// Gradio v4 protocol: POST /gradio_api/call/<fn> → { event_id }-// GET /gradio_api/call/<fn>/<event_id> → SSE stream-async function gradioCall(base, fn, payload, onProgress, signal) {- const post = await fetch(`${base}/gradio_api/call/${fn}`, {- method: 'POST',- headers: { 'Content-Type': 'application/json' },- body: JSON.stringify(payload),- signal,- });- if (!post.ok) throw new Error(`Wan2GP POST /call/${fn} → HTTP ${post.status}`);- const { event_id } = await post.json();- if (!event_id) throw new Error('Wan2GP did not return an event_id');-- const stream = await fetch(`${base}/gradio_api/call/${fn}/${event_id}`, { signal });- if (!stream.ok) throw new Error(`Wan2GP stream → HTTP ${stream.status}`);-- const reader = stream.body.getReader();- const dec = new TextDecoder();- let buf = '';-- while (true) {- const { value, done } = await reader.read();- if (done) break;- buf += dec.decode(value, { stream: true });-- const blocks = buf.split('\n\n');- buf = blocks.pop();- for (const block of blocks) {- const eMatch = block.match(/event:\s*(\S+)/);- const dMatch = block.match(/data:\s*(.*)$/s);- if (!eMatch) continue;- const evt = eMatch[1];- const dataStr = dMatch ? dMatch[1].trim() : '';+// ─── Inputs ───────────────────────────────────────────────────────────────────+// Start frames are stored locally; local runs pass the path to Wan2GP, remote+// runs upload the file to the MCP server at generation time.+function saveInput({ name, type, bytes }) {+ if (!bytes || !bytes.length) throw new Error('Empty file payload');+ fs.mkdirSync(INPUTS_DIR, { recursive: true });+ const ext = path.extname(name || '') || (type === 'image/jpeg' ? '.jpg' : '.png');+ const filePath = path.join(INPUTS_DIR, `${Date.now()}-${Math.random().toString(36).slice(2, 8)}${ext.toLowerCase()}`);+ fs.writeFileSync(filePath, Buffer.from(bytes));+ return { url: pathToFileURL(filePath).href, path: filePath };+}- if (evt === 'generating' || evt === 'process_generating') {- const m = dataStr.match(/"progress":\s*([\d.]+)/);- if (m && onProgress) onProgress(parseFloat(m[1]));- } else if (evt === 'complete' || evt === 'process_completes') {- try {- const parsed = JSON.parse(dataStr);- return Array.isArray(parsed) ? parsed : (parsed.data || parsed);- } catch {- throw new Error(`Wan2GP returned malformed completion: ${dataStr.slice(0, 200)}`);- }- } else if (evt === 'error' || evt === 'process_error') {- throw new Error(`Wan2GP error: ${dataStr.slice(0, 200)}`);- }- }+function inputPath(image) {+ if (!image) return null;+ const filePath = String(image).startsWith('file:') ? fileURLToPath(image) : String(image);+ if (!path.isAbsolute(filePath) || !fs.existsSync(filePath)) {+ throw new Error('Start-frame image is not available on this machine — upload it again.');}- throw new Error('Wan2GP stream ended without a completion event');+ return filePath;}-function resolveOutputUrl(base, output) {- const first = Array.isArray(output) ? output[0] : output;- if (!first) return null;- if (typeof first === 'string') {- return first.startsWith('http') ? first : `${base}/file=${first.replace(/^\/+/, '')}`;- }- if (first.url) return first.url.startsWith('http') ? first.url : `${base}${first.url}`;- if (first.path) return `${base}/file=${first.path.replace(/^\/+/, '')}`;- return null;+// ─── Generation ───────────────────────────────────────────────────────────────+let active = null; // { cancel: () => void, cancelled: boolean }++function mediaResult(filePath, seed) {+ const mediaType = VIDEO_EXTENSIONS.has(path.extname(filePath).toLowerCase()) ? 'video' : 'image';+ return { url: pathToFileURL(filePath).href, mediaType, seed, path: filePath };}async function generate(params, mainWindow) {- const { url } = readConfig();- if (!url) throw new Error('Wan2GP server URL not set. Open Settings → Local Models to configure.');- const base = normalizeUrl(url);-+ if (active) throw new Error('A Wan2GP generation is already running.');const model = getModelById(params.model);if (!model) throw new Error(`Unknown Wan2GP model: ${params.model}`);+ const cfg = readConfig();+ const mode = engineMode(cfg);+ if (!mode) throw new Error('Wan2GP is not configured — set its folder in Settings → Local Models.');+const send = (data) => mainWindow?.webContents.send('local-ai:progress', data);+ const imagePath = inputPath(params.image);+ const runDir = path.join(RUNS_DIR, `${Date.now()}`);+ fs.mkdirSync(runDir, { recursive: true });+ const run = { cancelled: false, cancel: () => {} };+ active = run;send({ status: 'starting', progress: 0 });- const [width, height] = arToDimensions(params.aspect_ratio || '1:1');- const seed = params.seed && params.seed !== -1 ? params.seed : Math.floor(Math.random() * 2147483647);- const steps = params.steps ?? model.defaultSteps;- const guidance = params.guidance_scale ?? model.defaultGuidance;-- // Image input → resolve to a Gradio file descriptor if we uploaded it.- let imageDescriptor = null;- if (params.image) {- const cached = uploadedFiles.get(params.image);- if (cached) {- imageDescriptor = { path: cached.path, url: cached.url, orig_name: cached.orig_name, mime_type: cached.mime_type, meta: { _type: 'gradio.FileData' } };- } else if (typeof params.image === 'string') {- imageDescriptor = params.image; // raw URL — Gradio fetches it- } else {- imageDescriptor = params.image;- }- }- if (model.needsImage && !imageDescriptor) {- throw new Error(`${model.name} requires a start-frame image — upload one first.`);- }-- // Generic positional input — adjust upstream `fn` if signature differs.- const payload = {- data: [- params.prompt || '',- params.negative_prompt || '',- width, height, steps, guidance, seed,- imageDescriptor,- ],- };-- // Resolve to whatever api_name the server actually exposes. Falls back to- // the catalog default so a user with a current Wan2GP build still works- // even if /info isn't reachable.- let cached = fnResolutionCache.get(base);- if (!cached) { await probe(url); cached = fnResolutionCache.get(base); }- const realFn = cached?.resolved.get(model.id) || model.fn;- if (cached && cached.apiNames.length && !cached.resolved.get(model.id)) {- const sample = cached.apiNames.slice(0, 8).join(', ');- throw new Error(- `${model.name}: Wan2GP server doesn't expose an api_name matching "${model.fn}". ` +- `Available endpoints: ${sample}${cached.apiNames.length > 8 ? '…' : ''}. ` +- `Make sure the model is loaded in Wan2GP, or update Wan2GP to a build that registers this api_name.`- );- }-- const ac = new AbortController();- activeAbort = ac;-try {- const result = await gradioCall(base, realFn, payload, (p) => {- send({ status: 'generating', progress: p });- }, ac.signal);- activeAbort = null;-- const mediaUrl = resolveOutputUrl(base, result);- if (!mediaUrl) throw new Error(`Wan2GP returned unrecognized output: ${JSON.stringify(result).slice(0, 200)}`);+ if (mode === 'local') {+ const install = local.resolveInstall(cfg);+ const installCheck = local.checkInstall(install);+ if (!installCheck.ok) throw new Error(installCheck.error);+ const settings = buildWan2gpSettings(model, params, { imageStart: imagePath });+ const { child, done } = local.runGeneration({ install, settings, runDir, onProgress: send });+ run.cancel = () => local.killTree(child);+ const mediaPath = await done.catch((err) => {+ throw run.cancelled ? new Error('Generation cancelled') : err;+ });+ send({ status: 'done', progress: 1 });+ return mediaResult(mediaPath, settings.seed);+ }+ const client = mcpClientFor(cfg.url);+ const abort = new AbortController();+ let jobId = null;+ run.cancel = () => {+ abort.abort();+ client.cancel(jobId).catch(() => {});+ };+ const imageStart = imagePath+ ? await client.uploadImage({ name: path.basename(imagePath), bytes: fs.readFileSync(imagePath) })+ : undefined;+ const settings = buildWan2gpSettings(model, params, { imageStart });+ const item = await client.generate(settings, {+ onProgress: send,+ onJobId: (id) => { jobId = id; },+ signal: abort.signal,+ });+ const { filename, bytes } = await client.download(item.media_id);+ const mediaPath = path.join(runDir, path.basename(filename || `${item.media_id.replace(/\W/g, '_')}.mp4`));+ fs.writeFileSync(mediaPath, bytes);send({ status: 'done', progress: 1 });- return { url: mediaUrl, mediaType: model.type, seed };- } catch (e) {- activeAbort = null;- if (e.name === 'AbortError') throw new Error('Generation cancelled');- throw e;+ return mediaResult(mediaPath, settings.seed);+ } catch (err) {+ if (run.cancelled) throw new Error('Generation cancelled');+ throw err;+ } finally {+ if (active === run) active = null;}}function cancelGeneration() {- if (activeAbort) {- activeAbort.abort();- activeAbort = null;+ if (active) {+ active.cancelled = true;+ active.cancel();}return { ok: true };}@@ -444,13 +233,32 @@ function cancelGeneration() {function getMainWindow() { return BrowserWindow.getAllWindows()[0] || null; }function register() {- ipcMain.handle('wan2gp:get-config', () => readConfig());- ipcMain.handle('wan2gp:set-url', (_, url) => { writeConfig({ url: normalizeUrl(url) }); return { ok: true }; });- ipcMain.handle('wan2gp:probe', (_, url) => probe(url));+ ipcMain.handle('wan2gp:get-config', () => {+ const cfg = readConfig();+ return { ...cfg, mode: engineMode(cfg), defaultPython: cfg.dir ? local.defaultPythonPath(cfg.dir) : '' };+ });+ ipcMain.handle('wan2gp:set-config', (_, cfg) => {+ mcpClients.clear();+ return { ok: true, config: writeConfig({ ...readConfig(), ...cfg }) };+ });+ ipcMain.handle('wan2gp:set-url', (_, url) => {+ mcpClients.clear();+ writeConfig({ ...readConfig(), url });+ return { ok: true };+ });+ ipcMain.handle('wan2gp:check', (_, cfg) => check({ ...readConfig(), ...cfg }));+ ipcMain.handle('wan2gp:probe', (_, url) => probeRemote(url));+ ipcMain.handle('wan2gp:pick-folder', async () => {+ const res = await dialog.showOpenDialog(getMainWindow(), { title: 'Select the Wan2GP folder', properties: ['openDirectory'] });+ return res.canceled ? null : res.filePaths[0];+ });ipcMain.handle('wan2gp:list-models', () => listModels());- ipcMain.handle('wan2gp:generate', (_, params) => generate(params, getMainWindow()));+ ipcMain.handle('wan2gp:generate', (_, params) => generate(params, getMainWindow()));ipcMain.handle('wan2gp:cancel-generation', () => cancelGeneration());- ipcMain.handle('wan2gp:upload-file', (_, payload) => uploadFile(payload));+ ipcMain.handle('wan2gp:upload-file', (_, payload) => saveInput(payload));++ // Never leave a detached wgp.py running after the app quits.+ app.on('before-quit', () => cancelGeneration());}module.exports = { register, WAN2GP_CATALOG };diff --git a/electron/preload.js b/electron/preload.jsindex d69d2477..6d3765f8 100644--- a/electron/preload.js+++ b/electron/preload.js@@ -16,10 +16,13 @@ contextBridge.exposeInMainWorld('localAI', {generate: (params) => ipcRenderer.invoke('local-ai:generate', params),cancelGeneration: () => ipcRenderer.invoke('local-ai:cancel-generation'),- // ── Wan2GP engine (remote Gradio server) ───────────────────────────────+ // ── Wan2GP engine (local folder or remote MCP server) ──────────────────wan2gp: {getConfig: () => ipcRenderer.invoke('wan2gp:get-config'),+ setConfig: (cfg) => ipcRenderer.invoke('wan2gp:set-config', cfg),setUrl: (url) => ipcRenderer.invoke('wan2gp:set-url', url),+ check: (cfg) => ipcRenderer.invoke('wan2gp:check', cfg),+ pickFolder: () => ipcRenderer.invoke('wan2gp:pick-folder'),probe: (url) => ipcRenderer.invoke('wan2gp:probe', url),listModels: () => ipcRenderer.invoke('wan2gp:list-models'),generate: (params) => ipcRenderer.invoke('wan2gp:generate', params),diff --git a/src/components/LocalModelManager.js b/src/components/LocalModelManager.jsindex 6962a311..3f3fc881 100644--- a/src/components/LocalModelManager.js+++ b/src/components/LocalModelManager.js@@ -7,6 +7,10 @@ const TrashIcon = `<svg width="14" height="14" viewBox="0 0 24 24" fill="none" sconst CheckIcon = `<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="3"><polyline points="20 6 9 17 4 12"/></svg>`;// ─── Helpers ─────────────────────────────────────────────────────────────────+function escapeHtml(s) {+ return String(s).replace(/[&<>"']/g, c => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c]));+}+function fmtGB(gb) {return gb >= 1 ? `${gb.toFixed(1)} GB` : `${(gb * 1024).toFixed(0)} MB`;}@@ -153,66 +157,95 @@ function AuxRow(label, auxKey, initStatus, onStateChange) {return row;}-// ─── Wan2GP Server Config ────────────────────────────────────────────────────+// ─── Wan2GP Config ───────────────────────────────────────────────────────────+// Two ways to reach Wan2GP: its folder on this machine (preferred — the app runs+// `wgp.py` itself) or a remote WanGP MCP server. A folder wins when both are set.function Wan2gpConfigBar(onChange) {+ const inputClass = 'flex-1 min-w-0 bg-white/5 border border-white/5 focus:border-primary/40 rounded-lg px-3 py-1.5 text-xs text-white placeholder-white/30 focus:outline-none';+ const secondaryBtn = 'px-3 py-1.5 rounded-lg text-xs font-bold bg-primary/20 text-primary border border-primary/30 hover:bg-primary/30 transition-all shrink-0';const wrap = document.createElement('div');wrap.className = 'flex flex-col gap-3 p-3 rounded-xl bg-white/3 border border-white/5';wrap.innerHTML = `<div class="flex flex-col gap-0.5">- <span class="text-xs font-bold text-white">Wan2GP server (optional)</span>- <span class="text-[11px] text-muted leading-relaxed">- Run <a href="https://github.com/deepbeepmeep/Wan2GP" target="_blank" class="text-primary hover:underline">Wan2GP</a>- on a CUDA box (<code class="text-primary/80">python wgp.py --listen --server-name 0.0.0.0</code>) to unlock video models from this UI.- </span>+ <span class="text-xs font-bold text-white">${t('localModels.wan2gpTitle')}</span>+ <span class="text-[11px] text-muted leading-relaxed">${t('localModels.wan2gpHelp')}</span>+ </div>+ <div class="flex flex-col gap-1.5">+ <span class="text-[10px] font-bold text-secondary uppercase tracking-wider">${t('localModels.wan2gpFolder')}</span>+ <div class="flex items-center gap-2">+ <input id="wan2gp-dir" type="text" placeholder="/home/you/Wan2GP" class="${inputClass}"/>+ <button id="wan2gp-browse" class="${secondaryBtn}">${t('localModels.browse')}</button>+ </div>+ <input id="wan2gp-python" type="text" class="${inputClass}"/></div>- <div class="flex items-center gap-2">- <input id="wan2gp-url" type="text" placeholder="http://127.0.0.1:7860"- class="flex-1 bg-white/5 border border-white/5 focus:border-primary/40 rounded-lg px-3 py-1.5 text-xs text-white placeholder-white/30 focus:outline-none"/>- <button id="wan2gp-test" class="px-3 py-1.5 rounded-lg text-xs font-bold bg-primary/20 text-primary border border-primary/30 hover:bg-primary/30 transition-all">Test</button>- <button id="wan2gp-save" class="px-3 py-1.5 rounded-lg text-xs font-bold bg-primary text-black hover:shadow-glow transition-all">Save</button>+ <div class="flex flex-col gap-1.5">+ <span class="text-[10px] font-bold text-secondary uppercase tracking-wider">${t('localModels.wan2gpRemote')}</span>+ <input id="wan2gp-url" type="text" placeholder="http://192.168.1.42:7866/mcp" class="${inputClass}"/>+ <span class="text-[10px] text-muted leading-relaxed">${t('localModels.wan2gpRemoteHelp')}</span></div>- <div id="wan2gp-status" class="text-[11px] text-muted">${t('localModels.notConfigured')}</div>+ <div class="flex items-center gap-2 justify-end">+ <button id="wan2gp-check" class="${secondaryBtn}">${t('localModels.check')}</button>+ <button id="wan2gp-save" class="px-3 py-1.5 rounded-lg text-xs font-bold bg-primary text-black hover:shadow-glow transition-all">${t('localModels.save')}</button>+ </div>+ <div id="wan2gp-status" class="text-[11px] text-muted whitespace-pre-wrap break-words">${t('localModels.notConfigured')}</div>`;- const input = wrap.querySelector('#wan2gp-url');- const testBtn = wrap.querySelector('#wan2gp-test');+ const dirInput = wrap.querySelector('#wan2gp-dir');+ const pythonInput = wrap.querySelector('#wan2gp-python');+ const urlInput = wrap.querySelector('#wan2gp-url');+ const checkBtn = wrap.querySelector('#wan2gp-check');const saveBtn = wrap.querySelector('#wan2gp-save');const statusEl = wrap.querySelector('#wan2gp-status');const setStatus = (text, kind = 'muted') => {const colorMap = { muted: 'text-muted', ok: 'text-green-400', warn: 'text-yellow-400', err: 'text-red-400' };- statusEl.className = `text-[11px] ${colorMap[kind] || colorMap.muted}`;+ statusEl.className = `text-[11px] whitespace-pre-wrap break-words ${colorMap[kind] || colorMap.muted}`;statusEl.textContent = text;};+ const updatePythonPlaceholder = () => {+ const dir = dirInput.value.trim().replace(/[\\/]+$/, '');+ pythonInput.placeholder = dir+ ? tf('localModels.pythonDefault', `${dir}/venv/bin/python`)+ : t('localModels.pythonPath');+ };+ const formValues = () => ({ dir: dirInput.value.trim(), python: pythonInput.value.trim(), url: urlInput.value.trim() });+ const runCheck = async (cfg) => {+ setStatus(t('localModels.probing'), 'muted');+ const r = await localAI.checkWan2gp(cfg);+ const modeLabel = r.mode === 'remote' ? t('localModels.modeRemote') : t('localModels.modeLocal');+ setStatus(r.ok ? `${modeLabel} · ${r.message}` : `${modeLabel} · ${r.error}`, r.ok ? 'ok' : 'err');+ return r;+ };+ dirInput.oninput = updatePythonPlaceholder;(async () => {const cfg = await localAI.getWan2gpConfig();- if (cfg.url) {- input.value = cfg.url;- const r = await localAI.probeWan2gp(cfg.url);- setStatus(r.ok ? `Connected · Gradio ${r.version}` : `Saved URL not reachable: ${r.error}`, r.ok ? 'ok' : 'warn');- } else {- setStatus(t('localModels.notConfiguredNote'), 'muted');- }+ dirInput.value = cfg.dir || '';+ pythonInput.value = cfg.python || '';+ urlInput.value = cfg.url || '';+ updatePythonPlaceholder();+ if (cfg.dir || cfg.url) await runCheck({ quick: true });+ else setStatus(t('localModels.notConfiguredNote'), 'muted');})();- testBtn.onclick = async () => {- const url = input.value.trim();- if (!url) { setStatus('Enter a URL first', 'warn'); return; }- setStatus(t('localModels.probing'), 'muted');- testBtn.disabled = true;- try {- const r = await localAI.probeWan2gp(url);- setStatus(r.ok ? `Reachable · Gradio ${r.version}` : `Unreachable: ${r.error}`, r.ok ? 'ok' : 'err');- } finally { testBtn.disabled = false; }+ wrap.querySelector('#wan2gp-browse').onclick = async () => {+ const dir = await localAI.pickWan2gpFolder();+ if (dir) { dirInput.value = dir; updatePythonPlaceholder(); }+ };++ checkBtn.onclick = async () => {+ const cfg = formValues();+ if (!cfg.dir && !cfg.url) { setStatus(t('localModels.wan2gpEnterFirst'), 'warn'); return; }+ checkBtn.disabled = true;+ try { await runCheck(cfg); } finally { checkBtn.disabled = false; }};saveBtn.onclick = async () => {- const url = input.value.trim();saveBtn.disabled = true;try {- await localAI.setWan2gpUrl(url);- const r = url ? await localAI.probeWan2gp(url) : { ok: false, error: 'cleared' };- setStatus(r.ok ? `Saved · Connected to Gradio ${r.version}` : (url ? `Saved, not reachable: ${r.error}` : 'Cleared'), r.ok ? 'ok' : 'warn');+ const cfg = formValues();+ await localAI.setWan2gpConfig(cfg);+ if (cfg.dir || cfg.url) await runCheck({ quick: true });+ else setStatus(t('localModels.notConfiguredNote'), 'muted');onChange?.();} finally { saveBtn.disabled = false; }};@@ -232,9 +265,11 @@ function Wan2gpModelCard(model) {${ready ? `<span class="text-green-400">${CheckIcon}</span>` : ''}</div><p class="text-[11px] text-muted leading-relaxed">${model.description}</p>+ ${!ready && model.unavailableReason ? `<p class="text-[10px] text-yellow-400/80 leading-relaxed">${escapeHtml(model.unavailableReason)}</p>` : ''}+ ${ready && model.availabilityNote ? `<p class="text-[10px] text-muted leading-relaxed">${escapeHtml(model.availabilityNote)}</p>` : ''}<div class="flex items-center gap-1.5 flex-wrap mt-1"><span class="px-1.5 py-0.5 rounded-md text-[10px] font-bold ${model.type === 'video' ? 'bg-purple-500/15 text-purple-300' : 'bg-primary/10 text-primary'}">${model.type.toUpperCase()}</span>- <span class="px-1.5 py-0.5 rounded-md text-[10px] font-bold bg-white/5 text-muted">via Wan2GP</span>+ <span class="px-1.5 py-0.5 rounded-md text-[10px] font-bold bg-white/5 text-muted">${model.modelType}</span>${(model.tags || []).filter(t => !['featured', 'remote'].includes(t)).map(t => `<span class="px-1.5 py-0.5 rounded-md text-[10px] font-bold bg-white/5 text-muted">${t}</span>`).join('')}</div></div>diff --git a/src/components/VideoStudio.js b/src/components/VideoStudio.jsindex aa2eba60..2853c2af 100644--- a/src/components/VideoStudio.js+++ b/src/components/VideoStudio.js@@ -1,7 +1,7 @@import { muapi } from '../lib/muapi.js';import { t2vModels, getAspectRatiosForVideoModel, getDurationsForModel, getResolutionsForVideoModel, i2vModels, getAspectRatiosForI2VModel, getDurationsForI2VModel, getResolutionsForI2VModel, v2vModels } from '../lib/models.js';import { AuthModal } from './AuthModal.js';-import { t } from '../lib/i18n.js';+import { t, tf } from '../lib/i18n.js';import { createUploadPicker } from './UploadPicker.js';import { savePendingJob, removePendingJob, getPendingJobs } from '../lib/pendingJobs.js';import { localAI, isLocalAIAvailable } from '../lib/localInferenceClient.js';@@ -12,6 +12,7 @@ import { isWan2gpModelId, getLocalModelById, localT2VModels, localI2VModels } frconst adaptLocalToVideoEntry = (m) => ({id: m.id,name: m.name,+ family: m.family,provider: 'wan2gp',inputs: {prompt: { type: 'string', name: 'prompt', title: 'Prompt' },@@ -19,18 +20,37 @@ const adaptLocalToVideoEntry = (m) => ({},});+const escapeHtml = (s) => String(s).replace(/[&<>"']/g, c => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c]));++const formatBytes = (bytes) => bytes >= 1024 ** 3 ? `${(bytes / 1024 ** 3).toFixed(1)} GB` : `${Math.round(bytes / 1024 ** 2)} MB`;++// Progress label for a Wan2GP event ({ status, step, totalSteps, downloadedBytes, message }).+const localProgressLabel = ({ status, step, totalSteps, downloadedBytes, message }) => {+ if (status === 'downloading') return tf('video.localDownloading', formatBytes(downloadedBytes || 0));+ if (status === 'loading') return t('video.localLoading');+ if (status === 'encoding') return t('video.localEncoding');+ if (status === 'generating' && totalSteps) return tf('video.localStep', step, totalSteps);+ if (status === 'decoding') return t('video.localDecoding');+ if (status === 'saving' || status === 'done') return t('video.localSaving');+ return message || t('video.localStarting');+};+export function VideoStudio() {const container = document.createElement('div');container.className = 'w-full h-full flex flex-col items-center justify-center bg-app-bg relative p-4 md:p-6 overflow-y-auto custom-scrollbar overflow-x-hidden';- // Merge Wan2GP video models in only when running inside Electron AND the- // user has a Wan2GP server configured. We can't probe synchronously, so- // we always include them when isLocalAIAvailable() — getCurrentModel()- // reads from these arrays, so they need to be present from init.+ // Cloud (Muapi) and local (Wan2GP) models live in separate lists; the+ // "⚡ Local" toggle — shown only in the desktop app — picks which one the+ // model dropdown and generation use.const localT2V = isLocalAIAvailable() ? localT2VModels.map(adaptLocalToVideoEntry) : [];const localI2V = isLocalAIAvailable() ? localI2VModels.map(adaptLocalToVideoEntry) : [];- const allT2V = [...t2vModels, ...localT2V];- const allI2V = [...i2vModels, ...localI2V];+ const allT2V = t2vModels;+ const allI2V = i2vModels;+ let useLocalModel = false;+ // id → { ready, unavailableReason, availabilityNote } from the Wan2GP engine+ let localAvailability = new Map();+ const t2vList = () => useLocalModel ? localT2V : allT2V;+ const i2vList = () => useLocalModel ? localI2V : allI2V;// --- State ---const defaultModel = allT2V[0];@@ -51,7 +71,7 @@ export function VideoStudio() {let v2vMode = false; // true = video-to-video tools modelet uploadedVideoUrl = null;- const getCurrentModels = () => v2vMode ? v2vModels : (imageMode ? allI2V : allT2V);+ const getCurrentModels = () => v2vMode ? v2vModels : (imageMode ? i2vList() : t2vList());// Local Wan2GP entries don't live in the Muapi-derived helpers, so we// resolve aspect ratios off the catalog when the selected id is local.const getCurrentAspectRatios = (id) => {@@ -140,11 +160,11 @@ export function VideoStudio() {}if (!imageMode) {imageMode = true;- const currentT2V = allT2V.find(m => m.id === selectedModel);+ const currentT2V = t2vList().find(m => m.id === selectedModel);const sibling = currentT2V?.family- ? allI2V.find(m => m.family === currentT2V.family)+ ? i2vList().find(m => m.family === currentT2V.family): null;- const target = sibling || allI2V[0];+ const target = sibling || i2vList()[0];selectedModel = target.id;selectedModelName = target.name;document.getElementById('v-model-btn-label').textContent = selectedModelName;@@ -161,17 +181,17 @@ export function VideoStudio() {// Clearing the start frame invalidates any selected end frame.uploadedEndImageUrl = null;endPicker?.reset();- selectedModel = allT2V[0].id;- selectedModelName = allT2V[0].name;+ selectedModel = t2vList()[0].id;+ selectedModelName = t2vList()[0].name;document.getElementById('v-model-btn-label').textContent = selectedModelName;updateControlsForModel(selectedModel);textarea.placeholder = t('video.placeholder');textarea.disabled = false;},- // Route the upload through the configured Wan2GP server when the active- // model is local; otherwise fall back to the Muapi-hosted upload.- uploadFn: (file) => isWan2gpModelId(selectedModel) ? localAI.uploadFileToWan2gp(file) : muapi.uploadFile(file),- requireApiKey: () => !isWan2gpModelId(selectedModel),+ // Local mode keeps the start frame on disk for Wan2GP; otherwise fall+ // back to the Muapi-hosted upload.+ uploadFn: (file) => useLocalModel ? localAI.uploadFileToWan2gp(file) : muapi.uploadFile(file),+ requireApiKey: () => !useLocalModel,});topRow.appendChild(picker.trigger);container.appendChild(picker.panel);@@ -184,8 +204,8 @@ export function VideoStudio() {anchorContainer: container,onSelect: ({ url }) => { uploadedEndImageUrl = url; },onClear: () => { uploadedEndImageUrl = null; },- uploadFn: (file) => isWan2gpModelId(selectedModel) ? localAI.uploadFileToWan2gp(file) : muapi.uploadFile(file),- requireApiKey: () => !isWan2gpModelId(selectedModel),+ uploadFn: (file) => useLocalModel ? localAI.uploadFileToWan2gp(file) : muapi.uploadFile(file),+ requireApiKey: () => !useLocalModel,});endPicker.trigger.title = 'End frame (optional)';// Visual marker: small "L" badge in the corner so users can tell the two@@ -277,8 +297,8 @@ export function VideoStudio() {return;}v2vMode = false;- selectedModel = allT2V[0].id;- selectedModelName = allT2V[0].name;+ selectedModel = t2vList()[0].id;+ selectedModelName = t2vList()[0].name;document.getElementById('v-model-btn-label').textContent = selectedModelName;updateControlsForModel(selectedModel);textarea.placeholder = 'Describe the video you want to create';@@ -413,6 +433,54 @@ export function VideoStudio() {<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" class="opacity-60 text-secondary"><path d="M12 2l2.4 7.4H22l-6.2 4.5 2.4 7.4L12 17l-6.2 4.3 2.4-7.4L2 9.4h7.6L12 2z"/></svg>`, 'Effect', 'v-effect-btn', 'Select effect type');+ // Local / API source toggle (only shown in Electron) — same pattern as Image Studio.+ const refreshLocalAvailability = async () => {+ try {+ const models = await localAI.listModels();+ localAvailability = new Map(models.filter(m => m.provider === 'wan2gp').map(m => [m.id, m]));+ } catch (err) {+ console.warn('[VideoStudio] Could not read Wan2GP status:', err);+ }+ };+ if (isLocalAIAvailable()) {+ const localToggleBtn = document.createElement('button');+ localToggleBtn.id = 'v-local-toggle-btn';+ const updateLocalToggleStyle = () => {+ localToggleBtn.className = useLocalModel+ ? 'flex items-center gap-1.5 px-3 py-2 rounded-xl transition-all border text-xs font-bold whitespace-nowrap bg-primary/20 border-primary/40 text-primary'+ : 'flex items-center gap-1.5 px-3 py-2 rounded-xl transition-all border text-xs font-bold whitespace-nowrap bg-white/5 border-white/5 text-white/60 hover:bg-white/10';+ localToggleBtn.textContent = useLocalModel ? t('image.local') : t('image.api');+ };+ updateLocalToggleStyle();+ localToggleBtn.onclick = async (e) => {+ e.stopPropagation();+ useLocalModel = !useLocalModel;+ updateLocalToggleStyle();+ // Uploads and video tools belong to one source — start clean after switching.+ if (v2vMode) {+ v2vMode = false;+ uploadedVideoUrl = null;+ showVideoIcon();+ textarea.disabled = false;+ }+ if (imageMode) {+ picker.reset();+ uploadedImageUrl = null;+ imageMode = false;+ }+ videoPickerBtn.classList.toggle('hidden', useLocalModel);+ if (useLocalModel) await refreshLocalAvailability();+ const list = getCurrentModels();+ const target = (useLocalModel && list.find(m => localAvailability.get(m.id)?.ready)) || list[0];+ selectedModel = target.id;+ selectedModelName = target.name;+ document.getElementById('v-model-btn-label').textContent = selectedModelName;+ updateControlsForModel(selectedModel);+ textarea.placeholder = t('video.placeholder');+ };+ controlsLeft.appendChild(localToggleBtn);+ }+controlsLeft.appendChild(modelBtn);controlsLeft.appendChild(arBtn);controlsLeft.appendChild(durationBtn);@@ -446,6 +514,61 @@ export function VideoStudio() {promptWrapper.appendChild(bar);container.appendChild(promptWrapper);+ // Local generation progress + error panel (hidden until a local run starts)+ const localPanel = document.createElement('div');+ localPanel.id = 'v-local-panel';+ localPanel.className = 'w-full max-w-4xl mt-4 hidden flex-col gap-2 relative z-30';+ localPanel.innerHTML = `+ <div class="v-local-progress flex flex-col gap-2">+ <div class="flex items-center justify-between gap-3">+ <span class="v-local-label text-xs font-bold text-white/60 truncate">${t('video.localStarting')}</span>+ <span class="v-local-pct text-xs font-bold text-primary shrink-0">0%</span>+ </div>+ <div class="h-1.5 rounded-full bg-white/10 overflow-hidden">+ <div class="v-local-fill h-full bg-primary transition-all duration-300" style="width:0%"></div>+ </div>+ <div class="flex justify-end">+ <button class="v-local-cancel text-xs text-red-400 hover:text-red-300 transition-colors">${t('common.cancel')}</button>+ </div>+ </div>+ <div class="v-local-error hidden flex-col gap-2 p-3 rounded-xl bg-red-500/10 border border-red-500/20">+ <div class="flex items-center justify-between gap-3">+ <span class="v-local-error-title text-xs font-bold text-red-300"></span>+ <button class="v-local-error-close text-xs text-white/50 hover:text-white">✕</button>+ </div>+ <pre class="v-local-error-log text-[10px] leading-relaxed text-white/60 whitespace-pre-wrap break-words max-h-48 overflow-y-auto custom-scrollbar"></pre>+ </div>+ `;+ container.appendChild(localPanel);+ const localProgressEl = localPanel.querySelector('.v-local-progress');+ const localErrorEl = localPanel.querySelector('.v-local-error');+ const showLocalPanel = (part) => {+ localPanel.classList.remove('hidden');+ localPanel.classList.add('flex');+ localProgressEl.classList.toggle('hidden', part !== 'progress');+ localErrorEl.classList.toggle('hidden', part !== 'error');+ localErrorEl.classList.toggle('flex', part === 'error');+ };+ const hideLocalPanel = () => {+ localPanel.classList.add('hidden');+ localPanel.classList.remove('flex');+ };+ const setLocalProgress = (event) => {+ const pct = Math.round((event.progress ?? 0) * 100);+ localPanel.querySelector('.v-local-label').textContent = localProgressLabel(event);+ localPanel.querySelector('.v-local-pct').textContent = `${pct}%`;+ localPanel.querySelector('.v-local-fill').style.width = `${pct}%`;+ };+ // First line is the reason; the rest (if any) is the Wan2GP log tail.+ const showLocalError = (message) => {+ const [title, ...rest] = String(message).replace(/^Error invoking remote method '[^']+': (Error: )?/, '').split('\n');+ localPanel.querySelector('.v-local-error-title').textContent = title;+ localPanel.querySelector('.v-local-error-log').textContent = rest.join('\n').trim();+ showLocalPanel('error');+ };+ localPanel.querySelector('.v-local-cancel').onclick = () => localAI.cancelGeneration();+ localPanel.querySelector('.v-local-error-close').onclick = hideLocalPanel;+// ==========================================// 3. DROPDOWNS// ==========================================@@ -560,7 +683,7 @@ export function VideoStudio() {<input type="text" id="v-model-search" placeholder="${t('common.searchModels')}" class="bg-transparent border-none text-xs text-white focus:ring-0 w-full p-0"></div></div>- <div class="text-[10px] font-bold text-secondary uppercase tracking-widest px-3 py-2 shrink-0">Video models</div>+ <div class="text-[10px] font-bold text-secondary uppercase tracking-widest px-3 py-2 shrink-0">${useLocalModel ? t('video.localModels') : 'Video models'}</div><div id="v-model-list-container" class="flex flex-col gap-1.5 overflow-y-auto custom-scrollbar pr-1 pb-2"></div></div>`;@@ -568,20 +691,28 @@ export function VideoStudio() {const makeModelItem = (m, isV2V = false) => {const item = document.createElement('div');- item.className = `flex items-center justify-between p-3.5 hover:bg-white/5 rounded-2xl cursor-pointer transition-all border border-transparent hover:border-white/5 ${selectedModel === m.id ? 'bg-white/5 border-white/5' : ''}`;+ // Local Wan2GP models carry the engine's verdict; unavailable ones stay visible with the reason.+ const local = m.provider === 'wan2gp' ? localAvailability.get(m.id) : null;+ const unavailable = local && !local.ready;+ item.className = `flex items-center justify-between p-3.5 rounded-2xl transition-all border border-transparent ${unavailable ? 'opacity-50 cursor-not-allowed' : 'hover:bg-white/5 cursor-pointer hover:border-white/5'} ${selectedModel === m.id ? 'bg-white/5 border-white/5' : ''}`;const iconColor = isV2V ? 'bg-orange-500/10 text-orange-400' : m.id.includes('kling') ? 'bg-blue-500/10 text-blue-400' : m.id.includes('veo') ? 'bg-purple-500/10 text-purple-400' : m.id.includes('sora') ? 'bg-rose-500/10 text-rose-400' : 'bg-primary/10 text-primary';+ const localNote = unavailable+ ? `<span class="v-local-reason text-[9px] text-yellow-400/80 leading-snug">${escapeHtml(local.unavailableReason)}</span>`+ : local?.availabilityNote ? `<span class="text-[9px] text-muted leading-snug">${escapeHtml(local.availabilityNote)}</span>` : '';item.innerHTML = `- <div class="flex items-center gap-3.5">- <div class="w-10 h-10 ${iconColor} border border-white/5 rounded-xl flex items-center justify-center font-black text-sm shadow-inner uppercase">${m.name.charAt(0)}</div>- <div class="flex flex-col gap-0.5">+ <div class="flex items-center gap-3.5 min-w-0">+ <div class="w-10 h-10 shrink-0 ${iconColor} border border-white/5 rounded-xl flex items-center justify-center font-black text-sm shadow-inner uppercase">${m.provider === 'wan2gp' ? '⚡' : m.name.charAt(0)}</div>+ <div class="flex flex-col gap-0.5 min-w-0"><span class="text-xs font-bold text-white tracking-tight">${m.name}</span>${isV2V ? `<span class="text-[9px] text-orange-400/70">${m.imageField ? 'Upload a video and image' : 'Upload a video to use'}</span>` : ''}+ ${localNote}</div></div>${selectedModel === m.id ? '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="#22d3ee" stroke-width="4"><polyline points="20 6 9 17 4 12"/></svg>' : ''}`;item.onclick = (e) => {e.stopPropagation();+ if (unavailable) return;if (isV2V) {// Switch to v2v modev2vMode = true;@@ -629,13 +760,13 @@ export function VideoStudio() {const lf = filter.toLowerCase();// Regular generation models (always t2v or i2v, never v2v)- const generationModels = imageMode ? allI2V : allT2V;+ const generationModels = imageMode ? i2vList() : t2vList();const filteredMain = generationModels.filter(m => m.name.toLowerCase().includes(lf) || m.id.toLowerCase().includes(lf));filteredMain.forEach(m => list.appendChild(makeModelItem(m, false)));- // Video Tools section- const filteredV2V = v2vModels.filter(m => m.name.toLowerCase().includes(lf) || m.id.toLowerCase().includes(lf));+ // Video Tools section (cloud-only)+ const filteredV2V = useLocalModel ? [] : v2vModels.filter(m => m.name.toLowerCase().includes(lf) || m.id.toLowerCase().includes(lf));if (filteredV2V.length > 0) {const sectionLabel = document.createElement('div');sectionLabel.className = 'text-[10px] font-bold text-orange-400/70 uppercase tracking-widest px-3 py-2 mt-1 border-t border-white/5';@@ -649,6 +780,10 @@ export function VideoStudio() {const searchInput = dropdown.querySelector('#v-model-search');searchInput.onclick = (e) => e.stopPropagation();searchInput.oninput = (e) => renderModels(e.target.value);+ // Settings may have changed since the toggle — re-read Wan2GP availability.+ if (useLocalModel) {+ refreshLocalAvailability().then(() => { if (dropdownOpen === 'model') renderModels(searchInput.value); });+ }} else if (type === 'ar') {dropdown.classList.add('max-w-[240px]');@@ -1053,8 +1188,9 @@ export function VideoStudio() {uploadedVideoUrl = null;v2vMode = false;showVideoIcon();- selectedModel = allT2V[0].id;- selectedModelName = allT2V[0].name;+ hideLocalPanel();+ selectedModel = t2vList()[0].id;+ selectedModelName = t2vList()[0].name;document.getElementById('v-model-btn-label').textContent = selectedModelName;updateControlsForModel(selectedModel);textarea.placeholder = 'Describe the video you want to create';@@ -1115,10 +1251,18 @@ export function VideoStudio() {}}- const isLocal = isWan2gpModelId(selectedModel);+ const isLocal = useLocalModel && isWan2gpModelId(selectedModel);- // Local Wan2GP generations don't go through Muapi — skip the auth gate.- if (!isLocal) {+ // Local Wan2GP generations don't go through Muapi — skip the auth gate,+ // but refuse models the engine reported as unavailable.+ if (isLocal) {+ if (!localAvailability.size) await refreshLocalAvailability();+ const status = localAvailability.get(selectedModel);+ if (status && !status.ready) {+ showLocalError(status.unavailableReason);+ return;+ }+ } else {const apiKey = localStorage.getItem('muapi_key');if (!apiKey) {AuthModal(() => generateBtn.click());@@ -1130,13 +1274,14 @@ export function VideoStudio() {generateBtn.disabled = true;generateBtn.innerHTML = `<span class="animate-spin inline-block mr-2 text-black">◌</span> ${t('common.generating')}`;- // For local generations, surface step progress in the button label.+ // Local generations stream phase/step progress into the panel and the button.let unsubscribeProgress = null;if (isLocal) {- unsubscribeProgress = localAI.onProgress(({ status, progress, message }) => {- const pct = typeof progress === 'number' ? Math.round(progress * 100) : null;- const label = message || `${status || t('common.generating')}${pct != null ? ` ${pct}%` : '...'}`;- generateBtn.innerHTML = `<span class="animate-spin inline-block mr-2 text-black">◌</span> ${label}`;+ setLocalProgress({ status: 'starting', progress: 0 });+ showLocalPanel('progress');+ unsubscribeProgress = localAI.onProgress((event) => {+ setLocalProgress(event);+ generateBtn.innerHTML = `<span class="animate-spin inline-block mr-2 text-black">◌</span> ${localProgressLabel(event)}`;});}@@ -1151,8 +1296,8 @@ export function VideoStudio() {try {// ─── Local Wan2GP path ───────────────────────────────────────────- // Uploaded image URLs were minted by uploadFileToWan2gp(), so- // wan2gpProvider can rehydrate the Gradio file descriptor.+ // Start frames were saved on disk by uploadFileToWan2gp(); the+ // provider passes that path to Wan2GP (or uploads it to a remote one).if (isLocal) {const localParams = {model: selectedModel,@@ -1162,11 +1307,12 @@ export function VideoStudio() {if (imageMode && uploadedImageUrl) localParams.image = uploadedImageUrl;const res = await localAI.generate(localParams);console.log('[VideoStudio] Local response:', res);+ hideLocalPanel();if (res && res.url) {const genId = Date.now().toString();lastGenerationId = null;lastGenerationModel = null;- addToHistory({ id: genId, url: res.url, prompt, model: selectedModel, aspect_ratio: selectedAr, timestamp: new Date().toISOString() });+ addToHistory({ id: genId, url: res.url, prompt, model: selectedModel, aspect_ratio: selectedAr, seed: res.seed, timestamp: new Date().toISOString() });showVideoInCanvas(res.url, selectedModel);} else {throw new Error('No video URL returned by Wan2GP');@@ -1295,7 +1441,15 @@ export function VideoStudio() {console.error(e);// Restore hero so the page doesn't look broken after a failed generationhero.classList.remove('opacity-0', 'scale-95', '-translate-y-10', 'pointer-events-none');- generateBtn.innerHTML = `Error: ${e.message.slice(0, 60)}`;+ const cancelled = isLocal && /Generation cancelled/.test(e.message);+ if (isLocal) {+ // Local errors carry the Wan2GP log tail — show all of it, not a 60-char button label.+ if (cancelled) hideLocalPanel();+ else showLocalError(e.message);+ }+ generateBtn.innerHTML = cancelled ? t('video.localCancelled')+ : isLocal ? t('video.localFailed')+ : `Error: ${e.message.slice(0, 60)}`;setTimeout(() => {generateBtn.innerHTML = t('common.generate');}, 4000);diff --git a/src/lib/i18n.js b/src/lib/i18n.jsindex 5c47197b..20ee7b7f 100644--- a/src/lib/i18n.js+++ b/src/lib/i18n.js@@ -157,6 +157,16 @@ const translations = {'video.extend': '↗ Extend','video.new': '+ New','video.videoTools': 'Video Tools',+ 'video.localModels': 'Local models (Wan2GP)',+ 'video.localStarting': 'Starting Wan2GP...',+ 'video.localDownloading': (size) => `Downloading model weights... ${size}`,+ 'video.localLoading': 'Loading model...',+ 'video.localEncoding': 'Encoding prompt...',+ 'video.localStep': (step, total) => `Denoising step ${step}/${total}`,+ 'video.localDecoding': 'Decoding video...',+ 'video.localSaving': 'Saving video...',+ 'video.localCancelled': 'Cancelled',+ 'video.localFailed': 'Failed — see details below',// Lip Sync Studio'lipsync.title': 'Lip Sync',@@ -235,6 +245,19 @@ const translations = {'localModels.get': 'Get','localModels.notConfigured': 'Not configured','localModels.notConfiguredNote': 'Not configured (Wan2GP models will appear offline)',+ 'localModels.wan2gpTitle': 'Wan2GP — local video models (optional)',+ 'localModels.wan2gpHelp': 'Install <a href="https://github.com/deepbeepmeep/Wan2GP" target="_blank" class="text-primary hover:underline">Wan2GP</a> on a machine with an NVIDIA/AMD GPU. With its folder set, the app runs Wan2GP itself — weights download on first use of each model.',+ 'localModels.wan2gpFolder': 'Wan2GP folder on this computer',+ 'localModels.browse': 'Browse',+ 'localModels.pythonPath': 'Python (default: <folder>/venv/bin/python)',+ 'localModels.pythonDefault': (path) => `Python (default: ${path})`,+ 'localModels.wan2gpRemote': 'Or: remote Wan2GP MCP server',+ 'localModels.wan2gpRemoteHelp': 'On the GPU machine: python wgp.py --mcp --mcp-api-version 1 --mcp-transport streamable-http --mcp-host 0.0.0.0 --mcp-port 7866. The web UI port (7860) is not an API. A folder takes precedence over a URL.',+ 'localModels.check': 'Check',+ 'localModels.save': 'Save',+ 'localModels.modeLocal': 'Local folder',+ 'localModels.modeRemote': 'Remote server',+ 'localModels.wan2gpEnterFirst': 'Set the Wan2GP folder or a server URL first','localModels.probing': 'Probing...','localModels.errorLoading': 'Error loading models: ','localModels.deleteConfirm': (name) => `Delete "${name}"? You'll need to re-download it to use it again.`,@@ -358,6 +381,16 @@ const translations = {'video.extend': '↗ 延伸','video.new': '+ 新建','video.videoTools': '视频工具',+ 'video.localModels': '本地模型 (Wan2GP)',+ 'video.localStarting': '正在启动 Wan2GP...',+ 'video.localDownloading': (size) => `正在下载模型权重... ${size}`,+ 'video.localLoading': '正在加载模型...',+ 'video.localEncoding': '正在编码提示词...',+ 'video.localStep': (step, total) => `去噪步骤 ${step}/${total}`,+ 'video.localDecoding': '正在解码视频...',+ 'video.localSaving': '正在保存视频...',+ 'video.localCancelled': '已取消',+ 'video.localFailed': '失败 — 详见下方',// Lip Sync Studio'lipsync.title': '唇语同步',@@ -436,6 +469,19 @@ const translations = {'localModels.get': '获取','localModels.notConfigured': '未配置','localModels.notConfiguredNote': '未配置(Wan2GP 模型将显示为离线)',+ 'localModels.wan2gpTitle': 'Wan2GP — 本地视频模型(可选)',+ 'localModels.wan2gpHelp': '在配备 NVIDIA/AMD GPU 的机器上安装 <a href="https://github.com/deepbeepmeep/Wan2GP" target="_blank" class="text-primary hover:underline">Wan2GP</a>。设置其文件夹后,应用会自行运行 Wan2GP——每个模型首次使用时下载权重。',+ 'localModels.wan2gpFolder': '本机上的 Wan2GP 文件夹',+ 'localModels.browse': '浏览',+ 'localModels.pythonPath': 'Python(默认:<文件夹>/venv/bin/python)',+ 'localModels.pythonDefault': (path) => `Python(默认:${path})`,+ 'localModels.wan2gpRemote': '或:远程 Wan2GP MCP 服务器',+ 'localModels.wan2gpRemoteHelp': '在 GPU 机器上运行:python wgp.py --mcp --mcp-api-version 1 --mcp-transport streamable-http --mcp-host 0.0.0.0 --mcp-port 7866。Web 界面端口 (7860) 不是 API。文件夹优先于 URL。',+ 'localModels.check': '检查',+ 'localModels.save': '保存',+ 'localModels.modeLocal': '本地文件夹',+ 'localModels.modeRemote': '远程服务器',+ 'localModels.wan2gpEnterFirst': '请先设置 Wan2GP 文件夹或服务器 URL','localModels.probing': '探测中...','localModels.errorLoading': '加载模型时出错:','localModels.deleteConfirm': (name) => `删除"${name}"?您需要重新下载才能再次使用。`,diff --git a/src/lib/localInferenceClient.js b/src/lib/localInferenceClient.jsindex 27dc07f1..3430f232 100644--- a/src/lib/localInferenceClient.js+++ b/src/lib/localInferenceClient.js@@ -1,7 +1,7 @@// Frontend client for local inference — wraps window.localAI (Electron IPC).// Two providers live behind the same surface:// - sd.cpp: bundled engine, downloads weights to disk, runs locally-// - wan2gp: user-run Gradio server, generation is remote HTTP+// - wan2gp: user-installed Wan2GP — a local folder (wgp.py CLI) or a remote MCP server// Provider is read off the model entry's `provider` field.import { getLocalModelById } from './localModels.js';@@ -32,22 +32,34 @@ class LocalInferenceClient {}// ── Wan2GP APIs ───────────────────────────────────────────────────────+ // Config: { dir, python, url, mode, defaultPython } — a folder wins over a URL.async getWan2gpConfig() {- if (!isLocalAIAvailable()) return { url: '' };+ if (!isLocalAIAvailable()) return { dir: '', python: '', url: '' };return window.localAI.wan2gp.getConfig();}+ async setWan2gpConfig(cfg) {+ if (!isLocalAIAvailable()) throw new Error('Local AI only available in the desktop app.');+ return window.localAI.wan2gp.setConfig(cfg);+ }async setWan2gpUrl(url) {if (!isLocalAIAvailable()) throw new Error('Local AI only available in the desktop app.');return window.localAI.wan2gp.setUrl(url);}+ // Local folder: `wgp.py --dry-run` on a test task. Remote URL: MCP probe.+ async checkWan2gp(cfg) {+ if (!isLocalAIAvailable()) return { ok: false, error: 'Not in desktop app' };+ return window.localAI.wan2gp.check(cfg);+ }+ async pickWan2gpFolder() {+ if (!isLocalAIAvailable()) return null;+ return window.localAI.wan2gp.pickFolder();+ }async probeWan2gp(url) {if (!isLocalAIAvailable()) return { ok: false, error: 'Not in desktop app' };return window.localAI.wan2gp.probe(url);}- // Pushes a File/Blob to the configured Wan2GP server's /upload endpoint- // and returns { url, path }. URL is a previewable HTTP link; the provider- // also remembers the path so a subsequent generate(params.image=url) call- // can rehydrate it as a Gradio file descriptor.+ // Saves a start frame on disk and returns { url: file://…, path }. Local+ // runs pass the path to Wan2GP; remote runs upload it to the MCP server.async uploadFileToWan2gp(file) {if (!isLocalAIAvailable()) throw new Error('Local AI only available in the desktop app.');const buf = await file.arrayBuffer();@@ -91,7 +103,8 @@ class LocalInferenceClient {/*** Subscribe to generation progress events.* sd.cpp emits { step, totalSteps, progress, status }.- * Wan2GP emits { progress, status }.+ * Wan2GP emits { progress, status, step?, totalSteps?, downloadedBytes? } with status+ * starting | downloading | loading | encoding | generating | decoding | saving | done.*/onProgress(callback) {if (!isLocalAIAvailable()) return () => {};diff --git a/src/lib/localModels.js b/src/lib/localModels.jsindex 6661125d..2ac065c3 100644--- a/src/lib/localModels.js+++ b/src/lib/localModels.js@@ -1,8 +1,8 @@// Frontend-side local model catalog.// Two providers:// - sdcpp: bundled engine, weights live on disk-// - wan2gp: user-run remote Gradio server-// Mirrors electron/lib/modelCatalog.js (sd.cpp) and electron/lib/wan2gpProvider.js (wan2gp).+// - wan2gp: user-installed Wan2GP (local folder or remote MCP server)+// Mirrors electron/lib/modelCatalog.js (sd.cpp) and electron/lib/wan2gpCatalog.js (wan2gp).export const LOCAL_MODEL_CATALOG = [// ── sd.cpp: Z-Image (Tongyi-MAI) ────────────────────────────────────────{@@ -89,78 +89,84 @@ export const LOCAL_MODEL_CATALOG = [},// ── Wan2GP: image models ────────────────────────────────────────────────+ // modelType = WanGP model_type (<Wan2GP>/defaults/<modelType>.json){id: 'wan2gp:flux-dev',name: 'Flux.1 Dev (Wan2GP)',- description: 'Image — FLUX.1 dev served by Wan2GP. Requires running Wan2GP server.',+ description: 'Image — FLUX.1 dev (12B) served by Wan2GP.',type: 'image',family: 'flux',provider: 'wan2gp',+ modelType: 'flux',aspectRatios: ['1:1', '4:3', '3:4', '16:9', '9:16'],- defaultSteps: 28,- defaultGuidance: 3.5,- tags: ['image', 'flux', 'remote'],+ tags: ['image', 'flux'],},{id: 'wan2gp:qwen-image',name: 'Qwen Image (Wan2GP)',- description: 'Image — Qwen-Image text-to-image served by Wan2GP.',+ description: 'Image — Qwen-Image 20B text-to-image served by Wan2GP.',type: 'image',family: 'qwen',provider: 'wan2gp',+ modelType: 'qwen_image_20B',aspectRatios: ['1:1', '4:3', '3:4', '16:9', '9:16'],- defaultSteps: 30,- defaultGuidance: 4.0,- tags: ['image', 'qwen', 'remote'],+ tags: ['image', 'qwen'],},// ── Wan2GP: video models ────────────────────────────────────────────────+ {+ id: 'wan2gp:wan21-t2v-1.3b',+ name: 'Wan 2.1 1.3B (Text-to-Video)',+ description: 'Video — lightest Wan model, 480p. Runs on ~8 GB VRAM; a 5 s clip takes a few minutes.',+ type: 'video',+ family: 'wan',+ provider: 'wan2gp',+ modelType: 't2v_1.3B',+ aspectRatios: ['16:9', '9:16', '1:1'],+ tags: ['video', 'wan', 'text-to-video', 'fast'],+ },{id: 'wan2gp:wan22-t2v',- name: 'Wan 2.2 (Text-to-Video)',- description: 'Video — Wan 2.2 text-to-video. Slow on consumer GPUs.',+ name: 'Wan 2.2 14B (Text-to-Video)',+ description: 'Video — Wan 2.2 text-to-video, 480p. Slow on consumer GPUs.',type: 'video',family: 'wan',provider: 'wan2gp',- aspectRatios: ['16:9', '1:1', '9:16'],- defaultSteps: 25,- defaultGuidance: 5.0,+ modelType: 't2v_2_2',+ aspectRatios: ['16:9', '9:16', '1:1'],tags: ['video', 'wan', 'text-to-video'],},{id: 'wan2gp:wan22-i2v',- name: 'Wan 2.2 (Image-to-Video)',- description: 'Video — Wan 2.2 image-to-video. Provide a start frame.',+ name: 'Wan 2.2 14B (Image-to-Video)',+ description: 'Video — Wan 2.2 image-to-video, 480p. Provide a start frame.',type: 'video',family: 'wan',provider: 'wan2gp',+ modelType: 'i2v_2_2',needsImage: true,- aspectRatios: ['16:9', '1:1', '9:16'],- defaultSteps: 25,- defaultGuidance: 5.0,+ aspectRatios: ['16:9', '9:16', '1:1'],tags: ['video', 'wan', 'image-to-video'],},{id: 'wan2gp:hunyuan-video',- name: 'Hunyuan Video (Wan2GP)',- description: 'Video — Hunyuan text-to-video via Wan2GP.',+ name: 'Hunyuan Video 1.5 (Wan2GP)',+ description: 'Video — Hunyuan Video 1.5 text-to-video, 480p.',type: 'video',family: 'hunyuan',provider: 'wan2gp',- aspectRatios: ['16:9', '1:1', '9:16'],- defaultSteps: 30,- defaultGuidance: 6.0,+ modelType: 'hunyuan_1_5_480_t2v',+ aspectRatios: ['16:9', '9:16', '1:1'],tags: ['video', 'hunyuan'],},{id: 'wan2gp:ltx-video',- name: 'LTX Video (Wan2GP)',- description: 'Video — LTX text-to-video. Fastest video option in Wan2GP.',+ name: 'LTX-2 Distilled (Wan2GP)',+ description: 'Video — LTX-2 distilled, 8 steps, 720p. Fastest large video model in Wan2GP.',type: 'video',family: 'ltx',provider: 'wan2gp',- aspectRatios: ['16:9', '1:1', '9:16'],- defaultSteps: 20,- defaultGuidance: 3.0,+ modelType: 'ltx2_22B_distilled',+ aspectRatios: ['16:9', '9:16', '1:1'],tags: ['video', 'ltx', 'fast'],},];diff --git a/tests/fixtures/fake-wan2gp/wgp.py b/tests/fixtures/fake-wan2gp/wgp.pynew file mode 100644index 00000000..163f4608--- /dev/null+++ b/tests/fixtures/fake-wan2gp/wgp.py@@ -0,0 +1,43 @@+// Fake Wan2GP entry point for tests/wan2gpLocal.test.js.+// Run with node (the tests use process.execPath as the "python" binary).+// Behaviour is picked by the prompt in the settings file: ok | fail | hang.+const fs = require('fs');+const path = require('path');+const { spawn } = require('child_process');++const args = process.argv.slice(2);+const settingsPath = args[args.indexOf('--process') + 1];+const outDir = args.includes('--output-dir') ? args[args.indexOf('--output-dir') + 1] : null;+const settings = JSON.parse(fs.readFileSync(settingsPath, 'utf-8'));++console.log(`WanGP CLI Mode - Processing settings: ${settingsPath}`);+console.log(`PYTHONUNBUFFERED=${process.env.PYTHONUNBUFFERED}`);++if (args.includes('--dry-run')) {+ console.log(` Task 1: model=${settings.model_type}, steps=30, frames=81`);+ console.log('[DRY-RUN] Validation complete. 1/1 task(s) valid.');+ process.exit(0);+}++if (settings.prompt === 'fail') {+ console.log('Loading Model Fake...');+ console.error('Traceback (most recent call last):\n File "wgp.py", line 1\ntorch.OutOfMemoryError: CUDA out of memory.');+ process.exit(1);+}++if (settings.prompt === 'hang') {+ const child = spawn(process.execPath, ['-e', 'setInterval(() => {}, 1000)'], { stdio: 'ignore' });+ fs.writeFileSync(path.join(outDir, 'child.pid'), String(child.pid));+ console.log(' [1/30] Denoising | 1.0s');+ setInterval(() => {}, 1000);+ return;+}++console.log('Loading Model Fake...');+process.stdout.write(' [1/2] Denoising | 1.0s\r [2/2] Denoising | 2.0s\n');+console.log(' [1/1] VAE Decoding | 3.0s');+const outFile = path.join(outDir, `seed${settings.seed}_${settings.prompt}.mp4`);+fs.writeFileSync(outFile, 'fake video');+console.log(` [0/1] Saving File x.mp4Video file saved to Path: ${outFile}`);+console.log(`image_start=${settings.image_start || ''}`);+console.log('Queue completed: 1/1 tasks in 3s');diff --git a/tests/wan2gpCatalog.test.js b/tests/wan2gpCatalog.test.jsnew file mode 100644index 00000000..1e67db08--- /dev/null+++ b/tests/wan2gpCatalog.test.js@@ -0,0 +1,82 @@+const test = require('node:test');+const assert = require('node:assert/strict');++const { WAN2GP_CATALOG, getModelById, buildWan2gpSettings } = require('../electron/lib/wan2gpCatalog');++test('catalog entries carry a real WanGP model_type and unique ids', () => {+ const ids = new Set();+ for (const model of WAN2GP_CATALOG) {+ assert.ok(model.modelType, `${model.id} has no modelType`);+ assert.equal(model.fn, undefined, `${model.id} still uses a Gradio fn`);+ assert.ok(!ids.has(model.id), `duplicate id ${model.id}`);+ ids.add(model.id);+ for (const ar of model.aspectRatios) {+ assert.match(model.resolutions[ar], /^\d+x\d+$/, `${model.id} has no resolution for ${ar}`);+ }+ }+});++test('frontend catalog (src/lib/localModels.js) mirrors the Wan2GP catalog', async () => {+ const { LOCAL_MODEL_CATALOG } = await import('../src/lib/localModels.js');+ const pick = (m) => ({ id: m.id, type: m.type, modelType: m.modelType, needsImage: !!m.needsImage, aspectRatios: m.aspectRatios });+ const frontend = LOCAL_MODEL_CATALOG.filter(m => m.provider === 'wan2gp').map(pick);+ assert.deepEqual(frontend, WAN2GP_CATALOG.map(pick));+});++test('getModelById finds catalog models and returns null otherwise', () => {+ assert.equal(getModelById('wan2gp:wan21-t2v-1.3b').modelType, 't2v_1.3B');+ assert.equal(getModelById('wan2gp:ltx-video').modelType, 'ltx2_22B_distilled');+ assert.equal(getModelById('dreamshaper-8'), null);+});++test('buildWan2gpSettings maps prompt, aspect ratio and seed onto WanGP settings', () => {+ const settings = buildWan2gpSettings(getModelById('wan2gp:wan21-t2v-1.3b'), {+ prompt: 'a fox in the snow',+ negative_prompt: 'blurry',+ aspect_ratio: '9:16',+ seed: 42,+ });++ assert.deepEqual(settings, {+ model_type: 't2v_1.3B',+ prompt: 'a fox in the snow',+ negative_prompt: 'blurry',+ resolution: '480x832',+ seed: 42,+ });+});++test('buildWan2gpSettings leaves steps and frames to the model defaults unless given', () => {+ const model = getModelById('wan2gp:ltx-video');+ const defaults = buildWan2gpSettings(model, { prompt: 'x', seed: 1 });+ assert.equal(defaults.num_inference_steps, undefined);+ assert.equal(defaults.video_length, undefined);+ assert.equal(defaults.resolution, '1280x720');++ const explicit = buildWan2gpSettings(model, { prompt: 'x', seed: 1, steps: '12', video_length: 97 });+ assert.equal(explicit.num_inference_steps, 12);+ assert.equal(explicit.video_length, 97);+});++test('buildWan2gpSettings picks a random seed when none or -1 is given', () => {+ const model = getModelById('wan2gp:wan21-t2v-1.3b');+ for (const seed of [undefined, -1, '']) {+ const settings = buildWan2gpSettings(model, { prompt: 'x', seed });+ assert.ok(Number.isInteger(settings.seed) && settings.seed >= 0);+ }+});++test('buildWan2gpSettings never sends frame counts to image models', () => {+ const settings = buildWan2gpSettings(getModelById('wan2gp:flux-dev'), { prompt: 'x', video_length: 81, aspect_ratio: '1:1', seed: 3 });+ assert.equal(settings.video_length, undefined);+ assert.equal(settings.resolution, '1024x1024');+});++test('buildWan2gpSettings passes the start frame as image_start and requires it for i2v', () => {+ const i2v = getModelById('wan2gp:wan22-i2v');+ assert.throws(() => buildWan2gpSettings(i2v, { prompt: 'x' }), /requires a start-frame image/);++ const settings = buildWan2gpSettings(i2v, { prompt: 'x', seed: 1 }, { imageStart: '/tmp/start.png' });+ assert.equal(settings.image_start, '/tmp/start.png');+ assert.equal(settings.model_type, 'i2v_2_2');+});diff --git a/tests/wan2gpLocal.test.js b/tests/wan2gpLocal.test.jsnew file mode 100644index 00000000..6e8b6dde--- /dev/null+++ b/tests/wan2gpLocal.test.js@@ -0,0 +1,98 @@+const test = require('node:test');+const assert = require('node:assert/strict');+const fs = require('fs');+const os = require('os');+const path = require('path');++const local = require('../electron/lib/wan2gpLocal');++const FAKE_DIR = path.join(__dirname, 'fixtures', 'fake-wan2gp');+// The fake wgp.py is a node script, so node plays the part of python.+const fakeInstall = () => local.resolveInstall({ dir: FAKE_DIR, python: process.execPath });+const tmpRunDir = () => fs.mkdtempSync(path.join(os.tmpdir(), 'wan2gp-run-'));++function isAlive(pid) {+ try { process.kill(pid, 0); return true; } catch { return false; }+}++test('resolveInstall defaults python to the Wan2GP venv', () => {+ assert.equal(local.resolveInstall({ dir: '' }), null);+ const posix = local.resolveInstall({ dir: '/opt/Wan2GP' }, 'linux');+ assert.equal(posix.python, path.join('/opt/Wan2GP', 'venv', 'bin', 'python'));+ assert.equal(posix.wgpPath, path.join('/opt/Wan2GP', 'wgp.py'));+ const custom = local.resolveInstall({ dir: '/opt/Wan2GP', python: '/usr/bin/python3.11' });+ assert.equal(custom.python, '/usr/bin/python3.11');+});++test('checkInstall explains what is missing', () => {+ assert.match(local.checkInstall(null).error, /not set/);+ assert.match(local.checkInstall(local.resolveInstall({ dir: '/definitely/not/here' })).error, /Folder not found/);+ assert.match(local.checkInstall(local.resolveInstall({ dir: os.tmpdir() })).error, /wgp\.py not found/);+ assert.match(local.checkInstall(local.resolveInstall({ dir: FAKE_DIR, python: '/no/python' })).error, /Python not found/);+ assert.deepEqual(local.checkInstall(fakeInstall()), { ok: true });+});++test('localModelStatus reads defaults/*.json and checks ckpts for weights', () => {+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'wan2gp-install-'));+ fs.mkdirSync(path.join(dir, 'defaults'));+ fs.mkdirSync(path.join(dir, 'ckpts'));+ const def = (urls) => JSON.stringify({ model: { name: 'x', URLs: urls } });+ fs.writeFileSync(path.join(dir, 'defaults', 't2v_1.3B.json'), def(['https://hf.co/x/resolve/main/wan_1.3B.safetensors']));+ fs.writeFileSync(path.join(dir, 'defaults', 't2v_2_2.json'), def(['https://hf.co/x/resolve/main/wan22_bf16.safetensors', 'https://hf.co/x/resolve/main/wan22_int8.safetensors']));+ fs.writeFileSync(path.join(dir, 'defaults', 'flux.json'), def('t2v_1.3B'));+ fs.writeFileSync(path.join(dir, 'ckpts', 'wan_1.3B.safetensors'), '');++ const status = local.localModelStatus(local.resolveInstall({ dir }), ['t2v_1.3B', 't2v_2_2', 'flux', 'ltx2_22B_distilled']);++ assert.equal(status.get('t2v_1.3B'), 'available');+ assert.equal(status.get('t2v_2_2'), 'missing');+ assert.equal(status.get('flux'), 'installed');+ assert.equal(status.has('ltx2_22B_distilled'), false);+});++test('runGeneration writes settings.json, streams progress and resolves the media path', async () => {+ const runDir = tmpRunDir();+ const events = [];+ const settings = { model_type: 't2v_1.3B', prompt: 'ok', seed: 7, image_start: '/tmp/start.png' };+ const { done } = local.runGeneration({ install: fakeInstall(), settings, runDir, onProgress: e => events.push(e) });+ const mediaPath = await done;++ assert.equal(mediaPath, path.join(runDir, 'out', 'seed7_ok.mp4'));+ assert.deepEqual(JSON.parse(fs.readFileSync(path.join(runDir, 'settings.json'), 'utf-8')), settings);+ assert.deepEqual(events.map(e => e.status), ['loading', 'generating', 'generating', 'decoding', 'saving']);+ const log = fs.readFileSync(path.join(runDir, 'wgp.log'), 'utf-8');+ assert.match(log, /PYTHONUNBUFFERED=1/);+ assert.match(log, /image_start=\/tmp\/start\.png/);+});++test('runGeneration rejects with a readable error and the log tail', async () => {+ const { done } = local.runGeneration({ install: fakeInstall(), settings: { model_type: 't2v_1.3B', prompt: 'fail' }, runDir: tmpRunDir() });+ await assert.rejects(done, (err) => {+ assert.match(err.message, /^GPU ran out of memory/);+ assert.match(err.message, /CUDA out of memory/);+ return true;+ });+});++test('killTree stops wgp.py and the processes it spawned', { skip: process.platform === 'win32' }, async () => {+ const runDir = tmpRunDir();+ const { child, done } = local.runGeneration({ install: fakeInstall(), settings: { model_type: 't2v_1.3B', prompt: 'hang' }, runDir });+ const pidFile = path.join(runDir, 'out', 'child.pid');+ for (let i = 0; i < 100 && !fs.existsSync(pidFile); i++) await new Promise(r => setTimeout(r, 50));+ const grandchild = Number(fs.readFileSync(pidFile, 'utf-8'));+ assert.ok(isAlive(child.pid) && isAlive(grandchild));++ local.killTree(child);+ await assert.rejects(done, /stopped \(SIGTERM\)/);+ for (let i = 0; i < 40 && isAlive(grandchild); i++) await new Promise(r => setTimeout(r, 50));+ assert.equal(isAlive(child.pid), false);+ assert.equal(isAlive(grandchild), false);+});++test('dryRun validates the install with --dry-run', async () => {+ const ok = await local.dryRun({ install: fakeInstall(), settings: { model_type: 't2v_1.3B', prompt: 'x' }, runDir: tmpRunDir() });+ assert.deepEqual(ok, { ok: true, message: 'Wan2GP validated a test task.' });++ const broken = await local.dryRun({ install: local.resolveInstall({ dir: FAKE_DIR, python: '/no/python' }), settings: {}, runDir: tmpRunDir() });+ assert.equal(broken.ok, false);+});diff --git a/tests/wan2gpMcp.test.js b/tests/wan2gpMcp.test.jsnew file mode 100644index 00000000..a5b072e1--- /dev/null+++ b/tests/wan2gpMcp.test.js@@ -0,0 +1,121 @@+const test = require('node:test');+const assert = require('node:assert/strict');++const { createMcpClient, normalizeMcpUrl, toProgressEvent } = require('../electron/lib/wan2gpMcp');++// Minimal fake of WanGP's MCP server (Streamable HTTP, SSE-framed replies).+function fakeServer(tools) {+ const calls = [];+ const reply = (body, extraHeaders = {}) => ({+ ok: true,+ status: 200,+ headers: new Map(Object.entries({ 'content-type': 'text/event-stream', ...extraHeaders })),+ text: async () => `event: message\ndata: ${JSON.stringify(body)}\n\n`,+ });+ const fetchImpl = async (url, init = {}) => {+ const u = String(url);+ if (u.includes('/wangp_api/gallery/download/')) {+ return { ok: true, status: 200, arrayBuffer: async () => new TextEncoder().encode('video-bytes').buffer };+ }+ if (u.includes('/wangp_api/gallery/upload/')) {+ calls.push({ upload: u, bytes: init.body.length });+ return { ok: true, status: 200, json: async () => ({ status: 'uploaded', media_id: 'visual:up1' }) };+ }+ const msg = JSON.parse(init.body);+ calls.push(msg);+ if (msg.method === 'initialize') return reply({ jsonrpc: '2.0', id: msg.id, result: { serverInfo: { name: 'WanGP', version: '1.30.0' } } }, { 'mcp-session-id': 's1' });+ if (msg.method === 'notifications/initialized') return { ok: true, status: 202, headers: new Map(), text: async () => '' };+ if (msg.method === 'tools/list') return reply({ jsonrpc: '2.0', id: msg.id, result: { tools: Object.keys(tools).map(name => ({ name })) } });+ const handler = tools[msg.params.name];+ const out = handler(msg.params.arguments);+ const result = out instanceof Error+ ? { isError: true, content: [{ type: 'text', text: out.message }] }+ : { structuredContent: out };+ return reply({ jsonrpc: '2.0', id: msg.id, result });+ };+ return { fetchImpl, calls };+}++test('normalizeMcpUrl appends /mcp once', () => {+ assert.equal(normalizeMcpUrl('http://gpu:7866'), 'http://gpu:7866/mcp');+ assert.equal(normalizeMcpUrl('http://gpu:7866/mcp/'), 'http://gpu:7866/mcp');+ assert.equal(normalizeMcpUrl(''), '');+});++test('toProgressEvent maps WanGP job events onto app progress', () => {+ assert.deepEqual(+ toProgressEvent({ kind: 'progress', data: { phase: 'inference', status: 'Denoising', progress: 36, current_step: 2, total_steps: 8 } }),+ { status: 'generating', progress: 0.36, step: 2, totalSteps: 8, message: 'Denoising' },+ );+ assert.equal(toProgressEvent({ kind: 'stream', data: { text: '25%|██' } }), null);++ const status = (phase) => toProgressEvent({ kind: 'progress', data: { phase, progress: 10 } }).status;+ assert.equal(status('encoding_text'), 'encoding');+ assert.equal(status('inference_stage_2'), 'generating');+ assert.equal(status('loading_model'), 'loading');+ assert.equal(status('downloading_output'), 'saving');+ assert.equal(status('something_new'), 'working');+});++test('probe initializes a session and requires WanGP generation tools', async () => {+ const { fetchImpl, calls } = fakeServer({ wangp_generate: () => ({}), wangp_get_job: () => ({}) });+ const client = createMcpClient('http://gpu:7866', { fetchImpl });+ assert.deepEqual(await client.probe(), { server: 'WanGP', version: '1.30.0' });+ assert.deepEqual(calls.map(c => c.method), ['initialize', 'notifications/initialized', 'tools/list']);++ const other = fakeServer({ some_tool: () => ({}) });+ await assert.rejects(createMcpClient('http://x', { fetchImpl: other.fetchImpl }).probe(), /does not expose WanGP generation tools/);+});++test('modelAvailability distinguishes downloaded, missing and unknown models', async () => {+ const { fetchImpl } = fakeServer({+ wangp_get_model_availability: ({ model_type }) => model_type === 'nope'+ ? new Error('Error executing tool wangp_get_model_availability: Unknown model_type: nope')+ : { model_type, status: model_type === 't2v_1.3B' ? 'available' : 'missing' },+ });+ const client = createMcpClient('http://gpu:7866', { fetchImpl });+ assert.equal(await client.modelAvailability('t2v_1.3B'), 'available');+ assert.equal(await client.modelAvailability('t2v_2_2'), 'missing');+ assert.equal(await client.modelAvailability('nope'), 'unknown-model');+});++test('generate submits, polls with progress, and download fetches the gallery item', { timeout: 20000 }, async () => {+ let polls = 0;+ const { fetchImpl, calls } = fakeServer({+ wangp_generate: () => ({ job_id: 'job1', done: false, events: [] }),+ wangp_get_job: () => (++polls < 2+ ? { job_id: 'job1', done: false, events: [{ kind: 'progress', data: { phase: 'inference', status: 'Denoising', progress: 50, current_step: 4, total_steps: 8 } }] }+ : { job_id: 'job1', done: true, result: { success: true, gallery_items: [{ media_id: 'visual:abc', media_type: 'video', filename: 'out.mp4' }] } }),+ wangp_create_gallery_download: () => ({ download_url: '/wangp_api/gallery/download/tok', filename: 'out.mp4' }),+ });+ const client = createMcpClient('http://gpu:7866/mcp', { fetchImpl });+ const progress = [];+ let jobId = null;+ const item = await client.generate({ model_type: 't2v_1.3B', prompt: 'x' }, { onProgress: p => progress.push(p), onJobId: id => { jobId = id; } });++ assert.equal(jobId, 'job1');+ assert.equal(item.media_id, 'visual:abc');+ assert.deepEqual(progress.map(p => [p.status, p.step]), [['generating', 4]]);+ const submit = calls.find(c => c.params?.name === 'wangp_generate');+ assert.deepEqual(submit.params.arguments, { source: { model_type: 't2v_1.3B', prompt: 'x' }, wait: false, event_limit: 0 });++ const file = await client.download(item.media_id);+ assert.equal(file.filename, 'out.mp4');+ assert.equal(file.bytes.toString(), 'video-bytes');+});++test('generate surfaces job errors', { timeout: 20000 }, async () => {+ const { fetchImpl } = fakeServer({+ wangp_generate: () => ({ job_id: 'job2', done: true, result: { success: false, errors: [{ message: 'CUDA out of memory' }] } }),+ });+ await assert.rejects(createMcpClient('http://gpu:7866', { fetchImpl }).generate({}), /generation failed:\nCUDA out of memory/);+});++test('uploadImage PUTs bytes to the one-time URL and returns the media id', async () => {+ const { fetchImpl, calls } = fakeServer({+ wangp_create_gallery_upload: ({ filename }) => ({ upload_url: '/wangp_api/gallery/upload/tok', filename }),+ });+ const mediaId = await createMcpClient('http://gpu:7866', { fetchImpl }).uploadImage({ name: 'start.png', bytes: Buffer.from('png'), mime: 'image/png' });+ assert.equal(mediaId, 'visual:up1');+ assert.deepEqual(calls.at(-1), { upload: 'http://gpu:7866/wangp_api/gallery/upload/tok', bytes: 3 });+});diff --git a/tests/wan2gpModelAvailability.test.js b/tests/wan2gpModelAvailability.test.jsindex 2d50810e..851371a2 100644--- a/tests/wan2gpModelAvailability.test.js+++ b/tests/wan2gpModelAvailability.test.js@@ -3,58 +3,54 @@ const assert = require('node:assert/strict');const { withWan2gpAvailability } = require('../electron/lib/wan2gpModelAvailability');-const fluxModel = {- id: 'wan2gp:flux-dev',- name: 'Flux.1 Dev (Wan2GP)',- fn: 'flux',+const wanModel = {+ id: 'wan2gp:wan21-t2v-1.3b',+ name: 'Wan 2.1 1.3B (Text-to-Video)',+ modelType: 't2v_1.3B',};-test('withWan2gpAvailability marks models unavailable when the server probe fails', () => {- const model = withWan2gpAvailability(fluxModel, { ok: false, error: 'Request timed out' });+const engine = (mode, statuses) => ({ ok: true, mode, modelStatus: new Map(Object.entries(statuses)) });++test('withWan2gpAvailability marks models unavailable when the engine is not usable', () => {+ const model = withWan2gpAvailability(wanModel, { ok: false, error: 'wgp.py not found in /tmp' });++ assert.equal(model.ready, false);+ assert.equal(model.unavailableReason, 'wgp.py not found in /tmp');+});++test('withWan2gpAvailability explains a missing configuration', () => {+ const model = withWan2gpAvailability(wanModel, undefined);assert.equal(model.ready, false);- assert.equal(model.unavailableReason, 'Request timed out');+ assert.match(model.unavailableReason, /not configured/);});-test('withWan2gpAvailability uses resolved api_name when endpoint metadata matches', () => {- const model = withWan2gpAvailability(- fluxModel,- { ok: true },- {- apiNames: ['flux_image'],- resolved: new Map([['wan2gp:flux-dev', 'flux_image']]),- }- );+test('withWan2gpAvailability marks models ready when weights are on disk', () => {+ const model = withWan2gpAvailability(wanModel, engine('local', { 't2v_1.3B': 'available' }));assert.equal(model.ready, true);- assert.equal(model.fn, 'flux_image');+ assert.equal(model.availabilityNote, undefined);});-test('withWan2gpAvailability keeps default api_name available when Gradio omits endpoint metadata', () => {- const model = withWan2gpAvailability(- fluxModel,- { ok: true },- {- apiNames: [],- resolved: new Map([['wan2gp:flux-dev', null]]),- }- );+test('withWan2gpAvailability keeps models with undownloaded weights ready, with a note', () => {+ const model = withWan2gpAvailability(wanModel, engine('remote', { 't2v_1.3B': 'missing' }));assert.equal(model.ready, true);- assert.equal(model.fn, 'flux');- assert.match(model.availabilityNote, /default api_name/);+ assert.match(model.availabilityNote, /first run downloads/);});-test('withWan2gpAvailability rejects unmatched models when endpoint metadata is present', () => {- const model = withWan2gpAvailability(- fluxModel,- { ok: true },- {- apiNames: ['qwen_image'],- resolved: new Map([['wan2gp:flux-dev', null]]),- }- );+test('withWan2gpAvailability treats a defined model with unknown weight files as ready', () => {+ const model = withWan2gpAvailability(wanModel, engine('local', { 't2v_1.3B': 'installed' }));- assert.equal(model.ready, false);- assert.match(model.unavailableReason, /no api_name matching "flux"/);+ assert.equal(model.ready, true);+});++test('withWan2gpAvailability rejects model types the Wan2GP install does not define', () => {+ const localModel = withWan2gpAvailability(wanModel, engine('local', {}));+ const remoteModel = withWan2gpAvailability(wanModel, engine('remote', { 't2v_1.3B': 'unknown-model' }));++ assert.equal(localModel.ready, false);+ assert.match(localModel.unavailableReason, /This Wan2GP install has no model "t2v_1.3B"/);+ assert.equal(remoteModel.ready, false);+ assert.match(remoteModel.unavailableReason, /The Wan2GP server has no model "t2v_1.3B"/);});diff --git a/tests/wan2gpOutput.test.js b/tests/wan2gpOutput.test.jsnew file mode 100644index 00000000..5b8c95d1--- /dev/null+++ b/tests/wan2gpOutput.test.js@@ -0,0 +1,90 @@+const test = require('node:test');+const assert = require('node:assert/strict');++const { createProgressParser, findSavedPath, summarizeRun } = require('../electron/lib/wan2gpOutput');++// Excerpts of real `wgp.py --process` console output (WanGP v13.141).+const LOADING = [+ 'Loading Model Wan2.1 Text2video 1.3B...',+ ' [0/1] Loading Wan2.1 Text2video 1.3B - Preparing Models',+ ' [3/12] Loading Wan2.1 Text2video 1.3B - Scanning Sizes VAE Model',+].join('\n') + '\n';+const DENOISING = ' [0/30] Denoising | 2.5s\r [0/30] Denoising | 3.5s\r 3%|▎ | 1/30 [00:09<04:31, 9.35s/it]\r [1/30] Denoising | 12.1s / 4m 51s\r [1/30] Denoising | 13.4s / 4m 51s\n';+const DECODING = ' [21/21] VAE Decoding | 4m 58s / 4m 58s\n';+const SUCCESS_TAIL = [+ ' [0/1] Saving File 2026-09-30-1...nowflakes falling, cinematic tr.mp4Video file saved to Path: /out/2026-09-30-10h08m24s_seed42_A red fox trotting.mp4',+ ' Task 1 completed',+ '==================================================',+ 'Queue completed: 1/1 tasks in 14m 39s',+].join('\n') + '\n';++test('progress parser reports loading, denoising steps and decoding in order', () => {+ const parse = createProgressParser();+ const events = [...parse(LOADING), ...parse(DENOISING), ...parse(DECODING)];++ assert.deepEqual(events.map(e => e.status), ['loading', 'loading', 'loading', 'generating', 'generating', 'decoding']);+ const denoise = events.filter(e => e.status === 'generating');+ assert.deepEqual(denoise.map(e => [e.step, e.totalSteps]), [[0, 30], [1, 30]]);+ for (let i = 1; i < events.length; i++) {+ assert.ok(events[i].progress >= events[i - 1].progress, 'progress never goes backwards');+ }+ assert.ok(events.at(-1).progress > 0.9 && events.at(-1).progress < 1);+});++test('progress parser handles lines split across chunks and ignores repeats', () => {+ const parse = createProgressParser();+ assert.deepEqual(parse(' [5/30] Deno'), []);+ const events = parse('ising | 50.9s / 4m 45s\n [5/30] Denoising | 52.1s / 4m 45s\n');+ assert.equal(events.length, 1);+ assert.equal(events[0].step, 5);+});++test('progress parser ignores compiler noise and tqdm bars', () => {+ const parse = createProgressParser();+ const noise = '/usr/include/features.h:319:10: note: previous definition\n 1796 | #define _POSIX_C_SOURCE 200809L\n 10%|█ | 3/30 [00:28<04:12, 9.35s/it]\n';+ assert.deepEqual(parse(noise), []);+});++test('findSavedPath returns the last reported path, including spaces', () => {+ assert.equal(findSavedPath(SUCCESS_TAIL), '/out/2026-09-30-10h08m24s_seed42_A red fox trotting.mp4');+ const remuxed = `${SUCCESS_TAIL}Remuxed Video saved to Path: /out/final.mp4\n`;+ assert.equal(findSavedPath(remuxed), '/out/final.mp4');+ assert.equal(findSavedPath('nothing here'), null);+});++test('summarizeRun accepts a completed queue and returns the media path', () => {+ const result = summarizeRun({ exitCode: 0, signal: null, output: LOADING + DENOISING + SUCCESS_TAIL });+ assert.deepEqual(result, { ok: true, savedPath: '/out/2026-09-30-10h08m24s_seed42_A red fox trotting.mp4' });+});++test('summarizeRun reports CUDA OOM in plain words with the log tail', () => {+ const output = `${DENOISING}Traceback (most recent call last):\n File "wgp.py", line 1\ntorch.OutOfMemoryError: CUDA out of memory. Tried to allocate 2.00 GiB\n`;+ const result = summarizeRun({ exitCode: 1, signal: null, output });+ assert.equal(result.ok, false);+ assert.match(result.error, /^GPU ran out of memory/);+ assert.match(result.error, /Tried to allocate 2\.00 GiB/);+});++test('summarizeRun surfaces the exception line of a Python traceback', () => {+ const output = 'Traceback (most recent call last):\n File "wgp.py", line 9, in <module>\n main()\nValueError: resolution 123x45 is not supported\n';+ const result = summarizeRun({ exitCode: 1, signal: null, output });+ assert.match(result.error, /^ValueError: resolution 123x45 is not supported/);+});++test('summarizeRun surfaces WanGP [ERROR] lines and missing input files', () => {+ const unknown = summarizeRun({ exitCode: 1, signal: null, output: '[load_settings] Unknown model type: nope for task #1. Skipping.\n[ERROR] Unknown model type: nope\n' });+ assert.match(unknown.error, /^Unknown model type: nope/);++ const missing = summarizeRun({ exitCode: 1, signal: null, output: "[load_settings] Warning: File not found for 'image_start': /tmp/gone.png\nQueue completed: 0/1 tasks in 1s\n" });+ assert.match(missing.error, /^Input file for "image_start" not found: \/tmp\/gone\.png/);+});++test('summarizeRun treats a partial queue or a signal as failure', () => {+ const partial = summarizeRun({ exitCode: 0, signal: null, output: 'Queue completed: 0/1 tasks in 3s\n' });+ assert.equal(partial.ok, false);+ assert.match(partial.error, /finished 0\/1 tasks/);++ const killed = summarizeRun({ exitCode: null, signal: 'SIGTERM', output: DENOISING });+ assert.equal(killed.ok, false);+ assert.match(killed.error, /stopped \(SIGTERM\)/);+});