session

package
v0.19.0 Latest Latest
Warning

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

Go to latest
Published: May 7, 2026 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrSessionNotFound    = errors.New("session not found")
	ErrSessionRunning     = errors.New("session is still running")
	ErrSessionNotRunning  = errors.New("session not running")
	ErrToolNotFound       = errors.New("tool not found")
	ErrUnsupportedTool    = errors.New("unsupported tool")
	ErrHasRunningChildren = errors.New("cannot remove session with running children")
	ErrNotTerminal        = errors.New("not a terminal session")
	ErrNoTmuxID           = errors.New("session has no tmux ID")
)

Sentinel errors for the session package.

Functions

func NeedsTmuxCheck added in v0.5.0

func NeedsTmuxCheck() bool

NeedsTmuxCheck returns whether the platform requires a tmux check at startup.

func ShellToolName added in v0.5.0

func ShellToolName() string

ShellToolName returns the internal tool name for terminal sessions.

func ShutdownSignals added in v0.5.0

func ShutdownSignals() []os.Signal

ShutdownSignals returns the OS signals for graceful shutdown.

func ToolAvailability

func ToolAvailability() map[string]ToolInfo

ToolAvailability checks which user-facing tools are available on this system.

Types

type Attachment added in v0.5.0

type Attachment struct {
	Path      string `json:"path"`
	Name      string `json:"name"`
	Size      int64  `json:"size"`
	Mime      string `json:"mime"`
	ModTime   string `json:"modTime"`
	CreatedAt string `json:"createdAt"`
}

Attachment represents a media file detected in session output.

type Manager

type Manager struct {

	// callback for session events
	OnSessionExit func(s *Session)
	// contains filtered or unexported fields
}

func NewManager

func NewManager(logger *slog.Logger) *Manager

func (*Manager) Create

func (m *Manager) Create(tool, workDir string, args []string, yoloMode bool, parentID string) (*Session, error)

func (*Manager) FindChildSession added in v0.4.0

func (m *Manager) FindChildSession(parentID, tool string) (*Session, bool)

FindChildSession returns a child session of the given parent with the specified tool.

func (*Manager) Get

func (m *Manager) Get(id string) (*Session, bool)

func (*Manager) GetCustomBaseURL added in v0.10.1

func (m *Manager) GetCustomBaseURL() string

GetCustomBaseURL returns the custom API base URL (empty if not configured).

func (*Manager) List

func (m *Manager) List() []*Session

func (*Manager) Remove added in v0.4.4

func (m *Manager) Remove(id string) error

Remove removes an exited session and its internal children from memory and persists the change.

func (*Manager) Restart

func (m *Manager) Restart(id string) (*Session, error)

func (*Manager) SaveAll

func (m *Manager) SaveAll()

SaveAll persists all sessions to disk. Called on shutdown.

func (*Manager) SetCustomBaseURL added in v0.10.1

func (m *Manager) SetCustomBaseURL(baseURL string)

SetCustomBaseURL configures the base URL for custom Anthropic API sessions.

func (*Manager) Stop

func (m *Manager) Stop(id string) error

func (*Manager) StopAll

func (m *Manager) StopAll()

func (*Manager) TmuxAction added in v0.4.3

func (m *Manager) TmuxAction(id, action string) error

TmuxAction executes a whitelisted tmux action on a terminal session.

type RingBuffer

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

func NewRingBuffer

func NewRingBuffer(size int) *RingBuffer

func (*RingBuffer) Bytes

func (r *RingBuffer) Bytes() []byte

func (*RingBuffer) Write

func (r *RingBuffer) Write(p []byte)

type Session

type Session struct {
	ID              string
	Tool            string
	WorkDir         string
	Args            []string
	PTY             io.ReadWriteCloser
	Cmd             *exec.Cmd
	CreatedAt       time.Time
	Status          Status
	ExitCode        *int
	YoloMode        bool
	Internal        bool   // internal session (e.g. tmux), not user-facing
	ToolSessionID   string // tool-specific session ID for resume
	ParentID        string // parent session ID (e.g. tmux child of a CLI session)
	TmuxSessionName string // tmux session name (kojo_<id>) for tmux-backed sessions
	// contains filtered or unexported fields
}

func (*Session) Attachments added in v0.5.0

func (s *Session) Attachments() []*Attachment

Attachments returns all tracked attachments, removing any whose files no longer exist.

func (*Session) BroadcastAttachments added in v0.5.0

func (s *Session) BroadcastAttachments(attachments []*Attachment)

