Line data Source code
1 : import { groupBy } from 'es-toolkit'
2 :
3 : import { useFeatureFlags } from '@/composables/useFeatureFlags'
4 : import {
5 : buildPromotedWidgetExecutionSources,
6 : hasActivePromotedWidgetConsumer,
7 : resolveActivePromotedWidgetConsumers
8 : } from '@/core/graph/subgraph/resolveConcretePromotedWidget'
9 : import { resolvePromotedWidgetSource } from '@/core/graph/subgraph/resolvePromotedWidgetSource'
10 : import { resolveWidgetValueAsset } from '@/platform/assets/utils/resolveWidgetValueAsset'
11 : import { isComboInputSpec } from '@comfyorg/object-info-parser/schemas/nodeDefSchemaV2'
12 : import type { InputSpec as InputSpecV2 } from '@comfyorg/object-info-parser/schemas/nodeDefSchemaV2'
13 : import { useNodeDefStore } from '@/stores/nodeDefStore'
14 : import type {
15 : MissingMediaCandidate,
16 : MissingMediaViewModel,
17 : MissingMediaGroup,
18 : MediaType
19 : } from './types'
20 : import type { LGraph } from '@/lib/litegraph/src/LGraph'
21 : import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode'
22 : import type { IBaseWidget, IComboWidget } from '@comfyorg/graph-model/widgets'
23 : import {
24 : collectAllNodes,
25 : getExecutionIdByNode,
26 : getNodeByExecutionId,
27 : isExecutionPathActive
28 : } from '@/utils/graphTraversalUtil'
29 : import { LGraphEventMode } from '@comfyorg/graph-model/globalEnums'
30 : import { resolveComboValues } from '@/utils/litegraphUtil'
31 : import { parseAnnotatedPath } from '@/utils/createAnnotatedPath'
32 :
33 53871 : function isComboWidget(widget: IBaseWidget): widget is IComboWidget {
34 53871 : return widget.type === 'combo'
35 : }
36 :
37 : /** The widget a user can actually edit. */
38 536 : function isEditableValueOwner(node: LGraphNode, widget: IBaseWidget): boolean {
39 536 : const input = node.getSlotFromWidget(widget)
40 536 : if (input?.link != null) return false
41 506 : if (!node.isSubgraphNode()) return true
42 536 : return !!input?.widgetId && hasActivePromotedWidgetConsumer(node, input.name)
43 : }
44 :
45 15610 : function mediaTypeFromSpec(
46 : spec: InputSpecV2 | undefined
47 : ): MediaType | undefined {
48 15610 : if (!spec || !isComboInputSpec(spec)) return undefined
49 15608 : if (spec.video_upload) return 'video'
50 15610 : if (spec.image_upload || spec.animated_image_upload) return 'image'
51 15604 : if (spec.audio_upload) return 'audio'
52 : return undefined
53 : }
54 :
55 : /**
56 : * Scan combo widgets on media nodes for file values that may be missing.
57 : *
58 : * With the asset API enabled, `isMissing` is left `undefined` so
59 : * `verifyMediaCandidates` resolves it against the asset listing on every
60 : * backend. Without it, `isMissing` is resolved immediately from widget
61 : * options unless an output annotation needs generated-history verification.
62 : */
63 3794 : export function scanAllMediaCandidates(
64 : rootGraph: LGraph
65 : ): MissingMediaCandidate[] {
66 3794 : const allNodes = collectAllNodes(rootGraph)
67 3794 : const candidates: MissingMediaCandidate[] = []
68 :
69 3794 : for (const node of allNodes) {
70 21640 : if (!node.widgets?.length) continue
71 21640 : if (
72 : node.mode === LGraphEventMode.NEVER ||
73 : node.mode === LGraphEventMode.BYPASS
74 : )
75 7 : continue
76 :
77 18431 : candidates.push(...scanNodeMediaCandidates(rootGraph, node))
78 : }
79 :
80 3794 : return candidates
81 : }
82 :
83 254 : function resolveMediaMissingState(
84 : widget: IComboWidget,
85 : value: string
86 : ): boolean | undefined {
87 254 : if (useFeatureFlags().flags.assetsEnabled) return undefined
88 254 : const options = resolveComboValues(widget)
89 254 : const { filepath, rootFolder } = parseAnnotatedPath(value)
90 254 : if (rootFolder !== 'input') return options.includes(value) ? false : undefined
91 254 : return !options.includes(value) && !options.includes(filepath)
92 : }
93 :
94 : /** Scan media; includeTemp is only for known deletions, not verification. */
95 22009 : export function scanNodeMediaCandidates(
96 : rootGraph: LGraph,
97 : node: LGraphNode,
98 : { includeTemp = false }: { includeTemp?: boolean } = {}
99 : ): MissingMediaCandidate[] {
100 22009 : if (!node.widgets?.length) return []
101 :
102 21574 : const executionId = getExecutionIdByNode(rootGraph, node)
103 22009 : if (!executionId) return []
104 :
105 22009 : if (node.isUploading) return []
106 :
107 21565 : const nodeDefStore = useNodeDefStore()
108 21565 : const candidates: MissingMediaCandidate[] = []
109 22009 : for (const widget of node.widgets) {
110 53871 : if (!isComboWidget(widget)) continue
111 :
112 15610 : const mediaType = mediaTypeFromSpec(
113 : nodeDefStore.getInputSpecForWidget(node, widget.name)
114 : )
115 21518 : if (!mediaType) continue
116 5926 : if (!isEditableValueOwner(node, widget)) continue
117 :
118 268 : const value = widget.value
119 53871 : if (typeof value !== 'string' || !value.trim()) continue
120 53871 : if (!includeTemp && parseAnnotatedPath(value).rootFolder === 'temp')
121 2 : continue
122 :
123 254 : const isMissing = resolveMediaMissingState(widget, value)
124 :
125 : // Label only, and leaf-derived to match missingModelScan: the overlay
126 : // formats nodeType directly and a SubgraphNode's own type is a UUID.
127 : const promotedSource = resolvePromotedWidgetSource(rootGraph, node, widget)
128 : const labelNode = promotedSource?.sourceNode ?? node
129 :
130 53871 : const candidate: MissingMediaCandidate = {
131 : nodeId: executionId,
132 : nodeType: labelNode.type,
133 : widgetName: widget.name,
134 : mediaType,
135 : name: value,
136 : isMissing
137 : }
138 53871 : if (node.isSubgraphNode()) {
139 48 : const consumers = resolveActivePromotedWidgetConsumers(node, widget.name)
140 48 : candidate.promotedSources = buildPromotedWidgetExecutionSources(
141 : executionId,
142 : consumers
143 : )
144 : }
145 254 : candidates.push(candidate)
146 : }
147 :
148 21565 : return candidates
149 : }
150 :
151 190 : export function isMissingMediaCandidateScopeActive(
152 : rootGraph: LGraph | null | undefined,
153 : candidate: MissingMediaCandidate
154 : ): boolean {
155 190 : if (!rootGraph) return false
156 :
157 190 : const executionId = String(candidate.nodeId)
158 190 : if (!isExecutionPathActive(rootGraph, executionId)) return false
159 :
160 190 : const node = getNodeByExecutionId(rootGraph, executionId)
161 190 : if (!node) return false
162 190 : const widget = node.widgets?.find(
163 : (candidateWidget) => candidateWidget.name === candidate.widgetName
164 : )
165 190 : if (!widget) return false
166 :
167 190 : return widget.value === candidate.name && isEditableValueOwner(node, widget)
168 : }
169 :
170 15 : export function isMissingMediaCandidateActive(
171 : rootGraph: LGraph | null | undefined,
172 : candidate: MissingMediaCandidate
173 : ): boolean {
174 15 : return (
175 : candidate.isMissing === true &&
176 : isMissingMediaCandidateScopeActive(rootGraph, candidate)
177 : )
178 : }
179 :
180 : /** Resolve pending candidates against the asset store, then a remote hash lookup when assets are enabled. */
181 9 : export async function verifyMediaCandidates(
182 : candidates: MissingMediaCandidate[],
183 : { signal }: { signal?: AbortSignal } = {}
184 : ): Promise<void> {
185 9 : if (signal?.aborted) return
186 9 : const pending = candidates.filter((c) => c.isMissing === undefined)
187 : const results = await Promise.allSettled(
188 : Object.entries(groupBy(pending, (p) => p.name)).map(
189 : async ([name, candidateGroup]) => {
190 13 : const asset = await resolveWidgetValueAsset(name, { signal })
191 13 : const isMissing = asset === undefined ? undefined : asset === null
192 13 : for (const candidate of candidateGroup) candidate.isMissing = isMissing
193 : }
194 : )
195 : )
196 9 : const firstRejection = results.find((r) => r.status === 'rejected')
197 9 : if (firstRejection && !signal?.aborted) throw firstRejection.reason
198 : }
199 :
200 : /** Group confirmed-missing candidates by file name into view models. */
201 38 : export function groupCandidatesByName(
202 : candidates: MissingMediaCandidate[]
203 : ): MissingMediaViewModel[] {
204 38 : const map = new Map<string, MissingMediaViewModel>()
205 38 : for (const c of candidates) {
206 42 : const existing = map.get(c.name)
207 42 : if (existing) {
208 0 : existing.referencingNodes.push({
209 : nodeId: c.nodeId,
210 : nodeType: c.nodeType,
211 : widgetName: c.widgetName
212 : })
213 : } else {
214 42 : map.set(c.name, {
215 : name: c.name,
216 : mediaType: c.mediaType,
217 : representative: c,
218 : referencingNodes: [
219 : { nodeId: c.nodeId, nodeType: c.nodeType, widgetName: c.widgetName }
220 : ]
221 : })
222 : }
223 : }
224 38 : return Array.from(map.values())
225 : }
226 :
227 : /** Group confirmed-missing candidates by media type. */
228 38 : export function groupCandidatesByMediaType(
229 : candidates: MissingMediaCandidate[]
230 : ): MissingMediaGroup[] {
231 38 : const grouped = groupBy(candidates, (c) => c.mediaType)
232 : const order: MediaType[] = ['image', 'video', 'audio']
233 38 : return order
234 : .filter((t) => t in grouped)
235 : .map((mediaType) => ({
236 : mediaType,
237 : items: groupCandidatesByName(grouped[mediaType])
238 : }))
239 : }
|