|
| 1 | +import { createLogger } from "../lib/logger.js"; |
| 2 | +import { config } from "../lib/config.js"; |
| 3 | +import type { StoreBackend } from "../store/base.js"; |
| 4 | +import { SearchEngine } from "../retrieval/search.js"; |
| 5 | +import { IngestionPipeline } from "../pipeline/ingest.js"; |
| 6 | +import { SearchRequestSchema, UpsertRequestSchema } from "../schemas/index.js"; |
| 7 | + |
| 8 | +const log = createLogger("API"); |
| 9 | + |
| 10 | +export function startServer(store: StoreBackend): void { |
| 11 | + const search = new SearchEngine(store); |
| 12 | + const pipeline = new IngestionPipeline(store); |
| 13 | + |
| 14 | + const server = Bun.serve({ |
| 15 | + port: config.API_PORT, |
| 16 | + async fetch(req) { |
| 17 | + const url = new URL(req.url); |
| 18 | + const method = req.method; |
| 19 | + |
| 20 | + // ── Health ────────────────────────────────────────────────────────────── |
| 21 | + if (url.pathname === "/health" && method === "GET") { |
| 22 | + return Response.json({ status: "ok", ts: Date.now() }); |
| 23 | + } |
| 24 | + |
| 25 | + // ── Stats ─────────────────────────────────────────────────────────────── |
| 26 | + if (url.pathname === "/stats" && method === "GET") { |
| 27 | + const stats = await store.stats(); |
| 28 | + return Response.json(stats); |
| 29 | + } |
| 30 | + |
| 31 | + // ── Search ────────────────────────────────────────────────────────────── |
| 32 | + if (url.pathname === "/search" && method === "POST") { |
| 33 | + const body = await req.json(); |
| 34 | + const parsed = SearchRequestSchema.safeParse(body); |
| 35 | + if (!parsed.success) { |
| 36 | + return Response.json({ error: parsed.error.flatten() }, { status: 400 }); |
| 37 | + } |
| 38 | + const results = await search.query(parsed.data); |
| 39 | + return Response.json({ results, count: results.length }); |
| 40 | + } |
| 41 | + |
| 42 | + // ── Store ─────────────────────────────────────────────────────────────── |
| 43 | + if (url.pathname === "/memories" && method === "POST") { |
| 44 | + const body = await req.json(); |
| 45 | + const parsed = UpsertRequestSchema.safeParse(body); |
| 46 | + if (!parsed.success) { |
| 47 | + return Response.json({ error: parsed.error.flatten() }, { status: 400 }); |
| 48 | + } |
| 49 | + const memories = await pipeline.ingest(parsed.data); |
| 50 | + return Response.json({ memories, count: memories.length }, { status: 201 }); |
| 51 | + } |
| 52 | + |
| 53 | + // ── Delete ────────────────────────────────────────────────────────────── |
| 54 | + if (url.pathname.startsWith("/memories/") && method === "DELETE") { |
| 55 | + const id = url.pathname.replace("/memories/", ""); |
| 56 | + await store.delete(id); |
| 57 | + return new Response(null, { status: 204 }); |
| 58 | + } |
| 59 | + |
| 60 | + // ── Prune ─────────────────────────────────────────────────────────────── |
| 61 | + if (url.pathname === "/prune" && method === "POST") { |
| 62 | + const pruned = await store.pruneExpired(); |
| 63 | + return Response.json({ pruned }); |
| 64 | + } |
| 65 | + |
| 66 | + return Response.json({ error: "Not found" }, { status: 404 }); |
| 67 | + }, |
| 68 | + }); |
| 69 | + |
| 70 | + log.info(`API running at http://localhost:${config.API_PORT}`); |
| 71 | +} |
0 commit comments