import fs from "node:fs";
import path from "node:path";
import { randomUUID } from "node:crypto";
export class SessionStorage {
    sessionId;
    filePath;
    constructor(dataDir, sessionId = `session_${randomUUID()}`) {
        this.sessionId = sessionId;
        const dir = path.join(dataDir, "sessions");
        fs.mkdirSync(dir, { recursive: true });
        this.filePath = path.join(dir, `${sessionId}.jsonl`);
    }
    append(record) {
        const full = {
            id: `msg_${randomUUID()}`,
            createdAtMs: Date.now(),
            ...record,
        };
        fs.appendFileSync(this.filePath, `${JSON.stringify(full)}\n`, "utf8");
        return full;
    }
    readAll(limit = 200) {
        if (!fs.existsSync(this.filePath))
            return [];
        return fs.readFileSync(this.filePath, "utf8")
            .split(/\r?\n/)
            .filter(Boolean)
            .slice(-limit)
            .map((line) => JSON.parse(line));
    }
    static list(dataDir, limit = 20) {
        const dir = path.join(dataDir, "sessions");
        if (!fs.existsSync(dir))
            return [];
        return fs.readdirSync(dir)
            .filter((name) => name.endsWith(".jsonl"))
            .map((name) => {
            const full = path.join(dir, name);
            const stat = fs.statSync(full);
            return {
                sessionId: name.slice(0, -".jsonl".length),
                path: full,
                updatedAtMs: stat.mtimeMs,
                bytes: stat.size,
            };
        })
            .sort((a, b) => b.updatedAtMs - a.updatedAtMs)
            .slice(0, limit);
    }
}
