| 1 | // Memoria persistente de Elffuss: hechos sobre el usuario guardados en SU |
| 2 | // navegador (IndexedDB). Sobreviven al refresco y a nuevas conversaciones; |
| 3 | // los últimos entran en el CONTEXTO AHORA de cada turno. |
| 4 | import * as db from '../db.js'; |
| 5 | import { require as requirePerm } from '../permissions.js'; |
| 6 | import { t } from '../i18n.js'; |
| 7 | |
| 8 | export async function save({ fact } = {}) { |
| 9 | if (!fact) throw new Error(t('toolMissing', { arg: 'fact' })); |
| 10 | await requirePerm('memory', fact); |
| 11 | const id = 'm' + Math.random().toString(36).slice(2, 8); |
| 12 | await db.set('memory', id, { id, fact: String(fact).slice(0, 300), at: Date.now() }); |
| 13 | return t('memSaved', { fact, id }); |
| 14 | } |
| 15 | |
| 16 | export async function listFacts() { |
| 17 | const all = (await db.all('memory')).sort((a, b) => a.at - b.at); |
| 18 | return all.length |
| 19 | ? all.map(f => `• [${f.id}] ${f.fact}`).join('\n') |
| 20 | : t('memNone'); |
| 21 | } |
| 22 | |
| 23 | export async function forget({ id } = {}) { |
| 24 | await db.del('memory', id); |
| 25 | return t('memForgot', { id }); |
| 26 | } |
| 27 | |
| 28 | export async function recent(n = 8) { |
| 29 | return (await db.all('memory')).sort((a, b) => b.at - a.at).slice(0, n).reverse(); |
| 30 | } |