|
| 1 | +/** |
| 2 | + * Vision executor primitives (shared parsing/execution helpers). |
| 3 | + * |
| 4 | + * This is used by higher-level agents when falling back to a vision model to propose |
| 5 | + * coordinate-based actions. |
| 6 | + */ |
| 7 | + |
| 8 | +export type VisionExecutorActionKind = 'click_xy' | 'click_rect' | 'press' | 'type' | 'finish'; |
| 9 | + |
| 10 | +export interface VisionExecutorAction { |
| 11 | + kind: VisionExecutorActionKind; |
| 12 | + args: Record<string, any>; |
| 13 | +} |
| 14 | + |
| 15 | +export function parseVisionExecutorAction(text: string): VisionExecutorAction { |
| 16 | + const t = String(text || '') |
| 17 | + .replace(/```[\w]*\n?/g, '') |
| 18 | + .trim(); |
| 19 | + |
| 20 | + if (/^FINISH\s*\(\s*\)\s*$/i.test(t)) return { kind: 'finish', args: {} }; |
| 21 | + |
| 22 | + let m = t.match(/^PRESS\s*\(\s*["']([^"']+)["']\s*\)\s*$/i); |
| 23 | + if (m) return { kind: 'press', args: { key: m[1] } }; |
| 24 | + |
| 25 | + m = t.match(/^TYPE\s*\(\s*["']([\s\S]*?)["']\s*\)\s*$/i); |
| 26 | + if (m) return { kind: 'type', args: { text: m[1] } }; |
| 27 | + |
| 28 | + m = t.match(/^CLICK_XY\s*\(\s*(-?\d+(?:\.\d+)?)\s*,\s*(-?\d+(?:\.\d+)?)\s*\)\s*$/i); |
| 29 | + if (m) return { kind: 'click_xy', args: { x: Number(m[1]), y: Number(m[2]) } }; |
| 30 | + |
| 31 | + m = t.match( |
| 32 | + /^CLICK_RECT\s*\(\s*(-?\d+(?:\.\d+)?)\s*,\s*(-?\d+(?:\.\d+)?)\s*,\s*(-?\d+(?:\.\d+)?)\s*,\s*(-?\d+(?:\.\d+)?)\s*\)\s*$/i |
| 33 | + ); |
| 34 | + if (m) |
| 35 | + return { |
| 36 | + kind: 'click_rect', |
| 37 | + args: { x: Number(m[1]), y: Number(m[2]), w: Number(m[3]), h: Number(m[4]) }, |
| 38 | + }; |
| 39 | + |
| 40 | + throw new Error(`unrecognized vision action: ${t.slice(0, 200)}`); |
| 41 | +} |
| 42 | + |
| 43 | +export async function executeVisionExecutorAction(params: { |
| 44 | + backend: any; |
| 45 | + page?: any; |
| 46 | + action: VisionExecutorAction; |
| 47 | +}): Promise<void> { |
| 48 | + const { backend, page, action } = params; |
| 49 | + |
| 50 | + if (action.kind === 'click_xy') { |
| 51 | + await backend.mouse_click(Number(action.args.x), Number(action.args.y)); |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + if (action.kind === 'click_rect') { |
| 56 | + const cx = Number(action.args.x) + Number(action.args.w) / 2; |
| 57 | + const cy = Number(action.args.y) + Number(action.args.h) / 2; |
| 58 | + await backend.mouse_click(cx, cy); |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + if (action.kind === 'press') { |
| 63 | + if (!page) throw new Error('PRESS requires a Playwright page'); |
| 64 | + await page.keyboard.press(String(action.args.key)); |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + if (action.kind === 'type') { |
| 69 | + await backend.type_text(String(action.args.text)); |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + if (action.kind === 'finish') return; |
| 74 | + |
| 75 | + throw new Error(`unknown vision action kind: ${(action as any).kind}`); |
| 76 | +} |
0 commit comments