/**
 * SessionStore - 백그라운드 에이전트 세션 저장소
 */
// TTL 설정
const SESSION_TTL = 60 * 60 * 1000; // 1시간
const SESSION_HISTORY_TTL = 24 * 60 * 60 * 1000; // 24시간
const CLEANUP_INTERVAL = 10 * 60 * 1000; // 10분
/**
 * SessionStore - 세션 저장 및 관리
 */
class SessionStoreImpl {
    activeSessions = new Map();
    sessionHistory = [];
    cleanupTimer = null;
    constructor() {
        this.startCleanupTimer();
    }
    startCleanupTimer() {
        if (this.cleanupTimer)
            return;
        this.cleanupTimer = setInterval(() => this.cleanup(), CLEANUP_INTERVAL);
        if (this.cleanupTimer.unref) {
            this.cleanupTimer.unref();
        }
    }
    cleanup() {
        const now = Date.now();
        // 활성 세션 정리
        for (const [sessionId, session] of this.activeSessions.entries()) {
            if (now - session.createdAt > SESSION_TTL) {
                if (session.handle.status === 'running') {
                    session.cancelController.abort();
                }
                this.activeSessions.delete(sessionId);
            }
        }
        // 히스토리 정리
        const cutoff = now - SESSION_HISTORY_TTL;
        while (this.sessionHistory.length > 0 && this.sessionHistory[0].startTime < cutoff) {
            this.sessionHistory.shift();
        }
    }
    add(sessionId, data) {
        this.activeSessions.set(sessionId, data);
    }
    get(sessionId) {
        return this.activeSessions.get(sessionId);
    }
    delete(sessionId) {
        this.activeSessions.delete(sessionId);
    }
    addToHistory(info) {
        this.sessionHistory.push(info);
    }
    findInHistory(sessionId) {
        return this.sessionHistory.find(s => s.sessionId === sessionId);
    }
    getActiveSessions() {
        return Array.from(this.activeSessions.values());
    }
    getRecentHistory(limit) {
        return this.sessionHistory.slice(-limit).reverse();
    }
}
// 싱글톤 인스턴스
export const sessionStore = new SessionStoreImpl();
/**
 * 활성 세션 목록 조회
 */
export function listActiveSessions() {
    const sessions = sessionStore.getActiveSessions().map(s => ({
        sessionId: s.handle.sessionId,
        agentName: s.handle.agentName,
        status: s.handle.status,
        startTime: new Date(s.handle.startTime).toISOString(),
        runningFor: `${((Date.now() - s.handle.startTime) / 1000).toFixed(0)}s`
    }));
    if (sessions.length === 0) {
        return {
            content: [{ type: 'text', text: 'No active background agents' }]
        };
    }
    let summary = `## Active Background Agents (${sessions.length})\n\n`;
    for (const session of sessions) {
        summary += `- **${session.agentName}** (${session.sessionId})\n`;
        summary += `  Status: ${session.status} | Running: ${session.runningFor}\n`;
    }
    return {
        content: [{ type: 'text', text: summary }],
        sessions
    };
}
/**
 * 세션 히스토리 조회
 */
export function getSessionHistory(limit = 10) {
    const recent = sessionStore.getRecentHistory(limit);
    if (recent.length === 0) {
        return {
            content: [{ type: 'text', text: 'No session history' }]
        };
    }
    let summary = `## Session History (last ${recent.length})\n\n`;
    for (const session of recent) {
        const duration = session.endTime
            ? `${((session.endTime - session.startTime) / 1000).toFixed(1)}s`
            : 'N/A';
        summary += `- **${session.agentName}** (${session.sessionId})\n`;
        summary += `  Status: ${session.status} | Duration: ${duration}\n`;
    }
    return {
        content: [{ type: 'text', text: summary }],
        history: recent
    };
}
//# sourceMappingURL=SessionStore.js.map