Line data Source code
1 : import type { SerializedNodeId } from '@comfyorg/graph-model/nodeId'
2 : import type { MissingMediaGroup } from '@/platform/workflowErrors/missingMedia/types'
3 : import type { MissingModelGroup } from '@/platform/workflowErrors/missingModel/types'
4 : import type {
5 : AgentErrorContext,
6 : AgentErrorContextScope,
7 : AgentErrorContextSeverity,
8 : AgentErrorProblem
9 : } from '@/platform/workflowErrors/agentErrorContext'
10 :
11 : import type { MissingPackGroup, SwapNodeGroup } from './useErrorGroups'
12 : import type { ErrorCardData, ErrorGroup, ErrorItem } from './types'
13 :
14 : export interface MissingGroupContext {
15 : missingPackGroups: MissingPackGroup[]
16 : swapNodeGroups: SwapNodeGroup[]
17 : missingModelGroups: MissingModelGroup[]
18 : missingMediaGroups: MissingMediaGroup[]
19 : }
20 :
21 : interface BuildOptions {
22 : workflowName?: string
23 : }
24 :
25 7 : function problemMessage(error: ErrorItem): string {
26 7 : const base = error.message || error.displayMessage || error.displayTitle || ''
27 7 : if (error.displayItemLabel && !base.includes(error.displayItemLabel))
28 7 : return `${base} [input: ${error.displayItemLabel}]`.trim()
29 0 : return base.trim()
30 : }
31 :
32 7 : function executionProblems(cards: ErrorCardData[]): AgentErrorProblem[] {
33 7 : return cards.flatMap((card) =>
34 : card.errors.map((error) => ({
35 : category: error.isRuntimeError
36 : ? ('execution_failure' as const)
37 : : ('validation' as const),
38 : message: problemMessage(error),
39 : details: error.details ?? error.displayDetails,
40 : nodeId: card.nodeId ?? card.graphNodeId ?? card.rawNodeId,
41 : nodeType: card.title,
42 : nodeTitle: card.nodeTitle
43 : }))
44 : )
45 : }
46 :
47 : interface NodeUse {
48 : nodeId: SerializedNodeId
49 : widgetName: string
50 : }
51 :
52 58 : function usedBy(references: NodeUse[]): string {
53 58 : if (references.length === 1) return ` (widget ${references[0].widgetName})`
54 : const uses = references.map(
55 : (reference) => `node #${reference.nodeId} widget ${reference.widgetName}`
56 : )
57 7 : return ` (used by ${uses.join(', ')})`
58 : }
59 :
60 58 : function resourceProblem(
61 : category: 'missing_model' | 'missing_media',
62 : message: string,
63 : references: NodeUse[],
64 : nodeType: string | undefined
65 : ): AgentErrorProblem {
66 58 : if (references.length === 0) return { category, message, nodeType }
67 58 : const problem: AgentErrorProblem = {
68 : category,
69 : message: `${message}${usedBy(references)}`
70 : }
71 58 : if (references.length !== 1) return problem
72 51 : return { ...problem, nodeId: String(references[0].nodeId), nodeType }
73 : }
74 :
75 40 : function missingModelProblems(
76 : groups: MissingModelGroup[]
77 : ): AgentErrorProblem[] {
78 40 : return groups.flatMap((group) =>
79 : group.models.map((model) => {
80 40 : const directory = group.directory ? ` in ${group.directory}` : ''
81 40 : const [only] = model.referencingNodes
82 40 : const nodeType =
83 : model.referencingNodes.length === 1 &&
84 : String(only.nodeId) === String(model.representative.nodeId)
85 : ? model.representative.nodeType
86 : : undefined
87 40 : return resourceProblem(
88 : 'missing_model',
89 : `Missing model "${model.name}"${directory}`,
90 : model.referencingNodes,
91 : nodeType
92 : )
93 : })
94 : )
95 : }
96 :
97 16 : function missingMediaProblems(
98 : groups: MissingMediaGroup[]
99 : ): AgentErrorProblem[] {
100 16 : return groups.flatMap((group) =>
101 : group.items.map((item) =>
102 : resourceProblem(
103 : 'missing_media',
104 : `Missing ${group.mediaType} input "${item.name}"`,
105 : item.referencingNodes,
106 : item.referencingNodes[0]?.nodeType ?? item.representative.nodeType
107 : )
108 : )
109 : )
110 : }
111 :
112 48 : function missingNodeTypeProblems(
113 : groups: MissingPackGroup[] | SwapNodeGroup[],
114 : suffix: string
115 : ): AgentErrorProblem[] {
116 48 : return groups.flatMap((group) =>
117 : group.nodeTypes.map((nodeType) => {
118 57 : const type = typeof nodeType === 'string' ? nodeType : nodeType.type
119 57 : const nodeId =
120 : typeof nodeType === 'string' || nodeType.nodeId == null
121 : ? undefined
122 : : String(nodeType.nodeId)
123 57 : return {
124 : category: 'missing_node' as const,
125 : message: `Missing node type "${type}"${suffix}`,
126 : nodeId,
127 : nodeType: type
128 : }
129 : })
130 : )
131 : }
132 :
133 111 : function problemsForGroup(
134 : group: ErrorGroup,
135 : context: MissingGroupContext
136 : ): AgentErrorProblem[] {
137 111 : switch (group.type) {
138 : case 'execution':
139 7 : return executionProblems(group.cards)
140 : case 'missing_model':
141 40 : return missingModelProblems(context.missingModelGroups)
142 : case 'missing_media':
143 16 : return missingMediaProblems(context.missingMediaGroups)
144 : case 'missing_node':
145 29 : return missingNodeTypeProblems(
146 : context.missingPackGroups,
147 : ' is not installed'
148 : )
149 : case 'swap_nodes':
150 19 : return missingNodeTypeProblems(
151 : context.swapNodeGroups,
152 : ' (a replacement is available)'
153 : )
154 : }
155 : }
156 :
157 2 : function severityFor(group: ErrorGroup): AgentErrorContextSeverity {
158 2 : return group.severity === 'error' ? 'error' : 'warning'
159 : }
160 :
161 2 : export function buildGroupErrorContext(
162 : group: ErrorGroup,
163 : context: MissingGroupContext,
164 : options: BuildOptions = {}
165 : ): AgentErrorContext {
166 2 : const problems = problemsForGroup(group, context)
167 2 : return {
168 : scope: 'card',
169 : count: problems.length,
170 : problems,
171 : label: group.displayTitle,
172 : severity: severityFor(group),
173 : workflowName: options.workflowName
174 : }
175 : }
176 :
177 : interface BuildAllOptions extends BuildOptions {
178 : label: string
179 : scope?: AgentErrorContextScope
180 : }
181 :
182 107 : export function buildAllErrorContext(
183 : groups: ErrorGroup[],
184 : context: MissingGroupContext,
185 : options: BuildAllOptions
186 : ): AgentErrorContext {
187 107 : const problems = groups.flatMap((group) => problemsForGroup(group, context))
188 107 : const severity: AgentErrorContextSeverity = groups.some(
189 : (group) => group.severity === 'error'
190 : )
191 : ? 'error'
192 : : 'warning'
193 107 : return {
194 : scope: options.scope ?? 'all',
195 : count: problems.length,
196 : problems,
197 : label: options.label,
198 : severity,
199 : workflowName: options.workflowName
200 : }
201 : }
|