From 4d3c878809b6e30632170e10224a30e05d5f4cba Mon Sep 17 00:00:00 2001 From: Simon <10131203+gaomeng1900@users.noreply.github.com> Date: Thu, 23 Oct 2025 19:59:17 +0800 Subject: [PATCH] feat: add experimentalScriptExecutionTool --- pages/docs/integration/configuration/page.tsx | 8 +++++++ pages/page.tsx | 2 ++ src/PageAgent.ts | 4 ++++ src/config/index.ts | 8 +++++++ src/tools/index.ts | 21 +++++++++++++++++++ 5 files changed, 43 insertions(+) diff --git a/pages/docs/integration/configuration/page.tsx b/pages/docs/integration/configuration/page.tsx index 3ebff5a..3eb3944 100644 --- a/pages/docs/integration/configuration/page.tsx +++ b/pages/docs/integration/configuration/page.tsx @@ -70,6 +70,14 @@ interface AgentConfig { // page behavior hooks + /** + * @experimental + * Enable the experimental script execution tool that allows executing generated JavaScript code on the page. + * @note Can cause unpredictable side effects. + * @note May bypass some safe guards and data-masking mechanisms. + */ + experimentalScriptExecutionTool?: boolean + /** * TODO: @unimplemented * hook when action causes a new page to be opened diff --git a/pages/page.tsx b/pages/page.tsx index 2eaf58c..022837b 100644 --- a/pages/page.tsx +++ b/pages/page.tsx @@ -53,6 +53,8 @@ export default function HomePage() { interactiveBlacklist: [document.getElementById('root')!], language: i18n.language as any, + // experimentalScriptExecutionTool: true, + // testing server // @note: rate limit. prompt limit. // model: DEMO_MODEL, diff --git a/src/PageAgent.ts b/src/PageAgent.ts index fe5457a..a8fca60 100644 --- a/src/PageAgent.ts +++ b/src/PageAgent.ts @@ -125,6 +125,10 @@ export class PageAgent extends EventTarget { } } + if (!this.config.experimentalScriptExecutionTool) { + this.tools.delete('execute_javascript') + } + patchReact(this) window.addEventListener('beforeunload', (e) => { diff --git a/src/config/index.ts b/src/config/index.ts index a99e239..5e07e63 100644 --- a/src/config/index.ts +++ b/src/config/index.ts @@ -72,6 +72,14 @@ export interface AgentConfig { // page behavior hooks + /** + * @experimental + * Enable the experimental script execution tool that allows executing generated JavaScript code on the page. + * @note Can cause unpredictable side effects. + * @note May bypass some safe guards and data-masking mechanisms. + */ + experimentalScriptExecutionTool?: boolean + /** * TODO: @unimplemented * hook when action causes a new page to be opened diff --git a/src/tools/index.ts b/src/tools/index.ts index 1267166..c1d676b 100644 --- a/src/tools/index.ts +++ b/src/tools/index.ts @@ -215,6 +215,27 @@ tools.set( }) ) +tools.set( + 'execute_javascript', + tool({ + description: + 'Execute JavaScript code on the current page. Supports async/await syntax. Use with caution!', + inputSchema: zod.object({ + script: zod.string(), + }), + execute: async function (this: PageAgent, input) { + try { + // Wrap script in async function to support await + const asyncFunction = eval(`(async () => { ${input.script} })`) + const result = await asyncFunction() + return `✅ Executed JavaScript. Result: ${result}` + (await getSystemInfo()) + } catch (error) { + return `❌ Error executing JavaScript: ${error}` + (await getSystemInfo()) + } + }, + }) +) + // @todo get_dropdown_options // @todo select_dropdown_option // @todo send_keys