MCPcopy Create free account
hub / github.com/FlowiseAI/Flowise / getUpstreamNodes

Function getUpstreamNodes

packages/agentflow/src/core/utils/variableUtils.ts:39–74  ·  view source on GitHub ↗
(nodeId: string, nodes: FlowNode[], edges: FlowEdge[], includeStart = false)

Source from the content-addressed store, hash-verified

37 * @param includeStart When true, include `startAgentflow` in the results.
38 */
39export 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.

Callers 3

useAvailableVariablesFunction · 0.90

Calls 2

collectFunction · 0.85
addMethod · 0.45

Tested by

no test coverage detected