@@ -2,7 +2,13 @@ import context from '@aws-lambda-powertools/testing-utils/context';
2
2
import type { Context } from 'aws-lambda' ;
3
3
import { beforeEach , describe , expect , it , vi } from 'vitest' ;
4
4
import { BedrockAgentFunctionResolver } from '../../../src/bedrock-agent-function/index.js' ;
5
- import type { BedrockAgentFunctionEvent , Parameter } from '../../../src/types' ;
5
+ import type {
6
+ BedrockAgentFunctionEvent ,
7
+ Configuration ,
8
+ Parameter ,
9
+ ParameterValue ,
10
+ ToolFunction ,
11
+ } from '../../../src/types/bedrock-agent-function.js' ;
6
12
7
13
function createEvent ( functionName : string , parameters ?: Parameter [ ] ) {
8
14
return {
@@ -178,6 +184,32 @@ describe('Class: BedrockAgentFunctionResolver', () => {
178
184
) . toEqual ( '20' ) ;
179
185
} ) ;
180
186
187
+ it ( 'tool function has access to the event variable' , async ( ) => {
188
+ // Prepare
189
+ const app = new BedrockAgentFunctionResolver ( ) ;
190
+
191
+ app . tool (
192
+ async ( _params , event ) => {
193
+ return event ;
194
+ } ,
195
+ {
196
+ name : 'event-accessor' ,
197
+ description : 'Accesses the event object' ,
198
+ }
199
+ ) ;
200
+
201
+ const event = createEvent ( 'event-accessor' ) ;
202
+
203
+ // Act
204
+ const result = await app . resolve ( event , context ) ;
205
+
206
+ // Assess
207
+ expect ( result . response . function ) . toEqual ( 'event-accessor' ) ;
208
+ expect ( result . response . functionResponse . responseBody . TEXT . body ) . toEqual (
209
+ JSON . stringify ( event )
210
+ ) ;
211
+ } ) ;
212
+
181
213
it ( 'can be invoked using the decorator pattern' , async ( ) => {
182
214
// Prepare
183
215
const app = new BedrockAgentFunctionResolver ( ) ;
@@ -327,6 +359,92 @@ describe('Class: BedrockAgentFunctionResolver', () => {
327
359
}
328
360
) ;
329
361
362
+ it . each < {
363
+ toolFunction : ToolFunction < Record < string , ParameterValue > > ;
364
+ toolParams : Configuration ;
365
+ parameters : Parameter [ ] ;
366
+ expected : string ;
367
+ } > ( [
368
+ {
369
+ toolFunction : async ( params : Record < string , ParameterValue > ) =>
370
+ params . arg ,
371
+ toolParams : {
372
+ name : 'boolean-handler' ,
373
+ description : 'Handles boolean parameters' ,
374
+ } ,
375
+ parameters : [
376
+ { name : 'arg' , type : 'boolean' , value : 'true' } ,
377
+ ] as Parameter [ ] ,
378
+ expected : 'true' ,
379
+ } ,
380
+ {
381
+ toolFunction : async ( params : Record < string , ParameterValue > ) =>
382
+ ( params . arg as number ) + 10 ,
383
+ toolParams : {
384
+ name : 'number-handler' ,
385
+ description : 'Handles number parameters' ,
386
+ } ,
387
+ parameters : [ { name : 'arg' , type : 'number' , value : '42' } ] as Parameter [ ] ,
388
+ expected : '52' ,
389
+ } ,
390
+ {
391
+ toolFunction : async ( params : Record < string , ParameterValue > ) =>
392
+ ( params . arg as number ) + 10 ,
393
+ toolParams : {
394
+ name : 'integer-handler' ,
395
+ description : 'Handles integer parameters' ,
396
+ } ,
397
+ parameters : [
398
+ { name : 'arg' , type : 'integer' , value : '37' } ,
399
+ ] as Parameter [ ] ,
400
+ expected : '47' ,
401
+ } ,
402
+ {
403
+ toolFunction : async ( params : Record < string , ParameterValue > ) =>
404
+ `String: ${ params . arg } ` ,
405
+ toolParams : {
406
+ name : 'string-handler' ,
407
+ description : 'Handles string parameters' ,
408
+ } ,
409
+ parameters : [
410
+ { name : 'arg' , type : 'string' , value : 'hello world' } ,
411
+ ] as Parameter [ ] ,
412
+ expected : 'String: hello world' ,
413
+ } ,
414
+ {
415
+ toolFunction : async ( params : Record < string , ParameterValue > ) =>
416
+ `Array as string: ${ params . arg } ` ,
417
+ toolParams : {
418
+ name : 'array-handler' ,
419
+ description : 'Handles array parameters (as string)' ,
420
+ } ,
421
+ parameters : [
422
+ { name : 'arg' , type : 'array' , value : '[1,2,3]' } ,
423
+ ] as Parameter [ ] ,
424
+ expected : 'Array as string: [1,2,3]' ,
425
+ } ,
426
+ ] ) (
427
+ 'correctly parses $toolParams.name parameters' ,
428
+ async ( { toolFunction, toolParams, parameters, expected } ) => {
429
+ // Prepare
430
+ const app = new BedrockAgentFunctionResolver ( ) ;
431
+
432
+ app . tool ( toolFunction , toolParams ) ;
433
+
434
+ // Act
435
+ const actual = await app . resolve (
436
+ createEvent ( toolParams . name , parameters ) ,
437
+ context
438
+ ) ;
439
+
440
+ // Assert
441
+ expect ( actual . response . function ) . toEqual ( toolParams . name ) ;
442
+ expect ( actual . response . functionResponse . responseBody . TEXT . body ) . toEqual (
443
+ expected
444
+ ) ;
445
+ }
446
+ ) ;
447
+
330
448
it ( 'handles functions that throw errors' , async ( ) => {
331
449
// Prepare
332
450
const app = new BedrockAgentFunctionResolver ( ) ;
0 commit comments