import { Hono } from 'hono' import type { Context } from 'hono' import { sessionAuth } from '../../auth/middleware' import { SYSTEM_USER_ID } from '../../auth/constants' import { createSqliteMemoryBackend } from '../../services/memory' import { getMemoryBackend, type MemoryBackend, type MemoryScope, } from '../../services/memoryBackend' const app = new Hono() /** * Web-UI memory routes (mounted under /web with CSRF protection). * * Every /web/memory/* endpoint serves the SAME unified memory UI regardless * of deployment mode — the shared MemoryBackend contract decides the store: * * - memdir (composite `slz rcs one` / `slz rcs web`): the in-process backend * injected by the host — file-based per-project memdir. * - sqlite (standalone multi-user `slz rcs`): a per-request adapter over * the multi-user store — same capability surface plus team-memory * permission/space management (create/promote take a team_id). * * The multi-user CRUD formerly at /web/memories* was removed with the old * split gallery; the CLI/worker surface stays at /v1/memories/* (pack + * write-back) untouched. */ function effectiveBackend(c: Context): MemoryBackend { const injected = getMemoryBackend() if (injected) return injected const userId = c.get('userId') as string const isAdmin = (c.get('isAdmin') as boolean) || userId === SYSTEM_USER_ID return createSqliteMemoryBackend(userId, isAdmin) } function parseScope(raw: string | undefined): MemoryScope | undefined { return raw === 'personal' || raw === 'team' ? raw : undefined } function badRequest(c: Context, message: string) { return c.json({ error: { type: 'bad_request', message } }, 400) } async function readJsonBody( c: Context, ): Promise | null> { try { return (await c.req.json()) as Record } catch { return null } } app.get('/memory/status', sessionAuth, async c => { const backend = effectiveBackend(c) return c.json(await backend.status(), 200) }) /** GET /web/memory/projects — enumerate scannable project memdirs (gallery * browsing facet; 'all' selector in the web UI maps to list/search reads). * Empty list in sqlite mode — the UI hides the project selector. */ app.get('/memory/projects', sessionAuth, async c => { const backend = effectiveBackend(c) return c.json(await backend.listProjects(), 200) }) app.get('/memory/list', sessionAuth, async c => { const backend = effectiveBackend(c) const scope = parseScope(c.req.query('scope')) const project = c.req.query('project') ?? undefined const files = await backend.list(scope, project) return c.json(files, 200) }) app.get('/memory/file', sessionAuth, async c => { const backend = effectiveBackend(c) const path = c.req.query('path') if (!path) return badRequest(c, 'path is required') const project = c.req.query('project') ?? undefined const content = await backend.read(path, project) if (content === null) { return c.json( { error: { type: 'not_found', message: 'Memory file not found' } }, 404, ) } return c.json({ path, content }, 200) }) app.put('/memory/file', sessionAuth, async c => { const backend = effectiveBackend(c) const body = await readJsonBody(c) if (!body) return badRequest(c, 'Invalid JSON') const path = typeof body.path === 'string' ? body.path : '' const content = typeof body.content === 'string' ? body.content : '' if (!path || !content.trim()) { return badRequest(c, 'path and content are required') } const result = await backend.write(path, content) if (result.error) { return c.json( { error: { type: 'write_failed', message: result.error } }, 400, ) } return c.json({ ok: true }, 200) }) app.delete('/memory/file', sessionAuth, async c => { const backend = effectiveBackend(c) const path = c.req.query('path') if (!path) return badRequest(c, 'path is required') const result = await backend.delete(path) if (result.error) { return c.json( { error: { type: 'delete_failed', message: result.error } }, 400, ) } return c.json({ ok: true }, 200) }) /** POST /web/memory/create — structured create (synthesizes path + frontmatter). * Errors return 400 with the backend's message (duplicate name, team memory * disabled, etc.). `team_id` targets a specific team in sqlite multi-team * mode; memdir ignores it (single implicit team subtree). */ app.post('/memory/create', sessionAuth, async c => { const backend = effectiveBackend(c) const body = await readJsonBody(c) if (!body) return badRequest(c, 'Invalid JSON') const name = typeof body.name === 'string' ? body.name.trim() : '' const content = typeof body.content === 'string' ? body.content : '' const scope = body.scope const type = body.type if (!name || !content.trim()) { return badRequest(c, 'name and content are required') } if (scope !== 'personal' && scope !== 'team') { return badRequest(c, 'scope must be personal or team') } if ( type !== 'user' && type !== 'feedback' && type !== 'project' && type !== 'reference' ) { return badRequest( c, 'type must be one of user, feedback, project, reference', ) } const description = typeof body.description === 'string' && body.description.trim() ? body.description.trim() : undefined const teamId = typeof body.team_id === 'string' && body.team_id ? body.team_id : undefined const result = await backend.create({ scope, name, description, type, content, teamId, }) if (result.error) { return c.json( { error: { type: 'create_failed', message: result.error } }, 400, ) } return c.json({ ok: true, path: result.path }, 201) }) /** POST /web/memory/promote — personal → team. `team_id` is required in * sqlite multi-team mode; memdir ignores it. */ app.post('/memory/promote', sessionAuth, async c => { const backend = effectiveBackend(c) const body = await readJsonBody(c) if (!body) return badRequest(c, 'Invalid JSON') const path = typeof body.path === 'string' ? body.path : '' if (!path) return badRequest(c, 'path is required') const teamId = typeof body.team_id === 'string' && body.team_id ? body.team_id : undefined const result = await backend.promoteToTeam(path, teamId) if (result.error) { return c.json( { error: { type: 'promote_failed', message: result.error } }, 400, ) } return c.json({ ok: true }, 200) }) /** GET /web/memory/search?q= — plain regex search over the listed * memory files (find-a-file tool; invalid regex degrades to substring). * Optional ?project=all| searches across projects (browse-only). */ app.get('/memory/search', sessionAuth, async c => { const backend = effectiveBackend(c) const pattern = c.req.query('q') ?? '' if (!pattern.trim()) return badRequest(c, 'q is required') const project = c.req.query('project') ?? undefined const matches = await backend.search(pattern, project) return c.json(matches, 200) }) /** GET /web/memory/recall-test?q= — preview of the mode's own scored * retrieval (memdir: smartRetrieve; sqlite: the store's FTS engine). * Diagnostic/governance tool, deliberately separate from plain search. */ app.get('/memory/recall-test', sessionAuth, async c => { const backend = effectiveBackend(c) const query = c.req.query('q') ?? '' const topK = Number(c.req.query('top_k')) || undefined if (!query.trim()) return badRequest(c, 'q is required') const hits = await backend.recallTest(query, topK) return c.json(hits, 200) }) app.get('/memory/governance', sessionAuth, async c => { const backend = effectiveBackend(c) const status = await backend.governanceStatus() return c.json(status, 200) }) export default app