diff --git a/packages/server/src/utils/buildChatflow.ts b/packages/server/src/utils/buildChatflow.ts index 93d4fd5e1..c7ab304c8 100644 --- a/packages/server/src/utils/buildChatflow.ts +++ b/packages/server/src/utils/buildChatflow.ts @@ -337,13 +337,21 @@ export const utilBuildChatflow = async (req: Request, socketIO?: Server, isInter nodeToExecute.data = replaceInputsWithConfig(nodeToExecute.data, incomingInput.overrideConfig) } + const flowData: ICommonObject = { + chatflowid, + chatId, + sessionId, + chatHistory, + ...incomingInput.overrideConfig + } + const reactFlowNodeData: INodeData = await resolveVariables( appServer.AppDataSource, nodeToExecute.data, reactFlowNodes, incomingInput.question, chatHistory, - incomingInput.overrideConfig + flowData ) nodeToExecuteData = reactFlowNodeData diff --git a/packages/server/src/utils/index.ts b/packages/server/src/utils/index.ts index 2f884b11a..2aa430f00 100644 --- a/packages/server/src/utils/index.ts +++ b/packages/server/src/utils/index.ts @@ -486,6 +486,14 @@ export const buildFlow = async ({ const initializedNodes: Set = new Set() const reversedGraph = constructGraphs(reactFlowNodes, reactFlowEdges, { isReversed: true }).graph + + const flowData: ICommonObject = { + chatflowid, + chatId, + sessionId, + chatHistory, + ...overrideConfig + } while (nodeQueue.length) { const { nodeId, depth } = nodeQueue.shift() as INodeQueue @@ -509,7 +517,7 @@ export const buildFlow = async ({ flowNodes, question, chatHistory, - overrideConfig + flowData ) if (isUpsert && stopNodeId && nodeId === stopNodeId) { @@ -760,7 +768,7 @@ export const getVariableValue = async ( question: string, chatHistory: IMessage[], isAcceptVariable = false, - overrideConfig?: ICommonObject + flowData?: ICommonObject ) => { const isObject = typeof paramValue === 'object' let returnVal = (isObject ? JSON.stringify(paramValue) : paramValue) ?? '' @@ -797,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 @@ -805,6 +813,14 @@ export const getVariableValue = async ( } } + if (variableFullPath.startsWith('$flow.') && flowData) { + const variableValue = get(flowData, 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 +913,7 @@ export const resolveVariables = async ( reactFlowNodes: IReactFlowNode[], question: string, chatHistory: IMessage[], - overrideConfig?: ICommonObject + flowData?: ICommonObject ): Promise => { let flowNodeData = cloneDeep(reactFlowNodeData) const types = 'inputs' @@ -915,7 +931,7 @@ export const resolveVariables = async ( question, chatHistory, undefined, - overrideConfig + flowData ) resolvedInstances.push(resolvedInstance) } @@ -929,7 +945,7 @@ export const resolveVariables = async ( question, chatHistory, isAcceptVariable, - overrideConfig + flowData ) paramsObj[key] = resolvedInstance }