browser

package
v2.22.0 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2026 License: GPL-3.0 Imports: 50 Imported by: 0

Documentation

Overview

Package browser implements the browser.extract and browser.run actions.

Index

Constants

View Source
const (
	// DefaultSessionIdleTimeout is how long a session's browser waits for
	// the next command unless the session says otherwise.
	DefaultSessionIdleTimeout = 30 * time.Minute
	// MaxSessionIdleTimeout bounds how long a session's browser waits.
	MaxSessionIdleTimeout = 24 * time.Hour
	// DefaultOutlineChars bounds the outline a command reports after an
	// operation, and DefaultDescribeChars the one it reports when asked.
	DefaultOutlineChars  = 4000
	DefaultDescribeChars = 8000
)
View Source
const (
	CodeInvalidInput    = "invalid_input"
	CodeSessionNotFound = "session_not_found"
	CodeSessionBusy     = "session_busy"
	CodeSessionEnded    = "session_ended"
	CodeModelRequired   = "model_required"
	CodeProfileInUse    = "profile_in_use"
	CodeLaunchFailed    = "launch_failed"
	CodeOperationFailed = "operation_failed"
	CodeExportInvalid   = "export_invalid"
	// CodeFailed reports any other failure, such as a file that could not
	// be written.
	CodeFailed = "failed"
)

Session error codes, which a command reports with its error.

View Source
const (
	SessionIdle  = "idle"
	SessionBusy  = "busy"
	SessionEnded = "ended"
)

Session summary states.

View Source
const (
	OperationDone    = "done"
	OperationFailed  = "failed"
	OperationSkipped = "skipped"
)

Session operation outcomes.

Variables

This section is empty.

Functions

func ParseSessionInput

func ParseSessionInput(data []byte) (json.RawMessage, map[string]SessionVariable, error)

ParseSessionInput reads one operation, in the form of an item of a browser step's with.do, and the values of the variables it uses, from JSON or YAML:

{"act": "Type %password% into the Password field",
 "variables": {"password": {"env": "PORTAL_PASSWORD"}}}

Types

type CloseRequest

type CloseRequest struct {
	ID string
	// Force closes the session even while a command holds it.
	Force bool
	// Keep ends the session instead of removing it: its browser closes and
	// its profile is free, and its history can still be exported for as long
	// as an ended session's.
	Keep bool
}

CloseRequest closes a session.

type DescribeRequest

type DescribeRequest struct {
	ID string
	// Find shows only the outline's entries containing it.
	Find string
	// MaxChars bounds the outline; zero means DefaultDescribeChars.
	MaxChars int
	// Tree returns the page's accessibility tree as the model sees it, in
	// place of the outline.
	Tree bool
	// Screenshot saves a screenshot of the page under this name.
	Screenshot string
}

DescribeRequest asks what a session's page shows.

type DescribeResult

type DescribeResult struct {
	ID string `json:"id"`
	SessionPageView
	Tree         string    `json:"tree,omitempty"`
	Matches      *int      `json:"matches,omitempty"`
	Screenshot   string    `json:"screenshot,omitempty"`
	IdleDeadline time.Time `json:"idle_deadline"`
	Warnings     []string  `json:"warnings"`
	// Refused is the refusal the page shows, if any.
	Refused *SessionRefusal `json:"refused,omitempty"`
}

DescribeResult reports what a session's page shows.

type DoRequest

type DoRequest struct {
	ID string
	// Operation is one item of a browser step's with.do.
	Operation json.RawMessage
	// Variables holds the values of the variables the operation uses.
	Variables map[string]SessionVariable
	// OutlineChars bounds the page outline the result reports; zero omits
	// it.
	OutlineChars int
}

DoRequest runs one operation in a session.

type DoResult

type DoResult struct {
	ID string `json:"id"`
	// Index is the operation's place in the session's history, which
	// export refers to.
	Index  int    `json:"index"`
	Kind   string `json:"kind"`
	Status string `json:"status"`
	Detail string `json:"detail,omitempty"`
	// Error is why the operation failed.
	Error   *SessionError   `json:"error,omitempty"`
	Actions []SessionAction `json:"actions"`
	// Act is the act a step writes for an element operation.
	Act string `json:"act,omitempty"`
	// Into is the into a step writes for an element upload.
	Into string `json:"into,omitempty"`
	// Recorded reports that a step exported from the session replays the
	// act's or the upload's actions.
	Recorded bool           `json:"recorded"`
	Outputs  map[string]any `json:"outputs,omitempty"`
	// Screenshot is the file a screenshot operation saved.
	Screenshot   string          `json:"screenshot,omitempty"`
	Downloads    []string        `json:"downloads"`
	Dialogs      []SessionDialog `json:"dialogs"`
	Blocked      map[string]int  `json:"blocked,omitempty"`
	Tokens       int             `json:"tokens"`
	DurationMS   int64           `json:"duration_ms"`
	Warnings     []string        `json:"warnings"`
	IdleDeadline time.Time       `json:"idle_deadline"`
	// Refused is the refusal the page shows after the operation, if any.
	Refused *SessionRefusal `json:"refused,omitempty"`
	SessionPageView
}

