/**
 * Session persistence for Clawx.
 *
 * EXTRACTION NOTE:
 * OpenClaw's session store (config/sessions/store.ts, 400+ lines) handles:
 * - TTL-based in-memory caching (45s)
 * - Atomic disk writes with lock acquisition
 * - Legacy session key migration
 * - Delivery context normalization
 * - Entry count capping, stale pruning, disk budget enforcement
 * - File rotation on size limits
 *
 * pi-coding-agent provides a SessionManager that handles session persistence
 * with file-based storage, entries, compaction, and branching.
 *
 * For Clawx, we use a SIMPLER approach:
 * - JSON file per session in ~/.clawx/sessions/
 * - Session = id + messages + metadata
 * - Load/save by session ID
 * - List recent sessions
 * - No caching, no TTL, no locking, no rotation
 *
 * This replaces the OpenClaw session store with ~100 lines.
 */
import fs from "node:fs";
import path from "node:path";
import crypto from "node:crypto";
import { log } from "../utils/logger.js";
function ensureDir(dir) {
    if (!fs.existsSync(dir)) {
        fs.mkdirSync(dir, { recursive: true });
    }
}
function sessionPath(dir, id) {
    return path.join(dir, `${id}.json`);
}
export function createSessionId() {
    return crypto.randomBytes(8).toString("hex");
}
export function saveSession(sessionDir, session, messages) {
    ensureDir(sessionDir);
    const data = { session, messages };
    const filePath = sessionPath(sessionDir, session.id);
    fs.writeFileSync(filePath, JSON.stringify(data, null, 2), "utf-8");
    log.debug(`Session saved: ${session.id}`);
}
export function loadSession(sessionDir, id) {
    const filePath = sessionPath(sessionDir, id);
    if (!fs.existsSync(filePath))
        return null;
    try {
        const raw = fs.readFileSync(filePath, "utf-8");
        return JSON.parse(raw);
    }
    catch (e) {
        log.warn(`Failed to load session ${id}: ${e}`);
        return null;
    }
}
export function listSessions(sessionDir) {
    ensureDir(sessionDir);
    try {
        const files = fs.readdirSync(sessionDir).filter((f) => f.endsWith(".json"));
        const sessions = [];
        for (const file of files) {
            try {
                const raw = fs.readFileSync(path.join(sessionDir, file), "utf-8");
                const data = JSON.parse(raw);
                sessions.push(data.session);
            }
            catch {
                // Skip corrupt sessions
            }
        }
        return sessions.sort((a, b) => b.startedAt - a.startedAt);
    }
    catch {
        return [];
    }
}
export function getLatestSession(sessionDir) {
    const sessions = listSessions(sessionDir);
    if (sessions.length === 0)
        return null;
    return loadSession(sessionDir, sessions[0].id);
}
//# sourceMappingURL=session.js.map