aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/chatbot/tools/CalculateTool.ts
blob: e96c9a98a1d2b33f009aea370a2751c154bec412 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { Observation } from '../types/types';
import { ParametersType } from './ToolTypes';
import { BaseTool } from './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',
            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: 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() }];
    }
}