Skip to content

armgit / E2B

Public Imported from GitHub
0

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

Fix python sdk action types handling

Tomas Valentaf593eeca2331
diff --git a/packages/js-sdk/src/sandbox/index.ts b/packages/js-sdk/src/sandbox/index.ts
index 745a17d92..30ffe4b18 100644
--- a/packages/js-sdk/src/sandbox/index.ts
+++ b/packages/js-sdk/src/sandbox/index.ts
@@ -75,6 +75,7 @@ export class Sandbox extends SandboxConnection {
*/
readonly process: ProcessManager
+ // We use any here because we cannot properly reference the type of the Sandbox subclass
readonly _actions: Map<string, Action<any, any>> = new Map()
private _startCmd?: Promise<Process>
diff --git a/packages/python-sdk/e2b/sandbox/main.py b/packages/python-sdk/e2b/sandbox/main.py
index 5b8a1216e..801687deb 100644
--- a/packages/python-sdk/e2b/sandbox/main.py
+++ b/packages/python-sdk/e2b/sandbox/main.py
@@ -3,7 +3,8 @@ import urllib.parse
import requests
from os import path
-from typing import Any, Callable, Dict, List, Literal, Optional, IO, Tuple, Union
+from typing import Any, Callable, Dict, List, Literal, Optional, IO, TypeVar, Union
+from typing_extensions import Self
from e2b.api import models
from e2b.constants import TIMEOUT, ENVD_PORT, FILE_ROUTE
@@ -17,6 +18,14 @@ from e2b.sandbox.terminal import TerminalManager
logger = logging.getLogger(__name__)
+S = TypeVar(
+ "S",
+ bound="Sandbox",
+)
+
+Action = Callable[[S, Dict[str, Any]], str]
+
+
class Sandbox(SandboxConnection):
"""
E2B cloud sandbox gives your agent a full cloud development environment that's sandboxed.
@@ -126,9 +135,9 @@ class Sandbox(SandboxConnection):
timeout=timeout,
)
self._on_close_child = self._close_services
- self._actions: Dict[str, Action] = {}
+ self._actions: Dict[str, Action[Self]] = {}
- def add_action(self, action: "Action", name: Optional[str] = None) -> "Sandbox":
+ def add_action(self, action: Action[Self], name: Optional[str] = None) -> "Sandbox":
"""
Add a new action. If the name is not specified, it is automatically extracted from the function name.
An action is a function that takes a sandbox and a dictionary of arguments and returns a string.
@@ -151,6 +160,8 @@ class Sandbox(SandboxConnection):
s.add_action(name="hello", action=lambda s, args: f"Hello {args['name']}!")
```
"""
+
+ action(self, {})
if not name:
name = action.__name__
@@ -169,7 +180,7 @@ class Sandbox(SandboxConnection):
return self
@property
- def actions(self) -> Dict[str, "Action"]:
+ def actions(self) -> Dict[str, Action[Self]]:
"""
Return a dict of added actions.
"""
@@ -183,7 +194,7 @@ class Sandbox(SandboxConnection):
:param name: The name of the action, if not provided, the name of the function will be used
"""
- def _action(action: Action):
+ def _action(action: Action[Self]):
self.add_action(action=action, name=name or action.__name__)
return action
@@ -207,7 +218,7 @@ class Sandbox(SandboxConnection):
from e2b.templates.openai import OpenAI, Actions
- return OpenAI(Actions(self))
+ return OpenAI[Self](Actions[Self](self))
def _handle_start_cmd_logs(self):
self._start_cmd = self.process.start(
@@ -296,6 +307,3 @@ class Sandbox(SandboxConnection):
def __exit__(self, exc_type, exc_value, traceback):
self.close()
-
-
-Action = Callable[[Sandbox, Dict[str, Any]], str]
diff --git a/packages/python-sdk/e2b/templates/openai.py b/packages/python-sdk/e2b/templates/openai.py
index 0d1d69414..105ec8aba 100644
--- a/packages/python-sdk/e2b/templates/openai.py
+++ b/packages/python-sdk/e2b/templates/openai.py
@@ -2,8 +2,10 @@ import json
import logging
from typing import (
+ Generic,
List,
TYPE_CHECKING,
+ TypeVar,
)
from e2b.sandbox.main import Sandbox
@@ -15,8 +17,14 @@ if TYPE_CHECKING:
from openai.types.beta.threads.run import Run
-class Actions:
- def __init__(self, sandbox: "Sandbox"):
+S = TypeVar(
+ "S",
+ bound=Sandbox,
+)
+
+
+class Actions(Generic[S]):
+ def __init__(self, sandbox: S):
self._sandbox = sandbox
def run(self, run: "Run") -> List["ToolOutput"]:
@@ -54,8 +62,8 @@ class Actions:
return outputs
-class OpenAI:
- def __init__(self, actions: Actions):
+class OpenAI(Generic[S]):
+ def __init__(self, actions: Actions[S]):
self._actions = actions
@property
diff --git a/packages/python-sdk/poetry.lock b/packages/python-sdk/poetry.lock
index 388e13c9d..1ce23bbde 100644
--- a/packages/python-sdk/poetry.lock
+++ b/packages/python-sdk/poetry.lock
@@ -1,9 +1,10 @@
-# This file is automatically @generated by Poetry 1.5.1 and should not be changed by hand.
+# This file is automatically @generated by Poetry 1.4.2 and should not be changed by hand.
[[package]]
name = "aenum"
version = "3.1.15"
description = "Advanced Enumerations (compatible with Python's stdlib Enum), NamedTuples, and NamedConstants"
+category = "main"
optional = false
python-versions = "*"
files = [
@@ -16,6 +17,7 @@ files = [
name = "aiohttp"
version = "3.8.6"
description = "Async http client/server framework (asyncio)"
+category = "main"
optional = false
python-versions = ">=3.6"
files = [
@@ -124,6 +126,7 @@ speedups = ["Brotli", "aiodns", "cchardet"]
name = "aiosignal"
version = "1.3.1"
description = "aiosignal: a list of registered asynchronous callbacks"
+category = "main"
optional = false
python-versions = ">=3.7"
files = [
@@ -138,6 +141,7 @@ frozenlist = ">=1.1.0"
name = "annotated-types"
version = "0.6.0"
description = "Reusable constraint types to use with typing.Annotated"
+category = "main"
optional = false
python-versions = ">=3.8"
files = [
@@ -152,6 +156,7 @@ typing-extensions = {version = ">=4.0.0", markers = "python_version < \"3.9\""}
name = "anyio"
version = "3.7.1"
description = "High level compatibility layer for multiple asynchronous event loop implementations"
+category = "dev"
optional = false
python-versions = ">=3.7"
files = [
@@ -173,6 +178,7 @@ trio = ["trio (<0.22)"]
name = "async-timeout"
version = "4.0.3"
description = "Timeout context manager for asyncio programs"
+category = "main"
optional = false
python-versions = ">=3.7"
files = [
@@ -184,6 +190,7 @@ files = [
name = "attrs"
version = "23.1.0"
description = "Classes Without Boilerplate"
+category = "main"
optional = false
python-versions = ">=3.7"
files = [
@@ -202,6 +209,7 @@ tests-no-zope = ["cloudpickle", "hypothesis", "mypy (>=1.1.1)", "pympler", "pyte
name = "black"
version = "23.9.1"
description = "The uncompromising code formatter."
+category = "dev"
optional = false
python-versions = ">=3.8"
files = [
@@ -248,6 +256,7 @@ uvloop = ["uvloop (>=0.15.2)"]
name = "certifi"
version = "2023.7.22"
description = "Python package for providing Mozilla's CA Bundle."
+category = "main"
optional = false
python-versions = ">=3.6"
files = [
@@ -259,6 +268,7 @@ files = [
name = "charset-normalizer"
version = "3.3.0"
description = "The Real First Universal Charset Detector. Open, modern and actively maintained alternative to Chardet."
+category = "main"
optional = false
python-versions = ">=3.7.0"
files = [
@@ -358,6 +368,7 @@ files = [
name = "click"
version = "8.1.7"
description = "Composable command line interface toolkit"
+category = "dev"
optional = false
python-versions = ">=3.7"
files = [
@@ -372,6 +383,7 @@ colorama = {version = "*", markers = "platform_system == \"Windows\""}
name = "colorama"
version = "0.4.6"
description = "Cross-platform colored terminal text."
+category = "dev"
optional = false
python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,!=3.6.*,>=2.7"
files = [
@@ -383,6 +395,7 @@ files = [
name = "distro"
version = "1.8.0"
description = "Distro - an OS platform information API"
+category = "dev"
optional = false
python-versions = ">=3.6"
files = [
@@ -394,6 +407,7 @@ files = [
name = "exceptiongroup"
version = "1.1.3"
description = "Backport of PEP 654 (exception groups)"
+category = "dev"
optional = false
python-versions = ">=3.7"
files = [
@@ -408,6 +422,7 @@ test = ["pytest (>=6)"]
name = "execnet"
version = "2.0.2"
description = "execnet: rapid multi-Python deployment"
+category = "dev"
optional = false
python-versions = ">=3.7"
files = [
@@ -422,6 +437,7 @@ testing = ["hatch", "pre-commit", "pytest", "tox"]
name = "frozenlist"
version = "1.4.0"
description = "A list-like structure which implements collections.abc.MutableSequence"
+category = "main"
optional = false
python-versions = ">=3.8"
files = [
@@ -492,6 +508,7 @@ files = [
name = "h11"
version = "0.14.0"
description = "A pure-Python, bring-your-own-I/O implementation of HTTP/1.1"
+category = "dev"
optional = false
python-versions = ">=3.7"
files = [
@@ -503,6 +520,7 @@ files = [
name = "httpcore"
version = "1.0.2"
description = "A minimal low-level HTTP client."
+category = "dev"
optional = false
python-versions = ">=3.8"
files = [
@@ -517,13 +535,14 @@ h11 = ">=0.13,<0.15"
[package.extras]
asyncio = ["anyio (>=4.0,<5.0)"]
http2 = ["h2 (>=3,<5)"]
-socks = ["socksio (==1.*)"]
+socks = ["socksio (>=1.0.0,<2.0.0)"]
trio = ["trio (>=0.22.0,<0.23.0)"]
[[package]]
name = "httpx"
version = "0.25.1"
description = "The next generation HTTP client."
+category = "dev"
optional = false
python-versions = ">=3.8"
files = [
@@ -540,14 +559,15 @@ sniffio = "*"
[package.extras]
brotli = ["brotli", "brotlicffi"]
-cli = ["click (==8.*)", "pygments (==2.*)", "rich (>=10,<14)"]
+cli = ["click (>=8.0.0,<9.0.0)", "pygments (>=2.0.0,<3.0.0)", "rich (>=10,<14)"]
http2 = ["h2 (>=3,<5)"]
-socks = ["socksio (==1.*)"]
+socks = ["socksio (>=1.0.0,<2.0.0)"]
[[package]]
name = "idna"
version = "3.4"
description = "Internationalized Domain Names in Applications (IDNA)"
+category = "main"
optional = false
python-versions = ">=3.5"
files = [
@@ -559,6 +579,7 @@ files = [
name = "iniconfig"
version = "2.0.0"
description = "brain-dead simple config-ini parsing"
+category = "dev"
optional = false
python-versions = ">=3.7"
files = [
@@ -570,6 +591,7 @@ files = [
name = "jsonrpcclient"
version = "4.0.3"
description = "Send JSON-RPC requests"
+category = "main"
optional = false
python-versions = ">=3.6"
files = [
@@ -583,6 +605,7 @@ qa = ["pytest", "pytest-cov", "tox"]
name = "multidict"
version = "6.0.4"
description = "multidict implementation"
+category = "main"
optional = false
python-versions = ">=3.7"
files = [
@@ -666,6 +689,7 @@ files = [
name = "mypy-extensions"
version = "1.0.0"
description = "Type system extensions for programs checked with the mypy type checker."
+category = "dev"
optional = false
python-versions = ">=3.5"
files = [
@@ -677,6 +701,7 @@ files = [
name = "openai"
version = "1.2.3"
description = "The official Python library for the openai API"
+category = "dev"
optional = false
python-versions = ">=3.7.1"
files = [
@@ -699,6 +724,7 @@ datalib = ["numpy (>=1)", "pandas (>=1.2.3)", "pandas-stubs (>=1.1.0.11)"]
name = "packaging"
version = "23.2"
description = "Core utilities for Python packages"
+category = "dev"
optional = false
python-versions = ">=3.7"
files = [
@@ -710,6 +736,7 @@ files = [
name = "pathspec"
version = "0.11.2"
description = "Utility library for gitignore style pattern matching of file paths."
+category = "dev"
optional = false
python-versions = ">=3.7"
files = [
@@ -721,6 +748,7 @@ files = [
name = "platformdirs"
version = "3.11.0"
description = "A small Python package for determining appropriate platform-specific dirs, e.g. a \"user data dir\"."
+category = "dev"
optional = false
python-versions = ">=3.7"
files = [
@@ -736,6 +764,7 @@ test = ["appdirs (==1.4.4)", "covdefaults (>=2.3)", "pytest (>=7.4)", "pytest-co
name = "pluggy"
version = "1.3.0"
description = "plugin and hook calling mechanisms for python"
+category = "dev"
optional = false
python-versions = ">=3.8"
files = [
@@ -747,30 +776,11 @@ files = [
dev = ["pre-commit", "tox"]
testing = ["pytest", "pytest-benchmark"]
-[[package]]
-name = "psycopg2"
-version = "2.9.9"
-description = "psycopg2 - Python-PostgreSQL Database Adapter"
-optional = false
-python-versions = ">=3.7"
-files = [
- {file = "psycopg2-2.9.9-cp310-cp310-win32.whl", hash = "sha256:38a8dcc6856f569068b47de286b472b7c473ac7977243593a288ebce0dc89516"},
- {file = "psycopg2-2.9.9-cp310-cp310-win_amd64.whl", hash = "sha256:426f9f29bde126913a20a96ff8ce7d73fd8a216cfb323b1f04da402d452853c3"},
- {file = "psycopg2-2.9.9-cp311-cp311-win32.whl", hash = "sha256:ade01303ccf7ae12c356a5e10911c9e1c51136003a9a1d92f7aa9d010fb98372"},
- {file = "psycopg2-2.9.9-cp311-cp311-win_amd64.whl", hash = "sha256:121081ea2e76729acfb0673ff33755e8703d45e926e416cb59bae3a86c6a4981"},
- {file = "psycopg2-2.9.9-cp37-cp37m-win32.whl", hash = "sha256:5e0d98cade4f0e0304d7d6f25bbfbc5bd186e07b38eac65379309c4ca3193efa"},
- {file = "psycopg2-2.9.9-cp37-cp37m-win_amd64.whl", hash = "sha256:7e2dacf8b009a1c1e843b5213a87f7c544b2b042476ed7755be813eaf4e8347a"},
- {file = "psycopg2-2.9.9-cp38-cp38-win32.whl", hash = "sha256:ff432630e510709564c01dafdbe996cb552e0b9f3f065eb89bdce5bd31fabf4c"},
- {file = "psycopg2-2.9.9-cp38-cp38-win_amd64.whl", hash = "sha256:bac58c024c9922c23550af2a581998624d6e02350f4ae9c5f0bc642c633a2d5e"},
- {file = "psycopg2-2.9.9-cp39-cp39-win32.whl", hash = "sha256:c92811b2d4c9b6ea0285942b2e7cac98a59e166d59c588fe5cfe1eda58e72d59"},
- {file = "psycopg2-2.9.9-cp39-cp39-win_amd64.whl", hash = "sha256:de80739447af31525feddeb8effd640782cf5998e1a4e9192ebdf829717e3913"},
- {file = "psycopg2-2.9.9.tar.gz", hash = "sha256:d1454bde93fb1e224166811694d600e746430c006fbb031ea06ecc2ea41bf156"},
-]
-
[[package]]
name = "pydantic"
version = "2.4.2"
description = "Data validation using Python type hints"
+category = "main"
optional = false
python-versions = ">=3.7"
files = [
@@ -790,6 +800,7 @@ email = ["email-validator (>=2.0.0)"]
name = "pydantic-core"
version = "2.10.1"
description = ""
+category = "main"
optional = false
python-versions = ">=3.7"
files = [
@@ -908,6 +919,7 @@ typing-extensions = ">=4.6.0,<4.7.0 || >4.7.0"
name = "pytest"
version = "7.4.2"
description = "pytest: simple powerful testing with Python"
+category = "dev"
optional = false
python-versions = ">=3.7"
files = [
@@ -930,6 +942,7 @@ testing = ["argcomplete", "attrs (>=19.2.0)", "hypothesis (>=3.56)", "mock", "no
name = "pytest-dotenv"
version = "0.5.2"
description = "A py.test plugin that parses environment files before running tests"
+category = "dev"
optional = false
python-versions = "*"
files = [
@@ -945,6 +958,7 @@ python-dotenv = ">=0.9.1"
name = "pytest-xdist"
version = "3.3.1"
description = "pytest xdist plugin for distributed testing, most importantly across multiple CPUs"
+category = "dev"
optional = false
python-versions = ">=3.7"
files = [
@@ -965,6 +979,7 @@ testing = ["filelock"]
name = "python-dateutil"
version = "2.8.2"
description = "Extensions to the standard Python datetime module"
+category = "main"
optional = false
python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,>=2.7"
files = [
@@ -979,6 +994,7 @@ six = ">=1.5"
name = "python-dotenv"
version = "1.0.0"
description = "Read key-value pairs from a .env file and set them as environment variables"
+category = "dev"
optional = false
python-versions = ">=3.8"
files = [
@@ -993,6 +1009,7 @@ cli = ["click (>=5.0)"]
name = "requests"
version = "2.31.0"
description = "Python HTTP for Humans."
+category = "main"
optional = false
python-versions = ">=3.7"
files = [
@@ -1014,6 +1031,7 @@ use-chardet-on-py3 = ["chardet (>=3.0.2,<6)"]
name = "six"
version = "1.16.0"
description = "Python 2 and 3 compatibility utilities"
+category = "main"
optional = false
python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*"
files = [
@@ -1025,6 +1043,7 @@ files = [
name = "sniffio"
version = "1.3.0"
description = "Sniff out which async library your code is running under"
+category = "dev"
optional = false
python-versions = ">=3.7"
files = [
@@ -1036,6 +1055,7 @@ files = [
name = "tomli"
version = "2.0.1"
description = "A lil' TOML parser"
+category = "dev"
optional = false
python-versions = ">=3.7"
files = [
@@ -1047,6 +1067,7 @@ files = [
name = "tqdm"
version = "4.66.1"
description = "Fast, Extensible Progress Meter"
+category = "dev"
optional = false
python-versions = ">=3.7"
files = [
@@ -1067,6 +1088,7 @@ telegram = ["requests"]
name = "typing-extensions"
version = "4.8.0"
description = "Backported and Experimental Type Hints for Python 3.8+"
+category = "main"
optional = false
python-versions = ">=3.8"
files = [
@@ -1078,6 +1100,7 @@ files = [
name = "urllib3"
version = "2.0.6"
description = "HTTP library with thread-safe connection pooling, file post, and more."
+category = "main"
optional = false
python-versions = ">=3.7"
files = [
@@ -1095,6 +1118,7 @@ zstd = ["zstandard (>=0.18.0)"]
name = "websockets"
version = "11.0.3"
description = "An implementation of the WebSocket Protocol (RFC 6455 & 7692)"
+category = "main"
optional = false
python-versions = ">=3.7"
files = [
@@ -1174,6 +1198,7 @@ files = [
name = "yarl"
version = "1.9.2"
description = "Yet another URL library"
+category = "main"
optional = false
python-versions = ">=3.7"
files = [
@@ -1260,4 +1285,4 @@ multidict = ">=4.0"
[metadata]
lock-version = "2.0"
python-versions = "^3.8"
-content-hash = "b9649e1889f1bf33364a96cadffad313dba1262c3ab5917892187802eec9c947"
+content-hash = "6dd6e2f20e2f1f2db29835134b7f092426655c8c97a7a8d97d07f6165fbe8deb"
diff --git a/packages/python-sdk/pyproject.toml b/packages/python-sdk/pyproject.toml
index e039964b0..8e4a162d4 100644
--- a/packages/python-sdk/pyproject.toml
+++ b/packages/python-sdk/pyproject.toml
@@ -20,12 +20,12 @@ aenum = ">=3.1.11"
websockets = "^11.0.3"
jsonrpcclient = "^4.0.3"
requests = "^2.31.0"
+typing-extensions = "^4.8.0"
[tool.poetry.group.dev.dependencies]
black = "^23.7.0"
pytest = "^7.4.0"
pytest-xdist = "^3.3.1"
-psycopg2 = "^2.9.7"
python-dotenv = "^1.0.0"
pytest-dotenv = "^0.5.2"
openai = "^1.2.3"