diff options
| author | A.J. Shulman <Shulman.aj@gmail.com> | 2024-10-17 10:41:49 -0400 |
|---|---|---|
| committer | A.J. Shulman <Shulman.aj@gmail.com> | 2024-10-17 10:41:49 -0400 |
| commit | 80d86bd5ae3e1d3dc70e7636f72a872a5fb2f01d (patch) | |
| tree | 0eaea49f596bd16720f05a6535958ab8270673c8 /src/client/views/nodes/chatbot/tools/CalculateTool.ts | |
| parent | 596502c232ea6b6b88c3c58486e139074ea056ff (diff) | |
Implemented strict typechecking for tools, specifically tool inputs
Diffstat (limited to 'src/client/views/nodes/chatbot/tools/CalculateTool.ts')
| -rw-r--r-- | src/client/views/nodes/chatbot/tools/CalculateTool.ts | 32 |
1 files changed, 18 insertions, 14 deletions
diff --git a/src/client/views/nodes/chatbot/tools/CalculateTool.ts b/src/client/views/nodes/chatbot/tools/CalculateTool.ts index 050e6f708..e96c9a98a 100644 --- a/src/client/views/nodes/chatbot/tools/CalculateTool.ts +++ b/src/client/views/nodes/chatbot/tools/CalculateTool.ts @@ -1,28 +1,32 @@ -import { Observation, ParametersType } from '../types/types'; +import { Observation } from '../types/types'; +import { ParametersType } from './ToolTypes'; import { BaseTool } from './BaseTool'; -export class CalculateTool extends BaseTool { +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', - [ - { - name: 'expression', - type: 'string', - description: 'The mathematical expression to evaluate', - required: true, - }, - ], + 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<Observation[]> { - // Since the 'expression' parameter is typed as 'string', TypeScript will ensure 'args.expression' is a string - const result = eval(args.expression); // Be cautious with eval(), as it can be dangerous. Consider using a safer alternative if possible. - + 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() }]; } } |
