@langchain/quickjs 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +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,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,6DAAoB,OAA6C;SAI3D;AACN;;;AAIJ,eAAe,kBACb,YACA,eACiB;AAMjB,SALiB,6CACf;EAAE,GAAG;EAAY,sBAAsB;EAAO,EAC9C,eACA;EAAE,eAAe;EAAI,sBAAsB;EAAO,CACnD,EACe,QAAQ,YAAY,GAAG,CAAC,SAAS;;AAGnD,SAAgB,WAAW,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AAS,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"}
@@ -0,0 +1,204 @@
1
+ import * as langchain from "langchain";
2
+ import { AgentMiddleware } from "langchain";
3
+ import { z } from "zod/v4";
4
+ import { StructuredToolInterface } from "@langchain/core/tools";
5
+ import { BackendFactory, BackendProtocol } from "deepagents";
6
+
7
+ //#region src/types.d.ts
8
+ /**
9
+ * Configuration options for the QuickJS REPL middleware.
10
+ */
11
+ interface QuickJSMiddlewareOptions {
12
+ /**
13
+ * Backend for file I/O (readFile/writeFile) inside the REPL.
14
+ * Accepts a BackendProtocol instance or a BackendFactory function.
15
+ * Defaults to StateBackend (reads/writes LangGraph checkpoint state).
16
+ * @default StateBackend
17
+ */
18
+ backend?: BackendProtocol | BackendFactory;
19
+ /**
20
+ * Enable programmatic tool calling from within the REPL.
21
+ *
22
+ * - `false` — disabled (default)
23
+ * - `true` — expose all agent tools except standard vfs tools
24
+ * - `string[]` — expose only these tools (alias for `{ include }`)
25
+ * - `{ include: string[] }` — expose only these tools
26
+ * - `{ exclude: string[] }` — expose all agent tools except these
27
+ *
28
+ * @default false
29
+ */
30
+ ptc?: boolean | string[] | {
31
+ include: string[];
32
+ } | {
33
+ exclude: string[];
34
+ };
35
+ /**
36
+ * Memory limit in bytes.
37
+ * @default 52428800 (50MB)
38
+ */
39
+ memoryLimitBytes?: number;
40
+ /**
41
+ * Max stack size in bytes.
42
+ * @default 327680 (320KB)
43
+ */
44
+ maxStackSizeBytes?: number;
45
+ /**
46
+ * Execution timeout in milliseconds per evaluation.
47
+ * Set to a negative value to disable the timeout entirely.
48
+ * @default 30000 (30s)
49
+ */
50
+ executionTimeoutMs?: number;
51
+ /**
52
+ * Custom system prompt override. Set to null to disable the system prompt.
53
+ * @default null (uses built-in prompt)
54
+ */
55
+ systemPrompt?: string | null;
56
+ }
57
+ /**
58
+ * Options for creating a ReplSession.
59
+ */
60
+ interface ReplSessionOptions {
61
+ memoryLimitBytes?: number;
62
+ maxStackSizeBytes?: number;
63
+ backend?: BackendProtocol;
64
+ tools?: StructuredToolInterface[];
65
+ }
66
+ /**
67
+ * Result of a single REPL evaluation.
68
+ */
69
+ interface ReplResult {
70
+ ok: boolean;
71
+ value?: unknown;
72
+ error?: {
73
+ name?: string;
74
+ message?: string;
75
+ stack?: string;
76
+ };
77
+ logs: string[];
78
+ }
79
+ //#endregion
80
+ //#region src/middleware.d.ts
81
+ /**
82
+ * Backend-provided tools excluded from PTC by default.
83
+ * These are redundant inside the REPL since VFS helpers (readFile/writeFile)
84
+ * already cover file I/O against the agent's in-memory working set.
85
+ */
86
+ declare const DEFAULT_PTC_EXCLUDED_TOOLS: readonly ["ls", "read_file", "write_file", "edit_file", "glob", "grep", "execute"];
87
+ /**
88
+ * Create the QuickJS REPL middleware.
89
+ */
90
+ declare function createQuickJSMiddleware(options?: QuickJSMiddlewareOptions): AgentMiddleware<undefined, undefined, unknown, readonly [langchain.DynamicStructuredTool<z.ZodObject<{
91
+ code: z.ZodString;
92
+ }, z.core.$strip>, {
93
+ code: string;
94
+ }, {
95
+ code: string;
96
+ }, string, "js_eval">]>;
97
+ //#endregion
98
+ //#region src/session.d.ts
99
+ declare const DEFAULT_MEMORY_LIMIT: number;
100
+ declare const DEFAULT_MAX_STACK_SIZE: number;
101
+ declare const DEFAULT_EXECUTION_TIMEOUT = 30000;
102
+ interface PendingWrite {
103
+ path: string;
104
+ content: string;
105
+ }
106
+ /**
107
+ * Sandboxed JavaScript REPL session backed by QuickJS WASM.
108
+ *
109
+ * Serializable — holds an `id` that keys into a static session map.
110
+ * The QuickJS runtime is lazily started on the first `.eval()` call
111
+ * and reconnected if a session with the same id already exists.
112
+ * This makes it safe to store in LangGraph state across interrupts.
113
+ *
114
+ * File writes are buffered during execution and flushed via
115
+ * `flushWrites(backend)` after eval completes.
116
+ */
117
+ declare class ReplSession {
118
+ private static sessions;
119
+ readonly id: string;
120
+ readonly pendingWrites: PendingWrite[];
121
+ private runtime;
122
+ private context;
123
+ private logs;
124
+ private _options;
125
+ private _backend;
126
+ constructor(id: string, options?: ReplSessionOptions);
127
+ get backend(): BackendProtocol | null;
128
+ set backend(b: BackendProtocol | null);
129
+ private ensureStarted;
130
+ /**
131
+ * Get or create a session for the given id.
132
+ *
133
+ * Sessions are deduped by id — calling `getOrCreate` twice with the
134
+ * same id returns the same instance. The QuickJS runtime is lazily
135
+ * started on the first `.eval()` call.
136
+ */
137
+ static getOrCreate(id: string, options?: ReplSessionOptions): ReplSession;
138
+ /**
139
+ * Retrieve an existing session by id, or null if none exists.
140
+ */
141
+ static get(id: string): ReplSession | null;
142
+ /**
143
+ * Evaluate code in this session.
144
+ *
145
+ * Lazily starts the QuickJS runtime on the first call. Code is
146
+ * transformed via an AST pipeline that strips TypeScript syntax,
147
+ * hoists top-level declarations to globalThis for cross-eval
148
+ * persistence, auto-returns the last expression, and wraps in an
149
+ * async IIFE.
150
+ */
151
+ eval(code: string, timeoutMs: number): Promise<ReplResult>;
152
+ flushWrites(backend: BackendProtocol): Promise<void>;
153
+ dispose(): void;
154
+ toJSON(): {
155
+ id: string;
156
+ };
157
+ static fromJSON(data: {
158
+ id: string;
159
+ }): ReplSession;
160
+ /**
161
+ * Clear the static session cache. Useful for testing.
162
+ * @internal
163
+ */
164
+ static clearCache(): void;
165
+ private setupConsole;
166
+ private injectVfs;
167
+ private injectTools;
168
+ }
169
+ //#endregion
170
+ //#region src/utils.d.ts
171
+ /**
172
+ * Convert a snake_case or kebab-case string to camelCase.
173
+ */
174
+ declare function toCamelCase(name: string): string;
175
+ /**
176
+ * Format the result of a REPL evaluation for the agent.
177
+ */
178
+ declare function formatReplResult(result: ReplResult): string;
179
+ //#endregion
180
+ //#region src/transform.d.ts
181
+ /**
182
+ * AST-based code transform pipeline for the REPL.
183
+ *
184
+ * Transforms TypeScript/JavaScript code into plain JS that can be
185
+ * evaluated inside QuickJS with proper state persistence:
186
+ *
187
+ * 1. Parse with acorn + acorn-typescript (handles TS syntax)
188
+ * 2. Strip TypeScript-only nodes (type annotations, interfaces, etc.)
189
+ * 3. Hoist top-level declarations to globalThis for cross-eval persistence
190
+ * 4. Auto-return the last expression
191
+ * 5. Wrap in async IIFE so top-level await works
192
+ */
193
+ /**
194
+ * Transform code for REPL evaluation.
195
+ *
196
+ * - Strips TypeScript syntax
197
+ * - Hoists top-level variable declarations to globalThis
198
+ * - Auto-returns the last expression
199
+ * - Wraps in async IIFE for top-level await support
200
+ */
201
+ declare function transformForEval(code: string): string;
202
+ //#endregion
203
+ export { DEFAULT_EXECUTION_TIMEOUT, DEFAULT_MAX_STACK_SIZE, DEFAULT_MEMORY_LIMIT, DEFAULT_PTC_EXCLUDED_TOOLS, type PendingWrite, type QuickJSMiddlewareOptions, type ReplResult, ReplSession, type ReplSessionOptions, createQuickJSMiddleware, formatReplResult, toCamelCase, transformForEval };
204
+ //# sourceMappingURL=index.d.cts.map
@@ -0,0 +1,204 @@
1
+ import * as langchain from "langchain";
2
+ import { AgentMiddleware } from "langchain";
3
+ import { z } from "zod/v4";
4
+ import { BackendFactory, BackendProtocol } from "deepagents";
5
+ import { StructuredToolInterface } from "@langchain/core/tools";
6
+
7
+ //#region src/types.d.ts
8
+ /**
9
+ * Configuration options for the QuickJS REPL middleware.
10
+ */
11
+ interface QuickJSMiddlewareOptions {
12
+ /**
13
+ * Backend for file I/O (readFile/writeFile) inside the REPL.
14
+ * Accepts a BackendProtocol instance or a BackendFactory function.
15
+ * Defaults to StateBackend (reads/writes LangGraph checkpoint state).
16
+ * @default StateBackend
17
+ */
18
+ backend?: BackendProtocol | BackendFactory;
19
+ /**
20
+ * Enable programmatic tool calling from within the REPL.
21
+ *
22
+ * - `false` — disabled (default)
23
+ * - `true` — expose all agent tools except standard vfs tools
24
+ * - `string[]` — expose only these tools (alias for `{ include }`)
25
+ * - `{ include: string[] }` — expose only these tools
26
+ * - `{ exclude: string[] }` — expose all agent tools except these
27
+ *
28
+ * @default false
29
+ */
30
+ ptc?: boolean | string[] | {
31
+ include: string[];
32
+ } | {
33
+ exclude: string[];
34
+ };
35
+ /**
36
+ * Memory limit in bytes.
37
+ * @default 52428800 (50MB)
38
+ */
39
+ memoryLimitBytes?: number;
40
+ /**
41
+ * Max stack size in bytes.
42
+ * @default 327680 (320KB)
43
+ */
44
+ maxStackSizeBytes?: number;
45
+ /**
46
+ * Execution timeout in milliseconds per evaluation.
47
+ * Set to a negative value to disable the timeout entirely.
48
+ * @default 30000 (30s)
49
+ */
50
+ executionTimeoutMs?: number;
51
+ /**
52
+ * Custom system prompt override. Set to null to disable the system prompt.
53
+ * @default null (uses built-in prompt)
54
+ */
55
+ systemPrompt?: string | null;
56
+ }
57
+ /**
58
+ * Options for creating a ReplSession.
59
+ */
60
+ interface ReplSessionOptions {
61
+ memoryLimitBytes?: number;
62
+ maxStackSizeBytes?: number;
63
+ backend?: BackendProtocol;
64
+ tools?: StructuredToolInterface[];
65
+ }
66
+ /**
67
+ * Result of a single REPL evaluation.
68
+ */
69
+ interface ReplResult {
70
+ ok: boolean;
71
+ value?: unknown;
72
+ error?: {
73
+ name?: string;
74
+ message?: string;
75
+ stack?: string;
76
+ };
77
+ logs: string[];
78
+ }
79
+ //#endregion
80
+ //#region src/middleware.d.ts
81
+ /**
82
+ * Backend-provided tools excluded from PTC by default.
83
+ * These are redundant inside the REPL since VFS helpers (readFile/writeFile)
84
+ * already cover file I/O against the agent's in-memory working set.
85
+ */
86
+ declare const DEFAULT_PTC_EXCLUDED_TOOLS: readonly ["ls", "read_file", "write_file", "edit_file", "glob", "grep", "execute"];
87
+ /**
88
+ * Create the QuickJS REPL middleware.
89
+ */
90
+ declare function createQuickJSMiddleware(options?: QuickJSMiddlewareOptions): AgentMiddleware<undefined, undefined, unknown, readonly [langchain.DynamicStructuredTool<z.ZodObject<{
91
+ code: z.ZodString;
92
+ }, z.core.$strip>, {
93
+ code: string;
94
+ }, {
95
+ code: string;
96
+ }, string, "js_eval">]>;
97
+ //#endregion
98
+ //#region src/session.d.ts
99
+ declare const DEFAULT_MEMORY_LIMIT: number;
100
+ declare const DEFAULT_MAX_STACK_SIZE: number;
101
+ declare const DEFAULT_EXECUTION_TIMEOUT = 30000;
102
+ interface PendingWrite {
103
+ path: string;
104
+ content: string;
105
+ }
106
+ /**
107
+ * Sandboxed JavaScript REPL session backed by QuickJS WASM.
108
+ *
109
+ * Serializable — holds an `id` that keys into a static session map.
110
+ * The QuickJS runtime is lazily started on the first `.eval()` call
111
+ * and reconnected if a session with the same id already exists.
112
+ * This makes it safe to store in LangGraph state across interrupts.
113
+ *
114
+ * File writes are buffered during execution and flushed via
115
+ * `flushWrites(backend)` after eval completes.
116
+ */
117
+ declare class ReplSession {
118
+ private static sessions;
119
+ readonly id: string;
120
+ readonly pendingWrites: PendingWrite[];
121
+ private runtime;
122
+ private context;
123
+ private logs;
124
+ private _options;
125
+ private _backend;
126
+ constructor(id: string, options?: ReplSessionOptions);
127
+ get backend(): BackendProtocol | null;
128
+ set backend(b: BackendProtocol | null);
129
+ private ensureStarted;
130
+ /**
131
+ * Get or create a session for the given id.
132
+ *
133
+ * Sessions are deduped by id — calling `getOrCreate` twice with the
134
+ * same id returns the same instance. The QuickJS runtime is lazily
135
+ * started on the first `.eval()` call.
136
+ */
137
+ static getOrCreate(id: string, options?: ReplSessionOptions): ReplSession;
138
+ /**
139
+ * Retrieve an existing session by id, or null if none exists.
140
+ */
141
+ static get(id: string): ReplSession | null;
142
+ /**
143
+ * Evaluate code in this session.
144
+ *
145
+ * Lazily starts the QuickJS runtime on the first call. Code is
146
+ * transformed via an AST pipeline that strips TypeScript syntax,
147
+ * hoists top-level declarations to globalThis for cross-eval
148
+ * persistence, auto-returns the last expression, and wraps in an
149
+ * async IIFE.
150
+ */
151
+ eval(code: string, timeoutMs: number): Promise<ReplResult>;
152
+ flushWrites(backend: BackendProtocol): Promise<void>;
153
+ dispose(): void;
154
+ toJSON(): {
155
+ id: string;
156
+ };
157
+ static fromJSON(data: {
158
+ id: string;
159
+ }): ReplSession;
160
+ /**
161
+ * Clear the static session cache. Useful for testing.
162
+ * @internal
163
+ */
164
+ static clearCache(): void;
165
+ private setupConsole;
166
+ private injectVfs;
167
+ private injectTools;
168
+ }
169
+ //#endregion
170
+ //#region src/utils.d.ts
171
+ /**
172
+ * Convert a snake_case or kebab-case string to camelCase.
173
+ */
174
+ declare function toCamelCase(name: string): string;
175
+ /**
176
+ * Format the result of a REPL evaluation for the agent.
177
+ */
178
+ declare function formatReplResult(result: ReplResult): string;
179
+ //#endregion
180
+ //#region src/transform.d.ts
181
+ /**
182
+ * AST-based code transform pipeline for the REPL.
183
+ *
184
+ * Transforms TypeScript/JavaScript code into plain JS that can be
185
+ * evaluated inside QuickJS with proper state persistence:
186
+ *
187
+ * 1. Parse with acorn + acorn-typescript (handles TS syntax)
188
+ * 2. Strip TypeScript-only nodes (type annotations, interfaces, etc.)
189
+ * 3. Hoist top-level declarations to globalThis for cross-eval persistence
190
+ * 4. Auto-return the last expression
191
+ * 5. Wrap in async IIFE so top-level await works
192
+ */
193
+ /**
194
+ * Transform code for REPL evaluation.
195
+ *
196
+ * - Strips TypeScript syntax
197
+ * - Hoists top-level variable declarations to globalThis
198
+ * - Auto-returns the last expression
199
+ * - Wraps in async IIFE for top-level await support
200
+ */
201
+ declare function transformForEval(code: string): string;
202
+ //#endregion
203
+ export { DEFAULT_EXECUTION_TIMEOUT, DEFAULT_MAX_STACK_SIZE, DEFAULT_MEMORY_LIMIT, DEFAULT_PTC_EXCLUDED_TOOLS, type PendingWrite, type QuickJSMiddlewareOptions, type ReplResult, ReplSession, type ReplSessionOptions, createQuickJSMiddleware, formatReplResult, toCamelCase, transformForEval };
204
+ //# sourceMappingURL=index.d.ts.map