'use strict';

var chunkPWHXCQ22_cjs = require('./chunk-PWHXCQ22.cjs');
var chunkXMFSD56J_cjs = require('./chunk-XMFSD56J.cjs');
var chunk2BIE3CHY_cjs = require('./chunk-2BIE3CHY.cjs');
var chunkZKMVCV4G_cjs = require('./chunk-ZKMVCV4G.cjs');
var chunkMCYD5LW7_cjs = require('./chunk-MCYD5LW7.cjs');
var chunkSUKFO7UM_cjs = require('./chunk-SUKFO7UM.cjs');
var chunkK52BGIHQ_cjs = require('./chunk-K52BGIHQ.cjs');
var chunk64ITUOXI_cjs = require('./chunk-64ITUOXI.cjs');
var chunkO7I5CWRX_cjs = require('./chunk-O7I5CWRX.cjs');
var tools = require('@mastra/core/tools');
var zodToJson = require('@mastra/core/utils/zod-to-json');

// src/server/handlers/tools.ts
var tools_exports = {};
chunkO7I5CWRX_cjs.__export(tools_exports, {
  EXECUTE_AGENT_TOOL_ROUTE: () => EXECUTE_AGENT_TOOL_ROUTE,
  EXECUTE_TOOL_ROUTE: () => EXECUTE_TOOL_ROUTE,
  GET_AGENT_TOOL_ROUTE: () => GET_AGENT_TOOL_ROUTE,
  GET_TOOL_BY_ID_ROUTE: () => GET_TOOL_BY_ID_ROUTE,
  LIST_TOOLS_ROUTE: () => LIST_TOOLS_ROUTE
});
var LIST_TOOLS_ROUTE = chunkK52BGIHQ_cjs.createRoute({
  method: "GET",
  path: "/tools",
  responseType: "json",
  responseSchema: chunkXMFSD56J_cjs.listToolsResponseSchema,
  summary: "List all tools",
  description: "Returns a list of all available tools in the system",
  tags: ["Tools"],
  requiresAuth: true,
  handler: async ({ mastra, registeredTools }) => {
    try {
      const allTools = registeredTools && Object.keys(registeredTools).length > 0 ? registeredTools : mastra.listTools() || {};
      const serializedTools = Object.entries(allTools).reduce(
        (acc, [id, _tool]) => {
          const tool = _tool;
          acc[id] = {
            ...tool,
            inputSchema: tool.inputSchema ? chunk2BIE3CHY_cjs.stringify(zodToJson.zodToJsonSchema(tool.inputSchema)) : void 0,
            outputSchema: tool.outputSchema ? chunk2BIE3CHY_cjs.stringify(zodToJson.zodToJsonSchema(tool.outputSchema)) : void 0,
            requestContextSchema: tool.requestContextSchema ? chunk2BIE3CHY_cjs.stringify(zodToJson.zodToJsonSchema(tool.requestContextSchema)) : void 0
          };
          return acc;
        },
        {}
      );
      return serializedTools;
    } catch (error) {
      return chunkSUKFO7UM_cjs.handleError(error, "Error getting tools");
    }
  }
});
var GET_TOOL_BY_ID_ROUTE = chunkK52BGIHQ_cjs.createRoute({
  method: "GET",
  path: "/tools/:toolId",
  responseType: "json",
  pathParamSchema: chunkXMFSD56J_cjs.toolIdPathParams,
  responseSchema: chunkXMFSD56J_cjs.serializedToolSchema,
  summary: "Get tool by ID",
  description: "Returns details for a specific tool including its schema and configuration",
  tags: ["Tools"],
  requiresAuth: true,
  handler: async ({ mastra, registeredTools, toolId }) => {
    try {
      let tool;
      if (registeredTools && Object.keys(registeredTools).length > 0) {
        tool = Object.values(registeredTools).find((t) => t.id === toolId);
      } else {
        tool = mastra.getToolById(toolId);
      }
      if (!tool) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: "Tool not found" });
      }
      const serializedTool = {
        ...tool,
        inputSchema: tool.inputSchema ? chunk2BIE3CHY_cjs.stringify(zodToJson.zodToJsonSchema(tool.inputSchema)) : void 0,
        outputSchema: tool.outputSchema ? chunk2BIE3CHY_cjs.stringify(zodToJson.zodToJsonSchema(tool.outputSchema)) : void 0,
        requestContextSchema: tool.requestContextSchema ? chunk2BIE3CHY_cjs.stringify(zodToJson.zodToJsonSchema(tool.requestContextSchema)) : void 0
      };
      return serializedTool;
    } catch (error) {
      return chunkSUKFO7UM_cjs.handleError(error, "Error getting tool");
    }
  }
});
var EXECUTE_TOOL_ROUTE = chunkK52BGIHQ_cjs.createRoute({
  method: "POST",
  path: "/tools/:toolId/execute",
  responseType: "json",
  pathParamSchema: chunkXMFSD56J_cjs.toolIdPathParams,
  queryParamSchema: chunkMCYD5LW7_cjs.optionalRunIdSchema,
  bodySchema: chunkXMFSD56J_cjs.executeToolContextBodySchema,
  responseSchema: chunkXMFSD56J_cjs.executeToolResponseSchema,
  summary: "Execute tool",
  description: "Executes a specific tool with the provided input data",
  tags: ["Tools"],
  requiresAuth: true,
  handler: async ({ mastra, runId, toolId, registeredTools, requestContext, ...bodyParams }) => {
    try {
      if (!toolId) {
        throw new chunk64ITUOXI_cjs.HTTPException(400, { message: "Tool ID is required" });
      }
      let tool;
      if (registeredTools && Object.keys(registeredTools).length > 0) {
        tool = Object.values(registeredTools).find((t) => t.id === toolId);
      } else {
        tool = mastra.getToolById(toolId);
      }
      if (!tool) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: "Tool not found" });
      }
      if (!tool?.execute) {
        throw new chunk64ITUOXI_cjs.HTTPException(400, { message: "Tool is not executable" });
      }
      const { data } = bodyParams;
      chunkZKMVCV4G_cjs.validateBody({ data });
      let result;
      if (tools.isVercelTool(tool)) {
        result = await tool.execute(data);
      } else {
        result = await tool.execute(data, {
          mastra,
          requestContext,
          // TODO: Pass proper tracing context when server API supports tracing
          tracingContext: { currentSpan: void 0 },
          ...runId ? {
            workflow: {
              runId,
              suspend: async () => {
              }
            }
          } : {}
        });
      }
      return result;
    } catch (error) {
      return chunkSUKFO7UM_cjs.handleError(error, "Error executing tool");
    }
  }
});
var GET_AGENT_TOOL_ROUTE = chunkK52BGIHQ_cjs.createRoute({
  method: "GET",
  path: "/agents/:agentId/tools/:toolId",
  responseType: "json",
  pathParamSchema: chunkXMFSD56J_cjs.agentToolPathParams,
  responseSchema: chunkXMFSD56J_cjs.serializedToolSchema,
  summary: "Get agent tool",
  description: "Returns details for a specific tool assigned to the agent",
  tags: ["Agents", "Tools"],
  requiresAuth: true,
  handler: async ({ mastra, agentId, toolId, requestContext }) => {
    try {
      if (!agentId) {
        throw new chunk64ITUOXI_cjs.HTTPException(400, { message: "Agent ID is required" });
      }
      const agent = await chunkPWHXCQ22_cjs.getAgentFromSystem({ mastra, agentId });
      const agentTools = await agent.listTools({ requestContext });
      const tool = Object.values(agentTools || {}).find((tool2) => tool2.id === toolId);
      if (!tool) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: "Tool not found" });
      }
      const serializedTool = {
        ...tool,
        inputSchema: tool.inputSchema ? chunk2BIE3CHY_cjs.stringify(zodToJson.zodToJsonSchema(tool.inputSchema)) : void 0,
        outputSchema: tool.outputSchema ? chunk2BIE3CHY_cjs.stringify(zodToJson.zodToJsonSchema(tool.outputSchema)) : void 0,
        requestContextSchema: tool.requestContextSchema ? chunk2BIE3CHY_cjs.stringify(zodToJson.zodToJsonSchema(tool.requestContextSchema)) : void 0
      };
      return serializedTool;
    } catch (error) {
      return chunkSUKFO7UM_cjs.handleError(error, "Error getting agent tool");
    }
  }
});
var EXECUTE_AGENT_TOOL_ROUTE = chunkK52BGIHQ_cjs.createRoute({
  method: "POST",
  path: "/agents/:agentId/tools/:toolId/execute",
  responseType: "json",
  pathParamSchema: chunkXMFSD56J_cjs.agentToolPathParams,
  bodySchema: chunkXMFSD56J_cjs.executeToolBodySchema,
  responseSchema: chunkXMFSD56J_cjs.executeToolResponseSchema,
  summary: "Execute agent tool",
  description: "Executes a specific tool assigned to the agent with the provided input data",
  tags: ["Agents", "Tools"],
  requiresAuth: true,
  handler: async ({ mastra, agentId, toolId, data, requestContext }) => {
    try {
      if (!agentId) {
        throw new chunk64ITUOXI_cjs.HTTPException(400, { message: "Agent ID is required" });
      }
      const agent = await chunkPWHXCQ22_cjs.getAgentFromSystem({ mastra, agentId });
      const agentTools = await agent.listTools({ requestContext });
      const tool = Object.values(agentTools || {}).find((tool2) => tool2.id === toolId);
      if (!tool) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: "Tool not found" });
      }
      if (!tool?.execute) {
        throw new chunk64ITUOXI_cjs.HTTPException(400, { message: "Tool is not executable" });
      }
      const result = await tool.execute(data, {
        mastra,
        requestContext,
        // TODO: Pass proper tracing context when server API supports tracing
        tracingContext: { currentSpan: void 0 }
      });
      return result;
    } catch (error) {
      return chunkSUKFO7UM_cjs.handleError(error, "Error executing agent tool");
    }
  }
});

exports.EXECUTE_AGENT_TOOL_ROUTE = EXECUTE_AGENT_TOOL_ROUTE;
exports.EXECUTE_TOOL_ROUTE = EXECUTE_TOOL_ROUTE;
exports.GET_AGENT_TOOL_ROUTE = GET_AGENT_TOOL_ROUTE;
exports.GET_TOOL_BY_ID_ROUTE = GET_TOOL_BY_ID_ROUTE;
exports.LIST_TOOLS_ROUTE = LIST_TOOLS_ROUTE;
exports.tools_exports = tools_exports;
//# sourceMappingURL=chunk-7FTAELTM.cjs.map
//# sourceMappingURL=chunk-7FTAELTM.cjs.map