import path from "node:path";
import os from "node:os";
import crypto from "node:crypto";
import { SessionManager, } from "./core/session-manager.js";
const SESSION_DIR = path.join(os.homedir(), ".gg", "sessions");
const sessionManager = new SessionManager(SESSION_DIR);
export async function createSession(cwd, provider, model, sessionsDir = SESSION_DIR) {
    const manager = sessionsDir === SESSION_DIR ? sessionManager : new SessionManager(sessionsDir);
    const created = await manager.create(cwd, provider, model);
    return {
        id: created.id,
        path: created.path,
        async append(entry) {
            if (entry.type !== "message")
                return;
            const managedEntry = {
                type: "message",
                id: crypto.randomUUID(),
                parentId: null,
                timestamp: entry.timestamp,
                message: entry.message,
            };
            await manager.appendEntry(created.path, managedEntry);
        },
    };
}
// ── Load Session ────────────────────────────────────────────
export async function loadSession(sessionPath, sessionsDir = SESSION_DIR) {
    const manager = sessionsDir === SESSION_DIR ? sessionManager : new SessionManager(sessionsDir);
    const loaded = await manager.load(sessionPath);
    const header = {
        type: "session",
        version: 1,
        id: loaded.header.id,
        timestamp: loaded.header.timestamp,
        cwd: loaded.header.cwd,
        provider: loaded.header.provider,
        model: loaded.header.model,
    };
    return {
        header,
        messages: manager.getMessages(loaded.entries, loaded.header.leafId),
    };
}
// ── List Sessions ───────────────────────────────────────────
export async function listSessions(cwd, sessionsDir = SESSION_DIR) {
    const manager = sessionsDir === SESSION_DIR ? sessionManager : new SessionManager(sessionsDir);
    return manager.list(cwd);
}
// ── Get Most Recent Session ─────────────────────────────────
export async function getMostRecentSession(cwd, sessionsDir = SESSION_DIR) {
    const manager = sessionsDir === SESSION_DIR ? sessionManager : new SessionManager(sessionsDir);
    return manager.getMostRecent(cwd);
}
// ── Persist Messages ────────────────────────────────────────
export function persistMessage(session, message) {
    const entry = {
        type: "message",
        timestamp: new Date().toISOString(),
        message,
    };
    return session.append(entry);
}
//# sourceMappingURL=session.js.map