src / memory / MemoryService.ts
/**
* @module MemoryService
* Manages the long-term, persistent memory of the AI Agent.
* This service simulates interaction with a vector database (e.g., Pinecone, ChromaDB).
*/
// Define the structure for a stored memory chunk
export interface MemoryChunk {
id: string;
timestamp: number;
topic: string; // Key topic derived from the conversation
summary: string; // The summarized content of the session
source_context: string[]; // List of key documents/sources used in this session
}
/**
* A simulated service for interacting with a persistent memory store.
*/
export class MemoryService {
private static storage: Map<string, MemoryChunk[]> = new Map();
/**
* Saves the current conversation summary and context to long-term memory.
* @param topic The primary subject of the conversation (e.g., "Q3 Budgeting").
* @param summary A detailed summary generated by the LLM describing what was accomplished.
* @param sources List of documents/sources used in this session.
*/
public static async saveMemory(topic: string, summary: string, sources: FileHandle[]): Promise<void> {
const newChunk: MemoryChunk = {
id: Date.now().toString(), // Simple unique ID for simulation
timestamp: Date.now(),
topic: topic,
summary: summary,
source_context: Array.from(sources).map(f => f.name),
};
if (!MemoryService.storage.has(topic)) {
MemoryService.storage.set(topic, []);
}
MemoryService.storage.get(topic)!.push(newChunk);
console.log(`\n[MEMORY] Successfully saved memory chunk for topic: "${topic}".`);
}
/**
* Retrieves relevant past memories based on a new query or focus topic.
* @param query The current user query.
* @param focusTopic The determined conversational focus.
* @returns A string containing synthesized context from past sessions.
*/
public static async retrieveMemory(query: string, focusTopic: string): Promise<string> {
console.log(`\n[MEMORY] Searching long-term memory for topics related to "${focusTopic}" and query: "${query}"...`);
// --- SIMULATION START ---
// In a real system, this would be an API call to the vector database (e.g., cosine similarity search).
// We simulate finding relevant memories based on keywords.
const allTopics = Array.from(MemoryService.storage.keys());
let retrievedContext = "";
if (allTopics.length === 0) {
return "No long-term memory found.";
}
// Simple simulation: If the query contains a keyword that matches any stored topic, we retrieve it.
for (const topic of allTopics) {
if (query.toLowerCase().includes(topic.toLowerCase()) || focusTopic.toLowerCase().includes(topic.toLowerCase())) {
const chunks = MemoryService.storage.get(topic)!;
retrievedContext += `\n--- RECALL: Previous Session on "${topic}" ---\n`;
// Just grab the summary of the most recent session for that topic
const latestChunk = chunks[chunks.length - 1];
retrievedContext += `Summary: ${latestChunk.summary}\n`;
}
}
if (retrievedContext) {
return `[CONTEXT FROM MEMORY]: ${retrievedContext}`;
} else {
return "No relevant memories found in past sessions.";
}
// --- SIMULATION END ---
}
}