Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
May 28, 2025
63 changes: 37 additions & 26 deletions packages/components/nodes/memory/Mem0/Mem0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
Expand All @@ -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
}

Expand Down Expand Up @@ -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') {
Expand Down