-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodec.ts
More file actions
40 lines (37 loc) · 1.11 KB
/
Copy pathcodec.ts
File metadata and controls
40 lines (37 loc) · 1.11 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
import type {
ProtocolMethod,
ProtocolNotification,
ProtocolRequest,
ProtocolResponse,
} from './types.js';
const protocolMethods = new Set<ProtocolMethod>([
'initialize',
'config/diagnostics',
'thread/start',
'thread/read',
'thread/resume',
'turn/start',
'turn/interrupt',
'approval/respond',
'user-input/respond',
]);
export function encodeProtocolMessage(
message: ProtocolRequest | ProtocolResponse | ProtocolNotification,
): string {
return JSON.stringify(message);
}
export function decodeProtocolRequest(raw: string): ProtocolRequest {
const value = JSON.parse(raw) as Partial<ProtocolRequest>;
const validParams =
value.params === undefined ||
(typeof value.params === 'object' && value.params !== null && !Array.isArray(value.params));
if (
(typeof value.id !== 'string' && typeof value.id !== 'number') ||
typeof value.method !== 'string' ||
!protocolMethods.has(value.method as ProtocolMethod) ||
!validParams
) {
throw new Error('invalid protocol request');
}
return { id: value.id, method: value.method, params: value.params ?? {} } as ProtocolRequest;
}