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.
Files changed (71) hide show
  1. package/README.md +23 -0
  2. package/dist/{run-serve-C78hD7t9.js → ServeApp-qXcQfdgT.js} +18 -445
  3. package/dist/ServeApp-qXcQfdgT.js.map +1 -0
  4. package/dist/cli.js +72 -9
  5. package/dist/cli.js.map +1 -1
  6. package/dist/exampleAssets-BfWzygoM.js +38 -0
  7. package/dist/exampleAssets-BfWzygoM.js.map +1 -0
  8. package/dist/{LogsSt-Ct7Le0S_.js → findDefinedWorkflow-0lG-VTEd.js} +201 -201
  9. package/dist/findDefinedWorkflow-0lG-VTEd.js.map +1 -0
  10. package/dist/index.cjs +182 -69
  11. package/dist/index.cjs.map +1 -1
  12. package/dist/index.d.cts +61 -8
  13. package/dist/index.d.ts +61 -8
  14. package/dist/index.js +182 -69
  15. package/dist/index.js.map +1 -1
  16. package/dist/run-run-tg82jUIk.js +450 -0
  17. package/dist/run-run-tg82jUIk.js.map +1 -0
  18. package/dist/run-serve-DaXzoVTS.js +464 -0
  19. package/dist/run-serve-DaXzoVTS.js.map +1 -0
  20. package/dist/{run-tui-nPETFRmk.js → run-tui-Bw7BA75d.js} +7 -38
  21. package/dist/run-tui-Bw7BA75d.js.map +1 -0
  22. package/dist/{schemaDrift-wb5XkrHV.js → schemaDrift-CbsABBuk.js} +3 -2
  23. package/dist/{schemaDrift-wb5XkrHV.js.map → schemaDrift-CbsABBuk.js.map} +1 -1
  24. package/dist/serve-web.js +56 -48
  25. package/dist/{state-BMW57_M0.js → state-zTow5ucI.js} +182 -70
  26. package/dist/state-zTow5ucI.js.map +1 -0
  27. package/dist/web.d.ts +61 -8
  28. package/dist/web.js +182 -69
  29. package/dist/web.js.map +1 -1
  30. package/examples/README.md +1 -0
  31. package/examples/rvion/15-tts-voice-clone.cflow.ts +8 -8
  32. package/examples/rvion/16-tts-voice-design.cflow.ts +71 -0
  33. package/guide-for-agents.md +3 -0
  34. package/package.json +2 -2
  35. package/src/cli/bunHop.ts +49 -0
  36. package/src/cli/comfy-ts-cli.ts +18 -5
  37. package/src/cli/run/findModules.ts +36 -0
  38. package/src/cli/run/flagValue.ts +61 -0
  39. package/src/cli/run/outputPaths.ts +41 -0
  40. package/src/cli/run/pickModule.ts +41 -0
  41. package/src/cli/run/run-run.ts +250 -0
  42. package/src/cli/run/runArgs.ts +61 -0
  43. package/src/cli/run/sidecar.ts +27 -0
  44. package/src/cli/serve/ServeApp.ts +13 -39
  45. package/src/cli/serve/run-serve.ts +51 -13
  46. package/src/cli/serve/web/components/App.tsx +2 -0
  47. package/src/cli/serve/web/components/DialogLayer.tsx +55 -0
  48. package/src/cli/serve/web/components/DraftTabs.tsx +16 -5
  49. package/src/cli/serve/web/components/PromptEnhancer.tsx +15 -9
  50. package/src/cli/serve/web/components/SideMenu.tsx +16 -8
  51. package/src/cli/serve/web/state/WebSt.ts +10 -1
  52. package/src/cli/serve/web/state/dialog.ts +59 -0
  53. package/src/cli/serve/web/state/draftTabs.ts +12 -0
  54. package/src/cli/serve/web/state/keptLines.ts +1 -1
  55. package/src/cli/serve/web/state/shortcutCatalog.ts +1 -0
  56. package/src/cli/serve/web/styles.ts +8 -0
  57. package/src/coreExports.ts +2 -0
  58. package/src/host/ComfyHost.ts +73 -40
  59. package/src/host/previewMethod.ts +29 -0
  60. package/src/litegraph/convertFlowToLiteGraphJSON.ts +38 -20
  61. package/src/promptEnhancers.ts +1 -1
  62. package/src/runner/ComfyExecution.ts +45 -1
  63. package/src/runner/ComfyWorkflow.ts +6 -1
  64. package/src/runner/ComfyWsApi.ts +1 -1
  65. package/src/runner/wsBinaryFrame.ts +48 -25
  66. package/src/vars/ComfyVars.ts +3 -0
  67. package/src/vars/DefinedWorkflow.ts +1 -0
  68. package/dist/LogsSt-Ct7Le0S_.js.map +0 -1
  69. package/dist/run-serve-C78hD7t9.js.map +0 -1
  70. package/dist/run-tui-nPETFRmk.js.map +0 -1
  71. package/dist/state-BMW57_M0.js.map +0 -1
