terminal

package
v0.4.3 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClassifyOutput

func ClassifyOutput(text string, agentType model.AgentType) model.AgentStatus

ClassifyOutput determines agent status from a single line of output text. Agent-specific patterns are checked first, then shared fallbacks.

func ClassifyScreen

func ClassifyScreen(content string, agentType model.AgentType) (model.AgentStatus, string)

ClassifyScreen checks each line of multi-line screen content and returns the highest priority status found. Active statuses (needs_input, error, working) are only matched in the bottom region where the live UI appears. The bottom region is measured from the last non-blank line (not the absolute bottom) to handle blank padding below dialog prompts. Idle/completed can match anywhere.

func DetectAgent

func DetectAgent(name string) model.AgentType

DetectAgent returns the agent type from a session name, or "" if not an agent. Claude: name starts with ✳ (U+2733) or contains "claude" (case-insensitive) Copilot: name starts with 🤖 (U+1F916) or contains "copilot" (case-insensitive) Codex: name contains "codex" (case-insensitive)

func DiscoverCWD

func DiscoverCWD(backend Backend, session Session, watchDirs []string, projectDirs []string) string

DiscoverCWD tries to find the working directory of a session using multiple strategies. watchDirs are the configured watch directories used to validate results. projectDirs are the known project directories for name matching.

Strategies in order:

  1. get-var path - fast but often returns $HOME for TUI agents
  2. lsof on TTY - most reliable, gets CWD from processes on the TTY
  3. Name matching - last resort, matches project basenames in session name

func ExtractActivity

func ExtractActivity(name string) string

ExtractActivity extracts activity description from a dynamic session name. E.g., "✳ Editing src/game.go (sourcekit-lsp)" -> "Editing src/game.go" Returns the name without the ✳ prefix and without the parenthesized suffix.

func HasAgentScreen

func HasAgentScreen(content string, agentType model.AgentType) bool

HasAgentScreen checks whether agent-specific patterns appear in the bottom region of the screen content. Unlike ClassifyScreen (which matches idle patterns anywhere), this restricts ALL patterns to the bottom region so scrollback from a previously-exited agent doesn't count as a positive signal.

func HasBell

func HasBell(text string) bool

HasBell checks if text contains a bell character (0x07).

func InferAgentFromScreen added in v0.4.2

func InferAgentFromScreen(content string) model.AgentType

InferAgentFromScreen tries to identify the agent from screen content. It prefers explicit product text, then falls back to agent-specific bottom region patterns. If multiple agents remain plausible, it returns "".

func ReadLastLine

func ReadLastLine(logPath string) string

ReadLastLine reads the last non-empty line from a log file. Returns an empty string if the file cannot be read or has no non-empty lines.

func ReadTail

func ReadTail(logPath string, n int) string

ReadTail reads the last n bytes of a log file.

func TTYForPID added in v0.3.0

func TTYForPID(pid int) string

TTYForPID returns the controlling TTY for a process by running ps. Returns empty string on any failure.

func TrimScreenTail added in v0.3.0

func TrimScreenTail(text string, n int) string

TrimScreenTail keeps at most the last n lines, anchored to the last nonblank line rather than the absolute bottom of the screen.

Types

type AgentPatterns

type AgentPatterns struct {
	NeedsInput     []*regexp.Regexp
	Working        []*regexp.Regexp
	WorkingExclude []*regexp.Regexp // lines matching these skip working detection
	Idle           []*regexp.Regexp
}

AgentPatterns holds compiled regexes for a specific agent type.

type Backend

