diff --git a/dashboard/src/app/api/comfy/models/add/route.ts b/dashboard/src/app/api/comfy/models/add/route.ts index 7a6f50d..a190482 100644 --- a/dashboard/src/app/api/comfy/models/add/route.ts +++ b/dashboard/src/app/api/comfy/models/add/route.ts @@ -7,7 +7,17 @@ export const runtime = "nodejs"; /** * POST /api/comfy/models/add * Body: { url: string, type: string } - * Queues a model download via model-worker. + * + * Queues a ComfyUI model download via model-worker. CivitAI URLs are + * automatically transformed (model page → API download URL) and the saved + * CivitAI token is appended. + * + * Returns: + * { ok, output, error?, originalUrl?, transformedUrl?, urlNote? } + * + * The originalUrl/transformedUrl/urlNote fields are only present when we + * actually modified the URL (e.g. user pasted a civitai.com/models/12345 + * page URL and we converted it to /api/download/models/12345?token=...). */ export async function POST(req: NextRequest) { const body = (await req.json()) as { url?: string; type?: string }; @@ -19,7 +29,14 @@ export async function POST(req: NextRequest) { } const result = await addComfyModel(body.url, body.type); return NextResponse.json( - { ok: result.ok, output: result.output, error: result.error }, + { + ok: result.ok, + output: result.output, + error: result.error, + originalUrl: result.originalUrl, + transformedUrl: result.transformedUrl, + urlNote: result.urlNote, + }, { status: result.ok ? 200 : 500 } ); } diff --git a/dashboard/src/app/page.tsx b/dashboard/src/app/page.tsx index 421cf7d..05366fd 100644 --- a/dashboard/src/app/page.tsx +++ b/dashboard/src/app/page.tsx @@ -288,6 +288,9 @@ export default function DashboardPage() { civitaiToken: "", }); const [urlPreview, setUrlPreview] = React.useState(null); + // Separate preview state for the Comfy tab's URL field (same logic as urlPreview, + // just tracks a different input) + const [comfyUrlPreview, setComfyUrlPreview] = React.useState(null); // Chat state — kept at top level so switching tabs doesn't reset the // conversation. ChatTab is mounted/unmounted when the user clicks the tab. @@ -896,6 +899,45 @@ export default function DashboardPage() { }; }, [downloadUrl, previewUrl]); + // Same debounced preview, but for the Comfy tab's URL field + const comfyPreviewDebounceRef = React.useRef | null>(null); + React.useEffect(() => { + if (comfyPreviewDebounceRef.current) + clearTimeout(comfyPreviewDebounceRef.current); + comfyPreviewDebounceRef.current = setTimeout(() => { + void (async () => { + if (!comfyAddUrl.trim()) { + setComfyUrlPreview(null); + return; + } + try { + const res = await fetch("/api/settings", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ action: "preview-url", url: comfyAddUrl }), + }); + if (!res.ok) return; + const data = await res.json(); + if (data.ok) { + setComfyUrlPreview({ + original: data.original, + transformed: data.transformed, + note: data.note, + isCivitai: data.isCivitai, + }); + } + } catch { + /* ignore */ + } + })(); + }, 400); + return () => { + if (comfyPreviewDebounceRef.current) + clearTimeout(comfyPreviewDebounceRef.current); + }; + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [comfyAddUrl]); + React.useEffect(() => { refreshSettings(); }, [refreshSettings]); @@ -1206,10 +1248,22 @@ export default function DashboardPage() { const data = await res.json(); setComfyAddUrl(""); await refreshComfyQueue(); - return { - ok: data.ok, - message: data.ok ? `Queued: ${comfyAddType}` : data.error || "Failed", - }; + // Build a helpful message — if the URL was transformed (CivitAI + // model page → API download URL with token), include the note so the + // user can see what happened. + let message: string; + if (data.ok) { + message = `Queued: ${comfyAddType}`; + if (data.urlNote) { + message += `\n${data.urlNote}`; + } + if (data.transformedUrl) { + message += `\n→ ${data.transformedUrl}`; + } + } else { + message = data.error || "Failed"; + } + return { ok: data.ok, message }; }, false); } @@ -1457,6 +1511,9 @@ export default function DashboardPage() { setAddType={setComfyAddType} onRemove={handleComfyRemoveModel} busy={busy} + civitaiToken={settings.civitaiToken} + onSaveCivitaiToken={handleSaveCivitaiToken} + urlPreview={comfyUrlPreview} /> @@ -2747,6 +2804,9 @@ function ComfyTab({ setAddType, onRemove, busy, + civitaiToken, + onSaveCivitaiToken, + urlPreview, }: { models: ComfyModelEntry[]; queue: ComfyQueueEntry[]; @@ -2763,7 +2823,17 @@ function ComfyTab({ setAddType: (v: string) => void; onRemove: (filename: string, type?: string) => void; busy: string | null; + civitaiToken: string; + onSaveCivitaiToken: (token: string) => void; + urlPreview: UrlPreview | null; }) { + const [showCivitaiSettings, setShowCivitaiSettings] = React.useState(false); + const [civitaiTokenDraft, setCivitaiTokenDraft] = React.useState(civitaiToken); + + React.useEffect(() => { + setCivitaiTokenDraft(civitaiToken); + }, [civitaiToken]); + return (
{/* Download queue */} @@ -2848,22 +2918,94 @@ function ComfyTab({ {/* Add model */} - - - - Queue Download - - - Mirrors gpuctl comfy models add <URL> <type> - + +
+ + + Queue Download + + + Mirrors gpuctl comfy models add <URL> <type>. + {" "}CivitAI URLs auto-converted (model page → API download), + token appended. + +
+
- + + {showCivitaiSettings && ( +
+ + setCivitaiTokenDraft(e.target.value)} + placeholder="Get it from https://civitai.com/user/account → API Keys" + className="font-mono text-xs" + /> +
+ + + + Get token ↗ + +
+

+ Shared with the llama-models downloader. Stored in{" "} + gpu/dashboard/settings.json. +

+
+ )} +
setAddUrl(e.target.value)} className="font-mono text-xs" + onKeyDown={(e) => { + if (e.key === "Enter" && !e.shiftKey) { + e.preventDefault(); + if (addUrl.trim() && busy === null) onAdd(); + } + }} /> -
+ + {/* URL preview — shows what we'll actually send to the worker */} + {urlPreview && urlPreview.original !== urlPreview.transformed && ( +
+
+ + {urlPreview.isCivitai ? "CivitAI" : "transformed"} + + {urlPreview.note} +
+
+                {urlPreview.transformed}
+              
+
+ )}
diff --git a/dashboard/src/lib/docker.ts b/dashboard/src/lib/docker.ts index ce0c62c..26d1cb6 100644 --- a/dashboard/src/lib/docker.ts +++ b/dashboard/src/lib/docker.ts @@ -1499,11 +1499,33 @@ export async function getComfyQueue(): Promise<{ } export async function addComfyModel( - url: string, + rawUrl: string, type: string -): Promise<{ ok: boolean; output: string; error?: string }> { +): Promise<{ + ok: boolean; + output: string; + error?: string; + originalUrl?: string; + transformedUrl?: string; + urlNote?: string; +}> { + // Apply CivitAI URL transformation (model page → API download URL, append token). + // We lazy-load the imports here so docker.ts doesn't pull in downloads.ts at + // module-load time (avoids circular import concerns). + const { prepareCivitaiUrl, loadSettings } = await import("./downloads"); + const settings = await loadSettings(); + const { url, note } = prepareCivitaiUrl(rawUrl, settings.civitaiToken || ""); + const res = await modelWorkerExec("add", url, type); - return { ok: res.ok, output: res.output, error: res.error }; + return { + ok: res.ok, + output: res.output, + error: res.error, + // Only include transformation metadata if we actually changed something + originalUrl: rawUrl !== url ? rawUrl : undefined, + transformedUrl: rawUrl !== url ? url : undefined, + urlNote: note || undefined, + }; } export async function removeComfyModel(