|
| 1 | +import { |
| 2 | + createServer, |
| 3 | + type IncomingMessage, |
| 4 | + type ServerResponse, |
| 5 | +} from 'node:http'; |
| 6 | +import { Database } from '../schema/database.js'; |
| 7 | + |
| 8 | +interface ServerConfig { |
| 9 | + port: number; |
| 10 | + dbPath: string; |
| 11 | +} |
| 12 | + |
| 13 | +function parseJson(req: IncomingMessage): Promise<unknown> { |
| 14 | + return new Promise((resolve, reject) => { |
| 15 | + const chunks: Buffer[] = []; |
| 16 | + req.on('data', (chunk: Buffer) => chunks.push(chunk)); |
| 17 | + req.on('end', () => { |
| 18 | + try { |
| 19 | + resolve(JSON.parse(Buffer.concat(chunks).toString())); |
| 20 | + } catch { |
| 21 | + reject(new Error('Invalid JSON')); |
| 22 | + } |
| 23 | + }); |
| 24 | + req.on('error', reject); |
| 25 | + }); |
| 26 | +} |
| 27 | + |
| 28 | +function json(res: ServerResponse, status: number, body: unknown): void { |
| 29 | + res.writeHead(status, { 'Content-Type': 'application/json' }); |
| 30 | + res.end(JSON.stringify(body)); |
| 31 | +} |
| 32 | + |
| 33 | +function parseQuery(url: string): URLSearchParams { |
| 34 | + const idx = url.indexOf('?'); |
| 35 | + return new URLSearchParams(idx >= 0 ? url.slice(idx + 1) : ''); |
| 36 | +} |
| 37 | + |
| 38 | +function parsePath(url: string): string { |
| 39 | + const idx = url.indexOf('?'); |
| 40 | + return idx >= 0 ? url.slice(0, idx) : url; |
| 41 | +} |
| 42 | + |
| 43 | +export function startServer(config: ServerConfig): void { |
| 44 | + const db = new Database(config.dbPath); |
| 45 | + |
| 46 | + const server = createServer(async (req, res) => { |
| 47 | + const method = req.method ?? 'GET'; |
| 48 | + const path = parsePath(req.url ?? '/'); |
| 49 | + const query = parseQuery(req.url ?? '/'); |
| 50 | + |
| 51 | + try { |
| 52 | + // GET /api/status |
| 53 | + if (method === 'GET' && path === '/api/status') { |
| 54 | + json(res, 200, db.getStatus()); |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + // GET /api/nodes/:id |
| 59 | + const nodeMatch = path.match(/^\/api\/nodes\/([^/]+)$/); |
| 60 | + if (method === 'GET' && nodeMatch) { |
| 61 | + const id = nodeMatch[1]!; |
| 62 | + const node = db.getNode(id); |
| 63 | + if (!node) { |
| 64 | + json(res, 404, { error: 'Node not found' }); |
| 65 | + return; |
| 66 | + } |
| 67 | + const edgesFrom = db.getEdgesFrom(id); |
| 68 | + const edgesTo = db.getEdgesTo(id); |
| 69 | + const sources = db.getSourcesForNode(id); |
| 70 | + json(res, 200, { |
| 71 | + ...node, |
| 72 | + embedding: undefined, |
| 73 | + edges: { from: edgesFrom, to: edgesTo }, |
| 74 | + sources, |
| 75 | + }); |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + // GET /api/nodes?keywords=...&limit=...&actor=... |
| 80 | + if (method === 'GET' && path === '/api/nodes') { |
| 81 | + const keywordsRaw = query.get('keywords') ?? ''; |
| 82 | + const keywords = keywordsRaw |
| 83 | + ? keywordsRaw.split(',').map((k) => k.trim()) |
| 84 | + : []; |
| 85 | + const limit = parseInt(query.get('limit') ?? '20', 10); |
| 86 | + const actor = query.get('actor') ?? undefined; |
| 87 | + const nodes = db.searchNodesByKeywords(keywords, limit, actor); |
| 88 | + json( |
| 89 | + res, |
| 90 | + 200, |
| 91 | + nodes.map((n) => ({ ...n, embedding: undefined })) |
| 92 | + ); |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + // POST /api/decisions |
| 97 | + if (method === 'POST' && path === '/api/decisions') { |
| 98 | + const body = (await parseJson(req)) as { |
| 99 | + content?: string; |
| 100 | + actor?: string; |
| 101 | + reasoning?: string; |
| 102 | + }; |
| 103 | + if (!body.content) { |
| 104 | + json(res, 400, { error: 'content is required' }); |
| 105 | + return; |
| 106 | + } |
| 107 | + const node = db.insertNode({ |
| 108 | + type: 'decision', |
| 109 | + content: body.content, |
| 110 | + embedding: null, |
| 111 | + actor: body.actor ?? null, |
| 112 | + confidence: 0.75, |
| 113 | + }); |
| 114 | + json(res, 201, { ...node, embedding: undefined }); |
| 115 | + return; |
| 116 | + } |
| 117 | + |
| 118 | + // GET /api/contradictions |
| 119 | + if (method === 'GET' && path === '/api/contradictions') { |
| 120 | + const contradictions = db.getPendingContradictions(); |
| 121 | + json(res, 200, contradictions); |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + json(res, 404, { error: 'Not found' }); |
| 126 | + } catch (err) { |
| 127 | + const message = err instanceof Error ? err.message : 'Internal error'; |
| 128 | + json(res, 500, { error: message }); |
| 129 | + } |
| 130 | + }); |
| 131 | + |
| 132 | + server.listen(config.port, () => { |
| 133 | + console.log(`Provenant API server listening on port ${config.port}`); |
| 134 | + console.log(` Database: ${config.dbPath}`); |
| 135 | + console.log(` Endpoints:`); |
| 136 | + console.log(` GET /api/status`); |
| 137 | + console.log(` GET /api/nodes?keywords=...&limit=...&actor=...`); |
| 138 | + console.log(` GET /api/nodes/:id`); |
| 139 | + console.log(` POST /api/decisions`); |
| 140 | + console.log(` GET /api/contradictions`); |
| 141 | + }); |
| 142 | + |
| 143 | + process.on('SIGINT', () => { |
| 144 | + db.close(); |
| 145 | + server.close(); |
| 146 | + process.exit(0); |
| 147 | + }); |
| 148 | + |
| 149 | + process.on('SIGTERM', () => { |
| 150 | + db.close(); |
| 151 | + server.close(); |
| 152 | + process.exit(0); |
| 153 | + }); |
| 154 | +} |
0 commit comments