MCPcopy Create free account
hub / github.com/MoonshotAI/kimi-code / contextRoute

Function contextRoute

apps/vis/server/src/routes/context.ts:10–54  ·  view source on GitHub ↗
(home: string = KIMI_CODE_HOME)

Source from the content-addressed store, hash-verified

8import { projectContext } from '../lib/context-projector';
9
10export 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}

Callers 2

context.test.tsFile · 0.90
createAppFunction · 0.90

Calls 7

isSafeAgentIdFunction · 0.90
readSessionDetailFunction · 0.90
readAgentWireFunction · 0.90
rehydrateWireEntriesFunction · 0.90
projectContextFunction · 0.90
jsonMethod · 0.80
getMethod · 0.65

Tested by

no test coverage detected