Implementation:FlowiseAI Flowise IsValidConnectionAgentflowV2
Appearance
| Property | Value |
|---|---|
| Page ID | FlowiseAI_Flowise_IsValidConnectionAgentflowV2 |
| Source Repository | FlowiseAI/Flowise |
| Source File | packages/ui/src/utils/genericHelper.js
|
| Domain | Graph Theory, DAG Validation, Visual Flow Editor |
| Last Updated | 2026-02-12 14:00 GMT |
Overview
Implementation of the edge connection validation function for agent flow V2 graphs. This function prevents self-connections and cycles by using depth-first search to detect whether a proposed edge would violate the DAG constraint.
Code Reference
Source Location
- File:
packages/ui/src/utils/genericHelper.js - Lines: 451-506
- Repository: FlowiseAI/Flowise
Signature
export const isValidConnectionAgentflowV2 = (connection, reactFlowInstance) => {
const source = connection.source
const target = connection.target
// Prevent self connections
if (source === target) {
return false
}
// Check if this connection would create a cycle in the graph
if (wouldCreateCycle(source, target, reactFlowInstance)) {
return false
}
return true
}
// Function to check if a new connection would create a cycle
const wouldCreateCycle = (sourceId, targetId, reactFlowInstance) => {
// The most direct cycle check: if target connects back to source
if (sourceId === targetId) {
return true
}
// Build directed graph from existing edges
const graph = {}
const edges = reactFlowInstance.getEdges()
// Initialize graph
edges.forEach((edge) => {
if (!graph[edge.source]) graph[edge.source] = []
graph[edge.source].push(edge.target)
})
// Check if there's a path from target to source (which would create a cycle when we add source -> target)
const visited = new Set()
function hasPath(current, destination) {
if (current === destination) return true
if (visited.has(current)) return false
visited.add(current)
const neighbors = graph[current] || []
for (const neighbor of neighbors) {
if (hasPath(neighbor, destination)) {
return true
}
}
return false
}
// If there's a path from target to source, adding an edge from source to target will create a cycle
return hasPath(targetId, sourceId)
}
Import
import { isValidConnectionAgentflowV2 } from '@/utils/genericHelper'
I/O Contract
Inputs
| Parameter | Type | Description |
|---|---|---|
connection |
object |
The proposed connection object from ReactFlow's onConnect or isValidConnection callback.
|
connection.source |
string |
The ID of the source node (where the edge originates). |
connection.target |
string |
The ID of the target node (where the edge terminates). |
reactFlowInstance |
ReactFlowInstance |
The ReactFlow instance providing access to the current graph state via getEdges().
|
Outputs
| Type | Description |
|---|---|
boolean |
Returns true if the connection is valid (no self-connection and no cycle would be created). Returns false if the connection would create a self-loop or a cycle.
|
Usage Examples
Using with ReactFlow isValidConnection prop
import { isValidConnectionAgentflowV2 } from '@/utils/genericHelper'
// In a ReactFlow canvas component
<ReactFlow
nodes={nodes}
edges={edges}
isValidConnection={(connection) =>
isValidConnectionAgentflowV2(connection, reactFlowInstance)
}
onConnect={onConnect}
/>
Validation flow walkthrough
// Given existing edges: A -> B, B -> C
// Proposed connection: C -> A (would create cycle A -> B -> C -> A)
const connection = { source: 'C', target: 'A' }
const isValid = isValidConnectionAgentflowV2(connection, reactFlowInstance)
// isValid === false (DFS finds path from A to C, so adding C -> A creates cycle)
// Proposed connection: A -> C (valid, no cycle)
const connection2 = { source: 'A', target: 'C' }
const isValid2 = isValidConnectionAgentflowV2(connection2, reactFlowInstance)
// isValid2 === true (no path from C back to A in current graph... wait, B->C exists)
// Actually: path from C to A? No edges from C to anything. So isValid2 === true.
Self-connection rejection
const connection = { source: 'nodeA', target: 'nodeA' }
const isValid = isValidConnectionAgentflowV2(connection, reactFlowInstance)
// isValid === false (self-connection detected immediately)
Algorithm Details
The implementation consists of two functions:
1. isValidConnectionAgentflowV2 (exported, public):
- Extracts source and target from the connection object.
- Performs the self-connection check as an early exit.
- Delegates cycle detection to
wouldCreateCycle.
2. wouldCreateCycle (private, module-scoped):
- Redundantly checks for self-connection (defensive programming).
- Builds an adjacency list from the current edges via
reactFlowInstance.getEdges(). - Runs a recursive DFS from
targetIdlooking forsourceId. - Uses a
visitedset to prevent revisiting nodes and avoid infinite recursion.
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment