import { Redis, RedisOptions } from "ioredis"; import { BaseMessage, BaseListChatMessageHistory } from "../../schema/index.js"; import { mapChatMessagesToStoredMessages, mapStoredMessagesToChatMessages, } from "./utils.js"; /** * Type for the input parameter of the RedisChatMessageHistory * constructor. It includes fields for the session ID, session TTL, Redis * URL, Redis configuration, and Redis client. */ export type RedisChatMessageHistoryInput = { sessionId: string; sessionTTL?: number; url?: string; config?: RedisOptions; client?: Redis; }; /** * Class used to store chat message history in Redis. It provides methods * to add, retrieve, and clear messages from the chat history. */ export class RedisChatMessageHistory extends BaseListChatMessageHistory { lc_namespace = ["langchain", "stores", "message", "ioredis"]; get lc_secrets() { return { url: "REDIS_URL", "config.username": "REDIS_USERNAME", "config.password": "REDIS_PASSWORD", }; } public client: Redis; private sessionId: string; private sessionTTL?: number; constructor(fields: RedisChatMessageHistoryInput) { super(fields); const { sessionId, sessionTTL, url, config, client } = fields; this.client = (client ?? (url ? new Redis(url) : new Redis(config ?? {}))) as Redis; this.sessionId = sessionId; this.sessionTTL = sessionTTL; } /** * Retrieves all messages from the chat history. * @returns Promise that resolves with an array of BaseMessage instances. */ async getMessages(): Promise { const rawStoredMessages = await this.client.lrange(this.sessionId, 0, -1); const orderedMessages = rawStoredMessages .reverse() .map((message) => JSON.parse(message)); return mapStoredMessagesToChatMessages(orderedMessages); } /** * Adds a message to the chat history. * @param message The message to add to the chat history. * @returns Promise that resolves when the message has been added. */ async addMessage(message: BaseMessage): Promise { const messageToAdd = mapChatMessagesToStoredMessages([message]); await this.client.lpush(this.sessionId, JSON.stringify(messageToAdd[0])); if (this.sessionTTL) { await this.client.expire(this.sessionId, this.sessionTTL); } } /** * Clears all messages from the chat history. * @returns Promise that resolves when the chat history has been cleared. */ async clear(): Promise { await this.client.del(this.sessionId); } }