From b19ca760aa8aa5deeb04528a9cc496afffae80ed Mon Sep 17 00:00:00 2001 From: Dharma Date: Sat, 17 Jan 2026 16:53:24 -0800 Subject: [PATCH] fix: support URL parameter in tab new command (#64) * fix: support URL parameter in tab new command The CLI was correctly sending the URL parameter when running `agent-browser tab new `, but the TypeScript daemon was ignoring it because: 1. The schema didn't include the url field (stripped during validation) 2. The TabNewCommand type didn't have a url property 3. The handler didn't pass the URL to browser.newTab() 4. browser.newTab() didn't accept or use a URL parameter This fix adds URL support throughout the chain so that `agent-browser tab new https://example.com` now correctly opens a new tab and navigates to the specified URL. Fixes #62 * fix: omit url field when not provided in tab new command Previously, the CLI always sent "url": null when no URL was provided, which caused Zod validation to fail with "Expected string, received null". Now the url field is only included when a URL is actually provided. Fixes issue reported by @ctate in PR review. * refactor: move navigation logic from BrowserManager to handleTabNew Address review feedback: - Add .min(1) to URL validation for consistency with navigateSchema - Keep BrowserManager.newTab() simple (single responsibility) - Handle navigation in handleTabNew following same pattern as handleNavigate --- cli/src/commands.rs | 16 +++++++++++++++- src/actions.ts | 10 +++++++++- src/protocol.test.ts | 8 ++++++++ src/protocol.ts | 1 + src/types.ts | 1 + 5 files changed, 34 insertions(+), 2 deletions(-) diff --git a/cli/src/commands.rs b/cli/src/commands.rs index c7cca7b..74b84d5 100644 --- a/cli/src/commands.rs +++ b/cli/src/commands.rs @@ -367,7 +367,13 @@ pub fn parse_command(args: &[String], flags: &Flags) -> Result { match rest.get(0).map(|s| *s) { - Some("new") => Ok(json!({ "id": id, "action": "tab_new", "url": rest.get(1) })), + Some("new") => { + let mut cmd = json!({ "id": id, "action": "tab_new" }); + if let Some(url) = rest.get(1) { + cmd["url"] = json!(url); + } + Ok(cmd) + } Some("list") => Ok(json!({ "id": id, "action": "tab_list" })), Some("close") => { Ok(json!({ "id": id, "action": "tab_close", "index": rest.get(1).and_then(|s| s.parse::().ok()) })) @@ -1238,6 +1244,14 @@ mod tests { fn test_tab_new() { let cmd = parse_command(&args("tab new"), &default_flags()).unwrap(); assert_eq!(cmd["action"], "tab_new"); + assert!(cmd.get("url").is_none(), "url should not be present when not provided"); + } + + #[test] + fn test_tab_new_with_url() { + let cmd = parse_command(&args("tab new https://example.com"), &default_flags()).unwrap(); + assert_eq!(cmd["action"], "tab_new"); + assert_eq!(cmd["url"], "https://example.com"); } #[test] diff --git a/src/actions.ts b/src/actions.ts index 66a2388..a5fd163 100644 --- a/src/actions.ts +++ b/src/actions.ts @@ -26,6 +26,7 @@ import type { SelectCommand, HoverCommand, ContentCommand, + TabNewCommand, TabSwitchCommand, TabCloseCommand, WindowNewCommand, @@ -709,10 +710,17 @@ async function handleClose( } async function handleTabNew( - command: Command & { action: 'tab_new' }, + command: TabNewCommand, browser: BrowserManager ): Promise> { const result = await browser.newTab(); + + // Navigate to URL if provided (same pattern as handleNavigate) + if (command.url) { + const page = browser.getPage(); + await page.goto(command.url, { waitUntil: 'domcontentloaded' }); + } + return successResponse(command.id, result); } diff --git a/src/protocol.test.ts b/src/protocol.test.ts index 720306f..25fa77e 100644 --- a/src/protocol.test.ts +++ b/src/protocol.test.ts @@ -373,6 +373,14 @@ describe('parseCommand', () => { expect(result.success).toBe(true); }); + it('should parse tab_new with url', () => { + const result = parseCommand(cmd({ id: '1', action: 'tab_new', url: 'https://example.com' })); + expect(result.success).toBe(true); + if (result.success) { + expect((result.command as { url?: string }).url).toBe('https://example.com'); + } + }); + it('should parse tab_list', () => { const result = parseCommand(cmd({ id: '1', action: 'tab_list' })); expect(result.success).toBe(true); diff --git a/src/protocol.ts b/src/protocol.ts index dc3de79..4f91f0a 100644 --- a/src/protocol.ts +++ b/src/protocol.ts @@ -730,6 +730,7 @@ const closeSchema = baseCommandSchema.extend({ // Tab/Window schemas const tabNewSchema = baseCommandSchema.extend({ action: z.literal('tab_new'), + url: z.string().min(1).optional(), }); const tabListSchema = baseCommandSchema.extend({ diff --git a/src/types.ts b/src/types.ts index 07da4f0..d0cc565 100644 --- a/src/types.ts +++ b/src/types.ts @@ -772,6 +772,7 @@ export interface CloseCommand extends BaseCommand { // Tab/Window commands export interface TabNewCommand extends BaseCommand { action: 'tab_new'; + url?: string; } export interface TabListCommand extends BaseCommand {