"use strict";
/**
 * Turn counting plugin with message deduplication.
 *
 * The SDK emits multiple assistant events per turn (one per content block)
 * with the same message ID. This plugin deduplicates and publishes turn
 * counts + a duplicate flag for downstream plugins.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.TurnCounterPlugin = void 0;
class TurnCounterPlugin {
    name = 'turns';
    lastMessageId = null;
    phaseTurns = 0;
    totalTurns = 0;
    isDuplicate = false;
    phaseSnapshots = [];
    currentPhase = 'setup';
    onMessage(message, _ctx, store) {
        if (message.type !== 'assistant') {
            this.isDuplicate = false;
            store.set('turns', this.getData());
            return;
        }
        const msgId = message.message?.id;
        this.isDuplicate = msgId != null && msgId === this.lastMessageId;
        if (msgId)
            this.lastMessageId = msgId;
        if (!this.isDuplicate) {
            this.phaseTurns++;
            this.totalTurns++;
        }
        store.set('turns', this.getData());
    }
    onPhaseTransition(fromPhase, _toPhase, _ctx, store) {
        this.phaseSnapshots.push({ phase: fromPhase, turns: this.phaseTurns });
        this.currentPhase = _toPhase;
        this.phaseTurns = 0;
        this.lastMessageId = null;
        store.set('turns', this.getData());
    }
    onFinalize(_resultMessage, _totalDurationMs, _ctx, store) {
        this.phaseSnapshots.push({
            phase: this.currentPhase,
            turns: this.phaseTurns,
        });
        store.set('turns', this.getData());
    }
    getData() {
        return {
            isDuplicate: this.isDuplicate,
            phaseTurns: this.phaseTurns,
            totalTurns: this.totalTurns,
            phaseSnapshots: [...this.phaseSnapshots],
        };
    }
}
exports.TurnCounterPlugin = TurnCounterPlugin;
