LCOV - code coverage report
Current view: top level - src/platform/customNodes/manager/composables/nodePack - useWorkflowPacks.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 92.1 % 63 58
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { createSharedComposable, useEventListener } from '@vueuse/core'
       2              : import { computed, onUnmounted, ref } from 'vue'
       3              : 
       4              : import type { LGraphNode } from '@/lib/litegraph/src/litegraph'
       5              : import type { ComfyWorkflowJSON } from '@comfyorg/graph-model/workflowSchema'
       6              : import { app } from '@/scripts/app'
       7              : import { useComfyRegistryStore } from '@/stores/comfyRegistryStore'
       8              : import { useNodeDefStore } from '@/stores/nodeDefStore'
       9              : import { useSystemStatsStore } from '@/stores/systemStatsStore'
      10              : import type { components } from '@/types/comfyRegistryTypes'
      11              : import { mapAllNodes } from '@/utils/graphTraversalUtil'
      12              : import { useNodePacks } from '@/platform/customNodes/manager/composables/nodePack/useNodePacks'
      13              : 
      14              : export type WorkflowPack = {
      15              :   id: ComfyWorkflowJSON['nodes'][number]['properties']['cnr_id']
      16              : 
      17              :   version: ComfyWorkflowJSON['nodes'][number]['properties']['ver']
      18              : }
      19              : 
      20         2355 : const CORE_NODES_PACK_NAME = 'comfy-core'
      21              : 
      22              : /**
      23              :  * Handles parsing node pack metadata from nodes on the graph and fetching the
      24              :  * associated node packs from the registry.
      25              :  * This is a shared singleton composable - all components use the same instance.
      26              :  */
      27         2355 : const _useWorkflowPacks = () => {
      28          104 :   const nodeDefStore = useNodeDefStore()
      29          104 :   const systemStatsStore = useSystemStatsStore()
      30          104 :   const { inferPackFromNodeName } = useComfyRegistryStore()
      31              : 
      32          104 :   const workflowPacks = ref<WorkflowPack[]>([])
      33          104 :   const unresolvedNodeNames = ref<string[]>([])
      34              : 
      35              :   /** A fetch asked for before the graph existed, owed a retry on `configured`. */
      36          104 :   let fetchDeferredUntilConfigured = false
      37              : 
      38          104 :   let fetchGeneration = 0
      39              : 
      40          104 :   const getWorkflowNodePackId = (node: LGraphNode): string | undefined => {
      41          293 :     if (typeof node.properties.cnr_id === 'string') {
      42            3 :       return node.properties.cnr_id
      43              :     }
      44          291 :     if (typeof node.properties.aux_id === 'string') {
      45            0 :       return node.properties.aux_id
      46              :     }
      47              :     return undefined
      48              :   }
      49              : 
      50              :   /**
      51              :    * Clean the version string to be used in the registry search.
      52              :    * Removes the leading 'v' and trims whitespace and line terminators.
      53              :    */
      54          104 :   const cleanVersionString = (version: string) =>
      55              :     version.replace(/^v/, '').trim()
      56              : 
      57              :   /**
      58              :    * Infer the pack for a node by searching the registry for packs that have nodes
      59              :    * with the same name.
      60              :    */
      61          104 :   const inferPack = async (
      62              :     node: LGraphNode
      63              :   ): Promise<WorkflowPack | undefined> => {
      64          290 :     const nodeName = node.type
      65              : 
      66          290 :     if (nodeDefStore.nodeDefsByName[nodeName]?.isCoreNode) {
      67          245 :       if (!systemStatsStore.systemStats) {
      68            0 :         await systemStatsStore.refetchSystemStats()
      69              :       }
      70          245 :       return {
      71              :         id: CORE_NODES_PACK_NAME,
      72              :         version:
      73              :           systemStatsStore.systemStats?.system.comfyui_version ?? 'nightly'
      74              :       }
      75              :     }
      76              : 
      77              :     // Query the registry to find which pack provides this node
      78           45 :     const pack = await inferPackFromNodeName.call(nodeName)
      79              : 
      80           74 :     if (pack) {
      81            0 :       return {
      82              :         id: pack.id,
      83              :         version: pack.latest_version?.version ?? 'nightly'
      84              :       }
      85              :     }
      86              : 
      87              :     // No pack found - this node doesn't exist in the registry or couldn't be
      88              :     // extracted from the parent node pack successfully
      89              :     return undefined
      90              :   }
      91              : 
      92              :   /**
      93              :    * Map a workflow node to its pack using the node pack metadata.
      94              :    * If the node pack metadata is not available, fallback to searching the
      95              :    * registry for packs that have nodes with the same name.
      96              :    */
      97          104 :   const workflowNodeToPack = async (
      98              :     node: LGraphNode
      99              :   ): Promise<WorkflowPack | undefined> => {
     100          293 :     const packId = getWorkflowNodePackId(node)
     101          293 :     if (!packId) return inferPack(node) // Fallback
     102           10 :     if (packId === CORE_NODES_PACK_NAME) return undefined
     103              : 
     104              :     const version =
     105              :       typeof node.properties.ver === 'string'
     106              :         ? cleanVersionString(node.properties.ver)
     107              :         : undefined
     108              : 
     109          293 :     return {
     110              :       id: packId,
     111              :       version
     112              :     }
     113              :   }
     114              : 
     115              :   /**
     116              :    * Get the node packs for all nodes in the workflow (including subgraphs).
     117              :    * Nodes that have no local definition and no registry match are tracked
     118              :    * as unresolved so downstream consumers can surface them to the user.
     119              :    *
     120              :    * @returns `undefined` when the root graph does not exist yet.
     121              :    */
     122          104 :   const getWorkflowPacks = async () => {
     123          104 :     const rootGraph = app.rootGraphOrUndefined
     124          104 :     if (!rootGraph) return undefined
     125              : 
     126          104 :     const resolvedPacks: WorkflowPack[] = []
     127          104 :     const unresolved: string[] = []
     128              : 
     129          104 :     await Promise.all(
     130              :       mapAllNodes(rootGraph, async (node) => {
     131          293 :         const pack = await workflowNodeToPack(node)
     132          293 :         if (pack) {
     133          248 :           resolvedPacks.push(pack)
     134              :         }
     135              :       })
     136              :     )
     137              : 
     138          104 :     return { resolvedPacks, unresolved }
     139              :   }
     140              : 
     141          104 :   const packsToUniqueIds = (packs: WorkflowPack[]) =>
     142              :     packs.reduce((acc, pack) => {
     143          248 :       if (pack.id) acc.add(pack.id)
     144          248 :       return acc
     145              :     }, new Set<string>())
     146              : 
     147          104 :   const workflowPacksIds = computed(() =>
     148              :     Array.from(packsToUniqueIds(workflowPacks.value))
     149              :   )
     150              : 
     151          104 :   const { startFetch, cleanup, error, isLoading, nodePacks, isReady } =
     152              :     useNodePacks(workflowPacksIds)
     153              : 
     154          104 :   const isIdInWorkflow = (packId: string) =>
     155              :     workflowPacksIds.value.includes(packId)
     156              : 
     157          104 :   const filterWorkflowPack = (packs: components['schemas']['Node'][]) =>
     158              :     packs.filter((pack) => !!pack.id && isIdInWorkflow(pack.id))
     159              : 
     160          104 :   const startFetchWorkflowPacks = async () => {
     161          104 :     const generation = ++fetchGeneration
     162              : 
     163          104 :     const parsed = await getWorkflowPacks()
     164          104 :     if (generation !== fetchGeneration) return
     165              : 
     166          104 :     fetchDeferredUntilConfigured = !parsed
     167          104 :     if (!parsed) return
     168              : 
     169          104 :     workflowPacks.value = parsed.resolvedPacks
     170          104 :     unresolvedNodeNames.value = [...new Set(parsed.unresolved)]
     171          104 :     await startFetch()
     172              :   }
     173              : 
     174              :   // Retry on `configured`, not on `isGraphReady`: `setup()` installs an empty
     175              :   // graph before the workflow is configured into it.
     176          104 :   useEventListener(
     177              :     () => (app.isGraphReady ? app.rootGraph.events : undefined),
     178              :     'configured',
     179              :     () => {
     180            1 :       if (!fetchDeferredUntilConfigured) return
     181            0 :       startFetchWorkflowPacks().catch((err: unknown) => {
     182            0 :         error.value = err instanceof Error ? err : new Error(String(err))
     183              :       })
     184              :     }
     185              :   )
     186              : 
     187          104 :   onUnmounted(() => {
     188           41 :     cleanup()
     189              :   })
     190              : 
     191          104 :   return {
     192              :     error,
     193              :     isLoading,
     194              :     isReady,
     195              :     workflowPacks: nodePacks,
     196              :     unresolvedNodeNames,
     197              :     startFetchWorkflowPacks,
     198              :     filterWorkflowPack
     199              :   }
     200              : }
     201              : 
     202         2355 : export const useWorkflowPacks = createSharedComposable(_useWorkflowPacks)
        

Generated by: LCOV version 2.0-1