Skip to content

Commit

Permalink
fix:variable not update in nested workflow runs (#2830)
Browse files Browse the repository at this point in the history
  • Loading branch information
newfish-cmyk authored Sep 28, 2024
1 parent d95f71e commit f7a8203
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 3 deletions.
3 changes: 2 additions & 1 deletion packages/global/core/workflow/runtime/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export enum DispatchNodeResponseKeyEnum {
rewriteHistories = 'rewriteHistories', // If have the response, workflow histories will be rewrite

interactive = 'INTERACTIVE', // is interactive
runTimes = 'runTimes' // run times
runTimes = 'runTimes', // run times
newVariables = 'newVariables' // new variables
}

export const needReplaceReferenceInputTypeList = [
Expand Down
1 change: 1 addition & 0 deletions packages/global/core/workflow/runtime/type.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ export type DispatchNodeResultType<T = {}> = {
[DispatchNodeResponseKeyEnum.assistantResponses]?: AIChatItemValueItemType[]; // Assistant response(Store to db)
[DispatchNodeResponseKeyEnum.rewriteHistories]?: ChatItemType[];
[DispatchNodeResponseKeyEnum.runTimes]?: number;
[DispatchNodeResponseKeyEnum.newVariables]?: Record<string, any>;
} & T;

/* Single node props */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ export const dispatchRunTools = async (props: DispatchToolModuleProps): Promise<

// flat child tool response
const childToolResponse = dispatchFlowResponse.map((item) => item.flowResponses).flat();
const newVariables = dispatchFlowResponse[dispatchFlowResponse.length - 1]?.newVariables;

// concat tool usage
const totalPointsUsage =
Expand Down Expand Up @@ -219,6 +220,7 @@ export const dispatchRunTools = async (props: DispatchToolModuleProps): Promise<
tokens: totalTokens
},
...flatUsages
]
],
[DispatchNodeResponseKeyEnum.newVariables]: newVariables
};
};
8 changes: 8 additions & 0 deletions packages/service/core/workflow/dispatch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,14 @@ export async function dispatchWorkFlow(data: Props): Promise<DispatchFlowRespons
dispatchRes[item.key] = valueTypeFormat(item.defaultValue, item.valueType);
});

// Update new variables
if (dispatchRes[DispatchNodeResponseKeyEnum.newVariables]) {
variables = {
...variables,
...dispatchRes[DispatchNodeResponseKeyEnum.newVariables]
};
}

return {
node,
runStatus: 'run',
Expand Down
8 changes: 7 additions & 1 deletion packages/service/core/workflow/dispatch/loop/runLoop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export const dispatchLoop = async (props: Props): Promise<Response> => {
const loopDetail: ChatHistoryItemResType[] = [];
let assistantResponses: AIChatItemValueItemType[] = [];
let totalPoints = 0;
let newVariables: Record<string, any> = {};

for await (const item of loopInputArray) {
const response = await dispatchWorkFlow({
Expand Down Expand Up @@ -72,6 +73,10 @@ export const dispatchLoop = async (props: Props): Promise<Response> => {
assistantResponses.push(...response.assistantResponses);

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

return {
Expand All @@ -88,6 +93,7 @@ export const dispatchLoop = async (props: Props): Promise<Response> => {
moduleName: name
}
],
[NodeOutputKeyEnum.loopArray]: outputValueArr
[NodeOutputKeyEnum.loopArray]: outputValueArr,
[DispatchNodeResponseKeyEnum.newVariables]: newVariables
};
};

0 comments on commit f7a8203

Please sign in to comment.