Documentation
¶
Overview ¶
Package runtime owns the op orchestration shared by every agent-facing surface: the JSONL loop (`ghostchrome agent`), the autonomous LLM loop (`ghostchrome ai`), and anything else that needs to drive a long-lived browser session op by op.
It deliberately knows nothing about cobra, stdout, or output redaction. A Session takes a Config, dispatches an op by name, and RETURNS the result; encoding and transport are the caller's job.
Index ¶
- func Handles(op string) bool
- func Ops() []string
- type Binding
- type Config
- type ScreenshotSpec
- type Session
- func (s *Session) Bind(b Binding)
- func (s *Session) CaptureScreenshot(spec ScreenshotSpec) ([]byte, error)
- func (s *Session) Dispatch(op string, args json.RawMessage) (any, error)
- func (s *Session) Drag(fromRef, toRef string, steps int) error
- func (s *Session) EnsurePage() (*engine.Browser, *rod.Page, error)
- func (s *Session) EvalTimeout(expr, ref string, d time.Duration) (string, error)
- func (s *Session) LastObservation() *feedback.Observation
- func (s *Session) MirrorEvents(events []engine.ObserverEvent)
- func (s *Session) Mutation(prev *engine.PageSnapshot, mode engine.SnapshotMode) (any, error)
- func (s *Session) Observer() *engine.Observer
- func (s *Session) Page() *rod.Page
- func (s *Session) Refs() *engine.PageSnapshot
- func (s *Session) RememberExtraction(result *engine.ExtractionResult) error
- func (s *Session) ResolveRef(ref string) (*rod.Element, error)
- func (s *Session) RunOp(op string, args json.RawMessage) (any, *feedback.Observation, []engine.ObserverEvent, error)
- func (s *Session) SetRefs(snap *engine.PageSnapshot)
- func (s *Session) Shutdown()
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Binding ¶
type Binding struct {
// Browser and Page are the handles the next op runs against. Page may be a
// short-lived, context-scoped clone: the session re-reads it on every Bind,
// so a cancelled clone is never reused by a later op.
Browser *engine.Browser
Page *rod.Page
// Runtime is the engine runtime holding the per-page sessions. Sharing the
// surface's instance (rather than building a second one) keeps page-session
// state single.
Runtime *engine.Runtime
// DialogPolicy is the live policy object the surface installed on the page.
// Ops that adopt a new target (click popup, tab switch) re-install this same
// object, so a policy configured through the surface survives the adoption.
DialogPolicy *engine.DialogAutoPolicy
}
Binding is the externally owned browser state a surface hands to the session before running an op.
The JSONL loop lets the Session open and close Chrome itself. The MCP server cannot: it holds one browser across tool calls behind its own liveness probe, target recovery, idle reaper and per-request page cloning. Bind lets it keep all of that and still run every op through this package, so ref resolution, recovery and mutation diffing exist in one place instead of two.
Every field stays owned by the caller. A bound session never launches and never closes a browser.
type Config ¶
type Config struct {
// SessionName returns the resolved named session, or "" when none. It is
// a func because BrowserOpts may resolve the name lazily (the implicit
// daemon picks a default on the first launch).
SessionName func() string
// BrowserOpts builds the launch/connect options. Called once, on the
// first EnsurePage.
BrowserOpts func() engine.BrowserOpts
// ResolveOutputPath validates and absolutises ObserveOut. Nil means
// "use the path as-is".
ResolveOutputPath func(string) (string, error)
// Stealth mirrors --stealth: applies the anti-detection patches and
// arms the bot-challenge recovery path.
Stealth bool
// Observe mirrors --observe: starts the CDP observer sidecar.
Observe bool
// ObserveOut mirrors --observe-out: NDJSON sidecar for observer events.
ObserveOut string
// TimeoutSec mirrors --timeout, in seconds. Zero falls back to 30s.
TimeoutSec int
// Version is reported by the "init" op.
Version string
}
Config carries everything the op layer used to read from cobra globals. The CLI still owns the flags; it fills a Config once and hands it over.
type ScreenshotSpec ¶
ScreenshotSpec describes one capture.
Format and Scale come from two different engine entry points and are mutually exclusive: Format is the MCP surface's explicit image format ("webp" | "jpeg" | "png"), Scale is the JSONL surface's device scale factor. Format wins when both are set.
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
Session holds long-lived state across ops: the browser, the current page, the last snapshot, and the observer sidecar. Refs handed out by one op stay valid for the next one.
func New ¶
New constructs a session ready to be driven op by op. The browser is opened lazily on the first EnsurePage (or first op that needs a page).
func (*Session) Bind ¶
Bind attaches externally owned browser state. It does not touch the ref table: refs handed out by one op stay valid for the next, which is the whole point of a long-lived surface session.
func (*Session) CaptureScreenshot ¶
func (s *Session) CaptureScreenshot(spec ScreenshotSpec) ([]byte, error)
CaptureScreenshot takes a screenshot, resolving an optional @ref through the session ref table with the usual recovery. Both the JSONL "screenshot" op and MCP's screenshot tool go through it, so neither resolves refs on its own.
func (*Session) Drag ¶
Drag performs a mouse drag-and-drop between two refs, resolving both through the session ref table with the usual recovery. There is no JSONL "drag" op — this is the shared implementation behind the MCP-only drag tool, kept here so the surface does not resolve refs on its own.
func (*Session) EnsurePage ¶
EnsurePage opens the browser and the first page on demand, and is a no-op once a page exists.
func (*Session) EvalTimeout ¶
EvalTimeout evaluates an expression in the page, resolving an optional @ref through the session ref table with the usual recovery. d <= 0 means no per-call deadline, which is what the JSONL "eval" op uses (the loop's --timeout governs it). MCP's eval tool has its own timeout_ms argument and passes it here rather than resolving the ref itself.
func (*Session) LastObservation ¶
func (s *Session) LastObservation() *feedback.Observation
LastObservation returns the observation built by the previous RunOp call.
func (*Session) MirrorEvents ¶
func (s *Session) MirrorEvents(events []engine.ObserverEvent)
MirrorEvents appends observer events to the ObserveOut sidecar when one is open. No-op otherwise.
func (*Session) Mutation ¶
func (s *Session) Mutation(prev *engine.PageSnapshot, mode engine.SnapshotMode) (any, error)
Mutation observes the page after an action performed outside the dispatch table (the MCP-only swipe and drag tools, and upload) and returns exactly what the mutation ops return for that mode: a compact SnapshotDiff, a skeleton ExtractionResult, or the unchanged marker.
prev is the ref table captured before the action; Refs gives it.
func (*Session) Refs ¶
func (s *Session) Refs() *engine.PageSnapshot
Refs returns the ref table (@1, @2, ...) in force for the bound page: the snapshot the browser has saved for it, or the session's in-memory table when the browser holds none. It is the same resolution order the ops use.
func (*Session) RememberExtraction ¶
func (s *Session) RememberExtraction(result *engine.ExtractionResult) error
RememberExtraction records an extraction result as the session ref table and persists it on the browser. Composite surface ops that extract outside the dispatch table (the MCP "snapshot" tool bundles navigate+extract+errors) call it so their refs resolve exactly like refs from the "extract" op.
func (*Session) ResolveRef ¶
ResolveRef resolves an @ref against the session ref table, with the same recovery chain the mutation ops get. Surfaces that need the raw element (MCP's upload tool hands it to SetFiles) call this instead of resolving refs themselves.
func (*Session) RunOp ¶
func (s *Session) RunOp(op string, args json.RawMessage) (any, *feedback.Observation, []engine.ObserverEvent, error)
RunOp executes one op and returns the result plus the per-op observer events and the freshly built Observation, so callers do not have to duplicate the snapshot bookkeeping around every dispatch.
func (*Session) SetRefs ¶
func (s *Session) SetRefs(snap *engine.PageSnapshot)
SetRefs replaces the in-memory ref table. Surfaces call it with nil after tearing the browser down, so ref-based ops fail closed and tell the agent to re-extract instead of clicking into a dead target.