instance

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CalculateCost

func CalculateCost(inputTokens, outputTokens, cacheRead, cacheWrite int64) float64

CalculateCost estimates the cost based on token counts using Claude API pricing Pricing as of 2024 for Claude 3.5 Sonnet (the model used by Claude Code): - Input: $3.00 per 1M tokens - Output: $15.00 per 1M tokens - Cache read: $0.30 per 1M tokens - Cache write: $3.75 per 1M tokens

func ExtractInstanceIDFromSession

func ExtractInstanceIDFromSession(sessionName string) string

ExtractInstanceIDFromSession extracts the instance ID from a claudio tmux session name. Supports both legacy format (claudio-{instanceID}) and new format (claudio-{sessionID}-{instanceID}). For PR workflow sessions (claudio-{id}-pr or claudio-{sessionID}-{id}-pr), removes the -pr suffix first.

func ExtractSessionAndInstanceID

func ExtractSessionAndInstanceID(sessionName string) (sessionID, instanceID string)

ExtractSessionAndInstanceID extracts both session ID and instance ID from a tmux session name. Returns (sessionID, instanceID). For legacy format, sessionID will be empty. For PR workflow sessions, removes the -pr suffix first.

func FormatCost

func FormatCost(cost float64) string

FormatCost formats a cost value for display (e.g., "$0.42")

func FormatTokens

func FormatTokens(tokens int64) string

FormatTokens formats a token count for display (e.g., "45.2K")

func ListClaudioTmuxSessions

func ListClaudioTmuxSessions() ([]string, error)

ListClaudioTmuxSessions returns a list of all tmux sessions with the claudio- prefix

func ListSessionTmuxSessions

func ListSessionTmuxSessions(sessionID string) ([]string, error)

ListSessionTmuxSessions returns tmux sessions for a specific Claudio session. Filters by session ID prefix in the tmux session name.

Types

type BellCallback

type BellCallback func(instanceID string)

BellCallback is called when a terminal bell is detected in the tmux session

type Detector

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

Detector analyzes Claude's output to determine if it's waiting for user input

func NewDetector

func NewDetector() *Detector

NewDetector creates a new output state detector

func (*Detector) Detect

func (d *Detector) Detect(output []byte) WaitingState

Detect analyzes output and returns the detected waiting state It examines the last portion of output (most recent content) for patterns

type Manager

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

Manager handles a single Claude Code instance running in a tmux session

func NewManager

func NewManager(id, workdir, task string) *Manager

NewManager creates a new instance manager with default configuration

func NewManagerWithConfig

func NewManagerWithConfig(id, workdir, task string, cfg ManagerConfig) *Manager

NewManagerWithConfig creates a new instance manager with the given configuration. Uses legacy tmux naming (claudio-{instanceID}) for backwards compatibility.

func NewManagerWithSession

func NewManagerWithSession(sessionID, id, workdir, task string, cfg ManagerConfig) *Manager

NewManagerWithSession creates a new instance manager with session-scoped tmux naming. The tmux session will be named claudio-{sessionID}-{instanceID} to prevent collisions when multiple Claudio sessions are running simultaneously.

func (*Manager) AttachCommand

func (m *Manager) AttachCommand() string

AttachCommand returns the command to attach to this instance's tmux session This allows users to attach directly if needed

func (*Manager) ClearTimeout

func (m *Manager) ClearTimeout()

ClearTimeout resets the timeout state (for recovery/restart scenarios)

func (*Manager) CurrentMetrics

func (m *Manager) CurrentMetrics() *ParsedMetrics

CurrentMetrics returns the currently parsed metrics

func (*Manager) CurrentState

func (m *Manager) CurrentState() WaitingState

CurrentState returns the currently detected waiting state

func (*Manager) GetOutput

func (m *Manager) GetOutput() []byte

GetOutput returns all buffered output

func (*Manager) ID

func (m *Manager) ID() string

ID returns the instance ID

func (*Manager) LastActivityTime

func (m *Manager) LastActivityTime() time.Time

LastActivityTime returns when the instance last had output activity

func (*Manager) PID

func (m *Manager) PID() int

PID returns the process ID of the shell in the tmux session

