File size: 962 Bytes
564e576
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa0485a
 
564e576
 
 
 
 
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 type { BackendTool } from ".";
import vm from "node:vm";

const calculator: BackendTool = {
	name: "query_calculator",
	displayName: "Calculator",
	description:
		"A simple calculator, takes a string containing a mathematical expression and returns the answer. Only supports +, -, *, ** (power) and /, as well as parenthesis ().",
	parameterDefinitions: {
		equation: {
			description:
				"The formula to evaluate. EXACTLY as you would plug into a calculator. No words, no letters, only numbers and operators. Letters will make the tool crash.",
			type: "formula",
			required: true,
		},
	},
	async *call(params) {
		try {
			const blocks = String(params.equation).split("\n");
			const query = blocks[blocks.length - 1].replace(/[^-()\d/*+.]/g, "");

			return {
				outputs: [{ calculator: `${query} = ${vm.runInNewContext(query)}` }],
			};
		} catch (cause) {
			throw Error("Invalid expression", { cause });
		}
	},
};

export default calculator;