@langchain/deno 0.1.1 → 0.1.2
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 +0 -98
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -39
- package/dist/index.d.ts +1 -39
- package/dist/index.js +0 -98
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -420,104 +420,6 @@ var DenoSandbox = class DenoSandbox extends deepagents.BaseSandbox {
|
|
|
420
420
|
return results;
|
|
421
421
|
}
|
|
422
422
|
/**
|
|
423
|
-
* Read a file's content with line numbers.
|
|
424
|
-
*
|
|
425
|
-
* Override of BaseSandbox.read() to use awk instead of Python,
|
|
426
|
-
* since Deno sandboxes don't have Python installed.
|
|
427
|
-
*
|
|
428
|
-
* @param filePath - Absolute path to the file
|
|
429
|
-
* @param offset - Line offset (0-indexed, default 0)
|
|
430
|
-
* @param limit - Maximum lines to return (default 500)
|
|
431
|
-
* @returns Formatted file content with line numbers, or error message
|
|
432
|
-
*/
|
|
433
|
-
async read(filePath, offset = 0, limit = 500) {
|
|
434
|
-
const safeOffset = Number.isFinite(offset) && offset > 0 ? Math.floor(offset) : 0;
|
|
435
|
-
const safeLimit = Number.isFinite(limit) && limit > 0 && limit < Number.MAX_SAFE_INTEGER ? Math.floor(limit) : 500;
|
|
436
|
-
const escapedPath = filePath.replace(/'/g, "'\\''");
|
|
437
|
-
const command = `
|
|
438
|
-
if [ ! -f '${escapedPath}' ]; then
|
|
439
|
-
echo "Error: File not found"
|
|
440
|
-
exit 1
|
|
441
|
-
fi
|
|
442
|
-
if [ ! -s '${escapedPath}' ]; then
|
|
443
|
-
echo "System reminder: File exists but has empty contents"
|
|
444
|
-
exit 0
|
|
445
|
-
fi
|
|
446
|
-
awk -v offset=${safeOffset} -v limit=${safeLimit} '
|
|
447
|
-
NR > offset && NR <= offset + limit {
|
|
448
|
-
printf "%6d\\t%s\\n", NR, $0
|
|
449
|
-
}
|
|
450
|
-
' '${escapedPath}'
|
|
451
|
-
`;
|
|
452
|
-
const result = await this.execute(command);
|
|
453
|
-
if (result.exitCode !== 0) return `Error: File '${filePath}' not found`;
|
|
454
|
-
return result.output;
|
|
455
|
-
}
|
|
456
|
-
/**
|
|
457
|
-
* Create a new file with content.
|
|
458
|
-
*
|
|
459
|
-
* Override of BaseSandbox.write() to use shell commands instead of Python,
|
|
460
|
-
* since Deno sandboxes don't have Python installed.
|
|
461
|
-
*
|
|
462
|
-
* @param filePath - Absolute path for the new file
|
|
463
|
-
* @param content - File content to write
|
|
464
|
-
* @returns WriteResult with error populated on failure
|
|
465
|
-
*/
|
|
466
|
-
async write(filePath, content) {
|
|
467
|
-
const escapedPath = filePath.replace(/'/g, "'\\''");
|
|
468
|
-
if ((await this.execute(`test -f '${escapedPath}'`)).exitCode === 0) return { error: `Cannot write to ${filePath} because it already exists. Read and then make an edit, or write to a new path.` };
|
|
469
|
-
const encoder = new TextEncoder();
|
|
470
|
-
const uploadResult = await this.uploadFiles([[filePath, encoder.encode(content)]]);
|
|
471
|
-
if (uploadResult[0]?.error) return { error: `Failed to write file: ${uploadResult[0].error}` };
|
|
472
|
-
return {
|
|
473
|
-
path: filePath,
|
|
474
|
-
filesUpdate: null
|
|
475
|
-
};
|
|
476
|
-
}
|
|
477
|
-
/**
|
|
478
|
-
* Edit a file by replacing string occurrences.
|
|
479
|
-
*
|
|
480
|
-
* Override of BaseSandbox.edit() to use shell commands instead of Python,
|
|
481
|
-
* since Deno sandboxes don't have Python installed.
|
|
482
|
-
*
|
|
483
|
-
* Uses sed for in-place replacement with proper escaping.
|
|
484
|
-
*
|
|
485
|
-
* @param filePath - Absolute path to the file
|
|
486
|
-
* @param oldString - String to find and replace
|
|
487
|
-
* @param newString - Replacement string
|
|
488
|
-
* @param replaceAll - If true, replace all occurrences (default: false)
|
|
489
|
-
* @returns EditResult with error, path, and occurrences
|
|
490
|
-
*/
|
|
491
|
-
async edit(filePath, oldString, newString, replaceAll = false) {
|
|
492
|
-
const escapedPath = filePath.replace(/'/g, "'\\''");
|
|
493
|
-
if ((await this.execute(`test -f '${escapedPath}'`)).exitCode !== 0) return { error: `Error: File '${filePath}' not found` };
|
|
494
|
-
const escapedOldForGrep = oldString.replace(/'/g, "'\\''");
|
|
495
|
-
const countResult = await this.execute(`grep -oF '${escapedOldForGrep}' '${escapedPath}' | wc -l`);
|
|
496
|
-
const count = parseInt(countResult.output.trim(), 10) || 0;
|
|
497
|
-
if (count === 0) return { error: `String not found in file '${filePath}'` };
|
|
498
|
-
if (count > 1 && !replaceAll) return { error: `Multiple occurrences found in '${filePath}'. Use replaceAll=true to replace all.` };
|
|
499
|
-
const awkCommand = `
|
|
500
|
-
OLD=$(echo '${Buffer.from(oldString, "utf-8").toString("base64")}' | base64 -d)
|
|
501
|
-
NEW=$(echo '${Buffer.from(newString, "utf-8").toString("base64")}' | base64 -d)
|
|
502
|
-
awk -v old="$OLD" -v new="$NEW" -v replace_all=${replaceAll ? 1 : 0} '
|
|
503
|
-
{
|
|
504
|
-
if (replace_all) {
|
|
505
|
-
gsub(old, new)
|
|
506
|
-
} else {
|
|
507
|
-
sub(old, new)
|
|
508
|
-
}
|
|
509
|
-
print
|
|
510
|
-
}
|
|
511
|
-
' '${escapedPath}' > '${escapedPath}.tmp' && mv '${escapedPath}.tmp' '${escapedPath}'
|
|
512
|
-
`;
|
|
513
|
-
if ((await this.execute(awkCommand)).exitCode !== 0) return { error: `Unknown error editing file '${filePath}'` };
|
|
514
|
-
return {
|
|
515
|
-
path: filePath,
|
|
516
|
-
filesUpdate: null,
|
|
517
|
-
occurrences: count
|
|
518
|
-
};
|
|
519
|
-
}
|
|
520
|
-
/**
|
|
521
423
|
* Close the sandbox and release all resources.
|
|
522
424
|
*
|
|
523
425
|
* After closing, the sandbox cannot be used again. Any unsaved data
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":["SandboxError","BaseSandbox","#id","#sandbox","#options","Sandbox","#uploadInitialFiles","#mapError","#setFromExisting"],"sources":["../src/auth.ts","../src/types.ts","../src/sandbox.ts"],"sourcesContent":["/**\n * Authentication utilities for Deno Sandbox.\n *\n * This module provides authentication credential resolution for the Deno Sandbox SDK.\n *\n * @packageDocumentation\n */\n\nimport type { DenoSandboxOptions } from \"./types.js\";\n\n/**\n * Authentication credentials for Deno Sandbox API.\n */\nexport interface DenoCredentials {\n /** Deno Deploy access token */\n token: string;\n}\n\n/**\n * Get the authentication token for Deno Sandbox API.\n *\n * Authentication is resolved in the following priority order:\n *\n * 1. **Explicit token**: If `options.token` is provided, it is used directly.\n * 2. **DENO_DEPLOY_TOKEN**: Environment variable for Deno Deploy access token.\n *\n * If no token is found, an error is thrown with setup instructions.\n *\n * ## Environment Variable Setup\n *\n * ```bash\n * # Go to https://app.deno.com -> Settings -> Organization Tokens\n * # Create a new token and set it as environment variable\n * export DENO_DEPLOY_TOKEN=your_token_here\n * ```\n *\n * @param options - Optional authentication configuration from DenoSandboxOptions\n * @returns The authentication token string\n * @throws {Error} If no authentication token is available\n *\n * @example\n * ```typescript\n * // With explicit token\n * const token = getAuthToken({ token: \"my-token\" });\n *\n * // Using environment variables (auto-detected)\n * const token = getAuthToken();\n *\n * // From DenoSandboxOptions\n * const options: DenoSandboxOptions = {\n * auth: { token: \"my-token\" }\n * };\n * const token = getAuthToken(options.auth);\n * ```\n */\nexport function getAuthToken(options?: DenoSandboxOptions[\"auth\"]): string {\n // Priority 1: Explicit token in options\n if (options?.token) {\n return options.token;\n }\n\n // Priority 2: DENO_DEPLOY_TOKEN environment variable\n const deployToken = process.env.DENO_DEPLOY_TOKEN;\n if (deployToken) {\n return deployToken;\n }\n\n // No token found - throw descriptive error\n throw new Error(\n \"Deno Deploy authentication required. Provide a token using one of these methods:\\n\\n\" +\n \"1. Set DENO_DEPLOY_TOKEN environment variable:\\n\" +\n \" Go to https://app.deno.com -> Settings -> Organization Tokens\\n\" +\n \" Create a new token and run: export DENO_DEPLOY_TOKEN=your_token_here\\n\\n\" +\n \"2. Pass token directly in options:\\n\" +\n \" new DenoSandbox({ auth: { token: '...' } })\",\n );\n}\n\n/**\n * Get authentication credentials for Deno Sandbox API.\n *\n * This function returns the credentials needed for the Deno SDK.\n *\n * @param options - Optional authentication configuration from DenoSandboxOptions\n * @returns Complete authentication credentials\n * @throws {Error} If no authentication token is available\n */\nexport function getAuthCredentials(\n options?: DenoSandboxOptions[\"auth\"],\n): DenoCredentials {\n return {\n token: getAuthToken(options),\n };\n}\n","/**\n * Type definitions for the Deno Sandbox backend.\n *\n * This module contains all type definitions for the @langchain/deno package,\n * including options and error types.\n */\n\nimport { type SandboxErrorCode, SandboxError } from \"deepagents\";\n\n/**\n * Supported regions for Deno Deploy sandboxes.\n *\n * Currently available regions:\n * - `ams`: Amsterdam\n * - `ord`: Chicago\n */\nexport type DenoSandboxRegion = \"ams\" | \"ord\";\n\n/**\n * Sandbox lifetime configuration.\n *\n * - `\"session\"`: Sandbox shuts down when you close/dispose the client (default)\n * - Duration string: Keep sandbox alive for a specific time (e.g., \"5m\", \"30s\")\n */\nexport type SandboxLifetime = \"session\" | `${number}s` | `${number}m`;\n\n/**\n * Configuration options for creating a Deno Sandbox.\n *\n * @example\n * ```typescript\n * const options: DenoSandboxOptions = {\n * memoryMb: 1024, // 1GB memory\n * lifetime: \"5m\", // 5 minutes\n * region: \"iad\", // US East\n * };\n * ```\n */\nexport interface DenoSandboxOptions {\n /**\n * Amount of memory allocated to the sandbox in megabytes.\n *\n * Memory limits:\n * - Minimum: 768MB\n * - Maximum: 4096MB\n *\n * @default 768\n */\n memoryMb?: number;\n\n /**\n * Sandbox lifetime configuration.\n *\n * - `\"session\"`: Sandbox shuts down when you close/dispose the client (default)\n * - Duration string: Keep sandbox alive for a specific time (e.g., \"5m\", \"30s\")\n *\n * Supported duration suffixes: `s` (seconds), `m` (minutes).\n *\n * @default \"session\"\n */\n lifetime?: SandboxLifetime;\n\n /**\n * Region where the sandbox will be created.\n *\n * If not specified, the sandbox will be created in the default region.\n *\n * @see DenoSandboxRegion for available regions\n */\n region?: DenoSandboxRegion;\n\n /**\n * Initial files to create in the sandbox after initialization.\n *\n * A map of file paths to their contents. Files will be created\n * in the sandbox filesystem before any commands are executed.\n * Parent directories are created automatically.\n *\n * @example\n * ```typescript\n * const options: DenoSandboxOptions = {\n * memoryMb: 1024,\n * initialFiles: {\n * \"/home/app/index.js\": \"console.log('Hello')\",\n * \"/home/app/package.json\": '{\"name\": \"test\"}',\n * },\n * };\n * ```\n */\n initialFiles?: Record<string, string>;\n\n /**\n * Authentication configuration for Deno Deploy API.\n *\n * ### Environment Variable Setup\n *\n * ```bash\n * # Go to https://app.deno.com -> Settings -> Organization Tokens\n * # Create a new token and set it as environment variable\n * export DENO_DEPLOY_TOKEN=your_token_here\n * ```\n *\n * Or pass the token directly in this auth configuration.\n */\n auth?: {\n /**\n * Deno Deploy access token.\n * If not provided, reads from `DENO_DEPLOY_TOKEN` environment variable.\n */\n token?: string;\n };\n}\n\n/**\n * Error codes for Deno Sandbox operations.\n *\n * Used to identify specific error conditions and handle them appropriately.\n */\nexport type DenoSandboxErrorCode =\n | SandboxErrorCode\n /** Authentication failed - check token configuration */\n | \"AUTHENTICATION_FAILED\"\n /** Failed to create sandbox - check options and quotas */\n | \"SANDBOX_CREATION_FAILED\"\n /** Sandbox not found - may have been stopped or expired */\n | \"SANDBOX_NOT_FOUND\"\n /** Resource limits exceeded (CPU, memory, storage) */\n | \"RESOURCE_LIMIT_EXCEEDED\";\n\nconst DENO_SANDBOX_ERROR_SYMBOL = Symbol.for(\"deno.sandbox.error\");\n\n/**\n * Custom error class for Deno Sandbox operations.\n *\n * Provides structured error information including:\n * - Human-readable message\n * - Error code for programmatic handling\n * - Original cause for debugging\n *\n * @example\n * ```typescript\n * try {\n * await sandbox.execute(\"some command\");\n * } catch (error) {\n * if (error instanceof DenoSandboxError) {\n * switch (error.code) {\n * case \"NOT_INITIALIZED\":\n * await sandbox.initialize();\n * break;\n * case \"COMMAND_TIMEOUT\":\n * console.error(\"Command took too long\");\n * break;\n * default:\n * throw error;\n * }\n * }\n * }\n * ```\n */\nexport class DenoSandboxError extends SandboxError {\n [DENO_SANDBOX_ERROR_SYMBOL]: true;\n\n /** Error name for instanceof checks and logging */\n override readonly name = \"DenoSandboxError\";\n\n /**\n * Creates a new DenoSandboxError.\n *\n * @param message - Human-readable error description\n * @param code - Structured error code for programmatic handling\n * @param cause - Original error that caused this error (for debugging)\n */\n constructor(\n message: string,\n public readonly code: DenoSandboxErrorCode,\n public override readonly cause?: Error,\n ) {\n super(message, code as SandboxErrorCode, cause);\n // Maintain proper prototype chain for instanceof checks\n Object.setPrototypeOf(this, DenoSandboxError.prototype);\n }\n\n /**\n * Checks if the error is an instance of DenoSandboxError.\n *\n * @param error - The error to check\n * @returns True if the error is an instance of DenoSandboxError, false otherwise\n */\n static isInstance(error: unknown): error is DenoSandboxError {\n return (\n typeof error === \"object\" &&\n error !== null &&\n (error as Record<symbol, unknown>)[DENO_SANDBOX_ERROR_SYMBOL] === true\n );\n }\n}\n","/* eslint-disable no-instanceof/no-instanceof */\n/**\n * Deno Sandbox implementation of the SandboxBackendProtocol.\n *\n * This module provides a Deno Sandbox backend for deepagents, enabling agents\n * to execute commands, read/write files, and manage isolated Linux microVM\n * environments using Deno Deploy's Sandbox infrastructure.\n *\n * @packageDocumentation\n */\n\nimport { Sandbox } from \"@deno/sandbox\";\nimport {\n BaseSandbox,\n type EditResult,\n type ExecuteResponse,\n type FileDownloadResponse,\n type FileOperationError,\n type FileUploadResponse,\n type BackendFactory,\n type WriteResult,\n} from \"deepagents\";\n\nimport { getAuthCredentials } from \"./auth.js\";\nimport { DenoSandboxError, type DenoSandboxOptions } from \"./types.js\";\n\n/**\n * Deno Sandbox backend for deepagents.\n *\n * Extends `BaseSandbox` to provide command execution, file operations, and\n * sandbox lifecycle management using Deno Deploy's Sandbox SDK.\n *\n * ## Basic Usage\n *\n * ```typescript\n * import { DenoSandbox } from \"@langchain/deno\";\n *\n * // Create and initialize a sandbox\n * const sandbox = await DenoSandbox.create({\n * memoryMb: 1024,\n * lifetime: \"5m\",\n * });\n *\n * try {\n * // Execute commands\n * const result = await sandbox.execute(\"deno --version\");\n * console.log(result.output);\n * } finally {\n * // Always cleanup\n * await sandbox.close();\n * }\n * ```\n *\n * ## Using with DeepAgent\n *\n * ```typescript\n * import { createDeepAgent } from \"deepagents\";\n * import { DenoSandbox } from \"@langchain/deno\";\n *\n * const sandbox = await DenoSandbox.create();\n *\n * const agent = createDeepAgent({\n * model: new ChatAnthropic({ model: \"claude-sonnet-4-20250514\" }),\n * systemPrompt: \"You are a coding assistant with sandbox access.\",\n * backend: sandbox,\n * });\n * ```\n */\nexport class DenoSandbox extends BaseSandbox {\n /** Private reference to the underlying Deno Sandbox instance */\n #sandbox: Sandbox | null = null;\n\n /** Configuration options for this sandbox */\n #options: DenoSandboxOptions;\n\n /** Unique identifier for this sandbox instance */\n #id: string;\n\n /**\n * Get the unique identifier for this sandbox.\n *\n * Before initialization, returns a temporary ID.\n * After initialization, returns the actual Deno sandbox ID.\n */\n get id(): string {\n return this.#id;\n }\n\n /**\n * Get the underlying Deno Sandbox instance.\n *\n * @throws {DenoSandboxError} If the sandbox is not initialized\n *\n * @example\n * ```typescript\n * const sandbox = await DenoSandbox.create();\n * const denoSdk = sandbox.sandbox; // Access the raw SDK\n * ```\n */\n get instance(): Sandbox {\n if (!this.#sandbox) {\n throw new DenoSandboxError(\n \"Sandbox not initialized. Call initialize() or use DenoSandbox.create()\",\n \"NOT_INITIALIZED\",\n );\n }\n return this.#sandbox;\n }\n\n /**\n * Check if the sandbox is initialized and running.\n */\n get isRunning(): boolean {\n return this.#sandbox !== null;\n }\n\n /**\n * Create a new DenoSandbox instance.\n *\n * Note: This only creates the instance. Call `initialize()` to actually\n * create the Deno Sandbox, or use the static `DenoSandbox.create()` method.\n *\n * @param options - Configuration options for the sandbox\n *\n * @example\n * ```typescript\n * // Two-step initialization\n * const sandbox = new DenoSandbox({ memoryMb: 1024 });\n * await sandbox.initialize();\n *\n * // Or use the factory method\n * const sandbox = await DenoSandbox.create({ memoryMb: 1024 });\n * ```\n */\n constructor(options: DenoSandboxOptions = {}) {\n super();\n\n // Set defaults\n this.#options = {\n memoryMb: 768,\n lifetime: \"session\",\n ...options,\n };\n\n // Generate temporary ID until initialized\n this.#id = `deno-sandbox-${Date.now()}`;\n }\n\n /**\n * Initialize the sandbox by creating a new Deno Sandbox instance.\n *\n * This method authenticates with Deno Deploy and provisions a new microVM\n * sandbox. After initialization, the `id` property will reflect the\n * actual Deno sandbox ID.\n *\n * @throws {DenoSandboxError} If already initialized (`ALREADY_INITIALIZED`)\n * @throws {DenoSandboxError} If authentication fails (`AUTHENTICATION_FAILED`)\n * @throws {DenoSandboxError} If sandbox creation fails (`SANDBOX_CREATION_FAILED`)\n *\n * @example\n * ```typescript\n * const sandbox = new DenoSandbox();\n * await sandbox.initialize();\n * console.log(`Sandbox ID: ${sandbox.id}`);\n * ```\n */\n async initialize(): Promise<void> {\n // Prevent double initialization\n if (this.#sandbox) {\n throw new DenoSandboxError(\n \"Sandbox is already initialized. Each DenoSandbox instance can only be initialized once.\",\n \"ALREADY_INITIALIZED\",\n );\n }\n\n // Get authentication credentials\n let credentials: { token: string };\n try {\n credentials = getAuthCredentials(this.#options.auth);\n } catch (error) {\n throw new DenoSandboxError(\n \"Failed to authenticate with Deno Deploy. Check your token configuration.\",\n \"AUTHENTICATION_FAILED\",\n error instanceof Error ? error : undefined,\n );\n }\n\n try {\n // Set the token in environment for the SDK\n process.env.DENO_DEPLOY_TOKEN = credentials.token;\n\n // Build SDK create options\n const createOptions: Parameters<typeof Sandbox.create>[0] = {};\n\n // Add optional memory configuration\n if (this.#options.memoryMb !== undefined) {\n createOptions.memoryMb = this.#options.memoryMb;\n }\n\n // Add optional lifetime configuration\n if (this.#options.lifetime !== undefined) {\n createOptions.lifetime = this.#options.lifetime;\n }\n\n // Add optional region configuration\n if (this.#options.region !== undefined) {\n createOptions.region = this.#options.region;\n }\n\n // Create the sandbox\n this.#sandbox = await Sandbox.create(createOptions);\n\n // Update ID to the actual sandbox ID\n this.#id = this.#sandbox.id;\n\n // Upload initial files if provided\n if (this.#options.initialFiles) {\n await this.#uploadInitialFiles(this.#options.initialFiles);\n }\n } catch (error) {\n throw new DenoSandboxError(\n `Failed to create Deno Sandbox: ${error instanceof Error ? error.message : String(error)}`,\n \"SANDBOX_CREATION_FAILED\",\n error instanceof Error ? error : undefined,\n );\n }\n }\n\n /**\n * Upload initial files to the sandbox.\n *\n * @param files - A map of file paths to their string contents\n */\n async #uploadInitialFiles(files: Record<string, string>): Promise<void> {\n const encoder = new TextEncoder();\n const fileEntries: Array<[string, Uint8Array]> = Object.entries(files).map(\n ([path, content]) => [path, encoder.encode(content)],\n );\n\n const results = await this.uploadFiles(fileEntries);\n\n // Check for any errors during upload\n const errors = results.filter((r) => r.error !== null);\n if (errors.length > 0) {\n const errorPaths = errors.map((e) => `${e.path}: ${e.error}`).join(\", \");\n throw new DenoSandboxError(\n `Failed to upload initial files: ${errorPaths}`,\n \"FILE_OPERATION_FAILED\",\n );\n }\n }\n\n /**\n * Execute a command in the sandbox.\n *\n * Commands are run using the sandbox's shell in the configured working directory.\n *\n * @param command - The shell command to execute\n * @returns Execution result with output, exit code, and truncation flag\n * @throws {DenoSandboxError} If the sandbox is not initialized\n *\n * @example\n * ```typescript\n * const result = await sandbox.execute(\"echo 'Hello World'\");\n * console.log(result.output); // \"Hello World\\n\"\n * console.log(result.exitCode); // 0\n * ```\n */\n async execute(command: string): Promise<ExecuteResponse> {\n const sandbox = this.instance; // Throws if not initialized\n\n try {\n // Use spawn with bash to execute the command\n const child = await sandbox.spawn(\"/bin/bash\", {\n args: [\"-c\", command],\n stdout: \"piped\",\n stderr: \"piped\",\n });\n\n // Use output() to get buffered stdout/stderr\n const { status, stdoutText, stderrText } = await child.output();\n\n return {\n output: (stdoutText ?? \"\") + (stderrText ?? \"\"),\n exitCode: status.code ?? 0,\n truncated: false,\n };\n } catch (error) {\n // Check for timeout\n if (error instanceof Error && error.message.includes(\"timeout\")) {\n throw new DenoSandboxError(\n `Command timed out: ${command}`,\n \"COMMAND_TIMEOUT\",\n error,\n );\n }\n\n throw new DenoSandboxError(\n `Command execution failed: ${error instanceof Error ? error.message : String(error)}`,\n \"COMMAND_FAILED\",\n error instanceof Error ? error : undefined,\n );\n }\n }\n\n /**\n * Upload files to the sandbox.\n *\n * Files are written to the sandbox filesystem. Parent directories are\n * created automatically if they don't exist.\n *\n * @param files - Array of [path, content] tuples to upload\n * @returns Upload result for each file, with success or error status\n *\n * @example\n * ```typescript\n * const encoder = new TextEncoder();\n * const results = await sandbox.uploadFiles([\n * [\"src/index.js\", encoder.encode(\"console.log('Hello')\")],\n * [\"package.json\", encoder.encode('{\"name\": \"test\"}')],\n * ]);\n * ```\n */\n async uploadFiles(\n files: Array<[string, Uint8Array]>,\n ): Promise<FileUploadResponse[]> {\n const sandbox = this.instance; // Throws if not initialized\n const results: FileUploadResponse[] = [];\n\n for (const [path, content] of files) {\n try {\n // Ensure parent directory exists using spawn (more reliable than sh template)\n const parentDir = path.substring(0, path.lastIndexOf(\"/\"));\n if (parentDir) {\n const mkdirChild = await sandbox.spawn(\"/bin/bash\", {\n args: [\"-c\", `mkdir -p \"${parentDir}\"`],\n stdout: \"piped\",\n stderr: \"piped\",\n });\n await mkdirChild.output();\n }\n\n // Write the file content\n const textContent = new TextDecoder().decode(content);\n await sandbox.writeTextFile(path, textContent);\n results.push({ path, error: null });\n } catch (error) {\n results.push({ path, error: this.#mapError(error) });\n }\n }\n\n return results;\n }\n\n /**\n * Download files from the sandbox.\n *\n * Each file is read individually, allowing partial success when some\n * files exist and others don't.\n *\n * @param paths - Array of file paths to download\n * @returns Download result for each file, with content or error\n *\n * @example\n * ```typescript\n * const results = await sandbox.downloadFiles([\"src/index.js\", \"missing.txt\"]);\n * for (const result of results) {\n * if (result.content) {\n * console.log(new TextDecoder().decode(result.content));\n * } else {\n * console.error(`Error: ${result.error}`);\n * }\n * }\n * ```\n */\n async downloadFiles(paths: string[]): Promise<FileDownloadResponse[]> {\n const sandbox = this.instance; // Throws if not initialized\n const results: FileDownloadResponse[] = [];\n\n for (const path of paths) {\n try {\n // Use spawn with bash to read file content (same approach as execute())\n const child = await sandbox.spawn(\"/bin/bash\", {\n args: [\"-c\", `cat \"${path}\"`],\n stdout: \"piped\",\n stderr: \"piped\",\n });\n\n const { status, stdoutText } = await child.output();\n\n if (!status.success) {\n results.push({\n path,\n content: null,\n error: \"file_not_found\",\n });\n } else {\n const content = new TextEncoder().encode(stdoutText ?? \"\");\n results.push({\n path,\n content,\n error: null,\n });\n }\n } catch (error) {\n results.push({\n path,\n content: null,\n error: this.#mapError(error),\n });\n }\n }\n\n return results;\n }\n\n // ============================================================================\n // Override BaseSandbox methods that use Python with pure shell implementations\n // Deno sandboxes don't have Python installed, only basic Unix tools\n // ============================================================================\n\n /**\n * Read a file's content with line numbers.\n *\n * Override of BaseSandbox.read() to use awk instead of Python,\n * since Deno sandboxes don't have Python installed.\n *\n * @param filePath - Absolute path to the file\n * @param offset - Line offset (0-indexed, default 0)\n * @param limit - Maximum lines to return (default 500)\n * @returns Formatted file content with line numbers, or error message\n */\n override async read(\n filePath: string,\n offset: number = 0,\n limit: number = 500,\n ): Promise<string> {\n // Coerce offset and limit to safe non-negative integers\n const safeOffset =\n Number.isFinite(offset) && offset > 0 ? Math.floor(offset) : 0;\n const safeLimit =\n Number.isFinite(limit) && limit > 0 && limit < Number.MAX_SAFE_INTEGER\n ? Math.floor(limit)\n : 500;\n\n // Escape path for shell\n const escapedPath = filePath.replace(/'/g, \"'\\\\''\");\n\n // Build shell command using awk for portable line number formatting\n // First check if file exists, then format with line numbers\n const command = `\nif [ ! -f '${escapedPath}' ]; then\n echo \"Error: File not found\"\n exit 1\nfi\nif [ ! -s '${escapedPath}' ]; then\n echo \"System reminder: File exists but has empty contents\"\n exit 0\nfi\nawk -v offset=${safeOffset} -v limit=${safeLimit} '\n NR > offset && NR <= offset + limit {\n printf \"%6d\\\\t%s\\\\n\", NR, $0\n }\n' '${escapedPath}'\n`;\n\n const result = await this.execute(command);\n\n if (result.exitCode !== 0) {\n return `Error: File '${filePath}' not found`;\n }\n\n return result.output;\n }\n\n /**\n * Create a new file with content.\n *\n * Override of BaseSandbox.write() to use shell commands instead of Python,\n * since Deno sandboxes don't have Python installed.\n *\n * @param filePath - Absolute path for the new file\n * @param content - File content to write\n * @returns WriteResult with error populated on failure\n */\n override async write(\n filePath: string,\n content: string,\n ): Promise<WriteResult> {\n // Escape path for shell\n const escapedPath = filePath.replace(/'/g, \"'\\\\''\");\n\n // Check if file already exists\n const checkResult = await this.execute(`test -f '${escapedPath}'`);\n if (checkResult.exitCode === 0) {\n return {\n error: `Cannot write to ${filePath} because it already exists. Read and then make an edit, or write to a new path.`,\n };\n }\n\n // Use uploadFiles for reliable content writing (handles binary, special chars, etc.)\n const encoder = new TextEncoder();\n const uploadResult = await this.uploadFiles([\n [filePath, encoder.encode(content)],\n ]);\n\n if (uploadResult[0]?.error) {\n return { error: `Failed to write file: ${uploadResult[0].error}` };\n }\n\n return { path: filePath, filesUpdate: null };\n }\n\n /**\n * Edit a file by replacing string occurrences.\n *\n * Override of BaseSandbox.edit() to use shell commands instead of Python,\n * since Deno sandboxes don't have Python installed.\n *\n * Uses sed for in-place replacement with proper escaping.\n *\n * @param filePath - Absolute path to the file\n * @param oldString - String to find and replace\n * @param newString - Replacement string\n * @param replaceAll - If true, replace all occurrences (default: false)\n * @returns EditResult with error, path, and occurrences\n */\n override async edit(\n filePath: string,\n oldString: string,\n newString: string,\n replaceAll: boolean = false,\n ): Promise<EditResult> {\n // Escape path for shell\n const escapedPath = filePath.replace(/'/g, \"'\\\\''\");\n\n // Check if file exists\n const checkResult = await this.execute(`test -f '${escapedPath}'`);\n if (checkResult.exitCode !== 0) {\n return { error: `Error: File '${filePath}' not found` };\n }\n\n // Count occurrences first using grep -F (fixed string)\n // Escape old string for grep\n const escapedOldForGrep = oldString.replace(/'/g, \"'\\\\''\");\n const countResult = await this.execute(\n `grep -oF '${escapedOldForGrep}' '${escapedPath}' | wc -l`,\n );\n const count = parseInt(countResult.output.trim(), 10) || 0;\n\n if (count === 0) {\n return { error: `String not found in file '${filePath}'` };\n }\n\n if (count > 1 && !replaceAll) {\n return {\n error: `Multiple occurrences found in '${filePath}'. Use replaceAll=true to replace all.`,\n };\n }\n\n // Perform the replacement using sed\n // Use a delimiter that's unlikely to be in the strings (we'll use \\x00 if available, otherwise |)\n // For safety, we'll use awk which handles arbitrary strings better than sed\n\n // Base64 encode both strings to safely pass them to awk\n const oldB64 = Buffer.from(oldString, \"utf-8\").toString(\"base64\");\n const newB64 = Buffer.from(newString, \"utf-8\").toString(\"base64\");\n\n // Use awk with base64 decoding for safe string replacement\n const awkCommand = `\nOLD=$(echo '${oldB64}' | base64 -d)\nNEW=$(echo '${newB64}' | base64 -d)\nawk -v old=\"$OLD\" -v new=\"$NEW\" -v replace_all=${replaceAll ? 1 : 0} '\n{\n if (replace_all) {\n gsub(old, new)\n } else {\n sub(old, new)\n }\n print\n}\n' '${escapedPath}' > '${escapedPath}.tmp' && mv '${escapedPath}.tmp' '${escapedPath}'\n`;\n\n const editResult = await this.execute(awkCommand);\n\n if (editResult.exitCode !== 0) {\n return { error: `Unknown error editing file '${filePath}'` };\n }\n\n return { path: filePath, filesUpdate: null, occurrences: count };\n }\n\n /**\n * Close the sandbox and release all resources.\n *\n * After closing, the sandbox cannot be used again. Any unsaved data\n * will be lost.\n *\n * @example\n * ```typescript\n * try {\n * await sandbox.execute(\"deno run build.ts\");\n * } finally {\n * await sandbox.close();\n * }\n * ```\n */\n async close(): Promise<void> {\n if (this.#sandbox) {\n try {\n await this.#sandbox.close();\n } finally {\n this.#sandbox = null;\n }\n }\n }\n\n /**\n * Forcefully terminate the sandbox.\n *\n * Use this when you need to immediately stop the sandbox, even if\n * operations are in progress.\n *\n * @example\n * ```typescript\n * await sandbox.kill();\n * ```\n */\n async kill(): Promise<void> {\n if (this.#sandbox) {\n try {\n await this.#sandbox.kill();\n } finally {\n this.#sandbox = null;\n }\n }\n }\n\n /**\n * Alias for close() to maintain compatibility with other sandbox implementations.\n */\n async stop(): Promise<void> {\n await this.close();\n }\n\n /**\n * Set the sandbox from an existing Deno Sandbox instance.\n * Used internally by the static `connect()` method.\n */\n #setFromExisting(existingSandbox: Sandbox, sandboxId: string): void {\n this.#sandbox = existingSandbox;\n this.#id = sandboxId;\n }\n\n /**\n * Map Deno SDK errors to standardized FileOperationError codes.\n *\n * @param error - The error from the Deno SDK\n * @returns A standardized error code\n */\n #mapError(error: unknown): FileOperationError {\n if (error instanceof Error) {\n const msg = error.message.toLowerCase();\n\n if (msg.includes(\"not found\") || msg.includes(\"enoent\")) {\n return \"file_not_found\";\n }\n if (msg.includes(\"permission\") || msg.includes(\"eacces\")) {\n return \"permission_denied\";\n }\n if (msg.includes(\"directory\") || msg.includes(\"eisdir\")) {\n return \"is_directory\";\n }\n }\n\n return \"invalid_path\";\n }\n\n /**\n * Create and initialize a new DenoSandbox in one step.\n *\n * This is the recommended way to create a sandbox. It combines\n * construction and initialization into a single async operation.\n *\n * @param options - Configuration options for the sandbox\n * @returns An initialized and ready-to-use sandbox\n *\n * @example\n * ```typescript\n * const sandbox = await DenoSandbox.create({\n * memoryMb: 1024,\n * lifetime: \"10m\",\n * region: \"iad\",\n * });\n * ```\n */\n static async create(options?: DenoSandboxOptions): Promise<DenoSandbox> {\n const sandbox = new DenoSandbox(options);\n await sandbox.initialize();\n return sandbox;\n }\n\n /**\n * Reconnect to an existing sandbox by ID.\n *\n * This allows you to resume working with a sandbox that was created\n * earlier with a duration-based lifetime.\n *\n * @param id - The ID of the sandbox to reconnect to\n * @param options - Optional auth configuration (for token)\n * @returns A connected sandbox instance\n *\n * @example\n * ```typescript\n * // Resume a sandbox from a stored ID\n * const sandbox = await DenoSandbox.fromId(\"sandbox-abc123\");\n * const result = await sandbox.execute(\"ls -la\");\n * ```\n */\n static async fromId(\n id: string,\n options?: Pick<DenoSandboxOptions, \"auth\">,\n ): Promise<DenoSandbox> {\n // Get authentication credentials\n let credentials: { token: string };\n try {\n credentials = getAuthCredentials(options?.auth);\n } catch (error) {\n throw new DenoSandboxError(\n \"Failed to authenticate with Deno Deploy. Check your token configuration.\",\n \"AUTHENTICATION_FAILED\",\n error instanceof Error ? error : undefined,\n );\n }\n\n try {\n // Set the token in environment for the SDK\n process.env.DENO_DEPLOY_TOKEN = credentials.token;\n\n const existingSandbox = await Sandbox.connect({ id });\n\n const denoSandbox = new DenoSandbox();\n // Set the existing sandbox directly (bypass initialize)\n denoSandbox.#setFromExisting(existingSandbox, id);\n\n return denoSandbox;\n } catch (error) {\n throw new DenoSandboxError(\n `Sandbox not found: ${id}`,\n \"SANDBOX_NOT_FOUND\",\n error instanceof Error ? error : undefined,\n );\n }\n }\n}\n\n/**\n * Async factory function type for creating Deno Sandbox instances.\n *\n * This is similar to BackendFactory but supports async creation,\n * which is required for Deno Sandbox since initialization is async.\n */\nexport type AsyncDenoSandboxFactory = () => Promise<DenoSandbox>;\n\n/**\n * Create an async factory function that creates a new Deno Sandbox per invocation.\n *\n * Each call to the factory will create and initialize a new sandbox.\n * This is useful when you want fresh, isolated environments for each\n * agent invocation.\n *\n * **Important**: This returns an async factory. For use with middleware that\n * requires synchronous BackendFactory, use `createDenoSandboxFactoryFromSandbox()`\n * with a pre-created sandbox instead.\n *\n * @param options - Optional configuration for sandbox creation\n * @returns An async factory function that creates new sandboxes\n *\n * @example\n * ```typescript\n * import { DenoSandbox, createDenoSandboxFactory } from \"@langchain/deno\";\n *\n * // Create a factory for new sandboxes\n * const factory = createDenoSandboxFactory({ memoryMb: 1024 });\n *\n * // Each call creates a new sandbox\n * const sandbox1 = await factory();\n * const sandbox2 = await factory();\n *\n * try {\n * // Use sandboxes...\n * } finally {\n * await sandbox1.close();\n * await sandbox2.close();\n * }\n * ```\n */\nexport function createDenoSandboxFactory(\n options?: DenoSandboxOptions,\n): AsyncDenoSandboxFactory {\n return async () => {\n return await DenoSandbox.create(options);\n };\n}\n\n/**\n * Create a backend factory that reuses an existing Deno Sandbox.\n *\n * This allows multiple agent invocations to share the same sandbox,\n * avoiding the startup overhead of creating new sandboxes.\n *\n * Important: You are responsible for managing the sandbox lifecycle\n * (calling `close()` when done).\n *\n * @param sandbox - An existing DenoSandbox instance (must be initialized)\n * @returns A BackendFactory that returns the provided sandbox\n *\n * @example\n * ```typescript\n * import { createDeepAgent, createFilesystemMiddleware } from \"deepagents\";\n * import { DenoSandbox, createDenoSandboxFactoryFromSandbox } from \"@langchain/deno\";\n *\n * // Create and initialize a sandbox\n * const sandbox = await DenoSandbox.create({ memoryMb: 1024 });\n *\n * try {\n * const agent = createDeepAgent({\n * model: new ChatAnthropic({ model: \"claude-sonnet-4-20250514\" }),\n * systemPrompt: \"You are a coding assistant.\",\n * middlewares: [\n * createFilesystemMiddleware({\n * backend: createDenoSandboxFactoryFromSandbox(sandbox),\n * }),\n * ],\n * });\n *\n * await agent.invoke({ messages: [...] });\n * } finally {\n * await sandbox.close();\n * }\n * ```\n */\nexport function createDenoSandboxFactoryFromSandbox(\n sandbox: DenoSandbox,\n): BackendFactory {\n return () => sandbox;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuDA,SAAgB,aAAa,SAA8C;AAEzE,KAAI,SAAS,MACX,QAAO,QAAQ;CAIjB,MAAM,cAAc,QAAQ,IAAI;AAChC,KAAI,YACF,QAAO;AAIT,OAAM,IAAI,MACR,sWAMD;;;;;;;;;;;AAYH,SAAgB,mBACd,SACiB;AACjB,QAAO,EACL,OAAO,aAAa,QAAQ,EAC7B;;;;;;;;;;;ACqCH,MAAM,4BAA4B,OAAO,IAAI,qBAAqB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8BlE,IAAa,mBAAb,MAAa,yBAAyBA,wBAAa;CACjD,C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,SAAS,SAAS,CACrD,QAAO;;AAIX,SAAO;;;;;;;;;;;;;;;;;;;;CAqBT,aAAa,OAAO,SAAoD;EACtE,MAAM,UAAU,IAAI,YAAY,QAAQ;AACxC,QAAM,QAAQ,YAAY;AAC1B,SAAO;;;;;;;;;;;;;;;;;;;CAoBT,aAAa,OACX,IACA,SACsB;EAEtB,IAAI;AACJ,MAAI;AACF,iBAAc,mBAAmB,SAAS,KAAK;WACxC,OAAO;AACd,SAAM,IAAI,iBACR,4EACA,yBACA,iBAAiB,QAAQ,QAAQ,OAClC;;AAGH,MAAI;AAEF,WAAQ,IAAI,oBAAoB,YAAY;GAE5C,MAAM,kBAAkB,MAAMG,sBAAQ,QAAQ,EAAE,IAAI,CAAC;GAErD,MAAM,cAAc,IAAI,aAAa;AAErC,gBAAYG,gBAAiB,iBAAiB,GAAG;AAEjD,UAAO;WACA,OAAO;AACd,SAAM,IAAI,iBACR,sBAAsB,MACtB,qBACA,iBAAiB,QAAQ,QAAQ,OAClC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8CP,SAAgB,yBACd,SACyB;AACzB,QAAO,YAAY;AACjB,SAAO,MAAM,YAAY,OAAO,QAAQ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyC5C,SAAgB,oCACd,SACgB;AAChB,cAAa"}
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["SandboxError","BaseSandbox","#id","#sandbox","#options","Sandbox","#uploadInitialFiles","#mapError","#setFromExisting"],"sources":["../src/auth.ts","../src/types.ts","../src/sandbox.ts"],"sourcesContent":["/**\n * Authentication utilities for Deno Sandbox.\n *\n * This module provides authentication credential resolution for the Deno Sandbox SDK.\n *\n * @packageDocumentation\n */\n\nimport type { DenoSandboxOptions } from \"./types.js\";\n\n/**\n * Authentication credentials for Deno Sandbox API.\n */\nexport interface DenoCredentials {\n /** Deno Deploy access token */\n token: string;\n}\n\n/**\n * Get the authentication token for Deno Sandbox API.\n *\n * Authentication is resolved in the following priority order:\n *\n * 1. **Explicit token**: If `options.token` is provided, it is used directly.\n * 2. **DENO_DEPLOY_TOKEN**: Environment variable for Deno Deploy access token.\n *\n * If no token is found, an error is thrown with setup instructions.\n *\n * ## Environment Variable Setup\n *\n * ```bash\n * # Go to https://app.deno.com -> Settings -> Organization Tokens\n * # Create a new token and set it as environment variable\n * export DENO_DEPLOY_TOKEN=your_token_here\n * ```\n *\n * @param options - Optional authentication configuration from DenoSandboxOptions\n * @returns The authentication token string\n * @throws {Error} If no authentication token is available\n *\n * @example\n * ```typescript\n * // With explicit token\n * const token = getAuthToken({ token: \"my-token\" });\n *\n * // Using environment variables (auto-detected)\n * const token = getAuthToken();\n *\n * // From DenoSandboxOptions\n * const options: DenoSandboxOptions = {\n * auth: { token: \"my-token\" }\n * };\n * const token = getAuthToken(options.auth);\n * ```\n */\nexport function getAuthToken(options?: DenoSandboxOptions[\"auth\"]): string {\n // Priority 1: Explicit token in options\n if (options?.token) {\n return options.token;\n }\n\n // Priority 2: DENO_DEPLOY_TOKEN environment variable\n const deployToken = process.env.DENO_DEPLOY_TOKEN;\n if (deployToken) {\n return deployToken;\n }\n\n // No token found - throw descriptive error\n throw new Error(\n \"Deno Deploy authentication required. Provide a token using one of these methods:\\n\\n\" +\n \"1. Set DENO_DEPLOY_TOKEN environment variable:\\n\" +\n \" Go to https://app.deno.com -> Settings -> Organization Tokens\\n\" +\n \" Create a new token and run: export DENO_DEPLOY_TOKEN=your_token_here\\n\\n\" +\n \"2. Pass token directly in options:\\n\" +\n \" new DenoSandbox({ auth: { token: '...' } })\",\n );\n}\n\n/**\n * Get authentication credentials for Deno Sandbox API.\n *\n * This function returns the credentials needed for the Deno SDK.\n *\n * @param options - Optional authentication configuration from DenoSandboxOptions\n * @returns Complete authentication credentials\n * @throws {Error} If no authentication token is available\n */\nexport function getAuthCredentials(\n options?: DenoSandboxOptions[\"auth\"],\n): DenoCredentials {\n return {\n token: getAuthToken(options),\n };\n}\n","/**\n * Type definitions for the Deno Sandbox backend.\n *\n * This module contains all type definitions for the @langchain/deno package,\n * including options and error types.\n */\n\nimport { type SandboxErrorCode, SandboxError } from \"deepagents\";\n\n/**\n * Supported regions for Deno Deploy sandboxes.\n *\n * Currently available regions:\n * - `ams`: Amsterdam\n * - `ord`: Chicago\n */\nexport type DenoSandboxRegion = \"ams\" | \"ord\";\n\n/**\n * Sandbox lifetime configuration.\n *\n * - `\"session\"`: Sandbox shuts down when you close/dispose the client (default)\n * - Duration string: Keep sandbox alive for a specific time (e.g., \"5m\", \"30s\")\n */\nexport type SandboxLifetime = \"session\" | `${number}s` | `${number}m`;\n\n/**\n * Configuration options for creating a Deno Sandbox.\n *\n * @example\n * ```typescript\n * const options: DenoSandboxOptions = {\n * memoryMb: 1024, // 1GB memory\n * lifetime: \"5m\", // 5 minutes\n * region: \"iad\", // US East\n * };\n * ```\n */\nexport interface DenoSandboxOptions {\n /**\n * Amount of memory allocated to the sandbox in megabytes.\n *\n * Memory limits:\n * - Minimum: 768MB\n * - Maximum: 4096MB\n *\n * @default 768\n */\n memoryMb?: number;\n\n /**\n * Sandbox lifetime configuration.\n *\n * - `\"session\"`: Sandbox shuts down when you close/dispose the client (default)\n * - Duration string: Keep sandbox alive for a specific time (e.g., \"5m\", \"30s\")\n *\n * Supported duration suffixes: `s` (seconds), `m` (minutes).\n *\n * @default \"session\"\n */\n lifetime?: SandboxLifetime;\n\n /**\n * Region where the sandbox will be created.\n *\n * If not specified, the sandbox will be created in the default region.\n *\n * @see DenoSandboxRegion for available regions\n */\n region?: DenoSandboxRegion;\n\n /**\n * Initial files to create in the sandbox after initialization.\n *\n * A map of file paths to their contents. Files will be created\n * in the sandbox filesystem before any commands are executed.\n * Parent directories are created automatically.\n *\n * @example\n * ```typescript\n * const options: DenoSandboxOptions = {\n * memoryMb: 1024,\n * initialFiles: {\n * \"/home/app/index.js\": \"console.log('Hello')\",\n * \"/home/app/package.json\": '{\"name\": \"test\"}',\n * },\n * };\n * ```\n */\n initialFiles?: Record<string, string>;\n\n /**\n * Authentication configuration for Deno Deploy API.\n *\n * ### Environment Variable Setup\n *\n * ```bash\n * # Go to https://app.deno.com -> Settings -> Organization Tokens\n * # Create a new token and set it as environment variable\n * export DENO_DEPLOY_TOKEN=your_token_here\n * ```\n *\n * Or pass the token directly in this auth configuration.\n */\n auth?: {\n /**\n * Deno Deploy access token.\n * If not provided, reads from `DENO_DEPLOY_TOKEN` environment variable.\n */\n token?: string;\n };\n}\n\n/**\n * Error codes for Deno Sandbox operations.\n *\n * Used to identify specific error conditions and handle them appropriately.\n */\nexport type DenoSandboxErrorCode =\n | SandboxErrorCode\n /** Authentication failed - check token configuration */\n | \"AUTHENTICATION_FAILED\"\n /** Failed to create sandbox - check options and quotas */\n | \"SANDBOX_CREATION_FAILED\"\n /** Sandbox not found - may have been stopped or expired */\n | \"SANDBOX_NOT_FOUND\"\n /** Resource limits exceeded (CPU, memory, storage) */\n | \"RESOURCE_LIMIT_EXCEEDED\";\n\nconst DENO_SANDBOX_ERROR_SYMBOL = Symbol.for(\"deno.sandbox.error\");\n\n/**\n * Custom error class for Deno Sandbox operations.\n *\n * Provides structured error information including:\n * - Human-readable message\n * - Error code for programmatic handling\n * - Original cause for debugging\n *\n * @example\n * ```typescript\n * try {\n * await sandbox.execute(\"some command\");\n * } catch (error) {\n * if (error instanceof DenoSandboxError) {\n * switch (error.code) {\n * case \"NOT_INITIALIZED\":\n * await sandbox.initialize();\n * break;\n * case \"COMMAND_TIMEOUT\":\n * console.error(\"Command took too long\");\n * break;\n * default:\n * throw error;\n * }\n * }\n * }\n * ```\n */\nexport class DenoSandboxError extends SandboxError {\n [DENO_SANDBOX_ERROR_SYMBOL]: true;\n\n /** Error name for instanceof checks and logging */\n override readonly name = \"DenoSandboxError\";\n\n /**\n * Creates a new DenoSandboxError.\n *\n * @param message - Human-readable error description\n * @param code - Structured error code for programmatic handling\n * @param cause - Original error that caused this error (for debugging)\n */\n constructor(\n message: string,\n public readonly code: DenoSandboxErrorCode,\n public override readonly cause?: Error,\n ) {\n super(message, code as SandboxErrorCode, cause);\n // Maintain proper prototype chain for instanceof checks\n Object.setPrototypeOf(this, DenoSandboxError.prototype);\n }\n\n /**\n * Checks if the error is an instance of DenoSandboxError.\n *\n * @param error - The error to check\n * @returns True if the error is an instance of DenoSandboxError, false otherwise\n */\n static isInstance(error: unknown): error is DenoSandboxError {\n return (\n typeof error === \"object\" &&\n error !== null &&\n (error as Record<symbol, unknown>)[DENO_SANDBOX_ERROR_SYMBOL] === true\n );\n }\n}\n","/* eslint-disable no-instanceof/no-instanceof */\n/**\n * Deno Sandbox implementation of the SandboxBackendProtocol.\n *\n * This module provides a Deno Sandbox backend for deepagents, enabling agents\n * to execute commands, read/write files, and manage isolated Linux microVM\n * environments using Deno Deploy's Sandbox infrastructure.\n *\n * @packageDocumentation\n */\n\nimport { Sandbox } from \"@deno/sandbox\";\nimport {\n BaseSandbox,\n type ExecuteResponse,\n type FileDownloadResponse,\n type FileOperationError,\n type FileUploadResponse,\n type BackendFactory,\n} from \"deepagents\";\n\nimport { getAuthCredentials } from \"./auth.js\";\nimport { DenoSandboxError, type DenoSandboxOptions } from \"./types.js\";\n\n/**\n * Deno Sandbox backend for deepagents.\n *\n * Extends `BaseSandbox` to provide command execution, file operations, and\n * sandbox lifecycle management using Deno Deploy's Sandbox SDK.\n *\n * ## Basic Usage\n *\n * ```typescript\n * import { DenoSandbox } from \"@langchain/deno\";\n *\n * // Create and initialize a sandbox\n * const sandbox = await DenoSandbox.create({\n * memoryMb: 1024,\n * lifetime: \"5m\",\n * });\n *\n * try {\n * // Execute commands\n * const result = await sandbox.execute(\"deno --version\");\n * console.log(result.output);\n * } finally {\n * // Always cleanup\n * await sandbox.close();\n * }\n * ```\n *\n * ## Using with DeepAgent\n *\n * ```typescript\n * import { createDeepAgent } from \"deepagents\";\n * import { DenoSandbox } from \"@langchain/deno\";\n *\n * const sandbox = await DenoSandbox.create();\n *\n * const agent = createDeepAgent({\n * model: new ChatAnthropic({ model: \"claude-sonnet-4-20250514\" }),\n * systemPrompt: \"You are a coding assistant with sandbox access.\",\n * backend: sandbox,\n * });\n * ```\n */\nexport class DenoSandbox extends BaseSandbox {\n /** Private reference to the underlying Deno Sandbox instance */\n #sandbox: Sandbox | null = null;\n\n /** Configuration options for this sandbox */\n #options: DenoSandboxOptions;\n\n /** Unique identifier for this sandbox instance */\n #id: string;\n\n /**\n * Get the unique identifier for this sandbox.\n *\n * Before initialization, returns a temporary ID.\n * After initialization, returns the actual Deno sandbox ID.\n */\n get id(): string {\n return this.#id;\n }\n\n /**\n * Get the underlying Deno Sandbox instance.\n *\n * @throws {DenoSandboxError} If the sandbox is not initialized\n *\n * @example\n * ```typescript\n * const sandbox = await DenoSandbox.create();\n * const denoSdk = sandbox.sandbox; // Access the raw SDK\n * ```\n */\n get instance(): Sandbox {\n if (!this.#sandbox) {\n throw new DenoSandboxError(\n \"Sandbox not initialized. Call initialize() or use DenoSandbox.create()\",\n \"NOT_INITIALIZED\",\n );\n }\n return this.#sandbox;\n }\n\n /**\n * Check if the sandbox is initialized and running.\n */\n get isRunning(): boolean {\n return this.#sandbox !== null;\n }\n\n /**\n * Create a new DenoSandbox instance.\n *\n * Note: This only creates the instance. Call `initialize()` to actually\n * create the Deno Sandbox, or use the static `DenoSandbox.create()` method.\n *\n * @param options - Configuration options for the sandbox\n *\n * @example\n * ```typescript\n * // Two-step initialization\n * const sandbox = new DenoSandbox({ memoryMb: 1024 });\n * await sandbox.initialize();\n *\n * // Or use the factory method\n * const sandbox = await DenoSandbox.create({ memoryMb: 1024 });\n * ```\n */\n constructor(options: DenoSandboxOptions = {}) {\n super();\n\n // Set defaults\n this.#options = {\n memoryMb: 768,\n lifetime: \"session\",\n ...options,\n };\n\n // Generate temporary ID until initialized\n this.#id = `deno-sandbox-${Date.now()}`;\n }\n\n /**\n * Initialize the sandbox by creating a new Deno Sandbox instance.\n *\n * This method authenticates with Deno Deploy and provisions a new microVM\n * sandbox. After initialization, the `id` property will reflect the\n * actual Deno sandbox ID.\n *\n * @throws {DenoSandboxError} If already initialized (`ALREADY_INITIALIZED`)\n * @throws {DenoSandboxError} If authentication fails (`AUTHENTICATION_FAILED`)\n * @throws {DenoSandboxError} If sandbox creation fails (`SANDBOX_CREATION_FAILED`)\n *\n * @example\n * ```typescript\n * const sandbox = new DenoSandbox();\n * await sandbox.initialize();\n * console.log(`Sandbox ID: ${sandbox.id}`);\n * ```\n */\n async initialize(): Promise<void> {\n // Prevent double initialization\n if (this.#sandbox) {\n throw new DenoSandboxError(\n \"Sandbox is already initialized. Each DenoSandbox instance can only be initialized once.\",\n \"ALREADY_INITIALIZED\",\n );\n }\n\n // Get authentication credentials\n let credentials: { token: string };\n try {\n credentials = getAuthCredentials(this.#options.auth);\n } catch (error) {\n throw new DenoSandboxError(\n \"Failed to authenticate with Deno Deploy. Check your token configuration.\",\n \"AUTHENTICATION_FAILED\",\n error instanceof Error ? error : undefined,\n );\n }\n\n try {\n // Set the token in environment for the SDK\n process.env.DENO_DEPLOY_TOKEN = credentials.token;\n\n // Build SDK create options\n const createOptions: Parameters<typeof Sandbox.create>[0] = {};\n\n // Add optional memory configuration\n if (this.#options.memoryMb !== undefined) {\n createOptions.memoryMb = this.#options.memoryMb;\n }\n\n // Add optional lifetime configuration\n if (this.#options.lifetime !== undefined) {\n createOptions.lifetime = this.#options.lifetime;\n }\n\n // Add optional region configuration\n if (this.#options.region !== undefined) {\n createOptions.region = this.#options.region;\n }\n\n // Create the sandbox\n this.#sandbox = await Sandbox.create(createOptions);\n\n // Update ID to the actual sandbox ID\n this.#id = this.#sandbox.id;\n\n // Upload initial files if provided\n if (this.#options.initialFiles) {\n await this.#uploadInitialFiles(this.#options.initialFiles);\n }\n } catch (error) {\n throw new DenoSandboxError(\n `Failed to create Deno Sandbox: ${error instanceof Error ? error.message : String(error)}`,\n \"SANDBOX_CREATION_FAILED\",\n error instanceof Error ? error : undefined,\n );\n }\n }\n\n /**\n * Upload initial files to the sandbox.\n *\n * @param files - A map of file paths to their string contents\n */\n async #uploadInitialFiles(files: Record<string, string>): Promise<void> {\n const encoder = new TextEncoder();\n const fileEntries: Array<[string, Uint8Array]> = Object.entries(files).map(\n ([path, content]) => [path, encoder.encode(content)],\n );\n\n const results = await this.uploadFiles(fileEntries);\n\n // Check for any errors during upload\n const errors = results.filter((r) => r.error !== null);\n if (errors.length > 0) {\n const errorPaths = errors.map((e) => `${e.path}: ${e.error}`).join(\", \");\n throw new DenoSandboxError(\n `Failed to upload initial files: ${errorPaths}`,\n \"FILE_OPERATION_FAILED\",\n );\n }\n }\n\n /**\n * Execute a command in the sandbox.\n *\n * Commands are run using the sandbox's shell in the configured working directory.\n *\n * @param command - The shell command to execute\n * @returns Execution result with output, exit code, and truncation flag\n * @throws {DenoSandboxError} If the sandbox is not initialized\n *\n * @example\n * ```typescript\n * const result = await sandbox.execute(\"echo 'Hello World'\");\n * console.log(result.output); // \"Hello World\\n\"\n * console.log(result.exitCode); // 0\n * ```\n */\n async execute(command: string): Promise<ExecuteResponse> {\n const sandbox = this.instance; // Throws if not initialized\n\n try {\n // Use spawn with bash to execute the command\n const child = await sandbox.spawn(\"/bin/bash\", {\n args: [\"-c\", command],\n stdout: \"piped\",\n stderr: \"piped\",\n });\n\n // Use output() to get buffered stdout/stderr\n const { status, stdoutText, stderrText } = await child.output();\n\n return {\n output: (stdoutText ?? \"\") + (stderrText ?? \"\"),\n exitCode: status.code ?? 0,\n truncated: false,\n };\n } catch (error) {\n // Check for timeout\n if (error instanceof Error && error.message.includes(\"timeout\")) {\n throw new DenoSandboxError(\n `Command timed out: ${command}`,\n \"COMMAND_TIMEOUT\",\n error,\n );\n }\n\n throw new DenoSandboxError(\n `Command execution failed: ${error instanceof Error ? error.message : String(error)}`,\n \"COMMAND_FAILED\",\n error instanceof Error ? error : undefined,\n );\n }\n }\n\n /**\n * Upload files to the sandbox.\n *\n * Files are written to the sandbox filesystem. Parent directories are\n * created automatically if they don't exist.\n *\n * @param files - Array of [path, content] tuples to upload\n * @returns Upload result for each file, with success or error status\n *\n * @example\n * ```typescript\n * const encoder = new TextEncoder();\n * const results = await sandbox.uploadFiles([\n * [\"src/index.js\", encoder.encode(\"console.log('Hello')\")],\n * [\"package.json\", encoder.encode('{\"name\": \"test\"}')],\n * ]);\n * ```\n */\n async uploadFiles(\n files: Array<[string, Uint8Array]>,\n ): Promise<FileUploadResponse[]> {\n const sandbox = this.instance; // Throws if not initialized\n const results: FileUploadResponse[] = [];\n\n for (const [path, content] of files) {\n try {\n // Ensure parent directory exists using spawn (more reliable than sh template)\n const parentDir = path.substring(0, path.lastIndexOf(\"/\"));\n if (parentDir) {\n const mkdirChild = await sandbox.spawn(\"/bin/bash\", {\n args: [\"-c\", `mkdir -p \"${parentDir}\"`],\n stdout: \"piped\",\n stderr: \"piped\",\n });\n await mkdirChild.output();\n }\n\n // Write the file content\n const textContent = new TextDecoder().decode(content);\n await sandbox.writeTextFile(path, textContent);\n results.push({ path, error: null });\n } catch (error) {\n results.push({ path, error: this.#mapError(error) });\n }\n }\n\n return results;\n }\n\n /**\n * Download files from the sandbox.\n *\n * Each file is read individually, allowing partial success when some\n * files exist and others don't.\n *\n * @param paths - Array of file paths to download\n * @returns Download result for each file, with content or error\n *\n * @example\n * ```typescript\n * const results = await sandbox.downloadFiles([\"src/index.js\", \"missing.txt\"]);\n * for (const result of results) {\n * if (result.content) {\n * console.log(new TextDecoder().decode(result.content));\n * } else {\n * console.error(`Error: ${result.error}`);\n * }\n * }\n * ```\n */\n async downloadFiles(paths: string[]): Promise<FileDownloadResponse[]> {\n const sandbox = this.instance; // Throws if not initialized\n const results: FileDownloadResponse[] = [];\n\n for (const path of paths) {\n try {\n // Use spawn with bash to read file content (same approach as execute())\n const child = await sandbox.spawn(\"/bin/bash\", {\n args: [\"-c\", `cat \"${path}\"`],\n stdout: \"piped\",\n stderr: \"piped\",\n });\n\n const { status, stdoutText } = await child.output();\n\n if (!status.success) {\n results.push({\n path,\n content: null,\n error: \"file_not_found\",\n });\n } else {\n const content = new TextEncoder().encode(stdoutText ?? \"\");\n results.push({\n path,\n content,\n error: null,\n });\n }\n } catch (error) {\n results.push({\n path,\n content: null,\n error: this.#mapError(error),\n });\n }\n }\n\n return results;\n }\n\n /**\n * Close the sandbox and release all resources.\n *\n * After closing, the sandbox cannot be used again. Any unsaved data\n * will be lost.\n *\n * @example\n * ```typescript\n * try {\n * await sandbox.execute(\"deno run build.ts\");\n * } finally {\n * await sandbox.close();\n * }\n * ```\n */\n async close(): Promise<void> {\n if (this.#sandbox) {\n try {\n await this.#sandbox.close();\n } finally {\n this.#sandbox = null;\n }\n }\n }\n\n /**\n * Forcefully terminate the sandbox.\n *\n * Use this when you need to immediately stop the sandbox, even if\n * operations are in progress.\n *\n * @example\n * ```typescript\n * await sandbox.kill();\n * ```\n */\n async kill(): Promise<void> {\n if (this.#sandbox) {\n try {\n await this.#sandbox.kill();\n } finally {\n this.#sandbox = null;\n }\n }\n }\n\n /**\n * Alias for close() to maintain compatibility with other sandbox implementations.\n */\n async stop(): Promise<void> {\n await this.close();\n }\n\n /**\n * Set the sandbox from an existing Deno Sandbox instance.\n * Used internally by the static `connect()` method.\n */\n #setFromExisting(existingSandbox: Sandbox, sandboxId: string): void {\n this.#sandbox = existingSandbox;\n this.#id = sandboxId;\n }\n\n /**\n * Map Deno SDK errors to standardized FileOperationError codes.\n *\n * @param error - The error from the Deno SDK\n * @returns A standardized error code\n */\n #mapError(error: unknown): FileOperationError {\n if (error instanceof Error) {\n const msg = error.message.toLowerCase();\n\n if (msg.includes(\"not found\") || msg.includes(\"enoent\")) {\n return \"file_not_found\";\n }\n if (msg.includes(\"permission\") || msg.includes(\"eacces\")) {\n return \"permission_denied\";\n }\n if (msg.includes(\"directory\") || msg.includes(\"eisdir\")) {\n return \"is_directory\";\n }\n }\n\n return \"invalid_path\";\n }\n\n /**\n * Create and initialize a new DenoSandbox in one step.\n *\n * This is the recommended way to create a sandbox. It combines\n * construction and initialization into a single async operation.\n *\n * @param options - Configuration options for the sandbox\n * @returns An initialized and ready-to-use sandbox\n *\n * @example\n * ```typescript\n * const sandbox = await DenoSandbox.create({\n * memoryMb: 1024,\n * lifetime: \"10m\",\n * region: \"iad\",\n * });\n * ```\n */\n static async create(options?: DenoSandboxOptions): Promise<DenoSandbox> {\n const sandbox = new DenoSandbox(options);\n await sandbox.initialize();\n return sandbox;\n }\n\n /**\n * Reconnect to an existing sandbox by ID.\n *\n * This allows you to resume working with a sandbox that was created\n * earlier with a duration-based lifetime.\n *\n * @param id - The ID of the sandbox to reconnect to\n * @param options - Optional auth configuration (for token)\n * @returns A connected sandbox instance\n *\n * @example\n * ```typescript\n * // Resume a sandbox from a stored ID\n * const sandbox = await DenoSandbox.fromId(\"sandbox-abc123\");\n * const result = await sandbox.execute(\"ls -la\");\n * ```\n */\n static async fromId(\n id: string,\n options?: Pick<DenoSandboxOptions, \"auth\">,\n ): Promise<DenoSandbox> {\n // Get authentication credentials\n let credentials: { token: string };\n try {\n credentials = getAuthCredentials(options?.auth);\n } catch (error) {\n throw new DenoSandboxError(\n \"Failed to authenticate with Deno Deploy. Check your token configuration.\",\n \"AUTHENTICATION_FAILED\",\n error instanceof Error ? error : undefined,\n );\n }\n\n try {\n // Set the token in environment for the SDK\n process.env.DENO_DEPLOY_TOKEN = credentials.token;\n\n const existingSandbox = await Sandbox.connect({ id });\n\n const denoSandbox = new DenoSandbox();\n // Set the existing sandbox directly (bypass initialize)\n denoSandbox.#setFromExisting(existingSandbox, id);\n\n return denoSandbox;\n } catch (error) {\n throw new DenoSandboxError(\n `Sandbox not found: ${id}`,\n \"SANDBOX_NOT_FOUND\",\n error instanceof Error ? error : undefined,\n );\n }\n }\n}\n\n/**\n * Async factory function type for creating Deno Sandbox instances.\n *\n * This is similar to BackendFactory but supports async creation,\n * which is required for Deno Sandbox since initialization is async.\n */\nexport type AsyncDenoSandboxFactory = () => Promise<DenoSandbox>;\n\n/**\n * Create an async factory function that creates a new Deno Sandbox per invocation.\n *\n * Each call to the factory will create and initialize a new sandbox.\n * This is useful when you want fresh, isolated environments for each\n * agent invocation.\n *\n * **Important**: This returns an async factory. For use with middleware that\n * requires synchronous BackendFactory, use `createDenoSandboxFactoryFromSandbox()`\n * with a pre-created sandbox instead.\n *\n * @param options - Optional configuration for sandbox creation\n * @returns An async factory function that creates new sandboxes\n *\n * @example\n * ```typescript\n * import { DenoSandbox, createDenoSandboxFactory } from \"@langchain/deno\";\n *\n * // Create a factory for new sandboxes\n * const factory = createDenoSandboxFactory({ memoryMb: 1024 });\n *\n * // Each call creates a new sandbox\n * const sandbox1 = await factory();\n * const sandbox2 = await factory();\n *\n * try {\n * // Use sandboxes...\n * } finally {\n * await sandbox1.close();\n * await sandbox2.close();\n * }\n * ```\n */\nexport function createDenoSandboxFactory(\n options?: DenoSandboxOptions,\n): AsyncDenoSandboxFactory {\n return async () => {\n return await DenoSandbox.create(options);\n };\n}\n\n/**\n * Create a backend factory that reuses an existing Deno Sandbox.\n *\n * This allows multiple agent invocations to share the same sandbox,\n * avoiding the startup overhead of creating new sandboxes.\n *\n * Important: You are responsible for managing the sandbox lifecycle\n * (calling `close()` when done).\n *\n * @param sandbox - An existing DenoSandbox instance (must be initialized)\n * @returns A BackendFactory that returns the provided sandbox\n *\n * @example\n * ```typescript\n * import { createDeepAgent, createFilesystemMiddleware } from \"deepagents\";\n * import { DenoSandbox, createDenoSandboxFactoryFromSandbox } from \"@langchain/deno\";\n *\n * // Create and initialize a sandbox\n * const sandbox = await DenoSandbox.create({ memoryMb: 1024 });\n *\n * try {\n * const agent = createDeepAgent({\n * model: new ChatAnthropic({ model: \"claude-sonnet-4-20250514\" }),\n * systemPrompt: \"You are a coding assistant.\",\n * middlewares: [\n * createFilesystemMiddleware({\n * backend: createDenoSandboxFactoryFromSandbox(sandbox),\n * }),\n * ],\n * });\n *\n * await agent.invoke({ messages: [...] });\n * } finally {\n * await sandbox.close();\n * }\n * ```\n */\nexport function createDenoSandboxFactoryFromSandbox(\n sandbox: DenoSandbox,\n): B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C1B,SAAO;;;;;;;;;;;;;;;;;;;CAoBT,aAAa,OACX,IACA,SACsB;EAEtB,IAAI;AACJ,MAAI;AACF,iBAAc,mBAAmB,SAAS,KAAK;WACxC,OAAO;AACd,SAAM,IAAI,iBACR,4EACA,yBACA,iBAAiB,QAAQ,QAAQ,OAClC;;AAGH,MAAI;AAEF,WAAQ,IAAI,oBAAoB,YAAY;GAE5C,MAAM,kBAAkB,MAAMG,sBAAQ,QAAQ,EAAE,IAAI,CAAC;GAErD,MAAM,cAAc,IAAI,aAAa;AAErC,gBAAYG,gBAAiB,iBAAiB,GAAG;AAEjD,UAAO;WACA,OAAO;AACd,SAAM,IAAI,iBACR,sBAAsB,MACtB,qBACA,iBAAiB,QAAQ,QAAQ,OAClC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8CP,SAAgB,yBACd,SACyB;AACzB,QAAO,YAAY;AACjB,SAAO,MAAM,YAAY,OAAO,QAAQ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyC5C,SAAgB,oCACd,SACgB;AAChB,cAAa"}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Sandbox } from "@deno/sandbox";
|
|
2
|
-
import { BackendFactory, BaseSandbox,
|
|
2
|
+
import { BackendFactory, BaseSandbox, ExecuteResponse, FileDownloadResponse, FileUploadResponse, SandboxError, SandboxErrorCode } from "deepagents";
|
|
3
3
|
|
|
4
4
|
//#region src/types.d.ts
|
|
5
5
|
/**
|
|
@@ -321,44 +321,6 @@ declare class DenoSandbox extends BaseSandbox {
|
|
|
321
321
|
* ```
|
|
322
322
|
*/
|
|
323
323
|
downloadFiles(paths: string[]): Promise<FileDownloadResponse[]>;
|
|
324
|
-
/**
|
|
325
|
-
* Read a file's content with line numbers.
|
|
326
|
-
*
|
|
327
|
-
* Override of BaseSandbox.read() to use awk instead of Python,
|
|
328
|
-
* since Deno sandboxes don't have Python installed.
|
|
329
|
-
*
|
|
330
|
-
* @param filePath - Absolute path to the file
|
|
331
|
-
* @param offset - Line offset (0-indexed, default 0)
|
|
332
|
-
* @param limit - Maximum lines to return (default 500)
|
|
333
|
-
* @returns Formatted file content with line numbers, or error message
|
|
334
|
-
*/
|
|
335
|
-
read(filePath: string, offset?: number, limit?: number): Promise<string>;
|
|
336
|
-
/**
|
|
337
|
-
* Create a new file with content.
|
|
338
|
-
*
|
|
339
|
-
* Override of BaseSandbox.write() to use shell commands instead of Python,
|
|
340
|
-
* since Deno sandboxes don't have Python installed.
|
|
341
|
-
*
|
|
342
|
-
* @param filePath - Absolute path for the new file
|
|
343
|
-
* @param content - File content to write
|
|
344
|
-
* @returns WriteResult with error populated on failure
|
|
345
|
-
*/
|
|
346
|
-
write(filePath: string, content: string): Promise<WriteResult>;
|
|
347
|
-
/**
|
|
348
|
-
* Edit a file by replacing string occurrences.
|
|
349
|
-
*
|
|
350
|
-
* Override of BaseSandbox.edit() to use shell commands instead of Python,
|
|
351
|
-
* since Deno sandboxes don't have Python installed.
|
|
352
|
-
*
|
|
353
|
-
* Uses sed for in-place replacement with proper escaping.
|
|
354
|
-
*
|
|
355
|
-
* @param filePath - Absolute path to the file
|
|
356
|
-
* @param oldString - String to find and replace
|
|
357
|
-
* @param newString - Replacement string
|
|
358
|
-
* @param replaceAll - If true, replace all occurrences (default: false)
|
|
359
|
-
* @returns EditResult with error, path, and occurrences
|
|
360
|
-
*/
|
|
361
|
-
edit(filePath: string, oldString: string, newString: string, replaceAll?: boolean): Promise<EditResult>;
|
|
362
324
|
/**
|
|
363
325
|
* Close the sandbox and release all resources.
|
|
364
326
|
*
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Sandbox } from "@deno/sandbox";
|
|
2
|
-
import { BackendFactory, BaseSandbox,
|
|
2
|
+
import { BackendFactory, BaseSandbox, ExecuteResponse, FileDownloadResponse, FileUploadResponse, SandboxError, SandboxErrorCode } from "deepagents";
|
|
3
3
|
|
|
4
4
|
//#region src/types.d.ts
|
|
5
5
|
/**
|
|
@@ -321,44 +321,6 @@ declare class DenoSandbox extends BaseSandbox {
|
|
|
321
321
|
* ```
|
|
322
322
|
*/
|
|
323
323
|
downloadFiles(paths: string[]): Promise<FileDownloadResponse[]>;
|
|
324
|
-
/**
|
|
325
|
-
* Read a file's content with line numbers.
|
|
326
|
-
*
|
|
327
|
-
* Override of BaseSandbox.read() to use awk instead of Python,
|
|
328
|
-
* since Deno sandboxes don't have Python installed.
|
|
329
|
-
*
|
|
330
|
-
* @param filePath - Absolute path to the file
|
|
331
|
-
* @param offset - Line offset (0-indexed, default 0)
|
|
332
|
-
* @param limit - Maximum lines to return (default 500)
|
|
333
|
-
* @returns Formatted file content with line numbers, or error message
|
|
334
|
-
*/
|
|
335
|
-
read(filePath: string, offset?: number, limit?: number): Promise<string>;
|
|
336
|
-
/**
|
|
337
|
-
* Create a new file with content.
|
|
338
|
-
*
|
|
339
|
-
* Override of BaseSandbox.write() to use shell commands instead of Python,
|
|
340
|
-
* since Deno sandboxes don't have Python installed.
|
|
341
|
-
*
|
|
342
|
-
* @param filePath - Absolute path for the new file
|
|
343
|
-
* @param content - File content to write
|
|
344
|
-
* @returns WriteResult with error populated on failure
|
|
345
|
-
*/
|
|
346
|
-
write(filePath: string, content: string): Promise<WriteResult>;
|
|
347
|
-
/**
|
|
348
|
-
* Edit a file by replacing string occurrences.
|
|
349
|
-
*
|
|
350
|
-
* Override of BaseSandbox.edit() to use shell commands instead of Python,
|
|
351
|
-
* since Deno sandboxes don't have Python installed.
|
|
352
|
-
*
|
|
353
|
-
* Uses sed for in-place replacement with proper escaping.
|
|
354
|
-
*
|
|
355
|
-
* @param filePath - Absolute path to the file
|
|
356
|
-
* @param oldString - String to find and replace
|
|
357
|
-
* @param newString - Replacement string
|
|
358
|
-
* @param replaceAll - If true, replace all occurrences (default: false)
|
|
359
|
-
* @returns EditResult with error, path, and occurrences
|
|
360
|
-
*/
|
|
361
|
-
edit(filePath: string, oldString: string, newString: string, replaceAll?: boolean): Promise<EditResult>;
|
|
362
324
|
/**
|
|
363
325
|
* Close the sandbox and release all resources.
|
|
364
326
|
*
|
package/dist/index.js
CHANGED
|
@@ -419,104 +419,6 @@ var DenoSandbox = class DenoSandbox extends BaseSandbox {
|
|
|
419
419
|
return results;
|
|
420
420
|
}
|
|
421
421
|
/**
|
|
422
|
-
* Read a file's content with line numbers.
|
|
423
|
-
*
|
|
424
|
-
* Override of BaseSandbox.read() to use awk instead of Python,
|
|
425
|
-
* since Deno sandboxes don't have Python installed.
|
|
426
|
-
*
|
|
427
|
-
* @param filePath - Absolute path to the file
|
|
428
|
-
* @param offset - Line offset (0-indexed, default 0)
|
|
429
|
-
* @param limit - Maximum lines to return (default 500)
|
|
430
|
-
* @returns Formatted file content with line numbers, or error message
|
|
431
|
-
*/
|
|
432
|
-
async read(filePath, offset = 0, limit = 500) {
|
|
433
|
-
const safeOffset = Number.isFinite(offset) && offset > 0 ? Math.floor(offset) : 0;
|
|
434
|
-
const safeLimit = Number.isFinite(limit) && limit > 0 && limit < Number.MAX_SAFE_INTEGER ? Math.floor(limit) : 500;
|
|
435
|
-
const escapedPath = filePath.replace(/'/g, "'\\''");
|
|
436
|
-
const command = `
|
|
437
|
-
if [ ! -f '${escapedPath}' ]; then
|
|
438
|
-
echo "Error: File not found"
|
|
439
|
-
exit 1
|
|
440
|
-
fi
|
|
441
|
-
if [ ! -s '${escapedPath}' ]; then
|
|
442
|
-
echo "System reminder: File exists but has empty contents"
|
|
443
|
-
exit 0
|
|
444
|
-
fi
|
|
445
|
-
awk -v offset=${safeOffset} -v limit=${safeLimit} '
|
|
446
|
-
NR > offset && NR <= offset + limit {
|
|
447
|
-
printf "%6d\\t%s\\n", NR, $0
|
|
448
|
-
}
|
|
449
|
-
' '${escapedPath}'
|
|
450
|
-
`;
|
|
451
|
-
const result = await this.execute(command);
|
|
452
|
-
if (result.exitCode !== 0) return `Error: File '${filePath}' not found`;
|
|
453
|
-
return result.output;
|
|
454
|
-
}
|
|
455
|
-
/**
|
|
456
|
-
* Create a new file with content.
|
|
457
|
-
*
|
|
458
|
-
* Override of BaseSandbox.write() to use shell commands instead of Python,
|
|
459
|
-
* since Deno sandboxes don't have Python installed.
|
|
460
|
-
*
|
|
461
|
-
* @param filePath - Absolute path for the new file
|
|
462
|
-
* @param content - File content to write
|
|
463
|
-
* @returns WriteResult with error populated on failure
|
|
464
|
-
*/
|
|
465
|
-
async write(filePath, content) {
|
|
466
|
-
const escapedPath = filePath.replace(/'/g, "'\\''");
|
|
467
|
-
if ((await this.execute(`test -f '${escapedPath}'`)).exitCode === 0) return { error: `Cannot write to ${filePath} because it already exists. Read and then make an edit, or write to a new path.` };
|
|
468
|
-
const encoder = new TextEncoder();
|
|
469
|
-
const uploadResult = await this.uploadFiles([[filePath, encoder.encode(content)]]);
|
|
470
|
-
if (uploadResult[0]?.error) return { error: `Failed to write file: ${uploadResult[0].error}` };
|
|
471
|
-
return {
|
|
472
|
-
path: filePath,
|
|
473
|
-
filesUpdate: null
|
|
474
|
-
};
|
|
475
|
-
}
|
|
476
|
-
/**
|
|
477
|
-
* Edit a file by replacing string occurrences.
|
|
478
|
-
*
|
|
479
|
-
* Override of BaseSandbox.edit() to use shell commands instead of Python,
|
|
480
|
-
* since Deno sandboxes don't have Python installed.
|
|
481
|
-
*
|
|
482
|
-
* Uses sed for in-place replacement with proper escaping.
|
|
483
|
-
*
|
|
484
|
-
* @param filePath - Absolute path to the file
|
|
485
|
-
* @param oldString - String to find and replace
|
|
486
|
-
* @param newString - Replacement string
|
|
487
|
-
* @param replaceAll - If true, replace all occurrences (default: false)
|
|
488
|
-
* @returns EditResult with error, path, and occurrences
|
|
489
|
-
*/
|
|
490
|
-
async edit(filePath, oldString, newString, replaceAll = false) {
|
|
491
|
-
const escapedPath = filePath.replace(/'/g, "'\\''");
|
|
492
|
-
if ((await this.execute(`test -f '${escapedPath}'`)).exitCode !== 0) return { error: `Error: File '${filePath}' not found` };
|
|
493
|
-
const escapedOldForGrep = oldString.replace(/'/g, "'\\''");
|
|
494
|
-
const countResult = await this.execute(`grep -oF '${escapedOldForGrep}' '${escapedPath}' | wc -l`);
|
|
495
|
-
const count = parseInt(countResult.output.trim(), 10) || 0;
|
|
496
|
-
if (count === 0) return { error: `String not found in file '${filePath}'` };
|
|
497
|
-
if (count > 1 && !replaceAll) return { error: `Multiple occurrences found in '${filePath}'. Use replaceAll=true to replace all.` };
|
|
498
|
-
const awkCommand = `
|
|
499
|
-
OLD=$(echo '${Buffer.from(oldString, "utf-8").toString("base64")}' | base64 -d)
|
|
500
|
-
NEW=$(echo '${Buffer.from(newString, "utf-8").toString("base64")}' | base64 -d)
|
|
501
|
-
awk -v old="$OLD" -v new="$NEW" -v replace_all=${replaceAll ? 1 : 0} '
|
|
502
|
-
{
|
|
503
|
-
if (replace_all) {
|
|
504
|
-
gsub(old, new)
|
|
505
|
-
} else {
|
|
506
|
-
sub(old, new)
|
|
507
|
-
}
|
|
508
|
-
print
|
|
509
|
-
}
|
|
510
|
-
' '${escapedPath}' > '${escapedPath}.tmp' && mv '${escapedPath}.tmp' '${escapedPath}'
|
|
511
|
-
`;
|
|
512
|
-
if ((await this.execute(awkCommand)).exitCode !== 0) return { error: `Unknown error editing file '${filePath}'` };
|
|
513
|
-
return {
|
|
514
|
-
path: filePath,
|
|
515
|
-
filesUpdate: null,
|
|
516
|
-
occurrences: count
|
|
517
|
-
};
|
|
518
|
-
}
|
|
519
|
-
/**
|
|
520
422
|
* Close the sandbox and release all resources.
|
|
521
423
|
*
|
|
522
424
|
* After closing, the sandbox cannot be used again. Any unsaved data
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["#id","#sandbox","#options","#uploadInitialFiles","#mapError","#setFromExisting"],"sources":["../src/auth.ts","../src/types.ts","../src/sandbox.ts"],"sourcesContent":["/**\n * Authentication utilities for Deno Sandbox.\n *\n * This module provides authentication credential resolution for the Deno Sandbox SDK.\n *\n * @packageDocumentation\n */\n\nimport type { DenoSandboxOptions } from \"./types.js\";\n\n/**\n * Authentication credentials for Deno Sandbox API.\n */\nexport interface DenoCredentials {\n /** Deno Deploy access token */\n token: string;\n}\n\n/**\n * Get the authentication token for Deno Sandbox API.\n *\n * Authentication is resolved in the following priority order:\n *\n * 1. **Explicit token**: If `options.token` is provided, it is used directly.\n * 2. **DENO_DEPLOY_TOKEN**: Environment variable for Deno Deploy access token.\n *\n * If no token is found, an error is thrown with setup instructions.\n *\n * ## Environment Variable Setup\n *\n * ```bash\n * # Go to https://app.deno.com -> Settings -> Organization Tokens\n * # Create a new token and set it as environment variable\n * export DENO_DEPLOY_TOKEN=your_token_here\n * ```\n *\n * @param options - Optional authentication configuration from DenoSandboxOptions\n * @returns The authentication token string\n * @throws {Error} If no authentication token is available\n *\n * @example\n * ```typescript\n * // With explicit token\n * const token = getAuthToken({ token: \"my-token\" });\n *\n * // Using environment variables (auto-detected)\n * const token = getAuthToken();\n *\n * // From DenoSandboxOptions\n * const options: DenoSandboxOptions = {\n * auth: { token: \"my-token\" }\n * };\n * const token = getAuthToken(options.auth);\n * ```\n */\nexport function getAuthToken(options?: DenoSandboxOptions[\"auth\"]): string {\n // Priority 1: Explicit token in options\n if (options?.token) {\n return options.token;\n }\n\n // Priority 2: DENO_DEPLOY_TOKEN environment variable\n const deployToken = process.env.DENO_DEPLOY_TOKEN;\n if (deployToken) {\n return deployToken;\n }\n\n // No token found - throw descriptive error\n throw new Error(\n \"Deno Deploy authentication required. Provide a token using one of these methods:\\n\\n\" +\n \"1. Set DENO_DEPLOY_TOKEN environment variable:\\n\" +\n \" Go to https://app.deno.com -> Settings -> Organization Tokens\\n\" +\n \" Create a new token and run: export DENO_DEPLOY_TOKEN=your_token_here\\n\\n\" +\n \"2. Pass token directly in options:\\n\" +\n \" new DenoSandbox({ auth: { token: '...' } })\",\n );\n}\n\n/**\n * Get authentication credentials for Deno Sandbox API.\n *\n * This function returns the credentials needed for the Deno SDK.\n *\n * @param options - Optional authentication configuration from DenoSandboxOptions\n * @returns Complete authentication credentials\n * @throws {Error} If no authentication token is available\n */\nexport function getAuthCredentials(\n options?: DenoSandboxOptions[\"auth\"],\n): DenoCredentials {\n return {\n token: getAuthToken(options),\n };\n}\n","/**\n * Type definitions for the Deno Sandbox backend.\n *\n * This module contains all type definitions for the @langchain/deno package,\n * including options and error types.\n */\n\nimport { type SandboxErrorCode, SandboxError } from \"deepagents\";\n\n/**\n * Supported regions for Deno Deploy sandboxes.\n *\n * Currently available regions:\n * - `ams`: Amsterdam\n * - `ord`: Chicago\n */\nexport type DenoSandboxRegion = \"ams\" | \"ord\";\n\n/**\n * Sandbox lifetime configuration.\n *\n * - `\"session\"`: Sandbox shuts down when you close/dispose the client (default)\n * - Duration string: Keep sandbox alive for a specific time (e.g., \"5m\", \"30s\")\n */\nexport type SandboxLifetime = \"session\" | `${number}s` | `${number}m`;\n\n/**\n * Configuration options for creating a Deno Sandbox.\n *\n * @example\n * ```typescript\n * const options: DenoSandboxOptions = {\n * memoryMb: 1024, // 1GB memory\n * lifetime: \"5m\", // 5 minutes\n * region: \"iad\", // US East\n * };\n * ```\n */\nexport interface DenoSandboxOptions {\n /**\n * Amount of memory allocated to the sandbox in megabytes.\n *\n * Memory limits:\n * - Minimum: 768MB\n * - Maximum: 4096MB\n *\n * @default 768\n */\n memoryMb?: number;\n\n /**\n * Sandbox lifetime configuration.\n *\n * - `\"session\"`: Sandbox shuts down when you close/dispose the client (default)\n * - Duration string: Keep sandbox alive for a specific time (e.g., \"5m\", \"30s\")\n *\n * Supported duration suffixes: `s` (seconds), `m` (minutes).\n *\n * @default \"session\"\n */\n lifetime?: SandboxLifetime;\n\n /**\n * Region where the sandbox will be created.\n *\n * If not specified, the sandbox will be created in the default region.\n *\n * @see DenoSandboxRegion for available regions\n */\n region?: DenoSandboxRegion;\n\n /**\n * Initial files to create in the sandbox after initialization.\n *\n * A map of file paths to their contents. Files will be created\n * in the sandbox filesystem before any commands are executed.\n * Parent directories are created automatically.\n *\n * @example\n * ```typescript\n * const options: DenoSandboxOptions = {\n * memoryMb: 1024,\n * initialFiles: {\n * \"/home/app/index.js\": \"console.log('Hello')\",\n * \"/home/app/package.json\": '{\"name\": \"test\"}',\n * },\n * };\n * ```\n */\n initialFiles?: Record<string, string>;\n\n /**\n * Authentication configuration for Deno Deploy API.\n *\n * ### Environment Variable Setup\n *\n * ```bash\n * # Go to https://app.deno.com -> Settings -> Organization Tokens\n * # Create a new token and set it as environment variable\n * export DENO_DEPLOY_TOKEN=your_token_here\n * ```\n *\n * Or pass the token directly in this auth configuration.\n */\n auth?: {\n /**\n * Deno Deploy access token.\n * If not provided, reads from `DENO_DEPLOY_TOKEN` environment variable.\n */\n token?: string;\n };\n}\n\n/**\n * Error codes for Deno Sandbox operations.\n *\n * Used to identify specific error conditions and handle them appropriately.\n */\nexport type DenoSandboxErrorCode =\n | SandboxErrorCode\n /** Authentication failed - check token configuration */\n | \"AUTHENTICATION_FAILED\"\n /** Failed to create sandbox - check options and quotas */\n | \"SANDBOX_CREATION_FAILED\"\n /** Sandbox not found - may have been stopped or expired */\n | \"SANDBOX_NOT_FOUND\"\n /** Resource limits exceeded (CPU, memory, storage) */\n | \"RESOURCE_LIMIT_EXCEEDED\";\n\nconst DENO_SANDBOX_ERROR_SYMBOL = Symbol.for(\"deno.sandbox.error\");\n\n/**\n * Custom error class for Deno Sandbox operations.\n *\n * Provides structured error information including:\n * - Human-readable message\n * - Error code for programmatic handling\n * - Original cause for debugging\n *\n * @example\n * ```typescript\n * try {\n * await sandbox.execute(\"some command\");\n * } catch (error) {\n * if (error instanceof DenoSandboxError) {\n * switch (error.code) {\n * case \"NOT_INITIALIZED\":\n * await sandbox.initialize();\n * break;\n * case \"COMMAND_TIMEOUT\":\n * console.error(\"Command took too long\");\n * break;\n * default:\n * throw error;\n * }\n * }\n * }\n * ```\n */\nexport class DenoSandboxError extends SandboxError {\n [DENO_SANDBOX_ERROR_SYMBOL]: true;\n\n /** Error name for instanceof checks and logging */\n override readonly name = \"DenoSandboxError\";\n\n /**\n * Creates a new DenoSandboxError.\n *\n * @param message - Human-readable error description\n * @param code - Structured error code for programmatic handling\n * @param cause - Original error that caused this error (for debugging)\n */\n constructor(\n message: string,\n public readonly code: DenoSandboxErrorCode,\n public override readonly cause?: Error,\n ) {\n super(message, code as SandboxErrorCode, cause);\n // Maintain proper prototype chain for instanceof checks\n Object.setPrototypeOf(this, DenoSandboxError.prototype);\n }\n\n /**\n * Checks if the error is an instance of DenoSandboxError.\n *\n * @param error - The error to check\n * @returns True if the error is an instance of DenoSandboxError, false otherwise\n */\n static isInstance(error: unknown): error is DenoSandboxError {\n return (\n typeof error === \"object\" &&\n error !== null &&\n (error as Record<symbol, unknown>)[DENO_SANDBOX_ERROR_SYMBOL] === true\n );\n }\n}\n","/* eslint-disable no-instanceof/no-instanceof */\n/**\n * Deno Sandbox implementation of the SandboxBackendProtocol.\n *\n * This module provides a Deno Sandbox backend for deepagents, enabling agents\n * to execute commands, read/write files, and manage isolated Linux microVM\n * environments using Deno Deploy's Sandbox infrastructure.\n *\n * @packageDocumentation\n */\n\nimport { Sandbox } from \"@deno/sandbox\";\nimport {\n BaseSandbox,\n type EditResult,\n type ExecuteResponse,\n type FileDownloadResponse,\n type FileOperationError,\n type FileUploadResponse,\n type BackendFactory,\n type WriteResult,\n} from \"deepagents\";\n\nimport { getAuthCredentials } from \"./auth.js\";\nimport { DenoSandboxError, type DenoSandboxOptions } from \"./types.js\";\n\n/**\n * Deno Sandbox backend for deepagents.\n *\n * Extends `BaseSandbox` to provide command execution, file operations, and\n * sandbox lifecycle management using Deno Deploy's Sandbox SDK.\n *\n * ## Basic Usage\n *\n * ```typescript\n * import { DenoSandbox } from \"@langchain/deno\";\n *\n * // Create and initialize a sandbox\n * const sandbox = await DenoSandbox.create({\n * memoryMb: 1024,\n * lifetime: \"5m\",\n * });\n *\n * try {\n * // Execute commands\n * const result = await sandbox.execute(\"deno --version\");\n * console.log(result.output);\n * } finally {\n * // Always cleanup\n * await sandbox.close();\n * }\n * ```\n *\n * ## Using with DeepAgent\n *\n * ```typescript\n * import { createDeepAgent } from \"deepagents\";\n * import { DenoSandbox } from \"@langchain/deno\";\n *\n * const sandbox = await DenoSandbox.create();\n *\n * const agent = createDeepAgent({\n * model: new ChatAnthropic({ model: \"claude-sonnet-4-20250514\" }),\n * systemPrompt: \"You are a coding assistant with sandbox access.\",\n * backend: sandbox,\n * });\n * ```\n */\nexport class DenoSandbox extends BaseSandbox {\n /** Private reference to the underlying Deno Sandbox instance */\n #sandbox: Sandbox | null = null;\n\n /** Configuration options for this sandbox */\n #options: DenoSandboxOptions;\n\n /** Unique identifier for this sandbox instance */\n #id: string;\n\n /**\n * Get the unique identifier for this sandbox.\n *\n * Before initialization, returns a temporary ID.\n * After initialization, returns the actual Deno sandbox ID.\n */\n get id(): string {\n return this.#id;\n }\n\n /**\n * Get the underlying Deno Sandbox instance.\n *\n * @throws {DenoSandboxError} If the sandbox is not initialized\n *\n * @example\n * ```typescript\n * const sandbox = await DenoSandbox.create();\n * const denoSdk = sandbox.sandbox; // Access the raw SDK\n * ```\n */\n get instance(): Sandbox {\n if (!this.#sandbox) {\n throw new DenoSandboxError(\n \"Sandbox not initialized. Call initialize() or use DenoSandbox.create()\",\n \"NOT_INITIALIZED\",\n );\n }\n return this.#sandbox;\n }\n\n /**\n * Check if the sandbox is initialized and running.\n */\n get isRunning(): boolean {\n return this.#sandbox !== null;\n }\n\n /**\n * Create a new DenoSandbox instance.\n *\n * Note: This only creates the instance. Call `initialize()` to actually\n * create the Deno Sandbox, or use the static `DenoSandbox.create()` method.\n *\n * @param options - Configuration options for the sandbox\n *\n * @example\n * ```typescript\n * // Two-step initialization\n * const sandbox = new DenoSandbox({ memoryMb: 1024 });\n * await sandbox.initialize();\n *\n * // Or use the factory method\n * const sandbox = await DenoSandbox.create({ memoryMb: 1024 });\n * ```\n */\n constructor(options: DenoSandboxOptions = {}) {\n super();\n\n // Set defaults\n this.#options = {\n memoryMb: 768,\n lifetime: \"session\",\n ...options,\n };\n\n // Generate temporary ID until initialized\n this.#id = `deno-sandbox-${Date.now()}`;\n }\n\n /**\n * Initialize the sandbox by creating a new Deno Sandbox instance.\n *\n * This method authenticates with Deno Deploy and provisions a new microVM\n * sandbox. After initialization, the `id` property will reflect the\n * actual Deno sandbox ID.\n *\n * @throws {DenoSandboxError} If already initialized (`ALREADY_INITIALIZED`)\n * @throws {DenoSandboxError} If authentication fails (`AUTHENTICATION_FAILED`)\n * @throws {DenoSandboxError} If sandbox creation fails (`SANDBOX_CREATION_FAILED`)\n *\n * @example\n * ```typescript\n * const sandbox = new DenoSandbox();\n * await sandbox.initialize();\n * console.log(`Sandbox ID: ${sandbox.id}`);\n * ```\n */\n async initialize(): Promise<void> {\n // Prevent double initialization\n if (this.#sandbox) {\n throw new DenoSandboxError(\n \"Sandbox is already initialized. Each DenoSandbox instance can only be initialized once.\",\n \"ALREADY_INITIALIZED\",\n );\n }\n\n // Get authentication credentials\n let credentials: { token: string };\n try {\n credentials = getAuthCredentials(this.#options.auth);\n } catch (error) {\n throw new DenoSandboxError(\n \"Failed to authenticate with Deno Deploy. Check your token configuration.\",\n \"AUTHENTICATION_FAILED\",\n error instanceof Error ? error : undefined,\n );\n }\n\n try {\n // Set the token in environment for the SDK\n process.env.DENO_DEPLOY_TOKEN = credentials.token;\n\n // Build SDK create options\n const createOptions: Parameters<typeof Sandbox.create>[0] = {};\n\n // Add optional memory configuration\n if (this.#options.memoryMb !== undefined) {\n createOptions.memoryMb = this.#options.memoryMb;\n }\n\n // Add optional lifetime configuration\n if (this.#options.lifetime !== undefined) {\n createOptions.lifetime = this.#options.lifetime;\n }\n\n // Add optional region configuration\n if (this.#options.region !== undefined) {\n createOptions.region = this.#options.region;\n }\n\n // Create the sandbox\n this.#sandbox = await Sandbox.create(createOptions);\n\n // Update ID to the actual sandbox ID\n this.#id = this.#sandbox.id;\n\n // Upload initial files if provided\n if (this.#options.initialFiles) {\n await this.#uploadInitialFiles(this.#options.initialFiles);\n }\n } catch (error) {\n throw new DenoSandboxError(\n `Failed to create Deno Sandbox: ${error instanceof Error ? error.message : String(error)}`,\n \"SANDBOX_CREATION_FAILED\",\n error instanceof Error ? error : undefined,\n );\n }\n }\n\n /**\n * Upload initial files to the sandbox.\n *\n * @param files - A map of file paths to their string contents\n */\n async #uploadInitialFiles(files: Record<string, string>): Promise<void> {\n const encoder = new TextEncoder();\n const fileEntries: Array<[string, Uint8Array]> = Object.entries(files).map(\n ([path, content]) => [path, encoder.encode(content)],\n );\n\n const results = await this.uploadFiles(fileEntries);\n\n // Check for any errors during upload\n const errors = results.filter((r) => r.error !== null);\n if (errors.length > 0) {\n const errorPaths = errors.map((e) => `${e.path}: ${e.error}`).join(\", \");\n throw new DenoSandboxError(\n `Failed to upload initial files: ${errorPaths}`,\n \"FILE_OPERATION_FAILED\",\n );\n }\n }\n\n /**\n * Execute a command in the sandbox.\n *\n * Commands are run using the sandbox's shell in the configured working directory.\n *\n * @param command - The shell command to execute\n * @returns Execution result with output, exit code, and truncation flag\n * @throws {DenoSandboxError} If the sandbox is not initialized\n *\n * @example\n * ```typescript\n * const result = await sandbox.execute(\"echo 'Hello World'\");\n * console.log(result.output); // \"Hello World\\n\"\n * console.log(result.exitCode); // 0\n * ```\n */\n async execute(command: string): Promise<ExecuteResponse> {\n const sandbox = this.instance; // Throws if not initialized\n\n try {\n // Use spawn with bash to execute the command\n const child = await sandbox.spawn(\"/bin/bash\", {\n args: [\"-c\", command],\n stdout: \"piped\",\n stderr: \"piped\",\n });\n\n // Use output() to get buffered stdout/stderr\n const { status, stdoutText, stderrText } = await child.output();\n\n return {\n output: (stdoutText ?? \"\") + (stderrText ?? \"\"),\n exitCode: status.code ?? 0,\n truncated: false,\n };\n } catch (error) {\n // Check for timeout\n if (error instanceof Error && error.message.includes(\"timeout\")) {\n throw new DenoSandboxError(\n `Command timed out: ${command}`,\n \"COMMAND_TIMEOUT\",\n error,\n );\n }\n\n throw new DenoSandboxError(\n `Command execution failed: ${error instanceof Error ? error.message : String(error)}`,\n \"COMMAND_FAILED\",\n error instanceof Error ? error : undefined,\n );\n }\n }\n\n /**\n * Upload files to the sandbox.\n *\n * Files are written to the sandbox filesystem. Parent directories are\n * created automatically if they don't exist.\n *\n * @param files - Array of [path, content] tuples to upload\n * @returns Upload result for each file, with success or error status\n *\n * @example\n * ```typescript\n * const encoder = new TextEncoder();\n * const results = await sandbox.uploadFiles([\n * [\"src/index.js\", encoder.encode(\"console.log('Hello')\")],\n * [\"package.json\", encoder.encode('{\"name\": \"test\"}')],\n * ]);\n * ```\n */\n async uploadFiles(\n files: Array<[string, Uint8Array]>,\n ): Promise<FileUploadResponse[]> {\n const sandbox = this.instance; // Throws if not initialized\n const results: FileUploadResponse[] = [];\n\n for (const [path, content] of files) {\n try {\n // Ensure parent directory exists using spawn (more reliable than sh template)\n const parentDir = path.substring(0, path.lastIndexOf(\"/\"));\n if (parentDir) {\n const mkdirChild = await sandbox.spawn(\"/bin/bash\", {\n args: [\"-c\", `mkdir -p \"${parentDir}\"`],\n stdout: \"piped\",\n stderr: \"piped\",\n });\n await mkdirChild.output();\n }\n\n // Write the file content\n const textContent = new TextDecoder().decode(content);\n await sandbox.writeTextFile(path, textContent);\n results.push({ path, error: null });\n } catch (error) {\n results.push({ path, error: this.#mapError(error) });\n }\n }\n\n return results;\n }\n\n /**\n * Download files from the sandbox.\n *\n * Each file is read individually, allowing partial success when some\n * files exist and others don't.\n *\n * @param paths - Array of file paths to download\n * @returns Download result for each file, with content or error\n *\n * @example\n * ```typescript\n * const results = await sandbox.downloadFiles([\"src/index.js\", \"missing.txt\"]);\n * for (const result of results) {\n * if (result.content) {\n * console.log(new TextDecoder().decode(result.content));\n * } else {\n * console.error(`Error: ${result.error}`);\n * }\n * }\n * ```\n */\n async downloadFiles(paths: string[]): Promise<FileDownloadResponse[]> {\n const sandbox = this.instance; // Throws if not initialized\n const results: FileDownloadResponse[] = [];\n\n for (const path of paths) {\n try {\n // Use spawn with bash to read file content (same approach as execute())\n const child = await sandbox.spawn(\"/bin/bash\", {\n args: [\"-c\", `cat \"${path}\"`],\n stdout: \"piped\",\n stderr: \"piped\",\n });\n\n const { status, stdoutText } = await child.output();\n\n if (!status.success) {\n results.push({\n path,\n content: null,\n error: \"file_not_found\",\n });\n } else {\n const content = new TextEncoder().encode(stdoutText ?? \"\");\n results.push({\n path,\n content,\n error: null,\n });\n }\n } catch (error) {\n results.push({\n path,\n content: null,\n error: this.#mapError(error),\n });\n }\n }\n\n return results;\n }\n\n // ============================================================================\n // Override BaseSandbox methods that use Python with pure shell implementations\n // Deno sandboxes don't have Python installed, only basic Unix tools\n // ============================================================================\n\n /**\n * Read a file's content with line numbers.\n *\n * Override of BaseSandbox.read() to use awk instead of Python,\n * since Deno sandboxes don't have Python installed.\n *\n * @param filePath - Absolute path to the file\n * @param offset - Line offset (0-indexed, default 0)\n * @param limit - Maximum lines to return (default 500)\n * @returns Formatted file content with line numbers, or error message\n */\n override async read(\n filePath: string,\n offset: number = 0,\n limit: number = 500,\n ): Promise<string> {\n // Coerce offset and limit to safe non-negative integers\n const safeOffset =\n Number.isFinite(offset) && offset > 0 ? Math.floor(offset) : 0;\n const safeLimit =\n Number.isFinite(limit) && limit > 0 && limit < Number.MAX_SAFE_INTEGER\n ? Math.floor(limit)\n : 500;\n\n // Escape path for shell\n const escapedPath = filePath.replace(/'/g, \"'\\\\''\");\n\n // Build shell command using awk for portable line number formatting\n // First check if file exists, then format with line numbers\n const command = `\nif [ ! -f '${escapedPath}' ]; then\n echo \"Error: File not found\"\n exit 1\nfi\nif [ ! -s '${escapedPath}' ]; then\n echo \"System reminder: File exists but has empty contents\"\n exit 0\nfi\nawk -v offset=${safeOffset} -v limit=${safeLimit} '\n NR > offset && NR <= offset + limit {\n printf \"%6d\\\\t%s\\\\n\", NR, $0\n }\n' '${escapedPath}'\n`;\n\n const result = await this.execute(command);\n\n if (result.exitCode !== 0) {\n return `Error: File '${filePath}' not found`;\n }\n\n return result.output;\n }\n\n /**\n * Create a new file with content.\n *\n * Override of BaseSandbox.write() to use shell commands instead of Python,\n * since Deno sandboxes don't have Python installed.\n *\n * @param filePath - Absolute path for the new file\n * @param content - File content to write\n * @returns WriteResult with error populated on failure\n */\n override async write(\n filePath: string,\n content: string,\n ): Promise<WriteResult> {\n // Escape path for shell\n const escapedPath = filePath.replace(/'/g, \"'\\\\''\");\n\n // Check if file already exists\n const checkResult = await this.execute(`test -f '${escapedPath}'`);\n if (checkResult.exitCode === 0) {\n return {\n error: `Cannot write to ${filePath} because it already exists. Read and then make an edit, or write to a new path.`,\n };\n }\n\n // Use uploadFiles for reliable content writing (handles binary, special chars, etc.)\n const encoder = new TextEncoder();\n const uploadResult = await this.uploadFiles([\n [filePath, encoder.encode(content)],\n ]);\n\n if (uploadResult[0]?.error) {\n return { error: `Failed to write file: ${uploadResult[0].error}` };\n }\n\n return { path: filePath, filesUpdate: null };\n }\n\n /**\n * Edit a file by replacing string occurrences.\n *\n * Override of BaseSandbox.edit() to use shell commands instead of Python,\n * since Deno sandboxes don't have Python installed.\n *\n * Uses sed for in-place replacement with proper escaping.\n *\n * @param filePath - Absolute path to the file\n * @param oldString - String to find and replace\n * @param newString - Replacement string\n * @param replaceAll - If true, replace all occurrences (default: false)\n * @returns EditResult with error, path, and occurrences\n */\n override async edit(\n filePath: string,\n oldString: string,\n newString: string,\n replaceAll: boolean = false,\n ): Promise<EditResult> {\n // Escape path for shell\n const escapedPath = filePath.replace(/'/g, \"'\\\\''\");\n\n // Check if file exists\n const checkResult = await this.execute(`test -f '${escapedPath}'`);\n if (checkResult.exitCode !== 0) {\n return { error: `Error: File '${filePath}' not found` };\n }\n\n // Count occurrences first using grep -F (fixed string)\n // Escape old string for grep\n const escapedOldForGrep = oldString.replace(/'/g, \"'\\\\''\");\n const countResult = await this.execute(\n `grep -oF '${escapedOldForGrep}' '${escapedPath}' | wc -l`,\n );\n const count = parseInt(countResult.output.trim(), 10) || 0;\n\n if (count === 0) {\n return { error: `String not found in file '${filePath}'` };\n }\n\n if (count > 1 && !replaceAll) {\n return {\n error: `Multiple occurrences found in '${filePath}'. Use replaceAll=true to replace all.`,\n };\n }\n\n // Perform the replacement using sed\n // Use a delimiter that's unlikely to be in the strings (we'll use \\x00 if available, otherwise |)\n // For safety, we'll use awk which handles arbitrary strings better than sed\n\n // Base64 encode both strings to safely pass them to awk\n const oldB64 = Buffer.from(oldString, \"utf-8\").toString(\"base64\");\n const newB64 = Buffer.from(newString, \"utf-8\").toString(\"base64\");\n\n // Use awk with base64 decoding for safe string replacement\n const awkCommand = `\nOLD=$(echo '${oldB64}' | base64 -d)\nNEW=$(echo '${newB64}' | base64 -d)\nawk -v old=\"$OLD\" -v new=\"$NEW\" -v replace_all=${replaceAll ? 1 : 0} '\n{\n if (replace_all) {\n gsub(old, new)\n } else {\n sub(old, new)\n }\n print\n}\n' '${escapedPath}' > '${escapedPath}.tmp' && mv '${escapedPath}.tmp' '${escapedPath}'\n`;\n\n const editResult = await this.execute(awkCommand);\n\n if (editResult.exitCode !== 0) {\n return { error: `Unknown error editing file '${filePath}'` };\n }\n\n return { path: filePath, filesUpdate: null, occurrences: count };\n }\n\n /**\n * Close the sandbox and release all resources.\n *\n * After closing, the sandbox cannot be used again. Any unsaved data\n * will be lost.\n *\n * @example\n * ```typescript\n * try {\n * await sandbox.execute(\"deno run build.ts\");\n * } finally {\n * await sandbox.close();\n * }\n * ```\n */\n async close(): Promise<void> {\n if (this.#sandbox) {\n try {\n await this.#sandbox.close();\n } finally {\n this.#sandbox = null;\n }\n }\n }\n\n /**\n * Forcefully terminate the sandbox.\n *\n * Use this when you need to immediately stop the sandbox, even if\n * operations are in progress.\n *\n * @example\n * ```typescript\n * await sandbox.kill();\n * ```\n */\n async kill(): Promise<void> {\n if (this.#sandbox) {\n try {\n await this.#sandbox.kill();\n } finally {\n this.#sandbox = null;\n }\n }\n }\n\n /**\n * Alias for close() to maintain compatibility with other sandbox implementations.\n */\n async stop(): Promise<void> {\n await this.close();\n }\n\n /**\n * Set the sandbox from an existing Deno Sandbox instance.\n * Used internally by the static `connect()` method.\n */\n #setFromExisting(existingSandbox: Sandbox, sandboxId: string): void {\n this.#sandbox = existingSandbox;\n this.#id = sandboxId;\n }\n\n /**\n * Map Deno SDK errors to standardized FileOperationError codes.\n *\n * @param error - The error from the Deno SDK\n * @returns A standardized error code\n */\n #mapError(error: unknown): FileOperationError {\n if (error instanceof Error) {\n const msg = error.message.toLowerCase();\n\n if (msg.includes(\"not found\") || msg.includes(\"enoent\")) {\n return \"file_not_found\";\n }\n if (msg.includes(\"permission\") || msg.includes(\"eacces\")) {\n return \"permission_denied\";\n }\n if (msg.includes(\"directory\") || msg.includes(\"eisdir\")) {\n return \"is_directory\";\n }\n }\n\n return \"invalid_path\";\n }\n\n /**\n * Create and initialize a new DenoSandbox in one step.\n *\n * This is the recommended way to create a sandbox. It combines\n * construction and initialization into a single async operation.\n *\n * @param options - Configuration options for the sandbox\n * @returns An initialized and ready-to-use sandbox\n *\n * @example\n * ```typescript\n * const sandbox = await DenoSandbox.create({\n * memoryMb: 1024,\n * lifetime: \"10m\",\n * region: \"iad\",\n * });\n * ```\n */\n static async create(options?: DenoSandboxOptions): Promise<DenoSandbox> {\n const sandbox = new DenoSandbox(options);\n await sandbox.initialize();\n return sandbox;\n }\n\n /**\n * Reconnect to an existing sandbox by ID.\n *\n * This allows you to resume working with a sandbox that was created\n * earlier with a duration-based lifetime.\n *\n * @param id - The ID of the sandbox to reconnect to\n * @param options - Optional auth configuration (for token)\n * @returns A connected sandbox instance\n *\n * @example\n * ```typescript\n * // Resume a sandbox from a stored ID\n * const sandbox = await DenoSandbox.fromId(\"sandbox-abc123\");\n * const result = await sandbox.execute(\"ls -la\");\n * ```\n */\n static async fromId(\n id: string,\n options?: Pick<DenoSandboxOptions, \"auth\">,\n ): Promise<DenoSandbox> {\n // Get authentication credentials\n let credentials: { token: string };\n try {\n credentials = getAuthCredentials(options?.auth);\n } catch (error) {\n throw new DenoSandboxError(\n \"Failed to authenticate with Deno Deploy. Check your token configuration.\",\n \"AUTHENTICATION_FAILED\",\n error instanceof Error ? error : undefined,\n );\n }\n\n try {\n // Set the token in environment for the SDK\n process.env.DENO_DEPLOY_TOKEN = credentials.token;\n\n const existingSandbox = await Sandbox.connect({ id });\n\n const denoSandbox = new DenoSandbox();\n // Set the existing sandbox directly (bypass initialize)\n denoSandbox.#setFromExisting(existingSandbox, id);\n\n return denoSandbox;\n } catch (error) {\n throw new DenoSandboxError(\n `Sandbox not found: ${id}`,\n \"SANDBOX_NOT_FOUND\",\n error instanceof Error ? error : undefined,\n );\n }\n }\n}\n\n/**\n * Async factory function type for creating Deno Sandbox instances.\n *\n * This is similar to BackendFactory but supports async creation,\n * which is required for Deno Sandbox since initialization is async.\n */\nexport type AsyncDenoSandboxFactory = () => Promise<DenoSandbox>;\n\n/**\n * Create an async factory function that creates a new Deno Sandbox per invocation.\n *\n * Each call to the factory will create and initialize a new sandbox.\n * This is useful when you want fresh, isolated environments for each\n * agent invocation.\n *\n * **Important**: This returns an async factory. For use with middleware that\n * requires synchronous BackendFactory, use `createDenoSandboxFactoryFromSandbox()`\n * with a pre-created sandbox instead.\n *\n * @param options - Optional configuration for sandbox creation\n * @returns An async factory function that creates new sandboxes\n *\n * @example\n * ```typescript\n * import { DenoSandbox, createDenoSandboxFactory } from \"@langchain/deno\";\n *\n * // Create a factory for new sandboxes\n * const factory = createDenoSandboxFactory({ memoryMb: 1024 });\n *\n * // Each call creates a new sandbox\n * const sandbox1 = await factory();\n * const sandbox2 = await factory();\n *\n * try {\n * // Use sandboxes...\n * } finally {\n * await sandbox1.close();\n * await sandbox2.close();\n * }\n * ```\n */\nexport function createDenoSandboxFactory(\n options?: DenoSandboxOptions,\n): AsyncDenoSandboxFactory {\n return async () => {\n return await DenoSandbox.create(options);\n };\n}\n\n/**\n * Create a backend factory that reuses an existing Deno Sandbox.\n *\n * This allows multiple agent invocations to share the same sandbox,\n * avoiding the startup overhead of creating new sandboxes.\n *\n * Important: You are responsible for managing the sandbox lifecycle\n * (calling `close()` when done).\n *\n * @param sandbox - An existing DenoSandbox instance (must be initialized)\n * @returns A BackendFactory that returns the provided sandbox\n *\n * @example\n * ```typescript\n * import { createDeepAgent, createFilesystemMiddleware } from \"deepagents\";\n * import { DenoSandbox, createDenoSandboxFactoryFromSandbox } from \"@langchain/deno\";\n *\n * // Create and initialize a sandbox\n * const sandbox = await DenoSandbox.create({ memoryMb: 1024 });\n *\n * try {\n * const agent = createDeepAgent({\n * model: new ChatAnthropic({ model: \"claude-sonnet-4-20250514\" }),\n * systemPrompt: \"You are a coding assistant.\",\n * middlewares: [\n * createFilesystemMiddleware({\n * backend: createDenoSandboxFactoryFromSandbox(sandbox),\n * }),\n * ],\n * });\n *\n * await agent.invoke({ messages: [...] });\n * } finally {\n * await sandbox.close();\n * }\n * ```\n */\nexport function createDenoSandboxFactoryFromSandbox(\n sandbox: DenoSandbox,\n): BackendFactory {\n return () => sandbox;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuDA,SAAgB,aAAa,SAA8C;AAEzE,KAAI,SAAS,MACX,QAAO,QAAQ;CAIjB,MAAM,cAAc,QAAQ,IAAI;AAChC,KAAI,YACF,QAAO;AAIT,OAAM,IAAI,MACR,sWAMD;;;;;;;;;;;AAYH,SAAgB,mBACd,SACiB;AACjB,QAAO,EACL,OAAO,aAAa,QAAQ,EAC7B;;;;;;;;;;;ACqCH,MAAM,4BAA4B,OAAO,IAAI,qBAAqB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8BlE,IAAa,mBAAb,MAAa,yBAAyB,aAAa;CACjD,CAAC;;CAGD,AAAkB,OAAO;;;;;;;;CASzB,YACE,SACA,AAAgB,MAChB,AAAyB,OACzB;AACA,QAAM,SAAS,MAA0B,MAAM;EAH/B;EACS;AAIzB,SAAO,eAAe,MAAM,iBAAiB,UAAU;;;;;;;;CASzD,OAAO,WA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;CAoBT,aAAa,OACX,IACA,SACsB;EAEtB,IAAI;AACJ,MAAI;AACF,iBAAc,mBAAmB,SAAS,KAAK;WACxC,OAAO;AACd,SAAM,IAAI,iBACR,4EACA,yBACA,iBAAiB,QAAQ,QAAQ,OAClC;;AAGH,MAAI;AAEF,WAAQ,IAAI,oBAAoB,YAAY;GAE5C,MAAM,kBAAkB,MAAM,QAAQ,QAAQ,EAAE,IAAI,CAAC;GAErD,MAAM,cAAc,IAAI,aAAa;AAErC,gBAAYK,gBAAiB,iBAAiB,GAAG;AAEjD,UAAO;WACA,OAAO;AACd,SAAM,IAAI,iBACR,sBAAsB,MACtB,qBACA,iBAAiB,QAAQ,QAAQ,OAClC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8CP,SAAgB,yBACd,SACyB;AACzB,QAAO,YAAY;AACjB,SAAO,MAAM,YAAY,OAAO,QAAQ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyC5C,SAAgB,oCACd,SACgB;AAChB,cAAa"}
|
|
1
|
+
{"version":3,"file":"index.js","names":["#id","#sandbox","#options","#uploadInitialFiles","#mapError","#setFromExisting"],"sources":["../src/auth.ts","../src/types.ts","../src/sandbox.ts"],"sourcesContent":["/**\n * Authentication utilities for Deno Sandbox.\n *\n * This module provides authentication credential resolution for the Deno Sandbox SDK.\n *\n * @packageDocumentation\n */\n\nimport type { DenoSandboxOptions } from \"./types.js\";\n\n/**\n * Authentication credentials for Deno Sandbox API.\n */\nexport interface DenoCredentials {\n /** Deno Deploy access token */\n token: string;\n}\n\n/**\n * Get the authentication token for Deno Sandbox API.\n *\n * Authentication is resolved in the following priority order:\n *\n * 1. **Explicit token**: If `options.token` is provided, it is used directly.\n * 2. **DENO_DEPLOY_TOKEN**: Environment variable for Deno Deploy access token.\n *\n * If no token is found, an error is thrown with setup instructions.\n *\n * ## Environment Variable Setup\n *\n * ```bash\n * # Go to https://app.deno.com -> Settings -> Organization Tokens\n * # Create a new token and set it as environment variable\n * export DENO_DEPLOY_TOKEN=your_token_here\n * ```\n *\n * @param options - Optional authentication configuration from DenoSandboxOptions\n * @returns The authentication token string\n * @throws {Error} If no authentication token is available\n *\n * @example\n * ```typescript\n * // With explicit token\n * const token = getAuthToken({ token: \"my-token\" });\n *\n * // Using environment variables (auto-detected)\n * const token = getAuthToken();\n *\n * // From DenoSandboxOptions\n * const options: DenoSandboxOptions = {\n * auth: { token: \"my-token\" }\n * };\n * const token = getAuthToken(options.auth);\n * ```\n */\nexport function getAuthToken(options?: DenoSandboxOptions[\"auth\"]): string {\n // Priority 1: Explicit token in options\n if (options?.token) {\n return options.token;\n }\n\n // Priority 2: DENO_DEPLOY_TOKEN environment variable\n const deployToken = process.env.DENO_DEPLOY_TOKEN;\n if (deployToken) {\n return deployToken;\n }\n\n // No token found - throw descriptive error\n throw new Error(\n \"Deno Deploy authentication required. Provide a token using one of these methods:\\n\\n\" +\n \"1. Set DENO_DEPLOY_TOKEN environment variable:\\n\" +\n \" Go to https://app.deno.com -> Settings -> Organization Tokens\\n\" +\n \" Create a new token and run: export DENO_DEPLOY_TOKEN=your_token_here\\n\\n\" +\n \"2. Pass token directly in options:\\n\" +\n \" new DenoSandbox({ auth: { token: '...' } })\",\n );\n}\n\n/**\n * Get authentication credentials for Deno Sandbox API.\n *\n * This function returns the credentials needed for the Deno SDK.\n *\n * @param options - Optional authentication configuration from DenoSandboxOptions\n * @returns Complete authentication credentials\n * @throws {Error} If no authentication token is available\n */\nexport function getAuthCredentials(\n options?: DenoSandboxOptions[\"auth\"],\n): DenoCredentials {\n return {\n token: getAuthToken(options),\n };\n}\n","/**\n * Type definitions for the Deno Sandbox backend.\n *\n * This module contains all type definitions for the @langchain/deno package,\n * including options and error types.\n */\n\nimport { type SandboxErrorCode, SandboxError } from \"deepagents\";\n\n/**\n * Supported regions for Deno Deploy sandboxes.\n *\n * Currently available regions:\n * - `ams`: Amsterdam\n * - `ord`: Chicago\n */\nexport type DenoSandboxRegion = \"ams\" | \"ord\";\n\n/**\n * Sandbox lifetime configuration.\n *\n * - `\"session\"`: Sandbox shuts down when you close/dispose the client (default)\n * - Duration string: Keep sandbox alive for a specific time (e.g., \"5m\", \"30s\")\n */\nexport type SandboxLifetime = \"session\" | `${number}s` | `${number}m`;\n\n/**\n * Configuration options for creating a Deno Sandbox.\n *\n * @example\n * ```typescript\n * const options: DenoSandboxOptions = {\n * memoryMb: 1024, // 1GB memory\n * lifetime: \"5m\", // 5 minutes\n * region: \"iad\", // US East\n * };\n * ```\n */\nexport interface DenoSandboxOptions {\n /**\n * Amount of memory allocated to the sandbox in megabytes.\n *\n * Memory limits:\n * - Minimum: 768MB\n * - Maximum: 4096MB\n *\n * @default 768\n */\n memoryMb?: number;\n\n /**\n * Sandbox lifetime configuration.\n *\n * - `\"session\"`: Sandbox shuts down when you close/dispose the client (default)\n * - Duration string: Keep sandbox alive for a specific time (e.g., \"5m\", \"30s\")\n *\n * Supported duration suffixes: `s` (seconds), `m` (minutes).\n *\n * @default \"session\"\n */\n lifetime?: SandboxLifetime;\n\n /**\n * Region where the sandbox will be created.\n *\n * If not specified, the sandbox will be created in the default region.\n *\n * @see DenoSandboxRegion for available regions\n */\n region?: DenoSandboxRegion;\n\n /**\n * Initial files to create in the sandbox after initialization.\n *\n * A map of file paths to their contents. Files will be created\n * in the sandbox filesystem before any commands are executed.\n * Parent directories are created automatically.\n *\n * @example\n * ```typescript\n * const options: DenoSandboxOptions = {\n * memoryMb: 1024,\n * initialFiles: {\n * \"/home/app/index.js\": \"console.log('Hello')\",\n * \"/home/app/package.json\": '{\"name\": \"test\"}',\n * },\n * };\n * ```\n */\n initialFiles?: Record<string, string>;\n\n /**\n * Authentication configuration for Deno Deploy API.\n *\n * ### Environment Variable Setup\n *\n * ```bash\n * # Go to https://app.deno.com -> Settings -> Organization Tokens\n * # Create a new token and set it as environment variable\n * export DENO_DEPLOY_TOKEN=your_token_here\n * ```\n *\n * Or pass the token directly in this auth configuration.\n */\n auth?: {\n /**\n * Deno Deploy access token.\n * If not provided, reads from `DENO_DEPLOY_TOKEN` environment variable.\n */\n token?: string;\n };\n}\n\n/**\n * Error codes for Deno Sandbox operations.\n *\n * Used to identify specific error conditions and handle them appropriately.\n */\nexport type DenoSandboxErrorCode =\n | SandboxErrorCode\n /** Authentication failed - check token configuration */\n | \"AUTHENTICATION_FAILED\"\n /** Failed to create sandbox - check options and quotas */\n | \"SANDBOX_CREATION_FAILED\"\n /** Sandbox not found - may have been stopped or expired */\n | \"SANDBOX_NOT_FOUND\"\n /** Resource limits exceeded (CPU, memory, storage) */\n | \"RESOURCE_LIMIT_EXCEEDED\";\n\nconst DENO_SANDBOX_ERROR_SYMBOL = Symbol.for(\"deno.sandbox.error\");\n\n/**\n * Custom error class for Deno Sandbox operations.\n *\n * Provides structured error information including:\n * - Human-readable message\n * - Error code for programmatic handling\n * - Original cause for debugging\n *\n * @example\n * ```typescript\n * try {\n * await sandbox.execute(\"some command\");\n * } catch (error) {\n * if (error instanceof DenoSandboxError) {\n * switch (error.code) {\n * case \"NOT_INITIALIZED\":\n * await sandbox.initialize();\n * break;\n * case \"COMMAND_TIMEOUT\":\n * console.error(\"Command took too long\");\n * break;\n * default:\n * throw error;\n * }\n * }\n * }\n * ```\n */\nexport class DenoSandboxError extends SandboxError {\n [DENO_SANDBOX_ERROR_SYMBOL]: true;\n\n /** Error name for instanceof checks and logging */\n override readonly name = \"DenoSandboxError\";\n\n /**\n * Creates a new DenoSandboxError.\n *\n * @param message - Human-readable error description\n * @param code - Structured error code for programmatic handling\n * @param cause - Original error that caused this error (for debugging)\n */\n constructor(\n message: string,\n public readonly code: DenoSandboxErrorCode,\n public override readonly cause?: Error,\n ) {\n super(message, code as SandboxErrorCode, cause);\n // Maintain proper prototype chain for instanceof checks\n Object.setPrototypeOf(this, DenoSandboxError.prototype);\n }\n\n /**\n * Checks if the error is an instance of DenoSandboxError.\n *\n * @param error - The error to check\n * @returns True if the error is an instance of DenoSandboxError, false otherwise\n */\n static isInstance(error: unknown): error is DenoSandboxError {\n return (\n typeof error === \"object\" &&\n error !== null &&\n (error as Record<symbol, unknown>)[DENO_SANDBOX_ERROR_SYMBOL] === true\n );\n }\n}\n","/* eslint-disable no-instanceof/no-instanceof */\n/**\n * Deno Sandbox implementation of the SandboxBackendProtocol.\n *\n * This module provides a Deno Sandbox backend for deepagents, enabling agents\n * to execute commands, read/write files, and manage isolated Linux microVM\n * environments using Deno Deploy's Sandbox infrastructure.\n *\n * @packageDocumentation\n */\n\nimport { Sandbox } from \"@deno/sandbox\";\nimport {\n BaseSandbox,\n type ExecuteResponse,\n type FileDownloadResponse,\n type FileOperationError,\n type FileUploadResponse,\n type BackendFactory,\n} from \"deepagents\";\n\nimport { getAuthCredentials } from \"./auth.js\";\nimport { DenoSandboxError, type DenoSandboxOptions } from \"./types.js\";\n\n/**\n * Deno Sandbox backend for deepagents.\n *\n * Extends `BaseSandbox` to provide command execution, file operations, and\n * sandbox lifecycle management using Deno Deploy's Sandbox SDK.\n *\n * ## Basic Usage\n *\n * ```typescript\n * import { DenoSandbox } from \"@langchain/deno\";\n *\n * // Create and initialize a sandbox\n * const sandbox = await DenoSandbox.create({\n * memoryMb: 1024,\n * lifetime: \"5m\",\n * });\n *\n * try {\n * // Execute commands\n * const result = await sandbox.execute(\"deno --version\");\n * console.log(result.output);\n * } finally {\n * // Always cleanup\n * await sandbox.close();\n * }\n * ```\n *\n * ## Using with DeepAgent\n *\n * ```typescript\n * import { createDeepAgent } from \"deepagents\";\n * import { DenoSandbox } from \"@langchain/deno\";\n *\n * const sandbox = await DenoSandbox.create();\n *\n * const agent = createDeepAgent({\n * model: new ChatAnthropic({ model: \"claude-sonnet-4-20250514\" }),\n * systemPrompt: \"You are a coding assistant with sandbox access.\",\n * backend: sandbox,\n * });\n * ```\n */\nexport class DenoSandbox extends BaseSandbox {\n /** Private reference to the underlying Deno Sandbox instance */\n #sandbox: Sandbox | null = null;\n\n /** Configuration options for this sandbox */\n #options: DenoSandboxOptions;\n\n /** Unique identifier for this sandbox instance */\n #id: string;\n\n /**\n * Get the unique identifier for this sandbox.\n *\n * Before initialization, returns a temporary ID.\n * After initialization, returns the actual Deno sandbox ID.\n */\n get id(): string {\n return this.#id;\n }\n\n /**\n * Get the underlying Deno Sandbox instance.\n *\n * @throws {DenoSandboxError} If the sandbox is not initialized\n *\n * @example\n * ```typescript\n * const sandbox = await DenoSandbox.create();\n * const denoSdk = sandbox.sandbox; // Access the raw SDK\n * ```\n */\n get instance(): Sandbox {\n if (!this.#sandbox) {\n throw new DenoSandboxError(\n \"Sandbox not initialized. Call initialize() or use DenoSandbox.create()\",\n \"NOT_INITIALIZED\",\n );\n }\n return this.#sandbox;\n }\n\n /**\n * Check if the sandbox is initialized and running.\n */\n get isRunning(): boolean {\n return this.#sandbox !== null;\n }\n\n /**\n * Create a new DenoSandbox instance.\n *\n * Note: This only creates the instance. Call `initialize()` to actually\n * create the Deno Sandbox, or use the static `DenoSandbox.create()` method.\n *\n * @param options - Configuration options for the sandbox\n *\n * @example\n * ```typescript\n * // Two-step initialization\n * const sandbox = new DenoSandbox({ memoryMb: 1024 });\n * await sandbox.initialize();\n *\n * // Or use the factory method\n * const sandbox = await DenoSandbox.create({ memoryMb: 1024 });\n * ```\n */\n constructor(options: DenoSandboxOptions = {}) {\n super();\n\n // Set defaults\n this.#options = {\n memoryMb: 768,\n lifetime: \"session\",\n ...options,\n };\n\n // Generate temporary ID until initialized\n this.#id = `deno-sandbox-${Date.now()}`;\n }\n\n /**\n * Initialize the sandbox by creating a new Deno Sandbox instance.\n *\n * This method authenticates with Deno Deploy and provisions a new microVM\n * sandbox. After initialization, the `id` property will reflect the\n * actual Deno sandbox ID.\n *\n * @throws {DenoSandboxError} If already initialized (`ALREADY_INITIALIZED`)\n * @throws {DenoSandboxError} If authentication fails (`AUTHENTICATION_FAILED`)\n * @throws {DenoSandboxError} If sandbox creation fails (`SANDBOX_CREATION_FAILED`)\n *\n * @example\n * ```typescript\n * const sandbox = new DenoSandbox();\n * await sandbox.initialize();\n * console.log(`Sandbox ID: ${sandbox.id}`);\n * ```\n */\n async initialize(): Promise<void> {\n // Prevent double initialization\n if (this.#sandbox) {\n throw new DenoSandboxError(\n \"Sandbox is already initialized. Each DenoSandbox instance can only be initialized once.\",\n \"ALREADY_INITIALIZED\",\n );\n }\n\n // Get authentication credentials\n let credentials: { token: string };\n try {\n credentials = getAuthCredentials(this.#options.auth);\n } catch (error) {\n throw new DenoSandboxError(\n \"Failed to authenticate with Deno Deploy. Check your token configuration.\",\n \"AUTHENTICATION_FAILED\",\n error instanceof Error ? error : undefined,\n );\n }\n\n try {\n // Set the token in environment for the SDK\n process.env.DENO_DEPLOY_TOKEN = credentials.token;\n\n // Build SDK create options\n const createOptions: Parameters<typeof Sandbox.create>[0] = {};\n\n // Add optional memory configuration\n if (this.#options.memoryMb !== undefined) {\n createOptions.memoryMb = this.#options.memoryMb;\n }\n\n // Add optional lifetime configuration\n if (this.#options.lifetime !== undefined) {\n createOptions.lifetime = this.#options.lifetime;\n }\n\n // Add optional region configuration\n if (this.#options.region !== undefined) {\n createOptions.region = this.#options.region;\n }\n\n // Create the sandbox\n this.#sandbox = await Sandbox.create(createOptions);\n\n // Update ID to the actual sandbox ID\n this.#id = this.#sandbox.id;\n\n // Upload initial files if provided\n if (this.#options.initialFiles) {\n await this.#uploadInitialFiles(this.#options.initialFiles);\n }\n } catch (error) {\n throw new DenoSandboxError(\n `Failed to create Deno Sandbox: ${error instanceof Error ? error.message : String(error)}`,\n \"SANDBOX_CREATION_FAILED\",\n error instanceof Error ? error : undefined,\n );\n }\n }\n\n /**\n * Upload initial files to the sandbox.\n *\n * @param files - A map of file paths to their string contents\n */\n async #uploadInitialFiles(files: Record<string, string>): Promise<void> {\n const encoder = new TextEncoder();\n const fileEntries: Array<[string, Uint8Array]> = Object.entries(files).map(\n ([path, content]) => [path, encoder.encode(content)],\n );\n\n const results = await this.uploadFiles(fileEntries);\n\n // Check for any errors during upload\n const errors = results.filter((r) => r.error !== null);\n if (errors.length > 0) {\n const errorPaths = errors.map((e) => `${e.path}: ${e.error}`).join(\", \");\n throw new DenoSandboxError(\n `Failed to upload initial files: ${errorPaths}`,\n \"FILE_OPERATION_FAILED\",\n );\n }\n }\n\n /**\n * Execute a command in the sandbox.\n *\n * Commands are run using the sandbox's shell in the configured working directory.\n *\n * @param command - The shell command to execute\n * @returns Execution result with output, exit code, and truncation flag\n * @throws {DenoSandboxError} If the sandbox is not initialized\n *\n * @example\n * ```typescript\n * const result = await sandbox.execute(\"echo 'Hello World'\");\n * console.log(result.output); // \"Hello World\\n\"\n * console.log(result.exitCode); // 0\n * ```\n */\n async execute(command: string): Promise<ExecuteResponse> {\n const sandbox = this.instance; // Throws if not initialized\n\n try {\n // Use spawn with bash to execute the command\n const child = await sandbox.spawn(\"/bin/bash\", {\n args: [\"-c\", command],\n stdout: \"piped\",\n stderr: \"piped\",\n });\n\n // Use output() to get buffered stdout/stderr\n const { status, stdoutText, stderrText } = await child.output();\n\n return {\n output: (stdoutText ?? \"\") + (stderrText ?? \"\"),\n exitCode: status.code ?? 0,\n truncated: false,\n };\n } catch (error) {\n // Check for timeout\n if (error instanceof Error && error.message.includes(\"timeout\")) {\n throw new DenoSandboxError(\n `Command timed out: ${command}`,\n \"COMMAND_TIMEOUT\",\n error,\n );\n }\n\n throw new DenoSandboxError(\n `Command execution failed: ${error instanceof Error ? error.message : String(error)}`,\n \"COMMAND_FAILED\",\n error instanceof Error ? error : undefined,\n );\n }\n }\n\n /**\n * Upload files to the sandbox.\n *\n * Files are written to the sandbox filesystem. Parent directories are\n * created automatically if they don't exist.\n *\n * @param files - Array of [path, content] tuples to upload\n * @returns Upload result for each file, with success or error status\n *\n * @example\n * ```typescript\n * const encoder = new TextEncoder();\n * const results = await sandbox.uploadFiles([\n * [\"src/index.js\", encoder.encode(\"console.log('Hello')\")],\n * [\"package.json\", encoder.encode('{\"name\": \"test\"}')],\n * ]);\n * ```\n */\n async uploadFiles(\n files: Array<[string, Uint8Array]>,\n ): Promise<FileUploadResponse[]> {\n const sandbox = this.instance; // Throws if not initialized\n const results: FileUploadResponse[] = [];\n\n for (const [path, content] of files) {\n try {\n // Ensure parent directory exists using spawn (more reliable than sh template)\n const parentDir = path.substring(0, path.lastIndexOf(\"/\"));\n if (parentDir) {\n const mkdirChild = await sandbox.spawn(\"/bin/bash\", {\n args: [\"-c\", `mkdir -p \"${parentDir}\"`],\n stdout: \"piped\",\n stderr: \"piped\",\n });\n await mkdirChild.output();\n }\n\n // Write the file content\n const textContent = new TextDecoder().decode(content);\n await sandbox.writeTextFile(path, textContent);\n results.push({ path, error: null });\n } catch (error) {\n results.push({ path, error: this.#mapError(error) });\n }\n }\n\n return results;\n }\n\n /**\n * Download files from the sandbox.\n *\n * Each file is read individually, allowing partial success when some\n * files exist and others don't.\n *\n * @param paths - Array of file paths to download\n * @returns Download result for each file, with content or error\n *\n * @example\n * ```typescript\n * const results = await sandbox.downloadFiles([\"src/index.js\", \"missing.txt\"]);\n * for (const result of results) {\n * if (result.content) {\n * console.log(new TextDecoder().decode(result.content));\n * } else {\n * console.error(`Error: ${result.error}`);\n * }\n * }\n * ```\n */\n async downloadFiles(paths: string[]): Promise<FileDownloadResponse[]> {\n const sandbox = this.instance; // Throws if not initialized\n const results: FileDownloadResponse[] = [];\n\n for (const path of paths) {\n try {\n // Use spawn with bash to read file content (same approach as execute())\n const child = await sandbox.spawn(\"/bin/bash\", {\n args: [\"-c\", `cat \"${path}\"`],\n stdout: \"piped\",\n stderr: \"piped\",\n });\n\n const { status, stdoutText } = await child.output();\n\n if (!status.success) {\n results.push({\n path,\n content: null,\n error: \"file_not_found\",\n });\n } else {\n const content = new TextEncoder().encode(stdoutText ?? \"\");\n results.push({\n path,\n content,\n error: null,\n });\n }\n } catch (error) {\n results.push({\n path,\n content: null,\n error: this.#mapError(error),\n });\n }\n }\n\n return results;\n }\n\n /**\n * Close the sandbox and release all resources.\n *\n * After closing, the sandbox cannot be used again. Any unsaved data\n * will be lost.\n *\n * @example\n * ```typescript\n * try {\n * await sandbox.execute(\"deno run build.ts\");\n * } finally {\n * await sandbox.close();\n * }\n * ```\n */\n async close(): Promise<void> {\n if (this.#sandbox) {\n try {\n await this.#sandbox.close();\n } finally {\n this.#sandbox = null;\n }\n }\n }\n\n /**\n * Forcefully terminate the sandbox.\n *\n * Use this when you need to immediately stop the sandbox, even if\n * operations are in progress.\n *\n * @example\n * ```typescript\n * await sandbox.kill();\n * ```\n */\n async kill(): Promise<void> {\n if (this.#sandbox) {\n try {\n await this.#sandbox.kill();\n } finally {\n this.#sandbox = null;\n }\n }\n }\n\n /**\n * Alias for close() to maintain compatibility with other sandbox implementations.\n */\n async stop(): Promise<void> {\n await this.close();\n }\n\n /**\n * Set the sandbox from an existing Deno Sandbox instance.\n * Used internally by the static `connect()` method.\n */\n #setFromExisting(existingSandbox: Sandbox, sandboxId: string): void {\n this.#sandbox = existingSandbox;\n this.#id = sandboxId;\n }\n\n /**\n * Map Deno SDK errors to standardized FileOperationError codes.\n *\n * @param error - The error from the Deno SDK\n * @returns A standardized error code\n */\n #mapError(error: unknown): FileOperationError {\n if (error instanceof Error) {\n const msg = error.message.toLowerCase();\n\n if (msg.includes(\"not found\") || msg.includes(\"enoent\")) {\n return \"file_not_found\";\n }\n if (msg.includes(\"permission\") || msg.includes(\"eacces\")) {\n return \"permission_denied\";\n }\n if (msg.includes(\"directory\") || msg.includes(\"eisdir\")) {\n return \"is_directory\";\n }\n }\n\n return \"invalid_path\";\n }\n\n /**\n * Create and initialize a new DenoSandbox in one step.\n *\n * This is the recommended way to create a sandbox. It combines\n * construction and initialization into a single async operation.\n *\n * @param options - Configuration options for the sandbox\n * @returns An initialized and ready-to-use sandbox\n *\n * @example\n * ```typescript\n * const sandbox = await DenoSandbox.create({\n * memoryMb: 1024,\n * lifetime: \"10m\",\n * region: \"iad\",\n * });\n * ```\n */\n static async create(options?: DenoSandboxOptions): Promise<DenoSandbox> {\n const sandbox = new DenoSandbox(options);\n await sandbox.initialize();\n return sandbox;\n }\n\n /**\n * Reconnect to an existing sandbox by ID.\n *\n * This allows you to resume working with a sandbox that was created\n * earlier with a duration-based lifetime.\n *\n * @param id - The ID of the sandbox to reconnect to\n * @param options - Optional auth configuration (for token)\n * @returns A connected sandbox instance\n *\n * @example\n * ```typescript\n * // Resume a sandbox from a stored ID\n * const sandbox = await DenoSandbox.fromId(\"sandbox-abc123\");\n * const result = await sandbox.execute(\"ls -la\");\n * ```\n */\n static async fromId(\n id: string,\n options?: Pick<DenoSandboxOptions, \"auth\">,\n ): Promise<DenoSandbox> {\n // Get authentication credentials\n let credentials: { token: string };\n try {\n credentials = getAuthCredentials(options?.auth);\n } catch (error) {\n throw new DenoSandboxError(\n \"Failed to authenticate with Deno Deploy. Check your token configuration.\",\n \"AUTHENTICATION_FAILED\",\n error instanceof Error ? error : undefined,\n );\n }\n\n try {\n // Set the token in environment for the SDK\n process.env.DENO_DEPLOY_TOKEN = credentials.token;\n\n const existingSandbox = await Sandbox.connect({ id });\n\n const denoSandbox = new DenoSandbox();\n // Set the existing sandbox directly (bypass initialize)\n denoSandbox.#setFromExisting(existingSandbox, id);\n\n return denoSandbox;\n } catch (error) {\n throw new DenoSandboxError(\n `Sandbox not found: ${id}`,\n \"SANDBOX_NOT_FOUND\",\n error instanceof Error ? error : undefined,\n );\n }\n }\n}\n\n/**\n * Async factory function type for creating Deno Sandbox instances.\n *\n * This is similar to BackendFactory but supports async creation,\n * which is required for Deno Sandbox since initialization is async.\n */\nexport type AsyncDenoSandboxFactory = () => Promise<DenoSandbox>;\n\n/**\n * Create an async factory function that creates a new Deno Sandbox per invocation.\n *\n * Each call to the factory will create and initialize a new sandbox.\n * This is useful when you want fresh, isolated environments for each\n * agent invocation.\n *\n * **Important**: This returns an async factory. For use with middleware that\n * requires synchronous BackendFactory, use `createDenoSandboxFactoryFromSandbox()`\n * with a pre-created sandbox instead.\n *\n * @param options - Optional configuration for sandbox creation\n * @returns An async factory function that creates new sandboxes\n *\n * @example\n * ```typescript\n * import { DenoSandbox, createDenoSandboxFactory } from \"@langchain/deno\";\n *\n * // Create a factory for new sandboxes\n * const factory = createDenoSandboxFactory({ memoryMb: 1024 });\n *\n * // Each call creates a new sandbox\n * const sandbox1 = await factory();\n * const sandbox2 = await factory();\n *\n * try {\n * // Use sandboxes...\n * } finally {\n * await sandbox1.close();\n * await sandbox2.close();\n * }\n * ```\n */\nexport function createDenoSandboxFactory(\n options?: DenoSandboxOptions,\n): AsyncDenoSandboxFactory {\n return async () => {\n return await DenoSandbox.create(options);\n };\n}\n\n/**\n * Create a backend factory that reuses an existing Deno Sandbox.\n *\n * This allows multiple agent invocations to share the same sandbox,\n * avoiding the startup overhead of creating new sandboxes.\n *\n * Important: You are responsible for managing the sandbox lifecycle\n * (calling `close()` when done).\n *\n * @param sandbox - An existing DenoSandbox instance (must be initialized)\n * @returns A BackendFactory that returns the provided sandbox\n *\n * @example\n * ```typescript\n * import { createDeepAgent, createFilesystemMiddleware } from \"deepagents\";\n * import { DenoSandbox, createDenoSandboxFactoryFromSandbox } from \"@langchain/deno\";\n *\n * // Create and initialize a sandbox\n * const sandbox = await DenoSandbox.create({ memoryMb: 1024 });\n *\n * try {\n * const agent = createDeepAgent({\n * model: new ChatAnthropic({ model: \"claude-sonnet-4-20250514\" }),\n * systemPrompt: \"You are a coding assistant.\",\n * middlewares: [\n * createFilesystemMiddleware({\n * backend: createDenoSandboxFactoryFromSandbox(sandbox),\n * }),\n * ],\n * });\n *\n * await agent.invoke({ messages: [...] });\n * } finally {\n * await sandbox.close();\n * }\n * ```\n */\nexport function createDenoSandboxFactoryFromSandbox(\n sandbox: DenoSandbox,\n): BackendFactory {\n return () => sandbox;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuDA,SAAgB,aAAa,SAA8C;AAEzE,KAAI,SAAS,MACX,QAAO,QAAQ;CAIjB,MAAM,cAAc,QAAQ,IAAI;AAChC,KAAI,YACF,QAAO;AAIT,OAA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,IAAI,oBAAoB,YAAY;GAE5C,MAAM,kBAAkB,MAAM,QAAQ,QAAQ,EAAE,IAAI,CAAC;GAErD,MAAM,cAAc,IAAI,aAAa;AAErC,gBAAYK,gBAAiB,iBAAiB,GAAG;AAEjD,UAAO;WACA,OAAO;AACd,SAAM,IAAI,iBACR,sBAAsB,MACtB,qBACA,iBAAiB,QAAQ,QAAQ,OAClC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8CP,SAAgB,yBACd,SACyB;AACzB,QAAO,YAAY;AACjB,SAAO,MAAM,YAAY,OAAO,QAAQ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyC5C,SAAgB,oCACd,SACgB;AAChB,cAAa"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@langchain/deno",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "Deno Sandbox backend for deepagents",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -41,8 +41,8 @@
|
|
|
41
41
|
"tsx": "^4.21.0",
|
|
42
42
|
"typescript": "^5.9.3",
|
|
43
43
|
"vitest": "^4.0.18",
|
|
44
|
-
"
|
|
45
|
-
"
|
|
44
|
+
"@langchain/standard-tests": "0.0.2",
|
|
45
|
+
"deepagents": "1.7.3"
|
|
46
46
|
},
|
|
47
47
|
"exports": {
|
|
48
48
|
".": {
|