fix: workflow parallel limit in ifelse node (#8242)

This commit is contained in:
zxhlyh 2024-09-11 11:30:33 +08:00 committed by GitHub
parent 60913970dc
commit 91942e37ff
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 20 additions and 10 deletions

View File

@ -283,15 +283,12 @@ export const useWorkflow = () => {
return isUsed return isUsed
}, [isVarUsedInNodes]) }, [isVarUsedInNodes])
const checkParallelLimit = useCallback((nodeId: string) => { const checkParallelLimit = useCallback((nodeId: string, nodeHandle = 'source') => {
const { const {
getNodes,
edges, edges,
} = store.getState() } = store.getState()
const nodes = getNodes() const connectedEdges = edges.filter(edge => edge.source === nodeId && edge.sourceHandle === nodeHandle)
const currentNode = nodes.find(node => node.id === nodeId)! if (connectedEdges.length > PARALLEL_LIMIT - 1) {
const sourceNodeOutgoers = getOutgoers(currentNode, nodes, edges)
if (sourceNodeOutgoers.length > PARALLEL_LIMIT - 1) {
const { setShowTips } = workflowStore.getState() const { setShowTips } = workflowStore.getState()
setShowTips(t('workflow.common.parallelTip.limit', { num: PARALLEL_LIMIT })) setShowTips(t('workflow.common.parallelTip.limit', { num: PARALLEL_LIMIT }))
return false return false
@ -322,7 +319,7 @@ export const useWorkflow = () => {
return true return true
}, [t, workflowStore]) }, [t, workflowStore])
const isValidConnection = useCallback(({ source, target }: Connection) => { const isValidConnection = useCallback(({ source, sourceHandle, target }: Connection) => {
const { const {
edges, edges,
getNodes, getNodes,
@ -331,7 +328,7 @@ export const useWorkflow = () => {
const sourceNode: Node = nodes.find(node => node.id === source)! const sourceNode: Node = nodes.find(node => node.id === source)!
const targetNode: Node = nodes.find(node => node.id === target)! const targetNode: Node = nodes.find(node => node.id === target)!
if (!checkParallelLimit(source!)) if (!checkParallelLimit(source!, sourceHandle || 'source'))
return false return false
if (sourceNode.type === CUSTOM_NOTE_NODE || targetNode.type === CUSTOM_NOTE_NODE) if (sourceNode.type === CUSTOM_NOTE_NODE || targetNode.type === CUSTOM_NOTE_NODE)

View File

@ -1,6 +1,7 @@
import { import {
memo, memo,
useCallback, useCallback,
useState,
} from 'react' } from 'react'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
import { import {
@ -10,6 +11,7 @@ import {
useAvailableBlocks, useAvailableBlocks,
useNodesInteractions, useNodesInteractions,
useNodesReadOnly, useNodesReadOnly,
useWorkflow,
} from '@/app/components/workflow/hooks' } from '@/app/components/workflow/hooks'
import BlockSelector from '@/app/components/workflow/block-selector' import BlockSelector from '@/app/components/workflow/block-selector'
import type { import type {
@ -30,9 +32,11 @@ const Add = ({
isParallel, isParallel,
}: AddProps) => { }: AddProps) => {
const { t } = useTranslation() const { t } = useTranslation()
const [open, setOpen] = useState(false)
const { handleNodeAdd } = useNodesInteractions() const { handleNodeAdd } = useNodesInteractions()
const { nodesReadOnly } = useNodesReadOnly() const { nodesReadOnly } = useNodesReadOnly()
const { availableNextBlocks } = useAvailableBlocks(nodeData.type, nodeData.isInIteration) const { availableNextBlocks } = useAvailableBlocks(nodeData.type, nodeData.isInIteration)
const { checkParallelLimit } = useWorkflow()
const handleSelect = useCallback<OnSelectBlock>((type, toolDefaultValue) => { const handleSelect = useCallback<OnSelectBlock>((type, toolDefaultValue) => {
handleNodeAdd( handleNodeAdd(
@ -47,6 +51,13 @@ const Add = ({
) )
}, [nodeId, sourceHandle, handleNodeAdd]) }, [nodeId, sourceHandle, handleNodeAdd])
const handleOpenChange = useCallback((newOpen: boolean) => {
if (newOpen && !checkParallelLimit(nodeId, sourceHandle))
return
setOpen(newOpen)
}, [checkParallelLimit, nodeId, sourceHandle])
const renderTrigger = useCallback((open: boolean) => { const renderTrigger = useCallback((open: boolean) => {
return ( return (
<div <div
@ -73,6 +84,8 @@ const Add = ({
return ( return (
<BlockSelector <BlockSelector
open={open}
onOpenChange={handleOpenChange}
disabled={nodesReadOnly} disabled={nodesReadOnly}
onSelect={handleSelect} onSelect={handleSelect}
placement='top' placement='top'

View File

@ -132,9 +132,9 @@ export const NodeSourceHandle = memo(({
}, []) }, [])
const handleHandleClick = useCallback((e: MouseEvent) => { const handleHandleClick = useCallback((e: MouseEvent) => {
e.stopPropagation() e.stopPropagation()
if (checkParallelLimit(id)) if (checkParallelLimit(id, handleId))
setOpen(v => !v) setOpen(v => !v)
}, [checkParallelLimit, id]) }, [checkParallelLimit, id, handleId])
const handleSelect = useCallback((type: BlockEnum, toolDefaultValue?: ToolDefaultValue) => { const handleSelect = useCallback((type: BlockEnum, toolDefaultValue?: ToolDefaultValue) => {
handleNodeAdd( handleNodeAdd(
{ {