feat(comfy): CivitAI URL transformation + token support for comfy models #13
Reference in New Issue
Block a user
Delete Branch "feat/civitai-for-comfy"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Что добавил
Проблема
'цивитай в comfy models нужен скорее' — CivitAI URL transformation (model page → API download URL, append token) применялся только к llama model downloads через
/api/models/download. ComfyUI model downloads через/api/comfy/models/addпросто передавали raw URL в model-worker — CivitAI model page URLs скачивали HTML вместо файла, а gated/NSFW модели падали с 401 без token.Фикс
Backend
dashboard/src/lib/docker.ts— addComfyModel()Теперь применяет тот же
prepareCivitaiUrl()чтоstartModelDownload():prepareCivitaiUrl(rawUrl, token):civitai.com/models/12345→civitai.com/api/download/models/12345civitai.red/...→ normalize host на civitai.com?token=XXXесли нетmodelWorkerExec('add', url, type)originalUrl,transformedUrl,urlNoteв responseИспользует lazy
await import('./downloads')чтобы избежать circular import между docker.ts (экспортирует paths) и downloads.ts (импортирует их).dashboard/src/app/api/comfy/models/add/route.tsВозвращает transformation metadata (
originalUrl,transformedUrl,urlNote) в JSON response.Frontend
dashboard/src/app/page.tsxcomfyUrlPreview— отдельный отurlPreview(который для llama Models tab) чтобы переключение вкладок их не смешивало.comfyAddUrlи вызываетPOST /api/settings {action:'preview-url'}для получения transformed URL.handleComfyAddModel()теперь читаетurlNoteиtransformedUrlиз response и включает их в toast message (multi-line) — юзер видит что произошло с его pasted URL.<ComfyTab>теперь принимаетcivitaiToken,onSaveCivitaiToken,urlPreviewprops.End-to-end flow
gpu/dashboard/settings.json, shared с llama downloader)https://civitai.com/models/12345в URL fieldaddComfyModel()трансформирует URL → вызываетdocker exec model-worker python3 /app/worker.py add <transformed-url> <type>gpu/comfy/models/<type>/<filename>Verification
npm run build— OK, 26 routesbash dashboard/validate-build.sh— все 8 COPY источников ✓, standalone ✓После мержа
Открой Comfy tab → 'CivitAI token' кнопка → вставь token → Save.
Вставь любой CivitAI URL (model page или API download URL) → увидь preview → Queue. Модель скачается с token auth и попадёт в нужный type subdirectory.
Files changed
dashboard/src/lib/docker.ts— addComfyModel() применяет prepareCivitaiUrldashboard/src/app/api/comfy/models/add/route.ts— возвращает transformation metadatadashboard/src/app/page.tsx— comfyUrlPreview state, debounced preview effect, handleComfyAddModel показывает transformation в toast, ComfyTab получает civitai token UI + URL preview + Enter-to-queue## Problem 'цивитай в comfy models нужен скорее' — the CivitAI URL transformation (model page → API download URL, token append) was only applied to llama model downloads via `/api/models/download`. ComfyUI model downloads via `/api/comfy/models/add` just passed the raw URL straight to model-worker, so CivitAI model page URLs would download HTML instead of the actual file, and gated/NSFW models would 401 without a token. ## Fix ### Backend #### `dashboard/src/lib/docker.ts` — addComfyModel() Now applies the same `prepareCivitaiUrl()` transformation as `startModelDownload()`: 1. Loads settings (to get the saved civitaiToken) 2. Calls `prepareCivitaiUrl(rawUrl, token)`: - civitai.com/models/12345 → civitai.com/api/download/models/12345 - civitai.red/... → normalize host to civitai.com - append ?token=XXX if missing 3. Passes the transformed URL to `modelWorkerExec('add', url, type)` 4. Returns `originalUrl`, `transformedUrl`, `urlNote` in the response so the frontend can show what happened Uses lazy `await import('./downloads')` to avoid a circular import between docker.ts (which exports paths) and downloads.ts (which imports them). #### `dashboard/src/app/api/comfy/models/add/route.ts` Returns the transformation metadata (`originalUrl`, `transformedUrl`, `urlNote`) in the JSON response. ### Frontend #### `dashboard/src/app/page.tsx` - New state: `comfyUrlPreview` — separate from `urlPreview` (which is for the llama Models tab) so switching tabs doesn't mix them up. - New debounced effect: watches `comfyAddUrl` and calls `POST /api/settings {action:'preview-url'}` to get the transformed URL. - `handleComfyAddModel()` now reads `urlNote` and `transformedUrl` from the response and includes them in the toast message (multi-line) so the user sees what happened to their pasted URL. - `<ComfyTab>` now accepts `civitaiToken`, `onSaveCivitaiToken`, and `urlPreview` props. - ComfyTab 'Queue Download' section: - 'CivitAI token' button (with badge 'set' / 'empty') in the header — opens a settings panel with password input, Save/Clear buttons, and a link to https://civitai.com/user/account. - The settings panel notes that the token is shared with the llama-models downloader (same settings.json). - URL input placeholder updated to show CivitAI URLs are accepted. - Enter key in the URL field queues the download (no need to click Queue button). - URL preview card appears below the input when the URL will be transformed (e.g. civitai.com/models/12345 → civitai.com/api/download/models/12345?token=...), showing the transformed URL + note. ## How it works end-to-end 1. User opens Comfy tab → 'CivitAI token' button → pastes token → Save (stored in `gpu/dashboard/settings.json`, shared with llama downloader) 2. User pastes `https://civitai.com/models/12345` in URL field 3. URL preview card shows: ``` [CivitAI] converted model page → API download URL, added API token https://civitai.com/api/download/models/12345?token=XXX... ``` 4. User selects type (checkpoints/loras/etc) and clicks Queue (or Enter) 5. Backend `addComfyModel()` transforms URL → calls `docker exec model-worker python3 /app/worker.py add <transformed-url> <type>` 6. Worker downloads the file with token auth → saves to `gpu/comfy/models/<type>/<filename>` 7. Toast shows: 'Queued: checkpoints\nconverted model page → API download URL, added API token\n→ https://civitai.com/api/download/models/...' ## Verification - `npm run build` — OK, 26 routes - `bash dashboard/validate-build.sh` — all 8 COPY sources ✓, standalone ✓ ## After deploy ```sh git pull && sh build.sh docker compose up -d --force-recreate dashboard ``` Open Comfy tab → 'CivitAI token' button → paste token → Save. Paste any CivitAI URL (model page or API download URL) → see preview → Queue. The model fetches with token auth and lands in the right type subdirectory. ## Files changed - dashboard/src/lib/docker.ts — addComfyModel() applies prepareCivitaiUrl - dashboard/src/app/api/comfy/models/add/route.ts — returns transformation metadata - dashboard/src/app/page.tsx — comfyUrlPreview state, debounced preview effect, handleComfyAddModel shows transformation in toast, ComfyTab gets civitai token UI + URL preview + Enter-to-queue