Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
Add new env; Fix playground errors; Improve playground tools descriptions
Tomas Valenta179be00f1819
diff --git a/api-service/app.py b/api-service/app.pyindex a4a0b00f3..84914c2f4 100644--- a/api-service/app.py+++ b/api-service/app.py@@ -8,7 +8,6 @@ from flask_cors import CORSfrom codegen import generate_req_handlerfrom database import Database, DeploymentState-from codegen.tools.playground.playground import NodeJSPlaygroundurl = os.environ.get("SUPABASE_URL")key = os.environ.get("SUPABASE_KEY")diff --git a/api-service/codegen/codegen.py b/api-service/codegen/codegen.pyindex 1c05222dc..035947f5d 100644--- a/api-service/codegen/codegen.py+++ b/api-service/codegen/codegen.py@@ -1,7 +1,7 @@from typing import List, Dictfrom langchain.llms.openai import OpenAIChat, OpenAI-from codegen.tools.documentation import ReadDocumentation+# from codegen.tools.documentation import ReadDocumentationfrom codegen.js_agent import create_js_agentfrom codegen.prompt import PREFIX, SUFFIXfrom codegen.tools.playground import create_playground_tools@@ -15,8 +15,8 @@ def generate_req_handler(blocks: List[str],method: str,envs: List[Dict[str, str]],-) -> str:- # playground, tools = create_playground_tools()+):+ playground_tools, playground = create_playground_tools()executor = create_js_agent(db=db,@@ -26,8 +26,8 @@ def generate_req_handler(# llm=OpenAIChat(temperature=0, max_tokens=1000),tools=[# ReadDocumentation()- JavascriptEvalTool(),- # *playground_tools,+ # JavascriptEvalTool(),+ *playground_tools,],verbose=True,)@@ -44,7 +44,7 @@ def generate_req_handler(handler_code = executor.run(prompt).strip("`").strip()- # playground.close()+ playground.close()return prompt, handler_code# server_code = server_template.replace('[HANDLER]', handler_code)diff --git a/api-service/codegen/tools/playground/main.py b/api-service/codegen/tools/playground/main.pyindex c2af255b5..9c27ea391 100644--- a/api-service/codegen/tools/playground/main.py+++ b/api-service/codegen/tools/playground/main.py@@ -1,14 +1,13 @@-from typing import List-from langchain.tools.base import BaseTool+from typing import List, Anyfrom .playground import NodeJSPlayground, Playgroundfrom .tools.filesystem import create_filesystem_toolsfrom .tools.process import create_process_toolsfrom .tools.code import create_code_tools--# TODO: Improve all descriptions of tools-def create_playground_tools() -> tuple[List[BaseTool], Playground]:+# 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()subtools = [tool@@ -22,4 +21,4 @@ def create_playground_tools() -> tuple[List[BaseTool], Playground]:)for tool in tools]- return playground, subtools+ return subtools, playgrounddiff --git a/api-service/codegen/tools/playground/playground.py b/api-service/codegen/tools/playground/playground.pyindex 2a2e6be95..5870b719e 100644--- a/api-service/codegen/tools/playground/playground.py+++ b/api-service/codegen/tools/playground/playground.py@@ -1,6 +1,3 @@-from enum import Enum-from typing import TypedDict, Union-import playground_client@@ -14,27 +11,12 @@ configuration.ssl_ca_cert = Noneconfiguration.cert_file = None-class FilesystemEntryType(Enum):- FILE = "file"- DIR = "directory"---class FilesystemEntry(TypedDict):- name: str- type: FilesystemEntryType---class CommandOutput(TypedDict):- error: Union[str, None]- output: str--class Playground:def __init__(self, env_id: str):self.client = playground_client.ApiClient(configuration)self.api = playground_client.DefaultApi(self.client)self.session = self.api.create_sessions(- playground_client.CreateSessionsRequest(env_id)+ playground_client.CreateSessionsRequest(envID=env_id))self.is_closed = False@@ -47,33 +29,16 @@ class Playground:self.api.delete_session(self.session.id)self.client.close()- def run_command(self, cmd: str):- result = self.api.run_process(- self.session.id, playground_client.RunProcessParams(cmd)- )- err_lines = [err.line for err in result.stderr]- out_lines = [out.line for out in result.stdout]-- return CommandOutput(- error=err_lines.join("\n") if len(err_lines) > 0 else None,- output="\n".join(out_lines),+ def run_command(self, cmd: str, env_vars = {}):+ return self.api.run_process(+ self.session.id, playground_client.RunProcessParams(cmd=cmd, envVars=env_vars))def read_file(self, path: str):- result = self.api.read_filesystem_file(self.session.id, path)- return result.content+ return self.api.read_filesystem_file(self.session.id, path).contentdef list_dir(self, path: str):- result = self.api.list_filesystem_dir(self.session.id, path)- return [- FilesystemEntry(- name=entry.name,- type=FilesystemEntryType.DIR- if entry.is_dir- else FilesystemEntryType.FILE,- )- for entry in result.entries- ]+ return self.api.list_filesystem_dir(self.session.id, path).entriesdef delete_file(self, path: str):self.api.delete_filesystem_entry(self.session.id, path)@@ -83,7 +48,7 @@ class Playground:def write_file(self, path: str, content: str):self.api.write_filesystem_file(- self.session.id, path, playground_client.WriteFilesystemFileRequest(content)+ self.session.id, path, playground_client.WriteFilesystemFileRequest(content=content))def make_dir(self, path: str):@@ -91,8 +56,7 @@ class Playground:class NodeJSPlayground(Playground):- # TODO: Check if node 18 is used- node_js_env_id = "rVGhg9SdIzZ2"+ node_js_env_id = "KctyCCI7Ijf9"default_javascript_code_file = "index.js"default_typescript_code_file = "index.ts"@@ -104,12 +68,10 @@ class NodeJSPlayground(Playground):self.write_file(self.default_javascript_code_file, code)return self.run_command(f"node {self.default_javascript_code_file}")- # TODO: Install `ts-node` in the envdef run_typescript_code(self, code: str):self.write_file(self.default_typescript_code_file, code)return self.run_command(f"ts-node {self.default_typescript_code_file}")- # TODO: Install `tsc` in the envdef check_typescript_code(self, code: str):self.write_file(self.default_typescript_code_file, code)return self.run_command(diff --git a/api-service/codegen/tools/playground/tools/code.py b/api-service/codegen/tools/playground/tools/code.pyindex fe9b05b50..2ec682e29 100644--- a/api-service/codegen/tools/playground/tools/code.py+++ b/api-service/codegen/tools/playground/tools/code.py@@ -5,9 +5,14 @@ from codegen.tools.playground.tools.process import encode_command_outputdef create_code_tools(playground: NodeJSPlayground):- @tool("InstallDependencies")+ # TODO: Escape command+ @tool("InstallNPMDependencies")def install_dependencies(dependencies: str) -> str:- """Install specified dependecies with NPM and return errors."""+ """+ Install specified dependecies with NPM and return errors.+ The input should be a list of Node.js dependencies separated by spaces.+ If there are no errors the returned result will be an empty string.+ """output = playground.install_dependencies(dependencies)return encode_command_output(output, only_errors=True)@@ -16,26 +21,45 @@ def create_code_tools(playground: NodeJSPlayground):# TODO: Escape code@tool("RunTypeScriptCode")def run_typescript_code(code: str) -> str:- """Run the specified TypeScript code and return errors and output."""+ """+ Run the specified TypeScript code and return errors and output.+ The input should be a valid TypeScript code.+ This code will be saved to `index.ts` and executed by `ts-node` CLI tool.+ The returned result will be the output and errors returned when executing the code.+ """output = playground.run_typescript_code(code)return encode_command_output(output)yield run_typescript_code# TODO: Escape code- @tool("CheckTypeScriptTypes")- def check_typescript_types(code: str) -> str:- """Check TypeScript types in the specified code and return errors."""+ @tool("CheckTypeScriptCodeTypes")+ def check_typescript_code_types(code: str) -> str:+ """+ Check TypeScript types in the specified code and return errors.+ The input should be a valid TypeScript code.+ This code will be saved to `index.ts` the `tsc --noEmit` command will be used to check for type errors.+ If there are no errors the returned result will be an empty string.+ """output = playground.check_typescript_code(code)- return encode_command_output(output)+ return encode_command_output(output, only_errors=True)- yield check_typescript_types+ yield check_typescript_code_types# TODO: Escape code@tool("RunJavaScriptCode")def run_javascript_code(code: str) -> str:- """Run the specified JavaScript code and return errors and output."""+ """+ Run the specified JavaScript code and return errors and output.+ The input should be a valid TypeScript code.+ This code will be saved to `index.js` and executed by `node` command.+ The returned result will be the output and errors returned when executing the code.+ """output = playground.run_javascript_code(code)return encode_command_output(output)yield run_javascript_code++ # @tool("JavaScriptREPL/Shell")+ # @tool("ServerRequestTest")+ # @tool("ServerRequestTest")diff --git a/api-service/codegen/tools/playground/tools/filesystem.py b/api-service/codegen/tools/playground/tools/filesystem.pyindex 479f58b8c..bf030b0c5 100644--- a/api-service/codegen/tools/playground/tools/filesystem.py+++ b/api-service/codegen/tools/playground/tools/filesystem.py@@ -1,11 +1,12 @@-from langchain.agents import toolfrom typing import List+from langchain.agents import tool-from codegen.tools.playground.playground import NodeJSPlayground, FilesystemEntry+from codegen.tools.playground.playground import NodeJSPlayground+from playground_client.models.entry_info import EntryInfo-def encode_directory_listing(entries: List[FilesystemEntry]) -> str:- return "\n".join([f"{entry.name} {entry.type}" for entry in entries])+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])def create_filesystem_tools(playground: NodeJSPlayground):diff --git a/api-service/codegen/tools/playground/tools/process.py b/api-service/codegen/tools/playground/tools/process.pyindex 79cd98e7f..1b0ed243a 100644--- a/api-service/codegen/tools/playground/tools/process.py+++ b/api-service/codegen/tools/playground/tools/process.py@@ -1,23 +1,42 @@from langchain.agents import tool-from codegen.tools.playground.playground import NodeJSPlayground, CommandOutput+from codegen.tools.playground.playground import NodeJSPlayground+from playground_client.models.run_process_response import RunProcessResponse-def encode_command_output(output: CommandOutput, only_errors: bool = False) -> str:+def encode_command_output(response: RunProcessResponse, only_errors: bool = False) -> str:+ result = ""++ err_lines = [err.line for err in response.stderr]+ errors = "\n".join(err_lines)++ if len(errors) > 0:+ result += f"Errors: {errors}"+if only_errors:- if output.error is not None:- return f"Errors: {output.error}"- return ""- errors = f"Errors: {output.error}\n" if output.error is not None else ""- return errors + f"Output: {output.output}"+ return result++ out_lines = [out.line for out in response.stdout]+ output = "\n".join(out_lines)++ if len(output) > 0:+ result += f"Output: {output}"++ return resultdef create_process_tools(playground: NodeJSPlayground):# TODO: Escape command- @tool("RunCommand")- def run_command(command: str) -> str:- """Run specified command in the terminal and return output and errors."""+ @tool("RunTerminalCommand")+ def run_terminal_command(command: str) -> str:+ """+ Run specified command in the terminal and return output and errors.+ The input should be a a valid terminal command.+ The environment where the command runs is Alpine Linux.+ It has access to the internet and to basic utility tools like `curl`.+ The returned result will be the output and errors returned when running the command.+ """output = playground.run_command(command)return encode_command_output(output)- yield run_command+ yield run_terminal_commanddiff --git a/dbk.toml b/dbk.tomlnew file mode 100644index 000000000..21c231442--- /dev/null+++ b/dbk.toml@@ -0,0 +1,8 @@+# This is a config for a Devbook environment++title = "ai-api"+template = "Nodejs"+id = "KctyCCI7Ijf9"++[filesystem]+local_root = "./files"\ No newline at end of file