Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
from typing import Dict, Type, TypeVar, Unpack, cast
from e2b import ApiParams, E2BClientParams
from e2b import E2B as CoreE2B
from e2b_desktop.main import Sandbox
T = TypeVar("T")
__all__ = ["E2B", "E2BClientParams"]
def _bind(cls: Type[T], api_params: ApiParams) -> Type[T]:
"""Generate a subclass of ``cls`` carrying ``api_params`` as its bound params."""
return cast(
Type[T],
type(cls.__name__, (cls,), {"_bound_api_params": api_params}),
)
class E2B:
"""
E2B client with an explicitly bound connection configuration.
The resource classes exposed by the client (`Sandbox`, `Volume`,
`AsyncVolume`, `Template`, `AsyncTemplate`, `Secret`, `AsyncSecret`) behave
exactly like the top-level exports of the same name, except the params
passed to the client are used as the defaults instead of the environment
variables.
Per-call params still take precedence over the client's params.
Multiple clients are fully isolated from each other and from the top-level
env-configured exports.
Example:
```python
from e2b_desktop import E2B
client = E2B(api_key="e2b_...", domain="e2b.dev")
desktop = client.Sandbox.create()
desktop.stream.start()
```
"""
def __init__(self, **opts: Unpack[E2BClientParams]):
"""
Create a new client with the API params bound to it.
:param opts: API params used as the defaults for every call made
through this client's resource classes.
"""
# Params are copied so later mutations of the caller's dicts cannot
# change the bound configuration.
api_params = cast(ApiParams, dict(cast(Dict[str, object], opts)))
headers = api_params.get("headers")
if headers is not None:
api_params["headers"] = dict(headers)
api_headers = api_params.get("api_headers")
if api_headers is not None:
api_params["api_headers"] = dict(api_headers)
self.Sandbox = _bind(Sandbox, api_params)
"""Desktop `Sandbox` class bound to this client's connection configuration."""
# The resources that are not specific to the Desktop are bound by the
# core client.
core = CoreE2B(**api_params)
self.Volume = core.Volume
"""`Volume` class bound to this client's connection configuration."""
self.AsyncVolume = core.AsyncVolume
"""`AsyncVolume` class bound to this client's connection configuration."""
self.Template = core.Template
"""`Template` class bound to this client's connection configuration."""
self.AsyncTemplate = core.AsyncTemplate
"""`AsyncTemplate` class bound to this client's connection configuration."""
self.Secret = core.Secret
"""`Secret` class bound to this client's connection configuration."""
self.AsyncSecret = core.AsyncSecret
"""`AsyncSecret` class bound to this client's connection configuration."""