/**
 * WebSocket Transport Layer
 *
 * Implements WebSocket-based transport for the Gateway server.
 * Provides real-time bidirectional communication for agents, tools, and sessions.
 *
 * Inspired by OpenClaw's WebSocket gateway (ws://127.0.0.1:18789)
 */
import WebSocket from 'ws';
import { EventEmitter } from 'events';
import { GatewayServer } from './server.js';
import type { GatewayConfig, GatewayMessage, ClientState } from './types.js';
/**
 * WebSocket-specific configuration
 */
export interface WebSocketTransportConfig extends GatewayConfig {
    /** Path for WebSocket endpoint */
    path: string;
    /** Enable compression */
    perMessageDeflate: boolean;
    /** Maximum payload size */
    maxPayload: number;
    /** Heartbeat interval (ms) */
    heartbeatInterval: number;
    /** Client timeout (ms) */
    clientTimeout: number;
    /** Enable binary messages */
    binaryMode: boolean;
    /** CORS origins */
    corsOrigins: string[];
}
/**
 * Default WebSocket configuration
 */
export declare const DEFAULT_WS_CONFIG: WebSocketTransportConfig;
/**
 * WebSocket client connection
 */
interface WebSocketClient {
    id: string;
    socket: WebSocket;
    state: ClientState;
    isAlive: boolean;
    ip?: string;
    userAgent?: string;
}
/**
 * WebSocket-based Gateway Server
 *
 * Extends the base GatewayServer with WebSocket transport
 */
export declare class WebSocketGateway extends GatewayServer {
    private wsConfig;
    private wss;
    private httpServer;
    private wsClients;
    private heartbeatInterval;
    constructor(config?: Partial<WebSocketTransportConfig>);
    /**
     * Start the WebSocket server
     */
    start(): Promise<void>;
    /**
     * Stop the WebSocket server
     */
    stop(): Promise<void>;
    /**
     * Handle new WebSocket connection
     */
    private handleConnection;
    /**
     * Handle incoming WebSocket message
     */
    private handleMessage;
    /**
     * Handle client disconnect
     */
    private handleDisconnect;
    /**
     * Send message to a specific client
     */
    protected sendToClient(clientId: string, message: GatewayMessage): void;
    /**
     * Broadcast to all connected clients
     */
    broadcast(message: GatewayMessage, filter?: (client: WebSocketClient) => boolean): void;
    /**
     * Broadcast to session (override with WebSocket-specific impl)
     */
    broadcastToSession(sessionId: string, message: GatewayMessage, excludeClientId?: string): void;
    /**
     * Start heartbeat interval
     */
    private startHeartbeat;
    /**
     * Stop heartbeat interval
     */
    private stopHeartbeat;
    /**
     * Get WebSocket-specific statistics
     */
    getWebSocketStats(): {
        running: boolean;
        host: string;
        port: number;
        path: string;
        clients: number;
        sessions: number;
        authenticatedClients: number;
        uptime: number;
    };
    /**
     * Get client info by ID
     */
    getClientInfo(clientId: string): {
        id: string;
        authenticated: boolean;
        sessions: string[];
        connectedAt: number;
        lastActivityAt: number;
        ip?: string;
        userAgent?: string;
    } | null;
    /**
     * Get all connected client IDs
     */
    getConnectedClientIds(): string[];
    /**
     * Kick a client
     */
    kickClient(clientId: string, reason?: string): boolean;
}
/**
 * Agent capabilities
 */
export interface AgentCapabilities {
    /** Agent can process chat messages */
    chat: boolean;
    /** Agent can execute tools */
    tools: string[];
    /** Agent can stream responses */
    streaming: boolean;
    /** Agent supported modes */
    modes: string[];
    /** Custom capabilities */
    custom?: Record<string, unknown>;
}
/**
 * Registered agent
 */
export interface RegisteredAgent {
    id: string;
    type: 'pi' | 'cli' | 'webchat' | 'companion' | 'channel' | 'custom';
    name: string;
    capabilities: AgentCapabilities;
    status: 'online' | 'offline' | 'busy';
    clientId?: string;
    registeredAt: number;
    lastSeenAt: number;
    metadata?: Record<string, unknown>;
}
/**
 * Agent Registry
 *
 * Manages registered agents and their capabilities
 */
export declare class AgentRegistry extends EventEmitter {
    private agents;
    private gateway;
    constructor(gateway: WebSocketGateway);
    /**
     * Register an agent
     */
    register(agent: Omit<RegisteredAgent, 'registeredAt' | 'lastSeenAt'>): RegisteredAgent;
    /**
     * Unregister an agent
     */
    unregister(agentId: string): boolean;
    /**
     * Update agent status
     */
    updateStatus(agentId: string, status: RegisteredAgent['status'], clientId?: string): boolean;
    /**
     * Get agent by ID
     */
    getAgent(agentId: string): RegisteredAgent | undefined;
    /**
     * Get all agents
     */
    getAllAgents(): RegisteredAgent[];
    /**
     * Get online agents
     */
    getOnlineAgents(): RegisteredAgent[];
    /**
     * Find agents by capability
     */
    findByCapability(capability: string): RegisteredAgent[];
    /**
     * Find agents by type
     */
    findByType(type: RegisteredAgent['type']): RegisteredAgent[];
    /**
     * Broadcast message to agents
     */
    broadcastToAgents(message: GatewayMessage, filter?: (agent: RegisteredAgent) => boolean): void;
    /**
     * Get registry statistics
     */
    getStats(): {
        total: number;
        online: number;
        offline: number;
        busy: number;
        byType: Record<string, number>;
    };
}
/**
 * Control message types for gateway coordination
 */
export type ControlMessageType = 'agent_register' | 'agent_unregister' | 'agent_heartbeat' | 'route_request' | 'route_response' | 'broadcast' | 'sync_state';
/**
 * Control message
 */
export interface ControlMessage {
    type: ControlMessageType;
    source: string;
    target?: string;
    payload: unknown;
    timestamp: number;
}
/**
 * Create a control message
 */
export declare function createControlMessage(type: ControlMessageType, source: string, payload: unknown, target?: string): ControlMessage;
/**
 * Get WebSocket gateway instance
 */
export declare function getWebSocketGateway(config?: Partial<WebSocketTransportConfig>): WebSocketGateway;
/**
 * Reset WebSocket gateway instance
 */
export declare function resetWebSocketGateway(): Promise<void>;
export default WebSocketGateway;
