Skip to content

Commit

Permalink
fix: if else node (#1383)
Browse files Browse the repository at this point in the history
* fix: if else node

* fix

* fix
  • Loading branch information
newfish-cmyk authored May 7, 2024
1 parent 8f9203c commit 2a99e46
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
5 changes: 5 additions & 0 deletions packages/global/core/workflow/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
Expand Down Expand Up @@ -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}`;
};
13 changes: 8 additions & 5 deletions packages/service/core/workflow/dispatch/tools/runIfElse.ts
Original file line number Diff line number Diff line change
@@ -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<{
Expand Down Expand Up @@ -75,18 +78,18 @@ export const dispatchIfElse = async (props: Props): Promise<Response> => {
} = 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);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -63,36 +63,40 @@ const ListItem = ({
>
<Container w={snapshot.isDragging ? '' : 'full'} className="nodrag">
<Flex mb={4} alignItems={'center'}>
<Box {...provided.dragHandleProps}>
<DragHandleIcon color={'blackAlpha.600'} />
</Box>
{ifElseList.length > 1 && (
<Box {...provided.dragHandleProps}>
<DragHandleIcon color={'blackAlpha.600'} />
</Box>
)}
<Box color={'black'} fontSize={'lg'} ml={2}>
{conditionIndex === 0 ? 'IF' : 'ELSE IF'}
{getElseIFLabel(conditionIndex)}
</Box>
<Flex
px={'2.5'}
color={'primary.600'}
fontWeight={'medium'}
alignItems={'center'}
cursor={'pointer'}
rounded={'md'}
onClick={() => {
onUpdateIfElseList(
ifElseList.map((ifElse, index) => {
if (index === conditionIndex) {
return {
...ifElse,
condition: ifElse.condition === 'AND' ? 'OR' : 'AND'
};
}
return ifElse;
})
);
}}
>
{conditionItem.condition}
<MyIcon ml={1} boxSize={5} name="change" />
</Flex>
{conditionItem.list?.length > 1 && (
<Flex
px={'2.5'}
color={'primary.600'}
fontWeight={'medium'}
alignItems={'center'}
cursor={'pointer'}
rounded={'md'}
onClick={() => {
onUpdateIfElseList(
ifElseList.map((ifElse, index) => {
if (index === conditionIndex) {
return {
...ifElse,
condition: ifElse.condition === 'AND' ? 'OR' : 'AND'
};
}
return ifElse;
})
);
}}
>
{conditionItem.condition}
<MyIcon ml={1} boxSize={5} name="change" />
</Flex>
)}
<Box flex={1}></Box>
{ifElseList.length > 1 && (
<MyIcon
Expand Down Expand Up @@ -257,7 +261,7 @@ const ListItem = ({
{!snapshot.isDragging && (
<SourceHandle
nodeId={nodeId}
handleId={getHandleId(nodeId, 'source', 'IF' + conditionIndex.toString())}
handleId={getHandleId(nodeId, 'source', getElseIFLabel(conditionIndex))}
position={Position.Right}
translate={[18, 0]}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<FlowNodeItemType>) => {
const { t } = useTranslation();
Expand Down Expand Up @@ -108,19 +109,19 @@ const NodeIfElse = ({ data, selected }: NodeProps<FlowNodeItemType>) => {
)}
</Draggable>
))}
<Box height={draggingItemHeight} />
{snapshot.isDraggingOver && <Box height={draggingItemHeight} />}
</Box>
)}
</Droppable>
</DragDropContext>
<Container position={'relative'}>
<Flex alignItems={'center'}>
<Box color={'black'} fontSize={'lg'} ml={2}>
ELSE
{IfElseResultEnum.ELSE}
</Box>
<SourceHandle
nodeId={nodeId}
handleId={getHandleId(nodeId, 'source', 'ELSE')}
handleId={getHandleId(nodeId, 'source', IfElseResultEnum.ELSE)}
position={Position.Right}
translate={[26, 0]}
/>
Expand Down
25 changes: 25 additions & 0 deletions projects/app/src/web/core/workflow/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) => {
Expand Down

0 comments on commit 2a99e46

Please sign in to comment.