Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
[WIP] Fix type exports; Fix files endpoint url; Remove unnecessary chunking; Improve exceptions; Remove arbitrary error codes
Tomas Valentaf28e71f13c41
diff --git a/packages/js-sdk/src/sandbox/process/processHandle.ts b/packages/js-sdk/src/sandbox/process/processHandle.tsindex 16a953a4a..2cdc0f789 100644--- a/packages/js-sdk/src/sandbox/process/processHandle.ts+++ b/packages/js-sdk/src/sandbox/process/processHandle.ts@@ -10,14 +10,14 @@ export type Stdout = Brand<string, 'stdout'>export type Stderr = Brand<string, 'stderr'>export type Pty = Brand<Uint8Array, 'pty'>-export class ProcessError extends Error {- constructor(message: any) {- super(message)- this.name = 'ProcessError'- }+export interface ProcessResult {+ exitCode: number+ error?: string+ stdout: string+ stderr: string}-export class ProcessExitError extends ProcessError implements ProcessResult {+export class ProcessExitError extends SandboxError implements ProcessResult {constructor(private readonly result: ProcessResult) {super(result.error)this.name = 'ProcessExitError'@@ -151,10 +151,3 @@ export class ProcessHandle implements Omit<ProcessResult, 'exitCode' | 'error'>,return this.result}}--export interface ProcessResult {- exitCode: number- error?: string- stdout: string- stderr: string-}diff --git a/packages/python-sdk/connect/__init__.py b/packages/python-sdk/connect/__init__.pyindex d29e6ac01..6f31c8cee 100644--- a/packages/python-sdk/connect/__init__.py+++ b/packages/python-sdk/connect/__init__.py@@ -1 +1 @@-from .client import Client, Code, GzipCompressor, Error # noqa: F401+from .client import Client, GzipCompressor, ConnectException, Code # noqa: F401diff --git a/packages/python-sdk/connect/client.py b/packages/python-sdk/connect/client.pyindex 0ae7579da..60c796075 100644--- a/packages/python-sdk/connect/client.py+++ b/packages/python-sdk/connect/client.py@@ -3,7 +3,7 @@ import jsonimport structimport httpcore-from enum import Flag, IntEnum+from enum import Flag, Enumfrom google.protobuf import json_format@@ -13,28 +13,28 @@ class EnvelopeFlags(Flag):end_stream = 0b00000010-class Code(IntEnum):- canceled = 408- unknown = 500- invalid_argument = 400- deadline_exceeded = 408- not_found = 404- already_exists = 409- permission_denied = 403- resource_exhausted = 429- failed_precondition = 412- aborted = 409- out_of_range = 400- unimplemented = 404- internal = 500- unavailable = 503- data_loss = 500- unauthenticated = 401---class Error(Exception):- def __init__(self, code, message):- self.code = code+class Code(Enum):+ canceled = "canceled"+ unknown = "unknown"+ invalid_argument = "invalid_argument"+ deadline_exceeded = "deadline_exceeded"+ not_found = "not_found"+ already_exists = "already_exists"+ permission_denied = "permission_denied"+ resource_exhausted = "resource_exhausted"+ failed_precondition = "failed_precondition"+ aborted = "aborted"+ out_of_range = "out_of_range"+ unimplemented = "unimplemented"+ internal = "internal"+ unavailable = "unavailable"+ data_loss = "data_loss"+ unauthenticated = "unauthenticated"+++class ConnectException(Exception):+ def __init__(self, status: Code, message: str):+ self.status = statusself.message = message@@ -59,17 +59,20 @@ def error_for_response(http_resp):try:error = json.loads(http_resp.content)except (json.decoder.JSONDecodeError, KeyError):- return Error(Code(http_resp.status), http_resp.reason)+ return ConnectException(http_resp.status, http_resp.reason)else:return make_error(error)def make_error(error):+ status = Nonetry:- code = Code[error["code"]]+ status = Code(error["code"])except KeyError:- code = Code.unknown- return Error(code, error.get("message", ""))+ status = Code.unknown+ pass++ return ConnectException(status, error.get("message", ""))class GzipCompressor:diff --git a/packages/python-sdk/e2b/__init__.py b/packages/python-sdk/e2b/__init__.pyindex 5457a5a84..087fcbba3 100644--- a/packages/python-sdk/e2b/__init__.py+++ b/packages/python-sdk/e2b/__init__.py@@ -12,4 +12,3 @@ from .sandbox.process.process_handle import (Stdout,)from .sandbox.filesystem.watch_handle import WatchHandle-from e2b.envd.filesystem.filesystem_pb2 import FileType, EntryInfodiff --git a/packages/python-sdk/e2b/connection_config.py b/packages/python-sdk/e2b/connection_config.pyindex e217e1138..132e58503 100644--- a/packages/python-sdk/e2b/connection_config.py+++ b/packages/python-sdk/e2b/connection_config.py@@ -11,10 +11,96 @@ ACCESS_TOKEN = os.getenv("E2B_ACCESS_TOKEN")REQUEST_TIMEOUT: float = 30 # 30s+# // This is the message for the sandbox timeout error when the response code is 502/Unavailable+# function formatSandboxTimeoutError(message: string) {+# return new TimeoutError(+# `${message}: This error is likely due to sandbox timeout. You can modify the sandbox timeout by passing 'timeoutMs' when starting the sandbox or calling '.setTimeout' on the sandbox with the desired timeout.`,+# )+# }+++def format_sandbox_timeout_error(message: str):+ return TimeoutException(+ f"{message}: This error is likely due to sandbox timeout. You can modify the sandbox timeout by passing 'timeoutMs' when starting the sandbox or calling '.setTimeout' on the sandbox with the desired timeout."+ )+++# export function handleEnvdApiError(err: {+# code: number;+# message: string;+# } | undefined) {+# switch (err?.code) {+# case 400:+# return new InvalidUserError(err.message)+# case 403:+# return new InvalidPathError(err.message)+# case 404:+# return new NotFoundError(err.message)+# case 502:+# return formatSandboxTimeoutError(err.message)+# case 507:+# return new NotEnoughDiskSpaceError(err.message)+# default:+# if (err) {+# return new SandboxError(`${err.code}: ${err.message}`)+# }+# }+# }++# export function handleRpcError(err: unknown) {+# if (err instanceof ConnectError) {+# switch (err.code) {+# case Code.InvalidArgument:+# return new InvalidUserError(err.message)+# case Code.NotFound:+# return new InvalidPathError(err.message)+# case Code.Unavailable:+# return formatSandboxTimeoutError(err.message)+# case Code.Canceled:+# return new TimeoutError(+# `${err.message}: This error is likely due to exceeding 'requestTimeoutMs'. You can pass the request timeout value as an option when making the request.`,+# )+# case Code.DeadlineExceeded:+# return new TimeoutError(+# `${err.message}: This error is likely due to exceeding 'timeoutMs' — the total time a long running request can be active. It can be modified by passing 'timeoutMs' when making the request. Use '0' to disable the timeout.`,+# )+# default:+# return new SandboxError(`${err.code}: ${err.message}`)+# }+# }++# return err+# }++class SandboxException(Exception):pass+# [unknown] is sometimes caused by the sandbox timeout when the request is not processed+# [unavailable] is caused by sandbox timeout+# [canceled] is caused by exceeding request timeout+# [deadline_exceeded] is caused by exceeding the timeout (for process handlers, watch, etc)+class TimeoutException(SandboxException):+ pass+++class NotEnoughDiskSpaceException(SandboxException):+ pass+++class NotFoundException(SandboxException):+ pass+++class InvalidPathException(SandboxException):+ pass+++class InvalidUserException(SandboxException):+ pass++class ConnectionConfig:def __init__(self,diff --git a/packages/python-sdk/e2b/sandbox/filesystem/filesystem.py b/packages/python-sdk/e2b/sandbox/filesystem/filesystem.pyindex 3d9888469..daf5da2d7 100644--- a/packages/python-sdk/e2b/sandbox/filesystem/filesystem.py+++ b/packages/python-sdk/e2b/sandbox/filesystem/filesystem.py@@ -15,13 +15,11 @@ from typing import ()from e2b.sandbox.filesystem.watch_handle import WatchHandle-from e2b.connection_config import Username, ConnectionConfig+from e2b.connection_config import SandboxException, Username, ConnectionConfigfrom e2b.envd.filesystem import filesystem_connect, filesystem_pb2from e2b.envd.permissions.permissions_pb2 import Userfrom e2b.envd.api import ENVD_API_FILES_ROUTE-READ_STREAM_CHUNK_SIZE = 2 << 16 # 64KiB-class Filesystem:def __init__(@@ -73,9 +71,10 @@ class Filesystem:user: Username = "user",request_timeout: Optional[float] = None,):- url = urllib.parse.urljoin(self._envd_api_url, f"/{ENVD_API_FILES_ROUTE}")+ url = urllib.parse.urljoin(self._envd_api_url, f"{ENVD_API_FILES_ROUTE}")r = requests.get(url,+ stream=True if format == "stream" else False,params={"path": path, "username": user},timeout=self._connection_config.get_request_timeout(request_timeout),)@@ -85,7 +84,7 @@ class Filesystem:elif format == "bytes":return bytearray(r.content)elif format == "stream":- iter: Iterator[bytes] = r.iter_content(chunk_size=READ_STREAM_CHUNK_SIZE)+ iter: Iterator[bytes] = r.iter_content()return iterdef write(@@ -95,7 +94,8 @@ class Filesystem:user: Username = "user",request_timeout: Optional[float] = None,) -> None:- url = urllib.parse.urljoin(self._envd_api_url, f"/{ENVD_API_FILES_ROUTE}")+ url = urllib.parse.urljoin(self._envd_api_url, f"{ENVD_API_FILES_ROUTE}")+files = {"file": data}r = requests.post(url,@@ -136,8 +136,8 @@ class Filesystem:)return True- except connect.Error as e:- if e.code == connect.Code.not_found:+ except connect.ConnectException as e:+ if e.status == connect.Code.already_exists:return Falseraise@@ -176,14 +176,21 @@ class Filesystem:path: str,user: Username = "user",request_timeout: Optional[float] = None,- ) -> None:- self._rpc.make_dir(- filesystem_pb2.MakeDirRequest(- path=path,- user=User(username=user),- ),- timeout=self._connection_config.get_request_timeout(request_timeout),- )+ ) -> bool:+ try:+ self._rpc.make_dir(+ filesystem_pb2.MakeDirRequest(+ path=path,+ user=User(username=user),+ ),+ timeout=self._connection_config.get_request_timeout(request_timeout),+ )++ return True+ except connect.ConnectException as e:+ if e.status == connect.Code.already_exists:+ return False+ raisedef watch(self,@@ -203,4 +210,9 @@ class Filesystem:),)- return WatchHandle(events=events)+ try:+ start_event = next(events)++ return WatchHandle(events=events)+ except Exception as e:+ raise SandboxException(f"Failed to start watch: {e}")diff --git a/packages/python-sdk/e2b/sandbox/filesystem/watch_handle.py b/packages/python-sdk/e2b/sandbox/filesystem/watch_handle.pyindex 639c24caa..c0894ff53 100644--- a/packages/python-sdk/e2b/sandbox/filesystem/watch_handle.py+++ b/packages/python-sdk/e2b/sandbox/filesystem/watch_handle.py@@ -2,6 +2,8 @@ from typing import Any, Generatorfrom e2b.envd.filesystem.filesystem_pb2 import WatchDirResponse+# TODO: Export custom types+class WatchHandle(Generator):def __init__(diff --git a/packages/python-sdk/e2b/sandbox/process/main.py b/packages/python-sdk/e2b/sandbox/process/main.pyindex ebe89b709..2e9fd06a6 100644--- a/packages/python-sdk/e2b/sandbox/process/main.py+++ b/packages/python-sdk/e2b/sandbox/process/main.py@@ -1,3 +1,4 @@+from dataclasses import dataclassimport connectimport httpcore@@ -6,7 +7,17 @@ from typing import Dict, List, Optional, Literal, overload, Union, Callablefrom e2b.envd.permissions import permissions_pb2from e2b.envd.process import process_connect, process_pb2from e2b.sandbox.process.process_handle import ProcessHandle, ProcessResult-from e2b.connection_config import Username, ConnectionConfig+from e2b.connection_config import SandboxException, Username, ConnectionConfig+++@dataclass+class ProcessInfo:+ pid: int+ tag: Optional[str]+ cmd: str+ args: List[str]+ envs: Dict[str, str]+ cwd: Optional[str]class Process:@@ -26,12 +37,22 @@ class Process:def list(self,request_timeout: Optional[float] = None,- ) -> List[process_pb2.ProcessInfo]:+ ) -> List[ProcessInfo]:res = self._rpc.list(process_pb2.ListRequest(),timeout=self._connection_config.get_request_timeout(request_timeout),)- return [p for p in res.processes]+ return [+ ProcessInfo(+ pid=p.pid,+ tag=p.tag,+ cmd=p.config.cmd,+ args=list(p.config.args),+ envs=dict(p.config.envs),+ cwd=p.config.cwd,+ )+ for p in res.processes+ ]def kill(self,@@ -145,13 +166,16 @@ class Process:),)- start_event = next(events)+ try:+ start_event = next(events)- return ProcessHandle(- pid=start_event.event.start.pid,- handle_kill=lambda: self.kill(start_event.event.start.pid),- events=events,- )+ return ProcessHandle(+ pid=start_event.event.start.pid,+ handle_kill=lambda: self.kill(start_event.event.start.pid),+ events=events,+ )+ except Exception as e:+ raise SandboxException(f"Failed to start process: {e}")def connect(self,@@ -169,8 +193,13 @@ class Process:),)- return ProcessHandle(- pid=pid,- handle_kill=lambda: self.kill(pid),- events=events,- )+ try:+ start_event = next(events)++ return ProcessHandle(+ pid=start_event.event.start.pid,+ handle_kill=lambda: self.kill(start_event.event.start.pid),+ events=events,+ )+ except Exception as e:+ raise SandboxException(f"Failed to start process: {e}")diff --git a/packages/python-sdk/e2b/sandbox/process/process_handle.py b/packages/python-sdk/e2b/sandbox/process/process_handle.pyindex 7b9673dba..1ec5faba6 100644--- a/packages/python-sdk/e2b/sandbox/process/process_handle.py+++ b/packages/python-sdk/e2b/sandbox/process/process_handle.py@@ -1,29 +1,25 @@+from dataclasses import dataclassfrom typing import Optional, Callable, Any, Generator, Union, Tuple-from pydantic import BaseModel+from e2b.connection_config import SandboxExceptionfrom e2b.envd.process import process_pb2Stdout = strStderr = str-class ProcessResult(BaseModel):+@dataclass+class ProcessResult:stderr: strstdout: strexit_code: interror: Optional[str]-class ProcessExitException(Exception):- def __init__(self, result: ProcessResult):- self._result = result-+@dataclass+class ProcessExitException(SandboxException, ProcessResult):def __str__(self):- return f"Process exited with code {self._result.exit_code} and error: {self._result.error}"-- @property- def exit_code(self):- return self._result.exit_code+ return f"Process exited with code {self.exit_code} and error: {self.error}"class ProcessHandle:@@ -110,7 +106,12 @@ class ProcessHandle:raise RuntimeError("Process ended without an end event")if self._result.exit_code != 0:- raise ProcessExitException(self._result)+ raise ProcessExitException(+ stdout=self._stdout,+ stderr=self._stderr,+ exit_code=self._result.exit_code,+ error=self._result.error,+ )return self._resultdiff --git a/packages/python-sdk/e2b/sandbox/sandbox_api.py b/packages/python-sdk/e2b/sandbox/sandbox_api.pyindex e97646856..0b0dc2330 100644--- a/packages/python-sdk/e2b/sandbox/sandbox_api.py+++ b/packages/python-sdk/e2b/sandbox/sandbox_api.py@@ -1,12 +1,13 @@+from dataclasses import dataclassfrom typing import Optional, Dict, List-from pydantic import BaseModelfrom datetime import datetimefrom e2b.api import ApiClient, models, clientfrom e2b.connection_config import ConnectionConfig-class RunningSandbox(BaseModel):+@dataclass+class RunningSandbox:sandbox_id: strtemplate_id: strname: Optional[str]