broadfield commited on
Commit
7805350
·
verified ·
1 Parent(s): f1e26e5

Add calculator module

Browse files
Files changed (1) hide show
  1. modules_calculator.js +75 -0
modules_calculator.js ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (function(MS) {
2
+ MS.register("calculator", {
3
+ name: "Calculator",
4
+ description: "Precise calculations: arithmetic, trig, logarithms, unit conversions (km⇔miles, C⇔F, kg⇔lbs, etc.)",
5
+ tools: [{
6
+ type: "function",
7
+ function: {
8
+ name: "calculate",
9
+ description: "Evaluate a math expression. Supports +, -, *, /, ^, sin, cos, tan, sqrt, log, ln, abs, pi, and unit conversions like '100 km to miles', '32 C to F', '5 kg to lbs'.",
10
+ parameters: {
11
+ type: "object",
12
+ properties: { expression: { type: "string", description: "Math expression to evaluate" } },
13
+ required: ["expression"]
14
+ }
15
+ }
16
+ }],
17
+ handleToolCall: async function(tn, args) {
18
+ if (tn !== "calculate") return null;
19
+ try {
20
+ var e = args.expression.trim();
21
+
22
+ // Unit conversions
23
+ var uc = {
24
+ "km to miles": function(v) { return v * 0.621371; },
25
+ "miles to km": function(v) { return v / 0.621371; },
26
+ "c to f": function(v) { return v * 9/5 + 32; },
27
+ "f to c": function(v) { return (v - 32) * 5/9; },
28
+ "kg to lbs": function(v) { return v * 2.20462; },
29
+ "lbs to kg": function(v) { return v / 2.20462; },
30
+ "meters to feet": function(v) { return v * 3.28084; },
31
+ "feet to meters": function(v) { return v / 3.28084; },
32
+ "inches to cm": function(v) { return v * 2.54; },
33
+ "cm to inches": function(v) { return v / 2.54; },
34
+ "liters to gallons": function(v) { return v * 0.264172; },
35
+ "gallons to liters": function(v) { return v / 0.264172; }
36
+ };
37
+
38
+ for (var key in uc) {
39
+ var idx = e.toLowerCase().indexOf(" " + key);
40
+ if (idx === -1) idx = e.toLowerCase().indexOf(key);
41
+ if (idx > -1) {
42
+ var numStr = e.substring(0, idx).trim();
43
+ var num = parseFloat(numStr);
44
+ if (!isNaN(num)) {
45
+ var result = uc[key](num);
46
+ var rounded = Math.round(result * 10000) / 10000;
47
+ return e + " = " + rounded + " (" + key + ")";
48
+ }
49
+ }
50
+ }
51
+
52
+ // Math evaluation
53
+ var p = e.replace(/\^/g, "**")
54
+ .replace(/\bpi\b/gi, "(" + Math.PI + ")")
55
+ .replace(/\be\b(?!\w)/g, "(" + Math.E + ")")
56
+ .replace(/\bsin\(/g, "Math.sin(")
57
+ .replace(/\bcos\(/g, "Math.cos(")
58
+ .replace(/\btan\(/g, "Math.tan(")
59
+ .replace(/\basin\(/g, "Math.asin(")
60
+ .replace(/\bacos\(/g, "Math.acos(")
61
+ .replace(/\batan\(/g, "Math.atan(")
62
+ .replace(/\bsqrt\(/g, "Math.sqrt(")
63
+ .replace(/\blog\(/g, "Math.log10(")
64
+ .replace(/\bln\(/g, "Math.log(")
65
+ .replace(/\babs\(/g, "Math.abs(")
66
+ .replace(/\bround\(/g, "Math.round(")
67
+ .replace(/\bfloor\(/g, "Math.floor(")
68
+ .replace(/\bceil\(/g, "Math.ceil(");
69
+ var result = Function('"use strict"; return (' + p + ')')();
70
+ var rounded = Math.round(result * 1000000) / 1000000;
71
+ return e + " = " + rounded;
72
+ } catch (ex) { return "Error: " + ex.message; }
73
+ }
74
+ });
75
+ })(ModuleSystem);