11import { Sandbox as BaseSandbox , InvalidArgumentError } from 'e2b'
22
3- import { Result , Execution , OutputMessage , parseOutput , extractError , ExecutionError } from './messaging'
3+ import { Result , Execution , OutputMessage , parseOutput , extractError , ExecutionError } from './messaging'
44import { formatExecutionTimeoutError , formatRequestTimeoutError , readLines } from "./utils" ;
55import { JUPYTER_PORT , DEFAULT_TIMEOUT_MS } from './consts'
6+
7+ /**
8+ * Represents a context for code execution.
9+ */
610export type Context = {
11+ /**
12+ * The ID of the context.
13+ */
714 id : string
15+ /**
16+ * The language of the context.
17+ */
818 language : string
19+ /**
20+ * The working directory of the context.
21+ */
922 cwd : string
1023}
1124
1225/**
13- * Code interpreter module for executing code in a stateful context.
26+ * Options for running code.
27+ */
28+ export interface RunCodeOpts {
29+ /**
30+ * Callback for handling stdout messages.
31+ */
32+ onStdout ?: ( output : OutputMessage ) => ( Promise < any > | any ) ,
33+ /**
34+ * Callback for handling stderr messages.
35+ */
36+ onStderr ?: ( output : OutputMessage ) => ( Promise < any > | any ) ,
37+ /**
38+ * Callback for handling the final execution result.
39+ */
40+ onResult ?: ( data : Result ) => ( Promise < any > | any ) ,
41+ /**
42+ * Callback for handling the `ExecutionError` object.
43+ */
44+ onError ?: ( error : ExecutionError ) => ( Promise < any > | any ) ,
45+ /**
46+ * Custom environment variables for code execution.
47+ *
48+ * @default {}
49+ */
50+ envs ?: Record < string , string > ,
51+ /**
52+ * Timeout for the code execution in **milliseconds**.
53+ *
54+ * @default 60_000 // 60 seconds
55+ */
56+ timeoutMs ?: number ,
57+ /**
58+ * Timeout for the request in **milliseconds**.
59+ *
60+ * @default 30_000 // 30 seconds
61+ */
62+ requestTimeoutMs ?: number ,
63+ }
64+
65+ /**
66+ * Options for creating a code context.
67+ */
68+ export interface CreateCodeContextOpts {
69+ /**
70+ * Working directory for the context.
71+ *
72+ * @default /home/user
73+ */
74+ cwd ?: string ,
75+ /**
76+ * Language for the context.
77+ *
78+ * @default python
79+ */
80+ language ?: string ,
81+ /**
82+ * Timeout for the request in **milliseconds**.
83+ *
84+ * @default 30_000 // 30 seconds
85+ */
86+ requestTimeoutMs ?: number ,
87+ }
88+
89+ /**
90+ * Extension for Sandbox that allows executing code with a stateful context.
1491 */
1592export class Sandbox extends BaseSandbox {
1693 protected static override readonly defaultTemplate : string = 'code-interpreter-v1'
1794
1895 /**
19- * Run the code for the specified language. If no language is specified, Python is used.
96+ * Run the code as Python.
97+ *
98+ * Specify the `language` or `context` option to run the code as a different language or in a different `Context`.
99+ *
20100 * You can reference previously defined variables, imports, and functions in the code.
21101 *
22- * @param code The code to execute
23- * @param opts Options for executing the code
24- * @param opts.language Based on the value, a default context for the language is used. If not defined, the default Python context is used.
25- * @param opts.onStdout Callback for handling stdout messages
26- * @param opts.onStderr Callback for handling stderr messages
27- * @param opts.onResult Callback for handling the final result
28- * @param opts.onError Callback for handling the `ExecutionError` object
29- * @param opts.envs Environment variables to set for the execution
30- * @param opts.timeoutMs Max time to wait for the execution to finish
31- * @param opts.requestTimeoutMs Max time to wait for the request to finish
32- * @returns Execution object
33- */
34- async runCode (
102+ * @param code code to execute.
103+ * @param opts options for executing the code.
104+ *
105+ * @returns `Execution` result object.
106+ */
107+ async runCode (
108+ code : string ,
109+ opts ?: RunCodeOpts & {
110+ /**
111+ * Language to use for code execution.
112+ *
113+ * If not defined, the default Python context is used.
114+ */
115+ language ?: 'python' ,
116+ } ,
117+ ) : Promise < Execution >
118+ /**
119+ * Run the code for the specified language.
120+ *
121+ * Specify the `language` or `context` option to run the code as a different language or in a different `Context`.
122+ * If no language is specified, Python is used.
123+ *
124+ * You can reference previously defined variables, imports, and functions in the code.
125+ *
126+ * @param code code to execute.
127+ * @param opts options for executing the code.
128+ *
129+ * @returns `Execution` result object.
130+ */
131+ async runCode (
35132 code : string ,
36- opts ?: {
133+ opts ?: RunCodeOpts & {
134+ /**
135+ * Language to use for code execution.
136+ *
137+ * If not defined, the default Python context is used.
138+ */
37139 language ?: string ,
38- onStdout ?: ( output : OutputMessage ) => ( Promise < any > | any ) ,
39- onStderr ?: ( output : OutputMessage ) => ( Promise < any > | any ) ,
40- onResult ?: ( data : Result ) => ( Promise < any > | any ) ,
41- onError ?: ( error : ExecutionError ) => ( Promise < any > | any ) ,
42- envs ?: Record < string , string > ,
43- timeoutMs ?: number ,
44- requestTimeoutMs ?: number ,
45140 } ,
46141 ) : Promise < Execution >
47- /**
142+ /**
48143 * Runs the code in the specified context, if not specified, the default context is used.
144+ *
145+ * Specify the `language` or `context` option to run the code as a different language or in a different `Context`.
146+ *
49147 * You can reference previously defined variables, imports, and functions in the code.
50148 *
51- * @param code The code to execute
52- * @param opts Options for executing the code
53- * @param opts.context Concrete context to run the code in. If not specified, the default Python context is used.
54- * @param opts.onStdout Callback for handling stdout messages
55- * @param opts.onStderr Callback for handling stderr messages
56- * @param opts.onResult Callback for handling the final result
57- * @param opts.onError Callback for handling the `ExecutionError` object
58- * @param opts.envs Environment variables to set for the execution
59- * @param opts.timeoutMs Max time to wait for the execution to finish
60- * @param opts.requestTimeoutMs Max time to wait for the request to finish
61- * @returns Execution object
149+ * @param code code to execute.
150+ * @param opts options for executing the code
151+ *
152+ * @returns `Execution` result object
62153 */
63154 async runCode (
64155 code : string ,
65- opts ?: {
156+ opts ?: RunCodeOpts & {
157+ /**
158+ * Context to run the code in.
159+ */
66160 context ?: Context ,
67- onStdout ?: ( output : OutputMessage ) => ( Promise < any > | any ) ,
68- onStderr ?: ( output : OutputMessage ) => ( Promise < any > | any ) ,
69- onResult ?: ( data : Result ) => ( Promise < any > | any ) ,
70- onError ?: ( error : ExecutionError ) => ( Promise < any > | any ) ,
71- envs ?: Record < string , string > ,
72- timeoutMs ?: number ,
73- requestTimeoutMs ?: number ,
74161 } ,
75162 ) : Promise < Execution >
76163 async runCode (
77164 code : string ,
78- opts ?: {
165+ opts ?: RunCodeOpts & {
79166 language ?: string ,
80167 context ?: Context ,
81- onStdout ?: ( output : OutputMessage ) => ( Promise < any > | any ) ,
82- onStderr ?: ( output : OutputMessage ) => ( Promise < any > | any ) ,
83- onResult ?: ( data : Result ) => ( Promise < any > | any ) ,
84- onError ?: ( error : ExecutionError ) => ( Promise < any > | any ) ,
85- envs ?: Record < string , string > ,
86- timeoutMs ?: number ,
87- requestTimeoutMs ?: number ,
88168 } ,
89169 ) : Promise < Execution > {
90170 if ( opts ?. context && opts ?. language ) {
@@ -157,20 +237,11 @@ async runCode(
157237 /**
158238 * Creates a new context to run code in.
159239 *
160- * @param cwd The working directory for the context
161- * @param language The name of the context
162- * @param requestTimeoutMs Max time to wait for the request to finish
163- * @returns The context ID
164- */
165- async createCodeContext ( {
166- cwd,
167- language,
168- requestTimeoutMs,
169- } : {
170- cwd ?: string ,
171- language ?: string ,
172- requestTimeoutMs ?: number ,
173- } = { } ) : Promise < Context > {
240+ * @param opts options for creating the context.
241+ *
242+ * @returns context object.
243+ */
244+ async createCodeContext ( opts ?: CreateCodeContextOpts ) : Promise < Context > {
174245 try {
175246
176247 const res = await fetch ( `${ this . jupyterUrl } /contexts` , {
@@ -179,11 +250,11 @@ async runCode(
179250 'Content-Type' : 'application/json' ,
180251 } ,
181252 body : JSON . stringify ( {
182- language : language ,
183- cwd,
253+ language : opts ?. language ,
254+ cwd : opts ?. cwd ,
184255 } ) ,
185256 keepalive : true ,
186- signal : this . connectionConfig . getSignal ( requestTimeoutMs ) ,
257+ signal : this . connectionConfig . getSignal ( opts ?. requestTimeoutMs ) ,
187258 } )
188259
189260 const error = await extractError ( res )
@@ -198,6 +269,6 @@ async runCode(
198269 }
199270
200271 protected get jupyterUrl ( ) : string {
201- return `${ this . connectionConfig . debug ? 'http' : 'https' } ://${ this . getHost ( JUPYTER_PORT ) } `
272+ return `${ this . connectionConfig . debug ? 'http' : 'https' } ://${ this . getHost ( JUPYTER_PORT ) } `
202273 }
203274}
0 commit comments