-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcomfy-codegen.ts
268 lines (222 loc) · 6.88 KB
/
comfy-codegen.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import { ComfyApiClient } from './comfy-api-client.ts'
import type { ComfyObjectInfo, ComfyInputInfo, ComfyOutputInfo, ComfyObjectInfoResponse } from './comfy-api-client.ts'
function generateNodeDefinition(object: ComfyObjectInfo) {
let code = 'export class '
code += object.name
code += ' extends ComfyNode {\n'
code += '\tclassType: string = "'
code += object.name
code += '"\n\n'
code += '\tsockets: {\n'
code += '\t\tinputs: Required<'
code += object.name
code += 'Inputs>\n'
code += '\t\toutputs: {\n'
for (const output of object.outputs) {
code += generateOutputDefinition(output)
}
code += '\t\t}\n'
code += '\t}\n\n'
code += '\tconstructor() {\n'
code += '\t\tsuper()\n'
code += '\t\tconst $node = this\n'
code += '\t\tthis.sockets = {\n'
code += '\t\t\tinputs: {\n'
for (const input of getNodeInputsIterator(object)) {
code += generateInputRefInstantiation(input.name, input.input, input.type === 'required')
}
code += '\t\t\t},\n'
code += '\t\t\toutputs: Object.create(Object.prototype, {\n'
for (const output of object.outputs.map((x, i) => [x, i] as [ComfyOutputInfo, number])) {
code += generateOutputRefInstantiation(output[0], output[1])
}
code += '\t\t\t})\n'
code += '\t\t}\n'
code += '\t}\n\n'
code += '\tconnectAll(sources: MappedSources<'
code += object.name
code += 'Inputs>) {\n'
for (const input of getNodeInputsIterator(object)) {
code += generateInputConnect(input.name, input.input, input.type === 'required')
code += '\n'
}
code += '\t}\n'
code += '}\n\n'
code += 'type '
code += object.name
code += 'Inputs = {\n'
for (const input of getNodeInputsIterator(object)) {
code += generateInputDefinition(input.name, input.input, input.type === 'required')
}
code += '}\n'
return code
}
function* getNodeInputsIterator(object: ComfyObjectInfo) {
for (const pair of Object.entries(object.inputs.required)) {
yield {
name: pair[0],
input: pair[1],
type: 'required' as 'required' | 'optional' | 'hidden'
}
}
if (object.inputs.optional != null) {
for (const pair of Object.entries(object.inputs.optional)) {
yield {
name: pair[0],
input: pair[1],
type: 'optional' as 'required' | 'optional' | 'hidden'
}
}
}
if (object.inputs.hidden != null) {
for (const pair of Object.entries(object.inputs.hidden)) {
yield {
name: pair[0],
input: pair[1],
type: 'hidden' as 'required' | 'optional' | 'hidden'
}
}
}
}
function generateInputDefinition(name: string, input: ComfyInputInfo, required: boolean) {
let code = '\t\t\t'
if (input.tooltip != null) {
code += '/** '
code += input.tooltip
code += '*/\n\t\t\t'
}
code += '"'
code += name
code += '"'
if (!required) {
code += '?'
}
code += ': ComfyNodeTypedInputRef<'
code += getInputType(input)
code += '>\n'
return code
}
function convertObjectInfoTypeToLocal(tp: string) {
if (tp === 'TEXT' || tp === 'STRING') {
return 'string'
} else if (tp === 'FLOAT' || tp === 'INT') {
return 'number'
} else if (tp === 'BOOLEAN') {
return 'boolean'
} else {
return 'ComfyValueType_' + tp
}
}
function getInputType(input: ComfyInputInfo) {
if ('type' in input) {
return convertObjectInfoTypeToLocal(input.type)
} else if ('genericType' in input) {
return convertObjectInfoTypeToLocal(input.genericType)
} else {
if (input.values.length === 0) {
return 'void'
} else {
return 'string'
}
}
}
function generateOutputDefinition(output: ComfyOutputInfo) {
let code = '\t\t\t'
if (output.tooltip != null) {
code += '/** '
code += output.tooltip
code += '*/\n\t\t\t'
}
code += output.name
code += ': ComfyNodeTypedSourceRef<'
code += convertObjectInfoTypeToLocal(output.type)
code += '>\n'
return code
}
function generateInputRefInstantiation(name: string, input: ComfyInputInfo, required: boolean) {
let code = '\t\t\t\t'
code += '"'
code += name
code += '": new ComfyNodeTypedInputRef<'
code += getInputType(input)
code += '>(this, "'
code += name
code += '"),\n'
return code
}
function generateOutputRefInstantiation(output: ComfyOutputInfo, index: number) {
let code = '\t\t\t\t'
code += '"'
code += output.name
code += '": {\n'
code += '\t\t\t\t\tconfigurable: false,\n'
code += '\t\t\t\t\tget: function () {\n'
code += '\t\t\t\t\t\treturn { sourceNodeId: $node.id, sourceNodeOutputIndex: '
code += index
code += ' }\n'
code += '\t\t\t\t\t}\n'
code += '\t\t\t\t},\n'
return code
}
function generateInputConnect(name: string, input: ComfyInputInfo, required: boolean) {
let code = ''
if (!required) {
code += '\t\tif (sources["'
code += name
code += '"] !== undefined) {\n'
}
code += '\t\tif (typeof sources["'
code += name
code += '"] === "object") {\n'
code += '\t\t\tthis.sockets.inputs["'
code += name
code += '"].connectTo(sources["'
code += name
code += '"])\n'
code += '\t\t} else {\n'
code += '\t\t\tthis.sockets.inputs["'
code += name
code += '"].value = sources["'
code += name
code += '"]\n'
code += ''
code += '\t\t}\n'
if (!required) {
code += '\t\t}\n'
}
return code
}
function generateComfyTypeDefs(response: ComfyObjectInfoResponse) {
const typeArray = Object.values(response)
.flatMap(o => [...getNodeInputsIterator(o)])
.map(x => getInputType(x.input))
.filter(t => t.startsWith('ComfyValueType_'))
const types = new Set(typeArray)
let code = ''
for (const t of types) {
code += 'const Symbol_'
code += t
code += ' = Symbol()\n'
code += 'type '
code += t
code += ' = typeof Symbol_'
code += t
code += '\n'
}
return code
}
let host = 'localhost:8188'
const client = new ComfyApiClient(host)
const info = await client.getObjectInfo()
console.log('import { ComfyNodeTypedInputRef, ComfyNode } from "./comfy-graph.ts"')
console.log('import type { ComfyNodeTypedSourceRef } from "./comfy-graph.ts"')
console.log('')
console.log('type MappedSources<TInputs> = {')
console.log('\t[K in keyof TInputs]: TInputs[K] extends ComfyNodeTypedInputRef<infer T> ? T | ComfyNodeTypedSourceRef<T> : never')
console.log('}\n')
console.log(generateComfyTypeDefs(info))
for (const object of Object.values(info)) {
console.log(generateNodeDefinition(object))
console.log('')
}
client.close()