Skip to content

Commit 5c54162

Browse files
authored
Chore/remove app server functions from utils file (#3671)
* remove app server functions from utils file * hide override config variables if its empty
1 parent ddca80d commit 5c54162

File tree

7 files changed

+55
-16
lines changed

7 files changed

+55
-16
lines changed

packages/server/src/services/documentstore/index.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,7 @@ const updateVectorStoreConfigOnly = async (data: ICommonObject) => {
914914
)
915915
}
916916
}
917-
const saveVectorStoreConfig = async (data: ICommonObject) => {
917+
const saveVectorStoreConfig = async (data: ICommonObject, isStrictSave = true) => {
918918
try {
919919
const appServer = getRunningExpressApp()
920920
const entity = await appServer.AppDataSource.getRepository(DocumentStore).findOneBy({
@@ -932,6 +932,7 @@ const saveVectorStoreConfig = async (data: ICommonObject) => {
932932
} else if (entity.embeddingConfig && !data.embeddingName && !data.embeddingConfig) {
933933
data.embeddingConfig = JSON.parse(entity.embeddingConfig)?.config
934934
data.embeddingName = JSON.parse(entity.embeddingConfig)?.name
935+
if (isStrictSave) entity.embeddingConfig = null
935936
} else if (!data.embeddingName && !data.embeddingConfig) {
936937
entity.embeddingConfig = null
937938
}
@@ -944,6 +945,7 @@ const saveVectorStoreConfig = async (data: ICommonObject) => {
944945
} else if (entity.vectorStoreConfig && !data.vectorStoreName && !data.vectorStoreConfig) {
945946
data.vectorStoreConfig = JSON.parse(entity.vectorStoreConfig)?.config
946947
data.vectorStoreName = JSON.parse(entity.vectorStoreConfig)?.name
948+
if (isStrictSave) entity.vectorStoreConfig = null
947949
} else if (!data.vectorStoreName && !data.vectorStoreConfig) {
948950
entity.vectorStoreConfig = null
949951
}
@@ -956,6 +958,7 @@ const saveVectorStoreConfig = async (data: ICommonObject) => {
956958
} else if (entity.recordManagerConfig && !data.recordManagerName && !data.recordManagerConfig) {
957959
data.recordManagerConfig = JSON.parse(entity.recordManagerConfig)?.config
958960
data.recordManagerName = JSON.parse(entity.recordManagerConfig)?.name
961+
if (isStrictSave) entity.recordManagerConfig = null
959962
} else if (!data.recordManagerName && !data.recordManagerConfig) {
960963
entity.recordManagerConfig = null
961964
}
@@ -975,15 +978,15 @@ const saveVectorStoreConfig = async (data: ICommonObject) => {
975978
}
976979
}
977980

978-
const insertIntoVectorStore = async (data: ICommonObject) => {
981+
const insertIntoVectorStore = async (data: ICommonObject, isStrictSave = true) => {
979982
try {
980983
const appServer = getRunningExpressApp()
981-
const entity = await saveVectorStoreConfig(data)
984+
const entity = await saveVectorStoreConfig(data, isStrictSave)
982985
entity.status = DocumentStoreStatus.UPSERTING
983986
await appServer.AppDataSource.getRepository(DocumentStore).save(entity)
984987

985988
// TODO: to be moved into a worker thread...
986-
const indexResult = await _insertIntoVectorStoreWorkerThread(data)
989+
const indexResult = await _insertIntoVectorStoreWorkerThread(data, isStrictSave)
987990
return indexResult
988991
} catch (error) {
989992
throw new InternalFlowiseError(
@@ -993,10 +996,10 @@ const insertIntoVectorStore = async (data: ICommonObject) => {
993996
}
994997
}
995998

996-
const _insertIntoVectorStoreWorkerThread = async (data: ICommonObject) => {
999+
const _insertIntoVectorStoreWorkerThread = async (data: ICommonObject, isStrictSave = true) => {
9971000
try {
9981001
const appServer = getRunningExpressApp()
999-
const entity = await saveVectorStoreConfig(data)
1002+
const entity = await saveVectorStoreConfig(data, isStrictSave)
10001003
let upsertHistory: Record<string, any> = {}
10011004
const chatflowid = data.storeId // fake chatflowid because this is not tied to any chatflow
10021005

@@ -1520,7 +1523,7 @@ const upsertDocStoreMiddleware = async (
15201523
recordManagerConfig
15211524
}
15221525

1523-
const res = await insertIntoVectorStore(insertData)
1526+
const res = await insertIntoVectorStore(insertData, false)
15241527
res.docId = newDocId
15251528

15261529
return res

packages/server/src/services/openai-realtime/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { IDepthQueue, IReactFlowNode } from '../../Interface'
1616
import { ICommonObject, INodeData } from 'flowise-components'
1717
import { convertToOpenAIFunction } from '@langchain/core/utils/function_calling'
1818
import { v4 as uuidv4 } from 'uuid'
19+
import { Variable } from '../../database/entities/Variable'
1920

2021
const SOURCE_DOCUMENTS_PREFIX = '\n\n----FLOWISE_SOURCE_DOCUMENTS----\n\n'
2122
const ARTIFACTS_PREFIX = '\n\n----FLOWISE_ARTIFACTS----\n\n'
@@ -59,6 +60,7 @@ const buildAndInitTool = async (chatflowid: string, _chatId?: string, _apiMessag
5960
}
6061
startingNodeIds = [...new Set(startingNodeIds)]
6162

63+
const availableVariables = await appServer.AppDataSource.getRepository(Variable).find()
6264
const { nodeOverrides, variableOverrides, apiOverrideStatus } = getAPIOverrideConfig(chatflow)
6365

6466
const reactFlowNodes = await buildFlow({
@@ -77,6 +79,7 @@ const buildAndInitTool = async (chatflowid: string, _chatId?: string, _apiMessag
7779
appDataSource: appServer.AppDataSource,
7880
apiOverrideStatus,
7981
nodeOverrides,
82+
availableVariables,
8083
variableOverrides
8184
})
8285

@@ -99,6 +102,7 @@ const buildAndInitTool = async (chatflowid: string, _chatId?: string, _apiMessag
99102
[],
100103
flowDataObj,
101104
'',
105+
availableVariables,
102106
variableOverrides
103107
)
104108
let nodeToExecuteData = reactFlowNodeData

packages/server/src/utils/buildAgentGraph.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import { replaceInputsWithConfig, resolveVariables } from '.'
4545
import { InternalFlowiseError } from '../errors/internalFlowiseError'
4646
import { getErrorMessage } from '../errors/utils'
4747
import logger from './logger'
48+
import { Variable } from '../database/entities/Variable'
4849

4950
/**
5051
* Build Agent Graph
@@ -114,6 +115,7 @@ export const buildAgentGraph = async (
114115
}
115116

116117
/*** Get API Config ***/
118+
const availableVariables = await appServer.AppDataSource.getRepository(Variable).find()
117119
const { nodeOverrides, variableOverrides, apiOverrideStatus } = getAPIOverrideConfig(chatflow)
118120

119121
// Initialize nodes like ChatModels, Tools, etc.
@@ -135,6 +137,7 @@ export const buildAgentGraph = async (
135137
overrideConfig: incomingInput?.overrideConfig,
136138
apiOverrideStatus,
137139
nodeOverrides,
140+
availableVariables,
138141
variableOverrides,
139142
cachePool: appServer.cachePool,
140143
isUpsert: false,
@@ -519,6 +522,7 @@ const compileMultiAgentsGraph = async (params: MultiAgentsGraphParams) => {
519522
const workerNodes = reactFlowNodes.filter((node) => workerNodeIds.includes(node.data.id))
520523

521524
/*** Get API Config ***/
525+
const availableVariables = await appServer.AppDataSource.getRepository(Variable).find()
522526
const { nodeOverrides, variableOverrides, apiOverrideStatus } = getAPIOverrideConfig(chatflow)
523527

524528
let supervisorWorkers: { [key: string]: IMultiAgentNode[] } = {}
@@ -540,6 +544,7 @@ const compileMultiAgentsGraph = async (params: MultiAgentsGraphParams) => {
540544
chatHistory,
541545
overrideConfig,
542546
uploadedFilesContent,
547+
availableVariables,
543548
variableOverrides
544549
)
545550

@@ -581,6 +586,7 @@ const compileMultiAgentsGraph = async (params: MultiAgentsGraphParams) => {
581586
chatHistory,
582587
overrideConfig,
583588
uploadedFilesContent,
589+
availableVariables,
584590
variableOverrides
585591
)
586592

@@ -753,6 +759,9 @@ const compileSeqAgentsGraph = async (params: SeqAgentsGraphParams) => {
753759
let conditionalToolNodes: Record<string, { source: ISeqAgentNode; toolNodes: ISeqAgentNode[] }> = {}
754760
let bindModel: Record<string, any> = {}
755761
let interruptToolNodeNames = []
762+
763+
/*** Get API Config ***/
764+
const availableVariables = await appServer.AppDataSource.getRepository(Variable).find()
756765
const { nodeOverrides, variableOverrides, apiOverrideStatus } = getAPIOverrideConfig(chatflow)
757766

758767
const initiateNode = async (node: IReactFlowNode) => {
@@ -771,6 +780,7 @@ const compileSeqAgentsGraph = async (params: SeqAgentsGraphParams) => {
771780
chatHistory,
772781
overrideConfig,
773782
uploadedFilesContent,
783+
availableVariables,
774784
variableOverrides
775785
)
776786

packages/server/src/utils/buildChatflow.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import { getErrorMessage } from '../errors/utils'
5757
import { ChatMessage } from '../database/entities/ChatMessage'
5858
import { IAction } from 'flowise-components'
5959
import { FLOWISE_METRIC_COUNTERS, FLOWISE_COUNTER_STATUS } from '../Interface.Metrics'
60+
import { Variable } from '../database/entities/Variable'
6061

6162
/**
6263
* Build Chatflow
@@ -350,6 +351,7 @@ export const utilBuildChatflow = async (req: Request, isInternal: boolean = fals
350351
const startingNodes = nodes.filter((nd) => startingNodeIds.includes(nd.id))
351352

352353
/*** Get API Config ***/
354+
const availableVariables = await appServer.AppDataSource.getRepository(Variable).find()
353355
const { nodeOverrides, variableOverrides, apiOverrideStatus } = getAPIOverrideConfig(chatflow)
354356

355357
logger.debug(`[server]: Start building chatflow ${chatflowid}`)
@@ -373,6 +375,7 @@ export const utilBuildChatflow = async (req: Request, isInternal: boolean = fals
373375
overrideConfig: incomingInput?.overrideConfig,
374376
apiOverrideStatus,
375377
nodeOverrides,
378+
availableVariables,
376379
variableOverrides,
377380
cachePool: appServer.cachePool,
378381
isUpsert: false,
@@ -427,6 +430,7 @@ export const utilBuildChatflow = async (req: Request, isInternal: boolean = fals
427430
chatHistory,
428431
flowData,
429432
uploadedFilesContent,
433+
availableVariables,
430434
variableOverrides
431435
)
432436
nodeToExecuteData = reactFlowNodeData

packages/server/src/utils/index.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/**
2+
* Strictly no getRepository, appServer here, must be passed as parameter
3+
*/
4+
15
import path from 'path'
26
import fs from 'fs'
37
import logger from './logger'
@@ -17,6 +21,7 @@ import {
1721
IOverrideConfig,
1822
IReactFlowEdge,
1923
IReactFlowNode,
24+
IVariable,
2025
IVariableDict,
2126
IVariableOverride,
2227
IncomingInput
@@ -439,6 +444,7 @@ type BuildFlowParams = {
439444
overrideConfig?: ICommonObject
440445
apiOverrideStatus?: boolean
441446
nodeOverrides?: INodeOverrides
447+
availableVariables?: IVariable[]
442448
variableOverrides?: IVariableOverride[]
443449
cachePool?: CachePool
444450
isUpsert?: boolean
@@ -470,6 +476,7 @@ export const buildFlow = async ({
470476
overrideConfig,
471477
apiOverrideStatus = false,
472478
nodeOverrides = {},
479+
availableVariables = [],
473480
variableOverrides = [],
474481
cachePool,
475482
isUpsert,
@@ -534,6 +541,7 @@ export const buildFlow = async ({
534541
chatHistory,
535542
flowData,
536543
uploadedFilesContent,
544+
availableVariables,
537545
variableOverrides
538546
)
539547

@@ -727,9 +735,12 @@ export const clearSessionMemory = async (
727735
}
728736
}
729737

730-
const getGlobalVariable = async (appDataSource: DataSource, overrideConfig?: ICommonObject, variableOverrides?: ICommonObject[]) => {
731-
const variables = await appDataSource.getRepository(Variable).find()
732-
738+
const getGlobalVariable = async (
739+
appDataSource: DataSource,
740+
overrideConfig?: ICommonObject,
741+
availableVariables: IVariable[] = [],
742+
variableOverrides?: ICommonObject[]
743+
) => {
733744
// override variables defined in overrideConfig
734745
// nodeData.inputs.vars is an Object, check each property and override the variable
735746
if (overrideConfig?.vars && variableOverrides) {
@@ -740,14 +751,14 @@ const getGlobalVariable = async (appDataSource: DataSource, overrideConfig?: ICo
740751
continue // Skip this variable if it's not enabled for override
741752
}
742753

743-
const foundVar = variables.find((v) => v.name === propertyName)
754+
const foundVar = availableVariables.find((v) => v.name === propertyName)
744755
if (foundVar) {
745756
// even if the variable was defined as runtime, we override it with static value
746757
foundVar.type = 'static'
747758
foundVar.value = overrideConfig.vars[propertyName]
748759
} else {
749760
// add it the variables, if not found locally in the db
750-
variables.push({
761+
availableVariables.push({
751762
name: propertyName,
752763
type: 'static',
753764
value: overrideConfig.vars[propertyName],
@@ -760,8 +771,8 @@ const getGlobalVariable = async (appDataSource: DataSource, overrideConfig?: ICo
760771
}
761772

762773
let vars = {}
763-
if (variables.length) {
764-
for (const item of variables) {
774+
if (availableVariables.length) {
775+
for (const item of availableVariables) {
765776
let value = item.value
766777

767778
// read from .env file
@@ -797,6 +808,7 @@ export const getVariableValue = async (
797808
isAcceptVariable = false,
798809
flowData?: ICommonObject,
799810
uploadedFilesContent?: string,
811+
availableVariables: IVariable[] = [],
800812
variableOverrides: ICommonObject[] = []
801813
) => {
802814
const isObject = typeof paramValue === 'object'
@@ -839,7 +851,7 @@ export const getVariableValue = async (
839851
}
840852

841853
if (variableFullPath.startsWith('$vars.')) {
842-
const vars = await getGlobalVariable(appDataSource, flowData, variableOverrides)
854+
const vars = await getGlobalVariable(appDataSource, flowData, availableVariables, variableOverrides)
843855
const variableValue = get(vars, variableFullPath.replace('$vars.', ''))
844856
if (variableValue) {
845857
variableDict[`{{${variableFullPath}}}`] = variableValue
@@ -949,6 +961,7 @@ export const resolveVariables = async (
949961
chatHistory: IMessage[],
950962
flowData?: ICommonObject,
951963
uploadedFilesContent?: string,
964+
availableVariables: IVariable[] = [],
952965
variableOverrides: ICommonObject[] = []
953966
): Promise<INodeData> => {
954967
let flowNodeData = cloneDeep(reactFlowNodeData)
@@ -969,6 +982,7 @@ export const resolveVariables = async (
969982
undefined,
970983
flowData,
971984
uploadedFilesContent,
985+
availableVariables,
972986
variableOverrides
973987
)
974988
resolvedInstances.push(resolvedInstance)
@@ -985,6 +999,7 @@ export const resolveVariables = async (
985999
isAcceptVariable,
9861000
flowData,
9871001
uploadedFilesContent,
1002+
availableVariables,
9881003
variableOverrides
9891004
)
9901005
paramsObj[key] = resolvedInstance

packages/server/src/utils/upsertVector.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { StatusCodes } from 'http-status-codes'
2525
import { getErrorMessage } from '../errors/utils'
2626
import { v4 as uuidv4 } from 'uuid'
2727
import { FLOWISE_COUNTER_STATUS, FLOWISE_METRIC_COUNTERS } from '../Interface.Metrics'
28+
import { Variable } from '../database/entities/Variable'
2829
/**
2930
* Upsert documents
3031
* @param {Request} req
@@ -157,6 +158,7 @@ export const upsertVector = async (req: Request, isInternal: boolean = false) =>
157158
const { startingNodeIds, depthQueue } = getStartingNodes(filteredGraph, stopNodeId)
158159

159160
/*** Get API Config ***/
161+
const availableVariables = await appServer.AppDataSource.getRepository(Variable).find()
160162
const { nodeOverrides, variableOverrides, apiOverrideStatus } = getAPIOverrideConfig(chatflow)
161163

162164
// For "files" input, add a new node override with the actual input name such as pdfFile, txtFile, etc.
@@ -189,6 +191,7 @@ export const upsertVector = async (req: Request, isInternal: boolean = false) =>
189191
overrideConfig: incomingInput?.overrideConfig,
190192
apiOverrideStatus,
191193
nodeOverrides,
194+
availableVariables,
192195
variableOverrides,
193196
cachePool: appServer.cachePool,
194197
isUpsert,

packages/ui/src/ui-component/extended/OverrideConfig.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ const OverrideConfig = ({ dialogProps }) => {
400400
</Stack>
401401
</Card>
402402
)}
403-
{variableOverrides && (
403+
{variableOverrides && variableOverrides.length > 0 && (
404404
<Card sx={{ borderColor: theme.palette.primary[200] + 75, p: 2 }} variant='outlined'>
405405
<Stack sx={{ mt: 1, mb: 2, ml: 1, alignItems: 'center' }} direction='row' spacing={2}>
406406
<IconVariable />

0 commit comments

Comments
 (0)