From 3e84751884b31ee5f0a041713747be65398ff5c2 Mon Sep 17 00:00:00 2001 From: garrytan Date: Thu, 8 Oct 2026 06:19:19 +0000 Subject: [PATCH] fix(browse): screenshot accepts --clip=x,y,w,h and --selector= Agent-generated commands often use the GNU `--flag=value` form, which threw "Unknown screenshot flag". Both value flags now accept it, through the same validation and conflict checks as the space-separated form. Adopts #1563. Co-authored-by: techcenter68 <75930234+techcenter68@users.noreply.github.com> Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> --- browse/src/meta-commands.ts | 15 +++++++++------ browse/test/commands.test.ts | 24 ++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/browse/src/meta-commands.ts b/browse/src/meta-commands.ts index 38d4b0593..b59247504 100644 --- a/browse/src/meta-commands.ts +++ b/browse/src/meta-commands.ts @@ -447,15 +447,18 @@ export async function handleMetaCommand( const remaining: string[] = []; let flagSelector: string | undefined; for (let i = 0; i < args.length; i++) { - if (args[i] === '--viewport') { + const inline = /^(--clip|--selector)=(.*)$/s.exec(args[i]); + const flag = inline ? inline[1] : args[i]; + const flagValue = () => (inline ? inline[2] : args[++i]); + if (flag === '--viewport') { viewportOnly = true; - } else if (args[i] === '--base64') { + } else if (flag === '--base64') { base64Mode = true; - } else if (args[i] === '--selector') { - flagSelector = args[++i]; + } else if (flag === '--selector') { + flagSelector = flagValue(); if (!flagSelector) throw new Error('Usage: screenshot --selector [path]'); - } else if (args[i] === '--clip') { - const coords = args[++i]; + } else if (flag === '--clip') { + const coords = flagValue(); if (!coords) throw new Error('Usage: screenshot --clip x,y,w,h [path]'); const parts = coords.split(',').map(Number); if (parts.length !== 4 || parts.some(isNaN)) diff --git a/browse/test/commands.test.ts b/browse/test/commands.test.ts index 5ea8d8823..427350c8d 100644 --- a/browse/test/commands.test.ts +++ b/browse/test/commands.test.ts @@ -790,6 +790,30 @@ describe('Visual', () => { } }); + test('screenshot accepts --clip=x,y,w,h and --selector=', async () => { + await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm); + const p = tmpp(`browse-test-clip-eq-${Date.now()}.png`); + try { + expect(await handleMetaCommand('screenshot', ['--clip=0,0,10,10', p], bm, async () => {})).toContain('Screenshot saved (clip 0,0,10,10)'); + const png = fs.readFileSync(p); + expect([png.readUInt32BE(16), png.readUInt32BE(20)]).toEqual([10, 10]); + expect(await handleMetaCommand('screenshot', ['--selector=#title', p], bm, async () => {})).toContain('Screenshot saved (element)'); + } finally { + fs.rmSync(p, { force: true }); + } + }); + + test('screenshot --clip= keeps the usage errors and the selector conflict', async () => { + await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm); + const run = (args: string[]) => handleMetaCommand('screenshot', args, bm, async () => {}); + await expect(run(['--clip='])).rejects.toThrow('Usage: screenshot --clip x,y,w,h'); + await expect(run(['--clip=1,2,3'])).rejects.toThrow('all must be numbers'); + await expect(run(['--selector='])).rejects.toThrow('Usage: screenshot --selector'); + await expect(run(['--clip=0,0,10,10', '#title'])).rejects.toThrow('Cannot use --clip with a selector/ref'); + await expect(run(['--selector=#title', '--clip=0,0,10,10'])).rejects.toThrow('Cannot use --clip with a selector/ref'); + await expect(run(['--viewport=1'])).rejects.toThrow('Unknown screenshot flag'); + }); + test('screenshot unknown flag throws', async () => { await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm); try {