Skip to content

Commit d7c233b

Browse files
committed
chore: implementation
1 parent ecc2470 commit d7c233b

File tree

3 files changed

+54
-19
lines changed

3 files changed

+54
-19
lines changed

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

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,36 @@ import { assertBedrockAgentFunctionEvent } from './utils.js';
1818
*
1919
* This resolver is designed to handle function invocations from Bedrock Agents.
2020
*
21+
* @example
22+
* ```ts
23+
* import {
24+
* BedrockAgentFunctionResolver
25+
* } from '@aws-lambda-powertools/event-handler/bedrock-agent';
2126
*
27+
* const app = new BedrockAgentFunctionResolver();
28+
*
29+
* app.tool(async (params) => {
30+
* const { name } = params;
31+
* return `Hello, ${name}!`;
32+
* }, {
33+
* name: 'greeting',
34+
* description: 'Greets a person by name',
35+
* });
36+
*
37+
* export const handler = async (event, context) =>
38+
* app.resolve(event, context);
39+
* ```
2240
*/
2341
export class BedrockAgentFunctionResolver {
42+
/**
43+
* Registry of tools added to the Bedrock Agent Function Resolver.
44+
*/
2445
readonly #tools: Map<string, Tool> = new Map();
46+
/**
47+
* A logger instance to be used for logging debug, warning, and error messages.
48+
*
49+
* When no logger is provided, we'll only log warnings and errors using the global `console` object.
50+
*/
2551
readonly #logger: Pick<GenericLogger, 'debug' | 'warn' | 'error'>;
2652

2753
constructor(options?: ResolverOptions) {
@@ -198,6 +224,7 @@ export class BedrockAgentFunctionResolver {
198224
actionGroup,
199225
sessionAttributes,
200226
promptSessionAttributes,
227+
// TODO: add knowledge bases
201228
} = event;
202229

203230
const tool = this.#tools.get(toolName);
@@ -210,7 +237,10 @@ export class BedrockAgentFunctionResolver {
210237
body: `Error: tool ${toolName} has not been registered.`,
211238
sessionAttributes,
212239
promptSessionAttributes,
213-
}).build();
240+
}).build({
241+
actionGroup,
242+
func: toolName,
243+
});
214244
}
215245

216246
const toolParams: Record<string, ParameterValue> = {};
@@ -237,7 +267,10 @@ export class BedrockAgentFunctionResolver {
237267
try {
238268
const response = await tool.handler(toolParams, { event, context });
239269
if (response instanceof BedrockFunctionResponse) {
240-
return response.build();
270+
const res = response.build({
271+
actionGroup,
272+
func: toolName,
273+
});
241274
}
242275
const body =
243276
isNullOrUndefined(response) || response === ''
@@ -249,7 +282,10 @@ export class BedrockAgentFunctionResolver {
249282
body,
250283
sessionAttributes,
251284
promptSessionAttributes,
252-
}).build();
285+
}).build({
286+
actionGroup,
287+
func: toolName,
288+
});
253289
} catch (error) {
254290
this.#logger.error(`An error occurred in tool ${toolName}.`, error);
255291
return new BedrockFunctionResponse({
@@ -258,7 +294,10 @@ export class BedrockAgentFunctionResolver {
258294
body: `Unable to complete tool execution due to ${error instanceof Error ? `${error.name} - ${error.message}` : String(error)}`,
259295
sessionAttributes,
260296
promptSessionAttributes,
261-
}).build();
297+
}).build({
298+
actionGroup,
299+
func: toolName,
300+
});
262301
}
263302
}
264303
}

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

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,6 @@ import type { BedrockAgentFunctionResolver } from './BedrockAgentFunctionResolve
1212
* when you return anything from your function handler other than an instance of this class.
1313
*/
1414
class BedrockFunctionResponse {
15-
/**
16-
* The name of the action group, this comes from the `event.actionGroup` field
17-
* in the Bedrock agent function event.
18-
*/
19-
readonly actionGroup: string;
20-
/**
21-
* The name of the function returning the response, this comes from the `event.function` field
22-
* in the Bedrock agent function event.
23-
*/
24-
readonly func: string;
2515
/**
2616
* The response object that defines the response from execution of the function.
2717
*/
@@ -60,8 +50,6 @@ class BedrockFunctionResponse {
6050
sessionAttributes?: Record<string, string>;
6151
promptSessionAttributes?: Record<string, string>;
6252
}) {
63-
this.actionGroup = actionGroup;
64-
this.func = func;
6553
this.body = body;
6654
this.responseState = responseState;
6755
this.sessionAttributes = sessionAttributes;
@@ -70,13 +58,20 @@ class BedrockFunctionResponse {
7058

7159
/**
7260
* Builds the Bedrock function response object according to the Bedrock agent function {@link https://docs.aws.amazon.com/bedrock/latest/userguide/agents-lambda.html#agents-lambda-response | response format}.
61+
*
62+
* @param options - The options for building the response.
63+
* @param options.actionGroup - The action group of the function, this comes from the `event.actionGroup` field in the Bedrock agent function event.
64+
* @param options.func - The name of the function being invoked by the agent, this comes from the `event.function` field in the Bedrock agent function event.
7365
*/
74-
build() {
66+
build(options: {
67+
actionGroup: string;
68+
func: string;
69+
}) {
7570
return {
7671
messageVersion: '1.0',
7772
response: {
78-
actionGroup: this.actionGroup,
79-
function: this.func,
73+
actionGroup: options.actionGroup,
74+
function: options.func,
8075
functionResponse: {
8176
...(this.responseState && { responseState: this.responseState }),
8277
responseBody: {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export { BedrockAgentFunctionResolver } from './BedrockAgentFunctionResolver.js';
2+
export { BedrockFunctionResponse } from './BedrockFunctionResponse.js';

0 commit comments

Comments
 (0)