Skip to content

armgit / E2B

Public Imported from GitHub
0

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

2.8 KB
Raw Blame
import {
  ConnectionOpts,
  E2BClientOpts,
  E2B as CoreE2B,
  Secret,
  Template,
  Volume,
} from 'e2b'

import { Sandbox } from './sandbox'

export type { E2BClientOpts }

/**
 * E2B client with an explicitly bound connection configuration.
 *
 * The resources exposed by the client ({@link E2B.Sandbox},
 * {@link E2B.Volume}, {@link E2B.Template}, {@link E2B.Secret}) behave exactly
 * like the top-level `Sandbox` / `Volume` / `Template` / `Secret` exports,
 * except the options passed to the client are used as the defaults instead of
 * the environment variables.
 * Per-call options still take precedence over the client's options.
 *
 * Multiple clients are fully isolated from each other and from the top-level
 * env-configured exports.
 *
 * @example
 * ```ts
 * import { E2B } from '@e2b/code-interpreter'
 *
 * const client = new E2B({ apiKey: 'e2b_...', domain: 'e2b.dev' })
 *
 * const sandbox = await client.Sandbox.create()
 * const execution = await sandbox.runCode('x = 1; x += 1; x')
 * ```
 */
export class E2B {
  /**
   * Code Interpreter `Sandbox` class bound to this client's connection
   * configuration.
   */
  readonly Sandbox: typeof Sandbox

  /**
   * `Volume` class bound to this client's connection configuration.
   */
  readonly Volume: typeof Volume

  /**
   * `Template` bound to this client's connection configuration. Both the
   * builder (`client.Template()`) and the statics
   * (`client.Template.build(...)`, `client.Template.exists(...)`, …) work like
   * the top-level `Template`.
   */
  readonly Template: typeof Template

  /**
   * `Secret` class bound to this client's connection configuration.
   */
  readonly Secret: typeof Secret

  /**
   * Create a new client with the connection options bound to it.
   *
   * @param opts connection options used as the defaults for every call made
   *   through this client's resource classes.
   */
  constructor(opts?: E2BClientOpts) {
    // Options are copied so later mutations of the caller's object cannot
    // change the bound configuration. `signal` is dropped rather than only
    // typed away, since it cancels a single request and a caller passing a
    // wider-typed object (or plain JS) would otherwise bind it to every call.
    const boundOpts: E2BClientOpts = { ...(opts ?? {}) }
    delete (boundOpts as ConnectionOpts).signal

    if (boundOpts.headers) {
      boundOpts.headers = { ...boundOpts.headers }
    }
    if (boundOpts.apiHeaders) {
      boundOpts.apiHeaders = { ...boundOpts.apiHeaders }
    }

    this.Sandbox = class extends Sandbox {
      protected static override readonly boundOpts = boundOpts
    }

    // The resources that are not specific to the Code Interpreter are bound by
    // the core client.
    const core = new CoreE2B(boundOpts)
    this.Volume = core.Volume
    this.Template = core.Template
    this.Secret = core.Secret
  }
}