import { z } from 'zod';
import { defineTool } from '../toolDefinition.js';
import { AgentToolNames } from './agentToolNames.js';
export const searchSnapshotArgumentsSchema = z.object({
    reasoning: z.string().optional().describe('Brief explanation of what you are looking for and why'),
    pattern: z.string().describe(`Regex pattern to match against lines of the page element listing. Case-insensitive.

The element listing is a YAML-shaped tree where each line describes one element on the page:
  - <role> "<accessible name>" [ref=eN] [attr=value]
Roles are aria-style ('button', 'textbox', 'link', 'dialog', 'heading', ...). Indentation shows parent/child nesting, so a textbox indented under a form belongs to that form. Link targets appear on their own child line as \`- /url: <href>\`.

Example patterns:
  - 'button.*[Ss]ubmit'        find submit-style buttons
  - 'textbox.*[Ee]mail'        find email inputs
  - '\\[ref=e42\\]'             locate a specific element by ref
  - 'dialog|alertdialog'       find modal containers
  - '/url:.*login'             find links whose href contains "login"`),
    context_lines: z
        .number()
        .int()
        .min(0)
        .max(20)
        .optional()
        .describe('How many surrounding lines of the element listing to include around each match, so ancestor and sibling elements are visible. Defaults to 5.'),
    limit: z.number().int().min(1).max(50).optional().describe('Maximum number of hits to return. Defaults to 10.'),
});
const searchSnapshotHitSchema = z.object({
    line_number: z.number().int().describe('1-based line number of the matching line in the full element listing'),
    match: z.string().describe('The matching line from the element listing'),
    context: z
        .string()
        .describe('The matching line plus surrounding lines joined by newlines, preserving original indentation so parent/child nesting is readable'),
    refs_in_context: z
        .array(z.string())
        .describe('Element refs (e.g. "e42") that appear anywhere in the returned context block. Use these refs with element-action tools to operate on the matching elements.'),
});
export const searchSnapshotResponseSchema = z.object({
    success: z.boolean().describe('Whether the search ran without error'),
    hits: z.array(searchSnapshotHitSchema).describe('Matches in line order, truncated to `limit`'),
    total_matched: z
        .number()
        .int()
        .describe('Total number of lines that matched before applying `limit`, so the agent knows when to narrow the pattern'),
    snapshot_total_lines: z.number().int().describe('Total number of lines in the full page element listing'),
    message: z
        .string()
        .optional()
        .describe('Error description if the search failed (invalid regex, element listing unavailable, etc.)'),
});
export const SearchSnapshotToolDefinition = defineTool({
    name: AgentToolNames.SearchSnapshot,
    description: `Search the page's element listing with a regex and return matching lines with surrounding context.

The element listing is a textual, indented tree of every interactive and semantic element currently rendered on the page — buttons, inputs, links, headings, dialogs, list items, and so on. Each line describes one element by its role, its accessible name, and attributes like \`[ref=eN]\` that can be passed to element-action tools. Indentation shows parent/child nesting, so an element indented under a \`dialog\` lives inside that dialog. The listing is computed live from the page DOM each time the tool runs, so it always reflects the current state.

Use this tool to:
- Discover what's actually on the page when a step failed to find or interact with an element
- Locate a specific control by role + accessible name (e.g. find the real "Submit" button after a label change)
- Confirm whether an obstacle (modal, dialog, banner, overlay) is present before deciding how to recover
- Pull the ref of a candidate element so an action tool can operate on it

Tips for effective queries:
- Start with the role plus a fragment of the visible name, e.g. \`button.*[Ss]ubmit\`
- Use \`context_lines\` to see the surrounding form / section / dialog ancestry — the default is usually enough
- If \`total_matched\` is high, narrow the pattern; refs and ancestry in the existing hits are usually enough to act on
- An invalid regex returns \`success: false\` with a \`message\` — fix the pattern and retry`,
    argumentsSchema: searchSnapshotArgumentsSchema,
    responseSchema: searchSnapshotResponseSchema,
});
//# sourceMappingURL=SearchSnapshotDefinition.js.map