@e2b/code-interpreter
Advanced tools
Comparing version 0.0.9-beta.71 to 0.0.9-beta.72
@@ -1,2 +0,2 @@ | ||
import { ConnectionConfig, Sandbox as Sandbox$1 } from 'e2b'; | ||
import { Sandbox as Sandbox$1 } from 'e2b'; | ||
export * from 'e2b'; | ||
@@ -307,13 +307,14 @@ | ||
type Context = { | ||
id: string; | ||
language: string; | ||
cwd: string; | ||
}; | ||
/** | ||
* Code interpreter module for executing code in a stateful context. | ||
*/ | ||
declare class JupyterExtension { | ||
private readonly url; | ||
private readonly connectionConfig; | ||
private static readonly execTimeoutMs; | ||
private static readonly defaultKernelID; | ||
constructor(url: string, connectionConfig: ConnectionConfig); | ||
declare class Sandbox extends Sandbox$1 { | ||
protected static readonly defaultTemplate: string; | ||
/** | ||
* Runs the code in the specified context, if not specified, the default context is used. | ||
* Run the code for the specified language. If no language is specified, Python is used. | ||
* You can reference previously defined variables, imports, and functions in the code. | ||
@@ -323,3 +324,3 @@ * | ||
* @param opts Options for executing the code | ||
* @param opts.kernelID The context ID to run the code in | ||
* @param opts.language Based on the value, a default context for the language is used. If not defined, the default Python context is used. | ||
* @param opts.onStdout Callback for handling stdout messages | ||
@@ -333,4 +334,4 @@ * @param opts.onStderr Callback for handling stderr messages | ||
*/ | ||
execCell(code: string, opts?: { | ||
kernelID?: string; | ||
runCode(code: string, opts?: { | ||
language?: string; | ||
onStdout?: (output: OutputMessage) => (Promise<any> | any); | ||
@@ -344,57 +345,41 @@ onStderr?: (output: OutputMessage) => (Promise<any> | any); | ||
/** | ||
* Runs the code in the specified context, if not specified, the default context is used. | ||
* You can reference previously defined variables, imports, and functions in the code. | ||
* | ||
* @param code The code to execute | ||
* @param opts Options for executing the code | ||
* @param opts.context Concrete context to run the code in. If not specified, the default Python context is used. | ||
* @param opts.onStdout Callback for handling stdout messages | ||
* @param opts.onStderr Callback for handling stderr messages | ||
* @param opts.onResult Callback for handling the final result | ||
* @param opts.envs Environment variables to set for the execution | ||
* @param opts.timeoutMs Max time to wait for the execution to finish | ||
* @param opts.requestTimeoutMs Max time to wait for the request to finish | ||
* @returns Execution object | ||
*/ | ||
runCode(code: string, opts?: { | ||
context?: Context; | ||
onStdout?: (output: OutputMessage) => (Promise<any> | any); | ||
onStderr?: (output: OutputMessage) => (Promise<any> | any); | ||
onResult?: (data: Result) => (Promise<any> | any); | ||
envs?: Record<string, string>; | ||
timeoutMs?: number; | ||
requestTimeoutMs?: number; | ||
}): Promise<Execution>; | ||
/** | ||
* Creates a new context to run code in. | ||
* | ||
* @param cwd The working directory for the context | ||
* @param kernelName The name of the context | ||
* @param language The name of the context | ||
* @param requestTimeoutMs Max time to wait for the request to finish | ||
* @returns The context ID | ||
*/ | ||
createKernel({ cwd, kernelName, requestTimeoutMs, }?: { | ||
createCodeContext({ cwd, language, requestTimeoutMs, }?: { | ||
cwd?: string; | ||
kernelName?: string; | ||
language?: string; | ||
requestTimeoutMs?: number; | ||
}): Promise<string>; | ||
/** | ||
* Restarts the context. | ||
* Restarting will clear all variables, imports, and other settings set during previous executions. | ||
* | ||
* @param kernelID The context ID to restart | ||
* @param requestTimeoutMs Max time to wait for the request to finish | ||
*/ | ||
restartKernel({ kernelID, requestTimeoutMs, }?: { | ||
kernelID?: string; | ||
requestTimeoutMs?: number; | ||
}): Promise<void>; | ||
/** | ||
* Shuts down the context. | ||
* | ||
* @param kernelID The context ID to shut down | ||
* @param requestTimeoutMs Max time to wait for the request to finish | ||
*/ | ||
shutdownKernel({ kernelID, requestTimeoutMs, }?: { | ||
kernelID?: string; | ||
requestTimeoutMs?: number; | ||
}): Promise<void>; | ||
/** | ||
* Lists all available contexts. | ||
* | ||
* @param requestTimeoutMs Max time to wait for the request to finish | ||
* @returns List of context IDs and names | ||
*/ | ||
listKernels({ requestTimeoutMs, }?: { | ||
requestTimeoutMs?: number; | ||
}): Promise<{ | ||
kernelID: string; | ||
name: string; | ||
}[]>; | ||
}): Promise<Context>; | ||
protected get jupyterUrl(): string; | ||
} | ||
/** | ||
* Code interpreter module for executing code in a stateful context. | ||
*/ | ||
declare class Sandbox extends Sandbox$1 { | ||
protected static readonly defaultTemplate: string; | ||
protected static readonly jupyterPort = 49999; | ||
readonly notebook: JupyterExtension; | ||
} | ||
export { type BarData, type BarGraph, type BoxAndWhiskerData, type BoxAndWhiskerGraph, Execution, ExecutionError, type Graph, GraphType, type GraphTypes, JupyterExtension, type LineGraph, type Logs, type MIMEType, OutputMessage, type PieData, type PieGraph, type PointData, type RawData, Result, Sandbox, ScaleType, type ScatterGraph, type SuperGraph, Sandbox as default }; | ||
export { type BarData, type BarGraph, type BoxAndWhiskerData, type BoxAndWhiskerGraph, type Context, Execution, ExecutionError, type Graph, GraphType, type GraphTypes, type LineGraph, type Logs, type MIMEType, OutputMessage, type PieData, type PieGraph, type PointData, type RawData, Result, Sandbox, ScaleType, type ScatterGraph, type SuperGraph, Sandbox as default }; |
@@ -78,3 +78,2 @@ "use strict"; | ||
__export(src_exports, { | ||
JupyterExtension: () => JupyterExtension, | ||
Sandbox: () => Sandbox, | ||
@@ -87,3 +86,3 @@ default: () => src_default | ||
// src/sandbox.ts | ||
var import_e2b2 = require("e2b"); | ||
var import_e2b3 = require("e2b"); | ||
@@ -288,3 +287,4 @@ // src/messaging.ts | ||
// src/sandbox.ts | ||
// src/utils.ts | ||
var import_e2b2 = require("e2b"); | ||
function formatRequestTimeoutError(error) { | ||
@@ -332,25 +332,15 @@ if (error instanceof Error && error.name === "AbortError") { | ||
} | ||
var _JupyterExtension = class _JupyterExtension { | ||
constructor(url, connectionConfig) { | ||
this.url = url; | ||
this.connectionConfig = connectionConfig; | ||
} | ||
/** | ||
* Runs the code in the specified context, if not specified, the default context is used. | ||
* You can reference previously defined variables, imports, and functions in the code. | ||
* | ||
* @param code The code to execute | ||
* @param opts Options for executing the code | ||
* @param opts.kernelID The context ID to run the code in | ||
* @param opts.onStdout Callback for handling stdout messages | ||
* @param opts.onStderr Callback for handling stderr messages | ||
* @param opts.onResult Callback for handling the final result | ||
* @param opts.envs Environment variables to set for the execution | ||
* @param opts.timeoutMs Max time to wait for the execution to finish | ||
* @param opts.requestTimeoutMs Max time to wait for the request to finish | ||
* @returns Execution object | ||
*/ | ||
execCell(code, opts) { | ||
// src/consts.ts | ||
var DEFAULT_TIMEOUT_MS = 6e4; | ||
var JUPYTER_PORT = 49999; | ||
// src/sandbox.ts | ||
var Sandbox = class extends import_e2b3.Sandbox { | ||
runCode(code, opts) { | ||
return __async(this, null, function* () { | ||
var _a, _b; | ||
var _a, _b, _c; | ||
if ((opts == null ? void 0 : opts.context) && (opts == null ? void 0 : opts.language)) { | ||
throw new import_e2b3.InvalidArgumentError("You can provide context or language, but not both at the same time."); | ||
} | ||
const controller = new AbortController(); | ||
@@ -362,3 +352,3 @@ const requestTimeout = (_a = opts == null ? void 0 : opts.requestTimeoutMs) != null ? _a : this.connectionConfig.requestTimeoutMs; | ||
try { | ||
const res = yield fetch(`${this.url}/execute`, { | ||
const res = yield fetch(`${this.jupyterUrl}/execute`, { | ||
method: "POST", | ||
@@ -370,3 +360,4 @@ headers: { | ||
code, | ||
context_id: opts == null ? void 0 : opts.kernelID, | ||
context_id: (_b = opts == null ? void 0 : opts.context) == null ? void 0 : _b.id, | ||
language: opts == null ? void 0 : opts.language, | ||
env_vars: opts == null ? void 0 : opts.envs | ||
@@ -385,3 +376,3 @@ }), | ||
clearTimeout(reqTimer); | ||
const bodyTimeout = (_b = opts == null ? void 0 : opts.timeoutMs) != null ? _b : _JupyterExtension.execTimeoutMs; | ||
const bodyTimeout = (_c = opts == null ? void 0 : opts.timeoutMs) != null ? _c : DEFAULT_TIMEOUT_MS; | ||
const bodyTimer = bodyTimeout ? setTimeout(() => { | ||
@@ -422,14 +413,14 @@ controller.abort(); | ||
* @param cwd The working directory for the context | ||
* @param kernelName The name of the context | ||
* @param language The name of the context | ||
* @param requestTimeoutMs Max time to wait for the request to finish | ||
* @returns The context ID | ||
*/ | ||
createKernel() { | ||
createCodeContext() { | ||
return __async(this, arguments, function* ({ | ||
cwd, | ||
kernelName, | ||
language, | ||
requestTimeoutMs | ||
} = {}) { | ||
try { | ||
const res = yield fetch(`${this.url}/contexts`, { | ||
const res = yield fetch(`${this.jupyterUrl}/contexts`, { | ||
method: "POST", | ||
@@ -440,3 +431,3 @@ headers: { | ||
body: JSON.stringify({ | ||
name: kernelName, | ||
language, | ||
cwd | ||
@@ -451,4 +442,3 @@ }), | ||
} | ||
const data = yield res.json(); | ||
return data.id; | ||
return yield res.json(); | ||
} catch (error) { | ||
@@ -459,101 +449,7 @@ throw formatRequestTimeoutError(error); | ||
} | ||
/** | ||
* Restarts the context. | ||
* Restarting will clear all variables, imports, and other settings set during previous executions. | ||
* | ||
* @param kernelID The context ID to restart | ||
* @param requestTimeoutMs Max time to wait for the request to finish | ||
*/ | ||
restartKernel() { | ||
return __async(this, arguments, function* ({ | ||
kernelID, | ||
requestTimeoutMs | ||
} = {}) { | ||
try { | ||
kernelID = kernelID || _JupyterExtension.defaultKernelID; | ||
const res = yield fetch(`${this.url}/contexts/${kernelID}/restart`, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json" | ||
}, | ||
keepalive: true, | ||
signal: this.connectionConfig.getSignal(requestTimeoutMs) | ||
}); | ||
const error = yield extractError(res); | ||
if (error) { | ||
throw error; | ||
} | ||
} catch (error) { | ||
throw formatRequestTimeoutError(error); | ||
} | ||
}); | ||
get jupyterUrl() { | ||
return `${this.connectionConfig.debug ? "http" : "https"}://${this.getHost(JUPYTER_PORT)}`; | ||
} | ||
/** | ||
* Shuts down the context. | ||
* | ||
* @param kernelID The context ID to shut down | ||
* @param requestTimeoutMs Max time to wait for the request to finish | ||
*/ | ||
shutdownKernel() { | ||
return __async(this, arguments, function* ({ | ||
kernelID, | ||
requestTimeoutMs | ||
} = {}) { | ||
try { | ||
kernelID = kernelID || _JupyterExtension.defaultKernelID; | ||
const res = yield fetch(`${this.url}/contexts/${kernelID}`, { | ||
method: "DELETE", | ||
keepalive: true, | ||
signal: this.connectionConfig.getSignal(requestTimeoutMs) | ||
}); | ||
const error = yield extractError(res); | ||
if (error) { | ||
throw error; | ||
} | ||
} catch (error) { | ||
throw formatRequestTimeoutError(error); | ||
} | ||
}); | ||
} | ||
/** | ||
* Lists all available contexts. | ||
* | ||
* @param requestTimeoutMs Max time to wait for the request to finish | ||
* @returns List of context IDs and names | ||
*/ | ||
listKernels() { | ||
return __async(this, arguments, function* ({ | ||
requestTimeoutMs | ||
} = {}) { | ||
try { | ||
const res = yield fetch(`${this.url}/contexts`, { | ||
keepalive: true, | ||
signal: this.connectionConfig.getSignal(requestTimeoutMs) | ||
}); | ||
const error = yield extractError(res); | ||
if (error) { | ||
throw error; | ||
} | ||
return (yield res.json()).map((kernel) => ({ kernelID: kernel.id, name: kernel.name })); | ||
} catch (error) { | ||
throw formatRequestTimeoutError(error); | ||
} | ||
}); | ||
} | ||
}; | ||
_JupyterExtension.execTimeoutMs = 3e5; | ||
_JupyterExtension.defaultKernelID = "default"; | ||
var JupyterExtension = _JupyterExtension; | ||
var _Sandbox = class _Sandbox extends import_e2b2.Sandbox { | ||
constructor() { | ||
super(...arguments); | ||
this.notebook = new JupyterExtension( | ||
`${this.connectionConfig.debug ? "http" : "https"}://${this.getHost(_Sandbox.jupyterPort)}`, | ||
this.connectionConfig | ||
); | ||
} | ||
}; | ||
_Sandbox.defaultTemplate = "code-interpreter-beta"; | ||
_Sandbox.jupyterPort = 49999; | ||
var Sandbox = _Sandbox; | ||
Sandbox.defaultTemplate = "code-interpreter-beta"; | ||
@@ -564,3 +460,2 @@ // src/index.ts | ||
0 && (module.exports = { | ||
JupyterExtension, | ||
Sandbox, | ||
@@ -567,0 +462,0 @@ ...require("e2b") |
{ | ||
"name": "@e2b/code-interpreter", | ||
"version": "0.0.9-beta.71", | ||
"version": "0.0.9-beta.72", | ||
"description": "E2B Code Interpreter - Stateful code execution", | ||
@@ -5,0 +5,0 @@ "homepage": "https://e2b.dev", |
@@ -21,11 +21,11 @@ # Code interpreter extension for JavaScript | ||
```js | ||
import { CodeInterpreter } from '@e2b/code-interpreter' | ||
import { Sandbox } from '@e2b/code-interpreter' | ||
const sandbox = await CodeInterpreter.create() | ||
await sandbox.notebook.execCell('x = 1') | ||
const sandbox = await Sandbox.create() | ||
await sandbox.runCode('x = 1') | ||
const execution = await sandbox.notebook.execCell('x+=1; x') | ||
const execution = await sandbox.runCode('x+=1; x') | ||
console.log(execution.text) // outputs 2 | ||
await sandbox.close() | ||
await sandbox.kill() | ||
``` | ||
@@ -36,5 +36,5 @@ | ||
```js | ||
import { CodeInterpreter } from '@e2b/code-interpreter' | ||
import { Sandbox } from '@e2b/code-interpreter' | ||
const sandbox = await CodeInterpreter.create() | ||
const sandbox = await Sandbox.create() | ||
@@ -53,5 +53,5 @@ const code = ` | ||
// you can install dependencies in "jupyter notebook style" | ||
await sandbox.notebook.execCell('!pip install matplotlib') | ||
await sandbox.runCode('!pip install matplotlib') | ||
const execution = await sandbox.notebook.execCell(code) | ||
const execution = await sandbox.runCode(code) | ||
@@ -61,3 +61,3 @@ // this contains the image data, you can e.g. save it to file or send to frontend | ||
await sandbox.close() | ||
await sandbox.kill() | ||
``` | ||
@@ -68,3 +68,3 @@ | ||
```js | ||
import { CodeInterpreter } from '@e2b/code-interpreter' | ||
import { Sandbox } from '@e2b/code-interpreter' | ||
@@ -83,5 +83,5 @@ const code = ` | ||
const sandbox = await CodeInterpreter.create() | ||
const sandbox = await Sandbox.create() | ||
await sandbox.notebook.execCell(code, { | ||
await sandbox.runCode(code, { | ||
onStdout: (out) => console.log(out), | ||
@@ -92,3 +92,3 @@ onStderr: (outErr) => console.error(outErr), | ||
await sandbox.close() | ||
await sandbox.kill() | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4
101221
1244