type Backend interface {
	// Available checks whether the backend binary is installed and usable.
	Available() error

	// ListSessions returns all active sessions.
	ListSessions() ([]Session, error)

	// NewSession creates a new session and returns its ID.
	NewSession() (string, error)

	// SendText sends raw text (keystrokes) to a session.
	SendText(sessionID, text string) error

	// RunCommand executes a shell command inside a session.
	RunCommand(sessionID, cmd string) error

	// FocusSession brings a session to the foreground.
	FocusSession(sessionID string) error

	// ReadScreen captures visible terminal output from a session.
	ReadScreen(sessionID string, lines int) (string, error)

	// GetVar reads a multiplexer variable from a session.
	GetVar(sessionID, varName string) (string, error)

	// MonitorOutput starts monitoring a session's output, logging to a file
	// and watching for patterns. Returns a process ID or pipe identifier.
	MonitorOutput(sessionID, logPath, patterns string) (int, error)
}

Backend defines the interface for interacting with a terminal multiplexer.

type CachedBackend

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

CachedBackend wraps any Backend and caches ListSessions results with a TTL.

func NewCachedBackend

func NewCachedBackend(inner Backend, ttlSeconds int) *CachedBackend

NewCachedBackend creates a CachedBackend wrapping inner with the given TTL in seconds.

func (*CachedBackend) Available

func (c *CachedBackend) Available() error

Available delegates to the inner backend.

func (*CachedBackend) Close

func (c *CachedBackend) Close()

Close forwards to the inner backend if it supports closing (e.g. PTY backend).

func (*CachedBackend) FocusSession

func (c *CachedBackend) FocusSession(sessionID string) error

FocusSession delegates to the inner backend.

func (*CachedBackend) GetVar

func (c *CachedBackend) GetVar(sessionID, varName string) (string, error)

GetVar delegates to the inner backend.

func (*CachedBackend) Inner

func (c *CachedBackend) Inner() Backend

Inner returns the wrapped backend.

func (*CachedBackend) Invalidate

func (c *CachedBackend) Invalidate()

Invalidate clears the cache, forcing the next ListSessions call to fetch fresh.

func (*CachedBackend) ListSessions

func (c *CachedBackend) ListSessions() ([]Session, error)

ListSessions returns cached sessions if TTL hasn't expired, otherwise fetches fresh.

func (*CachedBackend) MonitorOutput

func (c *CachedBackend) MonitorOutput(sessionID, logPath, patterns string) (int, error)

MonitorOutput delegates to the inner backend.

func (*CachedBackend) NewSession

func (c *CachedBackend) NewSession() (string, error)

NewSession delegates to the inner backend.

func (*CachedBackend) NewSessionOn added in v0.2.0

func (c *CachedBackend) NewSessionOn(source string) (string, error)

NewSessionOn delegates to the inner backend's NewSessionOn if it supports it.

func (*CachedBackend) ReadScreen

func (c *CachedBackend) ReadScreen(sessionID string, lines int) (string, error)

ReadScreen delegates to the inner backend.

func (*CachedBackend) Resize

func (c *CachedBackend) Resize(cols, rows int)

Resize forwards to the inner backend if it supports resizing (e.g. PTY backend).

func (*CachedBackend) RunCommand

func (c *CachedBackend) RunCommand(sessionID, cmd string) error

RunCommand delegates to the inner backend.

func (*CachedBackend) SendText

func (c *CachedBackend) SendText(sessionID, text string) error

SendText delegates to the inner backend.

type CompositeBackend

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

CompositeBackend merges a primary backend (for launching) with optional integration backends (for discovering existing sessions).

func NewCompositeBackend

func NewCompositeBackend(primary Backend, primarySource string, integrations []Integration) *CompositeBackend

NewCompositeBackend creates a composite that delegates launches to primary and merges session lists from all backends. primarySource labels sessions from the primary backend (e.g. "pty", "iterm", "tmux").

func (*CompositeBackend) AddIntegration

func (c *CompositeBackend) AddIntegration(integ Integration)

AddIntegration adds an integration backend. Thread-safe.

func (*CompositeBackend) Available

func (c *CompositeBackend) Available() error

Available checks the primary backend only. Integration failures are non-fatal.

func (*CompositeBackend) Close

func (c *CompositeBackend) Close()

