aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/chatbot/tools/CalculateTool.ts
diff options
context:
space:
mode:
authoreleanor-park <eleanor_park@brown.edu>2024-10-30 19:39:46 -0400
committereleanor-park <eleanor_park@brown.edu>2024-10-30 19:39:46 -0400
commitc11c760db62f78a07b624b98b209e6ee86036c8e (patch)
treec9587b50042a5115373e91ba8ecf9b76913cd321 /src/client/views/nodes/chatbot/tools/CalculateTool.ts
parentb5944e87f9d4f3149161de4de0d76db486461c76 (diff)
parent4c768162e0436115a05b9c8b0e4d837d626d45ba (diff)
Merge branch 'master' into eleanor-gptdraw
Diffstat (limited to 'src/client/views/nodes/chatbot/tools/CalculateTool.ts')
-rw-r--r--src/client/views/nodes/chatbot/tools/CalculateTool.ts30
1 files changed, 18 insertions, 12 deletions
diff --git a/src/client/views/nodes/chatbot/tools/CalculateTool.ts b/src/client/views/nodes/chatbot/tools/CalculateTool.ts
index 77ab1b39b..e96c9a98a 100644
--- a/src/client/views/nodes/chatbot/tools/CalculateTool.ts
+++ b/src/client/views/nodes/chatbot/tools/CalculateTool.ts
@@ -1,26 +1,32 @@
+import { Observation } from '../types/types';
+import { ParametersType } from './ToolTypes';
import { BaseTool } from './BaseTool';
-export class CalculateTool extends BaseTool<{ expression: string }> {
+const calculateToolParams = [
+ {
+ name: 'expression',
+ type: 'string',
+ description: 'The mathematical expression to evaluate',
+ required: true,
+ },
+] as const;
+
+type CalculateToolParamsType = typeof calculateToolParams;
+
+export class CalculateTool extends BaseTool<CalculateToolParamsType> {
constructor() {
super(
'calculate',
'Perform a calculation',
- {
- expression: {
- type: 'string',
- description: 'The mathematical expression to evaluate',
- required: 'true',
- max_inputs: '1',
- },
- },
+ calculateToolParams, // Use the reusable param config here
'Provide a mathematical expression to calculate that would work with JavaScript eval().',
'Runs a calculation and returns the number - uses JavaScript so be sure to use floating point syntax if necessary'
);
}
- async execute(args: { expression: string }): Promise<unknown> {
- // Note: Using eval() can be dangerous. Consider using a safer alternative.
- const result = eval(args.expression);
+ async execute(args: ParametersType<CalculateToolParamsType>): Promise<Observation[]> {
+ // TypeScript will ensure 'args.expression' is a string based on the param config
+ const result = eval(args.expression); // Be cautious with eval(), as it can be dangerous. Consider using a safer alternative.
return [{ type: 'text', text: result.toString() }];
}
}