Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: loop node #2675

Merged
merged 7 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions packages/global/core/workflow/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export enum WorkflowIOValueTypeEnum {
arrayNumber = 'arrayNumber',
arrayBoolean = 'arrayBoolean',
arrayObject = 'arrayObject',
arrayAny = 'arrayAny',
any = 'any',

chatHistory = 'chatHistory',
Expand Down Expand Up @@ -135,7 +136,17 @@ export enum NodeInputKeyEnum {
fileUrlList = 'fileUrlList',

// user select
userSelectOptions = 'userSelectOptions'
userSelectOptions = 'userSelectOptions',

// loop
loopInputArray = 'loopInputArray',
loopFlow = 'loopFlow',

// loop start
loopArrayElement = 'loopArrayElement',

// loop end
loopOutputArrayElement = 'loopOutputArrayElement'
}

export enum NodeOutputKeyEnum {
Expand Down Expand Up @@ -178,7 +189,13 @@ export enum NodeOutputKeyEnum {
ifElseResult = 'ifElseResult',

//user select
selectResult = 'selectResult'
selectResult = 'selectResult',

// loop
loopArray = 'loopArray',

// loop start
loopArrayElement = 'loopArrayElement'
}

export enum VariableInputEnum {
Expand Down
9 changes: 8 additions & 1 deletion packages/global/core/workflow/node/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@ export enum FlowNodeTypeEnum {
textEditor = 'textEditor',
customFeedback = 'customFeedback',
readFiles = 'readFiles',
userSelect = 'userSelect'
userSelect = 'userSelect',
loop = 'loop',
loopStart = 'loopStart',
loopEnd = 'loopEnd'
}

// node IO value type
Expand Down Expand Up @@ -162,6 +165,10 @@ export const FlowValueTypeMap = {
label: 'array<object>',
value: WorkflowIOValueTypeEnum.arrayObject
},
[WorkflowIOValueTypeEnum.arrayAny]: {
label: 'array',
value: WorkflowIOValueTypeEnum.arrayAny
},
[WorkflowIOValueTypeEnum.any]: {
label: 'any',
value: WorkflowIOValueTypeEnum.any
Expand Down
9 changes: 9 additions & 0 deletions packages/global/core/workflow/runtime/type.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,15 @@ export type DispatchNodeResponseType = {

// update var
updateVarResult?: any[];

// loop
loopResult?: any[];
loopInput?: any[];
loopDetail?: ChatHistoryItemResType[];
// loop start
loopInputValue?: any;
// loop end
loopOutputValue?: any;
};

export type DispatchNodeResultType<T = {}> = {
Expand Down
10 changes: 8 additions & 2 deletions packages/global/core/workflow/template/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ import { TextEditorNode } from './system/textEditor';
import { CustomFeedbackNode } from './system/customFeedback';
import { ReadFilesNodes } from './system/readFiles';
import { UserSelectNode } from './system/userSelect/index';
import { LoopNode } from './system/loop/loop';
import { LoopStartNode } from './system/loop/loopStart';
import { LoopEndNode } from './system/loop/loopEnd';

const systemNodes: FlowNodeTemplateType[] = [
AiChatModule,
Expand All @@ -46,7 +49,8 @@ const systemNodes: FlowNodeTemplateType[] = [
LafModule,
IfElseNode,
VariableUpdateNode,
CodeNode
CodeNode,
LoopNode
];
/* app flow module templates */
export const appSystemModuleTemplates: FlowNodeTemplateType[] = [
Expand Down Expand Up @@ -74,5 +78,7 @@ export const moduleTemplatesFlat: FlowNodeTemplateType[] = [
EmptyNode,
RunPluginModule,
RunAppNode,
RunAppModule
RunAppModule,
LoopStartNode,
LoopEndNode
];
57 changes: 57 additions & 0 deletions packages/global/core/workflow/template/system/loop/loop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import {
FlowNodeInputTypeEnum,
FlowNodeOutputTypeEnum,
FlowNodeTypeEnum
} from '../../../node/constant';
import { FlowNodeTemplateType } from '../../../type/node';
import {
FlowNodeTemplateTypeEnum,
NodeInputKeyEnum,
NodeOutputKeyEnum,
WorkflowIOValueTypeEnum
} from '../../../constants';
import { getHandleConfig } from '../../utils';
import { i18nT } from '../../../../../../web/i18n/utils';

export const LoopNode: FlowNodeTemplateType = {
id: FlowNodeTypeEnum.loop,
templateType: FlowNodeTemplateTypeEnum.tools,
flowNodeType: FlowNodeTypeEnum.loop,
sourceHandle: getHandleConfig(true, true, true, true),
targetHandle: getHandleConfig(true, true, true, true),
avatar: 'core/workflow/template/loop',
name: i18nT('workflow:loop'),
intro: i18nT('workflow:intro_loop'),
showStatus: true,
version: '4811',
inputs: [
{
key: NodeInputKeyEnum.loopInputArray,
renderTypeList: [FlowNodeInputTypeEnum.reference],
valueType: WorkflowIOValueTypeEnum.arrayAny,
required: true,
label: i18nT('workflow:loop_input_array'),
value: []
},
{
key: NodeInputKeyEnum.loopFlow,
renderTypeList: [FlowNodeInputTypeEnum.hidden],
valueType: WorkflowIOValueTypeEnum.any,
label: '',
value: {
width: 0,
height: 0,
childNodes: []
}
}
],
outputs: [
{
id: NodeOutputKeyEnum.loopArray,
key: NodeOutputKeyEnum.loopArray,
label: i18nT('workflow:loop_result'),
type: FlowNodeOutputTypeEnum.static,
valueType: WorkflowIOValueTypeEnum.arrayAny
}
]
};
35 changes: 35 additions & 0 deletions packages/global/core/workflow/template/system/loop/loopEnd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { i18nT } from '../../../../../../web/i18n/utils';
import {
FlowNodeTemplateTypeEnum,
NodeInputKeyEnum,
WorkflowIOValueTypeEnum
} from '../../../constants';
import { FlowNodeInputTypeEnum, FlowNodeTypeEnum } from '../../../node/constant';
import { FlowNodeTemplateType } from '../../../type/node';
import { getHandleConfig } from '../../utils';

export const LoopEndNode: FlowNodeTemplateType = {
id: FlowNodeTypeEnum.loopEnd,
templateType: FlowNodeTemplateTypeEnum.systemInput,
flowNodeType: FlowNodeTypeEnum.loopEnd,
sourceHandle: getHandleConfig(false, false, false, false),
targetHandle: getHandleConfig(false, false, false, true),
unique: true,
forbidDelete: true,
avatar: 'core/workflow/template/loopEnd',
name: i18nT('workflow:loop_end'),
showStatus: false,
version: '4811',
inputs: [
{
key: NodeInputKeyEnum.loopOutputArrayElement,
renderTypeList: [FlowNodeInputTypeEnum.reference],
valueType: WorkflowIOValueTypeEnum.any,
label: '',
required: true,
value: [],
showType: true
}
],
outputs: []
};
34 changes: 34 additions & 0 deletions packages/global/core/workflow/template/system/loop/loopStart.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { FlowNodeInputTypeEnum, FlowNodeTypeEnum } from '../../../node/constant';
import { FlowNodeTemplateType } from '../../../type/node.d';
import {
FlowNodeTemplateTypeEnum,
NodeInputKeyEnum,
WorkflowIOValueTypeEnum
} from '../../../constants';
import { getHandleConfig } from '../../utils';
import { i18nT } from '../../../../../../web/i18n/utils';

export const LoopStartNode: FlowNodeTemplateType = {
id: FlowNodeTypeEnum.loopStart,
templateType: FlowNodeTemplateTypeEnum.systemInput,
flowNodeType: FlowNodeTypeEnum.loopStart,
sourceHandle: getHandleConfig(false, true, false, false),
targetHandle: getHandleConfig(false, false, false, false),
avatar: 'core/workflow/template/loopStart',
name: i18nT('workflow:loop_start'),
unique: true,
forbidDelete: true,
showStatus: false,
version: '4811',
inputs: [
{
key: NodeInputKeyEnum.loopArrayElement,
renderTypeList: [FlowNodeInputTypeEnum.hidden],
valueType: WorkflowIOValueTypeEnum.any,
label: '',
required: true,
value: ''
}
],
outputs: []
};
1 change: 1 addition & 0 deletions packages/global/core/workflow/type/io.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export type FlowNodeInputItemType = InputComponentPropsType & {
// render components params
canEdit?: boolean; // dynamic inputs
isPro?: boolean; // Pro version field
showType?: boolean; // show data type
};

export type FlowNodeOutputItemType = {
Expand Down
1 change: 1 addition & 0 deletions packages/global/core/workflow/type/node.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export type NodeTemplateListType = {
// react flow node type
export type FlowNodeItemType = FlowNodeTemplateType & {
nodeId: string;
parentNodeId?: string;
isError?: boolean;
debugResult?: {
status: 'running' | 'success' | 'skipped' | 'failed';
Expand Down
6 changes: 6 additions & 0 deletions packages/service/core/workflow/dispatch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ import {
UserSelectInteractive
} from '@fastgpt/global/core/workflow/template/system/userSelect/type';
import { dispatchRunAppNode } from './plugin/runApp';
import { dispatchLoop } from './tools/runLoop';
import { dispatchLoopEnd } from './tools/runLoopEnd';
import { dispatchLoopStart } from './tools/runLoopStart';

const callbackMap: Record<FlowNodeTypeEnum, Function> = {
[FlowNodeTypeEnum.workflowStart]: dispatchWorkflowStart,
Expand All @@ -91,6 +94,9 @@ const callbackMap: Record<FlowNodeTypeEnum, Function> = {
[FlowNodeTypeEnum.customFeedback]: dispatchCustomFeedback,
[FlowNodeTypeEnum.readFiles]: dispatchReadFiles,
[FlowNodeTypeEnum.userSelect]: dispatchUserSelect,
[FlowNodeTypeEnum.loop]: dispatchLoop,
[FlowNodeTypeEnum.loopStart]: dispatchLoopStart,
[FlowNodeTypeEnum.loopEnd]: dispatchLoopEnd,

// none
[FlowNodeTypeEnum.systemConfig]: dispatchSystemConfig,
Expand Down
84 changes: 84 additions & 0 deletions packages/service/core/workflow/dispatch/tools/runLoop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { NodeInputKeyEnum, NodeOutputKeyEnum } from '@fastgpt/global/core/workflow/constants';
import { FlowNodeTypeEnum } from '@fastgpt/global/core/workflow/node/constant';
import {
DispatchNodeResultType,
ModuleDispatchProps
} from '@fastgpt/global/core/workflow/runtime/type';
import { dispatchWorkFlow } from '..';
import { DispatchNodeResponseKeyEnum } from '@fastgpt/global/core/workflow/runtime/constants';
import { ChatHistoryItemResType } from '@fastgpt/global/core/chat/type';

type Props = ModuleDispatchProps<{
[NodeInputKeyEnum.loopInputArray]: Array<any>;
[NodeInputKeyEnum.loopFlow]: { childNodes: Array<string> };
}>;
type Response = DispatchNodeResultType<{
[NodeOutputKeyEnum.loopArray]: Array<any>;
}>;

export const dispatchLoop = async (props: Props): Promise<Response> => {
const {
params,
runtimeNodes,
user,
node: { name }
} = props;
const {
loopInputArray,
loopFlow: { childNodes }
} = params;
const runNodes = runtimeNodes.filter((node) => childNodes.includes(node.nodeId));
const outputArray = [];
const loopDetail: ChatHistoryItemResType[] = [];
let totalPoints = 0;

for await (const element of loopInputArray) {
const response = await dispatchWorkFlow({
...props,
runtimeNodes: runNodes.map((node) =>
node.flowNodeType === FlowNodeTypeEnum.loopStart
? {
...node,
isEntry: true,
inputs: node.inputs.map((input) =>
input.key === NodeInputKeyEnum.loopArrayElement
? {
...input,
value: element
}
: input
)
}
: {
...node,
isEntry: false
}
)
});

const loopOutputValue = response.flowResponses.find(
(res) => res.moduleType === FlowNodeTypeEnum.loopEnd
)?.loopOutputValue;

outputArray.push(loopOutputValue);
loopDetail.push(...response.flowResponses);

totalPoints = response.flowUsages.reduce((acc, usage) => acc + usage.totalPoints, 0);
}

return {
[DispatchNodeResponseKeyEnum.nodeResponse]: {
totalPoints: totalPoints,
loopInput: loopInputArray,
loopResult: outputArray,
loopDetail: loopDetail
},
[DispatchNodeResponseKeyEnum.nodeDispatchUsages]: [
{
totalPoints: user.openaiAccount?.key ? 0 : totalPoints,
moduleName: name
}
],
[NodeOutputKeyEnum.loopArray]: outputArray
};
};
21 changes: 21 additions & 0 deletions packages/service/core/workflow/dispatch/tools/runLoopEnd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { NodeInputKeyEnum } from '@fastgpt/global/core/workflow/constants';
import { DispatchNodeResponseKeyEnum } from '@fastgpt/global/core/workflow/runtime/constants';
import {
DispatchNodeResultType,
ModuleDispatchProps
} from '@fastgpt/global/core/workflow/runtime/type';

type Props = ModuleDispatchProps<{
[NodeInputKeyEnum.loopOutputArrayElement]: any;
}>;
type Response = DispatchNodeResultType<{}>;

export const dispatchLoopEnd = async (props: Props): Promise<Response> => {
const { params } = props;

return {
[DispatchNodeResponseKeyEnum.nodeResponse]: {
loopOutputValue: params.loopOutputArrayElement
}
};
};
Loading
Loading