1
1
import { Sandbox as BaseSandbox , InvalidArgumentError } from 'e2b'
2
2
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'
5
16
import { JUPYTER_PORT , DEFAULT_TIMEOUT_MS } from './consts'
6
17
7
18
/**
@@ -29,37 +40,37 @@ export interface RunCodeOpts {
29
40
/**
30
41
* Callback for handling stdout messages.
31
42
*/
32
- onStdout ?: ( output : OutputMessage ) => ( Promise < any > | any ) ,
43
+ onStdout ?: ( output : OutputMessage ) => Promise < any > | any
33
44
/**
34
45
* Callback for handling stderr messages.
35
46
*/
36
- onStderr ?: ( output : OutputMessage ) => ( Promise < any > | any ) ,
47
+ onStderr ?: ( output : OutputMessage ) => Promise < any > | any
37
48
/**
38
49
* Callback for handling the final execution result.
39
50
*/
40
- onResult ?: ( data : Result ) => ( Promise < any > | any ) ,
51
+ onResult ?: ( data : Result ) => Promise < any > | any
41
52
/**
42
53
* Callback for handling the `ExecutionError` object.
43
54
*/
44
- onError ?: ( error : ExecutionError ) => ( Promise < any > | any ) ,
55
+ onError ?: ( error : ExecutionError ) => Promise < any > | any
45
56
/**
46
57
* Custom environment variables for code execution.
47
- *
58
+ *
48
59
* @default {}
49
60
*/
50
- envs ?: Record < string , string > ,
61
+ envs ?: Record < string , string >
51
62
/**
52
63
* Timeout for the code execution in **milliseconds**.
53
- *
64
+ *
54
65
* @default 60_000 // 60 seconds
55
66
*/
56
- timeoutMs ?: number ,
67
+ timeoutMs ?: number
57
68
/**
58
69
* Timeout for the request in **milliseconds**.
59
- *
70
+ *
60
71
* @default 30_000 // 30 seconds
61
72
*/
62
- requestTimeoutMs ?: number ,
73
+ requestTimeoutMs ?: number
63
74
}
64
75
65
76
/**
@@ -68,22 +79,22 @@ export interface RunCodeOpts {
68
79
export interface CreateCodeContextOpts {
69
80
/**
70
81
* Working directory for the context.
71
- *
82
+ *
72
83
* @default /home/user
73
84
*/
74
- cwd ?: string ,
85
+ cwd ?: string
75
86
/**
76
87
* Language for the context.
77
- *
88
+ *
78
89
* @default python
79
90
*/
80
- language ?: string ,
91
+ language ?: string
81
92
/**
82
93
* Timeout for the request in **milliseconds**.
83
- *
94
+ *
84
95
* @default 30_000 // 30 seconds
85
96
*/
86
- requestTimeoutMs ?: number ,
97
+ requestTimeoutMs ?: number
87
98
}
88
99
89
100
/**
@@ -108,65 +119,66 @@ export interface CreateCodeContextOpts {
108
119
* ```
109
120
*/
110
121
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'
112
124
113
125
/**
114
126
* Run the code as Python.
115
- *
127
+ *
116
128
* Specify the `language` or `context` option to run the code as a different language or in a different `Context`.
117
- *
129
+ *
118
130
* You can reference previously defined variables, imports, and functions in the code.
119
131
*
120
132
* @param code code to execute.
121
133
* @param opts options for executing the code.
122
- *
134
+ *
123
135
* @returns `Execution` result object.
124
136
*/
125
137
async runCode (
126
138
code : string ,
127
139
opts ?: RunCodeOpts & {
128
140
/**
129
141
* Language to use for code execution.
130
- *
142
+ *
131
143
* If not defined, the default Python context is used.
132
144
*/
133
- language ?: 'python' ,
134
- } ,
145
+ language ?: 'python'
146
+ }
135
147
) : Promise < Execution >
136
148
/**
137
149
* Run the code for the specified language.
138
- *
150
+ *
139
151
* Specify the `language` or `context` option to run the code as a different language or in a different `Context`.
140
152
* If no language is specified, Python is used.
141
- *
153
+ *
142
154
* You can reference previously defined variables, imports, and functions in the code.
143
155
*
144
156
* @param code code to execute.
145
157
* @param opts options for executing the code.
146
- *
158
+ *
147
159
* @returns `Execution` result object.
148
160
*/
149
161
async runCode (
150
162
code : string ,
151
163
opts ?: RunCodeOpts & {
152
164
/**
153
165
* Language to use for code execution.
154
- *
166
+ *
155
167
* If not defined, the default Python context is used.
156
168
*/
157
- language ?: string ,
158
- } ,
169
+ language ?: string
170
+ }
159
171
) : Promise < Execution >
160
172
/**
161
173
* Runs the code in the specified context, if not specified, the default context is used.
162
- *
174
+ *
163
175
* Specify the `language` or `context` option to run the code as a different language or in a different `Context`.
164
- *
176
+ *
165
177
* You can reference previously defined variables, imports, and functions in the code.
166
178
*
167
179
* @param code code to execute.
168
180
* @param opts options for executing the code
169
- *
181
+ *
170
182
* @returns `Execution` result object
171
183
*/
172
184
async runCode (
@@ -175,35 +187,44 @@ export class Sandbox extends BaseSandbox {
175
187
/**
176
188
* Context to run the code in.
177
189
*/
178
- context ?: Context ,
179
- } ,
190
+ context ?: Context
191
+ }
180
192
) : Promise < Execution >
181
193
async runCode (
182
194
code : string ,
183
195
opts ?: RunCodeOpts & {
184
- language ?: string ,
185
- context ?: Context ,
186
- } ,
196
+ language ?: string
197
+ context ?: Context
198
+ }
187
199
) : Promise < Execution > {
188
200
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
+ )
190
204
}
191
205
192
206
const controller = new AbortController ( )
193
207
194
- const requestTimeout = opts ?. requestTimeoutMs ?? this . connectionConfig . requestTimeoutMs
208
+ const requestTimeout =
209
+ opts ?. requestTimeoutMs ?? this . connectionConfig . requestTimeoutMs
195
210
196
- const reqTimer = requestTimeout ? setTimeout ( ( ) => {
197
- controller . abort ( )
198
- } , requestTimeout )
211
+ const reqTimer = requestTimeout
212
+ ? setTimeout ( ( ) => {
213
+ controller . abort ( )
214
+ } , requestTimeout )
199
215
: undefined
200
216
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
+
201
224
try {
202
225
const res = await fetch ( `${ this . jupyterUrl } /execute` , {
203
226
method : 'POST' ,
204
- headers : {
205
- 'Content-Type' : 'application/json' ,
206
- } ,
227
+ headers,
207
228
body : JSON . stringify ( {
208
229
code,
209
230
context_id : opts ?. context ?. id ,
@@ -220,7 +241,9 @@ export class Sandbox extends BaseSandbox {
220
241
}
221
242
222
243
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
+ )
224
247
}
225
248
226
249
clearTimeout ( reqTimer )
@@ -229,16 +252,22 @@ export class Sandbox extends BaseSandbox {
229
252
230
253
const bodyTimer = bodyTimeout
231
254
? setTimeout ( ( ) => {
232
- controller . abort ( )
233
- } , bodyTimeout )
255
+ controller . abort ( )
256
+ } , bodyTimeout )
234
257
: undefined
235
258
236
259
const execution = new Execution ( )
237
260
238
-
239
261
try {
240
262
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
+ )
242
271
}
243
272
} catch ( error ) {
244
273
throw formatExecutionTimeoutError ( error )
@@ -256,7 +285,7 @@ export class Sandbox extends BaseSandbox {
256
285
* Creates a new context to run code in.
257
286
*
258
287
* @param opts options for creating the context.
259
- *
288
+ *
260
289
* @returns context object.
261
290
*/
262
291
async createCodeContext ( opts ?: CreateCodeContextOpts ) : Promise < Context > {
@@ -265,6 +294,7 @@ export class Sandbox extends BaseSandbox {
265
294
method : 'POST' ,
266
295
headers : {
267
296
'Content-Type' : 'application/json' ,
297
+ ...this . connectionConfig . headers ,
268
298
} ,
269
299
body : JSON . stringify ( {
270
300
language : opts ?. language ,
@@ -286,6 +316,8 @@ export class Sandbox extends BaseSandbox {
286
316
}
287
317
288
318
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
+ ) } `
290
322
}
291
323
}
0 commit comments