Project Files
dist / memory / MemoryService.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MemoryService = void 0;
const STOPWORDS = new Set([
"about", "after", "again", "also", "another", "answer", "because", "before",
"between", "could", "detail", "details", "discuss", "explain", "from", "have",
"how", "into", "more", "other", "please", "question", "should", "show", "some",
"that", "their", "there", "these", "this", "those", "what", "when", "where",
"which", "with", "would", "your",
]);
class MemoryService {
static memory = new Map();
static topicCache = new Map();
static save(topic, summary) {
const cleanedTopic = this.normalizeTopic(topic);
if (!cleanedTopic)
return;
const existing = this.memory.get(cleanedTopic) ?? [];
existing.push({ topic: cleanedTopic, summary, timestamp: Date.now() });
this.memory.set(cleanedTopic, existing.length > 20 ? existing.slice(-20) : existing);
}
static retrieve(topic) {
const cleanedTopic = this.normalizeTopic(topic);
const entries = this.memory.get(cleanedTopic);
if (!entries?.length)
return "";
return entries
.slice(-5)
.map(entry => `- ${entry.summary}`)
.join("\n");
}
static extractTopic(prompt) {
const cached = this.topicCache.get(prompt);
if (cached !== undefined)
return cached;
const words = prompt.toLowerCase().split(/\W+/).filter(w => w.length > 3 && !STOPWORDS.has(w));
const topic = words.slice(0, 6).join(" ");
this.topicCache.set(prompt, topic);
if (this.topicCache.size > 256) {
const first = this.topicCache.keys().next().value;
if (first)
this.topicCache.delete(first);
}
return topic;
}
static normalizeTopic(topic) {
return topic.toLowerCase().replace(/\s+/g, " ").trim().slice(0, 80);
}
}
exports.MemoryService = MemoryService;