func (*Session) BroadcastYoloDebug

func (s *Session) BroadcastYoloDebug(tail string)

func (*Session) CaptureToolSessionID added in v0.2.0

func (s *Session) CaptureToolSessionID(data []byte)

CaptureToolSessionID tries to parse a tool-specific session ID from PTY output. Only captures once (when ToolSessionID is still empty). Accumulates data across chunk boundaries to handle split reads.

func (*Session) CheckAttachments added in v0.5.0

func (s *Session) CheckAttachments(data []byte) []*Attachment

CheckAttachments appends data to the trailing buffer, scans for media file paths, and returns newly detected attachments. Caller should broadcast the result.

func (*Session) CheckYolo

func (s *Session) CheckYolo(data []byte) (*YoloApproval, string)

CheckYolo appends data to a trailing buffer and checks for approval patterns. Returns non-nil YoloApproval if a match is found. Caller should write the response to PTY.

func (*Session) Done

func (s *Session) Done() <-chan struct{}

func (*Session) HasAttachment added in v0.5.0

func (s *Session) HasAttachment(path string) bool

HasAttachment checks if a path is tracked as an attachment.

func (*Session) Info

func (s *Session) Info() SessionInfo

func (*Session) InfoForSave added in v0.5.0

func (s *Session) InfoForSave() SessionInfo

InfoForSave returns session info including attachment metadata for persistence.

func (*Session) IsYoloMode

func (s *Session) IsYoloMode() bool

func (*Session) RemoveAttachment added in v0.5.0

func (s *Session) RemoveAttachment(path string)

RemoveAttachment removes an attachment from tracking (does not delete the file).

func (*Session) Resize

func (s *Session) Resize(cols, rows uint16) error

func (*Session) SetYoloMode

func (s *Session) SetYoloMode(enabled bool)

func (*Session) Subscribe

func (s *Session) Subscribe() (chan []byte, []byte)

func (*Session) SubscribeAttachments added in v0.5.0

func (s *Session) SubscribeAttachments() chan []*Attachment

func (*Session) SubscribeYoloDebug

func (s *Session) SubscribeYoloDebug() chan string

func (*Session) Unsubscribe

func (s *Session) Unsubscribe(ch chan []byte)

func (*Session) UnsubscribeAttachments added in v0.5.0

func (s *Session) UnsubscribeAttachments(ch chan []*Attachment)

func (*Session) UnsubscribeYoloDebug

func (s *Session) UnsubscribeYoloDebug(ch chan string)

func (*Session) Write

func (s *Session) Write(data []byte) (int, error)

type SessionInfo

type SessionInfo struct {
	ID              string        `json:"id"`
	Tool            string        `json:"tool"`
	WorkDir         string        `json:"workDir"`
	Args            []string      `json:"args,omitempty"`
	Status          Status        `json:"status"`
	ExitCode        *int          `json:"exitCode,omitempty"`
	YoloMode        bool          `json:"yoloMode"`
	Internal        bool          `json:"internal,omitempty"`
	CreatedAt       string        `json:"createdAt"`
	ToolSessionID   string        `json:"toolSessionId,omitempty"`
	ParentID        string        `json:"parentId,omitempty"`
	TmuxSessionName string        `json:"tmuxSessionName,omitempty"`
	LastOutput      string        `json:"lastOutput,omitempty"`
	LastCols        uint16        `json:"lastCols,omitempty"`
	LastRows        uint16        `json:"lastRows,omitempty"`
	Attachments     []*Attachment `json:"attachments,omitempty"`
}

type Status

type Status string
const (
	StatusRunning Status = "running"
	StatusExited  Status = "exited"
)

type Store

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

Store persists session metadata to disk.

func (*Store) Load

func (st *Store) Load() ([]SessionInfo, error)

Load reads persisted sessions, filtering out entries older than maxAge. Returns (nil, nil) when the file does not exist (first run). Returns (nil, err) on read/parse errors so callers can distinguish "no sessions" from "failed to load" (important for orphan cleanup).

func (*Store) Save

func (st *Store) Save(infos []SessionInfo)

Save writes session info to disk using atomic rename.

type ToolInfo

type ToolInfo struct {
	Available bool   `json:"available"`
	Path      string `json:"path"`
}

type YoloApproval

type YoloApproval struct {
	Matched  string `json:"matched"`
	Response string `json:"response"`
}

YoloApproval is broadcast when yolo auto-approves a prompt.

Jump to

Keyboard shortcuts

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