From 800d524e9308d9e74b7b7af773e7e24198426e3a Mon Sep 17 00:00:00 2001 From: Rafael Reis Date: Sat, 24 Aug 2024 22:29:36 -0300 Subject: [PATCH 1/4] Add $flow to Variable Resolution in Flow Building Process --- packages/server/src/utils/index.ts | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/packages/server/src/utils/index.ts b/packages/server/src/utils/index.ts index 50c43f99396..0791fe1f8a3 100644 --- a/packages/server/src/utils/index.ts +++ b/packages/server/src/utils/index.ts @@ -486,6 +486,13 @@ export const buildFlow = async ({ const initializedNodes: Set = new Set() const reversedGraph = constructGraphs(reactFlowNodes, reactFlowEdges, { isReversed: true }).graph + + const options: ICommonObject = { + chatflowid, + chatId, + sessionId, + chatHistory + } while (nodeQueue.length) { const { nodeId, depth } = nodeQueue.shift() as INodeQueue @@ -509,7 +516,8 @@ export const buildFlow = async ({ flowNodes, question, chatHistory, - overrideConfig + overrideConfig, + options ) if (isUpsert && stopNodeId && nodeId === stopNodeId) { @@ -760,7 +768,8 @@ export const getVariableValue = async ( question: string, chatHistory: IMessage[], isAcceptVariable = false, - overrideConfig?: ICommonObject + overrideConfig?: ICommonObject, + options?: ICommonObject ) => { const isObject = typeof paramValue === 'object' let returnVal = (isObject ? JSON.stringify(paramValue) : paramValue) ?? '' @@ -805,6 +814,14 @@ export const getVariableValue = async ( } } + if (variableFullPath.startsWith('$flow.') && options) { + const variableValue = get(options, variableFullPath.replace('$flow.', '')) + if (variableValue) { + variableDict[`{{${variableFullPath}}}`] = variableValue + returnVal = returnVal.split(`{{${variableFullPath}}}`).join(variableValue) + } + } + // Resolve values with following case. // 1: .data.instance // 2: .data.instance.pathtokey @@ -897,7 +914,8 @@ export const resolveVariables = async ( reactFlowNodes: IReactFlowNode[], question: string, chatHistory: IMessage[], - overrideConfig?: ICommonObject + overrideConfig?: ICommonObject, + options?: ICommonObject ): Promise => { let flowNodeData = cloneDeep(reactFlowNodeData) const types = 'inputs' @@ -915,7 +933,8 @@ export const resolveVariables = async ( question, chatHistory, undefined, - overrideConfig + overrideConfig, + options ) resolvedInstances.push(resolvedInstance) } @@ -929,7 +948,8 @@ export const resolveVariables = async ( question, chatHistory, isAcceptVariable, - overrideConfig + overrideConfig, + options ) paramsObj[key] = resolvedInstance } From fd4fb6f19883bf540f08b42cffba97355029af90 Mon Sep 17 00:00:00 2001 From: Rafael Reis Date: Sun, 25 Aug 2024 01:30:44 -0300 Subject: [PATCH 2/4] add overrideConfig values to $flow.... --- packages/server/src/utils/index.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/server/src/utils/index.ts b/packages/server/src/utils/index.ts index 0791fe1f8a3..f84c6c23a1f 100644 --- a/packages/server/src/utils/index.ts +++ b/packages/server/src/utils/index.ts @@ -491,7 +491,8 @@ export const buildFlow = async ({ chatflowid, chatId, sessionId, - chatHistory + chatHistory, + ...overrideConfig } while (nodeQueue.length) { const { nodeId, depth } = nodeQueue.shift() as INodeQueue From bbf8cee533ff2ecdb7c90df5a33e53ea5c322bb9 Mon Sep 17 00:00:00 2001 From: Rafael Reis Date: Mon, 26 Aug 2024 12:26:07 -0300 Subject: [PATCH 3/4] fix for replacement of $flow values inside text. --- packages/server/src/utils/buildChatflow.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/server/src/utils/buildChatflow.ts b/packages/server/src/utils/buildChatflow.ts index 93a52332200..585d7776003 100644 --- a/packages/server/src/utils/buildChatflow.ts +++ b/packages/server/src/utils/buildChatflow.ts @@ -328,13 +328,22 @@ export const utilBuildChatflow = async (req: Request, socketIO?: Server, isInter nodeToExecute.data = replaceInputsWithConfig(nodeToExecute.data, incomingInput.overrideConfig) } + const options: ICommonObject = { + chatflowid, + chatId, + sessionId, + chatHistory, + ...incomingInput.overrideConfig + } + const reactFlowNodeData: INodeData = await resolveVariables( appServer.AppDataSource, nodeToExecute.data, reactFlowNodes, incomingInput.question, chatHistory, - incomingInput.overrideConfig + incomingInput.overrideConfig, + options ) nodeToExecuteData = reactFlowNodeData From d3662bc9162d72ec1727a7a10d7bec70067a56cd Mon Sep 17 00:00:00 2001 From: Rafael Reis Date: Mon, 26 Aug 2024 12:48:13 -0300 Subject: [PATCH 4/4] refactor and compatibilize with agent flow --- packages/server/src/utils/buildChatflow.ts | 5 ++--- packages/server/src/utils/index.ts | 23 +++++++++------------- 2 files changed, 11 insertions(+), 17 deletions(-) diff --git a/packages/server/src/utils/buildChatflow.ts b/packages/server/src/utils/buildChatflow.ts index 585d7776003..686acf08cfe 100644 --- a/packages/server/src/utils/buildChatflow.ts +++ b/packages/server/src/utils/buildChatflow.ts @@ -328,7 +328,7 @@ export const utilBuildChatflow = async (req: Request, socketIO?: Server, isInter nodeToExecute.data = replaceInputsWithConfig(nodeToExecute.data, incomingInput.overrideConfig) } - const options: ICommonObject = { + const flowData: ICommonObject = { chatflowid, chatId, sessionId, @@ -342,8 +342,7 @@ export const utilBuildChatflow = async (req: Request, socketIO?: Server, isInter reactFlowNodes, incomingInput.question, chatHistory, - incomingInput.overrideConfig, - options + flowData ) nodeToExecuteData = reactFlowNodeData diff --git a/packages/server/src/utils/index.ts b/packages/server/src/utils/index.ts index f84c6c23a1f..548e4d4c8d8 100644 --- a/packages/server/src/utils/index.ts +++ b/packages/server/src/utils/index.ts @@ -487,7 +487,7 @@ export const buildFlow = async ({ const initializedNodes: Set = new Set() const reversedGraph = constructGraphs(reactFlowNodes, reactFlowEdges, { isReversed: true }).graph - const options: ICommonObject = { + const flowData: ICommonObject = { chatflowid, chatId, sessionId, @@ -517,8 +517,7 @@ export const buildFlow = async ({ flowNodes, question, chatHistory, - overrideConfig, - options + flowData ) if (isUpsert && stopNodeId && nodeId === stopNodeId) { @@ -769,8 +768,7 @@ export const getVariableValue = async ( question: string, chatHistory: IMessage[], isAcceptVariable = false, - overrideConfig?: ICommonObject, - options?: ICommonObject + flowData?: ICommonObject ) => { const isObject = typeof paramValue === 'object' let returnVal = (isObject ? JSON.stringify(paramValue) : paramValue) ?? '' @@ -807,7 +805,7 @@ export const getVariableValue = async ( } if (variableFullPath.startsWith('$vars.')) { - const vars = await getGlobalVariable(appDataSource, overrideConfig) + const vars = await getGlobalVariable(appDataSource, flowData) const variableValue = get(vars, variableFullPath.replace('$vars.', '')) if (variableValue) { variableDict[`{{${variableFullPath}}}`] = variableValue @@ -815,8 +813,8 @@ export const getVariableValue = async ( } } - if (variableFullPath.startsWith('$flow.') && options) { - const variableValue = get(options, variableFullPath.replace('$flow.', '')) + if (variableFullPath.startsWith('$flow.') && flowData) { + const variableValue = get(flowData, variableFullPath.replace('$flow.', '')) if (variableValue) { variableDict[`{{${variableFullPath}}}`] = variableValue returnVal = returnVal.split(`{{${variableFullPath}}}`).join(variableValue) @@ -915,8 +913,7 @@ export const resolveVariables = async ( reactFlowNodes: IReactFlowNode[], question: string, chatHistory: IMessage[], - overrideConfig?: ICommonObject, - options?: ICommonObject + flowData?: ICommonObject ): Promise => { let flowNodeData = cloneDeep(reactFlowNodeData) const types = 'inputs' @@ -934,8 +931,7 @@ export const resolveVariables = async ( question, chatHistory, undefined, - overrideConfig, - options + flowData ) resolvedInstances.push(resolvedInstance) } @@ -949,8 +945,7 @@ export const resolveVariables = async ( question, chatHistory, isAcceptVariable, - overrideConfig, - options + flowData ) paramsObj[key] = resolvedInstance }