comfy-ts 0.3.0 → 1.0.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.
- package/README.md +349 -255
- package/dist/DefinedWorkflow-O3ynII3Y.js +598 -0
- package/dist/DefinedWorkflow-O3ynII3Y.js.map +1 -0
- package/dist/cli.js +3969 -128
- package/dist/cli.js.map +1 -1
- package/dist/index.cjs +1208 -170
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +449 -115
- package/dist/index.d.ts +449 -115
- package/dist/index.js +1200 -171
- package/dist/index.js.map +1 -1
- package/dist/{run-tui-Io8Xea0L.js → run-tui-BFhba_oG.js} +744 -665
- package/dist/run-tui-BFhba_oG.js.map +1 -0
- package/examples/01-txt2img.cflow.ts +50 -0
- package/examples/02-img2img-upload.cflow.ts +66 -0
- package/examples/03-export-workflow-json.cflow.ts +56 -0
- package/examples/04-krea2-turbo-t2i.cflow.ts +125 -0
- package/examples/05-comfy-cloud.cflow.ts +67 -0
- package/examples/comfy-cloud/sdk.d.ts +122674 -0
- package/guide-for-agents.md +166 -0
- package/package.json +7 -3
- package/src/cli/comfy-ts-cli.ts +19 -2
- package/src/cli/gen.ts +21 -12
- package/src/cli/tui/components/Header.tsx +5 -0
- package/src/cli/tui/components/LogsPanel.tsx +23 -0
- package/src/cli/tui/components/PreviewPanel.tsx +52 -7
- package/src/cli/tui/components/StatusBar.tsx +10 -4
- package/src/cli/tui/components/TreePanel.tsx +15 -2
- package/src/cli/tui/components/TuiApp.tsx +26 -3
- package/src/cli/tui/discoverWorkflows.ts +79 -0
- package/src/cli/tui/protocolImagePainter.ts +54 -18
- package/src/cli/tui/run-tui.tsx +55 -24
- package/src/cli/tui/state/ExecSt.ts +4 -1
- package/src/cli/tui/state/HostSt.ts +123 -1
- package/src/cli/tui/state/LogsSt.ts +107 -0
- package/src/cli/tui/state/PreviewSt.ts +173 -23
- package/src/cli/tui/state/SettingsSt.ts +48 -21
- package/src/cli/tui/state/TreeSt.ts +30 -6
- package/src/cli/tui/state/TuiSt.ts +27 -3
- package/src/cli/tui/state/WorkflowsSt.ts +16 -1
- package/src/host/ComfyHost.ts +241 -71
- package/src/host/ComfyManager.ts +10 -9
- package/src/host/ComfyUploader.ts +1 -2
- package/src/host/ResilientWebsocket.ts +3 -1
- package/src/host/hostUrl.ts +59 -0
- package/src/host/loraManagerApi.ts +5 -4
- package/src/index.ts +20 -1
- package/src/litegraph/CanonicalWorkflow.ts +77 -0
- package/src/litegraph/LiteGraphGroup.ts +9 -7
- package/src/litegraph/LiteGraphJSON.ts +34 -13
- package/src/litegraph/LiteGraphLink.ts +32 -2
- package/src/litegraph/LiteGraphNode.ts +18 -33
- package/src/litegraph/LiteGraphNodeInput.ts +10 -8
- package/src/litegraph/LiteGraphNodeOutput.ts +2 -3
- package/src/litegraph/LiteGraphSubgraphDef.ts +74 -0
- package/src/litegraph/convertFlowToLiteGraphJSON.ts +4 -4
- package/src/litegraph/expandSubgraphs.ts +352 -0
- package/src/litegraph/normalizeWorkflow.ts +141 -0
- package/src/runner/ComfyExecution.ts +4 -5
- package/src/runner/ComfyWorkflow.ts +2 -2
- package/src/runner/ComfyWsApi.ts +12 -1
- package/src/runner/wsBinaryFrame.ts +48 -0
- package/src/sdk-generator/Primitives.ts +6 -24
- package/src/sdk-generator/comfy-api-json.ts +3 -1
- package/src/sdk-generator/inputWidgetKind.ts +179 -0
- package/src/sdk-generator/litegraphToApiRequestPayload.ts +372 -96
- package/src/ssh-host-manager/ssh-manager.ts +0 -15
- package/src/state.ts +13 -2
- package/src/utils/ansi.ts +7 -0
- package/src/utils/mojibake.ts +60 -0
- package/dist/log-CdfgILde.js +0 -10
- package/dist/log-CdfgILde.js.map +0 -1
- package/dist/run-tui-Io8Xea0L.js.map +0 -1
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
import type { CanonicalLink, CanonicalNode, CanonicalWorkflow } from 'src/litegraph/CanonicalWorkflow.ts'
|
|
2
|
+
import { asLiteGraphLinkID, type LiteGraphLinkID } from 'src/litegraph/LiteGraphLinkID.ts'
|
|
3
|
+
import { WorkflowNormalizeError } from 'src/litegraph/normalizeWorkflow.ts'
|
|
4
|
+
import { bang } from 'src/utils/bang.ts'
|
|
5
|
+
|
|
6
|
+
/** one exposed slot of a normalized subgraph definition */
|
|
7
|
+
export type CanonicalSubgraphIO = {
|
|
8
|
+
name: string
|
|
9
|
+
type: string
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** a subgraph definition normalized by the SAME per-node/per-link normalizers as the root */
|
|
13
|
+
export type CanonicalSubgraphDef = {
|
|
14
|
+
id: string // uuid, referenced by instance nodes' `type`
|
|
15
|
+
name: string
|
|
16
|
+
inputs: CanonicalSubgraphIO[]
|
|
17
|
+
outputs: CanonicalSubgraphIO[]
|
|
18
|
+
nodes: CanonicalNode[]
|
|
19
|
+
links: CanonicalLink[]
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** boundary sentinel ids inside definition link tables */
|
|
23
|
+
const SUBGRAPH_INPUT_NODE_ID = -10
|
|
24
|
+
const SUBGRAPH_OUTPUT_NODE_ID = -20
|
|
25
|
+
|
|
26
|
+
/** 48 corpus files nest defs inside defs; a self-referencing def would loop forever */
|
|
27
|
+
const MAX_EXPANSION_PASSES = 32
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* widget-kind fallback when a def input slot has no boundary target carrying a
|
|
31
|
+
* widget marker: these litegraph slot types carry widget values. LOCAL copy on
|
|
32
|
+
* purpose — the sdk-generator's seed-consumes-2 arithmetic does not apply here
|
|
33
|
+
* (the io-widget era serializes exactly one value per exposed widget slot).
|
|
34
|
+
*/
|
|
35
|
+
const PRIMITIVE_WIDGET_TYPES: ReadonlySet<string> = new Set(['INT', 'FLOAT', 'STRING', 'BOOLEAN', 'COMBO'])
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* inline every subgraph instance (node whose `type` is a definition uuid) into
|
|
39
|
+
* the root graph: fresh numeric ids, boundary links rewired, instance widget
|
|
40
|
+
* values promoted into the inner nodes' `named` overrides. Iterative so nested
|
|
41
|
+
* definitions expand on the next pass. Muted instances are dropped (their
|
|
42
|
+
* consumers resolve unconnected); bypassed instances stay for the converter's
|
|
43
|
+
* passthrough walk, which needs no interior.
|
|
44
|
+
*/
|
|
45
|
+
export const expandSubgraphs = (wf: CanonicalWorkflow, defs: Map<string, CanonicalSubgraphDef>): CanonicalWorkflow => {
|
|
46
|
+
let passes = 0
|
|
47
|
+
while (true) {
|
|
48
|
+
const expandable = wf.nodes.filter((n) => defs.has(n.type) && n.mode !== 'bypassed')
|
|
49
|
+
if (expandable.length === 0) return wf
|
|
50
|
+
if (++passes > MAX_EXPANSION_PASSES)
|
|
51
|
+
throw new WorkflowNormalizeError(
|
|
52
|
+
'subgraph-cycle',
|
|
53
|
+
`subgraph expansion did not terminate after ${MAX_EXPANSION_PASSES} passes — self-referencing definitions?`,
|
|
54
|
+
)
|
|
55
|
+
for (const instance of expandable) {
|
|
56
|
+
if (instance.mode === 'muted') dropMutedInstance(wf, instance)
|
|
57
|
+
else expandOneInstance(wf, instance, bang(defs.get(instance.type)))
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** converter mute semantics applied early: after expansion there is no single node left to skip */
|
|
63
|
+
const dropMutedInstance = (wf: CanonicalWorkflow, instance: CanonicalNode): void => {
|
|
64
|
+
const dead = new Set<LiteGraphLinkID>()
|
|
65
|
+
for (const link of wf.links) {
|
|
66
|
+
if (link.originId === instance.id || link.targetId === instance.id) dead.add(link.id)
|
|
67
|
+
}
|
|
68
|
+
wf.links = wf.links.filter((l) => !dead.has(l.id))
|
|
69
|
+
wf.nodes = wf.nodes.filter((n) => n.id !== instance.id)
|
|
70
|
+
pruneDeadLinkRefs(wf, dead)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const pruneDeadLinkRefs = (wf: CanonicalWorkflow, dead: ReadonlySet<LiteGraphLinkID>): void => {
|
|
74
|
+
if (dead.size === 0) return
|
|
75
|
+
for (const node of wf.nodes) {
|
|
76
|
+
for (const input of node.inputs) {
|
|
77
|
+
if (input.link != null && dead.has(input.link)) input.link = null
|
|
78
|
+
}
|
|
79
|
+
for (const output of node.outputs) {
|
|
80
|
+
output.links = output.links.filter((id) => !dead.has(id))
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const expandOneInstance = (wf: CanonicalWorkflow, instance: CanonicalNode, def: CanonicalSubgraphDef): void => {
|
|
86
|
+
// 1. fresh numeric ids, deterministic (def node order / def link order)
|
|
87
|
+
let nextNodeId = Math.max(0, ...wf.nodes.map((n) => n.id))
|
|
88
|
+
let nextLinkId = Math.max(0, ...wf.links.map((l) => l.id))
|
|
89
|
+
const newNodeIds = new Map<number, number>()
|
|
90
|
+
for (const inner of def.nodes) newNodeIds.set(inner.id, ++nextNodeId)
|
|
91
|
+
// null = the def link references a node the def does not contain: upstream
|
|
92
|
+
// editors leave STALE links behind after node deletion (corpus-verified,
|
|
93
|
+
// e.g. utility_video_upscale) and the frontend prunes them on load. We drop
|
|
94
|
+
// the link; the consumer resolves unconnected, loud at conversion when required.
|
|
95
|
+
const remapNodeId = (id: number): number | null => newNodeIds.get(id) ?? null
|
|
96
|
+
|
|
97
|
+
// 2. instance input slot → def input slot, BY NAME (instances may serialize
|
|
98
|
+
// a subset of the def's inputs). A name matching NO def input is LOUD:
|
|
99
|
+
// silently falling back to the positional index would wire the outer feed
|
|
100
|
+
// into the wrong inner slot (corpus-absent today, so a throw is safe)
|
|
101
|
+
const defSlotOfInstanceSlot = new Map<number, number>()
|
|
102
|
+
for (const [k, input] of instance.inputs.entries()) {
|
|
103
|
+
const defSlot = def.inputs.findIndex((d) => d.name === input.name)
|
|
104
|
+
if (defSlot < 0)
|
|
105
|
+
throw new WorkflowNormalizeError(
|
|
106
|
+
'subgraph-io-mismatch',
|
|
107
|
+
`subgraph instance ${instance.id}(${def.name}): input "${input.name}" (slot ${k}) matches no definition input (defined: ${def.inputs.map((d) => d.name).join(', ')})`,
|
|
108
|
+
)
|
|
109
|
+
defSlotOfInstanceSlot.set(k, defSlot)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// 3. outer links INTO the instance, keyed by def input slot
|
|
113
|
+
const outerLinkIntoDefSlot = new Map<number, CanonicalLink>()
|
|
114
|
+
for (const link of wf.links) {
|
|
115
|
+
if (link.targetId !== instance.id) continue
|
|
116
|
+
const defSlot = defSlotOfInstanceSlot.get(link.targetSlot)
|
|
117
|
+
if (defSlot == null)
|
|
118
|
+
throw new WorkflowNormalizeError(
|
|
119
|
+
'subgraph-io-mismatch',
|
|
120
|
+
`subgraph instance ${instance.id}(${def.name}): link ${link.id} targets undeclared instance slot ${link.targetSlot}`,
|
|
121
|
+
)
|
|
122
|
+
outerLinkIntoDefSlot.set(defSlot, link)
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// 4. classify definition links: plain interior links clone with fresh ids;
|
|
126
|
+
// input-boundary links (-10 origin) become real links from the outer origin
|
|
127
|
+
// when one exists (else they drop and the inner target falls back to its own
|
|
128
|
+
// widget values); output-boundary links (-20 target) just name the inner
|
|
129
|
+
// producer each def output slot maps to.
|
|
130
|
+
const newLinkIds = new Map<LiteGraphLinkID, LiteGraphLinkID>()
|
|
131
|
+
const splicedLinks: CanonicalLink[] = []
|
|
132
|
+
const producerByOutSlot = new Map<number, { nodeId: number; slot: number }>()
|
|
133
|
+
const passthroughOutSlot = new Map<number, number>() // def out slot → def in slot (-10 → -20 direct)
|
|
134
|
+
const boundaryTargetsByInSlot = new Map<number, { nodeId: number; slot: number }[]>() // remapped inner targets
|
|
135
|
+
for (const link of def.links) {
|
|
136
|
+
const fromInputBoundary = link.originId === SUBGRAPH_INPUT_NODE_ID
|
|
137
|
+
const toOutputBoundary = link.targetId === SUBGRAPH_OUTPUT_NODE_ID
|
|
138
|
+
if (fromInputBoundary && toOutputBoundary) {
|
|
139
|
+
passthroughOutSlot.set(link.targetSlot, link.originSlot)
|
|
140
|
+
continue
|
|
141
|
+
}
|
|
142
|
+
if (toOutputBoundary) {
|
|
143
|
+
const producerId = remapNodeId(link.originId)
|
|
144
|
+
if (producerId != null && !producerByOutSlot.has(link.targetSlot))
|
|
145
|
+
producerByOutSlot.set(link.targetSlot, { nodeId: producerId, slot: link.originSlot })
|
|
146
|
+
continue
|
|
147
|
+
}
|
|
148
|
+
if (fromInputBoundary) {
|
|
149
|
+
const targetId = remapNodeId(link.targetId)
|
|
150
|
+
if (targetId == null) continue
|
|
151
|
+
const target = { nodeId: targetId, slot: link.targetSlot }
|
|
152
|
+
const targets = boundaryTargetsByInSlot.get(link.originSlot) ?? []
|
|
153
|
+
targets.push(target)
|
|
154
|
+
boundaryTargetsByInSlot.set(link.originSlot, targets)
|
|
155
|
+
const outer = outerLinkIntoDefSlot.get(link.originSlot)
|
|
156
|
+
if (outer == null) continue // no outer feed: inner target's input.link nulls at clone time
|
|
157
|
+
const fresh = asLiteGraphLinkID(++nextLinkId)
|
|
158
|
+
newLinkIds.set(link.id, fresh)
|
|
159
|
+
splicedLinks.push({
|
|
160
|
+
id: fresh,
|
|
161
|
+
originId: outer.originId,
|
|
162
|
+
originSlot: outer.originSlot,
|
|
163
|
+
targetId: target.nodeId,
|
|
164
|
+
targetSlot: target.slot,
|
|
165
|
+
type: link.type,
|
|
166
|
+
})
|
|
167
|
+
const originNode = wf.nodes.find((n) => n.id === outer.originId)
|
|
168
|
+
originNode?.outputs[outer.originSlot]?.links.push(fresh)
|
|
169
|
+
continue
|
|
170
|
+
}
|
|
171
|
+
const interiorOrigin = remapNodeId(link.originId)
|
|
172
|
+
const interiorTarget = remapNodeId(link.targetId)
|
|
173
|
+
if (interiorOrigin == null || interiorTarget == null) continue
|
|
174
|
+
const fresh = asLiteGraphLinkID(++nextLinkId)
|
|
175
|
+
newLinkIds.set(link.id, fresh)
|
|
176
|
+
splicedLinks.push({
|
|
177
|
+
id: fresh,
|
|
178
|
+
originId: interiorOrigin,
|
|
179
|
+
originSlot: link.originSlot,
|
|
180
|
+
targetId: interiorTarget,
|
|
181
|
+
targetSlot: link.targetSlot,
|
|
182
|
+
type: link.type,
|
|
183
|
+
})
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// 5. clone inner nodes with remapped ids; link refs not in newLinkIds were
|
|
187
|
+
// dropped boundary links → null (the node falls back to its widget values)
|
|
188
|
+
const splicedNodes: CanonicalNode[] = def.nodes.map((inner) => ({
|
|
189
|
+
id: bang(newNodeIds.get(inner.id)),
|
|
190
|
+
type: inner.type,
|
|
191
|
+
title: inner.title,
|
|
192
|
+
mode: inner.mode,
|
|
193
|
+
isVirtualNode: inner.isVirtualNode,
|
|
194
|
+
inputs: inner.inputs.map((i) => ({
|
|
195
|
+
name: i.name,
|
|
196
|
+
type: i.type,
|
|
197
|
+
link: i.link != null ? (newLinkIds.get(i.link) ?? null) : null,
|
|
198
|
+
widgetName: i.widgetName,
|
|
199
|
+
})),
|
|
200
|
+
outputs: inner.outputs.map((o) => ({
|
|
201
|
+
name: o.name,
|
|
202
|
+
type: o.type,
|
|
203
|
+
links: o.links.map((id) => newLinkIds.get(id)).filter((id): id is LiteGraphLinkID => id != null),
|
|
204
|
+
})),
|
|
205
|
+
widgets: { positional: [...inner.widgets.positional], named: { ...inner.widgets.named } },
|
|
206
|
+
pos: [inner.pos[0], inner.pos[1]],
|
|
207
|
+
size: [inner.size[0], inner.size[1]],
|
|
208
|
+
properties: { ...inner.properties },
|
|
209
|
+
}))
|
|
210
|
+
|
|
211
|
+
// 6. rewire root links FROM the instance's outputs onto the inner producers;
|
|
212
|
+
// outer links INTO the instance were consumed by step 4
|
|
213
|
+
const dead = new Set<LiteGraphLinkID>()
|
|
214
|
+
for (const link of wf.links) {
|
|
215
|
+
if (link.targetId === instance.id) {
|
|
216
|
+
dead.add(link.id)
|
|
217
|
+
continue
|
|
218
|
+
}
|
|
219
|
+
if (link.originId !== instance.id) continue
|
|
220
|
+
const producer = producerByOutSlot.get(link.originSlot)
|
|
221
|
+
if (producer != null) {
|
|
222
|
+
link.originId = producer.nodeId
|
|
223
|
+
link.originSlot = producer.slot
|
|
224
|
+
const producerNode = splicedNodes.find((n) => n.id === producer.nodeId)
|
|
225
|
+
producerNode?.outputs[producer.slot]?.links.push(link.id)
|
|
226
|
+
continue
|
|
227
|
+
}
|
|
228
|
+
const passthroughInSlot = passthroughOutSlot.get(link.originSlot)
|
|
229
|
+
const outer = passthroughInSlot != null ? outerLinkIntoDefSlot.get(passthroughInSlot) : null
|
|
230
|
+
if (outer != null) {
|
|
231
|
+
link.originId = outer.originId
|
|
232
|
+
link.originSlot = outer.originSlot
|
|
233
|
+
continue
|
|
234
|
+
}
|
|
235
|
+
dead.add(link.id) // def output has no producer: consumers resolve unconnected
|
|
236
|
+
}
|
|
237
|
+
wf.links = wf.links.filter((l) => !dead.has(l.id))
|
|
238
|
+
pruneDeadLinkRefs(wf, dead)
|
|
239
|
+
|
|
240
|
+
// 7. widget promotion (both serialization eras), then splice
|
|
241
|
+
promoteInstanceWidgets(instance, def, splicedNodes, newNodeIds, boundaryTargetsByInSlot)
|
|
242
|
+
wf.nodes = wf.nodes.filter((n) => n.id !== instance.id)
|
|
243
|
+
wf.nodes.push(...splicedNodes)
|
|
244
|
+
wf.links.push(...splicedLinks)
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* instance widget values land in inner nodes' `named` overrides. TWO eras for
|
|
249
|
+
* the POSITIONAL values:
|
|
250
|
+
* - proxyWidgets era: `properties.proxyWidgets` pairs [innerNodeId, widgetName]
|
|
251
|
+
* zip against the instance's positional values (incl. `control_after_generate`
|
|
252
|
+
* phantom pairs — written to `named` and simply never read, no schema input
|
|
253
|
+
* has that name).
|
|
254
|
+
* - io-widget era (no proxyWidgets): positional values zip against the def's
|
|
255
|
+
* widget-kind input slots in def order, one value per slot (no phantom —
|
|
256
|
+
* corpus-verified). An outer link into the same slot wins in the converter;
|
|
257
|
+
* the named override is still written, matching ComfyUI.
|
|
258
|
+
* PLUS, in both eras, NAMED overrides on the instance keyed by def-input name:
|
|
259
|
+
* that is how a NESTED instance receives its outer values (the previous pass
|
|
260
|
+
* wrote them to `named`), so they must forward through THIS expansion too —
|
|
261
|
+
* named wins over positional, same precedence as the converter.
|
|
262
|
+
*/
|
|
263
|
+
const promoteInstanceWidgets = (
|
|
264
|
+
instance: CanonicalNode,
|
|
265
|
+
def: CanonicalSubgraphDef,
|
|
266
|
+
splicedNodes: CanonicalNode[],
|
|
267
|
+
newNodeIds: Map<number, number>,
|
|
268
|
+
boundaryTargetsByInSlot: Map<number, { nodeId: number; slot: number }[]>,
|
|
269
|
+
): void => {
|
|
270
|
+
const promoteToSlotTargets = (slot: number, value: unknown): void => {
|
|
271
|
+
for (const t of boundaryTargetsByInSlot.get(slot) ?? []) {
|
|
272
|
+
const target = splicedNodes.find((n) => n.id === t.nodeId)
|
|
273
|
+
const input = innerInputAt(splicedNodes, t)
|
|
274
|
+
if (target == null || input == null) continue
|
|
275
|
+
target.widgets.named[input.name] = value
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
promotePositionalWidgets(instance, def, splicedNodes, newNodeIds, boundaryTargetsByInSlot, promoteToSlotTargets)
|
|
280
|
+
|
|
281
|
+
for (const [slot, io] of def.inputs.entries()) {
|
|
282
|
+
if (!Object.hasOwn(instance.widgets.named, io.name)) continue
|
|
283
|
+
promoteToSlotTargets(slot, instance.widgets.named[io.name])
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
const promotePositionalWidgets = (
|
|
288
|
+
instance: CanonicalNode,
|
|
289
|
+
def: CanonicalSubgraphDef,
|
|
290
|
+
splicedNodes: CanonicalNode[],
|
|
291
|
+
newNodeIds: Map<number, number>,
|
|
292
|
+
boundaryTargetsByInSlot: Map<number, { nodeId: number; slot: number }[]>,
|
|
293
|
+
promoteToSlotTargets: (slot: number, value: unknown) => void,
|
|
294
|
+
): void => {
|
|
295
|
+
const values = instance.widgets.positional
|
|
296
|
+
if (values.length === 0) return // inner nodes keep their serialized defaults
|
|
297
|
+
|
|
298
|
+
const proxy = instance.properties['proxyWidgets']
|
|
299
|
+
if (Array.isArray(proxy) && proxy.length > 0) {
|
|
300
|
+
const count = Math.min(values.length, proxy.length)
|
|
301
|
+
for (let k = 0; k < count; k++) {
|
|
302
|
+
const pair: unknown = proxy[k]
|
|
303
|
+
if (!Array.isArray(pair) || pair.length !== 2)
|
|
304
|
+
throw new WorkflowNormalizeError(
|
|
305
|
+
'subgraph-io-mismatch',
|
|
306
|
+
`subgraph instance ${instance.id}(${def.name}): malformed proxyWidgets entry ${JSON.stringify(pair)}`,
|
|
307
|
+
)
|
|
308
|
+
const innerId = Number(pair[0])
|
|
309
|
+
const widgetName = String(pair[1])
|
|
310
|
+
if (innerId < 0) {
|
|
311
|
+
// id -1 names a widget on the subgraph node ITSELF (a promoted io
|
|
312
|
+
// widget): route the value through the matching def input slot
|
|
313
|
+
const slot = def.inputs.findIndex((d) => d.name === widgetName)
|
|
314
|
+
if (slot < 0)
|
|
315
|
+
throw new WorkflowNormalizeError(
|
|
316
|
+
'subgraph-io-mismatch',
|
|
317
|
+
`subgraph instance ${instance.id}(${def.name}): proxyWidgets pair ${k} names io widget "${widgetName}" but no definition input matches`,
|
|
318
|
+
)
|
|
319
|
+
promoteToSlotTargets(slot, values[k])
|
|
320
|
+
continue
|
|
321
|
+
}
|
|
322
|
+
const target = splicedNodes.find((n) => n.id === newNodeIds.get(innerId))
|
|
323
|
+
if (target == null)
|
|
324
|
+
throw new WorkflowNormalizeError(
|
|
325
|
+
'subgraph-io-mismatch',
|
|
326
|
+
`subgraph instance ${instance.id}(${def.name}): proxyWidgets pair ${k} names unknown inner node ${String(pair[0])}`,
|
|
327
|
+
)
|
|
328
|
+
target.widgets.named[widgetName] = values[k]
|
|
329
|
+
}
|
|
330
|
+
return
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
let valueIndex = 0
|
|
334
|
+
for (const [slot, io] of def.inputs.entries()) {
|
|
335
|
+
const targets = boundaryTargetsByInSlot.get(slot) ?? []
|
|
336
|
+
const isWidgetSlot =
|
|
337
|
+
targets.length > 0
|
|
338
|
+
? targets.some((t) => innerInputAt(splicedNodes, t)?.widgetName != null)
|
|
339
|
+
: PRIMITIVE_WIDGET_TYPES.has(io.type)
|
|
340
|
+
if (!isWidgetSlot) continue
|
|
341
|
+
if (valueIndex >= values.length) break
|
|
342
|
+
promoteToSlotTargets(slot, values[valueIndex++])
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
const innerInputAt = (
|
|
347
|
+
splicedNodes: CanonicalNode[],
|
|
348
|
+
t: { nodeId: number; slot: number },
|
|
349
|
+
): { name: string; widgetName: string | null } | null => {
|
|
350
|
+
const node = splicedNodes.find((n) => n.id === t.nodeId)
|
|
351
|
+
return node?.inputs[t.slot] ?? null
|
|
352
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { type } from 'arktype'
|
|
2
|
+
import type {
|
|
3
|
+
CanonicalGroup,
|
|
4
|
+
CanonicalInput,
|
|
5
|
+
CanonicalLink,
|
|
6
|
+
CanonicalNode,
|
|
7
|
+
CanonicalOutput,
|
|
8
|
+
CanonicalWidgetValues,
|
|
9
|
+
CanonicalWorkflow,
|
|
10
|
+
NodeMode,
|
|
11
|
+
} from 'src/litegraph/CanonicalWorkflow.ts'
|
|
12
|
+
import { type CanonicalSubgraphDef, expandSubgraphs } from 'src/litegraph/expandSubgraphs.ts'
|
|
13
|
+
import type { LiteGraphGroup } from 'src/litegraph/LiteGraphGroup.ts'
|
|
14
|
+
import { type LiteGraphJSON, LiteGraphJSON_ark } from 'src/litegraph/LiteGraphJSON.ts'
|
|
15
|
+
import type { LiteGraphLink } from 'src/litegraph/LiteGraphLink.ts'
|
|
16
|
+
import type { LiteGraphNode } from 'src/litegraph/LiteGraphNode.ts'
|
|
17
|
+
import type { LiteGraphNodeInput } from 'src/litegraph/LiteGraphNodeInput.ts'
|
|
18
|
+
import type { LiteGraphNodeOutput } from 'src/litegraph/LiteGraphNodeOutput.ts'
|
|
19
|
+
import type { LiteGraphSubgraphDef } from 'src/litegraph/LiteGraphSubgraphDef.ts'
|
|
20
|
+
|
|
21
|
+
export type WorkflowNormalizeErrorCode = 'schema-reject' | 'subgraph-cycle' | 'subgraph-io-mismatch' | 'bad-document'
|
|
22
|
+
|
|
23
|
+
/** a workflow JSON we cannot turn into a CanonicalWorkflow — loud, typed, never a cast */
|
|
24
|
+
export class WorkflowNormalizeError extends Error {
|
|
25
|
+
constructor(
|
|
26
|
+
public code: WorkflowNormalizeErrorCode,
|
|
27
|
+
message: string,
|
|
28
|
+
public detail?: unknown,
|
|
29
|
+
) {
|
|
30
|
+
super(`[workflow ${code}] ${message}`)
|
|
31
|
+
this.name = 'WorkflowNormalizeError'
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* THE one entry for foreign workflow JSON (files, clipboard, imports):
|
|
37
|
+
* tolerant ark validation, then normalization to the strict canonical shape.
|
|
38
|
+
*/
|
|
39
|
+
export const parseWorkflowJson = (json: unknown): CanonicalWorkflow => {
|
|
40
|
+
const out = LiteGraphJSON_ark(json)
|
|
41
|
+
if (out instanceof type.errors) throw new WorkflowNormalizeError('schema-reject', out.summary, out)
|
|
42
|
+
return normalizeWorkflow(out)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* loose wire document → strict canonical document. Defaults filled, both link
|
|
47
|
+
* eras unified, cosmetic fields dropped, subgraph definitions expanded away
|
|
48
|
+
* (see expandSubgraphs.ts — def interiors go through the SAME per-shape
|
|
49
|
+
* normalizers, they are v1 bodies).
|
|
50
|
+
*/
|
|
51
|
+
export const normalizeWorkflow = (raw: LiteGraphJSON): CanonicalWorkflow => {
|
|
52
|
+
const wf: CanonicalWorkflow = {
|
|
53
|
+
nodes: raw.nodes.map(normalizeNode),
|
|
54
|
+
links: raw.links.map(normalizeLink),
|
|
55
|
+
groups: (raw.groups ?? []).map(normalizeGroup),
|
|
56
|
+
}
|
|
57
|
+
const rawDefs = raw.definitions?.subgraphs ?? []
|
|
58
|
+
if (rawDefs.length === 0) return wf
|
|
59
|
+
const defs = new Map<string, CanonicalSubgraphDef>()
|
|
60
|
+
for (const d of rawDefs) defs.set(d.id, normalizeSubgraphDef(d))
|
|
61
|
+
return expandSubgraphs(wf, defs)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const normalizeSubgraphDef = (d: LiteGraphSubgraphDef): CanonicalSubgraphDef => {
|
|
65
|
+
return {
|
|
66
|
+
id: d.id,
|
|
67
|
+
name: d.name,
|
|
68
|
+
inputs: d.inputs.map((io) => ({ name: io.name, type: io.type })),
|
|
69
|
+
outputs: d.outputs.map((io) => ({ name: io.name, type: io.type })),
|
|
70
|
+
nodes: d.nodes.map(normalizeNode),
|
|
71
|
+
links: d.links.map(normalizeLink),
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const normalizeMode = (mode: number | undefined): NodeMode => {
|
|
76
|
+
if (mode === 2) return 'muted'
|
|
77
|
+
if (mode === 4) return 'bypassed'
|
|
78
|
+
return 'normal' // 0 (always) / 1 (on-event) / 3 (on-trigger) all execute
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const normalizeNode = (n: LiteGraphNode): CanonicalNode => {
|
|
82
|
+
return {
|
|
83
|
+
id: n.id,
|
|
84
|
+
type: n.type,
|
|
85
|
+
title: n.title ?? null,
|
|
86
|
+
mode: normalizeMode(n.mode),
|
|
87
|
+
isVirtualNode: n.isVirtualNode ?? false,
|
|
88
|
+
inputs: (n.inputs ?? []).map(normalizeInput),
|
|
89
|
+
outputs: (n.outputs ?? []).map(normalizeOutput),
|
|
90
|
+
widgets: normalizeWidgetValues(n.widgets_values),
|
|
91
|
+
pos: [n.pos[0], n.pos[1]],
|
|
92
|
+
size: [n.size[0], n.size[1]],
|
|
93
|
+
properties: { ...n.properties },
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const normalizeInput = (i: LiteGraphNodeInput): CanonicalInput => {
|
|
98
|
+
return {
|
|
99
|
+
name: i.name,
|
|
100
|
+
type: i.type,
|
|
101
|
+
link: i.link ?? null,
|
|
102
|
+
widgetName: i.widget?.name ?? null,
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const normalizeOutput = (o: LiteGraphNodeOutput): CanonicalOutput => {
|
|
107
|
+
return {
|
|
108
|
+
name: o.name,
|
|
109
|
+
type: o.type,
|
|
110
|
+
links: [...(o.links ?? [])],
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const normalizeWidgetValues = (wv: unknown[] | Record<string, unknown> | undefined): CanonicalWidgetValues => {
|
|
115
|
+
if (wv == null) return { positional: [], named: {} }
|
|
116
|
+
if (Array.isArray(wv)) return { positional: [...wv], named: {} }
|
|
117
|
+
return { positional: [], named: { ...wv } }
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const normalizeLink = (l: LiteGraphLink): CanonicalLink => {
|
|
121
|
+
if (Array.isArray(l)) {
|
|
122
|
+
return { id: l[0], originId: l[1], originSlot: l[2], targetId: l[3], targetSlot: l[4], type: l[5] }
|
|
123
|
+
}
|
|
124
|
+
return {
|
|
125
|
+
id: l.id,
|
|
126
|
+
originId: l.origin_id,
|
|
127
|
+
originSlot: l.origin_slot,
|
|
128
|
+
targetId: l.target_id,
|
|
129
|
+
targetSlot: l.target_slot,
|
|
130
|
+
type: l.type,
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const normalizeGroup = (g: LiteGraphGroup): CanonicalGroup => {
|
|
135
|
+
return {
|
|
136
|
+
title: g.title,
|
|
137
|
+
bounding: [g.bounding[0], g.bounding[1], g.bounding[2], g.bounding[3]],
|
|
138
|
+
color: g.color ?? '#3f789e',
|
|
139
|
+
fontSize: g.font_size ?? 24,
|
|
140
|
+
}
|
|
141
|
+
}
|
|
@@ -201,9 +201,8 @@ export class ComfyExecution {
|
|
|
201
201
|
const promptMeta = this.workflow.data.metadata?.[promptNodeID]
|
|
202
202
|
if (promptNode == null) throw new Error(`❌ invariant violation: promptNode is null`)
|
|
203
203
|
|
|
204
|
-
//
|
|
205
|
-
const
|
|
206
|
-
const imgUrl = serverURL + '/view?' + new URLSearchParams(comfyImageInfo).toString()
|
|
204
|
+
// image route on the host (cloud answers a 302 signed url — fetchFile follows it unauthed)
|
|
205
|
+
const imgRoute = '/view?' + new URLSearchParams(comfyImageInfo).toString()
|
|
207
206
|
|
|
208
207
|
// target path on disk
|
|
209
208
|
const sf = this.saveFormat
|
|
@@ -222,7 +221,7 @@ export class ComfyExecution {
|
|
|
222
221
|
|
|
223
222
|
// RE-ENCODE (COMPRESSED)
|
|
224
223
|
if (sf && sf.format !== 'raw') {
|
|
225
|
-
const response = await
|
|
224
|
+
const response = await this.host.fetchFile(imgRoute)
|
|
226
225
|
const buff = await response.arrayBuffer()
|
|
227
226
|
let textChunk = {}
|
|
228
227
|
try {
|
|
@@ -257,7 +256,7 @@ export class ComfyExecution {
|
|
|
257
256
|
|
|
258
257
|
// SAVE RAW ------------------------------------------------------------------------------------------
|
|
259
258
|
else {
|
|
260
|
-
const response = await
|
|
259
|
+
const response = await this.host.fetchFile(imgRoute)
|
|
261
260
|
const buff = await response.arrayBuffer()
|
|
262
261
|
const uint8arr = new Uint8Array(buff)
|
|
263
262
|
writeFileSync(absPath, uint8arr)
|
|
@@ -363,9 +363,9 @@ export class ComfyWorkflow<ID extends string = string> {
|
|
|
363
363
|
},
|
|
364
364
|
}
|
|
365
365
|
|
|
366
|
-
const
|
|
367
|
-
const res = await fetch(promptEndpoint, {
|
|
366
|
+
const res = await this.host.fetch('/prompt', {
|
|
368
367
|
method: 'POST',
|
|
368
|
+
headers: { 'Content-Type': 'application/json' },
|
|
369
369
|
body: JSON.stringify(out),
|
|
370
370
|
})
|
|
371
371
|
const promptInfoJson = await res.json()
|
package/src/runner/ComfyWsApi.ts
CHANGED
|
@@ -103,6 +103,14 @@ const tempScope = scope({
|
|
|
103
103
|
data: 'WSMsgManagerFeedbackData',
|
|
104
104
|
},
|
|
105
105
|
|
|
106
|
+
// server console stream (/internal/logs): entries are WRITE CHUNKS, not lines
|
|
107
|
+
LogEntry: { t: 'string', m: 'string' },
|
|
108
|
+
_WsMsgLogsData: { entries: 'LogEntry[]', 'size?': 'unknown' },
|
|
109
|
+
WsMsgLogs: {
|
|
110
|
+
type: "'logs'",
|
|
111
|
+
data: '_WsMsgLogsData',
|
|
112
|
+
},
|
|
113
|
+
|
|
106
114
|
// WsMsgProgress must be after NodeProgress
|
|
107
115
|
WsMsgProgress: { type: "'progress'", data: 'NodeProgress' },
|
|
108
116
|
|
|
@@ -119,7 +127,7 @@ const tempScope = scope({
|
|
|
119
127
|
// UNION TYPES
|
|
120
128
|
PromptRelated_WsMsg:
|
|
121
129
|
'WsMsgExecutionStart | WsMsgExecutionCached | WsMsgExecuting | WsMsgProgress | WsMsgProgressState | WsMsgExecuted | WsMsgExecutionError | WsMsgExecutionSuccess',
|
|
122
|
-
WsMsg: 'WsMsgStatus | WSMsgManagerFeedback | PromptRelated_WsMsg',
|
|
130
|
+
WsMsg: 'WsMsgStatus | WSMsgManagerFeedback | WsMsgLogs | PromptRelated_WsMsg',
|
|
123
131
|
})
|
|
124
132
|
|
|
125
133
|
export const types = tempScope.export()
|
|
@@ -145,6 +153,8 @@ export type WsMsgExecuted = typeof types.WsMsgExecuted.infer
|
|
|
145
153
|
export type WsMsgExecutionSuccess = typeof types.WsMsgExecutionSuccess.infer
|
|
146
154
|
export type WsMsgExecutionError = typeof types.WsMsgExecutionError.infer
|
|
147
155
|
export type WSMsgManagerFeedback = typeof types.WSMsgManagerFeedback.infer
|
|
156
|
+
export type LogEntry = typeof types.LogEntry.infer
|
|
157
|
+
export type WsMsgLogs = typeof types.WsMsgLogs.infer
|
|
148
158
|
export type _WsMsgExecutionStartData = typeof types._WsMsgExecutionStartData.infer
|
|
149
159
|
export type _WsMsgExecutionCachedData = typeof types._WsMsgExecutionCachedData.infer
|
|
150
160
|
export type ComfyImageInfo = typeof types.ComfyImageInfo.infer
|
|
@@ -157,3 +167,4 @@ export const PromptInfo_ark = type({ prompt_id: types.PromptID }).as<PromptInfo>
|
|
|
157
167
|
|
|
158
168
|
export const WsMsg_ark = types.WsMsg
|
|
159
169
|
export const ComfyImageInfo_ark = types.ComfyImageInfo
|
|
170
|
+
export const WsMsgLogsData_ark = types._WsMsgLogsData
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// pure parser for BINARY ws frames (formats per
|
|
2
|
+
// agent/external-docs/comfy-cloud/api-reference.md, all integers big-endian):
|
|
3
|
+
// 1 PREVIEW_IMAGE 4B type · 4B image_type (1 jpeg, 2 png) · image bytes
|
|
4
|
+
// 3 TEXT 4B type · 4B node_id_len · node_id · progress text
|
|
5
|
+
// 4 PREVIEW_IMAGE_WITH_METADATA 4B type · 4B metadata_len · metadata JSON · image bytes
|
|
6
|
+
// Unknown types come back as 'unknown' — the CALLER logs, never throws: the
|
|
7
|
+
// wire drifts faster than we do (type 4 appeared on the first live cloud run).
|
|
8
|
+
|
|
9
|
+
export type ParsedBinaryWsFrame =
|
|
10
|
+
| { kind: 'preview'; bytes: Uint8Array<ArrayBuffer>; mime: string; metadata: unknown }
|
|
11
|
+
| { kind: 'text'; nodeId: string; text: string }
|
|
12
|
+
| { kind: 'unknown'; eventType: number }
|
|
13
|
+
|
|
14
|
+
export function parseBinaryWsFrame(data: ArrayBuffer): ParsedBinaryWsFrame {
|
|
15
|
+
const view = new DataView(data)
|
|
16
|
+
const eventType = view.getUint32(0)
|
|
17
|
+
|
|
18
|
+
if (eventType === 1) {
|
|
19
|
+
const imageType = view.getUint32(4)
|
|
20
|
+
const bytes = new Uint8Array(data.slice(8))
|
|
21
|
+
const mime = imageType === 2 ? 'image/png' : 'image/jpeg'
|
|
22
|
+
return { kind: 'preview', bytes, mime, metadata: null }
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (eventType === 3) {
|
|
26
|
+
const nodeIdLen = view.getUint32(4)
|
|
27
|
+
const decoder = new TextDecoder()
|
|
28
|
+
const nodeId = decoder.decode(data.slice(8, 8 + nodeIdLen))
|
|
29
|
+
const text = decoder.decode(data.slice(8 + nodeIdLen))
|
|
30
|
+
return { kind: 'text', nodeId, text }
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (eventType === 4) {
|
|
34
|
+
const metadataLen = view.getUint32(4)
|
|
35
|
+
let metadata: unknown = null
|
|
36
|
+
try {
|
|
37
|
+
metadata = JSON.parse(new TextDecoder().decode(data.slice(8, 8 + metadataLen)))
|
|
38
|
+
} catch {
|
|
39
|
+
// tolerate broken metadata: the image bytes are the payload that matters
|
|
40
|
+
}
|
|
41
|
+
const bytes = new Uint8Array(data.slice(8 + metadataLen))
|
|
42
|
+
// the docs say "raw JPEG/PNG bytes" with no format field — sniff the magic
|
|
43
|
+
const mime = bytes[0] === 0x89 && bytes[1] === 0x50 ? 'image/png' : 'image/jpeg'
|
|
44
|
+
return { kind: 'preview', bytes, mime, metadata }
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return { kind: 'unknown', eventType }
|
|
48
|
+
}
|
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
import type { LiteGraphNode } from 'src/litegraph/LiteGraphNode.ts'
|
|
2
1
|
import type { ComfyNodeSchemaJSON } from 'src/sdk-generator/ComfyUIObjectInfoTypes.ts'
|
|
3
|
-
import type { NodeInputExt } from 'src/sdk-generator/comfyui-types.ts'
|
|
4
2
|
|
|
5
3
|
export const ComfyPrimitiveMapping: { [key: string]: string } = {
|
|
6
4
|
// BACK
|
|
@@ -20,24 +18,14 @@ export const ComfyPrimitiveMapping: { [key: string]: string } = {
|
|
|
20
18
|
|
|
21
19
|
export const ComfyPrimitives: string[] = Object.keys(ComfyPrimitiveMapping)
|
|
22
20
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
export const howManyWidgetValuesForThisSchemaType = (input: NodeInputExt): number => {
|
|
28
|
-
if (input.typeName === 'INT') {
|
|
29
|
-
if (input.nameInComfy === 'seed' || input.nameInComfy === 'noise_seed') return 2
|
|
30
|
-
}
|
|
31
|
-
if (input.isPrimitive) return 1
|
|
32
|
-
// 🔴
|
|
33
|
-
if (input.typeName.startsWith('E_')) return 1
|
|
34
|
-
// console.log(343, input)
|
|
35
|
-
return 0
|
|
36
|
-
}
|
|
21
|
+
// schema-driven widget counting moved to src/sdk-generator/inputWidgetKind.ts
|
|
22
|
+
// (config-driven classification); what stays below is the LEGACY serialized-
|
|
23
|
+
// type path, used only when an old file carries a widget-marked input whose
|
|
24
|
+
// schema now says slot.
|
|
37
25
|
|
|
38
26
|
/**
|
|
39
|
-
*
|
|
40
|
-
*
|
|
27
|
+
* when a litegraph node has an input entry with a widget marker, the
|
|
28
|
+
* serialized TYPE string decides how many widgets_values it consumed in that era
|
|
41
29
|
*/
|
|
42
30
|
export const howManyWidgetValuesForThisInputType = (type: string, nameInComfy: string): number => {
|
|
43
31
|
if (type === 'INT') {
|
|
@@ -68,9 +56,3 @@ export const ComfyDefaultNodeWhenUnknown_Schema: ComfyNodeSchemaJSON = {
|
|
|
68
56
|
output_node: false,
|
|
69
57
|
python_module: 'nodes',
|
|
70
58
|
}
|
|
71
|
-
|
|
72
|
-
export class UnknownCustomNode extends Error {
|
|
73
|
-
constructor(public node: LiteGraphNode) {
|
|
74
|
-
super(`❌ node ${node.id}(${node.type}) has no known schema; you probably need to install some custom node`)
|
|
75
|
-
}
|
|
76
|
-
}
|