import { Collection, Document as MongoDBDocument, ObjectId } from "mongodb"; import { BaseMessage, BaseListChatMessageHistory } from "../../schema/index.js"; import { mapChatMessagesToStoredMessages, mapStoredMessagesToChatMessages, } from "./utils.js"; export interface MongoDBChatMessageHistoryInput { collection: Collection; sessionId: string; } export class MongoDBChatMessageHistory extends BaseListChatMessageHistory { lc_namespace = ["langchain", "stores", "message", "mongodb"]; private collection: Collection; private sessionId: string; constructor({ collection, sessionId }: MongoDBChatMessageHistoryInput) { super(); this.collection = collection; this.sessionId = sessionId; } async getMessages(): Promise { const document = await this.collection.findOne({ _id: new ObjectId(this.sessionId), }); const messages = document?.messages || []; return mapStoredMessagesToChatMessages(messages); } async addMessage(message: BaseMessage): Promise { const messages = mapChatMessagesToStoredMessages([message]); await this.collection.updateOne( { _id: new ObjectId(this.sessionId) }, { $push: { messages: { $each: messages } }, }, { upsert: true } ); } async clear(): Promise { await this.collection.deleteOne({ _id: new ObjectId(this.sessionId) }); } }