Skip to content

armgit / E2B

Public Imported from GitHub
0

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

Fix JS SDK reconnect type

Tomas Valenta9c931a92a8b8
diff --git a/packages/js-sdk/src/sandbox/index.ts b/packages/js-sdk/src/sandbox/index.ts
index 0b8cbbacb..8bbe0fa62 100644
--- a/packages/js-sdk/src/sandbox/index.ts
+++ b/packages/js-sdk/src/sandbox/index.ts
@@ -503,7 +503,7 @@ export class Sandbox extends SandboxConnection {
* const reconnectedSandbox = await Sandbox.reconnect(sandboxID)
* ```
*/
- static async reconnect(sandboxID: string): Promise<Sandbox>;
+ static async reconnect<S extends Sandbox>(sandboxID: string): Promise<S>;
/**
* Reconnects to an existing Sandbox.
* @param opts Sandbox options
@@ -522,8 +522,8 @@ export class Sandbox extends SandboxConnection {
* })
* ```
*/
- static async reconnect(opts: Omit<SandboxOpts, 'id' | 'template'> & { sandboxID: string }): Promise<Sandbox>;
- static async reconnect(sandboxIDorOpts: string | Omit<SandboxOpts, 'id' | 'template'> & { sandboxID: string }) {
+ static async reconnect<S extends Sandbox>(opts: Omit<SandboxOpts, 'id' | 'template'> & { sandboxID: string }): Promise<S>;
+ static async reconnect<S extends Sandbox>(sandboxIDorOpts: string | Omit<SandboxOpts, 'id' | 'template'> & { sandboxID: string }): Promise<S> {
let sandboxID: string
let opts: SandboxOpts
if (typeof sandboxIDorOpts === 'string') {
@@ -539,7 +539,7 @@ export class Sandbox extends SandboxConnection {
const clientID = instanceIDAndClientID[1]
opts.__sandbox = { instanceID, clientID, envID: 'unknown' }
- const sandbox = new Sandbox(opts)
+ const sandbox = new this(opts) as S
await sandbox._open({ timeout: opts?.timeout })
return sandbox
diff --git a/packages/js-sdk/src/templates/dataAnalysis.ts b/packages/js-sdk/src/templates/dataAnalysis.ts
index 2d9515156..0365bb1da 100644
--- a/packages/js-sdk/src/templates/dataAnalysis.ts
+++ b/packages/js-sdk/src/templates/dataAnalysis.ts
@@ -23,7 +23,36 @@ export class DataAnalysis extends Sandbox {
super({ template: DataAnalysis.template, ...opts })
}
+ /**
+ * Reconnects to an existing Sandbox.
+ * @param sandboxID Sandbox ID
+ * @returns Sandbox
+ */
+ static async reconnect<S extends DataAnalysis>(sandboxID: string): Promise<S>;
+ /**
+ * Reconnects to an existing Sandbox with the specified options.
+ * @param opts Sandbox options
+ * @returns Sandbox
+ */
+ static async reconnect<S extends DataAnalysis>(opts: Omit<SandboxOpts, 'id' | 'template'> & { sandboxID: string }): Promise<S>;
+ static async reconnect(sandboxIDorOpts: string | Omit<SandboxOpts, 'id' | 'template'> & { sandboxID: string }) {
+ if (typeof sandboxIDorOpts === 'string') {
+ sandboxIDorOpts = { sandboxID: sandboxIDorOpts }
+ }
+
+ return super.reconnect(sandboxIDorOpts)
+ }
+
+ /**
+ * Creates a new Sandbox from the template.
+ * @returns New Sandbox
+ */
static override async create(): Promise<DataAnalysis>;
+ /**
+ * Creates a new Sandbox from the specified options.
+ * @param opts Sandbox options
+ * @returns New Sandbox
+ */
static override async create(opts: Omit<SandboxOpts, 'template'>): Promise<DataAnalysis>;
static override async create(opts?: Omit<SandboxOpts, 'template'>) {
const sandbox = new DataAnalysis({ ...opts ? opts : {} })
diff --git a/packages/js-sdk/test/run.mjs b/packages/js-sdk/test/run.mjs
index 617409ab1..43e30d3a9 100644
--- a/packages/js-sdk/test/run.mjs
+++ b/packages/js-sdk/test/run.mjs
@@ -1,104 +1,10 @@
-import fs from 'fs'
-import { runCode, Sandbox } from '../dist/index.js'
+import { Sandbox } from '../dist/index.js'
-async function localSandbox() {
- return await Sandbox.create({
- id: 'Nodejs',
- apiKey: process.env.E2B_API_KEY
- // __debug_hostname: 'localhost',
- // __debug_port: 49982,
- // __debug_devEnv: 'local',
- })
-}
+const sandbox = await Sandbox.create({
+ apiKey: process.env.E2B_API_KEY
+ // __debug_hostname: 'localhost',
+ // __debug_port: 49982,
+ // __debug_devEnv: 'local',
+})
-async function main() {
- const sandbox = await localSandbox()
-
- const f = fs.readFileSync('./package.json')
- await sandbox.uploadFile(f, 'package.json')
-
- const content = await sandbox.filesystem.list('/home/user')
- content.forEach(c => console.log(c.name))
- process.exit()
-
- const response = await sandbox.downloadFile('/.dbkenv')
- const text = await response.text()
- console.log('text', text)
- process.exit()
- // const sandbox = await Sandbox.create({
- // id: 'Nodejs',
- // apiKey: process.env.E2B_API_KEY,
- // })
-
- try {
- // const code = 'console.log("Hello World"); console.error("Bum")'
- const code = `
-print("hello")
-raise Exception("err")
-`
- const out = await runCode('Python3', code)
- console.log(out)
- // console.log('stdout', stdout)
- // console.log('stderr', stderr)
-
- // await sandbox.filesystem.write('/code/file', 'test')
- // const content = await sandbox.filesystem.read('/code/file')
- // console.log('content', content)
-
- // const process = await sandbox.process.start({
- // cmd: 'npm init -y',
- // // onExit: () => console.log('exit'),
- // // onStdout: data => console.log(data.line),
- // })
- // const cmd = 'npm i'
-
- // await sandbox.process.start({
- // cmd,
- // onStdout: o => console.log(o),
- // })
-
- // const term = await sandbox.terminal.start({
- // onData: data => console.log(data),
- // onExit: () => console.log('exit'),
- // size: {
- // cols: 9,
- // rows: 9,
- // },
- // cwd: '/code',
- // cmd: 'npm i',
- // })
-
- // term.sendData(`${cmd}\n`)
-
- // const dirWatchers = new Map()
-
- // const w2 = sandbox.filesystem.watchDir('/code/dir')
- // dirWatchers.set('/code/dir', w2)
- // await w2.start()
- // w2.addEventListener(fsevent => {
- // console.log('w2', fsevent)
- // //if (fsevent.operation === FilesystemOperation.Remove) {
- // // // Remove and stop watcher for a dir that got removed.
- // // const dirwatcher = dirWatchers.get(fsevent.path)
- // // if (dirwatcher) {
- // // dirwatcher.stop()
- // // dirWatchers.delete(fsevent.path)
- // // }
- // //}
- // })
-
- // const w3 = sandbox.filesystem.watchDir('/code/dir/subdir')
- // dirWatchers.set('/code/dir/subdir', w3)
- // await w3.start()
- // w3.addEventListener(fsevent => {
- // console.log('w3', fsevent)
- // //if (fsevent.operation === FilesystemOperation.Remove && fsevent.path === '/code/dir/subdir') {
- // // w3.stop()
- // //}
- // })
- } catch (e) {
- console.error('Sandbox error', e)
- }
-}
-
-main()
+await sandbox.close()