Line data Source code
1 : import type { NodeError, PromptError } from '@comfyorg/api-client/types'
2 : import type { SerializedNodeId } from '@comfyorg/graph-model/nodeId'
3 : import type { zPromptOutputsSkippedNodeError } from '@comfyorg/ingest-types/zod'
4 : import type { z } from 'zod'
5 :
6 : type PromptOutputsSkippedNodeError = z.infer<
7 : typeof zPromptOutputsSkippedNodeError
8 : >
9 :
10 : type RawPromptError =
11 : | string
12 : | { type?: string; message?: string; details?: string }
13 :
14 : /**
15 : * The standard prompt validation response shape (`{ error, node_errors }`).
16 : * In cloud, this is embedded as JSON inside `execution_error.exception_message`
17 : * because prompts are queued asynchronously and errors arrive via WebSocket
18 : * rather than as direct HTTP responses.
19 : */
20 : interface CloudValidationError {
21 : error?: RawPromptError
22 : node_errors?: Record<SerializedNodeId, NodeError>
23 : }
24 :
25 0 : export function isCloudValidationError(
26 : value: unknown
27 : ): value is CloudValidationError {
28 0 : return (
29 : value !== null &&
30 : typeof value === 'object' &&
31 : ('error' in value || 'node_errors' in value)
32 : )
33 : }
34 :
35 : /**
36 : * Extracts a prompt validation response embedded in an exception message string.
37 : *
38 : * Cloud example `exception_message`:
39 : * "Failed to send prompt request: ... 400: {\"error\":{...},\"node_errors\":{...}}"
40 : *
41 : * This function finds the first '{' and parses the trailing JSON.
42 : */
43 4 : export function tryExtractValidationError(
44 : exceptionMessage: string
45 : ): CloudValidationError | null {
46 4 : const jsonStart = exceptionMessage.indexOf('{')
47 4 : const jsonEnd = exceptionMessage.lastIndexOf('}')
48 4 : if (jsonStart === -1 || jsonEnd === -1) return null
49 :
50 4 : try {
51 0 : const parsed: unknown = JSON.parse(
52 : exceptionMessage.substring(jsonStart, jsonEnd + 1)
53 : )
54 4 : return isCloudValidationError(parsed) ? parsed : null
55 : } catch {
56 0 : return null
57 : }
58 : }
59 :
60 : type CloudValidationResult =
61 : | { kind: 'nodeErrors'; nodeErrors: Record<SerializedNodeId, NodeError> }
62 : | { kind: 'promptError'; promptError: PromptError }
63 :
64 36 : export function normalizePromptError(error: unknown): PromptError | null {
65 36 : if (error !== null && typeof error === 'object') {
66 35 : return {
67 : type:
68 : 'type' in error && typeof error.type === 'string'
69 : ? error.type
70 : : 'error',
71 : message:
72 : 'message' in error && typeof error.message === 'string'
73 : ? error.message
74 : : '',
75 : details:
76 : 'details' in error && typeof error.details === 'string'
77 : ? error.details
78 : : ''
79 : }
80 : }
81 :
82 36 : return typeof error === 'string'
83 : ? { type: 'error', message: error, details: '' }
84 : : null
85 : }
86 :
87 1 : function isExtraInfo(
88 : value: unknown
89 : ): value is NonNullable<NodeValidationError['extra_info']> {
90 1 : return (
91 : value !== null &&
92 : typeof value === 'object' &&
93 : !Array.isArray(value) &&
94 : (!('input_name' in value) || typeof value.input_name === 'string')
95 : )
96 : }
97 :
98 1 : function toNodeValidationErrors(error: unknown): NodeValidationError[] {
99 1 : const promptError = normalizePromptError(error)
100 1 : if (!promptError) return []
101 1 : const extraInfo =
102 : error !== null && typeof error === 'object' && 'extra_info' in error
103 : ? error.extra_info
104 : : undefined
105 1 : return [
106 : isExtraInfo(extraInfo)
107 : ? { ...promptError, extra_info: extraInfo }
108 : : promptError
109 : ]
110 : }
111 :
112 : /**
113 : * Converts relayed node errors to `NodeError`s; a string entry becomes the
114 : * message, and a node left with no errors is dropped.
115 : */
116 1 : export function normalizeRelayedNodeErrors(
117 : nodeErrors: Record<string, PromptOutputsSkippedNodeError>
118 : ): Record<string, NodeError> {
119 1 : return Object.fromEntries(
120 : Object.entries(nodeErrors).flatMap(([nodeId, nodeError]) => {
121 1 : const errors = nodeError.errors.flatMap(toNodeValidationErrors)
122 1 : return errors.length > 0 ? [[nodeId, { ...nodeError, errors }]] : []
123 : })
124 : )
125 : }
126 :
127 3860 : export function isMissingNodePromptError(
128 : promptError: PromptError | null | undefined
129 : ): boolean {
130 3860 : return promptError?.type === 'missing_node_type'
131 : }
132 :
133 : /**
134 : * Classifies an embedded cloud validation error from `exception_message`
135 : * as either node-level errors or a prompt-level error.
136 : *
137 : * Returns `null` if the message does not contain a recognizable validation error.
138 : */
139 0 : export function classifyCloudValidationError(
140 : exceptionMessage: string
141 : ): CloudValidationResult | null {
142 0 : const extracted = tryExtractValidationError(exceptionMessage)
143 0 : if (!extracted) return null
144 :
145 0 : const { error, node_errors } = extracted
146 : const hasNodeErrors = node_errors && Object.keys(node_errors).length > 0
147 :
148 0 : if (hasNodeErrors) {
149 0 : return { kind: 'nodeErrors', nodeErrors: node_errors }
150 : }
151 :
152 0 : const promptError = normalizePromptError(error)
153 0 : return promptError ? { kind: 'promptError', promptError } : null
154 : }
155 :
156 7 : export function errorsForSlot(
157 : errors: NodeError['errors'],
158 : slotName: string
159 : ): NodeError['errors'] {
160 7 : return errors.filter((error) => error.extra_info?.input_name === slotName)
161 : }
162 :
163 147 : export function hasErrorForSlot(
164 : errors: NodeError['errors'],
165 : slotName: string
166 : ): boolean {
167 147 : return errors.some((error) => error.extra_info?.input_name === slotName)
168 : }
169 :
170 : /**
171 : * Error types that can be resolved automatically when the user changes a
172 : * widget value or establishes a connection, without requiring a re-run.
173 : *
174 : * When adding new types, review {@link isValueStillOutOfRange} to ensure
175 : * the new type does not require range validation before auto-clearing.
176 : */
177 2356 : export const SIMPLE_ERROR_TYPES = new Set([
178 : 'value_bigger_than_max',
179 : 'value_smaller_than_min',
180 : 'value_not_in_list',
181 : 'required_input_missing'
182 : ])
183 :
184 : export type NodeValidationError = NodeError['errors'][number]
185 :
186 2356 : export const INPUT_LEVEL_VALIDATION_ERROR_TYPES = new Set([
187 : 'required_input_missing',
188 : 'bad_linked_input',
189 : 'return_type_mismatch',
190 : 'invalid_input_type',
191 : 'value_smaller_than_min',
192 : 'value_bigger_than_max',
193 : 'value_not_in_list',
194 : 'custom_validation_failed',
195 : 'exception_during_inner_validation'
196 : ])
197 :
198 2356 : export const NODE_LEVEL_VALIDATION_ERROR_TYPES = new Set([
199 : 'PARTNER_NODE_DISABLED',
200 : 'exception_during_validation',
201 : 'dependency_cycle'
202 : ])
203 :
204 : /** Decodes the `[type, { min, max, ... }]` tuple the backend attaches to range errors. */
205 50 : export function getInputConfigBounds(error: NodeValidationError): {
206 : min?: unknown
207 : max?: unknown
208 : } {
209 50 : const config = error.extra_info?.input_config
210 50 : if (!Array.isArray(config)) return {}
211 :
212 10 : const bounds = config[1]
213 50 : if (!bounds || typeof bounds !== 'object') return {}
214 :
215 8 : const { min, max } = bounds as { min?: unknown; max?: unknown }
216 8 : return { min, max }
217 : }
218 :
219 111 : export function isImageNotLoadedValidationError(
220 : error: NodeValidationError
221 : ): boolean {
222 111 : return (
223 : error.type === 'custom_validation_failed' &&
224 : /invalid image file|\[errno 21\].*is a directory/i.test(
225 : [error.message, error.details].filter(Boolean).join('\n')
226 : )
227 : )
228 : }
229 :
230 : // Anything not input-level (including unknown types) is node-level.
231 61 : export function isNodeLevelValidationError(
232 : error: NodeValidationError
233 : ): boolean {
234 61 : return (
235 : !INPUT_LEVEL_VALIDATION_ERROR_TYPES.has(error.type) ||
236 : isImageNotLoadedValidationError(error)
237 : )
238 : }
239 :
240 : /**
241 : * Returns true if `value` still violates a recorded range constraint.
242 : * Pass errors already filtered to the target widget (by `input_name`).
243 : * `options` should contain the widget's configured `min` / `max`.
244 : *
245 : * Returns true (keeps the error) when a bound is unknown (`undefined`).
246 : */
247 1 : export function isValueStillOutOfRange(
248 : value: number,
249 : errors: NodeError['errors'],
250 : options: { min?: number; max?: number }
251 : ): boolean {
252 1 : const hasMaxError = errors.some((e) => e.type === 'value_bigger_than_max')
253 1 : const hasMinError = errors.some((e) => e.type === 'value_smaller_than_min')
254 :
255 1 : return (
256 : (hasMaxError && (options.max === undefined || value > options.max)) ||
257 : (hasMinError && (options.min === undefined || value < options.min))
258 : )
259 : }
|