diff --git a/memory.py b/memory.py index acd1267..e90393a 100644 --- a/memory.py +++ b/memory.py @@ -25,6 +25,16 @@ async def store_memory(content: str, user_id: str = "default", pool = await get_pool() vec = await embed(content) async with pool.acquire() as conn: + # skip if near-duplicate already exists (cosine > 0.85) + existing = await conn.fetchval(""" + SELECT id FROM memories + WHERE user_id = $1 + AND (metadata->>'archived') IS NULL + AND 1 - (embedding <=> $2::vector) > 0.85 + LIMIT 1 + """, user_id, _vec_str(vec)) + if existing: + return await conn.execute(""" INSERT INTO memories (user_id, content, embedding, importance, memory_type, metadata) VALUES ($1, $2, $3::vector, $4, $5, $6)