(nodeId: string, nodes: FlowNode[], edges: FlowEdge[], includeStart = false)
| 37 | * @param includeStart When true, include `startAgentflow` in the results. |
| 38 | */ |
| 39 | export function getUpstreamNodes(nodeId: string, nodes: FlowNode[], edges: FlowEdge[], includeStart = false): FlowNode[] { |
| 40 | const collected = new Set<string>() |
| 41 | // Never include the queried node itself (prevents self-reference in cycles) |
| 42 | collected.add(nodeId) |
| 43 | |
| 44 | function collect(targetId: string) { |
| 45 | // In AgentFlow V2, targetHandle === targetNodeId for node-level connections |
| 46 | const inputEdges = edges.filter((e) => e.target === targetId) |
| 47 | |
| 48 | for (const edge of inputEdges) { |
| 49 | if (collected.has(edge.source)) continue |
| 50 | |
| 51 | const parentNode = nodes.find((n) => n.id === edge.source) |
| 52 | if (!parentNode) continue |
| 53 | |
| 54 | // Exclude startAgentflow unless explicitly requested |
| 55 | if (parentNode.data.name === 'startAgentflow' && !includeStart) { |
| 56 | continue |
| 57 | } |
| 58 | |
| 59 | collected.add(parentNode.id) |
| 60 | // Recurse to collect the full ancestor chain |
| 61 | collect(parentNode.id) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | collect(nodeId) |
| 66 | |
| 67 | // Also traverse the parentNode property (for nodes inside iteration groups) |
| 68 | const targetNode = nodes.find((n) => n.id === nodeId) |
| 69 | if (targetNode?.parentNode) { |
| 70 | collect(targetNode.parentNode) |
| 71 | } |
| 72 | |
| 73 | return nodes.filter((n) => n.id !== nodeId && collected.has(n.id)) |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Pattern matching input names that hold state key-value arrays. |
no test coverage detected