@@ -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 {
@@ -93,8 +99,8 @@ describe('Class: BedrockAgentFunctionResolver', () => {
93
99
}
94
100
95
101
app . tool (
96
- async ( params ) => {
97
- return Number ( params . a ) + Number ( params ) ;
102
+ async ( params : { a : number ; b : number } ) => {
103
+ return params . a + params . b ;
98
104
} ,
99
105
{
100
106
name : 'mult' ,
@@ -146,8 +152,8 @@ describe('Class: BedrockAgentFunctionResolver', () => {
146
152
] ) ;
147
153
148
154
app . tool (
149
- async ( params ) => {
150
- return Number ( params . a ) + Number ( params . b ) ;
155
+ async ( params : { a : number ; b : number } ) => {
156
+ return params . a + params . b ;
151
157
} ,
152
158
{
153
159
name : 'math' ,
@@ -162,8 +168,8 @@ describe('Class: BedrockAgentFunctionResolver', () => {
162
168
) ;
163
169
164
170
app . tool (
165
- async ( params ) => {
166
- return Number ( params . a ) * Number ( params . b ) ;
171
+ async ( params : { a : number ; b : number } ) => {
172
+ return params . a * params . b ;
167
173
} ,
168
174
{
169
175
name : 'math' ,
@@ -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 ( ) ;
@@ -189,9 +221,9 @@ describe('Class: BedrockAgentFunctionResolver', () => {
189
221
}
190
222
191
223
@app . tool ( { name : 'add' , description : 'Adds two numbers' } )
192
- async add ( params : { a : string ; b : string } ) {
224
+ async add ( params : { a : number ; b : number } ) {
193
225
const { a, b } = params ;
194
- return Number . parseInt ( a ) + Number . parseInt ( b ) ;
226
+ return a + b ;
195
227
}
196
228
197
229
public async handler ( event : BedrockAgentFunctionEvent , context : Context ) {
@@ -327,6 +359,156 @@ describe('Class: BedrockAgentFunctionResolver', () => {
327
359
}
328
360
) ;
329
361
362
+ it ( 'correctly parses boolean parameters' , async ( ) => {
363
+ // Prepare
364
+ const toolFunction : ToolFunction < { arg : boolean } > = async ( params ) =>
365
+ params . arg ;
366
+
367
+ const toolParams : Configuration = {
368
+ name : 'boolean' ,
369
+ description : 'Handles boolean parameters' ,
370
+ } ;
371
+
372
+ const parameters : Parameter [ ] = [
373
+ { name : 'arg' , type : 'boolean' , value : 'true' } ,
374
+ ] ;
375
+
376
+ const app = new BedrockAgentFunctionResolver ( ) ;
377
+ app . tool ( toolFunction , toolParams ) ;
378
+
379
+ //Act
380
+ const actual = await app . resolve (
381
+ createEvent ( toolParams . name , parameters ) ,
382
+ context
383
+ ) ;
384
+
385
+ // Assess
386
+ expect ( actual . response . function ) . toEqual ( toolParams . name ) ;
387
+ expect ( actual . response . functionResponse . responseBody . TEXT . body ) . toEqual (
388
+ 'true'
389
+ ) ;
390
+ } ) ;
391
+
392
+ it ( 'correctly parses number parameters' , async ( ) => {
393
+ // Prepare
394
+ const toolFunction : ToolFunction < { arg : number } > = async ( params ) =>
395
+ params . arg + 10 ;
396
+
397
+ const toolParams : Configuration = {
398
+ name : 'number' ,
399
+ description : 'Handles number parameters' ,
400
+ } ;
401
+
402
+ const parameters : Parameter [ ] = [
403
+ { name : 'arg' , type : 'number' , value : '42' } ,
404
+ ] ;
405
+
406
+ const app = new BedrockAgentFunctionResolver ( ) ;
407
+ app . tool ( toolFunction , toolParams ) ;
408
+
409
+ // Act
410
+ const actual = await app . resolve (
411
+ createEvent ( toolParams . name , parameters ) ,
412
+ context
413
+ ) ;
414
+
415
+ // Assess
416
+ expect ( actual . response . function ) . toEqual ( toolParams . name ) ;
417
+ expect ( actual . response . functionResponse . responseBody . TEXT . body ) . toEqual (
418
+ '52'
419
+ ) ;
420
+ } ) ;
421
+
422
+ it ( 'correctly parses integer parameters' , async ( ) => {
423
+ // Prepare
424
+ const toolFunction : ToolFunction < { arg : number } > = async ( params ) =>
425
+ params . arg + 10 ;
426
+
427
+ const toolParams : Configuration = {
428
+ name : 'integer' ,
429
+ description : 'Handles integer parameters' ,
430
+ } ;
431
+
432
+ const parameters : Parameter [ ] = [
433
+ { name : 'arg' , type : 'integer' , value : '37' } ,
434
+ ] ;
435
+
436
+ const app = new BedrockAgentFunctionResolver ( ) ;
437
+ app . tool ( toolFunction , toolParams ) ;
438
+
439
+ // Act
440
+ const actual = await app . resolve (
441
+ createEvent ( toolParams . name , parameters ) ,
442
+ context
443
+ ) ;
444
+
445
+ // Assess
446
+ expect ( actual . response . function ) . toEqual ( toolParams . name ) ;
447
+ expect ( actual . response . functionResponse . responseBody . TEXT . body ) . toEqual (
448
+ '47'
449
+ ) ;
450
+ } ) ;
451
+
452
+ it ( 'correctly parses string parameters' , async ( ) => {
453
+ // Prepare
454
+ const toolFunction : ToolFunction < { arg : string } > = async ( params ) =>
455
+ `String: ${ params . arg } ` ;
456
+
457
+ const toolParams : Configuration = {
458
+ name : 'string' ,
459
+ description : 'Handles string parameters' ,
460
+ } ;
461
+
462
+ const parameters : Parameter [ ] = [
463
+ { name : 'arg' , type : 'string' , value : 'hello world' } ,
464
+ ] ;
465
+
466
+ const app = new BedrockAgentFunctionResolver ( ) ;
467
+ app . tool ( toolFunction , toolParams ) ;
468
+
469
+ // Act
470
+ const actual = await app . resolve (
471
+ createEvent ( toolParams . name , parameters ) ,
472
+ context
473
+ ) ;
474
+
475
+ // Assess
476
+ expect ( actual . response . function ) . toEqual ( toolParams . name ) ;
477
+ expect ( actual . response . functionResponse . responseBody . TEXT . body ) . toEqual (
478
+ 'String: hello world'
479
+ ) ;
480
+ } ) ;
481
+
482
+ it ( 'correctly parses array parameters' , async ( ) => {
483
+ // Prepare
484
+ const toolFunction : ToolFunction < { arg : string } > = async ( params ) =>
485
+ `Array as string: ${ params . arg } ` ;
486
+
487
+ const toolParams : Configuration = {
488
+ name : 'array' ,
489
+ description : 'Handles array parameters (as string)' ,
490
+ } ;
491
+
492
+ const parameters : Parameter [ ] = [
493
+ { name : 'arg' , type : 'array' , value : '[1,2,3]' } ,
494
+ ] ;
495
+
496
+ const app = new BedrockAgentFunctionResolver ( ) ;
497
+ app . tool ( toolFunction , toolParams ) ;
498
+
499
+ // Act
500
+ const actual = await app . resolve (
501
+ createEvent ( toolParams . name , parameters ) ,
502
+ context
503
+ ) ;
504
+
505
+ // Assess
506
+ expect ( actual . response . function ) . toEqual ( toolParams . name ) ;
507
+ expect ( actual . response . functionResponse . responseBody . TEXT . body ) . toEqual (
508
+ 'Array as string: [1,2,3]'
509
+ ) ;
510
+ } ) ;
511
+
330
512
it ( 'handles functions that throw errors' , async ( ) => {
331
513
// Prepare
332
514
const app = new BedrockAgentFunctionResolver ( ) ;
0 commit comments