feat:Adds new Param in getChatMessages for better context handling (#4273)

* Adds Current Message Param in getChatMessages to use Mem0 more effectively

* Revert "Adds Current Message Param in getChatMessages to use Mem0 more effectively"

This reverts commit 086be60b0a717aaf2c9a2b95ac1c0f809bf21e3b.

* Used the input param for Getting the Input text inside of Mem0 Node

* Update pnpm-lock.yaml

* Update pnpm-lock.yaml

---------

Co-authored-by: Henry <hzj94@hotmail.com>
Co-authored-by: Henry Heng <henryheng@flowiseai.com>
This commit is contained in:
Saket Aryan
2025-05-28 20:32:52 +05:30
committed by GitHub
parent 27bc47ed57
commit 9682a0ccd9
+37 -26
View File
@@ -15,6 +15,12 @@ interface BufferMemoryExtendedInput {
chatflowid: string chatflowid: string
} }
interface NodeFields extends Mem0MemoryInput, Mem0MemoryExtendedInput, BufferMemoryExtendedInput {
searchOnly: boolean
useFlowiseChatId: boolean
input: string
}
class Mem0_Memory implements INode { class Mem0_Memory implements INode {
label: string label: string
name: string name: string
@@ -143,12 +149,12 @@ class Mem0_Memory implements INode {
] ]
} }
async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> { async init(nodeData: INodeData, input: string, options: ICommonObject): Promise<any> {
return await initializeMem0(nodeData, options) return await initializeMem0(nodeData, input, options)
} }
} }
const initializeMem0 = async (nodeData: INodeData, options: ICommonObject): Promise<BaseMem0Memory> => { const initializeMem0 = async (nodeData: INodeData, input: string, options: ICommonObject): Promise<BaseMem0Memory> => {
const initialUserId = nodeData.inputs?.user_id as string const initialUserId = nodeData.inputs?.user_id as string
const useFlowiseChatId = nodeData.inputs?.useFlowiseChatId as boolean const useFlowiseChatId = nodeData.inputs?.useFlowiseChatId as boolean
const orgId = options.orgId as string const orgId = options.orgId as string
@@ -184,24 +190,24 @@ const initializeMem0 = async (nodeData: INodeData, options: ICommonObject): Prom
filters: (nodeData.inputs?.filters as Record<string, any>) || {} filters: (nodeData.inputs?.filters as Record<string, any>) || {}
} }
const obj: Mem0MemoryInput & Mem0MemoryExtendedInput & BufferMemoryExtendedInput & { searchOnly: boolean; useFlowiseChatId: boolean } = const obj: NodeFields = {
{ apiKey: apiKey,
apiKey: apiKey, humanPrefix: nodeData.inputs?.humanPrefix as string,
humanPrefix: nodeData.inputs?.humanPrefix as string, aiPrefix: nodeData.inputs?.aiPrefix as string,
aiPrefix: nodeData.inputs?.aiPrefix as string, inputKey: nodeData.inputs?.inputKey as string,
inputKey: nodeData.inputs?.inputKey as string, sessionId: constructorSessionId,
sessionId: constructorSessionId, mem0Options: mem0Options,
mem0Options: mem0Options, memoryOptions: memoryOptions,
memoryOptions: memoryOptions, separateMessages: false,
separateMessages: false, returnMessages: false,
returnMessages: false, appDataSource: options.appDataSource as DataSource,
appDataSource: options.appDataSource as DataSource, databaseEntities: options.databaseEntities as IDatabaseEntity,
databaseEntities: options.databaseEntities as IDatabaseEntity, chatflowid: options.chatflowid as string,
chatflowid: options.chatflowid as string, searchOnly: (nodeData.inputs?.searchOnly as boolean) || false,
searchOnly: (nodeData.inputs?.searchOnly as boolean) || false, useFlowiseChatId: useFlowiseChatId,
useFlowiseChatId: useFlowiseChatId, input: input,
orgId: orgId orgId: orgId
} }
return new Mem0MemoryExtended(obj) return new Mem0MemoryExtended(obj)
} }
@@ -223,10 +229,9 @@ class Mem0MemoryExtended extends BaseMem0Memory implements MemoryMethods {
chatflowid: string chatflowid: string
searchOnly: boolean searchOnly: boolean
useFlowiseChatId: boolean useFlowiseChatId: boolean
input: string
constructor( constructor(fields: NodeFields) {
fields: Mem0MemoryInput & Mem0MemoryExtendedInput & BufferMemoryExtendedInput & { searchOnly: boolean; useFlowiseChatId: boolean }
) {
super(fields) super(fields)
this.initialUserId = fields.memoryOptions?.user_id ?? '' this.initialUserId = fields.memoryOptions?.user_id ?? ''
this.userId = this.initialUserId this.userId = this.initialUserId
@@ -237,6 +242,7 @@ class Mem0MemoryExtended extends BaseMem0Memory implements MemoryMethods {
this.chatflowid = fields.chatflowid this.chatflowid = fields.chatflowid
this.searchOnly = fields.searchOnly this.searchOnly = fields.searchOnly
this.useFlowiseChatId = fields.useFlowiseChatId this.useFlowiseChatId = fields.useFlowiseChatId
this.input = fields.input
this.orgId = fields.orgId this.orgId = fields.orgId
} }
@@ -323,11 +329,16 @@ class Mem0MemoryExtended extends BaseMem0Memory implements MemoryMethods {
if (prependMessages?.length) { if (prependMessages?.length) {
returnIMessages.unshift(...prependMessages) returnIMessages.unshift(...prependMessages)
// Reverted to original simpler unshift // Reverted to original simpler unshift
chatMessage.unshift(...(prependMessages as any)) // Cast as any chatMessage.unshift(...(prependMessages as any))
} }
if (returnBaseMessages) { if (returnBaseMessages) {
const memoryVariables = await this.loadMemoryVariables({}, overrideUserId) const memoryVariables = await this.loadMemoryVariables(
{
[this.inputKey]: this.input ?? ''
},
overrideUserId
)
const mem0History = memoryVariables[this.memoryKey] const mem0History = memoryVariables[this.memoryKey]
if (mem0History && typeof mem0History === 'string') { if (mem0History && typeof mem0History === 'string') {