Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

Fix action types

Tomas Valenta6f2bea636e45
diff --git a/packages/js-sdk/src/sandbox/index.ts b/packages/js-sdk/src/sandbox/index.ts
index 2f4bd38eb..745a17d92 100644
--- a/packages/js-sdk/src/sandbox/index.ts
+++ b/packages/js-sdk/src/sandbox/index.ts
@@ -29,8 +29,10 @@ export interface SandboxOpts extends SandboxConnectionOpts {
onExit?: (() => Promise<void> | void) | ((exitCode: number) => Promise<void> | void);
}
-export interface Action<T = { [key: string]: any }> {
- (sandbox: Sandbox, args: T): string | Promise<string>
+export interface Action<S extends Sandbox = Sandbox, T = {
+ [key: string]: any;
+}> {
+ (sandbox: S, args: T): string | Promise<string>;
}
/**
@@ -73,7 +75,7 @@ export class Sandbox extends SandboxConnection {
*/
readonly process: ProcessManager
- readonly _actions: Map<string, Action<any>> = new Map()
+ readonly _actions: Map<string, Action<any, any>> = new Map()
private _startCmd?: Promise<Process>
@@ -451,7 +453,7 @@ export class Sandbox extends SandboxConnection {
* const sandbox = await Sandbox.create()
* ```
*/
- static async create(): Promise<Sandbox>;
+ static async create<S extends Sandbox>(): Promise<S>;
/**
* Creates a new Sandbox from the template with the specified ID.
* @param template Sandbox template ID or name
@@ -462,7 +464,7 @@ export class Sandbox extends SandboxConnection {
* const sandbox = await Sandbox.create("sandboxTemplateID")
* ```
*/
- static async create(template: string): Promise<Sandbox>;
+ static async create<S extends Sandbox>(template: string): Promise<S>;
/**
* Creates a new Sandbox from the specified options.
* @param opts Sandbox options
@@ -476,7 +478,7 @@ export class Sandbox extends SandboxConnection {
* })
* ```
*/
- static async create(opts: SandboxOpts): Promise<Sandbox>;
+ static async create<S extends Sandbox>(opts: SandboxOpts): Promise<S>;
static async create(optsOrTemplate?: string | SandboxOpts) {
const opts: SandboxOpts | undefined = typeof optsOrTemplate === 'string' ? { template: optsOrTemplate } : optsOrTemplate
const sandbox = new Sandbox(opts)
@@ -545,7 +547,7 @@ export class Sandbox extends SandboxConnection {
* sandbox.addAction('readFile', (sandbox, args) => sandbox.filesystem.read(args.path))
* ```
*/
- addAction<T = { [name: string]: any }>(action: Action<T>): this;
+ addAction<T = { [name: string]: any }>(action: Action<this, T>): this;
/**
* Add a new action with a specified name.
*
@@ -565,8 +567,8 @@ export class Sandbox extends SandboxConnection {
* sandbox.addAction(readFile)
* ```
*/
- addAction<T = { [name: string]: any }>(name: string, action: Action<T>): this;
- addAction<T = { [name: string]: any }>(actionOrName: string | Action<T>, action?: Action<T>): this {
+ addAction<T = { [name: string]: any }>(name: string, action: Action<this, T>): this;
+ addAction<T = { [name: string]: any }>(actionOrName: string | Action<this, T>, action?: Action<this, T>): this {
if (typeof actionOrName === 'string') {
if (!action) throw new Error('Action is required')
this._actions.set(actionOrName, action)
diff --git a/packages/js-sdk/src/templates/dataAnalysis.ts b/packages/js-sdk/src/templates/dataAnalysis.ts
index 998ee295d..1c249f2ca 100644
--- a/packages/js-sdk/src/templates/dataAnalysis.ts
+++ b/packages/js-sdk/src/templates/dataAnalysis.ts
@@ -1,23 +1,19 @@
-import { DownloadFileFormat, Sandbox, SandboxOpts } from '../sandbox'
+import { Sandbox, SandboxOpts } from '../sandbox'
import type { ProcessOpts } from '../sandbox/process'
import { FilesystemEvent, FilesystemOperation } from '../sandbox/filesystemWatcher'
-export class Artifact {
+export class Artifact<S extends DataAnalysis> {
readonly path: string
- readonly _sandbox: Sandbox
+ readonly _sandbox: S
- constructor(path: string, sandbox: Sandbox) {
+ constructor(path: string, sandbox: S) {
this.path = path
this._sandbox = sandbox
}
-
- async download(format?: DownloadFileFormat) {
- return this._sandbox.downloadFile(this.path, format)
- }
}
-export interface RunPythonOpts extends Omit<ProcessOpts, 'cmd'> {
- onArtifact?: (artifact: Artifact) => Promise<void> | void;
+export interface RunPythonOpts<S extends DataAnalysis> extends Omit<ProcessOpts, 'cmd'> {
+ onArtifact?: (artifact: Artifact<S>) => Promise<void> | void;
}
export class DataAnalysis extends Sandbox {
@@ -35,12 +31,16 @@ export class DataAnalysis extends Sandbox {
return sandbox
}
- async runPython(code: string, opts: RunPythonOpts = {}) {
+ async runPython(code: string, opts: RunPythonOpts<this> = {}): Promise<{
+ stdout: string;
+ stderr: string;
+ artifacts: Artifact<DataAnalysis>[];
+ }> {
const artifacts: string[] = []
const registerArtifacts = async (event: FilesystemEvent) => {
if (event.operation === FilesystemOperation.Create) {
- const artifact = new Artifact(event.path, this)
+ const artifact = new Artifact<this>(event.path, this)
artifacts.push(event.path)
await opts.onArtifact?.(artifact)
}
@@ -64,7 +64,7 @@ export class DataAnalysis extends Sandbox {
return {
stdout: proc.output.stdout,
stderr: proc.output.stderr,
- artifacts: artifacts.map((artifact) => new Artifact(artifact, this)),
+ artifacts: artifacts.map((artifact) => new Artifact<this>(artifact, this)),
}
}