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
}
interface NodeFields extends Mem0MemoryInput, Mem0MemoryExtendedInput, BufferMemoryExtendedInput {
searchOnly: boolean
useFlowiseChatId: boolean
input: string
}
class Mem0_Memory implements INode {
label: string
name: string
@@ -143,12 +149,12 @@ class Mem0_Memory implements INode {
]
}
async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> {
return await initializeMem0(nodeData, options)
async init(nodeData: INodeData, input: string, options: ICommonObject): Promise<any> {
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 useFlowiseChatId = nodeData.inputs?.useFlowiseChatId as boolean
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>) || {}
}
const obj: Mem0MemoryInput & Mem0MemoryExtendedInput & BufferMemoryExtendedInput & { searchOnly: boolean; useFlowiseChatId: boolean } =
{
apiKey: apiKey,
humanPrefix: nodeData.inputs?.humanPrefix as string,
aiPrefix: nodeData.inputs?.aiPrefix as string,
inputKey: nodeData.inputs?.inputKey as string,
sessionId: constructorSessionId,
mem0Options: mem0Options,
memoryOptions: memoryOptions,
separateMessages: false,
returnMessages: false,
appDataSource: options.appDataSource as DataSource,
databaseEntities: options.databaseEntities as IDatabaseEntity,
chatflowid: options.chatflowid as string,
searchOnly: (nodeData.inputs?.searchOnly as boolean) || false,
useFlowiseChatId: useFlowiseChatId,
orgId: orgId
}
const obj: NodeFields = {
apiKey: apiKey,
humanPrefix: nodeData.inputs?.humanPrefix as string,
aiPrefix: nodeData.inputs?.aiPrefix as string,
inputKey: nodeData.inputs?.inputKey as string,
sessionId: constructorSessionId,
mem0Options: mem0Options,
memoryOptions: memoryOptions,
separateMessages: false,
returnMessages: false,
appDataSource: options.appDataSource as DataSource,
databaseEntities: options.databaseEntities as IDatabaseEntity,
chatflowid: options.chatflowid as string,
searchOnly: (nodeData.inputs?.searchOnly as boolean) || false,
useFlowiseChatId: useFlowiseChatId,
input: input,
orgId: orgId
}
return new Mem0MemoryExtended(obj)
}
@@ -223,10 +229,9 @@ class Mem0MemoryExtended extends BaseMem0Memory implements MemoryMethods {
chatflowid: string
searchOnly: boolean
useFlowiseChatId: boolean
input: string
constructor(
fields: Mem0MemoryInput & Mem0MemoryExtendedInput & BufferMemoryExtendedInput & { searchOnly: boolean; useFlowiseChatId: boolean }
) {
constructor(fields: NodeFields) {
super(fields)
this.initialUserId = fields.memoryOptions?.user_id ?? ''
this.userId = this.initialUserId
@@ -237,6 +242,7 @@ class Mem0MemoryExtended extends BaseMem0Memory implements MemoryMethods {
this.chatflowid = fields.chatflowid
this.searchOnly = fields.searchOnly
this.useFlowiseChatId = fields.useFlowiseChatId
this.input = fields.input
this.orgId = fields.orgId
}
@@ -323,11 +329,16 @@ class Mem0MemoryExtended extends BaseMem0Memory implements MemoryMethods {
if (prependMessages?.length) {
returnIMessages.unshift(...prependMessages)
// Reverted to original simpler unshift
chatMessage.unshift(...(prependMessages as any)) // Cast as any
chatMessage.unshift(...(prependMessages as any))
}
if (returnBaseMessages) {
const memoryVariables = await this.loadMemoryVariables({}, overrideUserId)
const memoryVariables = await this.loadMemoryVariables(
{
[this.inputKey]: this.input ?? ''
},
overrideUserId
)
const mem0History = memoryVariables[this.memoryKey]
if (mem0History && typeof mem0History === 'string') {