LCOV - code coverage report
Current view: top level - src/renderer/extensions/vueNodes/composables - processedWidgetRenderModel.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 98.2 % 109 107
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { showNodeOptions } from '@/composables/graph/useMoreOptionsMenu'
       2              : import { resolvePromotedWidgetSource } from '@/core/graph/subgraph/resolvePromotedWidgetSource'
       3              : import type { INodeInputSlot } from '@comfyorg/graph-model/slots'
       4              : import type { LGraph, LGraphNode } from '@/lib/litegraph/src/litegraph'
       5              : import type { IBaseWidget, IWidgetOptions } from '@comfyorg/graph-model/widgets'
       6              : import { useMissingMediaStore } from '@/platform/workflowErrors/missingMedia/missingMediaStore'
       7              : import { useMissingModelStore } from '@/platform/workflowErrors/missingModel/missingModelStore'
       8              : import type {
       9              :   WidgetGridItem,
      10              :   WidgetSlotMetadata
      11              : } from '@/renderer/extensions/vueNodes/types/widgetGrid'
      12              : import WidgetDOM from '@/renderer/extensions/vueNodes/widgets/components/WidgetDOM.vue'
      13              : import WidgetLegacy from '@/renderer/extensions/vueNodes/widgets/components/WidgetLegacy.vue'
      14              : import { getComponent } from '@/renderer/extensions/vueNodes/widgets/registry/widgetRegistry'
      15              : import { app } from '@/scripts/app'
      16              : import { useLinkStore } from '@/stores/linkStore'
      17              : import { graphScopeOf } from '@comfyorg/graph-model/graphScopeId'
      18              : import { useNodeDefStore } from '@/stores/nodeDefStore'
      19              : import { useExecutionErrorStore } from '@/stores/executionErrorStore'
      20              : import {
      21              :   stripGraphPrefix,
      22              :   useWidgetValueStore
      23              : } from '@/stores/widgetValueStore'
      24              : import {
      25              :   createNodeExecutionId,
      26              :   createNodeLocatorId
      27              : } from '@comfyorg/graph-model/nodeIdentification'
      28              : import type {
      29              :   NodeExecutionId,
      30              :   NodeLocatorId
      31              : } from '@comfyorg/graph-model/nodeIdentification'
      32              : import type { NodeId } from '@comfyorg/graph-model/nodeId'
      33              : import type { NodeState } from '@comfyorg/graph-model/nodeState'
      34              : import type { LinkTopology } from '@comfyorg/graph-model/linkTopology'
      35              : import { getControlWidget } from '@/types/simplifiedWidget'
      36              : import { isWidgetVisibleOnSurface } from '@/types/widgetVisibility'
      37              : import type { WidgetVisibilityComponent } from '@/types/widgetVisibility'
      38              : import type {
      39              :   LinkedUpstreamInfo,
      40              :   SafeControlWidget,
      41              :   SimplifiedWidget,
      42              :   WidgetValue
      43              : } from '@/types/simplifiedWidget'
      44              : import type { WidgetId } from '@comfyorg/graph-model/widgetId'
      45              : import {
      46              :   executionIdFromState,
      47              :   executionIdToNodeLocatorId,
      48              :   getNodeByState,
      49              :   subgraphIdFromState
      50              : } from '@/utils/graphTraversalUtil'
      51              : import { mapLiveWidgetsById } from '@/utils/litegraphUtil'
      52              : 
      53              : type TooltipValueType = 'asset' | 'combo' | 'number' | 'text'
      54        46558 : function isTooltipValueType(val: unknown): val is TooltipValueType {
      55        46558 :   return (
      56              :     val === 'asset' || val === 'combo' || val === 'number' || val === 'text'
      57              :   )
      58              : }
      59              : 
      60              : interface WidgetTooltipSource {
      61              :   name: string
      62              :   tooltip?: string
      63              : }
      64              : 
      65              : interface WidgetErrorTarget {
      66              :   executionId: NodeExecutionId
      67              :   widgetName: string
      68              : }
      69              : 
      70              : export interface ProcessedWidget extends WidgetGridItem {
      71              :   handleContextMenu: (e: PointerEvent) => void
      72              :   hasError: boolean
      73              :   hasLayoutSize: boolean
      74              :   tooltipText: string
      75              :   updateHandler: (value: WidgetValue) => void
      76              :   widgetId: WidgetId
      77              : }
      78              : 
      79              : export interface WidgetUiCallbacks {
      80              :   getTooltipText: (widget: WidgetTooltipSource, fullValue?: string) => string
      81              :   handleNodeRightClick: (e: PointerEvent, nodeId: NodeId) => void
      82              : }
      83              : 
      84              : export interface ComputeProcessedWidgetsOptions {
      85              :   nodeData: NodeState | undefined
      86              :   widgetIds?: readonly WidgetId[]
      87              :   graphId: string | undefined
      88              :   showAdvanced: boolean
      89              :   isGraphReady: boolean
      90              :   rootGraph: LGraph | null
      91              :   ui: WidgetUiCallbacks
      92              : }
      93              : 
      94        46850 : function normalizeWidgetValue(value: unknown): WidgetValue {
      95        46850 :   if (value === undefined) {
      96          490 :     return undefined
      97              :   }
      98        46850 :   if (value === null) {
      99           15 :     return null
     100              :   }
     101        46850 :   if (
     102              :     typeof value === 'string' ||
     103              :     typeof value === 'number' ||
     104              :     typeof value === 'boolean'
     105              :   ) {
     106        46179 :     return value
     107              :   }
     108          166 :   if (typeof value === 'object') return value
     109            0 :   console.warn(`Invalid widget value type: ${typeof value}`, value)
     110              :   return undefined
     111              : }
     112              : 
     113        42463 : function createSlotMetadata(
     114              :   input: INodeInputSlot,
     115              :   index: number,
     116              :   link: LinkTopology | undefined,
     117              :   graphRef: LGraph | null | undefined
     118              : ): WidgetSlotMetadata {
     119        42463 :   const originNode = link ? graphRef?.getNodeById(link.originNodeId) : null
     120        42463 :   return {
     121              :     index,
     122              :     linked: link !== undefined,
     123              :     originNodeId: link?.originNodeId,
     124              :     originOutputName: link
     125              :       ? originNode?.outputs[link.originSlot]?.name
     126              :       : undefined,
     127              :     promoted: input.widgetId !== undefined,
     128              :     type: String(input.type)
     129              :   }
     130              : }
     131              : 
     132        65053 : function getSlotWidgetName(input: INodeInputSlot): string | undefined {
     133        65053 :   return (
     134              :     input.widget?.name ||
     135              :     (input.widgetId !== undefined && input.name ? input.name : undefined)
     136              :   )
     137              : }
     138              : 
     139        18963 : function buildSlotMetadata(
     140              :   inputs: INodeInputSlot[] | undefined,
     141              :   graphRef: LGraph | null | undefined,
     142              :   nodeId: NodeId
     143              : ): Map<string, WidgetSlotMetadata> {
     144        18963 :   const linkStore = useLinkStore()
     145        18963 :   const scope = graphRef ? graphScopeOf(graphRef) : undefined
     146        18963 :   const metadata = new Map<string, WidgetSlotMetadata>()
     147        18963 :   for (const [index, input] of inputs?.entries() ?? []) {
     148        65053 :     const link = scope
     149              :       ? linkStore.getInputSlotLink(scope, nodeId, index)
     150              :       : undefined
     151        65053 :     const widgetName = getSlotWidgetName(input)
     152        65053 :     if (!widgetName || metadata.has(widgetName)) continue
     153        42463 :     metadata.set(widgetName, createSlotMetadata(input, index, link, graphRef))
     154              :   }
     155        18963 :   return metadata
     156              : }
     157              : 
     158        18963 : function getHostNode(
     159              :   rootGraph: LGraph | null,
     160              :   nodeData: NodeState
     161              : ): LGraphNode | null {
     162        18963 :   if (!rootGraph) return null
     163        18963 :   return getNodeByState(rootGraph, nodeData)
     164              : }
     165              : 
     166        46558 : function isWidgetVisible(
     167              :   visibility: WidgetVisibilityComponent | undefined,
     168              :   showAdvanced: boolean,
     169              :   ignoreAdvanced = false
     170              : ): boolean {
     171        46558 :   if (!visibility) return true
     172        46558 :   return isWidgetVisibleOnSurface(visibility, 'vueNode', {
     173              :     showAdvanced: showAdvanced || ignoreAdvanced
     174              :   })
     175              : }
     176              : 
     177        46558 : function hasWidgetError(
     178              :   widget: { name: string; errorTarget?: WidgetErrorTarget },
     179              :   nodeExecId: NodeExecutionId | null,
     180              :   nodeErrors:
     181              :     | { errors: { extra_info?: { input_name?: string } }[] }
     182              :     | undefined,
     183              :   executionErrorStore: ReturnType<typeof useExecutionErrorStore>,
     184              :   missingModelStore: ReturnType<typeof useMissingModelStore>,
     185              :   missingMediaStore: ReturnType<typeof useMissingMediaStore>
     186              : ): boolean {
     187        46558 :   const hasHostError =
     188              :     !!nodeErrors?.errors.some(
     189              :       (e) => e.extra_info?.input_name === widget.name
     190              :     ) ||
     191              :     (nodeExecId !== null &&
     192              :       (missingModelStore.isWidgetMissingModel(nodeExecId, widget.name) ||
     193              :         missingMediaStore.isWidgetMissingMedia(nodeExecId, widget.name)))
     194        46558 :   const target = widget.errorTarget
     195        46558 :   if (!target) return hasHostError
     196              : 
     197        46558 :   const sourceErrors = executionErrorStore.lastNodeErrors?.[target.executionId]
     198        46558 :   return (
     199              :     hasHostError ||
     200              :     !!sourceErrors?.errors.some(
     201              :       (e) => e.extra_info?.input_name === target.widgetName
     202              :     ) ||
     203              :     missingModelStore.isWidgetMissingModel(
     204              :       target.executionId,
     205              :       target.widgetName
     206              :     )
     207              :   )
     208              : }
     209              : 
     210        46558 : function createWidgetUpdateHandler({
     211              :   id,
     212              :   live,
     213              :   errorTarget,
     214              :   nodeExecId,
     215              :   widgetName,
     216              :   widgetOptions,
     217              :   executionErrorStore,
     218              :   widgetValueStore
     219              : }: {
     220              :   id: WidgetId
     221              :   live?: { node: LGraphNode; widget: IBaseWidget }
     222              :   errorTarget?: WidgetErrorTarget
     223              :   nodeExecId: NodeExecutionId | null
     224              :   widgetName: string
     225              :   widgetOptions: IWidgetOptions
     226              :   executionErrorStore: ReturnType<typeof useExecutionErrorStore>
     227              :   widgetValueStore: ReturnType<typeof useWidgetValueStore>
     228              : }): (newValue: WidgetValue) => void {
     229        46558 :   return (newValue: WidgetValue) => {
     230          292 :     widgetValueStore.setValue(id, newValue)
     231          292 :     if (live) {
     232          292 :       const normalized = normalizeWidgetValue(newValue)
     233          292 :       live.widget.value = normalized
     234          292 :       live.widget.callback?.(normalized, app.canvas, live.node)
     235          292 :       live.node.widgets?.forEach((w) => w.triggerDraw?.())
     236              :     }
     237              : 
     238          292 :     const options = { min: widgetOptions.min, max: widgetOptions.max }
     239          292 :     if (errorTarget) {
     240           25 :       executionErrorStore.clearWidgetRelatedErrors(
     241              :         errorTarget.executionId,
     242              :         errorTarget.widgetName,
     243              :         errorTarget.widgetName,
     244              :         newValue,
     245              :         options
     246              :       )
     247              :     }
     248          292 :     if (nodeExecId) {
     249          292 :       executionErrorStore.clearWidgetRelatedErrors(
     250              :         nodeExecId,
     251              :         widgetName,
     252              :         widgetName,
     253              :         newValue,
     254              :         options
     255              :       )
     256              :     }
     257              :   }
     258              : }
     259              : 
     260        18963 : function resolveWidgetIds(
     261              :   graphId: string | undefined,
     262              :   nodeId: NodeId,
     263              :   explicitWidgetIds: readonly WidgetId[] | undefined,
     264              :   widgetValueStore: ReturnType<typeof useWidgetValueStore>
     265              : ): readonly WidgetId[] {
     266        18963 :   if (explicitWidgetIds) return explicitWidgetIds
     267            0 :   const bareNodeId = stripGraphPrefix(nodeId)
     268        18963 :   return graphId && bareNodeId
     269              :     ? widgetValueStore.getNodeWidgetIds(graphId, bareNodeId)
     270              :     : []
     271              : }
     272              : 
     273              : interface LiveWidgetContext {
     274              :   live?: { node: LGraphNode; widget: IBaseWidget }
     275              :   errorTarget?: WidgetErrorTarget
     276              :   controlWidget?: SafeControlWidget
     277              :   sourceExecutionId?: NodeExecutionId
     278              : }
     279              : 
     280              : /**
     281              :  * Resolves the live litegraph widget (and, for promoted subgraph inputs, its
     282              :  * interior source) into the control widget and error target the render path
     283              :  * needs. Empty when the widget has no live counterpart (e.g. static previews).
     284              :  */
     285        46558 : function resolveLiveWidgetContext(
     286              :   rootGraph: LGraph | null,
     287              :   hostNode: LGraphNode | null,
     288              :   liveWidget: IBaseWidget | undefined
     289              : ): LiveWidgetContext {
     290        46558 :   if (!hostNode || !liveWidget) return {}
     291              : 
     292        46558 :   const promotedSource = resolvePromotedWidgetSource(
     293              :     rootGraph,
     294              :     hostNode,
     295              :     liveWidget
     296              :   )
     297        46558 :   const errorTarget: WidgetErrorTarget | undefined =
     298              :     promotedSource?.sourceExecutionId
     299              :       ? {
     300              :           executionId: promotedSource.sourceExecutionId,
     301              :           widgetName: promotedSource.sourceWidgetName
     302              :         }
     303              :       : undefined
     304        46558 :   const controlWidget =
     305              :     getControlWidget(liveWidget) ??
     306              :     (promotedSource?.sourceWidget
     307              :       ? getControlWidget(promotedSource.sourceWidget)
     308              :       : undefined)
     309              : 
     310        46558 :   return {
     311              :     live: { node: hostNode, widget: liveWidget },
     312              :     errorTarget,
     313              :     controlWidget,
     314              :     sourceExecutionId: promotedSource?.sourceExecutionId
     315              :   }
     316              : }
     317              : 
     318              : /**
     319              :  * A promoted widget renders the interior source node's identity, so consumers
     320              :  * that resolve a node from the locator reach the source rather than the host.
     321              :  */
     322        46558 : function widgetNodeLocatorId(
     323              :   ctx: WidgetProcessingContext,
     324              :   bareWidgetId: NodeId | null,
     325              :   sourceExecutionId: NodeExecutionId | undefined
     326              : ): NodeLocatorId | undefined {
     327        46558 :   if (sourceExecutionId && ctx.rootGraph) {
     328          554 :     const sourceLocator = executionIdToNodeLocatorId(
     329              :       ctx.rootGraph,
     330              :       sourceExecutionId
     331              :     )
     332          554 :     if (sourceLocator) return sourceLocator
     333              :   }
     334        46558 :   if (!bareWidgetId || bareWidgetId.includes(':')) return undefined
     335        46001 :   return createNodeLocatorId(
     336              :     subgraphIdFromState(ctx.nodeData, ctx.rootGraphId),
     337              :     bareWidgetId
     338              :   )
     339              : }
     340              : 
     341              : interface WidgetProcessingContext {
     342              :   nodeData: NodeState
     343              :   showAdvanced: boolean
     344              :   rootGraph: LGraph | null
     345              :   /** Root graph id, known even before `app.isGraphReady`. */
     346              :   rootGraphId: string | undefined
     347              :   hostNode: LGraphNode | null
     348              :   liveWidgets: Map<WidgetId, IBaseWidget>
     349              :   slotMetadata: Map<string, WidgetSlotMetadata>
     350              :   nodeExecId: NodeExecutionId | null
     351              :   nodeErrors: Parameters<typeof hasWidgetError>[2]
     352              :   widgetValueStore: ReturnType<typeof useWidgetValueStore>
     353              :   executionErrorStore: ReturnType<typeof useExecutionErrorStore>
     354              :   missingModelStore: ReturnType<typeof useMissingModelStore>
     355              :   missingMediaStore: ReturnType<typeof useMissingMediaStore>
     356              :   nodeDefStore: ReturnType<typeof useNodeDefStore>
     357              :   ui: WidgetUiCallbacks
     358              : }
     359              : 
     360        46558 : function processWidget(
     361              :   id: WidgetId,
     362              :   ctx: WidgetProcessingContext
     363              : ): ProcessedWidget | null {
     364        46558 :   const widgetState = ctx.widgetValueStore.getWidget(id)
     365        46558 :   if (!widgetState) return null
     366              : 
     367        46558 :   const liveWidget = ctx.liveWidgets.get(id)
     368        46558 :   const type = liveWidget?.type ?? widgetState.type
     369        46558 :   const renderState = ctx.widgetValueStore.getWidgetRenderState(id)
     370        46558 :   const visibility = ctx.widgetValueStore.getWidgetVisibility(id)
     371        46558 :   if (!type) return null
     372        46558 :   const options: IWidgetOptions = { ...widgetState.options }
     373              : 
     374        46558 :   const { live, errorTarget, controlWidget, sourceExecutionId } =
     375              :     resolveLiveWidgetContext(ctx.rootGraph, ctx.hostNode, liveWidget)
     376              : 
     377        46558 :   const slotInfo = ctx.slotMetadata.get(widgetState.name)
     378        46558 :   const visible = isWidgetVisible(
     379              :     visibility,
     380              :     ctx.showAdvanced,
     381              :     slotInfo?.linked || slotInfo?.promoted
     382              :   )
     383        46558 :   const advanced = visibility
     384              :     ? visibility.surfaces.vueNode === 'advanced'
     385              :     : (options.advanced ?? false)
     386              :   const isDisabled = slotInfo?.linked || widgetState.disabled
     387        46558 :   const widgetOptions = isDisabled ? { ...options, disabled: true } : options
     388        46558 :   const value = normalizeWidgetValue(widgetState.value)
     389        46558 :   const bareWidgetId = stripGraphPrefix(widgetState.nodeId)
     390        46558 :   const linkedUpstream: LinkedUpstreamInfo | undefined =
     391              :     slotInfo?.linked && slotInfo.originNodeId
     392              :       ? { nodeId: slotInfo.originNodeId, outputName: slotInfo.originOutputName }
     393              :       : undefined
     394              : 
     395        46558 :   const updateHandler = createWidgetUpdateHandler({
     396              :     id,
     397              :     live,
     398              :     errorTarget,
     399              :     nodeExecId: ctx.nodeExecId,
     400              :     widgetName: widgetState.name,
     401              :     widgetOptions,
     402              :     executionErrorStore: ctx.executionErrorStore,
     403              :     widgetValueStore: ctx.widgetValueStore
     404              :   })
     405              : 
     406        46558 :   const simplified: SimplifiedWidget = {
     407              :     name: widgetState.name,
     408              :     type,
     409              :     value,
     410              :     borderStyle: advanced ? 'ring ring-primary' : undefined,
     411              :     callback: updateHandler,
     412              :     controlWidget,
     413              :     label: widgetState.label,
     414              :     linkedUpstream,
     415              :     nodeLocatorId: widgetNodeLocatorId(ctx, bareWidgetId, sourceExecutionId),
     416              :     options: widgetOptions,
     417              :     spec: live
     418              :       ? ctx.nodeDefStore.getInputSpecForWidget(live.node, live.widget.name)
     419              :       : undefined
     420              :   }
     421              : 
     422        46558 :   const fullValue =
     423              :     isTooltipValueType(type) && String(value).length > 10
     424              :       ? String(value)
     425              :       : undefined
     426        46558 :   const tooltipText = ctx.ui.getTooltipText(
     427              :     { name: widgetState.name, tooltip: renderState?.tooltip },
     428              :     fullValue
     429              :   )
     430        46558 :   const handleContextMenu = (e: PointerEvent) => {
     431           36 :     e.preventDefault()
     432           36 :     e.stopPropagation()
     433           36 :     ctx.ui.handleNodeRightClick(e, ctx.nodeData.id)
     434           36 :     showNodeOptions(e, {
     435              :       nodeId: ctx.nodeData.id,
     436              :       widgetName: widgetState.name
     437              :     })
     438              :   }
     439              : 
     440        46558 :   return {
     441              :     handleContextMenu,
     442              :     hasLayoutSize: renderState?.hasLayoutSize ?? false,
     443              :     hasError: hasWidgetError(
     444              :       { name: widgetState.name, errorTarget },
     445              :       ctx.nodeExecId,
     446              :       ctx.nodeErrors,
     447              :       ctx.executionErrorStore,
     448              :       ctx.missingModelStore,
     449              :       ctx.missingMediaStore
     450              :     ),
     451              :     widgetId: id,
     452              :     renderKey: `${id}:${type}`,
     453              :     vueComponent:
     454              :       !renderState?.isDOMWidget && typeof liveWidget?.draw === 'function'
     455              :         ? WidgetLegacy
     456              :         : getComponent(type) ||
     457              :           (renderState?.isDOMWidget ? WidgetDOM : WidgetLegacy),
     458              :     simplified,
     459              :     visible,
     460              :     suppressedByConnection: visibility?.suppression.byConnection ?? false,
     461              :     updateHandler,
     462              :     tooltipText,
     463              :     slotMetadata: slotInfo
     464              :   }
     465              : }
     466              : 
     467        18963 : export function computeProcessedWidgets({
     468              :   nodeData,
     469              :   widgetIds,
     470              :   graphId,
     471              :   showAdvanced,
     472              :   isGraphReady,
     473              :   rootGraph,
     474              :   ui
     475              : }: ComputeProcessedWidgetsOptions): ProcessedWidget[] {
     476        18963 :   if (!nodeData) return []
     477              : 
     478        18963 :   const executionErrorStore = useExecutionErrorStore()
     479        18963 :   const missingModelStore = useMissingModelStore()
     480        18963 :   const missingMediaStore = useMissingMediaStore()
     481        18963 :   const widgetValueStore = useWidgetValueStore()
     482        18963 :   const nodeDefStore = useNodeDefStore()
     483              : 
     484        18963 :   const nodeExecId =
     485              :     isGraphReady && rootGraph
     486              :       ? executionIdFromState(rootGraph, nodeData)
     487              :       : createNodeExecutionId([nodeData.id])
     488              : 
     489        18963 :   const hostNode = getHostNode(rootGraph, nodeData)
     490        18963 :   const liveWidgets = hostNode
     491              :     ? mapLiveWidgetsById(hostNode)
     492              :     : new Map<WidgetId, IBaseWidget>()
     493        18963 :   const orderedIds = resolveWidgetIds(
     494              :     graphId,
     495              :     nodeData.id,
     496              :     widgetIds,
     497              :     widgetValueStore
     498              :   )
     499              :   // Drop ids whose live widget is gone (e.g. removed directly on node.widgets);
     500              :   // when the host node isn't resolvable yet, fall back to the stored order.
     501        18963 :   const ids = hostNode
     502              :     ? orderedIds.filter((id) => liveWidgets.has(id))
     503              :     : orderedIds
     504        18963 :   const graphRef = hostNode?.graph ?? rootGraph
     505              :   const slotMetadata = buildSlotMetadata(
     506              :     hostNode?.inputs,
     507              :     graphRef,
     508              :     nodeData.id
     509              :   )
     510        18963 :   const ctx: WidgetProcessingContext = {
     511              :     nodeData,
     512              :     showAdvanced,
     513              :     rootGraph,
     514              :     rootGraphId: graphId,
     515              :     hostNode,
     516              :     liveWidgets,
     517              :     slotMetadata,
     518              :     nodeExecId,
     519              :     nodeErrors: nodeExecId
     520              :       ? executionErrorStore.lastNodeErrors?.[nodeExecId]
     521              :       : undefined,
     522              :     widgetValueStore,
     523              :     executionErrorStore,
     524              :     missingModelStore,
     525              :     missingMediaStore,
     526              :     nodeDefStore,
     527              :     ui
     528              :   }
     529              : 
     530        18963 :   return Array.from(new Set(ids))
     531              :     .map((id) => processWidget(id, ctx))
     532              :     .filter((widget): widget is ProcessedWidget => widget !== null)
     533              : }
        

Generated by: LCOV version 2.0-1