LCOV - code coverage report
Current view: top level - src/core/graph/subgraph - resolveConcretePromotedWidget.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 95.5 % 89 85
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import type {
       2              :   LGraphNode,
       3              :   SubgraphNode,
       4              :   SubgraphInput
       5              : } from '@comfyorg/graph-model/entityRegistry'
       6              : import type {
       7              :   PromotedWidgetExecutionSource,
       8              :   ResolvedPromotedWidget
       9              : } from '@/core/graph/subgraph/promotedWidgetTypes'
      10              : import { resolveSubgraphInputTarget } from '@/core/graph/subgraph/resolveSubgraphInputTarget'
      11              : 
      12              : import { LGraphEventMode } from '@comfyorg/graph-model/globalEnums'
      13              : import type { NodeExecutionId } from '@comfyorg/graph-model/nodeIdentification'
      14              : import { createNodeExecutionId } from '@comfyorg/graph-model/nodeIdentification'
      15              : import { toNodeId } from '@comfyorg/graph-model/nodeId'
      16              : import type { NodeId, SerializedNodeId } from '@comfyorg/graph-model/nodeId'
      17              : 
      18              : import { hasPromotedWidgetTarget } from './hasPromotedWidgetTarget'
      19              : 
      20              : type PromotedWidgetResolutionFailure =
      21              :   | 'invalid-host'
      22              :   | 'cycle'
      23              :   | 'missing-node'
      24              :   | 'missing-widget'
      25              :   | 'max-depth-exceeded'
      26              : 
      27              : type PromotedWidgetResolutionResult =
      28              :   | { status: 'resolved'; resolved: ResolvedPromotedWidget }
      29              :   | { status: 'failure'; failure: PromotedWidgetResolutionFailure }
      30              : 
      31         2356 : const MAX_PROMOTED_WIDGET_CHAIN_DEPTH = 100
      32              : 
      33          508 : function isNodeActive(node: LGraphNode): boolean {
      34          508 :   return (
      35              :     node.mode !== LGraphEventMode.NEVER && node.mode !== LGraphEventMode.BYPASS
      36              :   )
      37              : }
      38              : 
      39          400 : export function resolveActivePromotedWidgetConsumers(
      40              :   hostNode: LGraphNode,
      41              :   inputName: string
      42              : ): ResolvedPromotedWidget[] {
      43          400 :   if (!hostNode.isSubgraphNode()) return []
      44              : 
      45          400 :   const pending: {
      46              :     host: SubgraphNode
      47              :     inputName: string
      48              :     nodePath: NodeId[]
      49              :     visited: ReadonlySet<SubgraphInput>
      50              :   }[] = [{ host: hostNode, inputName, nodePath: [], visited: new Set() }]
      51          400 :   const consumers: ResolvedPromotedWidget[] = []
      52              : 
      53          400 :   for (const entry of pending) {
      54          457 :     const { host, inputName, nodePath, visited } = entry
      55          457 :     if (nodePath.length >= MAX_PROMOTED_WIDGET_CHAIN_DEPTH) continue
      56          457 :     const input = host.subgraph.inputNode.slots.find(
      57              :       (slot) => slot.name === inputName
      58              :     )
      59          457 :     if (!input || visited.has(input)) continue
      60          457 :     const nextVisited = new Set(visited).add(input)
      61              : 
      62          457 :     for (const linkId of input.linkIds) {
      63          457 :       const link = host.subgraph.getLink(linkId)
      64          457 :       if (!link) continue
      65          457 :       const { inputNode, input: targetInput } = link.resolve(host.subgraph)
      66          457 :       if (!inputNode || !targetInput || !isNodeActive(inputNode)) continue
      67          457 :       const nextPath = [...nodePath, inputNode.id]
      68              : 
      69          457 :       if (inputNode.isSubgraphNode()) {
      70           57 :         if (targetInput.widgetId) {
      71           57 :           pending.push({
      72              :             host: inputNode,
      73              :             inputName: targetInput.name,
      74              :             nodePath: nextPath,
      75              :             visited: nextVisited
      76              :           })
      77              :         }
      78           57 :         continue
      79              :       }
      80              : 
      81          400 :       const widget = inputNode.getWidgetFromSlot(targetInput)
      82          400 :       if (widget)
      83          400 :         consumers.push({ node: inputNode, nodePath: nextPath, widget })
      84              :     }
      85              :   }
      86              : 
      87          400 :   return consumers
      88              : }
      89              : 
      90          400 : export function buildPromotedWidgetExecutionSources(
      91              :   executionId: NodeExecutionId,
      92              :   consumers: readonly ResolvedPromotedWidget[]
      93              : ): PromotedWidgetExecutionSource[] {
      94          400 :   return consumers.flatMap(({ nodePath, widget }) => {
      95          400 :     const sourceExecutionId = buildPromotedSourceExecutionId(
      96              :       executionId,
      97              :       nodePath
      98              :     )
      99          400 :     return sourceExecutionId
     100              :       ? [{ executionId: sourceExecutionId, widgetName: widget.name }]
     101              :       : []
     102              :   })
     103              : }
     104              : 
     105           51 : export function hasActivePromotedWidgetConsumer(
     106              :   hostNode: LGraphNode,
     107              :   inputName: string
     108              : ): boolean {
     109           51 :   if (!hostNode.isSubgraphNode()) return false
     110           51 :   const input = hostNode.subgraph.inputNode.slots.find(
     111              :     (slot) => slot.name === inputName
     112              :   )
     113           51 :   if (!input) return false
     114              :   const root = { host: hostNode, input }
     115              : 
     116           51 :   return hasPromotedWidgetTarget(
     117              :     root,
     118              :     MAX_PROMOTED_WIDGET_CHAIN_DEPTH,
     119              :     ({ input }) => input,
     120              :     ({ host, input }) => {
     121           51 :       const nested: Array<typeof root> = []
     122           51 :       let hasWidget = false
     123           51 :       for (const linkId of input.linkIds) {
     124           51 :         const link = host.subgraph.getLink(linkId)
     125           51 :         if (!link) continue
     126           51 :         const { inputNode } = link.resolve(host.subgraph)
     127           51 :         if (!inputNode || !isNodeActive(inputNode)) continue
     128           51 :         const targetInput = inputNode.inputs.find(
     129              :           (entry) => entry.link === linkId
     130              :         )
     131           51 :         if (!targetInput) continue
     132           51 :         if (!inputNode.isSubgraphNode()) {
     133           51 :           hasWidget ||= Boolean(inputNode.getWidgetFromSlot(targetInput))
     134           51 :           continue
     135              :         }
     136            0 :         const nestedInput = inputNode.subgraph.inputNode.slots.find(
     137              :           (slot) => slot.name === targetInput.name
     138              :         )
     139           51 :         if (targetInput.widgetId && nestedInput) {
     140            0 :           nested.push({ host: inputNode, input: nestedInput })
     141              :         }
     142              :       }
     143           51 :       return { hasWidget, nested }
     144              :     }
     145              :   )
     146              : }
     147              : 
     148         2343 : function traversePromotedWidgetChain(
     149              :   hostNode: SubgraphNode,
     150              :   nodeId: NodeId,
     151              :   widgetName: string
     152              : ): PromotedWidgetResolutionResult {
     153         2343 :   const visitedByHost = new WeakMap<SubgraphNode, Set<string>>()
     154         2343 :   let currentHost = hostNode
     155         2343 :   let currentNodeId = nodeId
     156         2343 :   let currentWidgetName = widgetName
     157         2343 :   const nodePath: NodeId[] = []
     158              : 
     159         2343 :   for (let depth = 0; depth < MAX_PROMOTED_WIDGET_CHAIN_DEPTH; depth++) {
     160         2703 :     const key = `${currentNodeId}:${currentWidgetName}`
     161         2703 :     const visited = visitedByHost.get(currentHost) ?? new Set<string>()
     162         2703 :     if (visited.has(key)) {
     163            0 :       return { status: 'failure', failure: 'cycle' }
     164              :     }
     165         2703 :     visited.add(key)
     166         2703 :     visitedByHost.set(currentHost, visited)
     167              : 
     168         2703 :     const sourceNode = currentHost.subgraph.getNodeById(currentNodeId)
     169         2703 :     if (!sourceNode) {
     170           77 :       return { status: 'failure', failure: 'missing-node' }
     171              :     }
     172         2626 :     nodePath.push(sourceNode.id)
     173              : 
     174         2673 :     if (sourceNode.isSubgraphNode()) {
     175          363 :       const target = resolveSubgraphInputTarget(sourceNode, currentWidgetName)
     176          363 :       if (!target) {
     177            3 :         return { status: 'failure', failure: 'missing-widget' }
     178              :       }
     179          360 :       currentHost = sourceNode
     180          360 :       currentNodeId = target.nodeId
     181          360 :       currentWidgetName = target.widgetName
     182          360 :       continue
     183              :     }
     184              : 
     185         2703 :     const sourceWidget = sourceNode.widgets?.find(
     186              :       (entry) => entry.name === currentWidgetName
     187              :     )
     188         2703 :     if (!sourceWidget) {
     189           14 :       return { status: 'failure', failure: 'missing-widget' }
     190              :     }
     191              : 
     192         2249 :     return {
     193              :       status: 'resolved',
     194              :       resolved: { node: sourceNode, nodePath, widget: sourceWidget }
     195              :     }
     196              :   }
     197              : 
     198          360 :   return { status: 'failure', failure: 'max-depth-exceeded' }
     199              : }
     200              : 
     201         2343 : export function resolveConcretePromotedWidget(
     202              :   hostNode: LGraphNode,
     203              :   rawNodeId: SerializedNodeId,
     204              :   widgetName: string
     205              : ): PromotedWidgetResolutionResult {
     206         2343 :   if (!hostNode.isSubgraphNode()) {
     207            0 :     return { status: 'failure', failure: 'invalid-host' }
     208              :   }
     209              :   const nodeId = toNodeId(rawNodeId)
     210         2343 :   return traversePromotedWidgetChain(hostNode, nodeId, widgetName)
     211              : }
     212              : 
     213         1357 : export function buildPromotedSourceExecutionId(
     214              :   hostExecutionId: NodeExecutionId,
     215              :   nodePath: readonly NodeId[]
     216              : ): NodeExecutionId | undefined {
     217         1357 :   const hostNodeIds = hostExecutionId.split(':').map(toNodeId)
     218         1357 :   return nodePath.length
     219              :     ? (createNodeExecutionId([...hostNodeIds, ...nodePath]) ?? undefined)
     220              :     : undefined
     221              : }
        

Generated by: LCOV version 2.0-1