Files
deepagentsjs/examples/repl/data-analysis-agent.ts
Colin Francis 254895457f chore(quickjs): rename to REPLMiddleware and adjust defaults (#520)
### Summary

- Rename `createQuickJSMiddleware` → `createREPLMiddleware` and
`QuickJSMiddlewareOptions` → `REPLMiddlewareOptions` to match the Python
`REPLMiddleware` naming
- Rename middleware registration from `"QuickJSMiddleware"` →
`"REPLMiddleware"`
- Rename default tool name from `"js_eval"` → `"eval"`, now configurable
via `toolName` option
- Add `captureConsole` option (default `true`) to toggle console output
capture
- Align default memory limit to 64 MiB (was 50 MiB) and timeout to 5s
(was 30s) to match Python

### Tests

- All 156 existing unit tests pass with updated references
- Updated `middleware.test.ts`, `middleware.int.test.ts`, and example
files to use new names

---------

Co-authored-by: Hunter Lovell <40191806+hntrl@users.noreply.github.com>
2026-05-04 15:33:17 -07:00

50 lines
1.6 KiB
TypeScript

/**
* Data Analysis Agent Example
*
* Demonstrates the QuickJS REPL as a computational scratch pad.
* The agent reads data from the VFS, processes it in sandboxed JavaScript,
* and writes results back — all without network access or Node.js APIs.
*
* This is useful for tasks where LLMs typically hallucinate:
* - Arithmetic and statistical calculations
* - Sorting, filtering, grouping data
* - JSON transformation and restructuring
* - Multi-step logic with intermediate state
*/
import "dotenv/config";
import dedent from "dedent";
import { HumanMessage } from "@langchain/core/messages";
import { ChatAnthropic } from "@langchain/anthropic";
import { createDeepAgent } from "deepagents";
import { createREPLMiddleware } from "@langchain/quickjs";
const model = new ChatAnthropic({
model: "claude-sonnet-4-5",
temperature: 0,
});
const agent = createDeepAgent({
model,
systemPrompt: dedent`
You are a data analyst. Use the eval REPL to perform calculations
and data transformations. Always show your work in code — never guess
at arithmetic or statistics.
`,
middleware: [createREPLMiddleware()],
});
const result = await agent.invoke({
messages: [
new HumanMessage(dedent`
I have sales data in /data/sales.json. Parse it, calculate the total
revenue per region, find the top-performing region, and write a
summary report to /reports/sales-summary.md.
`),
],
});
const last = result.messages[result.messages.length - 1];
console.log(
typeof last.content === "string" ? last.content.slice(0, 500) : last.content,
);