Skip to content

Commit afb1213

Browse files
committed
only use json.stringify
1 parent 3cab21a commit afb1213

File tree

3 files changed

+7
-21
lines changed

3 files changed

+7
-21
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import type {
1212
ToolFunction,
1313
} from '../types/bedrock-agent-function.js';
1414
import type { GenericLogger } from '../types/common.js';
15-
import { assertBedrockAgentFunctionEvent, isPrimitive } from './utils.js';
15+
import { assertBedrockAgentFunctionEvent } from './utils.js';
1616

1717
export class BedrockAgentFunctionResolver {
1818
readonly #tools: Map<string, Tool> = new Map();
@@ -206,8 +206,9 @@ export class BedrockAgentFunctionResolver {
206206
}
207207

208208
try {
209-
const res = (await tool.handler(toolParams, event, context)) ?? '';
210-
const body = isPrimitive(res) ? String(res) : JSON.stringify(res); //TODO just use JSON.stringify
209+
// 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
211212
return this.#buildResponse({
212213
actionGroup,
213214
function: toolName,

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

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,6 @@
1-
import type {
2-
JSONPrimitive,
3-
JSONValue,
4-
} from '@aws-lambda-powertools/commons/types';
51
import { isRecord, isString } from '@aws-lambda-powertools/commons/typeutils';
62
import type { BedrockAgentFunctionEvent } from '../types/bedrock-agent-function.js';
73

8-
export function isPrimitive(value: JSONValue): value is JSONPrimitive {
9-
return (
10-
value === null ||
11-
value === undefined ||
12-
typeof value === 'string' ||
13-
typeof value === 'number' ||
14-
typeof value === 'boolean'
15-
);
16-
}
17-
184
/**
195
* Asserts that the provided event is a BedrockAgentFunctionEvent.
206
*

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,6 @@ describe('Class: BedrockAgentFunctionResolver', () => {
235235
);
236236

237237
// Act
238-
239238
await app.resolve(createEvent('noop'), context);
240239
await app.resolve(createEvent('error'), context).catch(() => {});
241240

@@ -512,7 +511,7 @@ describe('Class: BedrockAgentFunctionResolver', () => {
512511
// Assess
513512
expect(actual.response.function).toEqual(toolParams.name);
514513
expect(actual.response.functionResponse.responseBody.TEXT.body).toEqual(
515-
'String: hello world'
514+
'"String: hello world"'
516515
);
517516
});
518517

@@ -542,7 +541,7 @@ describe('Class: BedrockAgentFunctionResolver', () => {
542541
// Assess
543542
expect(actual.response.function).toEqual(toolParams.name);
544543
expect(actual.response.functionResponse.responseBody.TEXT.body).toEqual(
545-
'Array as string: [1,2,3]'
544+
'"Array as string: [1,2,3]"'
546545
);
547546
});
548547

@@ -621,7 +620,7 @@ describe('Class: BedrockAgentFunctionResolver', () => {
621620
functionResponse: {
622621
responseBody: {
623622
TEXT: {
624-
body: 'Hello, John!',
623+
body: '"Hello, John!"',
625624
},
626625
},
627626
},

0 commit comments

Comments
 (0)