| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- #!/usr/bin/env node
- /**
- * CLI view of the audit trail — the offline equivalent of GET /admin/events.
- *
- * npm run events -- --limit 20
- * npm run events -- --conversation 1311
- * npm run events -- --jobs
- */
- import { db, disconnectDb } from '../src/store/db.js';
- function arg(name: string): string | undefined {
- const i = process.argv.indexOf(`--${name}`);
- return i >= 0 ? process.argv[i + 1] : undefined;
- }
- async function main(): Promise<void> {
- const limit = Number(arg('limit') ?? 30);
- const conversationId = arg('conversation') ? Number(arg('conversation')) : undefined;
- if (process.argv.includes('--jobs')) {
- const jobs = await db().job.findMany({ orderBy: { updatedAt: 'desc' }, take: limit });
- for (const j of jobs) {
- console.log(
- `${j.updatedAt.toISOString()} ${j.status.padEnd(10)} attempts=${j.attempts} ${j.type}` +
- (j.lastError ? `\n error: ${j.lastError}` : ''),
- );
- }
- return;
- }
- const events = await db().auditEvent.findMany({
- where: conversationId ? { conversationId } : undefined,
- orderBy: { createdAt: 'desc' },
- take: limit,
- });
- for (const e of events) {
- console.log(
- `${e.createdAt.toISOString()} conv=${e.conversationId ?? '-'} ${e.eventType.padEnd(22)} ${e.summary}`,
- );
- }
- console.log(`\n${events.length} event(s).`);
- }
- main()
- .catch((err: unknown) => {
- console.error(err instanceof Error ? err.message : String(err));
- process.exitCode = 1;
- })
- .finally(() => void disconnectDb());
|