Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
Add tool for testing server [WIP]
Tomas Valenta2e9103c9de43
diff --git a/api-service/codegen/tools/playground/playground.py b/api-service/codegen/tools/playground/playground.pyindex a03127f3a..8fcc7437f 100644--- a/api-service/codegen/tools/playground/playground.py+++ b/api-service/codegen/tools/playground/playground.py@@ -1,4 +1,5 @@import os+import timeimport playground_client@@ -14,6 +15,9 @@ configuration.cert_file = Noneclass Playground:+ port_check_interval = 1 # 1s+ max_port_checks = 10+def __init__(self, env_id: str):self.client = playground_client.ApiClient(configuration)self.api = playground_client.DefaultApi(self.client)@@ -31,9 +35,36 @@ class Playground:self.api.delete_session(self.session.id)self.client.close()+ def get_open_ports(self):+ response = self.api.get_session(+ id=self.session.id,+ )+ return response.ports++ def is_port_open(self, port: float):+ open_ports = self.get_open_ports()+ return any(open_port.port == port and open_port.state == "LISTEN" for open_port in open_ports)+def run_command(self, cmd: str, rootdir: str = "/", env_vars = {}):- return self.api.run_process(- self.session.id, playground_client.RunProcessParams(cmd=cmd, envVars=env_vars, rootdir=rootdir)+ return self.api.start_process(+ self.session.id,+ playground_client.StartProcessParams(cmd=cmd, envVars=env_vars, rootdir=rootdir),+ wait=True+ )++ def start_process(self, cmd: str, rootdir: str = "/", env_vars = {}):+ """Start process and return the process ID."""+ response = self.api.start_process(+ self.session.id,+ playground_client.StartProcessParams(cmd=cmd, envVars=env_vars, rootdir=rootdir),+ )+ return response.process_id++ def stop_process(self, process_id: str):+ return self.api.stop_process(+ self.session.id,+ process_id=process_id,+ results=True,)def read_file(self, path: str):@@ -56,6 +87,18 @@ class Playground:def make_dir(self, path: str):self.api.make_filesystem_dir(self.session.id, path)+ def test_server_code(self, server_cmd: str, test_cmd: str, port: float, rootdir: str):+ server_process_id = self.start_process(cmd=server_cmd, rootdir=rootdir)++ for _ in range(self.max_port_checks):+ if self.is_port_open(port):+ break+ time.sleep(self.port_check_interval)++ test_result = self.run_command(cmd=test_cmd, rootdir=rootdir)+ server_result = self.stop_process(server_process_id)+ return test_result, server_result+class NodeJSPlayground(Playground):node_js_env_id = "QfqlXmCo5iKl"@@ -85,6 +128,7 @@ class NodeJSPlayground(Playground):def install_dependencies(self, dependencies: str):return self.run_command(f"npm install {dependencies}", rootdir=self.rootdir)- # TODO: How to handle endless run_commands?- # If we actually run express server, the process will never end.- # Can we somehow start server and make a request so we check if it works? This is tests, right?++ def test_javascript_server_code(self, code: str, test_cmd: str, port: float):+ self.write_file(self.default_javascript_code_file, code)+ return self.test_server_code(server_cmd=f"node {self.default_javascript_code_file}", test_cmd=test_cmd, port=port, rootdir=self.rootdir)diff --git a/api-service/codegen/tools/playground/tools/code.py b/api-service/codegen/tools/playground/tools/code.pyindex 8ed37b903..0addeed8c 100644--- a/api-service/codegen/tools/playground/tools/code.py+++ b/api-service/codegen/tools/playground/tools/code.py@@ -61,6 +61,28 @@ def create_code_tools(playground: NodeJSPlayground):yield run_javascript_code+ @tool("TestJavaScriptServerCode")+ def test_server_code(input: str) -> str:+ """+ Execute JavaScript code that should start a server and then executes a testing command that should test if the server correctly handles needed requests.+ The server should run on http://localhost:3000 and the request should be made on http://localhost:3000 too.+ The testing command should be a `curl` command.+ The input to this tool should be the testing command followed by a newline then the server code wrapped in three backtics.+ The returned result is a the testing command output followed by the server code output and errors.+ """++ test_cmd, server_code = input.split("\n", 1)+ code = extract_code(server_code)++ port = 3000+ test_output, server_output = playground.test_javascript_server_code(code=code, test_cmd=test_cmd, port=port)++ test_result = encode_command_output(test_output)+ server_result = encode_command_output(server_output)++ return f"Test command result:\n{test_result}\n\nServer result:\n{server_result}"+++ # yield test_server_code+# @tool("JavaScriptREPL/Shell")- # @tool("ServerRequestTest")- # @tool("ServerRequestTest")diff --git a/api-service/codegen/tools/playground/tools/process.py b/api-service/codegen/tools/playground/tools/process.pyindex a7e1824cd..1f96a4eef 100644--- a/api-service/codegen/tools/playground/tools/process.py+++ b/api-service/codegen/tools/playground/tools/process.py@@ -1,10 +1,10 @@from langchain.agents import toolfrom codegen.tools.playground.playground import NodeJSPlayground-from playground_client.models.run_process_response import RunProcessResponse+from playground_client.models.process_response import ProcessResponse-def encode_command_output(response: RunProcessResponse, only_errors: bool = False) -> str:+def encode_command_output(response: ProcessResponse, only_errors: bool = False) -> str:result = ""err_lines = [err.line for err in response.stderr]