From 2a99e4635380a9b04c13ba2c2df053e11d926c04 Mon Sep 17 00:00:00 2001 From: heheer <71265218+newfish-cmyk@users.noreply.github.com> Date: Tue, 7 May 2024 17:16:33 +0800 Subject: [PATCH] fix: if else node (#1383) * fix: if else node * fix * fix --- .../template/system/ifElse/constant.ts | 5 ++ packages/global/core/workflow/utils.ts | 5 ++ .../core/workflow/dispatch/tools/runIfElse.ts | 13 ++-- .../Flow/nodes/NodeIfElse/ListItem.tsx | 64 ++++++++++--------- .../workflow/Flow/nodes/NodeIfElse/index.tsx | 7 +- projects/app/src/web/core/workflow/utils.ts | 25 ++++++++ 6 files changed, 81 insertions(+), 38 deletions(-) diff --git a/packages/global/core/workflow/template/system/ifElse/constant.ts b/packages/global/core/workflow/template/system/ifElse/constant.ts index 43dedbb59..49c000ef0 100644 --- a/packages/global/core/workflow/template/system/ifElse/constant.ts +++ b/packages/global/core/workflow/template/system/ifElse/constant.ts @@ -20,6 +20,11 @@ export enum VariableConditionEnum { lengthLessThan = 'lengthLessThan', lengthLessThanOrEqualTo = 'lengthLessThanOrEqualTo' } +export enum IfElseResultEnum { + IF = 'IF', + ELSE = 'ELSE', + ELSE_IF = 'ELSE IF' +} export const stringConditionList = [ { label: '为空', value: VariableConditionEnum.isEmpty }, diff --git a/packages/global/core/workflow/utils.ts b/packages/global/core/workflow/utils.ts index 9a010c823..d27d56a1a 100644 --- a/packages/global/core/workflow/utils.ts +++ b/packages/global/core/workflow/utils.ts @@ -15,6 +15,7 @@ import type { } from '../app/type'; import { EditorVariablePickerType } from '../../../web/components/common/Textarea/PromptEditor/type'; import { defaultWhisperConfig } from '../app/constants'; +import { IfElseResultEnum } from './template/system/ifElse/constant'; export const getHandleId = (nodeId: string, type: 'source' | 'target', key: string) => { return `${nodeId}-${type}-${key}`; @@ -136,3 +137,7 @@ export const formatEditorVariablePickerIcon = ( export const isReferenceValue = (value: any): boolean => { return Array.isArray(value) && value.length === 2 && typeof value[0] === 'string'; }; + +export const getElseIFLabel = (i: number) => { + return i === 0 ? IfElseResultEnum.IF : `${IfElseResultEnum.ELSE_IF} ${i}`; +}; diff --git a/packages/service/core/workflow/dispatch/tools/runIfElse.ts b/packages/service/core/workflow/dispatch/tools/runIfElse.ts index e4b2dd498..c9fda7201 100644 --- a/packages/service/core/workflow/dispatch/tools/runIfElse.ts +++ b/packages/service/core/workflow/dispatch/tools/runIfElse.ts @@ -1,14 +1,17 @@ import { NodeInputKeyEnum, NodeOutputKeyEnum } from '@fastgpt/global/core/workflow/constants'; import { DispatchNodeResponseKeyEnum } from '@fastgpt/global/core/workflow/runtime/constants'; import { DispatchNodeResultType } from '@fastgpt/global/core/workflow/runtime/type'; -import { VariableConditionEnum } from '@fastgpt/global/core/workflow/template/system/ifElse/constant'; +import { + IfElseResultEnum, + VariableConditionEnum +} from '@fastgpt/global/core/workflow/template/system/ifElse/constant'; import { ConditionListItemType, IfElseConditionType, IfElseListItemType } from '@fastgpt/global/core/workflow/template/system/ifElse/type'; import { ModuleDispatchProps } from '@fastgpt/global/core/workflow/type'; -import { getHandleId } from '@fastgpt/global/core/workflow/utils'; +import { getElseIFLabel, getHandleId } from '@fastgpt/global/core/workflow/utils'; import { getReferenceVariableValue } from '@fastgpt/global/core/workflow/runtime/utils'; type Props = ModuleDispatchProps<{ @@ -75,18 +78,18 @@ export const dispatchIfElse = async (props: Props): Promise => { } = props; const { ifElseList } = params; - let res = 'ELSE'; + let res = IfElseResultEnum.ELSE as string; for (let i = 0; i < ifElseList.length; i++) { const item = ifElseList[i]; const result = getResult(item.condition, item.list, variables, runtimeNodes); if (result) { - res = `IF${i}`; + res = getElseIFLabel(i); break; } } const resArray = Array.from({ length: ifElseList.length + 1 }, (_, index) => { - const label = index < ifElseList.length ? `IF${index}` : 'ELSE'; + const label = index < ifElseList.length ? getElseIFLabel(index) : IfElseResultEnum.ELSE; return getHandleId(nodeId, 'source', label); }); diff --git a/projects/app/src/components/core/workflow/Flow/nodes/NodeIfElse/ListItem.tsx b/projects/app/src/components/core/workflow/Flow/nodes/NodeIfElse/ListItem.tsx index 057623f6b..61c172e58 100644 --- a/projects/app/src/components/core/workflow/Flow/nodes/NodeIfElse/ListItem.tsx +++ b/projects/app/src/components/core/workflow/Flow/nodes/NodeIfElse/ListItem.tsx @@ -21,7 +21,7 @@ import React, { useMemo } from 'react'; import { WorkflowContext } from '../../../context'; import MySelect from '@fastgpt/web/components/common/MySelect'; import MyInput from '@/components/MyInput'; -import { getHandleId } from '@fastgpt/global/core/workflow/utils'; +import { getElseIFLabel, getHandleId } from '@fastgpt/global/core/workflow/utils'; import { SourceHandle } from '../render/Handle'; import { Position, useReactFlow } from 'reactflow'; import { getReferenceDataValueType } from '@/web/core/workflow/utils'; @@ -63,36 +63,40 @@ const ListItem = ({ > - - - + {ifElseList.length > 1 && ( + + + + )} - {conditionIndex === 0 ? 'IF' : 'ELSE IF'} + {getElseIFLabel(conditionIndex)} - { - onUpdateIfElseList( - ifElseList.map((ifElse, index) => { - if (index === conditionIndex) { - return { - ...ifElse, - condition: ifElse.condition === 'AND' ? 'OR' : 'AND' - }; - } - return ifElse; - }) - ); - }} - > - {conditionItem.condition} - - + {conditionItem.list?.length > 1 && ( + { + onUpdateIfElseList( + ifElseList.map((ifElse, index) => { + if (index === conditionIndex) { + return { + ...ifElse, + condition: ifElse.condition === 'AND' ? 'OR' : 'AND' + }; + } + return ifElse; + }) + ); + }} + > + {conditionItem.condition} + + + )} {ifElseList.length > 1 && ( diff --git a/projects/app/src/components/core/workflow/Flow/nodes/NodeIfElse/index.tsx b/projects/app/src/components/core/workflow/Flow/nodes/NodeIfElse/index.tsx index 3eade31a4..b1042ecdb 100644 --- a/projects/app/src/components/core/workflow/Flow/nodes/NodeIfElse/index.tsx +++ b/projects/app/src/components/core/workflow/Flow/nodes/NodeIfElse/index.tsx @@ -13,6 +13,7 @@ import { DragDropContext, DragStart, Draggable, DropResult, Droppable } from 're import { SourceHandle } from '../render/Handle'; import { getHandleId } from '@fastgpt/global/core/workflow/utils'; import ListItem from './ListItem'; +import { IfElseResultEnum } from '@fastgpt/global/core/workflow/template/system/ifElse/constant'; const NodeIfElse = ({ data, selected }: NodeProps) => { const { t } = useTranslation(); @@ -108,7 +109,7 @@ const NodeIfElse = ({ data, selected }: NodeProps) => { )} ))} - + {snapshot.isDraggingOver && } )} @@ -116,11 +117,11 @@ const NodeIfElse = ({ data, selected }: NodeProps) => { - ELSE + {IfElseResultEnum.ELSE} diff --git a/projects/app/src/web/core/workflow/utils.ts b/projects/app/src/web/core/workflow/utils.ts index 9cd549346..cd3318c08 100644 --- a/projects/app/src/web/core/workflow/utils.ts +++ b/projects/app/src/web/core/workflow/utils.ts @@ -25,6 +25,8 @@ import { import { getSystemVariables } from '../app/utils'; import { TFunction } from 'next-i18next'; import { ReferenceValueProps } from '@fastgpt/global/core/workflow/type/io'; +import { IfElseListItemType } from '@fastgpt/global/core/workflow/template/system/ifElse/type'; +import { VariableConditionEnum } from '@fastgpt/global/core/workflow/template/system/ifElse/constant'; export const nodeTemplate2FlowNode = ({ template, @@ -188,6 +190,29 @@ export const checkWorkflowNodeAndConnection = ({ continue; } + if (data.flowNodeType === FlowNodeTypeEnum.ifElseNode) { + const ifElseList: IfElseListItemType[] = inputs.find( + (input) => input.key === NodeInputKeyEnum.ifElseList + )?.value; + if ( + ifElseList.some((item) => { + return item.list.some((listItem) => { + return ( + listItem.variable === undefined || + listItem.condition === undefined || + (listItem.value === undefined && + listItem.condition !== VariableConditionEnum.isEmpty && + listItem.condition !== VariableConditionEnum.isNotEmpty) + ); + }); + }) + ) { + return [data.nodeId]; + } else { + continue; + } + } + // check node input if ( inputs.some((input) => {