Line data Source code
1 : import { PREFIX, SEPARATOR } from '@/constants/groupNodeConstants'
2 : import { t } from '@/i18n'
3 : import type { ISlotType } from '@comfyorg/graph-model/interfaces'
4 : import type { ISerialisedNode } from '@comfyorg/graph-model/serialisation'
5 : import type {
6 : LGraphNodeConstructor,
7 : LGraphNode
8 : } from '@/lib/litegraph/src/litegraph'
9 : import { LiteGraph } from '@/lib/litegraph/src/litegraph'
10 : import { outputLinks } from '@/lib/litegraph/src/node/slotLinks'
11 : import { selectedCanvasNodes } from '@/renderer/core/canvas/litegraph/selectionAdapter'
12 : import { slotTypeKey } from '@/lib/litegraph/src/utils/type'
13 : import type { SerializedNodeId } from '@comfyorg/graph-model/nodeId'
14 : import { parseNodeId } from '@comfyorg/graph-model/nodeId'
15 : import type {
16 : ComfyNode,
17 : ComfyWorkflowJSON
18 : } from '@comfyorg/graph-model/workflowSchema'
19 : import type { MissingNodeType } from '@/platform/workflowErrors/nodeReplacement/types'
20 : import type {
21 : ComfyNodeDef,
22 : ComfyOutputTypesSpec,
23 : InputSpec
24 : } from '@comfyorg/object-info-parser'
25 : import { useNodeDefStore } from '@/stores/nodeDefStore'
26 : import { useWidgetStore } from '@/stores/widgetStore'
27 : import type { ComfyExtension } from '@/types/comfy'
28 : import { deserialiseAndCreate } from '@/utils/vintageClipboard'
29 :
30 : import { app } from '@/scripts/app'
31 : import { mergeIfValid } from './widgetInputs'
32 :
33 : /**
34 : * Marker symbol stamped on a synthesized group-node type's `nodeData` (via
35 : * {@link markGroupNodeType}) so loaded group-node instances can be detected and
36 : * migrated to subgraphs.
37 : */
38 2355 : const GROUP = Symbol()
39 :
40 : /**
41 : * Stamp the group-node marker onto the registered node type so instances created
42 : * during load can be detected by {@link GroupNodeHandler.isGroupNode}. This is
43 : * stamped directly on the constructor rather than copied through
44 : * {@link ComfyNodeDefImpl} construction, keeping the migration self-contained.
45 : */
46 7 : function markGroupNodeType(typeName: string, config: GroupNodeConfig): void {
47 7 : const ctor = LiteGraph.registered_node_types[typeName] as
48 : | LGraphNodeConstructor
49 : | undefined
50 7 : if (ctor?.nodeData) ctor.nodeData[GROUP] = config
51 : }
52 :
53 : /**
54 : * Link stored in a group node's workflow data. `sourceNodeIndex` is `null` when
55 : * the source node was outside the group when it was created.
56 : */
57 : export type GroupNodeLink = [
58 : sourceNodeIndex: number | null,
59 : sourceSlot: number,
60 : targetNodeIndex: number,
61 : targetSlot: number,
62 : sourceNodeId: SerializedNodeId,
63 : type?: unknown
64 : ]
65 : type SlotLinks = Partial<Record<number, GroupNodeLink>>
66 : type LinksFromMap = Partial<
67 : Record<number, Partial<Record<number, GroupNodeLink[]>>>
68 : >
69 :
70 4 : function isSlotType(value: unknown): value is ISlotType {
71 4 : return (
72 : typeof value === 'number' ||
73 : typeof value === 'string' ||
74 : (Array.isArray(value) && value.every((item) => typeof item === 'string'))
75 : )
76 : }
77 : type LinksToMap = Partial<Record<number, SlotLinks>>
78 : type ExternalFromMap = Partial<
79 : Record<number, Partial<Record<number, string | number>>>
80 : >
81 :
82 : interface GroupNodeConfigEntry {
83 : input?: Record<string, { name?: string; visible?: boolean }>
84 : output?: Record<number, { name?: string; visible?: boolean }>
85 : }
86 :
87 : interface GroupNodeData extends Partial<
88 : Pick<
89 : ISerialisedNode,
90 : 'type' | 'title' | 'inputs' | 'outputs' | 'widgets_values'
91 : >
92 : > {
93 : index?: number
94 : }
95 :
96 : export interface GroupNodeWorkflowData {
97 : external?: (number | string)[][]
98 : links: GroupNodeLink[]
99 : nodes: GroupNodeData[]
100 : config?: Record<number, GroupNodeConfigEntry>
101 : }
102 :
103 13 : function isOutputTypeSpec(
104 : value: unknown
105 : ): value is ComfyOutputTypesSpec[number] {
106 13 : return (
107 : typeof value === 'string' ||
108 : (Array.isArray(value) &&
109 : value.every(
110 : (option) => typeof option === 'string' || typeof option === 'number'
111 : ))
112 : )
113 : }
114 :
115 : interface GroupNodeDef {
116 : input: {
117 : required: Record<string, unknown>
118 : optional?: Record<string, unknown>
119 : }
120 : output: unknown[]
121 : output_name: string[]
122 : output_is_list: boolean[]
123 : }
124 :
125 2355 : export class GroupNodeConfig {
126 : name: string
127 : nodeData: GroupNodeWorkflowData
128 : inputCount: number
129 : oldToNewOutputMap: Partial<Record<number, Record<number, number>>>
130 : newToOldOutputMap: Partial<
131 : Record<number, { node: GroupNodeData; slot: number }>
132 : >
133 : oldToNewInputMap: Partial<Record<number, Record<string, number>>>
134 : oldToNewWidgetMap: Partial<Record<number, Record<string, string | null>>>
135 : newToOldWidgetMap: Record<string, { node: GroupNodeData; inputName: string }>
136 : primitiveDefs: Partial<Record<number, GroupNodeDef>>
137 : widgetToPrimitive: Partial<Record<number, Record<string, number | number[]>>>
138 : primitiveToWidget: Partial<
139 : Record<number, { nodeId: number | string | null; inputName: string }[]>
140 : >
141 : nodeInputs: Partial<Record<number, Record<string, string>>>
142 : outputVisibility: boolean[]
143 : nodeDef: ComfyNodeDef | undefined
144 : inputs!: unknown[]
145 : linksFrom!: LinksFromMap
146 : linksTo!: LinksToMap
147 : externalFrom!: ExternalFromMap
148 :
149 : constructor(name: string, nodeData: GroupNodeWorkflowData) {
150 7 : this.name = name
151 7 : this.nodeData = nodeData
152 7 : this.getLinks()
153 :
154 7 : this.inputCount = 0
155 7 : this.oldToNewOutputMap = {}
156 7 : this.newToOldOutputMap = {}
157 7 : this.oldToNewInputMap = {}
158 7 : this.oldToNewWidgetMap = {}
159 7 : this.newToOldWidgetMap = {}
160 7 : this.primitiveDefs = {}
161 7 : this.widgetToPrimitive = {}
162 7 : this.primitiveToWidget = {}
163 7 : this.nodeInputs = {}
164 7 : this.outputVisibility = []
165 : }
166 :
167 : async registerType(source = PREFIX) {
168 7 : this.nodeDef = {
169 : output: [],
170 : output_name: [],
171 : output_is_list: [],
172 : output_node: false, // This is a lie (to satisfy the interface)
173 : name: source + SEPARATOR + this.name,
174 : display_name: this.name,
175 : category: 'group nodes' + (SEPARATOR + source),
176 : input: { required: {} },
177 : description: `Group node combining ${this.nodeData.nodes
178 : .map((n) => n.type)
179 : .join(', ')}`,
180 : python_module: 'custom_nodes.' + this.name
181 : }
182 :
183 7 : this.inputs = []
184 7 : const seenInputs = {}
185 7 : const seenOutputs = {}
186 7 : for (let i = 0; i < this.nodeData.nodes.length; i++) {
187 29 : const node = this.nodeData.nodes[i]
188 29 : node.index = i
189 29 : this.processNode(node, seenInputs, seenOutputs)
190 : }
191 :
192 7 : for (const p of this._convertedToProcess) {
193 26 : p()
194 : }
195 7 : this._convertedToProcess = []
196 7 : const typeName = `${PREFIX}${SEPARATOR}${this.name}`
197 7 : await app.registerNodeDef(typeName, this.nodeDef)
198 7 : markGroupNodeType(typeName, this)
199 7 : useNodeDefStore().addNodeDef(this.nodeDef)
200 : }
201 :
202 : getLinks() {
203 7 : this.linksFrom = {}
204 7 : this.linksTo = {}
205 7 : this.externalFrom = {}
206 :
207 : // Extract links for easy lookup
208 7 : for (const link of this.nodeData.links) {
209 33 : const [sourceNodeIndex, sourceSlot, targetNodeIndex, targetSlot] = link
210 :
211 : // Skip links outside the copy config
212 33 : if (sourceNodeIndex == null) continue
213 :
214 25 : const fromSlots = (this.linksFrom[sourceNodeIndex] ??= {})
215 25 : ;(fromSlots[sourceSlot] ??= []).push(link)
216 25 : ;(this.linksTo[targetNodeIndex] ??= {})[targetSlot] = link
217 : }
218 :
219 7 : for (const ext of this.nodeData.external ?? []) {
220 12 : const nodeIdx = Number(ext[0])
221 12 : const slotIdx = Number(ext[1])
222 12 : const typeVal = ext.at(2)
223 12 : if (typeVal == null) continue
224 12 : ;(this.externalFrom[nodeIdx] ??= {})[slotIdx] = typeVal
225 : }
226 : }
227 :
228 : processNode(
229 : node: GroupNodeData,
230 : seenInputs: Record<string, number>,
231 : seenOutputs: Record<string, number>
232 : ) {
233 29 : const def = this.getNodeDef(node)
234 29 : if (!def) return
235 :
236 29 : const inputs = { ...def.input?.required, ...def.input?.optional }
237 :
238 29 : this.inputs.push(this.processNodeInputs(node, seenInputs, inputs))
239 29 : if (def.output?.length) this.processNodeOutputs(node, seenOutputs, def)
240 : }
241 :
242 : getNodeDef(
243 : node: GroupNodeData
244 : ): GroupNodeDef | ComfyNodeDef | null | undefined {
245 29 : if (node.type) {
246 29 : const def = globalDefs[node.type]
247 29 : if (def) return def
248 : }
249 :
250 7 : const nodeIndex = node.index
251 13 : if (nodeIndex == null) return undefined
252 :
253 7 : const linksFrom = this.linksFrom[nodeIndex]
254 13 : if (node.type === 'PrimitiveNode') {
255 : // Skip as its not linked
256 7 : if (!linksFrom) return
257 :
258 7 : const firstLink = linksFrom[0]?.at(0)
259 7 : const linkedType: unknown = firstLink?.at(5)
260 7 : let type: string | number | null = firstLink
261 : ? linkedType == null
262 : ? null
263 : : isSlotType(linkedType)
264 : ? slotTypeKey(linkedType)
265 : : null
266 : : null
267 7 : if (type === 'COMBO') {
268 : // Use the array items
269 : const output = node.outputs?.[0]
270 0 : const source = output?.widget?.name
271 0 : const nodeIdx = firstLink?.[2]
272 0 : if (source && nodeIdx != null) {
273 0 : const fromTypeName = this.nodeData.nodes.at(nodeIdx)?.type
274 0 : if (fromTypeName) {
275 0 : const fromType = globalDefs[fromTypeName]
276 : const input =
277 : fromType?.input?.required?.[source] ??
278 : fromType?.input?.optional?.[source]
279 0 : const inputType = input?.[0]
280 0 : type =
281 : typeof inputType === 'string' || typeof inputType === 'number'
282 : ? inputType
283 : : null
284 : }
285 : }
286 : }
287 :
288 : const def = (this.primitiveDefs[nodeIndex] = {
289 : input: {
290 : required: {
291 : value: [type, {}]
292 : }
293 : },
294 : output: [type],
295 : output_name: [],
296 : output_is_list: []
297 : })
298 : return def
299 0 : } else if (node.type === 'Reroute') {
300 0 : const linksTo = this.linksTo[nodeIndex]
301 0 : if (linksTo && linksFrom && !this.externalFrom[nodeIndex]?.[0]) {
302 : // Being used internally
303 0 : return null
304 : }
305 :
306 0 : let config: Record<string, unknown> = {}
307 0 : let rerouteType: ISlotType = '*'
308 0 : if (linksFrom) {
309 0 : const links = linksFrom[0] ?? []
310 0 : for (const [, , targetNodeIndex, targetSlot] of links) {
311 0 : const targetNode = this.nodeData.nodes.at(targetNodeIndex)
312 0 : const input = targetNode?.inputs?.[targetSlot]
313 0 : if (input?.type && rerouteType === '*') {
314 0 : rerouteType = input.type
315 : }
316 0 : if (input?.widget && targetNode?.type) {
317 0 : const targetDef = globalDefs[targetNode.type]
318 0 : const targetWidget =
319 : targetDef?.input?.required?.[input.widget.name] ??
320 : targetDef?.input?.optional?.[input.widget.name]
321 :
322 0 : if (targetWidget) {
323 0 : const widgetSpec = [targetWidget[0], config] as Parameters<
324 : typeof mergeIfValid
325 : >[4]
326 : const res = mergeIfValid(
327 : { widget: widgetSpec } as unknown as Parameters<
328 : typeof mergeIfValid
329 : >[0],
330 : targetWidget,
331 : false,
332 : undefined,
333 : widgetSpec
334 : )
335 0 : config = res.customConfig ?? config
336 : }
337 : }
338 : }
339 0 : } else if (linksTo) {
340 0 : const link = linksTo[0]
341 0 : if (link?.[0] != null) {
342 0 : const output = this.nodeData.nodes.at(link[0])?.outputs?.[link[1]]
343 0 : if (output) rerouteType = output.type
344 : }
345 : } else {
346 : // Reroute used as a pipe
347 0 : for (const l of this.nodeData.links) {
348 0 : if (l[2] === node.index && isSlotType(l[5])) {
349 0 : rerouteType = l[5]
350 0 : break
351 : }
352 : }
353 0 : if (rerouteType === '*') {
354 : // Check for an external link
355 0 : const t = this.externalFrom[nodeIndex]?.[0]
356 0 : if (t) {
357 0 : rerouteType = t
358 : }
359 : }
360 : }
361 :
362 0 : config.forceInput = true
363 0 : const rerouteTypeKey = slotTypeKey(rerouteType)
364 0 : return {
365 : input: {
366 : required: {
367 : [rerouteTypeKey]: [rerouteTypeKey, config]
368 : }
369 : },
370 : output: [rerouteTypeKey],
371 : output_name: [],
372 : output_is_list: []
373 : }
374 : }
375 :
376 0 : console.warn(
377 : 'Skipping virtual node ' +
378 : node.type +
379 : ' when building group node ' +
380 : this.name
381 : )
382 : }
383 :
384 : getInputConfig(
385 : node: GroupNodeData,
386 : inputName: string,
387 : seenInputs: Record<string, number>,
388 : config: InputSpec,
389 : extra?: Record<string, unknown>
390 : ) {
391 : const nodeConfig = this.nodeData.config?.[node.index ?? -1]
392 68 : const customConfig = nodeConfig?.input?.[inputName]
393 68 : let name =
394 : customConfig?.name ??
395 : node.inputs?.find((inp) => inp.name === inputName)?.label ??
396 : inputName
397 68 : let key = name
398 68 : let prefix = ''
399 : // Special handling for primitive to include the title if it is set rather than just "value"
400 68 : if ((node.type === 'PrimitiveNode' && node.title) || name in seenInputs) {
401 16 : prefix = `${node.title ?? node.type} `
402 16 : key = name = `${prefix}${inputName}`
403 16 : if (name in seenInputs) {
404 0 : name = `${prefix}${seenInputs[name]} ${inputName}`
405 : }
406 : }
407 68 : seenInputs[key] = (seenInputs[key] ?? 1) + 1
408 :
409 68 : if (inputName === 'seed' || inputName === 'noise_seed') {
410 5 : if (!extra) extra = {}
411 5 : extra.control_after_generate = `${prefix}control_after_generate`
412 : }
413 68 : if (config[0] === 'IMAGEUPLOAD') {
414 0 : if (!extra) extra = {}
415 0 : const nodeIndex = node.index ?? -1
416 0 : const configOptions = config[1] ?? {}
417 0 : const widgetKey =
418 : 'widget' in configOptions && typeof configOptions.widget === 'string'
419 : ? configOptions.widget
420 : : 'image'
421 0 : extra.widget = this.oldToNewWidgetMap[nodeIndex]?.[widgetKey] ?? 'image'
422 : }
423 :
424 68 : if (extra) {
425 7 : config = [config[0], { ...config[1], ...extra }]
426 : }
427 :
428 68 : return { name, config, customConfig }
429 : }
430 :
431 : processWidgetInputs(
432 : inputs: Record<string, unknown>,
433 : node: GroupNodeData,
434 : inputNames: string[],
435 : seenInputs: Record<string, number>
436 : ) {
437 26 : const slots: string[] = []
438 26 : const converted = new Map<number, string>()
439 26 : const nodeIndex = node.index ?? -1
440 26 : const widgetMap: Record<string, string | null> = (this.oldToNewWidgetMap[
441 : nodeIndex
442 : ] = {})
443 26 : for (const inputName of inputNames) {
444 93 : const inputSpec = inputs[inputName]
445 : // The type is either a string (e.g. 'INT') or, for a combo widget, an
446 : // array of options (e.g. [['euler', 'ddim']]) — both are valid V1
447 : // input specs. Rejecting the array form here misclassified every combo
448 : // widget as a plain input slot.
449 93 : const isValidSpec =
450 : Array.isArray(inputSpec) &&
451 : inputSpec.length >= 1 &&
452 : (typeof inputSpec[0] === 'string' || Array.isArray(inputSpec[0]))
453 : // A `forceInput` spec is always a socket, even for a type (e.g. a
454 : // combo) that would otherwise materialize as a widget — matches the
455 : // rule `litegraphService.addInputSocket`/`addInputWidget` apply when
456 : // actually building the node.
457 93 : const specOptions =
458 : Array.isArray(inputSpec) &&
459 : typeof inputSpec[1] === 'object' &&
460 : inputSpec[1] !== null
461 : ? inputSpec[1]
462 : : {}
463 93 : const isForcedInput =
464 : 'forceInput' in specOptions && specOptions.forceInput === true
465 93 : if (
466 : isValidSpec &&
467 : !isForcedInput &&
468 : useWidgetStore().inputIsWidget(inputSpec as InputSpec)
469 : ) {
470 59 : const convertedIndex =
471 : node.inputs?.findIndex(
472 : (inp) => inp.name === inputName && inp.widget?.name === inputName
473 : ) ?? -1
474 59 : if (convertedIndex > -1) {
475 : // This widget has been converted to a widget
476 : // We need to store this in the correct position so link ids line up
477 7 : converted.set(convertedIndex, inputName)
478 7 : widgetMap[inputName] = null
479 : } else {
480 : // Normal widget
481 52 : const { name, config } = this.getInputConfig(
482 : node,
483 : inputName,
484 : seenInputs,
485 : inputSpec as InputSpec
486 : )
487 52 : if (this.nodeDef?.input?.required) {
488 52 : this.nodeDef.input.required[name] = config
489 : }
490 52 : widgetMap[inputName] = name
491 52 : this.newToOldWidgetMap[name] = { node, inputName }
492 : }
493 : } else {
494 : // Normal input
495 34 : slots.push(inputName)
496 : }
497 : }
498 26 : return { converted, slots }
499 : }
500 :
501 : checkPrimitiveConnection(
502 : link: GroupNodeLink,
503 : inputName: string,
504 : inputs: Record<string, InputSpec>
505 : ) {
506 25 : const [sourceNodeIndex, , targetNodeIndex] = link
507 25 : if (sourceNodeIndex == null) return
508 : const sourceNode = this.nodeData.nodes.at(sourceNodeIndex)
509 25 : if (sourceNode?.type === 'PrimitiveNode') {
510 : // Merge link configurations
511 4 : const primitiveDef = this.primitiveDefs[sourceNodeIndex]
512 4 : if (!primitiveDef) return
513 4 : const targetWidget = inputs[inputName]
514 4 : const primitiveConfig = primitiveDef.input.required.value as [
515 : unknown,
516 : Record<string, unknown>
517 : ]
518 4 : const config = mergeIfValid(
519 : {},
520 : targetWidget,
521 : false,
522 : undefined,
523 : primitiveConfig as InputSpec
524 : )
525 4 : const inputConfig = inputs[inputName]?.[1]
526 4 : primitiveConfig[1] =
527 : (config.customConfig ?? inputConfig)
528 : ? { ...(typeof inputConfig === 'object' ? inputConfig : {}) }
529 : : {}
530 :
531 4 : const widgetName = this.oldToNewWidgetMap[sourceNodeIndex]?.['value']
532 4 : if (widgetName) {
533 4 : const name = widgetName.substring(0, widgetName.length - 6)
534 4 : primitiveConfig[1].control_after_generate = true
535 4 : primitiveConfig[1].control_prefix = name
536 : }
537 :
538 4 : const toPrimitive = (this.widgetToPrimitive[targetNodeIndex] ??= {})
539 4 : const existing = toPrimitive[inputName]
540 4 : if (Array.isArray(existing)) {
541 0 : existing.push(sourceNodeIndex)
542 4 : } else if (typeof existing === 'number') {
543 0 : toPrimitive[inputName] = [existing, sourceNodeIndex]
544 : } else {
545 4 : toPrimitive[inputName] = sourceNodeIndex
546 : }
547 :
548 4 : ;(this.primitiveToWidget[sourceNodeIndex] ??= []).push({
549 : nodeId: targetNodeIndex,
550 : inputName
551 : })
552 : }
553 : }
554 :
555 : processInputSlots(
556 : inputs: Record<string, InputSpec>,
557 : node: GroupNodeData,
558 : slots: string[],
559 : linksTo: SlotLinks,
560 : inputMap: Record<string, number>,
561 : seenInputs: Record<string, number>
562 : ) {
563 26 : const nodeIdx = node.index ?? -1
564 26 : const nodeInputs: Record<string, string> = (this.nodeInputs[nodeIdx] = {})
565 26 : for (let i = 0; i < slots.length; i++) {
566 34 : const inputName = slots[i]
567 : // `linksTo` is keyed by this inner node's own original input-slot
568 : // index, which rarely lines up with `i` (this input's position within
569 : // the *filtered* `slots` list). Look the real slot index up by name so
570 : // an internally-linked input isn't matched against a different slot's
571 : // link (or missed/misread entirely) and either wrongly hidden or
572 : // wrongly exposed on the group node. A synthesized def can key its
573 : // input by type rather than by slot name (e.g. Reroute, whose actual
574 : // slot name is always ''), so fall back to the old positional index
575 : // when the name lookup misses.
576 34 : const namedSlotIndex = node.inputs?.findIndex(
577 : (inp) => inp.name === inputName
578 : )
579 : const slotIndex =
580 : namedSlotIndex != null && namedSlotIndex >= 0 ? namedSlotIndex : i
581 : const link = linksTo[slotIndex]
582 34 : if (link) {
583 21 : this.checkPrimitiveConnection(link, inputName, inputs)
584 : // This input is linked so we can skip it
585 21 : continue
586 : }
587 :
588 13 : const { name, config, customConfig } = this.getInputConfig(
589 : node,
590 : inputName,
591 : seenInputs,
592 : inputs[inputName]
593 : )
594 :
595 13 : nodeInputs[inputName] = name
596 34 : if (customConfig?.visible === false) continue
597 :
598 34 : if (this.nodeDef?.input?.required) {
599 13 : this.nodeDef.input.required[name] = config
600 : }
601 13 : inputMap[inputName] = this.inputCount++
602 : }
603 : }
604 :
605 : processConvertedWidgets(
606 : inputs: Record<string, InputSpec>,
607 : node: GroupNodeData,
608 : converted: Map<number, string>,
609 : linksTo: SlotLinks,
610 : inputMap: Record<string, number>,
611 : seenInputs: Record<string, number>
612 : ) {
613 : // Process converted widgets sorted into their index order (ordered as
614 : // they were converted) so link ids match up. `converted`'s keys are
615 : // this node's real serialized input-slot indices (set by the
616 : // `findIndex` in processWidgetInputs), so use that key - not this
617 : // widget's position among converted widgets - to look its link up in
618 : // `linksTo`, which is also keyed by real slot index.
619 26 : const convertedEntries = [...converted.entries()].sort(([a], [b]) => a - b)
620 26 : for (const [slotIndex, inputName] of convertedEntries) {
621 7 : if (!inputName) continue
622 7 : const link = linksTo[slotIndex]
623 7 : if (link) {
624 4 : this.checkPrimitiveConnection(link, inputName, inputs)
625 : // This input is linked so we can skip it
626 4 : continue
627 : }
628 :
629 3 : const { name, config } = this.getInputConfig(
630 : node,
631 : inputName,
632 : seenInputs,
633 : inputs[inputName],
634 : {
635 : defaultInput: true
636 : }
637 : )
638 :
639 7 : if (this.nodeDef?.input?.required) {
640 3 : this.nodeDef.input.required[name] = config
641 : }
642 3 : this.newToOldWidgetMap[name] = { node, inputName }
643 :
644 7 : const nodeIndex = node.index ?? -1
645 7 : ;(this.oldToNewWidgetMap[nodeIndex] ??= {})[inputName] = name
646 :
647 7 : inputMap[inputName] = this.inputCount++
648 : }
649 : }
650 :
651 : private _convertedToProcess: (() => void)[] = []
652 : processNodeInputs(
653 : node: GroupNodeData,
654 : seenInputs: Record<string, number>,
655 : inputs: Record<string, unknown>
656 : ) {
657 26 : const inputMapping: unknown[] = []
658 :
659 26 : const inputNames = Object.keys(inputs)
660 26 : if (!inputNames.length) return
661 :
662 26 : const { converted, slots } = this.processWidgetInputs(
663 : inputs,
664 : node,
665 : inputNames,
666 : seenInputs
667 : )
668 26 : const nodeIndex = node.index ?? -1
669 26 : const linksTo = this.linksTo[nodeIndex] ?? {}
670 26 : const inputMap: Record<string, number> = (this.oldToNewInputMap[nodeIndex] =
671 : {})
672 26 : this.processInputSlots(
673 : inputs as Record<string, InputSpec>,
674 : node,
675 : slots,
676 : linksTo,
677 : inputMap,
678 : seenInputs
679 : )
680 :
681 : // Converted inputs have to be processed after all other nodes as they'll be at the end of the list
682 26 : this._convertedToProcess.push(() =>
683 : this.processConvertedWidgets(
684 : inputs as Record<string, InputSpec>,
685 : node,
686 : converted,
687 : linksTo,
688 : inputMap,
689 : seenInputs
690 : )
691 : )
692 :
693 26 : return inputMapping
694 : }
695 :
696 : processNodeOutputs(
697 : node: GroupNodeData,
698 : seenOutputs: Record<string, number>,
699 : def: GroupNodeDef | ComfyNodeDef
700 : ) {
701 26 : const nodeIndex = node.index ?? -1
702 26 : const oldToNew: Record<number, number> = (this.oldToNewOutputMap[
703 : nodeIndex
704 : ] = {})
705 :
706 26 : const defOutput = def.output ?? []
707 : // Add outputs
708 26 : for (let outputId = 0; outputId < defOutput.length; outputId++) {
709 : const linksFrom = this.linksFrom[nodeIndex]
710 : // If this output is linked internally we flag it to hide
711 30 : const hasLink =
712 : linksFrom?.[outputId] && !this.externalFrom[nodeIndex]?.[outputId]
713 : const outputConfig = this.nodeData.config?.[node.index ?? -1]
714 30 : const customConfig = outputConfig?.output?.[outputId]
715 30 : const visible = customConfig?.visible ?? !hasLink
716 30 : this.outputVisibility.push(visible)
717 30 : if (!visible) {
718 17 : continue
719 : }
720 :
721 13 : const output = defOutput[outputId]
722 30 : if (!isOutputTypeSpec(output)) continue
723 :
724 30 : if (this.nodeDef?.output) {
725 13 : oldToNew[outputId] = this.nodeDef.output.length
726 13 : this.newToOldOutputMap[this.nodeDef.output.length] = {
727 : node,
728 : slot: outputId
729 : }
730 13 : this.nodeDef.output.push(output)
731 13 : this.nodeDef.output_is_list?.push(
732 : def.output_is_list?.[outputId] ?? false
733 : )
734 : }
735 :
736 30 : let label: string | undefined = customConfig?.name
737 30 : if (!label) {
738 13 : const outputVal = defOutput[outputId]
739 13 : label =
740 : def.output_name?.[outputId] ??
741 : (typeof outputVal === 'string' ? outputVal : undefined)
742 13 : const output = node.outputs?.find((o) => o.name === label)
743 13 : if (output?.label) {
744 2 : label = output.label
745 : }
746 : }
747 :
748 30 : let name = label ?? `output_${outputId}`
749 30 : if (name in seenOutputs) {
750 4 : const prefix = `${node.title ?? node.type} `
751 4 : name = `${prefix}${label ?? outputId}`
752 4 : if (name in seenOutputs) {
753 0 : name = `${prefix}${node.index} ${label ?? outputId}`
754 : }
755 : }
756 13 : seenOutputs[name] = 1
757 :
758 30 : this.nodeDef?.output_name?.push(name)
759 : }
760 : }
761 :
762 : static async registerFromWorkflow(
763 : groupNodes: Record<string, GroupNodeWorkflowData>,
764 : missingNodeTypes: (
765 : | string
766 : | {
767 : type: string
768 : nodeId?: string | number
769 : hint?: string
770 : action?: unknown
771 : }
772 : )[],
773 : instanceIdsByGroup?: Map<string, (string | number)[]>
774 : ) {
775 5 : for (const g in groupNodes) {
776 8 : const groupData = groupNodes[g]
777 :
778 8 : const missingInnerTypes = [
779 : ...new Set(
780 : groupData.nodes
781 : .filter(
782 : (n) => !n.type || !(n.type in LiteGraph.registered_node_types)
783 : )
784 : .map((n) => n.type ?? 'unknown')
785 : )
786 : ]
787 :
788 8 : if (missingInnerTypes.length) {
789 1 : const groupType = `${PREFIX}${SEPARATOR}${g}`
790 : const registeredGroupType = LiteGraph.registered_node_types[
791 : groupType
792 : ] as LGraphNodeConstructor | undefined
793 1 : if (registeredGroupType?.nodeData?.[GROUP]) {
794 0 : LiteGraph.unregisterNodeType(groupType)
795 0 : useNodeDefStore().removeNodeDef(groupType)
796 : }
797 1 : const removeAction = {
798 : text: 'Remove from workflow',
799 : callback: (e?: MouseEvent) => {
800 0 : delete groupNodes[g]
801 0 : const target = e?.target as HTMLElement | undefined
802 0 : if (!target) return
803 0 : target.textContent = 'Removed'
804 0 : target.style.pointerEvents = 'none'
805 0 : target.style.opacity = '0.7'
806 : }
807 : }
808 1 : if (instanceIdsByGroup) {
809 : // One report per canvas instance so every entry is backed by a
810 : // real node and can be retired when that node goes away. A group
811 : // definition with no instances gets no report: there is no node
812 : // to locate, and nothing that could ever retire the row.
813 1 : for (const id of instanceIdsByGroup.get(g) ?? []) {
814 0 : missingNodeTypes.push({
815 : type: groupType,
816 : nodeId: String(id),
817 : hint: t('g.missingNodeTypesInGroup', {
818 : types: missingInnerTypes.join(', ')
819 : }),
820 : action: removeAction
821 : })
822 : }
823 : } else {
824 0 : for (const missingType of missingInnerTypes) {
825 0 : missingNodeTypes.push({
826 : type: missingType,
827 : hint: ` (In group node '${groupType}')`
828 : })
829 : }
830 0 : missingNodeTypes.push({ type: groupType, action: removeAction })
831 : }
832 1 : continue
833 : }
834 :
835 : const config = new GroupNodeConfig(g, groupData)
836 7 : await config.registerType()
837 : }
838 : }
839 : }
840 :
841 : /**
842 : * Finds the outer widget matching `name`, skipping any index already in
843 : * `consumed`.
844 : *
845 : * Widget names inside a synthesized group-node type are meant to be unique
846 : * (see {@link GroupNodeConfig.getInputConfig}'s de-duplication), so pairing
847 : * by name alone is normally safe. But it is not *guaranteed* unique across
848 : * every inner node/widget combination, and a plain `Array.findIndex` always
849 : * resolves a shared name to the *first* matching widget — silently copying
850 : * that node's value into every other inner node exposing a widget with the
851 : * same final name. Consuming each match exactly once, in the same
852 : * node-by-node order the inner nodes are unpacked, keeps every widget paired
853 : * with the widget belonging to its own originating node even when two
854 : * inner nodes end up sharing an outer widget name (e.g. two `CLIPTextEncode`
855 : * nodes both exposing `text`).
856 : */
857 41 : export function findUnconsumedWidgetIndex(
858 : widgets: { name?: string }[] | undefined,
859 : name: string,
860 : consumed: Set<number>
861 : ): number {
862 41 : if (!widgets) return -1
863 41 : for (let i = 0; i < widgets.length; i++) {
864 332 : if (consumed.has(i)) continue
865 332 : if (widgets[i]?.name === name) return i
866 : }
867 0 : return -1
868 : }
869 :
870 : /**
871 : * Migration-only adapter for deprecated group nodes.
872 : *
873 : * Group nodes are no longer a supported feature. When a legacy workflow that
874 : * contains group nodes is loaded, {@link GroupNodeConfig.registerFromWorkflow}
875 : * synthesizes temporary node types so the instances can be created during
876 : * `configure`. The load-time migration unpacks each instance via
877 : * {@link convertToNodes} and {@link LGraph.convertToSubgraph} repackages the
878 : * result as a subgraph.
879 : *
880 : */
881 2355 : export class GroupNodeHandler {
882 : node: LGraphNode
883 : groupData: GroupNodeConfig
884 :
885 : constructor(node: LGraphNode, groupData: GroupNodeConfig) {
886 3 : this.node = node
887 3 : this.groupData = groupData
888 : }
889 :
890 : /**
891 : * Unpacks this group node into its constituent nodes within the root graph,
892 : * copying current widget values and reconnecting external links, then removes
893 : * the group node. Returns the newly created nodes.
894 : *
895 : * Lossiness is accepted: group nodes nested inside subgraphs are unpacked into
896 : * the root graph rather than their original container.
897 : */
898 : convertToNodes(): LGraphNode[] {
899 3 : const node = this.node
900 3 : const { nodeData, oldToNewWidgetMap, oldToNewInputMap, newToOldOutputMap } =
901 : this.groupData
902 :
903 3 : const addInnerNodes = () => {
904 : // Clone the node data so we don't mutate it for other instances
905 3 : const c = { ...nodeData }
906 3 : c.nodes = c.nodes.map((n) => ({ ...n, id: undefined }))
907 3 : deserialiseAndCreate(JSON.stringify(c), app.canvas)
908 :
909 3 : const [x, y] = node.pos
910 3 : let top: number | undefined
911 3 : let left: number | undefined
912 : // deserialiseAndCreate selects the created nodes in creation order, which
913 : // matches nodeData.nodes order.
914 3 : const selectedIds = selectedCanvasNodes(app.canvas).map((n) => n.id)
915 3 : const newNodes: LGraphNode[] = []
916 : // Shared across every inner node processed below, in nodeData.nodes
917 : // order, so a widget already paired to one inner node's value can
918 : // never be matched again for another (see findUnconsumedWidgetIndex).
919 3 : const consumedOuterWidgetIndices = new Set<number>()
920 3 : for (let i = 0; i < selectedIds.length; i++) {
921 16 : const selectedId = parseNodeId(selectedIds[i])
922 16 : const newNode = selectedId
923 : ? app.rootGraph.getNodeById(selectedId)
924 : : null
925 16 : const innerNodeData = nodeData.nodes.at(i)
926 16 : if (!newNode) continue
927 16 : newNodes.push(newNode)
928 :
929 16 : if (left == null || newNode.pos[0] < left) left = newNode.pos[0]
930 16 : if (top == null || newNode.pos[1] < top) top = newNode.pos[1]
931 :
932 16 : if (!newNode.widgets || !innerNodeData) continue
933 :
934 14 : const map = oldToNewWidgetMap[i]
935 16 : if (!map) continue
936 16 : for (const oldName of Object.keys(map)) {
937 41 : const newName = map[oldName]
938 41 : if (!newName) continue
939 :
940 41 : const widgetIndex = findUnconsumedWidgetIndex(
941 : node.widgets,
942 : newName,
943 : consumedOuterWidgetIndices
944 : )
945 41 : if (widgetIndex === -1) continue
946 :
947 : // An index is only marked consumed once its value is actually
948 : // copied, so a bail below (a missing inner widget, or fewer outer
949 : // widgets than this PrimitiveNode has) can't permanently burn an
950 : // index another inner node legitimately needs.
951 41 : if (innerNodeData.type === 'PrimitiveNode') {
952 0 : for (let j = 0; j < newNode.widgets.length; j++) {
953 0 : const srcWidget = node.widgets?.[widgetIndex + j]
954 0 : if (!srcWidget) continue
955 0 : consumedOuterWidgetIndices.add(widgetIndex + j)
956 0 : newNode.widgets[j].value = srcWidget.value
957 : }
958 : } else {
959 41 : const outerWidget = node.widgets?.[widgetIndex]
960 41 : const newWidget = newNode.widgets.find((w) => w.name === oldName)
961 41 : if (!newWidget || !outerWidget) continue
962 :
963 41 : consumedOuterWidgetIndices.add(widgetIndex)
964 41 : newWidget.value = outerWidget.value
965 41 : const linkedWidgets = outerWidget.linkedWidgets ?? []
966 41 : for (let w = 0; w < linkedWidgets.length; w++) {
967 4 : const newLinked = newWidget.linkedWidgets?.[w]
968 4 : if (newLinked && linkedWidgets[w]) {
969 4 : newLinked.value = linkedWidgets[w].value
970 : }
971 : }
972 : }
973 : }
974 : }
975 :
976 : // Shift each node so the unpacked group appears at the group node position
977 16 : for (const newNode of newNodes) {
978 16 : newNode.pos = [
979 : newNode.pos[0] - ((left ?? 0) - x),
980 : newNode.pos[1] - ((top ?? 0) - y)
981 : ]
982 : }
983 :
984 3 : return { newNodes, selectedIds }
985 : }
986 :
987 3 : const reconnectInputs = (selectedIds: (string | number)[]) => {
988 3 : for (const [innerNodeIndex, map] of Object.entries(oldToNewInputMap)) {
989 16 : const selectedId = parseNodeId(selectedIds[Number(innerNodeIndex)])
990 16 : const newNode = selectedId
991 : ? app.rootGraph.getNodeById(selectedId)
992 : : null
993 16 : if (!newNode || !map) continue
994 16 : for (const [innerInputName, groupSlotId] of Object.entries(map)) {
995 8 : const link = node.getInputLink(groupSlotId)
996 8 : if (!link) continue
997 0 : const originNode = app.rootGraph.getNodeById(link.origin_id)
998 0 : const innerInputId = newNode.findInputSlot(innerInputName)
999 0 : if (innerInputId !== -1) {
1000 0 : originNode?.connect(link.origin_slot, newNode, innerInputId)
1001 : }
1002 : }
1003 : }
1004 : }
1005 :
1006 3 : const reconnectOutputs = (selectedIds: (string | number)[]) => {
1007 3 : for (
1008 : let groupOutputId = 0;
1009 : groupOutputId < node.outputs.length;
1010 : groupOutputId++
1011 : ) {
1012 4 : const links = outputLinks(app.rootGraph, node.id, groupOutputId)
1013 4 : for (const link of links) {
1014 0 : const slot = newToOldOutputMap[groupOutputId]
1015 0 : if (!slot) continue
1016 0 : const targetNode = app.rootGraph.getNodeById(link.target_id)
1017 0 : const selectedId = parseNodeId(selectedIds[slot.node.index ?? 0])
1018 0 : const newNode = selectedId
1019 : ? app.rootGraph.getNodeById(selectedId)
1020 : : null
1021 0 : if (targetNode) {
1022 0 : newNode?.connect(slot.slot, targetNode, link.target_slot)
1023 : }
1024 : }
1025 : }
1026 : }
1027 :
1028 3 : app.canvas.emitBeforeChange()
1029 3 : try {
1030 3 : const { newNodes, selectedIds } = addInnerNodes()
1031 3 : reconnectInputs(selectedIds)
1032 3 : reconnectOutputs(selectedIds)
1033 3 : app.rootGraph.remove(this.node)
1034 3 : return newNodes
1035 : } finally {
1036 3 : app.canvas.emitAfterChange()
1037 : }
1038 : }
1039 :
1040 : static getHandler(node: LGraphNode): GroupNodeHandler | undefined {
1041 6 : const cached = (node as LGraphNode & { [GROUP]?: GroupNodeHandler })[GROUP]
1042 6 : if (cached) return cached
1043 3 : const groupData = groupConfigOf(node)
1044 6 : if (!groupData) return undefined
1045 3 : const handler = new GroupNodeHandler(node, groupData)
1046 3 : ;(node as LGraphNode & { [GROUP]: GroupNodeHandler })[GROUP] = handler
1047 3 : return handler
1048 : }
1049 :
1050 : static isGroupNode(node: LGraphNode) {
1051 44381 : return groupConfigOf(node) !== undefined
1052 : }
1053 : }
1054 :
1055 44384 : function groupConfigOf(node: LGraphNode): GroupNodeConfig | undefined {
1056 44384 : const config = node.constructor.nodeData?.[GROUP]
1057 44384 : return config instanceof GroupNodeConfig ? config : undefined
1058 : }
1059 :
1060 2355 : export const replaceLegacySeparators = (nodes: ComfyNode[]): void => {
1061 5 : for (const node of nodes) {
1062 25 : if (typeof node.type === 'string' && node.type.startsWith('workflow/')) {
1063 1 : node.type = node.type.replace(/^workflow\//, `${PREFIX}${SEPARATOR}`)
1064 : }
1065 : }
1066 : }
1067 :
1068 : /**
1069 : * Converts every group node in the root graph to a subgraph. Re-scans until none
1070 : * remain so group nodes revealed by a previous conversion are also migrated. A
1071 : * failed conversion removes the offending node so loading can continue (accepted
1072 : * lossiness) and the scan cannot loop forever.
1073 : * @returns the number of group nodes converted
1074 : */
1075 3897 : function convertLoadedGroupNodes(): number {
1076 3897 : let converted = 0
1077 3897 : const failed = new Set<LGraphNode>()
1078 3897 : for (;;) {
1079 3900 : const node = app.rootGraph.nodes.find(
1080 : (n) => GroupNodeHandler.isGroupNode(n) && !failed.has(n)
1081 : )
1082 3900 : if (!node) return converted
1083 9 : try {
1084 3 : const handler = GroupNodeHandler.getHandler(node)
1085 9 : if (!handler) {
1086 0 : console.error('Missing handler for group node')
1087 0 : failed.add(node)
1088 0 : app.rootGraph.remove(node)
1089 0 : continue
1090 : }
1091 3 : const innerNodes = handler.convertToNodes()
1092 17 : for (const inner of innerNodes) inner.updateArea()
1093 3 : app.rootGraph.convertToSubgraph(new Set(innerNodes))
1094 3 : converted++
1095 : } catch (error) {
1096 0 : console.error('Failed to convert group node to subgraph', error)
1097 0 : failed.add(node)
1098 0 : try {
1099 0 : app.rootGraph.remove(node)
1100 : } catch (removeError) {
1101 0 : console.error(
1102 : 'Failed to remove group node after conversion failure',
1103 : removeError
1104 : )
1105 : }
1106 : }
1107 : }
1108 : }
1109 :
1110 2355 : const groupNodeReportIndices = new WeakMap<
1111 : Exclude<MissingNodeType, string>,
1112 : number
1113 : >()
1114 :
1115 2355 : const id = 'Comfy.GroupNode'
1116 :
1117 : /**
1118 : * Global node definitions cache. Populated by `addCustomNodeDefs` during
1119 : * extension initialization and read by {@link GroupNodeConfig.getNodeDef} when
1120 : * synthesizing temporary group-node definitions during load.
1121 : */
1122 2355 : let globalDefs: Partial<Record<string, ComfyNodeDef>> = {}
1123 2355 : export const groupNodeExtension: ComfyExtension = {
1124 : name: id,
1125 : addCustomNodeDefs(defs: Record<string, ComfyNodeDef>) {
1126 2355 : globalDefs = defs
1127 : },
1128 : async beforeConfigureGraph(
1129 : graphData: ComfyWorkflowJSON,
1130 : missingNodeTypes: MissingNodeType[]
1131 : ) {
1132 3887 : const nodes = graphData.extra?.groupNodes as
1133 : | Record<string, GroupNodeWorkflowData>
1134 : | undefined
1135 3887 : if (nodes) {
1136 5 : replaceLegacySeparators(graphData.nodes)
1137 5 : const instanceIndicesByGroup = new Map<string, number[]>()
1138 5 : const groupTypePrefix = `${PREFIX}${SEPARATOR}`
1139 5 : for (const [nodeIndex, n] of graphData.nodes.entries()) {
1140 25 : if (!n.type.startsWith(groupTypePrefix)) continue
1141 3 : const groupName = n.type.slice(groupTypePrefix.length)
1142 25 : const indices = instanceIndicesByGroup.get(groupName) ?? []
1143 25 : indices.push(nodeIndex)
1144 25 : instanceIndicesByGroup.set(groupName, indices)
1145 : }
1146 5 : const groupNodeReports: MissingNodeType[] = []
1147 5 : await GroupNodeConfig.registerFromWorkflow(
1148 : nodes,
1149 : groupNodeReports,
1150 : instanceIndicesByGroup
1151 : )
1152 5 : for (const report of groupNodeReports) {
1153 0 : if (typeof report === 'string' || report.nodeId == null) continue
1154 0 : const nodeIndex = Number(report.nodeId)
1155 0 : if (Number.isInteger(nodeIndex)) {
1156 0 : groupNodeReportIndices.set(report, nodeIndex)
1157 : }
1158 : }
1159 5 : missingNodeTypes.push(...groupNodeReports)
1160 : }
1161 : },
1162 : afterConfigureGraph(missingNodeTypes) {
1163 3897 : for (let index = missingNodeTypes.length - 1; index >= 0; index--) {
1164 97 : const report = missingNodeTypes[index]
1165 97 : if (typeof report === 'string') continue
1166 97 : const nodeIndex = groupNodeReportIndices.get(report)
1167 97 : if (nodeIndex === undefined) continue
1168 0 : groupNodeReportIndices.delete(report)
1169 0 : const node = app.rootGraph.nodes.at(nodeIndex)
1170 0 : if (node) {
1171 0 : report.nodeId = String(node.id)
1172 : } else {
1173 0 : missingNodeTypes.splice(index, 1)
1174 : }
1175 : }
1176 3897 : if (convertLoadedGroupNodes() > 0) {
1177 3 : delete app.rootGraph.extra.groupNodes
1178 : }
1179 : },
1180 : nodeCreated(node: LGraphNode) {
1181 23115 : if (!GroupNodeHandler.isGroupNode(node)) return
1182 3 : const handler = GroupNodeHandler.getHandler(node)
1183 : // A stray group node created after load (e.g. paste) has no migration pass;
1184 : // convert it to a subgraph once it has joined a graph.
1185 59 : if (!app.configuringGraph) {
1186 0 : queueMicrotask(() => {
1187 0 : const graph = node.graph
1188 0 : if (graph && handler && GroupNodeHandler.isGroupNode(node)) {
1189 0 : try {
1190 0 : const innerNodes = handler.convertToNodes()
1191 0 : for (const inner of innerNodes) inner.updateArea()
1192 0 : graph.convertToSubgraph(new Set(innerNodes))
1193 : } catch (error) {
1194 0 : console.error(
1195 : 'Failed to convert stray group node to subgraph',
1196 : error
1197 : )
1198 : }
1199 : }
1200 : })
1201 : }
1202 : }
1203 : }
|