feat: add experimentalScriptExecutionTool

This commit is contained in:
Simon
2025-10-23 19:59:17 +08:00
parent e704398c43
commit 4d3c878809
5 changed files with 43 additions and 0 deletions

View File

@@ -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

View File

@@ -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,

View File

@@ -125,6 +125,10 @@ export class PageAgent extends EventTarget {
}
}
if (!this.config.experimentalScriptExecutionTool) {
this.tools.delete('execute_javascript')
}
patchReact(this)
window.addEventListener('beforeunload', (e) => {

View File

@@ -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

View File

@@ -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