Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
[WIP] Fix bug and add proper errors
Tomas Valentab3617d99d935
diff --git a/packages/js-sdk/example.mts b/packages/js-sdk/example.mtsindex 7ed58f2f0..c1f57a633 100644--- a/packages/js-sdk/example.mts+++ b/packages/js-sdk/example.mts@@ -1 +1,29 @@-import { Sandbox } from './src'+import { Sandbox } from './dist'++const sbx = await Sandbox.create('', {+ debug: true,+})+++// const watcher = await sbx.files.watch('./', (e) => {+// console.log(e)+// }, {+// user: 'user',+// timeout: 2,+// })+++// await sbx.files.write('test', 'hello world')+// const contents = await sbx.files.read('test')+// console.log(contents)+++// await sbx.files.makeDir('new-lore')++// const contents2 = await sbx.files.exists('%%\\test')+// console.log(contents2)++// watcher.stop()++const res = await sbx.commands.run('ls')+console.log(res)diff --git a/packages/js-sdk/src/api/index.ts b/packages/js-sdk/src/api/index.tsindex 2eeb819b0..4fef17aa1 100644--- a/packages/js-sdk/src/api/index.ts+++ b/packages/js-sdk/src/api/index.ts@@ -2,9 +2,9 @@ import createClient from 'openapi-fetch'import type { components, paths } from './schema.gen'import { defaultHeaders } from './metadata'-import { ConnectionConfig } from '../connectionConfig'+import { ConnectionConfig, SandboxError } from '../connectionConfig'-export class AuthenticationError extends Error {+export class AuthenticationError extends SandboxError {constructor(message: any) {super(message)this.name = 'AuthenticationError'diff --git a/packages/js-sdk/src/connectionConfig.ts b/packages/js-sdk/src/connectionConfig.tsindex 4bf66de66..403a0c271 100644--- a/packages/js-sdk/src/connectionConfig.ts+++ b/packages/js-sdk/src/connectionConfig.ts@@ -12,6 +12,20 @@ export interface ConnectionOpts {requestTimeoutMs?: number}+export class SandboxError extends Error {+ constructor(message: any) {+ super(message)+ this.name = 'SandboxError'+ }+}++export class TimeoutError extends SandboxError {+ constructor(message: string) {+ super(message)+ this.name = 'TimeoutError'+ }+}+export class ConnectionConfig {readonly debug: booleanreadonly domain: stringdiff --git a/packages/js-sdk/src/envd/schema.gen.ts b/packages/js-sdk/src/envd/schema.gen.tsindex d753c582f..0fb459617 100644--- a/packages/js-sdk/src/envd/schema.gen.ts+++ b/packages/js-sdk/src/envd/schema.gen.ts@@ -5,16 +5,14 @@export interface paths {- "/files/{path}": {+ "/files": {/** Download a file */get: {parameters: {query: {+ path?: components["parameters"]["FilePath"];username: components["parameters"]["User"];};- path: {- path: components["parameters"]["FilePath"];- };};responses: {200: components["responses"]["DownloadSuccess"];@@ -28,16 +26,13 @@ export interface paths {post: {parameters: {query: {+ path?: components["parameters"]["FilePath"];username: components["parameters"]["User"];};- path: {- path: components["parameters"]["FilePath"];- };};requestBody: components["requestBodies"]["File"];responses: {- 200: components["responses"]["ExistingFileUploadSuccess"];- 201: components["responses"]["NewFileUploadSuccess"];+ 204: components["responses"]["UploadSuccess"];400: components["responses"]["BadRequest"];403: components["responses"]["DirectoryPathError"];500: components["responses"]["InternalServerError"];@@ -88,10 +83,6 @@ export interface components {"application/octet-stream": string;};};- /** @description The file was updated successfully. */- ExistingFileUploadSuccess: {- content: never;- };/** @description File not found */FileNotFound: {content: {@@ -104,20 +95,20 @@ export interface components {"application/json": components["schemas"]["Error"];};};- /** @description The file was created successfully. */- NewFileUploadSuccess: {- content: never;- };/** @description Not enough disk space */NotEnoughDiskSpace: {content: {"application/json": components["schemas"]["Error"];};};+ /** @description The file was uploaded successfully. */+ UploadSuccess: {+ content: never;+ };};parameters: {/** @description Path to the file, URL encoded. Can be relative to user's home directory. */- FilePath: string;+ FilePath?: string;/** @description User used for setting the owner, or resolving relative paths. */User: string;};diff --git a/packages/js-sdk/src/index.ts b/packages/js-sdk/src/index.tsindex 8c3d522bd..8e73e1662 100644--- a/packages/js-sdk/src/index.ts+++ b/packages/js-sdk/src/index.ts@@ -4,30 +4,36 @@ export {} from './api'export type { components, paths } from './api'-export { ConnectionConfig } from './connectionConfig'+export { ConnectionConfig, SandboxError } from './connectionConfig'export type { ConnectionOpts, Username } from './connectionConfig'-export { WatchHandle } from './sandbox/filesystem/watchHandle'-export type { FilesystemEvent } from './sandbox/filesystem/watchHandle'+export type { FilesystemEvent, WatchHandle } from './sandbox/filesystem/watchHandle'export type {EntryInfo,} from './sandbox/filesystem'export {- EventType,+ NotEnoughDiskSpaceError,+ InvalidUserError,+ NotFoundError as FileNotFoundError,+ FilesystemError,+ InvalidPathError,FileType,-} from './envd/filesystem/filesystem_pb'+} from './sandbox/filesystem'-export { ProcessHandle } from './sandbox/process/processHandle'+export { ProcessError, ProcessExitError } from './sandbox/process/processHandle'export type {ProcessResult,Stdout,Stderr,Pty,+ ProcessHandle,} from './sandbox/process/processHandle'export type {ProcessInfo,ProcessRequestOpts,+ ProcessConnectOpts,+ ProcessStartOpts,} from './sandbox/process'import { Sandbox } from './sandbox'diff --git a/packages/js-sdk/src/sandbox/filesystem/index.ts b/packages/js-sdk/src/sandbox/filesystem/index.tsindex fb2dc4a48..61a4cfb16 100644--- a/packages/js-sdk/src/sandbox/filesystem/index.ts+++ b/packages/js-sdk/src/sandbox/filesystem/index.ts@@ -1,4 +1,3 @@-import { PlainMessage } from '@bufbuild/protobuf'import {createPromiseClient,Transport,@@ -7,20 +6,67 @@ import {Code,} from '@connectrpc/connect'-import { ConnectionConfig, defaultUsername, Username, ConnectionOpts } from '../../connectionConfig'+import { ConnectionConfig, defaultUsername, SandboxError, Username, ConnectionOpts, TimeoutError } from '../../connectionConfig'import { EnvdApiClient } from '../../envd/api'import { Filesystem as FilesystemService } from '../../envd/filesystem/filesystem_connect'-import {- EntryInfo as FsEntryInfo,-} from '../../envd/filesystem/filesystem_pb'+import { FileType as FsFileType } from '../../envd/filesystem/filesystem_pb'+import { WatchHandle, FilesystemEvent } from './watchHandle'-export type EntryInfo = PlainMessage<FsEntryInfo>+export interface EntryInfo {+ name: string+ type: FileType+}++export const enum FileType {+ FILE = 'file',+ DIR = 'dir',+}export interface FilesystemRequestOpts extends Partial<Pick<ConnectionOpts, 'requestTimeoutMs'>> {user?: Username}+export interface WatchOpts extends FilesystemRequestOpts {+ timeout?: number+ onExit?: (err: SandboxError) => void+}++export class FilesystemError extends SandboxError {+ constructor(message: any) {+ super(message)+ this.name = 'FilesystemError'+ }+}++export class InvalidUserError extends FilesystemError {+ constructor(message: string, readonly username: string) {+ super(message)+ this.name = 'InvalidUsernameError'+ }+}++export class InvalidPathError extends FilesystemError {+ constructor(message: string, readonly path: string) {+ super(message)+ this.name = 'InvalidPathError'+ }+}++export class NotEnoughDiskSpaceError extends FilesystemError {+ constructor(message: string) {+ super(message)+ this.name = 'NotEnoughDiskSpaceError'+ }+}++export class NotFoundError extends FilesystemError {+ constructor(message: string, readonly path: string) {+ super(message)+ this.name = 'FileNotFoundError'+ }+}+export class Filesystem {private readonly rpc: PromiseClient<typeof FilesystemService>private readonly envdApi: EnvdApiClient@@ -41,36 +87,49 @@ export class Filesystem {async read(path: string, opts?: FilesystemRequestOpts & { format?: 'text' | 'stream' | 'bytes' | 'blob' }): Promise<unknown> {const format = opts?.format ?? 'text'- const response = await this.envdApi.api.GET('/files/{path}', {+ const username = opts?.user || defaultUsername++ const { data, error } = await this.envdApi.api.GET('/files', {params: {- path: {- path,- },query: {- username: opts?.user || defaultUsername,+ path,+ username,},},parseAs: format === 'bytes' ? 'arrayBuffer' : format,signal: this.connectionConfig.getSignal(opts?.requestTimeoutMs),})+ switch (error?.code) {+ case 400:+ throw new InvalidUserError(error.message, username)+ case 403:+ throw new InvalidPathError(error.message, path)+ case 404:+ throw new NotFoundError(error.message, path)+ default:+ if (error) {+ throw new FilesystemError(error.message)+ }+ }+if (format === 'bytes') {- return new Uint8Array(response.data as ArrayBuffer)+ return new Uint8Array(data as ArrayBuffer)}- return response.data+ return data}async write(path: string, data: string | ArrayBuffer | Blob | ReadableStream, opts?: FilesystemRequestOpts): Promise<void> {const blob = await new Response(data).blob()- await this.envdApi.api.POST('/files/{path}', {+ const username = opts?.user || defaultUsername++ const { error } = await this.envdApi.api.POST('/files', {params: {- path: {- path,- },query: {- username: opts?.user || defaultUsername,+ path,+ username,},},bodySerializer() {@@ -83,60 +142,138 @@ export class Filesystem {body: {},signal: this.connectionConfig.getSignal(opts?.requestTimeoutMs),})++ switch (error?.code) {+ case 400:+ throw new InvalidUserError(error.message, username)+ case 403:+ throw new InvalidPathError(error.message, path)+ case 507:+ throw new NotEnoughDiskSpaceError(error.message)+ default:+ if (error) {+ throw new FilesystemError(error.message)+ }+ }}async list(path: string, opts?: FilesystemRequestOpts): Promise<EntryInfo[]> {- const res = await this.rpc.list({- path,- user: {- selector: {- case: 'username',- value: opts?.user || defaultUsername,+ const username = opts?.user || defaultUsername++ try {+ const res = await this.rpc.list({+ path,+ user: {+ selector: {+ case: 'username',+ value: username,+ },},- },- }, {- signal: this.connectionConfig.getSignal(opts?.requestTimeoutMs),- })+ }, {+ signal: this.connectionConfig.getSignal(opts?.requestTimeoutMs),+ })- return res.entries+ return res.entries.map(e => ({+ name: e.name,+ type: e.type === FsFileType.FILE ? FileType.FILE : FileType.DIR,+ }))+ } catch (err) {+ if (err instanceof ConnectError) {+ switch (err.code) {+ case Code.InvalidArgument:+ throw new InvalidUserError(err.message, username)+ case Code.NotFound:+ throw new InvalidPathError(err.message, path)+ case Code.DeadlineExceeded:+ throw new TimeoutError(err.message)+ default:+ throw new FilesystemError(err.message)+ }+ }+ throw err+ }}- async makeDir(path: string, opts?: FilesystemRequestOpts): Promise<void> {- await this.rpc.makeDir({- path,- user: {- selector: {- case: 'username',- value: opts?.user || defaultUsername,+ async makeDir(path: string, opts?: FilesystemRequestOpts): Promise<boolean> {+ const username = opts?.user || defaultUsername++ try {+ await this.rpc.makeDir({+ path,+ user: {+ selector: {+ case: 'username',+ value: username,+ },},- },- }, {- signal: this.connectionConfig.getSignal(opts?.requestTimeoutMs),- })+ }, {+ signal: this.connectionConfig.getSignal(opts?.requestTimeoutMs),+ })++ return true+ } catch (err) {+ if (err instanceof ConnectError) {+ switch (err.code) {+ case Code.InvalidArgument:+ throw new InvalidUserError(err.message, username)+ case Code.NotFound:+ throw new InvalidPathError(err.message, path)+ case Code.AlreadyExists:+ return false+ case Code.FailedPrecondition:+ throw new InvalidPathError(err.message, path)+ case Code.DeadlineExceeded:+ throw new TimeoutError(err.message)+ default:+ throw new FilesystemError(err.message)+ }+ }+ throw err+ }}async remove(path: string, opts?: FilesystemRequestOpts): Promise<void> {- await this.rpc.remove({- path,- user: {- selector: {- case: 'username',- value: opts?.user || defaultUsername,+ const username = opts?.user || defaultUsername++ try {+ await this.rpc.remove({+ path,+ user: {+ selector: {+ case: 'username',+ value: username,+ },},- },- }, {- signal: this.connectionConfig.getSignal(opts?.requestTimeoutMs),- })+ }, {+ signal: this.connectionConfig.getSignal(opts?.requestTimeoutMs),+ })+ } catch (err) {+ if (err instanceof ConnectError) {+ switch (err.code) {+ case Code.InvalidArgument:+ throw new InvalidUserError(err.message, username)+ case Code.NotFound:+ throw new InvalidPathError(err.message, path)+ case Code.DeadlineExceeded:+ throw new TimeoutError(err.message)+ default:+ throw new FilesystemError(err.message)+ }+ }+ throw err+ }}async exists(path: string, opts?: FilesystemRequestOpts): Promise<boolean> {+ const username = opts?.user || defaultUsername+try {await this.rpc.stat({path,user: {selector: {case: 'username',- value: opts?.user || defaultUsername,+ value: username,},},}, {@@ -145,18 +282,29 @@ export class Filesystem {return true} catch (err) {- const connectErr = ConnectError.from(err)-- if (connectErr.code === Code.NotFound) {- return false+ if (err instanceof ConnectError) {+ switch (err.code) {+ case Code.InvalidArgument:+ throw new InvalidUserError(err.message, username)+ case Code.NotFound:+ return false+ case Code.DeadlineExceeded:+ throw new TimeoutError(err.message)+ default:+ throw new FilesystemError(err.message)+ }}-throw err}}- async watch(path: string, onEvent: (event: FilesystemEvent) => void | Promise<void>, opts?: FilesystemRequestOpts & { timeout?: number, iterator?: boolean }): Promise<WatchHandle> {+ async watch(+ path: string,+ onEvent: (event: FilesystemEvent) => void | Promise<void>,+ opts?: FilesystemRequestOpts & { timeout?: number, iterator?: boolean },+ ): Promise<WatchHandle> {const requestTimeoutMs = opts?.requestTimeoutMs ?? this.connectionConfig.requestTimeoutMs+ const username = opts?.user || defaultUsernameconst controller = new AbortController()@@ -171,7 +319,7 @@ export class Filesystem {user: {selector: {case: 'username',- value: opts?.user || defaultUsername,+ value: username,},},}, {@@ -184,6 +332,8 @@ export class Filesystem {return new WatchHandle(() => controller.abort(),events,+ path,+ username,onEvent,opts?.iterator,)diff --git a/packages/js-sdk/src/sandbox/filesystem/watchHandle.ts b/packages/js-sdk/src/sandbox/filesystem/watchHandle.tsindex 939341800..1161accaa 100644--- a/packages/js-sdk/src/sandbox/filesystem/watchHandle.ts+++ b/packages/js-sdk/src/sandbox/filesystem/watchHandle.ts@@ -1,4 +1,6 @@import { PlainMessage } from '@bufbuild/protobuf'+import { ConnectError, Code } from '@connectrpc/connect'+import { FilesystemError, InvalidUserError, NotFoundError } from '.'import {FilesystemEvent as FsFilesystemEvent,@@ -13,6 +15,8 @@ export class WatchHandle {constructor(private readonly handleStop: () => void,private readonly events: AsyncIterable<WatchResponse>,+ private readonly path: string,+ private readonly username: string,private readonly onEvent?: (event: FilesystemEvent) => void | Promise<void>,iterator?: boolean,) {@@ -32,6 +36,18 @@ export class WatchHandle {yield event.event}}+ } catch (err) {+ if (err instanceof ConnectError) {+ switch (err.code) {+ case Code.InvalidArgument:+ throw new InvalidUserError(err.message, this.username)+ case Code.NotFound:+ throw new NotFoundError(err.message, this.path)+ default:+ throw new FilesystemError(err.message)+ }+ }+ throw err} finally {this.stop()}diff --git a/packages/js-sdk/src/sandbox/process/index.ts b/packages/js-sdk/src/sandbox/process/index.tsindex 28bbf33ee..c65592958 100644--- a/packages/js-sdk/src/sandbox/process/index.ts+++ b/packages/js-sdk/src/sandbox/process/index.ts@@ -11,14 +11,14 @@ import {Signal,StartResponse,} from '../../envd/process/process_pb'-import { ConnectionConfig, defaultUsername, Username, ConnectionOpts } from '../../connectionConfig'+import { ConnectionConfig, defaultUsername, Username, ConnectionOpts, SandboxError } from '../../connectionConfig'import { ProcessHandle, ProcessResult } from './processHandle'export type ProcessInfo = PlainMessage<PsProcessInfo>export interface ProcessRequestOpts extends Partial<Pick<ConnectionOpts, 'requestTimeoutMs'>> { }-interface ProcessStartOpts extends ProcessRequestOpts {+export interface ProcessStartOpts extends ProcessRequestOpts {background?: booleancwd?: stringuser?: Username@@ -28,6 +28,15 @@ interface ProcessStartOpts extends ProcessRequestOpts {timeout?: number}+export type ProcessConnectOpts = Pick<ProcessStartOpts, 'onStderr' | 'onStdout' | 'timeout'> & ProcessRequestOpts & { iterator?: boolean }++export class ProcessError extends SandboxError {+ constructor(message: string, public readonly pid: number) {+ super(message)+ this.name = 'ProcessError'+ }+}+export class Process {protected readonly rpc: PromiseClient<typeof ProcessService>@@ -79,15 +88,7 @@ export class Process {})}- async connect(- pid: number,- opts?: {- onStdout?: ((data: string) => void | Promise<void>),- onStderr?: ((data: string) => void | Promise<void>),- timeout?: number,- iterator?: boolean,- } & Pick<ConnectionOpts, 'requestTimeoutMs'>- ): Promise<ProcessHandle> {+ async connect(pid: number, opts?: ProcessConnectOpts): Promise<ProcessHandle> {const requestTimeoutMs = opts?.requestTimeoutMs ?? this.connectionConfig.requestTimeoutMsconst controller = new AbortController()@@ -126,7 +127,7 @@ export class Process {async run(cmd: string, opts?: ProcessStartOpts & { background?: false }): Promise<ProcessResult>async run(cmd: string, opts?: ProcessStartOpts & { background: true }): Promise<ProcessHandle>- async run(cmd: string, opts?: Omit<ProcessStartOpts, 'onStderr' | 'onStdout'> & { background?: boolean, iterator: true }): Promise<ProcessHandle>+ async run(cmd: string, opts?: Omit<ProcessStartOpts, 'onStderr' | 'onStdout'> & { iterator: true }): Promise<ProcessHandle>async run(cmd: string, opts?: ProcessStartOpts & { background?: boolean }): Promise<unknown> {const proc = await this.start(cmd, opts)@@ -135,10 +136,7 @@ export class Process {: proc.wait()}- private async start(- cmd: string,- opts?: ProcessStartOpts,- ): Promise<ProcessHandle> {+ private async start(cmd: string, opts?: ProcessStartOpts): Promise<ProcessHandle> {const requestTimeoutMs = opts?.requestTimeoutMs ?? this.connectionConfig.requestTimeoutMsconst controller = new AbortController()diff --git a/packages/python-sdk/example.py b/packages/python-sdk/example.pyindex aa61bd3b0..664eeae6c 100644--- a/packages/python-sdk/example.py+++ b/packages/python-sdk/example.py@@ -1 +1,6 @@from e2b import Sandbox++# sbx = Sandbox()+++s = Sandbox\ No newline at end of filediff --git a/spec/envd/envd.yaml b/spec/envd/envd.yamlindex 76d17f301..db8de8714 100644--- a/spec/envd/envd.yaml+++ b/spec/envd/envd.yaml@@ -15,7 +15,7 @@ paths:204:description: The time and metadata is synced with the host- /files/{path}:+ /files:get:summary: Download a filetags: [files]@@ -42,10 +42,8 @@ paths:requestBody:$ref: "#/components/requestBodies/File"responses:- 200:- $ref: "#/components/responses/ExistingFileUploadSuccess"- 201:- $ref: "#/components/responses/NewFileUploadSuccess"+ 204:+ $ref: "#/components/responses/UploadSuccess"400:$ref: "#/components/responses/BadRequest"403:@@ -59,9 +57,8 @@ components:parameters:FilePath:name: path- in: path- required: true- allowEmptyValue: true+ in: query+ required: falsedescription: Path to the file, URL encoded. Can be relative to user's home directory.schema:type: string@@ -87,10 +84,8 @@ components:format: binaryresponses:- ExistingFileUploadSuccess:- description: The file was updated successfully.- NewFileUploadSuccess:- description: The file was created successfully.+ UploadSuccess:+ description: The file was uploaded successfully.DownloadSuccess:description: Entire file downloaded successfully.content: