Skip to content

armgit / E2B

Public Imported from GitHub
0

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

Add and improve docstrings in SDKs (#249)

Tomas Valenta19fad9fea1f8
diff --git a/.changeset/green-taxis-talk.md b/.changeset/green-taxis-talk.md
new file mode 100644
index 000000000..544aff38b
--- /dev/null
+++ b/.changeset/green-taxis-talk.md
@@ -0,0 +1,6 @@
+---
+"@e2b/python-sdk": patch
+"@e2b/sdk": patch
+---
+
+Improve docstrings
diff --git a/packages/js-sdk/src/sandbox/index.ts b/packages/js-sdk/src/sandbox/index.ts
index 9a9e2916c..ae02e7cb3 100644
--- a/packages/js-sdk/src/sandbox/index.ts
+++ b/packages/js-sdk/src/sandbox/index.ts
@@ -494,7 +494,13 @@ export class Sandbox extends SandboxConnection {
*
* @example
* ```ts
- * const sandbox = await Sandbox.reconnect(sandboxID)
+ * const sandbox = await Sandbox.create()
+ * const sandboxID = sandbox.id
+ *
+ * await sandbox.keepAlive(300 * 1000)
+ * await sandbox.close()
+ *
+ * const reconnectedSandbox = await Sandbox.reconnect(sandboxID)
* ```
*/
static async reconnect(sandboxID: string): Promise<Sandbox>;
@@ -505,13 +511,19 @@ export class Sandbox extends SandboxConnection {
*
* @example
* ```ts
- * const sandbox = await Sandbox.reconnect({
+ * const sandbox = await Sandbox.create()
+ * const sandboxID = sandbox.id
+ *
+ * await sandbox.keepAlive(300 * 1000)
+ * await sandbox.close()
+ *
+ * const reconnectedSandbox = await Sandbox.reconnect({
* sandboxID,
* })
* ```
*/
- static async reconnect(opts: Omit<SandboxOpts, 'id'> & { sandboxID: string }): Promise<Sandbox>;
- static async reconnect(sandboxIDorOpts: string | Omit<SandboxOpts, 'id'> & { sandboxID: string }) {
+ static async reconnect(opts: Omit<SandboxOpts, 'id' | 'template'> & { sandboxID: string }): Promise<Sandbox>;
+ static async reconnect(sandboxIDorOpts: string | Omit<SandboxOpts, 'id' | 'template'> & { sandboxID: string }) {
let sandboxID: string
let opts: SandboxOpts
if (typeof sandboxIDorOpts === 'string') {
diff --git a/packages/js-sdk/src/sandbox/sandboxConnection.ts b/packages/js-sdk/src/sandbox/sandboxConnection.ts
index ff0fa3588..c26589951 100644
--- a/packages/js-sdk/src/sandbox/sandboxConnection.ts
+++ b/packages/js-sdk/src/sandbox/sandboxConnection.ts
@@ -69,7 +69,17 @@ const refreshSandbox = withAPIKey(
)
export class SandboxConnection {
+ /**
+ * Default working directory used in the sandbox.
+ *
+ * You can change the working directory by setting the `cwd` property.
+ **/
cwd: string | undefined
+ /**
+ * Default environment variables used in the sandbox.
+ *
+ * You can change the environment variables by setting the `envVars` property.
+ **/
envVars: EnvVars
protected readonly logger: Logger
@@ -80,7 +90,6 @@ export class SandboxConnection {
private readonly rpc = new RpcWebSocketClient()
private subscribers: Subscriber[] = []
-
// let's keep opts readonly, but public - for convenience, mainly when debugging
protected constructor(readonly opts: SandboxConnectionOpts) {
this.sandbox = opts.__sandbox
@@ -108,6 +117,11 @@ export class SandboxConnection {
this.logger.debug?.(`Sandbox "${this.templateID}" initialized`)
}
+ /**
+ * ID of the sandbox.
+ *
+ * You can use this ID to reconnect to the sandbox later.
+ */
get id() {
return `${this.sandbox?.instanceID}-${this.sandbox?.clientID}`
}
@@ -120,7 +134,7 @@ export class SandboxConnection {
* Keep the sandbox alive for the specified duration.
*
* `keepAlive` method requires `this` context - you may need to bind it.
- * @param duration Duration in milliseconds
+ * @param duration Duration in milliseconds. Must be between 0 and 3600000 milliseconds
* @returns Promise that resolves when the sandbox is kept alive
*/
public async keepAlive(duration: number) {
diff --git a/packages/python-sdk/e2b/sandbox/sandbox_connection.py b/packages/python-sdk/e2b/sandbox/sandbox_connection.py
index 961366e0f..2eaf55030 100644
--- a/packages/python-sdk/e2b/sandbox/sandbox_connection.py
+++ b/packages/python-sdk/e2b/sandbox/sandbox_connection.py
@@ -47,6 +47,8 @@ class SandboxConnection:
def id(self):
"""
The sandbox ID.
+
+ You can use this ID to reconnect to the sandbox later.
"""
if not self._sandbox:
return None
@@ -86,7 +88,17 @@ class SandboxConnection:
)
self.cwd = cwd
+ """
+ Default working directory used in the sandbox.
+
+ You can change the working directory by setting the `cwd` property.
+ """
self.env_vars = env_vars or {}
+ """
+ Default environment variables used in the sandbox.
+
+ You can change the environment variables by setting the `env_vars` property.
+ """
self._template = template
self._api_key = api_key
self._debug_hostname = _debug_hostname
@@ -117,6 +129,29 @@ class SandboxConnection:
*args,
**kwargs,
):
+ """
+ Reconnects to a previously created sandbox.
+
+ :param sandbox_id: ID of the sandbox to reconnect to
+ :param cwd: The current working directory to use
+ :param on_scan_ports: A callback to handle opened ports
+ :param on_stdout: A default callback that is called when stdout with a newline is received from the process
+ :param on_stderr: A default callback that is called when stderr with a newline is received from the process
+ :param on_exit: A default callback that is called when the process exits
+ :param timeout: Timeout for sandbox to initialize in seconds, default is 60 seconds
+
+ ```py
+ sandbox = Sandbox()
+ id = sandbox.id
+ sandbox.keep_alive(300)
+ sandbox.close()
+
+ # Reconnect to the sandbox
+ reconnected_sandbox = Sandbox.reconnect(id)
+ ```
+
+ """
+
logger.info(f"Reconnecting to sandbox {sandbox_id}")
instance_id, client_id = sandbox_id.split("-")
return cls(
@@ -157,6 +192,11 @@ class SandboxConnection:
return hostname
def keep_alive(self, duration: int) -> None:
+ """
+ Keep the sandbox alive for the specified duration.
+
+ :param duration: Duration in seconds. Must be between 0 and 3600 seconds.
+ """
if not 0 <= duration <= 3600:
raise ValueError("Duration must be between 0 and 3600 seconds")