LCOV - code coverage report
Current view: top level - packages/object-info-parser/src - inputSpecInterpreter.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 96.1 % 77 74
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import type { ZodError, z } from 'zod'
       2              : 
       3              : import { transformInputSpecV1ToV2 } from '#/migration'
       4              : import type {
       5              :   InputSpec as InputSpecV2,
       6              :   OutputSpec as OutputSpecV2
       7              : } from '#/schemas/nodeDefSchemaV2'
       8              : import type {
       9              :   ComfyInputsSpec,
      10              :   DynamicControlType
      11              : } from '#/schemas/nodeDefSchema'
      12              : import {
      13              :   isDynamicControlType,
      14              :   zAutogrowOptions,
      15              :   zDynamicComboOption,
      16              :   zDynamicComboSpecV2,
      17              :   zDynamicGroupInputSpec,
      18              :   zMatchTypeOptions
      19              : } from '#/schemas/nodeDefSchema'
      20              : 
      21              : interface DynamicControl {
      22              :   nestedInputs: (spec: InputSpecV2) => ComfyInputsSpec[]
      23              :   ownTypes: (spec: InputSpecV2) => string[]
      24              : }
      25              : 
      26         2356 : const none = () => []
      27              : 
      28              : export type InputSpecDiagnosticSink = (
      29              :   spec: InputSpecV2,
      30              :   error: ZodError,
      31              :   optionIndex?: number
      32              : ) => void
      33              : 
      34     57591097 : function splitSlotTypes(type: unknown): string[] {
      35     57591097 :   if (typeof type !== 'string') return []
      36              : 
      37     57591097 :   return type
      38              :     .split(',')
      39              :     .map((part) => part.trim())
      40              :     .filter(Boolean)
      41              : }
      42              : 
      43         4712 : export function createInputSpecInterpreter(
      44              :   warnSpecDrift: InputSpecDiagnosticSink,
      45              :   { skipValidation = () => false }: { skipValidation?: () => boolean } = {}
      46              : ) {
      47      3145826 :   function parseSpec<T>(
      48              :     schema: z.ZodType<T>,
      49              :     spec: InputSpecV2,
      50              :     option?: { option: unknown; index: number }
      51              :   ): T[] {
      52      3145826 :     const value = option ? option.option : spec
      53      3145826 :     if (skipValidation()) return [value as T]
      54              : 
      55      3145826 :     const parsed = schema.safeParse(value)
      56      3145826 :     if (parsed.success) return [parsed.data]
      57      3145826 :     warnSpecDrift(spec, parsed.error, option?.index)
      58      3145826 :     return []
      59              :   }
      60              : 
      61         4712 :   const parsedOptionCache = new WeakMap<
      62              :     object,
      63              :     ReturnType<typeof zDynamicComboOption.safeParse>
      64              :   >()
      65              : 
      66      1424001 :   function parseDynamicComboOptions(
      67              :     spec: InputSpecV2
      68              :   ): { key: string; inputs: ComfyInputsSpec }[] {
      69      1424001 :     return parseSpec(zDynamicComboSpecV2, spec).flatMap(({ options }) =>
      70              :       options.flatMap((option, index) => {
      71      4100417 :         if (skipValidation())
      72            0 :           return [option as { key: string; inputs: ComfyInputsSpec }]
      73              : 
      74      4100417 :         const cached =
      75              :           typeof option === 'object' && option !== null
      76              :             ? parsedOptionCache.get(option)
      77              :             : undefined
      78      4100417 :         const parsedOption = cached ?? zDynamicComboOption.safeParse(option)
      79      4100417 :         if (!cached && typeof option === 'object' && option !== null)
      80      2647646 :           parsedOptionCache.set(option, parsedOption)
      81      4100417 :         if (!parsedOption.success) {
      82            0 :           warnSpecDrift(spec, parsedOption.error, index)
      83            0 :           return []
      84              :         }
      85      4100417 :         return [parsedOption.data]
      86              :       })
      87              :     )
      88              :   }
      89              : 
      90         4712 :   const dynamicControls = {
      91              :     COMFY_DYNAMICGROUP_V3: {
      92              :       nestedInputs: (spec) =>
      93              :         parseSpec(zDynamicGroupInputSpec.def.items[1], spec).map(
      94              :           ({ template }) => template
      95              :         ),
      96              :       ownTypes: none
      97              :     },
      98              :     COMFY_AUTOGROW_V3: {
      99              :       nestedInputs: (spec) =>
     100              :         parseSpec(zAutogrowOptions, spec).map(({ template }) => template.input),
     101              :       ownTypes: none
     102              :     },
     103              :     COMFY_DYNAMICCOMBO_V3: {
     104              :       nestedInputs: (spec) =>
     105              :         parseDynamicComboOptions(spec).map(({ inputs }) => inputs),
     106              :       ownTypes: none
     107              :     },
     108              :     COMFY_MATCHTYPE_V3: {
     109              :       nestedInputs: none,
     110              :       ownTypes: (spec) =>
     111              :         parseSpec(zMatchTypeOptions, spec).flatMap(({ template }) =>
     112              :           splitSlotTypes(template.allowed_types)
     113              :         )
     114              :     }
     115              :   } satisfies Record<DynamicControlType, DynamicControl>
     116              : 
     117    100783442 :   function dynamicControlFor(spec: InputSpecV2): DynamicControl | undefined {
     118    100783442 :     return isDynamicControlType(spec.type)
     119              :       ? dynamicControls[spec.type]
     120              :       : undefined
     121              :   }
     122              : 
     123     50673468 :   function* walkNestedInputSpecs(spec: InputSpecV2): Generator<InputSpecV2> {
     124     50673468 :     const control = dynamicControlFor(spec)
     125     50673468 :     if (!control) return
     126              : 
     127     50673465 :     for (const inputs of control.nestedInputs(spec)) {
     128      5722990 :       for (const child of toInputSpecsV2(inputs)) {
     129     14896373 :         yield child
     130     14896373 :         yield* walkNestedInputSpecs(child)
     131              :       }
     132              :     }
     133              :   }
     134              : 
     135      5722994 :   function* toInputSpecsV2(inputs: ComfyInputsSpec): Generator<InputSpecV2> {
     136      5722994 :     const groups = [
     137              :       { specs: inputs.required, isOptional: false },
     138              :       { specs: inputs.optional, isOptional: true }
     139              :     ]
     140      5722994 :     for (const { specs, isOptional } of groups) {
     141     11445988 :       for (const [name, specV1] of Object.entries(specs ?? {})) {
     142     14896377 :         yield transformInputSpecV1ToV2(specV1, { name, isOptional })
     143              :       }
     144              :     }
     145              :   }
     146              : 
     147     35777095 :   function inputSpecTree(spec: InputSpecV2): InputSpecV2[] {
     148     35777095 :     return [spec, ...walkNestedInputSpecs(spec)]
     149              :   }
     150              : 
     151     50109974 :   function ownSlotTypes(spec: InputSpecV2): string[] {
     152     50109974 :     const control = dynamicControlFor(spec)
     153     50109974 :     return control ? control.ownTypes(spec) : splitSlotTypes(spec.type)
     154              :   }
     155              : 
     156            1 :   function dynamicComboOptionKeys(spec: InputSpecV2): string[] {
     157            1 :     if (spec.type !== 'COMFY_DYNAMICCOMBO_V3') return []
     158              : 
     159            1 :     return parseDynamicComboOptions(spec).map(({ key }) => key)
     160              :   }
     161              : 
     162            1 :   function dynamicComboOptionTypes(
     163              :     spec: InputSpecV2
     164              :   ): { key: string; types: string[] }[] {
     165            1 :     if (spec.type !== 'COMFY_DYNAMICCOMBO_V3') return []
     166              : 
     167            1 :     return parseDynamicComboOptions(spec).map(({ key, inputs }) => ({
     168              :       key,
     169              :       types: [...toInputSpecsV2(inputs)]
     170              :         .flatMap(inputSpecTree)
     171              :         .flatMap(ownSlotTypes)
     172              :     }))
     173              :   }
     174              : 
     175       255060 :   function matchTypeTemplate(
     176              :     spec: InputSpecV2
     177              :   ): { templateId: string; allowedTypes: string[] } | undefined {
     178       255060 :     if (spec.type !== 'COMFY_MATCHTYPE_V3') return undefined
     179              : 
     180        56680 :     return parseSpec(zMatchTypeOptions, spec)
     181              :       .map(({ template }) => ({
     182              :         templateId: template.template_id,
     183              :         allowedTypes: splitSlotTypes(template.allowed_types)
     184              :       }))
     185              :       .at(0)
     186              :   }
     187              : 
     188              :   /** Top-level and nested input specs, deduped by name with the first occurrence kept. */
     189          102 :   function flattenInputSpecs(
     190              :     inputs: Record<string, InputSpecV2>
     191              :   ): InputSpecV2[] {
     192          102 :     const seenNames = new Set<string>()
     193          102 :     return Object.values(inputs)
     194              :       .flatMap(inputSpecTree)
     195              :       .filter(({ name }) => {
     196          542 :         if (seenNames.has(name)) return false
     197          542 :         seenNames.add(name)
     198          542 :         return true
     199              :       })
     200              :   }
     201              : 
     202              :   /** Every slot type an input could expose across all DynamicCombo options; for search only. */
     203     25785402 :   function collectSearchableInputTypes(input: InputSpecV2): string[] {
     204     25785402 :     return inputSpecTree(input).flatMap(ownSlotTypes)
     205              :   }
     206              : 
     207              :   /** Every slot type an output could produce, resolving MatchType outputs to their template's allowed types. */
     208      7579340 :   function collectSearchableOutputTypes(
     209              :     outputs: OutputSpecV2[],
     210              :     inputs: Record<string, InputSpecV2>,
     211              :     outputMatchTypes: (string | undefined)[] | undefined
     212              :   ): string[] {
     213      7579340 :     if (!outputs.some((output) => output.type === 'COMFY_MATCHTYPE_V3')) {
     214      7543915 :       return outputs.flatMap((output) => splitSlotTypes(output.type))
     215              :     }
     216              : 
     217        35425 :     const allowedTypesByTemplate = new Map<string, string[]>()
     218      7578816 :     for (const spec of Object.values(inputs).flatMap(inputSpecTree)) {
     219       255060 :       const template = matchTypeTemplate(spec)
     220       255060 :       if (!template) continue
     221              : 
     222       255060 :       const seen = allowedTypesByTemplate.get(template.templateId) ?? []
     223       255060 :       allowedTypesByTemplate.set(template.templateId, [
     224              :         ...new Set([...seen, ...template.allowedTypes])
     225              :       ])
     226              :     }
     227              : 
     228        35425 :     return outputs.flatMap((output) => {
     229        63765 :       if (output.type !== 'COMFY_MATCHTYPE_V3')
     230        21255 :         return splitSlotTypes(output.type)
     231              : 
     232        63765 :       const templateId = outputMatchTypes?.[output.index]
     233        63765 :       return templateId ? (allowedTypesByTemplate.get(templateId) ?? []) : []
     234              :     })
     235              :   }
     236              : 
     237         4712 :   return {
     238              :     splitSlotTypes,
     239              :     inputSpecTree,
     240              :     ownSlotTypes,
     241              :     dynamicComboOptionKeys,
     242              :     dynamicComboOptionTypes,
     243              :     matchTypeTemplate,
     244              :     flattenInputSpecs,
     245              :     collectSearchableInputTypes,
     246              :     collectSearchableOutputTypes
     247              :   }
     248              : }
        

Generated by: LCOV version 2.0-1