diff --git a/src/lib/agentMemory.ts b/src/lib/agentMemory.ts index 2582d1c..b8d18d5 100644 --- a/src/lib/agentMemory.ts +++ b/src/lib/agentMemory.ts @@ -97,7 +97,7 @@ export const agentMemory = { return mem._key; }, - async listAll(email: string, signal?: AbortSignal): Promise { + async listAll(email: string, signal?: AbortSignal, cap = 200): Promise { const vaultKey = vaultKeyFor(email); const ok = await ensureVault(vaultKey, email, signal); if (!ok) return []; @@ -107,7 +107,8 @@ export const agentMemory = { const memKeys = (edges as any[]) .filter((e) => e?.role === "presentation") .map((e) => (e._to || "").split("/").pop()) - .filter(Boolean) as string[]; + .filter(Boolean) + .slice(0, cap) as string[]; const memos = await Promise.all( memKeys.map(async (k) => { try { @@ -132,7 +133,7 @@ export const agentMemory = { * Score = count of unique stem matches. Ties broken by recency. */ async recall(email: string, query: string, limit = 5, signal?: AbortSignal): Promise { - const all = await agentMemory.listAll(email, signal); + const all = await agentMemory.listAll(email, signal, 30); if (!query.trim() || all.length === 0) return all.slice(0, limit); const stems = new Set( query.toLowerCase().replace(/[^a-z0-9\s]/g, " ").split(/\s+/).filter((w) => w.length > 2) diff --git a/src/lib/agentTools.ts b/src/lib/agentTools.ts index c374914..d88f157 100644 --- a/src/lib/agentTools.ts +++ b/src/lib/agentTools.ts @@ -240,7 +240,12 @@ export async function routeAgentInput(text: string, ctx: ToolContext): Promise recallDeadline.abort(), 2000); + hints = await agentMemory.recall(ctx.userEmail, trimmed, 3, recallDeadline.signal); + clearTimeout(recallTimer); + } catch { /* recall is best-effort; never blocks the user */ } const llmReply = await llmFallback(trimmed, ctx, hints); if (llmReply) return llmReply; if (hints.length > 0) {