Skip to content

chore(event-handler): align implementation with other runtimes #3989

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 28, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore: remove decorator usage
  • Loading branch information
dreamorosi committed May 28, 2025
commit 310802dcc20a37d999da1750d803f602ebccbec3
Original file line number Diff line number Diff line change
Expand Up @@ -110,32 +110,6 @@ export class BedrockAgentFunctionResolver {
* app.resolve(event, context);
* ```
*
* The method also works as a class method decorator:
*
* @example
* ```ts
* import {
* BedrockAgentFunctionResolver
* } from '@aws-lambda-powertools/event-handler/bedrock-agent';
*
* const app = new BedrockAgentFunctionResolver();
*
* class Lambda {
* @app.tool({ name: 'greeting', description: 'Greets a person by name' })
* async greeting(params) {
* const { name } = params;
* return `Hello, ${name}!`;
* }
*
* async handler(event, context) {
* return app.resolve(event, context);
* }
* }
*
* const lambda = new Lambda();
* export const handler = lambda.handler.bind(lambda);
* ```
*
* When defining a tool, you can also access the original `event` and `context` objects from the Bedrock Agent function invocation.
* This is useful if you need to access the session attributes or other context-specific information.
*
Expand Down Expand Up @@ -172,26 +146,9 @@ export class BedrockAgentFunctionResolver {
public tool<TParams extends Record<string, ParameterValue>>(
fn: ToolFunction<TParams>,
config: Configuration
): undefined;
public tool<TParams extends Record<string, ParameterValue>>(
config: Configuration
): MethodDecorator;
public tool<TParams extends Record<string, ParameterValue>>(
fnOrConfig: ToolFunction<TParams> | Configuration,
config?: Configuration
): MethodDecorator | undefined {
// When used as a method (not a decorator)
if (typeof fnOrConfig === 'function') {
this.#registerTool(fnOrConfig, config as Configuration);
return;
}

// When used as a decorator
return (_target, _propertyKey, descriptor: PropertyDescriptor) => {
const toolFn = descriptor.value as ToolFunction;
this.#registerTool(toolFn, fnOrConfig);
return descriptor;
};
): undefined {
this.#registerTool(fn, config);
return;
}

#registerTool<TParams extends Record<string, ParameterValue>>(
Expand Down Expand Up @@ -267,7 +224,7 @@ export class BedrockAgentFunctionResolver {
try {
const response = await tool.handler(toolParams, { event, context });
if (response instanceof BedrockFunctionResponse) {
const res = response.build({
return response.build({
actionGroup,
func: toolName,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,52 +243,6 @@ describe('Class: BedrockAgentFunctionResolver', () => {
);
});

it('can be invoked using the decorator pattern', async () => {
// Prepare
const app = new BedrockAgentFunctionResolver();

class Lambda {
@app.tool({ name: 'hello', description: 'Says hello' })
async helloWorld() {
return 'Hello, world!';
}

@app.tool({ name: 'add', description: 'Adds two numbers' })
async add(params: { a: number; b: number }) {
const { a, b } = params;
return a + b;
}

public async handler(event: BedrockAgentFunctionEvent, context: Context) {
return app.resolve(event, context);
}
}

const lambda = new Lambda();

const addEvent = createEvent('add', [
{
name: 'a',
type: 'number',
value: '1',
},
{
name: 'b',
type: 'number',
value: '2',
},
]);

// Act
const actual = await lambda.handler(addEvent, context);

// Assess
expect(actual.response.function).toEqual('add');
expect(actual.response.functionResponse.responseBody.TEXT.body).toEqual(
'3'
);
});

it.each([
{
toolFunction: async () => ({
Expand Down