The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
feat(desktop): drive the preview with real input, not synthetic events
Brooklyn Nicholsonc358a6a570d1
diff --git a/apps/desktop/src/app/chat/right-rail/preview-drive.ts b/apps/desktop/src/app/chat/right-rail/preview-drive.tsnew file mode 100644index 00000000000..18b9dbf24ef--- /dev/null+++ b/apps/desktop/src/app/chat/right-rail/preview-drive.ts@@ -0,0 +1,138 @@+/**+ * PREVIEW DRIVE — the agent's hand on the mouse and keyboard.+ *+ * Everything here goes out through `sendInputEvent`, so the guest page receives+ * ordinary trusted input: the pointer really moves across it, `:hover` really+ * matches, focus really lands, and a menu that only exists while hovered is+ * actually open by the time the click arrives. That last part is the whole+ * point — a synthetic `el.click()` on a dropdown item fires at a node the page+ * never rendered, which is why script-driven clicking quietly misses.+ *+ * The pointer is walked to its target in steps rather than teleported, for the+ * same reason: a page learns it is hovered from a stream of moves, and one+ * move from nowhere to the target skips every element in between. Fourteen+ * steps over ~280ms is what Playwright's action cursor settles on too.+ */++import type { PreviewInputHandle } from './preview-input'++export interface DrivePoint {+ x: number+ y: number+}++/** The pointer JUMPS. These few steps over a couple of frames exist only so the+ * page receives a move stream at all — `:hover`, `pointerenter` and the menus+ * that hang off them need more than a single teleport to resolve — not so the+ * travel can be watched. A machine does not sweep a mouse across a screen. */+const GLIDE_STEPS = 3+const GLIDE_MS = 36++/** Gap between keystrokes. Fast enough not to pad the turn, slow enough that a+ * page debouncing its input handler still sees them as separate. */+const KEY_MS = 7++/** Notches in a wheel gesture, and how long the gesture takes. Sent as a stream+ * rather than one big delta so scroll-linked effects — sticky headers, reveal+ * animations, infinite-scroll loaders — get the same series of events a hand+ * would produce, and so it looks like momentum rather than a jump cut. */+const WHEEL_STEPS = 12+const WHEEL_MS = 240++/** Where the pointer was left, so the next glide starts from there instead of+ * jumping. Module-level because the pointer is a property of the pane, not of+ * any one action. The origin is a placeholder, not a position — see below. */+let pointer: DrivePoint = { x: 0, y: 0 }+let placed = false++/** Whether the pointer has ever been sent anywhere. Until it has, its notional+ * spot is the top-left corner, which is a real coordinate a caller can wheel+ * or click at by accident — so callers that only need it to be SOMEWHERE+ * sensible have to know the difference. */+export function pointerPlaced(): boolean {+ return placed+}++const wait = (ms: number) => new Promise(resolve => setTimeout(resolve, ms))++/** Decelerating, like a hand arriving at a target rather than a linear sweep. */+const easeOut = (t: number) => 1 - Math.pow(1 - t, 3)++/** Walk the pointer to `to`, letting the page hover everything on the way. */+export async function glideTo(input: PreviewInputHandle, to: DrivePoint): Promise<void> {+ const from = pointer++ for (let step = 1; step <= GLIDE_STEPS; step++) {+ const progress = easeOut(step / GLIDE_STEPS)++ input.send({+ type: 'mouseMove',+ x: Math.round(from.x + (to.x - from.x) * progress),+ y: Math.round(from.y + (to.y - from.y) * progress)+ })+ await wait(GLIDE_MS / GLIDE_STEPS)+ }++ pointer = to+ placed = true+}++/** Press and release at the pointer's current spot. `clicks` of 3 selects the+ * text under it, which is how a field gets cleared without a modifier key. */+export async function clickAt(input: PreviewInputHandle, clicks = 1): Promise<void> {+ for (let click = 1; click <= clicks; click++) {+ input.send({ button: 'left', clickCount: click, type: 'mouseDown', x: pointer.x, y: pointer.y })+ input.send({ button: 'left', clickCount: click, type: 'mouseUp', x: pointer.x, y: pointer.y })+ await wait(KEY_MS)+ }+}++/** Wheel `down` pixels' worth of notches at the pointer's current spot; negative+ * scrolls up. Electron's wheel delta is the legacy `wheelDelta` sign — positive+ * moves the content down, i.e. scrolls UP — so it is the inverse of the number+ * a caller asks for, and of DOM `WheelEvent.deltaY`. */+export async function wheelBy(input: PreviewInputHandle, down: number): Promise<void> {+ let sent = 0++ for (let step = 1; step <= WHEEL_STEPS; step++) {+ const so_far = Math.round(down * easeOut(step / WHEEL_STEPS))+ const notch = so_far - sent++ sent = so_far++ if (notch) {+ input.send({ deltaX: 0, deltaY: -notch, type: 'mouseWheel', x: pointer.x, y: pointer.y })+ }++ await wait(WHEEL_MS / WHEEL_STEPS)+ }+}++/** Send one key the long way round, so a page watching any of the three sees it. */+export async function pressKey(input: PreviewInputHandle, key: string): Promise<void> {+ input.send({ keyCode: key, type: 'keyDown' })+ input.send({ keyCode: key, type: 'char' })+ input.send({ keyCode: key, type: 'keyUp' })+ await wait(KEY_MS)+}++/** Select-all inside whatever has focus, which for a focused field is that+ * field's own text and nothing else. This replaced a triple-click: a triple+ * click is a POINTER gesture, so it selects whatever paragraph sits under the+ * cursor whenever the target turns out not to be a field, and the agent was+ * leaving pages with their body text highlighted. There is no `char` phase —+ * a chord is not text entry, and sending one types a literal 'a'. */+export async function selectAll(input: PreviewInputHandle): Promise<void> {+ const chord = ['control', 'meta']++ input.send({ keyCode: 'a', modifiers: chord, type: 'keyDown' })+ input.send({ keyCode: 'a', modifiers: chord, type: 'keyUp' })+ await wait(KEY_MS)+}++/** Type `text` a character at a time into whatever currently has focus. */+export async function typeText(input: PreviewInputHandle, text: string): Promise<void> {+ for (const character of text) {+ await pressKey(input, character)+ }+}diff --git a/apps/desktop/src/app/chat/right-rail/preview-input.ts b/apps/desktop/src/app/chat/right-rail/preview-input.tsnew file mode 100644index 00000000000..c0f1dbb5a0d--- /dev/null+++ b/apps/desktop/src/app/chat/right-rail/preview-input.ts@@ -0,0 +1,56 @@+/**+ * PREVIEW INPUT REGISTRY — real input into the preview pane's guest page, the+ * difference between the agent DRIVING the browser and merely poking its DOM.+ *+ * `executeJavaScript` can only ever dispatch synthetic events: `isTrusted` is+ * false, the browser's own hover target never moves, `:hover` rules never+ * match, and hover-gated menus never open — so a click lands on a dropdown item+ * that was never rendered. `sendInputEvent` goes in through Chromium's input+ * pipeline instead, producing the same events a hand on the mouse would.+ *+ * It has to be called on the `<webview>` ELEMENT. Sending to the embedder's+ * webContents does not reach a guest (electron/electron#20333), which is why+ * this is a per-pane registry rather than something main could do.+ *+ * Coordinates are relative to the webview, and the webview IS the guest+ * viewport — so a rect the act engine measured inside the page needs no+ * conversion on the way back out.+ */++import { $rightRailActiveTabId } from '@/store/layout'+import { $previewTabs } from '@/store/preview'++/** The subset of Electron's input events the agent needs to drive a page. */+export type PreviewInputEvent =+ | { button: 'left'; clickCount: number; type: 'mouseDown' | 'mouseUp'; x: number; y: number }+ | { deltaX: number; deltaY: number; type: 'mouseWheel'; x: number; y: number }+ | { keyCode: string; modifiers?: string[]; type: 'char' | 'keyDown' | 'keyUp' }+ | { type: 'mouseMove'; x: number; y: number }++export interface PreviewInputHandle {+ /** Give the guest keyboard focus, so key events reach its active element. */+ focus: () => void+ send: (event: PreviewInputEvent) => void+}++const handles = new Map<string, PreviewInputHandle>()++/** Register a live pane's input channel; returns an idempotent unregister. */+export function registerPreviewInput(tabId: string, handle: PreviewInputHandle): () => void {+ handles.set(tabId, handle)++ return () => {+ if (handles.get(tabId) === handle) {+ handles.delete(tabId)+ }+ }+}++/** The ACTIVE preview tab's input channel. Null = nothing real to drive, and+ * the caller falls back to synthesizing events inside the page. */+export function activePreviewInput(): PreviewInputHandle | null {+ const tabs = $previewTabs.get()+ const tab = tabs.find(t => t.id === $rightRailActiveTabId.get()) ?? tabs[0]++ return (tab && handles.get(tab.id)) || null+}diff --git a/apps/desktop/src/app/chat/right-rail/preview-pane.tsx b/apps/desktop/src/app/chat/right-rail/preview-pane.tsxindex e2f236cf2fa..076fc4a94f6 100644--- a/apps/desktop/src/app/chat/right-rail/preview-pane.tsx+++ b/apps/desktop/src/app/chat/right-rail/preview-pane.tsx@@ -28,6 +28,7 @@ import {import { type ConsoleEntry } from './preview-console-state'import { previewConsoleState } from './preview-console-store'import { LocalFilePreview, PreviewEmptyState } from './preview-file'+import { registerPreviewInput, type PreviewInputEvent } from './preview-input'import { PREVIEW_BROWSER_ATTR, registerPreviewNav } from './preview-nav'import { registerPreviewPageReader } from './preview-reader'import { registerPreviewScriptRunner } from './preview-script-runner'@@ -53,6 +54,7 @@ type PreviewWebview = HTMLElement & {reloadIgnoringCache?: () => voidreplaceMisspelling?: (word: string) => voidselectAll?: () => void+ sendInputEvent?: (event: PreviewInputEvent) => void}/** The raw Chromium params riding the webview tag's `context-menu` event. */@@ -457,7 +459,7 @@ export function PreviewPane({ embedded = false, onRestartServer, reloadRequest =// Publish the SCRIPT runner for this tab: the one channel into the guest// page, shared by the tour tool (injected driver.js walkthroughs) and the- // act_preview tool (clicking, typing, scrolling the page the user sees).+ // drive_preview tool (clicking, typing, scrolling the page the user sees).useEffect(() => {if (!isWebPreview || !tabId) {return@@ -474,6 +476,32 @@ export function PreviewPane({ embedded = false, onRestartServer, reloadRequest =})}, [isWebPreview, tabId])+ // Publish the INPUT channel for this tab. Same idea as the script runner, but+ // it carries real Chromium input rather than script — the agent's clicks and+ // keystrokes arrive as trusted events, so the page hovers, focuses and reacts+ // exactly as it would under a human hand.+ useEffect(() => {+ if (!isWebPreview || isRemoteHtml || !tabId) {+ return+ }++ return registerPreviewInput(tabId, {+ focus: () => webviewRef.current?.focus?.(),+ send: event => {+ const webview = webviewRef.current++ // Never optional-chain this call away: a missing method would make every+ // agent click a silent no-op that still reports success, because the+ // overlay and the read-back both run on the separate script channel.+ if (typeof webview?.sendInputEvent !== 'function') {+ throw new Error('preview webview cannot take input events')+ }++ webview.sendInputEvent(event)+ }+ })+ }, [isRemoteHtml, isWebPreview, tabId])+// eslint-disable-next-line no-restricted-syntax -- legitimate non-atom ref write (see eslint rule comment)useEffect(() => {if (!consoleOpen) {