import { ConversationCompactionService } from '../../../../../core/chat/engine/compaction/index.js';
import { ChatSessionRecords } from '../../../../../core/chat/engine/sessions/records/index.js';
import { FileChatSessionRepository } from '../../../../../core/chat/engine/sessions/repository/index.js';
import { ConversationTurnArtifacts } from './turn-artifacts.js';
/**
 * Owns writing the completed turn back to the session catalog.
 */
export class ConversationTurnPersistenceService {
    static async persistCompleted(args) {
        const artifactInput = args;
        const persisted = await ConversationTurnArtifacts.persist({
            ...artifactInput,
            summarizer: { credentialSource: args.credentialSource },
            createTurnId: () => `server-turn-${Date.now()}`,
            onCompactionStatus: (event) => {
                args.host.onCompactionStatus?.(event, 'final');
                if (event.status === 'running') {
                    ConversationTurnPersistenceService.persistFinalCompactionRunningSeed({
                        ...args,
                        archivePath: event.archivePath,
                    });
                }
            },
        });
        const repository = new FileChatSessionRepository({ sessionStoragePath: args.sessionStoragePath });
        const latestSessions = repository.list();
        const latestSession = latestSessions.find((candidate) => candidate.id === args.session.id);
        const session = latestSession
            ? {
                ...persisted.session,
                queuedPrompts: latestSession.queuedPrompts,
            }
            : persisted.session;
        repository.save(latestSessions.map((candidate) => (candidate.id === args.session.id ? session : candidate)));
        return {
            ...persisted,
            session,
        };
    }
    static persistFinalCompactionRunningSeed(args) {
        const sourceHistory = args.result.transcript;
        const compactionSeed = ChatSessionRecords.touch({
            ...args.session,
            history: sourceHistory,
            context: ConversationTurnPersistenceService.buildRunningCompactionContext({
                ...args,
                sourceHistory,
            }),
        });
        new FileChatSessionRepository({ sessionStoragePath: args.sessionStoragePath })
            .save(args.sessions.map((candidate) => (candidate.id === args.session.id ? compactionSeed : candidate)));
    }
    static buildRunningCompactionContext(args) {
        return ConversationCompactionService.buildSessionRunningContext({
            session: args.session,
            history: args.sourceHistory,
            lastArchivePath: args.archivePath,
        });
    }
}
//# sourceMappingURL=turn-persistence-service.js.map