{"version":3,"sources":["../../src/tools/custom.ts"],"names":["CustomToolCreateError","FrameworkError","CustomToolExecuteError","toolOptionsSchema","z","object","codeInterpreter","url","string","connectionOptions","any","optional","sourceCode","min","name","description","inputSchema","passthrough","CustomTool","Tool","emitter","Emitter","root","child","namespace","creator","options","register","validate","_run","input","run","result","callCodeInterpreter","body","tool_source_code","tool_input_json","JSON","stringify","env","merge","omitUndefined","signal","stderr","StringToolOutput","tool_output_json","e","ToolError","message","loadSnapshot","snapshot","fromSourceCode","error_messages","errorMessages","join","tool_name","tool_description","parse","tool_input_schema_json"],"mappings":";;;;;;;;;;;;;AAkCO,MAAMA,8BAA8BC,yBAAAA,CAAAA;EAlC3C;;;AAkC2D;AACpD,MAAMC,+BAA+BD,yBAAAA,CAAAA;EAnC5C;;;AAmC4D;AAE5D,MAAME,iBAAAA,GAAoBC,MACvBC,MAAO,CAAA;AACNC,EAAAA,eAAAA,EAAiBF,MAAEC,MAAO,CAAA;IACxBE,GAAKH,EAAAA,KAAAA,CAAEI,MAAM,EAAA,CAAGD,GAAG,EAAA;IACnBE,iBAAmBL,EAAAA,KAAAA,CAAEM,GAAG,EAAA,CAAGC,QAAQ;GACrC,CAAA;AACAC,EAAAA,UAAAA,EAAYR,KAAEI,CAAAA,MAAAA,EAASK,CAAAA,GAAAA,CAAI,CAAA,CAAA;AAC3BC,EAAAA,IAAAA,EAAMV,KAAEI,CAAAA,MAAAA,EAASK,CAAAA,GAAAA,CAAI,CAAA,CAAA;AACrBE,EAAAA,WAAAA,EAAaX,KAAEI,CAAAA,MAAAA,EAASK,CAAAA,GAAAA,CAAI,CAAA,CAAA;AAC5BG,EAAAA,WAAAA,EAAaZ,MAAEM,GAAG;AACpB,CAAA,EACCO,WAAW,EAAA;AASP,MAAMC,mBAAmBC,aAAAA,CAAAA;EAzDhC;;;AA0DEL,EAAAA,IAAAA;AACAC,EAAAA,WAAAA;EAEgBK,OAA0DC,GAAAA,mBAAAA,CAAQC,KAAKC,KAAM,CAAA;I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OAAS,EAAA;AAACF,UAAAA;AAAE,SAAA,CAAA;OACzC,MAAA;AACL,QAAMA,MAAAA,CAAAA;AACR;AACF;AACF;AACF","file":"custom.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 {\n  BaseToolOptions,\n  BaseToolRunOptions,\n  ToolEmitter,\n  StringToolOutput,\n  Tool,\n  ToolInput,\n} from \"@/tools/base.js\";\nimport { FrameworkError } from \"@/errors.js\";\nimport { z } from \"zod\";\nimport { validate } from \"@/internals/helpers/general.js\";\nimport { callCodeInterpreter, CodeInterpreterOptions } from \"./python/python.js\";\nimport { RunContext } from \"@/context.js\";\nimport { Emitter } from \"@/emitter/emitter.js\";\nimport { ToolError } from \"@/tools/base.js\";\nimport { merge } from \"remeda\";\nimport { omitUndefined } from \"@/internals/helpers/object.js\";\n\nexport class CustomToolCreateError extends FrameworkError {}\nexport class CustomToolExecuteError extends FrameworkError {}\n\nconst toolOptionsSchema = z\n  .object({\n    codeInterpreter: z.object({\n      url: z.string().url(),\n      connectionOptions: z.any().optional(),\n    }),\n    sourceCode: z.string().min(1),\n    name: z.string().min(1),\n    description: z.string().min(1),\n    inputSchema: z.any(),\n  })\n  .passthrough();\n\ntype CustomToolEnv = Record<string, string | boolean | null | undefined | number>;\n\nexport type CustomToolOptions = z.output<typeof toolOptionsSchema> &\n  BaseToolOptions & { env?: CustomToolEnv };\n\nexport type CustomToolRunOptions = Pick<CustomToolOptions, \"env\"> & BaseToolRunOptions;\n\nexport class CustomTool extends Tool<StringToolOutput, CustomToolOptions, CustomToolRunOptions> {\n  name: string;\n  description: string;\n\n  public readonly emitter: ToolEmitter<ToolInput<this>, StringToolOutput> = Emitter.root.child({\n    namespace: [\"tool\", \"custom\"],\n    creator: this,\n  });\n\n  public inputSchema() {\n    return this.options.inputSchema;\n  }\n\n  static {\n    this.register();\n  }\n\n  public constructor(options: CustomToolOptions) {\n    validate(options, toolOptionsSchema);\n    super(options);\n    this.name = options.name;\n    this.description = options.description;\n  }\n\n  protected async _run(\n    input: any,\n    options: Partial<CustomToolRunOptions>,\n    run: RunContext<typeof this>,\n  ) {\n    try {\n      const result = await callCodeInterpreter({\n        url: `${this.options.codeInterpreter.url}/v1/execute-custom-tool`,\n        body: {\n          tool_source_code: this.options.sourceCode,\n          tool_input_json: JSON.stringify(input),\n          env: merge(omitUndefined(this.options.env ?? {}), omitUndefined(options.env ?? {})),\n        },\n        signal: run.signal,\n      });\n\n      if (result.stderr) {\n        throw new CustomToolExecuteError(result.stderr);\n      }\n\n      return new StringToolOutput(result.tool_output_json);\n    } catch (e) {\n      if (e instanceof ToolError) {\n        throw new CustomToolExecuteError(e.message, [e]);\n      } else {\n        throw e;\n      }\n    }\n  }\n\n  loadSnapshot(snapshot: ReturnType<typeof this.createSnapshot>): void {\n    super.loadSnapshot(snapshot);\n  }\n\n  static async fromSourceCode(\n    { env, ...codeInterpreter }: CodeInterpreterOptions & Pick<CustomToolOptions, \"env\">,\n    sourceCode: string,\n  ) {\n    try {\n      const result = await callCodeInterpreter({\n        url: `${codeInterpreter.url}/v1/parse-custom-tool`,\n        body: {\n          tool_source_code: sourceCode,\n        },\n      });\n\n      if (result.error_messages) {\n        throw new CustomToolCreateError(result.errorMessages.join(\"\\n\"));\n      }\n\n      return new CustomTool({\n        codeInterpreter,\n        sourceCode,\n        name: result.tool_name,\n        description: result.tool_description,\n        inputSchema: JSON.parse(result.tool_input_schema_json),\n        env,\n      });\n    } catch (e) {\n      if (e instanceof ToolError) {\n        throw new CustomToolCreateError(e.message, [e]);\n      } else {\n        throw e;\n      }\n    }\n  }\n}\n"]}