@lgrammel/js-code-executor-tool
v1.0.1
Published
AI SDK 7 tool for executing JavaScript code in an in-process Node.js vm context.
Readme
@lgrammel/js-code-executor-tool
AI SDK 7 tool for executing JavaScript code in an in-process Node.js vm context. Use it only with trusted agents and prompts: node:vm is not a security boundary for hostile code.
Install
bun add @lgrammel/js-code-executor-tool aiUsage
import { ToolLoopAgent } from "ai";
import { jsCodeExecutorTool } from "@lgrammel/js-code-executor-tool";
const agent = new ToolLoopAgent({
model,
instructions:
"Use the JavaScript executor for small calculations and data transformations. Return values directly or print with console.log.",
tools: {
executeJavaScript: jsCodeExecutorTool({
context: {
userId: "user_123",
},
timeoutMs: 5_000,
maxOutputBytes: 64 * 1024,
}),
},
});The model supplies only JavaScript source code. Code runs as an async function body, so it can use await, return a value, print with the captured console, and read copied values from context.
Options
context: values exposed to executed code as thecontextobject. Defaults to{}.timeoutMs: maximum execution time. Defaults to5000and is capped at60000.maxOutputBytes: maximum combined stdout and stderr retained. Defaults to65536and is capped at1048576. Console calls that exceed this limit throw.
Output
The tool returns:
result: formatted returned value, when the code returns something other thanundefined.stdout: capturedconsole.log,console.info,console.debug, and related output.stderr: capturedconsole.error,console.warn, and trace output.error: structured execution error, when the code throws or times out.timedOut: whether execution exceededtimeoutMs.outputTruncated: whether output exceededmaxOutputBytes.durationMs: execution duration in milliseconds.
