session

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package session provides shared browser session management for CLI and MCP.

Package session provides shared browser session management for CLI and MCP.

Index

Constants

View Source
const (
	// DefaultSessionDir is the default directory for session files.
	DefaultSessionDir = ".w3pilot"

	// DefaultSessionFile is the default session file name.
	DefaultSessionFile = "session.json"
)

Variables

This section is empty.

Functions

func Clear

func Clear(path string) error

Clear removes the session file.

func DefaultSessionPath

func DefaultSessionPath() string

DefaultSessionPath returns the default session file path.

func Exists

func Exists(path string) bool

Exists checks if a session file exists.

func Save

func Save(path string, info *Info) error

Save persists session info to disk.

Types

type Config

type Config struct {
	// Headless runs the browser without a visible window.
	Headless bool

	// DefaultTimeout for browser operations.
	DefaultTimeout time.Duration

	// InitScripts are JavaScript files to inject before page scripts.
	InitScripts []string

	// SessionFile is the path to the session persistence file.
	// Defaults to ~/.w3pilot/session.json
	SessionFile string

	// AutoReconnect attempts to reconnect to an existing session on launch.
	AutoReconnect bool
}

Config holds session configuration options.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns a Config with sensible defaults.

type Info

type Info struct {
	// WebSocketURL is the BiDi WebSocket endpoint for reconnection.
	WebSocketURL string `json:"websocket_url,omitempty"`

	// ClickerPort is the port clicker is listening on (WebSocket mode).
	ClickerPort int `json:"clicker_port,omitempty"`

	// CDPPort is the Chrome DevTools Protocol port.
	CDPPort int `json:"cdp_port,omitempty"`

	// UserDataDir is the Chrome user data directory.
	UserDataDir string `json:"user_data_dir,omitempty"`

	// Headless indicates if the browser is running in headless mode.
	Headless bool `json:"headless"`

	// PID is the browser process ID.
	PID int `json:"pid,omitempty"`

	// ClickerPID is the clicker process ID (WebSocket mode).
	ClickerPID int `json:"clicker_pid,omitempty"`

	// LaunchedAt is when the session was started.
	LaunchedAt time.Time `json:"launched_at"`

	// InitScripts are JavaScript files injected before page scripts.
	InitScripts []string `json:"init_scripts,omitempty"`
}

Info stores information about a running browser session for persistence.

func Load

func Load(path string) (*Info, error)

Load reads session info from disk.

type Manager

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

Manager handles browser session lifecycle with optional persistence. It provides lazy launch, reconnection, and graceful shutdown.

func NewManager

func NewManager(config Config) *Manager

NewManager creates a new session manager with the given config.

func (*Manager) Close

func (m *Manager) Close(ctx context.Context) error

Close closes the browser and clears session info.

func (*Manager) Detach

func (m *Manager) Detach() error

Detach detaches from the browser without closing it. The session info is preserved for later reconnection.

func (*Manager) IsConnected

func (m *Manager) IsConnected() bool

IsConnected returns true if a browser is connected.

func (*Manager) LaunchIfNeeded

func (m *Manager) LaunchIfNeeded(ctx context.Context) error

LaunchIfNeeded launches a new browser or reconnects to an existing session.

func (*Manager) Pilot

func (m *Manager) Pilot(ctx context.Context) (*w3pilot.Pilot, error)

Pilot returns the browser controller, launching or reconnecting as needed.

func (*Manager) Refresh

func (m *Manager) Refresh(ctx context.Context) error

Refresh updates the session info (e.g., after navigation).

func (*Manager) State

func (m *Manager) State() State

State returns the current session state.

type State

type State struct {
	Status Status         `json:"status"`
	Info   *Info          `json:"info,omitempty"`
	Pilot  *w3pilot.Pilot `json:"-"`
	Error  error          `json:"error,omitempty"`
}

State provides current session state information.

type Status

type Status string

Status represents the current session state.

const (
	// StatusDisconnected means no browser is connected.
	StatusDisconnected Status = "disconnected"

	// StatusConnected means a browser is connected and ready.
	StatusConnected Status = "connected"

	// StatusReconnecting means attempting to reconnect to an existing session.
	StatusReconnecting Status = "reconnecting"
)

Jump to

Keyboard shortcuts

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