import { Hono } from 'hono' import type { Context } from 'hono' import { validateApiKey } from '../../auth/api-key' import { verifyWorkerJwt } from '../../auth/jwt' import { extractBearerToken } from '../../auth/middleware' import { resolveExistingSessionId } from '../../services/session' import { MemoryError, buildMemoryPack, syncMemories, type MemoryWriteBackEntry, } from '../../services/memory' const app = new Hono() /** * CLI/worker memory endpoints (API key or worker JWT auth — same credential * surface as the v1 artifact upload route). * * - GET /v1/memories/pack?session_id= — the injection pack for a session: * { files: { "": "" } }. Empty pack for unknown * sessions; the bridge treats an empty pack as "no memory override". * - POST /v1/memories/sync — write-back from the bridge: { session_id, * files: [{rel_path, content}] }. Identity is derived server-side from * the session's owners; any owner/team fields in the body are ignored * (anti-forgery). Entries land in the session owner's personal scope. */ function authenticateWorker(c: Context): boolean { const token = extractBearerToken(c) if (!token) return false if (validateApiKey(token)) return true return verifyWorkerJwt(token) !== null } app.get('/memories/pack', async c => { if (!authenticateWorker(c)) { return c.json( { error: { type: 'unauthorized', message: 'Invalid auth token' } }, 401, ) } const requested = c.req.query('session_id') if (!requested) { return c.json( { error: { type: 'bad_request', message: 'session_id is required' } }, 400, ) } const sessionId = resolveExistingSessionId(requested) if (!sessionId) { // Unknown session → empty pack; the bridge proceeds without memory // injection rather than failing the spawn. return c.json({ files: {} }, 200) } return c.json(buildMemoryPack(sessionId), 200) }) app.post('/memories/sync', async c => { if (!authenticateWorker(c)) { return c.json( { error: { type: 'unauthorized', message: 'Invalid auth token' } }, 401, ) } let body: Record try { body = await c.req.json() } catch { return c.json( { error: { type: 'bad_request', message: 'Invalid JSON body' } }, 400, ) } const requestedSessionId = typeof body.session_id === 'string' ? body.session_id : '' if (!requestedSessionId) { return c.json( { error: { type: 'bad_request', message: 'session_id is required' } }, 400, ) } const sessionId = resolveExistingSessionId(requestedSessionId) if (!sessionId) { return c.json( { error: { type: 'not_found', message: 'Unknown session' } }, 404, ) } const rawFiles = body.files if (!Array.isArray(rawFiles)) { return c.json( { error: { type: 'bad_request', message: 'files must be an array' } }, 400, ) } const entries: MemoryWriteBackEntry[] = [] for (const item of rawFiles) { if ( item && typeof item === 'object' && typeof (item as Record).rel_path === 'string' && typeof (item as Record).content === 'string' ) { entries.push({ rel_path: (item as Record).rel_path as string, content: (item as Record).content as string, }) } } if (entries.length === 0) { return c.json({ created: 0, updated: 0, skipped: 0 }, 200) } try { const result = syncMemories(sessionId, entries) return c.json(result, 200) } catch (err) { if (err instanceof MemoryError) { const status = err.code === 'no_owner' ? 409 : 400 return c.json({ error: { type: err.code, message: err.message } }, status) } throw err } }) export default app