/** * WebSocket transport envelope — the wire-framing layer for the live channel. * * The live channel is the live plane between core-mcp and the user. * This file defines HOW that plane is framed on a WebSocket: the * dispatch discriminator (`WebSocketMessageType`), the discriminated * union envelope (`WebSocketMessage`), and the client-side connection- * lifecycle enum (`ConnectionStatus`). * * The CONTRACT payload shapes (what each variant carries — * `SubscribePayload`, `AckPayload`, `StreamEnvelope`, etc.) live in * `../types/live-channel`. Renaming the envelope or swapping in an * alternate transport (e.g., a binary-framed variant, or SSE fallback) * would not change the payload contract; that asymmetry is why the * split exists. * * This module is exported as a subpath — `@ggui-ai/protocol/transport/ * websocket` — so only transport implementors (hosted Lambda handlers, * OSS `/ws` server, web/RN WebSocketManager, connector ws-connection) * pay its type/build cost. Consumers that only need contract payloads * stay on the root import. */ import type { ActionEnvelope } from '../types/events.js'; import type { JsonObject } from '../types/data-contract.js'; import type { SubscribePayload, AckPayload, ErrorPayload, RenderPayload, StreamEnvelope, PropsUpdatePayload, ChannelSubscribePayload, ChannelUnsubscribePayload, ChannelPayloadFrame, ChannelErrorPayload, DrainAckPayload } from '../types/live-channel.js'; import type { HostContextObservedPayload } from '../types/host-context.js'; import type { GguiSessionEvent } from '../types/ggui-session-event.js'; /** * WebSocket message types for client-server communication. * Each type maps to a specific payload shape in the {@link WebSocketMessage} discriminated union. */ export type WebSocketMessageType = 'action' | 'subscribe' | 'ping' | 'pong' | 'ack' | 'error' | 'render' | 'data' | 'props_update' | 'channel_subscribe' | 'channel_unsubscribe' | 'channel_payload' | 'channel_error' | 'drain_ack' | 'host_context_observed' | 'render_event'; /** Fields shared by all WebSocket message variants. */ interface WsMessageBase { requestId?: string; } /** * Discriminated union of all WebSocket messages. * The `type` field narrows `payload` automatically in switch/if blocks — * no type casts needed. Each variant pairs a `WebSocketMessageType` with * its corresponding payload interface. * * @example * ```typescript * function handle(msg: WebSocketMessage) { * switch (msg.type) { * case 'ack': * msg.payload.sequence; // AckPayload — auto-narrowed * break; * case 'error': * msg.payload.message; // ErrorPayload — auto-narrowed * msg.payload.details; // JsonValue | undefined * break; * case 'render': * msg.payload.session; // RenderPayload — auto-narrowed * break; * case 'data': * msg.payload.payload; // StreamEnvelope.payload * break; * } * } * ``` */ export type WebSocketMessage = (WsMessageBase & { type: 'action'; payload: ActionEnvelope; }) | (WsMessageBase & { type: 'subscribe'; payload: SubscribePayload; }) | (WsMessageBase & { type: 'ping'; payload: JsonObject; }) | (WsMessageBase & { type: 'pong'; payload: JsonObject; }) | (WsMessageBase & { type: 'ack'; payload: AckPayload; }) | (WsMessageBase & { type: 'error'; payload: ErrorPayload; }) | (WsMessageBase & { type: 'render'; payload: RenderPayload; }) | (WsMessageBase & { type: 'data'; payload: StreamEnvelope; }) | (WsMessageBase & { type: 'props_update'; payload: PropsUpdatePayload; }) | (WsMessageBase & { type: 'channel_subscribe'; payload: ChannelSubscribePayload; }) | (WsMessageBase & { type: 'channel_unsubscribe'; payload: ChannelUnsubscribePayload; }) | (WsMessageBase & { type: 'channel_payload'; payload: ChannelPayloadFrame; }) | (WsMessageBase & { type: 'channel_error'; payload: ChannelErrorPayload; }) | (WsMessageBase & { type: 'drain_ack'; payload: DrainAckPayload; }) | (WsMessageBase & { type: 'host_context_observed'; payload: HostContextObservedPayload; }) | (WsMessageBase & { type: 'render_event'; payload: GguiSessionEvent; }); /** * WebSocket connection status. Client-side transport enum describing * the browser/Node ws.readyState lifecycle. */ export type ConnectionStatus = 'connecting' | 'connected' | 'disconnected' | 'reconnecting'; export {}; //# sourceMappingURL=websocket.d.ts.map