(home: string = KIMI_CODE_HOME)
| 7 | import { isSafeBlobHash } from '../lib/blob-resolver'; |
| 8 | |
| 9 | export function blobsRoute(home: string = KIMI_CODE_HOME): Hono { |
| 10 | const r = new Hono(); |
| 11 | r.get('/:id/blobs/:hash', async (c) => { |
| 12 | const id = c.req.param('id'); |
| 13 | const agentId = c.req.query('agent') ?? 'main'; |
| 14 | const hash = c.req.param('hash'); |
| 15 | if (!isSafeAgentId(agentId)) { |
| 16 | return c.json({ error: 'invalid agent id', code: 'BAD_REQUEST' }, 400); |
| 17 | } |
| 18 | if (!isSafeBlobHash(hash)) { |
| 19 | return c.json({ error: 'invalid blob hash', code: 'BAD_REQUEST' }, 400); |
| 20 | } |
| 21 | const detail = await readSessionDetail(home, id); |
| 22 | if (!detail) { |
| 23 | return c.json({ error: 'session not found', code: 'NOT_FOUND' }, 404); |
| 24 | } |
| 25 | const agent = detail.agents.find((a) => a.agentId === agentId); |
| 26 | if (!agent) { |
| 27 | return c.json( |
| 28 | { error: `agent "${agentId}" not found`, code: 'NOT_FOUND' }, |
| 29 | 404, |
| 30 | ); |
| 31 | } |
| 32 | const blobPath = join(agent.homedir, 'blobs', hash); |
| 33 | let content: Buffer; |
| 34 | try { |
| 35 | content = await readFile(blobPath); |
| 36 | } catch { |
| 37 | return c.json({ error: 'blob not found', code: 'NOT_FOUND' }, 404); |
| 38 | } |
| 39 | const mimeType = c.req.query('mime') ?? 'application/octet-stream'; |
| 40 | return new Response(content, { |
| 41 | headers: { 'content-type': mimeType }, |
| 42 | }); |
| 43 | }); |
| 44 | return r; |
| 45 | } |
no test coverage detected