Path: blob/main/src/vs/platform/agentHost/common/state/sessionTransport.ts
13399 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45// Transport abstraction for the sessions process protocol.6// See protocol.md -> Client-server protocol for the full design.7//8// The transport is pluggable — the same protocol runs over MessagePort9// (ProxyChannel), WebSocket, or stdio. This module defines the contract;10// concrete implementations live in platform-specific folders.1112import { Event } from '../../../../base/common/event.js';13import { IDisposable } from '../../../../base/common/lifecycle.js';14import type { ProtocolMessage, AhpServerNotification, JsonRpcNotification, JsonRpcResponse, JsonRpcRequest } from './sessionProtocol.js';1516/**17* A bidirectional transport for protocol messages. Implementations handle18* serialization, framing, and connection management.19*/20export interface IProtocolTransport extends IDisposable {21/** Fires when a message is received from the remote end. */22readonly onMessage: Event<ProtocolMessage>;2324/** Fires when the transport connection closes. */25readonly onClose: Event<void>;2627/**28* Send a message to the remote end.29*30* Accepts:31* - `ProtocolMessage` — fully-typed client↔server messages.32* - `AhpServerNotification` — server→client notifications.33* - `JsonRpcResponse` — dynamically-constructed success/error responses.34*/35send(message: ProtocolMessage | AhpServerNotification | JsonRpcNotification | JsonRpcResponse | JsonRpcRequest): void;36}3738/**39* A client-side transport that requires an explicit connection step40* before messages can be exchanged.41*/42export interface IClientTransport extends IProtocolTransport {43/** Establish the underlying connection (e.g. open a WebSocket). */44connect(): Promise<void>;45}4647/** Type guard for transports that require an explicit connection step. */48export function isClientTransport(transport: IProtocolTransport): transport is IClientTransport {49return typeof (transport as IClientTransport).connect === 'function';50}5152/**53* Server-side transport that accepts multiple client connections.54* Each connected client gets its own {@link IProtocolTransport}.55*/56export interface IProtocolServer extends IDisposable {57/** Fires when a new client connects. */58readonly onConnection: Event<IProtocolTransport>;5960/** The port or address the server is listening on. */61readonly address: string | undefined;62}636465