-
Notifications
You must be signed in to change notification settings - Fork 511
Expand file tree
/
Copy pathmapperUtil.ts
More file actions
77 lines (67 loc) · 2.21 KB
/
mapperUtil.ts
File metadata and controls
77 lines (67 loc) · 2.21 KB
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
import { transformInputSpecV1ToV2 } from '@/schemas/nodeDef/migration'
import {
ComfyNodeDef as ComfyNodeDefV2,
InputSpec
} from '@/schemas/nodeDef/nodeDefSchemaV2'
import { ComfyNodeDef as ComfyNodeDefV1 } from '@/schemas/nodeDefSchema'
import { components } from '@/types/comfyRegistryTypes'
const registryToFrontendV2NodeOutputs = (
registryDef: components['schemas']['ComfyNode']
): ComfyNodeDefV2['outputs'] => {
const returnTypes = JSON.parse(registryDef.return_types ?? '{}')
if (!returnTypes.length) return []
const returnNames = JSON.parse(registryDef.return_names ?? '{}')
const outputsIsList = registryDef.output_is_list ?? []
const outputs = []
for (let i = 0; i < returnTypes.length; i++) {
outputs.push({
type: returnTypes[i],
name: returnNames[i] || returnTypes[i],
is_list: outputsIsList[i] ?? false,
index: i
})
}
return outputs
}
const registryToFrontendV2NodeInputs = (
registryDef: components['schemas']['ComfyNode']
): ComfyNodeDefV2['inputs'] => {
const inputTypes = JSON.parse(
registryDef.input_types ?? '{}'
) as ComfyNodeDefV1['input']
if (!inputTypes || !Object.keys(inputTypes).length) return {}
const inputsV2: Record<string, InputSpec> = {}
if (inputTypes.required) {
Object.entries(inputTypes.required).forEach(([name, inputSpecV1]) => {
inputsV2[name] = transformInputSpecV1ToV2(inputSpecV1, {
name,
isOptional: false
})
})
}
if (inputTypes.optional) {
Object.entries(inputTypes.optional).forEach(([name, inputSpecV1]) => {
inputsV2[name] = transformInputSpecV1ToV2(inputSpecV1, {
name,
isOptional: true
})
})
}
return inputsV2
}
export const registryToFrontendV2NodeDef = (
nodeDef: components['schemas']['ComfyNode'],
nodePack: components['schemas']['Node']
): ComfyNodeDefV2 => {
const name = nodeDef.comfy_node_name ?? 'Node Name'
return {
category: nodeDef.category ?? 'unknown',
description: nodeDef.description ?? '',
display_name: name,
name,
inputs: registryToFrontendV2NodeInputs(nodeDef),
outputs: registryToFrontendV2NodeOutputs(nodeDef),
output_node: false,
python_module: nodePack.name ?? nodePack.id ?? 'unknown'
}
}