fix(dashboard): restore GET /api/models + fs fallbacks for comfy models/types
## Three bugs found from /api/debug output User reported: 'in UI ни комфи (причем и типы и модели) ни ллама модели не видны' — even though /api/debug shows the directories exist with files in them. ### Bug 1: GET /api/models was deleted In PR #5 I added `DELETE /api/models?name=...` but accidentally removed the existing `GET /api/models` handler when I rewrote the file with the Write tool. The frontend was getting 405 Method Not Allowed, but `refreshModels()` silently swallowed the error (try/catch with no logging). Fix: restore the GET handler alongside DELETE. ### Bug 2: refreshComfyModels() was never called The initial useEffect only called refreshConfig, refreshModels, refreshComfyTypes, and an interval for refreshComfyQueue. refreshComfyModels was defined as a useCallback but never invoked on mount — so the Comfy models table was always empty until the user clicked Refresh manually. Fix: add refreshComfyModels() to the initial useEffect. ### Bug 3: /api/comfy/types and /api/comfy/models fail when model-worker is offline Both endpoints proxied to `docker exec model-worker python3 /app/worker.py` — which fails (returns empty / error) when the model-worker container isn't running (e.g. during sleep mode, or right after a reboot before wake). The /api/debug endpoint showed that the directories exist on disk via fs.readdir — so we have the data, just weren't using it. Fix: add fs-based fallbacks in dashboard/src/lib/docker.ts: - `listComfyTypesFromFs()` — reads subdirectories of /project/gpu/comfy/models directly. Same as what `worker.py types` does, but no docker exec required. - `listComfyModelsFromFs(type?)` — walks each subdirectory and lists files. Same as what `worker.py list --json` does, but no docker exec. - `listComfyModels()` and `getComfyTypes()` now try the worker first, then fall back to fs. If both succeed, the worker's output wins (it filters out metadata files like .txt). - New export `COMFY_MODELS_DIR` — the internal mount path, used by both the fallback functions and /api/debug. ### Bonus: error visibility All `refresh*()` callbacks in page.tsx now `console.error` on failure instead of silently swallowing. The next time something breaks, you'll see it in the browser DevTools console. ## Verification - `npm run build` — OK, all 19 routes (including restored GET /api/models) - `bash dashboard/validate-build.sh` — all 8 COPY sources ✓, standalone ✓ - /api/debug still works (uses the same fs approach) ## Files changed - dashboard/src/app/api/models/route.ts — restored GET handler - dashboard/src/app/page.tsx — added refreshComfyModels() to initial useEffect, console.error on refresh failures - dashboard/src/lib/docker.ts — added COMFY_MODELS_DIR export, fs-based fallbacks for comfy types/models, try-worker-first-then-fs flow
This commit is contained in:
@@ -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
|
||||
*
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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<string[]> {
|
||||
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<ComfyModelEntry[]> {
|
||||
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 {
|
||||
|
||||
Reference in New Issue
Block a user