61 lines
4.3 KiB
JavaScript
61 lines
4.3 KiB
JavaScript
// Raw layer: append/search/read/status/rotation/prune without touching Mnemon.
|
|
import { existsSync, mkdtempSync, readdirSync, rmSync, utimesSync } from 'node:fs'
|
|
import { tmpdir } from 'node:os'
|
|
import { join } from 'node:path'
|
|
const root = mkdtempSync(join(tmpdir(), 'brain-raw-'))
|
|
process.env.DSH_HOME = join(root, 'home')
|
|
const results = []
|
|
function check(name, ok, detail = '') { results.push({ name, ok }); console.log((ok ? 'PASS ' : 'FAIL ') + name + (detail ? ' :: ' + detail : '')) }
|
|
const { RawMemoryStore, createRawMemoryTools, rawMemorySettingsFromConfig } = await import('./lib/raw-memory/index.js')
|
|
|
|
const settings = rawMemorySettingsFromConfig({ rawEnabled: true, rawRetentionDays: 7, rawMaxTextChars: 2000 })
|
|
check('default directory is under DSH_HOME, not a mnemon database', settings.dir === join(root, 'home', 'brain', 'raw'), settings.dir)
|
|
|
|
const store = new RawMemoryStore({ root: join(root, 'raw'), maxBytesPerSession: 300 })
|
|
const base = Date.now() - 10_000
|
|
await store.append({ v: 1, sessionId: 'session-a', seq: 1, turn: 1, role: 'user', text: '我喜欢吃苹果和香蕉 apple banana', createdAt: base })
|
|
await store.append({ v: 1, sessionId: 'session-a', seq: 2, turn: 1, role: 'assistant', text: '好的,记住了。你喜欢苹果。', createdAt: base + 1000 })
|
|
await store.append({ v: 1, sessionId: 'session-b', seq: 1, turn: 1, role: 'user', text: 'Another topic: database indexing', createdAt: base + 2000 })
|
|
await store.append({ v: 1, sessionId: 'session-a', seq: 3, turn: 2, role: 'user', text: 'x'.repeat(400), createdAt: base + 3000 })
|
|
|
|
const search = await store.search('苹果')
|
|
check('keyword search finds original text', search.total === 2 && search.matches.some((hit) => hit.sessionId === 'session-a' && hit.seq === 1))
|
|
const english = await store.search('database indexing')
|
|
check('english keyword search works', english.total === 1 && english.matches[0].role === 'user')
|
|
const roleFiltered = await store.search('苹果', { role: 'assistant' })
|
|
check('role filter narrows search', roleFiltered.total === 1 && roleFiltered.matches[0].seq === 2)
|
|
const read = await store.readSession('session-a', { seq: 2 })
|
|
check('read by seq returns one exact record', read.total === 3 && read.items.length === 1 && read.items[0].text.includes('记住了'))
|
|
const firstPage = await store.readSession('session-a', { offset: 0, limit: 2 })
|
|
check('read pagination keeps order', firstPage.items.length === 2 && firstPage.items[0].seq === 1 && firstPage.items[1].seq === 2)
|
|
|
|
const status = await store.status(true, 7)
|
|
check('status counts sessions and records', status.sessions === 2 && status.records === 4 && status.bytes > 0)
|
|
check('raw store never creates mnemon.db', !existsSync(join(root, 'raw', 'mnemon.db')))
|
|
|
|
const tools = createRawMemoryTools({ store, settings: () => settings })
|
|
const searchTool = tools.find((tool) => tool.name === 'memory_raw_search')
|
|
const readTool = tools.find((tool) => tool.name === 'memory_raw_read')
|
|
const statusTool = tools.find((tool) => tool.name === 'memory_raw_status')
|
|
check('three raw tools are exported', tools.length === 3 && searchTool && readTool && statusTool)
|
|
const toolSearch = await searchTool.execute({ query: '苹果', k: 5 })
|
|
check('memory_raw_search returns matches', toolSearch.matches.length === 2 && toolSearch.matches[0].sessionId === 'session-a')
|
|
const toolRead = await readTool.execute({ session_id: 'session-b', limit: 10 })
|
|
check('memory_raw_read returns items', toolRead.total === 1 && toolRead.items[0].text.includes('database'))
|
|
const toolStatus = await statusTool.execute({})
|
|
check('memory_raw_status reports enabled', toolStatus.enabled === true && toolStatus.records === 4)
|
|
|
|
const names = readdirSync(join(root, 'raw'))
|
|
const oldFile = join(root, 'raw', names.find((name) => name.startsWith('session-b')) ?? 'missing')
|
|
const old = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000)
|
|
utimesSync(oldFile, old, old)
|
|
const removed = await store.prune(7)
|
|
const afterPrune = await store.status(true, 7)
|
|
check('retention removes expired session files only', removed === 1 && afterPrune.sessions === 1)
|
|
|
|
const failed = results.filter((result) => !result.ok)
|
|
console.log('')
|
|
console.log(failed.length ? 'FAILED ' + failed.length + '/' + results.length : 'ALL ' + results.length + ' CHECKS PASSED')
|
|
rmSync(root, { recursive: true, force: true })
|
|
process.exit(failed.length ? 1 : 0)
|