11export type JsonRpcResult = {
2- jsonrpc : string ;
3- id : number ;
4- result ?: {
5- content : Array < {
6- type : string ;
7- text : string ;
8- } > ;
9- } ;
10- error ?: {
11- code : number ;
12- message : string ;
13- }
14- }
2+ jsonrpc : string ;
3+ id : number ;
4+ result ?: {
5+ content : Array < {
6+ type : string ;
7+ text : string ;
8+ } > ;
9+ } ;
10+ error ?: {
11+ code : number ;
12+ message : string ;
13+ } ;
14+ } ;
1515
1616export const UNKNOWN_JSONRPC_RESULT : JsonRpcResult = {
17- jsonrpc : "2.0" ,
18- id : - 1 ,
19- error : {
20- code : - 1 ,
21- message : "Unknown JSONRPC result" ,
22- } ,
23- }
17+ jsonrpc : "2.0" ,
18+ id : - 1 ,
19+ error : {
20+ code : - 1 ,
21+ message : "Unknown JSONRPC result" ,
22+ } ,
23+ } ;
2424
2525const isValidJsonRpcResult = ( obj : any ) : obj is JsonRpcResult => {
26- // Check if obj is an object and not null
27- if ( typeof obj !== ' object' || obj === null ) {
28- return false ;
29- }
26+ // Check if obj is an object and not null
27+ if ( typeof obj !== " object" || obj === null ) {
28+ return false ;
29+ }
3030
31- // Check required properties
32- if ( typeof obj . jsonrpc !== ' string' || typeof obj . id !== ' number' ) {
33- return false ;
34- }
31+ // Check required properties
32+ if ( typeof obj . jsonrpc !== " string" || typeof obj . id !== " number" ) {
33+ return false ;
34+ }
3535
36- // Check that either result or error is present (but not both required)
37- const hasResult = obj . result !== undefined ;
38- const hasError = obj . error !== undefined ;
36+ // Check that either result or error is present (but not both required)
37+ const hasResult = obj . result !== undefined ;
38+ const hasError = obj . error !== undefined ;
3939
40- // Validate result structure if present
41- if ( hasResult ) {
42- if ( typeof obj . result !== 'object' || obj . result === null ) {
43- return false ;
44- }
45- if ( obj . result . content !== undefined ) {
46- if ( ! Array . isArray ( obj . result . content ) ) {
47- return false ;
48- }
49- // Validate each content item
50- for ( const item of obj . result . content ) {
51- if ( typeof item !== 'object' || item === null ||
52- typeof item . type !== 'string' || typeof item . text !== 'string' ) {
53- return false ;
54- }
55- }
40+ // Validate result structure if present
41+ if ( hasResult ) {
42+ if ( typeof obj . result !== "object" || obj . result === null ) {
43+ return false ;
44+ }
45+ if ( obj . result . content !== undefined ) {
46+ if ( ! Array . isArray ( obj . result . content ) ) {
47+ return false ;
48+ }
49+ // Validate each content item
50+ for ( const item of obj . result . content ) {
51+ if (
52+ typeof item !== "object" ||
53+ item === null ||
54+ typeof item . type !== "string" ||
55+ typeof item . text !== "string"
56+ ) {
57+ return false ;
5658 }
59+ }
5760 }
61+ }
5862
59- // Validate error structure if present
60- if ( hasError ) {
61- if ( typeof obj . error !== 'object' || obj . error === null ||
62- typeof obj . error . code !== 'number' || typeof obj . error . message !== 'string' ) {
63- return false ;
64- }
63+ // Validate error structure if present
64+ if ( hasError ) {
65+ if (
66+ typeof obj . error !== "object" ||
67+ obj . error === null ||
68+ typeof obj . error . code !== "number" ||
69+ typeof obj . error . message !== "string"
70+ ) {
71+ return false ;
6572 }
73+ }
6674
67- return true ;
75+ return true ;
6876} ;
6977
7078export const parseJsonRpcResult = ( message : string ) : JsonRpcResult | undefined => {
71- try {
72- const json = JSON . parse ( message ) ;
73-
74- // Validate the structure before casting
75- if ( isValidJsonRpcResult ( json ) ) {
76- return json ;
77- }
78-
79- return undefined ;
80- } catch ( error ) {
81- return undefined ;
79+ try {
80+ const json = JSON . parse ( message ) ;
81+
82+ // Validate the structure before casting
83+ if ( isValidJsonRpcResult ( json ) ) {
84+ return json ;
8285 }
83- }
86+
87+ return undefined ;
88+ } catch ( error ) {
89+ return undefined ;
90+ }
91+ } ;
0 commit comments