Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/platform/agentHost/common/state/protocol/messages.ts
13405 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
// allow-any-unicode-comment-file
7
// DO NOT EDIT -- auto-generated by scripts/sync-agent-host-protocol.ts
8
9
import 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';
10
11
import type { ActionEnvelope } from './actions.js';
12
import type { ProtocolNotification } from './notifications.js';
13
14
// ─── JSON-RPC Base Types ─────────────────────────────────────────────────────
15
16
/** A JSON-RPC request: has both `method` and `id`. */
17
export interface JsonRpcRequest {
18
readonly jsonrpc: '2.0';
19
readonly id: number;
20
readonly method: string;
21
readonly params?: unknown;
22
}
23
24
/** A JSON-RPC success response. */
25
export interface JsonRpcSuccessResponse {
26
readonly jsonrpc: '2.0';
27
readonly id: number;
28
readonly result: unknown;
29
}
30
31
/** A JSON-RPC error response. */
32
export interface JsonRpcErrorResponse {
33
readonly jsonrpc: '2.0';
34
readonly id: number;
35
readonly error: {
36
readonly code: number;
37
readonly message: string;
38
readonly data?: unknown;
39
};
40
}
41
42
/** A JSON-RPC response (success or error). */
43
export type JsonRpcResponse = JsonRpcSuccessResponse | JsonRpcErrorResponse;
44
45
/** A JSON-RPC notification: has `method` but no `id`. */
46
export interface JsonRpcNotification {
47
readonly jsonrpc: '2.0';
48
readonly method: string;
49
readonly params?: unknown;
50
}
51
52
// ─── Command Map ─────────────────────────────────────────────────────────────
53
54
/**
55
* Registry mapping each command method name to its params and result types.
56
*
57
* @category Commands
58
*/
59
export interface CommandMap {
60
'initialize': { params: InitializeParams; result: InitializeResult };
61
'reconnect': { params: ReconnectParams; result: ReconnectResult };
62
'subscribe': { params: SubscribeParams; result: SubscribeResult };
63
'createSession': { params: CreateSessionParams; result: null };
64
'disposeSession': { params: DisposeSessionParams; result: null };
65
'createTerminal': { params: CreateTerminalParams; result: null };
66
'disposeTerminal': { params: DisposeTerminalParams; result: null };
67
'listSessions': { params: ListSessionsParams; result: ListSessionsResult };
68
'resourceRead': { params: ResourceReadParams; result: ResourceReadResult };
69
'resourceWrite': { params: ResourceWriteParams; result: ResourceWriteResult };
70
'resourceList': { params: ResourceListParams; result: ResourceListResult };
71
'resourceCopy': { params: ResourceCopyParams; result: ResourceCopyResult };
72
'resourceDelete': { params: ResourceDeleteParams; result: ResourceDeleteResult };
73
'resourceMove': { params: ResourceMoveParams; result: ResourceMoveResult };
74
'fetchTurns': { params: FetchTurnsParams; result: FetchTurnsResult };
75
'authenticate': { params: AuthenticateParams; result: AuthenticateResult };
76
'resolveSessionConfig': { params: ResolveSessionConfigParams; result: ResolveSessionConfigResult };
77
'sessionConfigCompletions': { params: SessionConfigCompletionsParams; result: SessionConfigCompletionsResult };
78
}
79
80
// ─── Notification Maps ───────────────────────────────────────────────────────
81
82
/** Params for the server → client `notification` method. */
83
export interface NotificationMethodParams {
84
notification: ProtocolNotification;
85
}
86
87
/**
88
* Registry mapping each client → server notification method to its params type.
89
*
90
* @category Notifications
91
*/
92
export interface ClientNotificationMap {
93
'unsubscribe': { params: UnsubscribeParams };
94
'dispatchAction': { params: DispatchActionParams };
95
}
96
97
/**
98
* Registry mapping each server → client notification method to its params type.
99
*
100
* @category Notifications
101
*/
102
export interface ServerNotificationMap {
103
'action': { params: ActionEnvelope };
104
'notification': { params: NotificationMethodParams };
105
}
106
107
/** Combined notification map for all directions. */
108
export type NotificationMap = ClientNotificationMap & ServerNotificationMap;
109
110
// ─── Typed Requests ──────────────────────────────────────────────────────────
111
112
/**
113
* A fully typed JSON-RPC request for a specific AHP command.
114
*
115
* When used as a union (default generic), narrowing on `method` gives typed `params`:
116
*
117
* ```ts
118
* function handle(req: AhpRequest) {
119
* if (req.method === 'fetchTurns') {
120
* req.params.session; // typed as URI
121
* }
122
* }
123
* ```
124
*/
125
export type AhpRequest<M extends keyof CommandMap = keyof CommandMap> =
126
M extends unknown ? {
127
readonly jsonrpc: '2.0';
128
readonly id: number;
129
readonly method: M;
130
readonly params: CommandMap[M]['params'];
131
} : never;
132
133
// ─── Typed Responses ─────────────────────────────────────────────────────────
134
135
/**
136
* A fully typed JSON-RPC success response for a specific AHP command.
137
*
138
* Since JSON-RPC responses do not carry `method`, use this with an explicit
139
* generic parameter when you know the method from the associated request:
140
*
141
* ```ts
142
* const result: AhpSuccessResponse<'fetchTurns'> = ...;
143
* result.result.turns; // typed as Turn[]
144
* ```
145
*/
146
export type AhpSuccessResponse<M extends keyof CommandMap = keyof CommandMap> =
147
M extends unknown ? {
148
readonly jsonrpc: '2.0';
149
readonly id: number;
150
readonly result: CommandMap[M]['result'];
151
} : never;
152
153
/** Typed JSON-RPC response (success with known result type, or error). */
154
export type AhpResponse<M extends keyof CommandMap = keyof CommandMap> =
155
| AhpSuccessResponse<M>
156
| JsonRpcErrorResponse;
157
158
// ─── Typed Notifications ─────────────────────────────────────────────────────
159
160
/**
161
* A fully typed JSON-RPC notification for a specific AHP notification method.
162
*
163
* When used as a union (default generic), narrowing on `method` gives typed `params`:
164
*
165
* ```ts
166
* function handle(notif: AhpNotification) {
167
* if (notif.method === 'action') {
168
* notif.params.serverSeq; // typed as number
169
* }
170
* }
171
* ```
172
*/
173
export type AhpNotification<M extends keyof NotificationMap = keyof NotificationMap> =
174
M extends unknown ? {
175
readonly jsonrpc: '2.0';
176
readonly method: M;
177
readonly params: NotificationMap[M]['params'];
178
} : never;
179
180
/** A client → server notification. */
181
export type AhpClientNotification<M extends keyof ClientNotificationMap = keyof ClientNotificationMap> =
182
M extends unknown ? {
183
readonly jsonrpc: '2.0';
184
readonly method: M;
185
readonly params: ClientNotificationMap[M]['params'];
186
} : never;
187
188
/** A server → client notification. */
189
export type AhpServerNotification<M extends keyof ServerNotificationMap = keyof ServerNotificationMap> =
190
M extends unknown ? {
191
readonly jsonrpc: '2.0';
192
readonly method: M;
193
readonly params: ServerNotificationMap[M]['params'];
194
} : never;
195
196
// ─── Protocol Message Union ──────────────────────────────────────────────────
197
198
/**
199
* Discriminated union of all AHP protocol messages.
200
*
201
* Narrow using standard JSON-RPC structure:
202
* - Has `method` + `id` → request ({@link AhpRequest})
203
* - Has `method`, no `id` → notification ({@link AhpNotification})
204
* - Has `result` or `error` + `id` → response ({@link AhpResponse})
205
*
206
* Then narrow on `method` for fully typed params:
207
*
208
* ```ts
209
* function dispatch(msg: ProtocolMessage) {
210
* if ('method' in msg && 'id' in msg) {
211
* // msg is AhpRequest
212
* if (msg.method === 'fetchTurns') {
213
* msg.params.session; // URI
214
* }
215
* }
216
* }
217
* ```
218
*/
219
export type ProtocolMessage =
220
| AhpRequest
221
| AhpSuccessResponse
222
| JsonRpcErrorResponse
223
| AhpNotification;
224
225