{"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","constructor","config","imports","options","math","create","all","evaluate","import","Error","createUnit","reviver","parse","simplify","derivative","resolve","override","_run","result","StringToolOutput"],"mappings":";;;;;;;;;AAoCO,MAAMA,uBAAuBC,aAAAA,CAAAA;EApCpC;;;EAqCEC,IAAO,GAAA,YAAA;EACPC,WAAc,GAAA,CAAA;;EAGEC,OAA0DC,GAAAA,mBAAAA,CAAQC,KAAKC,KAAM,CAAA;IAC3FC,SAAW,EAAA;AAAC,MAAA,MAAA;AAAQ,MAAA;;IACpBC,OAAS,EAAA;GACX,CAAA;EAEAC,WAAc,GAAA;AACZ,IAAA,OAAOC,MAAEC,MAAO,CAAA;AACdC,MAAAA,UAAAA,EAAYF,MACTG,MAAM,EAAA,CACNC,IAAI,CAAA,CAAA,CACJC,SACC,CAAkH,gHAAA,CAAA;KAExH,CAAA;AACF;AAEUC,EAAAA,eAAAA;AAEVC,EAAAA,WAAAA,CAAY,EAAEC,MAAQC,EAAAA,OAAAA,EAAS,GAAGC,OAAAA,EAAAA,GAAiC,EAAI,EAAA;AACrE,IAAA,KAAA,CAAMA,OAAAA,CAAAA;AACN,IAAMC,MAAAA,IAAAA,GAAOC,aAAOC,CAAAA,UAAAA,EAAKL,MAAAA,CAAAA;AACzB,IAAA,IAAA,CAAKF,kBAAkBK,IAAKG,CAAAA,QAAAA;AAE5BH,IAAAA,IAAAA,CAAKI,MACH,CAAA;;AAEEA,MAAAA,MAAAA,kBAAQ,MAAA,CAAA,WAAA;AACN,QAAM,MAAA,IAAIC,MAAM,6BAAA,CAAA;OADV,EAAA,QAAA,CAAA;AAGRC,MAAAA,UAAAA,kBAAY,MAAA,CAAA,WAAA;AACV,QAAM,MAAA,IAAID,MAAM,iCAAA,CAAA;OADN,EAAA,YAAA,CAAA;AAGZE,MAAAA,OAAAA,kBAAS,MAAA,CAAA,WAAA;AACP,QAAM,MAAA,IAAIF,MAAM,8BAAA,CAAA;OADT,EAAA,SAAA,CAAA;;AAKTF,MAAAA,QAAAA,kBAAU,MAAA,CAAA,WAAA;AACR,QAAM,MAAA,IAAIE,MAAM,+BAAA,CAAA;OADR,EAAA,UAAA,CAAA;AAGVG,MAAAA,KAAAA,kBAAO,MAAA,CAAA,WAAA;AACL,QAAM,MAAA,IAAIH,MAAM,4BAAA,CAAA;OADX,EAAA,OAAA,CAAA;AAGPI,MAAAA,QAAAA,kBAAU,MAAA,CAAA,WAAA;AACR,QAAM,MAAA,IAAIJ,MAAM,+BAAA,CAAA;OADR,EAAA,UAAA,CAAA;AAGVK,MAAAA,UAAAA,kBAAY,MAAA,CAAA,WAAA;AACV,QAAM,MAAA,IAAIL,MAAM,iCAAA,CAAA;OADN,EAAA,YAAA,CAAA;AAGZM,MAAAA,OAAAA,kBAAS,MAAA,CAAA,WAAA;AACP,QAAM,MAAA,IAAIN,MAAM,8BAAA,CAAA;OADT,EAAA,SAAA;KAIX,EAAA;MAAEO,QAAU,EAAA,IAAA;AAAM,MAAA,GAAGd,OAASC,EAAAA;KAAQ,CAAA;AAE1C;EAEA,MAAgBc,IAAAA,CAAK,EAAEtB,UAAAA,EAA+B,EAAA;AACpD,IAAMuB,MAAAA,MAAAA,GAAS,IAAKnB,CAAAA,eAAAA,CAAgBJ,UAAAA,CAAAA;AACpC,IAAO,OAAA,IAAIwB,0BAAiBD,MAAAA,CAAAA;AAC9B;AACF","file":"calculator.cjs","sourcesContent":["/**\n * Copyright 2025 IBM Corp.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\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 * Waring: The CalculatorTool enbales the agent (and by proxy the user) to execute arbirtary\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"]}