comfy-ts 2.20.1 → 2.21.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.
Files changed (31) hide show
  1. package/README.md +21 -3
  2. package/dist/{ServeApp-B469W26E.js → ServeApp-ByMobrWW.js} +3 -3
  3. package/dist/{ServeApp-B469W26E.js.map → ServeApp-ByMobrWW.js.map} +1 -1
  4. package/dist/cli.js +5 -5
  5. package/dist/{exampleAssets-8MycEthF.js → exampleAssets-B5sMqNia.js} +58 -3
  6. package/dist/exampleAssets-B5sMqNia.js.map +1 -0
  7. package/dist/{findDefinedWorkflow-B90-xFxX.js → findDefinedWorkflow-BNZgYIww.js} +2 -2
  8. package/dist/{findDefinedWorkflow-B90-xFxX.js.map → findDefinedWorkflow-BNZgYIww.js.map} +1 -1
  9. package/dist/index.cjs +87 -19
  10. package/dist/index.cjs.map +1 -1
  11. package/dist/index.d.cts +19 -3
  12. package/dist/index.d.ts +19 -3
  13. package/dist/index.js +87 -19
  14. package/dist/index.js.map +1 -1
  15. package/dist/{run-run-BslhZ0Qm.js → run-run-CuBSygku.js} +5 -5
  16. package/dist/{run-run-BslhZ0Qm.js.map → run-run-CuBSygku.js.map} +1 -1
  17. package/dist/{run-serve-CsEYPGm9.js → run-serve-COk_G0Qz.js} +4 -4
  18. package/dist/{run-serve-CsEYPGm9.js.map → run-serve-COk_G0Qz.js.map} +1 -1
  19. package/dist/{run-tui-Cxa8ksav.js → run-tui-CB6i_x2y.js} +4 -4
  20. package/dist/{run-tui-Cxa8ksav.js.map → run-tui-CB6i_x2y.js.map} +1 -1
  21. package/dist/{schemaDrift-DaWsE-mt.js → schemaDrift-CHDVYlaj.js} +32 -19
  22. package/dist/{schemaDrift-DaWsE-mt.js.map → schemaDrift-CHDVYlaj.js.map} +1 -1
  23. package/dist/web.d.ts +19 -3
  24. package/dist/web.js +87 -19
  25. package/dist/web.js.map +1 -1
  26. package/package.json +2 -2
  27. package/src/host/ComfyHost.ts +21 -0
  28. package/src/host/leastBusy.ts +37 -0
  29. package/src/state.ts +6 -0
  30. package/src/vars/DefinedWorkflow.ts +38 -21
  31. package/dist/exampleAssets-8MycEthF.js.map +0 -1
package/dist/index.d.cts CHANGED
@@ -17588,17 +17588,22 @@ declare class DefinedWorkflow<ID extends string = string, V extends VarsSpec = V
17588
17588
  * the graph is built against (TUI host override): node/model availability
17589
17589
  * there surfaces as workflow.problems / server validation, by design.
17590
17590
  * `p.dry` builds a graph only to read it: nothing is uploaded, lastWorkflow is left alone.
17591
+ * `p.values` builds from a snapshot taken earlier (run() takes it at the call).
17591
17592
  */
17592
17593
  build(p?: {
17593
17594
  advance?: boolean;
17594
- host?: ComfyHost<ID>;
17595
+ host?: ComfyHost;
17595
17596
  dry?: boolean;
17597
+ values?: VarValues<V>;
17596
17598
  }): Promise<ComfyWorkflow<ID>>;
17597
- /** build a fresh graph from the current var values, send it, wait for outputs.
17599
+ /** the current var values; `advance` then moves each var to its next run (the seed) */
17600
+ private snapshot;
17601
+ /** build a fresh graph from the var values AT THE CALL, send it, wait for outputs:
17602
+ * set a var, call run(), set it again, and each run keeps its own values.
17598
17603
  * connects first (idempotent) — modules can import offline from the schema cache.
17599
17604
  * `settings.host` runs against THAT host instead of the defining one. */
17600
17605
  run(settings?: RunSettings & {
17601
- host?: ComfyHost<ID>;
17606
+ host?: ComfyHost;
17602
17607
  }): Promise<ComfyExecution>;
