The open creative studio on ComfyUI: images, video, 3D and audio on open weights, on your own GPU or the cloud. Camera motions, effects, apps, Cinema, Layers, Image to 3D and an AI agent.
TypeScript ai-videocomfyuicreative-toolsgenerative-aiideogramimage-generationimage-to-3dimage-to-videoltx-videominimax-h3nextjsopen-sourceopen-weightsqwen-imagetext-to-imagevideo-generationwan
Worker: map a folder a pack lists under another name to the subfolder that holds its files
Adarsh7f9efb498528
diff --git a/bin/lib/worker/comfy.mjs b/bin/lib/worker/comfy.mjsindex 4e774f33..0165f10d 100644--- a/bin/lib/worker/comfy.mjs+++ b/bin/lib/worker/comfy.mjs@@ -76,20 +76,40 @@ export const folders = (workDir) => ({* reverse. A folder name with an older twin (text_encoders and clip, diffusion_models and unet: ComfyUI reads both as* one) gets both subfolders, the name's own first (twinFolders), so a file under clip/ or unet/ is found too. The worker* checks the copy that this search finds first (inventory.mjs).+ *+ * A folder name may come as [name, subfolder] (yamlFolders): a pack that lists models/<subfolder> under another name+ * (kandinsky6: models/audio_vae as kandinsky6_audio_vae) gets `name: subfolder`, the subfolder where the files are and+ * where the inventory checks them.*/export function modelPathsYaml(modelDirs, folderNames, o = {}) {const lines = [];const entries = modelDirs.map((dir, i) => [dir, i]);- // Each subfolder once, under its own name (ComfyUI maps an older name to the folder it stands for).- const subs = [...new Set(folderNames.flatMap((f) => twinFolders(f)))];+ // Each name once, with its subfolder (ComfyUI maps an older name to the folder it stands for).+ const pairs = new Map();+ for (const f of folderNames) {+ for (const [name, sub] of Array.isArray(f) ? [f] : twinFolders(f).map((t) => [t, t])) if (!pairs.has(name)) pairs.set(name, sub);+ }+ const subs = [...pairs];for (const [dir, i] of o.first ? entries.reverse() : entries) {lines.push(`fuobe_worker_${i}:`, ` base_path: ${JSON.stringify(dir)}`, ...(o.first ? [" is_default: true"] : []));// is_default puts each one at the front: the twin is written before the name's own, so the name's own comes first.- for (const f of o.first ? [...subs].reverse() : subs) lines.push(` ${f}: ${f}`);+ for (const [name, sub] of o.first ? [...subs].reverse() : subs) lines.push(` ${name}: ${sub}`);}return `${lines.join("\n")}\n`;}+/**+ * The catalog's folder names for modelPathsYaml (requirements.json `folders`): a name a pack lists another subfolder+ * under (a file's `listFolder`) comes as [name, the file's folder], every other name as itself.+ * @param {{ folders?: string[]; files?: Record<string, { folder: string; listFolder?: string }> }} index+ * @returns {(string | [string, string])[]}+ */+export function yamlFolders(index) {+ const subOf = new Map();+ for (const f of Object.values(index?.files ?? {})) if (f?.listFolder && f.listFolder !== f.folder && !subOf.has(f.listFolder)) subOf.set(f.listFolder, f.folder);+ return (index?.folders ?? []).map((name) => (subOf.has(name) ? [name, subOf.get(name)] : name));+}+/** This ComfyUI reads `is_default` in an extra_model_paths.yaml (an older one stops on it). */export function defaultPathsFlag(comfyDir) {for (const file of [path.join(comfyDir ?? "", "utils", "extra_config.py"), path.join(comfyDir ?? "", "main.py")]) {diff --git a/bin/lib/worker/run.mjs b/bin/lib/worker/run.mjsindex daeb47a4..eca87e5a 100644--- a/bin/lib/worker/run.mjs+++ b/bin/lib/worker/run.mjs@@ -26,7 +26,7 @@ import path from "node:path";import { fileURLToPath } from "node:url";import { StayAwake } from "./awake.mjs";import { allowedNodes, cleanJob, emptyFolders, graphCheck, inputRules } from "./checks.mjs";-import { folders, GuardMissing, OwnComfy, PortBusy } from "./comfy.mjs";+import { folders, GuardMissing, OwnComfy, PortBusy, yamlFolders } from "./comfy.mjs";import { ffmpegOf, PROTOCOL, protocolOf, readControl, saveConfig, savedConfig, writeControl, writeStatus } from "./config.mjs";import { BRIEF_COOL_DOWN_MS, BRIEF_EVERY_MS, BRIEF_PAUSE_MS, GpuWatch, MID_JOB_GRACE_MS, othersOf, readGpu, readGpuGen, readGpuTotal } from "./gpu.mjs";import { Hold } from "./hold.mjs";@@ -252,7 +252,7 @@ async function work(saved, deps, owner) {let gpuGen = null;let studioCatalog = null;log(`This PC runs ${ceilingWords(ceiling)}${cfg.acceptGraphs === false ? ", from job tickets only" : ""}.`);- const comfy = new OwnComfy(cfg, { now, fetch: fetchFn, folderNames: index.folders ?? [], log, owner, workerLives: (rec) => workerLives(rec, { commandLineOf: deps.comfy?.commandLineOf }), ...deps.comfy });+ const comfy = new OwnComfy(cfg, { now, fetch: fetchFn, folderNames: yamlFolders(index), log, owner, workerLives: (rec) => workerLives(rec, { commandLineOf: deps.comfy?.commandLineOf }), ...deps.comfy });// Only its own ComfyUI, and only while the process it started is up and ready: never whatever listens on that port.// Its results come without what a save node writes into them unasked (results.mjs).const ownFetch = withPlainResults((url, init) => comfy.fetch(url, init));@@ -568,7 +568,7 @@ async function work(saved, deps, owner) {const cpuComfy = cpuOn? new OwnComfy({ ...cfg, workDir: cpuDir, port: null, cpuOnly: true, ...(cfg.cpuComfyCommand && { comfyCommand: cfg.cpuComfyCommand }) },- { now, fetch: fetchFn, folderNames: index.folders ?? [], log, owner, workerLives: (rec) => workerLives(rec, { commandLineOf: deps.comfy?.commandLineOf }), ...deps.comfy },+ { now, fetch: fetchFn, folderNames: yamlFolders(index), log, owner, workerLives: (rec) => workerLives(rec, { commandLineOf: deps.comfy?.commandLineOf }), ...deps.comfy },): null;const cpuHold = cpuOn ? new Hold(path.join(cfg.workDir, "hold-cpu")) : null;diff --git a/tests/worker-environment.test.ts b/tests/worker-environment.test.tsindex 12ac1956..a2cfd29e 100644--- a/tests/worker-environment.test.ts+++ b/tests/worker-environment.test.ts@@ -4,7 +4,7 @@ import net from "node:net";import path from "node:path";import { describe, expect, it } from "vitest";import { inputRules } from "../bin/lib/worker/checks.mjs";-import { comfyArgs, comfyEnv, defaultPathsFlag, modelPathsYaml, OFFLINE_ENV, OwnComfy } from "../bin/lib/worker/comfy.mjs";+import { comfyArgs, comfyEnv, defaultPathsFlag, modelPathsYaml, OFFLINE_ENV, OwnComfy, yamlFolders } from "../bin/lib/worker/comfy.mjs";import { modelFiles, nodePacks, packFolders, twinFolders, yamlModelDirs } from "../bin/lib/worker/inventory.mjs";import { minVramBytesFor, needsOf, VRAM_TOLERANCE } from "@/lib/lanes";import type { RequirementIndex } from "@/lib/gpu/types";@@ -245,6 +245,24 @@ describe("the worker's model files", () => {expect(older.text_encoders).toEqual(["<own>/text_encoders", "<own>/clip", "D:/a/text_encoders", "D:/a/clip", "E:/b/text_encoders", "E:/b/clip"]);expect(older.diffusion_models.slice(2)).toEqual(["D:/a/diffusion_models", "D:/a/unet", "E:/b/diffusion_models", "E:/b/unet"]);});++ it("maps a folder a pack lists under another name to the subfolder where its files are (kandinsky6_audio_vae: audio_vae)", () => {+ const index = JSON.parse(fs.readFileSync(path.join(process.cwd(), "workflows", "comfyui", "requirements.json"), "utf8")) as RequirementIndex;+ const names = yamlFolders(index);+ expect(names).toContainEqual(["kandinsky6_audio_vae", "audio_vae"]);+ expect(names).toContain("loras");+ expect(names.length).toBe(index.folders.length);+ const yaml = modelPathsYaml(["X:/models"], names, { first: true });+ expect(yaml).toContain("\n kandinsky6_audio_vae: audio_vae\n");+ expect(yaml).not.toContain("kandinsky6_audio_vae: kandinsky6_audio_vae");+ expect(yaml).toContain("\n clip: clip\n"); // the twins are still there+ // Where ComfyUI reads the pack's files through this yaml is where the inventory checks them (models/<folder>/<file>).+ const d = yamlModelDirs(yaml, "/");+ expect(d.kandinsky6_audio_vae).toEqual([path.resolve("X:/models/audio_vae")]);+ for (const f of Object.values(index.files).filter((x) => x.listFolder)) expect(path.resolve("X:/models", f.folder)).toBe(d[f.listFolder!][0]);+ // A plain list of names (an older caller) still writes name: name.+ expect(modelPathsYaml(["X:/models"], ["kandinsky6_audio_vae"])).toContain(" kandinsky6_audio_vae: kandinsky6_audio_vae\n");+ });});describe("its own ComfyUI's environment", () => {