// Package stagehand provides a browser tool built on Stagehand // (github.com/browserbase/stagehand), which drives the browser with // natural-language instructions — "click the sign in button", "extract every // invoice in the table" — instead of CSS selectors. Stagehand's own inference // runs through the agent's harness LLMProvider (see NewGenerateFunc). // // Two backends: a local Chrome launched on this machine (default), or a // Browserbase cloud browser when Config.BrowserbaseAPIKey is set. package stagehand import ( "context" "encoding/json" "errors" "fmt" "net" "strings" "sync" "time" sh "github.com/browserbase/stagehand/packages/sdk-go/v4" "github.com/sausheong/harness/llm" "github.com/sausheong/harness/tool" "github.com/sausheong/harness/tools/web" ) const ( // callTimeout bounds one tool call. act/extract each make one or more // LLM round trips on top of browser work, so this is looser than a // plain CDP action budget. callTimeout = 180 * time.Second // launchTimeout bounds browser launch + Stagehand init. launchTimeout = 90 * time.Second // closeTimeout bounds teardown so a wedged browser can't hang shutdown. closeTimeout = 15 * time.Second defaultViewportW = 1440 defaultViewportH = 900 maxTextOutput = 100_000 sessionIdleTimeout = 10 * time.Minute sessionMaxCount = 5 janitorInterval = 1 * time.Minute ) // Config configures a StagehandTool. type Config struct { // Provider and Model serve Stagehand's internal inference. Required. Provider llm.LLMProvider Model string // BrowserbaseAPIKey selects the Browserbase cloud backend when non-empty; // otherwise a local Chrome is launched. The tool never reads the // environment itself — callers opt in explicitly, e.g. // os.Getenv("BROWSERBASE_API_KEY"). BrowserbaseAPIKey string // Headful shows the local Chrome window. Ignored for Browserbase. Headful bool // ChromePath overrides the local Chrome executable. Empty = auto-detect. ChromePath string } // session is one browser + Stagehand client pair. Calls into the same // session serialize via mu: a Stagehand client drives one active page and // interleaved act/navigate calls would race on it. type session struct { mu sync.Mutex browser *sh.Browser client *sh.Stagehand lastUsed time.Time // proxy is the egress proxy a local Chrome is forced through; nil for // Browserbase and in tests. proxy *web.EgressProxy } // StagehandTool is an AI-driven browser tool. // // Like tools/browser it has two execution modes: // - Ephemeral (no "session"): each call launches a browser, runs the // action, and tears down. Requires "url". // - Persistent ("session"): the named browser persists across calls so // cookies, auth and page state carry over. Sessions auto-close after // sessionIdleTimeout, or on "action": "close". type StagehandTool struct { cfg Config // launch is the browser+client factory; tests replace it. launch func(ctx context.Context) (*session, error) mu sync.Mutex sessions map[string]*session stopJanitor chan struct{} } type stagehandInput struct { Action string `json:"action"` Session string `json:"session,omitempty"` URL string `json:"url,omitempty"` Instruction string `json:"instruction,omitempty"` Schema json.RawMessage `json:"schema,omitempty"` FullPage bool `json:"full_page,omitempty"` } // NewStagehandTool validates cfg and returns a tool. No browser is launched // until the first call. func NewStagehandTool(cfg Config) (*StagehandTool, error) { if cfg.Provider == nil { return nil, errors.New("stagehand: Config.Provider is required") } if cfg.Model == "" { return nil, errors.New("stagehand: Config.Model is required") } t := &StagehandTool{cfg: cfg} t.launch = t.launchSession return t, nil } func (t *StagehandTool) Name() string { return "stagehand" } func (t *StagehandTool) Description() string { return `Control a web browser with natural-language instructions (Stagehand). Prefer this over the "browser" tool when you don't know the page's CSS selectors: describe WHAT to do and Stagehand finds the elements. SESSIONS — each call without "session" uses a fresh browser that is closed afterwards, so it must include "url". For any multi-step flow (navigate, then act, then extract), pass the SAME "session": "