File size: 1,468 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
import { Hono } from 'hono';
import { rm } from 'node:fs/promises';
import { KIMI_CODE_HOME } from '../config';
import { revealInOs } from '../lib/reveal';
import { listSessions, readSessionDetail } from '../lib/session-store';

export function sessionsRoute(home: string = KIMI_CODE_HOME): Hono {
  const r = new Hono();
  r.get('/', async (c) => {
    const sessions = await listSessions(home);
    return c.json({ sessions });
  });
  r.delete('/:id', async (c) => {
    const id = c.req.param('id');
    const all = await listSessions(home);
    const target = all.find((s) => s.sessionId === id);
    if (!target) return c.json({ error: 'session not found', code: 'NOT_FOUND' }, 404);
    await rm(target.sessionDir, { recursive: true, force: true });
    return c.json({ sessionId: id, deleted: true });
  });
  // Open the session directory in the OS file manager. The folder is
  // opened on the SERVER host — only meaningful when vis runs locally.
  r.post('/:id/reveal', async (c) => {
    const id = c.req.param('id');
    const detail = await readSessionDetail(home, id);
    if (!detail) return c.json({ error: 'session not found', code: 'NOT_FOUND' }, 404);
    try {
      await revealInOs(detail.sessionDir);
      return c.json({ sessionId: id, opened: detail.sessionDir });
    } catch (err) {
      return c.json(
        { error: `failed to open: ${(err as Error).message}`, code: 'READ_ERROR' },
        500,
      );
    }
  });
  return r;
}