feat(comfy): CivitAI URL transformation + token support for comfy models #13
@@ -7,7 +7,17 @@ export const runtime = "nodejs";
|
|||||||
/**
|
/**
|
||||||
* POST /api/comfy/models/add
|
* POST /api/comfy/models/add
|
||||||
* Body: { url: string, type: string }
|
* 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) {
|
export async function POST(req: NextRequest) {
|
||||||
const body = (await req.json()) as { url?: string; type?: string };
|
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);
|
const result = await addComfyModel(body.url, body.type);
|
||||||
return NextResponse.json(
|
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 }
|
{ status: result.ok ? 200 : 500 }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -288,6 +288,9 @@ export default function DashboardPage() {
|
|||||||
civitaiToken: "",
|
civitaiToken: "",
|
||||||
});
|
});
|
||||||
const [urlPreview, setUrlPreview] = React.useState<UrlPreview | null>(null);
|
const [urlPreview, setUrlPreview] = React.useState<UrlPreview | null>(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<UrlPreview | null>(null);
|
||||||
|
|
||||||
// Chat state — kept at top level so switching tabs doesn't reset the
|
// Chat state — kept at top level so switching tabs doesn't reset the
|
||||||
// conversation. ChatTab is mounted/unmounted when the user clicks the tab.
|
// conversation. ChatTab is mounted/unmounted when the user clicks the tab.
|
||||||
@@ -896,6 +899,45 @@ export default function DashboardPage() {
|
|||||||
};
|
};
|
||||||
}, [downloadUrl, previewUrl]);
|
}, [downloadUrl, previewUrl]);
|
||||||
|
|
||||||
|
// Same debounced preview, but for the Comfy tab's URL field
|
||||||
|
const comfyPreviewDebounceRef = React.useRef<ReturnType<typeof setTimeout> | 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(() => {
|
React.useEffect(() => {
|
||||||
refreshSettings();
|
refreshSettings();
|
||||||
}, [refreshSettings]);
|
}, [refreshSettings]);
|
||||||
@@ -1206,10 +1248,22 @@ export default function DashboardPage() {
|
|||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
setComfyAddUrl("");
|
setComfyAddUrl("");
|
||||||
await refreshComfyQueue();
|
await refreshComfyQueue();
|
||||||
return {
|
// Build a helpful message — if the URL was transformed (CivitAI
|
||||||
ok: data.ok,
|
// model page → API download URL with token), include the note so the
|
||||||
message: data.ok ? `Queued: ${comfyAddType}` : data.error || "Failed",
|
// 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);
|
}, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1457,6 +1511,9 @@ export default function DashboardPage() {
|
|||||||
setAddType={setComfyAddType}
|
setAddType={setComfyAddType}
|
||||||
onRemove={handleComfyRemoveModel}
|
onRemove={handleComfyRemoveModel}
|
||||||
busy={busy}
|
busy={busy}
|
||||||
|
civitaiToken={settings.civitaiToken}
|
||||||
|
onSaveCivitaiToken={handleSaveCivitaiToken}
|
||||||
|
urlPreview={comfyUrlPreview}
|
||||||
/>
|
/>
|
||||||
</TabsContent>
|
</TabsContent>
|
||||||
|
|
||||||
@@ -2747,6 +2804,9 @@ function ComfyTab({
|
|||||||
setAddType,
|
setAddType,
|
||||||
onRemove,
|
onRemove,
|
||||||
busy,
|
busy,
|
||||||
|
civitaiToken,
|
||||||
|
onSaveCivitaiToken,
|
||||||
|
urlPreview,
|
||||||
}: {
|
}: {
|
||||||
models: ComfyModelEntry[];
|
models: ComfyModelEntry[];
|
||||||
queue: ComfyQueueEntry[];
|
queue: ComfyQueueEntry[];
|
||||||
@@ -2763,7 +2823,17 @@ function ComfyTab({
|
|||||||
setAddType: (v: string) => void;
|
setAddType: (v: string) => void;
|
||||||
onRemove: (filename: string, type?: string) => void;
|
onRemove: (filename: string, type?: string) => void;
|
||||||
busy: string | null;
|
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 (
|
return (
|
||||||
<div className="space-y-4">
|
<div className="space-y-4">
|
||||||
{/* Download queue */}
|
{/* Download queue */}
|
||||||
@@ -2848,22 +2918,94 @@ function ComfyTab({
|
|||||||
|
|
||||||
{/* Add model */}
|
{/* Add model */}
|
||||||
<Card>
|
<Card>
|
||||||
<CardHeader>
|
<CardHeader className="flex flex-row items-start justify-between gap-2">
|
||||||
<CardTitle className="text-base flex items-center gap-2">
|
<div>
|
||||||
<Plus className="h-4 w-4" />
|
<CardTitle className="text-base flex items-center gap-2">
|
||||||
Queue Download
|
<Plus className="h-4 w-4" />
|
||||||
</CardTitle>
|
Queue Download
|
||||||
<CardDescription>
|
</CardTitle>
|
||||||
Mirrors <code className="font-mono text-xs">gpuctl comfy models add <URL> <type></code>
|
<CardDescription>
|
||||||
</CardDescription>
|
Mirrors <code className="font-mono text-xs">gpuctl comfy models add <URL> <type></code>.
|
||||||
|
{" "}CivitAI URLs auto-converted (model page → API download),
|
||||||
|
token appended.
|
||||||
|
</CardDescription>
|
||||||
|
</div>
|
||||||
|
<Button
|
||||||
|
size="sm"
|
||||||
|
variant="outline"
|
||||||
|
onClick={() => setShowCivitaiSettings((v) => !v)}
|
||||||
|
>
|
||||||
|
<KeyIcon className="h-3.5 w-3.5" />
|
||||||
|
CivitAI token
|
||||||
|
{civitaiToken ? (
|
||||||
|
<Badge variant="success" className="ml-1 text-[10px]">
|
||||||
|
set
|
||||||
|
</Badge>
|
||||||
|
) : (
|
||||||
|
<Badge variant="secondary" className="ml-1 text-[10px]">
|
||||||
|
empty
|
||||||
|
</Badge>
|
||||||
|
)}
|
||||||
|
</Button>
|
||||||
</CardHeader>
|
</CardHeader>
|
||||||
<CardContent>
|
<CardContent className="space-y-3">
|
||||||
|
{showCivitaiSettings && (
|
||||||
|
<div className="rounded-md border bg-card p-3 space-y-2">
|
||||||
|
<Label htmlFor="comfy-civitai-token">CivitAI API token</Label>
|
||||||
|
<Input
|
||||||
|
id="comfy-civitai-token"
|
||||||
|
type="password"
|
||||||
|
value={civitaiTokenDraft}
|
||||||
|
onChange={(e) => setCivitaiTokenDraft(e.target.value)}
|
||||||
|
placeholder="Get it from https://civitai.com/user/account → API Keys"
|
||||||
|
className="font-mono text-xs"
|
||||||
|
/>
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<Button
|
||||||
|
size="sm"
|
||||||
|
onClick={() => onSaveCivitaiToken(civitaiTokenDraft)}
|
||||||
|
disabled={busy === "save-settings"}
|
||||||
|
>
|
||||||
|
Save
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
size="sm"
|
||||||
|
variant="ghost"
|
||||||
|
onClick={() => {
|
||||||
|
setCivitaiTokenDraft("");
|
||||||
|
onSaveCivitaiToken("");
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
Clear
|
||||||
|
</Button>
|
||||||
|
<a
|
||||||
|
href="https://civitai.com/user/account"
|
||||||
|
target="_blank"
|
||||||
|
rel="noopener noreferrer"
|
||||||
|
className="ml-auto text-xs text-muted-foreground hover:underline"
|
||||||
|
>
|
||||||
|
Get token ↗
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
<p className="text-[10px] text-muted-foreground">
|
||||||
|
Shared with the llama-models downloader. Stored in{" "}
|
||||||
|
<code className="font-mono">gpu/dashboard/settings.json</code>.
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
<div className="grid gap-2 sm:grid-cols-[1fr_180px_auto]">
|
<div className="grid gap-2 sm:grid-cols-[1fr_180px_auto]">
|
||||||
<Input
|
<Input
|
||||||
placeholder="https://huggingface.co/.../model.safetensors"
|
placeholder="https://civitai.com/models/12345 or HF/GitHub URL"
|
||||||
value={addUrl}
|
value={addUrl}
|
||||||
onChange={(e) => setAddUrl(e.target.value)}
|
onChange={(e) => setAddUrl(e.target.value)}
|
||||||
className="font-mono text-xs"
|
className="font-mono text-xs"
|
||||||
|
onKeyDown={(e) => {
|
||||||
|
if (e.key === "Enter" && !e.shiftKey) {
|
||||||
|
e.preventDefault();
|
||||||
|
if (addUrl.trim() && busy === null) onAdd();
|
||||||
|
}
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
<Select value={addType} onValueChange={setAddType}>
|
<Select value={addType} onValueChange={setAddType}>
|
||||||
<SelectTrigger>
|
<SelectTrigger>
|
||||||
@@ -2883,11 +3025,29 @@ function ComfyTab({
|
|||||||
)}
|
)}
|
||||||
</SelectContent>
|
</SelectContent>
|
||||||
</Select>
|
</Select>
|
||||||
<Button onClick={onAdd} disabled={busy !== null}>
|
<Button onClick={onAdd} disabled={busy !== null || !addUrl.trim()}>
|
||||||
<Plus className="h-3.5 w-3.5" />
|
<Plus className="h-3.5 w-3.5" />
|
||||||
Queue
|
Queue
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{/* URL preview — shows what we'll actually send to the worker */}
|
||||||
|
{urlPreview && urlPreview.original !== urlPreview.transformed && (
|
||||||
|
<div className="rounded-md border bg-card p-2.5 text-xs space-y-1">
|
||||||
|
<div className="flex items-center gap-1.5">
|
||||||
|
<Badge
|
||||||
|
variant={urlPreview.isCivitai ? "default" : "secondary"}
|
||||||
|
className="text-[10px]"
|
||||||
|
>
|
||||||
|
{urlPreview.isCivitai ? "CivitAI" : "transformed"}
|
||||||
|
</Badge>
|
||||||
|
<span className="text-muted-foreground">{urlPreview.note}</span>
|
||||||
|
</div>
|
||||||
|
<pre className="font-mono text-[10px] text-foreground whitespace-pre-wrap break-all">
|
||||||
|
{urlPreview.transformed}
|
||||||
|
</pre>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
</CardContent>
|
</CardContent>
|
||||||
</Card>
|
</Card>
|
||||||
|
|
||||||
|
|||||||
@@ -1499,11 +1499,33 @@ export async function getComfyQueue(): Promise<{
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function addComfyModel(
|
export async function addComfyModel(
|
||||||
url: string,
|
rawUrl: string,
|
||||||
type: 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);
|
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(
|
export async function removeComfyModel(
|
||||||
|
|||||||
Reference in New Issue
Block a user