(nodes: FlowNode[], edges: FlowEdge[], availableNodes?: NodeDataSchema[])
| 10 | * Validate the flow structure |
| 11 | */ |
| 12 | export function validateFlow(nodes: FlowNode[], edges: FlowEdge[], availableNodes?: NodeDataSchema[]): ValidationResult { |
| 13 | const errors: ValidationError[] = [] |
| 14 | |
| 15 | // Check for empty flow |
| 16 | if (nodes.length === 0) { |
| 17 | errors.push({ |
| 18 | message: 'Flow is empty - add at least one node', |
| 19 | type: 'error' |
| 20 | }) |
| 21 | return { valid: false, errors } |
| 22 | } |
| 23 | |
| 24 | // Check for start node |
| 25 | const startNode = nodes.find((n) => n.data.name === 'startAgentflow') |
| 26 | if (!startNode) { |
| 27 | errors.push({ |
| 28 | message: 'Flow must have a start node', |
| 29 | type: 'error' |
| 30 | }) |
| 31 | } |
| 32 | |
| 33 | // Check for multiple start nodes |
| 34 | const startNodes = nodes.filter((n) => n.data.name === 'startAgentflow') |
| 35 | if (startNodes.length > 1) { |
| 36 | errors.push({ |
| 37 | message: 'Flow can only have one start node', |
| 38 | type: 'error' |
| 39 | }) |
| 40 | } |
| 41 | |
| 42 | // Check for disconnected nodes (matching server-side pattern) |
| 43 | const connectedNodes = new Set<string>() |
| 44 | edges.forEach((edge) => { |
| 45 | connectedNodes.add(edge.source) |
| 46 | connectedNodes.add(edge.target) |
| 47 | }) |
| 48 | |
| 49 | const nonStickyNodes = nodes.filter((n) => n.data.name !== 'stickyNoteAgentflow') |
| 50 | nonStickyNodes.forEach((node) => { |
| 51 | if (!connectedNodes.has(node.id)) { |
| 52 | errors.push({ |
| 53 | nodeId: node.id, |
| 54 | message: 'This node is not connected to anything', |
| 55 | type: 'warning' |
| 56 | }) |
| 57 | } |
| 58 | }) |
| 59 | |
| 60 | // Check for cycles (should be handled during connection, but double-check) |
| 61 | const hasCycle = detectCycle(nodes, edges) |
| 62 | if (hasCycle) { |
| 63 | errors.push({ |
| 64 | message: 'Flow contains a cycle - this may cause infinite loops', |
| 65 | type: 'error' |
| 66 | }) |
| 67 | } |
| 68 | |
| 69 | // Validate each node's inputs |
no test coverage detected