Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
Add .fromBunImage and .bunInstall to the SDK (#993)
Mish Ushakove8576a51e6ee
diff --git a/.changeset/silly-lions-buy.md b/.changeset/silly-lions-buy.mdnew file mode 100644index 000000000..826e9e189--- /dev/null+++ b/.changeset/silly-lions-buy.md@@ -0,0 +1,6 @@+---+'@e2b/python-sdk': minor+'e2b': minor+---++add from bun image and bun installdiff --git a/packages/js-sdk/src/template/index.ts b/packages/js-sdk/src/template/index.tsindex 842c9745f..a207f9441 100644--- a/packages/js-sdk/src/template/index.ts+++ b/packages/js-sdk/src/template/index.ts@@ -138,6 +138,10 @@ export class TemplateBasereturn this.fromImage(`node:${variant}`)}+ fromBunImage(variant: string = 'latest'): TemplateBuilder {+ return this.fromImage(`oven/bun:${variant}`)+ }+fromBaseImage(): TemplateBuilder {return this.fromImage(this.defaultBaseImage)}@@ -448,7 +452,7 @@ export class TemplateBasenpmInstall(packages?: string | string[],- options?: { g?: boolean }+ options?: { g?: boolean; dev?: boolean }): TemplateBuilder {const args = ['npm', 'install']const packageList = packages@@ -459,6 +463,36 @@ export class TemplateBaseif (options?.g) {args.push('-g')}+ if (options?.dev) {+ args.push('--save-dev')+ }+ if (packageList) {+ args.push(...packageList)+ }++ return this.runInNewStackTraceContext(() =>+ this.runCmd(args.join(' '), {+ user: options?.g ? 'root' : undefined,+ })+ )+ }++ bunInstall(+ packages?: string | string[],+ options?: { g?: boolean; dev?: boolean }+ ): TemplateBuilder {+ const args = ['bun', 'install']+ const packageList = packages+ ? Array.isArray(packages)+ ? packages+ : [packages]+ : undefined+ if (options?.g) {+ args.push('-g')+ }+ if (options?.dev) {+ args.push('--dev')+ }if (packageList) {args.push(...packageList)}diff --git a/packages/js-sdk/src/template/types.ts b/packages/js-sdk/src/template/types.tsindex 621a56ec0..0529d366d 100644--- a/packages/js-sdk/src/template/types.ts+++ b/packages/js-sdk/src/template/types.ts@@ -150,6 +150,17 @@ export interface TemplateFromImage {*/fromNodeImage(variant?: string): TemplateBuilder+ /**+ * Start from a Bun-based Docker image.+ * @param variant Bun variant (default: 'latest')+ *+ * @example+ * ```ts+ * Template().fromBunImage('1.3')+ * ```+ */+ fromBunImage(variant?: string): TemplateBuilder+/*** Start from E2B's default base image (e2bdev/base:latest).*@@ -454,13 +465,32 @@ export interface TemplateBuilder {* ```ts* template.npmInstall('express')* template.npmInstall(['lodash', 'axios'])- * template.npmInstall('typescript', { g: true })+ * template.npmInstall('tsx', { g: true })+ * template.npmInstall('typescript', { dev: true })* template.npmInstall() // Installs from package.json* ```*/npmInstall(packages?: string | string[],- options?: { g?: boolean }+ options?: { g?: boolean; dev?: boolean }+ ): TemplateBuilder+ /**+ * Install Bun packages using bun.+ * @param packages Package name(s) or undefined for package.json+ * @param options Install options+ *+ * @example+ * ```ts+ * template.bunInstall('express')+ * template.bunInstall(['lodash', 'axios'])+ * template.bunInstall('tsx', { g: true })+ * template.bunInstall('typescript', { dev: true })+ * template.bunInstall() // Installs from package.json+ * ```+ */+ bunInstall(+ packages?: string | string[],+ options?: { g?: boolean; dev?: boolean }): TemplateBuilder/**diff --git a/packages/js-sdk/tests/template/methods/bunInstall.test.ts b/packages/js-sdk/tests/template/methods/bunInstall.test.tsnew file mode 100644index 000000000..3c9e15d99--- /dev/null+++ b/packages/js-sdk/tests/template/methods/bunInstall.test.ts@@ -0,0 +1,26 @@+import { Template } from '../../../src'+import { buildTemplateTest } from '../../setup'++buildTemplateTest('bun install', async ({ buildTemplate }) => {+ const template = Template()+ .fromBunImage('1.3')+ .bunInstall(['lodash', 'axios'])++ await buildTemplate(template)+})++buildTemplateTest('bun install global', async ({ buildTemplate }) => {+ const template = Template()+ .fromBunImage('1.3')+ .bunInstall(['tsx'], { g: true })++ await buildTemplate(template)+})++buildTemplateTest('bun install dev', async ({ buildTemplate }) => {+ const template = Template()+ .fromBunImage('1.3')+ .bunInstall(['typescript'], { dev: true })++ await buildTemplate(template)+})diff --git a/packages/js-sdk/tests/template/methods/fromMethods.test.ts b/packages/js-sdk/tests/template/methods/fromMethods.test.tsindex 3abd3c24a..a6cb81d7c 100644--- a/packages/js-sdk/tests/template/methods/fromMethods.test.ts+++ b/packages/js-sdk/tests/template/methods/fromMethods.test.ts@@ -64,6 +64,16 @@ buildTemplateTest('fromNodeImage with variant', async ({ buildTemplate }) => {await buildTemplate(template)})+buildTemplateTest('fromBunImage', async ({ buildTemplate }) => {+ const template = Template().fromBunImage()+ await buildTemplate(template)+})++buildTemplateTest('fromBunImage with variant', async ({ buildTemplate }) => {+ const template = Template().fromBunImage('1.3')+ await buildTemplate(template)+})+buildTemplateTest('fromImage', async ({ buildTemplate }) => {const template = Template().fromImage('ubuntu:22.04')await buildTemplate(template)diff --git a/packages/js-sdk/tests/template/methods/npmInstall.test.ts b/packages/js-sdk/tests/template/methods/npmInstall.test.tsindex c633e818e..fe043dd4e 100644--- a/packages/js-sdk/tests/template/methods/npmInstall.test.ts+++ b/packages/js-sdk/tests/template/methods/npmInstall.test.ts@@ -2,7 +2,9 @@ import { Template } from '../../../src'import { buildTemplateTest } from '../../setup'buildTemplateTest('npm install', async ({ buildTemplate }) => {- const template = Template().fromNodeImage('24').npmInstall(['lodash', 'ms'])+ const template = Template()+ .fromNodeImage('24')+ .npmInstall(['lodash', 'axios'])await buildTemplate(template)})@@ -10,7 +12,15 @@ buildTemplateTest('npm install', async ({ buildTemplate }) => {buildTemplateTest('npm install global', async ({ buildTemplate }) => {const template = Template().fromNodeImage('24')- .npmInstall(['lodash', 'ms'], { g: true })+ .npmInstall(['tsx'], { g: true })++ await buildTemplate(template)+})++buildTemplateTest('npm install dev', async ({ buildTemplate }) => {+ const template = Template()+ .fromNodeImage('24')+ .npmInstall(['typescript'], { dev: true })await buildTemplate(template)})diff --git a/packages/python-sdk/e2b/template/main.py b/packages/python-sdk/e2b/template/main.pyindex 9648f9845..54a03c792 100644--- a/packages/python-sdk/e2b/template/main.py+++ b/packages/python-sdk/e2b/template/main.py@@ -365,12 +365,14 @@ class TemplateBuilder:self,packages: Optional[Union[str, List[str]]] = None,g: Optional[bool] = False,+ dev: Optional[bool] = False,) -> "TemplateBuilder":"""Install Node.js packages using npm.:param packages: Package name(s) to install. If None, installs from package.json:param g: Install packages globally+ :param dev: Install packages as dev dependencies:return: `TemplateBuilder` class@@ -388,6 +390,47 @@ class TemplateBuilder:args = ["npm", "install"]if g:args.append("-g")+ if dev:+ args.append("--save-dev")+ if packages:+ args.extend(packages)++ return self._template._run_in_new_stack_trace_context(+ lambda: self.run_cmd(" ".join(args), user="root" if g else None)+ )++ def bun_install(+ self,+ packages: Optional[Union[str, List[str]]] = None,+ g: Optional[bool] = False,+ dev: Optional[bool] = False,+ ) -> "TemplateBuilder":+ """+ Install Bun packages using bun.++ :param packages: Package name(s) to install. If None, installs from package.json+ :param g: Install packages globally+ :param dev: Install packages as dev dependencies++ :return: `TemplateBuilder` class++ Example+ ```python+ template.bun_install('express')+ template.bun_install(['lodash', 'axios'])+ template.bun_install('tsx', g=True)+ template.bun_install('typescript', dev=True)+ template.bun_install() // Installs from package.json+ ```+ """+ if isinstance(packages, str):+ packages = [packages]++ args = ["bun", "install"]+ if g:+ args.append("-g")+ if dev:+ args.append("--dev")if packages:args.extend(packages)@@ -924,6 +967,18 @@ class TemplateBase:lambda: self.from_image(f"node:{variant}"))+ def from_bun_image(self, variant: str = "latest") -> TemplateBuilder:+ """+ Start template from a Bun base image.++ :param variant: Bun image variant (default: 'latest')++ :return: `TemplateBuilder` class+ """+ return self._run_in_new_stack_trace_context(+ lambda: self.from_image(f"oven/bun:{variant}")+ )+def from_base_image(self) -> TemplateBuilder:"""Start template from the E2B base image (e2bdev/base:latest).diff --git a/packages/python-sdk/tests/async/template_async/methods/test_bun_install.py b/packages/python-sdk/tests/async/template_async/methods/test_bun_install.pynew file mode 100644index 000000000..d525c9fcf--- /dev/null+++ b/packages/python-sdk/tests/async/template_async/methods/test_bun_install.py@@ -0,0 +1,26 @@+import pytest++from e2b import AsyncTemplate++[email protected]_debug()+async def test_bun_install(async_build):+ template = AsyncTemplate().from_bun_image("1.3").bun_install(["lodash", "axios"])++ await async_build(template)++[email protected]_debug()+async def test_bun_install_global(async_build):+ template = AsyncTemplate().from_bun_image("1.3").bun_install(["tsx"], g=True)++ await async_build(template)++[email protected]_debug()+async def test_bun_install_dev(async_build):+ template = (+ AsyncTemplate().from_bun_image("1.3").bun_install(["typescript"], dev=True)+ )++ await async_build(template)diff --git a/packages/python-sdk/tests/async/template_async/methods/test_from_methods.py b/packages/python-sdk/tests/async/template_async/methods/test_from_methods.pyindex 60507b9bc..85370913b 100644--- a/packages/python-sdk/tests/async/template_async/methods/test_from_methods.py+++ b/packages/python-sdk/tests/async/template_async/methods/test_from_methods.py@@ -70,6 +70,18 @@ async def test_from_node_image(async_build):await async_build(template)[email protected]_debug()+async def test_from_bun_image(async_build):+ template = AsyncTemplate().from_bun_image()+ await async_build(template)++[email protected]_debug()+async def test_from_bun_image_with_variant(async_build):+ template = AsyncTemplate().from_bun_image("1.3")+ await async_build(template)++@pytest.mark.skip_debug()async def test_from_node_image_with_variant(async_build):template = AsyncTemplate().from_node_image("24")diff --git a/packages/python-sdk/tests/async/template_async/methods/test_npm_install.py b/packages/python-sdk/tests/async/template_async/methods/test_npm_install.pyindex 3d4e296e5..00ce39302 100644--- a/packages/python-sdk/tests/async/template_async/methods/test_npm_install.py+++ b/packages/python-sdk/tests/async/template_async/methods/test_npm_install.py@@ -5,15 +5,22 @@ from e2b import AsyncTemplate@pytest.mark.skip_debug()async def test_npm_install(async_build):- template = AsyncTemplate().from_node_image("24").npm_install(["lodash", "ms"])+ template = AsyncTemplate().from_node_image("24").npm_install(["lodash", "axios"])await async_build(template)@pytest.mark.skip_debug()async def test_npm_install_global(async_build):+ template = AsyncTemplate().from_node_image("24").npm_install(["tsx"], g=True)++ await async_build(template)++[email protected]_debug()+async def test_npm_install_dev(async_build):template = (- AsyncTemplate().from_node_image("24").npm_install(["lodash", "ms"], g=True)+ AsyncTemplate().from_node_image("24").npm_install(["typescript"], dev=True))await async_build(template)diff --git a/packages/python-sdk/tests/sync/template_sync/methods/test_bun_install.py b/packages/python-sdk/tests/sync/template_sync/methods/test_bun_install.pynew file mode 100644index 000000000..6c402ab99--- /dev/null+++ b/packages/python-sdk/tests/sync/template_sync/methods/test_bun_install.py@@ -0,0 +1,24 @@+import pytest++from e2b import Template++[email protected]_debug()+def test_bun_install(build):+ template = Template().from_bun_image("1.3").bun_install(["lodash", "axios"])++ build(template)++[email protected]_debug()+def test_bun_install_global(build):+ template = Template().from_bun_image("1.3").bun_install(["tsx"], g=True)++ build(template)++[email protected]_debug()+def test_bun_install_dev(build):+ template = Template().from_bun_image("1.3").bun_install(["typescript"], dev=True)++ build(template)diff --git a/packages/python-sdk/tests/sync/template_sync/methods/test_from_methods.py b/packages/python-sdk/tests/sync/template_sync/methods/test_from_methods.pyindex 58759dfe2..b89c8a041 100644--- a/packages/python-sdk/tests/sync/template_sync/methods/test_from_methods.py+++ b/packages/python-sdk/tests/sync/template_sync/methods/test_from_methods.py@@ -77,6 +77,18 @@ def test_from_node_image_with_variant(build):build(template)[email protected]_debug()+def test_from_bun_image(build):+ template = Template().from_bun_image()+ build(template)++[email protected]_debug()+def test_from_bun_image_with_variant(build):+ template = Template().from_bun_image("1.3")+ build(template)++@pytest.mark.skip_debug()def test_from_image(build):template = Template().from_image("ubuntu:22.04")diff --git a/packages/python-sdk/tests/sync/template_sync/methods/test_npm_install.py b/packages/python-sdk/tests/sync/template_sync/methods/test_npm_install.pyindex e4ca45b35..f1b285c44 100644--- a/packages/python-sdk/tests/sync/template_sync/methods/test_npm_install.py+++ b/packages/python-sdk/tests/sync/template_sync/methods/test_npm_install.py@@ -5,13 +5,20 @@ from e2b import Template@pytest.mark.skip_debug()def test_npm_install(build):- template = Template().from_node_image("24").npm_install(["lodash", "ms"])+ template = Template().from_node_image("24").npm_install(["lodash", "axios"])build(template)@pytest.mark.skip_debug()def test_npm_install_global(build):- template = Template().from_node_image("24").npm_install(["lodash", "ms"], g=True)+ template = Template().from_node_image("24").npm_install(["tsx"], g=True)++ build(template)++[email protected]_debug()+def test_npm_install_dev(build):+ template = Template().from_node_image("24").npm_install(["typescript"], dev=True)build(template)