feat(core): make execute_javascript honor AbortSignal

Expose the task AbortSignal as `signal` in the script scope so cooperative
code can cancel promptly, and re-check signal.throwIfAborted() after the
script settles to discard stale results.

Closes #537.
This commit is contained in:
Simon
2026-06-09 21:30:27 +08:00
parent 73810b3ed8
commit 9ed0a09194
3 changed files with 17 additions and 10 deletions

View File

@@ -181,12 +181,15 @@ tools.set(
'execute_javascript', 'execute_javascript',
tool({ tool({
description: description:
'Execute JavaScript code on the current page. Supports async/await syntax. Use with caution!', 'Execute JavaScript code on the current page. Supports async/await syntax. Use with caution! ' +
'An `AbortSignal` named `signal` is available in scope: long-running async code MUST honor it ' +
'(e.g. `await fetch(url, { signal })`, or `signal.throwIfAborted()` in loops)',
inputSchema: z.object({ inputSchema: z.object({
script: z.string(), script: z.string(),
}), }),
execute: async function (this: PageAgentCore, input) { execute: async function (this: PageAgentCore, input, { signal }) {
const result = await this.pageController.executeJavascript(input.script) const result = await this.pageController.executeJavascript(input.script, signal)
signal.throwIfAborted()
return result.message return result.message
}, },
}) })

View File

@@ -133,8 +133,10 @@ export class RemotePageController {
return this.remoteCallDomAction('scroll_horizontally', args) return this.remoteCallDomAction('scroll_horizontally', args)
} }
async executeJavascript(...args: any[]): Promise<DomActionReturn> { async executeJavascript(script: string, _signal?: AbortSignal): Promise<DomActionReturn> {
return this.remoteCallDomAction('execute_javascript', args) // `AbortSignal` is not structured-cloneable across the messaging boundary.
// The remote script runs without it
return this.remoteCallDomAction('execute_javascript', [script])
} }
/** @note Managed by content script via storage polling. */ /** @note Managed by content script via storage polling. */

View File

@@ -376,13 +376,15 @@ export class PageController extends EventTarget {
} }
/** /**
* Execute arbitrary JavaScript on the page * Execute arbitrary JavaScript on the page.
* The optional `signal` is exposed to the script scope so cooperative code
* can abort promptly when the task is stopped.
*/ */
async executeJavascript(script: string): Promise<ActionResult> { async executeJavascript(script: string, signal?: AbortSignal): Promise<ActionResult> {
try { try {
// Wrap script in async function to support await // Wrap script in async function to support await, exposing `signal`.
const asyncFunction = eval(`(async () => { ${script} })`) const asyncFunction = eval(`(async (signal) => { ${script} })`)
const result = await asyncFunction() const result = await asyncFunction(signal)
return { return {
success: true, success: true,
message: `✅ Executed JavaScript. Result: ${result}`, message: `✅ Executed JavaScript. Result: ${result}`,