{"version":3,"sources":["../../src/tools/calculator.ts"],"names":["CalculatorTool","Tool","name","description","emitter","Emitter","root","child","namespace","creator","inputSchema","z","object","expression","string","min","describe","limitedEvaluate","config","imports","options","math","create","all","evaluate","import","Error","createUnit","reviver","parse","simplify","derivative","resolve","override","_run","result","StringToolOutput"],"mappings":";;;;;;;;;AAyBO,MAAMA,uBAAuBC,aAAAA,CAAAA;EAzBpC;;;EA0BEC,IAAAA,GAAO,YAAA;EACPC,WAAAA,GAAc,CAAA;;EAGEC,OAAAA,GAA0DC,mBAAAA,CAAQC,KAAKC,KAAAA,CAAM;IAC3FC,SAAAA,EAAW;AAAC,MAAA,MAAA;AAAQ,MAAA;;IACpBC,OAAAA,EAAS;GACX,CAAA;EAEAC,WAAAA,GAAc;AACZ,IAAA,OAAOC,MAAEC,MAAAA,CAAO;AACdC,MAAAA,UAAAA,EAAYF,MACTG,MAAAA,EAAM,CACNC,IAAI,CAAA,CAAA,CACJC,SACC,CAAA,gHAAA,CAAkH;KAExH,CAAA;AACF,EAAA;AAEUC,EAAAA,eAAAA;AAEV,EAAA,WAAA,CAAY,EAAEC,MAAAA,EAAQC,OAAAA,EAAS,GAAGC,OAAAA,EAAAA,GAAiC,EAAC,EAAG;AACrE,IAAA,KAAA,CAAMA,OAAAA,CAAAA;AACN,IAAA,MAAMC,IAAAA,GAAOC,aAAAA,CAAOC,UAAAA,EAAKL,MAAAA,CAAAA;AACzB,IAAA,IAAA,CAAKD,kBAAkBI,IAAAA,CAAKG,QAAAA;AAE5BH,IAAAA,IAAAA,CAAKI,MAAAA,CACH;;AAEEA,MAAAA,MAAAA,kBAAQ,MAAA,CAAA,WAAA;AACN,QAAA,MAAM,IAAIC,MAAM,6BAAA,CAAA;MAClB,CAAA,EAFQ,QAAA,CAAA;AAGRC,MAAAA,UAAAA,kBAAY,MAAA,CAAA,WAAA;AACV,QAAA,MAAM,IAAID,MAAM,iCAAA,CAAA;MAClB,CAAA,EAFY,YAAA,CAAA;AAGZE,MAAAA,OAAAA,kBAAS,MAAA,CAAA,WAAA;AACP,QAAA,MAAM,IAAIF,MAAM,8BAAA,CAAA;MAClB,CAAA,EAFS,SAAA,CAAA;;AAKTF,MAAAA,QAAAA,kBAAU,MAAA,CAAA,WAAA;AACR,QAAA,MAAM,IAAIE,MAAM,+BAAA,CAAA;MAClB,CAAA,EAFU,UAAA,CAAA;AAGVG,MAAAA,KAAAA,kBAAO,MAAA,CAAA,WAAA;AACL,QAAA,MAAM,IAAIH,MAAM,4BAAA,CAAA;MAClB,CAAA,EAFO,OAAA,CAAA;AAGPI,MAAAA,QAAAA,kBAAU,MAAA,CAAA,WAAA;AACR,QAAA,MAAM,IAAIJ,MAAM,+BAAA,CAAA;MAClB,CAAA,EAFU,UAAA,CAAA;AAGVK,MAAAA,UAAAA,kBAAY,MAAA,CAAA,WAAA;AACV,QAAA,MAAM,IAAIL,MAAM,iCAAA,CAAA;MAClB,CAAA,EAFY,YAAA,CAAA;AAGZM,MAAAA,OAAAA,kBAAS,MAAA,CAAA,WAAA;AACP,QAAA,MAAM,IAAIN,MAAM,8BAAA,CAAA;MAClB,CAAA,EAFS,SAAA;KAGX,EACA;MAAEO,QAAAA,EAAU,IAAA;AAAM,MAAA,GAAGd,OAAAA,EAASC;KAAQ,CAAA;AAE1C,EAAA;EAEA,MAAgBc,IAAAA,CAAK,EAAErB,UAAAA,EAAU,EAAqB;AACpD,IAAA,MAAMsB,MAAAA,GAAS,IAAA,CAAKlB,eAAAA,CAAgBJ,UAAAA,CAAAA;AACpC,IAAA,OAAO,IAAIuB,0BAAiBD,MAAAA,CAAAA;AAC9B,EAAA;AACF","file":"calculator.cjs","sourcesContent":["/**\n * Copyright 2025 © BeeAI a Series of LF Projects, LLC\n * SPDX-License-Identifier: Apache-2.0\n */\n\nimport { ToolEmitter, StringToolOutput, Tool, ToolInput } from \"@/tools/base.js\";\nimport { z } from \"zod\";\nimport { create, all, evaluate, ImportOptions, ImportObject, ConfigOptions } from \"mathjs\";\nimport { Emitter } from \"@/emitter/emitter.js\";\n\nexport interface CalculatorToolInput {\n  config?: ConfigOptions;\n  imports?: {\n    entries: ImportObject | ImportObject[];\n    options?: ImportOptions;\n  };\n}\n\n/**\n * Warning: The CalculatorTool enables the agent (and by proxy the user) to execute arbitrary\n * expressions via mathjs.\n *\n * Please consider the security and stability risks documented at\n * https://mathjs.org/docs/expressions/security.html before using this tool.\n */\nexport class CalculatorTool extends Tool<StringToolOutput> {\n  name = \"Calculator\";\n  description = `A calculator tool that performs basic arithmetic operations like addition, subtraction, multiplication, and division. \nOnly use the calculator tool if you need to perform a calculation.`;\n\n  public readonly emitter: ToolEmitter<ToolInput<this>, StringToolOutput> = Emitter.root.child({\n    namespace: [\"tool\", \"calculator\"],\n    creator: this,\n  });\n\n  inputSchema() {\n    return z.object({\n      expression: z\n        .string()\n        .min(1)\n        .describe(\n          `The mathematical expression to evaluate (e.g., \"2 + 3 * 4\"). Use Mathjs basic expression syntax. Constants only.`,\n        ),\n    });\n  }\n\n  protected limitedEvaluate: typeof evaluate;\n\n  constructor({ config, imports, ...options }: CalculatorToolInput = {}) {\n    super(options);\n    const math = create(all, config);\n    this.limitedEvaluate = math.evaluate;\n    // Disable use of potentially vulnerable functions\n    math.import(\n      {\n        // most important (hardly any functional impact)\n        import: function () {\n          throw new Error(\"Function import is disabled\");\n        },\n        createUnit: function () {\n          throw new Error(\"Function createUnit is disabled\");\n        },\n        reviver: function () {\n          throw new Error(\"Function reviver is disabled\");\n        },\n\n        // extra (has functional impact)\n        evaluate: function () {\n          throw new Error(\"Function evaluate is disabled\");\n        },\n        parse: function () {\n          throw new Error(\"Function parse is disabled\");\n        },\n        simplify: function () {\n          throw new Error(\"Function simplify is disabled\");\n        },\n        derivative: function () {\n          throw new Error(\"Function derivative is disabled\");\n        },\n        resolve: function () {\n          throw new Error(\"Function resolve is disabled\");\n        },\n      },\n      { override: true, ...imports?.options },\n    );\n  }\n\n  protected async _run({ expression }: ToolInput<this>) {\n    const result = this.limitedEvaluate(expression);\n    return new StringToolOutput(result);\n  }\n}\n"]}