feat: add update variable node (#1362)

* feat: add variable update node

* fix

* fix

* change component quote
This commit is contained in:
heheer
2024-05-06 12:20:29 +08:00
committed by GitHub
parent d057ba29f0
commit 59bd2a47b6
25 changed files with 503 additions and 36 deletions

View File

@@ -42,7 +42,8 @@ import { dispatchLafRequest } from './tools/runLaf';
import { dispatchIfElse } from './tools/runIfElse';
import { RuntimeEdgeItemType } from '@fastgpt/global/core/workflow/type/edge';
import { getReferenceVariableValue } from '@fastgpt/global/core/workflow/runtime/utils';
import { dispatchSystemConfig } from './init/systemConfiig';
import { dispatchSystemConfig } from './init/systemConfig';
import { dispatchUpdateVariable } from './tools/runUpdateVar';
const callbackMap: Record<`${FlowNodeTypeEnum}`, Function> = {
[FlowNodeTypeEnum.workflowStart]: dispatchWorkflowStart,
@@ -62,6 +63,7 @@ const callbackMap: Record<`${FlowNodeTypeEnum}`, Function> = {
[FlowNodeTypeEnum.stopTool]: dispatchStopToolCall,
[FlowNodeTypeEnum.lafModule]: dispatchLafRequest,
[FlowNodeTypeEnum.ifElseNode]: dispatchIfElse,
[FlowNodeTypeEnum.variableUpdate]: dispatchUpdateVariable,
// none
[FlowNodeTypeEnum.systemConfig]: dispatchSystemConfig,
@@ -355,7 +357,7 @@ export async function dispatchWorkFlow(data: Props): Promise<DispatchFlowRespons
if (pluginOutputModule && props.mode !== 'debug') {
await nodeRunWithActive(pluginOutputModule);
}
const { userChatInput, ...leftVariables } = variables;
return {
flowResponses: chatResponses,
flowUsages: chatNodeUsages,
@@ -366,7 +368,8 @@ export async function dispatchWorkFlow(data: Props): Promise<DispatchFlowRespons
},
[DispatchNodeResponseKeyEnum.assistantResponses]:
mergeAssistantResponseAnswerText(chatAssistantResponse),
[DispatchNodeResponseKeyEnum.toolResponses]: toolRunResponse
[DispatchNodeResponseKeyEnum.toolResponses]: toolRunResponse,
newVariables: leftVariables
};
}

View File

@@ -0,0 +1,4 @@
export const dispatchSystemConfig = (props: Record<string, any>) => {
const { variables } = props;
return variables;
};

View File

@@ -1,10 +0,0 @@
import { NodeInputKeyEnum } from '@fastgpt/global/core/workflow/constants';
import type { ModuleDispatchProps } from '@fastgpt/global/core/workflow/type/index.d';
export type UserChatInputProps = ModuleDispatchProps<{
[NodeInputKeyEnum.userChatInput]: string;
}>;
export const dispatchSystemConfig = (props: Record<string, any>) => {
const { variables } = props as UserChatInputProps;
return variables;
};

View File

@@ -0,0 +1,53 @@
import { NodeInputKeyEnum, VARIABLE_NODE_ID } 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 { getReferenceVariableValue } from '@fastgpt/global/core/workflow/runtime/utils';
import { TUpdateListItem } from '@fastgpt/global/core/workflow/template/system/variableUpdate/type';
import { ModuleDispatchProps } from '@fastgpt/global/core/workflow/type';
type Props = ModuleDispatchProps<{
[NodeInputKeyEnum.updateList]: TUpdateListItem[];
}>;
export const dispatchUpdateVariable = async (
props: Props
): Promise<DispatchNodeResultType<any>> => {
const { params, variables, runtimeNodes } = props;
const { updateList } = params;
updateList.forEach((item) => {
const varNodeId = item.variable?.[0];
const varKey = item.variable?.[1];
if (!varNodeId || !varKey) {
return;
}
let value = '';
if (!item.value?.[0]) {
value = item.value?.[1];
} else {
value = getReferenceVariableValue({
value: item.value,
variables,
nodes: runtimeNodes
});
}
if (varNodeId === VARIABLE_NODE_ID) {
variables[varKey] = value;
} else {
const node = runtimeNodes.find((node) => node.nodeId === varNodeId);
if (node) {
const output = node.outputs.find((output) => output.id === varKey);
if (output) {
output.value = value;
}
}
}
});
return {
[DispatchNodeResponseKeyEnum.nodeResponse]: {
totalPoints: 0
}
};
};

View File

@@ -19,4 +19,5 @@ export type DispatchFlowResponse = {
};
[DispatchNodeResponseKeyEnum.toolResponses]: ToolRunResponseItemType;
[DispatchNodeResponseKeyEnum.assistantResponses]: AIChatItemValueItemType[];
newVariables: Record<string, string>;
};

View File

@@ -12,4 +12,5 @@ export type DispatchFlowResponse = {
flowUsages: ChatNodeUsageType[];
[DispatchNodeResponseKeyEnum.toolResponses]: ToolRunResponseItemType;
[DispatchNodeResponseKeyEnum.assistantResponses]: AIChatItemValueItemType[];
newVariables: Record<string, string>;
};