(
nodeDependencies: INodeDependencies,
graph: INodeDirectedGraph,
allNodes: IReactFlowNode[]
)
| 293 | * @param {IReactFlowNode[]} allNodes |
| 294 | */ |
| 295 | export const getEndingNodes = ( |
| 296 | nodeDependencies: INodeDependencies, |
| 297 | graph: INodeDirectedGraph, |
| 298 | allNodes: IReactFlowNode[] |
| 299 | ): IReactFlowNode[] => { |
| 300 | const endingNodeIds: string[] = [] |
| 301 | Object.keys(graph).forEach((nodeId) => { |
| 302 | if (Object.keys(nodeDependencies).length === 1) { |
| 303 | endingNodeIds.push(nodeId) |
| 304 | } else if (!graph[nodeId].length && nodeDependencies[nodeId] > 0) { |
| 305 | endingNodeIds.push(nodeId) |
| 306 | } |
| 307 | }) |
| 308 | |
| 309 | let endingNodes = allNodes.filter((nd) => endingNodeIds.includes(nd.id)) |
| 310 | |
| 311 | // If there are multiple endingnodes, the failed ones will be automatically ignored. |
| 312 | // And only ensure that at least one can pass the verification. |
| 313 | const verifiedEndingNodes: typeof endingNodes = [] |
| 314 | let error: InternalFlowiseError | null = null |
| 315 | for (const endingNode of endingNodes) { |
| 316 | const endingNodeData = endingNode.data |
| 317 | if (!endingNodeData) { |
| 318 | error = new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Ending node ${endingNode.id} data not found`) |
| 319 | |
| 320 | continue |
| 321 | } |
| 322 | |
| 323 | const isEndingNode = endingNodeData?.outputs?.output === 'EndingNode' |
| 324 | |
| 325 | if (!isEndingNode) { |
| 326 | if ( |
| 327 | endingNodeData && |
| 328 | endingNodeData.category !== 'Chains' && |
| 329 | endingNodeData.category !== 'Agents' && |
| 330 | endingNodeData.category !== 'Engine' && |
| 331 | endingNodeData.category !== 'Multi Agents' && |
| 332 | endingNodeData.category !== 'Sequential Agents' |
| 333 | ) { |
| 334 | error = new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Ending node must be either a Chain or Agent or Engine`) |
| 335 | continue |
| 336 | } |
| 337 | } |
| 338 | verifiedEndingNodes.push(endingNode) |
| 339 | } |
| 340 | |
| 341 | if (verifiedEndingNodes.length > 0) { |
| 342 | return verifiedEndingNodes |
| 343 | } |
| 344 | |
| 345 | if (endingNodes.length === 0 || error === null) { |
| 346 | error = new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Ending nodes not found`) |
| 347 | } |
| 348 | |
| 349 | throw error |
| 350 | } |
| 351 | |
| 352 | /** |
no outgoing calls
no test coverage detected