Skip to content

armgit / E2B

Public Imported from GitHub
0

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

Separate sandbox static api as in Python

Tomas Valentae08895f1fee8
diff --git a/packages/js-sdk/src/api/index.ts b/packages/js-sdk/src/api/index.ts
index 61e87147e..2eeb819b0 100644
--- a/packages/js-sdk/src/api/index.ts
+++ b/packages/js-sdk/src/api/index.ts
@@ -32,7 +32,7 @@ class ApiClient {
if (opts?.requireAccessToken && !config.accessToken) {
throw new AuthenticationError(
'Access token is required, please visit https://e2b.dev/docs to get your access token. ' +
- 'You can either set the environment variable `E2B_ACCESS_TOKEN`',
+ 'You can set the environment variable `E2B_ACCESS_TOKEN` or pass the `accessToken` in options.',
)
}
diff --git a/packages/js-sdk/src/sandbox/index.ts b/packages/js-sdk/src/sandbox/index.ts
index ff39936c7..d7c9460d4 100644
--- a/packages/js-sdk/src/sandbox/index.ts
+++ b/packages/js-sdk/src/sandbox/index.ts
@@ -4,8 +4,8 @@ import { ConnectionOpts, ConnectionConfig } from '../connectionConfig'
import { Filesystem } from './filesystem'
import { Process } from './process'
import { EnvdApiClient } from '../envd/api'
-import { ApiClient } from '../api'
import { Terminal } from './terminal'
+import { SandboxApi } from './sandboxApi'
export interface Logger {
debug?: (...args: unknown[]) => void
@@ -28,7 +28,7 @@ export interface SandboxOpts extends ConnectionOpts {
timeoutMs?: number
}
-export class Sandbox {
+export class Sandbox extends SandboxApi {
protected static readonly defaultTemplate = 'base'
readonly filesystem: Filesystem
@@ -40,6 +40,8 @@ export class Sandbox {
private readonly connectionConfig: ConnectionConfig
constructor(readonly sandboxID: string, opts?: Omit<SandboxOpts, 'timeoutMs' | 'metadata'>) {
+ super()
+
this.connectionConfig = new ConnectionConfig(opts)
const sandboxServerUrl = `${this.connectionConfig.debug ? 'http' : 'https'}://${this.getHost(this.envdPort)}`
@@ -68,86 +70,6 @@ export class Sandbox {
return new this(sandboxID, opts) as InstanceType<S>
}
- static async kill(
- sandboxID: string,
- opts?: ConnectionOpts,
- ): Promise<void> {
- const config = new ConnectionConfig(opts)
- const client = new ApiClient(config)
-
- // TODO: Ensure the short id/long id works
- // TODO: Check if the errors are thrown properly
-
- await client.api.DELETE('/sandboxes/{sandboxID}', {
- params: {
- path: {
- sandboxID,
- },
- },
- })
- }
-
- static async list(opts?: ConnectionOpts): Promise<RunningSandbox[]> {
- const config = new ConnectionConfig(opts)
- const client = new ApiClient(config)
-
- const res = await client.api.GET('/sandboxes')
-
- return res.data?.map((sandbox) => ({
- sandboxID: this.getSandboxID(sandbox),
- templateID: sandbox.templateID,
- ...(sandbox.alias && { name: sandbox.alias }),
- ...(sandbox.metadata && { metadata: sandbox.metadata }),
- startedAt: new Date(sandbox.startedAt),
- })) ?? []
- }
-
- protected static async createSandbox(
- template: string,
- opts?: ConnectionOpts & {
- metadata?: Record<string, string>,
- timeout?: number,
- }): Promise<string> {
- const config = new ConnectionConfig(opts)
- const client = new ApiClient(config)
-
- const res = await client.api.POST('/sandboxes', {
- body: {
- templateID: template,
- metadata: opts?.metadata,
- timeout: opts?.timeout,
- },
- })
-
- return this.getSandboxID(res.data!)
- }
-
- protected static async setTimeout(
- sandboxID: string,
- timeoutMs: number,
- opts?: ConnectionOpts,
- ): Promise<void> {
- const config = new ConnectionConfig(opts)
- const client = new ApiClient(config)
-
- // TODO: Ensure the short id/long id works
-
- await client.api.POST('/sandboxes/{sandboxID}/timeout', {
- params: {
- path: {
- sandboxID,
- },
- },
- body: {
- timeout: timeoutMs,
- },
- })
- }
-
- private static getSandboxID({ sandboxID, clientID }: { sandboxID: string, clientID: string }): string {
- return `${sandboxID}-${clientID}`
- }
-
getHost(port: number) {
if (this.connectionConfig.debug) {
return `localhost:${port}`
diff --git a/packages/js-sdk/src/sandbox/sandboxApi.ts b/packages/js-sdk/src/sandbox/sandboxApi.ts
new file mode 100644
index 000000000..c373b45d2
--- /dev/null
+++ b/packages/js-sdk/src/sandbox/sandboxApi.ts
@@ -0,0 +1,94 @@
+import { ApiClient } from '../api'
+import { ConnectionConfig, ConnectionOpts } from '../connectionConfig'
+
+export interface RunningSandbox {
+ sandboxID: string
+ templateID: string
+ name?: string
+ metadata?: Record<string, string>
+ startedAt: Date
+}
+
+export class SandboxApi {
+ protected constructor() { }
+
+ static async kill(
+ sandboxID: string,
+ opts?: ConnectionOpts,
+ ): Promise<void> {
+ const config = new ConnectionConfig(opts)
+ const client = new ApiClient(config)
+
+ // TODO: Ensure the short id/long id works
+ // TODO: Check if the errors are thrown properly
+
+ await client.api.DELETE('/sandboxes/{sandboxID}', {
+ params: {
+ path: {
+ sandboxID,
+ },
+ },
+ })
+ }
+
+ static async list(opts?: ConnectionOpts): Promise<RunningSandbox[]> {
+ const config = new ConnectionConfig(opts)
+ const client = new ApiClient(config)
+
+ const res = await client.api.GET('/sandboxes')
+
+ return res.data?.map((sandbox) => ({
+ sandboxID: this.getSandboxID(sandbox),
+ templateID: sandbox.templateID,
+ ...(sandbox.alias && { name: sandbox.alias }),
+ ...(sandbox.metadata && { metadata: sandbox.metadata }),
+ startedAt: new Date(sandbox.startedAt),
+ })) ?? []
+ }
+
+ protected static async createSandbox(
+ template: string,
+ opts?: ConnectionOpts & {
+ metadata?: Record<string, string>,
+ timeout?: number,
+ }): Promise<string> {
+ const config = new ConnectionConfig(opts)
+ const client = new ApiClient(config)
+
+ const res = await client.api.POST('/sandboxes', {
+ body: {
+ templateID: template,
+ metadata: opts?.metadata,
+ timeout: opts?.timeout,
+ },
+ })
+
+ return this.getSandboxID(res.data!)
+ }
+
+ protected static async setTimeout(
+ sandboxID: string,
+ timeoutMs: number,
+ opts?: ConnectionOpts,
+ ): Promise<void> {
+ const config = new ConnectionConfig(opts)
+ const client = new ApiClient(config)
+
+ // TODO: Ensure the short id/long id works
+
+ await client.api.POST('/sandboxes/{sandboxID}/timeout', {
+ params: {
+ path: {
+ sandboxID,
+ },
+ },
+ body: {
+ timeout: timeoutMs,
+ },
+ })
+ }
+
+ private static getSandboxID({ sandboxID, clientID }: { sandboxID: string, clientID: string }): string {
+ return `${sandboxID}-${clientID}`
+ }
+}