/**
 * @fileoverview SSE stream manager — owns all SSE client state, broadcasting, and event batching.
 *
 * Extracted from server.ts for modularity. Handles:
 * - SSE client connection tracking with subscription filtering
 * - Backpressure-aware message delivery
 * - Terminal data batching with adaptive intervals (16-50ms for 60fps)
 * - Task update and session state batching
 * - Dead client cleanup and keepalive
 * - Cloudflare tunnel padding for proxy buffer flushing
 *
 * @dependencies CleanupManager (managed timers), config/server-timing (constants)
 * @consumedby web/server.ts (WebServer delegates all SSE operations here)
 *
 * @module web/sse-stream-manager
 */
import type { FastifyReply } from 'fastify';
import type { BackgroundTask } from '../session.js';
import { CleanupManager } from '../utils/index.js';
/** Dependencies injected by WebServer — keeps SseStreamManager decoupled from session/respawn state. */
interface SseStreamManagerDeps {
    /** Get session state with respawn info for session:updated broadcasts */
    getSessionStateWithRespawn(sessionId: string): unknown;
}
export declare class SseStreamManager {
    private deps;
    private cleanup;
    /**
     * SSE clients mapped to their session subscription filter.
     * Value is a Set of session IDs the client wants events for,
     * or `null` meaning "receive all events" (backwards-compatible default).
     */
    private sseClients;
    /** Optional client-supplied IDs → reply, for live filter updates without reconnecting */
    private sseClientsById;
    /** SSE clients connecting from non-localhost (i.e. through tunnel) */
    private remoteSseClients;
    /** Clients with backpressure — skip writes until 'drain' fires */
    private backpressuredClients;
    /** Cached tunnel active state — updated on TunnelStarted/TunnelStopped to avoid getUrl() on every broadcast */
    private _isTunnelActive;
    private terminalBatches;
    private terminalBatchSizes;
    private terminalBatchTimers;
    private lastTerminalEventTime;
    private taskUpdateBatches;
    private taskUpdateBatchTimerId;
    private stateUpdatePending;
    private stateUpdateTimerId;
    private _isStopping;
    constructor(deps: SseStreamManagerDeps, cleanup: CleanupManager);
    get clientCount(): number;
    get remoteClientCount(): number;
    get isTunnelActive(): boolean;
    setTunnelActive(active: boolean): void;
    addClient(reply: FastifyReply, sessionFilter: Set<string> | null, isRemote: boolean, clientId?: string): void;
    removeClient(reply: FastifyReply): void;
    /**
     * Update an existing client's session subscription filter without forcing
     * an SSE reconnect. Returns true if the client was found and updated.
     */
    updateClientFilter(clientId: string, sessions: string[] | null): boolean;
    /** Send a single SSE event to a specific client. */
    sendSSE(reply: FastifyReply, event: string, data: unknown): void;
    /** Send pre-formatted tunnel padding to a specific client. */
    sendPadding(reply: FastifyReply): void;
    private sendSSEPreformatted;
    broadcast(event: string, data: unknown): void;
    batchTerminalData(sessionId: string, data: string): void;
    /** Flush a single session's batched terminal data */
    private flushSessionTerminalBatch;
    batchTaskUpdate(sessionId: string, task: BackgroundTask): void;
    private flushTaskUpdateBatches;
    /**
     * Debounce expensive session:updated broadcasts.
     * Instead of calling toDetailedState() on every event, batch requests
     * and only serialize once per STATE_UPDATE_DEBOUNCE_INTERVAL.
     */
    broadcastSessionStateDebounced(sessionId: string): void;
    private flushStateUpdates;
    /**
     * Clean up dead SSE clients and send keep-alive comments.
     * Keep-alive prevents proxy/load-balancer timeouts on idle connections.
     * Dead client cleanup prevents memory leaks from abruptly terminated connections.
     */
    cleanupDeadClients(): void;
    /** Clean up all batching state for a session (call on session exit or deletion). */
    cleanupSessionBatches(sessionId: string): void;
    setStopping(): void;
    /** Graceful shutdown: notify clients, close connections, clear all state. */
    stop(): void;
}
export {};
//# sourceMappingURL=sse-stream-manager.d.ts.map