17603
17608
  }
17604
17609
  //#endregion
@@ -17847,6 +17852,13 @@ declare class ComfyHost<ID extends string = string> {
17847
17852
  * `'none'` (ComfyUI's default) means no live preview frame will ever arrive; null = the host
17848
17853
  * does not publish its launch flags (a cloud host), so only a run can tell */
17849
17854
  fetchPreviewMethod: () => Promise<PreviewMethod | null>;
17855
+ /** jobs waiting or running on this host, every client counted (`GET /prompt`) */
17856
+ fetchQueueSize: (p?: {
17857
+ timeoutMs?: number;
17858
+ }) => Promise<number>;
17859
+ /** runs started through DefinedWorkflow.run() and not finished yet: counted from the call,
17860
+ * before the prompt reaches the server queue, so runs started together spread out */
17861
+ runsInFlight: number;
17850
17862
  /** the server's recent console buffer (entries are write CHUNKS, not lines) */
17851
17863
  fetchRawLogs: () => Promise<{
17852
17864
  entries: LogEntry[];
@@ -18488,6 +18500,10 @@ declare class ComfyTS {
18488
18500
  host<const ID extends string>(data: ComfyHostData & {
18489
18501
  id: ID;
18490
18502
  }): ComfyHost<ID>;
18503
+ /** the host with the shortest queue, for `wf.run({ host: await comfy.leastBusy([a, b]) })` */
18504
+ leastBusy<H extends ComfyHost<string>>(hosts: readonly H[], p?: {
18505
+ timeoutMs?: number;
18506
+ }): Promise<H>;
18491
18507
  /**
18492
18508
  * A ComfyRegistry instance that provides access to all
18493
18509
  * known Comfy nodes, models, plugins, ...
package/dist/index.d.ts CHANGED
@@ -17588,17 +17588,22 @@ declare class DefinedWorkflow<ID extends string = string, V extends VarsSpec = V
17588
17588
  * the graph is built against (TUI host override): node/model availability
17589
17589
  * there surfaces as workflow.problems / server validation, by design.
17590
17590
  * `p.dry` builds a graph only to read it: nothing is uploaded, lastWorkflow is left alone.
17591
+ * `p.values` builds from a snapshot taken earlier (run() takes it at the call).
17591
17592
  */
17592
17593
  build(p?: {
17593
17594
  advance?: boolean;
17594
- host?: ComfyHost<ID>;
17595
+ host?: ComfyHost;
17595
17596
  dry?: boolean;
17597
+ values?: VarValues<V>;
17596
17598
  }): Promise<ComfyWorkflow<ID>>;
17597
- /** build a fresh graph from the current var values, send it, wait for outputs.
17599
+ /** the current var values; `advance` then moves each var to its next run (the seed) */
17600
+ private snapshot;
17601
+ /** build a fresh graph from the var values AT THE CALL, send it, wait for outputs:
17602
+ * set a var, call run(), set it again, and each run keeps its own values.
17598
17603
  * connects first (idempotent) — modules can import offline from the schema cache.
17599
17604
  * `settings.host` runs against THAT host instead of the defining one. */
17600
17605
  run(settings?: RunSettings & {
17601
- host?: ComfyHost<ID>;
17606
+ host?: ComfyHost;
17602
17607
  }): Promise<ComfyExecution>;
17603
17608
  }
17604
17609
  //#endregion
@@ -17847,6 +17852,13 @@ declare class ComfyHost<ID extends string = string> {
17847
17852
  * `'none'` (ComfyUI's default) means no live preview frame will ever arrive; null = the host
17848
17853
  * does not publish its launch flags (a cloud host), so only a run can tell */
17849
17854
  fetchPreviewMethod: () => Promise<PreviewMethod | null>;
17855
+ /** jobs waiting or running on this host, every client counted (`GET /prompt`) */
17856
+ fetchQueueSize: (p?: {
17857
+ timeoutMs?: number;
17858
+ }) => Promise<number>;
17859
+ /** runs started through DefinedWorkflow.run() and not finished yet: counted from the call,
17860
+ * before the prompt reaches the server queue, so runs started together spread out */
17861
+ runsInFlight: number;
17850
17862
  /** the server's recent console buffer (entries are write CHUNKS, not lines) */
17851
17863
  fetchRawLogs: () => Promise<{
17852
17864
  entries: LogEntry[];
@@ -18488,6 +18500,10 @@ declare class ComfyTS {
18488
18500
  host<const ID extends string>(data: ComfyHostData & {
18489
18501
  id: ID;
18490
18502
  }): ComfyHost<ID>;
18503
+ /** the host with the shortest queue, for `wf.run({ host: await comfy.leastBusy([a, b]) })` */
18504
+ leastBusy<H extends ComfyHost<string>>(hosts: readonly H[], p?: {
18505
+ timeoutMs?: number;
18506
+ }): Promise<H>;
18491
18507
  /**
18492
18508
  * A ComfyRegistry instance that provides access to all
18493
18509
  * known Comfy nodes, models, plugins, ...
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.20.1";
113
+ var version = "2.21.0";
114
114
  //#endregion
115
115
  //#region src/litegraph/LiteGraphLinkID.ts
116
116
  const asLiteGraphLinkID = (id) => id;
@@ -6693,37 +6693,50 @@ var DefinedWorkflow = class {
6693
6693
  * the graph is built against (TUI host override): node/model availability
6694
6694
  * there surfaces as workflow.problems / server validation, by design.
6695
6695
  * `p.dry` builds a graph only to read it: nothing is uploaded, lastWorkflow is left alone.
6696
+ * `p.values` builds from a snapshot taken earlier (run() takes it at the call).
6696
6697
  */
6697
6698
  async build(p = {}) {
6698
6699
  const wf = (p.host ?? this.host).workflow({ id: this.spec.id });
6699
6700
  wf.dry = p.dry === true;
6700
- const values = varValues(this.vars);
6701
- if (p.advance) for (const varDef of Object.values(this.vars)) varDef.afterRun();
6701
+ const values = p.values ?? this.snapshot({ advance: p.advance === true });
6702
6702
  await this.spec.build(wf.builder, values, wf);
6703
6703
  if (wf.dry) return wf;
6704
6704
  this.lastWorkflow = wf;
6705
6705
  return wf;
6706
6706
  }
6707
- /** build a fresh graph from the current var values, send it, wait for outputs.
6707
+ /** the current var values; `advance` then moves each var to its next run (the seed) */
6708
+ snapshot(p) {
6709
+ const values = varValues(this.vars);
6710
+ if (p.advance) for (const varDef of Object.values(this.vars)) varDef.afterRun();
6711
+ return values;
6712
+ }
6713
+ /** build a fresh graph from the var values AT THE CALL, send it, wait for outputs:
6714
+ * set a var, call run(), set it again, and each run keeps its own values.
6708
6715
  * connects first (idempotent) — modules can import offline from the schema cache.
6709
6716
  * `settings.host` runs against THAT host instead of the defining one. */
6710
6717
  async run(settings = {}) {
6711
6718
  const host = settings.host ?? this.host;
6712
- await host.connect();
6713
- const execution = await (await this.build({
6714
- advance: true,
6715
- host
6716
- })).run({
6717
- save: settings.save,
6718
- ephemeral: settings.ephemeral,
6719
- scrubHistory: settings.scrubHistory,
6720
- idMode: settings.idMode,
6721
- log: settings.log,
6722
- onProgress: settings.onProgress,
6723
- onPreview: settings.onPreview
6724
- });
6725
- this.lastExecution = execution;
6726
- return execution;
6719
+ const values = this.snapshot({ advance: true });
6720
+ host.runsInFlight++;
6721
+ try {
6722
+ await host.connect();
6723
+ const execution = await (await this.build({
6724
+ host,
6725
+ values
6726
+ })).run({
6727
+ save: settings.save,
6728
+ ephemeral: settings.ephemeral,
6729
+ scrubHistory: settings.scrubHistory,
6730
+ idMode: settings.idMode,
6731
+ log: settings.log,
6732
+ onProgress: settings.onProgress,
6733
+ onPreview: settings.onPreview
6734
+ });
6735
+ this.lastExecution = execution;
6736
+ return execution;
6737
+ } finally {
6738
+ host.runsInFlight--;
6739
+ }
6727
6740
  }
6728
6741
  };
6729
6742
  //#endregion
@@ -7035,6 +7048,14 @@ var ComfyHost = class {
7035
7048
  if (!res.ok) throw new Error(`GET /system_stats failed: ${res.status}`);
7036
7049
  return previewMethodFromSystemStats(await res.json());
7037
7050
  };
7051
+ this.fetchQueueSize = async (p = {}) => {
7052
+ const res = await this.fetch("/prompt", { signal: AbortSignal.timeout(p.timeoutMs ?? 3e3) });
7053
+ if (!res.ok) throw new Error(`GET /prompt failed: ${res.status}`);
7054
+ const size = queueRemainingOf(await res.json());
7055
+ if (size == null) throw new Error(`GET /prompt: no exec_info.queue_remaining in the reply`);
7056
+ return size;
7057
+ };
7058
+ this.runsInFlight = 0;
7038
7059
  this.fetchRawLogs = async () => {
7039
7060
  const res = await this.fetch("/internal/logs/raw", {}, { apiPrefix: false });
7040
7061
  if (!res.ok) throw new Error(`GET /internal/logs/raw failed: ${res.status}`);
@@ -7433,6 +7454,49 @@ var ComfyHost = class {
7433
7454
  }
7434
7455
  }
7435
7456
  };
7457
+ /** `exec_info.queue_remaining` of a `GET /prompt` reply, null when the shape is not there */
7458
+ function queueRemainingOf(json) {
7459
+ if (typeof json !== "object" || json == null || !("exec_info" in json)) return null;
7460
+ const info = json.exec_info;
7461
+ if (typeof info !== "object" || info == null || !("queue_remaining" in info)) return null;
7462
+ return typeof info.queue_remaining === "number" ? info.queue_remaining : null;
7463
+ }
7464
+ //#endregion
7465
+ //#region src/host/leastBusy.ts
7466
+ /**
7467
+ * the host with the shortest queue. Score = max(server queue, our runs in flight there):
7468
+ * the server counts every client, the local count sees runs not posted yet.
7469
+ * Ties go to the first host listed; a host that does not answer is skipped, all down throws.
7470
+ */
7471
+ async function leastBusy(hosts, p = {}) {
7472
+ if (hosts.length === 0) throw new Error("leastBusy: no host given");
7473
+ const probes = await Promise.all(hosts.map(async (host) => {
7474
+ try {
7475
+ const queue = await host.fetchQueueSize({ timeoutMs: p.timeoutMs });
7476
+ return {
7477
+ host,
7478
+ score: Math.max(queue, host.runsInFlight),
7479
+ error: null
7480
+ };
7481
+ } catch (err) {
7482
+ return {
7483
+ host,
7484
+ score: Infinity,
7485
+ error: extractErrorMessage(err)
7486
+ };
7487
+ }
7488
+ }));
7489
+ let best = null;
7490
+ for (const probe of probes) {
7491
+ if (probe.error != null) {
7492
+ console.warn(`🟡 leastBusy: host '${probe.host.data.id}' skipped: ${probe.error}`);
7493
+ continue;
7494
+ }
7495
+ if (best == null || probe.score < best.score) best = probe;
7496
+ }
7497
+ if (best == null) throw new Error(`🔴 leastBusy: no host answered (${probes.map((x) => `'${x.host.data.id}': ${x.error}`).join(", ")})`);
7498
+ return best.host;
7499
+ }
7436
7500
  //#endregion
7437
7501
  //#region src/utils/downloadFile.ts
7438
7502
  async function downloadFile(url, outputPath, logPrefix = " - ") {
@@ -8049,6 +8113,10 @@ var ComfyTS = class ComfyTS {
8049
8113
  this.hosts.set(data.id, created);
8050
8114
  return created;
8051
8115
  }
8116
+ /** the host with the shortest queue, for `wf.run({ host: await comfy.leastBusy([a, b]) })` */
8117
+ leastBusy(hosts, p = {}) {
8118
+ return leastBusy(hosts, p);
8119
+ }
8052
8120
  get registry() {
8053
8121
  if (this._registry == null) this._registry = new ComfyRegistry({
8054
8122
  check: false,