LCOV - code coverage report
Current view: top level - src/extensions/core - rerouteNode.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 67.5 % 114 77
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import type { IContextMenuValue, LGraph } from '@/lib/litegraph/src/litegraph'
       2              : import {
       3              :   LGraphCanvas,
       4              :   LGraphNode,
       5              :   LiteGraph
       6              : } from '@/lib/litegraph/src/litegraph'
       7              : import type { ISlotType } from '@comfyorg/graph-model/interfaces'
       8              : import { outputLinks } from '@/lib/litegraph/src/node/slotLinks'
       9              : import { slotTypeKey } from '@/lib/litegraph/src/utils/type'
      10              : 
      11              : import { app } from '@/scripts/app'
      12              : import { getWidgetConfig, mergeIfValid, setWidgetConfig } from './widgetInputs'
      13              : import type { ComfyExtension } from '@/types/comfy'
      14              : 
      15              : // Node that allows you to redirect connections for cleaner graphs
      16              : 
      17         2355 : export const rerouteNodeExtension: ComfyExtension = {
      18              :   name: 'Comfy.RerouteNode',
      19              :   registerCustomNodes() {
      20              :     interface RerouteNode extends LGraphNode {
      21              :       __outputType?: string | number
      22              :     }
      23              : 
      24         2355 :     class RerouteNode extends LGraphNode {
      25              :       static override category: string | undefined
      26              :       static defaultVisibility = false
      27              : 
      28              :       constructor(title?: string) {
      29            3 :         super(title ?? '')
      30            3 :         this.properties.showOutputText = RerouteNode.defaultVisibility
      31            3 :         this.properties.horizontal = false
      32              : 
      33            3 :         this.addInput('', '*')
      34            3 :         this.addOutput(this.properties.showOutputText ? '*' : '', '*')
      35              : 
      36            3 :         this.setSize(this.computeSize())
      37              : 
      38              :         // This node is purely frontend and does not impact the resulting prompt so should not be serialized
      39            3 :         this.isVirtualNode = true
      40              :       }
      41              :       override onAfterGraphConfigured() {
      42            2 :         requestAnimationFrame(() => {
      43            2 :           this.onConnectionsChange(LiteGraph.INPUT, undefined, true)
      44              :         })
      45              :       }
      46              :       override clone(): LGraphNode | null {
      47            0 :         const cloned = super.clone()
      48            0 :         if (!cloned) return cloned
      49            0 :         cloned.removeOutput(0)
      50            0 :         cloned.addOutput(this.properties.showOutputText ? '*' : '', '*')
      51            0 :         cloned.setSize(cloned.computeSize())
      52            0 :         return cloned
      53              :       }
      54              :       override onConnectionsChange(
      55              :         type: ISlotType,
      56              :         _index: number | undefined,
      57              :         connected: boolean
      58              :       ) {
      59            8 :         const { graph } = this
      60            8 :         if (!graph) return
      61            8 :         if (app.configuringGraph) return
      62              : 
      63              :         // Prevent multiple connections to different types when we have no input
      64            8 :         if (connected && type === LiteGraph.OUTPUT) {
      65            1 :           const links = outputLinks(graph, this.id, 0)
      66              :           // Ignore wildcard nodes as these will be updated to real types
      67              :           const types = new Set(
      68              :             links.map((l) => l.type).filter((t) => t && t !== '*')
      69              :           )
      70            1 :           if (types.size > 1) {
      71            0 :             const linksToDisconnect = links.slice(0, -1)
      72            0 :             for (const link of linksToDisconnect) {
      73              :               const node = graph.getNodeById(link.target_id)
      74            0 :               node?.disconnectInput(link.target_slot)
      75              :             }
      76              :           }
      77              :         }
      78              : 
      79            6 :         const rootInput = findRootInput(this, graph)
      80            8 :         if (!rootInput) return
      81            6 :         const { updateNodes, inputNode, inputType } = rootInput
      82              : 
      83              :         // Find all outputs
      84            6 :         const nodes: RerouteNode[] = [this]
      85            6 :         let outputType = null
      86            6 :         while (nodes.length) {
      87            6 :           const currentNode = nodes.pop()!
      88            6 :           for (const link of outputLinks(graph, currentNode.id, 0)) {
      89            3 :             const node = graph.getNodeById(link.target_id)
      90            3 :             if (!node) continue
      91            3 :             if (node instanceof RerouteNode) {
      92              :               // Follow reroute nodes
      93            0 :               nodes.push(node)
      94            0 :               updateNodes.push(node)
      95              :             } else {
      96              :               // We've found an output
      97            3 :               const nodeInput = node.inputs[link.target_slot]
      98            3 :               const nodeOutType = nodeInput.type
      99            3 :               const keep =
     100              :                 !inputType ||
     101              :                 !nodeOutType ||
     102              :                 LiteGraph.isValidConnection(inputType, nodeOutType)
     103            3 :               if (!keep) {
     104              :                 // The output doesnt match our input so disconnect it
     105            0 :                 node.disconnectInput(link.target_slot)
     106            0 :                 continue
     107              :               }
     108            3 :               node.onConnectionsChange?.(
     109              :                 LiteGraph.INPUT,
     110              :                 link.target_slot,
     111              :                 keep,
     112              :                 link,
     113              :                 nodeInput
     114              :               )
     115            3 :               outputType = node.inputs[link.target_slot].type
     116              :             }
     117              :           }
     118              :         }
     119              : 
     120            8 :         const displayType = slotTypeKey(inputType || outputType || '*')
     121            8 :         const color = LGraphCanvas.link_type_colors[displayType]
     122              : 
     123            8 :         let widgetConfig
     124            8 :         let widgetType
     125              :         // Update the types of each node
     126            8 :         for (const node of updateNodes) {
     127              :           // If we dont have an input type we are always wildcard but we'll show the output type
     128              :           // This lets you change the output link to a different type and all nodes will update
     129            6 :           node.outputs[0].type = inputType || '*'
     130            6 :           node.__outputType = displayType
     131            6 :           node.outputs[0].name = node.properties.showOutputText
     132              :             ? `${displayType}`
     133              :             : ''
     134            6 :           node.setSize(node.computeSize())
     135              : 
     136            6 :           for (const link of outputLinks(graph, node.id, 0)) {
     137            3 :             link.color = color
     138              : 
     139            3 :             const targetNode = graph.getNodeById(link.target_id)
     140            3 :             if (!targetNode) continue
     141            3 :             const targetInput = targetNode.inputs.at(link.target_slot)
     142            3 :             if (targetInput?.widget) {
     143            0 :               const config = getWidgetConfig(targetInput)
     144            0 :               if (!widgetConfig) {
     145            0 :                 widgetConfig = config[1] ?? {}
     146            0 :                 widgetType = config[0]
     147              :               }
     148              : 
     149            0 :               const merged = mergeIfValid(targetInput, [
     150              :                 config[0],
     151              :                 widgetConfig
     152              :               ])
     153            0 :               if (merged.customConfig) {
     154            0 :                 widgetConfig = merged.customConfig
     155              :               }
     156              :             }
     157              :           }
     158              :         }
     159              : 
     160            6 :         for (const node of updateNodes) {
     161            6 :           if (widgetConfig && outputType) {
     162            0 :             node.inputs[0].widget = { name: 'value' }
     163            0 :             setWidgetConfig(node.inputs[0], [
     164              :               widgetType ?? `${displayType}`,
     165              :               widgetConfig
     166              :             ])
     167              :           } else {
     168            6 :             setWidgetConfig(node.inputs[0], undefined)
     169              :           }
     170              :         }
     171              : 
     172            8 :         const inputNodeLink = inputNode?.getInputLink(0)
     173            8 :         if (inputNodeLink) {
     174            5 :           inputNodeLink.color = color
     175              :         }
     176              :       }
     177              : 
     178              :       override getExtraMenuOptions(
     179              :         _: unknown,
     180              :         options: (IContextMenuValue | null)[]
     181              :       ): IContextMenuValue[] {
     182            0 :         options.unshift(
     183              :           {
     184              :             content:
     185              :               (this.properties.showOutputText ? 'Hide' : 'Show') + ' Type',
     186              :             callback: () => {
     187            0 :               this.properties.showOutputText = !this.properties.showOutputText
     188            0 :               if (this.properties.showOutputText) {
     189            0 :                 this.outputs[0].name = `${this.__outputType || this.outputs[0].type}`
     190              :               } else {
     191            0 :                 this.outputs[0].name = ''
     192              :               }
     193            0 :               this.setSize(this.computeSize())
     194            0 :               app.canvas.setDirty(true, true)
     195              :             }
     196              :           },
     197              :           {
     198              :             content:
     199              :               (RerouteNode.defaultVisibility ? 'Hide' : 'Show') +
     200              :               ' Type By Default',
     201              :             callback: () => {
     202            0 :               RerouteNode.setDefaultTextVisibility(
     203              :                 !RerouteNode.defaultVisibility
     204              :               )
     205              :             }
     206              :           }
     207              :         )
     208            0 :         return []
     209              :       }
     210              :       override computeSize(): [number, number] {
     211           16 :         return [
     212              :           this.properties.showOutputText && this.outputs.length
     213              :             ? Math.max(
     214              :                 75,
     215              :                 LiteGraph.NODE_TEXT_SIZE * this.outputs[0].name.length * 0.6 +
     216              :                   40
     217              :               )
     218              :             : 75,
     219              :           26
     220              :         ]
     221              :       }
     222              : 
     223              :       static setDefaultTextVisibility(visible: boolean) {
     224         2355 :         RerouteNode.defaultVisibility = visible
     225         2355 :         if (visible) {
     226            0 :           localStorage['Comfy.RerouteNode.DefaultVisibility'] = 'true'
     227              :         } else {
     228         2355 :           delete localStorage['Comfy.RerouteNode.DefaultVisibility']
     229              :         }
     230              :       }
     231              :     }
     232              : 
     233              :     // Load default visibility
     234         2355 :     RerouteNode.setDefaultTextVisibility(
     235              :       !!localStorage['Comfy.RerouteNode.DefaultVisibility']
     236              :     )
     237              : 
     238            6 :     function findRootInput(start: RerouteNode, graph: LGraph) {
     239            6 :       const updateNodes: RerouteNode[] = [start]
     240            6 :       let currentNode = start
     241            6 :       let inputType: ISlotType | null = null
     242            6 :       let inputNode: RerouteNode | null = null
     243            6 :       while (currentNode.isInputConnected(0)) {
     244            5 :         const link = currentNode.getInputLink(0)
     245            5 :         if (!link) return null
     246            5 :         const node = graph.getNodeById(link.origin_id)
     247            5 :         if (!node) return null
     248            5 :         if (!(node instanceof RerouteNode)) {
     249              :           // We've found the end
     250            5 :           inputNode = currentNode
     251            5 :           inputType = node.outputs[link.origin_slot]?.type ?? null
     252            5 :           break
     253              :         }
     254            0 :         if (node === start) {
     255              :           // We've found a circle
     256            0 :           currentNode.disconnectInput(link.target_slot)
     257            0 :           break
     258              :         }
     259              :         // Move the previous node
     260            0 :         updateNodes.unshift(node)
     261            0 :         currentNode = node
     262              :       }
     263            6 :       return { updateNodes, inputNode, inputType }
     264              :     }
     265              : 
     266         2355 :     LiteGraph.registerNodeType(
     267              :       'Reroute',
     268              :       Object.assign(RerouteNode, {
     269              :         title_mode: LiteGraph.NO_TITLE,
     270              :         title: 'Reroute',
     271              :         collapsable: false
     272              :       })
     273              :     )
     274              : 
     275         2355 :     RerouteNode.category = 'utilities'
     276              :   }
     277              : }
        

Generated by: LCOV version 2.0-1