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/web.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/web.js CHANGED
@@ -101,7 +101,7 @@ function getComfyStorage() {
101
101
  }
102
102
  //#endregion
103
103
  //#region package.json
104
- var version = "2.20.1";
104
+ var version = "2.21.0";
105
105
  //#endregion
106
106
  //#region src/litegraph/LiteGraphLinkID.ts
107
107
  const asLiteGraphLinkID = (id) => id;
@@ -6684,37 +6684,50 @@ var DefinedWorkflow = class {
6684
6684
  * the graph is built against (TUI host override): node/model availability
6685
6685
  * there surfaces as workflow.problems / server validation, by design.
6686
6686
  * `p.dry` builds a graph only to read it: nothing is uploaded, lastWorkflow is left alone.
6687
+ * `p.values` builds from a snapshot taken earlier (run() takes it at the call).
6687
6688
  */
6688
6689
  async build(p = {}) {
6689
6690
  const wf = (p.host ?? this.host).workflow({ id: this.spec.id });
6690
6691
  wf.dry = p.dry === true;
6691
- const values = varValues(this.vars);
6692
- if (p.advance) for (const varDef of Object.values(this.vars)) varDef.afterRun();
6692
+ const values = p.values ?? this.snapshot({ advance: p.advance === true });
6693
6693
  await this.spec.build(wf.builder, values, wf);
6694
6694
  if (wf.dry) return wf;
6695
6695
  this.lastWorkflow = wf;
6696
6696
  return wf;
6697
6697
  }
6698
- /** build a fresh graph from the current var values, send it, wait for outputs.
6698
+ /** the current var values; `advance` then moves each var to its next run (the seed) */
6699
+ snapshot(p) {
6700
+ const values = varValues(this.vars);
6701
+ if (p.advance) for (const varDef of Object.values(this.vars)) varDef.afterRun();
6702
+ return values;
6703
+ }
6704
+ /** build a fresh graph from the var values AT THE CALL, send it, wait for outputs:
6705
+ * set a var, call run(), set it again, and each run keeps its own values.
6699
6706
  * connects first (idempotent) — modules can import offline from the schema cache.
6700
6707
  * `settings.host` runs against THAT host instead of the defining one. */
6701
6708
  async run(settings = {}) {
6702
6709
  const host = settings.host ?? this.host;
6703
- await host.connect();
6704
- const execution = await (await this.build({
6705
- advance: true,
6706
- host
6707
- })).run({
6708
- save: settings.save,
6709
- ephemeral: settings.ephemeral,
6710
- scrubHistory: settings.scrubHistory,
6711
- idMode: settings.idMode,
6712
- log: settings.log,
6713
- onProgress: settings.onProgress,
6714
- onPreview: settings.onPreview
6715
- });
6716
- this.lastExecution = execution;
6717
- return execution;
6710
+ const values = this.snapshot({ advance: true });
6711
+ host.runsInFlight++;
6712
+ try {
6713
+ await host.connect();
6714
+ const execution = await (await this.build({
6715
+ host,
6716
+ values
6717
+ })).run({
6718
+ save: settings.save,
6719
+ ephemeral: settings.ephemeral,
6720
+ scrubHistory: settings.scrubHistory,
6721
+ idMode: settings.idMode,
6722
+ log: settings.log,
6723
+ onProgress: settings.onProgress,
6724
+ onPreview: settings.onPreview
6725
+ });
6726
+ this.lastExecution = execution;
6727
+ return execution;
6728
+ } finally {
6729
+ host.runsInFlight--;
6730
+ }
6718
6731
  }
6719
6732
  };
6720
6733
  //#endregion
@@ -7026,6 +7039,14 @@ var ComfyHost = class {
7026
7039
  if (!res.ok) throw new Error(`GET /system_stats failed: ${res.status}`);
7027
7040
  return previewMethodFromSystemStats(await res.json());
7028
7041
  };
7042
+ this.fetchQueueSize = async (p = {}) => {
7043
+ const res = await this.fetch("/prompt", { signal: AbortSignal.timeout(p.timeoutMs ?? 3e3) });
7044
+ if (!res.ok) throw new Error(`GET /prompt failed: ${res.status}`);
7045
+ const size = queueRemainingOf(await res.json());
7046
+ if (size == null) throw new Error(`GET /prompt: no exec_info.queue_remaining in the reply`);
7047
+ return size;
7048
+ };
7049
+ this.runsInFlight = 0;
7029
7050
  this.fetchRawLogs = async () => {
7030
7051
  const res = await this.fetch("/internal/logs/raw", {}, { apiPrefix: false });
7031
7052
  if (!res.ok) throw new Error(`GET /internal/logs/raw failed: ${res.status}`);
@@ -7424,6 +7445,49 @@ var ComfyHost = class {
7424
7445
  }
7425
7446
  }
7426
7447
  };
7448
+ /** `exec_info.queue_remaining` of a `GET /prompt` reply, null when the shape is not there */
7449
+ function queueRemainingOf(json) {
7450
+ if (typeof json !== "object" || json == null || !("exec_info" in json)) return null;
7451
+ const info = json.exec_info;
7452
+ if (typeof info !== "object" || info == null || !("queue_remaining" in info)) return null;
7453
+ return typeof info.queue_remaining === "number" ? info.queue_remaining : null;
7454
+ }
7455
+ //#endregion
7456
+ //#region src/host/leastBusy.ts
7457
+ /**
7458
+ * the host with the shortest queue. Score = max(server queue, our runs in flight there):
7459
+ * the server counts every client, the local count sees runs not posted yet.
7460
+ * Ties go to the first host listed; a host that does not answer is skipped, all down throws.
7461
+ */
7462
+ async function leastBusy(hosts, p = {}) {
7463
+ if (hosts.length === 0) throw new Error("leastBusy: no host given");
7464
+ const probes = await Promise.all(hosts.map(async (host) => {
7465
+ try {
7466
+ const queue = await host.fetchQueueSize({ timeoutMs: p.timeoutMs });
7467
+ return {
7468
+ host,
7469
+ score: Math.max(queue, host.runsInFlight),
7470
+ error: null
7471
+ };
7472
+ } catch (err) {
7473
+ return {
7474
+ host,
7475
+ score: Infinity,
7476
+ error: extractErrorMessage(err)
7477
+ };
7478
+ }
7479
+ }));
7480
+ let best = null;
7481
+ for (const probe of probes) {
7482
+ if (probe.error != null) {
7483
+ console.warn(`🟡 leastBusy: host '${probe.host.data.id}' skipped: ${probe.error}`);
7484
+ continue;
7485
+ }
7486
+ if (best == null || probe.score < best.score) best = probe;
7487
+ }
7488
+ if (best == null) throw new Error(`🔴 leastBusy: no host answered (${probes.map((x) => `'${x.host.data.id}': ${x.error}`).join(", ")})`);
7489
+ return best.host;
7490
+ }
7427
7491
  //#endregion
7428
7492
  //#region src/utils/downloadFile.ts
7429
7493
  async function downloadFile(url, outputPath, logPrefix = " - ") {
@@ -8040,6 +8104,10 @@ var ComfyTS = class ComfyTS {
8040
8104
  this.hosts.set(data.id, created);
8041
8105
  return created;
8042
8106
  }
8107
+ /** the host with the shortest queue, for `wf.run({ host: await comfy.leastBusy([a, b]) })` */
8108
+ leastBusy(hosts, p = {}) {
8109
+ return leastBusy(hosts, p);
8110
+ }
8043
8111
  get registry() {
8044
8112
  if (this._registry == null) this._registry = new ComfyRegistry({
8045
8113
  check: false,