/**
 * AgentAnnouncer - 에이전트 실행 이벤트 통보 + 통계 수집
 * EventEmitter 기반, in-memory only
 */
import { EventEmitter } from 'events';
// ============================================
// AgentAnnouncer
// ============================================
const MAX_HISTORY = 100;
function truncate(text, maxLen) {
    if (text.length <= maxLen)
        return text;
    return text.slice(0, maxLen) + '...';
}
export class AgentAnnouncer extends EventEmitter {
    history = [];
    stats = this.createEmptyStats();
    constructor() {
        super();
        this.setMaxListeners(20);
    }
    announceStart(event) {
        event.prompt = truncate(event.prompt, 200);
        event.timestamp = event.timestamp || Date.now();
        this.stats.totalLaunched++;
        this.updateModelStats(event.model, 0);
        this.pushHistory(event);
        this.emit('agent-start', event);
    }
    announceComplete(event) {
        event.resultSummary = truncate(event.resultSummary, 500);
        event.timestamp = event.timestamp || Date.now();
        if (event.success) {
            this.stats.totalCompleted++;
        }
        else {
            this.stats.totalFailed++;
        }
        this.stats.totalDuration += event.duration;
        const finishedCount = this.stats.totalCompleted + this.stats.totalFailed;
        this.stats.avgDuration = finishedCount > 0
            ? this.stats.totalDuration / finishedCount
            : 0;
        this.updateModelStats(event.model, event.duration);
        this.updateAgentStats(event.agentName, event.success);
        this.pushHistory(event);
        this.emit('agent-complete', event);
    }
    announceError(event) {
        event.timestamp = event.timestamp || Date.now();
        this.stats.totalFailed++;
        this.stats.totalDuration += event.duration;
        const finishedCount = this.stats.totalCompleted + this.stats.totalFailed;
        this.stats.avgDuration = finishedCount > 0
            ? this.stats.totalDuration / finishedCount
            : 0;
        this.pushHistory(event);
        this.emit('agent-error', event);
    }
    getStats() {
        return { ...this.stats };
    }
    getRecentHistory(limit = 20) {
        return this.history.slice(-limit);
    }
    resetStats() {
        this.stats = this.createEmptyStats();
        this.history = [];
    }
    formatStatusLine(event) {
        if ('success' in event && 'resultSummary' in event) {
            const e = event;
            const icon = e.success ? 'OK' : 'FAIL';
            return `[${icon}] ${e.agentName} (${e.model}) ${e.duration}ms`;
        }
        if ('error' in event && 'retryable' in event) {
            const e = event;
            return `[ERR] ${e.agentName} ${e.duration}ms: ${truncate(e.error, 80)}`;
        }
        const e = event;
        return `[START] ${e.agentName} (${e.model})`;
    }
    // ============================================
    // Private
    // ============================================
    pushHistory(event) {
        this.history.push(event);
        if (this.history.length > MAX_HISTORY) {
            this.history.shift();
        }
    }
    updateModelStats(model, duration) {
        if (!this.stats.byModel[model]) {
            this.stats.byModel[model] = { count: 0, totalDuration: 0 };
        }
        this.stats.byModel[model].count++;
        this.stats.byModel[model].totalDuration += duration;
    }
    updateAgentStats(agentName, success) {
        if (!this.stats.byAgent[agentName]) {
            this.stats.byAgent[agentName] = { count: 0, successRate: 0 };
        }
        const agent = this.stats.byAgent[agentName];
        agent.count++;
        // Recalculate success rate (approximate based on running total)
        if (success) {
            agent.successRate = agent.successRate + (1 - agent.successRate) / agent.count;
        }
        else {
            agent.successRate = agent.successRate - agent.successRate / agent.count;
        }
    }
    createEmptyStats() {
        return {
            totalLaunched: 0,
            totalCompleted: 0,
            totalFailed: 0,
            totalDuration: 0,
            avgDuration: 0,
            byModel: {},
            byAgent: {},
        };
    }
}
// 싱글톤
export const agentAnnouncer = new AgentAnnouncer();
//# sourceMappingURL=AgentAnnouncer.js.map