Skip to content

Commit 1ccd976

Browse files
authored
Bugfix/Override config vars (#3524)
update bugfix for override config vars
1 parent 16ceed1 commit 1ccd976

File tree

3 files changed

+65
-25
lines changed

3 files changed

+65
-25
lines changed

packages/server/src/utils/buildAgentGraph.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,8 @@ const compileMultiAgentsGraph = async (params: MultiAgentsGraphParams) => {
529529
const newNodeInstance = new nodeModule.nodeClass()
530530

531531
let flowNodeData = cloneDeep(workerNode.data)
532-
if (overrideConfig && apiOverrideStatus) flowNodeData = replaceInputsWithConfig(flowNodeData, overrideConfig, nodeOverrides)
532+
if (overrideConfig && apiOverrideStatus)
533+
flowNodeData = replaceInputsWithConfig(flowNodeData, overrideConfig, nodeOverrides, variableOverrides)
533534
flowNodeData = await resolveVariables(
534535
appServer.AppDataSource,
535536
flowNodeData,
@@ -569,7 +570,8 @@ const compileMultiAgentsGraph = async (params: MultiAgentsGraphParams) => {
569570

570571
let flowNodeData = cloneDeep(supervisorNode.data)
571572

572-
if (overrideConfig && apiOverrideStatus) flowNodeData = replaceInputsWithConfig(flowNodeData, overrideConfig, nodeOverrides)
573+
if (overrideConfig && apiOverrideStatus)
574+
flowNodeData = replaceInputsWithConfig(flowNodeData, overrideConfig, nodeOverrides, variableOverrides)
573575
flowNodeData = await resolveVariables(
574576
appServer.AppDataSource,
575577
flowNodeData,
@@ -758,7 +760,8 @@ const compileSeqAgentsGraph = async (params: SeqAgentsGraphParams) => {
758760
const newNodeInstance = new nodeModule.nodeClass()
759761

760762
flowNodeData = cloneDeep(node.data)
761-
if (overrideConfig && apiOverrideStatus) flowNodeData = replaceInputsWithConfig(flowNodeData, overrideConfig, nodeOverrides)
763+
if (overrideConfig && apiOverrideStatus)
764+
flowNodeData = replaceInputsWithConfig(flowNodeData, overrideConfig, nodeOverrides, variableOverrides)
762765
flowNodeData = await resolveVariables(
763766
appServer.AppDataSource,
764767
flowNodeData,

packages/server/src/utils/buildChatflow.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,12 @@ export const utilBuildChatflow = async (req: Request, isInternal: boolean = fals
388388

389389
// Only override the config if its status is true
390390
if (incomingInput.overrideConfig && apiOverrideStatus) {
391-
nodeToExecute.data = replaceInputsWithConfig(nodeToExecute.data, incomingInput.overrideConfig, nodeOverrides)
391+
nodeToExecute.data = replaceInputsWithConfig(
392+
nodeToExecute.data,
393+
incomingInput.overrideConfig,
394+
nodeOverrides,
395+
variableOverrides
396+
)
392397
}
393398

394399
const flowData: ICommonObject = {

packages/server/src/utils/index.ts

+53-21
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ export const buildFlow = async ({
519519

520520
// Only override the config if its status is true
521521
if (overrideConfig && apiOverrideStatus) {
522-
flowNodeData = replaceInputsWithConfig(flowNodeData, overrideConfig, nodeOverrides)
522+
flowNodeData = replaceInputsWithConfig(flowNodeData, overrideConfig, nodeOverrides, variableOverrides)
523523
}
524524

525525
if (isUpsert) upsertHistory['flowData'] = saveUpsertFlowData(flowNodeData, upsertHistory)
@@ -1001,9 +1001,15 @@ export const resolveVariables = async (
10011001
* @param {INodeData} flowNodeData
10021002
* @param {ICommonObject} overrideConfig
10031003
* @param {ICommonObject} nodeOverrides
1004+
* @param {ICommonObject[]} variableOverrides
10041005
* @returns {INodeData}
10051006
*/
1006-
export const replaceInputsWithConfig = (flowNodeData: INodeData, overrideConfig: ICommonObject, nodeOverrides: ICommonObject) => {
1007+
export const replaceInputsWithConfig = (
1008+
flowNodeData: INodeData,
1009+
overrideConfig: ICommonObject,
1010+
nodeOverrides: ICommonObject,
1011+
variableOverrides: ICommonObject[]
1012+
) => {
10071013
const types = 'inputs'
10081014

10091015
const isParameterEnabled = (nodeType: string, paramName: string): boolean => {
@@ -1014,28 +1020,54 @@ export const replaceInputsWithConfig = (flowNodeData: INodeData, overrideConfig:
10141020

10151021
const getParamValues = (inputsObj: ICommonObject) => {
10161022
for (const config in overrideConfig) {
1017-
// Always allow analytics config: https://docs.flowiseai.com/using-flowise/analytic#api
1018-
if (config !== 'analytics') {
1019-
// If overrideConfig[key] is object
1020-
if (overrideConfig[config] && typeof overrideConfig[config] === 'object') {
1021-
const nodeIds = Object.keys(overrideConfig[config])
1022-
if (nodeIds.includes(flowNodeData.id)) {
1023-
// Check if this parameter is enabled for this node type
1024-
if (isParameterEnabled(flowNodeData.label, config)) {
1025-
inputsObj[config] = overrideConfig[config][flowNodeData.id]
1023+
/**
1024+
* Several conditions:
1025+
* 1. If config is 'analytics', always allow it
1026+
* 2. If config is 'vars', check its object and filter out the variables that are not enabled for override
1027+
* 3. If typeof config is an object, check if the node id is in the overrideConfig object and if the parameter (systemMessagePrompt) is enabled
1028+
* Example:
1029+
* "systemMessagePrompt": {
1030+
* "chatPromptTemplate_0": "You are an assistant"
1031+
* }
1032+
* 4. If typeof config is a string, check if the parameter is enabled
1033+
* Example:
1034+
* "systemMessagePrompt": "You are an assistant"
1035+
*/
1036+
1037+
if (config === 'analytics') {
1038+
// pass
1039+
} else if (config === 'vars') {
1040+
if (typeof overrideConfig[config] === 'object') {
1041+
const filteredVars: ICommonObject = {}
1042+
1043+
const vars = overrideConfig[config]
1044+
for (const variable in vars) {
1045+
const override = variableOverrides.find((v) => v.name === variable)
1046+
if (!override?.enabled) {
1047+
continue // Skip this variable if it's not enabled for override
10261048
}
1027-
continue
1028-
} else if (nodeIds.some((nodeId) => nodeId.includes(flowNodeData.name))) {
1029-
/*
1030-
* "systemMessagePrompt": {
1031-
* "chatPromptTemplate_0": "You are an assistant" <---- continue for loop if current node is chatPromptTemplate_1
1032-
* }
1033-
*/
1034-
continue
1049+
filteredVars[variable] = vars[variable]
10351050
}
1051+
overrideConfig[config] = filteredVars
10361052
}
1037-
1038-
// Only proceed if the parameter is enabled for this node type
1053+
} else if (overrideConfig[config] && typeof overrideConfig[config] === 'object') {
1054+
const nodeIds = Object.keys(overrideConfig[config])
1055+
if (nodeIds.includes(flowNodeData.id)) {
1056+
// Check if this parameter is enabled
1057+
if (isParameterEnabled(flowNodeData.label, config)) {
1058+
inputsObj[config] = overrideConfig[config][flowNodeData.id]
1059+
}
1060+
continue
1061+
} else if (nodeIds.some((nodeId) => nodeId.includes(flowNodeData.name))) {
1062+
/*
1063+
* "systemMessagePrompt": {
1064+
* "chatPromptTemplate_0": "You are an assistant" <---- continue for loop if current node is chatPromptTemplate_1
1065+
* }
1066+
*/
1067+
continue
1068+
}
1069+
} else {
1070+
// Only proceed if the parameter is enabled
10391071
if (!isParameterEnabled(flowNodeData.label, config)) {
10401072
continue
10411073
}

0 commit comments

Comments
 (0)