@@ -4,6 +4,7 @@ import type {
4
4
BedrockAgentFunctionEvent ,
5
5
BedrockAgentFunctionResponse ,
6
6
Configuration ,
7
+ ParameterValue ,
7
8
ResolverOptions ,
8
9
ResponseOptions ,
9
10
Tool ,
@@ -13,7 +14,8 @@ import type { GenericLogger } from '../types/common.js';
13
14
import { isPrimitive } from './utils.js' ;
14
15
15
16
export class BedrockAgentFunctionResolver {
16
- readonly #tools: Map < string , Tool > = new Map < string , Tool > ( ) ;
17
+ readonly #tools: Map < string , Tool < Record < string , ParameterValue > > > =
18
+ new Map ( ) ;
17
19
readonly #envService: EnvironmentVariablesService ;
18
20
readonly #logger: Pick < GenericLogger , 'debug' | 'warn' | 'error' > ;
19
21
@@ -77,10 +79,15 @@ export class BedrockAgentFunctionResolver {
77
79
* @param fn - The tool function
78
80
* @param config - The configuration object for the tool
79
81
*/
80
- public tool ( fn : ToolFunction , config : Configuration ) : undefined ;
81
- public tool ( config : Configuration ) : MethodDecorator ;
82
- public tool (
83
- fnOrConfig : ToolFunction | Configuration ,
82
+ public tool < TParams extends Record < string , ParameterValue > > (
83
+ fn : ToolFunction < TParams > ,
84
+ config : Configuration
85
+ ) : undefined ;
86
+ public tool < TParams extends Record < string , ParameterValue > > (
87
+ config : Configuration
88
+ ) : MethodDecorator ;
89
+ public tool < TParams extends Record < string , ParameterValue > > (
90
+ fnOrConfig : ToolFunction < TParams > | Configuration ,
84
91
config ?: Configuration
85
92
) : MethodDecorator | undefined {
86
93
// When used as a method (not a decorator)
@@ -97,7 +104,10 @@ export class BedrockAgentFunctionResolver {
97
104
} ;
98
105
}
99
106
100
- #registerTool( fn : ToolFunction , config : Configuration ) : void {
107
+ #registerTool< TParams extends Record < string , ParameterValue > > (
108
+ handler : ToolFunction < TParams > ,
109
+ config : Configuration
110
+ ) : void {
101
111
const { name } = config ;
102
112
103
113
if ( this . #tools. size >= 5 ) {
@@ -113,7 +123,10 @@ export class BedrockAgentFunctionResolver {
113
123
) ;
114
124
}
115
125
116
- this . #tools. set ( name , { function : fn , config } ) ;
126
+ this . #tools. set ( name , {
127
+ handler : handler as ToolFunction < Record < string , ParameterValue > > ,
128
+ config,
129
+ } ) ;
117
130
this . #logger. debug ( `Tool ${ name } has been registered.` ) ;
118
131
}
119
132
@@ -169,12 +182,29 @@ export class BedrockAgentFunctionResolver {
169
182
} ) ;
170
183
}
171
184
172
- const parameterObject : Record < string , string > = Object . fromEntries (
173
- parameters . map ( ( param ) => [ param . name , param . value ] )
174
- ) ;
185
+ const toolParams : Record < string , ParameterValue > = { } ;
186
+ for ( const param of parameters ) {
187
+ switch ( param . type ) {
188
+ case 'boolean' : {
189
+ toolParams [ param . name ] = param . value === 'true' ;
190
+ break ;
191
+ }
192
+ case 'number' :
193
+ case 'integer' : {
194
+ toolParams [ param . name ] = Number ( param . value ) ;
195
+ break ;
196
+ }
197
+ // this default will also catch array types but we leave them as strings
198
+ // because we cannot reliably parse them
199
+ default : {
200
+ toolParams [ param . name ] = param . value ;
201
+ break ;
202
+ }
203
+ }
204
+ }
175
205
176
206
try {
177
- const res = ( await tool . function ( parameterObject ) ) ?? '' ;
207
+ const res = ( await tool . handler ( toolParams , event , context ) ) ?? '' ;
178
208
const body = isPrimitive ( res ) ? String ( res ) : JSON . stringify ( res ) ;
179
209
return this . #buildResponse( {
180
210
actionGroup,
0 commit comments