ktg-plugin-marketplace/plugins/ai-psychosis/hooks/scripts/report-reader.mjs

83 lines
2.4 KiB
JavaScript

// report-reader.mjs — Aggregates sessions.jsonl into a JSON summary.
// Dual-mode: importable (named exports) or directly executable.
// Backward-compatible with v1.0.0 records that lack pushback / domain_context.
import { readFileSync, existsSync } from 'fs';
export function readSessions(path) {
if (!existsSync(path)) return [];
return readFileSync(path, 'utf8')
.split('\n')
.filter(Boolean)
.map(line => {
try { return JSON.parse(line); } catch { return null; }
})
.filter(Boolean);
}
export function aggregateSessions(sessions) {
let pushback_total = 0;
let relationship_domain_count = 0;
let other_domain_count = 0;
let null_domain_count = 0;
let v1_0_records = 0;
let v1_1_records = 0;
let total_end_records = 0;
let total_dependency = 0;
let total_escalation = 0;
let total_fatigue = 0;
let total_validation = 0;
for (const rec of sessions) {
if (!rec || rec.note === 'no_state_file') continue;
if (rec.duration_min === undefined) continue;
total_end_records += 1;
const flags = rec.flags || {};
const pushback = flags.pushback;
if (pushback === undefined || pushback === null) v1_0_records += 1;
else v1_1_records += 1;
pushback_total += Number(pushback) || 0;
total_dependency += Number(flags.dependency) || 0;
total_escalation += Number(flags.escalation) || 0;
total_fatigue += Number(flags.fatigue) || 0;
total_validation += Number(flags.validation) || 0;
const dc = rec.domain_context;
if (dc === null || dc === undefined) null_domain_count += 1;
else if (dc === 'relationship') relationship_domain_count += 1;
else other_domain_count += 1;
}
return {
pushback_total,
relationship_domain_count,
other_domain_count,
null_domain_count,
total_end_records,
flags_total: {
dependency: total_dependency,
escalation: total_escalation,
fatigue: total_fatigue,
validation: total_validation,
pushback: pushback_total,
},
schema_version: {
v1_0_records,
v1_1_records,
},
};
}
if (import.meta.url === `file://${process.argv[1]}`) {
const path = process.argv[2];
if (!path) {
process.stderr.write('Usage: node report-reader.mjs <path-to-sessions.jsonl>\n');
process.exit(1);
}
const result = aggregateSessions(readSessions(path));
process.stdout.write(JSON.stringify(result, null, 2) + '\n');
}