Skip to content

armgit / E2B

Public Imported from GitHub
0

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

Rename register action; Add add action overloading

Tomas Valentab7f010a609e4
diff --git a/packages/js-sdk/src/sandbox/index.ts b/packages/js-sdk/src/sandbox/index.ts
index c7159c568..9ccec662b 100644
--- a/packages/js-sdk/src/sandbox/index.ts
+++ b/packages/js-sdk/src/sandbox/index.ts
@@ -433,16 +433,16 @@ export class Sandbox extends SandboxConnection {
}
/**
- * Returns a map of registered actions.
+ * Returns a map of added actions.
*
- * @returns Map of registered actions
+ * @returns Map of added actions
*/
get actions() {
return new Map(this._actions)
}
/**
- * OpenAI integration that can be used to get output for the actions registered in the sandbox.
+ * OpenAI integration that can be used to get output for the actions added in the sandbox.
*
* @returns OpenAI integration
*/
@@ -476,36 +476,73 @@ export class Sandbox extends SandboxConnection {
}
/**
- * Registers a new action.
+ * Add a new action. The name of the action is automatically extracted from the function name.
+ *
+ * You can use this action with specific integrations like OpenAI to interact with the sandbox and get output for the action.
+ *
+ * @param action Action handler
+ * @returns Sandbox
+ *
+ * @example
+ * ```ts
+ * const sandbox = await Sandbox.create()
+ * sandbox.addAction('readFile', (sandbox, args) => sandbox.filesystem.read(args.path))
+ * ```
+ */
+ addAction<T = { [name: string]: any }>(action: Action<T>): this;
+ /**
+ * Add a new action with a specified name.
+ *
+ * You can use this action with specific integrations like OpenAI to interact with the sandbox and get output for the action.
+ *
* @param name Action name
* @param action Action handler
* @returns Sandbox
*
* @example
* ```ts
+ * async function readFile(sandbox: Sandbox, args: any) {
+ * return sandbox.filesystem.read(args.path)
+ * }
+ *
* const sandbox = await Sandbox.create()
- * sandbox.registerAction('hello', (sandbox, args) => 'Hello World')
+ * sandbox.addAction(readFile)
* ```
*/
- registerAction<T = { [name: string]: any }>(name: string, action: Action<T>) {
- this._actions.set(name, action)
+ addAction<T = { [name: string]: any }>(name: string, action: Action<T>): this;
+ addAction<T = { [name: string]: any }>(actionOrName: string | Action<T>, action?: Action<T>): this {
+ if (typeof actionOrName === 'string') {
+ if (!action) throw new Error('Action is required')
+ this._actions.set(actionOrName, action)
+ return this
+ } else if (typeof actionOrName === 'function') {
+ action = actionOrName
+
+ if (!action.name) {
+ throw new Error('Action name is required')
+ }
+
+ this._actions.set(action.name, action)
+ } else {
+ throw new Error('Action or action name and action is required')
+ }
return this
}
/**
- * Unregisters an action.
+ * Remove an action.
* @param name Action name
* @returns Sandbox
*
* @example
* ```ts
* const sandbox = await Sandbox.create()
- * sandbox.registerAction('hello', (sandbox, args) => 'Hello World')
- * sandbox.unregisterAction('hello')
+ * sandbox.addAction('hello', (sandbox, args) => 'Hello World')
+ * sandbox.removeAction('hello')
* ```
*/
- unregisterAction(name: string) {
+ removeAction(name: string) {
this._actions.delete(name)
return this
diff --git a/packages/js-sdk/test/action.test.mjs b/packages/js-sdk/test/action.test.mjs
index f71545e1c..5ba4038d8 100644
--- a/packages/js-sdk/test/action.test.mjs
+++ b/packages/js-sdk/test/action.test.mjs
@@ -3,10 +3,10 @@ import { test, expect } from 'vitest'
import { id } from './setup.mjs'
-test('register action', async () => {
+test('add action', async () => {
const sandbox = await Sandbox.create({ id })
- sandbox.registerAction('test', () => {
+ sandbox.addAction('test', () => {
return 'test'
})