Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add $flow to Variable Resolution in Flow Building Process #3075

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/server/src/utils/buildChatflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,13 +328,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
Loading