Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
Allow to connect to custom infra (#248)
Jakub Novák1b8e2ae8d78f
diff --git a/.changeset/modern-mangos-guess.md b/.changeset/modern-mangos-guess.mdnew file mode 100644index 000000000..77f25f882--- /dev/null+++ b/.changeset/modern-mangos-guess.md@@ -0,0 +1,7 @@+---+"@e2b/python-sdk": patch+"@e2b/sdk": patch+"@e2b/cli": patch+---++Allow for setting custom env to change domaindiff --git a/packages/cli/src/commands/shell.ts b/packages/cli/src/commands/shell.tsindex c28e41bbf..0e9874666 100644--- a/packages/cli/src/commands/shell.ts+++ b/packages/cli/src/commands/shell.ts@@ -78,7 +78,7 @@ export async function connectSandbox({sandbox.terminal,`Terminal connected to sandbox ${asFormattedSandboxTemplate(template,- )}\nwith sandbox URL ${asBold(`https://${sandbox.getHostname()}`)}`,+ )}\nwith sandbox URL ${asBold(`${sandbox.getProtocol()}://${sandbox.getHostname()}`)}}`,`Disconnecting terminal from sandbox ${asFormattedSandboxTemplate(template,)}`,diff --git a/packages/js-sdk/src/constants.ts b/packages/js-sdk/src/constants.tsindex b411b30bf..0427d4eb4 100644--- a/packages/js-sdk/src/constants.ts+++ b/packages/js-sdk/src/constants.ts@@ -3,11 +3,12 @@ export const WS_RECONNECT_INTERVAL = 600 // 600msexport const TIMEOUT = 60_000 // 60s-export const API_DOMAIN = 'api.e2b.dev'-export const API_HOST = process?.env?.E2B_DEBUG- ? 'http://localhost:3000'- : `https://${API_DOMAIN}`-export const SANDBOX_DOMAIN = 'e2b.dev'+const DEBUG = process?.env?.E2B_DEBUG+const DOMAIN = process?.env?.E2B_DOMAIN || 'e2b.dev'+export const SECURE = DEBUG ? false : (process?.env?.E2B_SECURE || 'true').toLowerCase() === 'true'+export const API_DOMAIN = DEBUG ? 'localhost:3000' : `api.${DOMAIN}`+export const API_HOST = `${SECURE ? 'https' : 'http'}://${API_DOMAIN}`+export const SANDBOX_DOMAIN = DEBUG ? 'localhost:3000' : DOMAINexport const ENVD_PORT = 49982export const WS_ROUTE = '/ws'diff --git a/packages/js-sdk/src/sandbox/index.ts b/packages/js-sdk/src/sandbox/index.tsindex 248b353d2..0b8cbbacb 100644--- a/packages/js-sdk/src/sandbox/index.ts+++ b/packages/js-sdk/src/sandbox/index.ts@@ -419,7 +419,7 @@ export class Sandbox extends SandboxConnection {* If a file with the same name already exists, it will be overwritten.*/get fileURL() {- const protocol = this.opts.__debug_devEnv === 'local' ? 'http' : 'https'+ const protocol = this.getProtocol()const hostname = this.getHostname(this.opts.__debug_port || ENVD_PORT)return `${protocol}://${hostname}${FILE_ROUTE}`}diff --git a/packages/js-sdk/src/sandbox/sandboxConnection.ts b/packages/js-sdk/src/sandbox/sandboxConnection.tsindex c26589951..484ac3689 100644--- a/packages/js-sdk/src/sandbox/sandboxConnection.ts+++ b/packages/js-sdk/src/sandbox/sandboxConnection.ts@@ -1,7 +1,7 @@import { IRpcNotification, RpcWebSocketClient } from 'rpc-websocket-client'import api, { components, withAPIKey } from '../api'-import { ENVD_PORT, SANDBOX_DOMAIN, SANDBOX_REFRESH_PERIOD, WS_RECONNECT_INTERVAL, WS_ROUTE } from '../constants'+import { ENVD_PORT, SANDBOX_DOMAIN, SANDBOX_REFRESH_PERIOD, SECURE, WS_RECONNECT_INTERVAL, WS_ROUTE } from '../constants'import { AuthenticationError } from '../error'import { assertFulfilled, formatSettledErrors, withTimeout } from '../utils/promise'import wait from '../utils/wait'@@ -36,13 +36,13 @@ export interface Logger {export interface SandboxConnectionOpts {/*** Sandbox Template ID or name.- *+ ** If not specified, the 'base' template will be used.*/template?: string;/*** @deprecated Use `template` instead.- *+ ** Sandbox Template ID or name.*/id?: string;@@ -173,7 +173,7 @@ export class SandboxConnection {}if (!this.sandbox) {- return undefined+ throw new Error('Cannot get sandbox\'s hostname - sandbox is not initialized')}const hostname = `${this.sandbox.instanceID}-${this.sandbox.clientID}.${SANDBOX_DOMAIN}`@@ -184,6 +184,15 @@ export class SandboxConnection {}}+ /**+ * 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`.+ * @returns Protocol for the connection to the sandbox+ */+ getProtocol(baseProtocol: string = 'http') {+ return SECURE ? `${baseProtocol}s` : baseProtocol+ }+/*** Close the connection to the sandbox*@@ -362,12 +371,7 @@ export class SandboxConnection {private async connectRpc() {const hostname = this.getHostname(this.opts.__debug_port || ENVD_PORT)-- if (!hostname) {- throw new Error('Cannot get sandbox\'s hostname')- }-- const protocol = this.opts.__debug_devEnv === 'local' ? 'ws' : 'wss'+ const protocol = this.getProtocol('ws')const sandboxURL = `${protocol}://${hostname}${WS_ROUTE}`this.rpc.onError((err) => {diff --git a/packages/js-sdk/testground/basic.cjs b/packages/js-sdk/testground/basic.cjsindex bd9524751..c5bf98bbf 100644--- a/packages/js-sdk/testground/basic.cjs+++ b/packages/js-sdk/testground/basic.cjs@@ -12,18 +12,10 @@ async function main() {await sandbox.filesystem.write('/code/hello.txt', "My friend!")- const sandboxID = sandbox.id-- await sandbox.keepAlive(30)- await sandbox.close()- await wait(15000)-- const sandbox2 = await e2b.Sandbox.reconnect(sandboxID)-- const files = await sandbox2.filesystem.read('/code/hello.txt')+ const files = await sandbox.filesystem.read('/code/hello.txt')console.log(files)- await sandbox2.close()+ await sandbox.close()}main().catch(console.error)diff --git a/packages/python-sdk/e2b/constants.py b/packages/python-sdk/e2b/constants.pyindex 16c55dee2..cd577140e 100644--- a/packages/python-sdk/e2b/constants.py+++ b/packages/python-sdk/e2b/constants.py@@ -4,12 +4,21 @@ SANDBOX_REFRESH_PERIOD = 5 # secondsTIMEOUT = 60-API_DOMAIN = "api.e2b.dev"-API_HOST = (- "http://localhost:3000" if os.getenv("E2B_DEBUG") else f"https://{API_DOMAIN}"-)+DOMAIN = os.getenv("E2B_DOMAIN") or "e2b.dev"+SECURE = os.getenv("E2B_SECURE", "TRUE").upper() == "TRUE"+DEBUG = os.getenv("E2B_DEBUG") or False+API_DOMAIN = f"api.{DOMAIN}"-SANDBOX_DOMAIN = "e2b.dev"+if DEBUG:+ DOMAIN = "localhost:3000"+ API_DOMAIN = "localhost:3000"+ SECURE = False++PROTOCOL = "https" if SECURE else "http"+API_HOST = f"{PROTOCOL}://{API_DOMAIN}"+++SANDBOX_DOMAIN = DOMAINENVD_PORT = 49982WS_ROUTE = "/ws"diff --git a/packages/python-sdk/e2b/sandbox/main.py b/packages/python-sdk/e2b/sandbox/main.pyindex 08851ae4d..030ef91f4 100644--- a/packages/python-sdk/e2b/sandbox/main.py+++ b/packages/python-sdk/e2b/sandbox/main.py@@ -258,7 +258,7 @@ class Sandbox(SandboxConnection):If a file with the same name already exists, it will be overwritten."""hostname = self.get_hostname(self._debug_port or ENVD_PORT)- protocol = "http" if self._debug_dev_env == "local" else "https"+ protocol = self.get_protocol()file_url = f"{protocol}://{hostname}{FILE_ROUTE}"diff --git a/packages/python-sdk/e2b/sandbox/sandbox_connection.py b/packages/python-sdk/e2b/sandbox/sandbox_connection.pyindex 2eaf55030..81a221bdf 100644--- a/packages/python-sdk/e2b/sandbox/sandbox_connection.py+++ b/packages/python-sdk/e2b/sandbox/sandbox_connection.py@@ -17,6 +17,7 @@ from e2b.constants import (TIMEOUT,ENVD_PORT,WS_ROUTE,+ SECURE,)from e2b.sandbox.env_vars import EnvVarsfrom e2b.sandbox.exception import (@@ -164,7 +165,7 @@ class SandboxConnection:**kwargs,)- def get_hostname(self, port: Optional[int] = None):+ def get_hostname(self, port: Optional[int] = None) -> str:"""Get the hostname for the sandbox or for the specified sandbox's port.@@ -191,6 +192,17 @@ class SandboxConnection:return hostname+ @staticmethod+ def get_protocol(base_protocol: str = "http") -> str:+ """+ The function decides whether to use the secure or insecure protocol.++ :param base_protocol: Specify the specific protocol you want to use. Do not include the `s` in `https` or `wss`.++ :return: Protocol for the connection to the sandbox+ """+ return f"{base_protocol}s" if SECURE else base_protocol+def keep_alive(self, duration: int) -> None:"""Keep the sandbox alive for the specified duration.@@ -295,8 +307,7 @@ class SandboxConnection:def _connect_rpc(self, timeout: Optional[float] = TIMEOUT):hostname = self.get_hostname(self._debug_port or ENVD_PORT)- protocol = "ws" if self._debug_dev_env == "local" else "wss"-+ protocol = self.get_protocol("ws")sandbox_url = f"{protocol}://{hostname}{WS_ROUTE}"self._rpc = SandboxRpc(url=sandbox_url,diff --git a/packages/python-sdk/example.py b/packages/python-sdk/example.pyindex fc999f138..9e0f3cc72 100644--- a/packages/python-sdk/example.py+++ b/packages/python-sdk/example.py@@ -15,7 +15,7 @@ logging.basicConfig(level=logging.ERROR)def main():s = Sandbox(api_key=E2B_API_KEY)- a = s.process.start("ls -la /var ")+ a = s.process.start("ls -la /var")a.wait()print(a.stdout)print(a.stderr)