Merge pull request 'fix(dashboard): restore GET /api/models + fs fallbacks for comfy models/types' (#6) from fix/dashboard-ui-visibility into master
Reviewed-on: #6
This commit was merged in pull request #6.
This commit is contained in:
@@ -4,6 +4,17 @@ import { deleteModel, listModels } from "@/lib/docker";
|
|||||||
export const dynamic = "force-dynamic";
|
export const dynamic = "force-dynamic";
|
||||||
export const runtime = "nodejs";
|
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
|
* DELETE /api/models?name=foo.gguf
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -254,11 +254,14 @@ export default function DashboardPage() {
|
|||||||
const refreshModels = React.useCallback(async () => {
|
const refreshModels = React.useCallback(async () => {
|
||||||
try {
|
try {
|
||||||
const res = await fetch("/api/models", { cache: "no-store" });
|
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();
|
const data = await res.json();
|
||||||
setModels(data.models || []);
|
setModels(data.models || []);
|
||||||
} catch {
|
} catch (err) {
|
||||||
/* ignore */
|
console.error("[refreshModels] error:", err);
|
||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
@@ -268,8 +271,8 @@ export default function DashboardPage() {
|
|||||||
if (!res.ok) return;
|
if (!res.ok) return;
|
||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
setConfig(data.config || DEFAULT_CONFIG);
|
setConfig(data.config || DEFAULT_CONFIG);
|
||||||
} catch {
|
} catch (err) {
|
||||||
/* ignore */
|
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
|
// If current selection isn't in the new list, switch to first available
|
||||||
setComfyAddType((cur) => (types.includes(cur) ? cur : types[0]));
|
setComfyAddType((cur) => (types.includes(cur) ? cur : types[0]));
|
||||||
}
|
}
|
||||||
} catch {
|
} catch (err) {
|
||||||
/* keep fallback list */
|
console.error("[refreshComfyTypes] error:", err);
|
||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
@@ -309,7 +312,8 @@ export default function DashboardPage() {
|
|||||||
const res = await fetch(url, { cache: "no-store" });
|
const res = await fetch(url, { cache: "no-store" });
|
||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
setComfyModels(data.models || []);
|
setComfyModels(data.models || []);
|
||||||
} catch {
|
} catch (err) {
|
||||||
|
console.error("[refreshComfyModels] error:", err);
|
||||||
setComfyModels([]);
|
setComfyModels([]);
|
||||||
}
|
}
|
||||||
}, [comfyFilter]);
|
}, [comfyFilter]);
|
||||||
@@ -319,7 +323,8 @@ export default function DashboardPage() {
|
|||||||
const res = await fetch("/api/comfy/queue", { cache: "no-store" });
|
const res = await fetch("/api/comfy/queue", { cache: "no-store" });
|
||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
setComfyQueue(data.queue || []);
|
setComfyQueue(data.queue || []);
|
||||||
} catch {
|
} catch (err) {
|
||||||
|
// Queue refresh runs every 5s — don't spam console on network blips
|
||||||
setComfyQueue([]);
|
setComfyQueue([]);
|
||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
@@ -328,9 +333,16 @@ export default function DashboardPage() {
|
|||||||
refreshConfig();
|
refreshConfig();
|
||||||
refreshModels();
|
refreshModels();
|
||||||
refreshComfyTypes();
|
refreshComfyTypes();
|
||||||
|
refreshComfyModels();
|
||||||
const id = setInterval(refreshComfyQueue, 5000);
|
const id = setInterval(refreshComfyQueue, 5000);
|
||||||
return () => clearInterval(id);
|
return () => clearInterval(id);
|
||||||
}, [refreshConfig, refreshModels, refreshComfyTypes, refreshComfyQueue]);
|
}, [
|
||||||
|
refreshConfig,
|
||||||
|
refreshModels,
|
||||||
|
refreshComfyTypes,
|
||||||
|
refreshComfyModels,
|
||||||
|
refreshComfyQueue,
|
||||||
|
]);
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// Live logs streaming via Server-Sent Events (replaces fetch+readablestream)
|
// 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 =
|
export const LLAMA_MODELS_DIR =
|
||||||
process.env.LLAMA_MODELS_DIR || path.join(PROJECT_DIR, "gpu", "llama_models");
|
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).
|
* Node instance name (used for filtering containers by label).
|
||||||
*/
|
*/
|
||||||
@@ -967,43 +975,127 @@ export interface ComfyModelEntry {
|
|||||||
sizeHuman?: string;
|
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(
|
export async function listComfyModels(
|
||||||
type?: string
|
type?: string
|
||||||
): Promise<{ ok: boolean; models: ComfyModelEntry[]; raw: string; error?: string }> {
|
): Promise<{ ok: boolean; models: ComfyModelEntry[]; raw: string; error?: string }> {
|
||||||
// Use the new --json flag so we get structured output instead of having to
|
// Try the model-worker first — it knows which files are real models vs
|
||||||
// parse the human-readable table.
|
// metadata files (.txt etc.) and follows ComfyUI's naming conventions.
|
||||||
const args = ["list"];
|
const args = ["list"];
|
||||||
if (type) args.push(type);
|
if (type) args.push(type);
|
||||||
args.push("--json");
|
args.push("--json");
|
||||||
const res = await modelWorkerExec(...args);
|
const res = await modelWorkerExec(...args);
|
||||||
if (!res.ok) {
|
|
||||||
return { ok: false, models: [], raw: res.output, error: res.error };
|
if (res.ok) {
|
||||||
}
|
try {
|
||||||
try {
|
const parsed = JSON.parse(res.output);
|
||||||
const parsed = JSON.parse(res.output);
|
if (parsed.error) {
|
||||||
// worker.py outputs {"models": [...], "total": N, "totalBytes": N}
|
return {
|
||||||
// or {"error": "...", "available_types": [...]} on type-not-found
|
ok: false,
|
||||||
if (parsed.error) {
|
models: [],
|
||||||
return {
|
raw: res.output,
|
||||||
ok: false,
|
error: parsed.error,
|
||||||
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<{
|
export async function getComfyTypes(): Promise<{
|
||||||
@@ -1012,17 +1104,30 @@ export async function getComfyTypes(): Promise<{
|
|||||||
raw: string;
|
raw: string;
|
||||||
error?: string;
|
error?: string;
|
||||||
}> {
|
}> {
|
||||||
// Uses the new `worker.py types` command — outputs JSON array of strings.
|
// Try the model-worker first.
|
||||||
// Falls back to an empty list if the worker version doesn't support it yet.
|
|
||||||
const res = await modelWorkerExec("types");
|
const res = await modelWorkerExec("types");
|
||||||
if (!res.ok) return { ok: false, types: [], raw: res.output, error: res.error };
|
if (res.ok) {
|
||||||
try {
|
try {
|
||||||
const parsed = JSON.parse(res.output);
|
const parsed = JSON.parse(res.output);
|
||||||
const arr: string[] = Array.isArray(parsed) ? parsed : [];
|
const arr: string[] = Array.isArray(parsed) ? parsed : [];
|
||||||
return { ok: true, types: arr, raw: res.output };
|
if (arr.length > 0) {
|
||||||
} catch {
|
return { ok: true, types: arr, raw: res.output };
|
||||||
return { ok: true, types: [], 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 {
|
export interface ComfyQueueEntry {
|
||||||
|
|||||||
Reference in New Issue
Block a user