Merge pull request #545 from alibaba/feat/execute-js-abort-signal
feat(core): make execute_javascript honor AbortSignal
This commit is contained in:
@@ -181,12 +181,15 @@ tools.set(
|
||||
'execute_javascript',
|
||||
tool({
|
||||
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({
|
||||
script: z.string(),
|
||||
}),
|
||||
execute: async function (this: PageAgentCore, input) {
|
||||
const result = await this.pageController.executeJavascript(input.script)
|
||||
execute: async function (this: PageAgentCore, input, { signal }) {
|
||||
const result = await this.pageController.executeJavascript(input.script, signal)
|
||||
signal.throwIfAborted()
|
||||
return result.message
|
||||
},
|
||||
})
|
||||
|
||||
@@ -53,6 +53,8 @@ export class MultiPageAgent extends PageAgentCore {
|
||||
|
||||
super({
|
||||
...config,
|
||||
// Disabled: AbortSignal cannot cross contexts
|
||||
experimentalScriptExecutionTool: false,
|
||||
pageController: pageController as any,
|
||||
customTools: customTools,
|
||||
customSystemPrompt: systemPrompt,
|
||||
|
||||
@@ -133,9 +133,7 @@ export class RemotePageController {
|
||||
return this.remoteCallDomAction('scroll_horizontally', args)
|
||||
}
|
||||
|
||||
async executeJavascript(...args: any[]): Promise<DomActionReturn> {
|
||||
return this.remoteCallDomAction('execute_javascript', args)
|
||||
}
|
||||
// `execute_javascript` is intentionally not implemented: AbortSignal cannot cross context
|
||||
|
||||
/** @note Managed by content script via storage polling. */
|
||||
async showMask(): Promise<void> {}
|
||||
|
||||
@@ -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 {
|
||||
// Wrap script in async function to support await
|
||||
const asyncFunction = eval(`(async () => { ${script} })`)
|
||||
const result = await asyncFunction()
|
||||
// Wrap script in async function to support await, exposing `signal`.
|
||||
const asyncFunction = eval(`(async (signal) => { ${script} })`)
|
||||
const result = await asyncFunction(signal)
|
||||
return {
|
||||
success: true,
|
||||
message: `✅ Executed JavaScript. Result: ${result}`,
|
||||
|
||||
Reference in New Issue
Block a user