LCOV - code coverage report
Current view: top level - src/workbench/utils - nodeDefOrderingUtil.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 100.0 % 19 19
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import type { TWidgetValue } from '@/lib/litegraph/src/litegraph'
       2              : import type { InputSpec } from '@comfyorg/object-info-parser/schemas/nodeDefSchemaV2'
       3              : import type { ComfyNodeDefImpl } from '@/core/graph/nodeDef/ComfyNodeDefImpl'
       4              : 
       5              : /**
       6              :  * Gets an ordered array of InputSpec objects based on input_order.
       7              :  * This is designed to work with V2 format used by litegraphService.
       8              :  *
       9              :  * @param nodeDefImpl - The ComfyNodeDefImpl containing both V1 and V2 formats
      10              :  * @param inputs - The V2 format inputs (flat Record<string, InputSpec>)
      11              :  * @returns Array of InputSpec objects in the correct order
      12              :  */
      13        23115 : export function getOrderedInputSpecs(
      14              :   nodeDefImpl: Pick<ComfyNodeDefImpl, 'input_order'>,
      15              :   inputs: Partial<Record<string, InputSpec>>
      16              : ): InputSpec[] {
      17        23115 :   const orderedInputSpecs: InputSpec[] = []
      18              : 
      19              :   // If no input_order, return default Object.values order
      20        23115 :   if (!nodeDefImpl.input_order) {
      21          531 :     return Object.values(inputs).filter((input) => input !== undefined)
      22              :   }
      23              : 
      24              :   // Process required inputs in specified order
      25        23115 :   const requiredOrder = Object.hasOwn(nodeDefImpl.input_order, 'required')
      26              :     ? nodeDefImpl.input_order.required
      27              :     : []
      28        23115 :   for (const name of requiredOrder) {
      29        72430 :     const inputSpec = inputs[name]
      30        72430 :     if (inputSpec && !inputSpec.isOptional) {
      31        72430 :       orderedInputSpecs.push(inputSpec)
      32              :     }
      33              :   }
      34              : 
      35              :   // Process optional inputs in specified order
      36        23115 :   const optionalOrder = Object.hasOwn(nodeDefImpl.input_order, 'optional')
      37              :     ? nodeDefImpl.input_order.optional
      38              :     : []
      39        23115 :   for (const name of optionalOrder) {
      40          228 :     const inputSpec = inputs[name]
      41          228 :     if (inputSpec?.isOptional) {
      42          228 :       orderedInputSpecs.push(inputSpec)
      43              :     }
      44              :   }
      45              : 
      46              :   // Add any remaining inputs not specified in input_order
      47        22584 :   const processedNames = new Set(orderedInputSpecs.map((spec) => spec.name))
      48        23112 :   for (const inputSpec of Object.values(inputs)) {
      49        72939 :     if (inputSpec && !processedNames.has(inputSpec.name)) {
      50          281 :       orderedInputSpecs.push(inputSpec)
      51              :     }
      52              :   }
      53              : 
      54        22584 :   return orderedInputSpecs
      55              : }
      56              : 
      57              : /**
      58              :  * Reorders widget values based on the input_order to match expected widget order.
      59              :  * This is used when widgets were created in a different order than input_order specifies.
      60              :  *
      61              :  * @param widgetValues - The current widget values array
      62              :  * @param currentWidgetOrder - The current order of widget names
      63              :  * @param inputOrder - The desired order from input_order
      64              :  * @returns Reordered widget values array
      65              :  */
      66              : export function sortWidgetValuesByInputOrder(
      67              :   widgetValues: TWidgetValue[],
      68              :   currentWidgetOrder: string[],
      69              :   inputOrder: string[]
      70              : ): TWidgetValue[] {
      71              :   if (inputOrder.length === 0) {
      72              :     return widgetValues
      73              :   }
      74              : 
      75              :   const widgetIndexByName = new Map<string, number>()
      76              :   currentWidgetOrder.forEach((name, index) => {
      77              :     if (index < widgetValues.length) {
      78              :       widgetIndexByName.set(name, index)
      79              :     }
      80              :   })
      81              : 
      82              :   // Reorder based on input_order
      83              :   const reordered: TWidgetValue[] = []
      84              :   const usedNames = new Set<string>()
      85              : 
      86              :   // First, add values in the order specified by input_order
      87              :   for (const name of inputOrder) {
      88              :     const index = widgetIndexByName.get(name)
      89              :     if (index === undefined) continue
      90              :     reordered.push(widgetValues[index])
      91              :     usedNames.add(name)
      92              :   }
      93              : 
      94              :   // Then add any remaining values not in input_order
      95              :   for (const [name, index] of widgetIndexByName) {
      96              :     if (!usedNames.has(name)) {
      97              :       reordered.push(widgetValues[index])
      98              :     }
      99              :   }
     100              : 
     101              :   // If there are extra values not in the map, append them
     102              :   if (widgetValues.length > currentWidgetOrder.length) {
     103              :     for (let i = currentWidgetOrder.length; i < widgetValues.length; i++) {
     104              :       reordered.push(widgetValues[i])
     105              :     }
     106              :   }
     107              : 
     108              :   return reordered
     109              : }
        

Generated by: LCOV version 2.0-1