e2b-dev / E2B

Secure open source cloud runtime for AI apps & AI agents
https://e2b.dev/docs
Apache License 2.0
7.02k stars 458 forks source link

Experimental stateless JavaScript SDK #313

Closed jakubno closed 4 months ago

jakubno commented 9 months ago

Experimental stateless JavaScript SDK for E2B

The stateless version of our SDK allows you to spin up a sandbox from a client without managing the sandbox lifecycle on the client. The sandbox stays stateful though.

Installation

npm i e2b@0.12.2-stateless-sdk.6

Usage

import * as e2b from 'e2b'

const apiKey = '...'
const lifetime = 60_000 * 5 // 5 minutes

console.log('> Creating sandbox...')
const sandboxID = await e2b.experimental_stateless.create({ apiKey }, {
  keepAliveFor: lifetime,
})
console.log('...sandbox created, sandbox id -', sandboxID)

const cmd = 'echo hello world'
console.log(`\n> Executing command "${cmd}"...`)
// This will stream command's stdout and stderr
await e2b.experimental_stateless.exec({
  apiKey,
  sandboxID
}, {
  cmd,
  onStdout: (data) => {
    console.log(data)
  },
  onStderr: (data) => {
    console.error(data)
  },
})
// Or you can wait and get the full output in the `result` object after the command has finished running:
// const result = await e2b.experimental_stateless.exec(...)
console.log('...command finished')

console.log('\n> Uploading file...')
await e2b.experimental_stateless.uploadFile({
  apiKey,
  sandboxID,
}, {
  path: '/tmp/hello.txt',
  content: new TextEncoder().encode('hello world'),
})
console.log('...file uploaded')

console.log('\n> Downloading file...')
const content = await e2b.experimental_stateless.downloadFile({
  apiKey,
  sandboxID,
}, {
  path: '/tmp/hello.txt'
})
console.log('...downloaded file:\n', content.toString())

console.log(`\n> Killing sandbox "${sandboxID}"...`)
await e2b.experimental_stateless.kill({
  apiKey,
  sandboxID,
})
console.log('...sandbox killed')

There are three methods on stateless module:

changeset-bot[bot] commented 9 months ago

⚠️ No Changeset found

Latest commit: 5719c9efbd2c9e226ecb66e9b6a140a311d4f49e

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

ValentaTomas commented 4 months ago

We closed this PR because our new SDK that is in beta incorporates most stateless ideas.