Skip to content

Commit c916396

Browse files
committed
put event and context parameters into an options object
1 parent 610e72a commit c916396

File tree

3 files changed

+32
-26
lines changed

3 files changed

+32
-26
lines changed

packages/event-handler/src/bedrock-agent/BedrockAgentFunctionResolver.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,9 @@ export class BedrockAgentFunctionResolver {
9595
}
9696

9797
// When used as a decorator
98-
return (target, propertyKey, descriptor: PropertyDescriptor) => {
99-
const originalMethod = descriptor.value;
100-
101-
// Store a wrapper function that will call the original method with the correct 'this'
102-
this.#registerTool(async (params, event, context) => {
103-
// This wrapper will receive the instance through closure when called
104-
return originalMethod.apply(target, [params, event, context]);
105-
}, fnOrConfig);
106-
98+
return (_target, _propertyKey, descriptor: PropertyDescriptor) => {
99+
const toolFn = descriptor.value as ToolFunction;
100+
this.#registerTool(toolFn, fnOrConfig);
107101
return descriptor;
108102
};
109103
}
@@ -210,7 +204,7 @@ export class BedrockAgentFunctionResolver {
210204
}
211205

212206
try {
213-
const res = await tool.handler(toolParams, event, context);
207+
const res = await tool.handler(toolParams, { event, context });
214208
const body = res == null ? '' : JSON.stringify(res);
215209
return this.#buildResponse({
216210
actionGroup,

packages/event-handler/src/types/bedrock-agent.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ type ParameterValue = ParameterPrimitives | Array<ParameterValue>;
1919

2020
type ToolFunction<TParams = Record<string, ParameterValue>> = (
2121
params: TParams,
22-
event: BedrockAgentFunctionEvent,
23-
context: Context
22+
options?: {
23+
event?: BedrockAgentFunctionEvent;
24+
context?: Context;
25+
}
2426
) => Promise<JSONValue>;
2527

2628
type Tool<TParams = Record<string, ParameterValue>> = {

packages/event-handler/tests/unit/bedrock-agent/BedrockAgentFunctionResolver.test.ts

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,8 @@ describe('Class: BedrockAgentFunctionResolver', () => {
273273
const app = new BedrockAgentFunctionResolver();
274274

275275
app.tool(
276-
async (_params, event) => {
277-
return event;
276+
async (_params, options) => {
277+
return options?.event;
278278
},
279279
{
280280
name: 'event-accessor',
@@ -421,8 +421,10 @@ describe('Class: BedrockAgentFunctionResolver', () => {
421421

422422
it('correctly parses boolean parameters', async () => {
423423
// Prepare
424-
const toolFunction: ToolFunction<{ arg: boolean }> = async (params) =>
425-
params.arg;
424+
const toolFunction: ToolFunction<{ arg: boolean }> = async (
425+
params,
426+
_options
427+
) => params.arg;
426428

427429
const toolParams: Configuration = {
428430
name: 'boolean',
@@ -451,8 +453,10 @@ describe('Class: BedrockAgentFunctionResolver', () => {
451453

452454
it('correctly parses number parameters', async () => {
453455
// Prepare
454-
const toolFunction: ToolFunction<{ arg: number }> = async (params) =>
455-
params.arg + 10;
456+
const toolFunction: ToolFunction<{ arg: number }> = async (
457+
params,
458+
_options
459+
) => params.arg + 10;
456460

457461
const toolParams: Configuration = {
458462
name: 'number',
@@ -481,8 +485,10 @@ describe('Class: BedrockAgentFunctionResolver', () => {
481485

482486
it('correctly parses integer parameters', async () => {
483487
// Prepare
484-
const toolFunction: ToolFunction<{ arg: number }> = async (params) =>
485-
params.arg + 10;
488+
const toolFunction: ToolFunction<{ arg: number }> = async (
489+
params,
490+
_options
491+
) => params.arg + 10;
486492

487493
const toolParams: Configuration = {
488494
name: 'integer',
@@ -511,8 +517,10 @@ describe('Class: BedrockAgentFunctionResolver', () => {
511517

512518
it('correctly parses string parameters', async () => {
513519
// Prepare
514-
const toolFunction: ToolFunction<{ arg: string }> = async (params) =>
515-
`String: ${params.arg}`;
520+
const toolFunction: ToolFunction<{ arg: string }> = async (
521+
params,
522+
_options
523+
) => `String: ${params.arg}`;
516524

517525
const toolParams: Configuration = {
518526
name: 'string',
@@ -541,8 +549,10 @@ describe('Class: BedrockAgentFunctionResolver', () => {
541549

542550
it('correctly parses array parameters', async () => {
543551
// Prepare
544-
const toolFunction: ToolFunction<{ arg: string }> = async (params) =>
545-
`Array as string: ${params.arg}`;
552+
const toolFunction: ToolFunction<{ arg: string }> = async (
553+
params,
554+
_options
555+
) => `Array as string: ${params.arg}`;
546556

547557
const toolParams: Configuration = {
548558
name: 'array',
@@ -574,7 +584,7 @@ describe('Class: BedrockAgentFunctionResolver', () => {
574584
const app = new BedrockAgentFunctionResolver();
575585

576586
app.tool(
577-
async () => {
587+
async (_params, _options) => {
578588
throw new Error('Something went wrong');
579589
},
580590
{
@@ -602,7 +612,7 @@ describe('Class: BedrockAgentFunctionResolver', () => {
602612
const app = new BedrockAgentFunctionResolver();
603613

604614
app.tool(
605-
async (params) => {
615+
async (params, _options) => {
606616
return `Hello, ${params.name}!`;
607617
},
608618
{

0 commit comments

Comments
 (0)