claude

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatMultiUsage added in v0.12.0

func FormatMultiUsage(w io.Writer, instances []InstanceUsage, now time.Time) error

FormatMultiUsage writes per-instance and aggregated total usage.

func FormatTmuxPanes added in v0.12.0

func FormatTmuxPanes(w io.Writer, panes []TmuxPane) error

FormatTmuxPanes writes the tmux pane listing to w.

func FormatUsage added in v0.11.0

func FormatUsage(w io.Writer, summary *UsageSummary, now time.Time) error

FormatUsage writes the usage summary to w.

func Install

func Install(w io.Writer, fw FileWriter, personal bool) error

Install writes the Claude Code skill and agent files to disk. When personal is true, files are written under ~/.claude/ instead of .claude/.

func MergeUsage added in v0.12.0

func MergeUsage(dst, src *UsageSummary)

MergeUsage adds all model usage from src into dst.

func WindowEnd added in v0.11.0

func WindowEnd(start time.Time) time.Time

WindowEnd returns the end of the current 5-hour usage window in UTC.

func WindowStart added in v0.11.0

func WindowStart(now time.Time) time.Time

WindowStart returns the start of the current 5-hour usage window in UTC.

Types

type ByteStateReader added in v0.12.0

type ByteStateReader struct {
	Data []byte
}

ByteStateReader reads state from in-memory data (e.g. container output).

func (*ByteStateReader) ReadState added in v0.12.0

func (b *ByteStateReader) ReadState(_ string) (InstanceState, error)

type ByteWalker added in v0.12.0

type ByteWalker struct {
	Data []byte
}

ByteWalker implements DirWalker over in-memory bytes (one JSONL line per text line).

func (*ByteWalker) WalkJSONL added in v0.12.0

func (b *ByteWalker) WalkJSONL(_ string, fn func(line []byte) error) error

type CombinedFinder added in v0.12.0

type CombinedFinder struct {
	Finders []InstanceFinder
}

CombinedFinder aggregates multiple InstanceFinders, logging and skipping failures.

func (*CombinedFinder) FindInstances added in v0.12.0

func (c *CombinedFinder) FindInstances(ctx context.Context) ([]Instance, error)

type CommandRunner added in v0.12.0

type CommandRunner interface {
	Run(ctx context.Context, name string, args ...string) ([]byte, error)
}

CommandRunner abstracts running external commands for testability.

type ContainerInfo added in v0.12.0

type ContainerInfo struct {
	ID   string
	Name string
}

ContainerInfo holds minimal container metadata.

type DirWalker added in v0.11.0

type DirWalker interface {
	WalkJSONL(root string, fn func(line []byte) error) error
}

DirWalker abstracts walking JSONL files for testability.

type DockerClient added in v0.12.0

type DockerClient interface {
	ListContainers(ctx context.Context) ([]ContainerInfo, error)
	Exec(ctx context.Context, containerID string, cmd []string) (int, io.Reader, error)
	ContainerStats(ctx context.Context, containerID string) (*MemoryInfo, error)
	Close() error
}

DockerClient abstracts Docker operations for testability.

func NewEngineDockerClient added in v0.12.0

func NewEngineDockerClient() (DockerClient, error)

NewEngineDockerClient creates a DockerClient backed by the Docker Engine API.

type DockerFinder added in v0.12.0

type DockerFinder struct {
	Client DockerClient
}

DockerFinder discovers Claude Code instances inside Docker containers.

func (*DockerFinder) FindInstances added in v0.12.0

func (d *DockerFinder) FindInstances(ctx context.Context) ([]Instance, error)

type FileWriter

type FileWriter interface {
	MkdirAll(path string, perm os.FileMode) error
	WriteFile(name string, data []byte, perm os.FileMode) error
	ReadFile(name string) ([]byte, error)
}

FileWriter abstracts filesystem operations for testability.

type HostFinder added in v0.12.0

type HostFinder struct {
	Runner  CommandRunner
	HomeDir string // override for testing; empty uses os.UserHomeDir result passed externally
}

HostFinder discovers Claude Code instances on the local host via pgrep.

func (*HostFinder) FindInstances added in v0.12.0

func (h *HostFinder) FindInstances(ctx context.Context) ([]Instance, error)

type Instance added in v0.12.0

type Instance struct {
	Label       string      // e.g. "Host (PID 7046)" or `Container "dev-myapp" (abc123)`
	Source      string      // "host" or "container"
	Walker      DirWalker   // how to read its JSONL data
	StateReader StateReader // determines busy/ready state
	Root        string      // JSONL root path (or virtual path for containers)
	Memory      *MemoryInfo // memory usage (containers only)
	ContainerID string      // full Docker container ID (containers only)
}

Instance represents a discovered Claude Code instance.

type InstanceFinder added in v0.12.0

type InstanceFinder interface {
	FindInstances(ctx context.Context) ([]Instance, error)
}

InstanceFinder discovers running Claude Code instances.

type InstanceState added in v0.12.0

type InstanceState int

InstanceState represents whether a Claude Code instance is busy or ready.

