"use strict";
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (C) 2024-2026 Gracker (Chris)
// This file is part of SmartPerfetto. See LICENSE for details.
Object.defineProperty(exports, "__esModule", { value: true });
exports.finalizeAgentDrivenSession = finalizeAgentDrivenSession;
function finalizeAgentDrivenSession(input, deps) {
    const { sessionId, query, traceId, session, result } = input;
    const { logger } = session;
    session.result = result;
    delete session.completedAnalysisFinalArtifacts;
    delete session.completedAnalysisSseEvents;
    delete session.completedAnalysisSseEventsQualityGateVersion;
    const existingIds = new Set(session.hypotheses.map(h => h.id));
    for (const h of result.hypotheses) {
        if (!existingIds.has(h.id)) {
            session.hypotheses.push(h);
            existingIds.add(h.id);
        }
        else {
            const idx = session.hypotheses.findIndex(existing => existing.id === h.id);
            if (idx >= 0)
                session.hypotheses[idx] = h;
        }
    }
    const currentTurn = session.runSequence || 1;
    if (!session.conclusionHistory)
        session.conclusionHistory = [];
    if (result.conclusion) {
        session.conclusionHistory.push({
            turn: currentTurn,
            conclusion: result.conclusion,
            confidence: result.confidence ?? 0,
            timestamp: Date.now(),
        });
    }
    const finalQualityIssue = deps.applyFinalResultQualityGate({ result, query });
    if (finalQualityIssue) {
        const update = {
            type: 'degraded',
            content: {
                module: 'agentRoutes',
                fallback: 'final_result_quality_gate',
                code: finalQualityIssue.code,
                partial: true,
                message: result.terminationMessage || finalQualityIssue.message,
            },
            timestamp: Date.now(),
        };
        deps.broadcast(sessionId, update);
        const conversationStep = deps.buildConversationStepUpdate(session, update);
        if (conversationStep) {
            deps.appendConversationStep(session, conversationStep);
            deps.broadcast(sessionId, conversationStep);
        }
    }
    deps.annotateLatestCompletedTurn(sessionId, traceId, result);
    const terminalRunStatus = deps.terminalRunStatusForResult(result);
    session.status = terminalRunStatus === 'quota_exceeded'
        ? 'quota_exceeded'
        : result.success ? 'completed' : 'failed';
    deps.markSessionRunStatus(session, terminalRunStatus);
    logger.info(input.logComponent, 'Agent-driven result finalized', {
        confidence: result.confidence,
        rounds: result.rounds,
        findingsCount: result.findings.length,
        hypothesesCount: result.hypotheses.length,
        claimSupportCount: result.claimSupport?.length || 0,
        claimVerifierStatus: result.claimVerificationResult?.status,
        partial: result.partial,
        terminationReason: result.terminationReason,
        runId: session.activeRun?.runId,
        requestId: session.activeRun?.requestId,
        runSequence: session.activeRun?.sequence,
    });
    deps.persistAgentTurn({
        session,
        sessionId,
        traceId,
        query,
        result: {
            conclusion: result.conclusion,
            totalDurationMs: result.totalDurationMs,
            partial: result.partial,
            terminationMessage: result.terminationMessage,
        },
        logger,
        logComponent: input.logComponent,
    });
    deps.ensureCompletedAnalysisSseEvents(session);
    const clientCount = session.sseClients.length;
    session.sseClients.forEach((client, index) => {
        try {
            logger.info('AgentRoutes', `Sending finalized result to client ${index + 1}/${clientCount}`);
            deps.sendAgentDrivenResult(client, session);
        }
        catch (e) {
            logger.error('AgentRoutes', `Error sending finalized result to client ${index + 1}`, e);
        }
    });
    logger.close();
}
//# sourceMappingURL=finalizeAgentDrivenSession.js.map