1- import Ajv from 'ajv'
1+ import Ajv , { ErrorObject , ValidateFunction } from 'ajv'
22import {
33 Agent ,
44 BedrockTools ,
@@ -14,7 +14,7 @@ type Tool<T, R> = {
1414 name : string
1515 description : string
1616 inputSchema : ObjectSchema
17- validate : ( input : T , token ?: CancellationToken ) => boolean
17+ validate : ( input : T , token ?: CancellationToken ) => boolean | ValidateFunction < unknown > [ 'errors' ]
1818 invoke : ( input : T , token ?: CancellationToken , updates ?: WritableStream ) => Promise < R >
1919}
2020
@@ -30,7 +30,8 @@ export const newAgent = (): Agent => {
3030 const validator = ajv . compile ( spec . inputSchema )
3131 const tool = {
3232 validate : ( input : InferSchema < S [ 'inputSchema' ] > ) => {
33- return validator ( input )
33+ const isValid = validator ( input )
34+ return validator . errors ?? isValid
3435 } ,
3536 invoke : handler ,
3637 name : spec . name ,
@@ -47,8 +48,14 @@ export const newAgent = (): Agent => {
4748 throw new Error ( `Tool ${ toolName } not found` )
4849 }
4950
50- if ( ! tool . validate ( input , token ) ) {
51- throw new Error ( `Input for tool ${ toolName } is invalid` )
51+ const validateResult = tool . validate ( input , token )
52+ if ( validateResult !== true ) {
53+ const errorDetails =
54+ ( ( validateResult as ValidateFunction [ 'errors' ] ) || [ ] )
55+ . map ( ( err : ErrorObject ) => `${ err . instancePath || 'root' } : ${ err . message } ` )
56+ . join ( '\n' ) || `\nReceived: ${ input } `
57+
58+ throw new Error ( `${ toolName } tool input validation failed: ${ errorDetails } ` )
5259 }
5360
5461 return tool . invoke ( input , token , updates )
0 commit comments