{"version":3,"file":"agent/tool-manager.mjs","sources":["webpack://@multimodal/agent/./src/agent/tool-manager.ts"],"sourcesContent":["/*\n * Copyright (c) 2025 Bytedance, Inc. and its affiliates.\n * SPDX-License-Identifier: Apache-2.0\n */\n\nimport { Tool } from '@multimodal/agent-interface';\nimport { ConsoleLogger } from '@agent-infra/logger';\n\n/**\n * Manages tools for the Agent, handling registration, lookup, and execution\n */\nexport class ToolManager {\n  private tools: Map<string, Tool> = new Map();\n\n  constructor(private logger: ConsoleLogger) {}\n\n  /**\n   * Registers a new tool that the agent can use during execution\n   */\n  registerTool(tool: Tool): void {\n    this.logger.info(`[Tool] Registered: ${tool.name} | Description: \"${tool.description}\"`);\n    if (tool.schema.type === 'object' && !tool.schema.properties) {\n      tool.schema.properties = {};\n    }\n    this.tools.set(tool.name, tool);\n  }\n\n  /**\n   * Returns all registered tools as an array\n   */\n  getTools(): Tool[] {\n    return Array.from(this.tools.values());\n  }\n\n  /**\n   * Gets a specific tool by name\n   * @param name Tool name to retrieve\n   * @returns The tool definition or undefined if not found\n   */\n  getTool(name: string): Tool | undefined {\n    return this.tools.get(name);\n  }\n\n  /**\n   * Checks if a tool exists\n   * @param name Tool name to check\n   * @returns True if the tool exists\n   */\n  hasTool(name: string): boolean {\n    return this.tools.has(name);\n  }\n\n  /**\n   * Execute a tool with the given arguments\n   * @param toolName Name of the tool to execute\n   * @param toolCallId ID of the tool call\n   * @param args Arguments to pass to the tool\n   * @returns Result of execution and execution time\n   */\n  async executeTool(\n    toolName: string,\n    toolCallId: string,\n    args: unknown,\n  ): Promise<{\n    result: unknown;\n    executionTime: number;\n    error?: string;\n  }> {\n    const tool = this.tools.get(toolName);\n\n    if (!tool) {\n      const errorMessage = `Tool \"${toolName}\" not found`;\n      this.logger.error(`[Tool] Not found: \"${toolName}\"`);\n      return {\n        result: `Error: ${errorMessage}`,\n        executionTime: 0,\n        error: errorMessage,\n      };\n    }\n\n    try {\n      this.logger.info(`[Tool] Executing: \"${toolName}\" | ToolCallId: ${toolCallId}`);\n      this.logger.debug(`[Tool] Arguments: ${JSON.stringify(args)}`);\n\n      const startTime = Date.now();\n      const result = await tool.function(args);\n      const executionTime = Date.now() - startTime;\n\n      this.logger.info(\n        `[Tool] Execution completed: \"${toolName}\" | Duration: ${executionTime}ms | ToolCallId: ${toolCallId}`,\n      );\n      this.logger.debug(\n        `[Tool] Result: ${typeof result === 'string' ? result : JSON.stringify(result)}`,\n      );\n\n      return {\n        result,\n        executionTime,\n      };\n    } catch (error) {\n      const errorMessage = String(error);\n      this.logger.error(\n        `[Tool] Execution failed: \"${toolName}\" | Error: ${errorMessage} | ToolCallId: ${toolCallId}`,\n      );\n\n      return {\n        result: `Error: ${errorMessage}`,\n        executionTime: 0,\n        error: errorMessage,\n      };\n    }\n  }\n}\n"],"names":["ToolManager","tool","Array","name","toolName","toolCallId","args","errorMessage","JSON","startTime","Date","result","executionTime","error","String","logger","Map"],"mappings":";;;;AAGC;;;;;;;;;;AAQM,MAAMA;IAQX,aAAaC,IAAU,EAAQ;QAC7B,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,mBAAmB,EAAEA,KAAK,IAAI,CAAC,iBAAiB,EAAEA,KAAK,WAAW,CAAC,CAAC,CAAC;QACvF,IAAIA,AAAqB,aAArBA,KAAK,MAAM,CAAC,IAAI,IAAiB,CAACA,KAAK,MAAM,CAAC,UAAU,EAC1DA,KAAK,MAAM,CAAC,UAAU,GAAG,CAAC;QAE5B,IAAI,CAAC,KAAK,CAAC,GAAG,CAACA,KAAK,IAAI,EAAEA;IAC5B;IAKA,WAAmB;QACjB,OAAOC,MAAM,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM;IACrC;IAOA,QAAQC,IAAY,EAAoB;QACtC,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAACA;IACxB;IAOA,QAAQA,IAAY,EAAW;QAC7B,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAACA;IACxB;IASA,MAAM,YACJC,QA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