(home: string = KIMI_CODE_HOME)
| 8 | import { projectContext } from '../lib/context-projector'; |
| 9 | |
| 10 | export function contextRoute(home: string = KIMI_CODE_HOME): Hono { |
| 11 | const r = new Hono(); |
| 12 | r.get('/:id/context', async (c) => { |
| 13 | const id = c.req.param('id'); |
| 14 | const agentId = c.req.query('agent') ?? 'main'; |
| 15 | if (!isSafeAgentId(agentId)) { |
| 16 | return c.json({ error: 'invalid agent id', code: 'BAD_REQUEST' }, 400); |
| 17 | } |
| 18 | const detail = await readSessionDetail(home, id); |
| 19 | if (!detail) { |
| 20 | return c.json({ error: 'session not found', code: 'NOT_FOUND' }, 404); |
| 21 | } |
| 22 | const agent = detail.agents.find((a) => a.agentId === agentId); |
| 23 | if (!agent || !agent.wireExists) { |
| 24 | return c.json({ error: 'agent wire not found', code: 'NOT_FOUND' }, 404); |
| 25 | } |
| 26 | try { |
| 27 | const wire = await readAgentWire( |
| 28 | join(detail.sessionDir, 'agents', agentId, 'wire.jsonl'), |
| 29 | ); |
| 30 | const baseUrl = new URL(c.req.url).origin; |
| 31 | rehydrateWireEntries(wire.records, id, agentId, baseUrl); |
| 32 | // `?history=full` reconstructs the FULL pre-compaction/undo/clear history |
| 33 | // for debugging; the default mirrors the model's-eye post-compaction view. |
| 34 | const mode = c.req.query('history') === 'full' ? 'full' : 'model'; |
| 35 | const proj = projectContext(wire.records, mode); |
| 36 | return c.json({ |
| 37 | sessionId: id, |
| 38 | agentId, |
| 39 | messages: proj.messages, |
| 40 | usage: proj.usage, |
| 41 | contextTokens: proj.contextTokens, |
| 42 | config: proj.config, |
| 43 | permission: proj.permission, |
| 44 | planMode: proj.planMode, |
| 45 | goal: proj.goal, |
| 46 | swarm: proj.swarm, |
| 47 | }); |
| 48 | } catch (err) { |
| 49 | const msg = (err as Error).message; |
| 50 | return c.json({ error: msg, code: 'READ_ERROR' }, 500); |
| 51 | } |
| 52 | }); |
| 53 | return r; |
| 54 | } |
no test coverage detected