Skip to content

Commit aa45123

Browse files
authored
SDK V2 release (#142)
* Update base e2b * Prepare for SDK v2 * Bump e2b * Increase the pytest worker count * Propagate the envs * Rebase * Add changeset * Fix headers * Pass header
1 parent f56a182 commit aa45123

25 files changed

+263
-230
lines changed

.changeset/new-kings-sleep.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@e2b/code-interpreter-python': major
3+
'@e2b/code-interpreter': major
4+
---
5+
6+
SDK v2

.changeset/strange-donkeys-clean.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@e2b/code-interpreter-template': minor
3+
---
4+
5+
Propagate access token

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ Python
6161
```py
6262
from e2b_code_interpreter import Sandbox
6363

64-
with Sandbox() as sandbox:
64+
with Sandbox.create() as sandbox:
6565
sandbox.run_code("x = 1")
6666
execution = sandbox.run_code("x+=1; x")
6767
print(execution.text) # outputs 2

js/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
"vm"
6767
],
6868
"dependencies": {
69-
"e2b": "^1.4.0"
69+
"e2b": "^2.0.1"
7070
},
7171
"engines": {
7272
"node": ">=18"

js/src/sandbox.ts

Lines changed: 86 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
import { Sandbox as BaseSandbox, InvalidArgumentError } from 'e2b'
22

3-
import { Result, Execution, OutputMessage, parseOutput, extractError, ExecutionError } from './messaging'
4-
import { formatExecutionTimeoutError, formatRequestTimeoutError, readLines } from "./utils";
3+
import {
4+
Result,
5+
Execution,
6+
OutputMessage,
7+
parseOutput,
8+
extractError,
9+
ExecutionError,
10+
} from './messaging'
11+
import {
12+
formatExecutionTimeoutError,
13+
formatRequestTimeoutError,
14+
readLines,
15+
} from './utils'
516
import { JUPYTER_PORT, DEFAULT_TIMEOUT_MS } from './consts'
617

718
/**
@@ -29,37 +40,37 @@ export interface RunCodeOpts {
2940
/**
3041
* Callback for handling stdout messages.
3142
*/
32-
onStdout?: (output: OutputMessage) => (Promise<any> | any),
43+
onStdout?: (output: OutputMessage) => Promise<any> | any
3344
/**
3445
* Callback for handling stderr messages.
3546
*/
36-
onStderr?: (output: OutputMessage) => (Promise<any> | any),
47+
onStderr?: (output: OutputMessage) => Promise<any> | any
3748
/**
3849
* Callback for handling the final execution result.
3950
*/
40-
onResult?: (data: Result) => (Promise<any> | any),
51+
onResult?: (data: Result) => Promise<any> | any
4152
/**
4253
* Callback for handling the `ExecutionError` object.
4354
*/
44-
onError?: (error: ExecutionError) => (Promise<any> | any),
55+
onError?: (error: ExecutionError) => Promise<any> | any
4556
/**
4657
* Custom environment variables for code execution.
47-
*
58+
*
4859
* @default {}
4960
*/
50-
envs?: Record<string, string>,
61+
envs?: Record<string, string>
5162
/**
5263
* Timeout for the code execution in **milliseconds**.
53-
*
64+
*
5465
* @default 60_000 // 60 seconds
5566
*/
56-
timeoutMs?: number,
67+
timeoutMs?: number
5768
/**
5869
* Timeout for the request in **milliseconds**.
59-
*
70+
*
6071
* @default 30_000 // 30 seconds
6172
*/
62-
requestTimeoutMs?: number,
73+
requestTimeoutMs?: number
6374
}
6475

6576
/**
@@ -68,22 +79,22 @@ export interface RunCodeOpts {
6879
export interface CreateCodeContextOpts {
6980
/**
7081
* Working directory for the context.
71-
*
82+
*
7283
* @default /home/user
7384
*/
74-
cwd?: string,
85+
cwd?: string
7586
/**
7687
* Language for the context.
77-
*
88+
*
7889
* @default python
7990
*/
80-
language?: string,
91+
language?: string
8192
/**
8293
* Timeout for the request in **milliseconds**.
83-
*
94+
*
8495
* @default 30_000 // 30 seconds
8596
*/
86-
requestTimeoutMs?: number,
97+
requestTimeoutMs?: number
8798
}
8899

89100
/**
@@ -108,65 +119,66 @@ export interface CreateCodeContextOpts {
108119
* ```
109120
*/
110121
export class Sandbox extends BaseSandbox {
111-
protected static override readonly defaultTemplate: string = 'code-interpreter-v1'
122+
protected static override readonly defaultTemplate: string =
123+
'code-interpreter-v1'
112124

113125
/**
114126
* Run the code as Python.
115-
*
127+
*
116128
* Specify the `language` or `context` option to run the code as a different language or in a different `Context`.
117-
*
129+
*
118130
* You can reference previously defined variables, imports, and functions in the code.
119131
*
120132
* @param code code to execute.
121133
* @param opts options for executing the code.
122-
*
134+
*
123135
* @returns `Execution` result object.
124136
*/
125137
async runCode(
126138
code: string,
127139
opts?: RunCodeOpts & {
128140
/**
129141
* Language to use for code execution.
130-
*
142+
*
131143
* If not defined, the default Python context is used.
132144
*/
133-
language?: 'python',
134-
},
145+
language?: 'python'
146+
}
135147
): Promise<Execution>
136148
/**
137149
* Run the code for the specified language.
138-
*
150+
*
139151
* Specify the `language` or `context` option to run the code as a different language or in a different `Context`.
140152
* If no language is specified, Python is used.
141-
*
153+
*
142154
* You can reference previously defined variables, imports, and functions in the code.
143155
*
144156
* @param code code to execute.
145157
* @param opts options for executing the code.
146-
*
158+
*
147159
* @returns `Execution` result object.
148160
*/
149161
async runCode(
150162
code: string,
151163
opts?: RunCodeOpts & {
152164
/**
153165
* Language to use for code execution.
154-
*
166+
*
155167
* If not defined, the default Python context is used.
156168
*/
157-
language?: string,
158-
},
169+
language?: string
170+
}
159171
): Promise<Execution>
160172
/**
161173
* Runs the code in the specified context, if not specified, the default context is used.
162-
*
174+
*
163175
* Specify the `language` or `context` option to run the code as a different language or in a different `Context`.
164-
*
176+
*
165177
* You can reference previously defined variables, imports, and functions in the code.
166178
*
167179
* @param code code to execute.
168180
* @param opts options for executing the code
169-
*
181+
*
170182
* @returns `Execution` result object
171183
*/
172184
async runCode(
@@ -175,35 +187,44 @@ export class Sandbox extends BaseSandbox {
175187
/**
176188
* Context to run the code in.
177189
*/
178-
context?: Context,
179-
},
190+
context?: Context
191+
}
180192
): Promise<Execution>
181193
async runCode(
182194
code: string,
183195
opts?: RunCodeOpts & {
184-
language?: string,
185-
context?: Context,
186-
},
196+
language?: string
197+
context?: Context
198+
}
187199
): Promise<Execution> {
188200
if (opts?.context && opts?.language) {
189-
throw new InvalidArgumentError("You can provide context or language, but not both at the same time.")
201+
throw new InvalidArgumentError(
202+
'You can provide context or language, but not both at the same time.'
203+
)
190204
}
191205

192206
const controller = new AbortController()
193207

194-
const requestTimeout = opts?.requestTimeoutMs ?? this.connectionConfig.requestTimeoutMs
208+
const requestTimeout =
209+
opts?.requestTimeoutMs ?? this.connectionConfig.requestTimeoutMs
195210

196-
const reqTimer = requestTimeout ? setTimeout(() => {
197-
controller.abort()
198-
}, requestTimeout)
211+
const reqTimer = requestTimeout
212+
? setTimeout(() => {
213+
controller.abort()
214+
}, requestTimeout)
199215
: undefined
200216

217+
const headers: Record<string, string> = {
218+
'Content-Type': 'application/json',
219+
}
220+
if (this.envdAccessToken) {
221+
headers['X-Access-Token'] = this.envdAccessToken
222+
}
223+
201224
try {
202225
const res = await fetch(`${this.jupyterUrl}/execute`, {
203226
method: 'POST',
204-
headers: {
205-
'Content-Type': 'application/json',
206-
},
227+
headers,
207228
body: JSON.stringify({
208229
code,
209230
context_id: opts?.context?.id,
@@ -220,7 +241,9 @@ export class Sandbox extends BaseSandbox {
220241
}
221242

222243
if (!res.body) {
223-
throw new Error(`Not response body: ${res.statusText} ${await res?.text()}`)
244+
throw new Error(
245+
`Not response body: ${res.statusText} ${await res?.text()}`
246+
)
224247
}
225248

226249
clearTimeout(reqTimer)
@@ -229,16 +252,22 @@ export class Sandbox extends BaseSandbox {
229252

230253
const bodyTimer = bodyTimeout
231254
? setTimeout(() => {
232-
controller.abort()
233-
}, bodyTimeout)
255+
controller.abort()
256+
}, bodyTimeout)
234257
: undefined
235258

236259
const execution = new Execution()
237260

238-
239261
try {
240262
for await (const chunk of readLines(res.body)) {
241-
await parseOutput(execution, chunk, opts?.onStdout, opts?.onStderr, opts?.onResult, opts?.onError)
263+
await parseOutput(
264+
execution,
265+
chunk,
266+
opts?.onStdout,
267+
opts?.onStderr,
268+
opts?.onResult,
269+
opts?.onError
270+
)
242271
}
243272
} catch (error) {
244273
throw formatExecutionTimeoutError(error)
@@ -256,7 +285,7 @@ export class Sandbox extends BaseSandbox {
256285
* Creates a new context to run code in.
257286
*
258287
* @param opts options for creating the context.
259-
*
288+
*
260289
* @returns context object.
261290
*/
262291
async createCodeContext(opts?: CreateCodeContextOpts): Promise<Context> {
@@ -265,6 +294,7 @@ export class Sandbox extends BaseSandbox {
265294
method: 'POST',
266295
headers: {
267296
'Content-Type': 'application/json',
297+
...this.connectionConfig.headers,
268298
},
269299
body: JSON.stringify({
270300
language: opts?.language,
@@ -286,6 +316,8 @@ export class Sandbox extends BaseSandbox {
286316
}
287317

288318
protected get jupyterUrl(): string {
289-
return `${this.connectionConfig.debug ? 'http' : 'https'}://${this.getHost(JUPYTER_PORT)}`
319+
return `${this.connectionConfig.debug ? 'http' : 'https'}://${this.getHost(
320+
JUPYTER_PORT
321+
)}`
290322
}
291323
}

0 commit comments

Comments
 (0)