Close forwards to all backends that support closing (primary + integrations). This ensures PTY sessions are cleaned up even when PTY is an integration.

func (*CompositeBackend) FailedSources added in v0.3.0

func (c *CompositeBackend) FailedSources() []string

FailedSources returns the integration sources that failed during the last ListSessions call. This allows callers to skip cleanup for sessions belonging to transiently unavailable integrations.

func (*CompositeBackend) FocusSession

func (c *CompositeBackend) FocusSession(sessionID string) error

FocusSession routes to the correct backend based on session ID prefix.

func (*CompositeBackend) GetVar

func (c *CompositeBackend) GetVar(sessionID, varName string) (string, error)

GetVar routes to the correct backend based on session ID prefix.

func (*CompositeBackend) Integrations

func (c *CompositeBackend) Integrations() []Integration

Integrations returns a snapshot of the current integrations. Thread-safe.

func (*CompositeBackend) ListSessions

func (c *CompositeBackend) ListSessions() ([]Session, error)

ListSessions merges sessions from primary and all integrations. Integration sessions are prefixed and tagged with Source. Deduplication by TTY ensures the same terminal isn't listed twice.

func (*CompositeBackend) MonitorOutput

func (c *CompositeBackend) MonitorOutput(sessionID, logPath, patterns string) (int, error)

MonitorOutput routes to the correct backend based on session ID prefix.

func (*CompositeBackend) NewSession

func (c *CompositeBackend) NewSession() (string, error)

NewSession always delegates to the primary backend.

func (*CompositeBackend) NewSessionOn added in v0.2.0

func (c *CompositeBackend) NewSessionOn(source string) (string, error)

NewSessionOn creates a session on a specific backend identified by source name. If source matches the primary backend, delegates directly. Otherwise, looks for a matching integration and prefixes the returned session ID.

func (*CompositeBackend) PrimarySource

func (c *CompositeBackend) PrimarySource() string

PrimarySource returns the source label for sessions from the primary backend.

func (*CompositeBackend) ReadScreen

func (c *CompositeBackend) ReadScreen(sessionID string, lines int) (string, error)

ReadScreen routes to the correct backend based on session ID prefix.

func (*CompositeBackend) RemoveIntegration

func (c *CompositeBackend) RemoveIntegration(prefix string)

RemoveIntegration removes integrations matching the given prefix. Thread-safe. If the removed backend implements a Close() method, it is called.

func (*CompositeBackend) Resize

func (c *CompositeBackend) Resize(cols, rows int)

Resize forwards to all backends that support resizing (primary + integrations). This ensures PTY sessions receive resize updates even when PTY is an integration.

func (*CompositeBackend) RunCommand

func (c *CompositeBackend) RunCommand(sessionID, cmd string) error

RunCommand routes to the correct backend based on session ID prefix.

func (*CompositeBackend) SendText

func (c *CompositeBackend) SendText(sessionID, text string) error

SendText routes to the correct backend based on session ID prefix.

func (*CompositeBackend) SetPrimary

func (c *CompositeBackend) SetPrimary(b Backend, source string)

SetPrimary changes the primary backend and its source label. Thread-safe.

func (*CompositeBackend) SetSelfTTY added in v0.3.0

func (c *CompositeBackend) SetSelfTTY(tty string)

SetSelfTTY sets Atria's own controlling TTY so that sessions on this TTY are filtered from ListSessions results. This prevents Atria's own pane from appearing as a phantom agent session when integration backends discover it via auto-title inheritance.

type Integration

type Integration struct {
	Prefix  string // "iterm:" or "tmux:"
	Source  string // "iterm" or "tmux"
	Backend Backend
}

Integration represents a discovery-only backend that contributes sessions.

type Session

type Session struct {
	ID     string
	Name   string
	TTY    string
	Job    string
	Source string // "pty", "iterm", "tmux" — set by composite backend
}

Session represents a terminal multiplexer session.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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