Line data Source code
1 : import { isEqual } from 'es-toolkit'
2 :
3 : import type { LGraphNode } from '@/lib/litegraph/src/litegraph'
4 : import type { InputSpec as InputSpecV2 } from '@comfyorg/object-info-parser/schemas/nodeDefSchemaV2'
5 : import type { IBaseWidget } from '@comfyorg/graph-model/widgets'
6 : import {
7 : dynamicComboOptionTypes,
8 : inputSpecTree
9 : } from '@/core/graph/nodeDef/inputSpecInterpreter'
10 :
11 : /**
12 : * Select DynamicCombo options so that `node` exposes a socket of `wantedType`.
13 : *
14 : * Node search reports the union of every DynamicCombo option's input types, so
15 : * a node can be offered for a link type whose socket only exists under an
16 : * option the node was not created with.
17 : *
18 : * Only reports success once the socket actually exists. A combo option's types
19 : * include types reachable through a *nested* combo, which selecting the outer
20 : * option alone does not materialize, so the result is verified rather than
21 : * assumed; any selection made while failing is rolled back so a failed reveal
22 : * leaves the node exactly as it was.
23 : *
24 : * @returns whether the node now exposes `wantedType`
25 : */
26 8 : export function revealDynamicInputSlot(
27 : node: LGraphNode,
28 : wantedType: string
29 : ): boolean {
30 8 : if (node.findInputByType(wantedType)) return false
31 :
32 8 : const defInputs = node.constructor.nodeData?.inputs
33 8 : if (!defInputs) return false
34 :
35 : // Each root input is attempted independently so a root that fails cannot
36 : // leave a combo selected on the way to a different root that succeeds.
37 2 : for (const rootSpec of Object.values(defInputs)) {
38 11 : if (revealWithin(node, rootSpec, wantedType)) return true
39 : }
40 :
41 1 : return false
42 : }
43 :
44 11 : function revealWithin(
45 : node: LGraphNode,
46 : rootSpec: InputSpecV2,
47 : wantedType: string
48 : ): boolean {
49 : const comboSpecs = inputSpecTree(rootSpec).filter(
50 : (spec) => spec.type === 'COMFY_DYNAMICCOMBO_V3'
51 : )
52 11 : return revealFromCombo(node, comboSpecs, wantedType, 0, new Set())
53 : }
54 :
55 11 : function revealFromCombo(
56 : node: LGraphNode,
57 : comboSpecs: InputSpecV2[],
58 : wantedType: string,
59 : startIndex: number,
60 : usedWidgets: Set<IBaseWidget>
61 : ): boolean {
62 11 : const widgets = node.widgets
63 11 : if (!widgets) return false
64 :
65 11 : for (let index = startIndex; index < comboSpecs.length; index++) {
66 1 : const spec = comboSpecs[index]
67 1 : const options = dynamicComboOptionTypes(spec)
68 1 : const matchingOptions = options.filter(({ types }) =>
69 : types.includes(wantedType)
70 : )
71 1 : if (!matchingOptions.length) continue
72 :
73 1 : const candidates = matchingWidgets(
74 : widgets,
75 : spec,
76 : options.map(({ key }) => key),
77 : usedWidgets
78 : )
79 :
80 1 : for (const option of matchingOptions) {
81 1 : for (const widget of candidates) {
82 1 : if (
83 : tryOption(
84 : node,
85 : comboSpecs,
86 : wantedType,
87 : index,
88 : usedWidgets,
89 : widget,
90 : option.key
91 : )
92 : )
93 1 : return true
94 : }
95 : }
96 : }
97 0 : return false
98 : }
99 :
100 1 : function matchingWidgets(
101 : widgets: IBaseWidget[],
102 : spec: InputSpecV2,
103 : keys: string[],
104 : usedWidgets: Set<IBaseWidget>
105 : ): IBaseWidget[] {
106 1 : return widgets
107 : .filter(
108 : (widget) =>
109 : !usedWidgets.has(widget) &&
110 : (widget.name === spec.name || isEqual(widget.options.values, keys))
111 : )
112 : .sort((a, b) => Number(b.name === spec.name) - Number(a.name === spec.name))
113 : }
114 :
115 1 : function tryOption(
116 : node: LGraphNode,
117 : comboSpecs: InputSpecV2[],
118 : wantedType: string,
119 : index: number,
120 : usedWidgets: Set<IBaseWidget>,
121 : widget: IBaseWidget,
122 : optionKey: string
123 : ): boolean {
124 1 : const previousValue = widget.value
125 1 : widget.value = optionKey
126 :
127 1 : const revealed =
128 : Boolean(node.findInputByType(wantedType)) ||
129 : revealFromCombo(
130 : node,
131 : comboSpecs,
132 : wantedType,
133 : index + 1,
134 : new Set(usedWidgets).add(widget)
135 : )
136 :
137 1 : if (!revealed) widget.value = previousValue
138 1 : return revealed
139 : }
|