Line data Source code
1 : import type { MissingMediaCandidate } from '@/platform/workflowErrors/missingMedia/types'
2 : import type { MissingModelCandidate } from '@/platform/workflowErrors/missingModel/types'
3 : import type { NodeExecutionId } from '@comfyorg/graph-model/nodeIdentification'
4 : import type { PromotedWidgetExecutionSource } from '@/core/graph/subgraph/promotedWidgetTypes'
5 : import { getLiftedErrorSource } from '@/core/graph/subgraph/liftNodeErrorsToBoundary'
6 : import type { LiftedErrorExtraInfo } from '@/core/graph/subgraph/liftNodeErrorsToBoundary'
7 : import {
8 : isImageNotLoadedValidationError,
9 : isMissingNodePromptError
10 : } from '@/utils/executionErrorUtil'
11 : import type { NodeValidationError } from '@/utils/executionErrorUtil'
12 :
13 : import type { PromptError } from '@comfyorg/api-client/types'
14 :
15 : export type MissingResourceAbsorption = 'missing_model' | 'missing_media'
16 :
17 0 : function normalizePath(value: string): string {
18 0 : return value.replace(/[\\/]+/g, '/').replace(/\/+$/, '')
19 : }
20 :
21 0 : function matchesReceivedValue(
22 : receivedValue: unknown,
23 : candidateName: string
24 : ): boolean {
25 0 : if (receivedValue == null) return false
26 :
27 0 : return normalizePath(String(receivedValue)) === normalizePath(candidateName)
28 : }
29 :
30 55 : function isEligibleValidationError(error: NodeValidationError): boolean {
31 55 : return (
32 : error.type === 'value_not_in_list' || isImageNotLoadedValidationError(error)
33 : )
34 : }
35 :
36 24 : function matchesErrorNodeId(
37 : candidateNodeIds: readonly (string | number | null | undefined)[],
38 : liftedSource: LiftedErrorExtraInfo | null,
39 : nodeId: NodeExecutionId
40 : ): boolean {
41 24 : const errorNodeIds = [nodeId, liftedSource?.source_execution_id]
42 : .filter((id) => id != null)
43 : .map(String)
44 :
45 24 : return candidateNodeIds.some(
46 : (candidateId) =>
47 : candidateId != null && errorNodeIds.includes(String(candidateId))
48 : )
49 : }
50 :
51 24 : function matchesErrorInputName(
52 : candidateNodeIds: readonly (string | number | null | undefined)[],
53 : candidateInputName: string,
54 : errorInputName: string | undefined,
55 : liftedSource: LiftedErrorExtraInfo | null
56 : ): boolean {
57 24 : if (candidateInputName === errorInputName) return true
58 0 : if (!liftedSource) return false
59 :
60 24 : return (
61 : candidateInputName === liftedSource.source_input_name &&
62 : candidateNodeIds.some(
63 : (id) => id != null && String(id) === liftedSource.source_execution_id
64 : )
65 : )
66 : }
67 :
68 0 : function matchesPromotedSource(
69 : source: PromotedWidgetExecutionSource,
70 : error: NodeValidationError,
71 : nodeId: NodeExecutionId
72 : ): boolean {
73 0 : if (String(source.executionId) !== String(nodeId)) return false
74 0 : return source.widgetName === error.extra_info?.input_name
75 : }
76 :
77 24 : function matchesCandidate(
78 : candidateNodeIds: readonly (string | number | null | undefined)[],
79 : candidateInputName: string,
80 : candidateName: string,
81 : isMissing: boolean | undefined,
82 : error: NodeValidationError,
83 : nodeId: NodeExecutionId,
84 : promotedSources: readonly PromotedWidgetExecutionSource[] = []
85 : ): boolean {
86 24 : if (isMissing !== true) return false
87 : // Checked before the host-identity gate: an unlifted interior error never
88 : // carries the host id the candidate is keyed by.
89 24 : if (
90 : promotedSources.some((source) =>
91 : matchesPromotedSource(source, error, nodeId)
92 : )
93 : ) {
94 0 : return true
95 : }
96 :
97 24 : const liftedSource = getLiftedErrorSource(error)
98 24 : if (!matchesErrorNodeId(candidateNodeIds, liftedSource, nodeId)) {
99 0 : return false
100 : }
101 :
102 24 : if (
103 : matchesErrorInputName(
104 : candidateNodeIds,
105 : candidateInputName,
106 : error.extra_info?.input_name,
107 : liftedSource
108 : )
109 : ) {
110 24 : return true
111 : }
112 : // Value equality alone is too weak a signal when the error names an input:
113 : // a same-valued sibling widget's genuinely blocking error must stay red.
114 24 : if (error.extra_info?.input_name != null) return false
115 24 : return matchesReceivedValue(error.extra_info?.received_value, candidateName)
116 : }
117 :
118 24 : function matchesMissingModel(
119 : candidate: MissingModelCandidate,
120 : error: NodeValidationError,
121 : nodeId: NodeExecutionId
122 : ): boolean {
123 24 : return matchesCandidate(
124 : [candidate.sourceExecutionId, candidate.nodeId],
125 : candidate.widgetName,
126 : candidate.name,
127 : candidate.isMissing,
128 : error,
129 : nodeId,
130 : candidate.promotedSources
131 : )
132 : }
133 :
134 0 : function matchesMissingMedia(
135 : candidate: MissingMediaCandidate,
136 : error: NodeValidationError,
137 : nodeId: NodeExecutionId
138 : ): boolean {
139 0 : return matchesCandidate(
140 : [candidate.nodeId],
141 : candidate.widgetName,
142 : candidate.name,
143 : candidate.isMissing,
144 : error,
145 : nodeId,
146 : candidate.promotedSources
147 : )
148 : }
149 :
150 55 : export function classifyValidationErrorAbsorption(
151 : missingModels: readonly MissingModelCandidate[] | null,
152 : missingMedia: readonly MissingMediaCandidate[] | null,
153 : error: NodeValidationError,
154 : nodeId: NodeExecutionId
155 : ): MissingResourceAbsorption | null {
156 55 : if (!isEligibleValidationError(error)) return null
157 :
158 55 : if (
159 : missingModels?.some((candidate) =>
160 : matchesMissingModel(candidate, error, nodeId)
161 : )
162 : ) {
163 24 : return 'missing_model'
164 : }
165 55 : if (
166 : missingMedia?.some((candidate) =>
167 : matchesMissingMedia(candidate, error, nodeId)
168 : )
169 : ) {
170 0 : return 'missing_media'
171 : }
172 4 : return null
173 : }
174 :
175 5 : export function isMissingNodePromptErrorAbsorbed(
176 : promptError: PromptError | null,
177 : hasMissingNodes: boolean
178 : ): boolean {
179 5 : return isMissingNodePromptError(promptError) && hasMissingNodes
180 : }
|