Path: blob/main/src/vs/platform/agentHost/common/state/protocol/messages.ts
13405 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// allow-any-unicode-comment-file6// DO NOT EDIT -- auto-generated by scripts/sync-agent-host-protocol.ts78import type { InitializeParams, InitializeResult, ReconnectParams, ReconnectResult, SubscribeParams, SubscribeResult, CreateSessionParams, DisposeSessionParams, CreateTerminalParams, DisposeTerminalParams, ListSessionsParams, ListSessionsResult, ResourceReadParams, ResourceReadResult, ResourceWriteParams, ResourceWriteResult, ResourceListParams, ResourceListResult, ResourceCopyParams, ResourceCopyResult, ResourceDeleteParams, ResourceDeleteResult, ResourceMoveParams, ResourceMoveResult, FetchTurnsParams, FetchTurnsResult, UnsubscribeParams, DispatchActionParams, AuthenticateParams, AuthenticateResult, ResolveSessionConfigParams, ResolveSessionConfigResult, SessionConfigCompletionsParams, SessionConfigCompletionsResult } from './commands.js';910import type { ActionEnvelope } from './actions.js';11import type { ProtocolNotification } from './notifications.js';1213// ─── JSON-RPC Base Types ─────────────────────────────────────────────────────1415/** A JSON-RPC request: has both `method` and `id`. */16export interface JsonRpcRequest {17readonly jsonrpc: '2.0';18readonly id: number;19readonly method: string;20readonly params?: unknown;21}2223/** A JSON-RPC success response. */24export interface JsonRpcSuccessResponse {25readonly jsonrpc: '2.0';26readonly id: number;27readonly result: unknown;28}2930/** A JSON-RPC error response. */31export interface JsonRpcErrorResponse {32readonly jsonrpc: '2.0';33readonly id: number;34readonly error: {35readonly code: number;36readonly message: string;37readonly data?: unknown;38};39}4041/** A JSON-RPC response (success or error). */42export type JsonRpcResponse = JsonRpcSuccessResponse | JsonRpcErrorResponse;4344/** A JSON-RPC notification: has `method` but no `id`. */45export interface JsonRpcNotification {46readonly jsonrpc: '2.0';47readonly method: string;48readonly params?: unknown;49}5051// ─── Command Map ─────────────────────────────────────────────────────────────5253/**54* Registry mapping each command method name to its params and result types.55*56* @category Commands57*/58export interface CommandMap {59'initialize': { params: InitializeParams; result: InitializeResult };60'reconnect': { params: ReconnectParams; result: ReconnectResult };61'subscribe': { params: SubscribeParams; result: SubscribeResult };62'createSession': { params: CreateSessionParams; result: null };63'disposeSession': { params: DisposeSessionParams; result: null };64'createTerminal': { params: CreateTerminalParams; result: null };65'disposeTerminal': { params: DisposeTerminalParams; result: null };66'listSessions': { params: ListSessionsParams; result: ListSessionsResult };67'resourceRead': { params: ResourceReadParams; result: ResourceReadResult };68'resourceWrite': { params: ResourceWriteParams; result: ResourceWriteResult };69'resourceList': { params: ResourceListParams; result: ResourceListResult };70'resourceCopy': { params: ResourceCopyParams; result: ResourceCopyResult };71'resourceDelete': { params: ResourceDeleteParams; result: ResourceDeleteResult };72'resourceMove': { params: ResourceMoveParams; result: ResourceMoveResult };73'fetchTurns': { params: FetchTurnsParams; result: FetchTurnsResult };74'authenticate': { params: AuthenticateParams; result: AuthenticateResult };75'resolveSessionConfig': { params: ResolveSessionConfigParams; result: ResolveSessionConfigResult };76'sessionConfigCompletions': { params: SessionConfigCompletionsParams; result: SessionConfigCompletionsResult };77}7879// ─── Notification Maps ───────────────────────────────────────────────────────8081/** Params for the server → client `notification` method. */82export interface NotificationMethodParams {83notification: ProtocolNotification;84}8586/**87* Registry mapping each client → server notification method to its params type.88*89* @category Notifications90*/91export interface ClientNotificationMap {92'unsubscribe': { params: UnsubscribeParams };93'dispatchAction': { params: DispatchActionParams };94}9596/**97* Registry mapping each server → client notification method to its params type.98*99* @category Notifications100*/101export interface ServerNotificationMap {102'action': { params: ActionEnvelope };103'notification': { params: NotificationMethodParams };104}105106/** Combined notification map for all directions. */107export type NotificationMap = ClientNotificationMap & ServerNotificationMap;108109// ─── Typed Requests ──────────────────────────────────────────────────────────110111/**112* A fully typed JSON-RPC request for a specific AHP command.113*114* When used as a union (default generic), narrowing on `method` gives typed `params`:115*116* ```ts117* function handle(req: AhpRequest) {118* if (req.method === 'fetchTurns') {119* req.params.session; // typed as URI120* }121* }122* ```123*/124export type AhpRequest<M extends keyof CommandMap = keyof CommandMap> =125M extends unknown ? {126readonly jsonrpc: '2.0';127readonly id: number;128readonly method: M;129readonly params: CommandMap[M]['params'];130} : never;131132// ─── Typed Responses ─────────────────────────────────────────────────────────133134/**135* A fully typed JSON-RPC success response for a specific AHP command.136*137* Since JSON-RPC responses do not carry `method`, use this with an explicit138* generic parameter when you know the method from the associated request:139*140* ```ts141* const result: AhpSuccessResponse<'fetchTurns'> = ...;142* result.result.turns; // typed as Turn[]143* ```144*/145export type AhpSuccessResponse<M extends keyof CommandMap = keyof CommandMap> =146M extends unknown ? {147readonly jsonrpc: '2.0';148readonly id: number;149readonly result: CommandMap[M]['result'];150} : never;151152/** Typed JSON-RPC response (success with known result type, or error). */153export type AhpResponse<M extends keyof CommandMap = keyof CommandMap> =154| AhpSuccessResponse<M>155| JsonRpcErrorResponse;156157// ─── Typed Notifications ─────────────────────────────────────────────────────158159/**160* A fully typed JSON-RPC notification for a specific AHP notification method.161*162* When used as a union (default generic), narrowing on `method` gives typed `params`:163*164* ```ts165* function handle(notif: AhpNotification) {166* if (notif.method === 'action') {167* notif.params.serverSeq; // typed as number168* }169* }170* ```171*/172export type AhpNotification<M extends keyof NotificationMap = keyof NotificationMap> =173M extends unknown ? {174readonly jsonrpc: '2.0';175readonly method: M;176readonly params: NotificationMap[M]['params'];177} : never;178179/** A client → server notification. */180export type AhpClientNotification<M extends keyof ClientNotificationMap = keyof ClientNotificationMap> =181M extends unknown ? {182readonly jsonrpc: '2.0';183readonly method: M;184readonly params: ClientNotificationMap[M]['params'];185} : never;186187/** A server → client notification. */188export type AhpServerNotification<M extends keyof ServerNotificationMap = keyof ServerNotificationMap> =189M extends unknown ? {190readonly jsonrpc: '2.0';191readonly method: M;192readonly params: ServerNotificationMap[M]['params'];193} : never;194195// ─── Protocol Message Union ──────────────────────────────────────────────────196197/**198* Discriminated union of all AHP protocol messages.199*200* Narrow using standard JSON-RPC structure:201* - Has `method` + `id` → request ({@link AhpRequest})202* - Has `method`, no `id` → notification ({@link AhpNotification})203* - Has `result` or `error` + `id` → response ({@link AhpResponse})204*205* Then narrow on `method` for fully typed params:206*207* ```ts208* function dispatch(msg: ProtocolMessage) {209* if ('method' in msg && 'id' in msg) {210* // msg is AhpRequest211* if (msg.method === 'fetchTurns') {212* msg.params.session; // URI213* }214* }215* }216* ```217*/218export type ProtocolMessage =219| AhpRequest220| AhpSuccessResponse221| JsonRpcErrorResponse222| AhpNotification;223224225