MCPcopy
hub / github.com/FlowiseAI/Flowise / getChatHistory

Function getChatHistory

packages/server/src/utils/buildChatflow.ts:208–279  ·  view source on GitHub ↗
({
    endingNodes,
    nodes,
    chatflowid,
    appDataSource,
    componentNodes,
    incomingInput,
    chatId,
    isInternal,
    isAgentFlow
}: {
    endingNodes: IReactFlowNode[]
    nodes: IReactFlowNode[]
    chatflowid: string
    appDataSource: DataSource
    componentNodes: IComponentNodes
    incomingInput: IncomingInput
    chatId: string
    isInternal: boolean
    isAgentFlow: boolean
})

Source from the content-addressed store, hash-verified

206 * This is used to fill in the {{chat_history}} variable if it is used in the Format Prompt Value
207 */
208const getChatHistory = async ({
209 endingNodes,
210 nodes,
211 chatflowid,
212 appDataSource,
213 componentNodes,
214 incomingInput,
215 chatId,
216 isInternal,
217 isAgentFlow
218}: {
219 endingNodes: IReactFlowNode[]
220 nodes: IReactFlowNode[]
221 chatflowid: string
222 appDataSource: DataSource
223 componentNodes: IComponentNodes
224 incomingInput: IncomingInput
225 chatId: string
226 isInternal: boolean
227 isAgentFlow: boolean
228}): Promise<IMessage[]> => {
229 const prependMessages = incomingInput.history ?? []
230 let chatHistory: IMessage[] = []
231
232 if (isAgentFlow) {
233 const startNode = nodes.find((node) => node.data.name === 'seqStart')
234 if (!startNode?.data?.inputs?.agentMemory) return prependMessages
235
236 const memoryNodeId = startNode.data.inputs.agentMemory.split('.')[0].replace('{{', '')
237 const memoryNode = nodes.find((node) => node.data.id === memoryNodeId)
238
239 if (memoryNode) {
240 chatHistory = await getSessionChatHistory(
241 chatflowid,
242 getMemorySessionId(memoryNode, incomingInput, chatId, isInternal),
243 memoryNode,
244 componentNodes,
245 appDataSource,
246 databaseEntities,
247 logger,
248 prependMessages
249 )
250 }
251 return chatHistory
252 }
253
254 /* In case there are multiple ending nodes, get the memory from the last available ending node
255 * By right, in each flow, there should only be one memory node
256 */
257 for (const endingNode of endingNodes) {
258 const endingNodeData = endingNode.data
259 if (!endingNodeData.inputs?.memory) continue
260
261 const memoryNodeId = endingNodeData.inputs?.memory.split('.')[0].replace('{{', '')
262 const memoryNode = nodes.find((node) => node.data.id === memoryNodeId)
263
264 if (!memoryNode) continue
265

Callers 1

executeFlowFunction · 0.85

Calls 2

getSessionChatHistoryFunction · 0.90
getMemorySessionIdFunction · 0.90

Tested by

no test coverage detected