import { Disposable } from "../../../base/common/lifecycle.js"; import { ILogService } from "../../log/common/log.service.js"; import { AgentHostClientConnectionKind } from "../common/agentHostTelemetry.js"; import { AhpJsonlLogger } from "../common/ahpJsonlLogger.js"; import { type AhpServerNotification, type JsonRpcNotification, type JsonRpcRequest, type JsonRpcResponse, type ProtocolMessage } from "../common/state/sessionProtocol.js"; import type { IClientTransport } from "../common/state/sessionTransport.js"; import { type ParseGroupNameOptions } from "../common/webPubSub/groups.js"; /** * Minimal structural subset of the browser `WebSocket` interface this transport * depends on, so a fake socket can be injected in tests via * {@link IWebPubSubRelayTransportOptions.webSocketFactory}. */ export interface IWebSocketLike { send(data: string): void; close(code?: number, reason?: string): void; onopen: (() => void) | null; onmessage: ((event: { data: unknown; }) => void) | null; onclose: ((event: { code: number; reason: string; }) => void) | null; onerror: ((event: unknown) => void) | null; } /** Opens an {@link IWebSocketLike} for `url` negotiating `subprotocol`. */ export type WebSocketFactory = (url: string, subprotocol: string) => IWebSocketLike; export interface IWebPubSubRelayTransportOptions { readonly clientConnectionKind?: AgentHostClientConnectionKind.MissionControl | AgentHostClientConnectionKind.WebPubSub; /** Mission Control's logical client ID, shared with the AHP initialize request. */ readonly clientId: string; /** Full WebSocket URL (including the `access_token` and `clientId` query params). */ readonly url: string; /** Group to publish outbound AHP messages to (the `to_host` lane). */ readonly toHostGroup: string; /** Groups to join on connect (`broadcast` + `to_client`). */ readonly joinGroups: readonly string[]; /** Forwarded to {@link parseInbound} for uid/eid/cid pinning. */ readonly groupValidation?: ParseGroupNameOptions; /** Opens the underlying WebSocket. Defaults to a real browser socket. */ readonly webSocketFactory?: WebSocketFactory; /** Invoked for malformed frames or failed relay operations. */ readonly onProtocolError?: (err: unknown) => void; /** Content-free receive counter, including control, malformed and chunk frames. */ readonly onDidReceiveFrame?: () => void; /** * Records every AHP frame to a JSONL transcript when * `chat.agentHost.ahpJsonlLoggingEnabled` is on. Cloud sandbox hosts do not implement * `vscode/collectAgentHostDebugLogs`, so this is the only way to see their frames. */ readonly ahpLogger?: AhpJsonlLogger; } /** * AHP client transport over the Web PubSub reliable JSON subprotocol. * * Lifecycle: * 1. {@link connect} opens the WebSocket, waits for the WPS `connected` system * event, joins the requested groups, then advertises receive capabilities * and awaits its publish acknowledgement before resolving. * 2. Inbound messages are acknowledged and deduplicated before group-fanout * reassembly and delivery via {@link onMessage}; outbound messages are * chunked and published to the `to_host` lane by {@link send}. * 3. {@link dispose} (or a socket close/error) fires {@link onClose} once. */ export declare class WebPubSubRelayTransport extends Disposable implements IClientTransport { private readonly _options; private readonly _logService; get clientConnectionKind(): AgentHostClientConnectionKind; private readonly _onMessage; readonly onMessage: import("../../../base/common/event.js").Event; private readonly _onDidReceiveData; /** Fires for each host chunk of a message that is still being reassembled. */ readonly onDidReceiveData: import("../../../base/common/event.js").Event; private readonly _onClose; readonly onClose: import("../../../base/common/event.js").Event; private readonly _reassembler; private readonly _sweepTimer; private readonly _publishAckTimer; private readonly _handshakeTimer; private _ws; private _ackId; private _lastReceivedSequenceId; private readonly _pendingJoinAcks; /** Publish acknowledgement deadlines in send order. */ private readonly _pendingPublishAcks; private _rejectConnect; private _initialCapabilitiesAckId; /** Guards against firing onClose / resolving connect more than once. */ private _closed; private _connectResolved; private _generation; private _handshakeId; private readonly _requests; private readonly _closedBeforeHandshake; private readonly _watch; private _publishAcknowledged; private _hostFrames; private _hostMessages; private _lastHostFrameMs; private _protocolErrors; private _expiredAssemblies; constructor(_options: IWebPubSubRelayTransportOptions, _logService: ILogService); get isOpen(): boolean; /** * Open a WPS WebSocket connection with the reliable JSON subprotocol and * complete the join handshake. Resolves once connected, rejects on * error/timeout/early-close. */ connect(): Promise; /** * Handle a frame received during the connect handshake: the WPS `connected` * system event, joinGroup acks, or (defensively) early payload frames. */ private _handleHandshakeFrame; private _advertiseReceiveCapabilities; /** Switch the socket handlers over to steady-state observation. */ private _startObserving; /** Handle publish acknowledgements and reassemble incoming group frames. */ private _handleInboundFrame; private _deliver; /** Publish each chunk once; rejection or a missing acknowledgement after 30 seconds fails the transport. */ send(message: ProtocolMessage | AhpServerNotification | JsonRpcNotification | JsonRpcResponse | JsonRpcRequest): void; private _publishFrame; private _logProtocolMessage; private _schedulePublishAckTimeout; private _checkPublishAckTimeout; private _sendRaw; private _logContext; private _reportProtocolError; /** Fire onClose exactly once and stop background work. */ private _fireClose; /** Stop background work and close the socket without firing onClose. */ private _closeSocket; dispose(): void; }