"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExecuteJavascriptToolDefinition = exports.executeJavascriptResponseSchema = exports.executeJavascriptArgumentsSchema = void 0;
var zod_1 = require("zod");
var toolDefinition_js_1 = require("../toolDefinition.js");
var agentToolNames_js_1 = require("./agentToolNames.js");
exports.executeJavascriptArgumentsSchema = zod_1.z.object({
    description: zod_1.z.string().max(100).describe('A brief description of what this step does (max 100 characters)'),
    reasoning: zod_1.z
        .string()
        .optional()
        .describe('Brief explanation of why this action is being taken and what it aims to achieve'),
    snippet_name: zod_1.z.string().describe('A short descriptive name for this snippet'),
    snippet_description: zod_1.z.string().describe('Description of what this snippet does and why'),
    snippet_body: zod_1.z
        .string()
        .describe('The full snippet function body. Must follow the format: function mablJavaScriptStep(mablInputs, callback) { ... callback(result); }'),
    parameters: zod_1.z
        .record(zod_1.z.string(), zod_1.z.string())
        .optional()
        .describe('Optional input parameters for the snippet as a JSON object. Keys are parameter names and values are existing test variables that will be passed into the snippet. The snippet can access these via mablInputs. Values can reference variables using {{@variableName}} syntax.'),
    save_result_to_variable: zod_1.z
        .string()
        .optional()
        .describe('Optional variable name to save the snippet result to. Variable names should only contain alphanumeric characters and underscores.'),
    record_step: zod_1.z
        .boolean()
        .optional()
        .describe('Whether to save this snippet as a step in the test. Defaults to true. ' +
        'Set to false to run the snippet WITHOUT adding a step: it executes against ' +
        'the live page and its return value comes back in `result`, but nothing is ' +
        'persisted to the test. Use this to inspect the page or compute a value you ' +
        "need to make a decision (e.g. read an element's outerHTML to choose a robust " +
        'selector) instead of creating a snippet step and deleting it afterward. When ' +
        'record_step is false, do not set save_result_to_variable (there is no step to ' +
        'attach a variable to; read the value from `result`). Only meaningful during ' +
        'test authoring.'),
});
exports.executeJavascriptResponseSchema = zod_1.z.object({
    success: zod_1.z.boolean().describe('Whether the snippet executed successfully'),
    message: zod_1.z.string().optional().describe('Any message or error from the snippet execution'),
    result: zod_1.z.any().optional().describe('The return value from the snippet, if any'),
    new_screenshot: zod_1.z.string().optional().describe('Base64-encoded PNG screenshot of the page after snippet execution'),
    current_url: zod_1.z.string().optional().describe('The current URL after snippet execution'),
    variable_name: zod_1.z
        .string()
        .optional()
        .describe('The name of the variable that was created (if save_result_to_variable was specified)'),
    variable_value: zod_1.z.string().optional().describe('The actual value saved to the variable'),
});
exports.ExecuteJavascriptToolDefinition = (0, toolDefinition_js_1.defineTool)({
    name: agentToolNames_js_1.AgentToolNames.ExecuteJavascript,
    description: "Execute a JavaScript snippet in the browser. The snippet can be used to fix state issues during test recovery or to perform custom actions during test authoring.\n\nThe snippet_body must be a JavaScript function with the following signature:\n  function mablJavaScriptStep(mablInputs, callback) {\n    // Your code here\n    callback(result);\n  }\n\nRules:\n- The function MUST be named mablJavaScriptStep\n- The function MUST call callback(result) with a return value, or throw an error. The value passed to callback() is what is returned as the result of the snippet execution.\n- The snippet runs in the browser context (ECMAScript 8) with access to the DOM and browser APIs\n- If there is no meaningful return value, call callback(true)\n- Do not use require() or import statements\n- NEVER create a snippet that simply reads a variable from mablInputs and returns it via callback \u2014 this is a no-op. The variable already exists and can be referenced directly with {{@variableName}} syntax in other steps. Snippets must perform real work (DOM manipulation, computation, API calls, etc.).\n- When using the parameters argument, those values are passed directly as named function arguments after callback. List them in the function signature and use them directly \u2014 do NOT re-declare or re-initialize them inside the function body. Example: function mablJavaScriptStep(mablInputs, callback, userId, itemCount) { ... }\n\nmablInputs:\nmablInputs is a read-only object containing variables available in the test. Variable values accessed through mablInputs retain their original type (e.g. an API response body will still be an object, not a string).\n\nCommon mablInputs paths:\n- mablInputs.variables.user.variable_name - Test-generated variables, data-driven variables, and environment variables\n- mablInputs.variables.web.runtime.flow_run_ordinal_index - The loop index\n- mablInputs.variables.web.test_run_id - The test run ID\n- mablInputs.variables.web.defaults.credentials.username - mabl credentials username\n- mablInputs.variables.web.defaults.credentials.password - mabl credentials password\n\nCommon use cases:\n- Refresh the page: window.location.reload() then callback(true)\n- Clear local storage: localStorage.clear() then callback(true)\n- Read DOM elements\n- Access mabl test variables via mablInputs\n\nSaving Results:\n- If the snippet returns a value, you can save it to a variable for later use\n- Use save_result_to_variable to specify the variable name\n- The variable can then be referenced in subsequent steps using {{@variableName}} syntax\n\nInspecting without recording a step (test authoring):\n- When you only need to READ the page or compute a value to decide what to do next\n  (not perform a real test action), set record_step: false.\n- The snippet runs and its return value is returned in the 'result' field, but NO step\n  is added to the test. This is the right tool for inspection \u2014 do not create a snippet\n  step and then delete it.\n- Example: to read an element's HTML before choosing a robust selector,\n  call execute_javascript with record_step: false and a snippet whose callback returns\n  document.querySelector('a.edit').outerHTML, then read the 'result' field.\n- Do not combine record_step: false with save_result_to_variable. Read the value from\n  'result' instead; to persist a value as a variable, use record_step: true.\n\nExample snippet_body:\nfunction mablJavaScriptStep(mablInputs, callback) {\n  localStorage.clear();\n  sessionStorage.clear();\n  window.location.reload();\n  callback(true);\n}\n\nThe client will return a new screenshot after execution to show the updated page state.",
    argumentsSchema: exports.executeJavascriptArgumentsSchema,
    responseSchema: exports.executeJavascriptResponseSchema,
    // The snippet result is user-written JavaScript returning `z.any()` — it is
    // unbounded and can easily dump the full DOM (a `querySelectorAll('*')` +
    // `innerText` scan captures inline <script>/<style> source of every
    // ancestor). Cap the tool output at ~500 tokens so the model is forced to
    // aggregate on the client (counts, filtered summaries) rather than return
    // raw DOM/text.
    maxOutputChars: 2000,
});
//# sourceMappingURL=ExecuteJavascriptDefinition.js.map