import { ChatSessionRecords } from '../../../../../core/chat/engine/sessions/records/index.js';
import { ConversationCompactionService } from '../../../../../core/chat/engine/compaction/index.js';
import { ChatSessionLeases } from '../../../../../core/chat/engine/sessions/leases/index.js';
import { FileChatSessionRepository } from '../../../../../core/chat/engine/sessions/repository/index.js';
/**
 * Owns pre-run session lease acquisition and preflight compaction persistence.
 */
export class ConversationTurnPreflightService {
    static async prepare(args) {
        const persistedSession = new FileChatSessionRepository({ sessionStoragePath: args.sessionStoragePath })
            .read(args.sessionId);
        const leaseConflict = persistedSession ? ChatSessionLeases.conflict(persistedSession, args.leaseOwner) : undefined;
        if (leaseConflict) {
            return {
                ok: false,
                reason: 'lease_conflict',
                message: leaseConflict,
            };
        }
        const leasedSession = persistedSession ? ChatSessionRecords.touch(ChatSessionLeases.acquire(persistedSession, args.leaseOwner)) : undefined;
        const initialHistory = leasedSession?.history ?? args.fallbackHistory;
        const compactionRuntime = args;
        const compactionRequest = {
            toolNames: args.toolNames,
            goal: args.prompt,
        };
        const preflightCompacted = await ConversationCompactionService.compact({
            history: initialHistory,
            runtime: compactionRuntime,
            session: { id: args.sessionId },
            request: compactionRequest,
            summarizer: args.summarizer,
            onStatusChange: (event) => {
                args.host.onCompactionStatus?.(event, 'preflight');
                if (event.status === 'running' && leasedSession) {
                    ConversationTurnPreflightService.persistRunningSeed({
                        ...args,
                        leasedSession,
                        archivePath: event.archivePath,
                    });
                }
            },
        });
        return ConversationTurnPreflightService.persistPrepared({
            ...args,
            session: persistedSession ?? ConversationTurnPreflightService.readRequiredFallbackSession(args.sessions, args.sessionId),
            compacted: preflightCompacted,
        });
    }
    static persistRunningSeed(args) {
        const compactionSeed = ChatSessionRecords.touch({
            ...args.leasedSession,
            context: ConversationTurnPreflightService.buildRunningCompactionContext(args),
        });
        new FileChatSessionRepository({ sessionStoragePath: args.sessionStoragePath })
            .save(args.sessions.map((candidate) => (candidate.id === args.sessionId ? compactionSeed : candidate)));
    }
    static persistPrepared(args) {
        const preparedSession = ChatSessionRecords.touch(ChatSessionRecords.applyCompactedHistory({
            session: args.session,
            compacted: args.compacted,
            preserveAcceptedUserMessages: true,
        }));
        new FileChatSessionRepository({ sessionStoragePath: args.sessionStoragePath })
            .save(args.sessions.map((candidate) => (candidate.id === args.session.id ? preparedSession : candidate)));
        return {
            ok: true,
            session: preparedSession,
            compacted: args.compacted,
        };
    }
    static readRequiredFallbackSession(sessions, sessionId) {
        const session = sessions.find((candidate) => candidate.id === sessionId);
        if (!session) {
            throw new Error(`Chat session not found: ${sessionId}`);
        }
        return session;
    }
    static buildRunningCompactionContext(args) {
        return ConversationCompactionService.buildSessionRunningContext({
            session: args.leasedSession,
            lastArchivePath: args.archivePath,
        });
    }
}
//# sourceMappingURL=turn-preflight-service.js.map