Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
[WIP] Add fs and process
Tomas Valentadf8e0e263063
diff --git a/packages/js-sdk/src/error.ts b/packages/js-sdk/src/error.tsindex c3b575f27..410cdb22e 100644--- a/packages/js-sdk/src/error.ts+++ b/packages/js-sdk/src/error.ts@@ -1,20 +1,6 @@-export class TimeoutError extends Error {- constructor(message: any) {- super(message)- this.name = 'TimeoutError'- }-}-export class AuthenticationError extends Error {constructor(message: any) {super(message)this.name = 'AuthenticationError'}}--export class CurrentWorkingDirectoryDoesntExistError extends Error {- constructor(message: any) {- super(message)- this.name = 'CurrentWorkingDirectoryDoesntExistError'- }-}\ No newline at end of filediff --git a/packages/js-sdk/src/index.ts b/packages/js-sdk/src/index.tsindex 757da4371..be37605d3 100644--- a/packages/js-sdk/src/index.ts+++ b/packages/js-sdk/src/index.ts@@ -1,8 +1,8 @@export { APIClient, withAPIKey, withAccessToken } from './api'export type { components, paths } from './api'-export { SANDBOX_DOMAIN, API_HOST } from './constants'-export type { SandboxOpts, DownloadFileFormat } from './sandbox'+export { DOMAIN, API_HOST } from './constants'+export type { SandboxOpts, DownloadFileFormat } from './sandboxFile'export type { OpenPort } from './sandbox/codeSnippet'export { Terminal, TerminalOutput } from './sandbox/terminal'export type { TerminalManager } from './sandbox/terminal'diff --git a/packages/js-sdk/src/sandbox/filesystem.ts b/packages/js-sdk/src/sandbox/filesystem.tsdeleted file mode 100644index b826d40fe..000000000--- a/packages/js-sdk/src/sandbox/filesystem.ts+++ /dev/null@@ -1,76 +0,0 @@-import FilesystemWatcher from './filesystemWatcher'-import { CallOpts } from './sandboxConnection'--export const filesystemService = 'filesystem'--export interface FileInfo {- isDir: boolean;- name: string-}--/**- * Manager for interacting with the filesystem in the sandbox.- */-export interface FilesystemManager {- /**- * Writes content to a new file on path.- * @param path Path to a new file. For example '/dirA/dirB/newFile.txt' when creating 'newFile.txt'- * @param content Content to write to a new file- * @param opts Call options- * @param {timeout} [opts.timeout] Timeout for call in milliseconds (default is 60 seconds)- */- write(path: string, content: string, opts?: CallOpts): Promise<void>;- /**- * Write array of bytes to a file.- * This can be used when you cannot represent the data as an UTF-8 string.- *- * A new file will be created if it doesn't exist.- * If the file already exists, it will be overwritten.- *- * @param path path to a file- * @param content byte array representing the content to write- */- writeBytes(path: string, content: Uint8Array): Promise<void>;- /**- * Reads the whole content of a file.- * @param path Path to a file- * @param opts Call options- * @param {timeout} [opts.timeout] Timeout for call in milliseconds (default is 60 seconds)- * @returns Content of a file- */- read(path: string, opts?: CallOpts): Promise<string>;- /**- * Reads the whole content of a file as an array of bytes.- * @param path path to a file- * @returns byte array representing the content of a file- */- readBytes(path: string): Promise<Uint8Array>;- /**- * Removes a file or a directory.- * @param path Path to a file or a directory- * @param opts Call options- * @param {timeout} [opts.timeout] Timeout for call in milliseconds (default is 60 seconds)- */- remove(path: string, opts?: CallOpts): Promise<void>;- /**- * List files in a directory.- * @param path Path to a directory- * @param opts Call options- * @param {timeout} [opts.timeout] Timeout for call in milliseconds (default is 60 seconds)- * @returns Array of files in a directory- */- list(path: string, opts?: CallOpts): Promise<FileInfo[]>;- /**- * Creates a new directory and all directories along the way if needed on the specified pth.- * @param path Path to a new directory. For example '/dirA/dirB' when creating 'dirB'.- * @param opts Call options- * @param {timeout} [opts.timeout] Timeout for call in milliseconds (default is 60 seconds)- */- makeDir(path: string, opts?: CallOpts): Promise<void>;- /**- * Watches directory for filesystem events.- * @param path Path to a directory that will be watched- * @returns New watcher- */- watchDir(path: string): FilesystemWatcher;-}diff --git a/packages/js-sdk/src/sandbox/index.ts b/packages/js-sdk/src/sandbox/index.tsindex e2853835b..244d0d727 100644--- a/packages/js-sdk/src/sandbox/index.ts+++ b/packages/js-sdk/src/sandbox/index.ts@@ -1,635 +1,113 @@-import normalizePath from 'normalize-path'--import { ENVD_PORT, FILE_ROUTE } from '../constants'-import { id } from '../utils/id'-import { createDeferredPromise, formatSettledErrors, withTimeout } from '../utils/promise'-import { codeSnippetService, ScanOpenedPortsHandler as ScanOpenPortsHandler } from './codeSnippet'-import { FileInfo, FilesystemManager, filesystemService } from './filesystem'-import FilesystemWatcher from './filesystemWatcher'-import { Process, ProcessManager, ProcessMessage, ProcessOpts, ProcessOutput, processService } from './process'-import { CallOpts, SandboxConnection, SandboxConnectionOpts } from './sandboxConnection'-import { Terminal, TerminalManager, TerminalOpts, TerminalOutput, terminalService } from './terminal'-import { resolvePath } from '../utils/filesystem'-import { CurrentWorkingDirectoryDoesntExistError } from '../error'--export type DownloadFileFormat =- | 'base64'- | 'blob'- | 'buffer'- | 'arraybuffer'- | 'text'--export interface SandboxOpts extends SandboxConnectionOpts {- timeout?: number;-}---/**- * E2B cloud sandbox gives your agent a full cloud development environment that's sandboxed.- *- * That means:- * - Access to Linux OS- * - Using filesystem (create, list, and delete files and dirs)- * - Run processes- * - Sandboxed - you can run any code- * - Access to the internet- *- * Check usage docs - https://e2b.dev/docs/sandbox/overview- *- * These cloud sandboxes are meant to be used for agents. Like a sandboxed playgrounds, where the agent can do whatever it wants.- *- * Use the {@link Sandbox.create} method to create a new sandbox.- *- * @example- * ```ts- * import { Sandbox } from '@e2b/sdk'- *- * const sandbox = await Sandbox.create()- *- * await sandbox.close()- * ```- */-export class Sandbox2 extends SandboxConnection {- /**- * Terminal manager used to create interactive terminals.- */- readonly terminal: TerminalManager+import { APIOpts } from '../api'+import { RPC_PORT, DEBUG, DOMAIN } from '../constants'+import { getApiKey } from '../utils/apiKey'+import { SandboxApi } from './sandboxApi'+import { SandboxRpc } from './rpc'+import { Logger } from './logger'+import { SandboxMetadata } from './metadata'++export interface SandboxOpts extends APIOpts {+ apiKey?: string/**- * Filesystem manager used to manage files.+ * Domain to use for the API requests. If not provided, the `E2B_DOMAIN` environment variable will be used.*/- readonly filesystem: FilesystemManager+ domain?: string/**- * Process manager used to run commands.+ * A dictionary of strings that is stored alongside the running sandbox.+ * You can see this metadata when you list running sandboxes.*/- readonly process: ProcessManager-- private readonly onScanPorts?: ScanOpenPortsHandler-- /**- * Use `Sandbox.create()` instead.- *- * @hidden- * @hide- * @internal- * @access protected- */- constructor(opts?: SandboxOpts, protected createCalled: boolean = false) {- opts = opts || {}- super(opts, createCalled)- this.onScanPorts = opts.onScanPorts-- // Init Filesystem handler- this.filesystem = {- list: async (path, opts?: CallOpts) => {- return (await this._call(- filesystemService,- 'list',- [_resolvePath(path)],- opts,- )) as FileInfo[]- },- read: async (path, opts?: CallOpts) => {- return (await this._call(- filesystemService,- 'read',- [_resolvePath(path)],- opts,- )) as string- },- remove: async (path, opts?: CallOpts) => {- await this._call(- filesystemService,- 'remove',- [_resolvePath(path)],- opts,- )- },- write: async (path, content, opts?: CallOpts) => {- await this._call(- filesystemService,- 'write',- [_resolvePath(path), content],- opts,- )- },- writeBytes: async (path: string, content: Uint8Array) => {- // We need to convert the byte array to base64 string without using browser or node specific APIs.- // This should be achieved by the node polyfills.- const base64Content = Buffer.from(content).toString('base64')- await this._call(filesystemService, 'writeBase64', [- _resolvePath(path),- base64Content,- ])- },- readBytes: async (path: string) => {- const base64Content = (await this._call(- filesystemService,- 'readBase64',- [_resolvePath(path)],- )) as string- // We need to convert the byte array to base64 string without using browser or node specific APIs.- // This should be achieved by the node polyfills.- return Buffer.from(base64Content, 'base64')- },- makeDir: async (path, opts?: CallOpts) => {- await this._call(- filesystemService,- 'makeDir',- [_resolvePath(path)],- opts,- )- },- watchDir: (path: string) => {- this.logger.debug?.(`Watching directory "${path}"`)- const npath = normalizePath(_resolvePath(path))- return new FilesystemWatcher(this, npath)- },- }-- // Init Terminal handler- this.terminal = {- start: async ({- onData,- size,- onExit,- envVars,- cmd,- cwd = '',- terminalID = id(12),- timeout = undefined,- }: TerminalOpts) => {- const start = async ({- onData,- size,- onExit,- envVars,- cmd,- cwd = '',- rootDir,- terminalID = id(12),- }: Omit<TerminalOpts, 'timeout'>) => {- this.logger.debug?.(`Starting terminal "${terminalID}"`)- if (!cwd && rootDir) {- this.logger.warn?.(- 'The rootDir parameter is deprecated, use cwd instead.',- )- cwd = rootDir- }- if (!cwd && this.cwd) {- cwd = this.cwd- }- envVars = envVars || {}- envVars = { ...this.envVars, ...envVars }-- const { promise: terminalExited, resolve: triggerExit } =- createDeferredPromise()-- const output = new TerminalOutput()-- function handleData(data: string) {- output.addData(data)- onData?.(data)- }-- const [onDataSubID, onExitSubID] = await this._handleSubscriptions(- this._subscribe(terminalService, handleData, 'onData', terminalID),- this._subscribe(terminalService, triggerExit, 'onExit', terminalID),- )-- const { promise: unsubscribing, resolve: handleFinishUnsubscribing } =- createDeferredPromise<TerminalOutput>()-- terminalExited.then(async () => {- Promise.allSettled([- this._unsubscribe(onExitSubID),- this._unsubscribe(onDataSubID),- ]).then((results) => {- const errMsg = formatSettledErrors(results)- if (errMsg) {- this.logger.debug?.(errMsg)- }- })-- this.logger.debug?.(`Terminal "${terminalID}" exited`)-- onExit?.()- handleFinishUnsubscribing(output)- })-- try {- await this._call(terminalService, 'start', [- terminalID,- size.cols,- size.rows,- // Handle optional args for old devbookd compatibility- ...(cmd !== undefined ? [envVars, cmd, cwd] : []),- ])- } catch (err) {- triggerExit()- await unsubscribing- throw err- }-- return new Terminal(- terminalID,- this,- triggerExit,- unsubscribing,- output,- )- }- return await withTimeout(- start,- timeout,- )({- onData,- size,- onExit,- envVars,- cmd,- cwd,- terminalID,- })- },- }-- // Init Process handler- this.process = {- start: async (optsOrCmd: string | ProcessOpts) => {- const opts = typeof optsOrCmd === 'string' ? { cmd: optsOrCmd } : optsOrCmd- const start = async ({- cmd,- onStdout,- onStderr,- onExit,- envVars = {},- cwd = '',- rootDir,- processID = id(12),- }: Omit<ProcessOpts, 'timeout'>) => {- if (!cwd && rootDir) {- this.logger.warn?.(- 'The rootDir parameter is deprecated, use cwd instead.',- )- cwd = rootDir- }- if (!cwd && this.cwd) {- cwd = this.cwd- }- if (!cmd) throw new Error('cmd is required')-- envVars = envVars || {}- envVars = { ...this.envVars, ...envVars }-- this.logger.debug?.(`Starting process "${processID}", cmd: "${cmd}"`)-- const { promise: processExited, resolve: triggerExit } =- createDeferredPromise()-- const output = new ProcessOutput()- const handleExit = (exitCode: number) => {- output.setExitCode(exitCode)- triggerExit()- }-- const handleStdout = (data: { line: string; timestamp: number }) => {- const message = new ProcessMessage(- data.line,- data.timestamp,- false,- )- output.addStdout(message)-- if (onStdout) {- onStdout(message)- } else if ((this.opts as SandboxOpts).onStdout) {- // @ts-expect-error TS2339- this.opts.onStdout(message)- }- }-- const handleStderr = (data: { line: string; timestamp: number }) => {- const message = new ProcessMessage(data.line, data.timestamp, true)- output.addStderr(message)-- if (onStderr) {- onStderr(message)- } else if ((this.opts as SandboxOpts).onStderr) {- // @ts-expect-error TS2339- this.opts.onStderr(message)- }- }+ metadata?: SandboxMetadata+ logger?: Logger+ timeout?: number+}- const [onExitSubID, onStdoutSubID, onStderrSubID] =- await this._handleSubscriptions(- this._subscribe(processService, handleExit, 'onExit', processID),- this._subscribe(- processService,- handleStdout,- 'onStdout',- processID,- ),- this._subscribe(- processService,- handleStderr,- 'onStderr',- processID,- ),- )+export class Sandbox extends SandboxApi {+ protected static readonly defaultTemplate = 'base'- const { promise: unsubscribing, resolve: handleFinishUnsubscribing } =- createDeferredPromise<ProcessOutput>()+ private readonly rpc: SandboxRpc- processExited.then(async () => {- Promise.allSettled([- this._unsubscribe(onExitSubID),- onStdoutSubID ? this._unsubscribe(onStdoutSubID) : undefined,- onStderrSubID ? this._unsubscribe(onStderrSubID) : undefined,- ]).then((results) => {- const errMsg = formatSettledErrors(results)- if (errMsg) {- this.logger.debug?.(errMsg)- }- })+ constructor(readonly sandboxID: string, private readonly opts: Omit<SandboxOpts, 'timeout' | 'metadata' | 'template' | ''>) {+ super()- this.logger.debug?.(`Process "${processID}" exited`)+ const rpcUrl = `${this.debug ? 'http' : 'https'}://${this.getHostname(RPC_PORT)}`- if (onExit) {- onExit(output.exitCode || 0)- } else if ((this.opts as SandboxOpts).onExit) {- // @ts-expect-error TS2339- this.opts.onExit()- }+ this.rpc = new SandboxRpc(rpcUrl)+ }- handleFinishUnsubscribing(output)- })+ get filesystem() {+ return this.rpc.filesystem+ }- try {- await this._call(processService, 'start', [- processID,- cmd,- envVars,- cwd,- ])- } catch (err) {- triggerExit()- await unsubscribing- if (- /error starting process '\w+': fork\/exec \/bin\/bash: no such file or directory/.test((err as Error)?.message)- ) {- throw new CurrentWorkingDirectoryDoesntExistError(- `Failed to start the process. You are trying set 'cwd' to a directory that does not exist.\n${(err as Error)?.message}`- )- }- throw err- }+ get process() {+ return this.rpc.process+ }- return new Process(- processID,- this,- triggerExit,- unsubscribing,- output,- )- }- const timeout = opts.timeout- return await withTimeout(start, timeout)(opts)- },- startAndWait: async (optsOrCmd: string | ProcessOpts) => {- const opts = typeof optsOrCmd === 'string' ? { cmd: optsOrCmd } : optsOrCmd- const process = await this.process.start(opts)- const out = await process.wait()- return out- }- }+ private get debug() {+ return this.opts?.debug ?? DEBUG+ }- const _resolvePath = (path: string): string =>- resolvePath(path, this.cwd, this.logger)+ private get domain() {+ return this.opts?.domain ?? DOMAIN}- /**- * URL that can be used to download or upload file to the sandbox via a multipart/form-data POST request.- * This is useful if you're uploading files directly from the browser.- * The file will be uploaded to the user's home directory with the same name.- * If a file with the same name already exists, it will be overwritten.- */- get fileURL() {- const protocol = this.getProtocol('http', this.opts.__debug_devEnv !== 'local')- const hostname = this.getHostname(this.opts.__debug_port || ENVD_PORT)- return `${protocol}://${hostname}${FILE_ROUTE}`+ private get apiKey() {+ return getApiKey(this.opts?.apiKey)}- /**- * Creates a new Sandbox from the default `base` sandbox template.- * @returns New Sandbox- *- * @example- * ```ts- * const sandbox = await Sandbox.create()- * ```- * @constructs Sandbox- */- static async create<S extends typeof Sandbox>(this: S): Promise<InstanceType<S>>- /**- * Creates a new Sandbox from the template with the specified ID.- * @param template Sandbox template ID or name- * @returns New Sandbox- *- * @example- * ```ts- * const sandbox = await Sandbox.create("sandboxTemplateID")- * ```- */- static async create<S extends typeof Sandbox>(this: S, template: string): Promise<InstanceType<S>>/*** Creates a new Sandbox from the specified options.- * @param opts Sandbox options- * @returns New Sandbox- *- * @example- * ```ts- * const sandbox = await Sandbox.create({- * template: "sandboxTemplate",- * onStdout: console.log,- * })- * ```*/- static async create<S extends typeof Sandbox>(this: S, opts: SandboxOpts): Promise<InstanceType<S>>- static async create(optsOrTemplate?: string | SandboxOpts) {- const opts: SandboxOpts | undefined = typeof optsOrTemplate === 'string' ? { template: optsOrTemplate } : optsOrTemplate- const sandbox = new this(opts, true)- await sandbox._open({ timeout: opts?.timeout })+ static async spawn<S extends typeof Sandbox>(this: S, template = this.defaultTemplate, opts: SandboxOpts = {}): Promise<InstanceType<S>> {+ const sandboxID = await this.createSandbox(template, opts)- return sandbox+ return new this(sandboxID, opts) as InstanceType<S>}/**- * Reconnects to an existing Sandbox.- * @param sandboxID Sandbox ID- * @returns Existing Sandbox- *- * @example- * ```ts- * const sandbox = await Sandbox.create()- * const sandboxID = sandbox.id- *- * await sandbox.keepAlive(300 * 1000)- * await sandbox.close()- *- * const reconnectedSandbox = await Sandbox.reconnect(sandboxID)- * ```+ * Connects to an existing Sandbox.*/- static async reconnect<S extends typeof Sandbox>(this: S, sandboxID: string): Promise<InstanceType<S>>- /**- * Reconnects to an existing Sandbox.- * @param opts Sandbox options- * @returns Existing Sandbox- *- * @example- * ```ts- * const sandbox = await Sandbox.create()- * const sandboxID = sandbox.id- *- * await sandbox.keepAlive(300 * 1000)- * await sandbox.close()- *- * const reconnectedSandbox = await Sandbox.reconnect({- * sandboxID,- * })- * ```- */- static async reconnect<S extends typeof Sandbox>(this: S, opts: Omit<SandboxOpts, 'id' | 'template'> & { sandboxID: string }): Promise<InstanceType<S>>- static async reconnect<S extends typeof Sandbox>(this: S, sandboxIDorOpts: string | Omit<SandboxOpts, 'id' | 'template'> & { sandboxID: string }): Promise<InstanceType<S>> {- let id: string- let opts: SandboxOpts- if (typeof sandboxIDorOpts === 'string') {- id = sandboxIDorOpts- opts = {}- } else {- id = sandboxIDorOpts.sandboxID- opts = sandboxIDorOpts- }-- const sandboxIDAndClientID = id.split('-')- const sandboxID = sandboxIDAndClientID[0]- const clientID = sandboxIDAndClientID[1]- opts.__sandbox = { sandboxID, clientID, templateID: 'unknown' }-- const sandbox = new this(opts, true) as InstanceType<S>- await sandbox._open({ timeout: opts?.timeout })-- return sandbox+ static async connect<S extends typeof Sandbox>(this: S, sandboxID: string, opts: Omit<SandboxOpts, 'id' | 'template'> = {}): Promise<InstanceType<S>> {+ return new this(sandboxID, opts) as InstanceType<S>}/**- * Uploads a file to the sandbox.- * The file will be uploaded to the user's home directory with the same name.- * If a file with the same name already exists, it will be overwritten.- *- * **You can use the {@link Sandbox.fileURL} property and upload file directly via POST multipart/form-data**- *- */- async uploadFile(file: Buffer | Blob, filename: string) {- const body = new FormData()-- const blob =- file instanceof Blob- ? file- : new Blob([file], { type: 'application/octet-stream' })-- body.append('file', blob, filename)-- // TODO: Ensure the this is bound in this function- const response = await fetch(this.fileURL, {- method: 'POST',- body,- })+ * Get the hostname for the sandbox or for the specified sandbox's port.+ *+ * `getHostname` method requires `this` context - you may need to bind it.+ *+ * @param port Specify if you want to connect to a specific port of the sandbox+ * @returns Hostname of the sandbox or sandbox's port+ */+ getHostname(port: number) {+ if (this.debug) {+ if (port) {+ return `localhost:${port}`+ }- if (!response.ok) {- const text = await response.text()- throw new Error(- `Failed to upload file ${response.status} - ${response.statusText}: ${text}`,- )+ return 'localhost'}- return `/home/user/${filename}`- }-- /**- * Downloads a file from the sandbox.- * @param remotePath Path to a file on the sandbox- * @param format Format of the downloaded file- * @returns File content- *- * @example- * ```ts- * const sandbox = await Sandbox.create()- * const content = await sandbox.downloadFile('/home/user/file.txt')- * ```- */- async downloadFile(remotePath: string, format?: DownloadFileFormat) {- remotePath = encodeURIComponent(remotePath)-- // TODO: Ensure the this is bound in this function- const response = await fetch(`${this.fileURL}?path=${remotePath}`)- if (!response.ok) {- const text = await response.text()- throw new Error(`Failed to download file '${remotePath}': ${text}`)+ const hostname = `${this.sandboxID}.${this.domain}`+ if (port) {+ return `${port}-${hostname}`}- switch (format) {- case 'base64':- return Buffer.from(await response.arrayBuffer()).toString('base64')- case 'blob':- return await response.blob()- case 'buffer':- return Buffer.from(await response.arrayBuffer())- case 'arraybuffer':- return await response.arrayBuffer()- case 'text':- return await response.text()- default:- return await response.arrayBuffer()- }+ return hostname}- protected override async _open(opts: CallOpts) {- await super._open(opts)-- const portsHandler = this.onScanPorts- ? (ports: { State: string; Ip: string; Port: number }[]) =>- this.onScanPorts?.(- ports.map((p) => ({ ip: p.Ip, port: p.Port, state: p.State })),- )- : undefined-- await this._handleSubscriptions(- portsHandler- ? this._subscribe(codeSnippetService, portsHandler, 'scanOpenedPorts')- : undefined,- )-- if (this.cwd) {- this.logger.debug?.(`Custom cwd for Sandbox set: "${this.cwd}"`)- await this.filesystem.makeDir(this.cwd)- }-- if ((this.opts as SandboxOpts).onStdout || (this.opts as SandboxOpts).onStderr) {- this.handleStartCmdLogs()- }-- return this+ async setTimeout(timeout: number) {+ await SandboxApi.setTimeout(this.sandboxID, timeout, {+ apiKey: this.apiKey,+ domain: this.domain,+ debug: this.debug,+ })}- private async handleStartCmdLogs() {- try {- await this.process.startAndWait({- cmd: 'sudo journalctl --follow --lines=all -o cat _SYSTEMD_UNIT=start_cmd.service',- envVars: {},- cwd: '/',- })- } catch (err) {- this.logger.debug?.('start command not started', err)- }+ async kill() {+ await SandboxApi.kill(this.sandboxID, {+ apiKey: this.apiKey,+ domain: this.domain,+ debug: this.debug,+ })}}diff --git a/packages/js-sdk/src/sandbox/logger.ts b/packages/js-sdk/src/sandbox/logger.tsnew file mode 100644index 000000000..2e1fed2b7--- /dev/null+++ b/packages/js-sdk/src/sandbox/logger.ts@@ -0,0 +1,6 @@+export interface Logger {+ debug?: (message: string, ...args: unknown[]) => void+ info?: (message: string, ...args: unknown[]) => void+ warn?: (message: string, ...args: unknown[]) => void+ error?: (message: string, ...args: unknown[]) => void+}diff --git a/packages/js-sdk/src/sandbox/metadata.ts b/packages/js-sdk/src/sandbox/metadata.tsnew file mode 100644index 000000000..d3d8d0725--- /dev/null+++ b/packages/js-sdk/src/sandbox/metadata.ts@@ -0,0 +1,3 @@+export type SandboxMetadata = {+ [key: string]: string+}diff --git a/packages/js-sdk/src/sandbox/rpc/filesystem.ts b/packages/js-sdk/src/sandbox/rpc/filesystem.tsnew file mode 100644index 000000000..499c0d26d--- /dev/null+++ b/packages/js-sdk/src/sandbox/rpc/filesystem.ts@@ -0,0 +1,81 @@+import { PlainMessage } from '@bufbuild/protobuf'+import {+ createPromiseClient,+ Transport,+ PromiseClient,+} from '@connectrpc/connect'++import { FilesystemService } from '../../envd/filesystem/v1/filesystem_connect'+import {+ CopyRequest,+ FilesystemEvent,+ ListDirRequest,+ ListDirResponse,+ RemoveRequest,+ RenameRequest,+ StatRequest,+ StatResponse,+ WatchRequest,+} from '../../envd/filesystem/v1/filesystem_pb'++// TODO: Resolve cwd and provide sane defaults+export class Filesystem {+ private readonly service: PromiseClient<typeof FilesystemService> = createPromiseClient(FilesystemService, this.transport)++ constructor(private readonly transport: Transport) { }++ async copy(params: PlainMessage<CopyRequest>): Promise<void> {+ await this.service.copy(params)+ }++ async createDir(params: PlainMessage<CopyRequest>): Promise<void> {+ await this.service.createDir(params)+ }++ async listDir(params: PlainMessage<ListDirRequest>): Promise<ListDirResponse['entries']> {+ const res = await this.service.listDir(params)+ return res.entries+ }++ async remove(params: PlainMessage<RemoveRequest>): Promise<void> {+ await this.service.remove(params)+ }++ async rename(params: PlainMessage<RenameRequest>): Promise<void> {+ await this.service.rename(params)+ }++ async exists(params: PlainMessage<StatRequest>): Promise<StatResponse['entry']> {+ const res = await this.service.stat(params)+ return res.entry+ }++ async watch(+ params: PlainMessage<WatchRequest>,+ {+ onEvent,+ }: {+ onEvent?: (event: PlainMessage<FilesystemEvent>) => any,+ }+ ): Promise<{ stop: () => void }> {+ const controller = new AbortController()++ const req = this.service.watch(params, {+ signal: controller.signal,+ })++ async function processStream() {+ for await (const event of req) {+ if (event.event) {+ onEvent?.(event.event)+ }+ }+ }++ processStream()++ return {+ stop: () => controller.abort(),+ }+ }+}diff --git a/packages/js-sdk/src/sandbox/rpc/index.ts b/packages/js-sdk/src/sandbox/rpc/index.tsnew file mode 100644index 000000000..70707d34c--- /dev/null+++ b/packages/js-sdk/src/sandbox/rpc/index.ts@@ -0,0 +1,21 @@+import { Transport } from '@connectrpc/connect'+import { createConnectTransport } from '@connectrpc/connect-web'++import { Filesystem } from './filesystem'+import { Process } from './process'++export class SandboxRpc {+ readonly filesystem: Filesystem+ readonly process: Process++ private readonly transport: Transport++ constructor(baseUrl: string) {+ this.transport = createConnectTransport({+ baseUrl,+ })++ this.filesystem = new Filesystem(this.transport)+ this.process = new Process(this.transport)+ }+}diff --git a/packages/js-sdk/src/sandbox/rpc/process.ts b/packages/js-sdk/src/sandbox/rpc/process.tsnew file mode 100644index 000000000..beaa4a6ae--- /dev/null+++ b/packages/js-sdk/src/sandbox/rpc/process.ts@@ -0,0 +1,218 @@+import { PlainMessage } from '@bufbuild/protobuf'+import {+ createPromiseClient,+ PromiseClient,+ Transport,+} from '@connectrpc/connect'++import { ProcessService } from '../../envd/process/v1/process_connect'+import {+ ListProcessesRequest,+ ListProcessesResponse,+ ProcessEvent_EndEvent,+ ProcessEvent_StartEvent,+ ReconnectProcessRequest,+ ReconnectProcessResponse,+ SendProcessInputRequest,+ SendProcessInputStreamRequest,+ SendProcessSignalRequest,+ Signal,+ StartProcessRequest,+ StartProcessResponse,+ UpdateProcessRequest,+} from '../../envd/process/v1/process_pb'+import { concatUint8Arrays } from '../../utils/array'+import { createDeferredPromise } from '../../utils/promise'++// TODO: Sane defaults+// TODO: Reconnect vs connect+// TODO: Add timeout to wait+// TODO: How to handle start + wait+// TODO: Move cwd resolution to envd?+export class Process {+ private readonly service: PromiseClient<typeof ProcessService> = createPromiseClient(ProcessService, this.transport)++ constructor(private readonly transport: Transport) { }++ async list(params: PlainMessage<ListProcessesRequest>): Promise<ListProcessesResponse['processes']> {+ const res = await this.service.listProcesses(params)+ return res.processes+ }++ async start(+ params: PlainMessage<StartProcessRequest>,+ {+ onStderr,+ onStdout,+ }: {+ onStdout?: (data: string) => any,+ onStderr?: (data: string) => any,+ }+ ) {+ const controller = new AbortController()++ const res = this.service.startProcess(params, {+ signal: controller.signal,+ })++ const {+ start,+ wait,+ } = await this.handleProcessStream(res, {+ onStdout,+ onStderr,+ })++ const startEvent = await start++ return {+ pid: startEvent.pid,+ disconnect: () => controller.abort(),+ wait,+ kill: () => this.sendSignal({+ process: {+ selector: {+ case: 'pid',+ value: startEvent.pid,+ }+ },+ signal: Signal.SIGKILL,+ }),+ }+ }++ private async handleProcessStream(+ stream: AsyncIterable<StartProcessResponse | ReconnectProcessResponse>,+ {+ onStderr,+ onStdout,+ }: {+ onStdout?: (data: string) => any,+ onStderr?: (data: string) => any,+ }+ ) {+ const start = createDeferredPromise<ProcessEvent_StartEvent>()+ const exit = createDeferredPromise<ProcessEvent_EndEvent>()++ const stdout: Uint8Array[] = []+ const stderr: Uint8Array[] = []++ async function processStream() {+ try {+ for await (const event of stream) {+ switch (event.event?.event.case) {+ case 'start':+ start.resolve(event.event.event.value)+ break+ case 'data':+ switch (event.event.event.value.output.case) {+ case 'stdout':+ stdout.push(event.event.event.value.output.value)+ onStdout?.(event.event.event.value.output.value.toString())+ break+ case 'stderr':+ stderr.push(event.event.event.value.output.value)+ onStderr?.(event.event.event.value.output.value.toString())+ break+ }+ break+ case 'end':+ exit.resolve(event.event.event.value)+ break+ }++ }+ } catch (error) {+ start.reject(error)+ exit.reject(error)+ }+ }++ processStream()++ return {+ start: start.promise,+ wait: async () => {+ const result = await exit.promise+ return {+ ...result,+ get stdout() {+ return concatUint8Arrays(stdout).toString()+ },+ get stderr() {+ return concatUint8Arrays(stderr).toString()+ },+ }+ },+ }+ }++ private async reconnect(+ params: PlainMessage<ReconnectProcessRequest>,+ {+ onStderr,+ onStdout,+ }: {+ onStdout?: (data: string) => any,+ onStderr?: (data: string) => any,+ }+ ) {+ const pid = params.process?.selector.case === 'pid' ? params.process?.selector.value : undefined++ if (pid === undefined) {+ throw new Error('Invalid process PID')+ }++ const controller = new AbortController()++ const res = this.service.reconnectProcess(params, {+ signal: controller.signal,+ })++ const {+ wait,+ } = await this.handleProcessStream(res, {+ onStdout,+ onStderr,+ })++ return {+ pid,+ disconnect: () => controller.abort(),+ wait,+ kill: () => this.sendSignal({+ process: {+ selector: {+ case: 'pid',+ value: pid,+ }+ },+ signal: Signal.SIGKILL,+ }),+ }+ }++ private async sendSignal(params: PlainMessage<SendProcessSignalRequest>): Promise<void> {+ await this.service.sendProcessSignal(params)+ }++ private async sendInput(params: PlainMessage<SendProcessInputRequest>): Promise<void> {+ await this.service.sendProcessInput(params)+ }++ private async update(params: PlainMessage<UpdateProcessRequest>): Promise<void> {+ await this.service.updateProcess(params)+ }++ private async streamInput(params: AsyncIterable<PlainMessage<SendProcessInputStreamRequest>>) {+ const controller = new AbortController()++ this.service.sendProcessInputStream(params, {+ signal: controller.signal,+ })++ return {+ stop: () => controller.abort(),+ }+ }+}diff --git a/packages/js-sdk/src/sandbox/sandbox.ts b/packages/js-sdk/src/sandbox/sandbox.tsdeleted file mode 100644index 720e134d7..000000000--- a/packages/js-sdk/src/sandbox/sandbox.ts+++ /dev/null@@ -1,170 +0,0 @@-import { APIOpts } from '../api'-import { RPC_PORT, DEBUG, DOMAIN } from '../constants'-import { getApiKey } from '../utils/apiKey'-import { SandboxApi } from './sandboxApi'-import { SandboxRpc } from './sandboxRpc'--export interface EnvVars {- [key: string]: string-}--export interface Logger {- debug?: (message: string, ...args: unknown[]) => void- info?: (message: string, ...args: unknown[]) => void- warn?: (message: string, ...args: unknown[]) => void- error?: (message: string, ...args: unknown[]) => void-}--export type SandboxMetadata = {- [key: string]: string-}--export interface SandboxOpts extends APIOpts {- /**- * Sandbox Template ID or name.- */- template?: string- apiKey?: string- /**- * Domain to use for the API requests. If not provided, the `E2B_DOMAIN` environment variable will be used.- */- domain?: string- cwd?: string- envVars?: EnvVars- /**- * A dictionary of strings that is stored alongside the running sandbox.- * You can see this metadata when you list running sandboxes.- */- metadata?: SandboxMetadata- logger?: Logger- timeout?: number-}--export class Sandbox extends SandboxApi {- private readonly rpc: SandboxRpc-- constructor(private readonly id: string, private readonly opts: Omit<SandboxOpts, 'timeout' | 'metadata' | 'template' | ''>) {- super()-- const hostname = this.getHostname(RPC_PORT)- const rpcUrl = `${this.debug ? 'http' : 'https'}://${hostname}`-- this.rpc = new SandboxRpc(rpcUrl)- }-- private get debug() {- return this.opts?.debug ?? DEBUG- }-- private get domain() {- return this.opts?.domain ?? DOMAIN- }-- private get apiKey() {- return getApiKey(this.opts?.apiKey)- }-- private get envVars() {- // return this.opts?.envVars ?? {}- }-- private get cwd() {- // return this.opts?.cwd ?? process.cwd()- }-- /**- * Creates a new Sandbox from the default `base` sandbox template.- * @returns New Sandbox- *- * @constructs Sandbox- */- static async spawn<S extends typeof Sandbox>(this: S): Promise<InstanceType<S>>- /**- * Creates a new Sandbox from the template with the specified ID.- * @param template Sandbox template ID or name- * @returns New Sandbox- */- static async spawn<S extends typeof Sandbox>(this: S, template: string): Promise<InstanceType<S>>- /**- * Creates a new Sandbox from the specified options.- * @param opts Sandbox options- * @returns New Sandbox- */- static async spawn<S extends typeof Sandbox>(this: S, opts: SandboxOpts): Promise<InstanceType<S>>- static async spawn(optsOrTemplate?: string | SandboxOpts) {- const opts: SandboxOpts = typeof optsOrTemplate === 'string'- ? { template: optsOrTemplate }- : optsOrTemplate-- const sandboxID = await this.spawn(opts.template, opts)-- return new this(sandboxID)- }-- /**- * Reconnects to an existing Sandbox.- * @param sandboxID Sandbox ID- * @returns Existing Sandbox- */- static async connect<S extends typeof Sandbox>(this: S, sandboxID: string): Promise<InstanceType<S>>- /**- * Reconnects to an existing Sandbox.- * @param opts Sandbox options- * @returns Existing Sandbox- */- static async connect<S extends typeof Sandbox>(this: S, opts: Omit<SandboxOpts, 'id' | 'template'> & { sandboxID: string }): Promise<InstanceType<S>>- static async connect<S extends typeof Sandbox>(this: S, sandboxIDorOpts: string | Omit<SandboxOpts, 'id' | 'template'> & { sandboxID: string }): Promise<InstanceType<S>> {- let id: string- let opts: SandboxOpts- if (typeof sandboxIDorOpts === 'string') {- id = sandboxIDorOpts- opts = {}- } else {- id = sandboxIDorOpts.sandboxID- opts = sandboxIDorOpts- }-- return new this(sandboxID) as InstanceType<S>- }-- /**- * Get the hostname for the sandbox or for the specified sandbox's port.- *- * `getHostname` method requires `this` context - you may need to bind it.- *- * @param port Specify if you want to connect to a specific port of the sandbox- * @returns Hostname of the sandbox or sandbox's port- */- getHostname(port: number) {- if (this.debug) {- if (port) {- return `localhost:${port}`- }-- return 'localhost'- }-- const hostname = `${this.id}.${this.domain}`- if (port) {- return `${port}-${hostname}`- }-- return hostname- }-- async setTimeout(timeout: number) {- await SandboxApi.setTimeout(this.id, timeout, {- apiKey: this.apiKey,- domain: this.domain,- debug: this.debug,- })- }-- async kill() {- await SandboxApi.kill(this.id, {- apiKey: this.apiKey,- domain: this.domain,- debug: this.debug,- })- }-}diff --git a/packages/js-sdk/src/sandbox/sandboxApi.ts b/packages/js-sdk/src/sandbox/sandboxApi.tsindex 5c80b9f15..ceabcb7be 100644--- a/packages/js-sdk/src/sandbox/sandboxApi.ts+++ b/packages/js-sdk/src/sandbox/sandboxApi.ts@@ -52,7 +52,7 @@ export class SandboxApi {}))}- protected static async spawn(+ protected static async createSandbox(template: string,opts: APIOpts & {metadata?: SandboxMetadata,diff --git a/packages/js-sdk/src/sandbox/sandboxConnection.ts b/packages/js-sdk/src/sandbox/sandboxConnection.tsdeleted file mode 100644index f882cfbbe..000000000--- a/packages/js-sdk/src/sandbox/sandboxConnection.ts+++ /dev/null@@ -1,522 +0,0 @@-import { IRpcNotification } from './sandboxRpc'--import { APIClient, components, withAPIKey } from '../api'-import {- ENVD_PORT,- SANDBOX_REFRESH_PERIOD,- WS_RECONNECT_INTERVAL,- WS_ROUTE,-} from '../constants'-import {- assertFulfilled,- formatSettledErrors,- withTimeout,-} from '../utils/promise'-import wait from '../utils/wait'-import { codeSnippetService } from './codeSnippet'-import { filesystemService } from './filesystem'-import { processService } from './process'-import { terminalService } from './terminal'-import { EnvVars } from './envVars'-import { getApiKey } from '../utils/apiKey'-import { watchDirect } from '../gen'--type Service =- | typeof processService- | typeof codeSnippetService- | typeof filesystemService- | typeof terminalService---export interface Logger {- debug?: (message: string, ...args: unknown[]) => void- info?: (message: string, ...args: unknown[]) => void- warn?: (message: string, ...args: unknown[]) => void- error?: (message: string, ...args: unknown[]) => void-}--export type SandboxMetadata = {- [key: string]: string-}--export interface RunningSandbox {- sandboxID: string- templateID: string- alias?: string- metadata?: SandboxMetadata- startedAt: Date-}--export interface SandboxConnectionOpts {- /**- * Sandbox Template ID or name.- */- template?: string- apiKey?: string- /**- * Domain to use for the API requests. If not provided, the `E2B_DOMAIN` environment variable will be used.- */- domain?: string- cwd?: string- envVars?: EnvVars- /**- * A dictionary of strings that is stored alongside the running sandbox.- * You can see this metadata when you list running sandboxes.- */- metadata?: SandboxMetadata- logger?: Logger- __sandbox?: components['schemas']['Sandbox']- __debug_hostname?: string- __debug_port?: number- __debug_devEnv?: 'remote' | 'local'-}--export class SandboxConnection {- /**- * Default working directory used in the sandbox.- *- * You can change the working directory by setting the `cwd` property.- **/- cwd?: string- /**- * Default environment variables used in the sandbox.- *- * You can change the environment variables by setting the `envVars` property.- **/- envVars: EnvVars-- protected readonly logger: Logger- protected readonly sandbox?: components['schemas']['Sandbox']-- private readonly apiKey: string- private readonly client: APIClient-- // let's keep opts readonly, but public - for convenience, mainly when debugging- protected constructor(- readonly opts: SandboxConnectionOpts,- protected createCalled: boolean,- ) {- if (!createCalled) {- throw new Error(- "Sandbox can't be instantiated directly, use a `.create()` method instead of calling `new` to get a new sandbox.\n\n" +- 'Example of correct usage:\n' +- '```\n' +- "import { CodeInterpreter } from '@e2b/code-interpreter'\n" +- 'const myCodeInterpreter = await CodeInterpreter.create()\n' +- '``',- )- }-- this.sandbox = opts.__sandbox- this.apiKey = getApiKey(opts.apiKey)-- this.client = new APIClient({ domain: opts.domain })-- this.cwd = opts.cwd- if (this.cwd && this.cwd.startsWith('~')) {- this.cwd = this.cwd.replace('~', '/home/user')- }-- const defaultEnvVars = { PYTHONUNBUFFERED: '1' }-- this.envVars = { ...defaultEnvVars, ...(opts.envVars || {}) }-- this.logger = opts.logger ?? {- // by default, we log to the console- // we don't log debug messages by default- info: console.info,- warn: console.warn,- error: console.error,- }- this.logger.debug?.(`Sandbox "${this.templateID}" initialized`)- }-- /**- * ID of the sandbox.- *- * You can use this ID to reconnect to the sandbox later.- */- get id() {- return `${this.sandbox?.sandboxID}-${this.sandbox?.clientID}`- }-- private get templateID(): string {- return this.opts.template || this.opts.id || 'base'- }-- private get refreshSandbox() {- return withAPIKey(- this.client.api- .path('/sandboxes/{sandboxID}/refreshes')- .method('post')- .create(),- )- }-- private get createSandbox() {- return withAPIKey(- this.client.api.path('/sandboxes').method('post').create(),- )- }-- /**- * Keep the sandbox alive for the specified duration.- *- * `keepAlive` method requires `this` context - you may need to bind it.- * @param duration Duration in milliseconds. Must be between 0 and 3600000 milliseconds- * @returns Promise that resolves when the sandbox is kept alive- */- public async keepAlive(duration: number) {- duration = Math.round(duration / 1000)-- if (duration < 0 || duration > 3600) {- throw new Error('Duration must be between 0 and 3600 seconds')- }-- if (!this.sandbox) {- throw new Error('Cannot keep alive - sandbox is not initialized')- }- await this.refreshSandbox(this.apiKey, {- sandboxID: this.sandbox?.sandboxID,- duration,- })- }-- /**- * Get the hostname for the sandbox or for the specified sandbox's port.- *- * `getHostname` method requires `this` context - you may need to bind it.- *- * @param port Specify if you want to connect to a specific port of the sandbox- * @returns Hostname of the sandbox or sandbox's port- */- getHostname(port?: number) {- if (this.opts.__debug_hostname) {- // Debugging remotely (with GitPod) and on local needs different formats of the hostname.- if (port && this.opts.__debug_devEnv === 'remote') {- return `${port}-${this.opts.__debug_hostname}`- } else if (port) {- return `${this.opts.__debug_hostname}:${port}`- } else {- return this.opts.__debug_hostname- }- }-- if (!this.sandbox) {- throw new Error(- "Cannot get sandbox's hostname - sandbox is not initialized",- )- }-- const hostname = `${this.id}.${this.client.domain}`- if (port) {- return `${port}-${hostname}`- } else {- return hostname- }- }-- /**- * The function decides whether to use the secure or insecure protocol.- * @param baseProtocol Specify the specific protocol you want to use. Do not include the `s` in `https` or `wss`.- * @param secure Specify if you want to use the secure protocol- * @returns Protocol for the connection to the sandbox- */- getProtocol(- baseProtocol: string = 'http',- secure: boolean = this.client.secure,- ) {- return secure ? `${baseProtocol}s` : baseProtocol- }-- /**- * Close the connection to the sandbox- *- * `close` method requires `this` context - you may need to bind it.- */- async close() {- if (this.isOpen) {- this.logger.debug?.(`Closing sandbox "${this.id}"`)- this.isOpen = false-- // This is `ws` way of closing connection- this.rpc.ws?.terminate?.()- // This is the browser WebSocket way of closing connection- this.rpc.ws?.close?.()- this.subscribers = []-- this.logger.debug?.('Disconnected from the sandbox')- }- }-- // eslint-disable-next-line @typescript-eslint/no-explicit-any- async _call(- service: Service,- method: string,- params?: any[],- opts?: CallOpts,- ) {- this.logger.debug?.(`Calling "${service}_${method}" with params:`, params)-- // Without the async function, the `this` context is lost.- // eslint-disable-next-line @typescript-eslint/no-explicit-any- const call = async (method: string, params?: any[]) =>- await this.rpc.call(method, params)-- return await withTimeout(call, opts?.timeout)(- `${service}_${method}`,- params,- )- }-- async _handleSubscriptions<- T extends (ReturnType<SandboxConnection['_subscribe']> | undefined)[],- >(- ...subs: T- ): Promise<{- [P in keyof T]: Awaited<T[P]>- }> {- const results = await Promise.allSettled(subs)-- if (results.every((r) => r.status === 'fulfilled')) {- return results.map((r) =>- r.status === 'fulfilled' ? r.value : undefined,- ) as {- [P in keyof T]: Awaited<T[P]>- }- }-- await Promise.all(- results- .filter(assertFulfilled)- .map((r) => (r.value ? this._unsubscribe(r.value) : undefined)),- )-- throw new Error(formatSettledErrors(results))- }-- // eslint-disable-next-line @typescript-eslint/member-ordering- async _unsubscribe(subID: string) {- const subscription = this.subscribers.find((s) => s.subID === subID)- if (!subscription) return-- await this._call(subscription.service, 'unsubscribe', [subscription.subID])-- this.subscribers = this.subscribers.filter((s) => s !== subscription)- this.logger.debug?.(- `Unsubscribed '${subID}' from '${subscription.service}'`,- )- }-- // eslint-disable-next-line @typescript-eslint/no-explicit-any,@typescript-eslint/member-ordering- async _subscribe(- service: Service,- handler: SubscriptionHandler,- method: string,- // eslint-disable-next-line @typescript-eslint/no-explicit-any- ...params: any[]- ) {- const subID = await this._call(service, 'subscribe', [method, ...params])-- if (typeof subID !== 'string') {- throw new Error(- `Cannot subscribe to ${service}_${method}${params.length > 0 ? ' with params [' + params.join(', ') + ']' : ''- }. Expected response should have been a subscription ID, instead we got ${JSON.stringify(- subID,- )}`,- )- }-- this.subscribers.push({- handler,- service,- subID,- })- this.logger.debug?.(- `Subscribed to "${service}_${method}"${params.length > 0 ? ' with params [' + params.join(', ') + '] and' : ''- } with id "${subID}"`,- )-- return subID- }-- /**- * Open a connection to a new sandbox- *- * `open` method requires `this` context - you may need to bind it.- * @param opts Call options- * @param {timeout} [opts.timeout] Timeout for sandbox to open in milliseconds (default is 60 seconds)- */- protected async _open(opts: CallOpts) {- const open = async () => {- if (this.isOpen) {- throw new Error('Sandbox connect was already called')- } else {- this.isOpen = true- }- this.logger.debug?.('Opening sandbox...')-- if (!this.sandbox && !this.opts.__debug_hostname) {- try {- const res = await this.createSandbox(this.apiKey, {- templateID: this.templateID,- metadata: this.opts.metadata,- })-- this.sandbox = res.data- this.logger.debug?.(`Acquired sandbox "${this.id}"`)- } catch (e) {- if (e instanceof this.createSandbox.Error) {- const error = e.getActualType()- if (error.status === 400) {- throw new Error(- `Error creating sandbox - (${error.status}) bad request: ${error.data.message}`,- )- }- if (error.status === 401) {- throw new Error(- `Error creating sandbox - (${error.status}) unauthenticated: ${error.data.message}`,- )- }- if (error.status === 500) {- throw new Error(- `Error creating sandbox - (${error.status}) server error: ${error.data.message}`,- )- }- }- throw e- }- }-- if (this.sandbox && !this.opts.__debug_hostname) {- this.refresh(this.sandbox.sandboxID)- }--- console.log('Connecting >>')-- await this.connectRpc()- return this- }-- try {- return await withTimeout(open, opts?.timeout)()- } catch (err) {- await this.close()- throw err- }- }-- private async connectRpc() {- const hostname = this.getHostname(this.opts.__debug_port || ENVD_PORT)- const protocol = this.getProtocol(- 'ws',- this.opts.__debug_devEnv !== 'local',- )- const sandboxURL = `${protocol}://${hostname}${WS_ROUTE}`-- console.log('url', sandboxURL)-- const start = Date.now()--- async function wait(ms: number) {- return new Promise(resolve => setTimeout(resolve, ms))- }-- try {--- const r = await watchDirect(`http://${hostname}`, {- path: '/',- })--- for await (const event of r) {- console.log('dir event', event)- }-- const end = Date.now()- console.log(`LIST - ${end - start}ms`)-- } catch (err) {- console.log(err)- }-- return-- let isFinished = false- let resolveOpening: (() => void) | undefined- let rejectOpening: (() => void) | undefined-- const openingPromise = new Promise<void>((resolve, reject) => {- resolveOpening = () => {- if (isFinished) return- isFinished = true- resolve()- }- rejectOpening = () => {- if (isFinished) return- isFinished = true- reject()- }- })-- this.rpc.onOpen(() => {- this.logger.debug?.(`Connected to sandbox "${this.id}"`)- resolveOpening?.()- })-- this.rpc.onError(async (err) => {- this.logger.debug?.(- `Error in WebSocket of sandbox "${this.id}": ${err.message ?? err.code ?? err.toString()- }. Trying to reconnect...`,- )-- if (this.isOpen) {- await wait(WS_RECONNECT_INTERVAL)- this.logger.debug?.(`Reconnecting to sandbox "${this.id}"`)- try {- // When the WS connection closes the subscribers in devbookd are removed.- // We want to delete the subscriber handlers here so there are no orphans.- this.subscribers = []- await this.rpc.connect(sandboxURL)- this.logger.debug?.(`Reconnected to sandbox "${this.id}"`)- // eslint-disable-next-line @typescript-eslint/no-explicit-any- } catch (err: any) {- // not warn, because this is somewhat expected behaviour during initialization- this.logger.debug?.(- `Failed reconnecting to sandbox "${this.id}": ${err.message ?? err.code ?? err.toString()- }`,- )- }- } else {- rejectOpening?.()- }- })-- this.rpc.onClose(async () => {- this.logger.debug?.(`WebSocket connection to sandbox "${this.id}" closed`)- })-- this.rpc.onNotification.push(this.handleNotification.bind(this))-- // We invoke the rpc.connect method in a separate promise because when using edge- // the rpc.connect does not throw or end on error.- ; (async () => {- try {- this.logger.debug?.(`Connecting to sandbox "${this.id}"`)- await this.rpc.connect(sandboxURL)- // eslint-disable-next-line @typescript-eslint/no-explicit-any- } catch (err: any) {- // not warn, because this is somewhat expected behaviour during initialization- this.logger.debug?.(- `Error connecting to sandbox "${this.id}": ${err.message ?? err.code ?? err.toString()- }`,- )- }- })()-- await openingPromise- }-}diff --git a/packages/js-sdk/src/sandbox/sandboxFiles.ts b/packages/js-sdk/src/sandbox/sandboxFiles.tsnew file mode 100644index 000000000..c09a4fb37--- /dev/null+++ b/packages/js-sdk/src/sandbox/sandboxFiles.ts@@ -0,0 +1,98 @@++export type DownloadFileFormat =+ | 'base64'+ | 'blob'+ | 'buffer'+ | 'arraybuffer'+ | 'text'+++export class SandboxFiles {+++++ constructor(private readonly url: string) {++ }+++ /**+* URL that can be used to download or upload file to the sandbox via a multipart/form-data POST request.+* This is useful if you're uploading files directly from the browser.+* The file will be uploaded to the user's home directory with the same name.+* If a file with the same name already exists, it will be overwritten.+*/+ get fileURL() {+ const protocol = this.getProtocol('http', this.opts.__debug_devEnv !== 'local')+ const hostname = this.getHostname(this.opts.__debug_port || ENVD_PORT)+ return `${protocol}://${hostname}${FILE_ROUTE}`+ }+++ /**+ * Uploads a file to the sandbox.+ * The file will be uploaded to the user's home directory with the same name.+ * If a file with the same name already exists, it will be overwritten.+ *+ * **You can use the {@link Sandbox.fileURL} property and upload file directly via POST multipart/form-data**+ *+ */+ async uploadFile(file: Buffer | Blob, filename: string) {+ const body = new FormData()++ const blob =+ file instanceof Blob+ ? file+ : new Blob([file], { type: 'application/octet-stream' })++ body.append('file', blob, filename)++ // TODO: Ensure the this is bound in this function+ const response = await fetch(this.fileURL, {+ method: 'POST',+ body,+ })++ if (!response.ok) {+ const text = await response.text()+ throw new Error(+ `Failed to upload file ${response.status} - ${response.statusText}: ${text}`,+ )+ }++ return `/home/user/${filename}`+ }++ /**+ * Downloads a file from the sandbox.+ * @param remotePath Path to a file on the sandbox+ * @param format Format of the downloaded file+ * @returns File content+ */+ async downloadFile(remotePath: string, format?: DownloadFileFormat) {+ remotePath = encodeURIComponent(remotePath)++ // TODO: Ensure the this is bound in this function+ const response = await fetch(`${this.fileURL}?path=${remotePath}`)+ if (!response.ok) {+ const text = await response.text()+ throw new Error(`Failed to download file '${remotePath}': ${text}`)+ }++ switch (format) {+ case 'base64':+ return Buffer.from(await response.arrayBuffer()).toString('base64')+ case 'blob':+ return await response.blob()+ case 'buffer':+ return Buffer.from(await response.arrayBuffer())+ case 'arraybuffer':+ return await response.arrayBuffer()+ case 'text':+ return await response.text()+ default:+ return await response.arrayBuffer()+ }+ }+}\ No newline at end of filediff --git a/packages/js-sdk/src/sandbox/sandboxRpc.ts b/packages/js-sdk/src/sandbox/sandboxRpc.tsdeleted file mode 100644index 712dc7973..000000000--- a/packages/js-sdk/src/sandbox/sandboxRpc.ts+++ /dev/null@@ -1,19 +0,0 @@-import { createPromiseClient, PromiseClient } from '@connectrpc/connect'-import { createConnectTransport } from '@connectrpc/connect-web'--import { FilesystemService } from '../envd/filesystem/v1/filesystem_connect'-import { ProcessService } from '../envd/process/v1/process_connect'--export class SandboxRpc {- readonly filesystem: PromiseClient<typeof FilesystemService>- readonly process: PromiseClient<typeof ProcessService>-- constructor(baseUrl: string) {- const transport = createConnectTransport({- baseUrl,- })-- this.filesystem = createPromiseClient(FilesystemService, transport)- this.process = createPromiseClient(ProcessService, transport)- }-}diff --git a/packages/js-sdk/src/utils/array.ts b/packages/js-sdk/src/utils/array.tsnew file mode 100644index 000000000..d727941b0--- /dev/null+++ b/packages/js-sdk/src/utils/array.ts@@ -0,0 +1,12 @@+export function concatUint8Arrays(arrays: Uint8Array[]) {+ const length = arrays.reduce((acc, cur) => acc + cur.length, 0)+ const result = new Uint8Array(length)+ let offset = 0++ for (const array of arrays) {+ result.set(array, offset)+ offset += array.length+ }++ return result+}\ No newline at end of filediff --git a/packages/js-sdk/src/utils/id.ts b/packages/js-sdk/src/utils/id.tsdeleted file mode 100644index 5c8c33733..000000000--- a/packages/js-sdk/src/utils/id.ts+++ /dev/null@@ -1,9 +0,0 @@-export function id(length: number) {- let result = ''- const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'- const charactersLength = characters.length- for (let i = 0; i < length; i++) {- result += characters.charAt(Math.floor(Math.random() * charactersLength))- }- return result-}diff --git a/packages/js-sdk/src/utils/promise.ts b/packages/js-sdk/src/utils/promise.tsindex 31ed2a49a..56884be84 100644--- a/packages/js-sdk/src/utils/promise.ts+++ b/packages/js-sdk/src/utils/promise.ts@@ -1,24 +1,3 @@-import pTimeout from './timeout'--import { TIMEOUT } from '../constants'--export function assertFulfilled<T>(- item: PromiseSettledResult<T>,-): item is PromiseFulfilledResult<T> {- return item.status === 'fulfilled'-}--export function formatSettledErrors<T>(settled: PromiseSettledResult<T>[]) {- if (settled.every(s => s.status === 'fulfilled')) return-- return settled.reduce((prev, curr, i) => {- if (curr.status === 'rejected') {- return prev + '\n' + `[${i}]: ` + `${JSON.stringify(curr)}`- }- return prev- }, 'errors:\n')-}-export function createDeferredPromise<T = void>() {let resolve: (value: T) => voidlet reject: (reason?: unknown) => void@@ -34,17 +13,4 @@ export function createDeferredPromise<T = void>() {// eslint-disable-next-line @typescript-eslint/no-non-null-assertionresolve: resolve!,}-}--// eslint-disable-next-line @typescript-eslint/no-explicit-any-export function withTimeout<T extends (...args: any[]) => any>(- fn: T,- timeout: number = TIMEOUT,-): T {- if (timeout === undefined || timeout <= 0 || timeout === Number.POSITIVE_INFINITY) {- return fn- }-- return ((...args: T extends (...args: infer A) => any ? A : never) =>- pTimeout(fn(...args), { milliseconds: timeout })) as T-}+}\ No newline at end of filediff --git a/packages/js-sdk/src/utils/timeout.d.ts b/packages/js-sdk/src/utils/timeout.d.tsdeleted file mode 100644index 727799d2f..000000000--- a/packages/js-sdk/src/utils/timeout.d.ts+++ /dev/null@@ -1,151 +0,0 @@-export class TimeoutError extends Error {- readonly name: 'TimeoutError'- constructor(message?: string)-}--export type ClearablePromise<T> = {- /**- Clear the timeout.- */- clear: () => void;-} & Promise<T>--export type Options<ReturnType> = {- /**- Milliseconds before timing out.-- Passing `Infinity` will cause it to never time out.- */- milliseconds: number;-- /**- Do something other than rejecting with an error on timeout.-- You could for example retry:-- @example- ```- import {setTimeout} from 'node:timers/promises';- import pTimeout from 'p-timeout';-- const delayedPromise = () => setTimeout(200);-- await pTimeout(delayedPromise(), {- milliseconds: 50,- fallback: () => {- return pTimeout(delayedPromise(), {- milliseconds: 300- });- },- });- ```- */- fallback?: () => ReturnType | Promise<ReturnType>;-- /**- Specify a custom error message or error to throw when it times out:-- - `message: 'too slow'` will throw `TimeoutError('too slow')`- - `message: new MyCustomError('it’s over 9000')` will throw the same error instance- - `message: false` will make the promise resolve with `undefined` instead of rejecting-- If you do a custom error, it's recommended to sub-class `TimeoutError`:-- ```- import {TimeoutError} from 'p-timeout';-- class MyCustomError extends TimeoutError {- name = "MyCustomError";- }- ```- */- message?: string | Error | false;-- /**- Custom implementations for the `setTimeout` and `clearTimeout` functions.-- Useful for testing purposes, in particular to work around [`sinon.useFakeTimers()`](https://sinonjs.org/releases/latest/fake-timers/).-- @example- ```- import pTimeout from 'p-timeout';- import sinon from 'sinon';-- const originalSetTimeout = setTimeout;- const originalClearTimeout = clearTimeout;-- sinon.useFakeTimers();-- // Use `pTimeout` without being affected by `sinon.useFakeTimers()`:- await pTimeout(doSomething(), {- milliseconds: 2000,- customTimers: {- setTimeout: originalSetTimeout,- clearTimeout: originalClearTimeout- }- });- ```- */- readonly customTimers?: {- setTimeout: typeof globalThis.setTimeout;- clearTimeout: typeof globalThis.clearTimeout;- };-- /**- You can abort the promise using [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController).-- _Requires Node.js 16 or later._-- @example- ```- import pTimeout from 'p-timeout';- import delay from 'delay';-- const delayedPromise = delay(3000);-- const abortController = new AbortController();-- setTimeout(() => {- abortController.abort();- }, 100);-- await pTimeout(delayedPromise, {- milliseconds: 2000,- signal: abortController.signal- });- ```- */- signal?: globalThis.AbortSignal;-}--/**-Timeout a promise after a specified amount of time.--If you pass in a cancelable promise, specifically a promise with a `.cancel()` method, that method will be called when the `pTimeout` promise times out.--@param input - Promise to decorate.-@returns A decorated `input` that times out after `milliseconds` time. It has a `.clear()` method that clears the timeout.--@example-```-import {setTimeout} from 'node:timers/promises';-import pTimeout from 'p-timeout';--const delayedPromise = () => setTimeout(200);--await pTimeout(delayedPromise(), {- milliseconds: 50,- fallback: () => {- return pTimeout(delayedPromise(), {milliseconds: 300});- }-});-```-*/-export default function pTimeout<ValueType, ReturnType = ValueType>(- input: PromiseLike<ValueType>,- options: Options<ReturnType> & { message: false }-): ClearablePromise<ValueType | ReturnType | undefined>-export default function pTimeout<ValueType, ReturnType = ValueType>(- input: PromiseLike<ValueType>,- options: Options<ReturnType>-): ClearablePromise<ValueType | ReturnType>\ No newline at end of filediff --git a/packages/js-sdk/src/utils/timeout.js b/packages/js-sdk/src/utils/timeout.jsdeleted file mode 100644index 458886c15..000000000--- a/packages/js-sdk/src/utils/timeout.js+++ /dev/null@@ -1,116 +0,0 @@-export class TimeoutError extends Error {- constructor(message) {- super(message)- this.name = 'TimeoutError'- }-}--/**-An error to be thrown when the request is aborted by AbortController.-DOMException is thrown instead of this Error when DOMException is available.-*/-export class AbortError extends Error {- constructor(message) {- super()- this.name = 'AbortError'- this.message = message- }-}--/**-TODO: Remove AbortError and just throw DOMException when targeting Node 18.-*/-const getDOMException = errorMessage => globalThis.DOMException === undefined- ? new AbortError(errorMessage)- : new DOMException(errorMessage)--/**-TODO: Remove below function and just 'reject(signal.reason)' when targeting Node 18.-*/-const getAbortedReason = signal => {- const reason = signal.reason === undefined- ? getDOMException('This operation was aborted.')- : signal.reason-- return reason instanceof Error ? reason : getDOMException(reason)-}--export default function pTimeout(promise, options) {- const {- milliseconds,- fallback,- message,- customTimers = { setTimeout, clearTimeout },- } = options-- let timer-- const wrappedPromise = new Promise((resolve, reject) => {- if (typeof milliseconds !== 'number' || Math.sign(milliseconds) !== 1) {- throw new TypeError(`Expected \`milliseconds\` to be a positive number, got \`${milliseconds}\``)- }-- if (options.signal) {- const { signal } = options- if (signal.aborted) {- reject(getAbortedReason(signal))- }-- signal.addEventListener('abort', () => {- reject(getAbortedReason(signal))- })- }-- if (milliseconds === Number.POSITIVE_INFINITY) {- promise.then(resolve, reject)- return- }-- // We create the error outside of `setTimeout` to preserve the stack trace.- const timeoutError = new TimeoutError()-- timer = customTimers.setTimeout.call(undefined, () => {- if (fallback) {- try {- resolve(fallback())- } catch (error) {- reject(error)- }-- return- }-- if (typeof promise.cancel === 'function') {- promise.cancel()- }-- if (message === false) {- resolve()- } else if (message instanceof Error) {- reject(message)- } else {- timeoutError.message = message ?? `Promise timed out after ${milliseconds} milliseconds`- reject(timeoutError)- }- }, milliseconds);-- (async () => {- try {- resolve(await promise)- } catch (error) {- reject(error)- }- })()- })-- const cancelablePromise = wrappedPromise.finally(() => {- cancelablePromise.clear()- })-- cancelablePromise.clear = () => {- customTimers.clearTimeout.call(undefined, timer)- timer = undefined- }-- return cancelablePromise-}\ No newline at end of file