From 3f0f0e4d28ff0aad1a74720153cf8235b1067161 Mon Sep 17 00:00:00 2001 From: Jared Tracy Date: Wed, 7 Feb 2024 20:18:06 -0600 Subject: [PATCH 1/4] DynamoDB Chat Memory fix Fixes #1624 Please note comment in class BufferMemoryExtended for further discussion, if necessary --- .../components/nodes/memory/DynamoDb/DynamoDb.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/packages/components/nodes/memory/DynamoDb/DynamoDb.ts b/packages/components/nodes/memory/DynamoDb/DynamoDb.ts index 91c1d369349..864ff7c848d 100644 --- a/packages/components/nodes/memory/DynamoDb/DynamoDb.ts +++ b/packages/components/nodes/memory/DynamoDb/DynamoDb.ts @@ -142,18 +142,26 @@ interface DynamoDBSerializedChatMessage { } class BufferMemoryExtended extends FlowiseMemory implements MemoryMethods { + tableName = '' sessionId = '' dynamodbClient: DynamoDBClient + dynamoKey = '' + partitionKey = '' constructor(fields: BufferMemoryInput & BufferMemoryExtendedInput) { super(fields) this.sessionId = fields.sessionId this.dynamodbClient = fields.dynamodbClient + + // These fields are coming in on chatHistory, but should they be on the dynamodbClient instead? + this.partitionKey = (fields?.chatHistory as unknown as { partitionKey: string }).partitionKey + this.dynamoKey = (fields?.chatHistory as unknown as { dynamoKey: string }).dynamoKey + this.tableName = (fields?.chatHistory as unknown as { tableName: string }).tableName } overrideDynamoKey(overrideSessionId = '') { - const existingDynamoKey = (this as any).dynamoKey - const partitionKey = (this as any).partitionKey + const existingDynamoKey = this.dynamoKey + const partitionKey = this.partitionKey let newDynamoKey: Record = {} @@ -210,7 +218,7 @@ class BufferMemoryExtended extends FlowiseMemory implements MemoryMethods { if (!this.dynamodbClient) return [] const dynamoKey = overrideSessionId ? this.overrideDynamoKey(overrideSessionId) : (this as any).dynamoKey - const tableName = (this as any).tableName + const tableName = this.tableName const messageAttributeName = (this as any).messageAttributeName const params: GetItemCommandInput = { From d7f9c0738151368b990ec24044528c51f4c7a036 Mon Sep 17 00:00:00 2001 From: Jared Tracy Date: Wed, 7 Feb 2024 22:07:12 -0600 Subject: [PATCH 2/4] return tableName, partiionKey, dynamoKey from initalizeDynamoDB --- .../nodes/memory/DynamoDb/DynamoDb.ts | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/packages/components/nodes/memory/DynamoDb/DynamoDb.ts b/packages/components/nodes/memory/DynamoDb/DynamoDb.ts index 864ff7c848d..c2085b91583 100644 --- a/packages/components/nodes/memory/DynamoDb/DynamoDb.ts +++ b/packages/components/nodes/memory/DynamoDb/DynamoDb.ts @@ -117,7 +117,10 @@ const initalizeDynamoDB = async (nodeData: INodeData, options: ICommonObject): P memoryKey: memoryKey ?? 'chat_history', chatHistory: dynamoDb, sessionId, - dynamodbClient: client + dynamodbClient: client, + tableName, + partitionKey, + dynamoKey: { [partitionKey]: { S: sessionId } } }) return memory } @@ -125,6 +128,9 @@ const initalizeDynamoDB = async (nodeData: INodeData, options: ICommonObject): P interface BufferMemoryExtendedInput { dynamodbClient: DynamoDBClient sessionId: string + tableName: string + partitionKey: string + dynamoKey: Record } interface DynamoDBSerializedChatMessage { @@ -143,10 +149,10 @@ interface DynamoDBSerializedChatMessage { class BufferMemoryExtended extends FlowiseMemory implements MemoryMethods { tableName = '' + partitionKey = '' + dynamoKey: Record sessionId = '' dynamodbClient: DynamoDBClient - dynamoKey = '' - partitionKey = '' constructor(fields: BufferMemoryInput & BufferMemoryExtendedInput) { super(fields) @@ -154,9 +160,9 @@ class BufferMemoryExtended extends FlowiseMemory implements MemoryMethods { this.dynamodbClient = fields.dynamodbClient // These fields are coming in on chatHistory, but should they be on the dynamodbClient instead? - this.partitionKey = (fields?.chatHistory as unknown as { partitionKey: string }).partitionKey - this.dynamoKey = (fields?.chatHistory as unknown as { dynamoKey: string }).dynamoKey - this.tableName = (fields?.chatHistory as unknown as { tableName: string }).tableName + this.tableName = fields.tableName + this.partitionKey = fields.partitionKey + this.dynamoKey = fields.dynamoKey } overrideDynamoKey(overrideSessionId = '') { From d0b1980482968b3cc4c165f391a28f81c00cf518 Mon Sep 17 00:00:00 2001 From: Jared Tracy Date: Wed, 7 Feb 2024 22:08:41 -0600 Subject: [PATCH 3/4] Removes prior comment --- packages/components/nodes/memory/DynamoDb/DynamoDb.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/components/nodes/memory/DynamoDb/DynamoDb.ts b/packages/components/nodes/memory/DynamoDb/DynamoDb.ts index c2085b91583..02ecc31f53d 100644 --- a/packages/components/nodes/memory/DynamoDb/DynamoDb.ts +++ b/packages/components/nodes/memory/DynamoDb/DynamoDb.ts @@ -158,8 +158,6 @@ class BufferMemoryExtended extends FlowiseMemory implements MemoryMethods { super(fields) this.sessionId = fields.sessionId this.dynamodbClient = fields.dynamodbClient - - // These fields are coming in on chatHistory, but should they be on the dynamodbClient instead? this.tableName = fields.tableName this.partitionKey = fields.partitionKey this.dynamoKey = fields.dynamoKey From dd89af8a098e48ef42556593127ad6b4b4e0b560 Mon Sep 17 00:00:00 2001 From: Jared Tracy Date: Thu, 8 Feb 2024 09:04:46 -0600 Subject: [PATCH 4/4] refactor (this as any) usage --- .../nodes/memory/DynamoDb/DynamoDb.ts | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/packages/components/nodes/memory/DynamoDb/DynamoDb.ts b/packages/components/nodes/memory/DynamoDb/DynamoDb.ts index 02ecc31f53d..22da396e4a6 100644 --- a/packages/components/nodes/memory/DynamoDb/DynamoDb.ts +++ b/packages/components/nodes/memory/DynamoDb/DynamoDb.ts @@ -148,9 +148,10 @@ interface DynamoDBSerializedChatMessage { } class BufferMemoryExtended extends FlowiseMemory implements MemoryMethods { - tableName = '' - partitionKey = '' - dynamoKey: Record + private tableName = '' + private partitionKey = '' + private dynamoKey: Record + private messageAttributeName: string sessionId = '' dynamodbClient: DynamoDBClient @@ -221,9 +222,9 @@ class BufferMemoryExtended extends FlowiseMemory implements MemoryMethods { async getChatMessages(overrideSessionId = '', returnBaseMessages = false): Promise { if (!this.dynamodbClient) return [] - const dynamoKey = overrideSessionId ? this.overrideDynamoKey(overrideSessionId) : (this as any).dynamoKey + const dynamoKey = overrideSessionId ? this.overrideDynamoKey(overrideSessionId) : this.dynamoKey const tableName = this.tableName - const messageAttributeName = (this as any).messageAttributeName + const messageAttributeName = this.messageAttributeName const params: GetItemCommandInput = { TableName: tableName, @@ -248,9 +249,9 @@ class BufferMemoryExtended extends FlowiseMemory implements MemoryMethods { async addChatMessages(msgArray: { text: string; type: MessageType }[], overrideSessionId = ''): Promise { if (!this.dynamodbClient) return - const dynamoKey = overrideSessionId ? this.overrideDynamoKey(overrideSessionId) : (this as any).dynamoKey - const tableName = (this as any).tableName - const messageAttributeName = (this as any).messageAttributeName + const dynamoKey = overrideSessionId ? this.overrideDynamoKey(overrideSessionId) : this.dynamoKey + const tableName = this.tableName + const messageAttributeName = this.messageAttributeName const input = msgArray.find((msg) => msg.type === 'userMessage') const output = msgArray.find((msg) => msg.type === 'apiMessage') @@ -271,8 +272,8 @@ class BufferMemoryExtended extends FlowiseMemory implements MemoryMethods { async clearChatMessages(overrideSessionId = ''): Promise { if (!this.dynamodbClient) return - const dynamoKey = overrideSessionId ? this.overrideDynamoKey(overrideSessionId) : (this as any).dynamoKey - const tableName = (this as any).tableName + const dynamoKey = overrideSessionId ? this.overrideDynamoKey(overrideSessionId) : this.dynamoKey + const tableName = this.tableName const params: DeleteItemCommandInput = { TableName: tableName,