@@ -28,20 +28,60 @@ export const executeToolCall = async (
2828 logger,
2929 } ;
3030
31+ let parsedJson : any ;
32+ try {
33+ parsedJson = JSON . parse ( toolCall . content ) ;
34+ } catch ( err ) {
35+ if ( err instanceof Error ) {
36+ logger . error ( err . message ) ;
37+ return JSON . stringify ( {
38+ error : true ,
39+ message : 'Invalid JSON for tool call: ' + err . message ,
40+ stack : err . stack ,
41+ } ) ;
42+ } else {
43+ logger . error ( err ) ;
44+ return JSON . stringify ( {
45+ error : true ,
46+ message : 'Invalid JSON for tool call: ' + err ,
47+ } ) ;
48+ }
49+ }
50+
51+ // validate JSON schema for input
52+ let validatedJson : any ;
53+ try {
54+ validatedJson = tool . parameters . parse ( parsedJson ) ;
55+ } catch ( err ) {
56+ if ( err instanceof Error ) {
57+ logger . error ( err . message ) ;
58+ return JSON . stringify ( {
59+ error : true ,
60+ message : 'Invalid format for tool call: ' + err . message ,
61+ stack : err . stack ,
62+ } ) ;
63+ } else {
64+ logger . error ( err ) ;
65+ return JSON . stringify ( {
66+ error : true ,
67+ message : 'Invalid format for tool call: ' + err ,
68+ } ) ;
69+ }
70+ }
71+
3172 // for each parameter log it and its name
3273 if ( tool . logParameters ) {
33- tool . logParameters ( toolCall . input , toolContext ) ;
74+ tool . logParameters ( validatedJson , toolContext ) ;
3475 } else {
3576 logger . info ( 'Parameters:' ) ;
36- Object . entries ( toolCall . input ) . forEach ( ( [ name , value ] ) => {
77+ Object . entries ( validatedJson ) . forEach ( ( [ name , value ] ) => {
3778 logger . info ( ` - ${ name } : ${ JSON . stringify ( value ) . substring ( 0 , 60 ) } ` ) ;
3879 } ) ;
3980 }
4081
41- // TODO: validate JSON schema for input
42- let output ;
82+ let output : any ;
4383 try {
44- output = await tool . execute ( toolCall . input , toolContext ) ;
84+ output = await tool . execute ( validatedJson , toolContext ) ;
4585 } catch ( err ) {
4686 if ( err instanceof Error ) {
4787 logger . error ( err . message ) ;
0 commit comments