import fs from "node:fs/promises";
import path from "node:path";
import { SESSIONS_DIR } from "../paths.js";
import { ensureDir, pathExists } from "../fs-utils.js";
export async function listSessionsOnDisk() {
    await ensureDir(SESSIONS_DIR);
    const files = await fs.readdir(SESSIONS_DIR);
    const out = [];
    for (const file of files) {
        if (!file.endsWith(".jsonl"))
            continue;
        const full = path.join(SESSIONS_DIR, file);
        try {
            const info = await summarize(full);
            if (info)
                out.push(info);
        }
        catch {
            // skip corrupt sessions
        }
    }
    out.sort((a, b) => b.startedAt.localeCompare(a.startedAt));
    return out;
}
async function summarize(file) {
    const raw = await fs.readFile(file, "utf8");
    const lines = raw.split("\n").filter(Boolean);
    if (lines.length === 0)
        return null;
    const header = JSON.parse(lines[0]);
    let messageCount = 0;
    let lastUser;
    for (let i = 1; i < lines.length; i++) {
        const entry = JSON.parse(lines[i]);
        if (entry.type !== "message")
            continue;
        messageCount++;
        if (entry.message.role === "user") {
            const text = textOf(entry.message);
            if (text)
                lastUser = text;
        }
    }
    return {
        id: header.id,
        path: file,
        startedAt: header.startedAt,
        cwd: header.cwd,
        model: header.model,
        messageCount,
        lastUserMessage: lastUser?.slice(0, 80),
    };
}
function textOf(message) {
    if (typeof message.content === "string")
        return message.content;
    return message.content
        .filter((b) => b.type === "text")
        .map((b) => b.text)
        .join(" ");
}
export async function loadSessionMessages(id) {
    const file = path.join(SESSIONS_DIR, `${id}.jsonl`);
    if (!(await pathExists(file))) {
        throw new Error(`session ${id} not found at ${file}`);
    }
    const raw = await fs.readFile(file, "utf8");
    const lines = raw.split("\n").filter(Boolean);
    const header = JSON.parse(lines[0]);
    const messages = [];
    for (let i = 1; i < lines.length; i++) {
        const entry = JSON.parse(lines[i]);
        if (entry.type === "message")
            messages.push(entry.message);
    }
    return { header, messages };
}
//# sourceMappingURL=list.js.map