Skip to content

armgit / E2B

Public Imported from GitHub
0

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

Update SDKs docstrings

Tomas Valentabc39e20184f0
diff --git a/packages/js-sdk/src/sandbox/index.ts b/packages/js-sdk/src/sandbox/index.ts
index 289e69243..39caaebfb 100644
--- a/packages/js-sdk/src/sandbox/index.ts
+++ b/packages/js-sdk/src/sandbox/index.ts
@@ -432,10 +432,20 @@ export class Sandbox extends SandboxConnection {
return `${protocol}://${hostname}${FILE_ROUTE}`
}
+ /**
+ * Returns a list of registered actions.
+ *
+ * @returns List of registered actions
+ */
get actions() {
return [...this._actions.entries()]
}
+ /**
+ * OpenAI integration that can be used to get output for the actions registered in the sandbox.
+ *
+ * @returns OpenAI integration
+ */
get openai() {
return {
assistant: new Assistant(this),
@@ -465,12 +475,36 @@ export class Sandbox extends SandboxConnection {
})
}
+ /**
+ * Registers a new action.
+ * @param name Action name
+ * @param action Action handler
+ * @returns Sandbox
+ *
+ * @example
+ * ```ts
+ * const sandbox = await Sandbox.create()
+ * sandbox.registerAction('hello', (sandbox, args) => 'Hello World')
+ * ```
+ */
registerAction<T = { [name: string]: any }>(name: string, action: Action<T>) {
this._actions.set(name, action)
return this
}
+ /**
+ * Unregisters an action.
+ * @param name Action name
+ * @returns Sandbox
+ *
+ * @example
+ * ```ts
+ * const sandbox = await Sandbox.create()
+ * sandbox.registerAction('hello', (sandbox, args) => 'Hello World')
+ * sandbox.unregisterAction('hello')
+ * ```
+ */
unregisterAction(name: string) {
this._actions.delete(name)
@@ -511,6 +545,18 @@ export class Sandbox extends SandboxConnection {
return `/home/user/${filename}`
}
+ /**
+ * Downloads a file from the sandbox.
+ * @param remotePath Path to a file on the sandbox
+ * @param format Format of the downloaded file
+ * @returns File content
+ *
+ * @example
+ * ```ts
+ * const sandbox = await Sandbox.create()
+ * const content = await sandbox.downloadFile('/home/user/file.txt')
+ * ```
+ */
async downloadFile(remotePath: string, format?: DownloadFileFormat) {
remotePath = encodeURIComponent(remotePath)
diff --git a/packages/js-sdk/src/templates/openai.ts b/packages/js-sdk/src/templates/openai.ts
index ba51b1cc1..31139cf78 100644
--- a/packages/js-sdk/src/templates/openai.ts
+++ b/packages/js-sdk/src/templates/openai.ts
@@ -2,9 +2,16 @@ import type { Run, RunSubmitToolOutputsParams } from 'openai/resources/beta/thre
import type { Sandbox } from '../sandbox'
+
export class Assistant {
constructor(private readonly sandbox: Sandbox) { }
+ /**
+ * Call the required actions for the provided run and return their outputs.
+ *
+ * @param run OpenAI run object from `openai.beta.threads.runs.retrieve` or `openai.beta.threads.runs.retrieve.create` call that contains the names of the required actions and their arguments.
+ * @returns The outputs of the required actions in the run.
+ */
async run(run: Run): Promise<RunSubmitToolOutputsParams.ToolOutput[]> {
if (run.status !== 'requires_action') {
return []
diff --git a/packages/python-sdk/e2b/sandbox/main.py b/packages/python-sdk/e2b/sandbox/main.py
index c991f8b64..e1afe6c55 100644
--- a/packages/python-sdk/e2b/sandbox/main.py
+++ b/packages/python-sdk/e2b/sandbox/main.py
@@ -113,20 +113,50 @@ class Sandbox(SandboxConnection):
self._actions: Dict[str, Action] = {}
def register_action(self, name: str, action: "Action"):
+ """
+ Register an action.
+ An action is a function that takes a sandbox and a dictionary of arguments and returns a string.
+
+ :param name: The name of the action
+ :param action: The action to register
+
+ Example:
+
+ ```python
+ from e2b import Sandbox
+
+ s = Sandbox()
+ s.register_action("hello", lambda s, args: f"Hello {args['name']}!")
+ ```
+ """
self._actions[name] = action
return self
def unregister_action(self, name: str):
+ """
+ Unregister an action.
+
+ :param name: The name of the action
+ """
del self._actions[name]
return self
@property
def actions(self) -> List[Tuple[str, "Action"]]:
+ """
+ Return a list of registered actions.
+ """
return [(name, action) for name, action in self._actions.items()]
def action(self, name: Union[str, None] = None):
+ """
+ Decorator to register an action.
+
+ :param name: The name of the action, if not provided, the name of the function will be used
+ """
+
def _action(action: Action):
self.register_action(name or action.__name__, action)
@@ -136,6 +166,19 @@ class Sandbox(SandboxConnection):
@property
def openai(self):
+ """
+ OpenAI integration that can be used to get output for the actions registered in the sandbox.
+
+ Example:
+
+ ```python
+ from e2b import Sandbox
+
+ s = Sandbox()
+ s.openai.assistant.run(run)
+ ```
+ """
+
from e2b.templates.openai import OpenAI, Assistant
return OpenAI(Assistant(self))
diff --git a/packages/python-sdk/e2b/templates/openai.py b/packages/python-sdk/e2b/templates/openai.py
index 106d4af43..c6e31ec45 100644
--- a/packages/python-sdk/e2b/templates/openai.py
+++ b/packages/python-sdk/e2b/templates/openai.py
@@ -17,6 +17,13 @@ class Assistant:
self._sandbox = sandbox
def run(self, run: "Run") -> List["ToolOutput"]:
+ """
+ Call the required actions for the provided run and return their outputs.
+
+ :param run: OpenAI run object from `openai.beta.threads.runs.retrieve` or `openai.beta.threads.runs.retrieve.create` call that contains the names of the required actions and their arguments.
+
+ :return: The outputs of the required actions in the run.
+ """
if run.status != "requires_action":
return []
diff --git a/packages/python-sdk/example.py b/packages/python-sdk/example.py
index 65708ef2a..e2fae5497 100644
--- a/packages/python-sdk/example.py
+++ b/packages/python-sdk/example.py
@@ -14,6 +14,10 @@ logging.basicConfig(level=logging.ERROR)
def main():
s = Sandbox(api_key=E2B_API_KEY)
+
+
+ s.openai.assistant.
+
a = s.process.start("ls -la /var ")
a.wait()
print(a.stdout)