func (*Manager) Pause

func (m *Manager) Pause() error

Pause pauses output capture (tmux session continues running)

func (*Manager) Paused

func (m *Manager) Paused() bool

Paused returns whether the instance is paused

func (*Manager) Reconnect

func (m *Manager) Reconnect() error

Reconnect attempts to reconnect to an existing tmux session This is used for session recovery after a restart

func (*Manager) Resize

func (m *Manager) Resize(width, height int) error

Resize changes the tmux pane dimensions This is useful when the display area changes (e.g., sidebar added/removed)

func (*Manager) Resume

func (m *Manager) Resume() error

Resume resumes output capture

func (*Manager) Running

func (m *Manager) Running() bool

Running returns whether the instance is running

func (*Manager) SendInput

func (m *Manager) SendInput(data []byte)

SendInput sends input to the tmux session

func (*Manager) SendKey

func (m *Manager) SendKey(key string)

SendKey sends a special key to the tmux session

func (*Manager) SendLiteral

func (m *Manager) SendLiteral(text string)

SendLiteral sends literal text to the tmux session (no interpretation)

func (*Manager) SendPaste

func (m *Manager) SendPaste(text string)

SendPaste sends pasted text to the tmux session with bracketed paste sequences This preserves the paste context for applications that support bracketed paste mode

func (*Manager) SessionName

func (m *Manager) SessionName() string

SessionName returns the tmux session name

func (*Manager) SetBellCallback

func (m *Manager) SetBellCallback(cb BellCallback)

SetBellCallback sets a callback that will be invoked when a terminal bell is detected

func (*Manager) SetMetricsCallback

func (m *Manager) SetMetricsCallback(cb MetricsChangeCallback)

SetMetricsCallback sets a callback that will be invoked when metrics are updated

func (*Manager) SetStateCallback

func (m *Manager) SetStateCallback(cb StateChangeCallback)

SetStateCallback sets a callback that will be invoked when the detected state changes

func (*Manager) SetTimeoutCallback

func (m *Manager) SetTimeoutCallback(cb TimeoutCallback)

SetTimeoutCallback sets a callback that will be invoked when a timeout is detected

func (*Manager) Start

func (m *Manager) Start() error

Start launches the Claude Code process in a tmux session

func (*Manager) StartTime

func (m *Manager) StartTime() *time.Time

StartTime returns when the instance was started

func (*Manager) Stop

func (m *Manager) Stop() error

Stop terminates the tmux session

func (*Manager) TimedOut

func (m *Manager) TimedOut() (bool, TimeoutType)

TimedOut returns whether the instance has timed out and the type of timeout

func (*Manager) TmuxSessionExists

func (m *Manager) TmuxSessionExists() bool

TmuxSessionExists checks if the tmux session for this instance exists

type ManagerConfig

type ManagerConfig struct {
	OutputBufferSize         int
	CaptureIntervalMs        int
	TmuxWidth                int
	TmuxHeight               int
	ActivityTimeoutMinutes   int  // 0 = disabled
	CompletionTimeoutMinutes int  // 0 = disabled
	StaleDetection           bool // Enable repeated output detection
}

ManagerConfig holds configuration for instance management

func DefaultManagerConfig

func DefaultManagerConfig() ManagerConfig

DefaultManagerConfig returns the default manager configuration

type MetricsChangeCallback

type MetricsChangeCallback func(instanceID string, metrics *ParsedMetrics)

MetricsChangeCallback is called when metrics are updated

type MetricsParser

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

MetricsParser extracts resource metrics from Claude Code output

func NewMetricsParser

func NewMetricsParser() *MetricsParser

NewMetricsParser creates a new metrics parser

func (*MetricsParser) Parse

func (p *MetricsParser) Parse(output []byte) *ParsedMetrics

Parse extracts metrics from Claude Code output text

type PRWorkflow

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

PRWorkflow manages the commit-push-PR workflow after an instance is stopped

func NewPRWorkflow

func NewPRWorkflow(instanceID, workdir, branch, task string, cfg PRWorkflowConfig) *PRWorkflow

