import crypto from "node:crypto";
export async function appendMessagesToSession(sessionManager, sessionPath, messages, startIndex = 0) {
    for (let i = startIndex; i < messages.length; i++) {
        const msg = messages[i];
        if (msg.role === "system")
            continue;
        const entry = {
            type: "message",
            id: crypto.randomUUID(),
            parentId: null,
            timestamp: new Date().toISOString(),
            message: msg,
        };
        await sessionManager.appendEntry(sessionPath, entry);
    }
}
export async function createCompactedSessionCheckpoint(sessionManager, options) {
    const session = await sessionManager.create(options.cwd, options.provider, options.model, {
        conversationId: options.conversationId,
        preview: options.preview ?? options.title,
    });
    await appendMessagesToSession(sessionManager, session.path, options.messages, 0);
    if (options.title) {
        const titleEntry = {
            type: "label",
            id: crypto.randomUUID(),
            parentId: null,
            timestamp: new Date().toISOString(),
            label: options.title,
        };
        await sessionManager.appendEntry(session.path, titleEntry);
    }
    return { path: session.path, id: session.id };
}
export function getRestoredMessagesForDisplay(messages) {
    return messages.filter((msg) => msg.role !== "system");
}
export function formatRestoreInfoText(originalCount, restoredCount) {
    if (originalCount === restoredCount) {
        return `↻ Restored session (${originalCount} messages)`;
    }
    return `↻ Restored compacted session (${originalCount} → ${restoredCount} messages)`;
}
//# sourceMappingURL=session-compaction.js.map