Skip to content

armgit / E2B

Public Imported from GitHub
0

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

Fix timeout check; Cleanup

Tomas Valenta50942ac80cf5
diff --git a/api-service/codegen/tools/playground/playground.py b/api-service/codegen/tools/playground/playground.py
index a63c22bf9..37b91a55d 100644
--- a/api-service/codegen/tools/playground/playground.py
+++ b/api-service/codegen/tools/playground/playground.py
@@ -1,5 +1,7 @@
import os
import time
+import math
+import pprint
from functools import reduce
from typing import List
@@ -26,8 +28,7 @@ class Playground:
port_check_interval = 1 # 1s
max_port_checks = 10
- rootdir = "/"
- env_vars = {}
+ run_command_timeout_frequency = 2 # in Hz
mock_data_filename = "index.ts"
@@ -64,24 +65,47 @@ class Playground:
def run_command(
self,
cmd: str,
- rootdir = rootdir,
- env_vars = env_vars,
+ env_vars={},
+ rootdir="/",
+ timeout: float | None = None,
):
- return self.api.start_process(
+ response = self.api.start_process(
self.session.id,
playground_client.StartProcessParams(
cmd=cmd,
envVars=env_vars,
rootdir=rootdir,
),
- wait=True,
+ wait=True if timeout is None else False,
)
+ 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(
+ self.session.id,
+ process_id=response.process_id,
+ )
+ if response.finished:
+ return response
+ time.sleep(1 / self.run_command_timeout_frequency)
+
+ if not response.finished:
+ self.api.stop_process(
+ self.session.id,
+ process_id=response.process_id,
+ results=False,
+ )
+
+ return response
+
def start_process(
self,
cmd: str,
- rootdir = rootdir,
- env_vars = env_vars,
+ rootdir="/",
+ env_vars={},
):
"""Start process and return the process ID."""
return self.api.start_process(
@@ -135,8 +159,8 @@ class Playground:
server_cmd: str,
request_cmd: str,
port: float,
- rootdir = rootdir,
- env_vars = env_vars,
+ rootdir="/",
+ env_vars={},
):
server_process_id = self.start_process(
cmd=server_cmd,
@@ -161,33 +185,30 @@ class Playground:
class NodeJSPlayground(Playground):
node_js_env_id = "dCeMnVVxu01L"
rootdir = "/code"
-
- run_code_timeout = 5 # 5s
-
default_javascript_code_file = os.path.join(rootdir, "index.js")
+ run_code_timeout = 5 # 5s
def __init__(self, envs: List[EnvVar]):
super().__init__(NodeJSPlayground.node_js_env_id)
self.env_vars = self.format_env_vars(envs)
- def run_javascript_code(self, code: str, timeout: float = run_code_timeout):
+ def run_javascript_code(self, code: str):
print(f"Running javascript code: {code}")
self.write_file(self.default_javascript_code_file, code)
- process_id = self.start_process(
+ result = self.run_command(
f"node {self.default_javascript_code_file}",
rootdir=self.rootdir,
env_vars=self.env_vars,
+ timeout=self.run_code_timeout,
)
-
- # For now we always wait the full timeout interval
- time.sleep(timeout)
-
- result = self.stop_process(process_id)
- print(result)
+ pprint.pprint(f"Result: {result}")
return result
def install_dependencies(self, dependencies: str):
- return self.run_command(f"npm install {dependencies}", rootdir=self.rootdir)
+ print(f"Installing dependencies: {dependencies}")
+ result = self.run_command(f"npm install {dependencies}", rootdir=self.rootdir)
+ pprint.pprint(f"Result: {result}")
+ return result
def run_javascript_server_code_with_request(
self,
@@ -195,7 +216,7 @@ class NodeJSPlayground(Playground):
request_cmd: str,
port: float,
):
- print("CALLING", code)
+ print(f"Running curl {request_cmd} for code: {code}")
self.write_file(self.default_javascript_code_file, code)
result = self.run_server_with_request(
server_cmd=f"node {self.default_javascript_code_file}",
@@ -204,5 +225,5 @@ class NodeJSPlayground(Playground):
rootdir=self.rootdir,
env_vars=self.env_vars,
)
- print(result)
+ pprint.pprint(f"Result: {result}")
return result
diff --git a/api-service/codegen/tools/playground/tools/code.py b/api-service/codegen/tools/playground/tools/code.py
index 0bbb6e2c1..df55626e0 100644
--- a/api-service/codegen/tools/playground/tools/code.py
+++ b/api-service/codegen/tools/playground/tools/code.py
@@ -26,7 +26,6 @@ def create_code_tools(playground: NodeJSPlayground, mock: MockRequestFactory):
yield install_dependencies
last_javascript_code: str | None
- # TODO: Add timeout that stops the code execution if it doesn't stop after some time
# This tool is just for executing JavaScript without doing the request to server
@tool("RunJavaScriptCode")
def run_javascript_code(code: str) -> str:
diff --git a/api-service/codegen/tools/playground/tools/filesystem.py b/api-service/codegen/tools/playground/tools/filesystem.py
index 3842f9cd8..60e201eec 100644
--- a/api-service/codegen/tools/playground/tools/filesystem.py
+++ b/api-service/codegen/tools/playground/tools/filesystem.py
@@ -7,7 +7,10 @@ from playground_client.models.entry_info import EntryInfo
def encode_directory_listing(entries: List[EntryInfo]) -> str:
return "\n".join(
- [f"{entry.name} {'directory' if entry.is_dir else 'file'}" for entry in entries]
+ [
+ f"{entry.name} - {'directory' if entry.is_dir else 'file'}"
+ for entry in entries
+ ]
)
@@ -22,7 +25,6 @@ def create_filesystem_tools(playground: NodeJSPlayground):
yield read_file
- # TODO: Escape file content?
@tool("SaveFile")
def save_file(input: str) -> str:
"""Save content to a file in the filesystem.