Skip to content

Commit aa3699e

Browse files
committed
Update JS docstrings
1 parent 3c9612a commit aa3699e

File tree

4 files changed

+159
-75
lines changed

4 files changed

+159
-75
lines changed

js/src/charts.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ export enum ScaleType {
2727
ASINH = "asinh",
2828
}
2929

30+
/**
31+
* Represents a chart.
32+
*/
3033
export type Chart = {
3134
type: ChartType
3235
title: string

js/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export * from 'e2b'
22

33
export { Sandbox } from './sandbox'
4-
export type { Context } from './sandbox'
4+
export type { Context, RunCodeOpts, CreateCodeContextOpts } from './sandbox'
55
export type {
66
Logs,
77
ExecutionError,

js/src/messaging.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,24 @@ export async function extractError(res: Response) {
1818
}
1919
}
2020

21+
/**
22+
* Represents an output message from the sandbox code execution.
23+
*/
2124
export class OutputMessage {
2225
constructor(
26+
/**
27+
* The output line.
28+
*/
2329
public readonly line: string,
2430
/**
25-
* Unix epoch in nanoseconds
31+
* Unix epoch in nanoseconds.
2632
*/
2733
public readonly timestamp: number,
34+
/**
35+
* Whether the output is an error.
36+
*/
2837
public readonly error: boolean
29-
) {}
38+
) { }
3039

3140
public toString() {
3241
return this.line
@@ -51,14 +60,15 @@ export class ExecutionError {
5160
* The raw traceback of the error.
5261
**/
5362
public traceback: string
54-
) {}
63+
) { }
5564
}
5665

5766
/**
5867
* Represents a MIME type.
5968
*/
6069
export type MIMEType = string
6170

71+
6272
type E2BData = {
6373
data: Record<string, unknown>
6474
chart: ChartTypes
@@ -281,7 +291,7 @@ export class Execution {
281291
* Execution count of the cell.
282292
*/
283293
public executionCount?: number
284-
) {}
294+
) { }
285295

286296
/**
287297
* Returns the text representation of the main result of the cell.

js/src/sandbox.ts

Lines changed: 141 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,170 @@
11
import { 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'
44
import { formatExecutionTimeoutError, formatRequestTimeoutError, readLines } from "./utils";
55
import { JUPYTER_PORT, DEFAULT_TIMEOUT_MS } from './consts'
6+
7+
/**
8+
* Represents a context for code execution.
9+
*/
610
export 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
*/
1592
export 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

Comments
 (0)