clawdie-ai/scripts/agent-logs.ts
Mevy Assistant 66a36a6548 refactor(multitenant): centralize controlplane session paths
Introduce a shared controlplane paths helper and use it in runtime plus operator tooling. This removes another tenant-derived path assumption and aligns controlplane session logs with the actual tmp-based layout used by the platform.

---
Build: pass | Tests: pass — 105 passed (7 files)
2026-04-23 10:11:40 +02:00

69 lines
1.9 KiB
TypeScript

import fs from 'fs';
import path from 'path';
import { TMP_DIR } from '../src/config.js';
import {
getControlplanePiSessionDir,
getControlplaneSessionDir,
} from '../src/controlplane-paths.js';
const agentId = process.argv[2] ?? '';
const SESSIONS_DIR = getControlplaneSessionDir(TMP_DIR);
const PI_SESSIONS_DIR = getControlplanePiSessionDir(SESSIONS_DIR);
function findSessionFiles(): string[] {
const files: string[] = [];
const roots = [SESSIONS_DIR, PI_SESSIONS_DIR];
for (const root of roots) {
if (!fs.existsSync(root)) continue;
for (const entry of fs.readdirSync(root, { withFileTypes: true })) {
if (entry.name.endsWith('.jsonl')) {
files.push(path.join(root, entry.name));
}
}
}
return files.sort().reverse();
}
function tailFile(filePath: string, n: number): string[] {
const content = fs.readFileSync(filePath, 'utf-8');
const lines = content.split('\n').filter(Boolean);
return lines.slice(-n);
}
const files = findSessionFiles();
if (files.length === 0) {
console.log('No session logs found.');
process.exit(0);
}
const filtered = agentId
? files.filter((f) => path.basename(f).includes(agentId))
: files;
if (filtered.length === 0) {
console.log(`No sessions matching: ${agentId}`);
process.exit(0);
}
for (const file of filtered.slice(0, 5)) {
const name = path.basename(file);
console.log(`\n=== ${name} ===`);
const lines = tailFile(file, 10);
for (const line of lines) {
try {
const entry = JSON.parse(line) as Record<string, unknown>;
const role = entry.role ?? '?';
const content =
typeof entry.content === 'string'
? entry.content.slice(0, 120)
: JSON.stringify(entry.content).slice(0, 120);
console.log(` [${role}] ${content}`);
} catch {
console.log(` ${line.slice(0, 120)}`);
}
}
}
console.log(`\n${filtered.length} session(s) found`);