@langchain/quickjs 0.2.0 → 0.2.1
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/dist/index.cjs +10 -20
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +3 -8
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
package/dist/index.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value:
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
//#region \0rolldown/runtime.js
|
|
3
3
|
var __create = Object.create;
|
|
4
4
|
var __defProp = Object.defineProperty;
|
|
@@ -7,16 +7,12 @@ var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
|
7
7
|
var __getProtoOf = Object.getPrototypeOf;
|
|
8
8
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
9
|
var __copyProps = (to, from, except, desc) => {
|
|
10
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
17
|
-
});
|
|
18
|
-
}
|
|
19
|
-
}
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
+
key = keys[i];
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
13
|
+
get: ((k) => from[k]).bind(null, key),
|
|
14
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
15
|
+
});
|
|
20
16
|
}
|
|
21
17
|
return to;
|
|
22
18
|
};
|
|
@@ -24,7 +20,6 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
24
20
|
value: mod,
|
|
25
21
|
enumerable: true
|
|
26
22
|
}) : target, mod));
|
|
27
|
-
|
|
28
23
|
//#endregion
|
|
29
24
|
let langchain = require("langchain");
|
|
30
25
|
let zod_v4 = require("zod/v4");
|
|
@@ -41,7 +36,6 @@ let estree_walker = require("estree-walker");
|
|
|
41
36
|
let magic_string = require("magic-string");
|
|
42
37
|
magic_string = __toESM(magic_string);
|
|
43
38
|
let _langchain_langgraph = require("@langchain/langgraph");
|
|
44
|
-
|
|
45
39
|
//#region src/utils.ts
|
|
46
40
|
/**
|
|
47
41
|
* Convert a snake_case or kebab-case string to camelCase.
|
|
@@ -104,7 +98,6 @@ async function toolToTypeSignature(name, description, jsonSchema) {
|
|
|
104
98
|
async tools.${name}(input: ${inputType}): Promise<string>
|
|
105
99
|
`;
|
|
106
100
|
}
|
|
107
|
-
|
|
108
101
|
//#endregion
|
|
109
102
|
//#region src/transform.ts
|
|
110
103
|
/**
|
|
@@ -266,7 +259,6 @@ function findLastNonEmptyNode(nodes, s) {
|
|
|
266
259
|
function isExpression(node) {
|
|
267
260
|
return node.type === "ExpressionStatement";
|
|
268
261
|
}
|
|
269
|
-
|
|
270
262
|
//#endregion
|
|
271
263
|
//#region src/session.ts
|
|
272
264
|
/**
|
|
@@ -294,7 +286,6 @@ function isExpression(node) {
|
|
|
294
286
|
const DEFAULT_MEMORY_LIMIT = 50 * 1024 * 1024;
|
|
295
287
|
const DEFAULT_MAX_STACK_SIZE = 320 * 1024;
|
|
296
288
|
const DEFAULT_EXECUTION_TIMEOUT = 3e4;
|
|
297
|
-
const DEFAULT_SESSION_ID = "__default__";
|
|
298
289
|
let asyncModulePromise;
|
|
299
290
|
async function getAsyncModule() {
|
|
300
291
|
if (!asyncModulePromise) asyncModulePromise = (async () => {
|
|
@@ -591,7 +582,6 @@ var ReplSession = class ReplSession {
|
|
|
591
582
|
toolsNs.dispose();
|
|
592
583
|
}
|
|
593
584
|
};
|
|
594
|
-
|
|
595
585
|
//#endregion
|
|
596
586
|
//#region src/middleware.ts
|
|
597
587
|
/**
|
|
@@ -738,10 +728,10 @@ function createQuickJSMiddleware(options = {}) {
|
|
|
738
728
|
return (0, langchain.createMiddleware)({
|
|
739
729
|
name: "QuickJSMiddleware",
|
|
740
730
|
tools: [(0, langchain.tool)(async (input, config) => {
|
|
741
|
-
const threadId = config.configurable?.thread_id ||
|
|
731
|
+
const threadId = config.configurable?.thread_id || "__default__";
|
|
742
732
|
const resolvedBackend = getBackend(backend, {
|
|
743
733
|
state: (0, _langchain_langgraph.getCurrentTaskInput)(config) || {},
|
|
744
|
-
store: config.
|
|
734
|
+
store: config.store
|
|
745
735
|
});
|
|
746
736
|
const session = ReplSession.getOrCreate(threadId, {
|
|
747
737
|
memoryLimitBytes,
|
|
@@ -773,7 +763,6 @@ function createQuickJSMiddleware(options = {}) {
|
|
|
773
763
|
}
|
|
774
764
|
});
|
|
775
765
|
}
|
|
776
|
-
|
|
777
766
|
//#endregion
|
|
778
767
|
exports.DEFAULT_EXECUTION_TIMEOUT = DEFAULT_EXECUTION_TIMEOUT;
|
|
779
768
|
exports.DEFAULT_MAX_STACK_SIZE = DEFAULT_MAX_STACK_SIZE;
|
|
@@ -784,4 +773,5 @@ exports.createQuickJSMiddleware = createQuickJSMiddleware;
|
|
|
784
773
|
exports.formatReplResult = formatReplResult;
|
|
785
774
|
exports.toCamelCase = toCamelCase;
|
|
786
775
|
exports.transformForEval = transformForEval;
|
|
776
|
+
|
|
787
777
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":["Parser","MagicString","StateBackend","z"],"sources":["../src/utils.ts","../src/transform.ts","../src/session.ts","../src/middleware.ts"],"sourcesContent":["import { compile } from \"json-schema-to-typescript\";\nimport { toJsonSchema } from \"@langchain/core/utils/json_schema\";\nimport dedent from \"dedent\";\nimport type { ReplResult } from \"./types.js\";\n\n/**\n * Convert a snake_case or kebab-case string to camelCase.\n */\nexport function toCamelCase(name: string): string {\n return name.replace(/[-_]([a-z])/g, (_, c) => c.toUpperCase());\n}\n\n/**\n * Recursively collect all string values from an object, array, or primitive.\n */\nexport function collectStrings(obj: unknown): string[] {\n const result: string[] = [];\n function walk(val: unknown) {\n if (typeof val === \"string\") {\n result.push(val);\n } else if (Array.isArray(val)) {\n for (const item of val) walk(item);\n } else if (typeof val === \"object\" && val !== null) {\n for (const v of Object.values(val)) walk(v);\n }\n }\n walk(obj);\n return result;\n}\n\n/**\n * Format the result of a REPL evaluation for the agent.\n */\nexport function formatReplResult(result: ReplResult): string {\n const parts: string[] = [];\n\n if (result.logs.length > 0) {\n parts.push(result.logs.join(\"\\n\"));\n }\n\n if (result.ok) {\n if (result.value !== undefined) {\n const formatted =\n typeof result.value === \"string\"\n ? result.value\n : JSON.stringify(result.value, null, 2);\n parts.push(`→ ${formatted}`);\n }\n } else if (result.error) {\n const errName = result.error.name || \"Error\";\n const errMsg = result.error.message || \"Unknown error\";\n parts.push(`${errName}: ${errMsg}`);\n if (result.error.stack) {\n parts.push(result.error.stack);\n }\n }\n\n return parts.join(\"\\n\") || \"(no output)\";\n}\n\nexport function safeToJsonSchema(\n schema: unknown,\n): Record<string, unknown> | undefined {\n try {\n return toJsonSchema(schema as Parameters<typeof toJsonSchema>[0]) as Record<\n string,\n unknown\n >;\n } catch {\n return undefined;\n }\n}\n\nasync function schemaToInterface(\n jsonSchema: Record<string, unknown>,\n interfaceName: string,\n): Promise<string> {\n const compiled = await compile(\n { ...jsonSchema, additionalProperties: false },\n interfaceName,\n { bannerComment: \"\", additionalProperties: false },\n );\n return compiled.replace(/^export /, \"\").trimEnd();\n}\n\nexport function capitalize(s: string): string {\n return s.charAt(0).toUpperCase() + s.slice(1);\n}\n\nexport async function toolToTypeSignature(\n name: string,\n description: string,\n jsonSchema: Record<string, unknown> | undefined,\n): Promise<string> {\n const inputType = `${capitalize(name)}Input`;\n\n if (!jsonSchema || !jsonSchema.properties) {\n return dedent`\n /**\n * ${description}\n */\n async tools.${name}(input: Record<string, unknown>): Promise<string>\n `;\n }\n\n const iface = await schemaToInterface(jsonSchema, inputType);\n return dedent`\n ${iface}\n\n /**\n * ${description}\n */\n async tools.${name}(input: ${inputType}): Promise<string>\n `;\n}\n","/**\n * AST-based code transform pipeline for the REPL.\n *\n * Transforms TypeScript/JavaScript code into plain JS that can be\n * evaluated inside QuickJS with proper state persistence:\n *\n * 1. Parse with acorn + acorn-typescript (handles TS syntax)\n * 2. Strip TypeScript-only nodes (type annotations, interfaces, etc.)\n * 3. Hoist top-level declarations to globalThis for cross-eval persistence\n * 4. Auto-return the last expression\n * 5. Wrap in async IIFE so top-level await works\n */\n\nimport { Parser } from \"acorn\";\nimport { tsPlugin } from \"@sveltejs/acorn-typescript\";\nimport { walk } from \"estree-walker\";\nimport MagicString from \"magic-string\";\nimport type {\n Node,\n Identifier,\n VariableDeclaration as EstreeVariableDeclaration,\n VariableDeclarator as EstreeVariableDeclarator,\n} from \"estree\";\n\nconst TSParser = Parser.extend(tsPlugin());\n\ntype AcornNode = Node & { start: number; end: number };\ntype AcornExpressionStatement = AcornNode & {\n type: \"ExpressionStatement\";\n expression: AcornNode;\n};\ntype AcornVariableDeclaration = EstreeVariableDeclaration & {\n start: number;\n end: number;\n declarations: AcornVariableDeclarator[];\n};\ntype AcornVariableDeclarator = EstreeVariableDeclarator & {\n start: number;\n end: number;\n id: AcornNode;\n init: AcornNode | null;\n};\n\n/**\n * Transform code for REPL evaluation.\n *\n * - Strips TypeScript syntax\n * - Hoists top-level variable declarations to globalThis\n * - Auto-returns the last expression\n * - Wraps in async IIFE for top-level await support\n */\nexport function transformForEval(code: string): string {\n let ast: AcornNode;\n try {\n ast = TSParser.parse(code, {\n ecmaVersion: \"latest\" as any,\n sourceType: \"module\",\n locations: true,\n }) as unknown as AcornNode;\n } catch {\n // If parsing fails, return the code as-is and let QuickJS report the error\n return `(async () => {\\n${code}\\n})()`;\n }\n\n const s = new MagicString(code);\n const program = ast as unknown as { body: AcornNode[] };\n const topLevelNodes = program.body;\n for (let i = 0; i < topLevelNodes.length; i++) {\n const node = topLevelNodes[i];\n\n // Remove TypeScript-only top-level declarations\n if (isTSOnlyNode(node)) {\n s.remove(node.start, node.end);\n continue;\n }\n\n // Remove import/export declarations (not supported in QuickJS eval)\n if (\n node.type === \"ImportDeclaration\" ||\n node.type === \"ExportNamedDeclaration\" ||\n node.type === \"ExportDefaultDeclaration\" ||\n node.type === \"ExportAllDeclaration\"\n ) {\n s.remove(node.start, node.end);\n continue;\n }\n\n // Hoist top-level variable declarations\n if (node.type === \"VariableDeclaration\") {\n hoistDeclaration(s, node as unknown as AcornVariableDeclaration);\n continue;\n }\n\n // Hoist function/class declarations to globalThis for cross-eval persistence\n if (\n node.type === \"FunctionDeclaration\" ||\n node.type === \"ClassDeclaration\"\n ) {\n stripTypeAnnotations(s, node);\n const name = (node as any).id?.name;\n if (name) {\n s.appendRight(node.end, `\\nglobalThis.${name} = ${name};`);\n }\n continue;\n }\n }\n\n // Strip type annotations from within expressions/statements\n for (const node of topLevelNodes) {\n if (isTSOnlyNode(node)) continue;\n if (\n node.type === \"ImportDeclaration\" ||\n node.type === \"ExportNamedDeclaration\" ||\n node.type === \"ExportDefaultDeclaration\" ||\n node.type === \"ExportAllDeclaration\"\n )\n continue;\n if (node.type !== \"VariableDeclaration\") {\n walk(node as any, {\n enter(n: any) {\n stripTypeAnnotationFromNode(s, n);\n },\n });\n }\n }\n\n // Auto-return the last expression. We insert `return (` before the\n // ExpressionStatement (to preserve any grouping parens like `({...})`),\n // but close `)` after the inner expression — not after the statement —\n // so any trailing semicolon stays outside: `return (expr);` not `return (expr;)`.\n const lastNode = findLastNonEmptyNode(topLevelNodes, s);\n if (lastNode && isExpression(lastNode)) {\n const { expression } = lastNode as AcornExpressionStatement;\n s.prependLeft(lastNode.start, \"return (\");\n s.appendRight(expression.end, \")\");\n }\n\n // Wrap in async IIFE\n s.prepend(\"(async () => {\\n\");\n s.append(\"\\n})()\");\n\n return s.toString();\n}\n\nfunction isTSOnlyNode(node: AcornNode): boolean {\n const t = node.type as string;\n return (\n t === \"TSTypeAliasDeclaration\" ||\n t === \"TSInterfaceDeclaration\" ||\n t === \"TSEnumDeclaration\" ||\n t === \"TSModuleDeclaration\" ||\n t === \"TSDeclareFunction\" ||\n t.startsWith(\"TS\")\n );\n}\n\n/**\n * Rewrite a top-level VariableDeclaration to globalThis assignments.\n *\n * `const x = 1, y = 2` → `globalThis.x = 1; globalThis.y = 2`\n *\n */\nfunction hoistDeclaration(\n s: MagicString,\n decl: AcornVariableDeclaration,\n): void {\n const parts: string[] = [];\n\n for (const d of decl.declarations) {\n const id = d.id as AcornNode;\n if (id.type === \"Identifier\") {\n const initCode = d.init ? extractCleanInit(s, d) : \"undefined\";\n parts.push(\n `globalThis.${(id as unknown as Identifier).name} = ${initCode}`,\n );\n } else if (id.type === \"ObjectPattern\" || id.type === \"ArrayPattern\") {\n const bindings = extractBindingNames(d.id as any);\n const initCode = d.init ? extractCleanInit(s, d) : \"undefined\";\n const patternCode = extractCleanSource(s, d.id as AcornNode);\n parts.push(`var ${patternCode} = ${initCode}`);\n for (const name of bindings) {\n parts.push(`globalThis.${name} = ${name}`);\n }\n }\n }\n\n s.overwrite(decl.start, decl.end, parts.join(\"; \") + \";\");\n}\n\n/**\n * Extract the initializer code, stripping TypeScript annotations from\n * within the expression (e.g. `as Type`, generics, parameter types in\n * arrow functions).\n */\nfunction extractCleanInit(s: MagicString, d: AcornVariableDeclarator): string {\n if (!d.init) return \"undefined\";\n return extractCleanSource(s, d.init as AcornNode);\n}\n\nfunction extractBindingNames(pattern: any): string[] {\n const names: string[] = [];\n if (pattern.type === \"Identifier\") {\n if (pattern.name) names.push(pattern.name);\n } else if (pattern.type === \"ObjectPattern\") {\n for (const prop of pattern.properties || []) {\n if (prop.type === \"RestElement\") {\n names.push(...extractBindingNames(prop.argument));\n } else {\n names.push(...extractBindingNames(prop.value));\n }\n }\n } else if (pattern.type === \"ArrayPattern\") {\n for (const el of pattern.elements || []) {\n if (el) names.push(...extractBindingNames(el));\n }\n } else if (pattern.type === \"RestElement\") {\n names.push(...extractBindingNames(pattern.argument));\n } else if (pattern.type === \"AssignmentPattern\") {\n names.push(...extractBindingNames(pattern.left));\n }\n return names;\n}\n\nfunction stripTypeAnnotations(s: MagicString, node: AcornNode): void {\n walk(node as any, {\n enter(n: any) {\n stripTypeAnnotationFromNode(s, n);\n },\n });\n}\n\nfunction stripTypeAnnotationFromNode(s: MagicString, n: any, offset = 0): void {\n // Type annotations on parameters, variables, return types\n if (n.typeAnnotation && n.typeAnnotation.start != null) {\n s.remove(n.typeAnnotation.start - offset, n.typeAnnotation.end - offset);\n }\n // Return type on functions\n if (n.returnType && n.returnType.start != null) {\n s.remove(n.returnType.start - offset, n.returnType.end - offset);\n }\n // Type parameters (generics)\n if (n.typeParameters && n.typeParameters.start != null) {\n s.remove(n.typeParameters.start - offset, n.typeParameters.end - offset);\n }\n // Type arguments on calls\n if (n.typeArguments && n.typeArguments.start != null) {\n s.remove(n.typeArguments.start - offset, n.typeArguments.end - offset);\n }\n // `as` expressions: keep the expression, remove `as Type`\n if (n.type === \"TSAsExpression\" && n.expression) {\n s.remove(n.expression.end - offset, n.end - offset);\n }\n // Non-null assertion: `x!` → `x`\n if (n.type === \"TSNonNullExpression\" && n.expression) {\n s.remove(n.expression.end - offset, n.end - offset);\n }\n // Satisfies expression: `x satisfies Type` → `x`\n if (n.type === \"TSSatisfiesExpression\" && n.expression) {\n s.remove(n.expression.end - offset, n.end - offset);\n }\n}\n\n/**\n * Extract a clean JS source string from an AST node, stripping all\n * TypeScript annotations. Works on a copy so the main MagicString is\n * not mutated.\n */\nfunction extractCleanSource(s: MagicString, node: AcornNode): string {\n const offset = node.start;\n const source = new MagicString(s.slice(node.start, node.end));\n walk(node as any, {\n enter(n: any) {\n stripTypeAnnotationFromNode(source, n, offset);\n },\n });\n return source.toString();\n}\n\nfunction findLastNonEmptyNode(\n nodes: AcornNode[],\n s: MagicString,\n): AcornNode | null {\n for (let i = nodes.length - 1; i >= 0; i--) {\n const node = nodes[i];\n // Skip nodes that were fully removed\n const slice = s.slice(node.start, node.end).trim();\n if (slice === \"\" || slice === \";\") continue;\n return node;\n }\n return null;\n}\n\nfunction isExpression(node: AcornNode): boolean {\n return node.type === \"ExpressionStatement\";\n}\n","/**\n * Core REPL engine built on quickjs-emscripten (asyncify variant).\n *\n * Host async functions (backend I/O, PTC tools) are exposed as\n * promise-returning functions inside the QuickJS guest. Guest code\n * uses `await` to consume them, enabling real concurrency via\n * `Promise.all`, `Promise.race`, etc.\n *\n * We still use the asyncify WASM variant because `evalCodeAsync` is\n * required to drive promise resolution from the host side.\n *\n * ## Architecture\n *\n * `ReplSession` is a serializable handle that can live in LangGraph state.\n * It holds an `id` that keys into a static session map. The heavy QuickJS\n * runtime is lazily started on the first `.eval()` call, making the session\n * safe across graph interrupts and checkpointing.\n *\n * File writes inside the REPL are buffered (`pendingWrites`) and only\n * flushed to the backend after a script finishes executing. Call\n * `session.flushWrites(backend)` after eval to persist them.\n */\n\nimport { shouldInterruptAfterDeadline } from \"quickjs-emscripten\";\nimport type { QuickJSHandle } from \"quickjs-emscripten\";\nimport { newQuickJSAsyncWASMModuleFromVariant } from \"quickjs-emscripten-core\";\nimport type {\n QuickJSAsyncContext,\n QuickJSAsyncRuntime,\n} from \"quickjs-emscripten-core\";\nimport type { BackendProtocol } from \"deepagents\";\nimport type { StructuredToolInterface } from \"@langchain/core/tools\";\n\nimport type { ReplSessionOptions, ReplResult } from \"./types.js\";\nimport { toCamelCase } from \"./utils.js\";\nimport { transformForEval } from \"./transform.js\";\n\nexport const DEFAULT_MEMORY_LIMIT = 50 * 1024 * 1024;\nexport const DEFAULT_MAX_STACK_SIZE = 320 * 1024;\nexport const DEFAULT_EXECUTION_TIMEOUT = 30_000;\nexport const DEFAULT_SESSION_ID = \"__default__\";\n\nlet asyncModulePromise: Promise<any> | undefined;\n\nasync function getAsyncModule() {\n if (!asyncModulePromise) {\n asyncModulePromise = (async () => {\n const variant =\n await import(\"@jitl/quickjs-ng-wasmfile-release-asyncify\");\n return newQuickJSAsyncWASMModuleFromVariant(\n (variant.default ?? variant) as any,\n );\n })();\n }\n return asyncModulePromise;\n}\n\nexport interface PendingWrite {\n path: string;\n content: string;\n}\n\n/**\n * Sandboxed JavaScript REPL session backed by QuickJS WASM.\n *\n * Serializable — holds an `id` that keys into a static session map.\n * The QuickJS runtime is lazily started on the first `.eval()` call\n * and reconnected if a session with the same id already exists.\n * This makes it safe to store in LangGraph state across interrupts.\n *\n * File writes are buffered during execution and flushed via\n * `flushWrites(backend)` after eval completes.\n */\nexport class ReplSession {\n private static sessions = new Map<string, ReplSession>();\n\n readonly id: string;\n readonly pendingWrites: PendingWrite[] = [];\n\n private runtime: QuickJSAsyncRuntime | null = null;\n private context: QuickJSAsyncContext | null = null;\n private logs: string[] = [];\n private _options: ReplSessionOptions;\n\n private _backend: BackendProtocol | null = null;\n\n constructor(id: string, options: ReplSessionOptions = {}) {\n this.id = id;\n this._options = options;\n }\n\n get backend(): BackendProtocol | null {\n return this._backend;\n }\n\n set backend(b: BackendProtocol | null) {\n this._backend = b;\n }\n\n private async ensureStarted(): Promise<void> {\n if (this.runtime) return;\n\n const {\n memoryLimitBytes = DEFAULT_MEMORY_LIMIT,\n maxStackSizeBytes = DEFAULT_MAX_STACK_SIZE,\n backend,\n tools,\n } = this._options;\n\n const asyncModule = await getAsyncModule();\n const runtime: QuickJSAsyncRuntime = asyncModule.newRuntime();\n runtime.setMemoryLimit(memoryLimitBytes);\n runtime.setMaxStackSize(maxStackSizeBytes);\n\n const context: QuickJSAsyncContext = runtime.newContext();\n this.runtime = runtime;\n this.context = context;\n\n this.setupConsole();\n\n if (backend) {\n this._backend = backend;\n }\n this.injectVfs();\n if (tools && tools.length > 0) {\n this.injectTools(tools);\n }\n }\n\n /**\n * Get or create a session for the given id.\n *\n * Sessions are deduped by id — calling `getOrCreate` twice with the\n * same id returns the same instance. The QuickJS runtime is lazily\n * started on the first `.eval()` call.\n */\n static getOrCreate(\n id: string,\n options: ReplSessionOptions = {},\n ): ReplSession {\n const existing = ReplSession.sessions.get(id);\n if (existing) {\n if (options.backend) {\n existing._backend = options.backend;\n }\n return existing;\n }\n\n const session = new ReplSession(id, options);\n ReplSession.sessions.set(id, session);\n return session;\n }\n\n /**\n * Retrieve an existing session by id, or null if none exists.\n */\n static get(id: string): ReplSession | null {\n return ReplSession.sessions.get(id) ?? null;\n }\n\n /**\n * Evaluate code in this session.\n *\n * Lazily starts the QuickJS runtime on the first call. Code is\n * transformed via an AST pipeline that strips TypeScript syntax,\n * hoists top-level declarations to globalThis for cross-eval\n * persistence, auto-returns the last expression, and wraps in an\n * async IIFE.\n */\n async eval(code: string, timeoutMs: number): Promise<ReplResult> {\n await this.ensureStarted();\n const runtime = this.runtime!;\n const context = this.context!;\n\n this.logs.length = 0;\n\n if (timeoutMs >= 0) {\n runtime.setInterruptHandler(\n shouldInterruptAfterDeadline(Date.now() + timeoutMs),\n );\n } else {\n runtime.setInterruptHandler(() => false);\n }\n\n const transformed = transformForEval(code);\n const result = await context.evalCodeAsync(transformed);\n\n if (result.error) {\n const error = context.dump(result.error);\n result.error.dispose();\n return { ok: false, error, logs: [...this.logs] };\n }\n\n const promiseState = context.getPromiseState(result.value);\n\n if (promiseState.type === \"fulfilled\") {\n if (promiseState.notAPromise) {\n const value = context.dump(result.value);\n result.value.dispose();\n return { ok: true, value, logs: [...this.logs] };\n }\n const value = context.dump(promiseState.value);\n promiseState.value.dispose();\n result.value.dispose();\n return { ok: true, value, logs: [...this.logs] };\n }\n\n if (promiseState.type === \"rejected\") {\n const error = context.dump(promiseState.error);\n promiseState.error.dispose();\n result.value.dispose();\n return { ok: false, error, logs: [...this.logs] };\n }\n\n const noTimeout = timeoutMs < 0;\n const deadline = noTimeout ? Infinity : Date.now() + timeoutMs;\n while (noTimeout || Date.now() < deadline) {\n context.runtime.executePendingJobs();\n const state = context.getPromiseState(result.value);\n if (state.type === \"fulfilled\") {\n const value = context.dump(state.value);\n state.value.dispose();\n result.value.dispose();\n return { ok: true, value, logs: [...this.logs] };\n }\n if (state.type === \"rejected\") {\n const error = context.dump(state.error);\n state.error.dispose();\n result.value.dispose();\n return { ok: false, error, logs: [...this.logs] };\n }\n await new Promise((r) => setTimeout(r, 1));\n }\n\n result.value.dispose();\n return {\n ok: false,\n error: { message: \"Promise timed out — execution interrupted\" },\n logs: [...this.logs],\n };\n }\n\n async flushWrites(backend: BackendProtocol): Promise<void> {\n const writes = this.pendingWrites.splice(0);\n for (const { path, content } of writes) {\n await backend.write(path, content);\n }\n }\n\n dispose(): void {\n try {\n this.context?.dispose();\n } catch {\n /* may already be disposed */\n }\n try {\n this.runtime?.dispose();\n } catch {\n /* may already be disposed */\n }\n this.runtime = null;\n this.context = null;\n ReplSession.sessions.delete(this.id);\n }\n\n toJSON(): { id: string } {\n return { id: this.id };\n }\n\n static fromJSON(data: { id: string }): ReplSession {\n return ReplSession.sessions.get(data.id) ?? new ReplSession(data.id);\n }\n\n /**\n * Clear the static session cache. Useful for testing.\n * @internal\n */\n static clearCache(): void {\n for (const session of ReplSession.sessions.values()) {\n session.dispose();\n }\n ReplSession.sessions.clear();\n }\n\n private setupConsole(): void {\n const context = this.context!;\n const logs = this.logs;\n const consoleHandle = context.newObject();\n for (const method of [\"log\", \"warn\", \"error\", \"info\", \"debug\"] as const) {\n const fnHandle = context.newFunction(\n method,\n (...args: QuickJSHandle[]) => {\n const nativeArgs = args.map((a: QuickJSHandle) => context.dump(a));\n const formatted = nativeArgs\n .map((a: unknown) =>\n typeof a === \"object\" && a !== null\n ? JSON.stringify(a)\n : String(a),\n )\n .join(\" \");\n logs.push(\n method === \"log\" || method === \"info\" || method === \"debug\"\n ? formatted\n : `[${method}] ${formatted}`,\n );\n },\n );\n context.setProp(consoleHandle, method, fnHandle);\n fnHandle.dispose();\n }\n context.setProp(context.global, \"console\", consoleHandle);\n consoleHandle.dispose();\n }\n\n private injectVfs(): void {\n const context = this.context!;\n const getBackend = () => this._backend;\n const { pendingWrites } = this;\n\n const readFileHandle = context.newFunction(\n \"readFile\",\n (pathHandle: QuickJSHandle) => {\n const backend = getBackend();\n if (!backend) {\n const promise = context.newPromise();\n const err = context.newError(\"Backend not available\");\n promise.reject(err);\n err.dispose();\n promise.settled.then(context.runtime.executePendingJobs);\n return promise.handle;\n }\n const path = context.getString(pathHandle);\n const promise = context.newPromise();\n (async () => {\n try {\n const fileData = await backend.readRaw(path);\n const val = context.newString(fileData.content.join(\"\\n\"));\n promise.resolve(val);\n val.dispose();\n } catch {\n const err = context.newError(\n `ENOENT: no such file or directory '${path}'.`,\n );\n promise.reject(err);\n err.dispose();\n }\n promise.settled.then(context.runtime.executePendingJobs);\n })();\n return promise.handle;\n },\n );\n context.setProp(context.global, \"readFile\", readFileHandle);\n readFileHandle.dispose();\n\n const writeFileHandle = context.newFunction(\n \"writeFile\",\n (pathHandle: QuickJSHandle, contentHandle: QuickJSHandle) => {\n const path = context.getString(pathHandle);\n const content = context.getString(contentHandle);\n const promise = context.newPromise();\n pendingWrites.push({ path, content });\n promise.resolve(context.undefined);\n promise.settled.then(context.runtime.executePendingJobs);\n return promise.handle;\n },\n );\n context.setProp(context.global, \"writeFile\", writeFileHandle);\n writeFileHandle.dispose();\n }\n\n private injectTools(tools: StructuredToolInterface[]): void {\n const context = this.context!;\n const toolsNs = context.newObject();\n\n for (const t of tools) {\n const camelName = toCamelCase(t.name);\n const fnHandle = context.newFunction(\n camelName,\n (inputHandle: QuickJSHandle) => {\n const input = context.dump(inputHandle);\n const promise = context.newPromise();\n (async () => {\n try {\n const rawInput =\n typeof input === \"object\" && input !== null ? input : {};\n const result = await t.invoke(rawInput);\n const val = context.newString(\n typeof result === \"string\" ? result : JSON.stringify(result),\n );\n promise.resolve(val);\n val.dispose();\n } catch (e: unknown) {\n const msg =\n e != null && typeof (e as Error).message === \"string\"\n ? (e as Error).message\n : String(e);\n const err = context.newError(`Tool '${t.name}' failed: ${msg}`);\n promise.reject(err);\n err.dispose();\n }\n promise.settled.then(context.runtime.executePendingJobs);\n })();\n return promise.handle;\n },\n );\n context.setProp(toolsNs, camelName, fnHandle);\n fnHandle.dispose();\n }\n\n context.setProp(context.global, \"tools\", toolsNs);\n toolsNs.dispose();\n }\n}\n","/**\n * QuickJS REPL middleware for deepagents.\n *\n * Provides a `js_eval` tool that runs JavaScript in a WASM-sandboxed QuickJS\n * interpreter. Supports:\n * - Persistent state across evaluations (true REPL)\n * - VFS integration via readFile/writeFile\n * - Programmatic tool calling (PTC)\n */\n\nimport {\n createMiddleware,\n tool,\n type AgentMiddleware as _AgentMiddleware,\n} from \"langchain\";\nimport { z } from \"zod/v4\";\nimport type { StructuredToolInterface } from \"@langchain/core/tools\";\nimport {\n StateBackend,\n type BackendProtocol,\n type BackendFactory,\n type StateAndStore,\n} from \"deepagents\";\n\nimport dedent from \"dedent\";\nimport type { QuickJSMiddlewareOptions } from \"./types.js\";\nimport {\n ReplSession,\n DEFAULT_EXECUTION_TIMEOUT,\n DEFAULT_MEMORY_LIMIT,\n DEFAULT_MAX_STACK_SIZE,\n DEFAULT_SESSION_ID,\n} from \"./session.js\";\nimport {\n formatReplResult,\n toCamelCase,\n toolToTypeSignature,\n safeToJsonSchema,\n} from \"./utils.js\";\n\n/**\n * These type-only imports are required for TypeScript's type inference to work\n * correctly with the langchain/langgraph middleware system. Without them, certain\n * generic type parameters fail to resolve properly, causing runtime issues with\n * tool schemas and message types.\n */\nimport type * as _zodTypes from \"@langchain/core/utils/types\";\nimport type * as _zodMeta from \"@langchain/langgraph/zod\";\nimport type * as _messages from \"@langchain/core/messages\";\nimport { getCurrentTaskInput } from \"@langchain/langgraph\";\n\n/**\n * Backend-provided tools excluded from PTC by default.\n * These are redundant inside the REPL since VFS helpers (readFile/writeFile)\n * already cover file I/O against the agent's in-memory working set.\n */\nexport const DEFAULT_PTC_EXCLUDED_TOOLS = [\n \"ls\",\n \"read_file\",\n \"write_file\",\n \"edit_file\",\n \"glob\",\n \"grep\",\n \"execute\",\n] as const;\n\nconst REPL_SYSTEM_PROMPT = dedent`\n ## TypeScript/JavaScript REPL (\\`js_eval\\`)\n\n You have access to a sandboxed TypeScript/JavaScript REPL running in an isolated interpreter.\n TypeScript syntax (type annotations, interfaces, generics, \\`as\\` casts) is supported and stripped at evaluation time.\n Variables, functions, and closures persist across calls within the same session.\n\n ### Hard rules\n\n - **No network, no filesystem** — only the helpers below. Do not attempt \\`fetch\\`, \\`require\\`, or \\`import\\`.\n - **Cite your sources** — when reporting values from files, include the path and key/index so the user can verify.\n - **Use console.log()** for output — it is captured and returned. \\`console.warn()\\` and \\`console.error()\\` are also available.\n - **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.\n\n ### First-time usage\n\n \\`\\`\\`typescript\n // Read a file from the agent's virtual filesystem\n const raw: string = await readFile(\"/data.json\");\n const data = JSON.parse(raw) as { n: number };\n console.log(data);\n\n // Write results back\n await writeFile(\"/output.txt\", JSON.stringify({ result: data.n }));\n \\`\\`\\`\n\n ### API Reference — built-in globals\n\n \\`\\`\\`typescript\n /**\n * Read a file from the agent's virtual filesystem. Throws if the file does not exist.\n */\n async readFile(path: string): Promise<string>\n\n /**\n * Write a file to the agent's virtual filesystem.\n */\n async writeFile(path: string, content: string): Promise<void>\n \\`\\`\\`\n\n ### Limitations\n\n - ES2023+ syntax with TypeScript support. No Node.js APIs, no \\`require\\`, no \\`import\\`.\n - Output is truncated beyond a fixed character limit — be selective about what you log.\n - Execution timeout per call (default 30 s).\n`;\n\n/**\n * Generate the PTC API Reference section for the system prompt.\n */\nexport async function generatePtcPrompt(\n tools: StructuredToolInterface[],\n): Promise<string> {\n if (tools.length === 0) return \"\";\n\n const signatures = await Promise.all(\n tools.map((t) => {\n const jsonSchema = t.schema ? safeToJsonSchema(t.schema) : undefined;\n return toolToTypeSignature(\n toCamelCase(t.name),\n t.description,\n jsonSchema,\n );\n }),\n );\n\n return dedent`\n\n ### API Reference — \\`tools\\` namespace\n\n The following agent tools are callable as async functions inside the REPL.\n Each takes a single object argument and returns a Promise that resolves to a string.\n Use \\`await\\` to call them. Promise APIs like \\`Promise.all\\` are also available.\n\n **Example usage:**\n \\`\\`\\`javascript\n // Call a tool\n const result = await tools.searchWeb({ query: \"QuickJS tutorial\" });\n console.log(result);\n\n // Concurrent calls\n const [a, b] = await Promise.all([\n tools.fetchData({ url: \"https://api.example.com/a\" }),\n tools.fetchData({ url: \"https://api.example.com/b\" }),\n ]);\n \\`\\`\\`\n\n **Available functions:**\n \\`\\`\\`typescript\n ${signatures.join(\"\\n\\n\")}\n \\`\\`\\`\n `;\n}\n\n/**\n * Resolve backend from factory or instance.\n */\nfunction getBackend(\n backend: BackendProtocol | BackendFactory,\n stateAndStore: StateAndStore,\n): BackendProtocol {\n if (typeof backend === \"function\") {\n return backend(stateAndStore);\n }\n return backend;\n}\n\n/**\n * Create the QuickJS REPL middleware.\n */\nexport function createQuickJSMiddleware(\n options: QuickJSMiddlewareOptions = {},\n) {\n const {\n backend = (stateAndStore: StateAndStore) => new StateBackend(stateAndStore),\n ptc = false,\n memoryLimitBytes = DEFAULT_MEMORY_LIMIT,\n maxStackSizeBytes = DEFAULT_MAX_STACK_SIZE,\n executionTimeoutMs = DEFAULT_EXECUTION_TIMEOUT,\n systemPrompt: customSystemPrompt = null,\n } = options;\n\n const usePtc = ptc !== false;\n const baseSystemPrompt = customSystemPrompt || REPL_SYSTEM_PROMPT;\n\n let cachedPtcPrompt: string | null = null;\n\n let ptcTools: StructuredToolInterface[] = [];\n\n function filterToolsForPtc(\n allTools: StructuredToolInterface[],\n ): StructuredToolInterface[] {\n if (ptc === false) return [];\n\n const candidates = allTools.filter((t) => t.name !== \"js_eval\");\n\n if (ptc === true) {\n const excluded = new Set<string>(DEFAULT_PTC_EXCLUDED_TOOLS);\n return candidates.filter((t) => !excluded.has(t.name));\n }\n\n if (Array.isArray(ptc)) {\n const included = new Set(ptc);\n return candidates.filter((t) => included.has(t.name));\n }\n\n if (\"include\" in ptc) {\n const included = new Set(ptc.include);\n return candidates.filter((t) => included.has(t.name));\n }\n\n if (\"exclude\" in ptc) {\n const excluded = new Set([...DEFAULT_PTC_EXCLUDED_TOOLS, ...ptc.exclude]);\n return candidates.filter((t) => !excluded.has(t.name));\n }\n\n return [];\n }\n\n const jsEvalTool = tool(\n async (input, config) => {\n const threadId = config.configurable?.thread_id || DEFAULT_SESSION_ID;\n\n const stateAndStore: StateAndStore = {\n state: getCurrentTaskInput(config) || {},\n store: config.configurable?.__pregel_store,\n };\n const resolvedBackend = getBackend(backend, stateAndStore);\n\n const session = ReplSession.getOrCreate(threadId, {\n memoryLimitBytes,\n maxStackSizeBytes,\n backend: resolvedBackend,\n tools: ptcTools,\n });\n\n const result = await session.eval(input.code, executionTimeoutMs);\n await session.flushWrites(resolvedBackend);\n\n return formatReplResult(result);\n },\n {\n name: \"js_eval\",\n description: dedent`\n Evaluate TypeScript/JavaScript code in a sandboxed REPL. State persists across calls.\n Use readFile(path) and writeFile(path, content) for file access.\n Use console.log() for output. Returns the result of the last expression.\n `,\n schema: z.object({\n code: z\n .string()\n .describe(\n \"TypeScript/JavaScript code to evaluate in the sandboxed REPL\",\n ),\n }),\n },\n );\n\n return createMiddleware({\n name: \"QuickJSMiddleware\",\n tools: [jsEvalTool],\n wrapModelCall: async (request, handler) => {\n const agentTools = (request.tools || []) as StructuredToolInterface[];\n ptcTools = usePtc ? filterToolsForPtc(agentTools) : [];\n\n if (ptcTools.length > 0 && !cachedPtcPrompt) {\n cachedPtcPrompt = await generatePtcPrompt(ptcTools);\n }\n\n const systemMessage = request.systemMessage\n .concat(baseSystemPrompt)\n .concat(cachedPtcPrompt || \"\");\n return handler({ ...request, systemMessage });\n },\n });\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAQA,SAAgB,YAAY,MAA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I,EAAE,KAAK,CAAC;;AAGvD,MAAI,aAAa,KAAK;GACpB,MAAM,WAAW,IAAI,IAAI,CAAC,GAAG,4BAA4B,GAAG,IAAI,QAAQ,CAAC;AACzE,UAAO,WAAW,QAAQ,MAAM,CAAC,SAAS,IAAI,EAAE,KAAK,CAAC;;AAGxD,SAAO,EAAE;;AA0CX,wCAAwB;EACtB,MAAM;EACN,OAAO,qBAxCP,OAAO,OAAO,WAAW;GACvB,MAAM,WAAW,OAAO,cAAc,aAAa;GAMnD,MAAM,kBAAkB,WAAW,SAJE;IACnC,qDAA2B,OAAO,IAAI,EAAE;IACxC,OAAO,OAAO,cAAc;IAC7B,CACyD;GAE1D,MAAM,UAAU,YAAY,YAAY,UAAU;IAChD;IACA;IACA,SAAS;IACT,OAAO;IACR,CAAC;GAEF,MAAM,SAAS,MAAM,QAAQ,KAAK,MAAM,MAAM,mBAAmB;AACjE,SAAM,QAAQ,YAAY,gBAAgB;AAE1C,UAAO,iBAAiB,OAAO;KAEjC;GACE,MAAM;GACN,aAAa,cAAM;;;;;GAKnB,QAAQC,SAAE,OAAO,EACf,MAAMA,SACH,QAAQ,CACR,SACC,+DACD,EACJ,CAAC;GACH,CACF,CAIoB;EACnB,eAAe,OAAO,SAAS,YAAY;GACzC,MAAM,aAAc,QAAQ,SAAS,EAAE;AACvC,cAAW,SAAS,kBAAkB,WAAW,GAAG,EAAE;AAEtD,OAAI,SAAS,SAAS,KAAK,CAAC,gBAC1B,mBAAkB,MAAM,kBAAkB,SAAS;GAGrD,MAAM,gBAAgB,QAAQ,cAC3B,OAAO,iBAAiB,CACxB,OAAO,mBAAmB,GAAG;AAChC,UAAO,QAAQ;IAAE,GAAG;IAAS;IAAe,CAAC;;EAEhD,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["Parser","MagicString","StateBackend","z"],"sources":["../src/utils.ts","../src/transform.ts","../src/session.ts","../src/middleware.ts"],"sourcesContent":["import { compile } from \"json-schema-to-typescript\";\nimport { toJsonSchema } from \"@langchain/core/utils/json_schema\";\nimport dedent from \"dedent\";\nimport type { ReplResult } from \"./types.js\";\n\n/**\n * Convert a snake_case or kebab-case string to camelCase.\n */\nexport function toCamelCase(name: string): string {\n return name.replace(/[-_]([a-z])/g, (_, c) => c.toUpperCase());\n}\n\n/**\n * Recursively collect all string values from an object, array, or primitive.\n */\nexport function collectStrings(obj: unknown): string[] {\n const result: string[] = [];\n function walk(val: unknown) {\n if (typeof val === \"string\") {\n result.push(val);\n } else if (Array.isArray(val)) {\n for (const item of val) walk(item);\n } else if (typeof val === \"object\" && val !== null) {\n for (const v of Object.values(val)) walk(v);\n }\n }\n walk(obj);\n return result;\n}\n\n/**\n * Format the result of a REPL evaluation for the agent.\n */\nexport function formatReplResult(result: ReplResult): string {\n const parts: string[] = [];\n\n if (result.logs.length > 0) {\n parts.push(result.logs.join(\"\\n\"));\n }\n\n if (result.ok) {\n if (result.value !== undefined) {\n const formatted =\n typeof result.value === \"string\"\n ? result.value\n : JSON.stringify(result.value, null, 2);\n parts.push(`→ ${formatted}`);\n }\n } else if (result.error) {\n const errName = result.error.name || \"Error\";\n const errMsg = result.error.message || \"Unknown error\";\n parts.push(`${errName}: ${errMsg}`);\n if (result.error.stack) {\n parts.push(result.error.stack);\n }\n }\n\n return parts.join(\"\\n\") || \"(no output)\";\n}\n\nexport function safeToJsonSchema(\n schema: unknown,\n): Record<string, unknown> | undefined {\n try {\n return toJsonSchema(schema as Parameters<typeof toJsonSchema>[0]) as Record<\n string,\n unknown\n >;\n } catch {\n return undefined;\n }\n}\n\nasync function schemaToInterface(\n jsonSchema: Record<string, unknown>,\n interfaceName: string,\n): Promise<string> {\n const compiled = await compile(\n { ...jsonSchema, additionalProperties: false },\n interfaceName,\n { bannerComment: \"\", additionalProperties: false },\n );\n return compiled.replace(/^export /, \"\").trimEnd();\n}\n\nexport function capitalize(s: string): string {\n return s.charAt(0).toUpperCase() + s.slice(1);\n}\n\nexport async function toolToTypeSignature(\n name: string,\n description: string,\n jsonSchema: Record<string, unknown> | undefined,\n): Promise<string> {\n const inputType = `${capitalize(name)}Input`;\n\n if (!jsonSchema || !jsonSchema.properties) {\n return dedent`\n /**\n * ${description}\n */\n async tools.${name}(input: Record<string, unknown>): Promise<string>\n `;\n }\n\n const iface = await schemaToInterface(jsonSchema, inputType);\n return dedent`\n ${iface}\n\n /**\n * ${description}\n */\n async tools.${name}(input: ${inputType}): Promise<string>\n `;\n}\n","/**\n * AST-based code transform pipeline for the REPL.\n *\n * Transforms TypeScript/JavaScript code into plain JS that can be\n * evaluated inside QuickJS with proper state persistence:\n *\n * 1. Parse with acorn + acorn-typescript (handles TS syntax)\n * 2. Strip TypeScript-only nodes (type annotations, interfaces, etc.)\n * 3. Hoist top-level declarations to globalThis for cross-eval persistence\n * 4. Auto-return the last expression\n * 5. Wrap in async IIFE so top-level await works\n */\n\nimport { Parser } from \"acorn\";\nimport { tsPlugin } from \"@sveltejs/acorn-typescript\";\nimport { walk } from \"estree-walker\";\nimport MagicString from \"magic-string\";\nimport type {\n Node,\n Identifier,\n VariableDeclaration as EstreeVariableDeclaration,\n VariableDeclarator as EstreeVariableDeclarator,\n} from \"estree\";\n\nconst TSParser = Parser.extend(tsPlugin());\n\ntype AcornNode = Node & { start: number; end: number };\ntype AcornExpressionStatement = AcornNode & {\n type: \"ExpressionStatement\";\n expression: AcornNode;\n};\ntype AcornVariableDeclaration = EstreeVariableDeclaration & {\n start: number;\n end: number;\n declarations: AcornVariableDeclarator[];\n};\ntype AcornVariableDeclarator = EstreeVariableDeclarator & {\n start: number;\n end: number;\n id: AcornNode;\n init: AcornNode | null;\n};\n\n/**\n * Transform code for REPL evaluation.\n *\n * - Strips TypeScript syntax\n * - Hoists top-level variable declarations to globalThis\n * - Auto-returns the last expression\n * - Wraps in async IIFE for top-level await support\n */\nexport function transformForEval(code: string): string {\n let ast: AcornNode;\n try {\n ast = TSParser.parse(code, {\n ecmaVersion: \"latest\" as any,\n sourceType: \"module\",\n locations: true,\n }) as unknown as AcornNode;\n } catch {\n // If parsing fails, return the code as-is and let QuickJS report the error\n return `(async () => {\\n${code}\\n})()`;\n }\n\n const s = new MagicString(code);\n const program = ast as unknown as { body: AcornNode[] };\n const topLevelNodes = program.body;\n for (let i = 0; i < topLevelNodes.length; i++) {\n const node = topLevelNodes[i];\n\n // Remove TypeScript-only top-level declarations\n if (isTSOnlyNode(node)) {\n s.remove(node.start, node.end);\n continue;\n }\n\n // Remove import/export declarations (not supported in QuickJS eval)\n if (\n node.type === \"ImportDeclaration\" ||\n node.type === \"ExportNamedDeclaration\" ||\n node.type === \"ExportDefaultDeclaration\" ||\n node.type === \"ExportAllDeclaration\"\n ) {\n s.remove(node.start, node.end);\n continue;\n }\n\n // Hoist top-level variable declarations\n if (node.type === \"VariableDeclaration\") {\n hoistDeclaration(s, node as unknown as AcornVariableDeclaration);\n continue;\n }\n\n // Hoist function/class declarations to globalThis for cross-eval persistence\n if (\n node.type === \"FunctionDeclaration\" ||\n node.type === \"ClassDeclaration\"\n ) {\n stripTypeAnnotations(s, node);\n const name = (node as any).id?.name;\n if (name) {\n s.appendRight(node.end, `\\nglobalThis.${name} = ${name};`);\n }\n continue;\n }\n }\n\n // Strip type annotations from within expressions/statements\n for (const node of topLevelNodes) {\n if (isTSOnlyNode(node)) continue;\n if (\n node.type === \"ImportDeclaration\" ||\n node.type === \"ExportNamedDeclaration\" ||\n node.type === \"ExportDefaultDeclaration\" ||\n node.type === \"ExportAllDeclaration\"\n )\n continue;\n if (node.type !== \"VariableDeclaration\") {\n walk(node as any, {\n enter(n: any) {\n stripTypeAnnotationFromNode(s, n);\n },\n });\n }\n }\n\n // Auto-return the last expression. We insert `return (` before the\n // ExpressionStatement (to preserve any grouping parens like `({...})`),\n // but close `)` after the inner expression — not after the statement —\n // so any trailing semicolon stays outside: `return (expr);` not `return (expr;)`.\n const lastNode = findLastNonEmptyNode(topLevelNodes, s);\n if (lastNode && isExpression(lastNode)) {\n const { expression } = lastNode as AcornExpressionStatement;\n s.prependLeft(lastNode.start, \"return (\");\n s.appendRight(expression.end, \")\");\n }\n\n // Wrap in async IIFE\n s.prepend(\"(async () => {\\n\");\n s.append(\"\\n})()\");\n\n return s.toString();\n}\n\nfunction isTSOnlyNode(node: AcornNode): boolean {\n const t = node.type as string;\n return (\n t === \"TSTypeAliasDeclaration\" ||\n t === \"TSInterfaceDeclaration\" ||\n t === \"TSEnumDeclaration\" ||\n t === \"TSModuleDeclaration\" ||\n t === \"TSDeclareFunction\" ||\n t.startsWith(\"TS\")\n );\n}\n\n/**\n * Rewrite a top-level VariableDeclaration to globalThis assignments.\n *\n * `const x = 1, y = 2` → `globalThis.x = 1; globalThis.y = 2`\n *\n */\nfunction hoistDeclaration(\n s: MagicString,\n decl: AcornVariableDeclaration,\n): void {\n const parts: string[] = [];\n\n for (const d of decl.declarations) {\n const id = d.id as AcornNode;\n if (id.type === \"Identifier\") {\n const initCode = d.init ? extractCleanInit(s, d) : \"undefined\";\n parts.push(\n `globalThis.${(id as unknown as Identifier).name} = ${initCode}`,\n );\n } else if (id.type === \"ObjectPattern\" || id.type === \"ArrayPattern\") {\n const bindings = extractBindingNames(d.id as any);\n const initCode = d.init ? extractCleanInit(s, d) : \"undefined\";\n const patternCode = extractCleanSource(s, d.id as AcornNode);\n parts.push(`var ${patternCode} = ${initCode}`);\n for (const name of bindings) {\n parts.push(`globalThis.${name} = ${name}`);\n }\n }\n }\n\n s.overwrite(decl.start, decl.end, parts.join(\"; \") + \";\");\n}\n\n/**\n * Extract the initializer code, stripping TypeScript annotations from\n * within the expression (e.g. `as Type`, generics, parameter types in\n * arrow functions).\n */\nfunction extractCleanInit(s: MagicString, d: AcornVariableDeclarator): string {\n if (!d.init) return \"undefined\";\n return extractCleanSource(s, d.init as AcornNode);\n}\n\nfunction extractBindingNames(pattern: any): string[] {\n const names: string[] = [];\n if (pattern.type === \"Identifier\") {\n if (pattern.name) names.push(pattern.name);\n } else if (pattern.type === \"ObjectPattern\") {\n for (const prop of pattern.properties || []) {\n if (prop.type === \"RestElement\") {\n names.push(...extractBindingNames(prop.argument));\n } else {\n names.push(...extractBindingNames(prop.value));\n }\n }\n } else if (pattern.type === \"ArrayPattern\") {\n for (const el of pattern.elements || []) {\n if (el) names.push(...extractBindingNames(el));\n }\n } else if (pattern.type === \"RestElement\") {\n names.push(...extractBindingNames(pattern.argument));\n } else if (pattern.type === \"AssignmentPattern\") {\n names.push(...extractBindingNames(pattern.left));\n }\n return names;\n}\n\nfunction stripTypeAnnotations(s: MagicString, node: AcornNode): void {\n walk(node as any, {\n enter(n: any) {\n stripTypeAnnotationFromNode(s, n);\n },\n });\n}\n\nfunction stripTypeAnnotationFromNode(s: MagicString, n: any, offset = 0): void {\n // Type annotations on parameters, variables, return types\n if (n.typeAnnotation && n.typeAnnotation.start != null) {\n s.remove(n.typeAnnotation.start - offset, n.typeAnnotation.end - offset);\n }\n // Return type on functions\n if (n.returnType && n.returnType.start != null) {\n s.remove(n.returnType.start - offset, n.returnType.end - offset);\n }\n // Type parameters (generics)\n if (n.typeParameters && n.typeParameters.start != null) {\n s.remove(n.typeParameters.start - offset, n.typeParameters.end - offset);\n }\n // Type arguments on calls\n if (n.typeArguments && n.typeArguments.start != null) {\n s.remove(n.typeArguments.start - offset, n.typeArguments.end - offset);\n }\n // `as` expressions: keep the expression, remove `as Type`\n if (n.type === \"TSAsExpression\" && n.expression) {\n s.remove(n.expression.end - offset, n.end - offset);\n }\n // Non-null assertion: `x!` → `x`\n if (n.type === \"TSNonNullExpression\" && n.expression) {\n s.remove(n.expression.end - offset, n.end - offset);\n }\n // Satisfies expression: `x satisfies Type` → `x`\n if (n.type === \"TSSatisfiesExpression\" && n.expression) {\n s.remove(n.expression.end - offset, n.end - offset);\n }\n}\n\n/**\n * Extract a clean JS source string from an AST node, stripping all\n * TypeScript annotations. Works on a copy so the main MagicString is\n * not mutated.\n */\nfunction extractCleanSource(s: MagicString, node: AcornNode): string {\n const offset = node.start;\n const source = new MagicString(s.slice(node.start, node.end));\n walk(node as any, {\n enter(n: any) {\n stripTypeAnnotationFromNode(source, n, offset);\n },\n });\n return source.toString();\n}\n\nfunction findLastNonEmptyNode(\n nodes: AcornNode[],\n s: MagicString,\n): AcornNode | null {\n for (let i = nodes.length - 1; i >= 0; i--) {\n const node = nodes[i];\n // Skip nodes that were fully removed\n const slice = s.slice(node.start, node.end).trim();\n if (slice === \"\" || slice === \";\") continue;\n return node;\n }\n return null;\n}\n\nfunction isExpression(node: AcornNode): boolean {\n return node.type === \"ExpressionStatement\";\n}\n","/**\n * Core REPL engine built on quickjs-emscripten (asyncify variant).\n *\n * Host async functions (backend I/O, PTC tools) are exposed as\n * promise-returning functions inside the QuickJS guest. Guest code\n * uses `await` to consume them, enabling real concurrency via\n * `Promise.all`, `Promise.race`, etc.\n *\n * We still use the asyncify WASM variant because `evalCodeAsync` is\n * required to drive promise resolution from the host side.\n *\n * ## Architecture\n *\n * `ReplSession` is a serializable handle that can live in LangGraph state.\n * It holds an `id` that keys into a static session map. The heavy QuickJS\n * runtime is lazily started on the first `.eval()` call, making the session\n * safe across graph interrupts and checkpointing.\n *\n * File writes inside the REPL are buffered (`pendingWrites`) and only\n * flushed to the backend after a script finishes executing. Call\n * `session.flushWrites(backend)` after eval to persist them.\n */\n\nimport { shouldInterruptAfterDeadline } from \"quickjs-emscripten\";\nimport type { QuickJSHandle } from \"quickjs-emscripten\";\nimport { newQuickJSAsyncWASMModuleFromVariant } from \"quickjs-emscripten-core\";\nimport type {\n QuickJSAsyncContext,\n QuickJSAsyncRuntime,\n} from \"quickjs-emscripten-core\";\nimport type { BackendProtocol } from \"deepagents\";\nimport type { StructuredToolInterface } from \"@langchain/core/tools\";\n\nimport type { ReplSessionOptions, ReplResult } from \"./types.js\";\nimport { toCamelCase } from \"./utils.js\";\nimport { transformForEval } from \"./transform.js\";\n\nexport const DEFAULT_MEMORY_LIMIT = 50 * 1024 * 1024;\nexport const DEFAULT_MAX_STACK_SIZE = 320 * 1024;\nexport const DEFAULT_EXECUTION_TIMEOUT = 30_000;\nexport const DEFAULT_SESSION_ID = \"__default__\";\n\nlet asyncModulePromise: Promise<any> | undefined;\n\nasync function getAsyncModule() {\n if (!asyncModulePromise) {\n asyncModulePromise = (async () => {\n const variant =\n await import(\"@jitl/quickjs-ng-wasmfile-release-asyncify\");\n return newQuickJSAsyncWASMModuleFromVariant(\n (variant.default ?? variant) as any,\n );\n })();\n }\n return asyncModulePromise;\n}\n\nexport interface PendingWrite {\n path: string;\n content: string;\n}\n\n/**\n * Sandboxed JavaScript REPL session backed by QuickJS WASM.\n *\n * Serializable — holds an `id` that keys into a static session map.\n * The QuickJS runtime is lazily started on the first `.eval()` call\n * and reconnected if a session with the same id already exists.\n * This makes it safe to store in LangGraph state across interrupts.\n *\n * File writes are buffered during execution and flushed via\n * `flushWrites(backend)` after eval completes.\n */\nexport class ReplSession {\n private static sessions = new Map<string, ReplSession>();\n\n readonly id: string;\n readonly pendingWrites: PendingWrite[] = [];\n\n private runtime: QuickJSAsyncRuntime | null = null;\n private context: QuickJSAsyncContext | null = null;\n private logs: string[] = [];\n private _options: ReplSessionOptions;\n\n private _backend: BackendProtocol | null = null;\n\n constructor(id: string, options: ReplSessionOptions = {}) {\n this.id = id;\n this._options = options;\n }\n\n get backend(): BackendProtocol | null {\n return this._backend;\n }\n\n set backend(b: BackendProtocol | null) {\n this._backend = b;\n }\n\n private async ensureStarted(): Promise<void> {\n if (this.runtime) return;\n\n const {\n memoryLimitBytes = DEFAULT_MEMORY_LIMIT,\n maxStackSizeBytes = DEFAULT_MAX_STACK_SIZE,\n backend,\n tools,\n } = this._options;\n\n const asyncModule = await getAsyncModule();\n const runtime: QuickJSAsyncRuntime = asyncModule.newRuntime();\n runtime.setMemoryLimit(memoryLimitBytes);\n runtime.setMaxStackSize(maxStackSizeBytes);\n\n const context: QuickJSAsyncContext = runtime.newContext();\n this.runtime = runtime;\n this.context = context;\n\n this.setupConsole();\n\n if (backend) {\n this._backend = backend;\n }\n this.injectVfs();\n if (tools && tools.length > 0) {\n this.injectTools(tools);\n }\n }\n\n /**\n * Get or create a session for the given id.\n *\n * Sessions are deduped by id — calling `getOrCreate` twice with the\n * same id returns the same instance. The QuickJS runtime is lazily\n * started on the first `.eval()` call.\n */\n static getOrCreate(\n id: string,\n options: ReplSessionOptions = {},\n ): ReplSession {\n const existing = ReplSession.sessions.get(id);\n if (existing) {\n if (options.backend) {\n existing._backend = options.backend;\n }\n return existing;\n }\n\n const session = new ReplSession(id, options);\n ReplSession.sessions.set(id, session);\n return session;\n }\n\n /**\n * Retrieve an existing session by id, or null if none exists.\n */\n static get(id: string): ReplSession | null {\n return ReplSession.sessions.get(id) ?? null;\n }\n\n /**\n * Evaluate code in this session.\n *\n * Lazily starts the QuickJS runtime on the first call. Code is\n * transformed via an AST pipeline that strips TypeScript syntax,\n * hoists top-level declarations to globalThis for cross-eval\n * persistence, auto-returns the last expression, and wraps in an\n * async IIFE.\n */\n async eval(code: string, timeoutMs: number): Promise<ReplResult> {\n await this.ensureStarted();\n const runtime = this.runtime!;\n const context = this.context!;\n\n this.logs.length = 0;\n\n if (timeoutMs >= 0) {\n runtime.setInterruptHandler(\n shouldInterruptAfterDeadline(Date.now() + timeoutMs),\n );\n } else {\n runtime.setInterruptHandler(() => false);\n }\n\n const transformed = transformForEval(code);\n const result = await context.evalCodeAsync(transformed);\n\n if (result.error) {\n const error = context.dump(result.error);\n result.error.dispose();\n return { ok: false, error, logs: [...this.logs] };\n }\n\n const promiseState = context.getPromiseState(result.value);\n\n if (promiseState.type === \"fulfilled\") {\n if (promiseState.notAPromise) {\n const value = context.dump(result.value);\n result.value.dispose();\n return { ok: true, value, logs: [...this.logs] };\n }\n const value = context.dump(promiseState.value);\n promiseState.value.dispose();\n result.value.dispose();\n return { ok: true, value, logs: [...this.logs] };\n }\n\n if (promiseState.type === \"rejected\") {\n const error = context.dump(promiseState.error);\n promiseState.error.dispose();\n result.value.dispose();\n return { ok: false, error, logs: [...this.logs] };\n }\n\n const noTimeout = timeoutMs < 0;\n const deadline = noTimeout ? Infinity : Date.now() + timeoutMs;\n while (noTimeout || Date.now() < deadline) {\n context.runtime.executePendingJobs();\n const state = context.getPromiseState(result.value);\n if (state.type === \"fulfilled\") {\n const value = context.dump(state.value);\n state.value.dispose();\n result.value.dispose();\n return { ok: true, value, logs: [...this.logs] };\n }\n if (state.type === \"rejected\") {\n const error = context.dump(state.error);\n state.error.dispose();\n result.value.dispose();\n return { ok: false, error, logs: [...this.logs] };\n }\n await new Promise((r) => setTimeout(r, 1));\n }\n\n result.value.dispose();\n return {\n ok: false,\n error: { message: \"Promise timed out — execution interrupted\" },\n logs: [...this.logs],\n };\n }\n\n async flushWrites(backend: BackendProtocol): Promise<void> {\n const writes = this.pendingWrites.splice(0);\n for (const { path, content } of writes) {\n await backend.write(path, content);\n }\n }\n\n dispose(): void {\n try {\n this.context?.dispose();\n } catch {\n /* may already be disposed */\n }\n try {\n this.runtime?.dispose();\n } catch {\n /* may already be disposed */\n }\n this.runtime = null;\n this.context = null;\n ReplSession.sessions.delete(this.id);\n }\n\n toJSON(): { id: string } {\n return { id: this.id };\n }\n\n static fromJSON(data: { id: string }): ReplSession {\n return ReplSession.sessions.get(data.id) ?? new ReplSession(data.id);\n }\n\n /**\n * Clear the static session cache. Useful for testing.\n * @internal\n */\n static clearCache(): void {\n for (const session of ReplSession.sessions.values()) {\n session.dispose();\n }\n ReplSession.sessions.clear();\n }\n\n private setupConsole(): void {\n const context = this.context!;\n const logs = this.logs;\n const consoleHandle = context.newObject();\n for (const method of [\"log\", \"warn\", \"error\", \"info\", \"debug\"] as const) {\n const fnHandle = context.newFunction(\n method,\n (...args: QuickJSHandle[]) => {\n const nativeArgs = args.map((a: QuickJSHandle) => context.dump(a));\n const formatted = nativeArgs\n .map((a: unknown) =>\n typeof a === \"object\" && a !== null\n ? JSON.stringify(a)\n : String(a),\n )\n .join(\" \");\n logs.push(\n method === \"log\" || method === \"info\" || method === \"debug\"\n ? formatted\n : `[${method}] ${formatted}`,\n );\n },\n );\n context.setProp(consoleHandle, method, fnHandle);\n fnHandle.dispose();\n }\n context.setProp(context.global, \"console\", consoleHandle);\n consoleHandle.dispose();\n }\n\n private injectVfs(): void {\n const context = this.context!;\n const getBackend = () => this._backend;\n const { pendingWrites } = this;\n\n const readFileHandle = context.newFunction(\n \"readFile\",\n (pathHandle: QuickJSHandle) => {\n const backend = getBackend();\n if (!backend) {\n const promise = context.newPromise();\n const err = context.newError(\"Backend not available\");\n promise.reject(err);\n err.dispose();\n promise.settled.then(context.runtime.executePendingJobs);\n return promise.handle;\n }\n const path = context.getString(pathHandle);\n const promise = context.newPromise();\n (async () => {\n try {\n const fileData = await backend.readRaw(path);\n const val = context.newString(fileData.content.join(\"\\n\"));\n promise.resolve(val);\n val.dispose();\n } catch {\n const err = context.newError(\n `ENOENT: no such file or directory '${path}'.`,\n );\n promise.reject(err);\n err.dispose();\n }\n promise.settled.then(context.runtime.executePendingJobs);\n })();\n return promise.handle;\n },\n );\n context.setProp(context.global, \"readFile\", readFileHandle);\n readFileHandle.dispose();\n\n const writeFileHandle = context.newFunction(\n \"writeFile\",\n (pathHandle: QuickJSHandle, contentHandle: QuickJSHandle) => {\n const path = context.getString(pathHandle);\n const content = context.getString(contentHandle);\n const promise = context.newPromise();\n pendingWrites.push({ path, content });\n promise.resolve(context.undefined);\n promise.settled.then(context.runtime.executePendingJobs);\n return promise.handle;\n },\n );\n context.setProp(context.global, \"writeFile\", writeFileHandle);\n writeFileHandle.dispose();\n }\n\n private injectTools(tools: StructuredToolInterface[]): void {\n const context = this.context!;\n const toolsNs = context.newObject();\n\n for (const t of tools) {\n const camelName = toCamelCase(t.name);\n const fnHandle = context.newFunction(\n camelName,\n (inputHandle: QuickJSHandle) => {\n const input = context.dump(inputHandle);\n const promise = context.newPromise();\n (async () => {\n try {\n const rawInput =\n typeof input === \"object\" && input !== null ? input : {};\n const result = await t.invoke(rawInput);\n const val = context.newString(\n typeof result === \"string\" ? result : JSON.stringify(result),\n );\n promise.resolve(val);\n val.dispose();\n } catch (e: unknown) {\n const msg =\n e != null && typeof (e as Error).message === \"string\"\n ? (e as Error).message\n : String(e);\n const err = context.newError(`Tool '${t.name}' failed: ${msg}`);\n promise.reject(err);\n err.dispose();\n }\n promise.settled.then(context.runtime.executePendingJobs);\n })();\n return promise.handle;\n },\n );\n context.setProp(toolsNs, camelName, fnHandle);\n fnHandle.dispose();\n }\n\n context.setProp(context.global, \"tools\", toolsNs);\n toolsNs.dispose();\n }\n}\n","/**\n * QuickJS REPL middleware for deepagents.\n *\n * Provides a `js_eval` tool that runs JavaScript in a WASM-sandboxed QuickJS\n * interpreter. Supports:\n * - Persistent state across evaluations (true REPL)\n * - VFS integration via readFile/writeFile\n * - Programmatic tool calling (PTC)\n */\n\nimport {\n createMiddleware,\n tool,\n type AgentMiddleware as _AgentMiddleware,\n} from \"langchain\";\nimport { z } from \"zod/v4\";\nimport type { StructuredToolInterface } from \"@langchain/core/tools\";\nimport {\n StateBackend,\n type BackendProtocol,\n type BackendFactory,\n type StateAndStore,\n} from \"deepagents\";\n\nimport dedent from \"dedent\";\nimport type { QuickJSMiddlewareOptions } from \"./types.js\";\nimport {\n ReplSession,\n DEFAULT_EXECUTION_TIMEOUT,\n DEFAULT_MEMORY_LIMIT,\n DEFAULT_MAX_STACK_SIZE,\n DEFAULT_SESSION_ID,\n} from \"./session.js\";\nimport {\n formatReplResult,\n toCamelCase,\n toolToTypeSignature,\n safeToJsonSchema,\n} from \"./utils.js\";\n\n/**\n * These type-only imports are required for TypeScript's type inference to work\n * correctly with the langchain/langgraph middleware system. Without them, certain\n * generic type parameters fail to resolve properly, causing runtime issues with\n * tool schemas and message types.\n */\nimport type * as _zodTypes from \"@langchain/core/utils/types\";\nimport type * as _zodMeta from \"@langchain/langgraph/zod\";\nimport type * as _messages from \"@langchain/core/messages\";\nimport {\n getCurrentTaskInput,\n LangGraphRunnableConfig,\n} from \"@langchain/langgraph\";\n\n/**\n * Backend-provided tools excluded from PTC by default.\n * These are redundant inside the REPL since VFS helpers (readFile/writeFile)\n * already cover file I/O against the agent's in-memory working set.\n */\nexport const DEFAULT_PTC_EXCLUDED_TOOLS = [\n \"ls\",\n \"read_file\",\n \"write_file\",\n \"edit_file\",\n \"glob\",\n \"grep\",\n \"execute\",\n] as const;\n\nconst REPL_SYSTEM_PROMPT = dedent`\n ## TypeScript/JavaScript REPL (\\`js_eval\\`)\n\n You have access to a sandboxed TypeScript/JavaScript REPL running in an isolated interpreter.\n TypeScript syntax (type annotations, interfaces, generics, \\`as\\` casts) is supported and stripped at evaluation time.\n Variables, functions, and closures persist across calls within the same session.\n\n ### Hard rules\n\n - **No network, no filesystem** — only the helpers below. Do not attempt \\`fetch\\`, \\`require\\`, or \\`import\\`.\n - **Cite your sources** — when reporting values from files, include the path and key/index so the user can verify.\n - **Use console.log()** for output — it is captured and returned. \\`console.warn()\\` and \\`console.error()\\` are also available.\n - **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.\n\n ### First-time usage\n\n \\`\\`\\`typescript\n // Read a file from the agent's virtual filesystem\n const raw: string = await readFile(\"/data.json\");\n const data = JSON.parse(raw) as { n: number };\n console.log(data);\n\n // Write results back\n await writeFile(\"/output.txt\", JSON.stringify({ result: data.n }));\n \\`\\`\\`\n\n ### API Reference — built-in globals\n\n \\`\\`\\`typescript\n /**\n * Read a file from the agent's virtual filesystem. Throws if the file does not exist.\n */\n async readFile(path: string): Promise<string>\n\n /**\n * Write a file to the agent's virtual filesystem.\n */\n async writeFile(path: string, content: string): Promise<void>\n \\`\\`\\`\n\n ### Limitations\n\n - ES2023+ syntax with TypeScript support. No Node.js APIs, no \\`require\\`, no \\`import\\`.\n - Output is truncated beyond a fixed character limit — be selective about what you log.\n - Execution timeout per call (default 30 s).\n`;\n\n/**\n * Generate the PTC API Reference section for the system prompt.\n */\nexport async function generatePtcPrompt(\n tools: StructuredToolInterface[],\n): Promise<string> {\n if (tools.length === 0) return \"\";\n\n const signatures = await Promise.all(\n tools.map((t) => {\n const jsonSchema = t.schema ? safeToJsonSchema(t.schema) : undefined;\n return toolToTypeSignature(\n toCamelCase(t.name),\n t.description,\n jsonSchema,\n );\n }),\n );\n\n return dedent`\n\n ### API Reference — \\`tools\\` namespace\n\n The following agent tools are callable as async functions inside the REPL.\n Each takes a single object argument and returns a Promise that resolves to a string.\n Use \\`await\\` to call them. Promise APIs like \\`Promise.all\\` are also available.\n\n **Example usage:**\n \\`\\`\\`javascript\n // Call a tool\n const result = await tools.searchWeb({ query: \"QuickJS tutorial\" });\n console.log(result);\n\n // Concurrent calls\n const [a, b] = await Promise.all([\n tools.fetchData({ url: \"https://api.example.com/a\" }),\n tools.fetchData({ url: \"https://api.example.com/b\" }),\n ]);\n \\`\\`\\`\n\n **Available functions:**\n \\`\\`\\`typescript\n ${signatures.join(\"\\n\\n\")}\n \\`\\`\\`\n `;\n}\n\n/**\n * Resolve backend from factory or instance.\n */\nfunction getBackend(\n backend: BackendProtocol | BackendFactory,\n stateAndStore: StateAndStore,\n): BackendProtocol {\n if (typeof backend === \"function\") {\n return backend(stateAndStore);\n }\n return backend;\n}\n\n/**\n * Create the QuickJS REPL middleware.\n */\nexport function createQuickJSMiddleware(\n options: QuickJSMiddlewareOptions = {},\n) {\n const {\n backend = (stateAndStore: StateAndStore) => new StateBackend(stateAndStore),\n ptc = false,\n memoryLimitBytes = DEFAULT_MEMORY_LIMIT,\n maxStackSizeBytes = DEFAULT_MAX_STACK_SIZE,\n executionTimeoutMs = DEFAULT_EXECUTION_TIMEOUT,\n systemPrompt: customSystemPrompt = null,\n } = options;\n\n const usePtc = ptc !== false;\n const baseSystemPrompt = customSystemPrompt || REPL_SYSTEM_PROMPT;\n\n let cachedPtcPrompt: string | null = null;\n\n let ptcTools: StructuredToolInterface[] = [];\n\n function filterToolsForPtc(\n allTools: StructuredToolInterface[],\n ): StructuredToolInterface[] {\n if (ptc === false) return [];\n\n const candidates = allTools.filter((t) => t.name !== \"js_eval\");\n\n if (ptc === true) {\n const excluded = new Set<string>(DEFAULT_PTC_EXCLUDED_TOOLS);\n return candidates.filter((t) => !excluded.has(t.name));\n }\n\n if (Array.isArray(ptc)) {\n const included = new Set(ptc);\n return candidates.filter((t) => included.has(t.name));\n }\n\n if (\"include\" in ptc) {\n const included = new Set(ptc.include);\n return candidates.filter((t) => included.has(t.name));\n }\n\n if (\"exclude\" in ptc) {\n const excluded = new Set([...DEFAULT_PTC_EXCLUDED_TOOLS, ...ptc.exclude]);\n return candidates.filter((t) => !excluded.has(t.name));\n }\n\n return [];\n }\n\n const jsEvalTool = tool(\n async (input, config: LangGraphRunnableConfig) => {\n const threadId = config.configurable?.thread_id || DEFAULT_SESSION_ID;\n\n const stateAndStore: StateAndStore = {\n state: getCurrentTaskInput(config) || {},\n store: config.store,\n };\n const resolvedBackend = getBackend(backend, stateAndStore);\n\n const session = ReplSession.getOrCreate(threadId, {\n memoryLimitBytes,\n maxStackSizeBytes,\n backend: resolvedBackend,\n tools: ptcTools,\n });\n\n const result = await session.eval(input.code, executionTimeoutMs);\n await session.flushWrites(resolvedBackend);\n\n return formatReplResult(result);\n },\n {\n name: \"js_eval\",\n description: dedent`\n Evaluate TypeScript/JavaScript code in a sandboxed REPL. State persists across calls.\n Use readFile(path) and writeFile(path, content) for file access.\n Use console.log() for output. Returns the result of the last expression.\n `,\n schema: z.object({\n code: z\n .string()\n .describe(\n \"TypeScript/JavaScript code to evaluate in the sandboxed REPL\",\n ),\n }),\n },\n );\n\n return createMiddleware({\n name: \"QuickJSMiddleware\",\n tools: [jsEvalTool],\n wrapModelCall: async (request, handler) => {\n const agentTools = (request.tools || []) as StructuredToolInterface[];\n ptcTools = usePtc ? filterToolsForPtc(agentTools) : [];\n\n if (ptcTools.length > 0 && !cachedPtcPrompt) {\n cachedPtcPrompt = await generatePtcPrompt(ptcTools);\n }\n\n const systemMessage = request.systemMessage\n .concat(baseSystemPrompt)\n .concat(cachedPtcPrompt || \"\");\n return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,QAAQ,cAC3B,OAAO,iBAAiB,CACxB,OAAO,mBAAmB,GAAG;AAChC,UAAO,QAAQ;IAAE,GAAG;IAAS;IAAe,CAAC;;EAEhD,CAAC"}
|
package/dist/index.d.cts
CHANGED
|
@@ -93,7 +93,7 @@ declare function createQuickJSMiddleware(options?: QuickJSMiddlewareOptions): Ag
|
|
|
93
93
|
code: string;
|
|
94
94
|
}, {
|
|
95
95
|
code: string;
|
|
96
|
-
}, string, "js_eval">]>;
|
|
96
|
+
}, string, unknown, "js_eval">]>;
|
|
97
97
|
//#endregion
|
|
98
98
|
//#region src/session.d.ts
|
|
99
99
|
declare const DEFAULT_MEMORY_LIMIT: number;
|
package/dist/index.d.ts
CHANGED
|
@@ -93,7 +93,7 @@ declare function createQuickJSMiddleware(options?: QuickJSMiddlewareOptions): Ag
|
|
|
93
93
|
code: string;
|
|
94
94
|
}, {
|
|
95
95
|
code: string;
|
|
96
|
-
}, string, "js_eval">]>;
|
|
96
|
+
}, string, unknown, "js_eval">]>;
|
|
97
97
|
//#endregion
|
|
98
98
|
//#region src/session.d.ts
|
|
99
99
|
declare const DEFAULT_MEMORY_LIMIT: number;
|
package/dist/index.js
CHANGED
|
@@ -11,7 +11,6 @@ import { tsPlugin } from "@sveltejs/acorn-typescript";
|
|
|
11
11
|
import { walk } from "estree-walker";
|
|
12
12
|
import MagicString from "magic-string";
|
|
13
13
|
import { getCurrentTaskInput } from "@langchain/langgraph";
|
|
14
|
-
|
|
15
14
|
//#region src/utils.ts
|
|
16
15
|
/**
|
|
17
16
|
* Convert a snake_case or kebab-case string to camelCase.
|
|
@@ -74,7 +73,6 @@ async function toolToTypeSignature(name, description, jsonSchema) {
|
|
|
74
73
|
async tools.${name}(input: ${inputType}): Promise<string>
|
|
75
74
|
`;
|
|
76
75
|
}
|
|
77
|
-
|
|
78
76
|
//#endregion
|
|
79
77
|
//#region src/transform.ts
|
|
80
78
|
/**
|
|
@@ -236,7 +234,6 @@ function findLastNonEmptyNode(nodes, s) {
|
|
|
236
234
|
function isExpression(node) {
|
|
237
235
|
return node.type === "ExpressionStatement";
|
|
238
236
|
}
|
|
239
|
-
|
|
240
237
|
//#endregion
|
|
241
238
|
//#region src/session.ts
|
|
242
239
|
/**
|
|
@@ -264,7 +261,6 @@ function isExpression(node) {
|
|
|
264
261
|
const DEFAULT_MEMORY_LIMIT = 50 * 1024 * 1024;
|
|
265
262
|
const DEFAULT_MAX_STACK_SIZE = 320 * 1024;
|
|
266
263
|
const DEFAULT_EXECUTION_TIMEOUT = 3e4;
|
|
267
|
-
const DEFAULT_SESSION_ID = "__default__";
|
|
268
264
|
let asyncModulePromise;
|
|
269
265
|
async function getAsyncModule() {
|
|
270
266
|
if (!asyncModulePromise) asyncModulePromise = (async () => {
|
|
@@ -561,7 +557,6 @@ var ReplSession = class ReplSession {
|
|
|
561
557
|
toolsNs.dispose();
|
|
562
558
|
}
|
|
563
559
|
};
|
|
564
|
-
|
|
565
560
|
//#endregion
|
|
566
561
|
//#region src/middleware.ts
|
|
567
562
|
/**
|
|
@@ -708,10 +703,10 @@ function createQuickJSMiddleware(options = {}) {
|
|
|
708
703
|
return createMiddleware({
|
|
709
704
|
name: "QuickJSMiddleware",
|
|
710
705
|
tools: [tool(async (input, config) => {
|
|
711
|
-
const threadId = config.configurable?.thread_id ||
|
|
706
|
+
const threadId = config.configurable?.thread_id || "__default__";
|
|
712
707
|
const resolvedBackend = getBackend(backend, {
|
|
713
708
|
state: getCurrentTaskInput(config) || {},
|
|
714
|
-
store: config.
|
|
709
|
+
store: config.store
|
|
715
710
|
});
|
|
716
711
|
const session = ReplSession.getOrCreate(threadId, {
|
|
717
712
|
memoryLimitBytes,
|
|
@@ -743,7 +738,7 @@ function createQuickJSMiddleware(options = {}) {
|
|
|
743
738
|
}
|
|
744
739
|
});
|
|
745
740
|
}
|
|
746
|
-
|
|
747
741
|
//#endregion
|
|
748
742
|
export { DEFAULT_EXECUTION_TIMEOUT, DEFAULT_MAX_STACK_SIZE, DEFAULT_MEMORY_LIMIT, DEFAULT_PTC_EXCLUDED_TOOLS, ReplSession, createQuickJSMiddleware, formatReplResult, toCamelCase, transformForEval };
|
|
743
|
+
|
|
749
744
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../src/utils.ts","../src/transform.ts","../src/session.ts","../src/middleware.ts"],"sourcesContent":["import { compile } from \"json-schema-to-typescript\";\nimport { toJsonSchema } from \"@langchain/core/utils/json_schema\";\nimport dedent from \"dedent\";\nimport type { ReplResult } from \"./types.js\";\n\n/**\n * Convert a snake_case or kebab-case string to camelCase.\n */\nexport function toCamelCase(name: string): string {\n return name.replace(/[-_]([a-z])/g, (_, c) => c.toUpperCase());\n}\n\n/**\n * Recursively collect all string values from an object, array, or primitive.\n */\nexport function collectStrings(obj: unknown): string[] {\n const result: string[] = [];\n function walk(val: unknown) {\n if (typeof val === \"string\") {\n result.push(val);\n } else if (Array.isArray(val)) {\n for (const item of val) walk(item);\n } else if (typeof val === \"object\" && val !== null) {\n for (const v of Object.values(val)) walk(v);\n }\n }\n walk(obj);\n return result;\n}\n\n/**\n * Format the result of a REPL evaluation for the agent.\n */\nexport function formatReplResult(result: ReplResult): string {\n const parts: string[] = [];\n\n if (result.logs.length > 0) {\n parts.push(result.logs.join(\"\\n\"));\n }\n\n if (result.ok) {\n if (result.value !== undefined) {\n const formatted =\n typeof result.value === \"string\"\n ? result.value\n : JSON.stringify(result.value, null, 2);\n parts.push(`→ ${formatted}`);\n }\n } else if (result.error) {\n const errName = result.error.name || \"Error\";\n const errMsg = result.error.message || \"Unknown error\";\n parts.push(`${errName}: ${errMsg}`);\n if (result.error.stack) {\n parts.push(result.error.stack);\n }\n }\n\n return parts.join(\"\\n\") || \"(no output)\";\n}\n\nexport function safeToJsonSchema(\n schema: unknown,\n): Record<string, unknown> | undefined {\n try {\n return toJsonSchema(schema as Parameters<typeof toJsonSchema>[0]) as Record<\n string,\n unknown\n >;\n } catch {\n return undefined;\n }\n}\n\nasync function schemaToInterface(\n jsonSchema: Record<string, unknown>,\n interfaceName: string,\n): Promise<string> {\n const compiled = await compile(\n { ...jsonSchema, additionalProperties: false },\n interfaceName,\n { bannerComment: \"\", additionalProperties: false },\n );\n return compiled.replace(/^export /, \"\").trimEnd();\n}\n\nexport function capitalize(s: string): string {\n return s.charAt(0).toUpperCase() + s.slice(1);\n}\n\nexport async function toolToTypeSignature(\n name: string,\n description: string,\n jsonSchema: Record<string, unknown> | undefined,\n): Promise<string> {\n const inputType = `${capitalize(name)}Input`;\n\n if (!jsonSchema || !jsonSchema.properties) {\n return dedent`\n /**\n * ${description}\n */\n async tools.${name}(input: Record<string, unknown>): Promise<string>\n `;\n }\n\n const iface = await schemaToInterface(jsonSchema, inputType);\n return dedent`\n ${iface}\n\n /**\n * ${description}\n */\n async tools.${name}(input: ${inputType}): Promise<string>\n `;\n}\n","/**\n * AST-based code transform pipeline for the REPL.\n *\n * Transforms TypeScript/JavaScript code into plain JS that can be\n * evaluated inside QuickJS with proper state persistence:\n *\n * 1. Parse with acorn + acorn-typescript (handles TS syntax)\n * 2. Strip TypeScript-only nodes (type annotations, interfaces, etc.)\n * 3. Hoist top-level declarations to globalThis for cross-eval persistence\n * 4. Auto-return the last expression\n * 5. Wrap in async IIFE so top-level await works\n */\n\nimport { Parser } from \"acorn\";\nimport { tsPlugin } from \"@sveltejs/acorn-typescript\";\nimport { walk } from \"estree-walker\";\nimport MagicString from \"magic-string\";\nimport type {\n Node,\n Identifier,\n VariableDeclaration as EstreeVariableDeclaration,\n VariableDeclarator as EstreeVariableDeclarator,\n} from \"estree\";\n\nconst TSParser = Parser.extend(tsPlugin());\n\ntype AcornNode = Node & { start: number; end: number };\ntype AcornExpressionStatement = AcornNode & {\n type: \"ExpressionStatement\";\n expression: AcornNode;\n};\ntype AcornVariableDeclaration = EstreeVariableDeclaration & {\n start: number;\n end: number;\n declarations: AcornVariableDeclarator[];\n};\ntype AcornVariableDeclarator = EstreeVariableDeclarator & {\n start: number;\n end: number;\n id: AcornNode;\n init: AcornNode | null;\n};\n\n/**\n * Transform code for REPL evaluation.\n *\n * - Strips TypeScript syntax\n * - Hoists top-level variable declarations to globalThis\n * - Auto-returns the last expression\n * - Wraps in async IIFE for top-level await support\n */\nexport function transformForEval(code: string): string {\n let ast: AcornNode;\n try {\n ast = TSParser.parse(code, {\n ecmaVersion: \"latest\" as any,\n sourceType: \"module\",\n locations: true,\n }) as unknown as AcornNode;\n } catch {\n // If parsing fails, return the code as-is and let QuickJS report the error\n return `(async () => {\\n${code}\\n})()`;\n }\n\n const s = new MagicString(code);\n const program = ast as unknown as { body: AcornNode[] };\n const topLevelNodes = program.body;\n for (let i = 0; i < topLevelNodes.length; i++) {\n const node = topLevelNodes[i];\n\n // Remove TypeScript-only top-level declarations\n if (isTSOnlyNode(node)) {\n s.remove(node.start, node.end);\n continue;\n }\n\n // Remove import/export declarations (not supported in QuickJS eval)\n if (\n node.type === \"ImportDeclaration\" ||\n node.type === \"ExportNamedDeclaration\" ||\n node.type === \"ExportDefaultDeclaration\" ||\n node.type === \"ExportAllDeclaration\"\n ) {\n s.remove(node.start, node.end);\n continue;\n }\n\n // Hoist top-level variable declarations\n if (node.type === \"VariableDeclaration\") {\n hoistDeclaration(s, node as unknown as AcornVariableDeclaration);\n continue;\n }\n\n // Hoist function/class declarations to globalThis for cross-eval persistence\n if (\n node.type === \"FunctionDeclaration\" ||\n node.type === \"ClassDeclaration\"\n ) {\n stripTypeAnnotations(s, node);\n const name = (node as any).id?.name;\n if (name) {\n s.appendRight(node.end, `\\nglobalThis.${name} = ${name};`);\n }\n continue;\n }\n }\n\n // Strip type annotations from within expressions/statements\n for (const node of topLevelNodes) {\n if (isTSOnlyNode(node)) continue;\n if (\n node.type === \"ImportDeclaration\" ||\n node.type === \"ExportNamedDeclaration\" ||\n node.type === \"ExportDefaultDeclaration\" ||\n node.type === \"ExportAllDeclaration\"\n )\n continue;\n if (node.type !== \"VariableDeclaration\") {\n walk(node as any, {\n enter(n: any) {\n stripTypeAnnotationFromNode(s, n);\n },\n });\n }\n }\n\n // Auto-return the last expression. We insert `return (` before the\n // ExpressionStatement (to preserve any grouping parens like `({...})`),\n // but close `)` after the inner expression — not after the statement —\n // so any trailing semicolon stays outside: `return (expr);` not `return (expr;)`.\n const lastNode = findLastNonEmptyNode(topLevelNodes, s);\n if (lastNode && isExpression(lastNode)) {\n const { expression } = lastNode as AcornExpressionStatement;\n s.prependLeft(lastNode.start, \"return (\");\n s.appendRight(expression.end, \")\");\n }\n\n // Wrap in async IIFE\n s.prepend(\"(async () => {\\n\");\n s.append(\"\\n})()\");\n\n return s.toString();\n}\n\nfunction isTSOnlyNode(node: AcornNode): boolean {\n const t = node.type as string;\n return (\n t === \"TSTypeAliasDeclaration\" ||\n t === \"TSInterfaceDeclaration\" ||\n t === \"TSEnumDeclaration\" ||\n t === \"TSModuleDeclaration\" ||\n t === \"TSDeclareFunction\" ||\n t.startsWith(\"TS\")\n );\n}\n\n/**\n * Rewrite a top-level VariableDeclaration to globalThis assignments.\n *\n * `const x = 1, y = 2` → `globalThis.x = 1; globalThis.y = 2`\n *\n */\nfunction hoistDeclaration(\n s: MagicString,\n decl: AcornVariableDeclaration,\n): void {\n const parts: string[] = [];\n\n for (const d of decl.declarations) {\n const id = d.id as AcornNode;\n if (id.type === \"Identifier\") {\n const initCode = d.init ? extractCleanInit(s, d) : \"undefined\";\n parts.push(\n `globalThis.${(id as unknown as Identifier).name} = ${initCode}`,\n );\n } else if (id.type === \"ObjectPattern\" || id.type === \"ArrayPattern\") {\n const bindings = extractBindingNames(d.id as any);\n const initCode = d.init ? extractCleanInit(s, d) : \"undefined\";\n const patternCode = extractCleanSource(s, d.id as AcornNode);\n parts.push(`var ${patternCode} = ${initCode}`);\n for (const name of bindings) {\n parts.push(`globalThis.${name} = ${name}`);\n }\n }\n }\n\n s.overwrite(decl.start, decl.end, parts.join(\"; \") + \";\");\n}\n\n/**\n * Extract the initializer code, stripping TypeScript annotations from\n * within the expression (e.g. `as Type`, generics, parameter types in\n * arrow functions).\n */\nfunction extractCleanInit(s: MagicString, d: AcornVariableDeclarator): string {\n if (!d.init) return \"undefined\";\n return extractCleanSource(s, d.init as AcornNode);\n}\n\nfunction extractBindingNames(pattern: any): string[] {\n const names: string[] = [];\n if (pattern.type === \"Identifier\") {\n if (pattern.name) names.push(pattern.name);\n } else if (pattern.type === \"ObjectPattern\") {\n for (const prop of pattern.properties || []) {\n if (prop.type === \"RestElement\") {\n names.push(...extractBindingNames(prop.argument));\n } else {\n names.push(...extractBindingNames(prop.value));\n }\n }\n } else if (pattern.type === \"ArrayPattern\") {\n for (const el of pattern.elements || []) {\n if (el) names.push(...extractBindingNames(el));\n }\n } else if (pattern.type === \"RestElement\") {\n names.push(...extractBindingNames(pattern.argument));\n } else if (pattern.type === \"AssignmentPattern\") {\n names.push(...extractBindingNames(pattern.left));\n }\n return names;\n}\n\nfunction stripTypeAnnotations(s: MagicString, node: AcornNode): void {\n walk(node as any, {\n enter(n: any) {\n stripTypeAnnotationFromNode(s, n);\n },\n });\n}\n\nfunction stripTypeAnnotationFromNode(s: MagicString, n: any, offset = 0): void {\n // Type annotations on parameters, variables, return types\n if (n.typeAnnotation && n.typeAnnotation.start != null) {\n s.remove(n.typeAnnotation.start - offset, n.typeAnnotation.end - offset);\n }\n // Return type on functions\n if (n.returnType && n.returnType.start != null) {\n s.remove(n.returnType.start - offset, n.returnType.end - offset);\n }\n // Type parameters (generics)\n if (n.typeParameters && n.typeParameters.start != null) {\n s.remove(n.typeParameters.start - offset, n.typeParameters.end - offset);\n }\n // Type arguments on calls\n if (n.typeArguments && n.typeArguments.start != null) {\n s.remove(n.typeArguments.start - offset, n.typeArguments.end - offset);\n }\n // `as` expressions: keep the expression, remove `as Type`\n if (n.type === \"TSAsExpression\" && n.expression) {\n s.remove(n.expression.end - offset, n.end - offset);\n }\n // Non-null assertion: `x!` → `x`\n if (n.type === \"TSNonNullExpression\" && n.expression) {\n s.remove(n.expression.end - offset, n.end - offset);\n }\n // Satisfies expression: `x satisfies Type` → `x`\n if (n.type === \"TSSatisfiesExpression\" && n.expression) {\n s.remove(n.expression.end - offset, n.end - offset);\n }\n}\n\n/**\n * Extract a clean JS source string from an AST node, stripping all\n * TypeScript annotations. Works on a copy so the main MagicString is\n * not mutated.\n */\nfunction extractCleanSource(s: MagicString, node: AcornNode): string {\n const offset = node.start;\n const source = new MagicString(s.slice(node.start, node.end));\n walk(node as any, {\n enter(n: any) {\n stripTypeAnnotationFromNode(source, n, offset);\n },\n });\n return source.toString();\n}\n\nfunction findLastNonEmptyNode(\n nodes: AcornNode[],\n s: MagicString,\n): AcornNode | null {\n for (let i = nodes.length - 1; i >= 0; i--) {\n const node = nodes[i];\n // Skip nodes that were fully removed\n const slice = s.slice(node.start, node.end).trim();\n if (slice === \"\" || slice === \";\") continue;\n return node;\n }\n return null;\n}\n\nfunction isExpression(node: AcornNode): boolean {\n return node.type === \"ExpressionStatement\";\n}\n","/**\n * Core REPL engine built on quickjs-emscripten (asyncify variant).\n *\n * Host async functions (backend I/O, PTC tools) are exposed as\n * promise-returning functions inside the QuickJS guest. Guest code\n * uses `await` to consume them, enabling real concurrency via\n * `Promise.all`, `Promise.race`, etc.\n *\n * We still use the asyncify WASM variant because `evalCodeAsync` is\n * required to drive promise resolution from the host side.\n *\n * ## Architecture\n *\n * `ReplSession` is a serializable handle that can live in LangGraph state.\n * It holds an `id` that keys into a static session map. The heavy QuickJS\n * runtime is lazily started on the first `.eval()` call, making the session\n * safe across graph interrupts and checkpointing.\n *\n * File writes inside the REPL are buffered (`pendingWrites`) and only\n * flushed to the backend after a script finishes executing. Call\n * `session.flushWrites(backend)` after eval to persist them.\n */\n\nimport { shouldInterruptAfterDeadline } from \"quickjs-emscripten\";\nimport type { QuickJSHandle } from \"quickjs-emscripten\";\nimport { newQuickJSAsyncWASMModuleFromVariant } from \"quickjs-emscripten-core\";\nimport type {\n QuickJSAsyncContext,\n QuickJSAsyncRuntime,\n} from \"quickjs-emscripten-core\";\nimport type { BackendProtocol } from \"deepagents\";\nimport type { StructuredToolInterface } from \"@langchain/core/tools\";\n\nimport type { ReplSessionOptions, ReplResult } from \"./types.js\";\nimport { toCamelCase } from \"./utils.js\";\nimport { transformForEval } from \"./transform.js\";\n\nexport const DEFAULT_MEMORY_LIMIT = 50 * 1024 * 1024;\nexport const DEFAULT_MAX_STACK_SIZE = 320 * 1024;\nexport const DEFAULT_EXECUTION_TIMEOUT = 30_000;\nexport const DEFAULT_SESSION_ID = \"__default__\";\n\nlet asyncModulePromise: Promise<any> | undefined;\n\nasync function getAsyncModule() {\n if (!asyncModulePromise) {\n asyncModulePromise = (async () => {\n const variant =\n await import(\"@jitl/quickjs-ng-wasmfile-release-asyncify\");\n return newQuickJSAsyncWASMModuleFromVariant(\n (variant.default ?? variant) as any,\n );\n })();\n }\n return asyncModulePromise;\n}\n\nexport interface PendingWrite {\n path: string;\n content: string;\n}\n\n/**\n * Sandboxed JavaScript REPL session backed by QuickJS WASM.\n *\n * Serializable — holds an `id` that keys into a static session map.\n * The QuickJS runtime is lazily started on the first `.eval()` call\n * and reconnected if a session with the same id already exists.\n * This makes it safe to store in LangGraph state across interrupts.\n *\n * File writes are buffered during execution and flushed via\n * `flushWrites(backend)` after eval completes.\n */\nexport class ReplSession {\n private static sessions = new Map<string, ReplSession>();\n\n readonly id: string;\n readonly pendingWrites: PendingWrite[] = [];\n\n private runtime: QuickJSAsyncRuntime | null = null;\n private context: QuickJSAsyncContext | null = null;\n private logs: string[] = [];\n private _options: ReplSessionOptions;\n\n private _backend: BackendProtocol | null = null;\n\n constructor(id: string, options: ReplSessionOptions = {}) {\n this.id = id;\n this._options = options;\n }\n\n get backend(): BackendProtocol | null {\n return this._backend;\n }\n\n set backend(b: BackendProtocol | null) {\n this._backend = b;\n }\n\n private async ensureStarted(): Promise<void> {\n if (this.runtime) return;\n\n const {\n memoryLimitBytes = DEFAULT_MEMORY_LIMIT,\n maxStackSizeBytes = DEFAULT_MAX_STACK_SIZE,\n backend,\n tools,\n } = this._options;\n\n const asyncModule = await getAsyncModule();\n const runtime: QuickJSAsyncRuntime = asyncModule.newRuntime();\n runtime.setMemoryLimit(memoryLimitBytes);\n runtime.setMaxStackSize(maxStackSizeBytes);\n\n const context: QuickJSAsyncContext = runtime.newContext();\n this.runtime = runtime;\n this.context = context;\n\n this.setupConsole();\n\n if (backend) {\n this._backend = backend;\n }\n this.injectVfs();\n if (tools && tools.length > 0) {\n this.injectTools(tools);\n }\n }\n\n /**\n * Get or create a session for the given id.\n *\n * Sessions are deduped by id — calling `getOrCreate` twice with the\n * same id returns the same instance. The QuickJS runtime is lazily\n * started on the first `.eval()` call.\n */\n static getOrCreate(\n id: string,\n options: ReplSessionOptions = {},\n ): ReplSession {\n const existing = ReplSession.sessions.get(id);\n if (existing) {\n if (options.backend) {\n existing._backend = options.backend;\n }\n return existing;\n }\n\n const session = new ReplSession(id, options);\n ReplSession.sessions.set(id, session);\n return session;\n }\n\n /**\n * Retrieve an existing session by id, or null if none exists.\n */\n static get(id: string): ReplSession | null {\n return ReplSession.sessions.get(id) ?? null;\n }\n\n /**\n * Evaluate code in this session.\n *\n * Lazily starts the QuickJS runtime on the first call. Code is\n * transformed via an AST pipeline that strips TypeScript syntax,\n * hoists top-level declarations to globalThis for cross-eval\n * persistence, auto-returns the last expression, and wraps in an\n * async IIFE.\n */\n async eval(code: string, timeoutMs: number): Promise<ReplResult> {\n await this.ensureStarted();\n const runtime = this.runtime!;\n const context = this.context!;\n\n this.logs.length = 0;\n\n if (timeoutMs >= 0) {\n runtime.setInterruptHandler(\n shouldInterruptAfterDeadline(Date.now() + timeoutMs),\n );\n } else {\n runtime.setInterruptHandler(() => false);\n }\n\n const transformed = transformForEval(code);\n const result = await context.evalCodeAsync(transformed);\n\n if (result.error) {\n const error = context.dump(result.error);\n result.error.dispose();\n return { ok: false, error, logs: [...this.logs] };\n }\n\n const promiseState = context.getPromiseState(result.value);\n\n if (promiseState.type === \"fulfilled\") {\n if (promiseState.notAPromise) {\n const value = context.dump(result.value);\n result.value.dispose();\n return { ok: true, value, logs: [...this.logs] };\n }\n const value = context.dump(promiseState.value);\n promiseState.value.dispose();\n result.value.dispose();\n return { ok: true, value, logs: [...this.logs] };\n }\n\n if (promiseState.type === \"rejected\") {\n const error = context.dump(promiseState.error);\n promiseState.error.dispose();\n result.value.dispose();\n return { ok: false, error, logs: [...this.logs] };\n }\n\n const noTimeout = timeoutMs < 0;\n const deadline = noTimeout ? Infinity : Date.now() + timeoutMs;\n while (noTimeout || Date.now() < deadline) {\n context.runtime.executePendingJobs();\n const state = context.getPromiseState(result.value);\n if (state.type === \"fulfilled\") {\n const value = context.dump(state.value);\n state.value.dispose();\n result.value.dispose();\n return { ok: true, value, logs: [...this.logs] };\n }\n if (state.type === \"rejected\") {\n const error = context.dump(state.error);\n state.error.dispose();\n result.value.dispose();\n return { ok: false, error, logs: [...this.logs] };\n }\n await new Promise((r) => setTimeout(r, 1));\n }\n\n result.value.dispose();\n return {\n ok: false,\n error: { message: \"Promise timed out — execution interrupted\" },\n logs: [...this.logs],\n };\n }\n\n async flushWrites(backend: BackendProtocol): Promise<void> {\n const writes = this.pendingWrites.splice(0);\n for (const { path, content } of writes) {\n await backend.write(path, content);\n }\n }\n\n dispose(): void {\n try {\n this.context?.dispose();\n } catch {\n /* may already be disposed */\n }\n try {\n this.runtime?.dispose();\n } catch {\n /* may already be disposed */\n }\n this.runtime = null;\n this.context = null;\n ReplSession.sessions.delete(this.id);\n }\n\n toJSON(): { id: string } {\n return { id: this.id };\n }\n\n static fromJSON(data: { id: string }): ReplSession {\n return ReplSession.sessions.get(data.id) ?? new ReplSession(data.id);\n }\n\n /**\n * Clear the static session cache. Useful for testing.\n * @internal\n */\n static clearCache(): void {\n for (const session of ReplSession.sessions.values()) {\n session.dispose();\n }\n ReplSession.sessions.clear();\n }\n\n private setupConsole(): void {\n const context = this.context!;\n const logs = this.logs;\n const consoleHandle = context.newObject();\n for (const method of [\"log\", \"warn\", \"error\", \"info\", \"debug\"] as const) {\n const fnHandle = context.newFunction(\n method,\n (...args: QuickJSHandle[]) => {\n const nativeArgs = args.map((a: QuickJSHandle) => context.dump(a));\n const formatted = nativeArgs\n .map((a: unknown) =>\n typeof a === \"object\" && a !== null\n ? JSON.stringify(a)\n : String(a),\n )\n .join(\" \");\n logs.push(\n method === \"log\" || method === \"info\" || method === \"debug\"\n ? formatted\n : `[${method}] ${formatted}`,\n );\n },\n );\n context.setProp(consoleHandle, method, fnHandle);\n fnHandle.dispose();\n }\n context.setProp(context.global, \"console\", consoleHandle);\n consoleHandle.dispose();\n }\n\n private injectVfs(): void {\n const context = this.context!;\n const getBackend = () => this._backend;\n const { pendingWrites } = this;\n\n const readFileHandle = context.newFunction(\n \"readFile\",\n (pathHandle: QuickJSHandle) => {\n const backend = getBackend();\n if (!backend) {\n const promise = context.newPromise();\n const err = context.newError(\"Backend not available\");\n promise.reject(err);\n err.dispose();\n promise.settled.then(context.runtime.executePendingJobs);\n return promise.handle;\n }\n const path = context.getString(pathHandle);\n const promise = context.newPromise();\n (async () => {\n try {\n const fileData = await backend.readRaw(path);\n const val = context.newString(fileData.content.join(\"\\n\"));\n promise.resolve(val);\n val.dispose();\n } catch {\n const err = context.newError(\n `ENOENT: no such file or directory '${path}'.`,\n );\n promise.reject(err);\n err.dispose();\n }\n promise.settled.then(context.runtime.executePendingJobs);\n })();\n return promise.handle;\n },\n );\n context.setProp(context.global, \"readFile\", readFileHandle);\n readFileHandle.dispose();\n\n const writeFileHandle = context.newFunction(\n \"writeFile\",\n (pathHandle: QuickJSHandle, contentHandle: QuickJSHandle) => {\n const path = context.getString(pathHandle);\n const content = context.getString(contentHandle);\n const promise = context.newPromise();\n pendingWrites.push({ path, content });\n promise.resolve(context.undefined);\n promise.settled.then(context.runtime.executePendingJobs);\n return promise.handle;\n },\n );\n context.setProp(context.global, \"writeFile\", writeFileHandle);\n writeFileHandle.dispose();\n }\n\n private injectTools(tools: StructuredToolInterface[]): void {\n const context = this.context!;\n const toolsNs = context.newObject();\n\n for (const t of tools) {\n const camelName = toCamelCase(t.name);\n const fnHandle = context.newFunction(\n camelName,\n (inputHandle: QuickJSHandle) => {\n const input = context.dump(inputHandle);\n const promise = context.newPromise();\n (async () => {\n try {\n const rawInput =\n typeof input === \"object\" && input !== null ? input : {};\n const result = await t.invoke(rawInput);\n const val = context.newString(\n typeof result === \"string\" ? result : JSON.stringify(result),\n );\n promise.resolve(val);\n val.dispose();\n } catch (e: unknown) {\n const msg =\n e != null && typeof (e as Error).message === \"string\"\n ? (e as Error).message\n : String(e);\n const err = context.newError(`Tool '${t.name}' failed: ${msg}`);\n promise.reject(err);\n err.dispose();\n }\n promise.settled.then(context.runtime.executePendingJobs);\n })();\n return promise.handle;\n },\n );\n context.setProp(toolsNs, camelName, fnHandle);\n fnHandle.dispose();\n }\n\n context.setProp(context.global, \"tools\", toolsNs);\n toolsNs.dispose();\n }\n}\n","/**\n * QuickJS REPL middleware for deepagents.\n *\n * Provides a `js_eval` tool that runs JavaScript in a WASM-sandboxed QuickJS\n * interpreter. Supports:\n * - Persistent state across evaluations (true REPL)\n * - VFS integration via readFile/writeFile\n * - Programmatic tool calling (PTC)\n */\n\nimport {\n createMiddleware,\n tool,\n type AgentMiddleware as _AgentMiddleware,\n} from \"langchain\";\nimport { z } from \"zod/v4\";\nimport type { StructuredToolInterface } from \"@langchain/core/tools\";\nimport {\n StateBackend,\n type BackendProtocol,\n type BackendFactory,\n type StateAndStore,\n} from \"deepagents\";\n\nimport dedent from \"dedent\";\nimport type { QuickJSMiddlewareOptions } from \"./types.js\";\nimport {\n ReplSession,\n DEFAULT_EXECUTION_TIMEOUT,\n DEFAULT_MEMORY_LIMIT,\n DEFAULT_MAX_STACK_SIZE,\n DEFAULT_SESSION_ID,\n} from \"./session.js\";\nimport {\n formatReplResult,\n toCamelCase,\n toolToTypeSignature,\n safeToJsonSchema,\n} from \"./utils.js\";\n\n/**\n * These type-only imports are required for TypeScript's type inference to work\n * correctly with the langchain/langgraph middleware system. Without them, certain\n * generic type parameters fail to resolve properly, causing runtime issues with\n * tool schemas and message types.\n */\nimport type * as _zodTypes from \"@langchain/core/utils/types\";\nimport type * as _zodMeta from \"@langchain/langgraph/zod\";\nimport type * as _messages from \"@langchain/core/messages\";\nimport { getCurrentTaskInput } from \"@langchain/langgraph\";\n\n/**\n * Backend-provided tools excluded from PTC by default.\n * These are redundant inside the REPL since VFS helpers (readFile/writeFile)\n * already cover file I/O against the agent's in-memory working set.\n */\nexport const DEFAULT_PTC_EXCLUDED_TOOLS = [\n \"ls\",\n \"read_file\",\n \"write_file\",\n \"edit_file\",\n \"glob\",\n \"grep\",\n \"execute\",\n] as const;\n\nconst REPL_SYSTEM_PROMPT = dedent`\n ## TypeScript/JavaScript REPL (\\`js_eval\\`)\n\n You have access to a sandboxed TypeScript/JavaScript REPL running in an isolated interpreter.\n TypeScript syntax (type annotations, interfaces, generics, \\`as\\` casts) is supported and stripped at evaluation time.\n Variables, functions, and closures persist across calls within the same session.\n\n ### Hard rules\n\n - **No network, no filesystem** — only the helpers below. Do not attempt \\`fetch\\`, \\`require\\`, or \\`import\\`.\n - **Cite your sources** — when reporting values from files, include the path and key/index so the user can verify.\n - **Use console.log()** for output — it is captured and returned. \\`console.warn()\\` and \\`console.error()\\` are also available.\n - **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.\n\n ### First-time usage\n\n \\`\\`\\`typescript\n // Read a file from the agent's virtual filesystem\n const raw: string = await readFile(\"/data.json\");\n const data = JSON.parse(raw) as { n: number };\n console.log(data);\n\n // Write results back\n await writeFile(\"/output.txt\", JSON.stringify({ result: data.n }));\n \\`\\`\\`\n\n ### API Reference — built-in globals\n\n \\`\\`\\`typescript\n /**\n * Read a file from the agent's virtual filesystem. Throws if the file does not exist.\n */\n async readFile(path: string): Promise<string>\n\n /**\n * Write a file to the agent's virtual filesystem.\n */\n async writeFile(path: string, content: string): Promise<void>\n \\`\\`\\`\n\n ### Limitations\n\n - ES2023+ syntax with TypeScript support. No Node.js APIs, no \\`require\\`, no \\`import\\`.\n - Output is truncated beyond a fixed character limit — be selective about what you log.\n - Execution timeout per call (default 30 s).\n`;\n\n/**\n * Generate the PTC API Reference section for the system prompt.\n */\nexport async function generatePtcPrompt(\n tools: StructuredToolInterface[],\n): Promise<string> {\n if (tools.length === 0) return \"\";\n\n const signatures = await Promise.all(\n tools.map((t) => {\n const jsonSchema = t.schema ? safeToJsonSchema(t.schema) : undefined;\n return toolToTypeSignature(\n toCamelCase(t.name),\n t.description,\n jsonSchema,\n );\n }),\n );\n\n return dedent`\n\n ### API Reference — \\`tools\\` namespace\n\n The following agent tools are callable as async functions inside the REPL.\n Each takes a single object argument and returns a Promise that resolves to a string.\n Use \\`await\\` to call them. Promise APIs like \\`Promise.all\\` are also available.\n\n **Example usage:**\n \\`\\`\\`javascript\n // Call a tool\n const result = await tools.searchWeb({ query: \"QuickJS tutorial\" });\n console.log(result);\n\n // Concurrent calls\n const [a, b] = await Promise.all([\n tools.fetchData({ url: \"https://api.example.com/a\" }),\n tools.fetchData({ url: \"https://api.example.com/b\" }),\n ]);\n \\`\\`\\`\n\n **Available functions:**\n \\`\\`\\`typescript\n ${signatures.join(\"\\n\\n\")}\n \\`\\`\\`\n `;\n}\n\n/**\n * Resolve backend from factory or instance.\n */\nfunction getBackend(\n backend: BackendProtocol | BackendFactory,\n stateAndStore: StateAndStore,\n): BackendProtocol {\n if (typeof backend === \"function\") {\n return backend(stateAndStore);\n }\n return backend;\n}\n\n/**\n * Create the QuickJS REPL middleware.\n */\nexport function createQuickJSMiddleware(\n options: QuickJSMiddlewareOptions = {},\n) {\n const {\n backend = (stateAndStore: StateAndStore) => new StateBackend(stateAndStore),\n ptc = false,\n memoryLimitBytes = DEFAULT_MEMORY_LIMIT,\n maxStackSizeBytes = DEFAULT_MAX_STACK_SIZE,\n executionTimeoutMs = DEFAULT_EXECUTION_TIMEOUT,\n systemPrompt: customSystemPrompt = null,\n } = options;\n\n const usePtc = ptc !== false;\n const baseSystemPrompt = customSystemPrompt || REPL_SYSTEM_PROMPT;\n\n let cachedPtcPrompt: string | null = null;\n\n let ptcTools: StructuredToolInterface[] = [];\n\n function filterToolsForPtc(\n allTools: StructuredToolInterface[],\n ): StructuredToolInterface[] {\n if (ptc === false) return [];\n\n const candidates = allTools.filter((t) => t.name !== \"js_eval\");\n\n if (ptc === true) {\n const excluded = new Set<string>(DEFAULT_PTC_EXCLUDED_TOOLS);\n return candidates.filter((t) => !excluded.has(t.name));\n }\n\n if (Array.isArray(ptc)) {\n const included = new Set(ptc);\n return candidates.filter((t) => included.has(t.name));\n }\n\n if (\"include\" in ptc) {\n const included = new Set(ptc.include);\n return candidates.filter((t) => included.has(t.name));\n }\n\n if (\"exclude\" in ptc) {\n const excluded = new Set([...DEFAULT_PTC_EXCLUDED_TOOLS, ...ptc.exclude]);\n return candidates.filter((t) => !excluded.has(t.name));\n }\n\n return [];\n }\n\n const jsEvalTool = tool(\n async (input, config) => {\n const threadId = config.configurable?.thread_id || DEFAULT_SESSION_ID;\n\n const stateAndStore: StateAndStore = {\n state: getCurrentTaskInput(config) || {},\n store: config.configurable?.__pregel_store,\n };\n const resolvedBackend = getBackend(backend, stateAndStore);\n\n const session = ReplSession.getOrCreate(threadId, {\n memoryLimitBytes,\n maxStackSizeBytes,\n backend: resolvedBackend,\n tools: ptcTools,\n });\n\n const result = await session.eval(input.code, executionTimeoutMs);\n await session.flushWrites(resolvedBackend);\n\n return formatReplResult(result);\n },\n {\n name: \"js_eval\",\n description: dedent`\n Evaluate TypeScript/JavaScript code in a sandboxed REPL. State persists across calls.\n Use readFile(path) and writeFile(path, content) for file access.\n Use console.log() for output. Returns the result of the last expression.\n `,\n schema: z.object({\n code: z\n .string()\n .describe(\n \"TypeScript/JavaScript code to evaluate in the sandboxed REPL\",\n ),\n }),\n },\n );\n\n return createMiddleware({\n name: \"QuickJSMiddleware\",\n tools: [jsEvalTool],\n wrapModelCall: async (request, handler) => {\n const agentTools = (request.tools || []) as StructuredToolInterface[];\n ptcTools = usePtc ? filterToolsForPtc(agentTools) : [];\n\n if (ptcTools.length > 0 && !cachedPtcPrompt) {\n cachedPtcPrompt = await generatePtcPrompt(ptcTools);\n }\n\n const systemMessage = request.systemMessage\n .concat(baseSystemPrompt)\n .concat(cachedPtcPrompt || \"\");\n return handler({ ...request, systemMessage });\n },\n });\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAQA,SAAgB,YAAY,MAAsB;AAChD,QAAO,KAAK,QAAQ,iBAAiB,GAAG,MAAM,EAAE,aAAa,CAAC;;;;;AAwBhE,SAAgB,iBAAiB,QAA4B;CAC3D,MAAM,QAAkB,EAAE;AAE1B,KAAI,OAAO,KAAK,SAAS,EACvB,OAAM,KAAK,OAAO,KAAK,KAAK,KAAK,CAAC;AAGpC,KAAI,OAAO,IACT;MAAI,OAAO,UAAU,QAAW;GAC9B,MAAM,YACJ,OAAO,OAAO,UAAU,WACpB,OAAO,QACP,KAAK,UAAU,OAAO,OAAO,MAAM,EAAE;AAC3C,SAAM,KAAK,KAAK,YAAY;;YAErB,OAAO,OAAO;EACvB,MAAM,UAAU,OAAO,MAAM,QAAQ;EACrC,MAAM,SAAS,OAAO,MAAM,WAAW;AACvC,QAAM,KAAK,GAAG,QAAQ,IAAI,SAAS;AACnC,MAAI,OAAO,MAAM,MACf,OAAM,KAAK,OAAO,MAAM,MAAM;;AAIlC,QAAO,MAAM,KAAK,KAAK,IAAI;;AAG7B,SAAgB,iBACd,QACqC;AACrC,KAAI;AACF,SAAO,aAAa,OAA6C;SAI3D;AACN;;;AAIJ,eAAe,kBACb,YACA,eACiB;AAMjB,SALiB,MAAM,QACrB;EAAE,GAAG;EAAY,sBAAsB;EAAO,EAC9C,eACA;EAAE,eAAe;EAAI,sBAAsB;EAAO,CACnD,EACe,QAAQ,YAAY,GAAG,CAAC,SAAS;;AAGn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AkB,WAAW,GAAG,EAAE;AAEtD,OAAI,SAAS,SAAS,KAAK,CAAC,gBAC1B,mBAAkB,MAAM,kBAAkB,SAAS;GAGrD,MAAM,gBAAgB,QAAQ,cAC3B,OAAO,iBAAiB,CACxB,OAAO,mBAAmB,GAAG;AAChC,UAAO,QAAQ;IAAE,GAAG;IAAS;IAAe,CAAC;;EAEhD,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/utils.ts","../src/transform.ts","../src/session.ts","../src/middleware.ts"],"sourcesContent":["import { compile } from \"json-schema-to-typescript\";\nimport { toJsonSchema } from \"@langchain/core/utils/json_schema\";\nimport dedent from \"dedent\";\nimport type { ReplResult } from \"./types.js\";\n\n/**\n * Convert a snake_case or kebab-case string to camelCase.\n */\nexport function toCamelCase(name: string): string {\n return name.replace(/[-_]([a-z])/g, (_, c) => c.toUpperCase());\n}\n\n/**\n * Recursively collect all string values from an object, array, or primitive.\n */\nexport function collectStrings(obj: unknown): string[] {\n const result: string[] = [];\n function walk(val: unknown) {\n if (typeof val === \"string\") {\n result.push(val);\n } else if (Array.isArray(val)) {\n for (const item of val) walk(item);\n } else if (typeof val === \"object\" && val !== null) {\n for (const v of Object.values(val)) walk(v);\n }\n }\n walk(obj);\n return result;\n}\n\n/**\n * Format the result of a REPL evaluation for the agent.\n */\nexport function formatReplResult(result: ReplResult): string {\n const parts: string[] = [];\n\n if (result.logs.length > 0) {\n parts.push(result.logs.join(\"\\n\"));\n }\n\n if (result.ok) {\n if (result.value !== undefined) {\n const formatted =\n typeof result.value === \"string\"\n ? result.value\n : JSON.stringify(result.value, null, 2);\n parts.push(`→ ${formatted}`);\n }\n } else if (result.error) {\n const errName = result.error.name || \"Error\";\n const errMsg = result.error.message || \"Unknown error\";\n parts.push(`${errName}: ${errMsg}`);\n if (result.error.stack) {\n parts.push(result.error.stack);\n }\n }\n\n return parts.join(\"\\n\") || \"(no output)\";\n}\n\nexport function safeToJsonSchema(\n schema: unknown,\n): Record<string, unknown> | undefined {\n try {\n return toJsonSchema(schema as Parameters<typeof toJsonSchema>[0]) as Record<\n string,\n unknown\n >;\n } catch {\n return undefined;\n }\n}\n\nasync function schemaToInterface(\n jsonSchema: Record<string, unknown>,\n interfaceName: string,\n): Promise<string> {\n const compiled = await compile(\n { ...jsonSchema, additionalProperties: false },\n interfaceName,\n { bannerComment: \"\", additionalProperties: false },\n );\n return compiled.replace(/^export /, \"\").trimEnd();\n}\n\nexport function capitalize(s: string): string {\n return s.charAt(0).toUpperCase() + s.slice(1);\n}\n\nexport async function toolToTypeSignature(\n name: string,\n description: string,\n jsonSchema: Record<string, unknown> | undefined,\n): Promise<string> {\n const inputType = `${capitalize(name)}Input`;\n\n if (!jsonSchema || !jsonSchema.properties) {\n return dedent`\n /**\n * ${description}\n */\n async tools.${name}(input: Record<string, unknown>): Promise<string>\n `;\n }\n\n const iface = await schemaToInterface(jsonSchema, inputType);\n return dedent`\n ${iface}\n\n /**\n * ${description}\n */\n async tools.${name}(input: ${inputType}): Promise<string>\n `;\n}\n","/**\n * AST-based code transform pipeline for the REPL.\n *\n * Transforms TypeScript/JavaScript code into plain JS that can be\n * evaluated inside QuickJS with proper state persistence:\n *\n * 1. Parse with acorn + acorn-typescript (handles TS syntax)\n * 2. Strip TypeScript-only nodes (type annotations, interfaces, etc.)\n * 3. Hoist top-level declarations to globalThis for cross-eval persistence\n * 4. Auto-return the last expression\n * 5. Wrap in async IIFE so top-level await works\n */\n\nimport { Parser } from \"acorn\";\nimport { tsPlugin } from \"@sveltejs/acorn-typescript\";\nimport { walk } from \"estree-walker\";\nimport MagicString from \"magic-string\";\nimport type {\n Node,\n Identifier,\n VariableDeclaration as EstreeVariableDeclaration,\n VariableDeclarator as EstreeVariableDeclarator,\n} from \"estree\";\n\nconst TSParser = Parser.extend(tsPlugin());\n\ntype AcornNode = Node & { start: number; end: number };\ntype AcornExpressionStatement = AcornNode & {\n type: \"ExpressionStatement\";\n expression: AcornNode;\n};\ntype AcornVariableDeclaration = EstreeVariableDeclaration & {\n start: number;\n end: number;\n declarations: AcornVariableDeclarator[];\n};\ntype AcornVariableDeclarator = EstreeVariableDeclarator & {\n start: number;\n end: number;\n id: AcornNode;\n init: AcornNode | null;\n};\n\n/**\n * Transform code for REPL evaluation.\n *\n * - Strips TypeScript syntax\n * - Hoists top-level variable declarations to globalThis\n * - Auto-returns the last expression\n * - Wraps in async IIFE for top-level await support\n */\nexport function transformForEval(code: string): string {\n let ast: AcornNode;\n try {\n ast = TSParser.parse(code, {\n ecmaVersion: \"latest\" as any,\n sourceType: \"module\",\n locations: true,\n }) as unknown as AcornNode;\n } catch {\n // If parsing fails, return the code as-is and let QuickJS report the error\n return `(async () => {\\n${code}\\n})()`;\n }\n\n const s = new MagicString(code);\n const program = ast as unknown as { body: AcornNode[] };\n const topLevelNodes = program.body;\n for (let i = 0; i < topLevelNodes.length; i++) {\n const node = topLevelNodes[i];\n\n // Remove TypeScript-only top-level declarations\n if (isTSOnlyNode(node)) {\n s.remove(node.start, node.end);\n continue;\n }\n\n // Remove import/export declarations (not supported in QuickJS eval)\n if (\n node.type === \"ImportDeclaration\" ||\n node.type === \"ExportNamedDeclaration\" ||\n node.type === \"ExportDefaultDeclaration\" ||\n node.type === \"ExportAllDeclaration\"\n ) {\n s.remove(node.start, node.end);\n continue;\n }\n\n // Hoist top-level variable declarations\n if (node.type === \"VariableDeclaration\") {\n hoistDeclaration(s, node as unknown as AcornVariableDeclaration);\n continue;\n }\n\n // Hoist function/class declarations to globalThis for cross-eval persistence\n if (\n node.type === \"FunctionDeclaration\" ||\n node.type === \"ClassDeclaration\"\n ) {\n stripTypeAnnotations(s, node);\n const name = (node as any).id?.name;\n if (name) {\n s.appendRight(node.end, `\\nglobalThis.${name} = ${name};`);\n }\n continue;\n }\n }\n\n // Strip type annotations from within expressions/statements\n for (const node of topLevelNodes) {\n if (isTSOnlyNode(node)) continue;\n if (\n node.type === \"ImportDeclaration\" ||\n node.type === \"ExportNamedDeclaration\" ||\n node.type === \"ExportDefaultDeclaration\" ||\n node.type === \"ExportAllDeclaration\"\n )\n continue;\n if (node.type !== \"VariableDeclaration\") {\n walk(node as any, {\n enter(n: any) {\n stripTypeAnnotationFromNode(s, n);\n },\n });\n }\n }\n\n // Auto-return the last expression. We insert `return (` before the\n // ExpressionStatement (to preserve any grouping parens like `({...})`),\n // but close `)` after the inner expression — not after the statement —\n // so any trailing semicolon stays outside: `return (expr);` not `return (expr;)`.\n const lastNode = findLastNonEmptyNode(topLevelNodes, s);\n if (lastNode && isExpression(lastNode)) {\n const { expression } = lastNode as AcornExpressionStatement;\n s.prependLeft(lastNode.start, \"return (\");\n s.appendRight(expression.end, \")\");\n }\n\n // Wrap in async IIFE\n s.prepend(\"(async () => {\\n\");\n s.append(\"\\n})()\");\n\n return s.toString();\n}\n\nfunction isTSOnlyNode(node: AcornNode): boolean {\n const t = node.type as string;\n return (\n t === \"TSTypeAliasDeclaration\" ||\n t === \"TSInterfaceDeclaration\" ||\n t === \"TSEnumDeclaration\" ||\n t === \"TSModuleDeclaration\" ||\n t === \"TSDeclareFunction\" ||\n t.startsWith(\"TS\")\n );\n}\n\n/**\n * Rewrite a top-level VariableDeclaration to globalThis assignments.\n *\n * `const x = 1, y = 2` → `globalThis.x = 1; globalThis.y = 2`\n *\n */\nfunction hoistDeclaration(\n s: MagicString,\n decl: AcornVariableDeclaration,\n): void {\n const parts: string[] = [];\n\n for (const d of decl.declarations) {\n const id = d.id as AcornNode;\n if (id.type === \"Identifier\") {\n const initCode = d.init ? extractCleanInit(s, d) : \"undefined\";\n parts.push(\n `globalThis.${(id as unknown as Identifier).name} = ${initCode}`,\n );\n } else if (id.type === \"ObjectPattern\" || id.type === \"ArrayPattern\") {\n const bindings = extractBindingNames(d.id as any);\n const initCode = d.init ? extractCleanInit(s, d) : \"undefined\";\n const patternCode = extractCleanSource(s, d.id as AcornNode);\n parts.push(`var ${patternCode} = ${initCode}`);\n for (const name of bindings) {\n parts.push(`globalThis.${name} = ${name}`);\n }\n }\n }\n\n s.overwrite(decl.start, decl.end, parts.join(\"; \") + \";\");\n}\n\n/**\n * Extract the initializer code, stripping TypeScript annotations from\n * within the expression (e.g. `as Type`, generics, parameter types in\n * arrow functions).\n */\nfunction extractCleanInit(s: MagicString, d: AcornVariableDeclarator): string {\n if (!d.init) return \"undefined\";\n return extractCleanSource(s, d.init as AcornNode);\n}\n\nfunction extractBindingNames(pattern: any): string[] {\n const names: string[] = [];\n if (pattern.type === \"Identifier\") {\n if (pattern.name) names.push(pattern.name);\n } else if (pattern.type === \"ObjectPattern\") {\n for (const prop of pattern.properties || []) {\n if (prop.type === \"RestElement\") {\n names.push(...extractBindingNames(prop.argument));\n } else {\n names.push(...extractBindingNames(prop.value));\n }\n }\n } else if (pattern.type === \"ArrayPattern\") {\n for (const el of pattern.elements || []) {\n if (el) names.push(...extractBindingNames(el));\n }\n } else if (pattern.type === \"RestElement\") {\n names.push(...extractBindingNames(pattern.argument));\n } else if (pattern.type === \"AssignmentPattern\") {\n names.push(...extractBindingNames(pattern.left));\n }\n return names;\n}\n\nfunction stripTypeAnnotations(s: MagicString, node: AcornNode): void {\n walk(node as any, {\n enter(n: any) {\n stripTypeAnnotationFromNode(s, n);\n },\n });\n}\n\nfunction stripTypeAnnotationFromNode(s: MagicString, n: any, offset = 0): void {\n // Type annotations on parameters, variables, return types\n if (n.typeAnnotation && n.typeAnnotation.start != null) {\n s.remove(n.typeAnnotation.start - offset, n.typeAnnotation.end - offset);\n }\n // Return type on functions\n if (n.returnType && n.returnType.start != null) {\n s.remove(n.returnType.start - offset, n.returnType.end - offset);\n }\n // Type parameters (generics)\n if (n.typeParameters && n.typeParameters.start != null) {\n s.remove(n.typeParameters.start - offset, n.typeParameters.end - offset);\n }\n // Type arguments on calls\n if (n.typeArguments && n.typeArguments.start != null) {\n s.remove(n.typeArguments.start - offset, n.typeArguments.end - offset);\n }\n // `as` expressions: keep the expression, remove `as Type`\n if (n.type === \"TSAsExpression\" && n.expression) {\n s.remove(n.expression.end - offset, n.end - offset);\n }\n // Non-null assertion: `x!` → `x`\n if (n.type === \"TSNonNullExpression\" && n.expression) {\n s.remove(n.expression.end - offset, n.end - offset);\n }\n // Satisfies expression: `x satisfies Type` → `x`\n if (n.type === \"TSSatisfiesExpression\" && n.expression) {\n s.remove(n.expression.end - offset, n.end - offset);\n }\n}\n\n/**\n * Extract a clean JS source string from an AST node, stripping all\n * TypeScript annotations. Works on a copy so the main MagicString is\n * not mutated.\n */\nfunction extractCleanSource(s: MagicString, node: AcornNode): string {\n const offset = node.start;\n const source = new MagicString(s.slice(node.start, node.end));\n walk(node as any, {\n enter(n: any) {\n stripTypeAnnotationFromNode(source, n, offset);\n },\n });\n return source.toString();\n}\n\nfunction findLastNonEmptyNode(\n nodes: AcornNode[],\n s: MagicString,\n): AcornNode | null {\n for (let i = nodes.length - 1; i >= 0; i--) {\n const node = nodes[i];\n // Skip nodes that were fully removed\n const slice = s.slice(node.start, node.end).trim();\n if (slice === \"\" || slice === \";\") continue;\n return node;\n }\n return null;\n}\n\nfunction isExpression(node: AcornNode): boolean {\n return node.type === \"ExpressionStatement\";\n}\n","/**\n * Core REPL engine built on quickjs-emscripten (asyncify variant).\n *\n * Host async functions (backend I/O, PTC tools) are exposed as\n * promise-returning functions inside the QuickJS guest. Guest code\n * uses `await` to consume them, enabling real concurrency via\n * `Promise.all`, `Promise.race`, etc.\n *\n * We still use the asyncify WASM variant because `evalCodeAsync` is\n * required to drive promise resolution from the host side.\n *\n * ## Architecture\n *\n * `ReplSession` is a serializable handle that can live in LangGraph state.\n * It holds an `id` that keys into a static session map. The heavy QuickJS\n * runtime is lazily started on the first `.eval()` call, making the session\n * safe across graph interrupts and checkpointing.\n *\n * File writes inside the REPL are buffered (`pendingWrites`) and only\n * flushed to the backend after a script finishes executing. Call\n * `session.flushWrites(backend)` after eval to persist them.\n */\n\nimport { shouldInterruptAfterDeadline } from \"quickjs-emscripten\";\nimport type { QuickJSHandle } from \"quickjs-emscripten\";\nimport { newQuickJSAsyncWASMModuleFromVariant } from \"quickjs-emscripten-core\";\nimport type {\n QuickJSAsyncContext,\n QuickJSAsyncRuntime,\n} from \"quickjs-emscripten-core\";\nimport type { BackendProtocol } from \"deepagents\";\nimport type { StructuredToolInterface } from \"@langchain/core/tools\";\n\nimport type { ReplSessionOptions, ReplResult } from \"./types.js\";\nimport { toCamelCase } from \"./utils.js\";\nimport { transformForEval } from \"./transform.js\";\n\nexport const DEFAULT_MEMORY_LIMIT = 50 * 1024 * 1024;\nexport const DEFAULT_MAX_STACK_SIZE = 320 * 1024;\nexport const DEFAULT_EXECUTION_TIMEOUT = 30_000;\nexport const DEFAULT_SESSION_ID = \"__default__\";\n\nlet asyncModulePromise: Promise<any> | undefined;\n\nasync function getAsyncModule() {\n if (!asyncModulePromise) {\n asyncModulePromise = (async () => {\n const variant =\n await import(\"@jitl/quickjs-ng-wasmfile-release-asyncify\");\n return newQuickJSAsyncWASMModuleFromVariant(\n (variant.default ?? variant) as any,\n );\n })();\n }\n return asyncModulePromise;\n}\n\nexport interface PendingWrite {\n path: string;\n content: string;\n}\n\n/**\n * Sandboxed JavaScript REPL session backed by QuickJS WASM.\n *\n * Serializable — holds an `id` that keys into a static session map.\n * The QuickJS runtime is lazily started on the first `.eval()` call\n * and reconnected if a session with the same id already exists.\n * This makes it safe to store in LangGraph state across interrupts.\n *\n * File writes are buffered during execution and flushed via\n * `flushWrites(backend)` after eval completes.\n */\nexport class ReplSession {\n private static sessions = new Map<string, ReplSession>();\n\n readonly id: string;\n readonly pendingWrites: PendingWrite[] = [];\n\n private runtime: QuickJSAsyncRuntime | null = null;\n private context: QuickJSAsyncContext | null = null;\n private logs: string[] = [];\n private _options: ReplSessionOptions;\n\n private _backend: BackendProtocol | null = null;\n\n constructor(id: string, options: ReplSessionOptions = {}) {\n this.id = id;\n this._options = options;\n }\n\n get backend(): BackendProtocol | null {\n return this._backend;\n }\n\n set backend(b: BackendProtocol | null) {\n this._backend = b;\n }\n\n private async ensureStarted(): Promise<void> {\n if (this.runtime) return;\n\n const {\n memoryLimitBytes = DEFAULT_MEMORY_LIMIT,\n maxStackSizeBytes = DEFAULT_MAX_STACK_SIZE,\n backend,\n tools,\n } = this._options;\n\n const asyncModule = await getAsyncModule();\n const runtime: QuickJSAsyncRuntime = asyncModule.newRuntime();\n runtime.setMemoryLimit(memoryLimitBytes);\n runtime.setMaxStackSize(maxStackSizeBytes);\n\n const context: QuickJSAsyncContext = runtime.newContext();\n this.runtime = runtime;\n this.context = context;\n\n this.setupConsole();\n\n if (backend) {\n this._backend = backend;\n }\n this.injectVfs();\n if (tools && tools.length > 0) {\n this.injectTools(tools);\n }\n }\n\n /**\n * Get or create a session for the given id.\n *\n * Sessions are deduped by id — calling `getOrCreate` twice with the\n * same id returns the same instance. The QuickJS runtime is lazily\n * started on the first `.eval()` call.\n */\n static getOrCreate(\n id: string,\n options: ReplSessionOptions = {},\n ): ReplSession {\n const existing = ReplSession.sessions.get(id);\n if (existing) {\n if (options.backend) {\n existing._backend = options.backend;\n }\n return existing;\n }\n\n const session = new ReplSession(id, options);\n ReplSession.sessions.set(id, session);\n return session;\n }\n\n /**\n * Retrieve an existing session by id, or null if none exists.\n */\n static get(id: string): ReplSession | null {\n return ReplSession.sessions.get(id) ?? null;\n }\n\n /**\n * Evaluate code in this session.\n *\n * Lazily starts the QuickJS runtime on the first call. Code is\n * transformed via an AST pipeline that strips TypeScript syntax,\n * hoists top-level declarations to globalThis for cross-eval\n * persistence, auto-returns the last expression, and wraps in an\n * async IIFE.\n */\n async eval(code: string, timeoutMs: number): Promise<ReplResult> {\n await this.ensureStarted();\n const runtime = this.runtime!;\n const context = this.context!;\n\n this.logs.length = 0;\n\n if (timeoutMs >= 0) {\n runtime.setInterruptHandler(\n shouldInterruptAfterDeadline(Date.now() + timeoutMs),\n );\n } else {\n runtime.setInterruptHandler(() => false);\n }\n\n const transformed = transformForEval(code);\n const result = await context.evalCodeAsync(transformed);\n\n if (result.error) {\n const error = context.dump(result.error);\n result.error.dispose();\n return { ok: false, error, logs: [...this.logs] };\n }\n\n const promiseState = context.getPromiseState(result.value);\n\n if (promiseState.type === \"fulfilled\") {\n if (promiseState.notAPromise) {\n const value = context.dump(result.value);\n result.value.dispose();\n return { ok: true, value, logs: [...this.logs] };\n }\n const value = context.dump(promiseState.value);\n promiseState.value.dispose();\n result.value.dispose();\n return { ok: true, value, logs: [...this.logs] };\n }\n\n if (promiseState.type === \"rejected\") {\n const error = context.dump(promiseState.error);\n promiseState.error.dispose();\n result.value.dispose();\n return { ok: false, error, logs: [...this.logs] };\n }\n\n const noTimeout = timeoutMs < 0;\n const deadline = noTimeout ? Infinity : Date.now() + timeoutMs;\n while (noTimeout || Date.now() < deadline) {\n context.runtime.executePendingJobs();\n const state = context.getPromiseState(result.value);\n if (state.type === \"fulfilled\") {\n const value = context.dump(state.value);\n state.value.dispose();\n result.value.dispose();\n return { ok: true, value, logs: [...this.logs] };\n }\n if (state.type === \"rejected\") {\n const error = context.dump(state.error);\n state.error.dispose();\n result.value.dispose();\n return { ok: false, error, logs: [...this.logs] };\n }\n await new Promise((r) => setTimeout(r, 1));\n }\n\n result.value.dispose();\n return {\n ok: false,\n error: { message: \"Promise timed out — execution interrupted\" },\n logs: [...this.logs],\n };\n }\n\n async flushWrites(backend: BackendProtocol): Promise<void> {\n const writes = this.pendingWrites.splice(0);\n for (const { path, content } of writes) {\n await backend.write(path, content);\n }\n }\n\n dispose(): void {\n try {\n this.context?.dispose();\n } catch {\n /* may already be disposed */\n }\n try {\n this.runtime?.dispose();\n } catch {\n /* may already be disposed */\n }\n this.runtime = null;\n this.context = null;\n ReplSession.sessions.delete(this.id);\n }\n\n toJSON(): { id: string } {\n return { id: this.id };\n }\n\n static fromJSON(data: { id: string }): ReplSession {\n return ReplSession.sessions.get(data.id) ?? new ReplSession(data.id);\n }\n\n /**\n * Clear the static session cache. Useful for testing.\n * @internal\n */\n static clearCache(): void {\n for (const session of ReplSession.sessions.values()) {\n session.dispose();\n }\n ReplSession.sessions.clear();\n }\n\n private setupConsole(): void {\n const context = this.context!;\n const logs = this.logs;\n const consoleHandle = context.newObject();\n for (const method of [\"log\", \"warn\", \"error\", \"info\", \"debug\"] as const) {\n const fnHandle = context.newFunction(\n method,\n (...args: QuickJSHandle[]) => {\n const nativeArgs = args.map((a: QuickJSHandle) => context.dump(a));\n const formatted = nativeArgs\n .map((a: unknown) =>\n typeof a === \"object\" && a !== null\n ? JSON.stringify(a)\n : String(a),\n )\n .join(\" \");\n logs.push(\n method === \"log\" || method === \"info\" || method === \"debug\"\n ? formatted\n : `[${method}] ${formatted}`,\n );\n },\n );\n context.setProp(consoleHandle, method, fnHandle);\n fnHandle.dispose();\n }\n context.setProp(context.global, \"console\", consoleHandle);\n consoleHandle.dispose();\n }\n\n private injectVfs(): void {\n const context = this.context!;\n const getBackend = () => this._backend;\n const { pendingWrites } = this;\n\n const readFileHandle = context.newFunction(\n \"readFile\",\n (pathHandle: QuickJSHandle) => {\n const backend = getBackend();\n if (!backend) {\n const promise = context.newPromise();\n const err = context.newError(\"Backend not available\");\n promise.reject(err);\n err.dispose();\n promise.settled.then(context.runtime.executePendingJobs);\n return promise.handle;\n }\n const path = context.getString(pathHandle);\n const promise = context.newPromise();\n (async () => {\n try {\n const fileData = await backend.readRaw(path);\n const val = context.newString(fileData.content.join(\"\\n\"));\n promise.resolve(val);\n val.dispose();\n } catch {\n const err = context.newError(\n `ENOENT: no such file or directory '${path}'.`,\n );\n promise.reject(err);\n err.dispose();\n }\n promise.settled.then(context.runtime.executePendingJobs);\n })();\n return promise.handle;\n },\n );\n context.setProp(context.global, \"readFile\", readFileHandle);\n readFileHandle.dispose();\n\n const writeFileHandle = context.newFunction(\n \"writeFile\",\n (pathHandle: QuickJSHandle, contentHandle: QuickJSHandle) => {\n const path = context.getString(pathHandle);\n const content = context.getString(contentHandle);\n const promise = context.newPromise();\n pendingWrites.push({ path, content });\n promise.resolve(context.undefined);\n promise.settled.then(context.runtime.executePendingJobs);\n return promise.handle;\n },\n );\n context.setProp(context.global, \"writeFile\", writeFileHandle);\n writeFileHandle.dispose();\n }\n\n private injectTools(tools: StructuredToolInterface[]): void {\n const context = this.context!;\n const toolsNs = context.newObject();\n\n for (const t of tools) {\n const camelName = toCamelCase(t.name);\n const fnHandle = context.newFunction(\n camelName,\n (inputHandle: QuickJSHandle) => {\n const input = context.dump(inputHandle);\n const promise = context.newPromise();\n (async () => {\n try {\n const rawInput =\n typeof input === \"object\" && input !== null ? input : {};\n const result = await t.invoke(rawInput);\n const val = context.newString(\n typeof result === \"string\" ? result : JSON.stringify(result),\n );\n promise.resolve(val);\n val.dispose();\n } catch (e: unknown) {\n const msg =\n e != null && typeof (e as Error).message === \"string\"\n ? (e as Error).message\n : String(e);\n const err = context.newError(`Tool '${t.name}' failed: ${msg}`);\n promise.reject(err);\n err.dispose();\n }\n promise.settled.then(context.runtime.executePendingJobs);\n })();\n return promise.handle;\n },\n );\n context.setProp(toolsNs, camelName, fnHandle);\n fnHandle.dispose();\n }\n\n context.setProp(context.global, \"tools\", toolsNs);\n toolsNs.dispose();\n }\n}\n","/**\n * QuickJS REPL middleware for deepagents.\n *\n * Provides a `js_eval` tool that runs JavaScript in a WASM-sandboxed QuickJS\n * interpreter. Supports:\n * - Persistent state across evaluations (true REPL)\n * - VFS integration via readFile/writeFile\n * - Programmatic tool calling (PTC)\n */\n\nimport {\n createMiddleware,\n tool,\n type AgentMiddleware as _AgentMiddleware,\n} from \"langchain\";\nimport { z } from \"zod/v4\";\nimport type { StructuredToolInterface } from \"@langchain/core/tools\";\nimport {\n StateBackend,\n type BackendProtocol,\n type BackendFactory,\n type StateAndStore,\n} from \"deepagents\";\n\nimport dedent from \"dedent\";\nimport type { QuickJSMiddlewareOptions } from \"./types.js\";\nimport {\n ReplSession,\n DEFAULT_EXECUTION_TIMEOUT,\n DEFAULT_MEMORY_LIMIT,\n DEFAULT_MAX_STACK_SIZE,\n DEFAULT_SESSION_ID,\n} from \"./session.js\";\nimport {\n formatReplResult,\n toCamelCase,\n toolToTypeSignature,\n safeToJsonSchema,\n} from \"./utils.js\";\n\n/**\n * These type-only imports are required for TypeScript's type inference to work\n * correctly with the langchain/langgraph middleware system. Without them, certain\n * generic type parameters fail to resolve properly, causing runtime issues with\n * tool schemas and message types.\n */\nimport type * as _zodTypes from \"@langchain/core/utils/types\";\nimport type * as _zodMeta from \"@langchain/langgraph/zod\";\nimport type * as _messages from \"@langchain/core/messages\";\nimport {\n getCurrentTaskInput,\n LangGraphRunnableConfig,\n} from \"@langchain/langgraph\";\n\n/**\n * Backend-provided tools excluded from PTC by default.\n * These are redundant inside the REPL since VFS helpers (readFile/writeFile)\n * already cover file I/O against the agent's in-memory working set.\n */\nexport const DEFAULT_PTC_EXCLUDED_TOOLS = [\n \"ls\",\n \"read_file\",\n \"write_file\",\n \"edit_file\",\n \"glob\",\n \"grep\",\n \"execute\",\n] as const;\n\nconst REPL_SYSTEM_PROMPT = dedent`\n ## TypeScript/JavaScript REPL (\\`js_eval\\`)\n\n You have access to a sandboxed TypeScript/JavaScript REPL running in an isolated interpreter.\n TypeScript syntax (type annotations, interfaces, generics, \\`as\\` casts) is supported and stripped at evaluation time.\n Variables, functions, and closures persist across calls within the same session.\n\n ### Hard rules\n\n - **No network, no filesystem** — only the helpers below. Do not attempt \\`fetch\\`, \\`require\\`, or \\`import\\`.\n - **Cite your sources** — when reporting values from files, include the path and key/index so the user can verify.\n - **Use console.log()** for output — it is captured and returned. \\`console.warn()\\` and \\`console.error()\\` are also available.\n - **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.\n\n ### First-time usage\n\n \\`\\`\\`typescript\n // Read a file from the agent's virtual filesystem\n const raw: string = await readFile(\"/data.json\");\n const data = JSON.parse(raw) as { n: number };\n console.log(data);\n\n // Write results back\n await writeFile(\"/output.txt\", JSON.stringify({ result: data.n }));\n \\`\\`\\`\n\n ### API Reference — built-in globals\n\n \\`\\`\\`typescript\n /**\n * Read a file from the agent's virtual filesystem. Throws if the file does not exist.\n */\n async readFile(path: string): Promise<string>\n\n /**\n * Write a file to the agent's virtual filesystem.\n */\n async writeFile(path: string, content: string): Promise<void>\n \\`\\`\\`\n\n ### Limitations\n\n - ES2023+ syntax with TypeScript support. No Node.js APIs, no \\`require\\`, no \\`import\\`.\n - Output is truncated beyond a fixed character limit — be selective about what you log.\n - Execution timeout per call (default 30 s).\n`;\n\n/**\n * Generate the PTC API Reference section for the system prompt.\n */\nexport async function generatePtcPrompt(\n tools: StructuredToolInterface[],\n): Promise<string> {\n if (tools.length === 0) return \"\";\n\n const signatures = await Promise.all(\n tools.map((t) => {\n const jsonSchema = t.schema ? safeToJsonSchema(t.schema) : undefined;\n return toolToTypeSignature(\n toCamelCase(t.name),\n t.description,\n jsonSchema,\n );\n }),\n );\n\n return dedent`\n\n ### API Reference — \\`tools\\` namespace\n\n The following agent tools are callable as async functions inside the REPL.\n Each takes a single object argument and returns a Promise that resolves to a string.\n Use \\`await\\` to call them. Promise APIs like \\`Promise.all\\` are also available.\n\n **Example usage:**\n \\`\\`\\`javascript\n // Call a tool\n const result = await tools.searchWeb({ query: \"QuickJS tutorial\" });\n console.log(result);\n\n // Concurrent calls\n const [a, b] = await Promise.all([\n tools.fetchData({ url: \"https://api.example.com/a\" }),\n tools.fetchData({ url: \"https://api.example.com/b\" }),\n ]);\n \\`\\`\\`\n\n **Available functions:**\n \\`\\`\\`typescript\n ${signatures.join(\"\\n\\n\")}\n \\`\\`\\`\n `;\n}\n\n/**\n * Resolve backend from factory or instance.\n */\nfunction getBackend(\n backend: BackendProtocol | BackendFactory,\n stateAndStore: StateAndStore,\n): BackendProtocol {\n if (typeof backend === \"function\") {\n return backend(stateAndStore);\n }\n return backend;\n}\n\n/**\n * Create the QuickJS REPL middleware.\n */\nexport function createQuickJSMiddleware(\n options: QuickJSMiddlewareOptions = {},\n) {\n const {\n backend = (stateAndStore: StateAndStore) => new StateBackend(stateAndStore),\n ptc = false,\n memoryLimitBytes = DEFAULT_MEMORY_LIMIT,\n maxStackSizeBytes = DEFAULT_MAX_STACK_SIZE,\n executionTimeoutMs = DEFAULT_EXECUTION_TIMEOUT,\n systemPrompt: customSystemPrompt = null,\n } = options;\n\n const usePtc = ptc !== false;\n const baseSystemPrompt = customSystemPrompt || REPL_SYSTEM_PROMPT;\n\n let cachedPtcPrompt: string | null = null;\n\n let ptcTools: StructuredToolInterface[] = [];\n\n function filterToolsForPtc(\n allTools: StructuredToolInterface[],\n ): StructuredToolInterface[] {\n if (ptc === false) return [];\n\n const candidates = allTools.filter((t) => t.name !== \"js_eval\");\n\n if (ptc === true) {\n const excluded = new Set<string>(DEFAULT_PTC_EXCLUDED_TOOLS);\n return candidates.filter((t) => !excluded.has(t.name));\n }\n\n if (Array.isArray(ptc)) {\n const included = new Set(ptc);\n return candidates.filter((t) => included.has(t.name));\n }\n\n if (\"include\" in ptc) {\n const included = new Set(ptc.include);\n return candidates.filter((t) => included.has(t.name));\n }\n\n if (\"exclude\" in ptc) {\n const excluded = new Set([...DEFAULT_PTC_EXCLUDED_TOOLS, ...ptc.exclude]);\n return candidates.filter((t) => !excluded.has(t.name));\n }\n\n return [];\n }\n\n const jsEvalTool = tool(\n async (input, config: LangGraphRunnableConfig) => {\n const threadId = config.configurable?.thread_id || DEFAULT_SESSION_ID;\n\n const stateAndStore: StateAndStore = {\n state: getCurrentTaskInput(config) || {},\n store: config.store,\n };\n const resolvedBackend = getBackend(backend, stateAndStore);\n\n const session = ReplSession.getOrCreate(threadId, {\n memoryLimitBytes,\n maxStackSizeBytes,\n backend: resolvedBackend,\n tools: ptcTools,\n });\n\n const result = await session.eval(input.code, executionTimeoutMs);\n await session.flushWrites(resolvedBackend);\n\n return formatReplResult(result);\n },\n {\n name: \"js_eval\",\n description: dedent`\n Evaluate TypeScript/JavaScript code in a sandboxed REPL. State persists across calls.\n Use readFile(path) and writeFile(path, content) for file access.\n Use console.log() for output. Returns the result of the last expression.\n `,\n schema: z.object({\n code: z\n .string()\n .describe(\n \"TypeScript/JavaScript code to evaluate in the sandboxed REPL\",\n ),\n }),\n },\n );\n\n return createMiddleware({\n name: \"QuickJSMiddleware\",\n tools: [jsEvalTool],\n wrapModelCall: async (request, handler) => {\n const agentTools = (request.tools || []) as StructuredToolInterface[];\n ptcTools = usePtc ? filterToolsForPtc(agentTools) : [];\n\n if (ptcTools.length > 0 && !cachedPtcPrompt) {\n cachedPtcPrompt = await generatePtcPrompt(ptcTools);\n }\n\n const systemMessage = request.systemMessage\n .concat(baseSystemPrompt)\n .concat(cachedPtcPrompt || \"\");\n return handler({ ...request, systemMessage });\n },\n });\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAQA,SAAgB,YAAY,MAAsB;AAChD,QAAO,KAAK,QAAQ,iBAAiB,GAAG,MAAM,EAAE,aAAa,CAAC;;;;;AAwBhE,SAAgB,iBAAiB,QAA4B;CAC3D,MAAM,QAAkB,EAAE;AAE1B,KAAI,OAAO,KAAK,SAAS,EACvB,OAAM,KAAK,OAAO,KAAK,KAAK,KAAK,CAAC;AAGpC,KAAI,OAAO;MACL,OAAO,UAAU,KAAA,GAAW;GAC9B,MAAM,YACJ,OAAO,OAAO,UAAU,WACpB,OAAO,QACP,KAAK,UAAU,OAAO,OAAO,MAAM,EAAE;AAC3C,SAAM,KAAK,KAAK,YAAY;;YAErB,OAAO,OAAO;EACvB,MAAM,UAAU,OAAO,MAAM,QAAQ;EACrC,MAAM,SAAS,OAAO,MAAM,WAAW;AACvC,QAAM,KAAK,GAAG,QAAQ,IAAI,SAAS;AACnC,MAAI,OAAO,MAAM,MACf,OAAM,KAAK,OAAO,MAAM,MAAM;;AAIlC,QAAO,MAAM,KAAK,KAAK,IAAI;;AAG7B,SAAgB,iBACd,QACqC;AACrC,KAAI;AACF,SAAO,aAAa,OAA6C;SAI3D;AACN;;;AAIJ,eAAe,kBACb,YACA,eACiB;AAMjB,SALiB,MAAM,QACrB;EAAE,GAAG;EAAY,sBAAsB;EAAO,EA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,EAAE;AAEtD,OAAI,SAAS,SAAS,KAAK,CAAC,gBAC1B,mBAAkB,MAAM,kBAAkB,SAAS;GAGrD,MAAM,gBAAgB,QAAQ,cAC3B,OAAO,iBAAiB,CACxB,OAAO,mBAAmB,GAAG;AAChC,UAAO,QAAQ;IAAE,GAAG;IAAS;IAAe,CAAC;;EAEhD,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@langchain/quickjs",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "Sandboxed JavaScript REPL for deepagents using QuickJS (WASM)",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
"deepagents": ">=1.6.0"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
|
-
"@langchain/core": "^1.1.
|
|
45
|
+
"@langchain/core": "^1.1.33",
|
|
46
46
|
"@langchain/langgraph": "^1.1.4",
|
|
47
47
|
"@tsconfig/recommended": "^1.0.13",
|
|
48
48
|
"@types/dedent": "^0.7.2",
|
|
@@ -50,13 +50,13 @@
|
|
|
50
50
|
"@types/node": "^25.2.3",
|
|
51
51
|
"@vitest/coverage-v8": "^4.0.18",
|
|
52
52
|
"dotenv": "^17.2.3",
|
|
53
|
-
"langchain": "1.2.
|
|
54
|
-
"tsdown": "^0.
|
|
53
|
+
"langchain": "1.2.36",
|
|
54
|
+
"tsdown": "^0.21.4",
|
|
55
55
|
"tsx": "^4.21.0",
|
|
56
56
|
"typescript": "^5.9.3",
|
|
57
57
|
"vitest": "^4.0.18",
|
|
58
58
|
"zod": "^4.3.6",
|
|
59
|
-
"deepagents": "1.8.
|
|
59
|
+
"deepagents": "1.8.5"
|
|
60
60
|
},
|
|
61
61
|
"exports": {
|
|
62
62
|
".": {
|