-
Notifications
You must be signed in to change notification settings - Fork 39.3k
Expand file tree
/
Copy pathprotocolServerHandler.ts
More file actions
590 lines (536 loc) · 21.3 KB
/
protocolServerHandler.ts
File metadata and controls
590 lines (536 loc) · 21.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Emitter } from '../../../base/common/event.js';
import { isJsonRpcResponse } from '../../../base/common/jsonRpcProtocol.js';
import { Disposable, DisposableStore } from '../../../base/common/lifecycle.js';
import { hasKey } from '../../../base/common/types.js';
import { URI } from '../../../base/common/uri.js';
import { ILogService } from '../../log/common/log.js';
import { AHPFileSystemProvider } from '../common/agentHostFileSystemProvider.js';
import { AgentSession, type IAgentService } from '../common/agentService.js';
import type { ICommandMap } from '../common/state/protocol/messages.js';
import { IActionEnvelope, INotification, isSessionAction, isTerminalAction, type ISessionAction } from '../common/state/sessionActions.js';
import { MIN_PROTOCOL_VERSION, PROTOCOL_VERSION } from '../common/state/sessionCapabilities.js';
import {
AHP_AUTH_REQUIRED,
AHP_PROVIDER_NOT_FOUND,
AHP_SESSION_NOT_FOUND,
AHP_UNSUPPORTED_PROTOCOL_VERSION,
IJsonRpcRequest,
isJsonRpcNotification,
isJsonRpcRequest,
JSON_RPC_INTERNAL_ERROR,
ProtocolError,
type IAhpServerNotification,
type IInitializeParams,
type IJsonRpcResponse,
type IReconnectParams,
type IStateSnapshot,
} from '../common/state/sessionProtocol.js';
import { ROOT_STATE_URI, SessionStatus } from '../common/state/sessionState.js';
import type { IProtocolServer, IProtocolTransport } from '../common/state/sessionTransport.js';
import { AgentHostStateManager } from './agentHostStateManager.js';
/** Default capacity of the server-side action replay buffer. */
const REPLAY_BUFFER_CAPACITY = 1000;
/** Build a JSON-RPC success response suitable for transport.send(). */
function jsonRpcSuccess(id: number, result: unknown): IJsonRpcResponse {
return { jsonrpc: '2.0', id, result };
}
/** Build a JSON-RPC error response suitable for transport.send(). */
function jsonRpcError(id: number, code: number, message: string, data?: unknown): IJsonRpcResponse {
return { jsonrpc: '2.0', id, error: { code, message, ...(data !== undefined ? { data } : {}) } };
}
/** Build a JSON-RPC error response from an unknown thrown value, preserving {@link ProtocolError} fields. */
function jsonRpcErrorFrom(id: number, err: unknown): IJsonRpcResponse {
if (err instanceof ProtocolError) {
return jsonRpcError(id, err.code, err.message, err.data);
}
const message = err instanceof Error ? (err.stack ?? err.message) : String(err);
return jsonRpcError(id, JSON_RPC_INTERNAL_ERROR, message);
}
/**
* Methods handled by the request dispatcher. Excludes `initialize` and
* `reconnect` which are handled during the handshake phase.
*/
type RequestMethod = Exclude<keyof ICommandMap, 'initialize' | 'reconnect'>;
/**
* Typed handler map: each key is a request method, each value is a handler
* that receives the correctly-typed params and must return the correctly-typed
* result. The compiler will error if a handler returns the wrong shape.
*/
type RequestHandlerMap = {
[M in RequestMethod]: (client: IConnectedClient, params: ICommandMap[M]['params']) => Promise<ICommandMap[M]['result']>;
};
/**
* Represents a connected protocol client with its subscription state.
*/
interface IConnectedClient {
readonly clientId: string;
readonly protocolVersion: number;
readonly transport: IProtocolTransport;
readonly subscriptions: Set<string>;
readonly disposables: DisposableStore;
}
/**
* Configuration for protocol-level concerns outside of IAgentService.
*/
export interface IProtocolServerConfig {
/** Default directory returned to clients during the initialize handshake. */
readonly defaultDirectory?: string;
}
/**
* Server-side handler that manages protocol connections, routes JSON-RPC
* messages to the agent service, and broadcasts actions/notifications
* to subscribed clients.
*/
export class ProtocolServerHandler extends Disposable {
private readonly _clients = new Map<string, IConnectedClient>();
private readonly _replayBuffer: IActionEnvelope[] = [];
private readonly _onDidChangeConnectionCount = this._register(new Emitter<number>());
/** Fires with the current client count whenever a client connects or disconnects. */
readonly onDidChangeConnectionCount = this._onDidChangeConnectionCount.event;
constructor(
private readonly _agentService: IAgentService,
private readonly _stateManager: AgentHostStateManager,
private readonly _server: IProtocolServer,
private readonly _config: IProtocolServerConfig,
private readonly _clientFileSystemProvider: AHPFileSystemProvider,
@ILogService private readonly _logService: ILogService,
) {
super();
this._register(this._server.onConnection(transport => {
this._handleNewConnection(transport);
}));
this._register(this._stateManager.onDidEmitEnvelope(envelope => {
this._replayBuffer.push(envelope);
if (this._replayBuffer.length > REPLAY_BUFFER_CAPACITY) {
this._replayBuffer.shift();
}
this._broadcastAction(envelope);
}));
this._register(this._stateManager.onDidEmitNotification(notification => {
this._broadcastNotification(notification);
}));
}
// ---- Connection handling -------------------------------------------------
private _handleNewConnection(transport: IProtocolTransport): void {
const disposables = new DisposableStore();
let client: IConnectedClient | undefined;
disposables.add(transport.onMessage(msg => {
if (isJsonRpcRequest(msg)) {
this._logService.trace(`[ProtocolServer] request: method=${msg.method} id=${msg.id}`);
// Handle initialize/reconnect as requests that set up the client
if (!client && msg.method === 'initialize') {
try {
const result = this._handleInitialize(msg.params, transport, disposables);
client = result.client;
transport.send(jsonRpcSuccess(msg.id, result.response));
} catch (err) {
transport.send(jsonRpcErrorFrom(msg.id, err));
}
return;
}
if (!client && msg.method === 'reconnect') {
try {
const result = this._handleReconnect(msg.params, transport, disposables);
client = result.client;
transport.send(jsonRpcSuccess(msg.id, result.response));
} catch (err) {
transport.send(jsonRpcErrorFrom(msg.id, err));
}
return;
}
if (!client) {
return;
}
this._handleRequest(client, msg.method, msg.params, msg.id);
} else if (isJsonRpcNotification(msg)) {
this._logService.trace(`[ProtocolServer] notification: method=${msg.method}`);
// Notification — fire-and-forget
switch (msg.method) {
case 'unsubscribe':
if (client) {
client.subscriptions.delete(msg.params.resource);
}
break;
case 'dispatchAction':
if (client) {
this._logService.trace(`[ProtocolServer] dispatchAction: ${JSON.stringify(msg.params.action.type)}`);
const action = msg.params.action as ISessionAction;
this._agentService.dispatchAction(action, client.clientId, msg.params.clientSeq);
}
break;
}
} else if (isJsonRpcResponse(msg)) {
const pending = this._pendingReverseRequests.get(msg.id);
if (pending) {
this._pendingReverseRequests.delete(msg.id);
if (hasKey(msg, { error: true })) {
pending.reject(new Error(msg.error?.message ?? 'Reverse RPC error'));
} else {
pending.resolve(msg.result);
}
}
}
}));
disposables.add(transport.onClose(() => {
if (client && this._clients.get(client.clientId) === client) {
this._logService.info(`[ProtocolServer] Client disconnected: ${client.clientId}`);
this._clients.delete(client.clientId);
this._rejectPendingReverseRequests(client.clientId);
this._onDidChangeConnectionCount.fire(this._clients.size);
}
disposables.dispose();
}));
disposables.add(transport);
}
// ---- Handshake handlers ----------------------------------------------------
private _handleInitialize(
params: IInitializeParams,
transport: IProtocolTransport,
disposables: DisposableStore,
): { client: IConnectedClient; response: unknown } {
this._logService.info(`[ProtocolServer] Initialize: clientId=${params.clientId}, version=${params.protocolVersion}`);
if (params.protocolVersion < MIN_PROTOCOL_VERSION) {
throw new ProtocolError(
AHP_UNSUPPORTED_PROTOCOL_VERSION,
`Client protocol version ${params.protocolVersion} is below minimum ${MIN_PROTOCOL_VERSION}`,
);
}
const client: IConnectedClient = {
clientId: params.clientId,
protocolVersion: params.protocolVersion,
transport,
subscriptions: new Set(),
disposables,
};
this._clients.set(params.clientId, client);
this._onDidChangeConnectionCount.fire(this._clients.size);
disposables.add(this._clientFileSystemProvider.registerAuthority(params.clientId, {
resourceList: (uri) => this._sendReverseRequest(params.clientId, 'resourceList', { uri: uri.toString() }),
resourceRead: (uri) => this._sendReverseRequest(params.clientId, 'resourceRead', { uri: uri.toString() }),
resourceWrite: (params_) => this._sendReverseRequest(params.clientId, 'resourceWrite', params_),
resourceDelete: (params_) => this._sendReverseRequest(params.clientId, 'resourceDelete', params_),
resourceMove: (params_) => this._sendReverseRequest(params.clientId, 'resourceMove', params_),
}));
const snapshots: IStateSnapshot[] = [];
if (params.initialSubscriptions) {
for (const uri of params.initialSubscriptions) {
const snapshot = this._stateManager.getSnapshot(uri);
if (snapshot) {
snapshots.push(snapshot);
client.subscriptions.add(uri.toString());
}
}
}
return {
client,
response: {
protocolVersion: PROTOCOL_VERSION,
serverSeq: this._stateManager.serverSeq,
snapshots,
defaultDirectory: this._config.defaultDirectory,
},
};
}
private _handleReconnect(
params: IReconnectParams,
transport: IProtocolTransport,
disposables: DisposableStore,
): { client: IConnectedClient; response: unknown } {
this._logService.info(`[ProtocolServer] Reconnect: clientId=${params.clientId}, lastSeenSeq=${params.lastSeenServerSeq}`);
const client: IConnectedClient = {
clientId: params.clientId,
protocolVersion: PROTOCOL_VERSION,
transport,
subscriptions: new Set(),
disposables,
};
this._clients.set(params.clientId, client);
this._onDidChangeConnectionCount.fire(this._clients.size);
const oldestBuffered = this._replayBuffer.length > 0 ? this._replayBuffer[0].serverSeq : this._stateManager.serverSeq;
const canReplay = params.lastSeenServerSeq >= oldestBuffered;
if (canReplay) {
const actions: IActionEnvelope[] = [];
for (const sub of params.subscriptions) {
client.subscriptions.add(sub.toString());
}
for (const envelope of this._replayBuffer) {
if (envelope.serverSeq > params.lastSeenServerSeq) {
if (this._isRelevantToClient(client, envelope)) {
actions.push(envelope);
}
}
}
return { client, response: { type: 'replay', actions } };
} else {
const snapshots: IStateSnapshot[] = [];
for (const sub of params.subscriptions) {
const snapshot = this._stateManager.getSnapshot(sub);
if (snapshot) {
snapshots.push(snapshot);
client.subscriptions.add(sub);
}
}
return { client, response: { type: 'snapshot', snapshots } };
}
}
// ---- Requests (expect a response) ---------------------------------------
/**
* Methods handled by the request dispatcher (excludes initialize/reconnect
* which are handled during the handshake phase).
*/
private readonly _requestHandlers: RequestHandlerMap = {
subscribe: async (client, params) => {
try {
const snapshot = await this._agentService.subscribe(URI.parse(params.resource));
client.subscriptions.add(params.resource);
return { snapshot };
} catch (err) {
if (err instanceof ProtocolError) {
throw err;
}
throw new ProtocolError(AHP_SESSION_NOT_FOUND, `Resource not found: ${params.resource}`);
}
},
createSession: async (_client, params) => {
let createdSession: URI;
// Resolve fork turnId to a 0-based index using the source session's
// turn list in the state manager.
let fork: { session: URI; turnIndex: number } | undefined;
if (params.fork) {
const sourceState = this._stateManager.getSessionState(params.fork.session);
if (!sourceState) {
throw new ProtocolError(AHP_SESSION_NOT_FOUND, `Fork source session not found: ${params.fork.session}`);
}
const turnIndex = sourceState.turns.findIndex(t => t.id === params.fork!.turnId);
if (turnIndex < 0) {
throw new ProtocolError(AHP_SESSION_NOT_FOUND, `Fork turn ID ${params.fork.turnId} not found in session ${params.fork.session}`);
}
fork = { session: URI.parse(params.fork.session), turnIndex };
}
try {
createdSession = await this._agentService.createSession({
provider: params.provider,
model: params.model,
workingDirectory: params.workingDirectory ? URI.parse(params.workingDirectory) : undefined,
session: URI.parse(params.session),
fork,
config: params.config,
});
} catch (err) {
if (err instanceof ProtocolError) {
throw err;
}
throw new ProtocolError(AHP_PROVIDER_NOT_FOUND, err instanceof Error ? err.message : String(err));
}
// Verify the provider honored the client-chosen session URI per the protocol contract
if (createdSession.toString() !== URI.parse(params.session).toString()) {
this._logService.warn(`[ProtocolServer] createSession: provider returned URI ${createdSession.toString()} but client requested ${params.session}`);
}
return null;
},
disposeSession: async (_client, params) => {
await this._agentService.disposeSession(URI.parse(params.session));
return null;
},
resourceWrite: async (_client, params) => {
return this._agentService.resourceWrite(params);
},
listSessions: async () => {
const sessions = await this._agentService.listSessions();
const items = sessions.map(s => ({
resource: s.session.toString(),
provider: AgentSession.provider(s.session) ?? 'copilot',
title: s.summary ?? 'Session',
status: s.status ?? SessionStatus.Idle,
createdAt: s.startTime,
modifiedAt: s.modifiedTime,
...(s.project ? { project: { uri: s.project.uri.toString(), displayName: s.project.displayName } } : {}),
model: s.model,
workingDirectory: s.workingDirectory?.toString(),
isRead: s.isRead,
isDone: s.isDone,
}));
return { items };
},
resolveSessionConfig: async (_client, params) => {
return this._agentService.resolveSessionConfig({
provider: params.provider,
workingDirectory: params.workingDirectory ? URI.parse(params.workingDirectory) : undefined,
config: params.config,
});
},
sessionConfigCompletions: async (_client, params) => {
return this._agentService.sessionConfigCompletions({
provider: params.provider,
workingDirectory: params.workingDirectory ? URI.parse(params.workingDirectory) : undefined,
config: params.config,
property: params.property,
query: params.query,
});
},
fetchTurns: async (_client, params) => {
const state = this._stateManager.getSessionState(params.session);
if (!state) {
throw new ProtocolError(AHP_SESSION_NOT_FOUND, `Session not found: ${params.session}`);
}
const turns = state.turns;
const limit = Math.min(params.limit ?? 50, 100);
let endIndex = turns.length;
if (params.before) {
const idx = turns.findIndex(t => t.id === params.before);
if (idx !== -1) {
endIndex = idx;
}
}
const startIndex = Math.max(0, endIndex - limit);
return {
turns: turns.slice(startIndex, endIndex),
hasMore: startIndex > 0,
};
},
resourceList: async (_client, params) => {
return this._agentService.resourceList(URI.parse(params.uri));
},
resourceRead: async (_client, params) => {
return this._agentService.resourceRead(URI.parse(params.uri));
},
resourceCopy: async (_client, params) => {
return this._agentService.resourceCopy(params);
},
resourceDelete: async (_client, params) => {
return this._agentService.resourceDelete(params);
},
resourceMove: async (_client, params) => {
return this._agentService.resourceMove(params);
},
authenticate: async (_client, params) => {
const result = await this._agentService.authenticate(params);
if (!result.authenticated) {
throw new ProtocolError(AHP_AUTH_REQUIRED, 'Authentication failed for resource: ' + params.resource);
}
return {};
},
createTerminal: async (_client, params) => {
await this._agentService.createTerminal(params);
return null;
},
disposeTerminal: async (_client, params) => {
await this._agentService.disposeTerminal(URI.parse(params.terminal));
return null;
},
};
// ---- Reverse RPC (server → client requests) ----------------------------
private _reverseRequestId = 0;
private readonly _pendingReverseRequests = new Map<number, { clientId: string; resolve: (value: unknown) => void; reject: (reason: unknown) => void }>();
/**
* Sends a JSON-RPC request to a connected client and waits for the response.
* Used for reverse-RPC operations like reading client-side files.
* Rejects if the client disconnects or the server is disposed.
*/
private _sendReverseRequest<T>(clientId: string, method: string, params: unknown): Promise<T> {
const client = this._clients.get(clientId);
if (!client) {
return Promise.reject(new Error(`Client ${clientId} is not connected`));
}
const id = ++this._reverseRequestId;
return new Promise<T>((resolve, reject) => {
this._pendingReverseRequests.set(id, { clientId, resolve: resolve as (value: unknown) => void, reject });
const request: IJsonRpcRequest = { jsonrpc: '2.0', id, method, params };
client.transport.send(request);
});
}
/**
* Rejects and clears all pending reverse-RPC requests for a given client.
*/
private _rejectPendingReverseRequests(clientId: string): void {
for (const [id, pending] of this._pendingReverseRequests) {
if (pending.clientId === clientId) {
this._pendingReverseRequests.delete(id);
pending.reject(new Error(`Client ${clientId} disconnected`));
}
}
}
private _handleRequest(client: IConnectedClient, method: string, params: unknown, id: number): void {
const handler = this._requestHandlers.hasOwnProperty(method) ? this._requestHandlers[method as RequestMethod] : undefined;
if (handler) {
(handler as (client: IConnectedClient, params: unknown) => Promise<unknown>)(client, params).then(result => {
this._logService.trace(`[ProtocolServer] Request '${method}' id=${id} succeeded`);
client.transport.send(jsonRpcSuccess(id, result ?? null));
}).catch(err => {
this._logService.error(`[ProtocolServer] Request '${method}' failed`, err);
client.transport.send(jsonRpcErrorFrom(id, err));
});
return;
}
// VS Code extension methods (not in the typed protocol maps yet)
const extensionResult = this._handleExtensionRequest(method, params);
if (extensionResult) {
extensionResult.then(result => {
client.transport.send(jsonRpcSuccess(id, result ?? null));
}).catch(err => {
this._logService.error(`[ProtocolServer] Extension request '${method}' failed`, err);
client.transport.send(jsonRpcErrorFrom(id, err));
});
return;
}
client.transport.send(jsonRpcError(id, JSON_RPC_INTERNAL_ERROR, `Unknown method: ${method}`));
}
/**
* Handle VS Code extension methods that are not yet part of the typed
* protocol. Returns a Promise if the method was recognized, undefined
* otherwise.
*/
private _handleExtensionRequest(method: string, _params: unknown): Promise<unknown> | undefined {
switch (method) {
case 'shutdown':
return this._agentService.shutdown();
default:
return undefined;
}
}
// ---- Broadcasting -------------------------------------------------------
private _broadcastAction(envelope: IActionEnvelope): void {
this._logService.trace(`[ProtocolServer] Broadcasting action: ${envelope.action.type}`);
const msg: IAhpServerNotification<'action'> = { jsonrpc: '2.0', method: 'action', params: envelope };
for (const client of this._clients.values()) {
if (this._isRelevantToClient(client, envelope)) {
client.transport.send(msg);
}
}
}
private _broadcastNotification(notification: INotification): void {
const msg: IAhpServerNotification<'notification'> = { jsonrpc: '2.0', method: 'notification', params: { notification } };
for (const client of this._clients.values()) {
client.transport.send(msg);
}
}
private _isRelevantToClient(client: IConnectedClient, envelope: IActionEnvelope): boolean {
const action = envelope.action;
if (action.type.startsWith('root/')) {
return client.subscriptions.has(ROOT_STATE_URI);
}
if (isSessionAction(action)) {
return client.subscriptions.has(action.session);
}
if (isTerminalAction(action)) {
return client.subscriptions.has(action.terminal);
}
return false;
}
override dispose(): void {
for (const client of this._clients.values()) {
client.disposables.dispose();
}
this._clients.clear();
for (const [, pending] of this._pendingReverseRequests) {
pending.reject(new Error('ProtocolServerHandler disposed'));
}
this._pendingReverseRequests.clear();
this._replayBuffer.length = 0;
super.dispose();
}
}