/** * E2B Sandbox Provider - Clean Architecture * * @requires @e2b/code-interpreter >= 1.0.0 or e2b >= 1.0.0 * @requires Node.js >= 18 (for ReadableStream support) * * Design principles: * - Single way to do things (no dual methods) * - Options objects only (no positional args) * - All interface methods required (no optional ?) * - Configuration externalized (no hardcoded mappings) * - Clear naming (run = blocking, spawn = background) */ /** Result of a completed sandbox command */ interface SandboxCommandResult { exitCode: number; stdout: string; stderr: string; } /** Handle to a running background process in sandbox */ interface SandboxCommandHandle { readonly processId: string; wait(): Promise; kill(): Promise; } /** Information about a running process */ interface ProcessInfo { processId: string; cmd: string; args: string[]; envs: Record; cwd?: string; tag?: string; } /** Sandbox metadata and lifecycle info */ interface SandboxInfo { sandboxId: string; image: string; name?: string; metadata: Record; startedAt: string; /** End time (undefined for running sandboxes) */ endAt?: string; } /** File or directory entry info */ interface FileInfo { name: string; path: string; type: "file" | "dir"; } /** Filesystem event from watchDir */ interface FilesystemEvent { /** Relative path to the changed file/directory */ name: string; /** Type of filesystem operation */ type: "create" | "remove" | "rename" | "chmod" | "write"; } /** Handle to stop watching a directory */ interface WatchHandle { stop(): Promise; } /** Options for watching a directory */ interface WatchOptions { recursive?: boolean; timeoutMs?: number; onExit?: (err?: Error) => void | Promise; } /** Options for blocking sandbox command execution */ interface SandboxRunOptions { timeoutMs?: number; envs?: Record; cwd?: string; onStdout?: (data: string) => void; onStderr?: (data: string) => void; } /** Options for spawning background sandbox processes */ interface SandboxSpawnOptions extends SandboxRunOptions { stdin?: boolean; } /** Options for connecting to a running process */ interface SandboxConnectOptions { onStdout?: (data: string) => void; onStderr?: (data: string) => void; timeoutMs?: number; } /** Options for creating a sandbox */ interface SandboxCreateOptions { image: string; envs?: Record; metadata?: Record; timeoutMs?: number; workingDirectory?: string; } /** Options for listing sandboxes */ interface SandboxListOptions { state?: ("running" | "paused")[]; metadata?: Record; limit?: number; } /** Command execution capabilities */ interface SandboxCommands { /** Run command and wait for completion */ run(command: string, options?: SandboxRunOptions): Promise; /** Spawn background process, returns handle for control */ spawn(command: string, options?: SandboxSpawnOptions): Promise; /** List running processes */ list(): Promise; /** Connect to existing process by ID */ connect(processId: string, options?: SandboxConnectOptions): Promise; /** Send data to process stdin */ sendStdin(processId: string, data: string): Promise; /** Kill process by ID */ kill(processId: string): Promise; } /** File system operations */ interface SandboxFiles { /** Read file (auto-detects binary by extension) */ read(path: string): Promise; /** Write single file */ write(path: string, content: string | Buffer | ArrayBuffer | Uint8Array): Promise; /** Write multiple files in batch */ writeBatch(files: Array<{ path: string; data: string | Buffer | ArrayBuffer | Uint8Array; }>): Promise; /** Read file as stream */ readStream(path: string): Promise>; /** Write from stream */ writeStream(path: string, stream: ReadableStream): Promise; /** Get pre-signed upload URL for large files (expiration in seconds) */ uploadUrl(path: string, expiresInSeconds?: number): Promise; /** Get pre-signed download URL for large files (expiration in seconds) */ downloadUrl(path: string, expiresInSeconds?: number): Promise; /** Create directory (recursive) */ makeDir(path: string): Promise; /** Check if file or directory exists */ exists(path: string): Promise; /** List directory contents */ list(path: string): Promise; /** Delete file or directory */ remove(path: string): Promise; /** Rename or move file/directory */ rename(oldPath: string, newPath: string): Promise; /** Watch directory for changes */ watchDir(path: string, onEvent: (event: FilesystemEvent) => void | Promise, options?: WatchOptions): Promise; } /** Sandbox instance with full capabilities */ interface SandboxInstance { readonly sandboxId: string; readonly commands: SandboxCommands; readonly files: SandboxFiles; /** Get public URL for port */ getHost(port: number): Promise; /** Check if sandbox is running */ isRunning(): Promise; /** Get sandbox metadata and timing */ getInfo(): Promise; /** Terminate sandbox */ kill(): Promise; /** Pause sandbox (preserves state) */ pause(): Promise; } /** Sandbox lifecycle management */ interface SandboxProvider { /** Provider type identifier */ readonly providerType: string; /** Create new sandbox */ create(options: SandboxCreateOptions): Promise; /** Connect to existing sandbox */ connect(sandboxId: string, timeoutMs?: number): Promise; /** List sandboxes (first page only, up to limit) */ list(options?: SandboxListOptions): Promise; } interface E2BConfig { /** E2B API key. Default: reads from E2B_API_KEY env var */ apiKey?: string; /** @internal E2B API base URL override, used for managed gateway routing */ apiUrl?: string; defaultTimeoutMs?: number; /** E2B template ID (default: 'evolve-all'). Create custom templates at https://e2b.dev/docs/sandbox-template */ templateId?: string; } /** Internal resolved config with required apiKey */ interface ResolvedE2BConfig { apiKey: string; apiUrl?: string; defaultTimeoutMs?: number; templateId?: string; } declare class E2BProvider implements SandboxProvider { readonly providerType: "e2b"; private readonly apiKey; private readonly apiUrl?; private readonly defaultTimeoutMs; private readonly templateId?; constructor(config: ResolvedE2BConfig); create(options: SandboxCreateOptions): Promise; connect(sandboxId: string, timeoutMs?: number): Promise; list(options?: SandboxListOptions): Promise; } /** * Create E2B sandbox provider. * * @param config - Optional configuration. If apiKey not provided, reads from E2B_API_KEY env var. * @throws Error if apiKey cannot be resolved */ declare function createE2BProvider(config?: E2BConfig): SandboxProvider; export { type E2BConfig, E2BProvider, type FileInfo, type FilesystemEvent, type ProcessInfo, type SandboxCommandHandle, type SandboxCommandResult, type SandboxCommands, type SandboxConnectOptions, type SandboxCreateOptions, type SandboxFiles, type SandboxInfo, type SandboxInstance, type SandboxListOptions, type SandboxProvider, type SandboxRunOptions, type SandboxSpawnOptions, type WatchHandle, type WatchOptions, createE2BProvider };