Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
SDK download/upload files (#194)
Jakub Novákcd3b156a9332
diff --git a/packages/js-sdk/package.json b/packages/js-sdk/package.jsonindex 9c235d9a9..03d608d89 100644--- a/packages/js-sdk/package.json+++ b/packages/js-sdk/package.json@@ -70,8 +70,11 @@"typescript"],"dependencies": {+ "@types/node-fetch": "^2.6.5","bufferutil": "^4.0.7","cross-fetch": "^3.1.5",+ "form-data": "^4.0.0",+ "node-fetch": "^2.7.0","normalize-path": "^3.0.0","openapi-typescript-fetch": "^1.1.3","platform": "^1.3.6",diff --git a/packages/js-sdk/src/constants.ts b/packages/js-sdk/src/constants.tsindex 91eab2afd..546ba80b0 100644--- a/packages/js-sdk/src/constants.ts+++ b/packages/js-sdk/src/constants.ts@@ -4,5 +4,5 @@ export const WS_RECONNECT_INTERVAL = 600 // 600msexport const TIMEOUT = 60_000 // 60sexport const SESSION_DOMAIN = 'ondevbook.com'-export const WS_PORT = 49982+export const ENVD_PORT = 49982export const WS_ROUTE = '/ws'diff --git a/packages/js-sdk/src/session/index.ts b/packages/js-sdk/src/session/index.tsindex a8f66922b..77a06d023 100644--- a/packages/js-sdk/src/session/index.ts+++ b/packages/js-sdk/src/session/index.ts@@ -1,5 +1,8 @@import normalizePath from 'normalize-path'+import FormData from 'form-data'+import fetch from 'node-fetch'+import { ENVD_PORT } from '../constants'import { components } from '../api'import { id } from '../utils/id'import { createDeferredPromise, formatSettledErrors, withTimeout } from '../utils/promise'@@ -358,6 +361,18 @@ export class Session extends SessionConnection {resolvePath(path, this.cwd, this.logger)}+ /**+ * URL that can be used to download or upload file to the session via a multipart/form-data POST request.+ * This is useful if you're uploading files directly from the browser.+ * The file will be uploaded to the user's home directory with the same name.+ * If a file with the same name already exists, it will be overwritten.+ */+ public get fileURL() {+ const hostname = this.getHostname(this.opts.__debug_port)+ const protocol = this.opts.__debug_devEnv === 'local' ? 'http' : 'https'+ return `${protocol}://${ENVD_PORT}-${hostname}/file`+ }+static async create(opts: SessionOpts) {return new Session(opts).open({ timeout: opts?.timeout }).then(async session => {if (opts.cwd) {@@ -384,4 +399,70 @@ export class Session extends SessionConnection {return this}++ /**+ * Uploads a file to the session.+ * The file will be uploaded to the user's home directory with the same name.+ * If a file with the same name already exists, it will be overwritten.+ *+ * **Use this method only in the non-browser environment.+ * In the browser environment, use the `uploadURL()` method and upload the file driectly via POST multipart/form-data**+ *+ */+ async uploadFile(file: Buffer | Blob, filename: string) {+ const formData = new FormData()+ formData.append('file', file, {+ filename,+ })++ const response = await fetch(this.fileURL, {+ method: 'POST',+ body: formData,+ })++ if (!response.ok) {+ const text = await response.text()+ throw new Error(`Failed to upload file: ${text}`)+ }++ return `/home/user/${filename}`+ }++ async downloadFile(+ remotePath: string,+ format?: 'base64' | 'blob' | 'buffer' | 'arraybuffer' | 'text',+ ) {+ remotePath = encodeURIComponent(remotePath)++ const response = await fetch(`${this.fileURL}?path=${remotePath}`)+ if (!response.ok) {+ const text = await response.text()+ throw new Error(`Failed to download file '${remotePath}': ${text}`)+ }++ let data+ switch (format) {+ case 'base64':+ data = await response.arrayBuffer()+ data = Buffer.from(data).toString('base64')+ break+ case 'blob':+ data = await response.blob()+ break+ case 'buffer':+ data = await response.buffer()+ break+ case 'arraybuffer':+ data = await response.arrayBuffer()+ break+ case 'text':+ data = await response.text()+ break+ default:+ data = await response.arrayBuffer()+ break+ }++ return data+ }}diff --git a/packages/js-sdk/src/session/sessionConnection.ts b/packages/js-sdk/src/session/sessionConnection.tsindex 0defc0c36..1ae60f6d1 100644--- a/packages/js-sdk/src/session/sessionConnection.ts+++ b/packages/js-sdk/src/session/sessionConnection.ts@@ -4,7 +4,7 @@ import api, { components } from '../api'import {SESSION_DOMAIN,SESSION_REFRESH_PERIOD,- WS_PORT,+ ENVD_PORT,WS_RECONNECT_INTERVAL,WS_ROUTE,} from '../constants'@@ -208,7 +208,7 @@ export class SessionConnection {}}- const hostname = this.getHostname(this.opts.__debug_port || WS_PORT)+ const hostname = this.getHostname(this.opts.__debug_port || ENVD_PORT)if (!hostname) {throw new Error("Cannot get session's hostname")diff --git a/packages/js-sdk/test/fileUploadDownload.test.mjs b/packages/js-sdk/test/fileUploadDownload.test.mjsnew file mode 100644index 000000000..251f4a07b--- /dev/null+++ b/packages/js-sdk/test/fileUploadDownload.test.mjs@@ -0,0 +1,20 @@+import { Session } from '../src'+import { expect, test } from 'vitest'+import * as fs from 'node:fs'+import * as path from 'node:path'++test('upload and download file', async () => {+ const session = await Session.create({ id: 'Nodejs' })++ const localFile = fs.readFileSync(path.join(__dirname, '/assets/video.webm'))++ const uploadedFilePath = await session.uploadFile(localFile, 'video.webm')++ const ls = await session.filesystem.list('/home/user')+ expect(ls.map(x => x.name)).contains('video.webm')++ const downloadedFile = await session.downloadFile(uploadedFilePath, 'buffer')+ expect(localFile).toEqual(downloadedFile)++ await session.close()+})diff --git a/packages/js-sdk/test/filesystem.test.mjs b/packages/js-sdk/test/filesystem.test.mjsindex 223d7a741..ada63e0a0 100644--- a/packages/js-sdk/test/filesystem.test.mjs+++ b/packages/js-sdk/test/filesystem.test.mjs@@ -1,10 +1,10 @@-import {Session} from '../src'-import {expect, test} from 'vitest'+import { Session } from '../src'+import { expect, test } from 'vitest'import * as fs from 'node:fs'import * as path from 'node:path'test('list files', async () => {- const session = await Session.create({id: 'Nodejs'})+ const session = await Session.create({ id: 'Nodejs' })await session.filesystem.makeDir('/test/new')const ls = await session.filesystem.list('/test')@@ -13,9 +13,8 @@ test('list files', async () => {await session.close()})-test('create file', async () => {- const session = await Session.create({id: 'Nodejs'})+ const session = await Session.create({ id: 'Nodejs' })await session.filesystem.makeDir('/test')await session.filesystem.write('/test/test.txt', 'Hello World!')@@ -26,7 +25,7 @@ test('create file', async () => {})test('read and write', async () => {- const session = await Session.create({id: 'Nodejs'})+ const session = await Session.create({ id: 'Nodejs' })// Stringawait session.filesystem.write('/tmp/test.txt', 'Hello World!')@@ -43,7 +42,7 @@ test('read and write', async () => {})test('list delete files', async () => {- const session = await Session.create({id: 'Nodejs'})+ const session = await Session.create({ id: 'Nodejs' })await session.filesystem.makeDir('/test/new')let ls = await session.filesystem.list('/test')@@ -58,7 +57,7 @@ test('list delete files', async () => {})test('watch dir', async () => {- const session = new Session({id: 'Nodejs'})+ const session = new Session({ id: 'Nodejs' })await session.open()await session.filesystem.write('/tmp/test.txt', 'Hello')diff --git a/packages/js-sdk/test/run.mjs b/packages/js-sdk/test/run.mjsindex 963415e1b..97249a399 100644--- a/packages/js-sdk/test/run.mjs+++ b/packages/js-sdk/test/run.mjs@@ -1,6 +1,30 @@-import { runCode } from '../dist/cjs/index.js'+import fs from 'fs'+import { Session, runCode } from '../dist/cjs/index.js'++async function localSession() {+ return await Session.create({+ id: 'Nodejs',+ apiKey: process.env.E2B_API_KEY,+ // __debug_hostname: 'localhost',+ // __debug_port: 49982,+ // __debug_devEnv: 'local',+ })+}async function main() {+ const session = await localSession()++ const f = fs.readFileSync('./package.json')+ await session.uploadFile(f, 'package.json')++ const content = await session.filesystem.list('/home/user')+ content.forEach(c => console.log(c.name))+ process.exit()++ const response = await session.downloadFile('/.dbkenv')+ const text = await response.text()+ console.log('text', text)+ process.exit()// const session = await Session.create({// id: 'Nodejs',// apiKey: process.env.E2B_API_KEY,diff --git a/packages/python-sdk/e2b/constants.py b/packages/python-sdk/e2b/constants.pyindex 710ddde2d..def45a5b5 100644--- a/packages/python-sdk/e2b/constants.py+++ b/packages/python-sdk/e2b/constants.py@@ -3,5 +3,5 @@ WS_RECONNECT_INTERVAL = 6 # secondsTIMEOUT = 60SESSION_DOMAIN = "ondevbook.com"-WS_PORT = 49982+ENVD_PORT = 49982WS_ROUTE = "/ws"diff --git a/packages/python-sdk/e2b/session/main.py b/packages/python-sdk/e2b/session/main.pyindex d4ef364ef..0c34ff448 100644--- a/packages/python-sdk/e2b/session/main.py+++ b/packages/python-sdk/e2b/session/main.py@@ -1,10 +1,16 @@import asyncioimport logging-from typing import Any, Callable, List, Literal, Optional, Union+import requests+import urllib.parse+from os import path+from typing import Any, Callable, List, Literal, Optional, Union, IOfrom async_timeout import timeout as async_timeout-from e2b.constants import TIMEOUT+from e2b.constants import (+ TIMEOUT,+ ENVD_PORT,+)from e2b.session.code_snippet import CodeSnippetManager, OpenPortfrom e2b.session.env_vars import EnvVarsfrom e2b.session.filesystem import FilesystemManager, SyncFilesystemManager@@ -147,6 +153,47 @@ class Session(SessionConnection):await super().close()await self._close()+ def file_url(self) -> str:+ """+ Returns a URL that can be used to upload files to the session via a multipart/form-data POST request.+ This is useful if you're uploading files directly from the browser.+ The file will be uploaded to the user's home directory with the same name.+ If a file with the same name already exists, it will be overwritten.+ """+ hostname = self.get_hostname(self._debug_port)+ protocol = "http" if self._debug_dev_env == "local" else "https"+ return f"{protocol}://{ENVD_PORT}-{hostname}/file"++ def upload_file(self, file: IO) -> str:+ """+ Uploads a file to the session.+ The file will be uploaded to the user's home (`/home/user`) directory with the same name.+ If a file with the same name already exists, it will be overwritten.++ :param file: The file to upload+ """+ files = {'file': file}+ r = requests.post(self.file_url(), files=files)+ if r.status_code != 200:+ raise Exception(f"Failed to upload file: {r.reason} {r.text}")++ filename = path.basename(file.name)+ return f"/home/user/{filename}"++ def download_file(self, remote_path: str) -> bytes:+ """+ Downloads a file from the session and returns it's content as bytes.++ :param remote_path: The path of the file to download+ """+ encoded_path = urllib.parse.quote(remote_path)+ url = f"{self.file_url()}?path={encoded_path}"+ r = requests.get(url)++ if r.status_code != 200:+ raise Exception(f"Failed to download file '{remote_path}'. {r.reason} {r.text}")+ return r.content+async def __aenter__(self):await self.open()return selfdiff --git a/packages/python-sdk/e2b/session/session_connection.py b/packages/python-sdk/e2b/session/session_connection.pyindex 6ebf60031..a1d28249d 100644--- a/packages/python-sdk/e2b/session/session_connection.py+++ b/packages/python-sdk/e2b/session/session_connection.py@@ -13,7 +13,7 @@ from e2b.constants import (SESSION_DOMAIN,SESSION_REFRESH_PERIOD,TIMEOUT,- WS_PORT,+ ENVD_PORT,WS_ROUTE,)from e2b.session.env_vars import EnvVars@@ -204,7 +204,7 @@ class SessionConnection:await self._close()raise e- hostname = self.get_hostname(self._debug_port or WS_PORT)+ hostname = self.get_hostname(self._debug_port or ENVD_PORT)protocol = "ws" if self._debug_dev_env == "local" else "wss"session_url = f"{protocol}://{hostname}{WS_ROUTE}"diff --git a/packages/python-sdk/example.py b/packages/python-sdk/example.pyindex bf8d71f1f..475e0661f 100644--- a/packages/python-sdk/example.py+++ b/packages/python-sdk/example.py@@ -14,9 +14,19 @@ logging.basicConfig(level=logging.ERROR)async def main():- s = Session("Nodejs", api_key=E2B_API_KEY)+ s = Session(+ id="YI58BPyX5KrK",+ api_key=E2B_API_KEY,+ _debug_hostname="localhost",+ _debug_port=49982,+ _debug_dev_env="local",+ )await s.open()+ s.upload_file(file=open("results.csv", "rb"))++ exit()+a = await s.process.start("ls -la /var ")await aprint(a.stdout)diff --git a/packages/python-sdk/poetry.lock b/packages/python-sdk/poetry.lockindex 5c9af5d57..360923764 100644--- a/packages/python-sdk/poetry.lock+++ b/packages/python-sdk/poetry.lock@@ -223,6 +223,17 @@ d = ["aiohttp (>=3.7.4)"]jupyter = ["ipython (>=7.8.0)", "tokenize-rt (>=3.2.0)"]uvloop = ["uvloop (>=0.15.2)"]+[[package]]+name = "certifi"+version = "2023.7.22"+description = "Python package for providing Mozilla's CA Bundle."+optional = false+python-versions = ">=3.6"+files = [+ {file = "certifi-2023.7.22-py3-none-any.whl", hash = "sha256:92d6037539857d8206b8f6ae472e8b77db8058fec5937a1ef3f54304089edbb9"},+ {file = "certifi-2023.7.22.tar.gz", hash = "sha256:539cc1d13202e33ca466e88b2807e29f4c13049d6d87031a3c110744495cb082"},+]+[[package]]name = "charset-normalizer"version = "3.2.0"@@ -870,6 +881,27 @@ files = [[package.extras]cli = ["click (>=5.0)"]+[[package]]+name = "requests"+version = "2.31.0"+description = "Python HTTP for Humans."+optional = false+python-versions = ">=3.7"+files = [+ {file = "requests-2.31.0-py3-none-any.whl", hash = "sha256:58cd2187c01e70e6e26505bca751777aa9f2ee0b7f4300988b709f44e013003f"},+ {file = "requests-2.31.0.tar.gz", hash = "sha256:942c5a758f98d790eaed1a29cb6eefc7ffb0d1cf7af05c3d2791656dbd6ad1e1"},+]++[package.dependencies]+certifi = ">=2017.4.17"+charset-normalizer = ">=2,<4"+idna = ">=2.5,<4"+urllib3 = ">=1.21.1,<3"++[package.extras]+socks = ["PySocks (>=1.5.6,!=1.5.7)"]+use-chardet-on-py3 = ["chardet (>=3.0.2,<6)"]+[[package]]name = "six"version = "1.16.0"@@ -1089,4 +1121,4 @@ multidict = ">=4.0"[metadata]lock-version = "2.0"python-versions = "^3.8"-content-hash = "b91f1d0c814873b8713ba3ad427a53654ab44ff021a016991e94e6d88f74b81c"+content-hash = "913774020976e4ff7cdb5a34d28c893ee0be9f4c723c05880aefd290f9cbc485"diff --git a/packages/python-sdk/pyproject.toml b/packages/python-sdk/pyproject.tomlindex 65d023231..3fefb53b5 100644--- a/packages/python-sdk/pyproject.toml+++ b/packages/python-sdk/pyproject.toml@@ -21,6 +21,7 @@ websockets = "^11.0.3"jsonrpcclient = "^4.0.3"janus = "^1.0.0"async-timeout = "^4.0.3"+requests = "^2.31.0"[tool.poetry.group.dev.dependencies]black = "^23.7.0"diff --git a/packages/python-sdk/tests/test_file_download.py b/packages/python-sdk/tests/test_file_download.pynew file mode 100644index 000000000..5044e4920--- /dev/null+++ b/packages/python-sdk/tests/test_file_download.py@@ -0,0 +1,26 @@+import filecmp+from os import path+from e2b import Session++async def test_download():+ file_name = "video.webm"+ local_dir = "tests/assets"++ local_path = path.join(local_dir, file_name)++ session = await Session.create("Nodejs")++ # Upload the file first (it's uploaded to /home/user)+ with open(local_path, "rb") as f:+ uploaded_file_path = session.upload_file(file=f)++ # Download the file back and save it in the local filesystem+ file_content = session.download_file(uploaded_file_path)+ with open(path.join(local_dir, "video-downloaded.webm"), 'wb') as f:+ f.write(file_content)+++ # Compare the downloaded file with the original+ assert filecmp.cmp(local_path, path.join(local_dir, "video-downloaded.webm"))++ await session.close()diff --git a/packages/python-sdk/tests/test_file_upload.py b/packages/python-sdk/tests/test_file_upload.pynew file mode 100644index 000000000..f0b787ce9--- /dev/null+++ b/packages/python-sdk/tests/test_file_upload.py@@ -0,0 +1,16 @@+from os import path+from e2b import Session++async def test_file_upload():+ file_name = "video.webm"+ local_dir = "tests/assets"+ filepath = path.join(local_dir, file_name)++ session = await Session.create("Nodejs")+ with open(filepath, "rb") as f:+ session.upload_file(file=f)++ files = await session.filesystem.list("/home/user")+ assert file_name in [x.name for x in files]++ await session.close()diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex d65255764..d3deff50c 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -457,12 +457,21 @@ importers:packages/js-sdk:dependencies:+ '@types/node-fetch':+ specifier: ^2.6.5+ version: 2.6.5bufferutil:specifier: ^4.0.7version: 4.0.7cross-fetch:specifier: ^3.1.5version: 3.1.8+ form-data:+ specifier: ^4.0.0+ version: 4.0.0+ node-fetch:+ specifier: ^2.7.0+ version: 2.7.0normalize-path:specifier: ^3.0.0version: 3.0.0@@ -3172,7 +3181,7 @@ packages:/@types/[email protected]:resolution: {integrity: sha512-BQ5aZNSCpj7D6K2ksrRCTmKRLEpnPvWDiLPfoGyhZ++8YtiK9d/3DBKPJgry359X/P1PfruyYwvnvwFjuEiEIg==}dependencies:- '@types/node': 20.8.0+ '@types/node': 18.17.18dev: false/@types/[email protected]:@@ -3205,9 +3214,8 @@ packages:/@types/[email protected]:resolution: {integrity: sha512-OZsUlr2nxvkqUFLSaY2ZbA+P1q22q+KrlxWOn/38RX+u5kTkYL2mTujEpzUhGkS+K/QCYp9oagfXG39XOzyySg==}dependencies:- '@types/node': 20.8.0+ '@types/node': 18.17.18form-data: 4.0.0- dev: true/@types/[email protected]:resolution: {integrity: sha512-J8xLz7q2OFulZ2cyGTLE1TbbZcjpno7FaN6zdJNrgAdrJ+DZzh/uFR6YrTb4C+nXakvud8Q4+rbhoIWlYQbUFQ==}@@ -3219,7 +3227,6 @@ packages:/@types/[email protected]:resolution: {integrity: sha512-/4QOuy3ZpV7Ya1GTRz5CYSz3DgkKpyUptXuQ5PPce7uuyJAOR7r9FhkmxJfvcNUXyklbC63a+YvB3jxy7s9ngw==}- dev: true/@types/[email protected]:resolution: {integrity: sha512-HksnYH4Ljr4VQgEy2lTStbCKv/P590tmPe5HqOnv9Gprffgv5WXAY+Y5Gqniu0GGqeTCUdBnzC3QSrzPkBkAMA==}@@ -3301,7 +3308,7 @@ packages:/@types/[email protected]:resolution: {integrity: sha512-85Y2BjiufFzaMIlvJDvTTB8Fxl2xfLo4HgmHzVBz08w4wDePCTjYw66PdrolO0kzli3yam/YCgRufyo1DdQVTA==}dependencies:- '@types/node': 20.8.0+ '@types/node': 18.17.18dev: false/@types/[email protected]:@@ -3320,13 +3327,13 @@ packages:/@types/[email protected]:resolution: {integrity: sha512-if3mugZfjVkXOMZdFjIHySxY13r6GXPpyOlsDmLffvyI7tLz9wXE8BFjNivXsvUeyJ1KNlOpfLnag+ISmxgxPw==}dependencies:- '@types/node': 20.8.0+ '@types/node': 18.17.18dev: false/@types/[email protected]:resolution: {integrity: sha512-LpKpmb7FGevYgXnBXYs6HWnmiFyVG07Pt1cnbgM1IhEacITTiUaBXXvOR3Y50ksaJWGSfhbEvQFivQEFGCC55w==}dependencies:- '@types/node': 20.8.0+ '@types/node': 18.17.18dev: true/@types/[email protected]:@@ -3349,7 +3356,7 @@ packages:/@types/[email protected]:resolution: {integrity: sha512-JXkliwz93B2cMWOI1ukElQBPN88vMg3CruvW4KVSKpflt3NyNCJImnhIuB/f97rG7kakqRJGFiwkA895Kn02Dg==}dependencies:- '@types/node': 20.8.0+ '@types/node': 18.17.18dev: false/@typescript-eslint/[email protected](@typescript-eslint/[email protected])([email protected])([email protected]):@@ -7059,7 +7066,7 @@ packages:resolution: {integrity: sha512-7vuh85V5cdDofPyxn58nrPjBktZo0u9x1g8WtjQol+jZDaE+fhN+cIvTj11GndBnMnyfrUOG1sZQxCdjKh+DKg==}engines: {node: '>= 10.13.0'}dependencies:- '@types/node': 20.8.0+ '@types/node': 18.17.18merge-stream: 2.0.0supports-color: 8.1.1dev: false