|
| 1 | +import { getBaseClasses, getCredentialData, getCredentialParam, ICommonObject, INode, INodeData, INodeParams } from '../../../src' |
| 2 | +import { MongoDBChatMessageHistory } from 'langchain/stores/message/mongodb' |
| 3 | +import { BufferMemory, BufferMemoryInput } from 'langchain/memory' |
| 4 | +import { BaseMessage, mapStoredMessageToChatMessage } from 'langchain/schema' |
| 5 | +import { MongoClient } from 'mongodb' |
| 6 | + |
| 7 | +class MongoDB_Memory implements INode { |
| 8 | + label: string |
| 9 | + name: string |
| 10 | + version: number |
| 11 | + description: string |
| 12 | + type: string |
| 13 | + icon: string |
| 14 | + category: string |
| 15 | + baseClasses: string[] |
| 16 | + credential: INodeParams |
| 17 | + inputs: INodeParams[] |
| 18 | + |
| 19 | + constructor() { |
| 20 | + this.label = 'MongoDB Atlas Chat Memory' |
| 21 | + this.name = 'MongoDBAtlasChatMemory' |
| 22 | + this.version = 1.0 |
| 23 | + this.type = 'MongoDBAtlasChatMemory' |
| 24 | + this.icon = 'mongodb.png' |
| 25 | + this.category = 'Memory' |
| 26 | + this.description = 'Stores the conversation in MongoDB Atlas' |
| 27 | + this.baseClasses = [this.type, ...getBaseClasses(BufferMemory)] |
| 28 | + this.credential = { |
| 29 | + label: 'Connect Credential', |
| 30 | + name: 'credential', |
| 31 | + type: 'credential', |
| 32 | + credentialNames: ['mongoDBUrlApi'] |
| 33 | + } |
| 34 | + this.inputs = [ |
| 35 | + { |
| 36 | + label: 'Database', |
| 37 | + name: 'databaseName', |
| 38 | + placeholder: '<DB_NAME>', |
| 39 | + type: 'string' |
| 40 | + }, |
| 41 | + { |
| 42 | + label: 'Collection Name', |
| 43 | + name: 'collectionName', |
| 44 | + placeholder: '<COLLECTION_NAME>', |
| 45 | + type: 'string' |
| 46 | + }, |
| 47 | + { |
| 48 | + label: 'Session Id', |
| 49 | + name: 'sessionId', |
| 50 | + type: 'string', |
| 51 | + description: 'If not specified, the first CHAT_MESSAGE_ID will be used as sessionId', |
| 52 | + default: '', |
| 53 | + additionalParams: true, |
| 54 | + optional: true |
| 55 | + }, |
| 56 | + { |
| 57 | + label: 'Memory Key', |
| 58 | + name: 'memoryKey', |
| 59 | + type: 'string', |
| 60 | + default: 'chat_history', |
| 61 | + additionalParams: true |
| 62 | + } |
| 63 | + ] |
| 64 | + } |
| 65 | + |
| 66 | + async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> { |
| 67 | + return initializeMongoDB(nodeData, options) |
| 68 | + } |
| 69 | + |
| 70 | + async clearSessionMemory(nodeData: INodeData, options: ICommonObject): Promise<void> { |
| 71 | + const mongodbMemory = await initializeMongoDB(nodeData, options) |
| 72 | + const sessionId = nodeData.inputs?.sessionId as string |
| 73 | + const chatId = options?.chatId as string |
| 74 | + options.logger.info(`Clearing MongoDB memory session ${sessionId ? sessionId : chatId}`) |
| 75 | + await mongodbMemory.clear() |
| 76 | + options.logger.info(`Successfully cleared MongoDB memory session ${sessionId ? sessionId : chatId}`) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +const initializeMongoDB = async (nodeData: INodeData, options: ICommonObject): Promise<BufferMemory> => { |
| 81 | + const databaseName = nodeData.inputs?.databaseName as string |
| 82 | + const collectionName = nodeData.inputs?.collectionName as string |
| 83 | + const sessionId = nodeData.inputs?.sessionId as string |
| 84 | + const memoryKey = nodeData.inputs?.memoryKey as string |
| 85 | + const chatId = options?.chatId as string |
| 86 | + |
| 87 | + let isSessionIdUsingChatMessageId = false |
| 88 | + if (!sessionId && chatId) isSessionIdUsingChatMessageId = true |
| 89 | + |
| 90 | + const credentialData = await getCredentialData(nodeData.credential ?? '', options) |
| 91 | + let mongoDBConnectUrl = getCredentialParam('mongoDBConnectUrl', credentialData, nodeData) |
| 92 | + |
| 93 | + const client = new MongoClient(mongoDBConnectUrl) |
| 94 | + await client.connect() |
| 95 | + const collection = client.db(databaseName).collection(collectionName) |
| 96 | + |
| 97 | + const mongoDBChatMessageHistory = new MongoDBChatMessageHistory({ |
| 98 | + collection, |
| 99 | + sessionId: sessionId ? sessionId : chatId |
| 100 | + }) |
| 101 | + |
| 102 | + mongoDBChatMessageHistory.getMessages = async (): Promise<BaseMessage[]> => { |
| 103 | + const document = await collection.findOne({ |
| 104 | + sessionId: (mongoDBChatMessageHistory as any).sessionId |
| 105 | + }) |
| 106 | + const messages = document?.messages || [] |
| 107 | + return messages.map(mapStoredMessageToChatMessage) |
| 108 | + } |
| 109 | + |
| 110 | + mongoDBChatMessageHistory.addMessage = async (message: BaseMessage): Promise<void> => { |
| 111 | + const messages = [message].map((msg) => msg.toDict()) |
| 112 | + await collection.updateOne( |
| 113 | + { sessionId: (mongoDBChatMessageHistory as any).sessionId }, |
| 114 | + { |
| 115 | + $push: { messages: { $each: messages } } |
| 116 | + }, |
| 117 | + { upsert: true } |
| 118 | + ) |
| 119 | + } |
| 120 | + |
| 121 | + mongoDBChatMessageHistory.clear = async (): Promise<void> => { |
| 122 | + await collection.deleteOne({ sessionId: (mongoDBChatMessageHistory as any).sessionId }) |
| 123 | + } |
| 124 | + |
| 125 | + return new BufferMemoryExtended({ |
| 126 | + memoryKey, |
| 127 | + chatHistory: mongoDBChatMessageHistory, |
| 128 | + returnMessages: true, |
| 129 | + isSessionIdUsingChatMessageId |
| 130 | + }) |
| 131 | +} |
| 132 | + |
| 133 | +interface BufferMemoryExtendedInput { |
| 134 | + isSessionIdUsingChatMessageId: boolean |
| 135 | +} |
| 136 | + |
| 137 | +class BufferMemoryExtended extends BufferMemory { |
| 138 | + isSessionIdUsingChatMessageId? = false |
| 139 | + |
| 140 | + constructor(fields: BufferMemoryInput & Partial<BufferMemoryExtendedInput>) { |
| 141 | + super(fields) |
| 142 | + this.isSessionIdUsingChatMessageId = fields.isSessionIdUsingChatMessageId |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +module.exports = { nodeClass: MongoDB_Memory } |
0 commit comments