import { z } from 'zod'; import type { BrowserLocator } from './browserLocator.js'; import { isAllowedBrowserScheme } from './browser.js'; import { probesSchema, verificationSchema } from './browserVerification.js'; import type { ToolResult } from '../../types.js'; /** Embedded hosts may call the tool directly, before the registry parses JSON. */ export function invalidBrowserInput(error: z.ZodError): ToolResult { return { ok:false, disposition:'not_executed', error:error.issues.slice(0,8) .map(i => `${i.path.join('.').slice(0,120) || ''}: ${i.message.slice(0,240)}`).join('; ') }; } // These are the public, shared action spellings. Entry-specific legacy actions, // coordinate fallback and batch-only steps remain in their existing adapters. export const actionFamilies = { navigation: ['navigate'], targeting: ['click', 'doubleClick', 'hover', 'getState'], input: ['fill', 'type', 'press', 'check', 'uncheck', 'setChecked'], observation: ['snapshot', 'observe', 'readEvidence', 'readField', 'frameInfo'], verification: ['probe', 'verify'], } as const; export const sharedActions = [ ...actionFamilies.navigation, ...actionFamilies.targeting, ...actionFamilies.input, ...actionFamilies.observation, ...actionFamilies.verification, ] as const; const targetActions = new Set([...actionFamilies.targeting, ...actionFamilies.input]); export const usesBrowserTarget = (action: string): boolean => targetActions.has(action); export const sharedActionHint = 'navigate: url; element actions: selector or locator; fill/type: +value; press: key (target optional); setChecked: +checked; probe: probes; verify: verification; readField: snapshotId+fieldNodeId+field; readEvidence: evidenceId.'; const locatorLeaf = z.object({ by: z.enum(['css', 'ax', 'ref', 'role', 'text', 'label', 'placeholder', 'alt', 'title', 'testid']), value: z.string(), name: z.string().optional(), exact: z.boolean().optional(), strict: z.boolean().optional(), nth: z.number().int().nonnegative().optional(), has_text: z.string().optional(), generation: z.number().int().positive().optional(), frameId: z.string().min(1).optional(), frame_id: z.string().min(1).optional(), }); const frameChain = z.array(locatorLeaf); export const locatorSchema = locatorLeaf.extend({ within: locatorLeaf.optional(), frame: frameChain.optional(), framePath: frameChain.optional(), frame_path: frameChain.optional(), }) as z.ZodType; export const observationFields = { cursor: z.string().optional(), baseSnapshotId: z.string().optional(), frameId: z.string().optional(), probes: probesSchema.optional(), verification: verificationSchema.optional(), evidenceId: z.string().optional(), snapshotId: z.string().optional(), fieldNodeId: z.string().optional(), field: z.enum(['name', 'description', 'value', 'valueText']).optional(), offset: z.number().int().nonnegative().optional(), }; /** Run before any step executes (in particular, before earlier steps in a batch). * Preserve empty input values, false checkbox states, and zero coordinates. */ export function checkBrowserAction(entry: 'single' | 'batch') { return (data: { action: string; [key: string]: unknown }, ctx: z.RefinementCtx): void => { const issue = (field: string, message: string) => ctx.addIssue({ code: 'custom', path: [field], message: `${data.action}: ${message}` }); const present = (key: string) => data[key] !== undefined && data[key] !== ''; const requireField = (key: string, allowEmpty = false) => { if (allowEmpty ? data[key] === undefined : !present(key)) issue(key, `requires ${key}`); }; const action = data.action; if (action === 'navigate') { requireField('url'); if (typeof data.url === 'string' && (!data.url.trim() || !isAllowedBrowserScheme(data.url))) issue('url', 'unsupported navigation URL'); } if (entry === 'single' && data.locator && !usesBrowserTarget(action)) issue('locator', 'locator not supported for this action'); if (usesBrowserTarget(action)) { const locator = data.locator as BrowserLocator | undefined; if (locator?.by === 'ref' && data.selector) issue('selector', 'elementRef cannot have a selector fallback'); const pointer = entry === 'single' && (action === 'click' || action === 'hover') && data.x !== undefined && data.y !== undefined; if (action !== 'press' && !present('selector') && !locator && !pointer) issue('selector', 'requires selector or locator' + (entry === 'single' && (action === 'click' || action === 'hover') ? ' or x/y' : '')); } if (action === 'fill' || action === 'type') requireField('value', true); if (action === 'press') requireField('key'); if (action === 'setChecked') requireField('checked'); if (action === 'probe') requireField('probes'); if (action === 'verify') requireField('verification'); if (action === 'readEvidence') requireField('evidenceId'); if (action === 'readField') for (const name of ['snapshotId', 'fieldNodeId', 'field']) requireField(name); if (action === 'waitForURL' && !(entry === 'single' && data.waitUrl)) requireField('url'); if (entry === 'batch' && ['typeNode', 'scrollNode', 'clickNode'].includes(action)) { requireField('node_id'); if (action === 'typeNode') requireField('value', true); } }; }