Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
Pass env vars to playground; Format
Tomas Valenta597fac55d56c
diff --git a/api-service/app.py b/api-service/app.pyindex 84914c2f4..599414c30 100644--- a/api-service/app.py+++ b/api-service/app.py@@ -2,10 +2,11 @@ import osimport uuidimport subprocess-from typing import Dict-from flask import Flask, request+from typing import Dict, List+from flask import Flask, abort, requestfrom flask_cors import CORS+from codegen.codegen import EnvVarfrom codegen import generate_req_handlerfrom database import Database, DeploymentState@@ -42,13 +43,16 @@ def health():def generate():body = request.json+ if body is None:+ abort(400)+run_id = str(uuid.uuid4())project_id = body["projectID"]route_id = body["routeID"]blocks = body["blocks"]method = body["method"]route = body["route"]- envs = body["envs"]+ envs: List[EnvVar] = body["envs"]db.create_deployment(run_id=run_id, project_id=project_id, route_id=route_id)diff --git a/api-service/codegen/codegen.py b/api-service/codegen/codegen.pyindex 51118ca61..a7ce631cd 100644--- a/api-service/codegen/codegen.py+++ b/api-service/codegen/codegen.py@@ -1,5 +1,6 @@from typing import List, Dictfrom langchain.llms.openai import OpenAIChat, OpenAI+from typing import TypedDict# from codegen.tools.documentation import ReadDocumentationfrom codegen.js_agent import create_js_agent@@ -9,14 +10,19 @@ from codegen.tools.javascript import JavascriptEvalToolfrom database import Database+class EnvVar(TypedDict):+ key: str+ value: str++def generate_req_handler(db: Database,run_id: str,blocks: List[Dict],method: str,- envs: List[Dict[str, str]],+ envs: List[EnvVar],):- playground_tools, playground = create_playground_tools()+ playground_tools, playground = create_playground_tools(envs)executor = create_js_agent(db=db,@@ -56,8 +62,8 @@ def generate_req_handler(handler_code = executor.run(prompt).strip("`").strip()- content = playground.read_file('/code/package.json')- print('PACKAGE.JSON')+ content = playground.read_file("/code/package.json")+ print("PACKAGE.JSON")print(content)playground.close()diff --git a/api-service/codegen/tools/playground/main.py b/api-service/codegen/tools/playground/main.pyindex 347301ef1..5412992a2 100644--- a/api-service/codegen/tools/playground/main.py+++ b/api-service/codegen/tools/playground/main.py@@ -1,5 +1,6 @@from typing import List, Any+from codegen.codegen import EnvVarfrom .playground import NodeJSPlayground, Playgroundfrom .tools.filesystem import create_filesystem_toolsfrom .tools.process import create_process_tools@@ -8,8 +9,8 @@ from .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() -> tuple[List[Any], Playground]:- playground = NodeJSPlayground()+def create_playground_tools(envs: List[EnvVar]) -> tuple[List[Any], Playground]:+ playground = NodeJSPlayground(envs)subtools = [toolfor tools in (diff --git a/api-service/codegen/tools/playground/playground.py b/api-service/codegen/tools/playground/playground.pyindex 8fcc7437f..cd42de6c0 100644--- a/api-service/codegen/tools/playground/playground.py+++ b/api-service/codegen/tools/playground/playground.py@@ -1,6 +1,11 @@import osimport time+from functools import reduce++from typing import Dict, List+from codegen.codegen import EnvVar+import playground_client@@ -15,7 +20,7 @@ configuration.cert_file = Noneclass Playground:- port_check_interval = 1 # 1s+ port_check_interval = 1 # 1smax_port_checks = 10def __init__(self, env_id: str):@@ -43,20 +48,29 @@ class Playground: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)+ 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 = {}):+ def run_command(self, cmd: str, rootdir: str = "/", env_vars: dict[str, str] = {}):return self.api.start_process(self.session.id,- playground_client.StartProcessParams(cmd=cmd, envVars=env_vars, rootdir=rootdir),- wait=True+ playground_client.StartProcessParams(+ cmd=cmd, envVars=env_vars, rootdir=rootdir+ ),+ wait=True,)- def start_process(self, cmd: str, rootdir: str = "/", env_vars = {}):+ def start_process(+ self, cmd: str, rootdir: str = "/", env_vars: dict[str, str] = {}+ ):"""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),+ playground_client.StartProcessParams(+ cmd=cmd, envVars=env_vars, rootdir=rootdir+ ),)return response.process_id@@ -81,14 +95,25 @@ class Playground:def write_file(self, path: str, content: str):self.api.write_filesystem_file(- self.session.id, path, playground_client.WriteFilesystemFileRequest(content=content)+ self.session.id,+ path,+ playground_client.WriteFilesystemFileRequest(content=content),)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)+ def test_server_code(+ self,+ server_cmd: str,+ test_cmd: str,+ port: float,+ rootdir: str,+ env_vars: dict[str, str] = {},+ ):+ server_process_id = self.start_process(+ cmd=server_cmd, rootdir=rootdir, env_vars=env_vars+ )for _ in range(self.max_port_checks):if self.is_port_open(port):@@ -99,6 +124,10 @@ class Playground:server_result = self.stop_process(server_process_id)return test_result, server_result+ @staticmethod+ def format_env_vars(envs: List[EnvVar]):+ return reduce(lambda acc, env: {**acc, **{env["key"]: env["value"]}}, envs, {})+class NodeJSPlayground(Playground):node_js_env_id = "QfqlXmCo5iKl"@@ -107,16 +136,25 @@ class NodeJSPlayground(Playground):default_javascript_code_file = os.path.join(rootdir, "index.js")default_typescript_code_file = os.path.join(rootdir, "index.ts")- def __init__(self):+ 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):self.write_file(self.default_javascript_code_file, code)- return self.run_command(f"node {self.default_javascript_code_file}", rootdir=self.rootdir)+ return self.run_command(+ f"node {self.default_javascript_code_file}",+ rootdir=self.rootdir,+ env_vars=self.env_vars,+ )def run_typescript_code(self, code: str):self.write_file(self.default_typescript_code_file, code)- return self.run_command(f"ts-node -T {self.default_typescript_code_file}", rootdir=self.rootdir)+ return self.run_command(+ f"ts-node -T {self.default_typescript_code_file}",+ rootdir=self.rootdir,+ env_vars=self.env_vars,+ )def check_typescript_code(self, code: str):self.write_file(self.default_typescript_code_file, code)@@ -128,7 +166,12 @@ class NodeJSPlayground(Playground):def install_dependencies(self, dependencies: str):return self.run_command(f"npm install {dependencies}", rootdir=self.rootdir)-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)+ return self.test_server_code(+ server_cmd=f"node {self.default_javascript_code_file}",+ test_cmd=test_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.pyindex 0addeed8c..02387f5b1 100644--- a/api-service/codegen/tools/playground/tools/code.py+++ b/api-service/codegen/tools/playground/tools/code.py@@ -7,6 +7,7 @@ from codegen.tools.playground.tools.process import encode_command_outputdef extract_code(code: str):return code.strip().strip("`").strip()+def create_code_tools(playground: NodeJSPlayground):# Ensure that the function is a generator even if no tools are yieldedyield from ()@@ -75,14 +76,15 @@ def create_code_tools(playground: NodeJSPlayground):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_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")diff --git a/api-service/codegen/tools/playground/tools/filesystem.py b/api-service/codegen/tools/playground/tools/filesystem.pyindex 2c415f7e7..3842f9cd8 100644--- a/api-service/codegen/tools/playground/tools/filesystem.py+++ b/api-service/codegen/tools/playground/tools/filesystem.py@@ -6,7 +6,9 @@ from playground_client.models.entry_info import EntryInfodef encode_directory_listing(entries: List[EntryInfo]) -> str:- return "\n".join([f"{entry.name} {'directory' if entry.is_dir else 'file'}" for entry in entries])+ return "\n".join(+ [f"{entry.name} {'directory' if entry.is_dir else 'file'}" for entry in entries]+ )def create_filesystem_tools(playground: NodeJSPlayground):