"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.toolExecutors = void 0;
exports.getExecutor = getExecutor;
exports.toolNeedsPostExecutionContext = toolNeedsPostExecutionContext;
exports.getExecutorByAuthoringName = getExecutorByAuthoringName;
const mablApi_1 = require("../../mablApi");
const RecordClickExecutor_1 = require("./toolExecutors/RecordClickExecutor");
const RecordEnterAuthCodeExecutor_1 = require("./toolExecutors/RecordEnterAuthCodeExecutor");
const RecordEnterTextExecutor_1 = require("./toolExecutors/RecordEnterTextExecutor");
const RecordAssertionExecutor_1 = require("./toolExecutors/RecordAssertionExecutor");
const RecordCountElementsExecutor_1 = require("./toolExecutors/RecordCountElementsExecutor");
const RecordCreateVariableExecutor_1 = require("./toolExecutors/RecordCreateVariableExecutor");
const WaitExecutor_1 = require("./toolExecutors/WaitExecutor");
const ScrollExecutor_1 = require("./toolExecutors/ScrollExecutor");
const RecordFlowExecutor_1 = require("./toolExecutors/RecordFlowExecutor");
const RecordAssertElementExecutor_1 = require("./toolExecutors/RecordAssertElementExecutor");
const VisitUrlExecutor_1 = require("./toolExecutors/VisitUrlExecutor");
const RecordPreexistingSnippetExecutor_1 = require("./toolExecutors/RecordPreexistingSnippetExecutor");
const RecordHoverExecutor_1 = require("./toolExecutors/RecordHoverExecutor");
const RecordClickAndHoldExecutor_1 = require("./toolExecutors/RecordClickAndHoldExecutor");
const RecordReleaseExecutor_1 = require("./toolExecutors/RecordReleaseExecutor");
const SwitchTabExecutor_1 = require("./toolExecutors/SwitchTabExecutor");
const StartStepGroupExecutor_1 = require("./toolExecutors/StartStepGroupExecutor");
const EndTestExecutor_1 = require("./toolExecutors/EndTestExecutor");
const PlayStepsExecutor_1 = require("./toolExecutors/PlayStepsExecutor");
const MoveCursorExecutor_1 = require("./toolExecutors/MoveCursorExecutor");
const CreateMablMailboxExecutor_1 = require("./toolExecutors/CreateMablMailboxExecutor");
const OpenMablEmailExecutor_1 = require("./toolExecutors/OpenMablEmailExecutor");
const OpenPdfExecutor_1 = require("./toolExecutors/OpenPdfExecutor");
const ValidateTestExecutor_1 = require("./toolExecutors/ValidateTestExecutor");
const MoveStepsExecutor_1 = require("./toolExecutors/MoveStepsExecutor");
const DeleteStepsFlexExecutor_1 = require("./toolExecutors/DeleteStepsFlexExecutor");
const ReloadPageExecutor_1 = require("./toolExecutors/ReloadPageExecutor");
const RecordCreateSnippetExecutor_1 = require("./toolExecutors/RecordCreateSnippetExecutor");
const RecordAssertVariableExecutor_1 = require("./toolExecutors/RecordAssertVariableExecutor");
const RecordEchoExecutor_1 = require("./toolExecutors/RecordEchoExecutor");
const RecordAssertUrlExecutor_1 = require("./toolExecutors/RecordAssertUrlExecutor");
const AssertDownloadExecutor_1 = require("./toolExecutors/AssertDownloadExecutor");
const GetRecentDownloadExecutor_1 = require("./toolExecutors/GetRecentDownloadExecutor");
const SetViewportExecutor_1 = require("./toolExecutors/SetViewportExecutor");
const DatabaseQueryExecutor_1 = require("./toolExecutors/DatabaseQueryExecutor");
const RecordElseExecutor_1 = require("./toolExecutors/RecordElseExecutor");
const RecordEndIfExecutor_1 = require("./toolExecutors/RecordEndIfExecutor");
const SendHttpRequestExecutor_1 = require("./toolExecutors/SendHttpRequestExecutor");
const ClearCookiesExecutor_1 = require("./toolExecutors/ClearCookiesExecutor");
const RemoveCookieExecutor_1 = require("./toolExecutors/RemoveCookieExecutor");
const SetCookieExecutor_1 = require("./toolExecutors/SetCookieExecutor");
const RecordAssertCookieExecutor_1 = require("./toolExecutors/RecordAssertCookieExecutor");
const AccessibilityCheckExecutor_1 = require("./toolExecutors/AccessibilityCheckExecutor");
const RecordWaitUntilExecutor_1 = require("./toolExecutors/RecordWaitUntilExecutor");
const TestAssertStepExecutor_1 = require("./toolExecutors/TestAssertStepExecutor");
const UploadFileExecutor_1 = require("./toolExecutors/UploadFileExecutor");
const GET_RECENT_DOWNLOAD = 'get_recent_download';
exports.toolExecutors = {
    [mablApi_1.ExecuteToolName.AskForHelp]: {
        needsFind: () => false,
        getStepType: () => 'ask-for-help',
        execute: () => Promise.resolve({
            success: true,
            errorMessage: undefined,
        }),
    },
    [mablApi_1.ExecuteToolName.Click]: RecordClickExecutor_1.recordClickExecutor,
    [mablApi_1.ExecuteToolName.ClickAndHold]: RecordClickAndHoldExecutor_1.recordClickAndHoldExecutor,
    [mablApi_1.ExecuteToolName.Release]: RecordReleaseExecutor_1.recordReleaseExecutor,
    [mablApi_1.ExecuteToolName.EnterAuthCode]: RecordEnterAuthCodeExecutor_1.recordEnterAuthCodeExecutor,
    [mablApi_1.ExecuteToolName.EnterText]: RecordEnterTextExecutor_1.recordEnterTextExecutor,
    [mablApi_1.ExecuteToolName.Assert]: RecordAssertionExecutor_1.recordAssertionExecutor,
    [mablApi_1.ExecuteToolName.CountElements]: RecordCountElementsExecutor_1.recordCountElementsExecutor,
    [mablApi_1.ExecuteToolName.CreateOrSetVariable]: RecordCreateVariableExecutor_1.recordCreateVariableExecutor,
    [mablApi_1.ExecuteToolName.Wait]: WaitExecutor_1.waitExecutor,
    [mablApi_1.ExecuteToolName.Scroll]: ScrollExecutor_1.scrollExecutor,
    [mablApi_1.ExecuteToolName.Flow]: RecordFlowExecutor_1.recordFlowExecutor,
    [mablApi_1.ExecuteToolName.AssertElement]: RecordAssertElementExecutor_1.recordAssertElementExecutor,
    [mablApi_1.ExecuteToolName.VisitUrl]: VisitUrlExecutor_1.visitUrlExecutor,
    [mablApi_1.ExecuteToolName.PreexistingSnippet]: RecordPreexistingSnippetExecutor_1.recordPreexistingSnippetExecutor,
    [mablApi_1.ExecuteToolName.Javascript]: RecordCreateSnippetExecutor_1.recordCreateSnippetExecutor,
    [mablApi_1.ExecuteToolName.Snippet]: RecordPreexistingSnippetExecutor_1.recordPreexistingSnippetExecutor,
    [mablApi_1.ExecuteToolName.Hover]: RecordHoverExecutor_1.recordHoverExecutor,
    [mablApi_1.ExecuteToolName.SwitchTab]: SwitchTabExecutor_1.switchTabExecutor,
    [mablApi_1.ExecuteToolName.StartStepGroup]: StartStepGroupExecutor_1.startStepGroupExecutor,
    [mablApi_1.ExecuteToolName.EndTest]: EndTestExecutor_1.endTestExecutor,
    [mablApi_1.ExecuteToolName.PlaySteps]: PlayStepsExecutor_1.playStepsExecutor,
    [mablApi_1.ExecuteToolName.MoveCursor]: MoveCursorExecutor_1.moveCursorExecutor,
    [mablApi_1.ExecuteToolName.CreateMablMailbox]: CreateMablMailboxExecutor_1.createMablMailboxExecutor,
    [mablApi_1.ExecuteToolName.OpenMablEmail]: OpenMablEmailExecutor_1.openMablEmailExecutor,
    [mablApi_1.ExecuteToolName.OpenPdf]: OpenPdfExecutor_1.openPdfExecutor,
    [mablApi_1.ExecuteToolName.MoveSteps]: MoveStepsExecutor_1.moveStepsExecutor,
    [mablApi_1.ExecuteToolName.DeleteSteps]: DeleteStepsFlexExecutor_1.deleteStepsFlexExecutor,
    [mablApi_1.ExecuteToolName.ReloadPage]: ReloadPageExecutor_1.reloadPageExecutor,
    [mablApi_1.ExecuteToolName.AssertVariable]: RecordAssertVariableExecutor_1.recordAssertVariableExecutor,
    [mablApi_1.ExecuteToolName.ValidateTest]: ValidateTestExecutor_1.validateTestExecutor,
    [mablApi_1.ExecuteToolName.Echo]: RecordEchoExecutor_1.recordEchoExecutor,
    [mablApi_1.ExecuteToolName.AssertUrl]: RecordAssertUrlExecutor_1.recordAssertUrlExecutor,
    [mablApi_1.ExecuteToolName.AssertDownload]: AssertDownloadExecutor_1.assertDownloadExecutor,
    [GET_RECENT_DOWNLOAD]: GetRecentDownloadExecutor_1.getRecentDownloadExecutor,
    [mablApi_1.ExecuteToolName.UploadFile]: UploadFileExecutor_1.uploadFileExecutor,
    [mablApi_1.ExecuteToolName.SetViewport]: SetViewportExecutor_1.setViewportExecutor,
    [mablApi_1.ExecuteToolName.DatabaseQuery]: DatabaseQueryExecutor_1.databaseQueryExecutor,
    [mablApi_1.ExecuteToolName.Else]: RecordElseExecutor_1.recordElseExecutor,
    [mablApi_1.ExecuteToolName.EndIf]: RecordEndIfExecutor_1.recordEndIfExecutor,
    [mablApi_1.ExecuteToolName.SendHttpRequest]: SendHttpRequestExecutor_1.sendHttpRequestExecutor,
    [mablApi_1.ExecuteToolName.ClearCookies]: ClearCookiesExecutor_1.clearCookiesExecutor,
    [mablApi_1.ExecuteToolName.RemoveCookie]: RemoveCookieExecutor_1.removeCookieExecutor,
    [mablApi_1.ExecuteToolName.SetCookie]: SetCookieExecutor_1.setCookieExecutor,
    [mablApi_1.ExecuteToolName.AssertCookie]: RecordAssertCookieExecutor_1.recordAssertCookieExecutor,
    [mablApi_1.ExecuteToolName.AccessibilityCheck]: AccessibilityCheckExecutor_1.accessibilityCheckExecutor,
    [mablApi_1.ExecuteToolName.WaitUntil]: RecordWaitUntilExecutor_1.recordWaitUntilExecutor,
    [mablApi_1.ExecuteToolName.TestAssertStep]: TestAssertStepExecutor_1.testAssertStepExecutor,
};
async function executeToolWithTracking(executor, payload, stateProvider, eventHandler, logger) {
    const stepType = payload.toolCall.toolName;
    try {
        eventHandler.trackAnalyticsEvent('agent_tool_executor_used', {
            stepType: stepType || 'unknown',
        });
        const result = await executor.execute(payload, stateProvider, eventHandler, logger);
        if (!result.success) {
            eventHandler.trackAnalyticsEvent('agent_tool_executor_failed', {
                stepType: stepType || 'unknown',
                errorMessage: result.errorMessage || 'Unknown error',
                errorCause: result.errorCause || 'Unknown cause',
            });
        }
        return result;
    }
    catch (error) {
        eventHandler.trackAnalyticsEvent('agent_tool_executor_failed', {
            stepType: stepType || 'unknown',
            errorMessage: error instanceof Error ? error.message : 'Unknown error',
            errorCause: 'UnexpectedError',
        });
        throw error;
    }
}
function getExecutor(toolCall, stateProvider, eventHandler, logger) {
    if (!toolCall.toolName) {
        logger.error('No step type on step');
        return undefined;
    }
    if (!exports.toolExecutors[toolCall.toolName]) {
        logger.error(`Received execute tool request for unknown step type ${toolCall.toolName}`);
        return undefined;
    }
    const originalExecutor = exports.toolExecutors[toolCall.toolName];
    return {
        needsFind: originalExecutor.needsFind,
        getStepType: originalExecutor.getStepType,
        execute: (payload) => executeToolWithTracking(originalExecutor, payload, stateProvider, eventHandler, logger),
    };
}
const pageMutatingTools = new Set([
    mablApi_1.ExecuteToolName.Click,
    mablApi_1.ExecuteToolName.ClickAndHold,
    mablApi_1.ExecuteToolName.Release,
    mablApi_1.ExecuteToolName.EnterAuthCode,
    mablApi_1.ExecuteToolName.EnterText,
    mablApi_1.ExecuteToolName.Hover,
    mablApi_1.ExecuteToolName.Scroll,
    mablApi_1.ExecuteToolName.VisitUrl,
    mablApi_1.ExecuteToolName.Flow,
    mablApi_1.ExecuteToolName.PreexistingSnippet,
    mablApi_1.ExecuteToolName.SwitchTab,
    mablApi_1.ExecuteToolName.PlaySteps,
    mablApi_1.ExecuteToolName.Javascript,
    mablApi_1.ExecuteToolName.OpenMablEmail,
    mablApi_1.ExecuteToolName.OpenPdf,
    mablApi_1.ExecuteToolName.ReloadPage,
    mablApi_1.ExecuteToolName.SetViewport,
    mablApi_1.ExecuteToolName.UploadFile,
]);
const urlRelevantTools = new Set([
    mablApi_1.ExecuteToolName.Click,
    mablApi_1.ExecuteToolName.EnterAuthCode,
    mablApi_1.ExecuteToolName.EnterText,
    mablApi_1.ExecuteToolName.Scroll,
    mablApi_1.ExecuteToolName.VisitUrl,
    mablApi_1.ExecuteToolName.Flow,
    mablApi_1.ExecuteToolName.PreexistingSnippet,
    mablApi_1.ExecuteToolName.SwitchTab,
    mablApi_1.ExecuteToolName.Javascript,
    mablApi_1.ExecuteToolName.OpenMablEmail,
    mablApi_1.ExecuteToolName.OpenPdf,
    mablApi_1.ExecuteToolName.PlaySteps,
    mablApi_1.ExecuteToolName.ReloadPage,
]);
const tabCreatingTools = new Set([
    mablApi_1.ExecuteToolName.Click,
    mablApi_1.ExecuteToolName.VisitUrl,
    mablApi_1.ExecuteToolName.Flow,
    mablApi_1.ExecuteToolName.PreexistingSnippet,
    mablApi_1.ExecuteToolName.Javascript,
    mablApi_1.ExecuteToolName.Snippet,
    mablApi_1.ExecuteToolName.OpenMablEmail,
    mablApi_1.ExecuteToolName.OpenPdf,
]);
function toolNeedsPostExecutionContext(toolName, success) {
    const needsScreenshot = pageMutatingTools.has(toolName);
    if (!success) {
        return { needsScreenshot, needsUrl: false, needsTabInfo: false };
    }
    return {
        needsScreenshot,
        needsUrl: urlRelevantTools.has(toolName),
        needsTabInfo: tabCreatingTools.has(toolName),
    };
}
function getExecutorByAuthoringName(authoringToolName, toolParams, stateProvider, eventHandler, logger) {
    if (!exports.toolExecutors[authoringToolName]) {
        logger.error(`No executor found for authoring tool name: ${authoringToolName}`);
        return undefined;
    }
    const executor = exports.toolExecutors[authoringToolName];
    const toolCall = {
        toolName: authoringToolName,
        toolParams,
    };
    const wrappedExecutor = {
        needsFind: executor.needsFind,
        getStepType: executor.getStepType,
        execute: (payload) => executeToolWithTracking(executor, payload, stateProvider, eventHandler, logger),
    };
    return { executor: wrappedExecutor, toolCall };
}
