|
| 1 | +import type { Hono } from 'hono'; |
| 2 | +import { timingSafeTokenEqual } from '../auth'; |
| 3 | +import { getBearerToken } from './gateway'; |
| 4 | +import type { Supervisor } from '../supervisor'; |
| 5 | + |
| 6 | +const MORNING_BRIEFING_PREFIX = '/api/plugins/kiloclaw-morning-briefing'; |
| 7 | + |
| 8 | +async function readJsonBody(c: { req: { json: () => Promise<unknown> } }): Promise<unknown> { |
| 9 | + try { |
| 10 | + return await c.req.json(); |
| 11 | + } catch { |
| 12 | + return {}; |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +async function proxyMorningBriefingRoute(params: { |
| 17 | + supervisor: Supervisor; |
| 18 | + gatewayToken: string; |
| 19 | + path: string; |
| 20 | + method: 'GET' | 'POST'; |
| 21 | + body?: unknown; |
| 22 | +}): Promise<Response> { |
| 23 | + if (params.supervisor.getState() !== 'running') { |
| 24 | + return new Response(JSON.stringify({ error: 'Gateway not running' }), { |
| 25 | + status: 503, |
| 26 | + headers: { 'content-type': 'application/json' }, |
| 27 | + }); |
| 28 | + } |
| 29 | + |
| 30 | + try { |
| 31 | + return await fetch(`http://127.0.0.1:3001${MORNING_BRIEFING_PREFIX}${params.path}`, { |
| 32 | + method: params.method, |
| 33 | + headers: { |
| 34 | + authorization: `Bearer ${params.gatewayToken}`, |
| 35 | + 'content-type': 'application/json', |
| 36 | + }, |
| 37 | + body: params.body !== undefined ? JSON.stringify(params.body) : undefined, |
| 38 | + }); |
| 39 | + } catch (error) { |
| 40 | + console.error('[controller] morning briefing proxy failed:', error); |
| 41 | + return new Response(JSON.stringify({ error: 'Failed to reach gateway' }), { |
| 42 | + status: 502, |
| 43 | + headers: { 'content-type': 'application/json' }, |
| 44 | + }); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +export function registerMorningBriefingRoutes( |
| 49 | + app: Hono, |
| 50 | + supervisor: Supervisor, |
| 51 | + expectedToken: string |
| 52 | +): void { |
| 53 | + app.use('/_kilo/morning-briefing/*', async (c, next) => { |
| 54 | + const token = getBearerToken(c.req.header('authorization')); |
| 55 | + if (!timingSafeTokenEqual(token, expectedToken)) { |
| 56 | + return c.json({ error: 'Unauthorized' }, 401); |
| 57 | + } |
| 58 | + await next(); |
| 59 | + }); |
| 60 | + |
| 61 | + app.get('/_kilo/morning-briefing/status', async c => { |
| 62 | + const response = await proxyMorningBriefingRoute({ |
| 63 | + supervisor, |
| 64 | + gatewayToken: expectedToken, |
| 65 | + path: '/status', |
| 66 | + method: 'GET', |
| 67 | + }); |
| 68 | + return response; |
| 69 | + }); |
| 70 | + |
| 71 | + app.post('/_kilo/morning-briefing/enable', async c => { |
| 72 | + const body = await readJsonBody(c); |
| 73 | + const response = await proxyMorningBriefingRoute({ |
| 74 | + supervisor, |
| 75 | + gatewayToken: expectedToken, |
| 76 | + path: '/enable', |
| 77 | + method: 'POST', |
| 78 | + body, |
| 79 | + }); |
| 80 | + return response; |
| 81 | + }); |
| 82 | + |
| 83 | + app.post('/_kilo/morning-briefing/disable', async c => { |
| 84 | + const response = await proxyMorningBriefingRoute({ |
| 85 | + supervisor, |
| 86 | + gatewayToken: expectedToken, |
| 87 | + path: '/disable', |
| 88 | + method: 'POST', |
| 89 | + body: {}, |
| 90 | + }); |
| 91 | + return response; |
| 92 | + }); |
| 93 | + |
| 94 | + app.post('/_kilo/morning-briefing/run', async c => { |
| 95 | + const response = await proxyMorningBriefingRoute({ |
| 96 | + supervisor, |
| 97 | + gatewayToken: expectedToken, |
| 98 | + path: '/run', |
| 99 | + method: 'POST', |
| 100 | + body: {}, |
| 101 | + }); |
| 102 | + return response; |
| 103 | + }); |
| 104 | + |
| 105 | + app.get('/_kilo/morning-briefing/read/today', async c => { |
| 106 | + const response = await proxyMorningBriefingRoute({ |
| 107 | + supervisor, |
| 108 | + gatewayToken: expectedToken, |
| 109 | + path: '/read/today', |
| 110 | + method: 'GET', |
| 111 | + }); |
| 112 | + return response; |
| 113 | + }); |
| 114 | + |
| 115 | + app.get('/_kilo/morning-briefing/read/yesterday', async c => { |
| 116 | + const response = await proxyMorningBriefingRoute({ |
| 117 | + supervisor, |
| 118 | + gatewayToken: expectedToken, |
| 119 | + path: '/read/yesterday', |
| 120 | + method: 'GET', |
| 121 | + }); |
| 122 | + return response; |
| 123 | + }); |
| 124 | +} |
0 commit comments