Skip to content

armgit / E2B

Public Imported from GitHub
0

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

Prepare ts code execution

Tomas Valentad9cb1c027ff1
diff --git a/api-service/codegen/tools/playground/playground.py b/api-service/codegen/tools/playground/playground.py
index 4626b0ba3..3f0577a01 100644
--- a/api-service/codegen/tools/playground/playground.py
+++ b/api-service/codegen/tools/playground/playground.py
@@ -169,10 +169,10 @@ class NodeJSPlayground(Playground):
env_vars=self.env_vars,
)
- def run_typescript_code(self, code: str):
+ def run_typescript_code(self, code: str, typecheck: bool = False):
self.write_file(self.default_typescript_code_file, code)
return self.run_command(
- f"ts-node -T {self.default_typescript_code_file}",
+ f"ts-node {'-T' if typecheck else ''} {self.default_typescript_code_file}",
rootdir=self.rootdir,
env_vars=self.env_vars,
)
@@ -201,3 +201,18 @@ class NodeJSPlayground(Playground):
rootdir=self.rootdir,
env_vars=self.env_vars,
)
+
+ def run_typescript_server_code_with_request(
+ self,
+ code: str,
+ request_cmd: str,
+ port: float,
+ ):
+ self.write_file(self.default_javascript_code_file, code)
+ return self.run_server_with_request(
+ server_cmd=f"ts-node {self.default_javascript_code_file}",
+ request_cmd=request_cmd,
+ port=port,
+ rootdir=self.rootdir,
+ env_vars=self.env_vars,
+ )
diff --git a/api-service/codegen/tools/playground/tools/code.py b/api-service/codegen/tools/playground/tools/code.py
index 00261c23f..f56edd5b6 100644
--- a/api-service/codegen/tools/playground/tools/code.py
+++ b/api-service/codegen/tools/playground/tools/code.py
@@ -25,17 +25,41 @@ def create_code_tools(playground: NodeJSPlayground, mock: MockRequestFactory):
yield install_dependencies
+ # This tool is just for executing TypeScript without doing the request to server
+ # @tool("RunTypeScriptCode")
+ # def run_typescript_code(code: str) -> str:
+ # """
+ # Run the specified TypeScript code and return errors and output.
+ # The input should be a valid TypeScript code.
+ # The returned result will be the output and errors returned when executing the code.
+ # """
+ # output = playground.run_typescript_code(extract_code(code))
+ # result = encode_command_output(output)
+
+ # return result if len(result) > 0 else "Code execution finished without error"
+
+ # # yield run_typescript_code
+
@tool("RunTypeScriptCode")
def run_typescript_code(code: str) -> str:
"""
- Run the specified TypeScript code and return errors and output.
+ Execute TypeScript code and make a request to see if server correctly handles needed requests.
The input should be a valid TypeScript code.
- The returned result will be the output and errors returned when executing the code.
+ The server should run on http://localhost:3000.
+ The returned result is a the request's output followed by the server code output and errors.
"""
- output = playground.run_typescript_code(extract_code(code))
- result = encode_command_output(output)
+ mock_request_cmd = mock.terminal_command()
+
+ port = 3000
+ request_output, server_output = playground.run_typescript_server_code_with_request(
+ code=code, request_cmd=mock_request_cmd, port=port,
+ )
+
+ request_result = encode_command_output(request_output)
+ server_result = encode_command_output(server_output)
+
+ return f"Request result:\n{request_result}\nCode execution result:\n{server_result}"
- return result if len(result) > 0 else "Code execution finished without error"
# yield run_typescript_code
@@ -67,8 +91,8 @@ def create_code_tools(playground: NodeJSPlayground, mock: MockRequestFactory):
@tool("RunJavaScriptCode")
def run_javascript_code(code: str) -> str:
"""
- Execute JavaScript code that should start a server then makes a request to see if server correctly handles needed requests.
- The input should be a valid JavaScriptCode.
+ Execute JavaScript code and make a request to see if server correctly handles needed requests.
+ The input should be a valid JavaScript code.
The server should run on http://localhost:3000.
The returned result is a the request's output followed by the server code output and errors.
"""