diff --git a/packages/agent-client/src/cli.js b/packages/agent-client/src/cli.js index cb806ba..592989b 100755 --- a/packages/agent-client/src/cli.js +++ b/packages/agent-client/src/cli.js @@ -665,18 +665,27 @@ async function main() { } if (command === 'eval') { - let expression = rest.join(' '); + const { tabId, rest: evalArgs } = extractTabFlag(rest); + // --await: wait for the result if the expression returns a Promise + const awaitIndex = evalArgs.indexOf('--await'); + const awaitPromise = awaitIndex !== -1; + if (awaitIndex !== -1) evalArgs.splice(awaitIndex, 1); + + let expression = evalArgs.join(' '); if (!expression || expression === '-') expression = await readStdin(); if (!expression) - throw new Error('Usage: eval (or pipe via stdin: echo "expr" | bbx eval -)'); + throw new Error( + 'Usage: eval [--tab ] [--await] (or pipe via stdin: echo "expr" | bbx eval -)' + ); const response = await requestBridge( client, 'page.evaluate', { expression, returnByValue: true, + ...(awaitPromise ? { awaitPromise: true } : {}), }, - { source: REQUEST_SOURCE } + { source: REQUEST_SOURCE, tabId } ); await printSummary(response); return; diff --git a/packages/agent-client/src/command-registry.js b/packages/agent-client/src/command-registry.js index a767f59..c5655cf 100644 --- a/packages/agent-client/src/command-registry.js +++ b/packages/agent-client/src/command-registry.js @@ -294,7 +294,7 @@ export const CLI_HELP_SECTIONS = Object.freeze([ { title: 'Page', lines: [ - 'bbx eval Evaluate JS in page context (use - for stdin)', + 'bbx eval [--await] Evaluate JS in page context (--await for async, - for stdin)', ...[ 'console', 'network', diff --git a/packages/agent-client/test/cli-commands.test.ts b/packages/agent-client/test/cli-commands.test.ts index 8a31d8f..d2dd8e3 100644 --- a/packages/agent-client/test/cli-commands.test.ts +++ b/packages/agent-client/test/cli-commands.test.ts @@ -1166,6 +1166,6 @@ test('bbx eval with empty stdin reports the usage error', async () => { assert.equal(payload.evidence, null); assert.equal( payload.summary, - 'ERROR: Usage: eval (or pipe via stdin: echo "expr" | bbx eval -)' + 'ERROR: Usage: eval [--tab ] [--await] (or pipe via stdin: echo "expr" | bbx eval -)' ); });