Skip to content

armgit / E2B

Public Imported from GitHub
0

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

Merge branch 'master' into tools

Tomas Valenta354eb78cbfc7
diff --cc api-service/app.py
index 99a30b34d,f9bdfd6c8..9582d3e21
--- a/api-service/app.py
+++ b/api-service/app.py
@@@ -50,31 -59,104 +50,42 @@@ def generate()
envs: List[EnvVar] = body["envs"]
db.create_deployment(run_id=run_id, project_id=project_id, route_id=route_id)
-
- request_body_blocks = [
- block for block in blocks if block.get("type") == "RequestBody"
- ]
- request_body_template = (
- request_body_blocks[0]["prompt"] if len(request_body_blocks) > 0 else None
- )
-
- # Create playground for the LLM
- playground_tools, playground = create_playground_tools(
- envs=envs,
- route=route,
- method=method,
- request_body_template=request_body_template,
- )
-
- # Create a new instance of code generator
- cg = Codegen.from_playground_and_database(
- playground_tools=playground_tools,
- database=db,
- )
-
- # Generate the code
- cg.generate(
- run_id=run_id,
- route=route,
- envs=envs,
- method=method,
- blocks=blocks,
- )
-
- db.update_state(run_id=run_id, state=DeploymentState.Finished)
-
- playground.close()
-
- return {
- "code": "",
- "prompt": "",
- "url": "",
- }
-
- ##################
-
- # db.create_deployment(run_id=run_id, project_id=project_id, route_id=route_id)
-
- # final_prompt, js_code = generate_req_handler(
- # db=db,
- # run_id=run_id,
- # blocks=blocks,
- # method=method,
- # route=route,
- # envs=envs,
- # )
-
- # db.update_state(run_id=run_id, state=DeploymentState.Deploying)
-
- # cf_worker_dir_path = (
- # os.path.abspath(os.path.dirname(__file__)) + "/cf-worker-template"
- # )
- # index_js_path = cf_worker_dir_path + "/index.js"
- # wrangler_template_path = cf_worker_dir_path + "/wrangler.template.toml"
- # wrangler_path = cf_worker_dir_path + "/wrangler.toml"
-
- # # TODO: Inject code to cf-worker-template/index.js
- # code = js_code.strip("`").strip()
- # with open(index_js_path, "w") as file:
- # file.write(code)
-
- # # Convert envs to the env vars string that looks like this:
- # # KEY_1 = VALUE_1
- # # KEY_2 = VALUE_2
- # envs_str = ""
- # for env in envs:
- # if env["key"]:
- # envs_str += f'{env["key"]} = "{env["value"]}"\n'
- # # TODO: Update name in cf-worker-template/wrangler.template.toml
- # replace_file_content(
- # wrangler_template_path,
- # wrangler_path,
- # {"<NAME>": f'"{project_id}"', "<VARS>": envs_str},
- # )
-
- # # TODO: Call npm run deploy
- # cmd = ["npm", "run", "deploy", "--prefix", cf_worker_dir_path]
- # with subprocess.Popen(cmd, stdout=subprocess.PIPE) as proc:
- # print(proc.stdout.read().decode())
-
- # db.update_state(run_id=run_id, state=DeploymentState.Finished)
-
- # api_url = f"https://{project_id}.devbook.workers.dev"
- # db.update_url(run_id=run_id, url=api_url)
-
- # # TODO: Report back the URL of deployed API
- # return {
- # "code": js_code.strip("`").strip(),
- # "prompt": final_prompt,
- # "url": api_url,
- # }
++ playground = None
+ try:
- request_body_template = get_request_body_template(blocks)
++ # Create playground for the LLM
+ playground_tools, playground = create_playground_tools(
+ envs=envs,
+ route=route,
+ method=method,
- request_body_template=request_body_template,
++ request_body_template=get_request_body_template(blocks),
+ )
+
- cg = Codegen.from_playground_tools(playground_tools)
++ # Create a new instance of code generator
++ cg = Codegen.from_playground_and_database(
++ playground_tools=playground_tools,
++ database=db,
++ )
++
++ # Generate the code
+ cg.generate(
++ run_id=run_id,
+ route=route,
+ envs=envs,
+ method=method,
+ blocks=blocks,
+ )
+
+ db.update_state(run_id=run_id, state=DeploymentState.Deploying)
+ url = playground.deploy(project_id, envs)
- db.finish_deployment(run_id=run_id, url=url)
+
- return {"url": url}
++ db.finish_deployment(run_id=run_id, url=url)
++ return "", 204
+ except:
+ db.update_state(run_id=run_id, state=DeploymentState.Error)
+ raise
++ finally:
++ if playground is not None:
++ playground.close()
if __name__ == "__main__":
diff --cc api-service/codegen/codegen.py
index d8747e5fd,7da126290..7fd1f7754
--- a/api-service/codegen/codegen.py
+++ b/api-service/codegen/codegen.py
@@@ -23,7 -24,8 +24,8 @@@ from langchain.tools import BaseToo
# from database import Database
# from codegen.tools.playground import create_playground_tools
+from session.env import EnvVar
+ from database import Database
-from codegen.env import EnvVar
from codegen.agent import CodegenAgent, CodegenAgentExecutor
from codegen.prompt import (
PREFIX,
diff --cc api-service/codegen/tools/playground/main.py
index b0231f1e4,ae8479465..7f179ac75
--- a/api-service/codegen/tools/playground/main.py
+++ b/api-service/codegen/tools/playground/main.py
@@@ -1,9 -1,11 +1,11 @@@
--from typing import List, Any, Tuple
++from typing import Any, List, Tuple
+
+ from langchain.tools import BaseTool
-from codegen.env import EnvVar
+from session.env import EnvVar
- from session.playground import NodeJSPlayground
++from session.playground.nodejs import NodeJSPlayground
from codegen.tools.playground.mock.request import MockRequestFactory
-from .playground import NodeJSPlayground, Playground
from .tools.filesystem import create_filesystem_tools
from .tools.process import create_process_tools
from .tools.code import create_code_tools
@@@ -14,9 -16,8 +16,9 @@@ def create_playground_tools
method: str,
route: str,
request_body_template: str | None,
- ) -> Tuple[List[Any], NodeJSPlayground]:
+ ) -> Tuple[List[BaseTool], NodeJSPlayground]:
playground = NodeJSPlayground(envs)
+
mock = MockRequestFactory(
method=method,
route=route,
@@@ -24,7 -25,7 +26,7 @@@
playground=playground,
)
-- subtools = [
++ subtools: List[Any] = [
tool
for tools in (
tool_factory(playground=playground, mock=mock)
diff --cc api-service/session/playground/nodejs.py
index 25fd28966,000000000..858596a3e
mode 100644,000000..100644
--- a/api-service/session/playground/nodejs.py
+++ b/api-service/session/playground/nodejs.py
@@@ -1,52 -1,0 +1,51 @@@
+import pprint
+import os
+
+from typing import List
+
+from session.env import EnvVar
+from session.playground import Playground
+
+
+class NodeJSPlayground(Playground):
+ node_js_env_id = "dCeMnVVxu01L"
+ rootdir = "/code"
+ default_javascript_code_file = os.path.join(rootdir, "index.js")
+ run_code_timeout = 5 # 5s
+
+ def __init__(self, envs: List[EnvVar]):
+ super().__init__(NodeJSPlayground.node_js_env_id, envs)
+
+ def run_javascript_code(self, code: str):
- # print(f"Running javascript code: {code}")
- print(f"Running javascript code...")
++ # print(f"Running javascript code...")
+ self.write_file(self.default_javascript_code_file, code)
+ result = self.run_command(
+ f"node {self.default_javascript_code_file}",
+ rootdir=self.rootdir,
+ timeout=self.run_code_timeout,
+ )
- pprint.pprint(f"Result: {result}")
++ # pprint.pprint(f"Result: {result}")
+ return result
+
+ def install_dependencies(self, dependencies: str):
- print(f"Installing dependencies: {dependencies}")
++ # print(f"Installing dependencies: {dependencies}")
+ result = self.run_command(f"npm install {dependencies}", rootdir=self.rootdir)
- pprint.pprint(f"Result: {result}")
++ # pprint.pprint(f"Result: {result}")
+ return result
+
+ def run_javascript_server_code_with_request(
+ self,
+ code: str,
+ request_cmd: str,
+ port: float,
+ ):
- print(f"Running '{request_cmd}' for code:\n{code}")
++ # print(f"Running '{request_cmd}' for code:\n{code}")
+ self.write_file(self.default_javascript_code_file, code)
+ result = self.run_server_with_request(
+ server_cmd=f"node {self.default_javascript_code_file}",
+ request_cmd=request_cmd,
+ port=port,
+ rootdir=self.rootdir,
+ )
- pprint.pprint(f"Result: {result}")
++ # pprint.pprint(f"Result: {result}")
+ return result