import { UserInputError } from "./errors-D_R568zP.js";
import { logIssue, logWarn } from "./logging-DqqDTe02.js";
import { ApiClientError, ApiServerError } from "./errors-CQZ-QlOB.js";
import { z } from "zod";
import { tool } from "ai";

//#region src/internal/agents/tools/utils.ts
/**
* Standard response schema for all embedded agent tools.
* Tools return either an error message or the result data, never both.
*/
const AgentToolResponseSchema = z.object({
	error: z.string().optional().describe("Error message if the operation failed"),
	result: z.unknown().optional().describe("The successful result data")
});
/**
* Handles errors from agent tool execution and returns appropriate error messages.
*
* SECURITY: Only returns trusted error messages to prevent prompt injection.
* We trust: Sentry API errors, our own UserInputError messages, and system templates.
*/
function handleAgentToolError(error) {
	if (error instanceof UserInputError) {
		logWarn(error, {
			loggerScope: ["agent-tools", "user-input"],
			contexts: { agentTool: { errorType: "UserInputError" } }
		});
		return { error: `Input Error: ${error.message}. You may be able to resolve this by addressing the concern and trying again.` };
	}
	if (error instanceof ApiClientError) {
		const message = error.toUserMessage();
		logWarn(message, {
			loggerScope: ["agent-tools", "api-client"],
			contexts: { agentTool: {
				errorType: error.name,
				status: error.status ?? null
			} }
		});
		return { error: `Input Error: ${message}. You may be able to resolve this by addressing the concern and trying again.` };
	}
	if (error instanceof ApiServerError) {
		const eventId$1 = logIssue(error);
		const statusText = error.status ? ` (${error.status})` : "";
		return { error: `Server Error${statusText}: ${error.message}. Event ID: ${eventId$1}. This is a system error that cannot be resolved by retrying.` };
	}
	const eventId = logIssue(error);
	return { error: `System Error: An unexpected error occurred. Event ID: ${eventId}. This is a system error that cannot be resolved by retrying.` };
}
/**
* Creates an embedded agent tool with automatic error handling and schema wrapping.
*
* This wrapper:
* - Maintains the same API as the AI SDK's tool() function
* - Automatically wraps the result schema with error/result structure
* - Handles all error types and returns them as structured responses
* - Preserves type inference from the original tool implementation
*
* @example
* ```typescript
* export function createMyTool(apiService: SentryApiService) {
*   return agentTool({
*     description: "My tool description",
*     parameters: z.object({ param: z.string() }),
*     execute: async (params) => {
*       // Tool implementation that might throw errors
*       const result = await apiService.someMethod(params);
*       return result; // Original return type preserved
*     }
*   });
* }
* ```
*/
function agentTool(config) {
	return tool({
		description: config.description,
		parameters: config.parameters,
		execute: async (params) => {
			try {
				const result = await config.execute(params);
				return { result };
			} catch (error) {
				return handleAgentToolError(error);
			}
		}
	});
}

//#endregion
export { agentTool };
//# sourceMappingURL=utils-iC0Uutcf.js.map