From 7c9081a8fcf6871722a943461a77b313abedd3d4 Mon Sep 17 00:00:00 2001 From: StyleZhang Date: Fri, 30 Aug 2024 13:44:01 +0800 Subject: [PATCH] fix --- web/app/components/workflow/constants.ts | 1 + .../components/workflow/hooks/use-workflow.ts | 62 ++++++++++++++++--- web/app/components/workflow/limit-tips.tsx | 7 +++ .../nodes/_base/components/node-handle.tsx | 17 ++--- web/app/components/workflow/utils.ts | 1 + web/i18n/en-US/workflow.ts | 1 + web/i18n/zh-Hans/workflow.ts | 1 + 7 files changed, 70 insertions(+), 20 deletions(-) diff --git a/web/app/components/workflow/constants.ts b/web/app/components/workflow/constants.ts index cc86b56df5..6a4629e9c8 100644 --- a/web/app/components/workflow/constants.ts +++ b/web/app/components/workflow/constants.ts @@ -327,6 +327,7 @@ export const ITERATION_PADDING = { left: 16, } export const PARALLEL_LIMIT = 10 +export const PARALLEL_DEPTH_LIMIT = 3 export const RETRIEVAL_OUTPUT_STRUCT = `{ "content": "", diff --git a/web/app/components/workflow/hooks/use-workflow.ts b/web/app/components/workflow/hooks/use-workflow.ts index 74cbf15e4a..f7ef4a807a 100644 --- a/web/app/components/workflow/hooks/use-workflow.ts +++ b/web/app/components/workflow/hooks/use-workflow.ts @@ -30,6 +30,7 @@ import { useWorkflowStore, } from '../store' import { + // PARALLEL_DEPTH_LIMIT, PARALLEL_LIMIT, SUPPORT_OUTPUT_VARS_NODE, } from '../constants' @@ -279,6 +280,53 @@ export const useWorkflow = () => { return isUsed }, [isVarUsedInNodes]) + const checkParallelLimit = useCallback((nodeId: string) => { + const { + getNodes, + edges, + } = store.getState() + const nodes = getNodes() + const currentNode = nodes.find(node => node.id === nodeId)! + const sourceNodeOutgoers = getOutgoers(currentNode, nodes, edges) + if (sourceNodeOutgoers.length > PARALLEL_LIMIT - 1) { + const { setShowTips } = workflowStore.getState() + setShowTips(t('workflow.common.parallelTip.limit', { num: PARALLEL_LIMIT })) + return false + } + // if (sourceNodeOutgoers.length > 0) { + // let hasOverDepth = false + // let parallelDepth = 1 + // const traverse = (root: Node, depth: number) => { + // if (depth > PARALLEL_DEPTH_LIMIT) { + // hasOverDepth = true + // return + // } + // if (depth > parallelDepth) + // parallelDepth = depth + + // const incomerNodes = getIncomers(root, nodes, edges) + + // if (incomerNodes.length) { + // incomerNodes.forEach((incomer) => { + // const incomerOutgoers = getOutgoers(incomer, nodes, edges) + + // if (incomerOutgoers.length > 1) + // traverse(incomer, depth + 1) + // else + // traverse(incomer, depth) + // }) + // } + // } + // traverse(currentNode, parallelDepth) + // if (hasOverDepth) { + // const { setShowTips } = workflowStore.getState() + // setShowTips(t('workflow.common.parallelTip.depthLimit', { num: PARALLEL_DEPTH_LIMIT })) + // return false + // } + // } + return true + }, [store, workflowStore, t]) + const isValidConnection = useCallback(({ source, target }: Connection) => { const { edges, @@ -288,17 +336,12 @@ export const useWorkflow = () => { const sourceNode: Node = nodes.find(node => node.id === source)! const targetNode: Node = nodes.find(node => node.id === target)! + if (!checkParallelLimit(source!)) + return false + if (sourceNode.type === CUSTOM_NOTE_NODE || targetNode.type === CUSTOM_NOTE_NODE) return false - const sourceNodeOutgoers = getOutgoers(sourceNode, nodes, edges) - - if (sourceNodeOutgoers.length > PARALLEL_LIMIT - 1) { - const { setShowTips } = workflowStore.getState() - setShowTips(t('workflow.common.parallelTip.limit', { num: PARALLEL_LIMIT })) - return false - } - if (sourceNode && targetNode) { const sourceNodeAvailableNextNodes = nodesExtraData[sourceNode.data.type].availableNextNodes const targetNodeAvailablePrevNodes = [...nodesExtraData[targetNode.data.type].availablePrevNodes, BlockEnum.Start] @@ -325,7 +368,7 @@ export const useWorkflow = () => { } return !hasCycle(targetNode) - }, [store, nodesExtraData]) + }, [store, nodesExtraData, checkParallelLimit]) const formatTimeFromNow = useCallback((time: number) => { return dayjs(time).locale(locale === 'zh-Hans' ? 'zh-cn' : locale).fromNow() @@ -348,6 +391,7 @@ export const useWorkflow = () => { isVarUsedInNodes, removeUsedVarInNodes, isNodeVarsUsedInNodes, + checkParallelLimit, isValidConnection, formatTimeFromNow, getNode, diff --git a/web/app/components/workflow/limit-tips.tsx b/web/app/components/workflow/limit-tips.tsx index 8fd1511609..8e90ff280c 100644 --- a/web/app/components/workflow/limit-tips.tsx +++ b/web/app/components/workflow/limit-tips.tsx @@ -14,6 +14,12 @@ const LimitTips = () => { return (
+
@@ -21,6 +27,7 @@ const LimitTips = () => { {showTips}
setShowTips('')} > diff --git a/web/app/components/workflow/nodes/_base/components/node-handle.tsx b/web/app/components/workflow/nodes/_base/components/node-handle.tsx index cbee6837c9..bcd03d6a5e 100644 --- a/web/app/components/workflow/nodes/_base/components/node-handle.tsx +++ b/web/app/components/workflow/nodes/_base/components/node-handle.tsx @@ -19,12 +19,11 @@ import { useIsChatMode, useNodesInteractions, useNodesReadOnly, + useWorkflow, } from '../../../hooks' import { useStore, - useWorkflowStore, } from '../../../store' -import { PARALLEL_LIMIT } from '../../../constants' import Tooltip from '@/app/components/base/tooltip' type NodeHandleProps = { @@ -119,13 +118,13 @@ export const NodeSourceHandle = memo(({ }: NodeHandleProps) => { const { t } = useTranslation() const notInitialWorkflow = useStore(s => s.notInitialWorkflow) - const workflowStore = useWorkflowStore() const [open, setOpen] = useState(false) const { handleNodeAdd } = useNodesInteractions() const { getNodesReadOnly } = useNodesReadOnly() const { availableNextBlocks } = useAvailableBlocks(data.type, data.isInIteration) const isConnectable = !!availableNextBlocks.length const isChatMode = useIsChatMode() + const { checkParallelLimit } = useWorkflow() const connected = data._connectedSourceHandleIds?.includes(handleId) const handleOpenChange = useCallback((v: boolean) => { @@ -133,13 +132,9 @@ export const NodeSourceHandle = memo(({ }, []) const handleHandleClick = useCallback((e: MouseEvent) => { e.stopPropagation() - if (data._connectedSourceHandleIds!.length > PARALLEL_LIMIT - 1) { - const { setShowTips } = workflowStore.getState() - setShowTips(t('workflow.common.parallelTip.limit', { num: PARALLEL_LIMIT })) - return - } - setOpen(v => !v) - }, [data._connectedSourceHandleIds]) + if (checkParallelLimit(id)) + setOpen(v => !v) + }, [checkParallelLimit, id]) const handleSelect = useCallback((type: BlockEnum, toolDefaultValue?: ToolDefaultValue) => { handleNodeAdd( { @@ -156,7 +151,7 @@ export const NodeSourceHandle = memo(({ useEffect(() => { if (notInitialWorkflow && data.type === BlockEnum.Start && !isChatMode) setOpen(true) - }, [notInitialWorkflow, data.type]) + }, [notInitialWorkflow, data.type, isChatMode]) return (