Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions apps/desktop/src/main/browser-agent/driver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,34 @@ describe('executeTool', () => {
}
)

it('waits for a tab still loading its first page instead of calling it blank', async () => {
await driver.executeTool('chat-test', 'browser_open_tab', {})
const contents = session.requireTab().view.webContents
vi.mocked(contents.getURL).mockReturnValue('')
vi.mocked(contents.isLoading).mockReturnValue(true)
const loadListeners = () =>
vi
.mocked(contents.on)
.mock.calls.filter(([event]) => String(event) === 'did-stop-loading')
.map(([, listener]) => listener as () => void)
const registered = loadListeners().length
const evaluated = new Promise<void>((resolve) => {
vi.mocked(contents.executeJavaScript).mockImplementation(() => {
resolve()
return new Promise<never>(() => {})
})
})

void driver.executeTool('chat-test', 'browser_snapshot', {})
await vi.waitFor(() => expect(loadListeners().length).toBeGreaterThan(registered))
expect(contents.executeJavaScript).not.toHaveBeenCalled()

vi.mocked(contents.getURL).mockReturnValue('http://127.0.0.1/popup')
vi.mocked(contents.isLoading).mockReturnValue(false)
for (const onLoadComplete of loadListeners().slice(registered)) onLoadComplete()
await evaluated
})

it('leaves no watchdog timer pending once a tool finishes', async () => {
vi.useFakeTimers()
try {
Expand Down
9 changes: 7 additions & 2 deletions apps/desktop/src/main/browser-agent/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1385,8 +1385,13 @@ async function execInPage<Args extends unknown[], Result>(
userGesture = false,
notAfter?: number
): Promise<Result> {
const url = 'getURL' in target ? target.getURL() : target.url
if ('getURL' in target && (url === '' || url === 'about:blank')) {
const isBlank = (url: string) => url === '' || url === 'about:blank'
// A tab a link or popup just opened reports no URL until its first page commits.
if ('getURL' in target && isBlank(target.getURL()) && target.isLoading()) {
const remaining = notAfter === undefined ? NAVIGATION_TIMEOUT_MS : notAfter - Date.now()
await waitForLoadComplete(target, Math.max(0, Math.min(NAVIGATION_TIMEOUT_MS, remaining)))
Comment thread
waleedlatif1 marked this conversation as resolved.
Resolved
Comment thread
waleedlatif1 marked this conversation as resolved.
Resolved
}
if ('getURL' in target && isBlank(target.getURL())) {
throw new ToolError(
'The active tab is blank. Call browser_navigate before using page inspection or interaction tools.'
)
Expand Down
Loading