open-swe/agent/integrations/e2b.py
Adam Moussa cfb5624663
Some checks are pending
CI / Lint (push) Waiting to run
CI / Format check (push) Waiting to run
CI / Typecheck (push) Waiting to run
CI / Unit tests (push) Waiting to run
CI / Playwright E2E (push) Waiting to run
CI / Docker build smoke (push) Waiting to run
CI / Triage ledger up to date (push) Waiting to run
CI / ui bun.lock in sync (push) Waiting to run
feat(open-swe): add E2B sandbox provider (port of upstream 48217b68, #1489) (#199)
Additive sandbox provider adapted to this fork's synchronous create_sandbox
factory: agent/integrations/e2b.py registered as a lazy-import entry in
SANDBOX_FACTORIES, so the e2b/langchain-e2b imports only load when
SANDBOX_TYPE=e2b (dark-safe on unset env; unset E2B_API_KEY raises a clean
ValueError only when the provider is selected). Supports reconnect-by-id and
optional E2B_TEMPLATE. Non-langsmith providers skip the GitHub proxy step,
so the GitHub-App token flow is untouched.

Upstream: langchain-ai/open-swe 48217b68 (#1489), re-implemented against the
fork's sync sandbox lifecycle rather than cherry-picked (upstream ships on
the deferred async sandbox.py base).

Docs: provider tables/lists in README, CUSTOMIZATION, INSTALLATION; the
CUSTOMIZATION registration example now shows the lazy-tuple form the code
actually uses. uv.lock refreshed (adds e2b, langchain-e2b, dockerfile-parse).
2026-07-16 18:23:42 -04:00

46 lines
1.3 KiB
Python

import os
from deepagents.backends.protocol import SandboxBackendProtocol
from e2b import Sandbox
from langchain_e2b import E2BSandbox
DEFAULT_E2B_SANDBOX_TIMEOUT = 60 * 60
E2B_TEMPLATE_ENV = "E2B_TEMPLATE"
def create_e2b_sandbox(sandbox_id: str | None = None) -> SandboxBackendProtocol:
"""Create or reconnect to an E2B sandbox.
Args:
sandbox_id: Optional existing sandbox ID to reconnect to.
If None, creates a new sandbox.
Returns:
E2BSandbox instance implementing SandboxBackendProtocol.
"""
api_key = os.getenv("E2B_API_KEY")
if not api_key:
raise ValueError("E2B_API_KEY environment variable is required")
template = os.getenv(E2B_TEMPLATE_ENV)
if template is not None:
template = template.strip()
if not template:
raise ValueError(f"{E2B_TEMPLATE_ENV} must not be empty")
if sandbox_id:
sandbox = Sandbox.connect(
sandbox_id,
timeout=DEFAULT_E2B_SANDBOX_TIMEOUT,
api_key=api_key,
)
elif template:
sandbox = Sandbox.create(
template=template,
timeout=DEFAULT_E2B_SANDBOX_TIMEOUT,
api_key=api_key,
)
else:
sandbox = Sandbox.create(timeout=DEFAULT_E2B_SANDBOX_TIMEOUT, api_key=api_key)
return E2BSandbox(sandbox=sandbox)