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
- Variables
- func FindChrome() string
- func ServeHost(ctx context.Context, o HostOptions) error
- func StateDirFor(dataDir string) string
- type Client
- type ConsoleEntry
- type Control
- type Host
- type HostOptions
- type Manager
- func (m *Manager) Click(selector string, x, y float64) (Result, error)
- func (m *Manager) Close()
- func (m *Manager) Console(limit int) Result
- func (m *Manager) Eval(expression string) (Result, error)
- func (m *Manager) Network(limit int) Result
- func (m *Manager) Open(rawURL string, viewport string) (Result, error)
- func (m *Manager) Press(key string) (Result, error)
- func (m *Manager) Screenshot(full bool) (Result, error)
- func (m *Manager) Scroll(dx, dy float64) (Result, error)
- func (m *Manager) Snapshot() (Result, error)
- func (m *Manager) Type(selector, text string, clear, submit bool) (Result, error)
- func (m *Manager) Viewport(spec string) (Result, error)
- type NetworkEntry
- type OpError
- type Options
- type Result
- type ViewportSpec
Constants ¶
const DefaultViewport = "1280x800"
DefaultViewport is the size a fresh page gets before any emulation.
Variables ¶
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 ¶
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.
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 ¶
Start builds a Manager. Chrome is launched on the first operation, so an agent that never touches the browser pays nothing.
func (*Manager) Click ¶
Click presses the first element matching selector, or a viewport coordinate when no selector is given.
func (*Manager) Screenshot ¶
Screenshot captures the viewport (or the whole page) into DataDir/shots and returns the file path an agent can read or attach.
func (*Manager) Snapshot ¶
Snapshot returns the page's identity, visible text, interactive controls and accessibility outline — enough to decide what to do next without guessing.
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.
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 ¶
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).