Bugfix/Array Input Variables (#5196)

- Replace manual template variable processing in multiple components with a new utility function `processTemplateVariables`.
This commit is contained in:
Henry Heng
2025-09-12 14:42:34 +01:00
committed by GitHub
parent 736c2b11a1
commit 4987a2880d
9 changed files with 199 additions and 81 deletions
+48 -1
View File
@@ -8,7 +8,7 @@ import TurndownService from 'turndown'
import { DataSource, Equal } from 'typeorm'
import { ICommonObject, IDatabaseEntity, IFileUpload, IMessage, INodeData, IVariable, MessageContentImageUrl } from './Interface'
import { AES, enc } from 'crypto-js'
import { omit } from 'lodash'
import { omit, get } from 'lodash'
import { AIMessage, HumanMessage, BaseMessage } from '@langchain/core/messages'
import { Document } from '@langchain/core/documents'
import { getFileFromStorage } from './storageUtils'
@@ -1609,3 +1609,50 @@ export const createCodeExecutionSandbox = (
return sandbox
}
/**
* Process template variables in state object, replacing {{ output }} and {{ output.property }} patterns
* @param {ICommonObject} state - The state object to process
* @param {any} finalOutput - The output value to substitute
* @returns {ICommonObject} - The processed state object
*/
export const processTemplateVariables = (state: ICommonObject, finalOutput: any): ICommonObject => {
if (!state || Object.keys(state).length === 0) {
return state
}
const newState = { ...state }
for (const key in newState) {
const stateValue = newState[key].toString()
if (stateValue.includes('{{ output') || stateValue.includes('{{output')) {
// Handle simple output replacement (with or without spaces)
if (stateValue === '{{ output }}' || stateValue === '{{output}}') {
newState[key] = finalOutput
continue
}
// Handle JSON path expressions like {{ output.updated }} or {{output.updated}}
// eslint-disable-next-line
const match = stateValue.match(/\{\{\s*output\.([\w\.]+)\s*\}\}/)
if (match) {
try {
// Parse the response if it's JSON
const jsonResponse = typeof finalOutput === 'string' ? JSON.parse(finalOutput) : finalOutput
// Get the value using lodash get
const path = match[1]
const value = get(jsonResponse, path)
newState[key] = value ?? stateValue // Fall back to original if path not found
} catch (e) {
// If JSON parsing fails, keep original template
newState[key] = stateValue
}
} else {
// Handle simple {{ output }} replacement for backward compatibility
newState[key] = newState[key].replaceAll('{{ output }}', finalOutput)
}
}
}
return newState
}