{"modulePath":"github.com/memcode-ai/memcode","version":"v0.56.16","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package browser\n\nPackage 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, uploads, viewport, screenshots, console logs, and arbitrary JS.\n\nThe 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. The profile is EPHEMERAL (a fresh temp profile per launch): no existing cookies or logins, nothing persisted across sessions. It reuses browserrender.Find to locate the Chrome binary (no new dependency, no Node).\n\nEvery operation runs under a bounded, caller-cancellable context derived from the tab's context: the timeout/cancel propagates into the in-flight CDP command (chromedp aborts it) while the tab itself survives for the next op.\n\n## Constants\n\n```go\nconst ChromeDevToolsMCPPackage = \"chrome-devtools-mcp@\" + ChromeDevToolsMCPVersion\n```\n\n```go\nconst ChromeDevToolsMCPVersion = \"1.8.0\"\n```\n\n## Types\n\n```go\ntype Controller interface {\n\tClose() error\n\tNavigate(context.Context, string) error\n\tNewTab(context.Context, string) error\n}\n```\nController is the stable browser boundary shared by ephemeral and brokered backends. Calls remain typed; autonomous agents never receive raw MCP access.\n\n```go\ntype EphemeralController struct{ *Session }\n```\nEphemeralController identifies the existing fresh-profile backend used by ordinary sessions. Session remains the concrete implementation while callers migrate behind Controller.\n\n```go\ntype Options struct {\n\t// Headless runs Chrome without a window — required when there is no desktop\n\t// session (a gateway job under launchd/systemd). Interactive sessions run\n\t// headed so the user can watch the agent work.\n\tHeadless bool\n}\n```\nOptions configures a Session launch.\n\n```go\ntype RemoteConfig struct{ SocketPath, AgentID, RunID, Token string }\n```\n\n```go\ntype Session struct {\n\t// contains filtered or unexported fields\n}\n```\nSession is a persistent Chrome instance controlled via CDP, with multi-tab support and a rolling console-log buffer.\n\n```go\nfunc New(opt Options) (*Session, error)\n```\nNew launches a Chrome instance and returns a persistent Session with one open tab. The Chrome binary is discovered via browserrender.Find (which honors CHROME\\_PATH) — a clear error is returned when none is available.\n\n```go\nfunc (s *Session) Alive() bool\n```\nAlive 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.\n\nTwo 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.\n\n```go\nfunc (s *Session) Back(ctx context.Context) error\n```\nBack navigates the current tab to the previous page in browser history.\n\n```go\nfunc (s *Session) ClearConsole()\n```\nClearConsole empties the console buffer.\n\n```go\nfunc (s *Session) Click(ctx context.Context, selector string) error\n```\nClick an element matching a CSS selector.\n\n```go\nfunc (s *Session) Close()\n```\nClose tears down the Chrome process and releases all CDP resources. Safe to call multiple times.\n\n```go\nfunc (s *Session) CloseTab(index int) (newActive int, err error)\n```\nCloseTab closes the tab at the given 1-based index via CDP (the real Chrome tab closes, not just our handle). Closing the ACTIVE tab deterministically activates the nearest lower index. Tab 1 hosts the session's CDP connection and can't be closed individually; neither can the last remaining tab. Returns the new active tab's 1-based index.\n\n```go\nfunc (s *Session) Console(level string) []string\n```\nConsole 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.\n\n```go\nfunc (s *Session) Emulate(ctx context.Context, width, height int, mobile bool) error\n```\nEmulate sets the viewport like Resize, and with mobile also emulates a phone (mobile meta-viewport handling and touch), so a site's responsive layout renders the way a phone shows it.\n\n```go\nfunc (s *Session) Eval(ctx context.Context, js string) (string, error)\n```\nEval runs JavaScript in the page and returns the result rendered as a string: strings verbatim, numbers/booleans/objects/arrays as JSON, undefined/null as \"undefined\". Accepts both expressions (\"1+1\", \"document.title\") and statements (\"var x = 5; x \\* 2\").\n\n```go\nfunc (s *Session) Forward(ctx context.Context) error\n```\nForward navigates the current tab to the next page in browser history.\n\n```go\nfunc (s *Session) Hover(ctx context.Context, selector string) error\n```\nHover moves the mouse over an element matching a CSS selector, triggering hover states, dropdown menus, tooltips, etc.\n\n```go\nfunc (s *Session) ListTabs(ctx context.Context) []TabInfo\n```\nListTabs returns the current tabs with LIVE urls and titles.\n\n```go\nfunc (s *Session) Navigate(ctx context.Context, url string) error\n```\nNavigate loads a URL in the current tab and waits for the body to be ready.\n\n```go\nfunc (s *Session) NewTab(ctx context.Context) (int, error)\n```\nNewTab opens a new tab (about:blank) and switches focus to it. Returns the tab index (1-based, for the agent's reference).\n\n```go\nfunc (s *Session) PressKey(ctx context.Context, key string) error\n```\nPressKey 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.\n\n```go\nfunc (s *Session) Resize(ctx context.Context, width, height int) error\n```\nResize sets the viewport to the given CSS dimensions.\n\n```go\nfunc (s *Session) Screenshot(ctx context.Context, fullPage bool) ([]byte, string, error)\n```\nScreenshot captures the current viewport (or the full scrollable page) and returns the image bytes plus their actual mime type. Captures above maxImageBytes are retried as JPEG at decreasing quality so a single screenshot can't flood the model's context.\n\n```go\nfunc (s *Session) Scroll(ctx context.Context, dx, dy int) (string, error)\n```\nScroll scrolls the page by the given x and y deltas in CSS pixels and returns the resulting position (\"scrolled to X,Y (viewport Hpx, total Tpx)\") so the agent knows whether it hit the bottom.\n\n```go\nfunc (s *Session) ScrollTo(ctx context.Context, selector string) error\n```\nScrollTo scrolls the element matching the selector into view (centered).\n\n```go\nfunc (s *Session) Select(ctx context.Context, selector, value string) error\n```\nSelect picks an option in a \\\u003cselect\u003e element by value (the option's value attribute). The selector must match a \\\u003cselect\u003e element.\n\n```go\nfunc (s *Session) SwitchTab(ctx context.Context, index int) (url, title string, err error)\n```\nSwitchTab switches focus to the tab at the given 1-based index and returns its LIVE url and title (queried from the page, not cached).\n\n```go\nfunc (s *Session) TabCount() int\n```\nTabCount returns the number of open tabs.\n\n```go\nfunc (s *Session) Text(ctx context.Context) (string, error)\n```\nText returns the visible text content of the current page (body.innerText).\n\n```go\nfunc (s *Session) Type(ctx context.Context, selector, text string, appendTo bool) error\n```\nType text into an element matching a CSS selector. By default the field is cleared first (what a user means by \"type X into the box\"); append=true keeps the existing value and appends.\n\n```go\nfunc (s *Session) Upload(ctx context.Context, selector string, files []string) error\n```\nUpload sets the files of a file input matching the selector. Path validation (project-root confinement, symlink resolution) is the CALLER's job — this layer only drives CDP.\n\n```go\nfunc (s *Session) WaitFor(ctx context.Context, selector, state string, timeout time.Duration) error\n```\nWaitFor waits until an element matching the selector reaches the given state (\"visible\", \"hidden\", or \"ready\" = attached to the DOM), bounded by timeout (capped at 60s). This is how the agent settles SPA navigation instead of racing it.\n\n```go\ntype TabInfo struct {\n\tIndex  int // 1-based\n\tActive bool\n\tURL    string\n\tTitle  string\n}\n```\nTabInfo is a live snapshot of one tab.\n\n","path":"github.com/memcode-ai/memcode/internal/browser","name":"browser","synopsis":"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.","isRedistributable":true}
