#!/usr/bin/env node
/**
 * CLI wrapper for voice-ingest-session-text.
 *
 * Invoked by the SessionEnd hook after each admin PTY session:
 *   node dist/cli/ingest-session-text.js <transcript_path>
 *
 * All other parameters are read from environment variables inherited from
 * the PTY session (which inherits from the session manager's systemd unit):
 *
 *   ACCOUNT_ID    — operator's account identifier
 *   USER_ID       — operator's userId (= adminUserId)
 *   SESSION_ID    — session identifier (parsed by hook from stdin)
 *   NEO4J_URI     — neo4j bolt URI
 *   NEO4J_USER    — neo4j username
 *   NEO4J_PASSWORD — neo4j password
 *
 * Exit codes:
 *   0 — success (includes no-new-turns case)
 *   1 — error (missing required env, JSONL not found, DB error)
 *
 * stdout: one-line result summary ("turns=N skipped=M") for the hook to
 * capture and forward to the log-ingest endpoint.
 * stderr: detailed diagnostics (tool log lines).
 */
import { voiceIngestSessionText } from "../tools/voice-ingest-session-text.js";
import { closeDriver } from "../lib/neo4j.js";
const transcriptPath = process.argv[2];
if (!transcriptPath) {
    process.stderr.write("[ingest-session-text] error: transcript path required as argv[2]\n");
    process.exit(1);
}
const accountId = process.env.ACCOUNT_ID ?? "";
const userId = process.env.USER_ID ?? "";
const sessionId = process.env.SESSION_ID ?? undefined;
if (!accountId || !userId) {
    process.stderr.write(`[ingest-session-text] error: ACCOUNT_ID and USER_ID must be set in env (accountId=${accountId || "empty"} userId=${userId || "empty"})\n`);
    process.exit(1);
}
try {
    const result = await voiceIngestSessionText({
        sessionJsonlPath: transcriptPath,
        accountId,
        userId,
        sessionId,
    });
    // Emit one-line summary to stdout for the hook to capture.
    process.stdout.write(`turns=${result.ingestedCount} skipped=${result.skippedCount} sessionId=${result.sessionId ?? "unknown"}\n`);
    process.exit(0);
}
catch (err) {
    process.stderr.write(`[ingest-session-text] error: ${err instanceof Error ? err.message : String(err)}\n`);
    process.exit(1);
}
finally {
    await closeDriver();
}
//# sourceMappingURL=ingest-session-text.js.map