package/dist/index.js CHANGED
@@ -110,7 +110,7 @@ function createNodeStorage() {
110
110
  }
111
111
  //#endregion
112
112
  //#region package.json
113
- var version = "2.16.0";
113
+ var version = "2.17.1";
114
114
  //#endregion
115
115
  //#region src/litegraph/LiteGraphLinkID.ts
116
116
  const asLiteGraphLinkID = (id) => id;
@@ -660,21 +660,41 @@ const normalizeGroup = (g) => {
660
660
  };
661
661
  };
662
662
  //#endregion
663
- //#region src/runner/wsBinaryFrame.ts
664
- function parseBinaryWsFrame(data) {
665
- const view = new DataView(data);
666
- const eventType = view.getUint32(0);
667
- if (eventType === 1) {
668
- const imageType = view.getUint32(4);
669
- return {
670
- kind: "preview",
671
- bytes: new Uint8Array(data.slice(8)),
672
- mime: imageType === 2 ? "image/png" : "image/jpeg",
673
- metadata: null
674
- };
663
+ //#region src/host/previewMethod.ts
664
+ function isPreviewMethod(x) {
665
+ return x === "none" || x === "auto" || x === "latent2rgb" || x === "taesd";
666
+ }
667
+ /** the preview method a host was launched with, from its /system_stats payload. `'none'` when
668
+ * the flag is absent (ComfyUI's default), null when the payload carries no argv to read (a cloud
669
+ * host) or the flag holds a value this parser does not know */
670
+ function previewMethodFromSystemStats(stats) {
671
+ if (stats == null || typeof stats !== "object" || !("system" in stats)) return null;
672
+ const system = stats.system;
673
+ if (system == null || typeof system !== "object" || !("argv" in system)) return null;
674
+ const argv = system.argv;
675
+ if (!Array.isArray(argv)) return null;
676
+ for (let i = 0; i < argv.length; i++) {
677
+ const arg = argv[i];
678
+ if (typeof arg !== "string") continue;
679
+ const value = arg === "--preview-method" ? argv[i + 1] : arg.startsWith("--preview-method=") ? arg.slice(17) : void 0;
680
+ if (value === void 0) continue;
681
+ return isPreviewMethod(value) ? value : null;
675
682
  }
683
+ return "none";
684
+ }
685
+ /** the frame's event type, read in place: deciding whether anyone wants a frame costs no copy */
686
+ function binaryFrameType(data) {
687
+ if (data.byteLength < 4) return -1;
688
+ return new DataView(data).getUint32(0);
689
+ }
690
+ function isPreviewFrameType(eventType) {
691
+ return eventType === 1 || eventType === 4;
692
+ }
693
+ function parseBinaryWsFrame(data) {
694
+ const eventType = binaryFrameType(data);
695
+ if (isPreviewFrameType(eventType)) return parsePreviewFrame(data);
676
696
  if (eventType === 3) {
677
- const nodeIdLen = view.getUint32(4);
697
+ const nodeIdLen = new DataView(data).getUint32(4);
678
698
  const decoder = new TextDecoder();
679
699
  return {
680
700
  kind: "text",
@@ -682,25 +702,41 @@ function parseBinaryWsFrame(data) {
682
702
  text: decoder.decode(data.slice(8 + nodeIdLen))
683
703
  };
684
704
  }
685
- if (eventType === 4) {
686
- const metadataLen = view.getUint32(4);
687
- let metadata = null;
688
- try {
689
- metadata = JSON.parse(new TextDecoder().decode(data.slice(8, 8 + metadataLen)));
690
- } catch {}
691
- const bytes = new Uint8Array(data.slice(8 + metadataLen));
705
+ return {
706
+ kind: "unknown",
707
+ eventType
708
+ };
709
+ }
710
+ /** a type 1 or type 4 frame; the caller checked the type with `isPreviewFrameType` */
711
+ function parsePreviewFrame(data) {
712
+ const view = new DataView(data);
713
+ if (view.getUint32(0) === 1) {
714
+ const imageType = view.getUint32(4);
692
715
  return {
693
716
  kind: "preview",
694
- bytes,
695
- mime: bytes[0] === 137 && bytes[1] === 80 ? "image/png" : "image/jpeg",
696
- metadata
717
+ bytes: new Uint8Array(data.slice(8)),
718
+ mime: imageType === 2 ? "image/png" : "image/jpeg",
719
+ metadata: null
697
720
  };
698
721
  }
722
+ const metadataLen = view.getUint32(4);
723
+ let metadata = null;
724
+ try {
725
+ metadata = JSON.parse(new TextDecoder().decode(data.slice(8, 8 + metadataLen)));
726
+ } catch {}
727
+ const bytes = new Uint8Array(data.slice(8 + metadataLen));
699
728
  return {
700
- kind: "unknown",
701
- eventType
729
+ kind: "preview",
730
+ bytes,
731
+ mime: bytes[0] === 137 && bytes[1] === 80 ? "image/png" : "image/jpeg",
732
+ metadata
702
733
  };
703
734
  }
735
+ /** the node a type 4 frame names (`node_id` in its metadata), null when absent or malformed */
736
+ function previewMetadataNodeId(metadata) {
737
+ if (metadata == null || typeof metadata !== "object" || !("node_id" in metadata)) return null;
738
+ return typeof metadata.node_id === "string" && metadata.node_id !== "" ? metadata.node_id : null;
739
+ }
704
740
  //#endregion
705
741
  //#region src/sdk-generator/Primitives.ts
706
742
  const ComfyPrimitiveMapping = {
@@ -1677,20 +1713,40 @@ const convertFlowToLiteGraphJSON = (graph) => {
1677
1713
  const convertNodeToLiteGraphNode = (ctx, node) => {
1678
1714
  const inputs = [];
1679
1715
  const widgets_values = [];
1716
+ const pushLinked = (name, type, link, shape) => {
1717
+ const nodeUidNumber = bang(ctx.graph.nodes.find((n) => n.uid === link[0])?.uidNumber, `link target node "${link[0]}" not found in graph`);
1718
+ const linkId = ctx.allocateLink(nodeUidNumber, link[1], node.uidNumber, asLiteGraphSlotIndex(inputs.length), type);
1719
+ inputs.push(shape == null ? {
1720
+ name,
1721
+ type,
1722
+ link: linkId
1723
+ } : {
1724
+ name,
1725
+ type,
1726
+ shape,
1727
+ link: linkId
1728
+ });
1729
+ };
1680
1730
  for (const ipt of node.$schema.inputs) {
1681
- const raw = node.serializeValue(ipt.nameInComfy, node.json.inputs[ipt.nameInComfy]);
1682
1731
  const kind = classifySchemaInput(ipt);
1683
- if (_isLink(raw)) {
1684
- const nodeUidNumber = bang(ctx.graph.nodes.find((n) => n.uid === raw[0])?.uidNumber, `link target node "${raw[0]}" not found in graph`);
1685
- inputs.push({
1686
- name: ipt.nameInComfy,
1687
- type: ipt.typeName,
1688
- link: ctx.allocateLink(nodeUidNumber, raw[1], node.uidNumber, asLiteGraphSlotIndex(inputs.length), ipt.typeName)
1689
- });
1690
- } else if (kind.kind === "widget") {
1732
+ if (kind.kind === "dynamic-container") {
1733
+ const type = containerInstanceSlotType(ipt.opts) ?? "*";
1734
+ for (const instance of kind.instanceNames) {
1735
+ const name = `${ipt.nameInComfy}.${instance}`;
1736
+ const value = node.json.inputs[name];
1737
+ if (value == null) continue;
1738
+ const raw = node.serializeValue(name, value);
1739
+ if (!_isLink(raw)) throw new Error(`🔴 [toWorkflowJson] ${node.$schema.nameInComfy}#${node.uid} "${name}" holds a literal; an autogrow instance is a link slot in the editor`);
1740
+ pushLinked(name, type, raw, 7);
1741
+ }
1742
+ continue;
1743
+ }
1744
+ const raw = node.serializeValue(ipt.nameInComfy, node.json.inputs[ipt.nameInComfy]);
1745
+ if (_isLink(raw)) pushLinked(ipt.nameInComfy, ipt.typeName, raw);
1746
+ else if (kind.kind === "widget") {
1691
1747
  widgets_values.push(...kind.prefixValues, raw);
1692
1748
  const phantoms = kind.consumes - 1 - kind.prefixValues.length;
1693
- for (let i = 0; i < phantoms; i++) widgets_values.push(false);
1749
+ for (let i = 0; i < phantoms; i++) widgets_values.push(controlMode(ipt.opts));
1694
1750
  } else inputs.push({
1695
1751
  name: ipt.nameInComfy,
1696
1752
  type: ipt.typeName,
@@ -1713,6 +1769,12 @@ const convertNodeToLiteGraphNode = (ctx, node) => {
1713
1769
  widgets_values
1714
1770
  };
1715
1771
  };
1772
+ /** the editor's control widget is a combo (fixed, increment, decrement, randomize):
1773
+ * a config naming a mode keeps it, anything else is `fixed`, the seed the code pinned */
1774
+ const controlMode = (opts) => {
1775
+ const mode = typeof opts === "object" && opts != null ? Reflect.get(opts, "control_after_generate") : null;
1776
+ return typeof mode === "string" ? mode : "fixed";
1777
+ };
1716
1778
  const _isLink = (v) => {
1717
1779
  return Array.isArray(v) && v.length === 2 && typeof v[0] === "string" && typeof v[1] === "number";
1718
1780
  };
@@ -2350,6 +2412,7 @@ var ComfyExecution = class {
2350
2412
  this.ephemeral = false;
2351
2413
  this.snapshot = null;
2352
2414
  this.onProgress = null;
2415
+ this.onPreview = null;
2353
2416
  this.logProgress = false;
2354
2417
  this.startedAt = Date.now();
2355
2418
  this._lastLoggedLine = "";
@@ -2363,6 +2426,7 @@ var ComfyExecution = class {
2363
2426
  bytes: msg.bytes,
2364
2427
  mime: msg.mime
2365
2428
  });
2429
+ else this.onPreviewFrame(msg);
2366
2430
  return;
2367
2431
  }
2368
2432
  if (msg.type === "execution_start") return;
@@ -2609,6 +2673,7 @@ var ComfyExecution = class {
2609
2673
  this.scrubHistory = init.scrubHistory ?? false;
2610
2674
  this.ephemeral = init.ephemeral ?? false;
2611
2675
  this.onProgress = init.onProgress ?? null;
2676
+ this.onPreview = init.onPreview ?? null;
2612
2677
  this.logProgress = init.logProgress ?? false;
2613
2678
  this.host.executions.set(data.id, this);
2614
2679
  this.onCreate();
@@ -2689,6 +2754,28 @@ var ComfyExecution = class {
2689
2754
  if (uid == null) return false;
2690
2755
  return (this.snapshot?.apiJson ?? this.workflow.apiJson)[uid]?.class_type === "SaveImageWebsocket";
2691
2756
  }
2757
+ /** @internal a live preview frame of this run; never an output (the host routes ws saver frames
2758
+ * to onWsImageOutput before they get here) */
2759
+ onPreviewFrame(p) {
2760
+ const listener = this.onPreview;
2761
+ if (listener == null) return;
2762
+ const current = this.workflow.currentExecutingNode;
2763
+ const nodeId = previewMetadataNodeId(p.metadata) ?? current?.uid ?? null;
2764
+ const apiJson = this.snapshot?.apiJson ?? this.workflow.apiJson;
2765
+ const counter = current != null && current.uid === nodeId ? current.progress : null;
2766
+ listener({
2767
+ promptId: this.data.id,
2768
+ bytes: p.bytes,
2769
+ mime: p.mime,
2770
+ nodeId,
2771
+ nodeName: nodeId != null ? apiJson[nodeId]?.class_type ?? null : null,
2772
+ step: counter != null ? {
2773
+ value: counter.value,
2774
+ max: counter.max
2775
+ } : null,
2776
+ metadata: p.metadata
2777
+ });
2778
+ }
2692
2779
  /** @internal a live-text frame for one node */
2693
2780
  onProgressText(p) {
2694
2781
  this.progressTexts.set(p.nodeId, p.text);
@@ -3028,6 +3115,7 @@ var ComfyWorkflow = class {
3028
3115
  ephemeral: p.ephemeral ?? false,
3029
3116
  scrubHistory: p.scrubHistory ?? p.ephemeral ?? false,
3030
3117
  onProgress: p.onProgress,
3118
+ onPreview: p.onPreview,
3031
3119
  logProgress: p.log ?? false
3032
3120
  });
3033
3121
  };
@@ -5699,7 +5787,8 @@ var DefinedWorkflow = class {
5699
5787
  scrubHistory: settings.scrubHistory,
5700
5788
  idMode: settings.idMode,
5701
5789
  log: settings.log,
5702
- onProgress: settings.onProgress
5790
+ onProgress: settings.onProgress,
5791
+ onPreview: settings.onPreview
5703
5792
  });
5704
5793
  this.lastExecution = execution;
5705
5794
  return execution;
@@ -6004,6 +6093,11 @@ var ComfyHost = class {
6004
6093
  this.clearHistory = () => {
6005
6094
  return this.postJSON_("/history", { clear: true });
6006
6095
  };
6096
+ this.fetchPreviewMethod = async () => {
6097
+ const res = await this.fetch("/system_stats", {});
6098
+ if (!res.ok) throw new Error(`GET /system_stats failed: ${res.status}`);
6099
+ return previewMethodFromSystemStats(await res.json());
6100
+ };
6007
6101
  this.fetchRawLogs = async () => {
6008
6102
  const res = await this.fetch("/internal/logs/raw", {}, { apiPrefix: false });
6009
6103
  if (!res.ok) throw new Error(`GET /internal/logs/raw failed: ${res.status}`);
@@ -6080,7 +6174,8 @@ var ComfyHost = class {
6080
6174
  this.executions = /* @__PURE__ */ new Map();
6081
6175
  this._pendingMsgs = /* @__PURE__ */ new Map();
6082
6176
  this.activePromptID = null;
6083
- this.latentPreview = null;
6177
+ this._latentFrame = null;
6178
+ this._latentPreview = null;
6084
6179
  this.onLatentPreview = null;
6085
6180
  this.onProgressText = null;
6086
6181
  this.onStatus = null;
@@ -6257,42 +6352,60 @@ var ComfyHost = class {
6257
6352
  }
6258
6353
  prompt.onPromptRelatedMessage(msg);
6259
6354
  }
6355
+ /** the latent image beeing generated, in case you want to display it. Built on read: a
6356
+ * session that never reads it never decodes a frame nor holds an object url */
6357
+ get latentPreview() {
6358
+ if (this._latentPreview != null || this._latentFrame == null) return this._latentPreview;
6359
+ const frame = parsePreviewFrame(this._latentFrame.data);
6360
+ const blob = new Blob([frame.bytes], { type: frame.mime });
6361
+ this._latentPreview = {
6362
+ blob,
6363
+ url: URL.createObjectURL(blob),
6364
+ receivedAt: this._latentFrame.receivedAt,
6365
+ promptID: this._latentFrame.promptID
6366
+ };
6367
+ return this._latentPreview;
6368
+ }
6369
+ /** a type 1 or 4 frame. Parsed only when someone wants it, in this order (architecture.md,
6370
+ * Live previews): the ws saver's output, the pre-registration buffer, the run's onPreview,
6371
+ * the host hook. Nobody listening = the frame is kept by reference and never copied */
6372
+ onPreviewFrame(data) {
6373
+ let parsed = null;
6374
+ const frame = () => parsed ??= parsePreviewFrame(data);
6375
+ const promptID = this.activePromptID;
6376
+ const activeExec = promptID != null ? this.executions.get(promptID) : null;
6377
+ if (activeExec?.wsOutputNodeExecuting) return activeExec.onWsImageOutput({
6378
+ bytes: frame().bytes,
6379
+ mime: frame().mime
6380
+ });
6381
+ const pending = promptID != null ? this._pendingMsgs.get(promptID) : void 0;
6382
+ if (pending != null) pending.push({
6383
+ type: "binary_preview_frame",
6384
+ bytes: frame().bytes,
6385
+ mime: frame().mime,
6386
+ metadata: frame().metadata
6387
+ });
6388
+ if (this._latentPreview != null) URL.revokeObjectURL(this._latentPreview.url);
6389
+ this._latentPreview = null;
6390
+ this._latentFrame = {
6391
+ data,
6392
+ promptID,
6393
+ receivedAt: Date.now()
6394
+ };
6395
+ if (activeExec?.onPreview != null) activeExec.onPreviewFrame(frame());
6396
+ if (this.onLatentPreview != null) this.onLatentPreview({
6397
+ bytes: frame().bytes,
6398
+ mime: frame().mime,
6399
+ promptID
6400
+ });
6401
+ }
6260
6402
  onMessage(e) {
6261
6403
  this.lastWsMessageAt = Date.now();
6262
6404
  if (this.data.onWsMessageAny) this.data.onWsMessageAny(e);
6263
6405
  if (e.data instanceof ArrayBuffer) {
6406
+ if (isPreviewFrameType(binaryFrameType(e.data))) return this.onPreviewFrame(e.data);
6264
6407
  const frame = parseBinaryWsFrame(e.data);
6265
- if (frame.kind === "preview") {
6266
- const activeExec = this.activePromptID != null ? this.executions.get(this.activePromptID) : null;
6267
- if (activeExec?.wsOutputNodeExecuting) {
6268
- activeExec.onWsImageOutput({
6269
- bytes: frame.bytes,
6270
- mime: frame.mime
6271
- });
6272
- return;
6273
- }
6274
- if (this.activePromptID != null) {
6275
- const pending = this._pendingMsgs.get(this.activePromptID);
6276
- if (pending != null) pending.push({
6277
- type: "binary_preview_frame",
6278
- bytes: frame.bytes,
6279
- mime: frame.mime
6280
- });
6281
- }
6282
- if (this.latentPreview != null) URL.revokeObjectURL(this.latentPreview.url);
6283
- const imageBlob = new Blob([frame.bytes], { type: frame.mime });
6284
- this.latentPreview = {
6285
- blob: imageBlob,
6286
- url: URL.createObjectURL(imageBlob),
6287
- receivedAt: Date.now(),
6288
- promptID: this.activePromptID
6289
- };
6290
- this.onLatentPreview?.({
6291
- bytes: frame.bytes,
6292
- mime: frame.mime,
6293
- promptID: this.activePromptID
6294
- });
6295
- } else if (frame.kind === "unknown" && !this._unknownBinaryFrameTypes.has(frame.eventType)) {
6408
+ if (frame.kind === "unknown" && !this._unknownBinaryFrameTypes.has(frame.eventType)) {
6296
6409
  this._unknownBinaryFrameTypes.add(frame.eventType);
6297
6410
  console.error(`🔴 unknown binary ws frame type ${frame.eventType} from host ${this.data.id} — ignored`);
6298
6411
  }