Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

feat(code-interpreter): route Jupyter requests through the unified sandbox endpoint (#1963)

devin-ai-integration[bot]0ad4ec1e02da
diff --git a/.changeset/code-interpreter-unified-endpoint.md b/.changeset/code-interpreter-unified-endpoint.md
new file mode 100644
index 000000000..88905ae49
--- /dev/null
+++ b/.changeset/code-interpreter-unified-endpoint.md
@@ -0,0 +1,6 @@
+---
+'@e2b/code-interpreter': patch
+'@e2b/code-interpreter-python': patch
+---
+
+Send Code Interpreter requests through the unified sandbox endpoint (`sandbox.<domain>`) on supported domains, like the rest of the SDK. Browsers (JS) and other domains keep using the per-port sandbox host.
diff --git a/packages/code-interpreter-js/src/sandbox.ts b/packages/code-interpreter-js/src/sandbox.ts
index 324e33665..0259d412b 100644
--- a/packages/code-interpreter-js/src/sandbox.ts
+++ b/packages/code-interpreter-js/src/sandbox.ts
@@ -132,7 +132,7 @@ export class Sandbox extends BaseSandbox {
'code-interpreter-v1'
protected get jupyterUrl(): string {
- return this.connectionConfig.getSandboxDirectUrl(this.sandboxId, {
+ return this.connectionConfig.getSandboxUrl(this.sandboxId, {
sandboxDomain: this.sandboxDomain,
envdPort: JUPYTER_PORT,
})
diff --git a/packages/code-interpreter-js/tests/sandboxUrl.test.ts b/packages/code-interpreter-js/tests/sandboxUrl.test.ts
index 4566f6cd0..24ebc8ab7 100644
--- a/packages/code-interpreter-js/tests/sandboxUrl.test.ts
+++ b/packages/code-interpreter-js/tests/sandboxUrl.test.ts
@@ -17,7 +17,7 @@ function createSandbox(opts: object = {}) {
const savedEnv: Record<string, string | undefined> = {}
beforeEach(() => {
- for (const key of ['E2B_SANDBOX_URL', 'E2B_DEBUG']) {
+ for (const key of ['E2B_SANDBOX_URL', 'E2B_DEBUG', 'E2B_DOMAIN']) {
savedEnv[key] = process.env[key]
delete process.env[key]
}
@@ -33,11 +33,29 @@ afterEach(() => {
}
})
-test('jupyterUrl points directly to the sandbox host by default', () => {
+test('jupyterUrl uses the unified sandbox endpoint on supported domains', () => {
+ const sandbox = createSandbox({ domain: 'e2b.app' })
+ expect(sandbox.jupyterUrl).toBe('https://sandbox.e2b.app')
+})
+
+test('jupyterUrl prefers the sandbox domain over the config domain', () => {
+ const sandbox = createSandbox({
+ domain: 'e2b.app',
+ sandboxDomain: 'e2b.dev',
+ })
+ expect(sandbox.jupyterUrl).toBe('https://sandbox.e2b.dev')
+})
+
+test('jupyterUrl points to the per-port sandbox host on unsupported domains', () => {
const sandbox = createSandbox({ domain: 'example.dev' })
expect(sandbox.jupyterUrl).toBe('https://49999-test-sandbox-id.example.dev')
})
+test('jupyterUrl points to localhost in debug mode', () => {
+ const sandbox = createSandbox({ debug: true })
+ expect(sandbox.jupyterUrl).toBe('http://localhost:49999')
+})
+
test('jupyterUrl honors the sandboxUrl option', () => {
const sandbox = createSandbox({ sandboxUrl: 'https://proxy.example.com' })
expect(sandbox.jupyterUrl).toBe('https://proxy.example.com')
diff --git a/packages/code-interpreter-python/e2b_code_interpreter/code_interpreter_async.py b/packages/code-interpreter-python/e2b_code_interpreter/code_interpreter_async.py
index 49205fcf5..50c8e4286 100644
--- a/packages/code-interpreter-python/e2b_code_interpreter/code_interpreter_async.py
+++ b/packages/code-interpreter-python/e2b_code_interpreter/code_interpreter_async.py
@@ -1,7 +1,7 @@
import logging
import httpx
-from typing import cast, Optional, Dict, overload, Union, List
+from typing import Optional, Dict, overload, Union, List
from httpx import AsyncClient
from e2b import (
@@ -61,12 +61,9 @@ class AsyncSandbox(BaseAsyncSandbox):
@property
def _jupyter_url(self) -> str:
- # Honors the `sandbox_url` option and the `E2B_SANDBOX_URL` environment
- # variable, same as the base SDK does for envd requests.
- sandbox_url = cast(Optional[str], self.connection_config._sandbox_url)
- if sandbox_url:
- return sandbox_url
- return f"{'http' if self.connection_config.debug else 'https'}://{self.get_host(JUPYTER_PORT)}"
+ return self.connection_config.get_sandbox_url(
+ self.sandbox_id, self.sandbox_domain, port=JUPYTER_PORT
+ )
@property
def _client(self) -> AsyncClient:
diff --git a/packages/code-interpreter-python/e2b_code_interpreter/code_interpreter_sync.py b/packages/code-interpreter-python/e2b_code_interpreter/code_interpreter_sync.py
index 35377bc64..29d43f634 100644
--- a/packages/code-interpreter-python/e2b_code_interpreter/code_interpreter_sync.py
+++ b/packages/code-interpreter-python/e2b_code_interpreter/code_interpreter_sync.py
@@ -1,7 +1,7 @@
import logging
import httpx
-from typing import cast, Optional, Dict, overload, Union, List
+from typing import Optional, Dict, overload, Union, List
from httpx import Client
from e2b import Sandbox as BaseSandbox, InvalidArgumentException
from e2b.api.client_sync import get_transport
@@ -58,12 +58,9 @@ class Sandbox(BaseSandbox):
@property
def _jupyter_url(self) -> str:
- # Honors the `sandbox_url` option and the `E2B_SANDBOX_URL` environment
- # variable, same as the base SDK does for envd requests.
- sandbox_url = cast(Optional[str], self.connection_config._sandbox_url)
- if sandbox_url:
- return sandbox_url
- return f"{'http' if self.connection_config.debug else 'https'}://{self.get_host(JUPYTER_PORT)}"
+ return self.connection_config.get_sandbox_url(
+ self.sandbox_id, self.sandbox_domain, port=JUPYTER_PORT
+ )
@property
def _client(self) -> Client:
diff --git a/packages/code-interpreter-python/pyproject.toml b/packages/code-interpreter-python/pyproject.toml
index 35cd1e23e..85a49028b 100644
--- a/packages/code-interpreter-python/pyproject.toml
+++ b/packages/code-interpreter-python/pyproject.toml
@@ -9,7 +9,7 @@ requires-python = ">=3.10"
dependencies = [
"httpx>=0.20.0,<1.0.0",
"attrs>=21.3.0",
- "e2b>=2.53.0,<3.0.0",
+ "e2b>=2.55.0,<3.0.0",
]
[project.urls]
diff --git a/packages/code-interpreter-python/tests/test_sandbox_url.py b/packages/code-interpreter-python/tests/test_sandbox_url.py
index 422cc5020..87fa0b780 100644
--- a/packages/code-interpreter-python/tests/test_sandbox_url.py
+++ b/packages/code-interpreter-python/tests/test_sandbox_url.py
@@ -5,26 +5,49 @@ from e2b.connection_config import ConnectionConfig
from e2b_code_interpreter import AsyncSandbox, Sandbox
-def make_sandbox(cls, **config_kwargs):
+def make_sandbox(cls, sandbox_domain=None, **config_kwargs):
# Constructing a sandbox instance makes no network requests, so URL
# resolution can be tested without a live sandbox.
return cls(
sandbox_id="test-sandbox-id",
- sandbox_domain=None,
+ sandbox_domain=sandbox_domain,
envd_version="0.2.0",
envd_access_token=None,
connection_config=ConnectionConfig(**config_kwargs),
)
[email protected]("cls", [Sandbox, AsyncSandbox])
-async def test_jupyter_url_points_to_sandbox_host_by_default(cls, monkeypatch):
[email protected](autouse=True)
+def clean_env(monkeypatch):
monkeypatch.delenv("E2B_SANDBOX_URL", raising=False)
monkeypatch.delenv("E2B_DEBUG", raising=False)
+ monkeypatch.delenv("E2B_DOMAIN", raising=False)
+
+
[email protected]("cls", [Sandbox, AsyncSandbox])
+async def test_jupyter_url_uses_unified_endpoint_on_supported_domain(cls):
+ sandbox = make_sandbox(cls, domain="e2b.app")
+ assert sandbox._jupyter_url == "https://sandbox.e2b.app"
+
+
[email protected]("cls", [Sandbox, AsyncSandbox])
+async def test_jupyter_url_prefers_sandbox_domain_over_config_domain(cls):
+ sandbox = make_sandbox(cls, sandbox_domain="e2b.dev", domain="e2b.app")
+ assert sandbox._jupyter_url == "https://sandbox.e2b.dev"
+
+
[email protected]("cls", [Sandbox, AsyncSandbox])
+async def test_jupyter_url_points_to_per_port_host_on_unsupported_domain(cls):
sandbox = make_sandbox(cls, domain="example.dev")
assert sandbox._jupyter_url == "https://49999-test-sandbox-id.example.dev"
[email protected]("cls", [Sandbox, AsyncSandbox])
+async def test_jupyter_url_points_to_localhost_in_debug(cls):
+ sandbox = make_sandbox(cls, debug=True)
+ assert sandbox._jupyter_url == "http://localhost:49999"
+
+
@pytest.mark.parametrize("cls", [Sandbox, AsyncSandbox])
async def test_jupyter_url_honors_sandbox_url_option(cls):
sandbox = make_sandbox(cls, sandbox_url="https://proxy.example.com")
diff --git a/packages/python-sdk/e2b/connection_config.py b/packages/python-sdk/e2b/connection_config.py
index 2802e0d34..6ad35b37f 100644
--- a/packages/python-sdk/e2b/connection_config.py
+++ b/packages/python-sdk/e2b/connection_config.py
@@ -326,27 +326,35 @@ class ConnectionConfig:
def get_request_timeout(self, request_timeout: Optional[float] = None):
return self._get_request_timeout(self.request_timeout, request_timeout)
- def get_sandbox_url(self, sandbox_id: str, sandbox_domain: str) -> str:
+ def get_sandbox_url(
+ self, sandbox_id: str, sandbox_domain: str, port: Optional[int] = None
+ ) -> str:
+ port = self.envd_port if port is None else port
+
if self._sandbox_url:
return self._sandbox_url # type: ignore[return-value]
if self.debug:
- return f"http://{self.get_host(sandbox_id, sandbox_domain, self.envd_port)}"
+ return f"http://{self.get_host(sandbox_id, sandbox_domain, port)}"
sandbox_domain = sandbox_domain or self.domain
if is_supported_sandbox_domain(sandbox_domain):
return f"https://sandbox.{sandbox_domain}"
- return f"https://{self.get_host(sandbox_id, sandbox_domain, self.envd_port)}"
+ return f"https://{self.get_host(sandbox_id, sandbox_domain, port)}"
+
+ def get_sandbox_direct_url(
+ self, sandbox_id: str, sandbox_domain: str, port: Optional[int] = None
+ ) -> str:
+ port = self.envd_port if port is None else port
- def get_sandbox_direct_url(self, sandbox_id: str, sandbox_domain: str) -> str:
if self._sandbox_url:
return self._sandbox_url # type: ignore[return-value]
if self.debug:
- return f"http://{self.get_host(sandbox_id, sandbox_domain, self.envd_port)}"
+ return f"http://{self.get_host(sandbox_id, sandbox_domain, port)}"
- return f"https://{self.get_host(sandbox_id, sandbox_domain, self.envd_port)}"
+ return f"https://{self.get_host(sandbox_id, sandbox_domain, port)}"
def get_host(self, sandbox_id: str, sandbox_domain: str, port: int) -> str:
"""
diff --git a/packages/python-sdk/tests/test_connection_config.py b/packages/python-sdk/tests/test_connection_config.py
index 18e7cbdeb..a20ecd555 100644
--- a/packages/python-sdk/tests/test_connection_config.py
+++ b/packages/python-sdk/tests/test_connection_config.py
@@ -91,6 +91,51 @@ def test_sandbox_direct_url_keeps_per_sandbox_host_for_supported_domain():
)
+def test_sandbox_url_with_port_uses_stable_host_for_supported_domain():
+ config = ConnectionConfig(domain="e2b.app")
+
+ assert (
+ config.get_sandbox_url("sandbox-id", "e2b.app", port=49999)
+ == "https://sandbox.e2b.app"
+ )
+
+
+def test_sandbox_url_with_port_uses_per_sandbox_host_for_unsupported_domain():
+ config = ConnectionConfig(domain="example.dev")
+
+ assert (
+ config.get_sandbox_url("sandbox-id", "example.dev", port=49999)
+ == "https://49999-sandbox-id.example.dev"
+ )
+
+
+def test_sandbox_url_with_port_debug_uses_localhost():
+ config = ConnectionConfig(debug=True)
+
+ assert (
+ config.get_sandbox_url("sandbox-id", "e2b.app", port=49999)
+ == "http://localhost:49999"
+ )
+
+
+def test_sandbox_direct_url_with_port_uses_per_sandbox_host():
+ config = ConnectionConfig(domain="e2b.app")
+
+ assert (
+ config.get_sandbox_direct_url("sandbox-id", "e2b.app", port=49999)
+ == "https://49999-sandbox-id.e2b.app"
+ )
+
+
+def test_sandbox_url_without_port_uses_envd_port():
+ config = ConnectionConfig(domain="example.dev")
+
+ assert (
+ config.get_sandbox_url("sandbox-id", "example.dev")
+ == "https://49983-sandbox-id.example.dev"
+ )
+
+
def test_sandbox_direct_url_uses_explicit_url_first():
config = ConnectionConfig(sandbox_url="https://sandbox.example.com")