Skip to content

Commit

Permalink
Add $flow to Variable Resolution in Flow Building Process (FlowiseAI#…
Browse files Browse the repository at this point in the history
…3075)

* Add $flow to Variable Resolution in Flow Building Process

* add overrideConfig values to $flow....

* fix for replacement of $flow values inside text.

* refactor and compatibilize with agent flow
  • Loading branch information
raffareis authored and patrickalvesexperian committed Sep 3, 2024
1 parent 0ae07e8 commit 9e8de50
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
10 changes: 9 additions & 1 deletion packages/server/src/utils/buildChatflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
28 changes: 22 additions & 6 deletions packages/server/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,14 @@ export const buildFlow = async ({

const initializedNodes: Set<string> = 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

Expand All @@ -509,7 +517,7 @@ export const buildFlow = async ({
flowNodes,
question,
chatHistory,
overrideConfig
flowData
)

if (isUpsert && stopNodeId && nodeId === stopNodeId) {
Expand Down Expand Up @@ -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) ?? ''
Expand Down Expand Up @@ -797,14 +805,22 @@ 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
returnVal = returnVal.split(`{{${variableFullPath}}}`).join(variableValue)
}
}

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: <variableNodeId>.data.instance
// 2: <variableNodeId>.data.instance.pathtokey
Expand Down Expand Up @@ -897,7 +913,7 @@ export const resolveVariables = async (
reactFlowNodes: IReactFlowNode[],
question: string,
chatHistory: IMessage[],
overrideConfig?: ICommonObject
flowData?: ICommonObject
): Promise<INodeData> => {
let flowNodeData = cloneDeep(reactFlowNodeData)
const types = 'inputs'
Expand All @@ -915,7 +931,7 @@ export const resolveVariables = async (
question,
chatHistory,
undefined,
overrideConfig
flowData
)
resolvedInstances.push(resolvedInstance)
}
Expand All @@ -929,7 +945,7 @@ export const resolveVariables = async (
question,
chatHistory,
isAcceptVariable,
overrideConfig
flowData
)
paramsObj[key] = resolvedInstance
}
Expand Down

0 comments on commit 9e8de50

Please sign in to comment.