Documentation
¶
Overview ¶
Package browserhost manages Chrome processes that browser steps keep alive between step executions: their durable records, reachability probes, and cleanup.
Index ¶
- Constants
- Variables
- func BrowserProcessID(ctx context.Context, cdpURL string) (int, error)
- func CloseBrowser(ctx context.Context, cdpURL string) error
- func DenyDownloads(ctx context.Context, cdpURL string) error
- func Probe(ctx context.Context, cdpURL string) error
- func RecordID(dagRunID, stepName string) string
- func Release(ctx context.Context, store *Store, record Record) error
- func RunReaper(ctx context.Context, store *Store, resumable ResumableFunc)
- func Sweep(ctx context.Context, store *Store, now time.Time, resumable ResumableFunc) error
- type Dialog
- type DialogWatcher
- type Download
- type DownloadWatcher
- type Extension
- type Record
- type ResumableFunc
- type State
- type Store
Constants ¶
const ( // AgentProvider identifies browser steps in agent sessions. AgentProvider = "browser" // DataDirName is the directory under the Dagu data directory that holds // browser state. DataDirName = "browser" )
const ( // ReapInterval is how often a long-lived process sweeps browser sessions. ReapInterval = time.Minute )
const StagehandExtensionName = "Stagehand Runtime"
StagehandExtensionName is the name the Stagehand runtime extension reports to Chrome.
Variables ¶
var ErrExtensionNotFound = errors.New("stagehand runtime extension is not loaded")
ErrExtensionNotFound reports that the connected browser has no enabled Stagehand runtime extension.
var ErrUnreachable = errors.New("browser is not running")
ErrUnreachable reports that nothing accepts connections at a browser's DevTools address, so the browser is no longer running.
Functions ¶
func BrowserProcessID ¶
BrowserProcessID returns the process ID of the browser at cdpURL.
func CloseBrowser ¶
CloseBrowser asks the browser at cdpURL to exit. A browser that no longer answers is treated as already closed.
func DenyDownloads ¶
DenyDownloads makes the browser at cdpURL refuse every download.
func Release ¶
Release closes the browser described by record, deletes the files it owns, and removes the record.
func RunReaper ¶
func RunReaper(ctx context.Context, store *Store, resumable ResumableFunc)
RunReaper sweeps the store until ctx is cancelled.
Types ¶
type Dialog ¶
type Dialog struct {
// Type is alert, confirm, prompt, or beforeunload.
Type string
Message string
}
Dialog is a JavaScript dialog a page opened and the watcher accepted.
type DialogWatcher ¶
type DialogWatcher struct {
// contains filtered or unexported fields
}
DialogWatcher accepts the JavaScript dialogs a browser's pages open. An open dialog blocks its page until it is answered.
func WatchDialogs ¶
func WatchDialogs(ctx context.Context, cdpURL string) (*DialogWatcher, error)
WatchDialogs accepts every dialog in the pages of the browser at cdpURL, including pages opened later, until the watcher is closed. A prompt is answered with its default text.
func (*DialogWatcher) Close ¶
func (w *DialogWatcher) Close() error
Close stops watching. Dialogs opened afterwards stay open.
func (*DialogWatcher) Take ¶
func (w *DialogWatcher) Take() []Dialog
Take returns the dialogs accepted since the previous call.
type DownloadWatcher ¶
type DownloadWatcher struct {
// contains filtered or unexported fields
}
DownloadWatcher saves the browser's downloads into a directory and tracks which are still in progress.
func WatchDownloads ¶
func WatchDownloads(ctx context.Context, cdpURL, dir string) (*DownloadWatcher, error)
WatchDownloads saves downloads from the browser at cdpURL into dir until the watcher is closed.
func (*DownloadWatcher) Close ¶
func (w *DownloadWatcher) Close() error
Close stops watching. Downloads still in progress are left to the browser.
func (*DownloadWatcher) Wait ¶
func (w *DownloadWatcher) Wait(ctx context.Context, grace, timeout time.Duration) ([]Download, error)
Wait returns the downloads completed since the previous call. It first allows grace for a download to begin, then waits until none is in progress. It fails when a download was canceled or is still running when timeout passes.
type Extension ¶
type Extension struct {
ID string `json:"id"`
Name string `json:"name"`
Path string `json:"path"`
Enabled bool `json:"enabled"`
}
Extension describes an unpacked extension loaded into a browser.
type Record ¶
type Record struct {
ID string `json:"id"`
DAGName string `json:"dagName"`
DAGRunID string `json:"dagRunId"`
StepName string `json:"stepName"`
// Generation is the agent-session generation that owns the browser.
Generation int `json:"generation"`
State State `json:"state"`
// Deadline bounds how long a detached browser waits to be resumed.
Deadline time.Time `json:"deadline,omitzero"`
CDPURL string `json:"cdpUrl"`
ExtensionID string `json:"extensionId,omitempty"`
ExtensionDir string `json:"extensionDir,omitempty"`
UserDataDir string `json:"userDataDir,omitempty"`
OwnsUserDataDir bool `json:"ownsUserDataDir,omitempty"`
Profile string `json:"profile,omitempty"`
DownloadsDir string `json:"downloadsDir,omitempty"`
// BrowserPID and BrowserStartedAt identify the browser process, so it can
// be ended when it no longer answers on its DevTools address.
BrowserPID int `json:"browserPid,omitempty"`
BrowserStartedAt int64 `json:"browserStartedAt,omitempty"`
// OwnerPID and OwnerStartedAt identify the process driving a running
// browser, so a crashed owner can be detected even after PID reuse.
OwnerPID int `json:"ownerPid,omitempty"`
OwnerStartedAt int64 `json:"ownerStartedAt,omitempty"`
// Cursor is the index of the next operation a resumed step runs.
Cursor int `json:"cursor"`
// Outputs holds values extracted before the step detached.
Outputs map[string]any `json:"outputs,omitempty"`
}
Record is the durable description of one browser session kept by a step.
type ResumableFunc ¶
ResumableFunc reports whether a detached session still belongs to a step that can resume it. A nil ResumableFunc treats every unexpired session as resumable.
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store persists browser session records as private files.