comfyui-node 1.6.2 → 1.6.4
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 +50 -0
- package/dist/.tsbuildinfo +1 -1
- package/dist/index.d.ts +18 -13
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +10 -7
- package/dist/index.js.map +1 -1
- package/dist/multipool/client-registry.d.ts +23 -32
- package/dist/multipool/client-registry.d.ts.map +1 -1
- package/dist/multipool/client-registry.js +152 -152
- package/dist/multipool/client-registry.js.map +1 -1
- package/dist/multipool/helpers.js +52 -52
- package/dist/multipool/helpers.js.map +1 -1
- package/dist/multipool/index.js +2 -2
- package/dist/multipool/interfaces.d.ts +135 -12
- package/dist/multipool/interfaces.d.ts.map +1 -1
- package/dist/multipool/interfaces.js +1 -1
- package/dist/multipool/job-profiler.d.ts +64 -127
- package/dist/multipool/job-profiler.d.ts.map +1 -1
- package/dist/multipool/job-profiler.js +221 -221
- package/dist/multipool/job-profiler.js.map +1 -1
- package/dist/multipool/job-queue-processor.d.ts +23 -27
- package/dist/multipool/job-queue-processor.d.ts.map +1 -1
- package/dist/multipool/job-queue-processor.js +196 -196
- package/dist/multipool/job-queue-processor.js.map +1 -1
- package/dist/multipool/job-state-registry.d.ts +42 -66
- package/dist/multipool/job-state-registry.d.ts.map +1 -1
- package/dist/multipool/job-state-registry.js +282 -282
- package/dist/multipool/job-state-registry.js.map +1 -1
- package/dist/multipool/multi-workflow-pool.d.ts +101 -42
- package/dist/multipool/multi-workflow-pool.d.ts.map +1 -1
- package/dist/multipool/multi-workflow-pool.js +424 -313
- package/dist/multipool/multi-workflow-pool.js.map +1 -1
- package/dist/multipool/pool-event-manager.d.ts +10 -10
- package/dist/multipool/pool-event-manager.d.ts.map +1 -1
- package/dist/multipool/pool-event-manager.js +27 -27
- package/dist/multipool/tests/client-registry-api-demo.d.ts +7 -0
- package/dist/multipool/tests/client-registry-api-demo.d.ts.map +1 -0
- package/dist/multipool/tests/client-registry-api-demo.js +136 -0
- package/dist/multipool/tests/client-registry-api-demo.js.map +1 -0
- package/dist/multipool/tests/client-registry.spec.d.ts +2 -0
- package/dist/multipool/tests/client-registry.spec.d.ts.map +1 -0
- package/dist/multipool/tests/client-registry.spec.js +191 -0
- package/dist/multipool/tests/client-registry.spec.js.map +1 -0
- package/dist/multipool/tests/error-classification-tests.js +373 -373
- package/dist/multipool/tests/event-forwarding-demo.d.ts +7 -0
- package/dist/multipool/tests/event-forwarding-demo.d.ts.map +1 -0
- package/dist/multipool/tests/event-forwarding-demo.js +88 -0
- package/dist/multipool/tests/event-forwarding-demo.js.map +1 -0
- package/dist/multipool/tests/helpers.spec.d.ts +2 -0
- package/dist/multipool/tests/helpers.spec.d.ts.map +1 -0
- package/dist/multipool/tests/helpers.spec.js +100 -0
- package/dist/multipool/tests/helpers.spec.js.map +1 -0
- package/dist/multipool/tests/job-queue-processor.spec.d.ts +2 -0
- package/dist/multipool/tests/job-queue-processor.spec.d.ts.map +1 -0
- package/dist/multipool/tests/job-queue-processor.spec.js +89 -0
- package/dist/multipool/tests/job-queue-processor.spec.js.map +1 -0
- package/dist/multipool/tests/job-state-registry.spec.d.ts +2 -0
- package/dist/multipool/tests/job-state-registry.spec.d.ts.map +1 -0
- package/dist/multipool/tests/job-state-registry.spec.js +143 -0
- package/dist/multipool/tests/job-state-registry.spec.js.map +1 -0
- package/dist/multipool/tests/multipool-basic.js +141 -141
- package/dist/multipool/tests/profiling-demo.js +87 -87
- package/dist/multipool/tests/profiling-demo.js.map +1 -1
- package/dist/multipool/tests/two-stage-edit-simulation.js +298 -298
- package/dist/multipool/tests/two-stage-edit-simulation.js.map +1 -1
- package/dist/multipool/workflow.d.ts +178 -178
- package/dist/multipool/workflow.d.ts.map +1 -1
- package/dist/multipool/workflow.js +333 -333
- package/package.json +1 -1
|
@@ -1,334 +1,334 @@
|
|
|
1
|
-
import { hashWorkflow } from "../pool/utils/hash.js";
|
|
2
|
-
class TinyEmitter {
|
|
3
|
-
listeners = new Map();
|
|
4
|
-
on(evt, fn) {
|
|
5
|
-
if (!this.listeners.has(evt))
|
|
6
|
-
this.listeners.set(evt, new Set());
|
|
7
|
-
this.listeners.get(evt).add(fn);
|
|
8
|
-
return () => this.off(evt, fn);
|
|
9
|
-
}
|
|
10
|
-
off(evt, fn) {
|
|
11
|
-
this.listeners.get(evt)?.delete(fn);
|
|
12
|
-
}
|
|
13
|
-
emit(evt, ...args) {
|
|
14
|
-
this.listeners.get(evt)?.forEach(fn => {
|
|
15
|
-
try {
|
|
16
|
-
fn(...args);
|
|
17
|
-
}
|
|
18
|
-
catch {
|
|
19
|
-
}
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
removeAll() {
|
|
23
|
-
this.listeners.clear();
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
export class WorkflowJob {
|
|
27
|
-
emitter = new TinyEmitter();
|
|
28
|
-
donePromise;
|
|
29
|
-
doneResolve;
|
|
30
|
-
doneReject;
|
|
31
|
-
lastProgressPct = -1;
|
|
32
|
-
constructor() {
|
|
33
|
-
this.donePromise = new Promise((res, rej) => {
|
|
34
|
-
this.doneResolve = res;
|
|
35
|
-
this.doneReject = rej;
|
|
36
|
-
});
|
|
37
|
-
// Prevent unhandled rejection warnings by attaching a catch handler
|
|
38
|
-
// The actual error handling happens when user calls done()
|
|
39
|
-
this.donePromise.catch(() => {
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
on(evt, fn) {
|
|
43
|
-
this.emitter.on(evt, fn);
|
|
44
|
-
return this;
|
|
45
|
-
}
|
|
46
|
-
off(evt, fn) {
|
|
47
|
-
this.emitter.off(evt, fn);
|
|
48
|
-
return this;
|
|
49
|
-
}
|
|
50
|
-
/** Await final mapped outputs */
|
|
51
|
-
done() {
|
|
52
|
-
return this.donePromise;
|
|
53
|
-
}
|
|
54
|
-
_emit(evt, ...args) {
|
|
55
|
-
this.emitter.emit(evt, ...args);
|
|
56
|
-
}
|
|
57
|
-
_finish(data) {
|
|
58
|
-
this.doneResolve(data);
|
|
59
|
-
this.emitter.emit("finished", data, data._promptId);
|
|
60
|
-
}
|
|
61
|
-
_fail(err, promptId) {
|
|
62
|
-
this.doneReject(err);
|
|
63
|
-
this.emitter.emit("failed", err, promptId);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
export class Workflow {
|
|
67
|
-
json;
|
|
68
|
-
outputNodeIds = [];
|
|
69
|
-
outputAliases = {}; // nodeId -> alias
|
|
70
|
-
bypassedNodes = []; // nodes to bypass during execution
|
|
71
|
-
// Pending assets to upload before execution
|
|
72
|
-
_pendingImageInputs = [];
|
|
73
|
-
_pendingFolderFiles = [];
|
|
74
|
-
/** Structural hash of the workflow JSON for compatibility tracking in failover scenarios */
|
|
75
|
-
structureHash;
|
|
76
|
-
static from(data, opts) {
|
|
77
|
-
if (typeof data === "string") {
|
|
78
|
-
try {
|
|
79
|
-
const parsed = JSON.parse(data);
|
|
80
|
-
return new Workflow(parsed, opts);
|
|
81
|
-
}
|
|
82
|
-
catch (e) {
|
|
83
|
-
throw new Error("Failed to parse workflow JSON string", { cause: e });
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
return new Workflow(structuredClone(data), opts);
|
|
87
|
-
}
|
|
88
|
-
constructor(json, opts) {
|
|
89
|
-
this.json = structuredClone(json);
|
|
90
|
-
// Compute structural hash by default unless explicitly disabled
|
|
91
|
-
if (opts?.autoHash !== false) {
|
|
92
|
-
this.structureHash = hashWorkflow(this.json);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* Like from(), but augments known node types (e.g., KSampler) with soft union hints
|
|
97
|
-
* for inputs such as sampler_name & scheduler while still allowing arbitrary strings.
|
|
98
|
-
*/
|
|
99
|
-
static fromAugmented(data, opts) {
|
|
100
|
-
return Workflow.from(data, opts);
|
|
101
|
-
}
|
|
102
|
-
/** Set a nested input path on a node e.g. set('9.inputs.text','hello') */
|
|
103
|
-
set(path, value) {
|
|
104
|
-
const keys = path.split(".");
|
|
105
|
-
let cur = this.json;
|
|
106
|
-
for (let i = 0; i < keys.length - 1; i++) {
|
|
107
|
-
if (cur[keys[i]] === undefined)
|
|
108
|
-
cur[keys[i]] = {};
|
|
109
|
-
cur = cur[keys[i]];
|
|
110
|
-
}
|
|
111
|
-
cur[keys[keys.length - 1]] = value;
|
|
112
|
-
return this;
|
|
113
|
-
}
|
|
114
|
-
/** Attach a single image buffer to a node input (e.g., LoadImage.image). Will upload on run() then set the input to the filename. */
|
|
115
|
-
attachImage(nodeId, inputName, data, fileName, opts) {
|
|
116
|
-
const blob = toBlob(data, fileName);
|
|
117
|
-
this._pendingImageInputs.push({
|
|
118
|
-
nodeId: String(nodeId),
|
|
119
|
-
inputName,
|
|
120
|
-
blob,
|
|
121
|
-
fileName,
|
|
122
|
-
subfolder: opts?.subfolder,
|
|
123
|
-
override: opts?.override
|
|
124
|
-
});
|
|
125
|
-
return this;
|
|
126
|
-
}
|
|
127
|
-
/** Attach multiple files into a server subfolder (useful for LoadImageSetFromFolderNode). */
|
|
128
|
-
attachFolderFiles(subfolder, files, opts) {
|
|
129
|
-
for (const f of files) {
|
|
130
|
-
const blob = toBlob(f.data, f.fileName);
|
|
131
|
-
this._pendingFolderFiles.push({ subfolder, blob, fileName: f.fileName, override: opts?.override });
|
|
132
|
-
}
|
|
133
|
-
return this;
|
|
134
|
-
}
|
|
135
|
-
/**
|
|
136
|
-
* Sugar for setting a node's input: wf.input('SAMPLER','steps',30)
|
|
137
|
-
* Equivalent to set('SAMPLER.inputs.steps', 30).
|
|
138
|
-
* Performs a light existence check to aid DX (doesn't throw if missing by design unless strict parameter is passed).
|
|
139
|
-
*/
|
|
140
|
-
input(nodeId, inputName, value, opts) {
|
|
141
|
-
const nodeKey = String(nodeId);
|
|
142
|
-
const node = this.json[nodeKey];
|
|
143
|
-
if (!node) {
|
|
144
|
-
if (opts?.strict)
|
|
145
|
-
throw new Error(`Workflow.input: node '${String(nodeId)}' not found`);
|
|
146
|
-
// create minimal node shell if non-strict (lets users build up dynamically)
|
|
147
|
-
this.json[nodeKey] = { inputs: { [inputName]: value } };
|
|
148
|
-
return this;
|
|
149
|
-
}
|
|
150
|
-
if (!node.inputs) {
|
|
151
|
-
if (opts?.strict)
|
|
152
|
-
throw new Error(`Workflow.input: node '${String(nodeId)}' missing inputs object`);
|
|
153
|
-
node.inputs = {};
|
|
154
|
-
}
|
|
155
|
-
node.inputs[inputName] = value;
|
|
156
|
-
return this;
|
|
157
|
-
}
|
|
158
|
-
batchInputs(a, b, c) {
|
|
159
|
-
// Form 1: (nodeId, values, opts)
|
|
160
|
-
if (typeof a === "string") {
|
|
161
|
-
const nodeId = a;
|
|
162
|
-
const values = b || {};
|
|
163
|
-
const opts = c || {};
|
|
164
|
-
for (const [k, v] of Object.entries(values)) {
|
|
165
|
-
this.input(nodeId, k, v, opts);
|
|
166
|
-
}
|
|
167
|
-
return this;
|
|
168
|
-
}
|
|
169
|
-
// Form 2: (batchObject, opts)
|
|
170
|
-
const batch = a || {};
|
|
171
|
-
const opts = b || {};
|
|
172
|
-
for (const [nodeId, values] of Object.entries(batch)) {
|
|
173
|
-
if (!values)
|
|
174
|
-
continue;
|
|
175
|
-
for (const [k, v] of Object.entries(values)) {
|
|
176
|
-
this.input(nodeId, k, v, opts);
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
return this;
|
|
180
|
-
}
|
|
181
|
-
/**
|
|
182
|
-
* Mark a node id whose outputs we want collected.
|
|
183
|
-
* Supports aliasing in two forms:
|
|
184
|
-
* - output('alias','9')
|
|
185
|
-
* - output('alias:9')
|
|
186
|
-
* - output('9') (no alias, raw node id key)
|
|
187
|
-
*/
|
|
188
|
-
output(a, b) {
|
|
189
|
-
let alias;
|
|
190
|
-
let nodeId;
|
|
191
|
-
if (b) {
|
|
192
|
-
// Heuristic: if first arg looks like a node id and second arg looks like an alias, swap
|
|
193
|
-
// Node ids are often numeric strings (e.g., '2'); aliases are non-numeric labels.
|
|
194
|
-
const looksLikeNodeId = (s) => /^\d+$/.test(s) || this.json[s];
|
|
195
|
-
if (looksLikeNodeId(String(a)) && !looksLikeNodeId(String(b))) {
|
|
196
|
-
nodeId = String(a);
|
|
197
|
-
alias = String(b);
|
|
198
|
-
try {
|
|
199
|
-
console.warn(`Workflow.output called as output(nodeId, alias). Interpreting as output(alias,nodeId): '${alias}:${nodeId}'`);
|
|
200
|
-
}
|
|
201
|
-
catch {
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
else {
|
|
205
|
-
alias = String(a);
|
|
206
|
-
nodeId = String(b);
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
else {
|
|
210
|
-
// single param variant: maybe "alias:node" or just node
|
|
211
|
-
if (a.includes(":")) {
|
|
212
|
-
const [al, id] = a.split(":");
|
|
213
|
-
if (al && id) {
|
|
214
|
-
alias = al;
|
|
215
|
-
nodeId = id;
|
|
216
|
-
}
|
|
217
|
-
else {
|
|
218
|
-
nodeId = a;
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
else {
|
|
222
|
-
nodeId = a;
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
if (!this.outputNodeIds.includes(nodeId))
|
|
226
|
-
this.outputNodeIds.push(nodeId);
|
|
227
|
-
if (alias) {
|
|
228
|
-
this.outputAliases[nodeId] = alias;
|
|
229
|
-
}
|
|
230
|
-
return this; // typed refinement handled via declaration merging below
|
|
231
|
-
}
|
|
232
|
-
bypass(nodes) {
|
|
233
|
-
if (!Array.isArray(nodes)) {
|
|
234
|
-
nodes = [nodes];
|
|
235
|
-
}
|
|
236
|
-
for (const node of nodes) {
|
|
237
|
-
if (!this.bypassedNodes.includes(node)) {
|
|
238
|
-
this.bypassedNodes.push(node);
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
return this;
|
|
242
|
-
}
|
|
243
|
-
reinstate(nodes) {
|
|
244
|
-
if (!Array.isArray(nodes)) {
|
|
245
|
-
nodes = [nodes];
|
|
246
|
-
}
|
|
247
|
-
for (const node of nodes) {
|
|
248
|
-
const idx = this.bypassedNodes.indexOf(node);
|
|
249
|
-
if (idx !== -1) {
|
|
250
|
-
this.bypassedNodes.splice(idx, 1);
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
return this;
|
|
254
|
-
}
|
|
255
|
-
/**
|
|
256
|
-
* Update the structural hash after making non-dynamic changes to the workflow.
|
|
257
|
-
* Call this if you modify the workflow structure after initialization and the autoHash was disabled,
|
|
258
|
-
* or if you want to recalculate the hash after making structural changes.
|
|
259
|
-
*
|
|
260
|
-
* Example:
|
|
261
|
-
* ```
|
|
262
|
-
* const wf = Workflow.from(data, { autoHash: false });
|
|
263
|
-
* wf.input('SAMPLER', 'ckpt_name', 'model_v1.safetensors');
|
|
264
|
-
* wf.updateHash(); // Recompute hash after structural change
|
|
265
|
-
* ```
|
|
266
|
-
*/
|
|
267
|
-
updateHash() {
|
|
268
|
-
this.structureHash = hashWorkflow(this.json);
|
|
269
|
-
return this;
|
|
270
|
-
}
|
|
271
|
-
/** IDE helper returning empty object typed as final result (aliases + metadata). */
|
|
272
|
-
typedResult() {
|
|
273
|
-
return {};
|
|
274
|
-
}
|
|
275
|
-
/** Get the raw workflow JSON structure. */
|
|
276
|
-
toJSON() {
|
|
277
|
-
return structuredClone(this.json);
|
|
278
|
-
}
|
|
279
|
-
/** Upload pending images to client */
|
|
280
|
-
async uploadAssets(api) {
|
|
281
|
-
// Upload any pending assets first, then patch JSON inputs
|
|
282
|
-
if (this._pendingFolderFiles.length || this._pendingImageInputs.length) {
|
|
283
|
-
// Upload folder files
|
|
284
|
-
for (const f of this._pendingFolderFiles) {
|
|
285
|
-
await api.ext.file.uploadImage(f.blob, f.fileName, { subfolder: f.subfolder, override: f.override });
|
|
286
|
-
}
|
|
287
|
-
// Upload and set single-image inputs
|
|
288
|
-
for (const it of this._pendingImageInputs) {
|
|
289
|
-
await api.ext.file.uploadImage(it.blob, it.fileName, { subfolder: it.subfolder, override: it.override });
|
|
290
|
-
// Prefer just the filename; many LoadImage nodes look up by filename (subfolder managed server-side)
|
|
291
|
-
this.input(it.nodeId, it.inputName, it.fileName);
|
|
292
|
-
}
|
|
293
|
-
// Clear pending once applied
|
|
294
|
-
this._pendingFolderFiles = [];
|
|
295
|
-
this._pendingImageInputs = [];
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
// Helper: normalize to Blob for upload
|
|
300
|
-
function toBlob(src, fileName) {
|
|
301
|
-
if (src instanceof Blob)
|
|
302
|
-
return src;
|
|
303
|
-
// Normalize everything to a plain ArrayBuffer for reliable BlobPart typing
|
|
304
|
-
let ab;
|
|
305
|
-
if (typeof Buffer !== "undefined" && src instanceof Buffer) {
|
|
306
|
-
const u8 = new Uint8Array(src);
|
|
307
|
-
ab = u8.slice(0).buffer;
|
|
308
|
-
}
|
|
309
|
-
else if (src instanceof Uint8Array) {
|
|
310
|
-
const u8 = new Uint8Array(src.byteLength);
|
|
311
|
-
u8.set(src);
|
|
312
|
-
ab = u8.buffer;
|
|
313
|
-
}
|
|
314
|
-
else if (src instanceof ArrayBuffer) {
|
|
315
|
-
ab = src;
|
|
316
|
-
}
|
|
317
|
-
else {
|
|
318
|
-
ab = new ArrayBuffer(0);
|
|
319
|
-
}
|
|
320
|
-
return new Blob([ab], { type: mimeFromName(fileName) });
|
|
321
|
-
}
|
|
322
|
-
function mimeFromName(name) {
|
|
323
|
-
if (!name)
|
|
324
|
-
return undefined;
|
|
325
|
-
const n = name.toLowerCase();
|
|
326
|
-
if (n.endsWith(".png"))
|
|
327
|
-
return "image/png";
|
|
328
|
-
if (n.endsWith(".jpg") || n.endsWith(".jpeg"))
|
|
329
|
-
return "image/jpeg";
|
|
330
|
-
if (n.endsWith(".webp"))
|
|
331
|
-
return "image/webp";
|
|
332
|
-
return undefined;
|
|
333
|
-
}
|
|
1
|
+
import { hashWorkflow } from "../pool/utils/hash.js";
|
|
2
|
+
class TinyEmitter {
|
|
3
|
+
listeners = new Map();
|
|
4
|
+
on(evt, fn) {
|
|
5
|
+
if (!this.listeners.has(evt))
|
|
6
|
+
this.listeners.set(evt, new Set());
|
|
7
|
+
this.listeners.get(evt).add(fn);
|
|
8
|
+
return () => this.off(evt, fn);
|
|
9
|
+
}
|
|
10
|
+
off(evt, fn) {
|
|
11
|
+
this.listeners.get(evt)?.delete(fn);
|
|
12
|
+
}
|
|
13
|
+
emit(evt, ...args) {
|
|
14
|
+
this.listeners.get(evt)?.forEach(fn => {
|
|
15
|
+
try {
|
|
16
|
+
fn(...args);
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
removeAll() {
|
|
23
|
+
this.listeners.clear();
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
export class WorkflowJob {
|
|
27
|
+
emitter = new TinyEmitter();
|
|
28
|
+
donePromise;
|
|
29
|
+
doneResolve;
|
|
30
|
+
doneReject;
|
|
31
|
+
lastProgressPct = -1;
|
|
32
|
+
constructor() {
|
|
33
|
+
this.donePromise = new Promise((res, rej) => {
|
|
34
|
+
this.doneResolve = res;
|
|
35
|
+
this.doneReject = rej;
|
|
36
|
+
});
|
|
37
|
+
// Prevent unhandled rejection warnings by attaching a catch handler
|
|
38
|
+
// The actual error handling happens when user calls done()
|
|
39
|
+
this.donePromise.catch(() => {
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
on(evt, fn) {
|
|
43
|
+
this.emitter.on(evt, fn);
|
|
44
|
+
return this;
|
|
45
|
+
}
|
|
46
|
+
off(evt, fn) {
|
|
47
|
+
this.emitter.off(evt, fn);
|
|
48
|
+
return this;
|
|
49
|
+
}
|
|
50
|
+
/** Await final mapped outputs */
|
|
51
|
+
done() {
|
|
52
|
+
return this.donePromise;
|
|
53
|
+
}
|
|
54
|
+
_emit(evt, ...args) {
|
|
55
|
+
this.emitter.emit(evt, ...args);
|
|
56
|
+
}
|
|
57
|
+
_finish(data) {
|
|
58
|
+
this.doneResolve(data);
|
|
59
|
+
this.emitter.emit("finished", data, data._promptId);
|
|
60
|
+
}
|
|
61
|
+
_fail(err, promptId) {
|
|
62
|
+
this.doneReject(err);
|
|
63
|
+
this.emitter.emit("failed", err, promptId);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
export class Workflow {
|
|
67
|
+
json;
|
|
68
|
+
outputNodeIds = [];
|
|
69
|
+
outputAliases = {}; // nodeId -> alias
|
|
70
|
+
bypassedNodes = []; // nodes to bypass during execution
|
|
71
|
+
// Pending assets to upload before execution
|
|
72
|
+
_pendingImageInputs = [];
|
|
73
|
+
_pendingFolderFiles = [];
|
|
74
|
+
/** Structural hash of the workflow JSON for compatibility tracking in failover scenarios */
|
|
75
|
+
structureHash;
|
|
76
|
+
static from(data, opts) {
|
|
77
|
+
if (typeof data === "string") {
|
|
78
|
+
try {
|
|
79
|
+
const parsed = JSON.parse(data);
|
|
80
|
+
return new Workflow(parsed, opts);
|
|
81
|
+
}
|
|
82
|
+
catch (e) {
|
|
83
|
+
throw new Error("Failed to parse workflow JSON string", { cause: e });
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return new Workflow(structuredClone(data), opts);
|
|
87
|
+
}
|
|
88
|
+
constructor(json, opts) {
|
|
89
|
+
this.json = structuredClone(json);
|
|
90
|
+
// Compute structural hash by default unless explicitly disabled
|
|
91
|
+
if (opts?.autoHash !== false) {
|
|
92
|
+
this.structureHash = hashWorkflow(this.json);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Like from(), but augments known node types (e.g., KSampler) with soft union hints
|
|
97
|
+
* for inputs such as sampler_name & scheduler while still allowing arbitrary strings.
|
|
98
|
+
*/
|
|
99
|
+
static fromAugmented(data, opts) {
|
|
100
|
+
return Workflow.from(data, opts);
|
|
101
|
+
}
|
|
102
|
+
/** Set a nested input path on a node e.g. set('9.inputs.text','hello') */
|
|
103
|
+
set(path, value) {
|
|
104
|
+
const keys = path.split(".");
|
|
105
|
+
let cur = this.json;
|
|
106
|
+
for (let i = 0; i < keys.length - 1; i++) {
|
|
107
|
+
if (cur[keys[i]] === undefined)
|
|
108
|
+
cur[keys[i]] = {};
|
|
109
|
+
cur = cur[keys[i]];
|
|
110
|
+
}
|
|
111
|
+
cur[keys[keys.length - 1]] = value;
|
|
112
|
+
return this;
|
|
113
|
+
}
|
|
114
|
+
/** Attach a single image buffer to a node input (e.g., LoadImage.image). Will upload on run() then set the input to the filename. */
|
|
115
|
+
attachImage(nodeId, inputName, data, fileName, opts) {
|
|
116
|
+
const blob = toBlob(data, fileName);
|
|
117
|
+
this._pendingImageInputs.push({
|
|
118
|
+
nodeId: String(nodeId),
|
|
119
|
+
inputName,
|
|
120
|
+
blob,
|
|
121
|
+
fileName,
|
|
122
|
+
subfolder: opts?.subfolder,
|
|
123
|
+
override: opts?.override
|
|
124
|
+
});
|
|
125
|
+
return this;
|
|
126
|
+
}
|
|
127
|
+
/** Attach multiple files into a server subfolder (useful for LoadImageSetFromFolderNode). */
|
|
128
|
+
attachFolderFiles(subfolder, files, opts) {
|
|
129
|
+
for (const f of files) {
|
|
130
|
+
const blob = toBlob(f.data, f.fileName);
|
|
131
|
+
this._pendingFolderFiles.push({ subfolder, blob, fileName: f.fileName, override: opts?.override });
|
|
132
|
+
}
|
|
133
|
+
return this;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Sugar for setting a node's input: wf.input('SAMPLER','steps',30)
|
|
137
|
+
* Equivalent to set('SAMPLER.inputs.steps', 30).
|
|
138
|
+
* Performs a light existence check to aid DX (doesn't throw if missing by design unless strict parameter is passed).
|
|
139
|
+
*/
|
|
140
|
+
input(nodeId, inputName, value, opts) {
|
|
141
|
+
const nodeKey = String(nodeId);
|
|
142
|
+
const node = this.json[nodeKey];
|
|
143
|
+
if (!node) {
|
|
144
|
+
if (opts?.strict)
|
|
145
|
+
throw new Error(`Workflow.input: node '${String(nodeId)}' not found`);
|
|
146
|
+
// create minimal node shell if non-strict (lets users build up dynamically)
|
|
147
|
+
this.json[nodeKey] = { inputs: { [inputName]: value } };
|
|
148
|
+
return this;
|
|
149
|
+
}
|
|
150
|
+
if (!node.inputs) {
|
|
151
|
+
if (opts?.strict)
|
|
152
|
+
throw new Error(`Workflow.input: node '${String(nodeId)}' missing inputs object`);
|
|
153
|
+
node.inputs = {};
|
|
154
|
+
}
|
|
155
|
+
node.inputs[inputName] = value;
|
|
156
|
+
return this;
|
|
157
|
+
}
|
|
158
|
+
batchInputs(a, b, c) {
|
|
159
|
+
// Form 1: (nodeId, values, opts)
|
|
160
|
+
if (typeof a === "string") {
|
|
161
|
+
const nodeId = a;
|
|
162
|
+
const values = b || {};
|
|
163
|
+
const opts = c || {};
|
|
164
|
+
for (const [k, v] of Object.entries(values)) {
|
|
165
|
+
this.input(nodeId, k, v, opts);
|
|
166
|
+
}
|
|
167
|
+
return this;
|
|
168
|
+
}
|
|
169
|
+
// Form 2: (batchObject, opts)
|
|
170
|
+
const batch = a || {};
|
|
171
|
+
const opts = b || {};
|
|
172
|
+
for (const [nodeId, values] of Object.entries(batch)) {
|
|
173
|
+
if (!values)
|
|
174
|
+
continue;
|
|
175
|
+
for (const [k, v] of Object.entries(values)) {
|
|
176
|
+
this.input(nodeId, k, v, opts);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
return this;
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Mark a node id whose outputs we want collected.
|
|
183
|
+
* Supports aliasing in two forms:
|
|
184
|
+
* - output('alias','9')
|
|
185
|
+
* - output('alias:9')
|
|
186
|
+
* - output('9') (no alias, raw node id key)
|
|
187
|
+
*/
|
|
188
|
+
output(a, b) {
|
|
189
|
+
let alias;
|
|
190
|
+
let nodeId;
|
|
191
|
+
if (b) {
|
|
192
|
+
// Heuristic: if first arg looks like a node id and second arg looks like an alias, swap
|
|
193
|
+
// Node ids are often numeric strings (e.g., '2'); aliases are non-numeric labels.
|
|
194
|
+
const looksLikeNodeId = (s) => /^\d+$/.test(s) || this.json[s];
|
|
195
|
+
if (looksLikeNodeId(String(a)) && !looksLikeNodeId(String(b))) {
|
|
196
|
+
nodeId = String(a);
|
|
197
|
+
alias = String(b);
|
|
198
|
+
try {
|
|
199
|
+
console.warn(`Workflow.output called as output(nodeId, alias). Interpreting as output(alias,nodeId): '${alias}:${nodeId}'`);
|
|
200
|
+
}
|
|
201
|
+
catch {
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
else {
|
|
205
|
+
alias = String(a);
|
|
206
|
+
nodeId = String(b);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
// single param variant: maybe "alias:node" or just node
|
|
211
|
+
if (a.includes(":")) {
|
|
212
|
+
const [al, id] = a.split(":");
|
|
213
|
+
if (al && id) {
|
|
214
|
+
alias = al;
|
|
215
|
+
nodeId = id;
|
|
216
|
+
}
|
|
217
|
+
else {
|
|
218
|
+
nodeId = a;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
else {
|
|
222
|
+
nodeId = a;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
if (!this.outputNodeIds.includes(nodeId))
|
|
226
|
+
this.outputNodeIds.push(nodeId);
|
|
227
|
+
if (alias) {
|
|
228
|
+
this.outputAliases[nodeId] = alias;
|
|
229
|
+
}
|
|
230
|
+
return this; // typed refinement handled via declaration merging below
|
|
231
|
+
}
|
|
232
|
+
bypass(nodes) {
|
|
233
|
+
if (!Array.isArray(nodes)) {
|
|
234
|
+
nodes = [nodes];
|
|
235
|
+
}
|
|
236
|
+
for (const node of nodes) {
|
|
237
|
+
if (!this.bypassedNodes.includes(node)) {
|
|
238
|
+
this.bypassedNodes.push(node);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
return this;
|
|
242
|
+
}
|
|
243
|
+
reinstate(nodes) {
|
|
244
|
+
if (!Array.isArray(nodes)) {
|
|
245
|
+
nodes = [nodes];
|
|
246
|
+
}
|
|
247
|
+
for (const node of nodes) {
|
|
248
|
+
const idx = this.bypassedNodes.indexOf(node);
|
|
249
|
+
if (idx !== -1) {
|
|
250
|
+
this.bypassedNodes.splice(idx, 1);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
return this;
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Update the structural hash after making non-dynamic changes to the workflow.
|
|
257
|
+
* Call this if you modify the workflow structure after initialization and the autoHash was disabled,
|
|
258
|
+
* or if you want to recalculate the hash after making structural changes.
|
|
259
|
+
*
|
|
260
|
+
* Example:
|
|
261
|
+
* ```
|
|
262
|
+
* const wf = Workflow.from(data, { autoHash: false });
|
|
263
|
+
* wf.input('SAMPLER', 'ckpt_name', 'model_v1.safetensors');
|
|
264
|
+
* wf.updateHash(); // Recompute hash after structural change
|
|
265
|
+
* ```
|
|
266
|
+
*/
|
|
267
|
+
updateHash() {
|
|
268
|
+
this.structureHash = hashWorkflow(this.json);
|
|
269
|
+
return this;
|
|
270
|
+
}
|
|
271
|
+
/** IDE helper returning empty object typed as final result (aliases + metadata). */
|
|
272
|
+
typedResult() {
|
|
273
|
+
return {};
|
|
274
|
+
}
|
|
275
|
+
/** Get the raw workflow JSON structure. */
|
|
276
|
+
toJSON() {
|
|
277
|
+
return structuredClone(this.json);
|
|
278
|
+
}
|
|
279
|
+
/** Upload pending images to client */
|
|
280
|
+
async uploadAssets(api) {
|
|
281
|
+
// Upload any pending assets first, then patch JSON inputs
|
|
282
|
+
if (this._pendingFolderFiles.length || this._pendingImageInputs.length) {
|
|
283
|
+
// Upload folder files
|
|
284
|
+
for (const f of this._pendingFolderFiles) {
|
|
285
|
+
await api.ext.file.uploadImage(f.blob, f.fileName, { subfolder: f.subfolder, override: f.override });
|
|
286
|
+
}
|
|
287
|
+
// Upload and set single-image inputs
|
|
288
|
+
for (const it of this._pendingImageInputs) {
|
|
289
|
+
await api.ext.file.uploadImage(it.blob, it.fileName, { subfolder: it.subfolder, override: it.override });
|
|
290
|
+
// Prefer just the filename; many LoadImage nodes look up by filename (subfolder managed server-side)
|
|
291
|
+
this.input(it.nodeId, it.inputName, it.fileName);
|
|
292
|
+
}
|
|
293
|
+
// Clear pending once applied
|
|
294
|
+
this._pendingFolderFiles = [];
|
|
295
|
+
this._pendingImageInputs = [];
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
// Helper: normalize to Blob for upload
|
|
300
|
+
function toBlob(src, fileName) {
|
|
301
|
+
if (src instanceof Blob)
|
|
302
|
+
return src;
|
|
303
|
+
// Normalize everything to a plain ArrayBuffer for reliable BlobPart typing
|
|
304
|
+
let ab;
|
|
305
|
+
if (typeof Buffer !== "undefined" && src instanceof Buffer) {
|
|
306
|
+
const u8 = new Uint8Array(src);
|
|
307
|
+
ab = u8.slice(0).buffer;
|
|
308
|
+
}
|
|
309
|
+
else if (src instanceof Uint8Array) {
|
|
310
|
+
const u8 = new Uint8Array(src.byteLength);
|
|
311
|
+
u8.set(src);
|
|
312
|
+
ab = u8.buffer;
|
|
313
|
+
}
|
|
314
|
+
else if (src instanceof ArrayBuffer) {
|
|
315
|
+
ab = src;
|
|
316
|
+
}
|
|
317
|
+
else {
|
|
318
|
+
ab = new ArrayBuffer(0);
|
|
319
|
+
}
|
|
320
|
+
return new Blob([ab], { type: mimeFromName(fileName) });
|
|
321
|
+
}
|
|
322
|
+
function mimeFromName(name) {
|
|
323
|
+
if (!name)
|
|
324
|
+
return undefined;
|
|
325
|
+
const n = name.toLowerCase();
|
|
326
|
+
if (n.endsWith(".png"))
|
|
327
|
+
return "image/png";
|
|
328
|
+
if (n.endsWith(".jpg") || n.endsWith(".jpeg"))
|
|
329
|
+
return "image/jpeg";
|
|
330
|
+
if (n.endsWith(".webp"))
|
|
331
|
+
return "image/webp";
|
|
332
|
+
return undefined;
|
|
333
|
+
}
|
|
334
334
|
//# sourceMappingURL=workflow.js.map
|