Skip to content

Commit

Permalink
style: apply prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomas2D committed Sep 6, 2024
1 parent c296218 commit a555460
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 57 deletions.
8 changes: 3 additions & 5 deletions src/tools/calculator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,9 @@ describe("Calculator", () => {
const x2 = 4;
const y2 = 5;

const response = await instance.run(
{
expression: `sqrt( (${x2}-${x1})^2 + (${y2}-${y1})^2 )`
}
);
const response = await instance.run({
expression: `sqrt( (${x2}-${x1})^2 + (${y2}-${y1})^2 )`,
});
expect(response.result).toBe(5);
});

Expand Down
124 changes: 72 additions & 52 deletions src/tools/calculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,64 +14,84 @@
* limitations under the License.
*/

import {
StringToolOutput,
Tool,
ToolInput,
} from "@/tools/base.js";
import { z } from "zod";
import { create, all, evaluate, ImportOptions, ImportObject, ConfigOptions } from 'mathjs'
import { StringToolOutput, Tool, ToolInput } from "@/tools/base.js";
import { z } from "zod";
import { create, all, evaluate, ImportOptions, ImportObject, ConfigOptions } from "mathjs";

export interface CalculatorToolInput {
config?: ConfigOptions
imports?: {
entries: ImportObject | ImportObject[],
options?: ImportOptions
}
}
export interface CalculatorToolInput {
config?: ConfigOptions;
imports?: {
entries: ImportObject | ImportObject[];
options?: ImportOptions;
};
}

/**
* Waring: The CalculatorTool enbales the agent (and by proxy the user) to execute arbirtary
* expressions via mathjs.
*
* Please consider the security and stability risks documented at
* https://mathjs.org/docs/expressions/security.html before using this tool.
*/
export class CalculatorTool extends Tool<StringToolOutput> {
name = "Calculator";
description = `A calculator tool that performs basic arithmetic operations like addition, subtraction, multiplication, and division.
/**
* Waring: The CalculatorTool enbales the agent (and by proxy the user) to execute arbirtary
* expressions via mathjs.
*
* Please consider the security and stability risks documented at
* https://mathjs.org/docs/expressions/security.html before using this tool.
*/
export class CalculatorTool extends Tool<StringToolOutput> {
name = "Calculator";
description = `A calculator tool that performs basic arithmetic operations like addition, subtraction, multiplication, and division.
Only use the calculator tool if you need to perform a calculation.`;

inputSchema() {
return z.object({
expression: z.string().min(1).describe(`The mathematical expression to evaluate (e.g., "2 + 3 * 4"). Use Mathjs basic expression syntax. Constants only.`),
});
}
inputSchema() {
return z.object({
expression: z
.string()
.min(1)
.describe(
`The mathematical expression to evaluate (e.g., "2 + 3 * 4"). Use Mathjs basic expression syntax. Constants only.`,
),
});
}

protected limitedEvaluate: typeof evaluate;
protected limitedEvaluate: typeof evaluate;

constructor({ config, imports, ...options}: CalculatorToolInput = {}) {
super(options);
const math = create(all, config)
this.limitedEvaluate = math.evaluate
// Disable use of potentially vulnerable functions
math.import({
constructor({ config, imports, ...options }: CalculatorToolInput = {}) {
super(options);
const math = create(all, config);
this.limitedEvaluate = math.evaluate;
// Disable use of potentially vulnerable functions
math.import(
{
// most important (hardly any functional impact)
'import': function () { throw new Error('Function import is disabled') },
'createUnit': function () { throw new Error('Function createUnit is disabled') },
'reviver': function () { throw new Error('Function reviver is disabled') },

import: function () {
throw new Error("Function import is disabled");
},
createUnit: function () {
throw new Error("Function createUnit is disabled");
},
reviver: function () {
throw new Error("Function reviver is disabled");
},

// extra (has functional impact)
'evaluate': function () { throw new Error('Function evaluate is disabled') },
'parse': function () { throw new Error('Function parse is disabled') },
'simplify': function () { throw new Error('Function simplify is disabled') },
'derivative': function () { throw new Error('Function derivative is disabled') },
'resolve': function () { throw new Error('Function resolve is disabled') },
}, { override: true, ...imports?.options })
}
evaluate: function () {
throw new Error("Function evaluate is disabled");
},
parse: function () {
throw new Error("Function parse is disabled");
},
simplify: function () {
throw new Error("Function simplify is disabled");
},
derivative: function () {
throw new Error("Function derivative is disabled");
},
resolve: function () {
throw new Error("Function resolve is disabled");
},
},
{ override: true, ...imports?.options },
);
}

protected async _run({ expression }: ToolInput<this>) {
const result = this.limitedEvaluate(expression)
return new StringToolOutput(result);
}
}
protected async _run({ expression }: ToolInput<this>) {
const result = this.limitedEvaluate(expression);
return new StringToolOutput(result);
}
}

0 comments on commit a555460

Please sign in to comment.