DoResult reports what an operation did.

type ExportRequest

type ExportRequest struct {
	ID string
	// DAG and Step name the step that will run the exported operations; its
	// replay cache receives the acts' recordings.
	DAG  string
	Step string
	// Skip lists the history indices of operations to leave out.
	Skip []int
	// DryRun builds the step without writing recordings.
	DryRun bool
}

ExportRequest turns a session's history into a browser step.

type ExportResult

type ExportResult struct {
	ID  string `json:"id"`
	DAG string `json:"dag"`
	// Step is the step to put in the DAG's steps.
	Step ExportedStep `json:"step"`
	// CacheFile is where the recordings were written; empty on a dry run.
	CacheFile string `json:"cache_file,omitempty"`
	// Recordings counts the acts and uploads a first run replays.
	Recordings int `json:"recordings"`
	// Ops maps each operation of the step to the session's history.
	Ops []ExportedOperation `json:"ops"`
	// Variables says how to give the step each variable's value.
	Variables []ExportedVariable `json:"variables"`
	// LLM is the model the session used, for the DAG's llm block.
	LLM      *ir.LLMConfig `json:"llm,omitempty"`
	Warnings []string      `json:"warnings"`
}

ExportResult is a browser step built from a session.

type ExportedOperation

type ExportedOperation struct {
	// SessionIndex is the operation's place in the session's history, and
	// Index its place in the step's with.do.
	SessionIndex int    `json:"session_index"`
	Index        int    `json:"index"`
	Kind         string `json:"kind"`
	// Recorded reports that the step's first run replays the act or upload.
	Recorded bool `json:"recorded"`
}

ExportedOperation is one operation of an exported step.

type ExportedStep

type ExportedStep struct {
	ID     string       `json:"id"`
	Action string       `json:"action"`
	With   ExportedWith `json:"with"`
}

ExportedStep is a browser.run step, in the order a person writes one.

func (ExportedStep) YAML

func (s ExportedStep) YAML() ([]byte, error)

YAML returns the step as an item of a DAG's steps list.

type ExportedVariable

type ExportedVariable struct {
	Name  string `json:"name"`
	Value string `json:"value"`
	Note  string `json:"note"`
}

ExportedVariable is a variable the exported step uses.

type ExportedWith

type ExportedWith struct {
	URL       string            `json:"url,omitempty"`
	Browser   *exportedBrowser  `json:"browser,omitempty"`
	Variables map[string]string `json:"variables,omitempty"`
	Do        []json.RawMessage `json:"do"`
}

ExportedWith is the with block of an exported step.

type OpenResult

type OpenResult struct {
	ID string `json:"id"`
	// TLSVerified confirms the proxy verified the final HTTPS page's host.
	// False means no such evidence was available, not a certificate failure.
	TLSVerified bool `json:"tls_verified"`
	// CDPURL is the browser's DevTools address, where an application can
	// show the page and let a person act on it.
	CDPURL       string    `json:"cdp_url"`
	Headless     bool      `json:"headless"`
	Profile      string    `json:"profile,omitempty"`
	Model        string    `json:"model,omitempty"`
	IdleDeadline time.Time `json:"idle_deadline"`
	Warnings     []string  `json:"warnings"`
	// Refused is the refusal the page shows, if any.
	Refused *SessionRefusal `json:"refused,omitempty"`
	SessionPageView
}

OpenResult reports an opened session.

type SessionAction

type SessionAction struct {
	Selector    string   `json:"selector"`
	Description string   `json:"description,omitempty"`
	Method      string   `json:"method,omitempty"`
	Arguments   []string `json:"arguments,omitempty"`
}

SessionAction is one action an act performed on the page.

type SessionDialog

type SessionDialog struct {
	Type    string `json:"type"`
	Message string `json:"message"`
}

SessionDialog is a JavaScript dialog the browser accepted.

type SessionError

type SessionError struct {
	Code    string `json:"code"`
	Message string `json:"message"`
}

SessionError is a failed session command, with a code a program can act on.

func (*SessionError) Error

func (e *SessionError) Error() string

type SessionOptions

