{"version":3,"sources":["../../../../src/agents/requirement/utils/tool.ts"],"names":["runTool","tools","msg","context","isToolCallValid","ChatModelToolCallError","toolName","generatedContent","JSON","stringify","name","parameters","input","generatedError","result","tool","output","StringToolOutput","error","find","ToolError","run","toolCallMsg","Tool","contextKeys","Memory","state","memory","e","FrameworkError","ensure","FinalAnswerToolSchema","z","object","response","string","describe","FinalAnswerTool","description","instructions","customSchema","emitter","expectedOutput","undefined","ZodSchema","Emitter","root","child","namespace","creator","inputSchema","_run","_options","_ctx","answer","AssistantMessage"],"mappings":";;;;;;;;;;;;AAmCA,eAAsBA,OAAAA,CACpBC,KAAAA,EACAC,GAAAA,EACAC,OAAAA,EAA4B;AAE5B,EAAA,IAAI,CAACC,yBAAAA,CAAgBF,GAAAA,CAAAA,EAAM;AACzB,IAAA,MAAM,IAAIG,iCAAAA,CAAuB,CAAA,MAAA,EAASH,IAAII,QAAQ,CAAA,iBAAA,CAAA,EAAqB,EAAA,EAAI;AAC7EC,MAAAA,gBAAAA,EAAkBC,KAAKC,SAAAA,CAAU;AAAEC,QAAAA,IAAAA,EAAMR,GAAAA,CAAII,QAAAA;AAAUK,QAAAA,UAAAA,EAAYT,GAAAA,CAAIU;OAAM,CAAA;MAC7EC,cAAAA,EAAgB;KAClB,CAAA;AACF,EAAA;AAEA,EAAA,MAAMC,MAAAA,GAA+B;AACnCZ,IAAAA,GAAAA;IACAa,IAAAA,EAAM,IAAA;AACNH,IAAAA,KAAAA,EAAOV,GAAAA,CAAIU,KAAAA;IACXI,MAAAA,EAAQ,IAAIC,0BAAiB,EAAA,CAAA;IAC7BC,KAAAA,EAAO;AACT,GAAA;AAEA,EAAA,IAAI;AACFJ,IAAAA,MAAAA,CAAOC,IAAAA,GAAOd,MAAMkB,IAAAA,CAAK,CAACJ,SAASA,IAAAA,CAAKL,IAAAA,KAASR,GAAAA,CAAII,QAAQ,CAAA,IAAK,IAAA;AAClE,IAAA,IAAI,CAACQ,OAAOC,IAAAA,EAAM;AAChB,MAA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AACzD,IAAA;AAEA,IAAA,OAAO,IAAIjB,0BAAiB,uBAAA,CAAA;AAC9B,EAAA;AACF","file":"tool.cjs","sourcesContent":["/**\n * Copyright 2025 © BeeAI a Series of LF Projects, LLC\n * SPDX-License-Identifier: Apache-2.0\n */\n\nimport {\n  AnyTool,\n  BaseToolRunOptions,\n  StringToolOutput,\n  Tool,\n  ToolError,\n  ToolOutput,\n} from \"@/tools/base.js\";\nimport { ToolCallPart } from \"ai\";\nimport { FrameworkError } from \"@/errors.js\";\nimport { Emitter } from \"@/emitter/emitter.js\";\nimport { z, ZodSchema } from \"zod\";\nimport { RequirementAgentRunState } from \"@/agents/requirement/types.js\";\nimport { AssistantMessage } from \"@/backend/message.js\";\nimport { RunContext } from \"@/context.js\";\nimport { isToolCallValid } from \"@/adapters/vercel/backend/utils.js\";\nimport { ChatModelToolCallError } from \"@/backend/errors.js\";\n\n// Tool invocation result\nexport interface ToolInvocationResult {\n  msg: ToolCallPart;\n  tool: AnyTool | null;\n  input: unknown;\n  output: ToolOutput;\n  error: FrameworkError | null;\n}\n\n/**\n * Run a single tool with error handling\n */\nexport async function runTool(\n  tools: AnyTool[],\n  msg: ToolCallPart,\n  context: Record<string, any>,\n): Promise<ToolInvocationResult> {\n  if (!isToolCallValid(msg)) {\n    throw new ChatModelToolCallError(`Tool '${msg.toolName}' does not exist!`, [], {\n      generatedContent: JSON.stringify({ name: msg.toolName, parameters: msg.input }),\n      generatedError: \"The generated tool call is invalid. Cannot parse the args.\",\n    });\n  }\n\n  const result: ToolInvocationResult = {\n    msg,\n    tool: null,\n    input: msg.input,\n    output: new StringToolOutput(\"\"),\n    error: null,\n  };\n\n  try {\n    result.tool = tools.find((tool) => tool.name === msg.toolName) || null;\n    if (!result.tool) {\n      throw new ToolError(`Tool '${msg.toolName}' does not exist!`);\n    }\n\n    result.output = await result.tool.run(result.input).context({\n      ...context,\n      toolCallMsg: msg,\n      [Tool.contextKeys.Memory]: context.state.memory,\n    });\n  } catch (e) {\n    if (e instanceof ToolError) {\n      result.error = FrameworkError.ensure(e);\n    } else {\n      throw e;\n    }\n  }\n\n  return result;\n}\n\n// Final answer tool schema\nconst FinalAnswerToolSchema = z.object({\n  response: z.string().describe(\"The final answer to the user\"),\n});\n\n/**\n * Special tool for capturing final answers\n */\nexport class FinalAnswerTool extends Tool {\n  public readonly name = \"final_answer\";\n  public readonly description = \"Sends the final answer to the user\";\n  public instructions?: string;\n  public customSchema: boolean;\n  public readonly emitter: Emitter<any>;\n\n  protected expectedOutput: string | ZodSchema | null;\n  protected state: RequirementAgentRunState;\n\n  constructor(expectedOutput: string | ZodSchema | null, state: RequirementAgentRunState) {\n    super();\n    this.expectedOutput = expectedOutput;\n    this.state = state;\n    this.instructions = typeof expectedOutput === \"string\" ? expectedOutput : undefined;\n    this.customSchema = expectedOutput instanceof ZodSchema;\n    this.emitter = Emitter.root.child({\n      namespace: [\"tool\", \"final_answer\"],\n      creator: this,\n    });\n  }\n\n  inputSchema(): ZodSchema {\n    const expectedOutput = this.expectedOutput;\n\n    if (!expectedOutput) {\n      return FinalAnswerToolSchema;\n    } else if (expectedOutput instanceof ZodSchema) {\n      return expectedOutput;\n    } else if (typeof expectedOutput === \"string\") {\n      return z.object({\n        response: z.string().describe(expectedOutput),\n      });\n    } else {\n      return FinalAnswerToolSchema;\n    }\n  }\n\n  async _run(\n    input: any,\n    _options: BaseToolRunOptions,\n    _ctx: RunContext<typeof this>,\n  ): Promise<StringToolOutput> {\n    this.state.result = input;\n\n    if (this.expectedOutput instanceof ZodSchema) {\n      // For custom schemas, serialize the entire input\n      this.state.answer = new AssistantMessage(JSON.stringify(input));\n    } else {\n      // For string schemas, use the response field\n      this.state.answer = new AssistantMessage(input.response);\n    }\n\n    return new StringToolOutput(\"Message has been sent\");\n  }\n}\n"]}