Line data Source code
1 : import { defineStore } from 'pinia'
2 : import { computed, ref } from 'vue'
3 :
4 : import { st } from '@/i18n'
5 : import type { LGraphNode } from '@/lib/litegraph/src/litegraph'
6 : import { isCloud } from '@/platform/distribution/types'
7 : import { isMissingWarningVisible } from '@/platform/settings/missingWarningVisibility'
8 : import { useSettingStore } from '@/platform/settings/settingStore'
9 : import {
10 : dedupeMissingNodeTypes,
11 : removePendingMissingNodeTypesByExecutionIdPrefix,
12 : removePendingMissingNodeTypesByNodeId,
13 : removePendingMissingNodeTypesByType
14 : } from '@/platform/workflow/core/utils/pendingWarnings'
15 : import { useApp } from '@/scripts/appInstance'
16 : import type { MissingNodeType } from '@/platform/workflowErrors/nodeReplacement/types'
17 : import { getAncestorExecutionIds } from '@comfyorg/graph-model/nodeIdentification'
18 : import type { NodeExecutionId } from '@comfyorg/graph-model/nodeIdentification'
19 : import { getExecutionIdByNode } from '@/utils/graphTraversalUtil'
20 :
21 : interface MissingNodesError {
22 : message: string
23 : nodeTypes: MissingNodeType[]
24 : }
25 :
26 2356 : export const useMissingNodesErrorStore = defineStore(
27 : 'missingNodesError',
28 : () => {
29 2355 : const missingNodesError = ref<MissingNodesError | null>(null)
30 :
31 : /** Error to display; `null` while the missing nodes warning is off. */
32 2355 : const visibleMissingNodesError = computed(() =>
33 : isMissingWarningVisible('nodes') ? missingNodesError.value : null
34 : )
35 :
36 7834 : function setMissingNodeTypes(types: MissingNodeType[]) {
37 7834 : if (!types.length) {
38 7772 : missingNodesError.value = null
39 7772 : return
40 : }
41 62 : const uniqueTypes = dedupeMissingNodeTypes(types)
42 62 : missingNodesError.value = {
43 : message: isCloud
44 : ? st(
45 : 'rightSidePanel.missingNodePacks.unsupportedTitle',
46 : 'Unsupported Node Packs'
47 : )
48 : : st('rightSidePanel.missingNodePacks.title', 'Missing Node Packs'),
49 : nodeTypes: uniqueTypes
50 : }
51 : }
52 :
53 : /** Set missing node types. Returns true if the Errors tab and the missing nodes warning are enabled and types were set. */
54 3902 : function surfaceMissingNodes(types: MissingNodeType[]): boolean {
55 3902 : setMissingNodeTypes(types)
56 3902 : return (
57 : hasMissingNodes.value &&
58 : useSettingStore().get('Comfy.RightSidePanel.ShowErrorsTab')
59 : )
60 : }
61 :
62 10156 : function removeMissingNodesByNodeId(nodeId: string) {
63 10156 : if (!missingNodesError.value) return
64 : const remaining = removePendingMissingNodeTypesByNodeId(
65 : missingNodesError.value.nodeTypes,
66 : nodeId
67 : )
68 4 : setMissingNodeTypes(remaining)
69 : }
70 :
71 : /**
72 : * Remove all object-type entries whose nodeId starts with `prefix`.
73 : * String entries (group nodes) have no nodeId and are preserved.
74 : *
75 : * Intended for clearing all interior errors when a subgraph container
76 : * is removed. Callers are expected to pass `${execId}:` (with trailing
77 : * colon) so that sibling IDs sharing a numeric prefix are not matched.
78 : */
79 88 : function removeMissingNodesByPrefix(prefix: string) {
80 88 : if (!missingNodesError.value) return
81 : const remaining = removePendingMissingNodeTypesByExecutionIdPrefix(
82 : missingNodesError.value.nodeTypes,
83 : prefix
84 : )
85 0 : setMissingNodeTypes(remaining)
86 : }
87 :
88 : /** Remove specific node types from the missing nodes list (e.g. after replacement). */
89 13 : function removeMissingNodesByType(typesToRemove: string[]) {
90 13 : if (!missingNodesError.value) return
91 : const remaining = removePendingMissingNodeTypesByType(
92 : missingNodesError.value.nodeTypes,
93 : typesToRemove
94 : )
95 13 : setMissingNodeTypes(remaining)
96 : }
97 :
98 2355 : const hasMissingNodes = computed(() => !!visibleMissingNodesError.value)
99 :
100 2355 : const missingNodeCount = computed(
101 : () => visibleMissingNodesError.value?.nodeTypes.length ?? 0
102 : )
103 :
104 : /**
105 : * Set of all execution ID prefixes derived from missing node execution IDs,
106 : * including the missing nodes themselves.
107 : *
108 : * Example: missing node at "65:70:63" → Set { "65", "65:70", "65:70:63" }
109 : */
110 2355 : const missingAncestorExecutionIds = computed<Set<NodeExecutionId>>(() => {
111 2440 : const ids = new Set<NodeExecutionId>()
112 2440 : const error = visibleMissingNodesError.value
113 2440 : if (!error) return ids
114 :
115 812 : for (const nodeType of error.nodeTypes) {
116 102 : if (typeof nodeType === 'string') continue
117 102 : if (nodeType.nodeId == null) continue
118 102 : for (const id of getAncestorExecutionIds(String(nodeType.nodeId))) {
119 107 : ids.add(id)
120 : }
121 : }
122 :
123 62 : return ids
124 : })
125 :
126 : /** True if the node has a missing node inside it at any nesting depth. */
127 19609 : function isContainerWithMissingNode(node: LGraphNode): boolean {
128 19609 : if (!useApp().isGraphReady) return false
129 19609 : const execId = getExecutionIdByNode(useApp().rootGraph, node)
130 19609 : if (!execId) return false
131 19306 : return missingAncestorExecutionIds.value.has(execId)
132 : }
133 :
134 2355 : return {
135 : missingNodesError,
136 : visibleMissingNodesError,
137 : setMissingNodeTypes,
138 : surfaceMissingNodes,
139 : removeMissingNodesByNodeId,
140 : removeMissingNodesByPrefix,
141 : removeMissingNodesByType,
142 : hasMissingNodes,
143 : missingNodeCount,
144 : missingAncestorExecutionIds,
145 : isContainerWithMissingNode
146 : }
147 : }
148 : )
|