Master the design and implementation of transparent, user-controllable AI memory systems that enhance personalization while maintaining privacy and user agency.
class MemoryStore {
constructor(private crypto: CryptoAdapter, private db: DbAdapter) {}
async create(userId: string, item: Omit) {
const now = new Date().toISOString()
const encrypted = await this.crypto.encrypt(JSON.stringify(item.data))
const record = { ...item, id: crypto.randomUUID(), data: encrypted, userId, createdAt: now, updatedAt: now }
await this.db.insert('memories', record)
return record
}
async search(userId: string, q: { type?: MemoryType; text?: string; maxAgeDays?: number }) {
const rows = await this.db.query('memories', { userId, type: q.type })
const fresh = rows.filter(r => !q.maxAgeDays || daysSince(r.updatedAt) <= (q.maxAgeDays ?? 90))
const items = await Promise.all(fresh.map(async r => ({ ...r, data: JSON.parse(await this.crypto.decrypt(r.data)) })))
// Simple text match; replace with hybrid semantic+attribute retrieval in prod
return q.text ? items.filter(i => (i.summary || '').toLowerCase().includes(q.text!.toLowerCase())) : items
}
}