Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
Test JS code with a mocked request [WIP]
Tomas Valenta48d14529ef32
diff --git a/api-service/app.py b/api-service/app.pyindex 4241439b1..fdb08033f 100644--- a/api-service/app.py+++ b/api-service/app.py@@ -61,6 +61,7 @@ def generate():run_id=run_id,blocks=blocks,method=method,+ route=route,envs=envs,)diff --git a/api-service/codegen/codegen.py b/api-service/codegen/codegen.pyindex 717e109c3..dca0d1bba 100644--- a/api-service/codegen/codegen.py+++ b/api-service/codegen/codegen.py@@ -15,9 +15,19 @@ def generate_req_handler(run_id: str,blocks: List[Dict],method: str,+ route: str,envs: List[EnvVar],):- playground_tools, playground = create_playground_tools(envs)+ request_body_template = next(+ block for block in blocks if block["type"] == "RequestBody"+ )["prompt"]++ playground_tools, playground = create_playground_tools(+ envs=envs,+ route=route,+ method=method,+ request_body_template=request_body_template,+ )executor = create_js_agent(db=db,@@ -33,21 +43,14 @@ def generate_req_handler(verbose=True,)- # Convert description of the body block to a Typescript interface declaration.- body_block = next(block for block in blocks if block["type"] == "RequestBody")- req_body_str = f"""interface RequestBody {{- {body_block["prompt"]}- }}- """- print("REQ BODY BLOCK")- print(req_body_str)-# Convert env vars to a markdown list enumerating env names on separate lines.envs_str = ""for env in envs:envs_str += f'- `{env["key"]}`\n'- prompt = PREFIX.format(method=method, envs=envs_str, request_body=req_body_str)+ prompt = PREFIX.format(+ method=method, envs=envs_str, request_body=request_body_template+ )for idx, block in enumerate(blocks):if block["type"] == "Basic":diff --git a/api-service/codegen/tools/playground/main.py b/api-service/codegen/tools/playground/main.pyindex a55052d07..3d6a4f568 100644--- a/api-service/codegen/tools/playground/main.py+++ b/api-service/codegen/tools/playground/main.py@@ -2,17 +2,23 @@ from typing import List, Anyfrom codegen.env import EnvVarfrom codegen.tools.playground.mock.request import MockRequestFactory+from .playground import NodeJSPlayground, Playgroundfrom .tools.filesystem import create_filesystem_toolsfrom .tools.process import create_process_toolsfrom .tools.code import create_code_tools-# TODO: Specify that the environmetn is persistent between tools' invocations?-# TODO: Use ubuntu instead of alpine?-def create_playground_tools(envs: List[EnvVar]) -> tuple[List[Any], Playground]:+def create_playground_tools(+ envs: List[EnvVar],+ method: str,+ route: str,+ request_body_template: str,+) -> tuple[List[Any], Playground]:playground = NodeJSPlayground(envs)- mock = MockRequestFactory()+ mock = MockRequestFactory(+ method=method, route=route, body_template=request_body_template+ )subtools = [tooldiff --git a/api-service/codegen/tools/playground/mock/request.py b/api-service/codegen/tools/playground/mock/request.pyindex 54a102301..1d5e177e0 100644--- a/api-service/codegen/tools/playground/mock/request.py+++ b/api-service/codegen/tools/playground/mock/request.py@@ -1,2 +1,35 @@+from enum import Enum+import json++import typing as t++JSON = t.Union[str, int, float, bool, None, t.Mapping[str, "JSON"], t.List["JSON"]]++class MockRequestFactory:- pass+ def __init__(+ self,+ method: str,+ route: str,+ body_template: str,+ hostname: str = "http://localhost:3000",+ ) -> None:+ self.hostname = hostname+ self.method = method.upper()+ self.route = route+ self.body_template = body_template++ def terminal_command(self) -> str:+ body = self.generate_body_data()+ return f"curl -X {self.method} {self.hostname}/{self.route} -H 'Content-Type: application/json' -d {json.dumps(body)}"++ def generate_body_data(self) -> JSON:+ self.body_template+ # request_body_template = f"""interface RequestBody {{+ # {body_template}+ # }}+ # """+ # https://github.com/joke2k/faker+ # https://github.com/faker-js/faker+ # https://github.com/google/intermock+ passdiff --git a/api-service/codegen/tools/playground/tools/code.py b/api-service/codegen/tools/playground/tools/code.pyindex 50dd9c107..c7dbfb4dc 100644--- a/api-service/codegen/tools/playground/tools/code.py+++ b/api-service/codegen/tools/playground/tools/code.py@@ -52,37 +52,37 @@ def create_code_tools(playground: NodeJSPlayground, mock: MockRequestFactory):# yield check_typescript_code_types# This tool is just for executing JavaScript without doing the request to server- @tool("RunJavaScriptCode")- def run_javascript_code(code: str) -> str:- """- Run JavaScript code and return errors and output.- Input should be a valid JavaScript code.- """- output = playground.run_javascript_code(extract_code(code))- result = encode_command_output(output)- return result if len(result) > 0 else "Code execution finished without error"-- yield run_javascript_code-# @tool("RunJavaScriptCode")# def run_javascript_code(code: str) -> str:# """- # Execute JavaScript code that should start a server then tests if server correctly handles needed requests.- # The input should be a valid JavaScriptCode.- # The server should run on http://localhost:3000.- # The returned result is a the testing request output followed by the server code output and errors.+ # Run JavaScript code and return errors and output.+ # Input should be a valid JavaScript code.# """+ # output = playground.run_javascript_code(extract_code(code))+ # result = encode_command_output(output)+ # return result if len(result) > 0 else "Code execution finished without error"- # mock_request = mock.requestComand()+ # yield run_javascript_code- # port = 3000- # test_output, server_output = playground.test_javascript_server_code(- # code=code, test_cmd=mock_request.command, port=port- # )+ @tool("RunJavaScriptCode")+ def run_javascript_code(code: str) -> str:+ """+ Execute JavaScript code that should start a server then tests if server correctly handles needed requests.+ The input should be a valid JavaScriptCode.+ The server should run on http://localhost:3000.+ The returned result is a the testing request output followed by the server code output and errors.+ """- # test_result = encode_command_output(test_output)- # server_result = encode_command_output(server_output)+ mock_request_command = mock.terminal_command()- # return f"Test request result:\n{test_result}\nCode execution result:\n{server_result}"+ port = 3000+ test_output, server_output = playground.test_javascript_server_code(+ code=code, test_cmd=mock_request_command, port=port+ )- # yield run_javascript_code+ test_result = encode_command_output(test_output)+ server_result = encode_command_output(server_output)++ return f"Test request result:\n{test_result}\nCode execution result:\n{server_result}"++ yield run_javascript_code