Line data Source code
1 : import type {
2 : DeclaredModelFile,
3 : ModelFile
4 : } from '@comfyorg/graph-model/workflowSchema'
5 : import { getComboWidgetInventory } from '@/core/graph/widgets/comboWidgetInventory'
6 : import type { FlattenableWorkflowGraph } from '@/platform/workflow/core/utils/workflowFlattening'
7 : import { flattenWorkflowNodes } from '@/platform/workflow/core/utils/workflowFlattening'
8 : import {
9 : getSelectedModelsMetadata,
10 : isInactiveWorkflowNodeMode,
11 : isNodeAndAncestorsActive
12 : } from '@/platform/workflow/core/utils/modelRequirements'
13 : import type { MissingModelCandidate } from './types'
14 : import { findModelReplacements } from './modelReplacements'
15 : import { getAssetFilename } from '@/platform/assets/utils/assetMetadataUtils'
16 : import type { AssetItem } from '@/platform/assets/schemas/assetSchema'
17 : import {
18 : inputForWidget,
19 : promotedInputWidgets
20 : } from '@/core/graph/subgraph/promotedInputWidget'
21 : import { resolvePromotedWidgetSource } from '@/core/graph/subgraph/resolvePromotedWidgetSource'
22 : import {
23 : buildPromotedWidgetExecutionSources,
24 : resolveActivePromotedWidgetConsumers
25 : } from '@/core/graph/subgraph/resolveConcretePromotedWidget'
26 : import type { PromotedWidgetExecutionSource } from '@/core/graph/subgraph/promotedWidgetTypes'
27 : import type { LGraph } from '@/lib/litegraph/src/LGraph'
28 : import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode'
29 : import type {
30 : IAssetWidget,
31 : IBaseWidget,
32 : IComboWidget
33 : } from '@comfyorg/graph-model/widgets'
34 : import { useAssetsStore } from '@/stores/assetsStore'
35 : import type { NodeExecutionId } from '@comfyorg/graph-model/nodeIdentification'
36 : import {
37 : collectAllNodes,
38 : getExecutionIdByNode,
39 : isExecutionPathActive
40 : } from '@/utils/graphTraversalUtil'
41 : import { resolveComboValues } from '@/utils/litegraphUtil'
42 :
43 : export type MissingModelWorkflowData = FlattenableWorkflowGraph & {
44 : models?: ModelFile[]
45 : }
46 :
47 : type CandidateVerification = Pick<
48 : MissingModelCandidate,
49 : 'isMissing' | 'replacements'
50 : >
51 :
52 : type DeferredVerification = (
53 : signal?: AbortSignal
54 : ) => Promise<CandidateVerification>
55 :
56 : /**
57 : * On a developer-platform deployment (FE-2434) the editor's node catalog is
58 : * the deployment's Release, so a loader's model options there are models the
59 : * deployment has. Resolves those options for a loader input, or undefined
60 : * when the editor runs on Comfy Cloud, when that is not known yet, or once
61 : * `signal` aborts.
62 : */
63 : export type ReleaseModelOptions = (
64 : nodeType: string,
65 : widgetName: string,
66 : signal?: AbortSignal
67 : ) => Promise<readonly (string | number)[] | undefined>
68 :
69 2356 : const pendingVerifications = new WeakMap<
70 : MissingModelCandidate,
71 : DeferredVerification
72 : >()
73 :
74 2913 : export function hasPendingVerification(
75 : candidate: MissingModelCandidate
76 : ): boolean {
77 2913 : return pendingVerifications.has(candidate)
78 : }
79 :
80 5612 : function copyCandidate(
81 : candidate: MissingModelCandidate,
82 : overrides: Partial<MissingModelCandidate> = {}
83 : ): MissingModelCandidate {
84 5612 : const copy = { ...candidate, ...overrides }
85 5612 : const verify = pendingVerifications.get(candidate)
86 5612 : if (verify) pendingVerifications.set(copy, verify)
87 5612 : return copy
88 : }
89 :
90 45603 : function isComboWidget(widget: IBaseWidget): widget is IComboWidget {
91 45603 : return widget.type === 'combo'
92 : }
93 :
94 : /**
95 : * Fills url/hash/directory onto a candidate from the node's embedded
96 : * `properties.models` metadata when the names match. The full pipeline
97 : * does this via enrichWithEmbeddedMetadata + graphData.models, but the
98 : * realtime single-node scan (paste, un-bypass) otherwise loses these
99 : * fields — making the Missing Model row's download/copy-url buttons
100 : * disappear after a bypass/un-bypass cycle.
101 : */
102 2973 : function enrichCandidateFromNodeProperties(
103 : candidate: MissingModelCandidate,
104 : embeddedModels: readonly ModelFile[] | undefined
105 : ): MissingModelCandidate {
106 2973 : if (!embeddedModels?.length) return candidate
107 : // Require directory agreement when the candidate already has one —
108 : // a single node can reference two models with the same name under
109 : // different directories (e.g. a LoRA present in multiple folders);
110 : // name-only matching would stamp the wrong url/hash onto the
111 : // candidate. Mirrors the directory check in enrichWithEmbeddedMetadata.
112 2704 : const match = embeddedModels.find(
113 : (m) =>
114 : m.name === candidate.name &&
115 : (!candidate.directory || candidate.directory === m.directory)
116 : )
117 2959 : if (!match) return candidate
118 2973 : return copyCandidate(candidate, {
119 : directory: candidate.directory ?? match.directory,
120 : url: candidate.url ?? match.url,
121 : hash: candidate.hash ?? match.hash,
122 : hashType: candidate.hashType ?? match.hash_type
123 : })
124 : }
125 :
126 45603 : function isAssetWidget(widget: IBaseWidget): widget is IAssetWidget {
127 45603 : return widget.type === 'asset'
128 : }
129 :
130 : interface ModelWidgetScanTarget {
131 : executionId: NodeExecutionId
132 : nodeType: string
133 : candidateWidgetName: string
134 : definitionWidgetName: string
135 : sourceExecutionId?: NodeExecutionId
136 : promotedSources?: PromotedWidgetExecutionSource[]
137 : valueWidget: IBaseWidget
138 : definitionWidget: IBaseWidget
139 : embeddedModels?: ModelFile[]
140 : }
141 :
142 : type NodeWithEmbeddedModels = {
143 : properties?: {
144 : models?: ModelFile[]
145 : }
146 : }
147 :
148 : // Full set of model file extensions used for scanning candidate widgets.
149 : // Intentionally broader than ALLOWED_SUFFIXES in missingModelDownload.ts,
150 : // which restricts which files are eligible for download.
151 2356 : export const MODEL_FILE_EXTENSIONS = new Set([
152 : '.safetensors',
153 : '.ckpt',
154 : '.pt',
155 : '.pth',
156 : '.bin',
157 : '.sft',
158 : '.onnx',
159 : '.gguf'
160 : ])
161 :
162 13168 : export function isModelFileName(name: string): boolean {
163 13168 : const lower = name.toLowerCase()
164 13168 : return Array.from(MODEL_FILE_EXTENSIONS).some((ext) => lower.endsWith(ext))
165 : }
166 :
167 : /**
168 : * Scan COMBO and asset widgets on configured graph nodes for model-like values.
169 : * Must be called after `graph.configure()` so widget name/value mappings are accurate.
170 : *
171 : * `isMissing` resolves immediately from static combo options and stays
172 : * `undefined` for asset-supported nodes and loading remote combos.
173 : */
174 3796 : export function scanAllModelCandidates(
175 : rootGraph: LGraph,
176 : isAssetSupported: (nodeType: string, widgetName: string) => boolean,
177 : getDirectory?: (nodeType: string) => string | undefined,
178 : releaseModelOptions?: ReleaseModelOptions
179 : ): MissingModelCandidate[] {
180 3796 : const allNodes = collectAllNodes(rootGraph)
181 3796 : const candidates: MissingModelCandidate[] = []
182 :
183 3796 : for (const node of allNodes) {
184 21579 : if (isInactiveWorkflowNodeMode(node.mode)) continue
185 :
186 21572 : candidates.push(
187 : ...scanNodeModelCandidates(
188 : rootGraph,
189 : node,
190 : isAssetSupported,
191 : getDirectory,
192 : releaseModelOptions
193 : )
194 : )
195 : }
196 :
197 3796 : return candidates
198 : }
199 :
200 : /** Scan a single node's widgets for missing model candidates (OSS immediate resolution). */
201 22178 : export function scanNodeModelCandidates(
202 : rootGraph: LGraph,
203 : node: LGraphNode,
204 : isAssetSupported: (nodeType: string, widgetName: string) => boolean,
205 : getDirectory?: (nodeType: string) => string | undefined,
206 : releaseModelOptions?: ReleaseModelOptions
207 : ): MissingModelCandidate[] {
208 : const isSubgraphNode =
209 : typeof node.isSubgraphNode === 'function' && node.isSubgraphNode()
210 22178 : const widgets = isSubgraphNode
211 : ? promotedInputWidgets(node)
212 : : (node.widgets ?? [])
213 22178 : if (!widgets.length) return []
214 :
215 18896 : const executionId = getExecutionIdByNode(rootGraph, node)
216 22178 : if (!executionId) return []
217 :
218 18893 : const candidates: MissingModelCandidate[] = []
219 :
220 22177 : for (const widget of widgets) {
221 46328 : const target = getModelWidgetScanTarget(
222 : rootGraph,
223 : node,
224 : widget,
225 : executionId
226 : )
227 46328 : if (!target) continue
228 :
229 45603 : let candidate: MissingModelCandidate | null = null
230 :
231 46328 : if (isAssetScanTarget(target)) {
232 0 : candidate = scanAssetWidget(target, getDirectory, releaseModelOptions)
233 45603 : } else if (isComboScanTarget(target)) {
234 13225 : candidate = scanComboWidget(
235 : target,
236 : isAssetSupported,
237 : getDirectory,
238 : releaseModelOptions
239 : )
240 : }
241 :
242 46290 : if (!candidate) continue
243 46007 : if (target.promotedSources) {
244 48 : candidate.promotedSources = target.promotedSources
245 : }
246 2973 : candidates.push(
247 : enrichCandidateFromNodeProperties(candidate, target.embeddedModels)
248 : )
249 : }
250 :
251 18893 : return candidates
252 : }
253 :
254 46328 : function getModelWidgetScanTarget(
255 : rootGraph: LGraph,
256 : node: LGraphNode,
257 : widget: IBaseWidget,
258 : executionId: NodeExecutionId
259 : ): ModelWidgetScanTarget | null {
260 46328 : const input = getInputForWidget(node, widget)
261 46328 : if (input && node.isInputConnected(node.inputs.indexOf(input))) return null
262 :
263 : const isSubgraphNode =
264 : typeof node.isSubgraphNode === 'function' && node.isSubgraphNode()
265 46328 : if (!isSubgraphNode) {
266 45251 : return {
267 : executionId,
268 : nodeType: node.type,
269 : candidateWidgetName: widget.name,
270 : definitionWidgetName: widget.name,
271 : valueWidget: widget,
272 : definitionWidget: widget,
273 : embeddedModels: getEmbeddedModels(node)
274 : }
275 : }
276 :
277 4758 : if (!input) return null
278 :
279 352 : const source = resolvePromotedWidgetSource(rootGraph, node, widget)
280 46328 : const sourceExecutionId = source?.sourceExecutionId
281 46328 : if (!sourceExecutionId) return null
282 352 : const consumers = resolveActivePromotedWidgetConsumers(node, widget.name)
283 352 : const promotedSources = buildPromotedWidgetExecutionSources(
284 : executionId,
285 : consumers
286 : )
287 4758 : if (
288 : !promotedSources.some((source) =>
289 : isExecutionPathActive(rootGraph, source.executionId)
290 : )
291 : ) {
292 0 : return null
293 : }
294 :
295 352 : return {
296 : executionId,
297 : nodeType: source.sourceNode.type,
298 : candidateWidgetName: widget.name,
299 : definitionWidgetName: source.sourceWidgetName,
300 : sourceExecutionId,
301 : promotedSources,
302 : valueWidget: widget,
303 : definitionWidget: source.sourceWidget,
304 : embeddedModels: getEmbeddedModels(source.sourceNode)
305 : }
306 : }
307 :
308 46328 : function getInputForWidget(node: LGraphNode, widget: IBaseWidget) {
309 46328 : if (typeof node.getSlotFromWidget !== 'function') return undefined
310 46328 : return inputForWidget(node, widget)
311 : }
312 :
313 45603 : function getEmbeddedModels(node: LGraphNode): ModelFile[] | undefined {
314 45603 : return (node as NodeWithEmbeddedModels).properties?.models
315 : }
316 :
317 45603 : function isAssetScanTarget(
318 : target: ModelWidgetScanTarget
319 : ): target is ModelWidgetScanTarget & { definitionWidget: IAssetWidget } {
320 45603 : return isAssetWidget(target.definitionWidget)
321 : }
322 :
323 45603 : function isComboScanTarget(
324 : target: ModelWidgetScanTarget
325 : ): target is ModelWidgetScanTarget & { definitionWidget: IComboWidget } {
326 45603 : return isComboWidget(target.definitionWidget)
327 : }
328 :
329 : /**
330 : * Verification then takes a model the deployment's Release lists for this
331 : * loader as present, whatever Cloud's model library holds. A model the
332 : * Release does not list, and every model on Comfy Cloud, falls through to
333 : * the library check.
334 : */
335 0 : function deferToReleaseOptions(
336 : candidate: MissingModelCandidate,
337 : target: ModelWidgetScanTarget,
338 : releaseModelOptions: ReleaseModelOptions | undefined
339 : ): void {
340 0 : if (!releaseModelOptions) return
341 0 : pendingVerifications.set(candidate, async (signal) => {
342 : const options = await releaseModelOptions(
343 : target.nodeType,
344 : target.definitionWidgetName,
345 : signal
346 : )
347 0 : return { isMissing: options?.includes(candidate.name) ? false : undefined }
348 : })
349 : }
350 :
351 0 : function scanAssetWidget(
352 : target: ModelWidgetScanTarget & { definitionWidget: IAssetWidget },
353 : getDirectory: ((nodeType: string) => string | undefined) | undefined,
354 : releaseModelOptions: ReleaseModelOptions | undefined
355 : ): MissingModelCandidate | null {
356 0 : const value = target.valueWidget.value
357 0 : if (typeof value !== 'string' || !value.trim()) return null
358 0 : if (!isModelFileName(value)) return null
359 :
360 0 : const candidate: MissingModelCandidate = {
361 : nodeId: target.executionId,
362 : ...(target.sourceExecutionId && {
363 : sourceExecutionId: target.sourceExecutionId
364 : }),
365 : nodeType: target.nodeType,
366 : widgetName: target.candidateWidgetName,
367 : isAssetSupported: true,
368 : name: value,
369 : directory: getDirectory?.(target.nodeType),
370 : isMissing: undefined
371 : }
372 0 : deferToReleaseOptions(candidate, target, releaseModelOptions)
373 0 : return candidate
374 : }
375 :
376 13225 : function scanComboWidget(
377 : target: ModelWidgetScanTarget & { definitionWidget: IComboWidget },
378 : isAssetSupported: (nodeType: string, widgetName: string) => boolean,
379 : getDirectory: ((nodeType: string) => string | undefined) | undefined,
380 : releaseModelOptions: ReleaseModelOptions | undefined
381 : ): MissingModelCandidate | null {
382 13225 : const value = target.valueWidget.value
383 13225 : if (typeof value !== 'string' || !value.trim()) return null
384 13223 : if (!isModelFileName(value)) return null
385 :
386 2973 : const nodeIsAssetSupported = isAssetSupported(
387 : target.nodeType,
388 : target.definitionWidgetName
389 : )
390 13225 : const candidate: MissingModelCandidate = {
391 : nodeId: target.executionId,
392 : ...(target.sourceExecutionId && {
393 : sourceExecutionId: target.sourceExecutionId
394 : }),
395 : nodeType: target.nodeType,
396 : widgetName: target.candidateWidgetName,
397 : isAssetSupported: nodeIsAssetSupported,
398 : name: value,
399 : directory: getDirectory?.(target.nodeType),
400 : isMissing: undefined
401 : }
402 13225 : if (nodeIsAssetSupported) {
403 0 : deferToReleaseOptions(candidate, target, releaseModelOptions)
404 0 : return candidate
405 : }
406 :
407 2973 : const verifyAgainstOptions = (): CandidateVerification => {
408 2972 : const options = resolveComboValues(target.definitionWidget)
409 2972 : if (options.includes(value)) return { isMissing: false }
410 2883 : const replacements = findModelReplacements(value, options)
411 2972 : return replacements.length
412 : ? { isMissing: true, replacements }
413 : : { isMissing: true }
414 : }
415 2973 : const inventory = getComboWidgetInventory(target.definitionWidget)
416 13225 : if (inventory && inventory.getStatus() !== 'ready') {
417 1 : pendingVerifications.set(candidate, async (signal) => {
418 1 : await untilSettledOrAborted(inventory.waitForSettled(signal), signal)
419 1 : if (signal?.aborted || inventory.getStatus() !== 'ready') {
420 0 : return { isMissing: undefined }
421 : }
422 1 : if (target.valueWidget.value !== value) return { isMissing: undefined }
423 0 : return verifyAgainstOptions()
424 : })
425 1 : return candidate
426 : }
427 :
428 2972 : return Object.assign(candidate, verifyAgainstOptions())
429 : }
430 :
431 1 : async function untilSettledOrAborted(
432 : settled: Promise<void>,
433 : signal?: AbortSignal
434 : ): Promise<void> {
435 1 : if (!signal) return settled
436 1 : if (signal.aborted) return
437 1 : let onAbort = () => {}
438 1 : const aborted = new Promise<void>((resolve) => {
439 1 : onAbort = resolve
440 1 : signal.addEventListener('abort', onAbort, { once: true })
441 : })
442 1 : try {
443 1 : await Promise.race([settled, aborted])
444 : } finally {
445 1 : signal.removeEventListener('abort', onAbort)
446 : }
447 : }
448 :
449 3796 : export function enrichWithEmbeddedMetadata(
450 : candidates: readonly MissingModelCandidate[],
451 : graphData: MissingModelWorkflowData
452 : ): MissingModelCandidate[] {
453 : const allNodes = flattenWorkflowNodes(graphData)
454 3796 : const embeddedModels = collectEmbeddedModels(allNodes, graphData)
455 :
456 3796 : const enriched = candidates.map((c) => copyCandidate(c))
457 3796 : const candidatesByKey = new Map<string, MissingModelCandidate[]>()
458 3796 : for (const c of enriched) {
459 2913 : const dirKey = `${c.name}::${c.directory ?? ''}`
460 2913 : const dirList = candidatesByKey.get(dirKey)
461 2913 : if (dirList) dirList.push(c)
462 2831 : else candidatesByKey.set(dirKey, [c])
463 :
464 2913 : const nameKey = c.name
465 2913 : const nameList = candidatesByKey.get(nameKey)
466 2913 : if (nameList) nameList.push(c)
467 2831 : else candidatesByKey.set(nameKey, [c])
468 : }
469 :
470 3796 : const deduped: DeclaredModelFile[] = []
471 3796 : const enrichedKeys = new Set<string>()
472 3796 : for (const model of embeddedModels) {
473 2726 : const dedupeKey = `${model.name}::${model.directory}`
474 2726 : if (enrichedKeys.has(dedupeKey)) continue
475 2718 : enrichedKeys.add(dedupeKey)
476 2718 : deduped.push(model)
477 : }
478 :
479 3796 : for (const model of deduped) {
480 2718 : const dirKey = `${model.name}::${model.directory}`
481 2718 : const exact = candidatesByKey.get(dirKey)
482 2718 : const fallback = candidatesByKey.get(model.name)
483 2718 : const existing = exact?.length ? exact : fallback
484 2718 : if (!existing) continue
485 2704 : for (const c of existing) {
486 2712 : if (c.directory && c.directory !== model.directory) continue
487 2712 : c.directory ??= model.directory
488 2712 : c.url ??= model.url
489 2712 : c.hash ??= model.hash
490 2712 : c.hashType ??= model.hash_type
491 : }
492 : }
493 :
494 3796 : return enriched
495 : }
496 :
497 3796 : function collectEmbeddedModels(
498 : allNodes: ReturnType<typeof flattenWorkflowNodes>,
499 : graphData: MissingModelWorkflowData
500 : ): DeclaredModelFile[] {
501 : // Node entries only name a model; enrichment fills each field where present.
502 3796 : const result: DeclaredModelFile[] = []
503 3796 : const nodesById = new Map(allNodes.map((node) => [String(node.id), node]))
504 :
505 3796 : for (const node of allNodes) {
506 21563 : if (!isNodeAndAncestorsActive(node, nodesById)) continue
507 :
508 21554 : const selected = getSelectedModelsMetadata(node)
509 21563 : if (!selected?.length) continue
510 :
511 2686 : result.push(...selected)
512 : }
513 :
514 3796 : if (graphData.models?.length) result.push(...graphData.models)
515 :
516 3796 : return result
517 : }
518 :
519 1 : export async function verifyAssetSupportedCandidates(
520 : candidates: MissingModelCandidate[],
521 : signal?: AbortSignal
522 : ): Promise<void> {
523 1 : if (signal?.aborted) return
524 :
525 1 : await Promise.all(
526 : candidates.map(async (candidate) => {
527 1 : const verify = pendingVerifications.get(candidate)
528 1 : if (!verify) return
529 1 : pendingVerifications.delete(candidate)
530 1 : const verification = await verify(signal)
531 1 : if (!signal?.aborted) Object.assign(candidate, verification)
532 : })
533 : )
534 :
535 1 : if (signal?.aborted) return
536 :
537 1 : const pendingCandidates = candidates.filter(
538 : (c) => c.isAssetSupported && c.isMissing === undefined
539 : )
540 1 : if (pendingCandidates.length === 0) return
541 :
542 0 : const pendingNodeTypes = new Set(
543 : pendingCandidates.map((candidate) => candidate.nodeType)
544 : )
545 :
546 0 : const store = useAssetsStore()
547 :
548 0 : const failedNodeTypes = new Set<string>()
549 0 : await Promise.allSettled(
550 : [...pendingNodeTypes].map(async (nodeType) => {
551 0 : if (signal?.aborted) return
552 0 : try {
553 0 : await store.updateModelsForNodeType(nodeType)
554 : } catch (err) {
555 0 : failedNodeTypes.add(nodeType)
556 0 : console.warn(
557 : `[Missing Model Pipeline] Failed to load assets for ${nodeType}:`,
558 : err
559 : )
560 : }
561 : })
562 : )
563 :
564 1 : if (signal?.aborted) return
565 :
566 0 : for (const c of candidates) {
567 0 : if (!c.isAssetSupported || c.isMissing !== undefined) continue
568 0 : if (failedNodeTypes.has(c.nodeType)) continue
569 :
570 : const assets = store.getAssets(c.nodeType)
571 0 : c.isMissing = !isAssetInstalled(c, assets)
572 : }
573 : }
574 :
575 0 : function normalizePath(path: string): string {
576 0 : return path.replace(/\\/g, '/')
577 : }
578 :
579 0 : function isAssetInstalled(
580 : candidate: MissingModelCandidate,
581 : assets: AssetItem[]
582 : ): boolean {
583 0 : if (candidate.hash && candidate.hashType) {
584 0 : const candidateHash = `${candidate.hashType}:${candidate.hash}`
585 0 : if (assets.some((a) => a.hash === candidateHash)) return true
586 : }
587 :
588 0 : const normalizedName = normalizePath(candidate.name)
589 0 : return assets.some((a) => {
590 0 : const f = normalizePath(getAssetFilename(a))
591 0 : return f === normalizedName || f.endsWith('/' + normalizedName)
592 : })
593 : }
|