@aksolab/recall
Version:
A memory management package for AI SDK memory functionality
186 lines • 6.81 kB
TypeScript
import { CoreMessage } from "ai";
import { ArchivalMemoryPayload, CoreBlock, CoreMemoryEntry, MemoryState } from "@aksolab/recall-types";
import { StorageProvider } from "@aksolab/recall-storage-provider";
import { ArchiveProvider, ArchiveEntry } from "@aksolab/recall-archive-provider";
export declare class MemoryManager {
private provider;
private archiveProvider;
private openaiApiKey;
private memoryKey;
private threadId;
private chatHistory;
private coreMemory;
private encoder;
private chatTokenLimit;
private _maxContextSize;
private coreBlockTokenLimit;
/**
* Creates a new instance of the MemoryManager.
* @param provider - Storage provider for chat history and core memory
* @param archiveProvider - Provider for archival memory and RAG capabilities
* @param openaiApiKey - OpenAI API key for embeddings and summarization
* @param memoryKey - Unique identifier for this memory instance
* @param threadId - Conversation thread identifier
* @param maxContextSize - Optional maximum context size in tokens
* @param coreBlockTokenLimit - Optional token limit for core memory blocks
*/
constructor(provider: StorageProvider, archiveProvider: ArchiveProvider, openaiApiKey: string, memoryKey: string, threadId: string, maxContextSize?: number, coreBlockTokenLimit?: number, chatTokenLimit?: number);
/**
* Initializes the memory manager with optional previous state.
* @param previousState - Optional previous memory state to restore
*/
initialize(previousState?: MemoryState): Promise<void>;
/**
* Saves the current chat history to the storage provider.
* @private
*/
private saveChatHistory;
/**
* Saves the current core memory to the storage provider.
* @private
*/
private saveCoreMemory;
/**
* Converts core memory blocks to a string representation.
* @private
* @returns Formatted string of core memory blocks
*/
private coreMemoryToString;
/**
* Retrieves the complete chat history.
* @returns Promise resolving to an array of chat messages
*/
getChatHistory(): Promise<CoreMessage[]>;
/**
* Adds one or more messages to the chat history.
* @param messages - A single message or array of messages to add
* @example
* // Add a single message
* await memoryManager.addMessages({ role: 'user', content: 'Hello!' });
*
* // Add multiple messages
* await memoryManager.addMessages([
* { role: 'assistant', content: 'Hi!' },
* { role: 'user', content: 'How are you?' }
* ]);
*/
addMessages(messages: CoreMessage | CoreMessage[]): Promise<void>;
/**
* @deprecated Use {@link addMessages} instead. This method will be removed in the next major version.
* @example
* // Instead of:
* await memoryManager.addUserMessage(message);
*
* // Use:
* await memoryManager.addMessages(message);
*/
addUserMessage(message: CoreMessage): Promise<void>;
/**
* @deprecated Use {@link addMessages} instead. This method will be removed in the next major version.
* @example
* // Instead of:
* await memoryManager.addAIMessage(message);
*
* // Use:
* await memoryManager.addMessages(message);
*/
addAIMessage(message: CoreMessage): Promise<void>;
/**
* @deprecated Use {@link addMessages} instead. This method will be removed in the next major version.
* @example
* // Instead of:
* await memoryManager.addAIMessages(messages);
*
* // Use:
* await memoryManager.addMessages(messages);
*/
addAIMessages(messages: CoreMessage[]): Promise<void>;
/**
* Retrieves the current state of core memory.
* @returns Promise resolving to a record of core memory blocks and their entries
*/
getCoreMemory(): Promise<Record<CoreBlock, CoreMemoryEntry> | null>;
/**
* Updates a core memory block with new content.
* @param block - The core memory block to update
* @param content - New content for the block
* @param description - Optional description for the block
* @throws {Error} If content exceeds token limit
*/
updateCoreMemory(block: CoreBlock, content: string, description?: string): Promise<void>;
/**
* Searches the archive memory using semantic similarity.
* @param query - Search query text
* @returns Promise resolving to array of matching archive entries
*/
searchArchiveMemory(query: string): Promise<ArchiveEntry[]>;
/**
* Adds a new entry to the archive memory.
* @param payload - Archive entry data to add
* @returns Promise resolving to the created archive entry
*/
addToArchiveMemory(payload: ArchivalMemoryPayload): Promise<ArchiveEntry>;
/**
* Updates an existing archive memory entry.
* @param id - ID of the entry to update
* @param payload - Updated archive entry data
* @returns Promise resolving to the updated archive entry
*/
updateArchiveMemory(id: string, payload: ArchivalMemoryPayload): Promise<ArchiveEntry | null>;
/**
* Removes an entry from archive memory.
* @param id - ID of the entry to remove
* @returns Promise resolving to the removed entry, or null if not found
*/
removeArchivalMemory(id: string): Promise<ArchiveEntry | null>;
/**
* Gets the current context size in tokens.
*/
get contextSize(): number;
/**
* Gets the maximum allowed context size in tokens.
*/
get maxContextSize(): number;
/**
* Sets the maximum allowed context size in tokens.
* Triggers chat history summarization if needed.
*/
set maxContextSize(size: number);
/**
* Gets or initializes the token encoder.
* @private
* @returns Token encoder instance
*/
private getEncoder;
/**
* Counts the number of tokens in a text string.
* @private
* @param text - Text to count tokens for
* @returns Number of tokens
*/
private countTokens;
/**
* Calculates the total token count of the chat history.
* @private
* @returns Total number of tokens
*/
private totalTokenCount;
/**
* Checks and manages chat history size, summarizing if needed.
* @private
*/
private checkChatHistorySize;
/**
* Cleans up resources when the instance is no longer needed.
*/
dispose(): void;
/**
* Gets the token limit for core memory blocks.
*/
get coreMemoryBlockLimit(): number;
/**
* Sets the token limit for core memory blocks.
*/
set coreMemoryBlockLimit(limit: number);
}
//# sourceMappingURL=memoryManager.d.ts.map