"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.clickTool = void 0;
const ai_1 = require("ai");
const zod_1 = require("zod");
const coordinateNormalization_js_1 = require("../utils/coordinateNormalization.js");
const xpath_js_1 = require("../utils/xpath.js");
const screenshotHandler_js_1 = require("../utils/screenshotHandler.js");
const clickTool = (v3, provider) => (0, ai_1.tool)({
    description: "Click on an element using its coordinates (this is the most reliable way to click on an element, always use this over act, unless the element is not visible in the screenshot, but shown in ariaTree)",
    inputSchema: zod_1.z.object({
        describe: zod_1.z
            .string()
            .describe("Describe the element to click on in a short, specific phrase that mentions the element type and a good visual description"),
        coordinates: zod_1.z
            .array(zod_1.z.number())
            .describe("The (x, y) coordinates to click on"),
    }),
    execute: async ({ describe, coordinates }) => {
        try {
            const page = await v3.context.awaitActivePage();
            const processed = (0, coordinateNormalization_js_1.processCoordinates)(coordinates[0], coordinates[1], provider, v3);
            v3.logger({
                category: "agent",
                message: `Agent calling tool: click`,
                level: 1,
                auxiliary: {
                    arguments: {
                        value: JSON.stringify({ describe }),
                        type: "object",
                    },
                },
            });
            // Only request XPath when caching is enabled to avoid unnecessary computation
            const shouldCollectXpath = v3.isAgentReplayActive();
            const xpath = await page.click(processed.x, processed.y, {
                returnXpath: shouldCollectXpath,
            });
            const screenshotBase64 = await (0, screenshotHandler_js_1.waitAndCaptureScreenshot)(page);
            // Record as an "act" step with proper Action for deterministic replay (only when caching)
            if (shouldCollectXpath) {
                const normalizedXpath = (0, xpath_js_1.ensureXPath)(xpath);
                if (normalizedXpath) {
                    const action = {
                        selector: normalizedXpath,
                        description: describe,
                        method: "click",
                        arguments: [],
                    };
                    v3.recordAgentReplayStep({
                        type: "act",
                        instruction: describe,
                        actions: [action],
                        actionDescription: describe,
                    });
                }
            }
            return {
                success: true,
                describe,
                coordinates: [processed.x, processed.y],
                screenshotBase64,
            };
        }
        catch (error) {
            return {
                success: false,
                error: `Error clicking: ${error.message}`,
            };
        }
    },
    toModelOutput: (result) => {
        if (result.success) {
            const content = [
                {
                    type: "text",
                    text: JSON.stringify({
                        success: result.success,
                        describe: result.describe,
                        coordinates: result.coordinates,
                    }),
                },
            ];
            if (result.screenshotBase64) {
                content.push({
                    type: "media",
                    mediaType: "image/png",
                    data: result.screenshotBase64,
                });
            }
            return { type: "content", value: content };
        }
        return {
            type: "content",
            value: [
                {
                    type: "text",
                    text: JSON.stringify({
                        success: result.success,
                        error: result.error,
                    }),
                },
            ],
        };
    },
});
exports.clickTool = clickTool;
//# sourceMappingURL=click.js.map