NewPRWorkflow creates a new PR workflow manager. Uses legacy tmux naming (claudio-{instanceID}-pr) for backwards compatibility.

func NewPRWorkflowWithSession

func NewPRWorkflowWithSession(sessionID, instanceID, workdir, branch, task string, cfg PRWorkflowConfig) *PRWorkflow

NewPRWorkflowWithSession creates a new PR workflow manager with session-scoped tmux naming. The tmux session will be named claudio-{sessionID}-{instanceID}-pr to prevent collisions when multiple Claudio sessions are running simultaneously.

func (*PRWorkflow) GetOutput

func (p *PRWorkflow) GetOutput() []byte

GetOutput returns the buffered output from the PR workflow

func (*PRWorkflow) Running

func (p *PRWorkflow) Running() bool

Running returns whether the PR workflow is running

func (*PRWorkflow) SessionName

func (p *PRWorkflow) SessionName() string

SessionName returns the tmux session name

func (*PRWorkflow) SetCallback

func (p *PRWorkflow) SetCallback(cb PRWorkflowCallback)

SetCallback sets the completion callback

func (*PRWorkflow) Start

func (p *PRWorkflow) Start() error

Start launches the PR workflow in a tmux session

func (*PRWorkflow) Stop

func (p *PRWorkflow) Stop() error

Stop terminates the PR workflow tmux session

type PRWorkflowCallback

type PRWorkflowCallback func(instanceID string, success bool, output string)

PRWorkflowCallback is called when the PR workflow completes

type PRWorkflowConfig

type PRWorkflowConfig struct {
	UseAI      bool
	Draft      bool
	AutoRebase bool
	TmuxWidth  int
	TmuxHeight int
}

PRWorkflowConfig holds configuration for the PR workflow

type ParsedMetrics

type ParsedMetrics struct {
	InputTokens  int64
	OutputTokens int64
	CacheRead    int64
	CacheWrite   int64
	Cost         float64
	APICalls     int
}

ParsedMetrics holds metrics extracted from Claude Code output

type RingBuffer

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

RingBuffer is a thread-safe ring buffer for output

func NewRingBuffer

func NewRingBuffer(size int) *RingBuffer

NewRingBuffer creates a new ring buffer with the given capacity

func (*RingBuffer) Bytes

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

Bytes returns all data in the buffer

func (*RingBuffer) Len

func (r *RingBuffer) Len() int

Len returns the number of bytes in the buffer

func (*RingBuffer) Reset

func (r *RingBuffer) Reset()

Reset clears the buffer

func (*RingBuffer) Write

func (r *RingBuffer) Write(p []byte) (n int, err error)

Write writes data to the buffer

type StateChangeCallback

type StateChangeCallback func(instanceID string, state WaitingState)

StateChangeCallback is called when the detected waiting state changes

type TimeoutCallback

type TimeoutCallback func(instanceID string, timeoutType TimeoutType)

TimeoutCallback is called when a timeout condition is detected

type TimeoutType

type TimeoutType int

TimeoutType represents the type of timeout that occurred

const (
	TimeoutActivity   TimeoutType = iota // No activity for configured period
	TimeoutCompletion                    // Total runtime exceeded limit
	TimeoutStale                         // Repeated output detected (stuck in loop)
)

type WaitingState

type WaitingState int

WaitingState represents different types of waiting conditions Claude can be in

const (
	// StateWorking means Claude is actively working (not waiting)
	StateWorking WaitingState = iota
	// StateWaitingPermission means Claude is asking for permission to perform an action
	StateWaitingPermission
	// StateWaitingQuestion means Claude is asking the user a question
	StateWaitingQuestion
	// StateWaitingInput means Claude is waiting for general input
	StateWaitingInput
	// StateCompleted means Claude has finished its task
	StateCompleted
	// StateError means Claude encountered an error
	StateError
	// StatePROpened means Claude opened a pull request (PR URL detected in output)
	StatePROpened
)

func (WaitingState) IsWaiting

func (s WaitingState) IsWaiting() bool

IsWaiting returns true if the state represents any waiting condition

func (WaitingState) String

func (s WaitingState) String() string

String returns a human-readable string for the waiting state

Jump to

Keyboard shortcuts

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