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(desktop): let the browser fill tool choose a native select option (#20416)

Arvin Xu8cf3cfd9afa8
diff --git a/apps/desktop/src/main/controllers/BrowserControlCtr.ts b/apps/desktop/src/main/controllers/BrowserControlCtr.ts
index f43fa6a7d95..7e66173d352 100644
--- a/apps/desktop/src/main/controllers/BrowserControlCtr.ts
+++ b/apps/desktop/src/main/controllers/BrowserControlCtr.ts
@@ -114,11 +114,29 @@ const resolveRefScript = (ref: string) => `((ref) => {
return JSON.stringify({ x: Math.round(r.x + r.width / 2), y: Math.round(r.y + r.height / 2) });
})(${JSON.stringify(ref)})`;
-const fillScript = (ref: string, text: string) => `((ref, text) => {
+export const fillScript = (ref: string, text: string) => `((ref, text) => {
const el = window.__lobeBrowserRefs && window.__lobeBrowserRefs[ref];
if (!el || !el.isConnected) return JSON.stringify({ error: 'ref not found — take a new snapshot first' });
el.scrollIntoView({ behavior: 'instant', block: 'center', inline: 'center' });
el.focus();
+ // A native <select> has no text to type into, and calling the input value
+ // setter on it throws. Pick the option by value, then by visible label.
+ if (el.tagName === 'SELECT') {
+ const options = Array.from(el.options);
+ const label = (option) => option.text.trim().replaceAll(/\\s+/g, ' ').toLowerCase();
+ const wanted = String(text).trim().replaceAll(/\\s+/g, ' ').toLowerCase();
+ const option = options.find((o) => o.value === text) || options.find((o) => label(o) === wanted);
+ if (!option) {
+ const names = options.map((o) => o.text.trim()).slice(0, 30).join(', ');
+ return JSON.stringify({ error: 'no option matches "' + text + '"; options: ' + names });
+ }
+ // Assign by index: option values need not be unique (an empty placeholder
+ // and an empty "None"), and el.value would pick the first match.
+ el.selectedIndex = options.indexOf(option);
+ el.dispatchEvent(new Event('input', { bubbles: true }));
+ el.dispatchEvent(new Event('change', { bubbles: true }));
+ return JSON.stringify({ ok: true });
+ }
if (el.isContentEditable) {
el.textContent = text;
el.dispatchEvent(new InputEvent('input', { bubbles: true }));
diff --git a/apps/desktop/src/main/controllers/__tests__/BrowserControlCtr.fill.test.ts b/apps/desktop/src/main/controllers/__tests__/BrowserControlCtr.fill.test.ts
new file mode 100644
index 00000000000..112818271ce
--- /dev/null
+++ b/apps/desktop/src/main/controllers/__tests__/BrowserControlCtr.fill.test.ts
@@ -0,0 +1,63 @@
+/**
+ * @vitest-environment happy-dom
+ */
+import { describe, expect, it, vi } from 'vitest';
+
+import { fillScript } from '../BrowserControlCtr';
+
+vi.mock('electron', () => ({
+ ipcMain: { handle: vi.fn() },
+}));
+
+const fillSelect = (text: string) => {
+ document.body.innerHTML = `
+ <select aria-label="Card type">
+ <option value="attack">Attack</option>
+ <option value="skill">Skill</option>
+ <option value="power">Power Card</option>
+ </select>`;
+ const select = document.querySelector('select')!;
+ const changes: string[] = [];
+ select.addEventListener('change', () => changes.push(select.value));
+ (window as any).__lobeBrowserRefs = { e1: select };
+
+ const result = JSON.parse((0, eval)(fillScript('e1', text)));
+ return { changes, result, value: select.value };
+};
+
+describe('fillScript on a native select', () => {
+ it('chooses the option by its visible label and fires change', () => {
+ expect(fillSelect('Skill')).toEqual({
+ changes: ['skill'],
+ result: { ok: true },
+ value: 'skill',
+ });
+ });
+
+ it('chooses the option by value, and matches labels ignoring case and spacing', () => {
+ expect(fillSelect('power').value).toBe('power');
+ expect(fillSelect('power card').value).toBe('power');
+ });
+
+ it('selects the matched option itself when option values repeat', () => {
+ document.body.innerHTML = `
+ <select aria-label="Owner">
+ <option value="">Choose…</option>
+ <option value="">None</option>
+ <option value="ann">Ann</option>
+ </select>`;
+ const select = document.querySelector('select')!;
+ (window as any).__lobeBrowserRefs = { e1: select };
+
+ expect(JSON.parse((0, eval)(fillScript('e1', 'None')))).toEqual({ ok: true });
+
+ expect(select.selectedIndex).toBe(1);
+ });
+
+ it('lists the available options when nothing matches', () => {
+ const { result, value } = fillSelect('Curse');
+
+ expect(value).toBe('attack');
+ expect(result.error).toBe('no option matches "Curse"; options: Attack, Skill, Power Card');
+ });
+});
diff --git a/apps/desktop/src/main/modules/heterogeneousAgent/browserMcpTools.ts b/apps/desktop/src/main/modules/heterogeneousAgent/browserMcpTools.ts
index 952d0ea1512..b40d3776f5e 100644
--- a/apps/desktop/src/main/modules/heterogeneousAgent/browserMcpTools.ts
+++ b/apps/desktop/src/main/modules/heterogeneousAgent/browserMcpTools.ts
@@ -60,14 +60,14 @@ export const BROWSER_MCP_TOOLS: BrowserMcpToolSpec[] = [
{
apiName: 'fill',
description:
- 'Fill a text input, textarea, or contenteditable in the in-app browser identified by a snapshot ref. Set submit=true to press Enter afterwards.',
+ 'Fill a text input, textarea, or contenteditable in the in-app browser identified by a snapshot ref, or choose an option of a native select (combobox) by its value or visible label. Set submit=true to press Enter afterwards.',
inputSchema: {
ref: z.string().describe('Element ref from the latest snapshot.'),
submit: z
.boolean()
.optional()
.describe('Press Enter after filling (submit search/login forms).'),
- text: z.string().describe('Text to fill.'),
+ text: z.string().describe('Text to fill; for a select, the option value or visible label.'),
},
name: 'browser_fill',
title: 'Fill input in in-app browser',
diff --git a/packages/builtin-tool-browser/src/manifest.ts b/packages/builtin-tool-browser/src/manifest.ts
index 31880936dcb..7c86821cef8 100644
--- a/packages/builtin-tool-browser/src/manifest.ts
+++ b/packages/builtin-tool-browser/src/manifest.ts
@@ -53,7 +53,7 @@ export const BrowserManifest: BuiltinToolManifest = {
},
{
description:
- 'Fill a text input, textarea, or contenteditable identified by a snapshot ref. Set submit=true to press Enter afterwards.',
+ 'Fill a text input, textarea, or contenteditable identified by a snapshot ref, or choose an option of a native select (combobox) by its value or visible label. Set submit=true to press Enter afterwards.',
name: BrowserApiName.fill,
parameters: {
properties: {
@@ -66,7 +66,7 @@ export const BrowserManifest: BuiltinToolManifest = {
type: 'boolean',
},
text: {
- description: 'Text to fill.',
+ description: 'Text to fill; for a select, the option value or visible label.',
type: 'string',
},
},