const (
	StateUnknown InstanceState = iota
	StateBusy
	StateReady
)

func DetermineState added in v0.12.0

func DetermineState(lines [][]byte) InstanceState

DetermineState walks lines backward and returns the instance state based on the last user or assistant entry.

func (InstanceState) String added in v0.12.0

func (s InstanceState) String() string

type InstanceUsage added in v0.12.0

type InstanceUsage struct {
	Instance Instance
	Summary  *UsageSummary
	State    InstanceState
}

InstanceUsage pairs an Instance with its calculated usage.

type MemoryInfo added in v0.12.0

type MemoryInfo struct {
	Usage uint64 // current memory usage in bytes
	Limit uint64 // memory limit in bytes (0 = unlimited)
}

MemoryInfo holds memory usage and limit for a container.

type ModelUsage added in v0.11.0

type ModelUsage struct {
	InputTokens  int
	OutputTokens int
	CacheCreate  int
	CacheRead    int
}

ModelUsage holds aggregated token counts for one model class.

type OSCommandRunner added in v0.12.0

type OSCommandRunner struct{}

OSCommandRunner implements CommandRunner using os/exec.

func (OSCommandRunner) Run added in v0.12.0

func (OSCommandRunner) Run(ctx context.Context, name string, args ...string) ([]byte, error)

type OSDirWalker added in v0.11.0

type OSDirWalker struct{}

OSDirWalker implements DirWalker using the real filesystem.

func (OSDirWalker) WalkJSONL added in v0.11.0

func (OSDirWalker) WalkJSONL(root string, fn func(line []byte) error) error

type OSFileWriter

type OSFileWriter struct{}

OSFileWriter implements FileWriter using the os package.

func (OSFileWriter) MkdirAll

func (OSFileWriter) MkdirAll(path string, perm os.FileMode) error

func (OSFileWriter) ReadFile

func (OSFileWriter) ReadFile(name string) ([]byte, error)

func (OSFileWriter) WriteFile

func (OSFileWriter) WriteFile(name string, data []byte, perm os.FileMode) error

type OSProcessLister added in v0.12.0

type OSProcessLister struct {
	Runner CommandRunner
}

OSProcessLister implements ProcessLister using ps.

func (*OSProcessLister) ListProcesses added in v0.12.0

func (l *OSProcessLister) ListProcesses(ctx context.Context) ([]ProcessInfo, error)

ListProcesses returns all processes with their PID, PPID, command name, and args.

type OSStateReader added in v0.12.0

type OSStateReader struct{}

OSStateReader reads state from the filesystem by finding the most recent JSONL file and reading its tail.

func (OSStateReader) ReadState added in v0.12.0

func (OSStateReader) ReadState(root string) (InstanceState, error)

type OSTmuxClient added in v0.12.0

type OSTmuxClient struct {
	Runner CommandRunner
}

OSTmuxClient implements TmuxClient using the real tmux command.

func (*OSTmuxClient) ListPanes added in v0.12.0

func (c *OSTmuxClient) ListPanes(ctx context.Context) ([]TmuxPane, error)

ListPanes runs tmux list-panes and parses the output.

type ProcessInfo added in v0.12.0

type ProcessInfo struct {
	PID  int
	PPID int
	Comm string
	Args string // full command line (may be empty)
}

ProcessInfo holds PID, parent PID, command name, and full argument line.

type ProcessLister added in v0.12.0

type ProcessLister interface {
	ListProcesses(ctx context.Context) ([]ProcessInfo, error)
}

ProcessLister abstracts listing all processes with PID, PPID, and command info.

type StateReader added in v0.12.0

type StateReader interface {
	ReadState(root string) (InstanceState, error)
}

StateReader determines the current state of a Claude Code instance.

type TmuxClient added in v0.12.0

type TmuxClient interface {
	ListPanes(ctx context.Context) ([]TmuxPane, error)
}

TmuxClient abstracts listing tmux panes for testability.

type TmuxPane added in v0.12.0

type TmuxPane struct {
	PID          int
	SessionName  string
	WindowIndex  int
	PaneIndex    int
	Devcontainer bool // true when claude runs inside a devcontainer in this pane
}

TmuxPane represents a tmux pane with an active Claude process.

func FindClaudePanes added in v0.12.0

func FindClaudePanes(ctx context.Context, client TmuxClient, lister ProcessLister, claudeContainerIDs []string) ([]TmuxPane, error)

FindClaudePanes discovers tmux panes that have a Claude process running in them. It lists all tmux panes, builds a process tree from ps output, and checks each pane's descendant processes for a "claude" command or a "docker exec" into a container known to run Claude (identified by claudeContainerIDs).

type UsageSummary added in v0.11.0

type UsageSummary struct {
	Models map[string]*ModelUsage
}

UsageSummary holds the full usage breakdown for the current window.

func CalculateUsage added in v0.11.0

func CalculateUsage(walker DirWalker, root string, now time.Time) (*UsageSummary, error)

CalculateUsage scans JSONL files under root and returns usage broken down by model.

Jump to

Keyboard shortcuts

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