browser

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package browser drives a long-lived Chrome instance via the Chrome DevTools Protocol (chromedp) so the agent can fully interact with web pages as tools — the same dispatch as read_file/bash. It exposes the full browser interaction surface a user has: navigate, click, type, scroll, hover, keyboard, dropdowns, history, tabs, screenshots, console logs, and arbitrary JS.

The Session wraps a persistent chromedp context created once (on New) and reused across tool calls — a single Chrome process for the whole agent session, torn down on Close. It tracks multiple tabs (each a child chromedp context) and a rolling console-log buffer captured via CDP Runtime events. It reuses browserrender.Find to locate the Chrome binary (no new dependency, no Node).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Session

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

Session is a persistent Chrome instance controlled via CDP, with multi-tab support and a rolling console-log buffer.

func New

func New() (*Session, error)

New launches a Chrome instance with a visible window (headed) and returns a persistent Session with one open tab. You can watch Chrome work as the agent navigates, clicks, and types. The Chrome binary is discovered via browserrender.Find — a clear error is returned when none is available.

func (*Session) Alive

func (s *Session) Alive() bool

Alive reports whether the Chrome process backing this session is still reachable. The runtime calls this on the cached session before each browser tool call so that a Chrome that died (the user quit the window, the OS killed it, a crash) is detected and relaunched — without it, every tool call would drive a dead chromedp context and return "context canceled" forever.

Two independent signals: chromedp's LostConnection channel (closed when the CDP websocket to Chrome drops) and a zero-signal probe of the OS process (ESRCH = the process is gone). Either being dead means the session is dead.

func (*Session) Back

func (s *Session) Back(ctx context.Context) error

Back navigates the current tab to the previous page in browser history.

func (*Session) ClearConsole

func (s *Session) ClearConsole()

ClearConsole empties the console buffer.

func (*Session) Click

func (s *Session) Click(ctx context.Context, selector string) error

Click an element matching a CSS selector.

func (*Session) Close

func (s *Session) Close()

Close tears down the Chrome process and releases all CDP resources. Safe to call multiple times.

func (*Session) CloseTab

func (s *Session) CloseTab(index int) error

CloseTab closes the tab at the given 1-based index. If closing the active tab, focus moves to the previous tab (or the first one). Closing the last tab is not allowed (the browser needs at least one tab).

func (*Session) Console

func (s *Session) Console(level string) []string

Console returns the captured console messages, optionally filtered by level. When level is empty, all entries are returned. Each entry is formatted as "[tab N] LEVEL: text". Entries are returned oldest-first.

func (*Session) Eval

func (s *Session) Eval(ctx context.Context, js string) (string, error)

Eval runs a JavaScript expression and returns its result as a string.

func (*Session) Forward

func (s *Session) Forward(ctx context.Context) error

Forward navigates the current tab to the next page in browser history.

func (*Session) Hover

func (s *Session) Hover(ctx context.Context, selector string) error

Hover moves the mouse over an element matching a CSS selector, triggering hover states, dropdown menus, tooltips, etc.

func (*Session) ListTabs

func (s *Session) ListTabs() []string

ListTabs returns the current tabs as "N. url — title" strings (1-based).

func (*Session) Navigate

func (s *Session) Navigate(ctx context.Context, url string) error

Navigate loads a URL in the current tab and waits for the body to be ready.

func (*Session) NewTab

func (s *Session) NewTab(ctx context.Context) (int, error)

NewTab opens a new tab (about:blank) and switches focus to it. Returns the tab index (1-based, for the agent's reference).

func (*Session) PressKey

func (s *Session) PressKey(ctx context.Context, key string) error

PressKey sends a key event (e.g. "Enter", "Escape", "Tab", "ArrowDown") to the page. The key is sent to whatever element currently has focus. Use after browser_click or browser_type to submit forms, close modals, navigate dropdowns, etc.

func (*Session) Screenshot

func (s *Session) Screenshot(ctx context.Context, fullPage bool) ([]byte, error)

Screenshot captures the current viewport as a PNG. When fullPage is true it captures the entire scrollable page instead (can be very large — token cost).

func (*Session) Scroll

func (s *Session) Scroll(ctx context.Context, dx, dy int) error

Scroll scrolls the page by the given x and y deltas in CSS pixels. Positive y scrolls down; positive x scrolls right. Use (0, 500) to scroll down half a viewport, or a large y to reach the bottom.

func (*Session) ScrollTo

func (s *Session) ScrollTo(ctx context.Context, selector string) error

ScrollTo scrolls the element matching the selector into view (centered).

func (*Session) Select

func (s *Session) Select(ctx context.Context, selector, value string) error

Select picks an option in a <select> element by value (the option's value attribute). The selector must match a <select> element.

func (*Session) SwitchTab

func (s *Session) SwitchTab(index int) (url, title string, err error)

SwitchTab switches focus to the tab at the given 1-based index. Returns the tab's current URL and title.

func (*Session) TabCount

func (s *Session) TabCount() int

TabCount returns the number of open tabs.

func (*Session) Text

func (s *Session) Text(ctx context.Context) (string, error)

Text returns the visible text content of the current page (body.innerText).

func (*Session) Type

func (s *Session) Type(ctx context.Context, selector, text string) error

Type text into an element matching a CSS selector.

Jump to

Keyboard shortcuts

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