(
paramValue: string | object,
reactFlowNodes: IReactFlowNode[],
question: string,
chatHistory: IMessage[],
isAcceptVariable = false,
flowConfig?: ICommonObject,
uploadedFilesContent?: string,
availableVariables: IVariable[] = [],
variableOverrides: ICommonObject[] = []
)
| 872 | * @returns {string} |
| 873 | */ |
| 874 | export const getVariableValue = async ( |
| 875 | paramValue: string | object, |
| 876 | reactFlowNodes: IReactFlowNode[], |
| 877 | question: string, |
| 878 | chatHistory: IMessage[], |
| 879 | isAcceptVariable = false, |
| 880 | flowConfig?: ICommonObject, |
| 881 | uploadedFilesContent?: string, |
| 882 | availableVariables: IVariable[] = [], |
| 883 | variableOverrides: ICommonObject[] = [] |
| 884 | ) => { |
| 885 | const isObject = typeof paramValue === 'object' |
| 886 | const initialValue = (isObject ? JSON.stringify(paramValue) : paramValue) ?? '' |
| 887 | let returnVal = initialValue |
| 888 | const variableStack = [] |
| 889 | const variableDict = {} as IVariableDict |
| 890 | let startIdx = 0 |
| 891 | const endIdx = initialValue.length - 1 |
| 892 | |
| 893 | while (startIdx < endIdx) { |
| 894 | const substr = initialValue.substring(startIdx, startIdx + 2) |
| 895 | |
| 896 | // Store the opening double curly bracket |
| 897 | if (substr === '{{') { |
| 898 | variableStack.push({ substr, startIdx: startIdx + 2 }) |
| 899 | } |
| 900 | |
| 901 | // Found the complete variable |
| 902 | if (substr === '}}' && variableStack.length > 0 && variableStack[variableStack.length - 1].substr === '{{') { |
| 903 | const variableStartIdx = variableStack[variableStack.length - 1].startIdx |
| 904 | const variableEndIdx = startIdx |
| 905 | const variableFullPath = initialValue.substring(variableStartIdx, variableEndIdx) |
| 906 | |
| 907 | /** |
| 908 | * Apply string transformation to convert special chars: |
| 909 | * FROM: hello i am ben\n\n\thow are you? |
| 910 | * TO: hello i am benFLOWISE_NEWLINEFLOWISE_NEWLINEFLOWISE_TABhow are you? |
| 911 | */ |
| 912 | if (isAcceptVariable && variableFullPath === QUESTION_VAR_PREFIX) { |
| 913 | variableDict[`{{${variableFullPath}}}`] = handleEscapeCharacters(question, false) |
| 914 | } |
| 915 | |
| 916 | if (isAcceptVariable && variableFullPath === FILE_ATTACHMENT_PREFIX) { |
| 917 | variableDict[`{{${variableFullPath}}}`] = handleEscapeCharacters(uploadedFilesContent, false) |
| 918 | } |
| 919 | |
| 920 | if (isAcceptVariable && variableFullPath === CHAT_HISTORY_VAR_PREFIX) { |
| 921 | variableDict[`{{${variableFullPath}}}`] = handleEscapeCharacters(convertChatHistoryToText(chatHistory), false) |
| 922 | } |
| 923 | |
| 924 | if (variableFullPath.startsWith('$vars.')) { |
| 925 | const vars = await getGlobalVariable(flowConfig, availableVariables, variableOverrides) |
| 926 | const variableValue = get(vars, variableFullPath.replace('$vars.', '')) |
| 927 | if (variableValue != null) { |
| 928 | variableDict[`{{${variableFullPath}}}`] = variableValue |
| 929 | returnVal = returnVal.split(`{{${variableFullPath}}}`).join(variableValue) |
| 930 | } |
| 931 | } |
no test coverage detected