comfy-ts 2.5.0 → 2.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -1
- package/dist/{DefinedWorkflow-CnzmQrBh.js → DefinedWorkflow-BJgt5t0x.js} +14 -3
- package/dist/DefinedWorkflow-BJgt5t0x.js.map +1 -0
- package/dist/{DraftsSt-C1MbWnEp.js → DraftsSt-dizp3VcC.js} +2 -2
- package/dist/{DraftsSt-C1MbWnEp.js.map → DraftsSt-dizp3VcC.js.map} +1 -1
- package/dist/cli.js +4 -4
- package/dist/index.cjs +3 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +21 -18
- package/dist/index.d.ts +21 -18
- package/dist/index.js +3 -2
- package/dist/index.js.map +1 -1
- package/dist/{run-serve-D47Zas-a.js → run-serve-CVu39RNd.js} +330 -16
- package/dist/run-serve-CVu39RNd.js.map +1 -0
- package/dist/{run-tui-t2TRBN0H.js → run-tui-idFhdO4f.js} +704 -524
- package/dist/run-tui-idFhdO4f.js.map +1 -0
- package/dist/serve-web.js +280 -0
- package/dist/{state-DY3Mp9Cl.js → state-C90kdjtB.js} +3 -13
- package/dist/state-C90kdjtB.js.map +1 -0
- package/dist/web.d.ts +21 -18
- package/dist/web.js +3 -2
- package/dist/web.js.map +1 -1
- package/package.json +7 -5
- package/src/cli/serve/ServeApp.ts +314 -13
- package/src/cli/serve/describeVar.ts +6 -1
- package/src/cli/serve/run-serve.ts +4 -2
- package/src/cli/serve/web/api.ts +105 -0
- package/src/cli/serve/web/clipboard.ts +25 -0
- package/src/cli/serve/web/components/App.tsx +53 -0
- package/src/cli/serve/web/components/Gallery.tsx +186 -0
- package/src/cli/serve/web/components/PromptEnhancer.tsx +210 -0
- package/src/cli/serve/web/components/Sidebar.tsx +42 -0
- package/src/cli/serve/web/components/VarsForm.tsx +173 -0
- package/src/cli/serve/web/components/controls/BasicControls.tsx +95 -0
- package/src/cli/serve/web/components/controls/ImageControl.tsx +71 -0
- package/src/cli/serve/web/components/controls/LorasControl.tsx +340 -0
- package/src/cli/serve/web/components/controls/SeedControl.tsx +43 -0
- package/src/cli/serve/web/components/controls/SizeControl.tsx +49 -0
- package/src/cli/serve/web/main.tsx +18 -0
- package/src/cli/serve/web/openrouter.ts +142 -0
- package/src/cli/serve/web/state/EnhancerSt.ts +370 -0
- package/src/cli/serve/web/state/FormSt.ts +177 -0
- package/src/cli/serve/web/state/RunSt.ts +136 -0
- package/src/cli/serve/web/state/WebSt.ts +210 -0
- package/src/cli/serve/web/state/payload.ts +110 -0
- package/src/cli/serve/web/styles.ts +259 -0
- package/src/cli/serve/web/tsconfig.json +15 -0
- package/src/cli/serve/webBundle.ts +66 -0
- package/src/cli/tui/imageEscapes.ts +105 -0
- package/src/cli/tui/imagePicker/ImagePickerSt.ts +21 -8
- package/src/cli/tui/protocolImage.ts +39 -0
- package/src/cli/tui/protocolImagePainter.ts +56 -23
- package/src/cli/tui/run-tui.tsx +3 -2
- package/src/cli/tui/state/LorasSt.ts +22 -9
- package/src/cli/tui/state/PreviewSt.ts +31 -13
- package/src/cli/tui/state/SettingsSt.ts +1 -1
- package/src/vars/ComfyVars.ts +4 -1
- package/dist/DefinedWorkflow-CnzmQrBh.js.map +0 -1
- package/dist/run-serve-D47Zas-a.js.map +0 -1
- package/dist/run-tui-t2TRBN0H.js.map +0 -1
- package/dist/state-DY3Mp9Cl.js.map +0 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { C as extractErrorMessage } from "./DefinedWorkflow-
|
|
2
|
-
import { c as findDefinedWorkflow, i as listDraftsForFile, n as draftKeyForFile, r as draftsDirForFile, u as scanCflowFiles } from "./DraftsSt-
|
|
3
|
-
import { pathToFileURL } from "node:url";
|
|
1
|
+
import { C as loraPreviewMapFrom, S as loraKey, T as bang, _ as fetchLoraList, d as getLoraPreviewUrl, f as getLoraTriggerWords, h as refreshLoraInfoCacheIfChanged, u as getLoraDisplayName, v as fetchLoraPreviewBytes, w as extractErrorMessage } from "./DefinedWorkflow-BJgt5t0x.js";
|
|
2
|
+
import { c as findDefinedWorkflow, i as listDraftsForFile, n as draftKeyForFile, r as draftsDirForFile, u as scanCflowFiles } from "./DraftsSt-dizp3VcC.js";
|
|
3
|
+
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
4
4
|
import { existsSync, mkdirSync, readFileSync, statSync, writeFileSync } from "node:fs";
|
|
5
5
|
import { basename, dirname, extname, join, resolve } from "pathe";
|
|
6
6
|
import { nanoid } from "nanoid";
|
|
@@ -48,7 +48,7 @@ function describeVar(varDef) {
|
|
|
48
48
|
return {
|
|
49
49
|
...base,
|
|
50
50
|
default: {
|
|
51
|
-
mode: v.
|
|
51
|
+
mode: v.defaultMode,
|
|
52
52
|
value: v.defaultValue
|
|
53
53
|
},
|
|
54
54
|
payload: "number (fixed) or {\"mode\":\"=|+|-|?\",\"value\":number}"
|
|
@@ -225,6 +225,14 @@ const CONTENT_TYPES = {
|
|
|
225
225
|
".json": "application/json",
|
|
226
226
|
".txt": "text/plain"
|
|
227
227
|
};
|
|
228
|
+
/** magic-byte sniff for the preview proxy — fetchLoraPreviewBytes already guarantees an image */
|
|
229
|
+
function sniffImageContentType(bytes) {
|
|
230
|
+
if (bytes[0] === 137 && bytes[1] === 80) return "image/png";
|
|
231
|
+
if (bytes[0] === 255 && bytes[1] === 216) return "image/jpeg";
|
|
232
|
+
if (bytes[0] === 71 && bytes[1] === 73) return "image/gif";
|
|
233
|
+
if (bytes[0] === 82 && bytes[1] === 73) return "image/webp";
|
|
234
|
+
return "application/octet-stream";
|
|
235
|
+
}
|
|
228
236
|
function json(status, payload) {
|
|
229
237
|
return {
|
|
230
238
|
status,
|
|
@@ -233,18 +241,46 @@ function json(status, payload) {
|
|
|
233
241
|
};
|
|
234
242
|
}
|
|
235
243
|
const USAGE = [
|
|
244
|
+
"GET / in a browser — web control panel (every var as a form control)",
|
|
236
245
|
"GET /drafts — every served workflow, its drafts and var descriptors",
|
|
237
246
|
"GET /drafts/<module>/<draft> — one draft with its stored values",
|
|
238
247
|
"POST /generate/<module>/<draft> with { ...vars } — run, blocking",
|
|
239
248
|
"POST /generate/<draft> — unqualified, when unambiguous",
|
|
249
|
+
"PUT /drafts/<module>/<draft> with { ...vars } — save (or duplicate to a new name) a draft",
|
|
250
|
+
"GET /run/<module> — live run status · /run/<module>/preview — latent preview bytes",
|
|
251
|
+
"POST /upload with {\"name\",\"dataBase64\"} — store a browser file for an image var",
|
|
252
|
+
"GET /lora-info/<hostId>/<lora> — display name + trigger words (local mirror)",
|
|
253
|
+
"GET /lora-preview/<hostId>/<lora> — preview image bytes",
|
|
240
254
|
"GET /outputs/<path> — generated files"
|
|
241
255
|
];
|
|
256
|
+
/** the html shell; the app itself is ONE bundle at /web/app.js */
|
|
257
|
+
const WEB_SHELL = `<!doctype html>
|
|
258
|
+
<html lang="en">
|
|
259
|
+
<head>
|
|
260
|
+
<meta charset="utf-8">
|
|
261
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
262
|
+
<title>comfy-ts serve</title>
|
|
263
|
+
<style>html{background:#101217;color:#e8eaf0}</style>
|
|
264
|
+
</head>
|
|
265
|
+
<body>
|
|
266
|
+
<div id="root"></div>
|
|
267
|
+
<script type="module" src="/web/app.js"><\/script>
|
|
268
|
+
</body>
|
|
269
|
+
</html>
|
|
270
|
+
`;
|
|
242
271
|
var ServeApp = class {
|
|
243
272
|
constructor(modules, opts = {}) {
|
|
244
273
|
this.modules = modules;
|
|
245
274
|
this.opts = opts;
|
|
246
275
|
this.seedState = /* @__PURE__ */ new Map();
|
|
247
276
|
this.chains = /* @__PURE__ */ new Map();
|
|
277
|
+
this.webJsCache = null;
|
|
278
|
+
this.liveRuns = /* @__PURE__ */ new Map();
|
|
279
|
+
this.latentPreviews = /* @__PURE__ */ new Map();
|
|
280
|
+
this.latentSeq = 0;
|
|
281
|
+
this.promptOwner = /* @__PURE__ */ new Map();
|
|
282
|
+
this.latentHosts = /* @__PURE__ */ new Set();
|
|
283
|
+
this.previewSweeps = /* @__PURE__ */ new Map();
|
|
248
284
|
this.starter = opts.starter ?? realStarter;
|
|
249
285
|
}
|
|
250
286
|
get outputRoot() {
|
|
@@ -260,8 +296,21 @@ var ServeApp = class {
|
|
|
260
296
|
return json(400, { error: `bad url encoding: ${path}` });
|
|
261
297
|
}
|
|
262
298
|
if (req.method === "GET") {
|
|
299
|
+
if (segs.length === 0 && this.opts.webJs != null && req.accept?.includes("text/html") === true) {
|
|
300
|
+
this.webJsCache ??= this.opts.webJs();
|
|
301
|
+
if (await this.webJsCache != null) return {
|
|
302
|
+
status: 200,
|
|
303
|
+
contentType: "text/html; charset=utf-8",
|
|
304
|
+
body: WEB_SHELL
|
|
305
|
+
};
|
|
306
|
+
}
|
|
263
307
|
if (segs.length === 0 || segs[0] === "drafts" && segs.length === 1) return this.replyIndex();
|
|
308
|
+
if (segs[0] === "web" && segs[1] === "app.js" && segs.length === 2) return await this.replyWebJs();
|
|
264
309
|
if (segs[0] === "drafts" && segs.length === 3 && segs[1] != null && segs[2] != null) return this.replyDraft(segs[1], segs[2]);
|
|
310
|
+
if (segs[0] === "run" && segs.length === 2 && segs[1] != null) return this.replyRunStatus(segs[1]);
|
|
311
|
+
if (segs[0] === "run" && segs.length === 3 && segs[1] != null && segs[2] === "preview") return this.replyRunPreview(segs[1]);
|
|
312
|
+
if (segs[0] === "lora-info" && segs.length === 3 && segs[1] != null && segs[2] != null) return this.replyLoraInfo(segs[1], segs[2]);
|
|
313
|
+
if (segs[0] === "lora-preview" && segs.length === 3 && segs[1] != null && segs[2] != null) return await this.replyLoraPreview(segs[1], segs[2]);
|
|
265
314
|
if (segs[0] === "outputs") return this.replyOutput(segs.slice(1));
|
|
266
315
|
return json(404, {
|
|
267
316
|
error: `no route: GET ${path}`,
|
|
@@ -273,6 +322,8 @@ var ServeApp = class {
|
|
|
273
322
|
if ("error" in target) return json(target.status, { error: target.error });
|
|
274
323
|
return await this.generate(target.mod, target.draft, req);
|
|
275
324
|
}
|
|
325
|
+
if (req.method === "POST" && segs[0] === "upload" && segs.length === 1) return this.replyUpload(req);
|
|
326
|
+
if (req.method === "PUT" && segs[0] === "drafts" && segs.length === 3 && segs[1] != null && segs[2] != null) return await this.replySaveDraft(segs[1], segs[2], req);
|
|
276
327
|
return json(404, {
|
|
277
328
|
error: `no route: ${req.method} ${path}`,
|
|
278
329
|
usage: USAGE
|
|
@@ -289,8 +340,23 @@ var ServeApp = class {
|
|
|
289
340
|
draftsFor(mod) {
|
|
290
341
|
return ["default", ...listDraftsForFile(mod.file).filter((n) => n !== "default")];
|
|
291
342
|
}
|
|
343
|
+
/** THE draft-name gate, shared by every route that turns a url segment into a path.
|
|
344
|
+
* handle() decodes per segment, so `%2F` reaches here as a real `/` while the segment
|
|
345
|
+
* count still looks right — an unguarded reader would read (and RUN) any json on disk */
|
|
346
|
+
validDraftName(raw) {
|
|
347
|
+
const name = raw.trim();
|
|
348
|
+
if (name.length > 100) return null;
|
|
349
|
+
return /^[\w][\w .-]*$/.test(name) ? name : null;
|
|
350
|
+
}
|
|
351
|
+
/** absolute path of a draft file, or null when the name is not one we accept */
|
|
352
|
+
draftPath(mod, draft) {
|
|
353
|
+
const name = this.validDraftName(draft);
|
|
354
|
+
return name == null ? null : join(draftsDirForFile(mod.file), `${name}.json`);
|
|
355
|
+
}
|
|
292
356
|
draftExists(mod, draft) {
|
|
293
|
-
|
|
357
|
+
const path = this.draftPath(mod, draft);
|
|
358
|
+
if (path == null) return false;
|
|
359
|
+
return draft === "default" || existsSync(path);
|
|
294
360
|
}
|
|
295
361
|
resolveDraft(segs) {
|
|
296
362
|
if (segs.length === 2 && segs[0] != null && segs[1] != null) {
|
|
@@ -341,8 +407,21 @@ var ServeApp = class {
|
|
|
341
407
|
};
|
|
342
408
|
}
|
|
343
409
|
describeModule(mod) {
|
|
410
|
+
refreshLoraInfoCacheIfChanged();
|
|
411
|
+
const hostId = mod.dw.host.data.id;
|
|
344
412
|
const vars = {};
|
|
345
|
-
for (const [name, varDef] of mod.dw.entries())
|
|
413
|
+
for (const [name, varDef] of mod.dw.entries()) {
|
|
414
|
+
const desc = describeVar(varDef);
|
|
415
|
+
if (desc.kind === "loras" && desc.options != null) {
|
|
416
|
+
const labels = {};
|
|
417
|
+
for (const option of desc.options) {
|
|
418
|
+
const label = getLoraDisplayName(option, hostId);
|
|
419
|
+
if (label !== option) labels[option] = label;
|
|
420
|
+
}
|
|
421
|
+
if (Object.keys(labels).length > 0) desc.optionLabels = labels;
|
|
422
|
+
}
|
|
423
|
+
vars[name] = desc;
|
|
424
|
+
}
|
|
346
425
|
return {
|
|
347
426
|
module: mod.key,
|
|
348
427
|
file: mod.file,
|
|
@@ -372,8 +451,8 @@ var ServeApp = class {
|
|
|
372
451
|
}
|
|
373
452
|
/** stored values (toJSON shapes); 'default' without a file falls back to descriptor defaults */
|
|
374
453
|
draftValues(mod, draft) {
|
|
375
|
-
const path =
|
|
376
|
-
if (existsSync(path)) return JSON.parse(readFileSync(path, "utf8"));
|
|
454
|
+
const path = this.draftPath(mod, draft);
|
|
455
|
+
if (path != null && existsSync(path)) return JSON.parse(readFileSync(path, "utf8"));
|
|
377
456
|
const out = {};
|
|
378
457
|
for (const [name, varDef] of mod.dw.entries()) out[name] = describeVar(varDef).default;
|
|
379
458
|
return out;
|
|
@@ -388,6 +467,165 @@ var ServeApp = class {
|
|
|
388
467
|
body: new Uint8Array(readFileSync(abs))
|
|
389
468
|
};
|
|
390
469
|
}
|
|
470
|
+
async replyWebJs() {
|
|
471
|
+
if (this.opts.webJs == null) return json(404, { error: "web ui not enabled on this server" });
|
|
472
|
+
this.webJsCache ??= this.opts.webJs();
|
|
473
|
+
const js = await this.webJsCache;
|
|
474
|
+
if (js == null) return json(404, { error: "web ui bundle unavailable — see the server log" });
|
|
475
|
+
return {
|
|
476
|
+
status: 200,
|
|
477
|
+
contentType: "text/javascript; charset=utf-8",
|
|
478
|
+
body: js
|
|
479
|
+
};
|
|
480
|
+
}
|
|
481
|
+
/** browser file → local file an image var can point at (downloadInput family) */
|
|
482
|
+
replyUpload(req) {
|
|
483
|
+
let parsed;
|
|
484
|
+
try {
|
|
485
|
+
parsed = JSON.parse(req.body ?? "");
|
|
486
|
+
} catch (e) {
|
|
487
|
+
return json(400, { error: `body is not valid json: ${extractErrorMessage(e)}` });
|
|
488
|
+
}
|
|
489
|
+
const o = parsed ?? {};
|
|
490
|
+
if (typeof o.name !== "string" || o.name === "" || typeof o.dataBase64 !== "string" || o.dataBase64 === "") return json(400, { error: "upload expects {\"name\":\"<filename>\",\"dataBase64\":\"<base64 bytes>\"}" });
|
|
491
|
+
const bytes = Buffer.from(o.dataBase64, "base64");
|
|
492
|
+
if (bytes.length === 0) return json(400, { error: "dataBase64 decoded to zero bytes" });
|
|
493
|
+
const safe = basename(o.name).replace(/[^\w.-]+/g, "_") || "upload";
|
|
494
|
+
const abs = join(this.outputRoot, "serve-inputs", `${nanoid(6)}-${safe}`);
|
|
495
|
+
mkdirSync(dirname(abs), { recursive: true });
|
|
496
|
+
writeFileSync(abs, bytes);
|
|
497
|
+
console.log(`[serve] upload → ${abs} (${bytes.length} bytes)`);
|
|
498
|
+
return json(200, {
|
|
499
|
+
ok: true,
|
|
500
|
+
path: abs,
|
|
501
|
+
url: this.outputUrl(abs)
|
|
502
|
+
});
|
|
503
|
+
}
|
|
504
|
+
async replySaveDraft(modKey, rawDraft, req) {
|
|
505
|
+
const mod = this.moduleByKey(modKey);
|
|
506
|
+
if (mod == null) return json(404, { error: `unknown module '${modKey}'` });
|
|
507
|
+
const draft = this.validDraftName(rawDraft);
|
|
508
|
+
if (draft == null) return json(400, { error: `invalid draft name '${rawDraft}' — letters/digits then letters, digits, ". -_"` });
|
|
509
|
+
let parsed;
|
|
510
|
+
try {
|
|
511
|
+
parsed = JSON.parse(req.body ?? "");
|
|
512
|
+
} catch (e) {
|
|
513
|
+
return json(400, { error: `body is not valid json: ${extractErrorMessage(e)}` });
|
|
514
|
+
}
|
|
515
|
+
if (parsed == null || typeof parsed !== "object" || Array.isArray(parsed)) return json(400, { error: "body must be a json object: { \"<var>\": value, … }" });
|
|
516
|
+
const values = parsed;
|
|
517
|
+
const known = new Set(mod.dw.entries().map(([k]) => k));
|
|
518
|
+
const unknown = Object.keys(values).filter((k) => !known.has(k));
|
|
519
|
+
if (unknown.length > 0) return json(400, { error: `unknown var(s) ${unknown.join(", ")} — vars: ${[...known].join(", ")}` });
|
|
520
|
+
const path = bang(this.draftPath(mod, draft), "draft name validated just above");
|
|
521
|
+
await this.exclusive(mod.key, () => {
|
|
522
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
523
|
+
writeFileSync(path, JSON.stringify(values, null, 2));
|
|
524
|
+
return Promise.resolve();
|
|
525
|
+
});
|
|
526
|
+
return json(200, {
|
|
527
|
+
ok: true,
|
|
528
|
+
module: mod.key,
|
|
529
|
+
draft,
|
|
530
|
+
drafts: this.draftsFor(mod)
|
|
531
|
+
});
|
|
532
|
+
}
|
|
533
|
+
runningModules() {
|
|
534
|
+
return [...this.liveRuns.entries()].filter(([, e]) => e.status !== "Success" && e.status !== "Failure").map(([key]) => key);
|
|
535
|
+
}
|
|
536
|
+
/** one dispatcher per host, installed once and left in place (idempotent) */
|
|
537
|
+
wireLatents(host) {
|
|
538
|
+
if (this.latentHosts.has(host)) return;
|
|
539
|
+
this.latentHosts.add(host);
|
|
540
|
+
host.onLatentPreview = (p) => {
|
|
541
|
+
const owner = p.promptID != null ? this.promptOwner.get(p.promptID) : void 0;
|
|
542
|
+
const running = this.runningModules();
|
|
543
|
+
const key = owner ?? (running.length === 1 ? running[0] : void 0);
|
|
544
|
+
if (key == null) return;
|
|
545
|
+
this.latentPreviews.set(key, {
|
|
546
|
+
bytes: p.bytes,
|
|
547
|
+
mime: p.mime,
|
|
548
|
+
seq: ++this.latentSeq
|
|
549
|
+
});
|
|
550
|
+
};
|
|
551
|
+
}
|
|
552
|
+
replyRunStatus(modKey) {
|
|
553
|
+
const mod = this.moduleByKey(modKey);
|
|
554
|
+
if (mod == null) return json(404, { error: `unknown module '${modKey}'` });
|
|
555
|
+
const exec = this.liveRuns.get(mod.key);
|
|
556
|
+
const running = exec != null && exec.status !== "Success" && exec.status !== "Failure";
|
|
557
|
+
return json(200, {
|
|
558
|
+
running,
|
|
559
|
+
status: exec?.status ?? "idle",
|
|
560
|
+
percent: running ? exec.progressGlobal?.percent ?? null : null,
|
|
561
|
+
hasPreview: this.latentPreviews.has(mod.key),
|
|
562
|
+
previewSeq: this.latentPreviews.get(mod.key)?.seq ?? null
|
|
563
|
+
});
|
|
564
|
+
}
|
|
565
|
+
replyRunPreview(modKey) {
|
|
566
|
+
const mod = this.moduleByKey(modKey);
|
|
567
|
+
if (mod == null) return json(404, { error: `unknown module '${modKey}'` });
|
|
568
|
+
const preview = this.latentPreviews.get(mod.key);
|
|
569
|
+
if (preview == null) return json(404, { error: "no latent preview yet" });
|
|
570
|
+
return {
|
|
571
|
+
status: 200,
|
|
572
|
+
contentType: preview.mime !== "" ? preview.mime : sniffImageContentType(preview.bytes),
|
|
573
|
+
body: preview.bytes
|
|
574
|
+
};
|
|
575
|
+
}
|
|
576
|
+
hostById(hostId) {
|
|
577
|
+
return this.modules.map((m) => m.dw.host).find((h) => h.data.id === hostId) ?? null;
|
|
578
|
+
}
|
|
579
|
+
hostIds() {
|
|
580
|
+
return [...new Set(this.modules.map((m) => m.dw.host.data.id))].join(", ");
|
|
581
|
+
}
|
|
582
|
+
/** mirror-backed text for the hover pane, no network */
|
|
583
|
+
replyLoraInfo(hostId, lora) {
|
|
584
|
+
if (this.hostById(hostId) == null) return json(404, { error: `unknown host '${hostId}' — hosts: ${this.hostIds()}` });
|
|
585
|
+
refreshLoraInfoCacheIfChanged();
|
|
586
|
+
return json(200, {
|
|
587
|
+
name: lora,
|
|
588
|
+
displayName: getLoraDisplayName(lora, hostId),
|
|
589
|
+
triggerWords: getLoraTriggerWords(lora, hostId)
|
|
590
|
+
});
|
|
591
|
+
}
|
|
592
|
+
async replyLoraPreview(hostId, lora) {
|
|
593
|
+
const host = this.hostById(hostId);
|
|
594
|
+
if (host == null) return json(404, { error: `unknown host '${hostId}' — hosts: ${this.hostIds()}` });
|
|
595
|
+
refreshLoraInfoCacheIfChanged();
|
|
596
|
+
let url = getLoraPreviewUrl(lora, hostId);
|
|
597
|
+
if (url == null) {
|
|
598
|
+
let sweep = this.previewSweeps.get(hostId);
|
|
599
|
+
if (sweep == null) {
|
|
600
|
+
sweep = fetchLoraList(host).then((r) => {
|
|
601
|
+
if (r.status === "unreachable") {
|
|
602
|
+
this.previewSweeps.delete(hostId);
|
|
603
|
+
return {
|
|
604
|
+
status: r.status,
|
|
605
|
+
map: null
|
|
606
|
+
};
|
|
607
|
+
}
|
|
608
|
+
return {
|
|
609
|
+
status: r.status,
|
|
610
|
+
map: r.status === "absent" ? null : loraPreviewMapFrom(r.items)
|
|
611
|
+
};
|
|
612
|
+
});
|
|
613
|
+
this.previewSweeps.set(hostId, sweep);
|
|
614
|
+
}
|
|
615
|
+
const swept = await sweep;
|
|
616
|
+
url = swept.map?.get(loraKey(lora)) ?? null;
|
|
617
|
+
if (url == null && swept.status === "unreachable") return json(404, { error: `host '${hostId}' is unreachable — no preview to fetch` });
|
|
618
|
+
if (url == null && swept.status === "absent") return json(404, { error: `lora-manager extension not detected on '${hostId}'` });
|
|
619
|
+
}
|
|
620
|
+
if (url == null) return json(404, { error: `no preview known for '${lora}' (run: comfy-ts loras --id ${hostId})` });
|
|
621
|
+
const bytes = await fetchLoraPreviewBytes(host, url);
|
|
622
|
+
if (bytes == null) return json(404, { error: `no image preview for '${lora}' (host '${hostId}' unreachable, or not an image)` });
|
|
623
|
+
return {
|
|
624
|
+
status: 200,
|
|
625
|
+
contentType: sniffImageContentType(bytes),
|
|
626
|
+
body: bytes
|
|
627
|
+
};
|
|
628
|
+
}
|
|
391
629
|
outputUrl(absPath) {
|
|
392
630
|
if (!absPath.startsWith(this.outputRoot + "/")) return null;
|
|
393
631
|
return "/outputs/" + absPath.slice(this.outputRoot.length + 1).split("/").map(encodeURIComponent).join("/");
|
|
@@ -409,8 +647,14 @@ var ServeApp = class {
|
|
|
409
647
|
const started = await this.exclusive(mod.key, () => this.prepareAndStart(mod, draft, payload, seeds));
|
|
410
648
|
if ("error" in started) return json(started.status, { error: started.error });
|
|
411
649
|
const execution = started.execution;
|
|
650
|
+
this.liveRuns.set(mod.key, execution);
|
|
651
|
+
this.promptOwner.set(execution.data.id, mod.key);
|
|
412
652
|
console.log(`[serve] → ${mod.key}/${draft} (prompt ${execution.data.id})`);
|
|
413
|
-
|
|
653
|
+
try {
|
|
654
|
+
await execution.done;
|
|
655
|
+
} finally {
|
|
656
|
+
this.promptOwner.delete(execution.data.id);
|
|
657
|
+
}
|
|
414
658
|
const durationMs = Date.now() - t0;
|
|
415
659
|
if (execution.status === "Failure") {
|
|
416
660
|
console.error(`[serve] 🔴 ${mod.key}/${draft} failed after ${durationMs}ms`);
|
|
@@ -447,7 +691,11 @@ var ServeApp = class {
|
|
|
447
691
|
/** everything that touches the module's SHARED vars, under the module mutex */
|
|
448
692
|
async prepareAndStart(mod, draft, payload, seeds) {
|
|
449
693
|
for (const [, varDef] of mod.dw.entries()) varDef.reset();
|
|
450
|
-
const draftPath =
|
|
694
|
+
const draftPath = this.draftPath(mod, draft);
|
|
695
|
+
if (draftPath == null) return {
|
|
696
|
+
status: 404,
|
|
697
|
+
error: `no draft '${draft}'`
|
|
698
|
+
};
|
|
451
699
|
if (existsSync(draftPath)) try {
|
|
452
700
|
const values = JSON.parse(readFileSync(draftPath, "utf8"));
|
|
453
701
|
for (const [k, varDef] of mod.dw.entries()) if (k in values) varDef.loadJSON(values[k]);
|
|
@@ -457,6 +705,8 @@ var ServeApp = class {
|
|
|
457
705
|
error: `draft file unreadable: ${draftPath} — ${extractErrorMessage(e)}`
|
|
458
706
|
};
|
|
459
707
|
}
|
|
708
|
+
const draftSeedValues = /* @__PURE__ */ new Map();
|
|
709
|
+
for (const [k, varDef] of mod.dw.entries()) if (varDef.kind === "seed") draftSeedValues.set(k, varDef.value);
|
|
460
710
|
const varMap = new Map(mod.dw.entries());
|
|
461
711
|
for (const [k, rawValue] of Object.entries(payload)) {
|
|
462
712
|
const varDef = varMap.get(k);
|
|
@@ -491,16 +741,22 @@ var ServeApp = class {
|
|
|
491
741
|
if (varDef.kind !== "seed") continue;
|
|
492
742
|
const seedVar = varDef;
|
|
493
743
|
const stateKey = `${mod.key}/${draft}/${k}`;
|
|
744
|
+
const draftValue = draftSeedValues.get(k) ?? seedVar.value;
|
|
494
745
|
if (!(k in payload)) {
|
|
495
746
|
if (seedVar.mode === "?") seedVar.randomize();
|
|
496
747
|
else if (seedVar.mode === "+" || seedVar.mode === "-") {
|
|
497
|
-
const
|
|
498
|
-
if (
|
|
748
|
+
const prev = this.seedState.get(stateKey);
|
|
749
|
+
if (prev != null && prev.draftBase === draftValue) seedVar.set(prev.last + (seedVar.mode === "+" ? 1 : -1));
|
|
499
750
|
}
|
|
500
751
|
}
|
|
501
|
-
this.seedState.set(stateKey,
|
|
752
|
+
this.seedState.set(stateKey, {
|
|
753
|
+
last: seedVar.value,
|
|
754
|
+
draftBase: draftValue
|
|
755
|
+
});
|
|
502
756
|
seeds[k] = seedVar.value;
|
|
503
757
|
}
|
|
758
|
+
this.latentPreviews.delete(mod.key);
|
|
759
|
+
this.wireLatents(mod.dw.host);
|
|
504
760
|
try {
|
|
505
761
|
return { execution: await this.starter(mod) };
|
|
506
762
|
} catch (e) {
|
|
@@ -537,6 +793,60 @@ var ServeApp = class {
|
|
|
537
793
|
}
|
|
538
794
|
};
|
|
539
795
|
//#endregion
|
|
796
|
+
//#region src/cli/serve/webBundle.ts
|
|
797
|
+
const WEB_ENTRY = "src/cli/serve/web/main.tsx";
|
|
798
|
+
/** nearest dir upward holding a package.json — the comfy-ts package root, from dist/ and src/ alike */
|
|
799
|
+
function packageRoot(fromDir) {
|
|
800
|
+
let dir = fromDir;
|
|
801
|
+
while (true) {
|
|
802
|
+
if (existsSync(join(dir, "package.json"))) return dir;
|
|
803
|
+
const parent = dirname(dir);
|
|
804
|
+
if (parent === dir) return null;
|
|
805
|
+
dir = parent;
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
/** bundle the web app to one browser file. Bun.build reads no tsconfig, so the
|
|
809
|
+
* house 'src/...' absolute imports resolve through the same plugin shape as
|
|
810
|
+
* tests/web-bundle.test.ts */
|
|
811
|
+
async function buildWebJsText(pkgRoot) {
|
|
812
|
+
const result = await Bun.build({
|
|
813
|
+
entrypoints: [join(pkgRoot, WEB_ENTRY)],
|
|
814
|
+
target: "browser",
|
|
815
|
+
minify: true,
|
|
816
|
+
throw: false,
|
|
817
|
+
define: { "process.env.NODE_ENV": "\"production\"" },
|
|
818
|
+
plugins: [{
|
|
819
|
+
name: "repo-src-paths",
|
|
820
|
+
setup(build) {
|
|
821
|
+
build.onResolve({ filter: /^src\// }, (args) => ({ path: join(pkgRoot, args.path) }));
|
|
822
|
+
}
|
|
823
|
+
}]
|
|
824
|
+
});
|
|
825
|
+
if (!result.success || result.outputs[0] == null) throw new Error(`web ui bundle failed:\n${result.logs.map((l) => String(l)).join("\n")}`);
|
|
826
|
+
return await result.outputs[0].text();
|
|
827
|
+
}
|
|
828
|
+
/** the runtime resolution chain; every miss logs loud and degrades to api-only */
|
|
829
|
+
async function loadOrBuildWebJs() {
|
|
830
|
+
const here = dirname(fileURLToPath(import.meta.url));
|
|
831
|
+
const prebuilt = join(here, "serve-web.js");
|
|
832
|
+
if (existsSync(prebuilt)) return readFileSync(prebuilt, "utf8");
|
|
833
|
+
if (typeof Bun === "undefined") {
|
|
834
|
+
console.error("[serve] 🔴 no serve-web.js next to the cli and not running under bun — api only, no web ui");
|
|
835
|
+
return null;
|
|
836
|
+
}
|
|
837
|
+
const pkgRoot = packageRoot(here);
|
|
838
|
+
if (pkgRoot == null || !existsSync(join(pkgRoot, WEB_ENTRY))) {
|
|
839
|
+
console.error(`[serve] 🔴 web ui source not found (looked for ${WEB_ENTRY} above ${here}) — api only, no web ui`);
|
|
840
|
+
return null;
|
|
841
|
+
}
|
|
842
|
+
try {
|
|
843
|
+
return await buildWebJsText(pkgRoot);
|
|
844
|
+
} catch (e) {
|
|
845
|
+
console.error("[serve] 🔴 web ui bundle failed — api only:", e);
|
|
846
|
+
return null;
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
//#endregion
|
|
540
850
|
//#region src/cli/serve/run-serve.ts
|
|
541
851
|
const DEFAULT_PORT = 8288;
|
|
542
852
|
const BODY_CAP = 1e7;
|
|
@@ -571,7 +881,7 @@ function makeRequestListener(app) {
|
|
|
571
881
|
return (req, res) => {
|
|
572
882
|
const cors = {
|
|
573
883
|
"access-control-allow-origin": "*",
|
|
574
|
-
"access-control-allow-methods": "GET, POST, OPTIONS",
|
|
884
|
+
"access-control-allow-methods": "GET, POST, PUT, OPTIONS",
|
|
575
885
|
"access-control-allow-headers": "content-type, accept"
|
|
576
886
|
};
|
|
577
887
|
const chunks = [];
|
|
@@ -654,7 +964,10 @@ async function runServe(args) {
|
|
|
654
964
|
console.error("[comfy-ts serve] every module failed to load — nothing to serve");
|
|
655
965
|
return 1;
|
|
656
966
|
}
|
|
657
|
-
const app = new ServeApp(modules, {
|
|
967
|
+
const app = new ServeApp(modules, {
|
|
968
|
+
loadErrors,
|
|
969
|
+
webJs: loadOrBuildWebJs
|
|
970
|
+
});
|
|
658
971
|
printStartup(app, parsed.bind, parsed.port);
|
|
659
972
|
const server = createServer(makeRequestListener(app));
|
|
660
973
|
server.on("error", (e) => {
|
|
@@ -681,8 +994,9 @@ function printStartup(app, bind, port) {
|
|
|
681
994
|
const firstMod = app.modules[0];
|
|
682
995
|
if (firstMod != null) console.log(`\ntry: curl -X POST ${base}/generate/${encodeURIComponent(firstMod.key)}/default -H 'content-type: application/json' -d '{}'`);
|
|
683
996
|
console.log(`docs: GET ${base}/drafts (json index of everything above)`);
|
|
997
|
+
console.log(`ui: open ${base}/ in a browser (every var as a form control)`);
|
|
684
998
|
}
|
|
685
999
|
//#endregion
|
|
686
1000
|
export { runServe };
|
|
687
1001
|
|
|
688
|
-
//# sourceMappingURL=run-serve-
|
|
1002
|
+
//# sourceMappingURL=run-serve-CVu39RNd.js.map
|