type SessionOptions struct {
	// URL is the page to open, if any.
	URL string
	// Profile keeps the browser's cookies and storage between sessions and
	// steps that name it.
	Profile        string
	Headless       bool
	Viewport       *SessionViewport
	Executable     string
	Proxy          string
	AllowedDomains []string
	// IdleTimeout closes the browser when no command arrives for that long;
	// zero means DefaultSessionIdleTimeout.
	IdleTimeout time.Duration
	// LLM is the model acts, extracts, and judged conditions ask; nil opens
	// a session without one.
	LLM *ir.LLMConfig
	// OutlineChars bounds the page outline the result reports; zero omits
	// it.
	OutlineChars int
}

SessionOptions configures the browser a session opens.

type SessionPageView

type SessionPageView struct {
	URL   string `json:"url"`
	Title string `json:"title"`
	// Outline describes the page; empty when not asked for.
	Outline          string `json:"outline,omitempty"`
	OutlineTruncated bool   `json:"outline_truncated,omitempty"`
}

SessionPageView is the page a command left the browser on.

type SessionRefusal

type SessionRefusal struct {
	Host   string `json:"host"`
	Status int    `json:"status"`
	// BotCheck names the check the site answered with, such as cloudflare.
	BotCheck string `json:"bot_check,omitempty"`
	// Message says what happened in a sentence.
	Message string `json:"message"`
}

SessionRefusal is a site answering with a refusal or a bot check in place of the page the browser asked for.

type SessionSummary

type SessionSummary struct {
	ID string `json:"id"`
	// State is idle, busy while a command runs, or ended once the browser
	// closed.
	State        string     `json:"state"`
	URL          string     `json:"url"`
	Title        string     `json:"title"`
	CDPURL       string     `json:"cdp_url,omitempty"`
	Profile      string     `json:"profile,omitempty"`
	Headless     bool       `json:"headless"`
	Model        string     `json:"model,omitempty"`
	Operations   int        `json:"operations"`
	CreatedAt    time.Time  `json:"created_at"`
	LastUsedAt   time.Time  `json:"last_used_at"`
	IdleDeadline *time.Time `json:"idle_deadline,omitempty"`
}

SessionSummary describes a session for a list.

type SessionVariable

type SessionVariable struct {
	Value string `json:"value,omitempty"`
	Env   string `json:"env,omitempty"`
}

SessionVariable is the value of a variable an operation uses: a literal, used for one command and never kept, or the name of an environment variable, read on every command and treated as a secret.

func (*SessionVariable) UnmarshalJSON

func (v *SessionVariable) UnmarshalJSON(data []byte) error

UnmarshalJSON accepts a literal string or an object naming an environment variable, {"env": NAME}.

type SessionViewport

type SessionViewport struct {
	Width  int
	Height int
}

SessionViewport is the size of the browser's page area.

type Sessions

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

Sessions runs browser sessions: browsers kept open between commands that each run one operation of a browser step, so a person or an agent can work a site the way a step will, look at the page after every operation, and keep what worked as a step.

func NewSessions

func NewSessions() *Sessions

NewSessions returns the browser sessions of this host.

func (*Sessions) Close

func (s *Sessions) Close(ctx context.Context, req CloseRequest) error

Close closes a session's browser and removes the session, or with Keep ends it.

func (*Sessions) Describe

func (s *Sessions) Describe(ctx context.Context, req DescribeRequest) (DescribeResult, error)

Describe reports what a session's page shows, without asking a model.

func (*Sessions) Do

func (s *Sessions) Do(ctx context.Context, req DoRequest) (DoResult, error)

Do runs one operation in a session. An operation that fails leaves the session open: the result reports the failure and the error is a SessionError with CodeOperationFailed.

func (*Sessions) Export

func (s *Sessions) Export(ctx context.Context, req ExportRequest) (ExportResult, error)

Export builds a browser step from the operations a session ran: those that succeeded, and those skipped because their when did not hold, in the order they ran. Unless DryRun is set, it writes the acts' recordings to the replay cache of the named DAG and step, so the step's first run replays them when it meets the pages the session met. A session whose browser closed can still be exported.

func (*Sessions) List

func (s *Sessions) List(ctx context.Context) ([]SessionSummary, error)

List returns this host's sessions, closing those nothing will use again first.

func (*Sessions) Open

func (s *Sessions) Open(ctx context.Context, opts SessionOptions) (OpenResult, error)

Open launches a session's browser and leaves it waiting for commands.

func (*Sessions) Reap

func (s *Sessions) Reap(ctx context.Context, id string) error

Reap watches the session id and closes its browser once the session is idle past its deadline, keeping its history, unless a command renewed it meanwhile. It returns once the session has ended or been closed, so the browser of a session nobody closes does not outlive its idle timeout even with no server running.

Jump to

Keyboard shortcuts

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