diff --git a/dashboard/src/app/api/models/route.ts b/dashboard/src/app/api/models/route.ts index fb16653..7fed163 100644 --- a/dashboard/src/app/api/models/route.ts +++ b/dashboard/src/app/api/models/route.ts @@ -4,6 +4,17 @@ import { deleteModel, listModels } from "@/lib/docker"; export const dynamic = "force-dynamic"; export const runtime = "nodejs"; +/** + * GET /api/models + * + * Lists all .gguf files in gpu/llama_models/. Returns: + * { models: [{ name, sizeBytes, sizeHuman, modifiedAt }] } + */ +export async function GET() { + const models = await listModels(); + return NextResponse.json({ models }); +} + /** * DELETE /api/models?name=foo.gguf * diff --git a/dashboard/src/app/page.tsx b/dashboard/src/app/page.tsx index dd5e5f9..5a5aa05 100644 --- a/dashboard/src/app/page.tsx +++ b/dashboard/src/app/page.tsx @@ -254,11 +254,14 @@ export default function DashboardPage() { const refreshModels = React.useCallback(async () => { try { const res = await fetch("/api/models", { cache: "no-store" }); - if (!res.ok) return; + if (!res.ok) { + console.error("[refreshModels] HTTP", res.status, await res.text().catch(() => "")); + return; + } const data = await res.json(); setModels(data.models || []); - } catch { - /* ignore */ + } catch (err) { + console.error("[refreshModels] error:", err); } }, []); @@ -268,8 +271,8 @@ export default function DashboardPage() { if (!res.ok) return; const data = await res.json(); setConfig(data.config || DEFAULT_CONFIG); - } catch { - /* ignore */ + } catch (err) { + console.error("[refreshConfig] error:", err); } }, []); @@ -296,8 +299,8 @@ export default function DashboardPage() { // If current selection isn't in the new list, switch to first available setComfyAddType((cur) => (types.includes(cur) ? cur : types[0])); } - } catch { - /* keep fallback list */ + } catch (err) { + console.error("[refreshComfyTypes] error:", err); } }, []); @@ -309,7 +312,8 @@ export default function DashboardPage() { const res = await fetch(url, { cache: "no-store" }); const data = await res.json(); setComfyModels(data.models || []); - } catch { + } catch (err) { + console.error("[refreshComfyModels] error:", err); setComfyModels([]); } }, [comfyFilter]); @@ -319,7 +323,8 @@ export default function DashboardPage() { const res = await fetch("/api/comfy/queue", { cache: "no-store" }); const data = await res.json(); setComfyQueue(data.queue || []); - } catch { + } catch (err) { + // Queue refresh runs every 5s — don't spam console on network blips setComfyQueue([]); } }, []); @@ -328,9 +333,16 @@ export default function DashboardPage() { refreshConfig(); refreshModels(); refreshComfyTypes(); + refreshComfyModels(); const id = setInterval(refreshComfyQueue, 5000); return () => clearInterval(id); - }, [refreshConfig, refreshModels, refreshComfyTypes, refreshComfyQueue]); + }, [ + refreshConfig, + refreshModels, + refreshComfyTypes, + refreshComfyModels, + refreshComfyQueue, + ]); // --------------------------------------------------------------------------- // Live logs streaming via Server-Sent Events (replaces fetch+readablestream) diff --git a/dashboard/src/lib/docker.ts b/dashboard/src/lib/docker.ts index e85487e..bdab012 100644 --- a/dashboard/src/lib/docker.ts +++ b/dashboard/src/lib/docker.ts @@ -124,6 +124,14 @@ export const TAILSCALE_HOSTNAME = envOrFile("TAILSCALE_HOSTNAME", "gpu-node"); export const LLAMA_MODELS_DIR = process.env.LLAMA_MODELS_DIR || path.join(PROJECT_DIR, "gpu", "llama_models"); +/** + * Internal mount of the comfy models directory (mirrors the host path that + * the model-worker container mounts as /models). + */ +export const COMFY_MODELS_DIR = + process.env.COMFY_MODELS_DIR || + path.join(PROJECT_DIR, "gpu", "comfy", "models"); + /** * Node instance name (used for filtering containers by label). */ @@ -967,43 +975,127 @@ export interface ComfyModelEntry { sizeHuman?: string; } +/** + * Read comfy model types directly from the filesystem (subdirs of + * /project/gpu/comfy/models). Used as a fallback when the model-worker + * container isn't running. + * + * Returns the same shape as `worker.py types`. + */ +async function listComfyTypesFromFs(): Promise { + try { + const entries = await fs.readdir(COMFY_MODELS_DIR, { withFileTypes: true }); + return entries + .filter((e) => e.isDirectory() && !e.isSymbolicLink()) + .map((e) => e.name) + .sort(); + } catch { + return []; + } +} + +/** + * Read comfy models directly from the filesystem. Walks each subdirectory + * (one per model type) and lists all files inside. Used as a fallback when + * the model-worker container isn't running. + * + * Returns the same shape as `worker.py list --json`. + */ +async function listComfyModelsFromFs( + type?: string +): Promise { + const out: ComfyModelEntry[] = []; + try { + if (type) { + // Single type — list its contents + const typeDir = path.join(COMFY_MODELS_DIR, type); + const stat = await fs.stat(typeDir).catch(() => null); + if (!stat || !stat.isDirectory()) return []; + const entries = await fs.readdir(typeDir, { withFileTypes: true }); + for (const entry of entries) { + if (!entry.isFile()) continue; + if (entry.name.startsWith(".")) continue; + const full = path.join(typeDir, entry.name); + const size = await fs.stat(full).then((s) => s.size).catch(() => 0); + out.push({ + type, + filename: entry.name, + sizeBytes: size, + sizeHuman: size > 0 ? humanSize(size) : undefined, + }); + } + } else { + // All types — walk each subdir + const types = await listComfyTypesFromFs(); + for (const t of types) { + const typeDir = path.join(COMFY_MODELS_DIR, t); + const entries = await fs.readdir(typeDir, { withFileTypes: true }); + for (const entry of entries) { + if (!entry.isFile()) continue; + if (entry.name.startsWith(".")) continue; + const full = path.join(typeDir, entry.name); + const size = await fs.stat(full).then((s) => s.size).catch(() => 0); + out.push({ + type: t, + filename: entry.name, + sizeBytes: size, + sizeHuman: size > 0 ? humanSize(size) : undefined, + }); + } + } + } + } catch { + /* ignore */ + } + return out; +} + export async function listComfyModels( type?: string ): Promise<{ ok: boolean; models: ComfyModelEntry[]; raw: string; error?: string }> { - // Use the new --json flag so we get structured output instead of having to - // parse the human-readable table. + // Try the model-worker first — it knows which files are real models vs + // metadata files (.txt etc.) and follows ComfyUI's naming conventions. const args = ["list"]; if (type) args.push(type); args.push("--json"); const res = await modelWorkerExec(...args); - if (!res.ok) { - return { ok: false, models: [], raw: res.output, error: res.error }; - } - try { - const parsed = JSON.parse(res.output); - // worker.py outputs {"models": [...], "total": N, "totalBytes": N} - // or {"error": "...", "available_types": [...]} on type-not-found - if (parsed.error) { - return { - ok: false, - models: [], - raw: res.output, - error: parsed.error, - }; + + if (res.ok) { + try { + const parsed = JSON.parse(res.output); + if (parsed.error) { + return { + ok: false, + models: [], + raw: res.output, + error: parsed.error, + }; + } + const arr: ComfyModelEntry[] = (parsed.models || []).map( + (m: { type: string; filename: string; sizeBytes?: number }) => ({ + type: m.type, + filename: m.filename, + sizeBytes: m.sizeBytes, + sizeHuman: m.sizeBytes ? humanSize(m.sizeBytes) : undefined, + }) + ); + return { ok: true, models: arr, raw: res.output }; + } catch { + // Fall through to fs fallback } - const arr: ComfyModelEntry[] = (parsed.models || []).map( - (m: { type: string; filename: string; sizeBytes?: number }) => ({ - type: m.type, - filename: m.filename, - sizeBytes: m.sizeBytes, - sizeHuman: m.sizeBytes ? humanSize(m.sizeBytes) : undefined, - }) - ); - return { ok: true, models: arr, raw: res.output }; - } catch { - // Probably worker.py without --json support (older version) - return { ok: true, models: [], raw: res.output }; } + + // Fallback: read directly from the filesystem. Useful when: + // - model-worker container isn't running (e.g. during sleep mode) + // - worker.py doesn't support --json flag (older version) + // - docker exec fails for any reason + const models = await listComfyModelsFromFs(type); + return { + ok: true, + models, + raw: res.output || "", + error: res.ok ? undefined : res.error, + }; } export async function getComfyTypes(): Promise<{ @@ -1012,17 +1104,30 @@ export async function getComfyTypes(): Promise<{ raw: string; error?: string; }> { - // Uses the new `worker.py types` command — outputs JSON array of strings. - // Falls back to an empty list if the worker version doesn't support it yet. + // Try the model-worker first. const res = await modelWorkerExec("types"); - if (!res.ok) return { ok: false, types: [], raw: res.output, error: res.error }; - try { - const parsed = JSON.parse(res.output); - const arr: string[] = Array.isArray(parsed) ? parsed : []; - return { ok: true, types: arr, raw: res.output }; - } catch { - return { ok: true, types: [], raw: res.output }; + if (res.ok) { + try { + const parsed = JSON.parse(res.output); + const arr: string[] = Array.isArray(parsed) ? parsed : []; + if (arr.length > 0) { + return { ok: true, types: arr, raw: res.output }; + } + } catch { + /* fall through */ + } } + + // Fallback: read subdirectories of gpu/comfy/models directly. + // The model-worker container might not be running (sleep mode), but the + // directory is mounted into the dashboard container at /project/gpu/comfy. + const types = await listComfyTypesFromFs(); + return { + ok: types.length > 0, + types, + raw: res.output || "", + error: types.length > 0 ? undefined : res.error || "no types found", + }; } export interface ComfyQueueEntry {