Skip to content

Commit 23332b4

Browse files
committed
don't allow too function to be void
1 parent afb1213 commit 23332b4

File tree

4 files changed

+62
-42
lines changed

4 files changed

+62
-42
lines changed

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,15 @@ export class BedrockAgentFunctionResolver {
9797
}
9898

9999
// When used as a decorator
100-
return (_target, _propertyKey, descriptor: PropertyDescriptor) => {
101-
const toolFn = descriptor.value as ToolFunction;
102-
this.#registerTool(toolFn, fnOrConfig);
100+
return (target, propertyKey, descriptor: PropertyDescriptor) => {
101+
const originalMethod = descriptor.value;
102+
103+
// Store a wrapper function that will call the original method with the correct 'this'
104+
this.#registerTool(async (params, event, context) => {
105+
// This wrapper will receive the instance through closure when called
106+
return originalMethod.apply(target, [params, event, context]);
107+
}, fnOrConfig);
108+
103109
return descriptor;
104110
};
105111
}
@@ -207,8 +213,9 @@ export class BedrockAgentFunctionResolver {
207213

208214
try {
209215
// TODO: use apply to ensure that `this` is bound properly when used as decorator
210-
const res = await tool.handler(toolParams, event, context);
211-
const body = res == null ? '' : JSON.stringify(res); //TODO just use JSON.stringify
216+
const res = await tool.handler.apply(this, [toolParams, event, context]);
217+
// const res = await tool.handler(toolParams, event, context);
218+
const body = res == null ? '' : JSON.stringify(res);
212219
return this.#buildResponse({
213220
actionGroup,
214221
function: toolName,

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,13 @@ type ToolFunction<TParams = Record<string, ParameterValue>> = (
2121
params: TParams,
2222
event: BedrockAgentFunctionEvent,
2323
context: Context
24-
// biome-ignore lint/suspicious/noConfusingVoidType: we need to support async functions that don't have an explicit return value
25-
) => Promise<JSONValue | void>;
24+
) => Promise<JSONValue>;
2625

2726
type Tool<TParams = Record<string, ParameterValue>> = {
2827
handler: ToolFunction<TParams>;
2928
config: Configuration;
3029
};
3130

32-
type Attributes = Record<string, string>;
33-
3431
type FunctionIdentifier = {
3532
actionGroup: string;
3633
function: string;
@@ -50,8 +47,8 @@ type BedrockAgentFunctionEvent = FunctionInvocation & {
5047
};
5148
inputText: string;
5249
sessionId: string;
53-
sessionAttributes: Attributes;
54-
promptSessionAttributes: Attributes;
50+
sessionAttributes: Record<string, string>;
51+
promptSessionAttributes: Record<string, string>;
5552
};
5653

5754
type ResponseState = 'ERROR' | 'REPROMPT';
@@ -63,8 +60,8 @@ type TextResponseBody = {
6360
};
6461

6562
type SessionData = {
66-
sessionAttributes?: Attributes;
67-
promptSessionAttributes?: Attributes;
63+
sessionAttributes?: Record<string, string>;
64+
promptSessionAttributes?: Record<string, string>;
6865
};
6966

7067
type BedrockAgentFunctionResponse = SessionData & {
@@ -100,7 +97,6 @@ export type {
10097
Tool,
10198
ToolFunction,
10299
Parameter,
103-
Attributes,
104100
ParameterValue,
105101
FunctionIdentifier,
106102
FunctionInvocation,

packages/event-handler/src/types/index.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,8 @@ export type {
1010
} from './appsync-events.js';
1111

1212
export type {
13-
Configuration,
14-
Tool,
15-
ToolFunction,
16-
Parameter,
17-
Attributes,
18-
FunctionIdentifier,
19-
FunctionInvocation,
2013
BedrockAgentFunctionEvent,
2114
BedrockAgentFunctionResponse,
22-
ResponseOptions,
2315
ResolverOptions,
2416
} from './bedrock-agent-function.js';
2517

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

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,15 @@ describe('Class: BedrockAgentFunctionResolver', () => {
8383
// Prepare
8484
const app = new BedrockAgentFunctionResolver();
8585

86-
app.tool(async (_params) => {}, {
87-
name: 'noop',
88-
description: 'Does nothing',
89-
});
86+
app.tool(
87+
async (params: { arg: string }) => {
88+
return params.arg;
89+
},
90+
{
91+
name: 'identity',
92+
description: 'Returns its arg',
93+
}
94+
);
9095

9196
// Assess
9297
expect(console.debug).not.toHaveBeenCalled();
@@ -97,10 +102,15 @@ describe('Class: BedrockAgentFunctionResolver', () => {
97102
vi.stubEnv('AWS_LAMBDA_LOG_LEVEL', 'DEBUG');
98103
const app = new BedrockAgentFunctionResolver();
99104

100-
app.tool(async (_params) => {}, {
101-
name: 'noop',
102-
description: 'Does nothing',
103-
});
105+
app.tool(
106+
async (params: { arg: string }) => {
107+
return params.arg;
108+
},
109+
{
110+
name: 'identity',
111+
description: 'Returns its arg',
112+
}
113+
);
104114

105115
// Assess
106116
expect(console.debug).toHaveBeenCalled();
@@ -111,10 +121,15 @@ describe('Class: BedrockAgentFunctionResolver', () => {
111121
const app = new BedrockAgentFunctionResolver();
112122

113123
for (const num of [1, 2, 3, 4, 5]) {
114-
app.tool(async (_params) => {}, {
115-
name: `noop${num}`,
116-
description: 'Does nothing',
117-
});
124+
app.tool(
125+
async (params: { arg: string }) => {
126+
return params.arg;
127+
},
128+
{
129+
name: `identity${num}`,
130+
description: 'Returns its arg',
131+
}
132+
);
118133
}
119134

120135
app.tool(
@@ -214,15 +229,25 @@ describe('Class: BedrockAgentFunctionResolver', () => {
214229
};
215230
const app = new BedrockAgentFunctionResolver({ logger });
216231

217-
app.tool(async (_params) => {}, {
218-
name: 'noop',
219-
description: 'Does nothing',
220-
});
232+
app.tool(
233+
async (params: { arg: string }) => {
234+
return params.arg;
235+
},
236+
{
237+
name: 'identity',
238+
description: 'Returns its arg',
239+
}
240+
);
221241

222-
app.tool(async (_params) => {}, {
223-
name: 'noop',
224-
description: 'Does nothing',
225-
});
242+
app.tool(
243+
async (params: { arg: string }) => {
244+
return params.arg;
245+
},
246+
{
247+
name: 'identity',
248+
description: 'Returns its arg',
249+
}
250+
);
226251

227252
app.tool(
228253
async (_params) => {

0 commit comments

Comments
 (0)