Line data Source code
1 : import { cloneDeep, uniq } from 'es-toolkit/compat'
2 :
3 : import { resolveNodeDefText } from '@/i18n'
4 : import {
5 : inputSpecInterpreter,
6 : validatedInputSpecInterpreter
7 : } from '@/core/graph/nodeDef/inputSpecInterpreter'
8 : import { transformNodeDefV1ToV2 } from '@comfyorg/object-info-parser/migration'
9 : import type {
10 : ComfyNodeDef as ComfyNodeDefV2,
11 : InputSpec as InputSpecV2,
12 : OutputSpec as OutputSpecV2
13 : } from '@comfyorg/object-info-parser/schemas/nodeDefSchemaV2'
14 : import type {
15 : ComfyInputsSpec as ComfyInputSpecV1,
16 : ComfyNodeDef as ComfyNodeDefV1,
17 : ComfyOutputTypesSpec as ComfyOutputSpecV1,
18 : PriceBadge
19 : } from '@comfyorg/object-info-parser'
20 : import { NODE_TO_ESSENTIALS_CATEGORY } from '@/constants/essentialsNodes'
21 : import { useNodeFrequencyStore } from '@/stores/nodeFrequencyStore'
22 : import { CORE_NODE_MODULES, getNodeSource } from '@/types/nodeSource'
23 : import type { NodeSource } from '@/types/nodeSource'
24 : import type { FuseSearchable, SearchAuxScore } from '@/utils/fuseUtil'
25 :
26 2356 : export class ComfyNodeDefImpl
27 : implements ComfyNodeDefV1, ComfyNodeDefV2, FuseSearchable
28 : {
29 : // ComfyNodeDef fields (V1)
30 : readonly name: string
31 : /**
32 : * Category is not marked as readonly as the bookmark system
33 : * needs to write to it to assign a node to a custom folder.
34 : */
35 : category: string
36 : readonly main_category?: string
37 : readonly python_module: string
38 : readonly help: string
39 : readonly deprecated: boolean
40 : readonly experimental: boolean
41 : readonly dev_only: boolean
42 : readonly output_node: boolean
43 : readonly api_node: boolean
44 : /**
45 : * @deprecated Use `inputs` instead
46 : */
47 : readonly input: ComfyInputSpecV1
48 : /**
49 : * @deprecated Use `outputs` instead
50 : */
51 : readonly output: ComfyOutputSpecV1
52 : /**
53 : * @deprecated Use `outputs[n].is_list` instead
54 : */
55 : readonly output_is_list?: boolean[]
56 : /**
57 : * @deprecated Use `outputs[n].name` instead
58 : */
59 : readonly output_name?: string[]
60 : /**
61 : * @deprecated Use `outputs[n].tooltip` instead
62 : */
63 : readonly output_tooltips?: string[]
64 : /**
65 : * Order of inputs for each category (required, optional, hidden)
66 : */
67 : readonly input_order?: Record<string, string[]>
68 : /**
69 : * Price badge definition for API nodes.
70 : * Contains a JSONata expression to calculate pricing based on widget values
71 : * and input connectivity.
72 : */
73 : readonly price_badge?: PriceBadge
74 : /**
75 : * Alternative names for search. Useful for synonyms, abbreviations,
76 : * or old names after renaming a node.
77 : */
78 : readonly search_aliases?: string[]
79 : /** Category for the Essentials tab. If set, the node appears in Essentials. */
80 : readonly essentials_category?: string
81 : /** Whether the blueprint is a global/installed blueprint (not user-created). */
82 : readonly isGlobal?: boolean
83 : readonly isCoreNode: boolean
84 :
85 : // V2 fields
86 : readonly inputs: Record<string, InputSpecV2>
87 : readonly outputs: OutputSpecV2[]
88 : readonly hidden?: Record<string, unknown>
89 :
90 : // ComfyNodeDefImpl fields
91 : readonly nodeSource: NodeSource
92 : readonly inputTypes: string[]
93 : readonly outputTypes: string[]
94 :
95 : /**
96 : * Raw `/object_info` text, kept unresolved so `display_name` and
97 : * `description` can be resolved against the active locale on every read.
98 : * Declared with TypeScript `private` rather than `#private`: Vue wraps store
99 : * instances in a Proxy, and `#private` reads throw through one.
100 : */
101 : private readonly backendDisplayName?: string
102 : private readonly backendDescription?: string
103 :
104 : /**
105 : * @internal
106 : * Migrate default input options to forceInput.
107 : */
108 : private static _migrateDefaultInput(nodeDef: ComfyNodeDefV1): ComfyNodeDefV1 {
109 : const usesDefaultInput = [
110 : ...Object.values(nodeDef.input?.required ?? {}),
111 : ...Object.values(nodeDef.input?.optional ?? {})
112 : ].some((spec) => spec[1]?.defaultInput)
113 5154403 : if (!usesDefaultInput) return { ...nodeDef, input: nodeDef.input ?? {} }
114 4735 : const def = cloneDeep(nodeDef)
115 4735 : def.input ??= {}
116 : // For required inputs, now we have the input socket always present. Specifying
117 : // it now has no effect.
118 5154403 : for (const [name, spec] of Object.entries(def.input.required ?? {})) {
119 9492 : const inputOptions = spec[1]
120 9492 : if (inputOptions && inputOptions.defaultInput) {
121 4739 : console.warn(
122 : `Use of defaultInput on required input ${nodeDef.python_module}:${nodeDef.name}:${name} is deprecated. Please drop the defaultInput option.`
123 : )
124 : }
125 : }
126 : // For optional inputs, defaultInput is used to distinguish the null state.
127 : // We migrate it to forceInput. One example is the "seed_override" input usage.
128 : // User can connect the socket to override the seed.
129 5154403 : for (const [name, spec] of Object.entries(def.input.optional ?? {})) {
130 4733 : const inputOptions = spec[1]
131 4733 : if (inputOptions && inputOptions.defaultInput) {
132 4733 : console.warn(
133 : `Use of defaultInput on optional input ${nodeDef.python_module}:${nodeDef.name}:${name} is deprecated. Please use forceInput instead.`
134 : )
135 4733 : inputOptions.forceInput = true
136 : }
137 : }
138 4735 : return def
139 : }
140 :
141 : constructor(
142 : def: ComfyNodeDefV1,
143 : { validateInputs = false }: { validateInputs?: boolean } = {}
144 : ) {
145 5154403 : const obj = ComfyNodeDefImpl._migrateDefaultInput(def)
146 :
147 5154403 : const { display_name, description, ...assignable } = obj
148 5154403 : Object.assign(this, assignable)
149 :
150 5154403 : this.name = obj.name
151 5154403 : this.backendDisplayName = display_name || undefined
152 5154403 : this.backendDescription = description || undefined
153 5154403 : this.category = obj.category
154 5154403 : this.main_category = obj.main_category
155 5154403 : this.python_module = obj.python_module
156 5154403 : this.help = obj.help ?? ''
157 5154403 : this.deprecated = obj.deprecated ?? obj.category === ''
158 5154403 : this.experimental =
159 : obj.experimental ?? obj.category.startsWith('_for_testing')
160 5154403 : this.dev_only = obj.dev_only ?? false
161 5154403 : this.output_node = obj.output_node
162 5154403 : this.api_node = !!obj.api_node
163 5154403 : this.input = obj.input ?? {}
164 5154403 : this.output = obj.output ?? []
165 5154403 : this.output_is_list = obj.output_is_list
166 5154403 : this.output_name = obj.output_name
167 5154403 : this.output_tooltips = obj.output_tooltips
168 5154403 : this.input_order = obj.input_order
169 5154403 : this.price_badge = obj.price_badge
170 5154403 : this.essentials_category =
171 : NODE_TO_ESSENTIALS_CATEGORY[obj.name] ?? obj.essentials_category
172 5154403 : this.isGlobal = obj.isGlobal
173 5154403 : this.isCoreNode = CORE_NODE_MODULES.includes(
174 : this.python_module.split('.')[0]
175 : )
176 :
177 5154403 : const defV2 = transformNodeDefV1ToV2(obj)
178 5154403 : this.inputs = defV2.inputs
179 5154403 : this.outputs = defV2.outputs
180 5154403 : this.hidden = defV2.hidden
181 :
182 5154403 : this.nodeSource = getNodeSource(obj.python_module, this.essentials_category)
183 5154403 : const { collectSearchableInputTypes, collectSearchableOutputTypes } =
184 : validateInputs ? validatedInputSpecInterpreter : inputSpecInterpreter
185 5154403 : this.inputTypes = uniq(
186 : Object.values(this.inputs).flatMap(collectSearchableInputTypes)
187 : )
188 5154403 : this.outputTypes = uniq(
189 : collectSearchableOutputTypes(
190 : this.outputs,
191 : this.inputs,
192 : obj.output_matchtypes
193 : )
194 : )
195 : }
196 :
197 : /**
198 : * Resolved against the active locale on read, so a locale switch retitles
199 : * every def without refetching `/object_info`.
200 : */
201 : get display_name(): string {
202 12340412 : return resolveNodeDefText(
203 : 'display_name',
204 : this.name,
205 : this.backendDisplayName
206 : )
207 : }
208 :
209 : get description(): string {
210 26441 : return resolveNodeDefText('description', this.name, this.backendDescription)
211 : }
212 :
213 : get nodePath(): string {
214 16576 : return (this.category ? this.category + '/' : '') + this.name
215 : }
216 :
217 : get isDummyFolder(): boolean {
218 6 : return this.name === ''
219 : }
220 :
221 : postProcessSearchScores(scores: SearchAuxScore): SearchAuxScore {
222 : const nodeFrequencyStore = useNodeFrequencyStore()
223 2502496 : const nodeFrequency = nodeFrequencyStore.getNodeFrequencyByName(this.name)
224 2502496 : return [scores[0], -nodeFrequency, ...scores.slice(1)]
225 : }
226 :
227 : get nodeLifeCycleBadgeText(): string {
228 19971 : if (this.deprecated) return '[DEPR]'
229 19970 : if (this.experimental) return '[BETA]'
230 19968 : if (this.dev_only) return '[DEV]'
231 19907 : return ''
232 : }
233 : }
|