Skip to content

The agent that grows with you

fix(desktop): report typed-so-far when a type action is interrupted

Brooklyn Nicholsonb768cb3932ad
diff --git a/apps/desktop/src/app/chat/right-rail/preview-act.test.ts b/apps/desktop/src/app/chat/right-rail/preview-act.test.ts
index e5369175111..9cbfc375bc8 100644
--- a/apps/desktop/src/app/chat/right-rail/preview-act.test.ts
+++ b/apps/desktop/src/app/chat/right-rail/preview-act.test.ts
@@ -270,6 +270,36 @@ describe('actOnActivePreview (drive_preview tool)', () => {
expect(result.error).toMatch(/interrupt/i)
})
+ it('sends Command+A on macOS and Control+A elsewhere for select-all', async () => {
+ const pin = (platform: string, userAgent: string) => {
+ Object.defineProperty(window.navigator, 'platform', { configurable: true, value: platform })
+ Object.defineProperty(window.navigator, 'userAgent', { configurable: true, value: userAgent })
+ }
+
+ try {
+ pin('MacIntel', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)')
+ const mac = withDrivenPane()
+ await actOnActivePreview({ kind: 'type', ref: '@e1', text: 'x' })
+ expect(
+ mac.mock.calls.map(([event]) => event).find(event => event.type === 'keyDown' && event.keyCode === 'a')
+ ).toMatchObject({ modifiers: ['meta'] })
+
+ pin('Win32', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)')
+ const win = withDrivenPane()
+ await actOnActivePreview({ kind: 'type', ref: '@e1', text: 'x' })
+ expect(
+ win.mock.calls.map(([event]) => event).find(event => event.type === 'keyDown' && event.keyCode === 'a')
+ ).toMatchObject({ modifiers: ['control'] })
+ } finally {
+ // jsdom's defaults; whatever suite runs next sees the host's real navigator.
+ delete (window.navigator as { platform?: string }).platform
+ Object.defineProperty(window.navigator, 'userAgent', {
+ configurable: true,
+ value: 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) jsdom/vitest'
+ })
+ }
+ })
+
it('types by pressing keys, after selecting whatever the field held', async () => {
const send = withDrivenPane()
@@ -316,16 +346,18 @@ describe('actOnActivePreview (drive_preview tool)', () => {
cleanups.push(
registerPreviewScriptRunner(tabId, async code =>
- code.includes('document.activeElement')
- ? JSON.stringify({ success: true, field: true, len: fieldLen })
- : code.includes('"kind":"locate"')
- ? JSON.stringify({
- acted: 'looking at textbox "Price"',
- point: { x: 120, y: 80 },
- success: true,
- typable: true
- })
- : JSON.stringify({ elements: [], hit: { tag: 'INPUT', trusted: true }, success: true })
+ code.includes('hermes-focus-probe')
+ ? JSON.stringify({ focused: true, success: true, tag: 'INPUT' })
+ : code.includes('document.activeElement')
+ ? JSON.stringify({ success: true, field: true, len: fieldLen })
+ : code.includes('"kind":"locate"')
+ ? JSON.stringify({
+ acted: 'looking at textbox "Price"',
+ point: { x: 120, y: 80 },
+ success: true,
+ typable: true
+ })
+ : JSON.stringify({ elements: [], hit: { tag: 'INPUT', trusted: true }, success: true })
)
)
cleanups.push(registerPreviewInput(tabId, { focus: vi.fn(), send }))
@@ -402,18 +434,20 @@ describe('actOnActivePreview (drive_preview tool)', () => {
cleanups.push(
registerPreviewScriptRunner(tabId, async code =>
- code.includes('"kind":"locate"')
- ? JSON.stringify({
- acted: 'looking at textbox "Price"',
- point: { x: 120, y: 80 },
- success: true,
- typable: true
- })
- : !code.includes('__hermesAct') && code.includes('getOwnPropertyDescriptor')
- ? JSON.stringify({ success: true }) // the direct-set fallback succeeded (no preamble)
- : !code.includes('__hermesAct') && code.includes('document.activeElement')
- ? JSON.stringify({ success: true, field: true, len: 4 }) // field-state read
- : JSON.stringify({ elements: [], hit: { tag: 'INPUT', trusted: true }, success: true }) // finish trip
+ code.includes('hermes-focus-probe')
+ ? JSON.stringify({ focused: true, success: true, tag: 'INPUT' })
+ : code.includes('"kind":"locate"')
+ ? JSON.stringify({
+ acted: 'looking at textbox "Price"',
+ point: { x: 120, y: 80 },
+ success: true,
+ typable: true
+ })
+ : !code.includes('__hermesAct') && code.includes('getOwnPropertyDescriptor')
+ ? JSON.stringify({ success: true }) // the direct-set fallback succeeded (no preamble)
+ : !code.includes('__hermesAct') && code.includes('document.activeElement')
+ ? JSON.stringify({ success: true, field: true, len: 4 }) // field-state read
+ : JSON.stringify({ elements: [], hit: { tag: 'INPUT', trusted: true }, success: true }) // finish trip
)
)
cleanups.push(registerPreviewInput(tabId, { focus: vi.fn(), send }))
diff --git a/apps/desktop/src/app/chat/right-rail/preview-act.ts b/apps/desktop/src/app/chat/right-rail/preview-act.ts
index ee0ab5c997b..62167359b38 100644
--- a/apps/desktop/src/app/chat/right-rail/preview-act.ts
+++ b/apps/desktop/src/app/chat/right-rail/preview-act.ts
@@ -379,7 +379,7 @@ async function replaceExisting(
input: PreviewInputHandle,
text: string,
signal?: AbortSignal
-): Promise<string | null> {
+): Promise<{ error?: string; typed: number }> {
// (a) Settle after the focus click: let the controlled input's re-render
// finish mounting/rebuilding its node before we try to select that node.
await waitMs(TYPE_RENDER_SETTLE_MS)
@@ -404,7 +404,7 @@ async function replaceExisting(
// that reset/replaced the node, or never landed). Give up only after every
// deterministic clear has also failed.
if (backspaced === TYPE_CLEAR_ATTEMPTS) {
- return TYPE_FAILED_CLEAR
+ return { error: TYPE_FAILED_CLEAR, typed: 0 }
}
// (b) Clear without relying on a selection: End parks the caret at the
@@ -412,8 +412,7 @@ async function replaceExisting(
await clearCharsBack(input, state.len)
}
- await typeText(input, text, signal)
- return null
+ return { typed: await typeText(input, text, signal) }
}
/** Set a focused field's value directly through its own DOM setter — no
@@ -453,12 +452,12 @@ async function setFocusedField(
input: PreviewInputHandle,
text: string,
signal?: AbortSignal
-): Promise<string | null> {
- const clearError = await replaceExisting(run, input, text, signal)
+): Promise<{ error?: string; typed: number }> {
+ const cleared = await replaceExisting(run, input, text, signal)
// Real keystrokes cleared it and typed fine — no fallback needed.
- if (!clearError) {
- return null
+ if (!cleared.error) {
+ return { typed: cleared.typed }
}
// Real keystrokes could not empty the field (a mask keeps refilling it).
@@ -466,10 +465,10 @@ async function setFocusedField(
const trip = await runJson(run, buildDirectSetScript(text))
if (trip.kind !== 'answered') {
- return clearError
+ return { error: cleared.error, typed: 0 }
}
- return trip.result.success ? null : clearError
+ return trip.result.success ? { typed: 0 } : { error: cleared.error, typed: 0 }
}
/** The outcome of one round trip into the page. `silent` is its own case on
@@ -624,10 +623,14 @@ async function driveAction(
// typing over — never append to stale content. If real keystrokes cannot
// clear a masked field at all (money/date/number masks keep refilling it),
// fall back to setting the value directly through the DOM.
- const setError = await setFocusedField(run, input, text, signal)
+ const outcome = await setFocusedField(run, input, text, signal)
- if (setError) {
- return { error: setError, success: false }
+ if (outcome.error) {
+ return { error: outcome.error, success: false }
+ }
+
+ if (signal?.aborted) {
+ return stoppedType(signal, outcome.typed, text.length)
}
if (action.submit) {