Skip to content

Commit 71f24b4

Browse files
committed
make tool method generic for type Record<string, ParameterValue>>
1 parent bb483f3 commit 71f24b4

File tree

3 files changed

+242
-48
lines changed

3 files changed

+242
-48
lines changed

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

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type {
44
BedrockAgentFunctionEvent,
55
BedrockAgentFunctionResponse,
66
Configuration,
7+
ParameterValue,
78
ResolverOptions,
89
ResponseOptions,
910
Tool,
@@ -13,7 +14,8 @@ import type { GenericLogger } from '../types/common.js';
1314
import { isPrimitive } from './utils.js';
1415

1516
export class BedrockAgentFunctionResolver {
16-
readonly #tools: Map<string, Tool> = new Map<string, Tool>();
17+
readonly #tools: Map<string, Tool<Record<string, ParameterValue>>> =
18+
new Map();
1719
readonly #envService: EnvironmentVariablesService;
1820
readonly #logger: Pick<GenericLogger, 'debug' | 'warn' | 'error'>;
1921

@@ -77,10 +79,15 @@ export class BedrockAgentFunctionResolver {
7779
* @param fn - The tool function
7880
* @param config - The configuration object for the tool
7981
*/
80-
public tool(fn: ToolFunction, config: Configuration): undefined;
81-
public tool(config: Configuration): MethodDecorator;
82-
public tool(
83-
fnOrConfig: ToolFunction | Configuration,
82+
public tool<TParams extends Record<string, ParameterValue>>(
83+
fn: ToolFunction<TParams>,
84+
config: Configuration
85+
): undefined;
86+
public tool<TParams extends Record<string, ParameterValue>>(
87+
config: Configuration
88+
): MethodDecorator;
89+
public tool<TParams extends Record<string, ParameterValue>>(
90+
fnOrConfig: ToolFunction<TParams> | Configuration,
8491
config?: Configuration
8592
): MethodDecorator | undefined {
8693
// When used as a method (not a decorator)
@@ -97,7 +104,10 @@ export class BedrockAgentFunctionResolver {
97104
};
98105
}
99106

100-
#registerTool(fn: ToolFunction, config: Configuration): void {
107+
#registerTool<TParams extends Record<string, ParameterValue>>(
108+
handler: ToolFunction<TParams>,
109+
config: Configuration
110+
): void {
101111
const { name } = config;
102112

103113
if (this.#tools.size >= 5) {
@@ -113,7 +123,10 @@ export class BedrockAgentFunctionResolver {
113123
);
114124
}
115125

116-
this.#tools.set(name, { function: fn, config });
126+
this.#tools.set(name, {
127+
handler: handler as ToolFunction<Record<string, ParameterValue>>,
128+
config,
129+
});
117130
this.#logger.debug(`Tool ${name} has been registered.`);
118131
}
119132

@@ -169,12 +182,29 @@ export class BedrockAgentFunctionResolver {
169182
});
170183
}
171184

172-
const parameterObject: Record<string, string> = Object.fromEntries(
173-
parameters.map((param) => [param.name, param.value])
174-
);
185+
const toolParams: Record<string, ParameterValue> = {};
186+
for (const param of parameters) {
187+
switch (param.type) {
188+
case 'boolean': {
189+
toolParams[param.name] = param.value === 'true';
190+
break;
191+
}
192+
case 'number':
193+
case 'integer': {
194+
toolParams[param.name] = Number(param.value);
195+
break;
196+
}
197+
// this default will also catch array types but we leave them as strings
198+
// because we cannot reliably parse them
199+
default: {
200+
toolParams[param.name] = param.value;
201+
break;
202+
}
203+
}
204+
}
175205

176206
try {
177-
const res = (await tool.function(parameterObject)) ?? '';
207+
const res = (await tool.handler(toolParams, event, context)) ?? '';
178208
const body = isPrimitive(res) ? String(res) : JSON.stringify(res);
179209
return this.#buildResponse({
180210
actionGroup,

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { JSONValue } from '@aws-lambda-powertools/commons/types';
2+
import type { Context } from 'aws-lambda';
23
import type { GenericLogger } from '../types/common.js';
34

45
type Configuration = {
@@ -16,14 +17,18 @@ type ParameterPrimitives = string | number | boolean;
1617

1718
type ParameterValue = ParameterPrimitives | Array<ParameterValue>;
1819

19-
type Tool<TParams = Record<string, ParameterValue>> = {
20+
type ToolFunction<TParams = Record<string, ParameterValue>> = (
21+
params: TParams,
22+
event: BedrockAgentFunctionEvent,
23+
context: Context
2024
// biome-ignore lint/suspicious/noConfusingVoidType: we need to support async functions that don't have an explicit return value
21-
function: (params: TParams) => Promise<JSONValue | void>;
25+
) => Promise<JSONValue | void>;
26+
27+
type Tool<TParams = Record<string, ParameterValue>> = {
28+
handler: ToolFunction<TParams>;
2229
config: Configuration;
2330
};
2431

25-
type ToolFunction = Tool['function'];
26-
2732
type Attributes = Record<string, string>;
2833

2934
type FunctionIdentifier = {
@@ -96,6 +101,7 @@ export type {
96101
ToolFunction,
97102
Parameter,
98103
Attributes,
104+
ParameterValue,
99105
FunctionIdentifier,
100106
FunctionInvocation,
101107
BedrockAgentFunctionEvent,

0 commit comments

Comments
 (0)