@langchain/quickjs 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +166 -0
- package/dist/index.cjs +787 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +204 -0
- package/dist/index.d.ts +204 -0
- package/dist/index.js +749 -0
- package/dist/index.js.map +1 -0
- package/package.json +86 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) LangChain, Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
# @langchain/quickjs
|
|
2
|
+
|
|
3
|
+
Sandboxed JavaScript/TypeScript REPL for [deepagents](https://github.com/langchain-ai/deepagentsjs), powered by [QuickJS-NG](https://github.com/quickjs-ng/quickjs) through [QuickJS-Emscripten](https://github.com/justjake/quickjs-emscripten)
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@langchain/quickjs)
|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @langchain/quickjs deepagents
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Quick Start
|
|
15
|
+
|
|
16
|
+
```typescript
|
|
17
|
+
import { createDeepAgent } from "deepagents";
|
|
18
|
+
import { createQuickJSMiddleware } from "@langchain/quickjs";
|
|
19
|
+
|
|
20
|
+
const agent = createDeepAgent({
|
|
21
|
+
model: "claude-sonnet-4-5",
|
|
22
|
+
middleware: [createQuickJSMiddleware()],
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
const result = await agent.invoke({
|
|
26
|
+
messages: [
|
|
27
|
+
{ role: "user", content: "Calculate the first 20 Fibonacci numbers" },
|
|
28
|
+
],
|
|
29
|
+
});
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
The agent now has a `js_eval` tool. It can write and execute JavaScript/TypeScript in a sandboxed REPL where variables persist across calls:
|
|
33
|
+
|
|
34
|
+
```typescript
|
|
35
|
+
// Call 1: the agent writes
|
|
36
|
+
var fibs = [0, 1];
|
|
37
|
+
for (let i = 2; i < 20; i++) fibs.push(fibs[i - 1] + fibs[i - 2]);
|
|
38
|
+
console.log(fibs);
|
|
39
|
+
|
|
40
|
+
// Call 2: state persists — `fibs` is still available
|
|
41
|
+
console.log(`Sum: ${fibs.reduce((a, b) => a + b, 0)}`);
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Features
|
|
45
|
+
|
|
46
|
+
### WASM Sandbox
|
|
47
|
+
|
|
48
|
+
All code runs inside a QuickJS WASM interpreter. There is no `require`, no `import`, no `fetch`, no filesystem access — only the explicitly bridged helpers (`readFile`, `writeFile`, and optionally `tools.*`).
|
|
49
|
+
|
|
50
|
+
### TypeScript Support
|
|
51
|
+
|
|
52
|
+
LLMs naturally produce TypeScript. An AST-based transform pipeline strips type annotations, interfaces, and generics before evaluation — the model doesn't need to write pure JavaScript.
|
|
53
|
+
|
|
54
|
+
### Virtual Filesystem
|
|
55
|
+
|
|
56
|
+
The REPL has `readFile(path)` and `writeFile(path, content)` functions that read from and write to the agent's backend (LangGraph state by default):
|
|
57
|
+
|
|
58
|
+
```typescript
|
|
59
|
+
const raw = await readFile("/data.json");
|
|
60
|
+
const data = JSON.parse(raw);
|
|
61
|
+
const summary = { total: data.items.length };
|
|
62
|
+
await writeFile("/summary.json", JSON.stringify(summary, null, 2));
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### Programmatic Tool Calling (PTC)
|
|
66
|
+
|
|
67
|
+
Any agent tool can be exposed inside the REPL as a typed async function. Instead of the LLM emitting tool calls one at a time, it writes code that calls tools directly — loops, conditionals, parallel execution, and result transformation all happen in code:
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
const agent = createDeepAgent({
|
|
71
|
+
model: "claude-sonnet-4-5-20250929",
|
|
72
|
+
middleware: [
|
|
73
|
+
createQuickJSMiddleware({
|
|
74
|
+
ptc: true, // expose all agent tools inside the REPL
|
|
75
|
+
}),
|
|
76
|
+
],
|
|
77
|
+
});
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Inside the REPL, the agent can then write:
|
|
81
|
+
|
|
82
|
+
```typescript
|
|
83
|
+
const urls = ["/users", "/orders", "/products"];
|
|
84
|
+
const results = await Promise.all(
|
|
85
|
+
urls.map((u) => tools.httpRequest({ url: "https://api.example.com" + u })),
|
|
86
|
+
);
|
|
87
|
+
const parsed = results.map((r) => JSON.parse(r));
|
|
88
|
+
console.log(`Users: ${parsed[0].length}, Orders: ${parsed[1].length}`);
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
PTC configuration is progressive:
|
|
92
|
+
|
|
93
|
+
| Value | Behavior |
|
|
94
|
+
| ----------------------- | ----------------------------------- |
|
|
95
|
+
| `false` | Disabled (default) |
|
|
96
|
+
| `true` | All agent tools except VFS builtins |
|
|
97
|
+
| `string[]` | Only these tools |
|
|
98
|
+
| `{ include: string[] }` | Only these tools |
|
|
99
|
+
| `{ exclude: string[] }` | All tools except these |
|
|
100
|
+
|
|
101
|
+
### Recursive Language Model (RLM)
|
|
102
|
+
|
|
103
|
+
When the `task` tool is exposed via PTC, the agent can spawn sub-agents in parallel from within the REPL:
|
|
104
|
+
|
|
105
|
+
```typescript
|
|
106
|
+
const agent = createDeepAgent({
|
|
107
|
+
model: "claude-sonnet-4-5-20250929",
|
|
108
|
+
subagents: [
|
|
109
|
+
{
|
|
110
|
+
name: "general-purpose",
|
|
111
|
+
description: "Research agent",
|
|
112
|
+
systemPrompt: "...",
|
|
113
|
+
},
|
|
114
|
+
],
|
|
115
|
+
middleware: [createQuickJSMiddleware({ ptc: ["task"] })],
|
|
116
|
+
});
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
The agent then writes code like:
|
|
120
|
+
|
|
121
|
+
```typescript
|
|
122
|
+
const topics = ["quantum computing", "fusion energy", "CRISPR"];
|
|
123
|
+
const results = await Promise.all(
|
|
124
|
+
topics.map((topic) =>
|
|
125
|
+
tools.task({
|
|
126
|
+
description: `Research ${topic} in depth`,
|
|
127
|
+
subagentType: "general-purpose",
|
|
128
|
+
}),
|
|
129
|
+
),
|
|
130
|
+
);
|
|
131
|
+
const report = topics.map((t, i) => `## ${t}\n${results[i]}`).join("\n\n");
|
|
132
|
+
await writeFile("/research.md", report);
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
## API
|
|
136
|
+
|
|
137
|
+
### `createQuickJSMiddleware(options?)`
|
|
138
|
+
|
|
139
|
+
Creates a middleware that adds the `js_eval` tool to your agent.
|
|
140
|
+
|
|
141
|
+
```typescript
|
|
142
|
+
interface QuickJSMiddlewareOptions {
|
|
143
|
+
backend?: BackendProtocol | BackendFactory; // File I/O backend (default: StateBackend)
|
|
144
|
+
ptc?: boolean | string[] | { include: string[] } | { exclude: string[] }; // PTC config
|
|
145
|
+
memoryLimitBytes?: number; // Default: 50MB
|
|
146
|
+
maxStackSizeBytes?: number; // Default: 320KB
|
|
147
|
+
executionTimeoutMs?: number; // Default: 30s (-1 to disable)
|
|
148
|
+
systemPrompt?: string | null; // Override the built-in REPL system prompt
|
|
149
|
+
}
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
### `ReplSession`
|
|
153
|
+
|
|
154
|
+
The underlying session class. Usually you don't interact with this directly — the middleware manages sessions per thread.
|
|
155
|
+
|
|
156
|
+
```typescript
|
|
157
|
+
ReplSession.getOrCreate(id, options?) // Get or create a session
|
|
158
|
+
ReplSession.get(id) // Look up existing session
|
|
159
|
+
session.eval(code, timeoutMs) // Execute code
|
|
160
|
+
session.flushWrites(backend) // Persist buffered file writes
|
|
161
|
+
session.toJSON() / ReplSession.fromJSON(data) // Serialization
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
## License
|
|
165
|
+
|
|
166
|
+
MIT — see [LICENSE](./LICENSE).
|