runtime

package
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2026 License: MIT Imports: 14 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Handles

func Handles(op string) bool

Handles reports whether op is part of the JSONL surface.

func Ops

func Ops() []string

Ops returns the sorted op names this package handles. It is derived from the generated dispatch table, so every surface that needs to enumerate the JSONL protocol (contract generation, SDK coverage) reads the truth instead of a hand-maintained copy.

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

type ScreenshotSpec struct {
	FullPage bool
	Ref      string
	Format   string
	Quality  int
	Scale    float64
}

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

func New(cfg Config) *Session

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

func (s *Session) Bind(b Binding)

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) Dispatch

func (s *Session) Dispatch(op string, args json.RawMessage) (any, error)

Dispatch routes the op to its handler.

func (*Session) Drag

func (s *Session) Drag(fromRef, toRef string, steps int) error

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

func (s *Session) EnsurePage() (*engine.Browser, *rod.Page, error)

EnsurePage opens the browser and the first page on demand, and is a no-op once a page exists.

func (*Session) EvalTimeout

func (s *Session) EvalTimeout(expr, ref string, d time.Duration) (string, error)

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) Observer

func (s *Session) Observer() *engine.Observer

Observer returns the CDP observer sidecar, or nil when none is attached.

func (*Session) Page

func (s *Session) Page() *rod.Page

Page returns the current page, or nil before the first op that opens one.

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

func (s *Session) ResolveRef(ref string) (*rod.Element, error)

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

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.

func (*Session) Shutdown

func (s *Session) Shutdown()

Shutdown releases the browser, the observer and the sidecar file. It is a no-op on a bound session: the surface that owns the browser closes it.

Jump to

Keyboard shortcuts

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