LCOV - code coverage report
Current view: top level - src/utils - executionUtil.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 94.8 % 58 55
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import type {
       2              :   ExecutableLGraphNode,
       3              :   ExecutionId,
       4              :   LGraph
       5              : } from '@/lib/litegraph/src/litegraph'
       6              : import {
       7              :   ExecutableNodeDTO,
       8              :   LGraphEventMode
       9              : } from '@/lib/litegraph/src/litegraph'
      10              : import type {
      11              :   ComfyApiWorkflow,
      12              :   ComfyWorkflowJSON
      13              : } from '@comfyorg/graph-model/workflowSchema'
      14              : 
      15              : import { zNodePackMetadata } from '@comfyorg/graph-model/workflowSchema'
      16              : 
      17              : import { compressWidgetInputSlots } from './litegraphUtil'
      18              : 
      19              : type ExportedWidgetValueWrapper = {
      20              :   __type__?: unknown
      21              :   __value__: unknown
      22              : }
      23              : 
      24           73 : function isExportedWidgetValueWrapper(
      25              :   value: unknown
      26              : ): value is ExportedWidgetValueWrapper {
      27           73 :   return (
      28              :     typeof value === 'object' &&
      29              :     value !== null &&
      30              :     !Array.isArray(value) &&
      31              :     '__value__' in value
      32              :   )
      33              : }
      34              : 
      35              : /**
      36              :  * Inverse of the wrapping applied during Export (API). Curve values carry a
      37              :  * type marker and may be objects; untyped wrappers are reserved for arrays so
      38              :  * ordinary objects containing a `__value__` property pass through unchanged.
      39              :  */
      40           73 : export function unwrapExportedWidgetValue(value: unknown): unknown {
      41           73 :   if (
      42              :     isExportedWidgetValueWrapper(value) &&
      43              :     (value.__type__ === 'CURVE' || Array.isArray(value.__value__))
      44              :   ) {
      45            0 :     return value.__value__
      46              :   }
      47           73 :   return value
      48              : }
      49              : 
      50              : /**
      51              :  * Converts the current graph workflow for sending to the API.
      52              :  * @note Node widgets are updated before serialization to prepare queueing.
      53              :  *
      54              :  * @param graph The graph to convert.
      55              :  * @param options The options for the conversion.
      56              :  *  - `sortNodes`: Whether to sort the nodes by execution order.
      57              :  * @returns The workflow and node links
      58              :  */
      59         2356 : export const graphToPrompt = async (
      60              :   graph: LGraph,
      61              :   options: { sortNodes?: boolean } = {}
      62              : ): Promise<{ workflow: ComfyWorkflowJSON; output: ComfyApiWorkflow }> => {
      63          275 :   const { sortNodes = false } = options
      64              : 
      65          275 :   for (const node of graph.computeExecutionOrder(false)) {
      66         1141 :     const innerNodes = node.getInnerNodes
      67              :       ? node.getInnerNodes(new Map())
      68              :       : [node]
      69         1141 :     for (const innerNode of innerNodes) {
      70         1158 :       if (innerNode.isVirtualNode) {
      71            0 :         innerNode.applyToGraph?.()
      72              :       }
      73              :     }
      74              :   }
      75              : 
      76          275 :   const workflow = graph.serialize({ sortNodes })
      77              : 
      78              :   // Remove localized_name from the workflow
      79          275 :   for (const node of workflow.nodes) {
      80         1141 :     for (const slot of node.inputs ?? []) {
      81         3500 :       delete slot.localized_name
      82              :     }
      83         1141 :     for (const slot of node.outputs ?? []) {
      84         1477 :       delete slot.localized_name
      85              :     }
      86              :   }
      87              : 
      88          275 :   compressWidgetInputSlots(workflow)
      89          275 :   workflow.extra ??= {}
      90          275 :   workflow.extra.frontendVersion = __COMFYUI_FRONTEND_VERSION__
      91              : 
      92          275 :   const nodeDtoMap = new Map<ExecutionId, ExecutableLGraphNode>()
      93          275 :   for (const node of graph.computeExecutionOrder(false)) {
      94         1141 :     const dto: ExecutableLGraphNode = new ExecutableNodeDTO(
      95              :       node,
      96              :       [],
      97              :       nodeDtoMap
      98              :     )
      99              : 
     100         1141 :     nodeDtoMap.set(dto.id, dto)
     101              : 
     102         1141 :     if (
     103              :       node.mode === LGraphEventMode.NEVER ||
     104              :       node.mode === LGraphEventMode.BYPASS
     105              :     ) {
     106            2 :       continue
     107              :     }
     108              : 
     109         1139 :     for (const innerNode of dto.getInnerNodes()) {
     110         1156 :       nodeDtoMap.set(innerNode.id, innerNode)
     111              :     }
     112              :   }
     113              : 
     114          275 :   const output: ComfyApiWorkflow = {}
     115              :   // Process nodes in order of execution
     116          275 :   for (const node of nodeDtoMap.values()) {
     117              :     // Don't serialize muted nodes
     118         1193 :     if (
     119              :       node.isVirtualNode ||
     120              :       node.mode === LGraphEventMode.NEVER ||
     121              :       node.mode === LGraphEventMode.BYPASS
     122              :     ) {
     123           39 :       continue
     124              :     }
     125              : 
     126         1154 :     const inputs: ComfyApiWorkflow[string]['inputs'] = {}
     127         1154 :     const { widgets } = node
     128              : 
     129              :     // Store all widget values in the API prompt.
     130              :     // Note: widget.options.serialize controls prompt inclusion (checked here).
     131              :     // widget.serialize controls workflow persistence (checked by LGraphNode).
     132         1154 :     if (widgets) {
     133         1153 :       for (const [i, widget] of widgets.entries()) {
     134         2790 :         if (!widget.name || widget.options.serialize === false) continue
     135              : 
     136         2790 :         const widgetValue = widget.serializeValue
     137              :           ? await widget.serializeValue(node, i)
     138              :           : widget.value
     139              :         // By default, Array values are reserved to represent node connections.
     140              :         // We need to wrap the array as an object to avoid the misinterpretation
     141              :         // of the array as a node connection.
     142              :         // The backend automatically unwraps the object to an array during
     143              :         // execution.
     144         2790 :         inputs[widget.name] =
     145              :           widget.type === 'curve' && widgetValue != null
     146              :             ? { __type__: 'CURVE', __value__: widgetValue }
     147              :             : Array.isArray(widgetValue)
     148              :               ? { __value__: widgetValue }
     149              :               : widgetValue
     150              :       }
     151              :     }
     152              : 
     153              :     // Store all node links
     154         1189 :     for (const [i, input] of node.inputs.entries()) {
     155         3705 :       const resolvedInput = node.resolveInput(i)
     156         3705 :       if (!resolvedInput) continue
     157              : 
     158              :       // Resolved to an actual widget value rather than a node connection
     159         3419 :       if (resolvedInput.widgetInfo) {
     160           38 :         const { value } = resolvedInput.widgetInfo
     161           38 :         inputs[input.name] = Array.isArray(value) ? { __value__: value } : value
     162           38 :         continue
     163              :       }
     164              : 
     165         1177 :       inputs[input.name] = [resolvedInput.origin_id, resolvedInput.origin_slot]
     166              :     }
     167              : 
     168         1152 :     const cnrId = zNodePackMetadata.shape.cnr_id.safeParse(
     169              :       node.properties.cnr_id
     170              :     ).data
     171         1152 :     const auxId = zNodePackMetadata.shape.aux_id.safeParse(
     172              :       node.properties.aux_id
     173              :     ).data
     174         1152 :     const packVersion = zNodePackMetadata.shape.ver.safeParse(
     175              :       node.properties.ver
     176              :     ).data
     177         1193 :     output[node.id] = {
     178              :       inputs,
     179              :       // TODO(huchenlei): Filter out all nodes that cannot be mapped to a
     180              :       // comfyClass.
     181              :       class_type: node.comfyClass!,
     182              :       // Ignored by the backend. Pack identity rides along so a re-imported
     183              :       // prompt can offer install/locate for missing types.
     184              :       _meta: {
     185              :         title: node.title,
     186              :         ...(cnrId && { cnr_id: cnrId }),
     187              :         ...(auxId && { aux_id: auxId }),
     188              :         ...(packVersion && { ver: packVersion })
     189              :       }
     190              :     }
     191              :   }
     192              : 
     193              :   // Remove inputs connected to removed nodes
     194          273 :   for (const { inputs } of Object.values(output)) {
     195         1152 :     for (const [i, input] of Object.entries(inputs)) {
     196         3425 :       if (
     197              :         Array.isArray(input) &&
     198              :         input.length === 2 &&
     199              :         !Object.hasOwn(output, input[0])
     200              :       ) {
     201            0 :         delete inputs[i]
     202              :       }
     203              :     }
     204              :   }
     205              : 
     206          273 :   return { workflow: workflow as ComfyWorkflowJSON, output }
     207              : }
        

Generated by: LCOV version 2.0-1