|
|
|
@@ -1,6 +1,7 @@ |
|
|
|
import { |
|
|
|
Connection, |
|
|
|
Edge, |
|
|
|
getOutgoers, |
|
|
|
Node, |
|
|
|
Position, |
|
|
|
ReactFlowInstance, |
|
|
|
@@ -15,9 +16,6 @@ import { get, lowerFirst, omit } from 'lodash'; |
|
|
|
import { UseFormReturn } from 'react-hook-form'; |
|
|
|
import { useTranslation } from 'react-i18next'; |
|
|
|
import { |
|
|
|
NodeMap, |
|
|
|
Operator, |
|
|
|
RestrictedUpstreamMap, |
|
|
|
initialAgentValues, |
|
|
|
initialAkShareValues, |
|
|
|
initialArXivValues, |
|
|
|
@@ -57,6 +55,9 @@ import { |
|
|
|
initialWenCaiValues, |
|
|
|
initialWikipediaValues, |
|
|
|
initialYahooFinanceValues, |
|
|
|
NodeMap, |
|
|
|
Operator, |
|
|
|
RestrictedUpstreamMap, |
|
|
|
} from './constant'; |
|
|
|
import useGraphStore, { RFState } from './store'; |
|
|
|
import { |
|
|
|
@@ -333,9 +334,8 @@ export const useHandleFormValuesChange = ( |
|
|
|
}; |
|
|
|
|
|
|
|
export const useValidateConnection = () => { |
|
|
|
const { getOperatorTypeFromId, getParentIdById } = useGraphStore( |
|
|
|
(state) => state, |
|
|
|
); |
|
|
|
const { getOperatorTypeFromId, getParentIdById, edges, nodes } = |
|
|
|
useGraphStore((state) => state); |
|
|
|
|
|
|
|
const isSameNodeChild = useCallback( |
|
|
|
(connection: Connection | Edge) => { |
|
|
|
@@ -349,6 +349,27 @@ export const useValidateConnection = () => { |
|
|
|
[getParentIdById], |
|
|
|
); |
|
|
|
|
|
|
|
const hasCanvasCycle = useCallback( |
|
|
|
(connection: Connection | Edge) => { |
|
|
|
const target = nodes.find((node) => node.id === connection.target); |
|
|
|
const hasCycle = (node: RAGFlowNodeType, visited = new Set()) => { |
|
|
|
if (visited.has(node.id)) return false; |
|
|
|
|
|
|
|
visited.add(node.id); |
|
|
|
|
|
|
|
for (const outgoer of getOutgoers(node, nodes, edges)) { |
|
|
|
if (outgoer.id === connection.source) return true; |
|
|
|
if (hasCycle(outgoer, visited)) return true; |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
if (target?.id === connection.source) return false; |
|
|
|
|
|
|
|
return target ? !hasCycle(target) : false; |
|
|
|
}, |
|
|
|
[edges, nodes], |
|
|
|
); |
|
|
|
|
|
|
|
// restricted lines cannot be connected successfully. |
|
|
|
const isValidConnection = useCallback( |
|
|
|
(connection: Connection | Edge) => { |
|
|
|
@@ -365,10 +386,11 @@ export const useValidateConnection = () => { |
|
|
|
RestrictedUpstreamMap[ |
|
|
|
getOperatorTypeFromId(connection.source) as Operator |
|
|
|
]?.every((x) => x !== getOperatorTypeFromId(connection.target)) && |
|
|
|
isSameNodeChild(connection); |
|
|
|
isSameNodeChild(connection) && |
|
|
|
hasCanvasCycle(connection); |
|
|
|
return ret; |
|
|
|
}, |
|
|
|
[getOperatorTypeFromId, isSameNodeChild], |
|
|
|
[getOperatorTypeFromId, hasCanvasCycle, isSameNodeChild], |
|
|
|
); |
|
|
|
|
|
|
|
return isValidConnection; |