mirror of
https://github.com/langgenius/dify.git
synced 2026-05-04 09:28:04 +08:00
fix: checklist
This commit is contained in:
@ -1,9 +1,13 @@
|
||||
import {
|
||||
Position,
|
||||
getConnectedEdges,
|
||||
getOutgoers,
|
||||
} from 'reactflow'
|
||||
import dagre from 'dagre'
|
||||
import { cloneDeep } from 'lodash-es'
|
||||
import {
|
||||
cloneDeep,
|
||||
uniqBy,
|
||||
} from 'lodash-es'
|
||||
import type {
|
||||
Edge,
|
||||
Node,
|
||||
@ -186,3 +190,41 @@ export const generateNewNode = ({ data, position, id }: Pick<Node, 'data' | 'pos
|
||||
sourcePosition: Position.Right,
|
||||
} as Node
|
||||
}
|
||||
|
||||
export const getValidTreeNodes = (nodes: Node[], edges: Edge[]) => {
|
||||
const startNode = nodes.find(node => node.data.type === BlockEnum.Start)
|
||||
|
||||
if (!startNode) {
|
||||
return {
|
||||
validNodes: [],
|
||||
maxDepth: 0,
|
||||
}
|
||||
}
|
||||
|
||||
const list: Node[] = [startNode]
|
||||
let maxDepth = 1
|
||||
|
||||
const traverse = (root: Node, depth: number) => {
|
||||
if (depth > maxDepth)
|
||||
maxDepth = depth
|
||||
|
||||
const outgoers = getOutgoers(root, nodes, edges)
|
||||
|
||||
if (outgoers.length) {
|
||||
outgoers.forEach((outgoer) => {
|
||||
list.push(outgoer)
|
||||
traverse(outgoer, depth + 1)
|
||||
})
|
||||
}
|
||||
else {
|
||||
list.push(root)
|
||||
}
|
||||
}
|
||||
|
||||
traverse(startNode, maxDepth)
|
||||
|
||||
return {
|
||||
validNodes: uniqBy(list, 'id'),
|
||||
maxDepth,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user