Line data Source code
1 : import type {
2 : ComfyNodeDef as ComfyNodeDefV2,
3 : InputSpec as InputSpecV2,
4 : OutputSpec as OutputSpecV2
5 : } from '#/schemas/nodeDefSchemaV2'
6 : import type {
7 : ComfyNodeDef as ComfyNodeDefV1,
8 : InputSpec as InputSpecV1
9 : } from '#/schemas/nodeDefSchema'
10 : import {
11 : getComboSpecComboOptions,
12 : isComboInputSpec,
13 : isComboInputSpecV1
14 : } from '#/schemas/nodeDefSchema'
15 :
16 : /**
17 : * Transforms a V1 node definition to V2 format
18 : * @param nodeDefV1 The V1 node definition to transform
19 : * @returns The transformed V2 node definition
20 : */
21 7579340 : export function transformNodeDefV1ToV2(
22 : nodeDefV1: ComfyNodeDefV1
23 : ): ComfyNodeDefV2 {
24 : // Transform inputs
25 7579340 : const inputs: Record<string, InputSpecV2> = {}
26 :
27 : // Process required inputs
28 7579340 : if (nodeDefV1.input?.required) {
29 7558068 : Object.entries(nodeDefV1.input.required).forEach(([name, inputSpecV1]) => {
30 31806993 : inputs[name] = transformInputSpecV1ToV2(inputSpecV1, {
31 : name,
32 : isOptional: false
33 : })
34 : })
35 : }
36 :
37 : // Process optional inputs
38 7579340 : if (nodeDefV1.input?.optional) {
39 1972036 : Object.entries(nodeDefV1.input.optional).forEach(([name, inputSpecV1]) => {
40 5653833 : inputs[name] = transformInputSpecV1ToV2(inputSpecV1, {
41 : name,
42 : isOptional: true
43 : })
44 : })
45 : }
46 :
47 : // Transform outputs
48 7579340 : const outputs: OutputSpecV2[] = []
49 :
50 7579340 : if (nodeDefV1.output) {
51 7579340 : if (Array.isArray(nodeDefV1.output)) {
52 7579340 : nodeDefV1.output.forEach((outputType, index) => {
53 10454344 : const outputSpec: OutputSpecV2 = {
54 : index,
55 : name: nodeDefV1.output_name?.[index] || `output_${index}`,
56 : type: Array.isArray(outputType) ? 'COMBO' : outputType,
57 : is_list: nodeDefV1.output_is_list?.[index] || false,
58 : tooltip: nodeDefV1.output_tooltips?.[index]
59 : }
60 :
61 : // Add options for combo outputs
62 10454344 : if (Array.isArray(outputType)) {
63 7085 : outputSpec.options = outputType
64 : }
65 :
66 10454344 : outputs.push(outputSpec)
67 : })
68 : } else {
69 0 : console.warn('nodeDefV1.output is not an array:', nodeDefV1.output)
70 : }
71 : }
72 :
73 : // Create the V2 node definition
74 7579340 : return {
75 : inputs,
76 : outputs,
77 : hidden: nodeDefV1.input?.hidden,
78 : name: nodeDefV1.name,
79 : display_name: nodeDefV1.display_name,
80 : description: nodeDefV1.description,
81 : category: nodeDefV1.category,
82 : output_node: nodeDefV1.output_node,
83 : python_module: nodeDefV1.python_module,
84 : deprecated: nodeDefV1.deprecated,
85 : experimental: nodeDefV1.experimental
86 : }
87 : }
88 :
89 : /**
90 : * Transforms a V1 input specification to V2 format
91 : * @param inputSpecV1 The V1 input specification to transform
92 : * @param name The name of the input
93 : * @param isOptional Whether the input is optional
94 : * @returns The transformed V2 input specification
95 : */
96 52402847 : export function transformInputSpecV1ToV2(
97 : inputSpecV1: InputSpecV1,
98 : kwargs: {
99 : name: string
100 : isOptional?: boolean
101 : }
102 : ): InputSpecV2 {
103 52402847 : const { name, isOptional = false } = kwargs
104 :
105 : // Extract options from the input spec
106 52402847 : const options = inputSpecV1[1] || {}
107 :
108 : // Base properties for all input types
109 52402847 : const baseProps = {
110 : name,
111 : isOptional,
112 : ...options
113 : }
114 :
115 : // Handle different input types
116 52402847 : if (isComboInputSpec(inputSpecV1)) {
117 8126117 : return {
118 : type: 'COMBO',
119 : ...baseProps,
120 : options: isComboInputSpecV1(inputSpecV1)
121 : ? inputSpecV1[0]
122 : : getComboSpecComboOptions(inputSpecV1)
123 : }
124 44276730 : } else if (inputSpecV1[0] === 'CHART') {
125 : // CHART uses `chartType` in V2 because `type` is the discriminator key
126 0 : const { type, ...chartOptions } = options
127 0 : const chartType =
128 : type === 'bar' || type === 'line'
129 : ? type
130 : : options.chartType === 'bar' || options.chartType === 'line'
131 : ? options.chartType
132 : : undefined
133 0 : return {
134 : type: 'CHART',
135 : name,
136 : isOptional,
137 : ...chartOptions,
138 : chartType
139 : }
140 44276730 : } else if (
141 : inputSpecV1[0] === 'COMFY_DYNAMICCOMBO_V3' &&
142 : inputSpecV1[1] === undefined
143 : ) {
144 : // Synthetic nodedefs (e.g. subgraphStore.ts boundary inputs) promote the
145 : // type into a bare [type, undefined] tuple with no options supplied
146 4452 : return {
147 : type: 'COMFY_DYNAMICCOMBO_V3',
148 : ...baseProps,
149 : options: []
150 : }
151 44272278 : } else if (typeof inputSpecV1[0] === 'string') {
152 : // Handle standard types (INT, FLOAT, BOOLEAN, STRING) and custom types
153 44272278 : return {
154 : type: inputSpecV1[0],
155 : ...baseProps
156 : }
157 : }
158 :
159 : // Fallback for any unhandled cases
160 0 : return {
161 : type: 'UNKNOWN',
162 : ...baseProps
163 : }
164 : }
165 :
166 91738 : export function transformInputSpecV2ToV1(
167 : inputSpecV2: InputSpecV2
168 : ): InputSpecV1 {
169 91738 : return [inputSpecV2.type, inputSpecV2]
170 : }
|