import z from "zod";
import { createToolFactory, ToolCategory } from "../tools2/types.js";
export const buildGetRuntimeErrorsToolFactory = createToolFactory("get_runtime_errors", ({ clark }) => {
    return {
        category: ToolCategory.DEBUG,
        description: `
      Retrieve runtime errors from the user's application.

      Captures React component errors, JavaScript exceptions, unhandled promise rejections,
      and global errors that occurred during the current session.

      Use this tool to: debug runtime errors, investigate exceptions, or understand what went wrong in the user's app.

      Returns an array of error objects with:
      - id: unique identifier for the error
      - error: error message string
      - errorInfo: React error info with componentStack
      - identifier: component source info (sourceId, instanceId) where error occurred
      - timestamp: when the error was captured (milliseconds since epoch)
      - generationTimestamp: ISO timestamp of when error was captured

      Errors are sorted by timestamp (most recent first) and retained for 10 minutes.

      Optional filtering:
      - limit: Maximum number of errors to return (default: all errors). Use this to get the most recent N errors.

      Returns:
      - errors: Array of error objects (most recent first)
      - count: Number of errors returned
      - totalCount: Total number of errors in buffer

      Note: Errors older than 10 minutes are automatically removed.
      The buffer holds up to 128KB total of error data.
    `,
        inputSchema: z.object({
            limit: z
                .number()
                .positive()
                .optional()
                .describe("Maximum number of errors to return (most recent first). If not specified, returns all errors."),
        }),
        execute: async ({ limit }) => {
            const allErrors = clark.context.runtimeErrors ?? [];
            // Sort by timestamp descending (most recent first)
            let errors = allErrors.sort((a, b) => (b.timestamp ?? 0) - (a.timestamp ?? 0));
            // Apply limit if specified
            if (limit !== undefined) {
                errors = errors.slice(0, limit);
            }
            return {
                errors,
                count: errors.length,
                totalCount: allErrors.length,
            };
        },
        readOnly: true,
        getSummary: (_input, output) => {
            const count = output.count ?? 0;
            return { output: `${count} error${count === 1 ? "" : "s"}` };
        },
    };
});
//# sourceMappingURL=get-runtime-errors.js.map