browser

package
v0.1.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 9, 2026 License: AGPL-3.0 Imports: 32 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package browser is the node's headless browser: one Chrome tab an agent can open, read, click, type into, resize, screenshot and inspect through the console and network logs. It is what makes "check the page I just built" a real step instead of a claim.

The browser is deliberately separate from the human preview. The console frames a project's real dev server in the reader's browser; this package drives its own headless Chrome on the node, so an agent can verify a page without stealing the reader's pane, and works even when no one is watching.

Chrome is an external binary, auto-detected here or named by OPENPANDA_CHROME. Nothing in this package requires CGO.

Index

Constants

View Source
const DefaultViewport = "1280x800"

DefaultViewport is the size a fresh page gets before any emulation.

Variables

View Source
var ErrNoChrome = errors.New("browser: no Chrome/Chromium found (install one or set OPENPANDA_CHROME)")

ErrNoChrome says the node has no browser this package can drive.

Functions

func FindChrome

func FindChrome() string

FindChrome looks for a browser this package can drive. OPENPANDA_CHROME wins; the rest are the install locations and PATH names of Chrome, Chromium and Edge on each platform.

func ServeHost

func ServeHost(ctx context.Context, o HostOptions) error

ServeHost runs until ctx is canceled, the host is closed by a `close` op, or it has been idle for the configured period.

func StateDirFor

func StateDirFor(dataDir string) string

StateDirFor names the node's browser state directory. The caller passes the data directory that also holds panda.db.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client reaches (and if needed starts) the node's browser host.

func NewClient

func NewClient(stateDir, exe string) *Client

NewClient builds a client for the host rooted at stateDir. exe is the panda binary used to spawn a missing host; empty resolves os.Executable.

func (*Client) Do

func (c *Client) Do(ctx context.Context, op string, args map[string]any) (Result, error)

Do runs one operation, starting the host first when none is healthy.

type ConsoleEntry

type ConsoleEntry struct {
	At    time.Time `json:"at"`
	Type  string    `json:"type"`
	Text  string    `json:"text"`
	URL   string    `json:"url,omitempty"`
	Line  int64     `json:"line,omitempty"`
	Stack string    `json:"stack,omitempty"`
}

ConsoleEntry is one line the page wrote to the console, or one uncaught exception.

type Control

type Control struct {
	Selector string  `json:"selector"`
	Role     string  `json:"role"`
	Name     string  `json:"name"`
	Text     string  `json:"text,omitempty"`
	Value    string  `json:"value,omitempty"`
	X        float64 `json:"x"`
	Y        float64 `json:"y"`
	W        float64 `json:"w"`
	H        float64 `json:"h"`
	Disabled bool    `json:"disabled,omitempty"`
}

Control is one interactive element the agent can address by CSS selector.

type Host

type Host struct {
	// contains filtered or unexported fields
}

Host is the local control server.

type HostOptions

type HostOptions struct {
	StateDir string
	// IdleTimeout ends the host (and Chrome) after a quiet period. Zero uses
	// 15 minutes.
	IdleTimeout time.Duration
	// IdleCheck is how often idleness is tested. Zero uses 30 seconds.
	IdleCheck time.Duration
	// ChromePath overrides discovery (OPENPANDA_CHROME also works).
	ChromePath string
	Logger     *slog.Logger
}

HostOptions configure ServeHost.

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager owns one headless browser and its single tab. Operations are serialized: one tab cannot be driven from two goroutines at once.

func Start

func Start(o Options) *Manager

Start builds a Manager. Chrome is launched on the first operation, so an agent that never touches the browser pays nothing.

func (*Manager) Click

func (m *Manager) Click(selector string, x, y float64) (Result, error)

Click presses the first element matching selector, or a viewport coordinate when no selector is given.

func (*Manager) Close

func (m *Manager) Close()

Close ends the tab and the browser.

func (*Manager) Console

func (m *Manager) Console(limit int) Result

Console returns the newest console messages, oldest first.

func (*Manager) Eval

func (m *Manager) Eval(expression string) (Result, error)

Eval runs a JavaScript expression and returns its JSON value.

func (*Manager) Network

func (m *Manager) Network(limit int) Result

Network returns the newest requests, oldest first.

func (*Manager) Open

func (m *Manager) Open(rawURL string, viewport string) (Result, error)

Open navigates the tab to url and returns what landed there.

func (*Manager) Press

func (m *Manager) Press(key string) (Result, error)

Press sends one named key to the focused element.

func (*Manager) Screenshot

func (m *Manager) Screenshot(full bool) (Result, error)

Screenshot captures the viewport (or the whole page) into DataDir/shots and returns the file path an agent can read or attach.

func (*Manager) Scroll

func (m *Manager) Scroll(dx, dy float64) (Result, error)

Scroll sends a wheel event at the viewport centre.

func (*Manager) Snapshot

func (m *Manager) Snapshot() (Result, error)

Snapshot returns the page's identity, visible text, interactive controls and accessibility outline — enough to decide what to do next without guessing.

func (*Manager) Type

func (m *Manager) Type(selector, text string, clear, submit bool) (Result, error)

Type enters text into selector (or the focused element). clear replaces the field's value first; submit presses Enter afterwards.

func (*Manager) Viewport

func (m *Manager) Viewport(spec string) (Result, error)

Viewport applies an emulated viewport: mobile, tablet, desktop, or WIDTHxHEIGHT.

type NetworkEntry

type NetworkEntry struct {
	At         time.Time `json:"at"`
	Method     string    `json:"method"`
	URL        string    `json:"url"`
	Type       string    `json:"type,omitempty"`
	Status     int64     `json:"status,omitempty"`
	MimeType   string    `json:"mime,omitempty"`
	Failed     string    `json:"failed,omitempty"`
	DurationMS int64     `json:"ms,omitempty"`
}

NetworkEntry is one request the page made, with its outcome when known.

type OpError

type OpError string

OpError is a tool-level failure reported by the host — a bad selector, a page that is not open, Chrome missing. It is an answer, not a broken transport, so the client returns it instead of replacing the host.

func (OpError) Error

func (e OpError) Error() string

type Options

type Options struct {
	// ChromePath overrides discovery (OPENPANDA_CHROME also works).
	ChromePath string
	// DataDir holds the browser profile and screenshots.
	DataDir string
	Logger  *slog.Logger
}

Options configure a Manager.

type Result

type Result struct {
	URL        string         `json:"url,omitempty"`
	Title      string         `json:"title,omitempty"`
	Text       string         `json:"text,omitempty"`
	Tree       string         `json:"tree,omitempty"`
	Controls   []Control      `json:"controls,omitempty"`
	Value      any            `json:"value,omitempty"`
	Screenshot string         `json:"screenshot,omitempty"`
	Width      int            `json:"width,omitempty"`
	Height     int            `json:"height,omitempty"`
	Console    []ConsoleEntry `json:"console,omitempty"`
	Network    []NetworkEntry `json:"network,omitempty"`
	Message    string         `json:"message,omitempty"`
}

Result is what an operation hands back. Every field is optional; the host serializes it as-is and the CLI/MCP format it.

type ViewportSpec

type ViewportSpec struct {
	Width, Height int64
	Scale         float64
	Mobile, Touch bool
	Reset         bool
}

ViewportSpec is one emulated screen.

func ParseViewport

func ParseViewport(spec string) (ViewportSpec, error)

ParseViewport understands the three names the console uses plus WIDTHxHEIGHT (optionally at a scale, WIDTHxHEIGHT@SCALE).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL