Repository navigation
Expand file tree
/
Copy pathrun-code.ts
More file actions
71 lines (59 loc) · 2.21 KB
/
Copy pathrun-code.ts
File metadata and controls
71 lines (59 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import z from "zod"
import { Tool } from "@/tool/tool"
import { BrowserManager } from "@/browser/manager"
import { Log } from "@/util/log"
const log = Log.create({ service: "browser-tools" })
const DESCRIPTION = `Run custom JavaScript/TypeScript code in the browser page context.
Executes arbitrary code with access to DOM, window, document, etc.
Returns the result of the last expression.
Parameters:
- code (string, required): JavaScript/TypeScript code to execute
- timeout (number, optional): Maximum execution time in milliseconds (default: 30000)
Example:
- code: "document.querySelectorAll('a').length" returns the number of links
- code: "window.innerWidth" returns the viewport width
- code: "localStorage.getItem('key')" reads from localStorage
`
export const BrowserRunCodeTool = Tool.define("browser_run_code", {
description: DESCRIPTION,
parameters: z.object({
code: z.string().describe("JavaScript/TypeScript code to execute"),
timeout: z.number().default(30000).describe("Maximum execution time in milliseconds"),
}),
async execute(params, ctx) {
await ctx.ask({
permission: "browser",
patterns: ["run_code"],
always: ["*"],
metadata: { action: "run_code", codeLength: params.code.length },
})
log.info("running code", { codeLength: params.code.length, timeout: params.timeout })
try {
const response = await BrowserManager.runCode(params.code)
if (!response.success) {
throw new Error(response.error || "Unknown error")
}
const result = response.result
let output: string
if (result === undefined) {
output = "(undefined)"
} else if (result === null) {
output = "(null)"
} else if (typeof result === "object") {
output = JSON.stringify(result, null, 2)
} else {
output = String(result)
}
return {
title: "Code executed",
metadata: { resultType: typeof result },
output: output,
}
} catch (error) {
const message = error instanceof Error ? error.message : String(error)
log.error("run code failed", { error: message })
throw new Error(`Code execution failed: ${message}`)
}
},
})
Tool.attachExecute(BrowserRunCodeTool)