LLM config persistence plugin — reads/writes config to ~/.openroom/config.json
()
| 19 | |
| 20 | /** LLM config persistence plugin — reads/writes config to ~/.openroom/config.json */ |
| 21 | function llmConfigPlugin(): Plugin { |
| 22 | return { |
| 23 | name: 'llm-config', |
| 24 | configureServer(server) { |
| 25 | server.middlewares.use('/api/llm-config', (req, res) => { |
| 26 | res.setHeader('Content-Type', 'application/json'); |
| 27 | |
| 28 | if (req.method === 'GET') { |
| 29 | try { |
| 30 | if (fs.existsSync(LLM_CONFIG_FILE)) { |
| 31 | const content = fs.readFileSync(LLM_CONFIG_FILE, 'utf-8'); |
| 32 | res.writeHead(200); |
| 33 | res.end(content); |
| 34 | } else { |
| 35 | res.writeHead(200); |
| 36 | res.end('{}'); |
| 37 | } |
| 38 | } catch (err) { |
| 39 | res.writeHead(500); |
| 40 | res.end(JSON.stringify({ error: String(err) })); |
| 41 | } |
| 42 | return; |
| 43 | } |
| 44 | |
| 45 | if (req.method === 'POST') { |
| 46 | const chunks: Buffer[] = []; |
| 47 | req.on('data', (chunk: Buffer) => chunks.push(chunk)); |
| 48 | req.on('end', () => { |
| 49 | try { |
| 50 | const body = Buffer.concat(chunks).toString(); |
| 51 | // Validate JSON before writing |
| 52 | JSON.parse(body); |
| 53 | fs.mkdirSync(resolve(os.homedir(), '.openroom'), { recursive: true }); |
| 54 | fs.writeFileSync(LLM_CONFIG_FILE, body, 'utf-8'); |
| 55 | res.writeHead(200); |
| 56 | res.end(JSON.stringify({ ok: true })); |
| 57 | } catch (err) { |
| 58 | res.writeHead(500); |
| 59 | res.end(JSON.stringify({ error: String(err) })); |
| 60 | } |
| 61 | }); |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | res.writeHead(405); |
| 66 | res.end(JSON.stringify({ error: 'Method not allowed' })); |
| 67 | }); |
| 68 | }, |
| 69 | }; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Session data plugin — reads/writes files under ~/.openroom/sessions/ |