File size: 2,008 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 47 48 49 | import { Hono } from 'hono';
import { join } from 'node:path';
import { KIMI_CODE_HOME } from '../config';
import { discoverLogFiles, readLogs } from '../lib/log-reader';
import { readSessionDetail } from '../lib/session-store';
const SESSION_LOG_REL = ['logs', 'kimi-code.log'] as const;
const GLOBAL_LOG_REL = ['logs', 'global', 'kimi-code.log'] as const;
const HOME_GLOBAL_LOG_REL = ['logs', 'kimi-code.log'] as const;
export function logsRoute(home: string = KIMI_CODE_HOME): Hono {
const r = new Hono();
r.get('/:id/logs', async (c) => {
const id = c.req.param('id');
const which = c.req.query('which') === 'global' ? 'global' : 'session';
const detail = await readSessionDetail(home, id);
if (!detail) {
return c.json({ error: 'session not found', code: 'NOT_FOUND' }, 404);
}
const sessionLog = join(detail.sessionDir, ...SESSION_LOG_REL);
// The global diagnostic log is a single shared file. In an exported bundle
// it is captured under the session dir (logs/global/kimi-code.log); for a
// live local session it lives at <KIMI_CODE_HOME>/logs/kimi-code.log
// (the engine's global log path), NOT under the session dir.
const globalLog = detail.imported
? join(detail.sessionDir, ...GLOBAL_LOG_REL)
: join(home, ...HOME_GLOBAL_LOG_REL);
// Either log may have rotated (kimi-code.log.1, .2, …); discover the active
// file plus its archives so a bundle with only rotated logs still surfaces.
const sessionFiles = await discoverLogFiles(sessionLog);
const globalFiles = await discoverLogFiles(globalLog);
const available = {
session: sessionFiles.length > 0,
global: globalFiles.length > 0,
};
const targetFiles = which === 'global' ? globalFiles : sessionFiles;
const result = await readLogs(targetFiles);
return c.json({
sessionId: id,
which,
available,
lines: result?.lines ?? [],
truncated: result?.truncated ?? false,
});
});
return r;
}
|