-
Notifications
You must be signed in to change notification settings - Fork 206
Expand file tree
/
Copy pathmessaging.ts
More file actions
372 lines (347 loc) · 8.41 KB
/
messaging.ts
File metadata and controls
372 lines (347 loc) · 8.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
import { NotFoundError, SandboxError, TimeoutError } from 'e2b'
import { ChartTypes } from './charts'
export async function extractError(res: Response) {
if (res.ok) {
return
}
switch (res.status) {
case 502:
return new TimeoutError(
`${await res.text()}: This error is likely due to sandbox timeout. You can modify the sandbox timeout by passing 'timeoutMs' when starting the sandbox or calling '.setTimeout' on the sandbox with the desired timeout.`
)
case 404:
return new NotFoundError(await res.text())
default:
return new SandboxError(`${res.status} ${res.statusText}`)
}
}
/**
* Represents an output message from the sandbox code execution.
*/
export class OutputMessage {
constructor(
/**
* The output line.
*/
public readonly line: string,
/**
* Unix epoch in nanoseconds.
*/
public readonly timestamp: number,
/**
* Whether the output is an error.
*/
public readonly error: boolean
) {}
public toString() {
return this.line
}
}
/**
* Represents an error that occurred during the execution of a cell.
* The error contains the name of the error, the value of the error, and the traceback.
*/
export class ExecutionError {
constructor(
/**
* Name of the error.
**/
public name: string,
/**
* Value of the error.
**/
public value: string,
/**
* The raw traceback of the error.
**/
public traceback: string
) {}
}
/**
* Represents a MIME type.
*/
export type MIMEType = string
type E2BData = {
data: Record<string, unknown>
chart: ChartTypes
}
/**
* Dictionary that maps MIME types to their corresponding representations of the data.
*/
export type RawData = {
[key: MIMEType]: string
} & E2BData
/**
* Represents the data to be displayed as a result of executing a cell in a Jupyter notebook.
* The result is similar to the structure returned by ipython kernel: https://ipython.readthedocs.io/en/stable/development/execution.html#execution-semantics
*
*
* The result can contain multiple types of data, such as text, images, plots, etc. Each type of data is represented
* as a string, and the result can contain multiple types of data. The display calls don't have to have text representation,
* for the actual result the representation is always present for the result, the other representations are always optional.
*/
export class Result {
/**
* Text representation of the result.
*/
readonly text?: string
/**
* HTML representation of the data.
*/
readonly html?: string
/**
* Markdown representation of the data.
*/
readonly markdown?: string
/**
* SVG representation of the data.
*/
readonly svg?: string
/**
* PNG representation of the data.
*/
readonly png?: string
/**
* JPEG representation of the data.
*/
readonly jpeg?: string
/**
* PDF representation of the data.
*/
readonly pdf?: string
/**
* LaTeX representation of the data.
*/
readonly latex?: string
/**
* JSON representation of the data.
*/
readonly json?: string
/**
* JavaScript representation of the data.
*/
readonly javascript?: string
/**
* Contains the data from DataFrame.
*/
readonly data?: Record<string, unknown>
/**
* Contains the chart data.
*/
readonly chart?: ChartTypes
/**
* Extra data that can be included. Not part of the standard types.
*/
readonly extra?: any
readonly raw: RawData
constructor(rawData: RawData, public readonly isMainResult: boolean) {
const data = { ...rawData }
delete data['type']
delete data['is_main_result']
this.text = data['text']
this.html = data['html']
this.markdown = data['markdown']
this.svg = data['svg']
this.png = data['png']
this.jpeg = data['jpeg']
this.pdf = data['pdf']
this.latex = data['latex']
this.json = data['json']
this.javascript = data['javascript']
this.isMainResult = isMainResult
this.raw = data
this.data = data['data']
this.chart = data['chart']
this.extra = {}
for (const key of Object.keys(data)) {
if (
![
'plain',
'html',
'markdown',
'svg',
'png',
'jpeg',
'pdf',
'latex',
'json',
'javascript',
'data',
'chart',
'extra',
'text',
].includes(key)
) {
this.extra[key] = data[key]
}
}
}
/**
* Returns all the formats available for the result.
*
* @returns Array of strings representing the formats available for the result.
*/
formats(): string[] {
const formats = []
if (this.html) {
formats.push('html')
}
if (this.markdown) {
formats.push('markdown')
}
if (this.svg) {
formats.push('svg')
}
if (this.png) {
formats.push('png')
}
if (this.jpeg) {
formats.push('jpeg')
}
if (this.pdf) {
formats.push('pdf')
}
if (this.latex) {
formats.push('latex')
}
if (this.json) {
formats.push('json')
}
if (this.javascript) {
formats.push('javascript')
}
if (this.data) {
formats.push('data')
}
for (const key of Object.keys(this.extra)) {
formats.push(key)
}
return formats
}
/**
* Returns the serializable representation of the result.
*/
toJSON() {
return {
text: this.text,
html: this.html,
markdown: this.markdown,
svg: this.svg,
png: this.png,
jpeg: this.jpeg,
pdf: this.pdf,
latex: this.latex,
json: this.json,
javascript: this.javascript,
...(Object.keys(this.extra).length > 0 ? { extra: this.extra } : {}),
}
}
}
/**
* Data printed to stdout and stderr during execution, usually by print statements, logs, warnings, subprocesses, etc.
*/
export type Logs = {
/**
* List of strings printed to stdout by prints, subprocesses, etc.
*/
stdout: string[]
/**
* List of strings printed to stderr by prints, subprocesses, etc.
*/
stderr: string[]
}
/**
* Represents the result of a cell execution.
*/
export class Execution {
constructor(
/**
* List of result of the cell (interactively interpreted last line), display calls (e.g. matplotlib plots).
*/
public results: Result[] = [],
/**
* Logs printed to stdout and stderr during execution.
*/
public logs: Logs = { stdout: [], stderr: [] },
/**
* An Error object if an error occurred, null otherwise.
*/
public error?: ExecutionError,
/**
* Execution count of the cell.
*/
public executionCount?: number
) {}
/**
* Returns the text representation of the main result of the cell.
*/
get text(): string | undefined {
for (const data of this.results) {
if (data.isMainResult) {
return data.text
}
}
}
/**
* Returns the serializable representation of the execution result.
*/
toJSON() {
return {
results: this.results,
logs: this.logs,
error: this.error,
}
}
}
export async function parseOutput(
execution: Execution,
line: string,
onStdout?: (output: OutputMessage) => Promise<any> | any,
onStderr?: (output: OutputMessage) => Promise<any> | any,
onResult?: (data: Result) => Promise<any> | any,
onError?: (error: ExecutionError) => Promise<any> | any
) {
const msg = JSON.parse(line)
switch (msg.type) {
case 'result': {
const result = new Result(
{ ...msg, type: undefined, is_main_result: undefined },
msg.is_main_result
)
execution.results.push(result)
if (onResult) {
await onResult(result)
}
break
}
case 'stdout':
execution.logs.stdout.push(msg.text)
if (onStdout) {
await onStdout({
error: false,
line: msg.text,
timestamp: new Date().getTime() * 1000,
})
}
break
case 'stderr':
execution.logs.stderr.push(msg.text)
if (onStderr) {
await onStderr({
error: true,
line: msg.text,
timestamp: new Date().getTime() * 1000,
})
}
break
case 'error':
execution.error = new ExecutionError(msg.name, msg.value, msg.traceback)
if (onError) {
await onError(execution.error)
}
break
case 'number_of_executions':
execution.executionCount = msg.execution_count
break
}
}