(home: string = KIMI_CODE_HOME)
| 7 | import { readAgentWire } from '../lib/wire-reader'; |
| 8 | |
| 9 | export function wireRoute(home: string = KIMI_CODE_HOME): Hono { |
| 10 | const r = new Hono(); |
| 11 | r.get('/:id/wire', async (c) => { |
| 12 | const id = c.req.param('id'); |
| 13 | const agentId = c.req.query('agent') ?? 'main'; |
| 14 | if (!isSafeAgentId(agentId)) { |
| 15 | return c.json({ error: 'invalid agent id', code: 'BAD_REQUEST' }, 400); |
| 16 | } |
| 17 | const detail = await readSessionDetail(home, id); |
| 18 | if (!detail) { |
| 19 | return c.json({ error: 'session not found', code: 'NOT_FOUND' }, 404); |
| 20 | } |
| 21 | const agent = detail.agents.find((a) => a.agentId === agentId); |
| 22 | if (!agent) { |
| 23 | return c.json({ error: `agent "${agentId}" not found`, code: 'NOT_FOUND' }, 404); |
| 24 | } |
| 25 | if (!agent.wireExists) { |
| 26 | return c.json({ error: 'wire missing', code: 'NOT_FOUND' }, 404); |
| 27 | } |
| 28 | try { |
| 29 | const result = await readAgentWire( |
| 30 | join(detail.sessionDir, 'agents', agentId, 'wire.jsonl'), |
| 31 | ); |
| 32 | const baseUrl = new URL(c.req.url).origin; |
| 33 | rehydrateWireEntries(result.records, id, agentId, baseUrl); |
| 34 | return c.json({ |
| 35 | sessionId: id, |
| 36 | agentId, |
| 37 | protocolVersion: result.metadata.protocolVersion, |
| 38 | metadata: result.metadata, |
| 39 | records: result.records, |
| 40 | warnings: result.warnings, |
| 41 | }); |
| 42 | } catch (err) { |
| 43 | const msg = (err as Error).message; |
| 44 | return c.json({ error: msg, code: 'READ_ERROR' }, 500); |
| 45 | } |
| 46 | }); |
| 47 | return r; |
| 48 | } |
no test coverage detected