Line data Source code
1 : import { defineStore } from 'pinia'
2 : import { computed, ref } from 'vue'
3 :
4 : import { t } from '@/i18n'
5 : // oxlint-disable-next-line comfy/no-restricted-paths
6 : import { useCanvasStore } from '@/renderer/core/canvas/canvasStore'
7 : import { useApp } from '@/scripts/appInstance'
8 : import { isMissingWarningVisible } from '@/platform/settings/missingWarningVisibility'
9 : import { useToast } from '@/components/ui/toast/toastStore'
10 : import { useWorkflowStore } from '@/platform/workflow/management/stores/workflowStore'
11 : import type { MissingModelCandidate } from '@/platform/workflowErrors/missingModel/types'
12 : import type { LGraphNode } from '@/lib/litegraph/src/litegraph'
13 : import { getAncestorExecutionIds } from '@comfyorg/graph-model/nodeIdentification'
14 : import type {
15 : NodeExecutionId,
16 : NodeLocatorId
17 : } from '@comfyorg/graph-model/nodeIdentification'
18 : import { getActiveGraphNodeIds } from '@/utils/graphTraversalUtil'
19 :
20 : /**
21 : * Missing model error state and interaction state.
22 : * Separated from executionErrorStore to keep domain boundaries clean.
23 : * The executionErrorStore composes from this store for aggregate error flags.
24 : */
25 2356 : export const useMissingModelStore = defineStore('missingModel', () => {
26 2355 : const canvasStore = useCanvasStore()
27 2355 : const workflowStore = useWorkflowStore()
28 :
29 2355 : const missingModelCandidates = ref<MissingModelCandidate[] | null>(null)
30 2355 : const isRefreshingMissingModels = ref(false)
31 :
32 : /** Candidates to display; `null` while the missing models warning is off. */
33 2355 : const visibleMissingModelCandidates = computed(() =>
34 : isMissingWarningVisible('models') ? missingModelCandidates.value : null
35 : )
36 :
37 2355 : const hasMissingModels = computed(
38 : () => !!visibleMissingModelCandidates.value?.length
39 : )
40 :
41 2355 : const missingModelCount = computed(
42 : () => visibleMissingModelCandidates.value?.length ?? 0
43 : )
44 :
45 2355 : const missingModelNodeIds = computed<Set<string>>(() => {
46 9440 : const ids = new Set<string>()
47 9440 : if (!visibleMissingModelCandidates.value) return ids
48 5754 : for (const m of visibleMissingModelCandidates.value) {
49 : // Promoted-widget candidates are scoped to the subgraph host node
50 : // (`nodeId`) but originate at an interior node (`sourceExecutionId`);
51 : // both execution ids carry the missing model.
52 5896 : if (m.nodeId != null) ids.add(String(m.nodeId))
53 5896 : if (m.sourceExecutionId != null) ids.add(String(m.sourceExecutionId))
54 : }
55 5680 : return ids
56 : })
57 :
58 2355 : const missingModelWidgetKeys = computed<Set<string>>(() => {
59 3331 : const keys = new Set<string>()
60 3331 : if (!visibleMissingModelCandidates.value) return keys
61 1966 : for (const m of visibleMissingModelCandidates.value) {
62 1979 : keys.add(`${String(m.nodeId)}::${m.widgetName}`)
63 : }
64 1941 : return keys
65 : })
66 :
67 : /**
68 : * Set of all execution ID prefixes derived from missing model node IDs,
69 : * including the missing model nodes themselves.
70 : *
71 : * Example: missing model on node "65:70:63" → Set { "65", "65:70", "65:70:63" }
72 : */
73 2355 : const missingModelAncestorExecutionIds = computed<Set<NodeExecutionId>>(
74 : () => {
75 3661 : const ids = new Set<NodeExecutionId>()
76 3661 : for (const nodeId of missingModelNodeIds.value) {
77 2207 : for (const id of getAncestorExecutionIds(nodeId)) {
78 2295 : ids.add(id)
79 : }
80 : }
81 3661 : return ids
82 : }
83 : )
84 :
85 2355 : const activeMissingModelGraphIds = computed<Set<string>>(() => {
86 3492 : const rootGraph = useApp().rootGraphOrUndefined
87 3492 : if (!rootGraph) return new Set()
88 3492 : return getActiveGraphNodeIds(
89 : rootGraph,
90 : canvasStore.currentGraph ?? rootGraph,
91 : missingModelAncestorExecutionIds.value
92 : )
93 : })
94 :
95 : // Persists across component re-mounts so that download progress
96 : // survives tab switches within the properties panel.
97 2355 : const modelExpandState = ref<Record<string, boolean>>({})
98 2355 : const selectedLibraryModel = ref<Record<string, string>>({})
99 2355 : const importTaskIds = ref<Record<string, string>>({})
100 2355 : const folderPaths = ref<Record<string, string[]>>({})
101 2355 : const fileSizes = ref<Record<string, number>>({})
102 2355 : const gatedRepoUrls = ref<Record<string, string>>({})
103 :
104 2355 : let _verificationAbortController: AbortController | null = null
105 :
106 3892 : function createVerificationAbortController(): AbortController {
107 3892 : _verificationAbortController?.abort()
108 3892 : _verificationAbortController = new AbortController()
109 3892 : return _verificationAbortController
110 : }
111 :
112 6634 : function setMissingModels(models: MissingModelCandidate[]) {
113 6634 : missingModelCandidates.value = models.length ? models : null
114 : }
115 :
116 0 : function removeMissingModelByNameOnNodes(
117 : modelName: string,
118 : nodeIds: Set<string>
119 : ) {
120 0 : if (!missingModelCandidates.value) return
121 0 : missingModelCandidates.value = missingModelCandidates.value.filter(
122 : (m) =>
123 : m.name !== modelName ||
124 : m.nodeId == null ||
125 : !nodeIds.has(String(m.nodeId))
126 : )
127 0 : if (!missingModelCandidates.value.length)
128 0 : missingModelCandidates.value = null
129 : }
130 :
131 447 : function removeMissingModelByWidget(nodeId: string, widgetName: string) {
132 447 : if (!missingModelCandidates.value) return
133 105 : missingModelCandidates.value = missingModelCandidates.value.filter(
134 : (m) => !(String(m.nodeId) === nodeId && m.widgetName === widgetName)
135 : )
136 105 : if (!missingModelCandidates.value.length)
137 4 : missingModelCandidates.value = null
138 : }
139 :
140 152 : function clearInteractionStateForName(name: string) {
141 152 : delete modelExpandState.value[name]
142 152 : delete selectedLibraryModel.value[name]
143 152 : delete importTaskIds.value[name]
144 : }
145 :
146 10156 : function removeMissingModelsByNodeId(nodeId: string) {
147 10156 : if (!missingModelCandidates.value) return
148 1008 : const removedNames = new Set(
149 : missingModelCandidates.value
150 : .filter((m) => String(m.nodeId) === nodeId)
151 : .map((m) => m.name)
152 : )
153 1008 : missingModelCandidates.value = missingModelCandidates.value.filter(
154 : (m) => String(m.nodeId) !== nodeId
155 : )
156 4753 : for (const name of removedNames) {
157 144 : if (!missingModelCandidates.value.some((m) => m.name === name)) {
158 143 : clearInteractionStateForName(name)
159 : }
160 : }
161 1623 : if (!missingModelCandidates.value.length)
162 143 : missingModelCandidates.value = null
163 : }
164 :
165 : /**
166 : * Remove all candidates whose nodeId starts with `prefix`.
167 : *
168 : * Intended for clearing all interior errors when a subgraph container is
169 : * removed. Callers are expected to pass `${execId}:` (with trailing
170 : * colon) so that sibling IDs sharing a numeric prefix (e.g. `"705"` vs
171 : * `"70"`) are not matched.
172 : */
173 88 : function removeMissingModelsByPrefix(prefix: string) {
174 88 : if (!missingModelCandidates.value) return
175 8 : const removedNames = new Set<string>()
176 8 : const remaining: MissingModelCandidate[] = []
177 8 : for (const m of missingModelCandidates.value) {
178 : // Preserve workflow-level candidates with no nodeId; they are not
179 : // tied to any subgraph scope and should never be matched by prefix.
180 8 : if (m.nodeId == null) {
181 0 : remaining.push(m)
182 0 : continue
183 : }
184 8 : if (String(m.nodeId).startsWith(prefix)) {
185 6 : removedNames.add(m.name)
186 : } else {
187 2 : remaining.push(m)
188 : }
189 : }
190 12 : if (removedNames.size === 0) return
191 88 : missingModelCandidates.value = remaining.length ? remaining : null
192 88 : for (const name of removedNames) {
193 6 : if (!remaining.some((m) => m.name === name)) {
194 6 : clearInteractionStateForName(name)
195 : }
196 : }
197 : }
198 :
199 10156 : function removeMissingModelsBySourceScope(executionId: string) {
200 10156 : if (!missingModelCandidates.value) return
201 865 : const prefix = `${executionId}:`
202 865 : const removedNames = new Set<string>()
203 865 : const remaining: MissingModelCandidate[] = []
204 899 : for (const candidate of missingModelCandidates.value) {
205 871 : const sourceExecutionId =
206 : candidate.sourceExecutionId == null
207 : ? undefined
208 : : String(candidate.sourceExecutionId)
209 871 : if (
210 : sourceExecutionId === executionId ||
211 : sourceExecutionId?.startsWith(prefix)
212 : ) {
213 5 : removedNames.add(candidate.name)
214 : } else {
215 866 : remaining.push(candidate)
216 : }
217 : }
218 892 : if (removedNames.size === 0) return
219 10156 : missingModelCandidates.value = remaining.length ? remaining : null
220 10156 : for (const name of removedNames) {
221 5 : if (!remaining.some((candidate) => candidate.name === name)) {
222 3 : clearInteractionStateForName(name)
223 : }
224 : }
225 : }
226 :
227 40 : function addMissingModels(models: MissingModelCandidate[]) {
228 40 : if (!models.length) return
229 40 : const existing = missingModelCandidates.value ?? []
230 40 : const existingKeys = new Set(
231 : existing.map((m) => `${String(m.nodeId)}::${m.widgetName}::${m.name}`)
232 : )
233 40 : const newModels = models.filter(
234 : (m) =>
235 : !existingKeys.has(`${String(m.nodeId)}::${m.widgetName}::${m.name}`)
236 : )
237 40 : if (!newModels.length) return
238 39 : missingModelCandidates.value = [...existing, ...newModels]
239 : }
240 :
241 21626 : function hasMissingModelOnNode(nodeLocatorId: NodeLocatorId): boolean {
242 21626 : const executionId =
243 : workflowStore.nodeLocatorIdToNodeExecutionId(nodeLocatorId)
244 21626 : return executionId ? missingModelNodeIds.value.has(executionId) : false
245 : }
246 :
247 47059 : function isWidgetMissingModel(nodeId: string, widgetName: string): boolean {
248 47059 : return missingModelWidgetKeys.value.has(`${nodeId}::${widgetName}`)
249 : }
250 :
251 19572 : function isContainerWithMissingModel(node: LGraphNode): boolean {
252 19572 : return activeMissingModelGraphIds.value.has(String(node.id))
253 : }
254 :
255 2681 : function setFolderPaths(paths: Record<string, string[]>) {
256 2681 : folderPaths.value = paths
257 : }
258 :
259 36 : function setFileSize(url: string, size: number) {
260 36 : fileSizes.value[url] = size
261 : }
262 :
263 4 : function setGatedRepoUrl(url: string, repoUrl: string) {
264 4 : gatedRepoUrls.value[url] = repoUrl
265 : }
266 :
267 3791 : function clearMissingModels() {
268 3791 : _verificationAbortController?.abort()
269 3791 : _verificationAbortController = null
270 3791 : missingModelCandidates.value = null
271 3791 : modelExpandState.value = {}
272 3791 : selectedLibraryModel.value = {}
273 3791 : importTaskIds.value = {}
274 3791 : folderPaths.value = {}
275 3791 : fileSizes.value = {}
276 3791 : gatedRepoUrls.value = {}
277 : }
278 :
279 0 : function isAbortError(error: unknown) {
280 0 : return error instanceof Error && error.name === 'AbortError'
281 : }
282 :
283 11 : async function refreshMissingModels(options: { reloadDefs?: boolean } = {}) {
284 11 : if (isRefreshingMissingModels.value) return
285 :
286 11 : isRefreshingMissingModels.value = true
287 11 : try {
288 11 : await useApp().refreshMissingModels({
289 : silent: true,
290 : reloadDefs: options.reloadDefs
291 : })
292 : } catch (error) {
293 0 : if (isAbortError(error)) return
294 :
295 0 : console.error('Failed to refresh missing models:', error)
296 0 : useToast().error(t('g.error'), {
297 : description: t('rightSidePanel.missingModels.refreshFailed')
298 : })
299 : } finally {
300 11 : isRefreshingMissingModels.value = false
301 : }
302 : }
303 :
304 2355 : return {
305 : missingModelCandidates,
306 : visibleMissingModelCandidates,
307 : isRefreshingMissingModels,
308 : hasMissingModels,
309 : missingModelCount,
310 : missingModelNodeIds,
311 : activeMissingModelGraphIds,
312 : missingModelAncestorExecutionIds,
313 :
314 : setMissingModels,
315 : addMissingModels,
316 : removeMissingModelByNameOnNodes,
317 : removeMissingModelByWidget,
318 : removeMissingModelsByNodeId,
319 : removeMissingModelsByPrefix,
320 : removeMissingModelsBySourceScope,
321 : clearMissingModels,
322 : refreshMissingModels,
323 : createVerificationAbortController,
324 :
325 : hasMissingModelOnNode,
326 : isWidgetMissingModel,
327 : isContainerWithMissingModel,
328 :
329 : modelExpandState,
330 : selectedLibraryModel,
331 : importTaskIds,
332 : folderPaths,
333 : fileSizes,
334 : gatedRepoUrls,
335 :
336 : setFolderPaths,
337 : setFileSize,
338 : setGatedRepoUrl
339 : }
340 : })
|