Documentation
¶
Overview ¶
Package e2b provides a Go SDK for E2B (https://e2b.dev/), a cloud runtime for AI agents that provides secure sandboxed environments for code execution.
Index ¶
- type APIError
- type CodeInterpreter
- type ErrMissingRequiredArgument
- type ErrToolArgument
- type ErrToolNotFound
- type Event
- type EventParams
- type EventResult
- type Execution
- type ExecutionError
- type ExecutionLogs
- type LsResult
- type Method
- type Option
- func WithBaseURL(baseURL string) Option
- func WithClient(client *http.Client) Option
- func WithCwd(cwd string) Option
- func WithLogger(logger *slog.Logger) Option
- func WithMetaData(metaData map[string]string) Option
- func WithTemplate(template SandboxTemplate) Option
- func WithWsURL(wsURL func(s *Sandbox) string) Option
- type Process
- func (p *Process) Done() <-chan struct{}
- func (p *Process) Start(ctx context.Context) (err error)
- func (p *Process) SubscribeExit(ctx context.Context) (chan Event, chan error)
- func (p *Process) SubscribeStderr(ctx context.Context) (chan Event, chan error)
- func (p *Process) SubscribeStdout(ctx context.Context) (chan Event, chan error)
- type ProcessEvents
- type ProcessOption
- type Request
- type Response
- type Result
- type Sandbox
- func (s *Sandbox) Close(ctx context.Context) error
- func (s *Sandbox) GetHost(port int) string
- func (s *Sandbox) KeepAlive(ctx context.Context, timeout time.Duration) error
- func (s *Sandbox) Ls(ctx context.Context, path string) ([]LsResult, error)
- func (s *Sandbox) Mkdir(ctx context.Context, path string) error
- func (s *Sandbox) NewProcess(cmd string, opts ...ProcessOption) (*Process, error)
- func (s *Sandbox) Read(ctx context.Context, path string) (string, error)
- func (s *Sandbox) ReadBytes(ctx context.Context, path string) ([]byte, error)
- func (s *Sandbox) Reconnect(ctx context.Context) (err error)
- func (s *Sandbox) Stop(ctx context.Context) error
- func (s *Sandbox) Watch(ctx context.Context, path string, eCh chan<- Event) error
- func (s *Sandbox) Write(ctx context.Context, path string, data []byte) error
- func (s *Sandbox) WriteBytes(ctx context.Context, path string, data []byte) error
- type SandboxTemplate
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type APIError ¶
type APIError struct {
Code int `json:"code,omitempty"` // Code is the code of the error.
Message string `json:"message"` // Message is the message of the error.
}
APIError is the error of the API.
type CodeInterpreter ¶
type CodeInterpreter struct {
*Sandbox
}
CodeInterpreter is a specialized sandbox for executing AI-generated code. It uses a Jupyter-like notebook environment.
func NewCodeInterpreter ¶
func NewCodeInterpreter(ctx context.Context, apiKey string, opts ...Option) (*CodeInterpreter, error)
NewCodeInterpreter creates a new CodeInterpreter sandbox. By default, it uses the "code-interpreter-v1" template.
type ErrMissingRequiredArgument ¶
ErrMissingRequiredArgument is returned when a required argument is missing.
func (ErrMissingRequiredArgument) Error ¶
func (e ErrMissingRequiredArgument) Error() string
Error implements the error interface for ErrMissingRequiredArgument.
type ErrToolArgument ¶
ErrToolArgument is returned when an argument is invalid.
func (ErrToolArgument) Error ¶
func (e ErrToolArgument) Error() string
Error implements the error interface for ErrToolArgument.
type ErrToolNotFound ¶
type ErrToolNotFound struct {
ToolName string
}
ErrToolNotFound is returned when a tool is not found.
func (ErrToolNotFound) Error ¶
func (e ErrToolNotFound) Error() string
Error implements the error interface for ErrToolNotFound.
type Event ¶
type Event struct {
Path string `json:"path"` // Path is the path of the event.
Name string `json:"name"` // Name is the name of file or directory.
Timestamp int64 `json:"timestamp"` // Timestamp is the timestamp of the event.
Error string `json:"error"` // Error is the possible error of the event.
Params EventParams `json:"params"` // Params is the parameters of the event.
}
Event is a file system event.
type EventParams ¶
type EventParams struct {
Subscription string `json:"subscription"` // Subscription is the subscription id of the event.
Result EventResult `json:"result"` // Result is the result of the event.
}
EventParams is the params for subscribing to a process event.
type EventResult ¶
type EventResult struct {
Type string `json:"type"`
Line string `json:"line"`
Timestamp int64 `json:"timestamp"`
IsDirectory bool `json:"isDirectory"`
Error string `json:"error"`
}
EventResult is a file system event response.
type Execution ¶
type Execution struct {
Results []Result `json:"results"`
Logs ExecutionLogs `json:"logs"`
Error *ExecutionError `json:"error"`
}
Execution represents the full result of a cell execution.
type ExecutionError ¶
type ExecutionError struct {
Name string `json:"name"`
Value string `json:"value"`
Traceback string `json:"traceback"`
}
ExecutionError contains the error information from a cell execution.
type ExecutionLogs ¶
ExecutionLogs contains the stdout and stderr from a cell execution.
type LsResult ¶
type LsResult struct {
Name string `json:"name"` // Name is the name of the file or directory.
IsDir bool `json:"isDir"` // isDir is true if the entry is a directory.
}
LsResult is a result of the list request.
type Option ¶
type Option func(*Sandbox)
Option is an option for the sandbox.
func WithBaseURL ¶
WithBaseURL sets the base URL for the e2b sandbox.
func WithClient ¶
WithClient sets the client for the e2b sandbox.
func WithLogger ¶
WithLogger sets the logger for the e2b sandbox.
func WithMetaData ¶
WithMetaData sets the meta data for the e2b sandbox.
func WithTemplate ¶
func WithTemplate(template SandboxTemplate) Option
WithTemplate sets the template for the e2b sandbox.
type Process ¶
type Process struct {
Cwd string // cwd is process's current working directory.
Env map[string]string // env is process's environment variables.
// contains filtered or unexported fields
}
Process is a process in the sandbox.
func (*Process) Done ¶
func (p *Process) Done() <-chan struct{}
Done returns a channel that is closed when the process is done.
func (*Process) SubscribeExit ¶
SubscribeExit subscribes to the process's exit.
func (*Process) SubscribeStderr ¶
SubscribeStderr subscribes to the process's stderr.
type ProcessEvents ¶
type ProcessEvents string
ProcessEvents is a process event type.
const ( // OnStdout is the event for the stdout. OnStdout ProcessEvents = "onStdout" // OnStderr is the event for the stderr. OnStderr ProcessEvents = "onStderr" // OnExit is the event for the exit. OnExit ProcessEvents = "onExit" )
type ProcessOption ¶
type ProcessOption func(*Process)
ProcessOption is an option for the process.
func ProcessWithCwd ¶
func ProcessWithCwd(cwd string) ProcessOption
ProcessWithCwd sets the current working directory for the process.
func ProcessWithEnv ¶
func ProcessWithEnv(env map[string]string) ProcessOption
ProcessWithEnv sets the environment variables for the process.
type Request ¶
type Request struct {
JSONRPC string `json:"jsonrpc"` // JSONRPC is the JSON-RPC version of the request.
Method Method `json:"method"` // Method is the request method.
ID int `json:"id"` // ID of the request.
Params []any `json:"params"` // Params of the request.
}
Request is a JSON-RPC request.
type Response ¶
type Response[T any, Q any] struct { ID int `json:"id"` // ID of the response. Result T `json:"result"` // Result of the response. Error Q `json:"error"` // Error of the message. }
Response is a JSON-RPC response.
type Result ¶
type Result struct {
IsMainResult bool `json:"isMainResult"`
Data map[string]string `json:"data"`
Formats []string `json:"formats"`
}
Result represents an output from a cell execution.
type Sandbox ¶
type Sandbox struct {
ID string `json:"sandboxID"` // ID of the sandbox.
ClientID string `json:"clientID"` // ClientID of the sandbox.
Cwd string `json:"cwd"` // Cwd is the sandbox's current working directory.
Template SandboxTemplate `json:"templateID"` // Template of the sandbox.
Metadata map[string]string `json:"metadata"` // Metadata of the sandbox.
Map *sync.Map `json:"-"` // Map is the map of the sandbox.
// contains filtered or unexported fields
}
Sandbox is a code sandbox.
The sandbox is like an isolated, but interactive system.
func ConnectSandbox ¶
func ConnectSandbox( ctx context.Context, sandboxID string, apiKey string, opts ...Option, ) (*Sandbox, error)
ConnectSandbox connects to an existing sandbox.
func NewSandbox ¶
NewSandbox creates a new sandbox.
func (*Sandbox) Ls ¶
Ls lists the files and/or directories in the sandbox file system at the given path.
func (*Sandbox) NewProcess ¶
func (s *Sandbox) NewProcess( cmd string, opts ...ProcessOption, ) (*Process, error)
NewProcess creates a new process startable in the sandbox.
func (*Sandbox) Watch ¶
Watch watches a directory in the sandbox file system.
This is intended to be run in a goroutine as it will block until the connection is closed, an error occurs, or the context is canceled.
While blocking, filesystem events will be written to the provided channel.
type SandboxTemplate ¶
type SandboxTemplate string
SandboxTemplate is a sandbox template.
const ( // DefaultCodeInterpreterTemplate is the default template for CodeInterpreter. DefaultCodeInterpreterTemplate SandboxTemplate = "code-interpreter-v1" )