Skip to content

Commit 49b4c74

Browse files
committed
chore: add docstrings
1 parent 310802d commit 49b4c74

File tree

7 files changed

+113
-17
lines changed

7 files changed

+113
-17
lines changed

packages/event-handler/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ You can use the library in both TypeScript and JavaScript code bases.
66

77
## Intro
88

9-
Event handler for Amazon API Gateway REST and HTTP APIs, Application Loader Balancer (ALB), Lambda Function URLs, and VPC Lattice.
9+
Event handler for Amazon API Gateway REST and HTTP APIs, Application Loader Balancer (ALB), Lambda Function URLs, VPC Lattice, AWS AppSync Events APIs, and Amazon Bedrock Agent Functions.
1010

1111
## Usage
1212

@@ -102,6 +102,8 @@ export const handler = async (event, context) =>
102102
app.resolve(event, context);
103103
```
104104

105+
## Bedrock Agent Functions
106+
105107
See the [documentation](https://docs.powertools.aws.dev/lambda/typescript/latest/features/event-handler/appsync-events) for more details on how to use the AppSync event handler.
106108

107109
## Contribute

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

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,32 @@ export class BedrockAgentFunctionResolver {
169169
this.#logger.debug(`Tool "${name}" has been registered.`);
170170
}
171171

172+
/**
173+
* Resolve an incoming Bedrock Agent function invocation event.
174+
*
175+
* @example
176+
* ```ts
177+
* import {
178+
* BedrockAgentFunctionResolver
179+
* } from '@aws-lambda-powertools/event-handler/bedrock-agent';
180+
*
181+
* const app = new BedrockAgentFunctionResolver();
182+
*
183+
* app.tool(async (params) => {
184+
* const { name } = params;
185+
* return `Hello, ${name}!`;
186+
* }, {
187+
* name: 'greeting',
188+
* description: 'Greets a person by name',
189+
* });
190+
*
191+
* export const handler = async (event, context) =>
192+
* app.resolve(event, context);
193+
* ```
194+
*
195+
* @param event - The incoming payload of the AWS Lambda function.
196+
* @param context - The context object provided by AWS Lambda, which contains information about the invocation, function, and execution environment.
197+
*/
172198
async resolve(
173199
event: unknown,
174200
context: Context
@@ -181,19 +207,18 @@ export class BedrockAgentFunctionResolver {
181207
actionGroup,
182208
sessionAttributes,
183209
promptSessionAttributes,
184-
// TODO: add knowledge bases
210+
knowledgeBasesConfiguration,
185211
} = event;
186212

187213
const tool = this.#tools.get(toolName);
188214

189215
if (tool == null) {
190216
this.#logger.error(`Tool ${toolName} has not been registered.`);
191217
return new BedrockFunctionResponse({
192-
actionGroup,
193-
func: toolName,
194218
body: `Error: tool ${toolName} has not been registered.`,
195219
sessionAttributes,
196220
promptSessionAttributes,
221+
knowledgeBasesConfiguration,
197222
}).build({
198223
actionGroup,
199224
func: toolName,
@@ -234,23 +259,21 @@ export class BedrockAgentFunctionResolver {
234259
? ''
235260
: JSON.stringify(response);
236261
return new BedrockFunctionResponse({
237-
actionGroup,
238-
func: toolName,
239262
body,
240263
sessionAttributes,
241264
promptSessionAttributes,
265+
knowledgeBasesConfiguration,
242266
}).build({
243267
actionGroup,
244268
func: toolName,
245269
});
246270
} catch (error) {
247271
this.#logger.error(`An error occurred in tool ${toolName}.`, error);
248272
return new BedrockFunctionResponse({
249-
actionGroup,
250-
func: toolName,
251273
body: `Unable to complete tool execution due to ${error instanceof Error ? `${error.name} - ${error.message}` : String(error)}`,
252274
sessionAttributes,
253275
promptSessionAttributes,
276+
knowledgeBasesConfiguration,
254277
}).build({
255278
actionGroup,
256279
func: toolName,

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,26 +34,30 @@ class BedrockFunctionResponse {
3434
* @see {@link https://docs.aws.amazon.com/bedrock/latest/userguide/agents-session-state.html#session-state-attributes | Bedrock Agent Session State Attributes} for more details.
3535
*/
3636
readonly promptSessionAttributes: Record<string, string>;
37+
/**
38+
* Optional field to configure knowledge bases for the agent.
39+
* @see {@link https://docs.aws.amazon.com/bedrock/latest/userguide/agents-session-state.html#session-state-kb | Bedrock Agent Knowledge Bases} for more details.
40+
*/
41+
readonly knowledgeBasesConfiguration?: Record<string, unknown>;
3742

3843
constructor({
39-
actionGroup,
40-
func,
4144
body,
4245
responseState = undefined,
4346
sessionAttributes = {},
4447
promptSessionAttributes = {},
48+
knowledgeBasesConfiguration = {},
4549
}: {
46-
actionGroup: string;
47-
func: string;
4850
body: string;
4951
responseState?: 'FAILURE' | 'REPROMPT';
5052
sessionAttributes?: Record<string, string>;
5153
promptSessionAttributes?: Record<string, string>;
54+
knowledgeBasesConfiguration?: Record<string, unknown>;
5255
}) {
5356
this.body = body;
5457
this.responseState = responseState;
5558
this.sessionAttributes = sessionAttributes;
5659
this.promptSessionAttributes = promptSessionAttributes;
60+
this.knowledgeBasesConfiguration = knowledgeBasesConfiguration;
5761
}
5862

5963
/**
@@ -87,6 +91,9 @@ class BedrockFunctionResponse {
8791
...(this.promptSessionAttributes && {
8892
promptSessionAttributes: this.promptSessionAttributes,
8993
}),
94+
...(this.knowledgeBasesConfiguration && {
95+
knowledgeBasesConfiguration: this.knowledgeBasesConfiguration,
96+
}),
9097
};
9198
}
9299
}

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

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,45 @@ import type { BedrockAgentFunctionResolver } from '../bedrock-agent/BedrockAgent
44
import type { BedrockFunctionResponse } from '../bedrock-agent/BedrockFunctionResponse.js';
55
import type { GenericLogger } from '../types/common.js';
66

7+
/**
8+
* Configuration for a tool in the Bedrock Agent Function Resolver.
9+
*/
710
type Configuration = {
11+
/**
12+
* The name of the tool, which must be unique across all registered tools.
13+
*/
814
name: string;
9-
description: string;
15+
/**
16+
* A description of the tool, which is optional but highly recommended.
17+
*/
18+
description?: string;
1019
};
1120

21+
/**
22+
* Parameter for a tool function in the Bedrock Agent Function Resolver.
23+
* This is used to define the structure of parameters in tool functions.
24+
*/
1225
type Parameter = {
1326
name: string;
1427
type: 'string' | 'number' | 'integer' | 'boolean' | 'array';
1528
value: string;
1629
};
1730

31+
/**
32+
* Primitive types that can be used as parameter values in tool functions.
33+
* This is used to define the structure of parameters in tool functions.
34+
*/
1835
type ParameterPrimitives = string | number | boolean;
1936

37+
/**
38+
* Represents a value for a parameter, which can be a primitive type or an array of values.
39+
* This is used to define the structure of parameters in tool functions.
40+
*/
2041
type ParameterValue = ParameterPrimitives | Array<ParameterValue>;
2142

43+
/**
44+
* Function to handle tool invocations in the Bedrock Agent Function Resolver.
45+
*/
2246
type ToolFunction<TParams = Record<string, ParameterValue>> = (
2347
params: TParams,
2448
options?: {
@@ -27,11 +51,21 @@ type ToolFunction<TParams = Record<string, ParameterValue>> = (
2751
}
2852
) => Promise<JSONValue | BedrockFunctionResponse>;
2953

54+
/**
55+
* Tool in the Bedrock Agent Function Resolver.
56+
*
57+
* Used to register a tool in {@link BedrockAgentFunctionResolver | `BedrockAgentFunctionResolver`}.
58+
*/
3059
type Tool<TParams = Record<string, ParameterValue>> = {
3160
handler: ToolFunction<TParams>;
3261
config: Configuration;
3362
};
3463

64+
/**
65+
* Function invocation in the Bedrock Agent Function Resolver.
66+
*
67+
* This is used to define the structure of function invocations in tool functions.
68+
*/
3569
type FunctionInvocation = {
3670
actionGroup: string;
3771
function: string;
@@ -43,7 +77,28 @@ type FunctionInvocation = {
4377
*
4478
* @example
4579
* ```json
46-
*
80+
* {
81+
* "messageVersion": "1.0",
82+
* "actionGroup": "exampleActionGroup",
83+
* "function": "getWeather",
84+
* "agent": {
85+
* "name": "WeatherAgent",
86+
* "id": "agent-id-123",
87+
* "alias": "v1",
88+
* "version": "1.0"
89+
* },
90+
* "parameters": [{
91+
* "name": "location",
92+
* "type": "string",
93+
* "value": "Seattle"
94+
* }],
95+
* "inputText": "What's the weather like in Seattle?",
96+
* "sessionId": "session-id-456",
97+
* "sessionAttributes": {
98+
* "userId": "user-789",
99+
* },
100+
* "promptSessionAttributes": {},
101+
* }
47102
* ```
48103
*/
49104
type BedrockAgentFunctionEvent = {
@@ -61,10 +116,19 @@ type BedrockAgentFunctionEvent = {
61116
sessionId: string;
62117
sessionAttributes: Record<string, string>;
63118
promptSessionAttributes: Record<string, string>;
119+
knowledgeBasesConfiguration?: Record<string, unknown>;
64120
};
65121

122+
/**
123+
* Represents the state of the response from a Bedrock agent function:
124+
* - `FAILURE`: The agent throws a `DependencyFailedException` for the current session.
125+
* - `REPROMPT`: The agent passes a response string to the model to reprompt it.
126+
*/
66127
type ResponseState = 'FAILURE' | 'REPROMPT';
67128

129+
/**
130+
* Response structure for a Bedrock agent function.
131+
*/
68132
type BedrockAgentFunctionResponse = {
69133
messageVersion: string;
70134
response: {
@@ -84,7 +148,7 @@ type BedrockAgentFunctionResponse = {
84148
};
85149

86150
/**
87-
* Options for the {@link BedrockAgentFunctionResolver} class
151+
* Options for the {@link BedrockAgentFunctionResolver | `BedrockAgentFunctionResolver`} class
88152
*/
89153
type ResolverOptions = {
90154
/**

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export type {
1313
BedrockAgentFunctionEvent,
1414
BedrockAgentFunctionResponse,
1515
ResolverOptions,
16+
Parameter,
1617
} from './bedrock-agent.js';
1718

1819
export type {

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,6 @@ describe('Class: BedrockAgentFunctionResolver', () => {
336336
app.tool(
337337
async () => {
338338
return new BedrockFunctionResponse({
339-
actionGroup: 'customActionGroup',
340-
func: 'custom-response',
341339
body: 'I am not sure',
342340
responseState: 'REPROMPT',
343341
sessionAttributes: { customAttr: 'value' },

packages/event-handler/typedoc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
],
55
"entryPoints": [
66
"./src/appsync-events/index.ts",
7+
"./src/bedrock-agent/index.ts",
78
"./src/types/index.ts",
89
],
910
"readme": "README.md"

0 commit comments

Comments
 (0)