Skip to content

armgit / E2B

Public Imported from GitHub
0

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

Deprecate id in JS SDK

Tomas Valenta56517c2a7099
diff --git a/packages/js-sdk/src/runCmd.ts b/packages/js-sdk/src/runCmd.ts
index aaf034045..becaf62a9 100644
--- a/packages/js-sdk/src/runCmd.ts
+++ b/packages/js-sdk/src/runCmd.ts
@@ -2,7 +2,7 @@ import { Sandbox } from './sandbox'
export async function runCmd(command: string, opts?: { apiKey?: string }) {
const sandbox = await Sandbox.create({
- id: 'Bash',
+ template: 'Bash',
apiKey: opts?.apiKey || process?.env?.E2B_API_KEY || '', // Sandbox.create will throw an error if the API key is not provided so no need to check here
})
diff --git a/packages/js-sdk/src/runCode.ts b/packages/js-sdk/src/runCode.ts
index 2a5d57345..dae6be30a 100644
--- a/packages/js-sdk/src/runCode.ts
+++ b/packages/js-sdk/src/runCode.ts
@@ -58,7 +58,7 @@ export async function runCode(
}
const sandbox = await Sandbox.create({
- id: envID,
+ template: envID,
apiKey: opts?.apiKey || process?.env?.E2B_API_KEY || '', // Sandbox.create will throw an error if the API key is not provided so no need to check here
})
diff --git a/packages/js-sdk/src/sandbox/index.ts b/packages/js-sdk/src/sandbox/index.ts
index 0808f184b..879e5aae0 100644
--- a/packages/js-sdk/src/sandbox/index.ts
+++ b/packages/js-sdk/src/sandbox/index.ts
@@ -467,15 +467,15 @@ export class Sandbox extends SandboxConnection {
static async create(): Promise<Sandbox>;
/**
* Creates a new Sandbox from the template with the specified ID.
- * @param id Sandbox ID
+ * @param template Sandbox template ID or name
* @returns New Sandbox
*
* @example
* ```ts
- * const sandbox = await Sandbox.create("sandboxID")
+ * const sandbox = await Sandbox.create("sandboxTemplateID")
* ```
*/
- static async create(id: string): Promise<Sandbox>;
+ static async create(template: string): Promise<Sandbox>;
/**
* Creates a new Sandbox from the specified options.
* @param opts Sandbox options
@@ -484,14 +484,14 @@ export class Sandbox extends SandboxConnection {
* @example
* ```ts
* const sandbox = await Sandbox.create({
- * id: "sandboxID",
+ * template: "sandboxTemplate",
* onStdout: console.log,
* })
* ```
*/
static async create(opts: SandboxOpts): Promise<Sandbox>;
- static async create(optsOrID?: string | SandboxOpts) {
- const opts = typeof optsOrID === 'string' ? { id: optsOrID } : optsOrID
+ static async create(optsOrTemplate?: string | SandboxOpts) {
+ const opts = typeof optsOrTemplate === 'string' ? { template: optsOrTemplate } : optsOrTemplate
return new Sandbox(opts)
._open({ timeout: opts?.timeout })
.then(async (sandbox) => {
diff --git a/packages/js-sdk/src/sandbox/sandboxConnection.ts b/packages/js-sdk/src/sandbox/sandboxConnection.ts
index b1b06cfd4..a6139bf3e 100644
--- a/packages/js-sdk/src/sandbox/sandboxConnection.ts
+++ b/packages/js-sdk/src/sandbox/sandboxConnection.ts
@@ -34,6 +34,12 @@ export interface Logger {
}
export interface SandboxConnectionOpts {
+ template?: string;
+ /**
+ * @deprecated Use `template` instead.
+ *
+ * Sandbox Template ID or name.
+ */
id?: string;
apiKey?: string;
cwd?: string;
@@ -102,10 +108,9 @@ export class SandboxConnection {
}
private get templateID(): string {
- return this.opts.id || 'base'
+ return this.opts.template || this.opts.id || 'base'
}
-
/**
* Keep the sandbox alive for the specified duration.
*
@@ -128,8 +133,6 @@ export class SandboxConnection {
})
}
-
-
/**
* Get the hostname for the sandbox or for the specified sandbox's port.
*
@@ -337,93 +340,93 @@ export class SandboxConnection {
private async connectRpc() {
const hostname = this.getHostname(this.opts.__debug_port || ENVD_PORT)
- if (!hostname) {
- throw new Error('Cannot get sandbox\'s hostname')
- }
+ if (!hostname) {
+ throw new Error('Cannot get sandbox\'s hostname')
+ }
- const protocol = this.opts.__debug_devEnv === 'local' ? 'ws' : 'wss'
- const sandboxURL = `${protocol}://${hostname}${WS_ROUTE}`
+ const protocol = this.opts.__debug_devEnv === 'local' ? 'ws' : 'wss'
+ const sandboxURL = `${protocol}://${hostname}${WS_ROUTE}`
- this.rpc.onError((err) => {
- // not warn, because this is somewhat expected behaviour during initialization
- this.logger.debug?.(
- `Error in WebSocket of sandbox "${this.sandbox?.instanceID}": ${err.message ?? err.code ?? err.toString()
- }. Trying to reconnect...`,
- )
- })
+ this.rpc.onError((err) => {
+ // not warn, because this is somewhat expected behaviour during initialization
+ this.logger.debug?.(
+ `Error in WebSocket of sandbox "${this.sandbox?.instanceID}": ${err.message ?? err.code ?? err.toString()
+ }. Trying to reconnect...`,
+ )
+ })
- let isFinished = false
- let resolveOpening: (() => void) | undefined
- let rejectOpening: (() => void) | undefined
+ let isFinished = false
+ let resolveOpening: (() => void) | undefined
+ let rejectOpening: (() => void) | undefined
- const openingPromise = new Promise<void>((resolve, reject) => {
- resolveOpening = () => {
- if (isFinished) return
- isFinished = true
- resolve()
- }
- rejectOpening = () => {
- if (isFinished) return
- isFinished = true
- reject()
- }
- })
+ const openingPromise = new Promise<void>((resolve, reject) => {
+ resolveOpening = () => {
+ if (isFinished) return
+ isFinished = true
+ resolve()
+ }
+ rejectOpening = () => {
+ if (isFinished) return
+ isFinished = true
+ reject()
+ }
+ })
- this.rpc.onOpen(() => {
- this.logger.debug?.(
- `Connected to sandbox "${this.sandbox?.instanceID}"`,
- )
- resolveOpening?.()
- })
+ this.rpc.onOpen(() => {
+ this.logger.debug?.(
+ `Connected to sandbox "${this.sandbox?.instanceID}"`,
+ )
+ resolveOpening?.()
+ })
- this.rpc.onClose(async () => {
+ this.rpc.onClose(async () => {
+ this.logger.debug?.(
+ `Closing WebSocket connection to sandbox "${this.sandbox?.instanceID}"`,
+ )
+ if (this.isOpen) {
+ await wait(WS_RECONNECT_INTERVAL)
this.logger.debug?.(
- `Closing WebSocket connection to sandbox "${this.sandbox?.instanceID}"`,
+ `Reconnecting to sandbox "${this.sandbox?.instanceID}"`,
)
- if (this.isOpen) {
- await wait(WS_RECONNECT_INTERVAL)
+ try {
+ // When the WS connection closes the subscribers in devbookd are removed.
+ // We want to delete the subscriber handlers here so there are no orphans.
+ this.subscribers = []
+ await this.rpc.connect(sandboxURL)
this.logger.debug?.(
- `Reconnecting to sandbox "${this.sandbox?.instanceID}"`,
+ `Reconnected to sandbox "${this.sandbox?.instanceID}"`,
+ )
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ } catch (err: any) {
+ // not warn, because this is somewhat expected behaviour during initialization
+ this.logger.debug?.(
+ `Failed reconnecting to sandbox "${this.sandbox?.instanceID}": ${err.message ?? err.code ?? err.toString()
+ }`,
)
- try {
- // When the WS connection closes the subscribers in devbookd are removed.
- // We want to delete the subscriber handlers here so there are no orphans.
- this.subscribers = []
- await this.rpc.connect(sandboxURL)
- this.logger.debug?.(
- `Reconnected to sandbox "${this.sandbox?.instanceID}"`,
- )
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- } catch (err: any) {
- // not warn, because this is somewhat expected behaviour during initialization
- this.logger.debug?.(
- `Failed reconnecting to sandbox "${this.sandbox?.instanceID}": ${err.message ?? err.code ?? err.toString()
- }`,
- )
- }
- } else {
- rejectOpening?.()
}
- })
-
- this.rpc.onNotification.push(this.handleNotification.bind(this))
+ } else {
+ rejectOpening?.()
+ }
+ })
- try {
- this.logger.debug?.(
- `Connection to sandbox "${this.sandbox?.instanceID}"`,
- )
- await this.rpc.connect(sandboxURL)
+ this.rpc.onNotification.push(this.handleNotification.bind(this))
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- } catch (err: any) {
- // not warn, because this is somewhat expected behaviour during initialization
- this.logger.debug?.(
- `Error connecting to sandbox "${this.sandbox?.instanceID}": ${err.message ?? err.code ?? err.toString()
- }`,
- )
- }
+ try {
+ this.logger.debug?.(
+ `Connection to sandbox "${this.sandbox?.instanceID}"`,
+ )
+ await this.rpc.connect(sandboxURL)
+
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ } catch (err: any) {
+ // not warn, because this is somewhat expected behaviour during initialization
+ this.logger.debug?.(
+ `Error connecting to sandbox "${this.sandbox?.instanceID}": ${err.message ?? err.code ?? err.toString()
+ }`,
+ )
+ }
- await openingPromise
+ await openingPromise
}
private handleNotification(data: IRpcNotification) {
this.logger.debug?.('Handling notification:', data)
diff --git a/packages/js-sdk/src/templates/dataAnalysis.ts b/packages/js-sdk/src/templates/dataAnalysis.ts
index 6a1da33ec..18859e11f 100644
--- a/packages/js-sdk/src/templates/dataAnalysis.ts
+++ b/packages/js-sdk/src/templates/dataAnalysis.ts
@@ -20,15 +20,15 @@ export interface RunPythonOpts extends Omit<ProcessOpts, 'cmd'> {
onArtifact?: (artifact: Artifact) => Promise<void> | void;
}
-const DataAnalysisEnvId = 'Python3-DataAnalysis'
-
export class DataAnalysis extends Sandbox {
- constructor(opts: Omit<SandboxOpts, 'id'>) {
- super({ id: DataAnalysisEnvId, ...opts })
+ private static template = 'Python3-DataAnalysis'
+
+ constructor(opts: Omit<SandboxOpts, 'template'>) {
+ super({ template: DataAnalysis.template, ...opts })
}
static override async create(): Promise<DataAnalysis>;
- static override async create(opts?: Omit<SandboxOpts, 'id'>) {
+ static override async create(opts?: Omit<SandboxOpts, 'template'>) {
return new DataAnalysis({ ...opts })
._open({ timeout: opts?.timeout })
.then(async (sandbox) => {