Skip to content

armgit / E2B

Public Imported from GitHub
0

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

Make playground tools async

Tomas Valenta778dec63fa21
diff --git a/api-service/codegen/tools/playground/async_tool.py b/api-service/codegen/tools/playground/async_tool.py
new file mode 100644
index 000000000..d6a5645a2
--- /dev/null
+++ b/api-service/codegen/tools/playground/async_tool.py
@@ -0,0 +1,25 @@
+from typing import Awaitable, Callable, Optional
+from langchain.agents import Tool
+from langchain.tools.base import BaseTool
+
+
+def func(_: str):
+ raise NotImplementedError()
+
+
+def async_tool(name: str) -> Callable:
+ def _make_tool(arun: Optional[Callable[[str], Awaitable[str]]]) -> BaseTool:
+ if arun.__doc__ is None:
+ raise NotImplementedError(f"Missing docstring for {name} async tool")
+
+ description = f"{arun.__doc__.strip()}"
+ tool_ = Tool(
+ name=name,
+ func=func,
+ description=description,
+ return_direct=False,
+ )
+ tool_.coroutine = arun
+ return tool_
+
+ return _make_tool
diff --git a/api-service/codegen/tools/playground/tools/code.py b/api-service/codegen/tools/playground/tools/code.py
index e9c458730..934d31e22 100644
--- a/api-service/codegen/tools/playground/tools/code.py
+++ b/api-service/codegen/tools/playground/tools/code.py
@@ -1,3 +1,4 @@
+from codegen.tools.playground.async_tool import async_tool
from langchain.agents import tool
from codegen.tools.playground.mock.request import MockRequestFactory
@@ -13,8 +14,8 @@ def create_code_tools(playground: NodeJSPlayground, mock: MockRequestFactory, **
# Ensure that the function is a generator even if no tools are yielded
yield from ()
- @tool("InstallNPMDependencies")
- def install_dependencies(dependencies: str) -> str:
+ @async_tool("InstallNPMDependencies")
+ async def install_dependencies(dependencies: str) -> str:
"""Install JavaScript packages with NPM. The input should be valid names of NPM packages. Example usage:
```
<action tool="InstallNPMDependencies">
@@ -30,8 +31,8 @@ def create_code_tools(playground: NodeJSPlayground, mock: MockRequestFactory, **
last_javascript_code: str | None = None
# This tool is just for executing JavaScript without doing the request to server
- @tool("RunJavaScriptCode")
- def run_javascript_code(code: str) -> str:
+ @async_tool("RunJavaScriptCode")
+ async def run_javascript_code(code: str) -> str:
"""
Run JavaScript code as ECMAScript module and return output.
Input should be a valid JavaScript code. Example usage:
@@ -55,8 +56,8 @@ def create_code_tools(playground: NodeJSPlayground, mock: MockRequestFactory, **
yield run_javascript_code
- @tool("CurlJavaScriptServer")
- def curl_javascript_server(empty: str) -> str:
+ @async_tool("CurlJavaScriptServer")
+ async def curl_javascript_server(empty: str) -> str:
"""
Make a request to check if the previously run JavaScript code is a server that can handle the needed request. Example usage:
```
diff --git a/api-service/codegen/tools/playground/tools/filesystem.py b/api-service/codegen/tools/playground/tools/filesystem.py
index d55849562..3efe6677c 100644
--- a/api-service/codegen/tools/playground/tools/filesystem.py
+++ b/api-service/codegen/tools/playground/tools/filesystem.py
@@ -1,6 +1,6 @@
from typing import List
-from langchain.agents import tool
+from codegen.tools.playground.async_tool import async_tool
from session.playground import NodeJSPlayground
from playground_client.models.entry_info import EntryInfo
@@ -18,15 +18,15 @@ def create_filesystem_tools(playground: NodeJSPlayground, **kwargs):
# Ensure that the function is a generator even if no tools are yielded
yield from ()
- @tool("ReadFile")
- def read_file(path: str) -> str:
+ @async_tool("ReadFile")
+ async def read_file(path: str) -> str:
"""Read content of the file from filesystem."""
return playground.read_file(path)
yield read_file
- @tool("SaveFile")
- def save_file(input: str) -> str:
+ @async_tool("SaveFile")
+ async def save_file(input: str) -> str:
"""Save content to a file in the filesystem.
The input should be a path to a file followed by the content of the file.
Separate the path and content by a newline character.
@@ -38,24 +38,24 @@ def create_filesystem_tools(playground: NodeJSPlayground, **kwargs):
yield save_file
- @tool("DeleteFile")
- def delete_file(path: str) -> str:
+ @async_tool("DeleteFile")
+ async def delete_file(path: str) -> str:
"""Delete the file."""
playground.delete_file(path)
return "File deleted"
yield delete_file
- @tool("ListDirectory")
- def list_directory(path: str) -> str:
+ @async_tool("ListDirectory")
+ async def list_directory(path: str) -> str:
"""List all files and subdirectories from the directory."""
entries = playground.list_dir(path)
return encode_directory_listing(entries)
yield list_directory
- @tool("DeleteDirectory")
- def delete_directory(path: str) -> str:
+ @async_tool("DeleteDirectory")
+ async def delete_directory(path: str) -> str:
"""Delete the directory."""
playground.delete_dir(path)
return "Directory deleted"
diff --git a/api-service/codegen/tools/playground/tools/process.py b/api-service/codegen/tools/playground/tools/process.py
index 014b4d203..9d4bb71c1 100644
--- a/api-service/codegen/tools/playground/tools/process.py
+++ b/api-service/codegen/tools/playground/tools/process.py
@@ -1,4 +1,4 @@
-from langchain.agents import tool
+from codegen.tools.playground.async_tool import async_tool
from session.playground import NodeJSPlayground
from playground_client.models.process_response import ProcessResponse
@@ -32,9 +32,8 @@ def create_process_tools(playground: NodeJSPlayground, **kwargs):
# Ensure that the function is a generator even if no tools are yielded
yield from ()
- # TODO: Escape command?
- @tool("RunTerminalCommand")
- def run_terminal_command(command: str) -> str:
+ @async_tool("RunTerminalCommand")
+ async 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.