Skip to content

Commit 7959115

Browse files
committed
improvement(desktop-browser): show the page at the end of every browser_wait_for
1 parent f69d049 commit 7959115

3 files changed

Lines changed: 36 additions & 4 deletions

File tree

‎apps/desktop/src/main/browser-agent/driver.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ import {
8888
} from '@/main/browser-agent/page-prompts'
8989
import { isPanelVisible, panelWindow } from '@/main/browser-agent/panel'
9090
import {
91+
observesAfterAction,
9192
withFailedPostActionObservation,
9293
withPostActionObservation,
9394
} from '@/main/browser-agent/post-action-observation'
@@ -5367,7 +5368,9 @@ export async function executeTool(
53675368
actionOutcome = outcome
53685369
}
53695370
)
5370-
if (params.observe !== undefined) actionOutcome = { status: 'acknowledged', result }
5371+
if (observesAfterAction(tool, params)) {
5372+
actionOutcome = { status: 'acknowledged', result }
5373+
}
53715374
return result
53725375
},
53735376
(query) =>
@@ -5392,7 +5395,7 @@ export async function executeTool(
53925395
tool === 'browser_snapshot' ||
53935396
tool === 'browser_open_url' ||
53945397
tool === 'browser_find' ||
5395-
params.observe !== undefined
5398+
observesAfterAction(tool, params)
53965399
) {
53975400
invalidateSnapshot(state)
53985401
}
@@ -5402,7 +5405,7 @@ export async function executeTool(
54025405
observedResult = await guardedExecution
54035406
} catch (error) {
54045407
if (!actionOutcome) {
5405-
if (params.observe !== undefined) cancelPendingSnapshotCapture(state)
5408+
if (observesAfterAction(tool, params)) cancelPendingSnapshotCapture(state)
54065409
throw error
54075410
}
54085411
invalidateSnapshot(state)

‎apps/desktop/src/main/browser-agent/post-action-observation.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,26 @@ import { describe, expect, it, vi } from 'vitest'
22
import { withPostActionObservation } from '@/main/browser-agent/post-action-observation'
33

44
describe('post-action observation', () => {
5+
it('shows the page after a wait without being asked, since a wait exists to see what changed', async () => {
6+
const result = await withPostActionObservation(
7+
'browser_wait_for',
8+
{ timeoutMs: 500 },
9+
async (params) => {
10+
expect(params).toEqual({ timeoutMs: 500 })
11+
return { found: false }
12+
},
13+
async (query) => {
14+
expect(query).toBeUndefined()
15+
return { outline: '- heading "Order confirmed"' }
16+
},
17+
vi.fn()
18+
)
19+
expect(result).toMatchObject({
20+
found: false,
21+
observation: { ok: true, result: { outline: '- heading "Order confirmed"' } },
22+
})
23+
})
24+
525
it('observes after the action, preserving partial form results and replacing refs', async () => {
626
const order: string[] = []
727
const result = await withPostActionObservation(

‎apps/desktop/src/main/browser-agent/post-action-observation.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,17 @@ const OBSERVABLE_ACTIONS: ReadonlySet<BrowserToolName> = new Set([
1717
'browser_hover',
1818
'browser_drag',
1919
'browser_batch',
20+
'browser_wait_for',
2021
])
2122

23+
/** Waits exist to see what the page became, so they observe even when the model omits observe. */
24+
const OBSERVES_BY_DEFAULT: ReadonlySet<BrowserToolName> = new Set(['browser_wait_for'])
25+
26+
/** Whether this call ends with a post-action observation, requested or by default. */
27+
export function observesAfterAction(tool: BrowserToolName, params: Record<string, unknown>): boolean {
28+
return params.observe !== undefined || OBSERVES_BY_DEFAULT.has(tool)
29+
}
30+
2231
/** Preserves a dispatched action when its acknowledgement or observation is interrupted. */
2332
export function withFailedPostActionObservation(result: unknown, error: unknown): unknown {
2433
const observation = {
@@ -38,7 +47,7 @@ export async function withPostActionObservation(
3847
observe: (query: string | undefined) => Promise<unknown>,
3948
assertCurrent: () => void
4049
): Promise<unknown> {
41-
const request = params.observe
50+
const request = params.observe ?? (OBSERVES_BY_DEFAULT.has(tool) ? {} : undefined)
4251
if (request === undefined) return action(params)
4352
if (
4453
!OBSERVABLE_ACTIONS.has(tool) ||

0 commit comments

Comments
 (0)