"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.resumeSession = resumeSession;
exports.forkSession = forkSession;
exports.getLatestSession = getLatestSession;
const sessionStorage_1 = require("./sessionStorage");
async function resumeSession(projectDir, sessionId, model) {
    const storage = new sessionStorage_1.SessionStorage(projectDir, model, sessionId);
    const events = await storage.readAll();
    const meta = await storage.getMeta();
    // Rebuild messages from transcript events
    // Note: session-scoped permissions are NOT restored (deliberate safety choice)
    const messages = eventsToMessages(events);
    return {
        messages,
        meta,
        eventCount: events.length,
    };
}
async function forkSession(projectDir, sourceSessionId, model) {
    const source = new sessionStorage_1.SessionStorage(projectDir, model, sourceSessionId);
    const events = await source.readAll();
    const messages = eventsToMessages(events);
    // Create new session with a reference to the parent
    const newStorage = new sessionStorage_1.SessionStorage(projectDir, model);
    await newStorage.initialize();
    // Replay messages into the new session transcript
    for (const event of events) {
        await newStorage.append(event.type, event.data);
    }
    await newStorage.updateMeta({ parentSessionId: sourceSessionId });
    // Add fork marker
    await newStorage.append('system', {
        kind: 'system',
        content: `Forked from session ${sourceSessionId}`,
    });
    return { newStorage, messages };
}
function eventsToMessages(events) {
    const messages = [];
    for (const event of events) {
        switch (event.data.kind) {
            case 'message':
                messages.push(event.data.message);
                break;
            case 'tool_result':
                messages.push({
                    role: 'tool',
                    content: event.data.result.output,
                });
                break;
            case 'compact_boundary':
                messages.length = 0;
                messages.push({
                    role: 'system',
                    content: `[Compacted summary]\n${event.data.summary}`,
                });
                break;
            case 'continuity_checkpoint':
                messages.length = 0;
                messages.push({
                    role: 'system',
                    content: checkpointToMessage(event.data.checkpoint),
                });
                break;
            // tool_call and system events don't produce standalone messages
        }
    }
    return messages;
}
function checkpointToMessage(checkpoint) {
    return [
        '[Continuity checkpoint]',
        `Goal: ${checkpoint.currentGoal}`,
        `Summary: ${checkpoint.summary}`,
        `Next action: ${checkpoint.nextAction}`,
        checkpoint.openQuestions.length ? `Open questions: ${checkpoint.openQuestions.join('; ')}` : '',
        checkpoint.recentMessages.length ? `Recent context: ${checkpoint.recentMessages.join(' | ')}` : '',
    ].filter(Boolean).join('\n');
}
async function getLatestSession(projectDir) {
    const sessions = await sessionStorage_1.SessionStorage.listSessions(projectDir);
    return sessions.length > 0 ? sessions[0] : null;
}
//# sourceMappingURL=resume.js.map