"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.fillFormTool = void 0;
const ai_1 = require("ai");
const zod_1 = require("zod");
const fillFormTool = (v3, executionModel, variables) => {
    const hasVariables = variables && Object.keys(variables).length > 0;
    const valueDescription = hasVariables
        ? `Text to type into the target. Use %variableName% to substitute a variable value. Available: ${Object.keys(variables).join(", ")}`
        : "Text to type into the target";
    return (0, ai_1.tool)({
        description: `📝 FORM FILL - MULTI-FIELD INPUT TOOL\nFor any form with 2+ inputs/textareas. Faster than individual typing.`,
        inputSchema: zod_1.z.object({
            fields: zod_1.z
                .array(zod_1.z.object({
                action: zod_1.z
                    .string()
                    .describe('Description of typing action, e.g. "type foo into the email field"'),
                value: zod_1.z.string().describe(valueDescription),
            }))
                .min(1, "Provide at least one field to fill"),
        }),
        execute: async ({ fields }) => {
            v3.logger({
                category: "agent",
                message: `Agent calling tool: fillForm`,
                level: 1,
                auxiliary: {
                    arguments: {
                        value: JSON.stringify(fields),
                        type: "object",
                    },
                },
            });
            const instruction = `Return observation results for the following actions: ${fields
                .map((f) => f.action)
                .join(", ")}`;
            const observeOptions = executionModel
                ? { model: executionModel }
                : undefined;
            const observeResults = await v3.observe(instruction, observeOptions);
            const completed = [];
            const replayableActions = [];
            for (const res of observeResults) {
                const actOptions = variables ? { variables } : undefined;
                const actResult = await v3.act(res, actOptions);
                completed.push(actResult);
                if (Array.isArray(actResult.actions)) {
                    replayableActions.push(...actResult.actions);
                }
            }
            v3.recordAgentReplayStep({
                type: "fillForm",
                fields,
                observeResults,
                actions: replayableActions,
            });
            return {
                success: true,
                actions: completed,
                playwrightArguments: replayableActions,
            };
        },
    });
};
exports.fillFormTool = fillFormTool;
//# sourceMappingURL=fillform.js.map