Removed duplicates from loop state

This commit is contained in:
Emir Karabeg
2025-02-10 16:12:46 -08:00
parent 3ba2164d64
commit f42bfdfd1b
2 changed files with 12 additions and 5 deletions

View File

@@ -194,10 +194,10 @@ export const useWorkflowStore = create<WorkflowStoreWithHistory>()(
const newEdges = [...get().edges, newEdge]
const { hasCycle, path } = detectCycle(newEdges, edge.source)
// Create new loops state
// Only create a loop if we have a valid cycle with at least 2 unique nodes
const newLoops = { ...get().loops }
if (hasCycle) {
if (hasCycle && path.length > 1) {
const loopId = crypto.randomUUID()
newLoops[loopId] = {
id: loopId,

View File

@@ -43,16 +43,23 @@ export function detectCycle(edges: Edge[], startNode: string): { hasCycle: boole
// Perform DFS and construct cycle path if found
const hasCycle = dfs(startNode)
// If cycle found, construct the path
// If cycle found, construct the path and ensure no duplicates
const cyclePath: string[] = []
if (hasCycle) {
let current = startNode
const seenNodes = new Set<string>()
do {
cyclePath.unshift(current)
// Only add node if we haven't seen it before
if (!seenNodes.has(current)) {
cyclePath.unshift(current)
seenNodes.add(current)
}
current = pathMap.get(current)!
} while (current !== startNode && current !== undefined)
if (current === startNode) {
// Add starting node only if it's not already in the path
if (current === startNode && !seenNodes.has(startNode)) {
cyclePath.unshift(startNode)
}
}