Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(browser): report a page with nothing interactive as an empty snapshot (#20413)

Arvin Xu16bb62e7887b
diff --git a/packages/builtin-tool-browser/src/client/executor/index.test.ts b/packages/builtin-tool-browser/src/client/executor/index.test.ts
new file mode 100644
index 00000000000..b935e6a34e9
--- /dev/null
+++ b/packages/builtin-tool-browser/src/client/executor/index.test.ts
@@ -0,0 +1,67 @@
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+
+const snapshot = vi.fn();
+
+vi.mock('@/services/electron/browserControl', () => ({
+ electronBrowserControlService: { snapshot },
+}));
+
+const ctx = { agentId: 'agt_1', topicId: 'tpc_1' } as any;
+
+describe('browserExecutor.snapshot', () => {
+ beforeEach(() => {
+ snapshot.mockReset();
+ });
+
+ it('reports a page with no interactive elements as a successful, empty snapshot', async () => {
+ // e.g. an image URL or a JSON API response: the page loaded fine, there is
+ // just nothing to list.
+ snapshot.mockResolvedValue({
+ snapshot: '',
+ success: true,
+ title: 'api.printful.com/products/19',
+ url: 'https://api.printful.com/products/19',
+ });
+ const { browserExecutor } = await import('./index');
+
+ const result = await browserExecutor.snapshot({}, ctx);
+
+ expect(result.success).toBe(true);
+ expect(result.error).toBeUndefined();
+ expect(result.content).toContain(
+ 'Page: api.printful.com/products/19 (https://api.printful.com/products/19)',
+ );
+ expect(result.content).toContain('No interactive elements');
+ expect(result.content).toContain('readPage');
+ });
+
+ it('still lists elements when the page has them', async () => {
+ snapshot.mockResolvedValue({
+ snapshot: '- button "Continue" [ref=e1]',
+ success: true,
+ title: 'Login',
+ url: 'http://127.0.0.1:8099/login',
+ });
+ const { browserExecutor } = await import('./index');
+
+ const result = await browserExecutor.snapshot({}, ctx);
+
+ expect(result).toMatchObject({ success: true });
+ expect(result.content).toBe(
+ 'Page: Login (http://127.0.0.1:8099/login)\n- button "Continue" [ref=e1]',
+ );
+ });
+
+ it('keeps surfacing a real snapshot failure', async () => {
+ snapshot.mockResolvedValue({
+ error: 'Browser is not open for this conversation',
+ success: false,
+ });
+ const { browserExecutor } = await import('./index');
+
+ const result = await browserExecutor.snapshot({}, ctx);
+
+ expect(result.success).toBe(false);
+ expect(result.content).toBe('Browser is not open for this conversation');
+ });
+});
diff --git a/packages/builtin-tool-browser/src/client/executor/index.ts b/packages/builtin-tool-browser/src/client/executor/index.ts
index ac4afbb82ae..c7e710236a4 100644
--- a/packages/builtin-tool-browser/src/client/executor/index.ts
+++ b/packages/builtin-tool-browser/src/client/executor/index.ts
@@ -65,10 +65,19 @@ class BrowserExecutor extends BaseExecutor<typeof BrowserApiEnum> {
const result = await electronBrowserControlService.snapshot({
sessionId: this.sessionIdOf(ctx),
});
- if (!result.success || !result.snapshot)
- return this.failure(result.error ?? 'Snapshot failed');
+ if (!result.success) return this.failure(result.error ?? 'Snapshot failed');
const header = `Page: ${result.title ?? ''} (${result.url ?? ''})`;
+ // A page with nothing interactive in view (an image, a JSON response, a
+ // plain-text document) yields an empty snapshot. That is an answer, not a
+ // failure — reporting it as one sent agents into retry loops.
+ if (!result.snapshot) {
+ return this.success(
+ `${header}\nNo interactive elements (links, buttons, form fields, headings) are visible on this page. Use readPage to read its text, or screenshot to see it.`,
+ { snapshot: '', title: result.title, url: result.url },
+ );
+ }
+
return this.success(`${header}\n${result.snapshot}`, {
snapshot: result.snapshot,
title: result.title,
diff --git a/packages/builtin-tool-browser/vitest.config.mts b/packages/builtin-tool-browser/vitest.config.mts
index 4ac6027d578..e8853d408eb 100644
--- a/packages/builtin-tool-browser/vitest.config.mts
+++ b/packages/builtin-tool-browser/vitest.config.mts
@@ -1,6 +1,11 @@
+import { resolve } from 'node:path';
+
import { defineConfig } from 'vitest/config';
export default defineConfig({
+ resolve: {
+ alias: { '@': resolve(__dirname, '../../src') },
+ },
test: {
environment: 'node',
},