Skip to content

Commit 9682a0c

Browse files
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 086be60. * 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 <[email protected]> Co-authored-by: Henry Heng <[email protected]>
1 parent 27bc47e commit 9682a0c

File tree

1 file changed

+37
-26
lines changed
  • packages/components/nodes/memory/Mem0

1 file changed

+37
-26
lines changed

packages/components/nodes/memory/Mem0/Mem0.ts

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ interface BufferMemoryExtendedInput {
1515
chatflowid: string
1616
}
1717

18+
interface NodeFields extends Mem0MemoryInput, Mem0MemoryExtendedInput, BufferMemoryExtendedInput {
19+
searchOnly: boolean
20+
useFlowiseChatId: boolean
21+
input: string
22+
}
23+
1824
class Mem0_Memory implements INode {
1925
label: string
2026
name: string
@@ -143,12 +149,12 @@ class Mem0_Memory implements INode {
143149
]
144150
}
145151

146-
async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> {
147-
return await initializeMem0(nodeData, options)
152+
async init(nodeData: INodeData, input: string, options: ICommonObject): Promise<any> {
153+
return await initializeMem0(nodeData, input, options)
148154
}
149155
}
150156

151-
const initializeMem0 = async (nodeData: INodeData, options: ICommonObject): Promise<BaseMem0Memory> => {
157+
const initializeMem0 = async (nodeData: INodeData, input: string, options: ICommonObject): Promise<BaseMem0Memory> => {
152158
const initialUserId = nodeData.inputs?.user_id as string
153159
const useFlowiseChatId = nodeData.inputs?.useFlowiseChatId as boolean
154160
const orgId = options.orgId as string
@@ -184,24 +190,24 @@ const initializeMem0 = async (nodeData: INodeData, options: ICommonObject): Prom
184190
filters: (nodeData.inputs?.filters as Record<string, any>) || {}
185191
}
186192

187-
const obj: Mem0MemoryInput & Mem0MemoryExtendedInput & BufferMemoryExtendedInput & { searchOnly: boolean; useFlowiseChatId: boolean } =
188-
{
189-
apiKey: apiKey,
190-
humanPrefix: nodeData.inputs?.humanPrefix as string,
191-
aiPrefix: nodeData.inputs?.aiPrefix as string,
192-
inputKey: nodeData.inputs?.inputKey as string,
193-
sessionId: constructorSessionId,
194-
mem0Options: mem0Options,
195-
memoryOptions: memoryOptions,
196-
separateMessages: false,
197-
returnMessages: false,
198-
appDataSource: options.appDataSource as DataSource,
199-
databaseEntities: options.databaseEntities as IDatabaseEntity,
200-
chatflowid: options.chatflowid as string,
201-
searchOnly: (nodeData.inputs?.searchOnly as boolean) || false,
202-
useFlowiseChatId: useFlowiseChatId,
203-
orgId: orgId
204-
}
193+
const obj: NodeFields = {
194+
apiKey: apiKey,
195+
humanPrefix: nodeData.inputs?.humanPrefix as string,
196+
aiPrefix: nodeData.inputs?.aiPrefix as string,
197+
inputKey: nodeData.inputs?.inputKey as string,
198+
sessionId: constructorSessionId,
199+
mem0Options: mem0Options,
200+
memoryOptions: memoryOptions,
201+
separateMessages: false,
202+
returnMessages: false,
203+
appDataSource: options.appDataSource as DataSource,
204+
databaseEntities: options.databaseEntities as IDatabaseEntity,
205+
chatflowid: options.chatflowid as string,
206+
searchOnly: (nodeData.inputs?.searchOnly as boolean) || false,
207+
useFlowiseChatId: useFlowiseChatId,
208+
input: input,
209+
orgId: orgId
210+
}
205211

206212
return new Mem0MemoryExtended(obj)
207213
}
@@ -223,10 +229,9 @@ class Mem0MemoryExtended extends BaseMem0Memory implements MemoryMethods {
223229
chatflowid: string
224230
searchOnly: boolean
225231
useFlowiseChatId: boolean
232+
input: string
226233

227-
constructor(
228-
fields: Mem0MemoryInput & Mem0MemoryExtendedInput & BufferMemoryExtendedInput & { searchOnly: boolean; useFlowiseChatId: boolean }
229-
) {
234+
constructor(fields: NodeFields) {
230235
super(fields)
231236
this.initialUserId = fields.memoryOptions?.user_id ?? ''
232237
this.userId = this.initialUserId
@@ -237,6 +242,7 @@ class Mem0MemoryExtended extends BaseMem0Memory implements MemoryMethods {
237242
this.chatflowid = fields.chatflowid
238243
this.searchOnly = fields.searchOnly
239244
this.useFlowiseChatId = fields.useFlowiseChatId
245+
this.input = fields.input
240246
this.orgId = fields.orgId
241247
}
242248

@@ -323,11 +329,16 @@ class Mem0MemoryExtended extends BaseMem0Memory implements MemoryMethods {
323329
if (prependMessages?.length) {
324330
returnIMessages.unshift(...prependMessages)
325331
// Reverted to original simpler unshift
326-
chatMessage.unshift(...(prependMessages as any)) // Cast as any
332+
chatMessage.unshift(...(prependMessages as any))
327333
}
328334

329335
if (returnBaseMessages) {
330-
const memoryVariables = await this.loadMemoryVariables({}, overrideUserId)
336+
const memoryVariables = await this.loadMemoryVariables(
337+
{
338+
[this.inputKey]: this.input ?? ''
339+
},
340+
overrideUserId
341+
)
331342
const mem0History = memoryVariables[this.memoryKey]
332343

333344
if (mem0History && typeof mem0History === 'string') {

0 commit comments

Comments
 (0)