Line data Source code
1 : import type { Ref } from 'vue'
2 : import { computed, watch } from 'vue'
3 :
4 : import type { LGraph, LGraphNode } from '@/lib/litegraph/src/litegraph'
5 : import type { useMissingModelStore } from '@/platform/workflowErrors/missingModel/missingModelStore'
6 : import type { useMissingMediaStore } from '@/platform/workflowErrors/missingMedia/missingMediaStore'
7 : import type { useMissingNodesErrorStore } from '@/platform/workflowErrors/nodeReplacement/missingNodesErrorStore'
8 : import { useSettingStore } from '@/platform/settings/settingStore'
9 : import { useApp } from '@/scripts/appInstance'
10 : import type { NodeError } from '@comfyorg/api-client/types'
11 : import { getParentExecutionIds } from '@comfyorg/graph-model/nodeIdentification'
12 : import { hasErrorForSlot } from '@/utils/executionErrorUtil'
13 : import { forEachNode, getNodeByExecutionId } from '@/utils/graphTraversalUtil'
14 :
15 41336 : function setNodeHasErrors(node: LGraphNode, hasErrors: boolean): void {
16 41336 : if (node.has_errors === hasErrors) return
17 20272 : const oldValue = node.has_errors
18 20272 : node.has_errors = hasErrors
19 41336 : node.graph?.trigger('node:property:changed', {
20 : type: 'node:property:changed',
21 : nodeId: node.id,
22 : property: 'has_errors',
23 : oldValue,
24 : newValue: hasErrors
25 : })
26 : }
27 :
28 : /**
29 : * Single-pass reconciliation of node error flags.
30 : * Collects the set of nodes that should have errors, then walks all nodes
31 : * once, setting each flag exactly once. This avoids the redundant
32 : * true→false→true transition (and duplicate events) that a clear-then-apply
33 : * approach would cause.
34 : */
35 7252 : function reconcileNodeErrorFlags(
36 : rootGraph: LGraph,
37 : nodeErrors: Record<string, NodeError> | null,
38 : missingModelExecIds: Set<string>,
39 : missingMediaExecIds: Set<string> = new Set(),
40 : missingNodeExecIds: Set<string> = new Set()
41 : ): void {
42 : // Collect nodes and slot info that should be flagged
43 : // Includes both error-owning nodes and their ancestor containers
44 7252 : const flaggedNodes = new Set<LGraphNode>()
45 7252 : const errorsByNode = new Map<LGraphNode, NodeError['errors']>()
46 :
47 7252 : if (nodeErrors) {
48 32 : for (const [executionId, nodeError] of Object.entries(nodeErrors)) {
49 32 : const node = getNodeByExecutionId(rootGraph, executionId)
50 32 : if (!node) continue
51 :
52 32 : flaggedNodes.add(node)
53 32 : errorsByNode.set(node, [
54 : ...(errorsByNode.get(node) ?? []),
55 : ...nodeError.errors
56 : ])
57 :
58 32 : for (const parentId of getParentExecutionIds(executionId)) {
59 1 : const parentNode = getNodeByExecutionId(rootGraph, parentId)
60 1 : if (parentNode) flaggedNodes.add(parentNode)
61 : }
62 : }
63 : }
64 :
65 7252 : for (const execId of missingModelExecIds) {
66 373 : const node = getNodeByExecutionId(rootGraph, execId)
67 373 : if (node) flaggedNodes.add(node)
68 : }
69 :
70 7252 : for (const execId of missingMediaExecIds) {
71 24 : const node = getNodeByExecutionId(rootGraph, execId)
72 24 : if (node) flaggedNodes.add(node)
73 : }
74 7252 : for (const execId of missingNodeExecIds) {
75 110 : const node = getNodeByExecutionId(rootGraph, execId)
76 110 : if (node) flaggedNodes.add(node)
77 : }
78 :
79 7252 : forEachNode(rootGraph, (node) => {
80 41336 : setNodeHasErrors(node, flaggedNodes.has(node))
81 :
82 41336 : const ownErrors = errorsByNode.get(node)
83 41336 : for (const slot of node.inputs) {
84 129042 : slot.hasErrors =
85 : !!slot.name && !!ownErrors && hasErrorForSlot(ownErrors, slot.name)
86 : }
87 : })
88 : }
89 :
90 2355 : export function useNodeErrorFlagSync(
91 : nodeErrors: Ref<Record<string, NodeError> | null>,
92 : missingModelStore: ReturnType<typeof useMissingModelStore>,
93 : missingMediaStore: ReturnType<typeof useMissingMediaStore>,
94 : missingNodesStore: ReturnType<typeof useMissingNodesErrorStore>
95 : ): () => void {
96 2355 : const settingStore = useSettingStore()
97 2355 : const showErrorsTab = computed(() =>
98 : settingStore.get('Comfy.RightSidePanel.ShowErrorsTab')
99 : )
100 :
101 : const stop = watch(
102 : [
103 : nodeErrors,
104 : () => missingModelStore.missingModelNodeIds,
105 : () => missingMediaStore.missingMediaNodeIds,
106 : () => missingNodesStore.missingAncestorExecutionIds,
107 : showErrorsTab
108 : ],
109 : () => {
110 9607 : const rootGraph = useApp().rootGraphOrUndefined
111 9607 : if (!rootGraph) return
112 : // Legacy (LGraphNode) only: suppress missing-resource error flags
113 : // when the Errors tab is hidden, since legacy nodes lack the per-widget
114 : // red highlight that Vue nodes use to indicate *why* a node has errors.
115 : // Vue nodes compute hasAnyError independently and are unaffected.
116 9607 : reconcileNodeErrorFlags(
117 : rootGraph,
118 : nodeErrors.value,
119 : showErrorsTab.value
120 : ? missingModelStore.missingModelAncestorExecutionIds
121 : : new Set(),
122 : showErrorsTab.value
123 : ? missingMediaStore.missingMediaAncestorExecutionIds
124 : : new Set(),
125 : // Placeholders for missing nodes are flagged at creation regardless
126 : // of the Errors tab, so keep that and let only the nodes warning
127 : // setting clear them.
128 : missingNodesStore.missingAncestorExecutionIds
129 : )
130 : },
131 : { flush: 'post' }
132 : )
133 2355 : return stop
134 : }
|