Line data Source code
1 : import { st } from '@/i18n'
2 : import type { LGraph } from '@/lib/litegraph/src/litegraph'
3 : import {
4 : isMissingMediaCandidateActive,
5 : isMissingMediaCandidateScopeActive,
6 : scanAllMediaCandidates,
7 : verifyMediaCandidates
8 : } from '@/platform/workflowErrors/missingMedia/missingMediaScan'
9 : import { useMissingMediaStore } from '@/platform/workflowErrors/missingMedia/missingMediaStore'
10 : import type { MissingMediaCandidate } from '@/platform/workflowErrors/missingMedia/types'
11 : import { useToast } from '@/components/ui/toast/toastStore'
12 : import { updatePendingWarnings } from '@/platform/workflow/core/utils/pendingWarnings'
13 : import type { ComfyWorkflow } from '@/platform/workflow/management/stores/comfyWorkflow'
14 : import { useExecutionErrorStore } from '@/stores/executionErrorStore'
15 : import { useWorkspaceStore } from '@/stores/workspaceStore'
16 :
17 : interface RunMissingMediaPipelineOptions {
18 : rootGraph: LGraph
19 : silent?: boolean
20 : onVerified?: (candidates: MissingMediaCandidate[]) => void
21 : }
22 :
23 3785 : function cacheMediaCandidates(
24 : wf: Pick<ComfyWorkflow, 'pendingWarnings'> | null | undefined,
25 : confirmed: MissingMediaCandidate[]
26 : ) {
27 3785 : if (!wf) return
28 3785 : updatePendingWarnings(wf, {
29 : missingMediaCandidates: confirmed
30 : })
31 : }
32 :
33 3785 : export async function runMissingMediaPipeline({
34 : rootGraph,
35 : silent = false,
36 : onVerified
37 : }: RunMissingMediaPipelineOptions): Promise<void> {
38 3785 : const missingMediaStore = useMissingMediaStore()
39 3785 : const activeWf = useWorkspaceStore().workflow.activeWorkflow
40 : const allCandidates = scanAllMediaCandidates(rootGraph)
41 : // Drop candidates whose enclosing subgraph is muted/bypassed.
42 3785 : const candidates = allCandidates.filter((candidate) =>
43 : isMissingMediaCandidateScopeActive(rootGraph, candidate)
44 : )
45 :
46 3785 : if (!candidates.length) {
47 3639 : cacheMediaCandidates(activeWf, [])
48 3639 : onVerified?.([])
49 3639 : return
50 : }
51 :
52 : const pending = candidates.some((c) => c.isMissing === undefined)
53 3785 : if (pending) {
54 6 : const controller = missingMediaStore.createVerificationAbortController()
55 6 : void verifyMediaCandidates(candidates, { signal: controller.signal })
56 : .then(() => {
57 6 : if (controller.signal.aborted) return
58 : // Re-check ancestor after async verification (see model pipeline).
59 6 : const confirmed = candidates.filter((candidate) =>
60 : isMissingMediaCandidateActive(rootGraph, candidate)
61 : )
62 6 : if (confirmed.length) {
63 0 : useExecutionErrorStore().surfaceMissingMedia(confirmed, { silent })
64 : }
65 6 : cacheMediaCandidates(activeWf, confirmed)
66 6 : onVerified?.(
67 : candidates.filter((candidate) =>
68 : isMissingMediaCandidateScopeActive(rootGraph, candidate)
69 : )
70 : )
71 : })
72 : .catch((err) => {
73 0 : console.warn('[Missing Media Pipeline] Asset verification failed:', err)
74 0 : useToast().warning(
75 : st(
76 : 'toastMessages.missingMediaVerificationFailed',
77 : 'Failed to verify missing media. Some inputs may not be shown in the Issues tab.'
78 : ),
79 : { duration: 5000 }
80 : )
81 : })
82 : } else {
83 140 : const confirmed = candidates.filter((c) => c.isMissing === true)
84 140 : useExecutionErrorStore().surfaceMissingMedia(confirmed, { silent })
85 140 : cacheMediaCandidates(activeWf, confirmed)
86 140 : onVerified?.(candidates)
87 : }
88 : }
|