@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/dist/index.js
ADDED
|
@@ -0,0 +1,749 @@
|
|
|
1
|
+
import { createMiddleware, tool } from "langchain";
|
|
2
|
+
import { z } from "zod/v4";
|
|
3
|
+
import { StateBackend } from "deepagents";
|
|
4
|
+
import dedent from "dedent";
|
|
5
|
+
import { shouldInterruptAfterDeadline } from "quickjs-emscripten";
|
|
6
|
+
import { newQuickJSAsyncWASMModuleFromVariant } from "quickjs-emscripten-core";
|
|
7
|
+
import { compile } from "json-schema-to-typescript";
|
|
8
|
+
import { toJsonSchema } from "@langchain/core/utils/json_schema";
|
|
9
|
+
import { Parser } from "acorn";
|
|
10
|
+
import { tsPlugin } from "@sveltejs/acorn-typescript";
|
|
11
|
+
import { walk } from "estree-walker";
|
|
12
|
+
import MagicString from "magic-string";
|
|
13
|
+
import { getCurrentTaskInput } from "@langchain/langgraph";
|
|
14
|
+
|
|
15
|
+
//#region src/utils.ts
|
|
16
|
+
/**
|
|
17
|
+
* Convert a snake_case or kebab-case string to camelCase.
|
|
18
|
+
*/
|
|
19
|
+
function toCamelCase(name) {
|
|
20
|
+
return name.replace(/[-_]([a-z])/g, (_, c) => c.toUpperCase());
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Format the result of a REPL evaluation for the agent.
|
|
24
|
+
*/
|
|
25
|
+
function formatReplResult(result) {
|
|
26
|
+
const parts = [];
|
|
27
|
+
if (result.logs.length > 0) parts.push(result.logs.join("\n"));
|
|
28
|
+
if (result.ok) {
|
|
29
|
+
if (result.value !== void 0) {
|
|
30
|
+
const formatted = typeof result.value === "string" ? result.value : JSON.stringify(result.value, null, 2);
|
|
31
|
+
parts.push(`→ ${formatted}`);
|
|
32
|
+
}
|
|
33
|
+
} else if (result.error) {
|
|
34
|
+
const errName = result.error.name || "Error";
|
|
35
|
+
const errMsg = result.error.message || "Unknown error";
|
|
36
|
+
parts.push(`${errName}: ${errMsg}`);
|
|
37
|
+
if (result.error.stack) parts.push(result.error.stack);
|
|
38
|
+
}
|
|
39
|
+
return parts.join("\n") || "(no output)";
|
|
40
|
+
}
|
|
41
|
+
function safeToJsonSchema(schema) {
|
|
42
|
+
try {
|
|
43
|
+
return toJsonSchema(schema);
|
|
44
|
+
} catch {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
async function schemaToInterface(jsonSchema, interfaceName) {
|
|
49
|
+
return (await compile({
|
|
50
|
+
...jsonSchema,
|
|
51
|
+
additionalProperties: false
|
|
52
|
+
}, interfaceName, {
|
|
53
|
+
bannerComment: "",
|
|
54
|
+
additionalProperties: false
|
|
55
|
+
})).replace(/^export /, "").trimEnd();
|
|
56
|
+
}
|
|
57
|
+
function capitalize(s) {
|
|
58
|
+
return s.charAt(0).toUpperCase() + s.slice(1);
|
|
59
|
+
}
|
|
60
|
+
async function toolToTypeSignature(name, description, jsonSchema) {
|
|
61
|
+
const inputType = `${capitalize(name)}Input`;
|
|
62
|
+
if (!jsonSchema || !jsonSchema.properties) return dedent`
|
|
63
|
+
/**
|
|
64
|
+
* ${description}
|
|
65
|
+
*/
|
|
66
|
+
async tools.${name}(input: Record<string, unknown>): Promise<string>
|
|
67
|
+
`;
|
|
68
|
+
return dedent`
|
|
69
|
+
${await schemaToInterface(jsonSchema, inputType)}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* ${description}
|
|
73
|
+
*/
|
|
74
|
+
async tools.${name}(input: ${inputType}): Promise<string>
|
|
75
|
+
`;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
//#endregion
|
|
79
|
+
//#region src/transform.ts
|
|
80
|
+
/**
|
|
81
|
+
* AST-based code transform pipeline for the REPL.
|
|
82
|
+
*
|
|
83
|
+
* Transforms TypeScript/JavaScript code into plain JS that can be
|
|
84
|
+
* evaluated inside QuickJS with proper state persistence:
|
|
85
|
+
*
|
|
86
|
+
* 1. Parse with acorn + acorn-typescript (handles TS syntax)
|
|
87
|
+
* 2. Strip TypeScript-only nodes (type annotations, interfaces, etc.)
|
|
88
|
+
* 3. Hoist top-level declarations to globalThis for cross-eval persistence
|
|
89
|
+
* 4. Auto-return the last expression
|
|
90
|
+
* 5. Wrap in async IIFE so top-level await works
|
|
91
|
+
*/
|
|
92
|
+
const TSParser = Parser.extend(tsPlugin());
|
|
93
|
+
/**
|
|
94
|
+
* Transform code for REPL evaluation.
|
|
95
|
+
*
|
|
96
|
+
* - Strips TypeScript syntax
|
|
97
|
+
* - Hoists top-level variable declarations to globalThis
|
|
98
|
+
* - Auto-returns the last expression
|
|
99
|
+
* - Wraps in async IIFE for top-level await support
|
|
100
|
+
*/
|
|
101
|
+
function transformForEval(code) {
|
|
102
|
+
let ast;
|
|
103
|
+
try {
|
|
104
|
+
ast = TSParser.parse(code, {
|
|
105
|
+
ecmaVersion: "latest",
|
|
106
|
+
sourceType: "module",
|
|
107
|
+
locations: true
|
|
108
|
+
});
|
|
109
|
+
} catch {
|
|
110
|
+
return `(async () => {\n${code}\n})()`;
|
|
111
|
+
}
|
|
112
|
+
const s = new MagicString(code);
|
|
113
|
+
const topLevelNodes = ast.body;
|
|
114
|
+
for (let i = 0; i < topLevelNodes.length; i++) {
|
|
115
|
+
const node = topLevelNodes[i];
|
|
116
|
+
if (isTSOnlyNode(node)) {
|
|
117
|
+
s.remove(node.start, node.end);
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
if (node.type === "ImportDeclaration" || node.type === "ExportNamedDeclaration" || node.type === "ExportDefaultDeclaration" || node.type === "ExportAllDeclaration") {
|
|
121
|
+
s.remove(node.start, node.end);
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
if (node.type === "VariableDeclaration") {
|
|
125
|
+
hoistDeclaration(s, node);
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
if (node.type === "FunctionDeclaration" || node.type === "ClassDeclaration") {
|
|
129
|
+
stripTypeAnnotations(s, node);
|
|
130
|
+
const name = node.id?.name;
|
|
131
|
+
if (name) s.appendRight(node.end, `\nglobalThis.${name} = ${name};`);
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
for (const node of topLevelNodes) {
|
|
136
|
+
if (isTSOnlyNode(node)) continue;
|
|
137
|
+
if (node.type === "ImportDeclaration" || node.type === "ExportNamedDeclaration" || node.type === "ExportDefaultDeclaration" || node.type === "ExportAllDeclaration") continue;
|
|
138
|
+
if (node.type !== "VariableDeclaration") walk(node, { enter(n) {
|
|
139
|
+
stripTypeAnnotationFromNode(s, n);
|
|
140
|
+
} });
|
|
141
|
+
}
|
|
142
|
+
const lastNode = findLastNonEmptyNode(topLevelNodes, s);
|
|
143
|
+
if (lastNode && isExpression(lastNode)) {
|
|
144
|
+
const { expression } = lastNode;
|
|
145
|
+
s.prependLeft(lastNode.start, "return (");
|
|
146
|
+
s.appendRight(expression.end, ")");
|
|
147
|
+
}
|
|
148
|
+
s.prepend("(async () => {\n");
|
|
149
|
+
s.append("\n})()");
|
|
150
|
+
return s.toString();
|
|
151
|
+
}
|
|
152
|
+
function isTSOnlyNode(node) {
|
|
153
|
+
const t = node.type;
|
|
154
|
+
return t === "TSTypeAliasDeclaration" || t === "TSInterfaceDeclaration" || t === "TSEnumDeclaration" || t === "TSModuleDeclaration" || t === "TSDeclareFunction" || t.startsWith("TS");
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Rewrite a top-level VariableDeclaration to globalThis assignments.
|
|
158
|
+
*
|
|
159
|
+
* `const x = 1, y = 2` → `globalThis.x = 1; globalThis.y = 2`
|
|
160
|
+
*
|
|
161
|
+
*/
|
|
162
|
+
function hoistDeclaration(s, decl) {
|
|
163
|
+
const parts = [];
|
|
164
|
+
for (const d of decl.declarations) {
|
|
165
|
+
const id = d.id;
|
|
166
|
+
if (id.type === "Identifier") {
|
|
167
|
+
const initCode = d.init ? extractCleanInit(s, d) : "undefined";
|
|
168
|
+
parts.push(`globalThis.${id.name} = ${initCode}`);
|
|
169
|
+
} else if (id.type === "ObjectPattern" || id.type === "ArrayPattern") {
|
|
170
|
+
const bindings = extractBindingNames(d.id);
|
|
171
|
+
const initCode = d.init ? extractCleanInit(s, d) : "undefined";
|
|
172
|
+
const patternCode = extractCleanSource(s, d.id);
|
|
173
|
+
parts.push(`var ${patternCode} = ${initCode}`);
|
|
174
|
+
for (const name of bindings) parts.push(`globalThis.${name} = ${name}`);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
s.overwrite(decl.start, decl.end, parts.join("; ") + ";");
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Extract the initializer code, stripping TypeScript annotations from
|
|
181
|
+
* within the expression (e.g. `as Type`, generics, parameter types in
|
|
182
|
+
* arrow functions).
|
|
183
|
+
*/
|
|
184
|
+
function extractCleanInit(s, d) {
|
|
185
|
+
if (!d.init) return "undefined";
|
|
186
|
+
return extractCleanSource(s, d.init);
|
|
187
|
+
}
|
|
188
|
+
function extractBindingNames(pattern) {
|
|
189
|
+
const names = [];
|
|
190
|
+
if (pattern.type === "Identifier") {
|
|
191
|
+
if (pattern.name) names.push(pattern.name);
|
|
192
|
+
} else if (pattern.type === "ObjectPattern") for (const prop of pattern.properties || []) if (prop.type === "RestElement") names.push(...extractBindingNames(prop.argument));
|
|
193
|
+
else names.push(...extractBindingNames(prop.value));
|
|
194
|
+
else if (pattern.type === "ArrayPattern") {
|
|
195
|
+
for (const el of pattern.elements || []) if (el) names.push(...extractBindingNames(el));
|
|
196
|
+
} else if (pattern.type === "RestElement") names.push(...extractBindingNames(pattern.argument));
|
|
197
|
+
else if (pattern.type === "AssignmentPattern") names.push(...extractBindingNames(pattern.left));
|
|
198
|
+
return names;
|
|
199
|
+
}
|
|
200
|
+
function stripTypeAnnotations(s, node) {
|
|
201
|
+
walk(node, { enter(n) {
|
|
202
|
+
stripTypeAnnotationFromNode(s, n);
|
|
203
|
+
} });
|
|
204
|
+
}
|
|
205
|
+
function stripTypeAnnotationFromNode(s, n, offset = 0) {
|
|
206
|
+
if (n.typeAnnotation && n.typeAnnotation.start != null) s.remove(n.typeAnnotation.start - offset, n.typeAnnotation.end - offset);
|
|
207
|
+
if (n.returnType && n.returnType.start != null) s.remove(n.returnType.start - offset, n.returnType.end - offset);
|
|
208
|
+
if (n.typeParameters && n.typeParameters.start != null) s.remove(n.typeParameters.start - offset, n.typeParameters.end - offset);
|
|
209
|
+
if (n.typeArguments && n.typeArguments.start != null) s.remove(n.typeArguments.start - offset, n.typeArguments.end - offset);
|
|
210
|
+
if (n.type === "TSAsExpression" && n.expression) s.remove(n.expression.end - offset, n.end - offset);
|
|
211
|
+
if (n.type === "TSNonNullExpression" && n.expression) s.remove(n.expression.end - offset, n.end - offset);
|
|
212
|
+
if (n.type === "TSSatisfiesExpression" && n.expression) s.remove(n.expression.end - offset, n.end - offset);
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Extract a clean JS source string from an AST node, stripping all
|
|
216
|
+
* TypeScript annotations. Works on a copy so the main MagicString is
|
|
217
|
+
* not mutated.
|
|
218
|
+
*/
|
|
219
|
+
function extractCleanSource(s, node) {
|
|
220
|
+
const offset = node.start;
|
|
221
|
+
const source = new MagicString(s.slice(node.start, node.end));
|
|
222
|
+
walk(node, { enter(n) {
|
|
223
|
+
stripTypeAnnotationFromNode(source, n, offset);
|
|
224
|
+
} });
|
|
225
|
+
return source.toString();
|
|
226
|
+
}
|
|
227
|
+
function findLastNonEmptyNode(nodes, s) {
|
|
228
|
+
for (let i = nodes.length - 1; i >= 0; i--) {
|
|
229
|
+
const node = nodes[i];
|
|
230
|
+
const slice = s.slice(node.start, node.end).trim();
|
|
231
|
+
if (slice === "" || slice === ";") continue;
|
|
232
|
+
return node;
|
|
233
|
+
}
|
|
234
|
+
return null;
|
|
235
|
+
}
|
|
236
|
+
function isExpression(node) {
|
|
237
|
+
return node.type === "ExpressionStatement";
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
//#endregion
|
|
241
|
+
//#region src/session.ts
|
|
242
|
+
/**
|
|
243
|
+
* Core REPL engine built on quickjs-emscripten (asyncify variant).
|
|
244
|
+
*
|
|
245
|
+
* Host async functions (backend I/O, PTC tools) are exposed as
|
|
246
|
+
* promise-returning functions inside the QuickJS guest. Guest code
|
|
247
|
+
* uses `await` to consume them, enabling real concurrency via
|
|
248
|
+
* `Promise.all`, `Promise.race`, etc.
|
|
249
|
+
*
|
|
250
|
+
* We still use the asyncify WASM variant because `evalCodeAsync` is
|
|
251
|
+
* required to drive promise resolution from the host side.
|
|
252
|
+
*
|
|
253
|
+
* ## Architecture
|
|
254
|
+
*
|
|
255
|
+
* `ReplSession` is a serializable handle that can live in LangGraph state.
|
|
256
|
+
* It holds an `id` that keys into a static session map. The heavy QuickJS
|
|
257
|
+
* runtime is lazily started on the first `.eval()` call, making the session
|
|
258
|
+
* safe across graph interrupts and checkpointing.
|
|
259
|
+
*
|
|
260
|
+
* File writes inside the REPL are buffered (`pendingWrites`) and only
|
|
261
|
+
* flushed to the backend after a script finishes executing. Call
|
|
262
|
+
* `session.flushWrites(backend)` after eval to persist them.
|
|
263
|
+
*/
|
|
264
|
+
const DEFAULT_MEMORY_LIMIT = 50 * 1024 * 1024;
|
|
265
|
+
const DEFAULT_MAX_STACK_SIZE = 320 * 1024;
|
|
266
|
+
const DEFAULT_EXECUTION_TIMEOUT = 3e4;
|
|
267
|
+
const DEFAULT_SESSION_ID = "__default__";
|
|
268
|
+
let asyncModulePromise;
|
|
269
|
+
async function getAsyncModule() {
|
|
270
|
+
if (!asyncModulePromise) asyncModulePromise = (async () => {
|
|
271
|
+
const variant = await import("@jitl/quickjs-ng-wasmfile-release-asyncify");
|
|
272
|
+
return newQuickJSAsyncWASMModuleFromVariant(variant.default ?? variant);
|
|
273
|
+
})();
|
|
274
|
+
return asyncModulePromise;
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Sandboxed JavaScript REPL session backed by QuickJS WASM.
|
|
278
|
+
*
|
|
279
|
+
* Serializable — holds an `id` that keys into a static session map.
|
|
280
|
+
* The QuickJS runtime is lazily started on the first `.eval()` call
|
|
281
|
+
* and reconnected if a session with the same id already exists.
|
|
282
|
+
* This makes it safe to store in LangGraph state across interrupts.
|
|
283
|
+
*
|
|
284
|
+
* File writes are buffered during execution and flushed via
|
|
285
|
+
* `flushWrites(backend)` after eval completes.
|
|
286
|
+
*/
|
|
287
|
+
var ReplSession = class ReplSession {
|
|
288
|
+
static sessions = /* @__PURE__ */ new Map();
|
|
289
|
+
id;
|
|
290
|
+
pendingWrites = [];
|
|
291
|
+
runtime = null;
|
|
292
|
+
context = null;
|
|
293
|
+
logs = [];
|
|
294
|
+
_options;
|
|
295
|
+
_backend = null;
|
|
296
|
+
constructor(id, options = {}) {
|
|
297
|
+
this.id = id;
|
|
298
|
+
this._options = options;
|
|
299
|
+
}
|
|
300
|
+
get backend() {
|
|
301
|
+
return this._backend;
|
|
302
|
+
}
|
|
303
|
+
set backend(b) {
|
|
304
|
+
this._backend = b;
|
|
305
|
+
}
|
|
306
|
+
async ensureStarted() {
|
|
307
|
+
if (this.runtime) return;
|
|
308
|
+
const { memoryLimitBytes = DEFAULT_MEMORY_LIMIT, maxStackSizeBytes = DEFAULT_MAX_STACK_SIZE, backend, tools } = this._options;
|
|
309
|
+
const runtime = (await getAsyncModule()).newRuntime();
|
|
310
|
+
runtime.setMemoryLimit(memoryLimitBytes);
|
|
311
|
+
runtime.setMaxStackSize(maxStackSizeBytes);
|
|
312
|
+
const context = runtime.newContext();
|
|
313
|
+
this.runtime = runtime;
|
|
314
|
+
this.context = context;
|
|
315
|
+
this.setupConsole();
|
|
316
|
+
if (backend) this._backend = backend;
|
|
317
|
+
this.injectVfs();
|
|
318
|
+
if (tools && tools.length > 0) this.injectTools(tools);
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* Get or create a session for the given id.
|
|
322
|
+
*
|
|
323
|
+
* Sessions are deduped by id — calling `getOrCreate` twice with the
|
|
324
|
+
* same id returns the same instance. The QuickJS runtime is lazily
|
|
325
|
+
* started on the first `.eval()` call.
|
|
326
|
+
*/
|
|
327
|
+
static getOrCreate(id, options = {}) {
|
|
328
|
+
const existing = ReplSession.sessions.get(id);
|
|
329
|
+
if (existing) {
|
|
330
|
+
if (options.backend) existing._backend = options.backend;
|
|
331
|
+
return existing;
|
|
332
|
+
}
|
|
333
|
+
const session = new ReplSession(id, options);
|
|
334
|
+
ReplSession.sessions.set(id, session);
|
|
335
|
+
return session;
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* Retrieve an existing session by id, or null if none exists.
|
|
339
|
+
*/
|
|
340
|
+
static get(id) {
|
|
341
|
+
return ReplSession.sessions.get(id) ?? null;
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* Evaluate code in this session.
|
|
345
|
+
*
|
|
346
|
+
* Lazily starts the QuickJS runtime on the first call. Code is
|
|
347
|
+
* transformed via an AST pipeline that strips TypeScript syntax,
|
|
348
|
+
* hoists top-level declarations to globalThis for cross-eval
|
|
349
|
+
* persistence, auto-returns the last expression, and wraps in an
|
|
350
|
+
* async IIFE.
|
|
351
|
+
*/
|
|
352
|
+
async eval(code, timeoutMs) {
|
|
353
|
+
await this.ensureStarted();
|
|
354
|
+
const runtime = this.runtime;
|
|
355
|
+
const context = this.context;
|
|
356
|
+
this.logs.length = 0;
|
|
357
|
+
if (timeoutMs >= 0) runtime.setInterruptHandler(shouldInterruptAfterDeadline(Date.now() + timeoutMs));
|
|
358
|
+
else runtime.setInterruptHandler(() => false);
|
|
359
|
+
const transformed = transformForEval(code);
|
|
360
|
+
const result = await context.evalCodeAsync(transformed);
|
|
361
|
+
if (result.error) {
|
|
362
|
+
const error = context.dump(result.error);
|
|
363
|
+
result.error.dispose();
|
|
364
|
+
return {
|
|
365
|
+
ok: false,
|
|
366
|
+
error,
|
|
367
|
+
logs: [...this.logs]
|
|
368
|
+
};
|
|
369
|
+
}
|
|
370
|
+
const promiseState = context.getPromiseState(result.value);
|
|
371
|
+
if (promiseState.type === "fulfilled") {
|
|
372
|
+
if (promiseState.notAPromise) {
|
|
373
|
+
const value = context.dump(result.value);
|
|
374
|
+
result.value.dispose();
|
|
375
|
+
return {
|
|
376
|
+
ok: true,
|
|
377
|
+
value,
|
|
378
|
+
logs: [...this.logs]
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
const value = context.dump(promiseState.value);
|
|
382
|
+
promiseState.value.dispose();
|
|
383
|
+
result.value.dispose();
|
|
384
|
+
return {
|
|
385
|
+
ok: true,
|
|
386
|
+
value,
|
|
387
|
+
logs: [...this.logs]
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
if (promiseState.type === "rejected") {
|
|
391
|
+
const error = context.dump(promiseState.error);
|
|
392
|
+
promiseState.error.dispose();
|
|
393
|
+
result.value.dispose();
|
|
394
|
+
return {
|
|
395
|
+
ok: false,
|
|
396
|
+
error,
|
|
397
|
+
logs: [...this.logs]
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
const noTimeout = timeoutMs < 0;
|
|
401
|
+
const deadline = noTimeout ? Infinity : Date.now() + timeoutMs;
|
|
402
|
+
while (noTimeout || Date.now() < deadline) {
|
|
403
|
+
context.runtime.executePendingJobs();
|
|
404
|
+
const state = context.getPromiseState(result.value);
|
|
405
|
+
if (state.type === "fulfilled") {
|
|
406
|
+
const value = context.dump(state.value);
|
|
407
|
+
state.value.dispose();
|
|
408
|
+
result.value.dispose();
|
|
409
|
+
return {
|
|
410
|
+
ok: true,
|
|
411
|
+
value,
|
|
412
|
+
logs: [...this.logs]
|
|
413
|
+
};
|
|
414
|
+
}
|
|
415
|
+
if (state.type === "rejected") {
|
|
416
|
+
const error = context.dump(state.error);
|
|
417
|
+
state.error.dispose();
|
|
418
|
+
result.value.dispose();
|
|
419
|
+
return {
|
|
420
|
+
ok: false,
|
|
421
|
+
error,
|
|
422
|
+
logs: [...this.logs]
|
|
423
|
+
};
|
|
424
|
+
}
|
|
425
|
+
await new Promise((r) => setTimeout(r, 1));
|
|
426
|
+
}
|
|
427
|
+
result.value.dispose();
|
|
428
|
+
return {
|
|
429
|
+
ok: false,
|
|
430
|
+
error: { message: "Promise timed out — execution interrupted" },
|
|
431
|
+
logs: [...this.logs]
|
|
432
|
+
};
|
|
433
|
+
}
|
|
434
|
+
async flushWrites(backend) {
|
|
435
|
+
const writes = this.pendingWrites.splice(0);
|
|
436
|
+
for (const { path, content } of writes) await backend.write(path, content);
|
|
437
|
+
}
|
|
438
|
+
dispose() {
|
|
439
|
+
try {
|
|
440
|
+
this.context?.dispose();
|
|
441
|
+
} catch {}
|
|
442
|
+
try {
|
|
443
|
+
this.runtime?.dispose();
|
|
444
|
+
} catch {}
|
|
445
|
+
this.runtime = null;
|
|
446
|
+
this.context = null;
|
|
447
|
+
ReplSession.sessions.delete(this.id);
|
|
448
|
+
}
|
|
449
|
+
toJSON() {
|
|
450
|
+
return { id: this.id };
|
|
451
|
+
}
|
|
452
|
+
static fromJSON(data) {
|
|
453
|
+
return ReplSession.sessions.get(data.id) ?? new ReplSession(data.id);
|
|
454
|
+
}
|
|
455
|
+
/**
|
|
456
|
+
* Clear the static session cache. Useful for testing.
|
|
457
|
+
* @internal
|
|
458
|
+
*/
|
|
459
|
+
static clearCache() {
|
|
460
|
+
for (const session of ReplSession.sessions.values()) session.dispose();
|
|
461
|
+
ReplSession.sessions.clear();
|
|
462
|
+
}
|
|
463
|
+
setupConsole() {
|
|
464
|
+
const context = this.context;
|
|
465
|
+
const logs = this.logs;
|
|
466
|
+
const consoleHandle = context.newObject();
|
|
467
|
+
for (const method of [
|
|
468
|
+
"log",
|
|
469
|
+
"warn",
|
|
470
|
+
"error",
|
|
471
|
+
"info",
|
|
472
|
+
"debug"
|
|
473
|
+
]) {
|
|
474
|
+
const fnHandle = context.newFunction(method, (...args) => {
|
|
475
|
+
const formatted = args.map((a) => context.dump(a)).map((a) => typeof a === "object" && a !== null ? JSON.stringify(a) : String(a)).join(" ");
|
|
476
|
+
logs.push(method === "log" || method === "info" || method === "debug" ? formatted : `[${method}] ${formatted}`);
|
|
477
|
+
});
|
|
478
|
+
context.setProp(consoleHandle, method, fnHandle);
|
|
479
|
+
fnHandle.dispose();
|
|
480
|
+
}
|
|
481
|
+
context.setProp(context.global, "console", consoleHandle);
|
|
482
|
+
consoleHandle.dispose();
|
|
483
|
+
}
|
|
484
|
+
injectVfs() {
|
|
485
|
+
const context = this.context;
|
|
486
|
+
const getBackend = () => this._backend;
|
|
487
|
+
const { pendingWrites } = this;
|
|
488
|
+
const readFileHandle = context.newFunction("readFile", (pathHandle) => {
|
|
489
|
+
const backend = getBackend();
|
|
490
|
+
if (!backend) {
|
|
491
|
+
const promise = context.newPromise();
|
|
492
|
+
const err = context.newError("Backend not available");
|
|
493
|
+
promise.reject(err);
|
|
494
|
+
err.dispose();
|
|
495
|
+
promise.settled.then(context.runtime.executePendingJobs);
|
|
496
|
+
return promise.handle;
|
|
497
|
+
}
|
|
498
|
+
const path = context.getString(pathHandle);
|
|
499
|
+
const promise = context.newPromise();
|
|
500
|
+
(async () => {
|
|
501
|
+
try {
|
|
502
|
+
const fileData = await backend.readRaw(path);
|
|
503
|
+
const val = context.newString(fileData.content.join("\n"));
|
|
504
|
+
promise.resolve(val);
|
|
505
|
+
val.dispose();
|
|
506
|
+
} catch {
|
|
507
|
+
const err = context.newError(`ENOENT: no such file or directory '${path}'.`);
|
|
508
|
+
promise.reject(err);
|
|
509
|
+
err.dispose();
|
|
510
|
+
}
|
|
511
|
+
promise.settled.then(context.runtime.executePendingJobs);
|
|
512
|
+
})();
|
|
513
|
+
return promise.handle;
|
|
514
|
+
});
|
|
515
|
+
context.setProp(context.global, "readFile", readFileHandle);
|
|
516
|
+
readFileHandle.dispose();
|
|
517
|
+
const writeFileHandle = context.newFunction("writeFile", (pathHandle, contentHandle) => {
|
|
518
|
+
const path = context.getString(pathHandle);
|
|
519
|
+
const content = context.getString(contentHandle);
|
|
520
|
+
const promise = context.newPromise();
|
|
521
|
+
pendingWrites.push({
|
|
522
|
+
path,
|
|
523
|
+
content
|
|
524
|
+
});
|
|
525
|
+
promise.resolve(context.undefined);
|
|
526
|
+
promise.settled.then(context.runtime.executePendingJobs);
|
|
527
|
+
return promise.handle;
|
|
528
|
+
});
|
|
529
|
+
context.setProp(context.global, "writeFile", writeFileHandle);
|
|
530
|
+
writeFileHandle.dispose();
|
|
531
|
+
}
|
|
532
|
+
injectTools(tools) {
|
|
533
|
+
const context = this.context;
|
|
534
|
+
const toolsNs = context.newObject();
|
|
535
|
+
for (const t of tools) {
|
|
536
|
+
const camelName = toCamelCase(t.name);
|
|
537
|
+
const fnHandle = context.newFunction(camelName, (inputHandle) => {
|
|
538
|
+
const input = context.dump(inputHandle);
|
|
539
|
+
const promise = context.newPromise();
|
|
540
|
+
(async () => {
|
|
541
|
+
try {
|
|
542
|
+
const rawInput = typeof input === "object" && input !== null ? input : {};
|
|
543
|
+
const result = await t.invoke(rawInput);
|
|
544
|
+
const val = context.newString(typeof result === "string" ? result : JSON.stringify(result));
|
|
545
|
+
promise.resolve(val);
|
|
546
|
+
val.dispose();
|
|
547
|
+
} catch (e) {
|
|
548
|
+
const msg = e != null && typeof e.message === "string" ? e.message : String(e);
|
|
549
|
+
const err = context.newError(`Tool '${t.name}' failed: ${msg}`);
|
|
550
|
+
promise.reject(err);
|
|
551
|
+
err.dispose();
|
|
552
|
+
}
|
|
553
|
+
promise.settled.then(context.runtime.executePendingJobs);
|
|
554
|
+
})();
|
|
555
|
+
return promise.handle;
|
|
556
|
+
});
|
|
557
|
+
context.setProp(toolsNs, camelName, fnHandle);
|
|
558
|
+
fnHandle.dispose();
|
|
559
|
+
}
|
|
560
|
+
context.setProp(context.global, "tools", toolsNs);
|
|
561
|
+
toolsNs.dispose();
|
|
562
|
+
}
|
|
563
|
+
};
|
|
564
|
+
|
|
565
|
+
//#endregion
|
|
566
|
+
//#region src/middleware.ts
|
|
567
|
+
/**
|
|
568
|
+
* QuickJS REPL middleware for deepagents.
|
|
569
|
+
*
|
|
570
|
+
* Provides a `js_eval` tool that runs JavaScript in a WASM-sandboxed QuickJS
|
|
571
|
+
* interpreter. Supports:
|
|
572
|
+
* - Persistent state across evaluations (true REPL)
|
|
573
|
+
* - VFS integration via readFile/writeFile
|
|
574
|
+
* - Programmatic tool calling (PTC)
|
|
575
|
+
*/
|
|
576
|
+
/**
|
|
577
|
+
* Backend-provided tools excluded from PTC by default.
|
|
578
|
+
* These are redundant inside the REPL since VFS helpers (readFile/writeFile)
|
|
579
|
+
* already cover file I/O against the agent's in-memory working set.
|
|
580
|
+
*/
|
|
581
|
+
const DEFAULT_PTC_EXCLUDED_TOOLS = [
|
|
582
|
+
"ls",
|
|
583
|
+
"read_file",
|
|
584
|
+
"write_file",
|
|
585
|
+
"edit_file",
|
|
586
|
+
"glob",
|
|
587
|
+
"grep",
|
|
588
|
+
"execute"
|
|
589
|
+
];
|
|
590
|
+
const REPL_SYSTEM_PROMPT = dedent`
|
|
591
|
+
## TypeScript/JavaScript REPL (\`js_eval\`)
|
|
592
|
+
|
|
593
|
+
You have access to a sandboxed TypeScript/JavaScript REPL running in an isolated interpreter.
|
|
594
|
+
TypeScript syntax (type annotations, interfaces, generics, \`as\` casts) is supported and stripped at evaluation time.
|
|
595
|
+
Variables, functions, and closures persist across calls within the same session.
|
|
596
|
+
|
|
597
|
+
### Hard rules
|
|
598
|
+
|
|
599
|
+
- **No network, no filesystem** — only the helpers below. Do not attempt \`fetch\`, \`require\`, or \`import\`.
|
|
600
|
+
- **Cite your sources** — when reporting values from files, include the path and key/index so the user can verify.
|
|
601
|
+
- **Use console.log()** for output — it is captured and returned. \`console.warn()\` and \`console.error()\` are also available.
|
|
602
|
+
- **Reuse state from previous cells** — variables, functions, and results from earlier \`js_eval\` calls persist across calls. Reference them by name in follow-up cells instead of re-embedding data as inline JSON literals.
|
|
603
|
+
|
|
604
|
+
### First-time usage
|
|
605
|
+
|
|
606
|
+
\`\`\`typescript
|
|
607
|
+
// Read a file from the agent's virtual filesystem
|
|
608
|
+
const raw: string = await readFile("/data.json");
|
|
609
|
+
const data = JSON.parse(raw) as { n: number };
|
|
610
|
+
console.log(data);
|
|
611
|
+
|
|
612
|
+
// Write results back
|
|
613
|
+
await writeFile("/output.txt", JSON.stringify({ result: data.n }));
|
|
614
|
+
\`\`\`
|
|
615
|
+
|
|
616
|
+
### API Reference — built-in globals
|
|
617
|
+
|
|
618
|
+
\`\`\`typescript
|
|
619
|
+
/**
|
|
620
|
+
* Read a file from the agent's virtual filesystem. Throws if the file does not exist.
|
|
621
|
+
*/
|
|
622
|
+
async readFile(path: string): Promise<string>
|
|
623
|
+
|
|
624
|
+
/**
|
|
625
|
+
* Write a file to the agent's virtual filesystem.
|
|
626
|
+
*/
|
|
627
|
+
async writeFile(path: string, content: string): Promise<void>
|
|
628
|
+
\`\`\`
|
|
629
|
+
|
|
630
|
+
### Limitations
|
|
631
|
+
|
|
632
|
+
- ES2023+ syntax with TypeScript support. No Node.js APIs, no \`require\`, no \`import\`.
|
|
633
|
+
- Output is truncated beyond a fixed character limit — be selective about what you log.
|
|
634
|
+
- Execution timeout per call (default 30 s).
|
|
635
|
+
`;
|
|
636
|
+
/**
|
|
637
|
+
* Generate the PTC API Reference section for the system prompt.
|
|
638
|
+
*/
|
|
639
|
+
async function generatePtcPrompt(tools) {
|
|
640
|
+
if (tools.length === 0) return "";
|
|
641
|
+
return dedent`
|
|
642
|
+
|
|
643
|
+
### API Reference — \`tools\` namespace
|
|
644
|
+
|
|
645
|
+
The following agent tools are callable as async functions inside the REPL.
|
|
646
|
+
Each takes a single object argument and returns a Promise that resolves to a string.
|
|
647
|
+
Use \`await\` to call them. Promise APIs like \`Promise.all\` are also available.
|
|
648
|
+
|
|
649
|
+
**Example usage:**
|
|
650
|
+
\`\`\`javascript
|
|
651
|
+
// Call a tool
|
|
652
|
+
const result = await tools.searchWeb({ query: "QuickJS tutorial" });
|
|
653
|
+
console.log(result);
|
|
654
|
+
|
|
655
|
+
// Concurrent calls
|
|
656
|
+
const [a, b] = await Promise.all([
|
|
657
|
+
tools.fetchData({ url: "https://api.example.com/a" }),
|
|
658
|
+
tools.fetchData({ url: "https://api.example.com/b" }),
|
|
659
|
+
]);
|
|
660
|
+
\`\`\`
|
|
661
|
+
|
|
662
|
+
**Available functions:**
|
|
663
|
+
\`\`\`typescript
|
|
664
|
+
${(await Promise.all(tools.map((t) => {
|
|
665
|
+
const jsonSchema = t.schema ? safeToJsonSchema(t.schema) : void 0;
|
|
666
|
+
return toolToTypeSignature(toCamelCase(t.name), t.description, jsonSchema);
|
|
667
|
+
}))).join("\n\n")}
|
|
668
|
+
\`\`\`
|
|
669
|
+
`;
|
|
670
|
+
}
|
|
671
|
+
/**
|
|
672
|
+
* Resolve backend from factory or instance.
|
|
673
|
+
*/
|
|
674
|
+
function getBackend(backend, stateAndStore) {
|
|
675
|
+
if (typeof backend === "function") return backend(stateAndStore);
|
|
676
|
+
return backend;
|
|
677
|
+
}
|
|
678
|
+
/**
|
|
679
|
+
* Create the QuickJS REPL middleware.
|
|
680
|
+
*/
|
|
681
|
+
function createQuickJSMiddleware(options = {}) {
|
|
682
|
+
const { backend = (stateAndStore) => new StateBackend(stateAndStore), ptc = false, memoryLimitBytes = DEFAULT_MEMORY_LIMIT, maxStackSizeBytes = DEFAULT_MAX_STACK_SIZE, executionTimeoutMs = DEFAULT_EXECUTION_TIMEOUT, systemPrompt: customSystemPrompt = null } = options;
|
|
683
|
+
const usePtc = ptc !== false;
|
|
684
|
+
const baseSystemPrompt = customSystemPrompt || REPL_SYSTEM_PROMPT;
|
|
685
|
+
let cachedPtcPrompt = null;
|
|
686
|
+
let ptcTools = [];
|
|
687
|
+
function filterToolsForPtc(allTools) {
|
|
688
|
+
if (ptc === false) return [];
|
|
689
|
+
const candidates = allTools.filter((t) => t.name !== "js_eval");
|
|
690
|
+
if (ptc === true) {
|
|
691
|
+
const excluded = new Set(DEFAULT_PTC_EXCLUDED_TOOLS);
|
|
692
|
+
return candidates.filter((t) => !excluded.has(t.name));
|
|
693
|
+
}
|
|
694
|
+
if (Array.isArray(ptc)) {
|
|
695
|
+
const included = new Set(ptc);
|
|
696
|
+
return candidates.filter((t) => included.has(t.name));
|
|
697
|
+
}
|
|
698
|
+
if ("include" in ptc) {
|
|
699
|
+
const included = new Set(ptc.include);
|
|
700
|
+
return candidates.filter((t) => included.has(t.name));
|
|
701
|
+
}
|
|
702
|
+
if ("exclude" in ptc) {
|
|
703
|
+
const excluded = new Set([...DEFAULT_PTC_EXCLUDED_TOOLS, ...ptc.exclude]);
|
|
704
|
+
return candidates.filter((t) => !excluded.has(t.name));
|
|
705
|
+
}
|
|
706
|
+
return [];
|
|
707
|
+
}
|
|
708
|
+
return createMiddleware({
|
|
709
|
+
name: "QuickJSMiddleware",
|
|
710
|
+
tools: [tool(async (input, config) => {
|
|
711
|
+
const threadId = config.configurable?.thread_id || DEFAULT_SESSION_ID;
|
|
712
|
+
const resolvedBackend = getBackend(backend, {
|
|
713
|
+
state: getCurrentTaskInput(config) || {},
|
|
714
|
+
store: config.configurable?.__pregel_store
|
|
715
|
+
});
|
|
716
|
+
const session = ReplSession.getOrCreate(threadId, {
|
|
717
|
+
memoryLimitBytes,
|
|
718
|
+
maxStackSizeBytes,
|
|
719
|
+
backend: resolvedBackend,
|
|
720
|
+
tools: ptcTools
|
|
721
|
+
});
|
|
722
|
+
const result = await session.eval(input.code, executionTimeoutMs);
|
|
723
|
+
await session.flushWrites(resolvedBackend);
|
|
724
|
+
return formatReplResult(result);
|
|
725
|
+
}, {
|
|
726
|
+
name: "js_eval",
|
|
727
|
+
description: dedent`
|
|
728
|
+
Evaluate TypeScript/JavaScript code in a sandboxed REPL. State persists across calls.
|
|
729
|
+
Use readFile(path) and writeFile(path, content) for file access.
|
|
730
|
+
Use console.log() for output. Returns the result of the last expression.
|
|
731
|
+
`,
|
|
732
|
+
schema: z.object({ code: z.string().describe("TypeScript/JavaScript code to evaluate in the sandboxed REPL") })
|
|
733
|
+
})],
|
|
734
|
+
wrapModelCall: async (request, handler) => {
|
|
735
|
+
const agentTools = request.tools || [];
|
|
736
|
+
ptcTools = usePtc ? filterToolsForPtc(agentTools) : [];
|
|
737
|
+
if (ptcTools.length > 0 && !cachedPtcPrompt) cachedPtcPrompt = await generatePtcPrompt(ptcTools);
|
|
738
|
+
const systemMessage = request.systemMessage.concat(baseSystemPrompt).concat(cachedPtcPrompt || "");
|
|
739
|
+
return handler({
|
|
740
|
+
...request,
|
|
741
|
+
systemMessage
|
|
742
|
+
});
|
|
743
|
+
}
|
|
744
|
+
});
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
//#endregion
|
|
748
|
+
export { DEFAULT_EXECUTION_TIMEOUT, DEFAULT_MAX_STACK_SIZE, DEFAULT_MEMORY_LIMIT, DEFAULT_PTC_EXCLUDED_TOOLS, ReplSession, createQuickJSMiddleware, formatReplResult, toCamelCase, transformForEval };
|
|
749
|
+
//# sourceMappingURL=index.js.map
|