Skip to content

armgit / E2B

Public Imported from GitHub
0

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

Add async playground calls

Tomas Valentaa2faa9160acc
diff --git a/api-service/app.py b/api-service/app.py
index c43877331..215818fee 100644
--- a/api-service/app.py
+++ b/api-service/app.py
@@ -87,7 +87,7 @@ async def generate():
)
await db.update_state(run_id=run_id, state=DeploymentState.Deploying)
- url = playground.deploy(project_id)
+ url = await playground.deploy(project_id)
await db.finish_deployment(run_id=run_id, url=url)
return {}
diff --git a/api-service/codegen/tools/human/tools.py b/api-service/codegen/tools/human/tools.py
index 81c0874c6..330c16405 100644
--- a/api-service/codegen/tools/human/tools.py
+++ b/api-service/codegen/tools/human/tools.py
@@ -2,6 +2,7 @@ from collections.abc import Generator
from typing import Any
from codegen.tools.async_tool import async_tool
+from playground_client.models.tools_human_response import ToolsHumanResponse
from session.playground.base import Playground
@@ -19,9 +20,12 @@ def create_human_tools(
<action tool="AskHuman">
I'm not sure what to do, can you specify what should to do next?
</action>"""
- response = playground.api.wait_for_human_response(
+ thread: Any = playground.api.wait_for_human_response(
run_id=run_id,
+ async_req=True,
)
+
+ response: ToolsHumanResponse = thread.get()
return response.response
yield ask_human
diff --git a/api-service/codegen/tools/playground/tools/code.py b/api-service/codegen/tools/playground/tools/code.py
index df681f616..08a157a98 100644
--- a/api-service/codegen/tools/playground/tools/code.py
+++ b/api-service/codegen/tools/playground/tools/code.py
@@ -19,7 +19,7 @@ def create_code_tools(playground: NodeJSPlayground, mock: MockRequestFactory, **
package_name_1 package_name_2
</action>
"""
- output = playground.install_dependencies(extract_code(dependencies))
+ output = await playground.install_dependencies(extract_code(dependencies))
result = encode_command_output(output, only_errors=True)
return result if len(result) > 0 else "All dependencies installed"
@@ -42,7 +42,7 @@ def create_code_tools(playground: NodeJSPlayground, mock: MockRequestFactory, **
nonlocal last_javascript_code
last_javascript_code = code
- output = playground.run_javascript_code(code)
+ output = await playground.run_javascript_code(code)
result = encode_command_output(output)
return result if len(result) > 0 else "Code execution finished without error"
@@ -74,7 +74,7 @@ def create_code_tools(playground: NodeJSPlayground, mock: MockRequestFactory, **
(
request_output,
server_output,
- ) = playground.run_javascript_server_code_with_request(
+ ) = await playground.run_javascript_server_code_with_request(
code=last_javascript_code,
# request_cmd=mock_request_cmd,
request_cmd=curl_command.strip(),
diff --git a/api-service/codegen/tools/playground/tools/filesystem.py b/api-service/codegen/tools/playground/tools/filesystem.py
index 57b092bb2..ce5a996f2 100644
--- a/api-service/codegen/tools/playground/tools/filesystem.py
+++ b/api-service/codegen/tools/playground/tools/filesystem.py
@@ -21,7 +21,7 @@ def create_filesystem_tools(playground: NodeJSPlayground, **kwargs):
@async_tool("ReadFile")
async def read_file(path: str) -> str:
"""Read content of the file from filesystem."""
- return playground.read_file(path)
+ return await playground.read_file(path)
yield read_file
@@ -33,7 +33,7 @@ def create_filesystem_tools(playground: NodeJSPlayground, **kwargs):
"""
path, content = input.lstrip().split("\n", 1)
- playground.write_file(path, content)
+ await playground.write_file(path, content)
return "File saved"
yield save_file
@@ -41,7 +41,7 @@ def create_filesystem_tools(playground: NodeJSPlayground, **kwargs):
@async_tool("DeleteFile")
async def delete_file(path: str) -> str:
"""Delete the file."""
- playground.delete_file(path)
+ await playground.delete_file(path)
return "File deleted"
yield delete_file
@@ -49,7 +49,7 @@ def create_filesystem_tools(playground: NodeJSPlayground, **kwargs):
@async_tool("ListDirectory")
async def list_directory(path: str) -> str:
"""List all files and subdirectories from the directory."""
- entries = playground.list_dir(path)
+ entries = await playground.list_dir(path)
return encode_directory_listing(entries)
yield list_directory
@@ -57,7 +57,7 @@ def create_filesystem_tools(playground: NodeJSPlayground, **kwargs):
@async_tool("DeleteDirectory")
async def delete_directory(path: str) -> str:
"""Delete the directory."""
- playground.delete_dir(path)
+ await playground.delete_dir(path)
return "Directory deleted"
yield delete_directory
diff --git a/api-service/codegen/tools/playground/tools/process.py b/api-service/codegen/tools/playground/tools/process.py
index 1d09629e1..1dc28a021 100644
--- a/api-service/codegen/tools/playground/tools/process.py
+++ b/api-service/codegen/tools/playground/tools/process.py
@@ -39,7 +39,7 @@ def create_process_tools(playground: NodeJSPlayground, **kwargs):
The returned result will be the output and errors returned when running the command.
"""
await playground.update_envs()
- output = playground.run_command(command)
+ output = await playground.run_command(command)
result = encode_command_output(output)
return result if len(result) > 0 else "Process finished without error"
diff --git a/api-service/session/playground/base.py b/api-service/session/playground/base.py
index 46646e748..443e2cf2e 100644
--- a/api-service/session/playground/base.py
+++ b/api-service/session/playground/base.py
@@ -1,5 +1,11 @@
import time
import math
+from typing import Any
+from playground_client.models.list_filesystem_dir_response import ListFilesystemDirResponse
+from playground_client.models.mock_data_response import MockDataResponse
+from playground_client.models.process_response import ProcessResponse
+from playground_client.models.read_filesystem_file_response import ReadFilesystemFileResponse
+from playground_client.models.session_response import SessionResponse
from session.env import cmd_with_env_vars
from session.session import GetEnvs, Session
@@ -22,24 +28,25 @@ class Playground(Session):
def __init__(self, env_id: str, get_envs: GetEnvs):
super().__init__(env_id, get_envs)
- def get_open_ports(self):
- response = self.api.get_session(self.id)
+ async def get_open_ports(self):
+ thread: Any = self.api.get_session(self.id, async_req=True)
+ response: SessionResponse = thread.get()
return response.ports
- def is_port_open(self, port: float) -> bool:
- open_ports = self.get_open_ports()
+ async def is_port_open(self, port: float) -> bool:
+ open_ports = await self.get_open_ports()
return any(
open_port.port == port and open_port.state == "LISTEN"
for open_port in open_ports
)
- def run_command(
+ async def run_command(
self,
cmd: str,
rootdir=rootdir,
timeout: float | None = None,
):
- response = self.api.start_process(
+ thread: Any = self.api.start_process(
self.id,
playground_client.StartProcessParams(
cmd=cmd_with_env_vars(cmd, self.env_vars),
@@ -48,37 +55,46 @@ class Playground(Session):
rootdir=rootdir,
),
wait=True if timeout is None else False,
+ async_req=True,
)
+ response: ProcessResponse = thread.get()
+
if timeout is None or response.finished:
return response
# Multiply the timeout by the frequency so when we sleep for the 1/frequency the total time sleeping will be the timeout.
for _ in range(math.ceil(timeout * self.run_command_timeout_frequency)):
- response = self.api.get_process(
+ thread = self.api.get_process(
self.id,
process_id=response.process_id,
+ async_req=True,
)
+
+ response: ProcessResponse = thread.get()
+
if response.finished:
return response
time.sleep(1 / self.run_command_timeout_frequency)
if not response.finished:
- self.api.stop_process(
+ thread: Any = self.api.stop_process(
self.id,
process_id=response.process_id,
results=False,
+ async_req=True,
)
+ thread.get()
return response
- def start_process(
+ async def start_process(
self,
cmd: str,
rootdir=rootdir,
):
"""Start process and return the process ID."""
- return self.api.start_process(
+ thread: Any = self.api.start_process(
self.id,
playground_client.StartProcessParams(
cmd=cmd_with_env_vars(cmd, self.env_vars),
@@ -86,62 +102,83 @@ class Playground(Session):
envVars=self.env_vars,
rootdir=rootdir,
),
- ).process_id
+ async_req=True,
+ )
+
+ response: ProcessResponse = thread.get()
+ return response.process_id
- def stop_process(self, process_id: str):
- return self.api.stop_process(
+ async def stop_process(self, process_id: str):
+ thread: Any = self.api.stop_process(
self.id,
process_id=process_id,
results=True,
+ async_req=True,
)
- def read_file(self, path: str):
- return self.api.read_filesystem_file(self.id, path).content
+ response: ProcessResponse = thread.get()
+ return response
- def list_dir(self, path: str):
- return self.api.list_filesystem_dir(self.id, path).entries
+ async def read_file(self, path: str):
+ thread: Any = self.api.read_filesystem_file(self.id, path, async_req=True)
+ response: ReadFilesystemFileResponse = thread.get()
+ return response.content
- def delete_file(self, path: str):
- self.api.delete_filesystem_entry(self.id, path)
+ async def list_dir(self, path: str):
+ thread: Any = self.api.list_filesystem_dir(self.id, path, async_req=True)
+ response: ListFilesystemDirResponse = thread.get()
+ return response.entries
- def mock_body_data(self, code: str, interface: str):
- return self.api.create_mock_body_data(
+ async def delete_file(self, path: str):
+ thread: Any = self.api.delete_filesystem_entry(self.id, path, async_req=True)
+ thread.get()
+
+ async def mock_body_data(self, code: str, interface: str):
+ thread: Any = self.api.create_mock_body_data(
CreateMockBodyDataRequest(
targetInterface=interface,
files=[File(name=self.mock_data_filename, content=code)],
- )
- ).body_data
+ ),
+ async_req=True,
+ )
+
+ response: MockDataResponse = thread.get()
+ return response.body_data
- def delete_dir(self, path: str):
- self.api.delete_filesystem_entry(self.id, path)
+ async def delete_dir(self, path: str):
+ thread: Any = self.api.delete_filesystem_entry(self.id, path, async_req=True)
+ thread.get()
- def write_file(self, path: str, content: str):
- self.api.write_filesystem_file(
+ async def write_file(self, path: str, content: str):
+ thread: Any = self.api.write_filesystem_file(
self.id,
path,
playground_client.WriteFilesystemFileRequest(content=content),
+ async_req=True
)
+ thread.get()
- def make_dir(self, path: str):
- self.api.make_filesystem_dir(self.id, path)
+ async def make_dir(self, path: str):
+ thread: Any = self.api.make_filesystem_dir(self.id, path, async_req=True)
+ thread.get()
- def run_server_with_request(
+ async def run_server_with_request(
self,
server_cmd: str,
request_cmd: str,
port: float,
rootdir=rootdir,
):
- server_process_id = self.start_process(
+ server_process_id = await self.start_process(
cmd=server_cmd,
rootdir=rootdir,
)
for _ in range(self.max_port_checks):
- if self.is_port_open(port):
+ if await self.is_port_open(port):
break
time.sleep(self.port_check_interval)
- request_result = self.run_command(cmd=request_cmd, rootdir=rootdir)
- server_result = self.stop_process(server_process_id)
+ request_result = await self.run_command(cmd=request_cmd, rootdir=rootdir)
+ server_result = await self.stop_process(server_process_id)
return request_result, server_result
diff --git a/api-service/session/playground/nodejs.py b/api-service/session/playground/nodejs.py
index d9fecde9e..0e8a089b5 100644
--- a/api-service/session/playground/nodejs.py
+++ b/api-service/session/playground/nodejs.py
@@ -13,10 +13,10 @@ class NodeJSPlayground(Playground):
def __init__(self, get_envs: GetEnvs):
super().__init__(NodeJSPlayground.node_js_env_id, get_envs)
- def run_javascript_code(self, code: str):
+ async def run_javascript_code(self, code: str):
# print(f"Running javascript code...")
- self.write_file(self.default_javascript_code_file, code)
- result = self.run_command(
+ await self.write_file(self.default_javascript_code_file, code)
+ result = await self.run_command(
f"node {self.default_javascript_code_file}",
rootdir=self.rootdir,
timeout=self.run_code_timeout,
@@ -24,21 +24,21 @@ class NodeJSPlayground(Playground):
# pprint.pprint(f"Result: {result}")
return result
- def install_dependencies(self, dependencies: str):
+ async def install_dependencies(self, dependencies: str):
# print(f"Installing dependencies: {dependencies}")
- result = self.run_command(f"npm install {dependencies}", rootdir=self.rootdir)
+ result = await self.run_command(f"npm install {dependencies}", rootdir=self.rootdir)
# pprint.pprint(f"Result: {result}")
return result
- def run_javascript_server_code_with_request(
+ async def run_javascript_server_code_with_request(
self,
code: str,
request_cmd: str,
port: float,
):
# print(f"Running '{request_cmd}' for code:\n{code}")
- self.write_file(self.default_javascript_code_file, code)
- result = self.run_server_with_request(
+ await self.write_file(self.default_javascript_code_file, code)
+ result = await self.run_server_with_request(
server_cmd=f"node {self.default_javascript_code_file}",
request_cmd=request_cmd,
port=port,
diff --git a/api-service/session/session.py b/api-service/session/session.py
index f5dcbd6e2..896052918 100644
--- a/api-service/session/session.py
+++ b/api-service/session/session.py
@@ -1,6 +1,7 @@
from typing import Callable, Coroutine, List, Any
import playground_client
+from playground_client.models.deployment_response import DeploymentResponse
from session.env import EnvVar, format_env_vars
@@ -46,11 +47,15 @@ class Session:
self.is_closed = True
self.client.close()
- def deploy(self, project_id: str):
- return self.api.create_deployment(
+ async def deploy(self, project_id: str):
+ thread: Any = self.api.create_deployment(
project_id=project_id,
session_id=self.id,
create_deployment_request=playground_client.CreateDeploymentRequest(
envVars=self.env_vars,
),
- ).url
+ async_req=True,
+ )
+
+ response: DeploymentResponse = thread.get()
+ return response.url