UNPKG

@aksolab/recall

Version:

A memory management package for AI SDK memory functionality

94 lines (93 loc) 3.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RedisProvider = void 0; class RedisProvider { redis; prefix; constructor(config) { this.redis = config.client; this.prefix = config.prefix || 'recall:memory:'; // Verify Redis connection in constructor this.redis.ping().catch(error => { throw new Error('Failed to connect to Redis: ' + error); }); } getKey(memoryKey) { return `${this.prefix}${memoryKey}`; } getChatHistoryKey(memoryKey, threadId = 'default') { return `${this.getKey(memoryKey)}::chat-history::thread::${threadId}`; } getCoreMemoryKey(memoryKey) { return `${this.getKey(memoryKey)}::core-memory`; } async flush() { const keys = await this.redis.keys(`${this.prefix}*`); if (keys.length > 0) { await this.redis.del(keys); } } // Export is for in memory provider only async export(memoryKey, threadId = 'default') { const state = await this.getMemoryState(memoryKey, threadId); if (!state) { throw new Error('Memory state not found'); } return state; } async getMemoryState(memoryKey, threadId = 'default') { const chatHistoryData = await this.redis.get(this.getChatHistoryKey(memoryKey, threadId)); const coreMemoryData = await this.redis.get(this.getCoreMemoryKey(memoryKey)); return { chatHistory: chatHistoryData ? JSON.parse(chatHistoryData) : [], coreMemory: coreMemoryData ? JSON.parse(coreMemoryData) : null, }; } async updateChatHistory({ memoryKey, threadId, messages }) { await this.redis.set(this.getChatHistoryKey(memoryKey, threadId), JSON.stringify(messages)); } async addChatHistoryMessage({ memoryKey, message, threadId }) { const chatHistory = await this.redis.get(this.getChatHistoryKey(memoryKey, threadId)); const messages = chatHistory ? JSON.parse(chatHistory) : []; messages.push(message); await this.redis.set(this.getChatHistoryKey(memoryKey, threadId), JSON.stringify(messages)); } async getChatHistory(memoryKey, threadId = 'default') { const state = await this.getMemoryState(memoryKey, threadId); return state?.chatHistory || []; } async getCoreMemory(memoryKey) { const coreMemory = await this.redis.get(this.getCoreMemoryKey(memoryKey)); return coreMemory ? JSON.parse(coreMemory) : null; } async updateCoreMemory(key, memory) { await this.redis.set(this.getCoreMemoryKey(key), JSON.stringify(memory)); } async initializeMemoryState(memoryKey, threadId = 'default') { const existingState = await this.getMemoryState(memoryKey, threadId); if (!existingState) { const initialState = { chatHistory: [], coreMemory: null, }; // Initialize each component separately await this.updateChatHistory({ memoryKey, threadId, messages: initialState.chatHistory }); await this.updateCoreMemory(memoryKey, initialState.coreMemory); return initialState; } return existingState; } async deleteMemoryState(memoryKey) { const multi = this.redis.multi(); // Delete all keys associated with this memory key multi.del(this.getKey(memoryKey)); multi.del(this.getChatHistoryKey(memoryKey)); multi.del(this.getCoreMemoryKey(memoryKey)); await multi.exec(); } // Additional method to close Redis connection async disconnect() { await this.redis.quit(); } } exports.RedisProvider = RedisProvider;