File size: 1,545 Bytes
7c5ed63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { Hono } from 'hono';
import { join } from 'node:path';
import { readFile } from 'node:fs/promises';

import { KIMI_CODE_HOME } from '../config';
import { isSafeAgentId, readSessionDetail } from '../lib/session-store';
import { isSafeBlobHash } from '../lib/blob-resolver';

export function blobsRoute(home: string = KIMI_CODE_HOME): Hono {
  const r = new Hono();
  r.get('/:id/blobs/:hash', async (c) => {
    const id = c.req.param('id');
    const agentId = c.req.query('agent') ?? 'main';
    const hash = c.req.param('hash');
    if (!isSafeAgentId(agentId)) {
      return c.json({ error: 'invalid agent id', code: 'BAD_REQUEST' }, 400);
    }
    if (!isSafeBlobHash(hash)) {
      return c.json({ error: 'invalid blob hash', code: 'BAD_REQUEST' }, 400);
    }
    const detail = await readSessionDetail(home, id);
    if (!detail) {
      return c.json({ error: 'session not found', code: 'NOT_FOUND' }, 404);
    }
    const agent = detail.agents.find((a) => a.agentId === agentId);
    if (!agent) {
      return c.json(
        { error: `agent "${agentId}" not found`, code: 'NOT_FOUND' },
        404,
      );
    }
    const blobPath = join(agent.homedir, 'blobs', hash);
    let content: Buffer;
    try {
      content = await readFile(blobPath);
    } catch {
      return c.json({ error: 'blob not found', code: 'NOT_FOUND' }, 404);
    }
    const mimeType = c.req.query('mime') ?? 'application/octet-stream';
    return new Response(content, {
      headers: { 'content-type': mimeType },
    });
  });
  return r;
}