feat: add elseif to ifelse node (#1378)

This commit is contained in:
heheer
2024-05-07 14:50:58 +08:00
committed by GitHub
parent 9e192c6d11
commit 2053bbdb1b
12 changed files with 756 additions and 423 deletions

View File

@@ -141,8 +141,7 @@ export enum NodeOutputKeyEnum {
// plugin
pluginStart = 'pluginStart',
if = 'IF',
else = 'ELSE'
ifElseResult = 'ifElseResult'
}
export enum VariableInputEnum {

View File

@@ -75,7 +75,7 @@ export type DispatchNodeResponseType = {
pluginDetail?: ChatHistoryItemResType[];
// if-else
ifElseResult?: 'IF' | 'ELSE';
ifElseResult?: string;
// tool
toolCallTokens?: number;

View File

@@ -23,14 +23,6 @@ export const IfElseNode: FlowNodeTemplateType = {
intro: '根据一定的条件,执行不同的分支。',
showStatus: true,
inputs: [
{
key: NodeInputKeyEnum.condition,
valueType: WorkflowIOValueTypeEnum.string,
label: '',
renderTypeList: [FlowNodeInputTypeEnum.hidden],
required: false,
value: 'AND' // AND, OR
},
{
key: NodeInputKeyEnum.ifElseList,
renderTypeList: [FlowNodeInputTypeEnum.hidden],
@@ -38,27 +30,25 @@ export const IfElseNode: FlowNodeTemplateType = {
label: '',
value: [
{
variable: undefined,
condition: undefined,
value: undefined
condition: 'AND', // AND, OR
list: [
{
variable: undefined,
condition: undefined,
value: undefined
}
]
}
]
}
],
outputs: [
{
id: NodeOutputKeyEnum.if,
key: NodeOutputKeyEnum.if,
label: 'IF',
valueType: WorkflowIOValueTypeEnum.any,
type: FlowNodeOutputTypeEnum.source
},
{
id: NodeOutputKeyEnum.else,
key: NodeOutputKeyEnum.else,
label: 'ELSE',
valueType: WorkflowIOValueTypeEnum.any,
type: FlowNodeOutputTypeEnum.source
id: NodeOutputKeyEnum.ifElseResult,
key: NodeOutputKeyEnum.ifElseResult,
label: 'IF ELSE',
valueType: WorkflowIOValueTypeEnum.string,
type: FlowNodeOutputTypeEnum.static
}
]
};

View File

@@ -2,8 +2,12 @@ import { ReferenceValueProps } from 'core/workflow/type/io';
import { VariableConditionEnum } from './constant';
export type IfElseConditionType = 'AND' | 'OR';
export type IfElseListItemType = {
export type ConditionListItemType = {
variable?: ReferenceValueProps;
condition?: VariableConditionEnum;
value?: string;
};
export type IfElseListItemType = {
condition: IfElseConditionType;
list: ConditionListItemType[];
};

View File

@@ -3,6 +3,7 @@ import { DispatchNodeResponseKeyEnum } from '@fastgpt/global/core/workflow/runti
import { DispatchNodeResultType } from '@fastgpt/global/core/workflow/runtime/type';
import { VariableConditionEnum } from '@fastgpt/global/core/workflow/template/system/ifElse/constant';
import {
ConditionListItemType,
IfElseConditionType,
IfElseListItemType
} from '@fastgpt/global/core/workflow/template/system/ifElse/type';
@@ -41,15 +42,13 @@ function checkCondition(condition: VariableConditionEnum, variableValue: any, va
return (operations[condition] || (() => false))();
}
export const dispatchIfElse = async (props: Props): Promise<DispatchNodeResultType<{}>> => {
const {
params,
runtimeNodes,
variables,
node: { nodeId }
} = props;
const { condition, ifElseList } = params;
const listResult = ifElseList.map((item) => {
function getResult(
condition: IfElseConditionType,
list: ConditionListItemType[],
variables: any,
runtimeNodes: any[]
) {
const listResult = list.map((item) => {
const { variable, condition: variableCondition, value } = item;
const variableValue = getReferenceVariableValue({
@@ -61,15 +60,40 @@ export const dispatchIfElse = async (props: Props): Promise<DispatchNodeResultTy
return checkCondition(variableCondition as VariableConditionEnum, variableValue, value || '');
});
const result = condition === 'AND' ? listResult.every(Boolean) : listResult.some(Boolean);
return condition === 'AND' ? listResult.every(Boolean) : listResult.some(Boolean);
}
export const dispatchIfElse = async (props: Props): Promise<DispatchNodeResultType<{}>> => {
const {
params,
runtimeNodes,
variables,
node: { nodeId }
} = props;
const { ifElseList } = params;
let res = 'ELSE';
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}`;
break;
}
}
const resArray = Array.from({ length: ifElseList.length + 1 }, (_, index) => {
const label = index < ifElseList.length ? `IF${index}` : 'ELSE';
return getHandleId(nodeId, 'source', label);
});
return {
[DispatchNodeResponseKeyEnum.nodeResponse]: {
totalPoints: 0,
ifElseResult: result ? 'IF' : 'ELSE'
ifElseResult: res
},
[DispatchNodeResponseKeyEnum.skipHandleId]: result
? [getHandleId(nodeId, 'source', 'ELSE')]
: [getHandleId(nodeId, 'source', 'IF')]
[DispatchNodeResponseKeyEnum.skipHandleId]: resArray.filter(
(item) => item !== getHandleId(nodeId, 'source', res)
)
};
};