Line data Source code
1 : import { groupBy } from 'es-toolkit/compat'
2 : import { createSharedComposable, useEventListener } from '@vueuse/core'
3 : import { computed, ref, watch } from 'vue'
4 :
5 : import type { NodeProperty } from '@/lib/litegraph/src/LGraphNode'
6 : import type { LGraphNode } from '@/lib/litegraph/src/litegraph'
7 : import { useWorkflowStore } from '@/platform/workflow/management/stores/workflowStore'
8 : import { app } from '@/scripts/app'
9 : import { useNodeDefStore } from '@/stores/nodeDefStore'
10 : import type { components } from '@/types/comfyRegistryTypes'
11 : import { collectAllNodes } from '@/utils/graphTraversalUtil'
12 : import { useWorkflowPacks } from '@/platform/customNodes/manager/composables/nodePack/useWorkflowPacks'
13 : import { useComfyManagerStore } from '@/platform/customNodes/manager/stores/comfyManagerStore'
14 :
15 : /**
16 : * Composable to find missing NodePacks from workflow
17 : * Automatically fetches workflow pack data when initialized
18 : * This is a shared singleton composable - all components use the same instance
19 : */
20 2355 : export const useMissingNodes = createSharedComposable(() => {
21 104 : const nodeDefStore = useNodeDefStore()
22 104 : const comfyManagerStore = useComfyManagerStore()
23 104 : const workflowStore = useWorkflowStore()
24 104 : const {
25 : workflowPacks,
26 : unresolvedNodeNames,
27 : isLoading,
28 : error,
29 : startFetchWorkflowPacks
30 : } = useWorkflowPacks()
31 :
32 104 : const filterMissingPacks = (packs: components['schemas']['Node'][]) =>
33 : packs.filter((pack) => !comfyManagerStore.isPackInstalled(pack.id))
34 :
35 : // Filter only uninstalled packs from workflow packs
36 104 : const missingNodePacks = computed(() => {
37 28 : if (!workflowPacks.value.length) return []
38 0 : return filterMissingPacks(workflowPacks.value)
39 : })
40 :
41 : /**
42 : * Check if a pack is the ComfyUI builtin node pack (nodes that come pre-installed)
43 : * @param packId - The id of the pack to check
44 : * @returns True if the pack is the comfy-core pack, false otherwise
45 : */
46 104 : const isCorePack = (packId: NodeProperty) => {
47 2 : return packId === 'comfy-core'
48 : }
49 :
50 : /**
51 : * Check if a node is a missing core node
52 : * A missing core node is a node that is in the workflow and originates from
53 : * the comfy-core pack (pre-installed) but not registered in the node def
54 : * store (the node def was not found on the server)
55 : * @param node - The node to check
56 : * @returns True if the node is a missing core node, false otherwise
57 : */
58 104 : const isMissingCoreNode = (node: LGraphNode) => {
59 20 : const packId = node.properties.cnr_id
60 20 : if (packId === undefined || !isCorePack(packId)) return false
61 0 : const nodeName = node.type
62 : const isRegisteredNodeDef = !!nodeDefStore.nodeDefsByName[nodeName]
63 : return !isRegisteredNodeDef
64 : }
65 :
66 104 : const graphConfiguredCount = ref(0)
67 104 : useEventListener(
68 : () => (app.isGraphReady ? app.rootGraph.events : undefined),
69 : 'configured',
70 : () => {
71 1 : graphConfiguredCount.value++
72 : }
73 : )
74 :
75 104 : const missingCoreNodes = computed<Record<string, LGraphNode[]>>(() => {
76 14 : void graphConfiguredCount.value
77 :
78 14 : const rootGraph = app.rootGraphOrUndefined
79 14 : if (!rootGraph) return {}
80 :
81 14 : const missingNodes = collectAllNodes(rootGraph, isMissingCoreNode)
82 14 : return groupBy(missingNodes, (node) => String(node.properties.ver || ''))
83 : })
84 :
85 : // Check if workflow has any missing nodes
86 104 : const hasMissingNodes = computed(() => {
87 0 : return (
88 : missingNodePacks.value.length > 0 ||
89 : Object.keys(missingCoreNodes.value).length > 0 ||
90 : unresolvedNodeNames.value.length > 0
91 : )
92 : })
93 :
94 : // Re-fetch workflow packs when active workflow changes
95 104 : watch(
96 : () => workflowStore.activeWorkflow,
97 : async () => {
98 104 : await startFetchWorkflowPacks()
99 : },
100 : { immediate: true }
101 : )
102 :
103 104 : return {
104 : missingNodePacks,
105 : missingCoreNodes,
106 : unresolvedNodeNames,
107 : hasMissingNodes,
108 : isLoading,
109 : error
110 : }
111 : })
|