Feature: Add access to chat history and other useful variables in post-processing (#5511)

* access chat history and other useful variables in post-processing

* cloning data to prevent mutations in post-processing

* Enhance post-processing capabilities by adding support for additional variables and improving the UI for available variables display. Update CustomFunction implementations to utilize post-processing options consistently across components.

---------

Co-authored-by: Henry <hzj94@hotmail.com>
This commit is contained in:
Nikitas Papadopoulos
2025-11-27 13:59:00 +01:00
committed by GitHub
parent 562370b8e2
commit 6a59af11e6
6 changed files with 203 additions and 36 deletions
+56 -1
View File
@@ -2122,7 +2122,62 @@ export const executeAgentFlow = async ({
// check if last agentFlowExecutedData.data.output contains the key "content"
const lastNodeOutput = agentFlowExecutedData[agentFlowExecutedData.length - 1].data?.output as ICommonObject | undefined
const content = (lastNodeOutput?.content as string) ?? ' '
let content = (lastNodeOutput?.content as string) ?? ' '
/* Check for post-processing settings */
let chatflowConfig: ICommonObject = {}
try {
if (chatflow.chatbotConfig) {
chatflowConfig = typeof chatflow.chatbotConfig === 'string' ? JSON.parse(chatflow.chatbotConfig) : chatflow.chatbotConfig
}
} catch (e) {
logger.error('[server]: Error parsing chatflow config:', e)
}
if (chatflowConfig?.postProcessing?.enabled === true && content) {
try {
const postProcessingFunction = JSON.parse(chatflowConfig?.postProcessing?.customFunction)
const nodeInstanceFilePath = componentNodes['customFunctionAgentflow'].filePath as string
const nodeModule = await import(nodeInstanceFilePath)
//set the outputs.output to EndingNode to prevent json escaping of content...
const nodeData = {
inputs: { customFunctionJavascriptFunction: postProcessingFunction }
}
const runtimeChatHistory = agentflowRuntime.chatHistory || []
const chatHistory = [...pastChatHistory, ...runtimeChatHistory]
const options: ICommonObject = {
chatflowid: chatflow.id,
sessionId,
chatId,
input: question || form,
postProcessing: {
rawOutput: content,
chatHistory: cloneDeep(chatHistory),
sourceDocuments: lastNodeOutput?.sourceDocuments ? cloneDeep(lastNodeOutput.sourceDocuments) : undefined,
usedTools: lastNodeOutput?.usedTools ? cloneDeep(lastNodeOutput.usedTools) : undefined,
artifacts: lastNodeOutput?.artifacts ? cloneDeep(lastNodeOutput.artifacts) : undefined,
fileAnnotations: lastNodeOutput?.fileAnnotations ? cloneDeep(lastNodeOutput.fileAnnotations) : undefined
},
appDataSource,
databaseEntities,
workspaceId,
orgId,
logger
}
const customFuncNodeInstance = new nodeModule.nodeClass()
const customFunctionResponse = await customFuncNodeInstance.run(nodeData, question || form, options)
const moderatedResponse = customFunctionResponse.output.content
if (typeof moderatedResponse === 'string') {
content = moderatedResponse
} else if (typeof moderatedResponse === 'object') {
content = '```json\n' + JSON.stringify(moderatedResponse, null, 2) + '\n```'
} else {
content = moderatedResponse
}
} catch (e) {
logger.error('[server]: Post Processing Error:', e)
}
}
// remove credentialId from agentFlowExecutedData
agentFlowExecutedData = agentFlowExecutedData.map((data) => _removeCredentialId(data))
+9 -2
View File
@@ -2,7 +2,7 @@ import { Request } from 'express'
import * as path from 'path'
import { DataSource } from 'typeorm'
import { v4 as uuidv4 } from 'uuid'
import { omit } from 'lodash'
import { omit, cloneDeep } from 'lodash'
import {
IFileUpload,
convertSpeechToText,
@@ -817,7 +817,14 @@ export const executeFlow = async ({
sessionId,
chatId,
input: question,
rawOutput: resultText,
postProcessing: {
rawOutput: resultText,
chatHistory: cloneDeep(chatHistory),
sourceDocuments: result?.sourceDocuments ? cloneDeep(result.sourceDocuments) : undefined,
usedTools: result?.usedTools ? cloneDeep(result.usedTools) : undefined,
artifacts: result?.artifacts ? cloneDeep(result.artifacts) : undefined,
fileAnnotations: result?.fileAnnotations ? cloneDeep(result.fileAnnotations) : undefined
},
appDataSource,
databaseEntities,
workspaceId,