Documentation
¶
Overview ¶
Package computer is a sandboxed computer for an agent: a shell (gbash) over mounted directories, with the tools of an MCP server as commands and as a Python package, jq and sqlite3, and python3 running as WebAssembly. Nothing in it reaches the network or the host, except through those tools and mounts.
Index ¶
- func TerminalHandler(get func(*http.Request) (*Computer, string, error), opts ...TerminalOption) http.Handler
- func WebHandler(get func(*http.Request) (*Computer, string, error), errorLog ...ErrorLog) http.Handler
- type Computer
- func (c *Computer) About(toolCommands, toolModules []string) string
- func (c *Computer) Close() error
- func (c *Computer) Complete(ctx context.Context, cwd, before string) []string
- func (c *Computer) Describe(ctx context.Context) (string, error)
- func (c *Computer) Environment() *Environment
- func (c *Computer) Exec(ctx context.Context, req *gbash.ExecutionRequest) (*gbash.ExecutionResult, error)
- func (c *Computer) MCPServer(ctx context.Context, limit time.Duration) (*mcp.Server, error)
- func (c *Computer) Mounts() []Mount
- func (c *Computer) Options(ctx context.Context) ([]gbash.Option, error)
- func (c *Computer) Prompt(ctx context.Context, term io.ReadWriter, workDir string, ...) error
- func (c *Computer) Session(ctx context.Context) (*gbash.Session, error)
- func (c *Computer) ToolCommands() []string
- func (c *Computer) ToolModules() []string
- type Config
- type Environment
- func (e *Environment) List(ctx context.Context, name string) ([]fs.FileInfo, error)
- func (e *Environment) Mkdir(ctx context.Context, name string, parents bool) error
- func (e *Environment) Read(ctx context.Context, name string, offset, length int64) ([]byte, error)
- func (e *Environment) Remove(ctx context.Context, name string, recursive bool) error
- func (e *Environment) Rename(ctx context.Context, from, to string) error
- func (e *Environment) Run(ctx context.Context, script, dir string, env map[string]string, ...) (int, error)
- func (e *Environment) Stat(ctx context.Context, name string, follow bool) (fs.FileInfo, error)
- func (e *Environment) Truncate(ctx context.Context, name string, size int64) error
- func (e *Environment) Write(ctx context.Context, name string, content []byte, appending bool) error
- type ErrorLog
- type Mount
- type Python
- type TerminalOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func TerminalHandler ¶
func TerminalHandler(get func(*http.Request) (*Computer, string, error), opts ...TerminalOption) http.Handler
TerminalHandler serves a computer's Prompt to a terminal in a browser, over a WebSocket. get is the host's: it returns the requester's computer and the working directory to start in, after the host's own checks of who is asking; an error that is fs.ErrPermission refuses the request with 403, and another with 500, which is logged and not told to the client. The computer stays the host's, to close. A request that is not a WebSocket upgrade, or whose page the origin rule refuses (the request's own origin alone, unless CheckOrigin says otherwise), is refused before get is called.
Binary messages carry what is typed in, and the terminal's output out. A text message {"cols": 80, "rows": 24} is the terminal's size. Closing the socket ends the prompt and the command that is running, as does a client that answers no ping for a minute or takes more than ten seconds to take a message; the prompt's end, at exit or Ctrl-D, closes the socket.
func WebHandler ¶
func WebHandler(get func(*http.Request) (*Computer, string, error), errorLog ...ErrorLog) http.Handler
WebHandler serves a folder of a computer as python3 -m http.server serves one: the request's path is a path under the folder, a file is sent as it is, a Python file's source too, and a folder as its index.html or as a list of what it holds (http.FileServer).
A Python web application is asked for by its name in the path, written as gunicorn and uvicorn write one, module:callable, where the module's file is: /shop/app:app/cart is the object app of shop/app.py, asked for /cart, and /shop/app.py is that file. So nothing is declared: a file is reached by its name, and an application by its own. The application is a WSGI one (PEP 3333), served as a CGI script is (RFC 3875): each request is a python3 of its own, in the module's folder, so requests run side by side and nothing is kept in memory between them. A request that ends stops its python3, and an application that fails once its answer has begun ends the answer short.
get is the host's, as TerminalHandler's is: it returns the requester's computer and the folder, after the host's own checks of who is asking; a folder that is not absolute is under the computer's WorkDir. An error that is fs.ErrPermission refuses the request with 403, and another with 500, which is logged and not told to the client. The computer stays the host's, to close. The handler may be mounted under http.StripPrefix.
Policy is the host's too: how large a request may be (http.MaxBytesHandler; a request to an application is read whole before the application runs), how long it may take (a deadline on the request's context), and which of the request's headers an application sees: every one reaches it, a credential or a cookie too, unless the host removes it first. An application's error output is the host's own, unless an ErrorLog says where.
Types ¶
type Computer ¶
type Computer struct {
// contains filtered or unexported fields
}
A Computer is one agent's computer. Its methods are safe for concurrent use.
func (*Computer) About ¶
About is what an agent is told about this computer, given the commands that call tools and their modules in Python, as ToolCommands and ToolModules name them.
func (*Computer) Close ¶
Close removes what the computer keeps on the host. The caller's mounts are the caller's and stay.
func (*Computer) Complete ¶
Complete is what could complete the last word of before, a line typed in the shell at cwd: a command when it is the first word, a tool or one of its arguments after a command that calls tools, and otherwise a file. Only such a command is run, to list its tools; completing a line runs nothing else of it.
func (*Computer) Describe ¶
Describe is what a model is told about this computer, with the commands that call tools as they are now.
func (*Computer) Environment ¶
func (c *Computer) Environment() *Environment
Environment is the computer as an Environment. Every method may be called from any goroutine.
func (*Computer) Exec ¶
func (c *Computer) Exec(ctx context.Context, req *gbash.ExecutionRequest) (*gbash.ExecutionResult, error)
Exec runs a script or a command in the computer's shell, with the tools as they are now. A request with no Env starts where the last one ended: gbash keeps files between requests, and Exec carries the working directory and the exported variables.
func (*Computer) MCPServer ¶
MCPServer is the computer as an MCP server for an agent, with the tools bash, read, edit and write. The caller connects it: server.Run(ctx, &mcp.StdioTransport{}) for an agent that starts the computer as a program, or server.Connect with one end of mcp.NewInMemoryTransports for an agent in this process. limit is how long a bash call may run when the agent sets no shorter time. The file tools and their wording are gbash's codingtools' read, edit and write.
A computer is one agent's: every server made from it shares the one shell, with its working directory and variables. The file tools take a relative path from the computer's WorkDir, wherever the shell has since moved. The bash tool describes the commands that call tools as they are when MCPServer is called.
func (*Computer) Options ¶
Options is the computer as gbash options, with the tools as they are now, for gbash's own front ends: its command-line program, its server and its tool for a model.
func (*Computer) Prompt ¶
func (c *Computer) Prompt(ctx context.Context, term io.ReadWriter, workDir string, interrupt <-chan struct{}, size <-chan [2]int) error
Prompt is the computer's shell for a person at a terminal: a line editor with history and Tab completion, which gbash's own interactive mode does not have. Each line is run in the computer; the working directory and exported variables carry from one to the next, and are this prompt's own, as a shell's are: two prompts on one computer do not see each other's, and Exec does not see theirs. It returns at the end of term's input, at Ctrl-D on an empty line, at exit, or when ctx ends, and ends the command that is running.
term is a terminal in raw mode, or what stands for one, such as a WebSocket to a terminal in a browser: Prompt reads keystrokes from it and writes output for a terminal to it, a newline as "\r\n". While a command runs, what is typed is echoed and given to the command a line at a time, as a terminal's line discipline does, and Ctrl-C ends the command and its read of the terminal. Ctrl-C at the prompt drops the line. A value on interrupt ends the running command as Ctrl-C does, and a value on size, columns and rows, is the terminal's new size; either may be nil. Prompt reads term from one goroutine until it returns, and that read may outlast it: the caller closes term, or stops giving it input, to end the read.
After a command ends, Prompt writes "\x1b]133;D;code\x07", the mark of shell integration in terminals (OSC 133) that a command has ended with its exit code: 130 for one interrupted.
func (*Computer) Session ¶
Session is the computer's shell session, for its filesystem and for interactive use. The tools are listed on every call: when they have changed, the commands and the Python package are made again and the session is a new one over the same files.
func (*Computer) ToolCommands ¶
ToolCommands are the commands that call tools, as the shell last built names them.
func (*Computer) ToolModules ¶
ToolModules are the modules of the Python package tools, one per command of ToolCommands, in its order.
type Config ¶
type Config struct {
Mounts []Mount
WorkDir string // where a command starts; a mount, or a directory in one
User string // whose computer it is: $USER, $LOGNAME, $HOME as /home/<user>, and what whoami, id and logname name; gbash's own default, and $HOME WorkDir, when empty
Commands []commands.Command // commands beyond the shell's own
Tools []tools.Source // the MCP servers whose tools become commands and the Python package `tools`
Python *Python // python3; nil for none
// Options are further gbash options, applied last: gbash.WithHTTPAccess or WithNetwork for curl, which the
// computer otherwise lacks, WithLimitOverrides, WithTracing, WithPolicy.
Options []gbash.Option
}
Config is what one computer is made of. Only Mounts is needed for a working shell.
type Environment ¶
type Environment struct {
// contains filtered or unexported fields
}
An Environment is the computer as the place an agent harness's own tools act: shell commands, and files, under the rules the shell's commands are held to. Its methods use only the standard library's types, so a harness can declare the interface it needs and be given this without either importing the other.
A name is an absolute path in the computer. An error is a *fs.PathError naming that path and never the host's, and errors.Is tells fs.ErrNotExist, fs.ErrPermission (also a write the rules refuse), fs.ErrExist, fs.ErrInvalid (also a file past the size limit), syscall.ENOTDIR, syscall.EISDIR and syscall.ENOTEMPTY apart. Which of them a misuse gets is the mount's to say: a host directory answers as a file system does, and a mount that is another afero.Fs as that one does: afero's memory filesystem allows a file made under a file, which turns that file into a directory and loses what it held.
func (*Environment) Mkdir ¶
Mkdir makes a directory, and with parents every directory above it that is missing; with parents, one that is there already is no error.
func (*Environment) Read ¶
Read reads length bytes of a file from offset, or as many as it has; the rest of it when length is negative.
func (*Environment) Remove ¶
Remove removes a file or an empty directory, or with recursive a directory and everything in it. Nothing there to remove is fs.ErrNotExist.
func (*Environment) Rename ¶
func (e *Environment) Rename(ctx context.Context, from, to string) error
Rename moves a file or a directory.
func (*Environment) Run ¶
func (e *Environment) Run(ctx context.Context, script, dir string, env map[string]string, replaceEnv bool, stdout, stderr io.Writer) (int, error)
Run runs script in the computer's shell with dir as its working directory, and returns its exit code. env is added to the shell's environment, or is the whole of it when replaceEnv is set. Each Run has a shell of its own over the computer's files: nothing is carried from one to the next, and one neither waits for another nor for the computer's own Exec. Output is written to stdout and stderr while the command runs, and a pipeline's commands may write at the same time. When ctx ends the command is stopped and ctx's error returned. A command that ran and failed, or that the shell does not have (127), is an exit code and no error.
type ErrorLog ¶
An ErrorLog is where the error output of the application a request asks goes, a failure's traceback among it, such as a file of the computer its author reads; os.Stderr for a request it returns nil for. The writer is the host's: it is written to while the request runs, by requests side by side, and is not closed.
type Mount ¶
A Mount is a directory the shell and every program see at the path Guest: the filesystem FS, which can be anything that implements afero.Fs, or, when FS is nil, the host directory Host (see files.Dir).
type Python ¶
type Python struct {
// contains filtered or unexported fields
}
Python is a wasi-python, compiled once and shared by every computer of the process.
func EmbeddedPython ¶
EmbeddedPython compiles the wasi-python release this module carries: CPython with numpy, pandas, scipy, scikit-learn, statsmodels, matplotlib, Pillow, lxml, python-docx, python-pptx and more, and Flask. On first use it unpacks the release, about 270 MB, into computer/python-<checksum> in the user's cache directory (os.UserCacheDir), and later processes reuse it; a host must not mount that directory. Each call compiles the program again, so a host calls it once and shares the Python, as with LoadPython.
func LoadPython ¶
LoadPython reads the wasi-python release unpacked in dir, laid out as EmbeddedPython unpacks it: python.wasm, lib/python315.zip, mpl-data/, matplotlib's data, and, when it has more packages than the zip's, lib/site-packages/, and starts compiling it; the first python3 waits for the compile, and fails with its error. p's Binary is read from the file, its host call is answered by the computer's tools, and a zero MemoryPages is 8192 (512 MiB); its other fields are the caller's.
type TerminalOption ¶
type TerminalOption func(*terminalOptions)
A TerminalOption changes how TerminalHandler serves.
func CheckOrigin ¶
func CheckOrigin(check func(*http.Request) bool) TerminalOption
CheckOrigin is the rule for which pages may open the terminal, for a host whose pages are on another origin than its own, such as a development server's, or behind a proxy that changes the Host header. The rule is the host's: the terminal is the person a cookie names, which a page of any site may send. Without this option a page of the request's own origin alone may, as with CheckOrigin(nil).
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
computer
command
Command computer runs a sandboxed computer from the command line.
|
Command computer runs a sandboxed computer from the command line. |
|
Package extra holds the PDF commands the shell lacks, pdftotext and pdftoppm.
|
Package extra holds the PDF commands the shell lacks, pdftotext and pdftoppm. |
|
Package files has the filesystems a computer can be given.
|
Package files has the filesystems a computer can be given. |
|
Package tools turns the tools of an MCP server into what a shell and a script can use: a command per group of tools, with a subcommand per tool, and a Python package with a module per group and a function per tool.
|
Package tools turns the tools of an MCP server into what a shell and a script can use: a command per group of tools, with a subcommand per tool, and a Python package with a module per group and a function per tool. |
|
Package wasm runs a WebAssembly program built for WASI the way a command runs: with arguments, an environment, standard streams and directories, to an exit code.
|
Package wasm runs a WebAssembly program built for WASI the way a command runs: with arguments, an environment, standard streams and directories, to an exit code. |