comfy-ts 2.16.0 → 2.17.1
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 +23 -0
- package/dist/{run-serve-C78hD7t9.js → ServeApp-qXcQfdgT.js} +18 -445
- package/dist/ServeApp-qXcQfdgT.js.map +1 -0
- package/dist/cli.js +72 -9
- package/dist/cli.js.map +1 -1
- package/dist/exampleAssets-BfWzygoM.js +38 -0
- package/dist/exampleAssets-BfWzygoM.js.map +1 -0
- package/dist/{LogsSt-Ct7Le0S_.js → findDefinedWorkflow-0lG-VTEd.js} +201 -201
- package/dist/findDefinedWorkflow-0lG-VTEd.js.map +1 -0
- package/dist/index.cjs +182 -69
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +61 -8
- package/dist/index.d.ts +61 -8
- package/dist/index.js +182 -69
- package/dist/index.js.map +1 -1
- package/dist/run-run-tg82jUIk.js +450 -0
- package/dist/run-run-tg82jUIk.js.map +1 -0
- package/dist/run-serve-DaXzoVTS.js +464 -0
- package/dist/run-serve-DaXzoVTS.js.map +1 -0
- package/dist/{run-tui-nPETFRmk.js → run-tui-Bw7BA75d.js} +7 -38
- package/dist/run-tui-Bw7BA75d.js.map +1 -0
- package/dist/{schemaDrift-wb5XkrHV.js → schemaDrift-CbsABBuk.js} +3 -2
- package/dist/{schemaDrift-wb5XkrHV.js.map → schemaDrift-CbsABBuk.js.map} +1 -1
- package/dist/serve-web.js +56 -48
- package/dist/{state-BMW57_M0.js → state-zTow5ucI.js} +182 -70
- package/dist/state-zTow5ucI.js.map +1 -0
- package/dist/web.d.ts +61 -8
- package/dist/web.js +182 -69
- package/dist/web.js.map +1 -1
- package/examples/README.md +1 -0
- package/examples/rvion/15-tts-voice-clone.cflow.ts +8 -8
- package/examples/rvion/16-tts-voice-design.cflow.ts +71 -0
- package/guide-for-agents.md +3 -0
- package/package.json +2 -2
- package/src/cli/bunHop.ts +49 -0
- package/src/cli/comfy-ts-cli.ts +18 -5
- package/src/cli/run/findModules.ts +36 -0
- package/src/cli/run/flagValue.ts +61 -0
- package/src/cli/run/outputPaths.ts +41 -0
- package/src/cli/run/pickModule.ts +41 -0
- package/src/cli/run/run-run.ts +250 -0
- package/src/cli/run/runArgs.ts +61 -0
- package/src/cli/run/sidecar.ts +27 -0
- package/src/cli/serve/ServeApp.ts +13 -39
- package/src/cli/serve/run-serve.ts +51 -13
- package/src/cli/serve/web/components/App.tsx +2 -0
- package/src/cli/serve/web/components/DialogLayer.tsx +55 -0
- package/src/cli/serve/web/components/DraftTabs.tsx +16 -5
- package/src/cli/serve/web/components/PromptEnhancer.tsx +15 -9
- package/src/cli/serve/web/components/SideMenu.tsx +16 -8
- package/src/cli/serve/web/state/WebSt.ts +10 -1
- package/src/cli/serve/web/state/dialog.ts +59 -0
- package/src/cli/serve/web/state/draftTabs.ts +12 -0
- package/src/cli/serve/web/state/keptLines.ts +1 -1
- package/src/cli/serve/web/state/shortcutCatalog.ts +1 -0
- package/src/cli/serve/web/styles.ts +8 -0
- package/src/coreExports.ts +2 -0
- package/src/host/ComfyHost.ts +73 -40
- package/src/host/previewMethod.ts +29 -0
- package/src/litegraph/convertFlowToLiteGraphJSON.ts +38 -20
- package/src/promptEnhancers.ts +1 -1
- package/src/runner/ComfyExecution.ts +45 -1
- package/src/runner/ComfyWorkflow.ts +6 -1
- package/src/runner/ComfyWsApi.ts +1 -1
- package/src/runner/wsBinaryFrame.ts +48 -25
- package/src/vars/ComfyVars.ts +3 -0
- package/src/vars/DefinedWorkflow.ts +1 -0
- package/dist/LogsSt-Ct7Le0S_.js.map +0 -1
- package/dist/run-serve-C78hD7t9.js.map +0 -1
- package/dist/run-tui-nPETFRmk.js.map +0 -1
- package/dist/state-BMW57_M0.js.map +0 -1
package/README.md
CHANGED
|
@@ -220,6 +220,27 @@ await txt2img.run({ log: true }) // fresh graph, fresh image
|
|
|
220
220
|
|
|
221
221
|
`vars` can be a lambda receiving `v` so vars reference each other: `v.prompt('a cozy house', { loraKeywordsFrom: loras })` prefixes the active loras' trigger keywords. Name the file `*.cflow.ts` and the TUI finds it.
|
|
222
222
|
|
|
223
|
+
## 👀 Live previews: watch the image form
|
|
224
|
+
|
|
225
|
+
Pass `onPreview` to any run and every sampling step hands you the image so far:
|
|
226
|
+
|
|
227
|
+
```ts
|
|
228
|
+
const execution = await txt2img.run({
|
|
229
|
+
onPreview: (p) => {
|
|
230
|
+
// p.bytes: jpeg or png (p.mime) · p.nodeId / p.nodeName: 'KSampler' · p.step: { value: 7, max: 20 }
|
|
231
|
+
showImage(new Blob([p.bytes], { type: p.mime }))
|
|
232
|
+
},
|
|
233
|
+
})
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
It works the same on `definedWorkflow.run()`, `workflow.run()` and `workflow.start()`. Without `onPreview`, no frame is decoded or copied for the run. A preview is never an output: `execution.images` holds only what the graph saved.
|
|
237
|
+
|
|
238
|
+
ComfyUI sends previews only when it was launched with `--preview-method auto` (or `latent2rgb`, `taesd`). Its default is `none`, and then no frame ever arrives. Ask the host:
|
|
239
|
+
|
|
240
|
+
```ts
|
|
241
|
+
await host.fetchPreviewMethod() // 'auto' · 'none' = relaunch with --preview-method auto · null = the host does not say (cloud)
|
|
242
|
+
```
|
|
243
|
+
|
|
223
244
|
## 🕶️ Ephemeral outputs: leave no traces
|
|
224
245
|
|
|
225
246
|
Some images should not outlive the run — client work, private subjects, anything you would not leave in a shared server's `output/` folder. ComfyUI has three image savers, and they differ exactly there:
|
|
@@ -297,6 +318,8 @@ bunx comfy-ts loras # mirror your loras' names + trigger words (b
|
|
|
297
318
|
bunx comfy-ts tui # your *.cflow.ts + bundled examples
|
|
298
319
|
bunx comfy-ts tui [dir | module] # scan just that
|
|
299
320
|
bunx comfy-ts serve [dir | module] # drafts as a local HTTP API (below)
|
|
321
|
+
bunx comfy-ts run anima-t2i --prompt "a knight in the rain" --out knight.png
|
|
322
|
+
# one generation, headless: every var a flag
|
|
300
323
|
```
|
|
301
324
|
|
|
302
325
|
Any reachable host: the box under your desk or a GPU machine across the network, same command, same output.
|
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import { $ as DEFAULT_MEDIA_EXTENSIONS, A as reloadLoraInfoCache, B as lmFileSize, C as getLoraTriggers, E as lorasMissingFromMirror, F as fetchLoraList, G as lmTags, I as fetchLoraPreviewBytes, J as loraKey, L as lmBaseModel, M as fetchLoraCivitai, N as fetchLoraDescription, O as recordCivitaiMiss, P as fetchLoraExampleImages,
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { $ as DEFAULT_MEDIA_EXTENSIONS, A as reloadLoraInfoCache, B as lmFileSize, C as getLoraTriggers, E as lorasMissingFromMirror, F as fetchLoraList, G as lmTags, I as fetchLoraPreviewBytes, J as loraKey, L as lmBaseModel, M as fetchLoraCivitai, N as fetchLoraDescription, O as recordCivitaiMiss, P as fetchLoraExampleImages, R as lmCivitai, S as getLoraTriggerWords, T as loraMirrorEntries, U as lmNotes, V as lmFolder, W as lmSha256, X as ansi, Y as loraPreviewMapFrom, _ as buildLoraMirror, at as extractErrorMessage, b as getLoraInfo, c as isLoraLanes, d as isLoraStrength, et as MEDIA_KINDS, ht as sha1HexOfString, i as summarizeDrift, j as writeLoraMirror, k as refreshLoraInfoCacheIfChanged, l as isLorasInput, mt as statelessRegex, p as getLoraKeyword, r as driftChanged, st as bang, u as isPromptInput, v as getLoraAddedAt, x as getLoraPreviewUrl, y as getLoraDisplayName, z as lmFilePath } from "./schemaDrift-CbsABBuk.js";
|
|
2
|
+
import { d as isDraftMetaKey, l as assembleLogChunks, o as draftsDirForFile, s as listDraftsForFile, u as ENHANCE_KEY } from "./findDefinedWorkflow-0lG-VTEd.js";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
4
5
|
import { closeSync, existsSync, mkdirSync, openSync, readFileSync, readSync, readdirSync, realpathSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
5
6
|
import { basename, dirname, extname, isAbsolute, join, resolve } from "pathe";
|
|
6
7
|
import { nanoid } from "nanoid";
|
|
7
|
-
import { createServer } from "node:http";
|
|
8
|
-
import { homedir, networkInterfaces } from "node:os";
|
|
9
8
|
//#region src/cli/serve/describeVar.ts
|
|
10
9
|
/** an empty list is absent from the json: every var without presets would otherwise carry `[]` */
|
|
11
10
|
function presetsOrUndefined(presets) {
|
|
@@ -314,7 +313,7 @@ Rewrite it into ONE paragraph a reader could picture with their eyes closed:
|
|
|
314
313
|
- plain descriptive english. Flowing sentences and comma separated descriptive clauses both read well; disconnected keyword soup does not. No praise words, never "masterpiece", "8k", "highly detailed", "award winning", "trending on". The model is steered by what you name, not by what you forbid.
|
|
315
314
|
- text that must appear inside the image is spelled out in "quotes", exactly as it should be rendered.
|
|
316
315
|
- roughly 60 to 150 words, one paragraph, no lists, no headings, no quotes around the result itself.
|
|
317
|
-
-
|
|
316
|
+
- never write a negative ("- ...") line or a comment ("// ...") line: the panel keeps the user's own and adds them back after your prompt.
|
|
318
317
|
|
|
319
318
|
Answer with the rewritten prompt and nothing else: no preamble, no explanation.
|
|
320
319
|
`
|
|
@@ -999,7 +998,10 @@ const realStarter = async (mod, opts) => {
|
|
|
999
998
|
return await (await mod.dw.build({
|
|
1000
999
|
advance: true,
|
|
1001
1000
|
host
|
|
1002
|
-
})).start({
|
|
1001
|
+
})).start({
|
|
1002
|
+
save: opts.saveToDisk ? { prefix: opts.savePrefix } : false,
|
|
1003
|
+
onPreview: opts.onPreview
|
|
1004
|
+
});
|
|
1003
1005
|
};
|
|
1004
1006
|
const CONTENT_TYPES = {
|
|
1005
1007
|
".png": "image/png",
|
|
@@ -1167,8 +1169,6 @@ var ServeApp = class {
|
|
|
1167
1169
|
this.liveRuns = /* @__PURE__ */ new Map();
|
|
1168
1170
|
this.latentPreviews = /* @__PURE__ */ new Map();
|
|
1169
1171
|
this.latentSeq = 0;
|
|
1170
|
-
this.promptOwner = /* @__PURE__ */ new Map();
|
|
1171
|
-
this.latentHosts = /* @__PURE__ */ new Set();
|
|
1172
1172
|
this.previewSweeps = /* @__PURE__ */ new Map();
|
|
1173
1173
|
this.starter = opts.starter ?? realStarter;
|
|
1174
1174
|
}
|
|
@@ -2111,25 +2111,6 @@ var ServeApp = class {
|
|
|
2111
2111
|
configs: listLlmConfigs()
|
|
2112
2112
|
});
|
|
2113
2113
|
}
|
|
2114
|
-
runningModules() {
|
|
2115
|
-
return [...this.liveRuns.entries()].filter(([, e]) => e.status !== "Success" && e.status !== "Failure").map(([key]) => key);
|
|
2116
|
-
}
|
|
2117
|
-
/** one dispatcher per host, installed once and left in place (idempotent) */
|
|
2118
|
-
wireLatents(host) {
|
|
2119
|
-
if (this.latentHosts.has(host)) return;
|
|
2120
|
-
this.latentHosts.add(host);
|
|
2121
|
-
host.onLatentPreview = (p) => {
|
|
2122
|
-
const owner = p.promptID != null ? this.promptOwner.get(p.promptID) : void 0;
|
|
2123
|
-
const running = this.runningModules();
|
|
2124
|
-
const key = owner ?? (running.length === 1 ? running[0] : void 0);
|
|
2125
|
-
if (key == null) return;
|
|
2126
|
-
this.latentPreviews.set(key, {
|
|
2127
|
-
bytes: p.bytes,
|
|
2128
|
-
mime: p.mime,
|
|
2129
|
-
seq: ++this.latentSeq
|
|
2130
|
-
});
|
|
2131
|
-
};
|
|
2132
|
-
}
|
|
2133
2114
|
replyRunStatus(modKey) {
|
|
2134
2115
|
const mod = this.moduleByKey(modKey);
|
|
2135
2116
|
if (mod == null) return json(404, { error: `unknown module '${modKey}'` });
|
|
@@ -2273,13 +2254,8 @@ var ServeApp = class {
|
|
|
2273
2254
|
if ("error" in started) return json(started.status, { error: started.error });
|
|
2274
2255
|
const execution = started.execution;
|
|
2275
2256
|
this.liveRuns.set(mod.key, execution);
|
|
2276
|
-
this.promptOwner.set(execution.data.id, mod.key);
|
|
2277
2257
|
console.log(`[serve] → ${mod.key}/${draft} (prompt ${execution.data.id})`);
|
|
2278
|
-
|
|
2279
|
-
await execution.done;
|
|
2280
|
-
} finally {
|
|
2281
|
-
this.promptOwner.delete(execution.data.id);
|
|
2282
|
-
}
|
|
2258
|
+
await execution.done;
|
|
2283
2259
|
const durationMs = Date.now() - t0;
|
|
2284
2260
|
if (execution.status === "Failure") {
|
|
2285
2261
|
const error = describeRunError(execution.data.error);
|
|
@@ -2478,12 +2454,16 @@ var ServeApp = class {
|
|
|
2478
2454
|
}
|
|
2479
2455
|
this.latentPreviews.delete(mod.key);
|
|
2480
2456
|
const runHost = this.runHostFor(mod);
|
|
2481
|
-
this.wireLatents(runHost ?? mod.dw.host);
|
|
2482
2457
|
try {
|
|
2483
2458
|
return { execution: await this.starter(mod, {
|
|
2484
2459
|
saveToDisk: this.settings.saveToDisk,
|
|
2485
2460
|
savePrefix: this.savePrefixFor(mod.key),
|
|
2486
|
-
host: runHost ?? void 0
|
|
2461
|
+
host: runHost ?? void 0,
|
|
2462
|
+
onPreview: (p) => this.latentPreviews.set(mod.key, {
|
|
2463
|
+
bytes: p.bytes,
|
|
2464
|
+
mime: p.mime,
|
|
2465
|
+
seq: ++this.latentSeq
|
|
2466
|
+
})
|
|
2487
2467
|
}) };
|
|
2488
2468
|
} catch (e) {
|
|
2489
2469
|
if (e instanceof Error && (e.name === "MediaVarEmptyError" || e.name === "ImageVarEmptyError")) return {
|
|
@@ -2535,413 +2515,6 @@ var ServeApp = class {
|
|
|
2535
2515
|
}
|
|
2536
2516
|
};
|
|
2537
2517
|
//#endregion
|
|
2538
|
-
|
|
2539
|
-
/** one range, the only kind media players send: `bytes=a-b`, `bytes=a-`, `bytes=-n` */
|
|
2540
|
-
function parseRange(header, size) {
|
|
2541
|
-
const m = /^bytes=(\d*)-(\d*)$/.exec(header.trim());
|
|
2542
|
-
if (m == null) return null;
|
|
2543
|
-
const a = m[1] ?? "";
|
|
2544
|
-
const b = m[2] ?? "";
|
|
2545
|
-
if (a === "" && b === "") return null;
|
|
2546
|
-
if (a === "") {
|
|
2547
|
-
const n = Number(b);
|
|
2548
|
-
if (n === 0) return "unsatisfiable";
|
|
2549
|
-
return {
|
|
2550
|
-
start: Math.max(0, size - n),
|
|
2551
|
-
end: size - 1
|
|
2552
|
-
};
|
|
2553
|
-
}
|
|
2554
|
-
const start = Number(a);
|
|
2555
|
-
const end = b === "" ? size - 1 : Math.min(Number(b), size - 1);
|
|
2556
|
-
if (start >= size || start > end) return "unsatisfiable";
|
|
2557
|
-
return {
|
|
2558
|
-
start,
|
|
2559
|
-
end
|
|
2560
|
-
};
|
|
2561
|
-
}
|
|
2562
|
-
/** a whole 200 binary reply gets its length and `accept-ranges`; a range request gets 206 with
|
|
2563
|
-
* those bytes, or 416 past the end. Text replies and errors pass through untouched */
|
|
2564
|
-
function withByteRange(reply, req) {
|
|
2565
|
-
if (req.method !== "GET" || reply.status !== 200 || typeof reply.body === "string") return reply;
|
|
2566
|
-
const size = reply.body.length;
|
|
2567
|
-
const base = {
|
|
2568
|
-
...reply.headers,
|
|
2569
|
-
"accept-ranges": "bytes"
|
|
2570
|
-
};
|
|
2571
|
-
const range = req.range == null ? null : parseRange(req.range, size);
|
|
2572
|
-
if (range == null) return {
|
|
2573
|
-
...reply,
|
|
2574
|
-
headers: {
|
|
2575
|
-
...base,
|
|
2576
|
-
"content-length": String(size)
|
|
2577
|
-
}
|
|
2578
|
-
};
|
|
2579
|
-
if (range === "unsatisfiable") return {
|
|
2580
|
-
status: 416,
|
|
2581
|
-
contentType: reply.contentType,
|
|
2582
|
-
body: /* @__PURE__ */ new Uint8Array(0),
|
|
2583
|
-
headers: {
|
|
2584
|
-
...base,
|
|
2585
|
-
"content-range": `bytes */${size}`
|
|
2586
|
-
}
|
|
2587
|
-
};
|
|
2588
|
-
return {
|
|
2589
|
-
status: 206,
|
|
2590
|
-
contentType: reply.contentType,
|
|
2591
|
-
body: reply.body.subarray(range.start, range.end + 1),
|
|
2592
|
-
headers: {
|
|
2593
|
-
...base,
|
|
2594
|
-
"content-range": `bytes ${range.start}-${range.end}/${size}`,
|
|
2595
|
-
"content-length": String(range.end - range.start + 1)
|
|
2596
|
-
}
|
|
2597
|
-
};
|
|
2598
|
-
}
|
|
2599
|
-
//#endregion
|
|
2600
|
-
//#region src/cli/serve/startupPrint.ts
|
|
2601
|
-
const RULE_WIDTH = 64;
|
|
2602
|
-
/** 100.64.0.0/10 is the CGNAT range tailscale hands out — worth naming, it is the one
|
|
2603
|
-
* you want when the phone is not on the same wifi */
|
|
2604
|
-
function addressNote(addr) {
|
|
2605
|
-
const octets = addr.split(".").map(Number);
|
|
2606
|
-
const second = octets[1] ?? 0;
|
|
2607
|
-
if (octets[0] === 100 && second >= 64 && second <= 127) return "tailnet";
|
|
2608
|
-
if (addr === "127.0.0.1" || addr === "localhost") return "this machine";
|
|
2609
|
-
return "";
|
|
2610
|
-
}
|
|
2611
|
-
function paint(color) {
|
|
2612
|
-
const id = (s) => s;
|
|
2613
|
-
if (!color) return {
|
|
2614
|
-
title: id,
|
|
2615
|
-
name: id,
|
|
2616
|
-
dim: id,
|
|
2617
|
-
url: id,
|
|
2618
|
-
warn: id
|
|
2619
|
-
};
|
|
2620
|
-
return {
|
|
2621
|
-
title: (s) => ansi.bold(s),
|
|
2622
|
-
name: (s) => ansi.bold.cyan(s),
|
|
2623
|
-
dim: (s) => ansi.gray(s),
|
|
2624
|
-
url: (s) => ansi.green(s),
|
|
2625
|
-
warn: (s) => ansi.yellow(s)
|
|
2626
|
-
};
|
|
2627
|
-
}
|
|
2628
|
-
/** a box that stays square whatever the colors do: padding measures the VISIBLE text */
|
|
2629
|
-
function boxed(lines, p = { color: false }) {
|
|
2630
|
-
const width = Math.max(...lines.map((l) => stripAnsi(l).length), 0);
|
|
2631
|
-
const edge = (l, r) => `${l}${"─".repeat(width + 2)}${r}`;
|
|
2632
|
-
const border = (s) => p.color ? ansi.gray(s) : s;
|
|
2633
|
-
return [
|
|
2634
|
-
border(edge("┌", "┐")),
|
|
2635
|
-
...lines.map((l) => `${border("│")} ${l}${" ".repeat(width - stripAnsi(l).length)} ${border("│")}`),
|
|
2636
|
-
border(edge("└", "┘"))
|
|
2637
|
-
];
|
|
2638
|
-
}
|
|
2639
|
-
function renderStartupLines(p) {
|
|
2640
|
-
const c = paint(p.color);
|
|
2641
|
-
const loopback = p.bind === "127.0.0.1" || p.bind === "localhost" || p.bind === "::1";
|
|
2642
|
-
const primary = `http://${p.urls[0] ?? p.bind}:${p.port}/`;
|
|
2643
|
-
const out = [];
|
|
2644
|
-
const count = `${p.modules.length} workflow${p.modules.length === 1 ? "" : "s"}`;
|
|
2645
|
-
out.push(c.title(`comfy-ts serve · ${count} · ${primary}`));
|
|
2646
|
-
out.push(c.warn(loopback ? "⚠️ no auth: this API runs workflows, and any page open in your browser can call it" : `⚠️ bound to ${p.bind}, no auth: anyone on this network can run workflows on your ComfyUI host`));
|
|
2647
|
-
for (const mod of p.modules) {
|
|
2648
|
-
out.push("", c.dim("─".repeat(RULE_WIDTH)));
|
|
2649
|
-
out.push(`${c.name(mod.key)} ${c.dim(`host: ${mod.hostId}`)}`);
|
|
2650
|
-
for (const route of mod.routes) out.push(c.dim(` POST ${route}`));
|
|
2651
|
-
out.push(...mod.varLines);
|
|
2652
|
-
}
|
|
2653
|
-
out.push(c.dim("─".repeat(RULE_WIDTH)), "");
|
|
2654
|
-
const box = [`${c.title("web ui")} ${c.url(primary)}`, `${c.dim("json api")} ${c.dim(`${primary}drafts`)}`];
|
|
2655
|
-
if (p.urls.length > 1) {
|
|
2656
|
-
box.push("", c.dim("also reachable at:"));
|
|
2657
|
-
for (const url of p.urls.slice(1)) {
|
|
2658
|
-
const note = addressNote(url);
|
|
2659
|
-
box.push(` ${c.url(`http://${url}:${p.port}/`)}${note === "" ? "" : c.dim(` (${note})`)}`);
|
|
2660
|
-
}
|
|
2661
|
-
}
|
|
2662
|
-
if (loopback) {
|
|
2663
|
-
box.push("", c.dim("to open it from your phone or another machine:"));
|
|
2664
|
-
box.push(c.dim(` comfy-ts serve --host 0.0.0.0 (--bind is the same flag)`));
|
|
2665
|
-
box.push(c.dim(" every reachable url gets printed here, tailnet included"));
|
|
2666
|
-
}
|
|
2667
|
-
out.push(...boxed(box, { color: p.color }));
|
|
2668
|
-
return out;
|
|
2669
|
-
}
|
|
2670
|
-
//#endregion
|
|
2671
|
-
//#region src/cli/serve/webBundle.ts
|
|
2672
|
-
const WEB_ENTRY = "src/cli/serve/web/main.tsx";
|
|
2673
|
-
/** nearest dir upward holding a package.json — the comfy-ts package root, from dist/ and src/ alike */
|
|
2674
|
-
function packageRoot(fromDir) {
|
|
2675
|
-
let dir = fromDir;
|
|
2676
|
-
while (true) {
|
|
2677
|
-
if (existsSync(join(dir, "package.json"))) return dir;
|
|
2678
|
-
const parent = dirname(dir);
|
|
2679
|
-
if (parent === dir) return null;
|
|
2680
|
-
dir = parent;
|
|
2681
|
-
}
|
|
2682
|
-
}
|
|
2683
|
-
/** bundle the web app to one browser file. Bun.build reads no tsconfig, so the
|
|
2684
|
-
* house 'src/...' absolute imports resolve through the same plugin shape as
|
|
2685
|
-
* tests/web-bundle.test.ts */
|
|
2686
|
-
async function buildWebJsText(pkgRoot) {
|
|
2687
|
-
const result = await Bun.build({
|
|
2688
|
-
entrypoints: [join(pkgRoot, WEB_ENTRY)],
|
|
2689
|
-
target: "browser",
|
|
2690
|
-
minify: true,
|
|
2691
|
-
throw: false,
|
|
2692
|
-
define: { "process.env.NODE_ENV": "\"production\"" },
|
|
2693
|
-
plugins: [{
|
|
2694
|
-
name: "repo-src-paths",
|
|
2695
|
-
setup(build) {
|
|
2696
|
-
build.onResolve({ filter: /^src\// }, (args) => ({ path: join(pkgRoot, args.path) }));
|
|
2697
|
-
}
|
|
2698
|
-
}]
|
|
2699
|
-
});
|
|
2700
|
-
if (!result.success || result.outputs[0] == null) throw new Error(`web ui bundle failed:\n${result.logs.map((l) => String(l)).join("\n")}`);
|
|
2701
|
-
return await result.outputs[0].text();
|
|
2702
|
-
}
|
|
2703
|
-
/** changes whenever a file under `dir` is added, removed or rewritten. The web bundle reaches
|
|
2704
|
-
* outside web/ (vars, protocol types), so the whole src/ tree is the input: ~300 stats, a few ms */
|
|
2705
|
-
function sourceSignature(dir) {
|
|
2706
|
-
let count = 0;
|
|
2707
|
-
let newest = 0;
|
|
2708
|
-
let bytes = 0;
|
|
2709
|
-
const walk = (d) => {
|
|
2710
|
-
for (const entry of readdirSync(d, { withFileTypes: true })) {
|
|
2711
|
-
const abs = join(d, entry.name);
|
|
2712
|
-
if (entry.isDirectory()) walk(abs);
|
|
2713
|
-
else if (entry.isFile()) {
|
|
2714
|
-
const st = statSync(abs);
|
|
2715
|
-
count += 1;
|
|
2716
|
-
bytes += st.size;
|
|
2717
|
-
newest = Math.max(newest, st.mtimeMs);
|
|
2718
|
-
}
|
|
2719
|
-
}
|
|
2720
|
-
};
|
|
2721
|
-
walk(dir);
|
|
2722
|
-
return `${count}:${bytes}:${newest}`;
|
|
2723
|
-
}
|
|
2724
|
-
let prebuiltJs = null;
|
|
2725
|
-
let fromSource = null;
|
|
2726
|
-
/** the runtime resolution chain; every miss logs loud and degrades to api-only. Called on every
|
|
2727
|
-
* page load: a prebuilt file is read once, a source build is redone only when src/ changed, so
|
|
2728
|
-
* an edited panel shows on refresh under `bun --watch`, which never sees the bundled files */
|
|
2729
|
-
async function loadOrBuildWebJs() {
|
|
2730
|
-
const here = dirname(fileURLToPath(import.meta.url));
|
|
2731
|
-
const prebuilt = join(here, "serve-web.js");
|
|
2732
|
-
if (prebuiltJs != null) return prebuiltJs;
|
|
2733
|
-
if (existsSync(prebuilt)) {
|
|
2734
|
-
prebuiltJs = readFileSync(prebuilt, "utf8");
|
|
2735
|
-
return prebuiltJs;
|
|
2736
|
-
}
|
|
2737
|
-
if (typeof Bun === "undefined") {
|
|
2738
|
-
console.error("[serve] 🔴 no serve-web.js next to the cli and not running under bun — api only, no web ui");
|
|
2739
|
-
return null;
|
|
2740
|
-
}
|
|
2741
|
-
const pkgRoot = packageRoot(here);
|
|
2742
|
-
if (pkgRoot == null || !existsSync(join(pkgRoot, WEB_ENTRY))) {
|
|
2743
|
-
console.error(`[serve] 🔴 web ui source not found (looked for ${WEB_ENTRY} above ${here}) — api only, no web ui`);
|
|
2744
|
-
return null;
|
|
2745
|
-
}
|
|
2746
|
-
const signature = sourceSignature(join(pkgRoot, "src"));
|
|
2747
|
-
if (fromSource?.signature !== signature) {
|
|
2748
|
-
const rebuilt = fromSource != null;
|
|
2749
|
-
fromSource = {
|
|
2750
|
-
signature,
|
|
2751
|
-
js: buildWebJsText(pkgRoot).then((js) => {
|
|
2752
|
-
if (rebuilt) console.log("[serve] web ui rebuilt from source");
|
|
2753
|
-
return js;
|
|
2754
|
-
}, (e) => {
|
|
2755
|
-
console.error("[serve] 🔴 web ui bundle failed — api only until the next edit:", e);
|
|
2756
|
-
return null;
|
|
2757
|
-
})
|
|
2758
|
-
};
|
|
2759
|
-
}
|
|
2760
|
-
return await fromSource.js;
|
|
2761
|
-
}
|
|
2762
|
-
//#endregion
|
|
2763
|
-
//#region src/cli/serve/run-serve.ts
|
|
2764
|
-
const DEFAULT_PORT = 8288;
|
|
2765
|
-
const BODY_CAP = 1e7;
|
|
2766
|
-
/** a video dropped on a media var rides POST /upload as base64 json: ~200 MB of file */
|
|
2767
|
-
const UPLOAD_BODY_CAP = 28e7;
|
|
2768
|
-
function parseArgs(args) {
|
|
2769
|
-
let target;
|
|
2770
|
-
let port = DEFAULT_PORT;
|
|
2771
|
-
let bind = "127.0.0.1";
|
|
2772
|
-
for (let i = 0; i < args.length; i++) {
|
|
2773
|
-
const a = args[i];
|
|
2774
|
-
if (a === "--port") {
|
|
2775
|
-
const n = Number(args[++i]);
|
|
2776
|
-
if (!Number.isInteger(n) || n <= 0 || n > 65535) return { error: `--port expects 1..65535, got '${args[i]}'` };
|
|
2777
|
-
port = n;
|
|
2778
|
-
} else if (a === "--host" || a === "--bind") {
|
|
2779
|
-
const b = args[++i];
|
|
2780
|
-
if (b == null) return { error: `${a} expects an address (0.0.0.0 to reach it from another machine)` };
|
|
2781
|
-
bind = b;
|
|
2782
|
-
} else if (a != null && a.startsWith("--")) return { error: `unknown flag '${a}'` };
|
|
2783
|
-
else if (a != null) {
|
|
2784
|
-
if (target != null) return { error: `one target only (got '${target}' and '${a}')` };
|
|
2785
|
-
target = a;
|
|
2786
|
-
}
|
|
2787
|
-
}
|
|
2788
|
-
return {
|
|
2789
|
-
target,
|
|
2790
|
-
port,
|
|
2791
|
-
bind
|
|
2792
|
-
};
|
|
2793
|
-
}
|
|
2794
|
-
/** node:http glue around ServeApp.handle — exported so tests can drive a real socket */
|
|
2795
|
-
/** open CORS, because a browser page IS a first-class client here: the web entry runs the
|
|
2796
|
-
* library in a tab and rides serve as its bridge to hosts that send no CORS headers
|
|
2797
|
-
* (examples/web is on another port by construction). The cost is real and stated at launch:
|
|
2798
|
-
* with no auth, any page you visit can drive this api. That is the same trade as the port
|
|
2799
|
-
* being open at all, so it is said out loud rather than hidden behind a flag. */
|
|
2800
|
-
function makeRequestListener(app) {
|
|
2801
|
-
const cors = {
|
|
2802
|
-
"access-control-allow-origin": "*",
|
|
2803
|
-
"access-control-allow-methods": "GET, POST, PUT, DELETE, OPTIONS",
|
|
2804
|
-
"access-control-allow-headers": "content-type, accept"
|
|
2805
|
-
};
|
|
2806
|
-
return (req, res) => {
|
|
2807
|
-
const cap = req.method === "POST" && (req.url ?? "").split("?")[0] === "/upload" ? UPLOAD_BODY_CAP : BODY_CAP;
|
|
2808
|
-
const chunks = [];
|
|
2809
|
-
let size = 0;
|
|
2810
|
-
let overflow = false;
|
|
2811
|
-
req.on("error", (e) => console.error(`[serve] request stream error: ${extractErrorMessage(e)}`));
|
|
2812
|
-
req.on("data", (c) => {
|
|
2813
|
-
if (overflow) return;
|
|
2814
|
-
size += c.length;
|
|
2815
|
-
if (size > cap) {
|
|
2816
|
-
overflow = true;
|
|
2817
|
-
res.writeHead(413, {
|
|
2818
|
-
"content-type": "application/json",
|
|
2819
|
-
...cors
|
|
2820
|
-
});
|
|
2821
|
-
res.end(JSON.stringify({ error: "payload too large" }), () => req.destroy());
|
|
2822
|
-
} else chunks.push(c);
|
|
2823
|
-
});
|
|
2824
|
-
req.on("end", () => {
|
|
2825
|
-
if (overflow) return;
|
|
2826
|
-
if (req.method === "OPTIONS") {
|
|
2827
|
-
res.writeHead(204, cors);
|
|
2828
|
-
res.end();
|
|
2829
|
-
return;
|
|
2830
|
-
}
|
|
2831
|
-
app.handle({
|
|
2832
|
-
method: req.method ?? "GET",
|
|
2833
|
-
url: req.url ?? "/",
|
|
2834
|
-
accept: req.headers.accept,
|
|
2835
|
-
body: Buffer.concat(chunks).toString("utf8")
|
|
2836
|
-
}).then((whole) => {
|
|
2837
|
-
const range = req.headers.range;
|
|
2838
|
-
const reply = withByteRange(whole, {
|
|
2839
|
-
method: req.method ?? "GET",
|
|
2840
|
-
range
|
|
2841
|
-
});
|
|
2842
|
-
res.writeHead(reply.status, {
|
|
2843
|
-
"content-type": reply.contentType,
|
|
2844
|
-
...reply.headers,
|
|
2845
|
-
...cors
|
|
2846
|
-
});
|
|
2847
|
-
res.end(reply.body);
|
|
2848
|
-
}).catch((e) => {
|
|
2849
|
-
console.error(`[serve] response write failed: ${extractErrorMessage(e)}`);
|
|
2850
|
-
res.destroy();
|
|
2851
|
-
});
|
|
2852
|
-
});
|
|
2853
|
-
};
|
|
2854
|
-
}
|
|
2855
|
-
async function runServe(args) {
|
|
2856
|
-
const parsed = parseArgs(args);
|
|
2857
|
-
if ("error" in parsed) {
|
|
2858
|
-
console.error(`[comfy-ts serve] ${parsed.error}`);
|
|
2859
|
-
return 1;
|
|
2860
|
-
}
|
|
2861
|
-
const target = resolve(parsed.target ?? ".");
|
|
2862
|
-
let files;
|
|
2863
|
-
try {
|
|
2864
|
-
files = statSync(target).isDirectory() ? scanCflowFiles(target) : [target];
|
|
2865
|
-
} catch {
|
|
2866
|
-
console.error(`[comfy-ts serve] no such file or directory: ${target}`);
|
|
2867
|
-
return 1;
|
|
2868
|
-
}
|
|
2869
|
-
if (files.length === 0) {
|
|
2870
|
-
console.error(`[comfy-ts serve] no *.cflow.ts modules under ${target}`);
|
|
2871
|
-
return 1;
|
|
2872
|
-
}
|
|
2873
|
-
const modules = [];
|
|
2874
|
-
const loadErrors = {};
|
|
2875
|
-
for (const file of files) try {
|
|
2876
|
-
const dw = findDefinedWorkflow(await import(pathToFileURL(file).href));
|
|
2877
|
-
if (dw == null) throw new Error("exports no DefinedWorkflow");
|
|
2878
|
-
const key = draftKeyForFile(file);
|
|
2879
|
-
const clash = modules.find((m) => m.key === key);
|
|
2880
|
-
if (clash != null) throw new Error(`module key '${key}' already taken by ${clash.file}`);
|
|
2881
|
-
modules.push({
|
|
2882
|
-
key,
|
|
2883
|
-
file,
|
|
2884
|
-
dw
|
|
2885
|
-
});
|
|
2886
|
-
} catch (e) {
|
|
2887
|
-
loadErrors[file] = extractErrorMessage(e);
|
|
2888
|
-
console.error(`[comfy-ts serve] ✗ ${file}: ${loadErrors[file]}`);
|
|
2889
|
-
}
|
|
2890
|
-
if (modules.length === 0) {
|
|
2891
|
-
console.error("[comfy-ts serve] every module failed to load — nothing to serve");
|
|
2892
|
-
return 1;
|
|
2893
|
-
}
|
|
2894
|
-
const app = new ServeApp(modules, {
|
|
2895
|
-
loadErrors,
|
|
2896
|
-
webJs: loadOrBuildWebJs,
|
|
2897
|
-
workspace: typeof comfyts === "undefined" ? process.cwd() : comfyts.rootPath,
|
|
2898
|
-
root: statSync(target).isDirectory() ? target : dirname(target)
|
|
2899
|
-
});
|
|
2900
|
-
printStartup(app, parsed.bind, parsed.port);
|
|
2901
|
-
const server = createServer(makeRequestListener(app));
|
|
2902
|
-
server.on("error", (e) => {
|
|
2903
|
-
console.error(`[comfy-ts serve] server error: ${extractErrorMessage(e)}`);
|
|
2904
|
-
process.exitCode = 1;
|
|
2905
|
-
server.close();
|
|
2906
|
-
});
|
|
2907
|
-
server.listen(parsed.port, parsed.bind);
|
|
2908
|
-
return new Promise((resolveExit) => {
|
|
2909
|
-
server.on("close", () => resolveExit(typeof process.exitCode === "number" ? process.exitCode : 0));
|
|
2910
|
-
});
|
|
2911
|
-
}
|
|
2912
|
-
/** the addresses this bind actually answers on. `0.0.0.0` means every interface, so printing
|
|
2913
|
-
* only that back is useless: the LAN ip and the tailnet 100.x are the urls a phone can open */
|
|
2914
|
-
function reachableAddresses(bind, nics) {
|
|
2915
|
-
if (bind !== "0.0.0.0" && bind !== "::") return [bind];
|
|
2916
|
-
const found = [];
|
|
2917
|
-
for (const list of Object.values(nics)) for (const nic of list ?? []) {
|
|
2918
|
-
if (nic.internal || nic.family !== "IPv4" && nic.family !== 4) continue;
|
|
2919
|
-
found.push(nic.address);
|
|
2920
|
-
}
|
|
2921
|
-
return ["127.0.0.1", ...found];
|
|
2922
|
-
}
|
|
2923
|
-
function printStartup(app, bind, port) {
|
|
2924
|
-
const base = `http://${bind}:${port}`;
|
|
2925
|
-
const color = colorsAvailable();
|
|
2926
|
-
const lines = renderStartupLines({
|
|
2927
|
-
bind,
|
|
2928
|
-
port,
|
|
2929
|
-
urls: reachableAddresses(bind, networkInterfaces()),
|
|
2930
|
-
color,
|
|
2931
|
-
modules: app.modules.map((mod) => {
|
|
2932
|
-
const entries = mod.dw.entries();
|
|
2933
|
-
const nameWidth = Math.max(...entries.map(([n]) => n.length), 4);
|
|
2934
|
-
return {
|
|
2935
|
-
key: mod.key,
|
|
2936
|
-
hostId: mod.dw.host.data.id,
|
|
2937
|
-
routes: app.draftsFor(mod).map((d) => `${base}/generate/${encodeURIComponent(mod.key)}/${encodeURIComponent(d)}`),
|
|
2938
|
-
varLines: entries.map(([name, varDef]) => renderDescriptorLine(name, describeVar(varDef), nameWidth, color))
|
|
2939
|
-
};
|
|
2940
|
-
})
|
|
2941
|
-
});
|
|
2942
|
-
for (const line of lines) console.log(line);
|
|
2943
|
-
}
|
|
2944
|
-
//#endregion
|
|
2945
|
-
export { runServe };
|
|
2518
|
+
export { describeVar as n, renderDescriptorLine as r, ServeApp as t };
|
|
2946
2519
|
|
|
2947
|
-
//# sourceMappingURL=
|
|
2520
|
+
//# sourceMappingURL=ServeApp-qXcQfdgT.js.map
|