added a memory cooldown

This commit is contained in:
Luna
2026-03-03 20:34:17 +01:00
parent 7fc2c683cd
commit 65de299320
4 changed files with 29 additions and 3 deletions

View File

@@ -41,6 +41,15 @@ const parseEmbedding = (raw) => {
}
};
const memoryUsageMap = new Map();
const getMemoryUsageMapForUser = (userId) => {
if (!memoryUsageMap.has(userId)) {
memoryUsageMap.set(userId, new Map());
}
return memoryUsageMap.get(userId);
};
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const wasmDir = path.resolve(__dirname, '../node_modules/sql.js/dist');
@@ -333,8 +342,16 @@ const retrieveRelevantMemories = async (db, userId, query, options = {}) => {
if (!rows.length) {
return [];
}
const now = Date.now();
const cooldown = config.memoryCooldownMs || 0;
const usage = memoryUsageMap.get(userId);
const eligibleRows =
cooldown && usage
? rows.filter((entry) => now - (usage.get(entry.id) || 0) > cooldown)
: rows;
const rowsToScore = eligibleRows.length ? eligibleRows : rows;
const queryEmbedding = await createEmbedding(query);
return rows
const scored = rowsToScore
.map((entry) => {
const embedding = parseEmbedding(entry.embedding);
return {
@@ -345,6 +362,11 @@ const retrieveRelevantMemories = async (db, userId, query, options = {}) => {
})
.sort((a, b) => b.score - a.score)
.slice(0, config.relevantMemoryCount);
if (scored.length) {
const usageMap = getMemoryUsageMapForUser(userId);
scored.forEach((entry) => usageMap.set(entry.id, now));
}
return scored;
};
export async function appendShortTerm(userId, role, content) {