Line data Source code
1 : import {
2 : zDynamicComboInputSpec,
3 : zDynamicGroupInputSpec
4 : } from '@comfyorg/object-info-parser'
5 : import type { ComfyInputsSpec, InputSpec } from '@comfyorg/object-info-parser'
6 : import type { WidgetValue } from '@/types/simplifiedWidget'
7 :
8 9955 : export function resolveDynamicInputSpec(
9 : inputs: ComfyInputsSpec,
10 : name: string,
11 : getValue: (name: string) => WidgetValue,
12 : prefix = ''
13 : ): { spec: InputSpec; isOptional: boolean } | undefined {
14 9955 : for (const [fields, isOptional] of [
15 : [inputs.required, false],
16 : [inputs.optional, true]
17 : ] as const) {
18 18371 : if (fields && Object.hasOwn(fields, name))
19 232 : return { spec: fields[name], isOptional }
20 18371 : for (const [key, spec] of Object.entries(fields ?? {})) {
21 71163 : if (!name.startsWith(`${key}.`)) continue
22 1711 : const resolved = resolveNestedInputSpec(spec, key, name, getValue, prefix)
23 4043 : if (resolved) return resolved
24 : }
25 : }
26 : }
27 :
28 1711 : function resolveNestedInputSpec(
29 : spec: InputSpec,
30 : key: string,
31 : name: string,
32 : getValue: (name: string) => WidgetValue,
33 : prefix: string
34 : ): { spec: InputSpec; isOptional: boolean } | undefined {
35 1711 : const suffix = name.slice(key.length + 1)
36 1711 : const group = zDynamicGroupInputSpec.safeParse(spec).data
37 1711 : if (group) {
38 1511 : const separator = suffix.indexOf('.')
39 1511 : if (separator === -1) return
40 1108 : const index = suffix.slice(0, separator)
41 1505 : if (!/^(0|[1-9][0-9]*)$/.test(index)) return
42 1108 : const field = suffix.slice(separator + 1)
43 1108 : const template = group[1].template
44 1511 : if (template.required && Object.hasOwn(template.required, field))
45 798 : return { spec: template.required[field], isOptional: false }
46 1511 : if (template.optional && Object.hasOwn(template.optional, field))
47 310 : return { spec: template.optional[field], isOptional: true }
48 : }
49 : const combo = zDynamicComboInputSpec.safeParse(spec).data
50 1711 : const selected = combo?.[1].options.find(
51 : ({ key: option }) => option === getValue(`${prefix}${key}`)
52 : )
53 1711 : if (selected)
54 200 : return resolveDynamicInputSpec(
55 : selected.inputs,
56 : suffix,
57 : getValue,
58 : `${prefix}${key}.`
59 : )
60 : }
|