Line data Source code
1 : import { defineStore } from 'pinia'
2 : import { computed, ref } from 'vue'
3 :
4 : // oxlint-disable-next-line comfy/no-restricted-paths
5 : import { useCanvasStore } from '@/renderer/core/canvas/canvasStore'
6 : import { useApp } from '@/scripts/appInstance'
7 : import type { MissingMediaCandidate } from '@/platform/workflowErrors/missingMedia/types'
8 : import { isMissingWarningVisible } from '@/platform/settings/missingWarningVisibility'
9 : import { useWorkflowStore } from '@/platform/workflow/management/stores/workflowStore'
10 : import { useTemplateInputDownloadStore } from '@/platform/workflow/templates/stores/templateInputDownloadStore'
11 : import { getAncestorExecutionIds } from '@comfyorg/graph-model/nodeIdentification'
12 : import type {
13 : NodeExecutionId,
14 : NodeLocatorId
15 : } from '@comfyorg/graph-model/nodeIdentification'
16 : import { getActiveGraphNodeIds } from '@/utils/graphTraversalUtil'
17 : import type { LGraphNode } from '@/lib/litegraph/src/litegraph'
18 :
19 : /**
20 : * Missing media error state.
21 : * Separated from executionErrorStore to keep domain boundaries clean.
22 : * The executionErrorStore composes from this store for aggregate error flags.
23 : */
24 2356 : export const useMissingMediaStore = defineStore('missingMedia', () => {
25 2355 : const canvasStore = useCanvasStore()
26 2355 : const templateInputDownloadStore = useTemplateInputDownloadStore()
27 :
28 2355 : const missingMediaCandidates = ref<MissingMediaCandidate[] | null>(null)
29 :
30 : /** Candidates to display; `null` while the missing media warning is off. */
31 2355 : const visibleMissingMediaCandidates = computed(() =>
32 : isMissingWarningVisible('media') ? missingMediaCandidates.value : null
33 : )
34 :
35 : /** Visible candidates the user can still act on, excluding in-flight template input downloads. */
36 2355 : const actionableMissingMediaCandidates = computed(() =>
37 : (visibleMissingMediaCandidates.value ?? []).filter(
38 : ({ name }) => !templateInputDownloadStore.blockingFilenames.has(name)
39 : )
40 : )
41 :
42 2355 : const hasMissingMedia = computed(
43 : () => actionableMissingMediaCandidates.value.length > 0
44 : )
45 :
46 2355 : const missingMediaCount = computed(
47 : () => actionableMissingMediaCandidates.value.length
48 : )
49 :
50 2355 : const missingMediaNodeIds = computed(
51 : () =>
52 : new Set(
53 : actionableMissingMediaCandidates.value.map((m) => String(m.nodeId))
54 : )
55 : )
56 :
57 : /** `nodeId::widgetName` keys, so per-widget render lookups stay O(1). */
58 2355 : const missingMediaWidgetKeys = computed<Set<string>>(() => {
59 857 : const keys = new Set<string>()
60 857 : for (const candidate of actionableMissingMediaCandidates.value) {
61 28 : keys.add(`${String(candidate.nodeId)}::${candidate.widgetName}`)
62 : }
63 857 : return keys
64 : })
65 :
66 : /**
67 : * Set of all execution ID prefixes derived from missing media node IDs,
68 : * including the missing media nodes themselves.
69 : */
70 2355 : const missingMediaAncestorExecutionIds = computed<Set<NodeExecutionId>>(
71 : () => {
72 926 : const ids = new Set<NodeExecutionId>()
73 926 : for (const nodeId of missingMediaNodeIds.value) {
74 37 : for (const id of getAncestorExecutionIds(nodeId)) {
75 37 : ids.add(id)
76 : }
77 : }
78 926 : return ids
79 : }
80 : )
81 :
82 2355 : const activeMissingMediaGraphIds = computed<Set<string>>(() => {
83 973 : const rootGraph = useApp().rootGraphOrUndefined
84 973 : if (!rootGraph) return new Set()
85 973 : return getActiveGraphNodeIds(
86 : rootGraph,
87 : canvasStore.currentGraph ?? rootGraph,
88 : missingMediaAncestorExecutionIds.value
89 : )
90 : })
91 :
92 2355 : let _verificationAbortController: AbortController | null = null
93 :
94 102 : function createVerificationAbortController(): AbortController {
95 102 : _verificationAbortController?.abort()
96 102 : _verificationAbortController = new AbortController()
97 102 : return _verificationAbortController
98 : }
99 :
100 260 : function setMissingMedia(media: MissingMediaCandidate[]) {
101 260 : missingMediaCandidates.value = media.length ? media : null
102 : }
103 :
104 19637 : function hasMissingMediaOnNode(nodeLocatorId: NodeLocatorId): boolean {
105 19637 : const executionId =
106 : useWorkflowStore().nodeLocatorIdToNodeExecutionId(nodeLocatorId)
107 19637 : return executionId ? missingMediaNodeIds.value.has(executionId) : false
108 : }
109 :
110 19558 : function isContainerWithMissingMedia(node: LGraphNode): boolean {
111 19558 : return activeMissingMediaGraphIds.value.has(String(node.id))
112 : }
113 :
114 44600 : function isWidgetMissingMedia(
115 : nodeId: NodeExecutionId,
116 : widgetName: string
117 : ): boolean {
118 44600 : return missingMediaWidgetKeys.value.has(`${String(nodeId)}::${widgetName}`)
119 : }
120 :
121 476 : function removeMissingMediaByWidget(nodeId: string, widgetName: string) {
122 476 : if (!missingMediaCandidates.value) return
123 7 : missingMediaCandidates.value = missingMediaCandidates.value.filter(
124 : (m) => !(String(m.nodeId) === nodeId && m.widgetName === widgetName)
125 : )
126 7 : if (!missingMediaCandidates.value.length)
127 5 : missingMediaCandidates.value = null
128 : }
129 :
130 10156 : function removeMissingMediaByNodeId(nodeId: string) {
131 10156 : if (!missingMediaCandidates.value) return
132 2 : missingMediaCandidates.value = missingMediaCandidates.value.filter(
133 : (m) => String(m.nodeId) !== nodeId
134 : )
135 2 : if (!missingMediaCandidates.value.length)
136 2 : missingMediaCandidates.value = null
137 : }
138 :
139 : /**
140 : * Remove all candidates whose nodeId starts with `prefix`.
141 : *
142 : * Intended for clearing all interior errors when a subgraph container is
143 : * removed. Callers are expected to pass `${execId}:` (with trailing
144 : * colon) so that sibling IDs sharing a numeric prefix (e.g. `"705"` vs
145 : * `"70"`) are not matched.
146 : */
147 88 : function removeMissingMediaByPrefix(prefix: string) {
148 88 : if (!missingMediaCandidates.value) return
149 0 : const remaining: MissingMediaCandidate[] = []
150 0 : for (const m of missingMediaCandidates.value) {
151 : // Preserve candidates without a nodeId; they cannot belong to any
152 : // subgraph scope. The type marks nodeId as required, but defensive
153 : // handling matches the rest of the missing-media code.
154 0 : const nodeId: unknown = m.nodeId
155 0 : if (nodeId == null) {
156 0 : remaining.push(m)
157 0 : continue
158 : }
159 0 : if (!String(nodeId).startsWith(prefix)) {
160 0 : remaining.push(m)
161 : }
162 : }
163 0 : if (remaining.length === missingMediaCandidates.value.length) return
164 88 : missingMediaCandidates.value = remaining.length ? remaining : null
165 : }
166 :
167 11 : function addMissingMedia(media: MissingMediaCandidate[]) {
168 11 : if (!media.length) return
169 11 : const existing = missingMediaCandidates.value ?? []
170 11 : const existingKeys = new Set(
171 : existing.map((m) => `${String(m.nodeId)}::${m.widgetName}::${m.name}`)
172 : )
173 11 : const newMedia = media.filter(
174 : (m) =>
175 : !existingKeys.has(`${String(m.nodeId)}::${m.widgetName}::${m.name}`)
176 : )
177 11 : if (!newMedia.length) return
178 11 : missingMediaCandidates.value = [...existing, ...newMedia]
179 : }
180 :
181 3791 : function clearMissingMedia() {
182 3791 : _verificationAbortController?.abort()
183 3791 : _verificationAbortController = null
184 3791 : missingMediaCandidates.value = null
185 : }
186 :
187 2355 : return {
188 : missingMediaCandidates,
189 : visibleMissingMediaCandidates,
190 : hasMissingMedia,
191 : missingMediaCount,
192 : missingMediaNodeIds,
193 : missingMediaAncestorExecutionIds,
194 : activeMissingMediaGraphIds,
195 :
196 : hasMissingMediaOnNode,
197 : setMissingMedia,
198 : addMissingMedia,
199 : removeMissingMediaByWidget,
200 : removeMissingMediaByNodeId,
201 : removeMissingMediaByPrefix,
202 : clearMissingMedia,
203 : createVerificationAbortController,
204 :
205 : isContainerWithMissingMedia,
206 : isWidgetMissingMedia
207 : }
208 : })
|