"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const test_1 = require("@playwright/test");
const v3_js_1 = require("../../lib/v3/v3.js");
const v3_config_js_1 = require("./v3.config.js");
const index_js_1 = require("../../lib/v3/agent/tools/index.js");
const agentSystemPrompt_js_1 = require("../../lib/v3/agent/prompts/agentSystemPrompt.js");
test_1.test.describe("Stagehand agent hybrid mode", () => {
    let v3;
    test_1.test.beforeEach(async () => {
        v3 = new v3_js_1.V3({
            ...v3_config_js_1.v3TestConfig,
            experimental: true,
        });
        await v3.init();
    });
    test_1.test.afterEach(async () => {
        await v3?.close?.().catch(() => { });
    });
    test_1.test.describe("Tool filtering by mode", () => {
        (0, test_1.test)("DOM mode includes DOM-based tools and excludes coordinate-based tools", () => {
            const tools = (0, index_js_1.createAgentTools)(v3, { mode: "dom" });
            // DOM mode should have these tools
            (0, test_1.expect)(tools).toHaveProperty("act");
            (0, test_1.expect)(tools).toHaveProperty("fillForm");
            (0, test_1.expect)(tools).toHaveProperty("ariaTree");
            (0, test_1.expect)(tools).toHaveProperty("screenshot");
            (0, test_1.expect)(tools).toHaveProperty("extract");
            (0, test_1.expect)(tools).toHaveProperty("goto");
            (0, test_1.expect)(tools).toHaveProperty("scroll");
            (0, test_1.expect)(tools).toHaveProperty("wait");
            (0, test_1.expect)(tools).toHaveProperty("navback");
            (0, test_1.expect)(tools).toHaveProperty("keys");
            (0, test_1.expect)(tools).toHaveProperty("think");
            // DOM mode should NOT have coordinate-based tools
            (0, test_1.expect)(tools).not.toHaveProperty("click");
            (0, test_1.expect)(tools).not.toHaveProperty("type");
            (0, test_1.expect)(tools).not.toHaveProperty("dragAndDrop");
            (0, test_1.expect)(tools).not.toHaveProperty("clickAndHold");
            (0, test_1.expect)(tools).not.toHaveProperty("fillFormVision");
        });
        (0, test_1.test)("Hybrid mode includes coordinate-based tools and excludes DOM fillForm", () => {
            const tools = (0, index_js_1.createAgentTools)(v3, { mode: "hybrid" });
            // Hybrid mode should have coordinate-based tools
            (0, test_1.expect)(tools).toHaveProperty("click");
            (0, test_1.expect)(tools).toHaveProperty("type");
            (0, test_1.expect)(tools).toHaveProperty("dragAndDrop");
            (0, test_1.expect)(tools).toHaveProperty("clickAndHold");
            (0, test_1.expect)(tools).toHaveProperty("fillFormVision");
            // Hybrid mode should also have common tools
            (0, test_1.expect)(tools).toHaveProperty("act");
            (0, test_1.expect)(tools).toHaveProperty("ariaTree");
            (0, test_1.expect)(tools).toHaveProperty("screenshot");
            (0, test_1.expect)(tools).toHaveProperty("extract");
            (0, test_1.expect)(tools).toHaveProperty("goto");
            (0, test_1.expect)(tools).toHaveProperty("scroll");
            (0, test_1.expect)(tools).toHaveProperty("wait");
            (0, test_1.expect)(tools).toHaveProperty("navback");
            (0, test_1.expect)(tools).toHaveProperty("keys");
            (0, test_1.expect)(tools).toHaveProperty("think");
            // Hybrid mode should NOT have DOM-based fillForm
            (0, test_1.expect)(tools).not.toHaveProperty("fillForm");
        });
        (0, test_1.test)("Default mode is DOM when not specified", () => {
            const tools = (0, index_js_1.createAgentTools)(v3, {});
            // Should behave like DOM mode
            (0, test_1.expect)(tools).toHaveProperty("fillForm");
            (0, test_1.expect)(tools).not.toHaveProperty("click");
            (0, test_1.expect)(tools).not.toHaveProperty("type");
        });
    });
    test_1.test.describe("System prompt generation", () => {
        (0, test_1.test)("DOM mode system prompt emphasizes ariaTree and act tool", () => {
            const prompt = (0, agentSystemPrompt_js_1.buildAgentSystemPrompt)({
                url: "https://example.com",
                executionInstruction: "Test instruction",
                mode: "dom",
            });
            // DOM mode should prioritize ariaTree
            (0, test_1.expect)(prompt).toContain("ariaTree");
            (0, test_1.expect)(prompt).toContain("act");
            (0, test_1.expect)(prompt).toContain("fillForm");
            // Should have DOM-specific strategy
            (0, test_1.expect)(prompt).toContain("Use act tool for all clicking and typing");
            (0, test_1.expect)(prompt).toContain("Always check ariaTree first");
        });
        (0, test_1.test)("Hybrid mode system prompt emphasizes screenshot and coordinate tools", () => {
            const prompt = (0, agentSystemPrompt_js_1.buildAgentSystemPrompt)({
                url: "https://example.com",
                executionInstruction: "Test instruction",
                mode: "hybrid",
            });
            // Hybrid mode should have coordinate-based tools mentioned
            (0, test_1.expect)(prompt).toContain("click");
            (0, test_1.expect)(prompt).toContain("type");
            (0, test_1.expect)(prompt).toContain("fillFormVision");
            (0, test_1.expect)(prompt).toContain("dragAndDrop");
            // Should have hybrid-specific strategy
            (0, test_1.expect)(prompt).toContain("Use specific tools (click, type) when elements are visible");
            (0, test_1.expect)(prompt).toContain("Always use screenshot");
        });
        (0, test_1.test)("System prompt includes custom instructions when provided", () => {
            const customInstructions = "Always be polite and thorough";
            const prompt = (0, agentSystemPrompt_js_1.buildAgentSystemPrompt)({
                url: "https://example.com",
                executionInstruction: "Test instruction",
                mode: "dom",
                systemInstructions: customInstructions,
            });
            (0, test_1.expect)(prompt).toContain("customInstructions");
            (0, test_1.expect)(prompt).toContain(customInstructions);
        });
        (0, test_1.test)("System prompt includes Browserbase captcha message when isBrowserbase is true", () => {
            const prompt = (0, agentSystemPrompt_js_1.buildAgentSystemPrompt)({
                url: "https://example.com",
                executionInstruction: "Test instruction",
                mode: "dom",
                isBrowserbase: true,
            });
            (0, test_1.expect)(prompt).toContain("captcha");
            (0, test_1.expect)(prompt).toContain("automatically be solved");
        });
        (0, test_1.test)("System prompt does not include captcha message when isBrowserbase is false", () => {
            const prompt = (0, agentSystemPrompt_js_1.buildAgentSystemPrompt)({
                url: "https://example.com",
                executionInstruction: "Test instruction",
                mode: "dom",
                isBrowserbase: false,
            });
            (0, test_1.expect)(prompt).not.toContain("automatically be solved");
        });
    });
    test_1.test.describe("Agent creation with mode", () => {
        (0, test_1.test)("agent({ mode: 'dom' }) creates DOM-mode agent", () => {
            const agent = v3.agent({
                mode: "dom",
                model: "anthropic/claude-haiku-4-5-20251001",
            });
            (0, test_1.expect)(agent).toHaveProperty("execute");
        });
        (0, test_1.test)("agent({ mode: 'hybrid' }) creates hybrid-mode agent", () => {
            const agent = v3.agent({
                mode: "hybrid",
                model: "anthropic/claude-haiku-4-5-20251001",
            });
            (0, test_1.expect)(agent).toHaveProperty("execute");
        });
        (0, test_1.test)("agent without mode defaults to DOM mode", () => {
            const agent = v3.agent({
                model: "anthropic/claude-haiku-4-5-20251001",
            });
            (0, test_1.expect)(agent).toHaveProperty("execute");
        });
        (0, test_1.test)("hybrid mode can be combined with streaming", () => {
            const agent = v3.agent({
                mode: "hybrid",
                stream: true,
                model: "anthropic/claude-haiku-4-5-20251001",
            });
            (0, test_1.expect)(agent).toHaveProperty("execute");
        });
    });
    test_1.test.describe("Hybrid mode execution", () => {
        (0, test_1.test)("hybrid mode agent uses coordinate-based tools when available", async () => {
            test_1.test.setTimeout(90000);
            const toolCalls = [];
            const agent = v3.agent({
                mode: "hybrid",
                model: "anthropic/claude-haiku-4-5-20251001",
            });
            const page = v3.context.pages()[0];
            await page.goto("https://example.com");
            await agent.execute({
                instruction: "Take a screenshot to see the page, then describe what you see briefly and mark the task as complete.",
                maxSteps: 5,
                callbacks: {
                    onStepFinish: async (event) => {
                        if (event.toolCalls) {
                            for (const tc of event.toolCalls) {
                                toolCalls.push({
                                    toolName: tc.toolName,
                                    input: tc.input,
                                });
                            }
                        }
                    },
                },
            });
            // Should have captured tool calls
            (0, test_1.expect)(toolCalls.length).toBeGreaterThan(0);
            const toolNames = toolCalls.map((tc) => tc.toolName);
            // Should include screenshot (hybrid mode emphasizes visual)
            (0, test_1.expect)(toolNames).toContain("screenshot");
        });
        (0, test_1.test)("DOM mode agent uses DOM-based tools", async () => {
            test_1.test.setTimeout(90000);
            const toolCalls = [];
            const agent = v3.agent({
                mode: "dom",
                model: "anthropic/claude-haiku-4-5-20251001",
            });
            const page = v3.context.pages()[0];
            await page.goto("https://example.com");
            await agent.execute({
                instruction: "Use the ariaTree to understand the page, then provide the final requested output or a summary of the page.",
                maxSteps: 5,
                callbacks: {
                    onStepFinish: async (event) => {
                        if (event.toolCalls) {
                            for (const tc of event.toolCalls) {
                                toolCalls.push({
                                    toolName: tc.toolName,
                                    input: tc.input,
                                });
                            }
                        }
                    },
                },
            });
            // Should have captured tool calls
            (0, test_1.expect)(toolCalls.length).toBeGreaterThan(0);
            // Should include ariaTree (DOM mode emphasizes aria-based interaction)
            const toolNames = toolCalls.map((tc) => tc.toolName);
            (0, test_1.expect)(toolNames).toContain("ariaTree");
        });
    });
    test_1.test.describe("Scroll tool variants by mode", () => {
        (0, test_1.test)("DOM mode uses simple scroll tool without coordinates", () => {
            const tools = (0, index_js_1.createAgentTools)(v3, { mode: "dom" });
            (0, test_1.expect)(tools).toHaveProperty("scroll");
            // The DOM scroll tool should exist
            (0, test_1.expect)(typeof tools.scroll).toBe("object");
        });
        (0, test_1.test)("Hybrid mode uses vision scroll tool with optional coordinates", () => {
            const tools = (0, index_js_1.createAgentTools)(v3, { mode: "hybrid" });
            (0, test_1.expect)(tools).toHaveProperty("scroll");
            // The hybrid scroll tool should exist
            (0, test_1.expect)(typeof tools.scroll).toBe("object");
        });
    });
    test_1.test.describe("Keys tool availability in both modes", () => {
        (0, test_1.test)("Keys tool is available in DOM mode", () => {
            const tools = (0, index_js_1.createAgentTools)(v3, { mode: "dom" });
            (0, test_1.expect)(tools).toHaveProperty("keys");
        });
        (0, test_1.test)("Keys tool is available in hybrid mode", () => {
            const tools = (0, index_js_1.createAgentTools)(v3, { mode: "hybrid" });
            (0, test_1.expect)(tools).toHaveProperty("keys");
        });
    });
    test_1.test.describe("Think tool availability", () => {
        (0, test_1.test)("Think tool is available in DOM mode", () => {
            const tools = (0, index_js_1.createAgentTools)(v3, { mode: "dom" });
            (0, test_1.expect)(tools).toHaveProperty("think");
        });
        (0, test_1.test)("Think tool is available in hybrid mode", () => {
            const tools = (0, index_js_1.createAgentTools)(v3, { mode: "hybrid" });
            (0, test_1.expect)(tools).toHaveProperty("think");
        });
    });
});
//# sourceMappingURL=agent-hybrid-mode.spec.js.map