claude

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2026 License: MIT Imports: 27 Imported by: 0

Documentation

Overview

Package claude implements the Claude CLI process manager.

Package claude implements the Claude CLI process manager.

Package claude implements the Claude CLI process manager.

Package claude implements the Claude CLI process manager.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendBashToSession

func AppendBashToSession(workDir, sessionID, command, stdout, stderr string) error

AppendBashToSession writes bash command execution to a session JSONL file. This uses the same format as Claude Code for compatibility. Returns the UUIDs of the generated messages.

func DeleteAllSessions

func DeleteAllSessions(repoPath string) (int, error)

DeleteAllSessions deletes all session files for a repository.

func DeleteSession

func DeleteSession(repoPath, sessionID string) error

DeleteSession deletes a specific session file.

func GetSessionsDir

func GetSessionsDir(repoPath string) string

GetSessionsDir exports the sessions directory path for a repo.

Types

type ANSIParser

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

ANSIParser handles terminal escape code processing. It can strip ANSI codes for clean text display or extract formatting info.

func NewANSIParser

func NewANSIParser() *ANSIParser

NewANSIParser creates a new ANSI parser.

func (*ANSIParser) ContainsText

func (p *ANSIParser) ContainsText(line, text string) bool

ContainsText checks if the line contains the given text (after stripping ANSI codes).

func (*ANSIParser) IsEmptyLine

func (p *ANSIParser) IsEmptyLine(line string) bool

IsEmptyLine returns true if the line contains only whitespace or control codes.

func (*ANSIParser) ParseLine

func (p *ANSIParser) ParseLine(raw string) *ParsedLine

ParseLine extracts clean text from a raw terminal line.

func (*ANSIParser) StripCodes

func (p *ANSIParser) StripCodes(text string) string

StripCodes removes all ANSI escape codes and control characters from text.

type Manager

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

Manager implements the ClaudeManager port interface.

func NewManager

func NewManager(command string, args []string, timeoutMinutes int, hub ports.EventHub, skipPermissions bool, rotationConfig *config.LogRotationConfig) *Manager

NewManager creates a new Claude CLI manager (legacy constructor for backward compatibility).

func NewManagerWithContext

func NewManagerWithContext(hub ports.EventHub, command string, args []string, timeoutMinutes int, skipPermissions bool, workDir, workspaceID, sessionID string, rotationConfig *config.LogRotationConfig) *Manager

NewManagerWithContext creates a new Claude CLI manager with workspace/session context. This is the preferred constructor for multi-workspace support.

func (*Manager) ClaudeSessionID

func (m *Manager) ClaudeSessionID() string

ClaudeSessionID returns the current Claude CLI session ID.

func (*Manager) CurrentPrompt

func (m *Manager) CurrentPrompt() string

CurrentPrompt returns the current prompt if running.

func (*Manager) GetPendingPTYPermission

func (m *Manager) GetPendingPTYPermission() *PTYPermissionPrompt

GetPendingPTYPermission returns the pending PTY permission prompt if any. Used when client reconnects to re-show the permission dialog.

func (*Manager) GetPendingToolUse

func (m *Manager) GetPendingToolUse() (toolUseID, toolName string)

GetPendingToolUse returns the pending tool use info if waiting for input.

func (*Manager) HasPendingPermission

func (m *Manager) HasPendingPermission() bool

HasPendingPermission returns true if there's a pending permission request.

func (*Manager) IsPTYMode

func (m *Manager) IsPTYMode() bool

IsPTYMode returns true if running in PTY mode.

func (*Manager) IsRunning

func (m *Manager) IsRunning() bool

IsRunning returns true if Claude is currently running.

func (*Manager) IsWaitingForInput

func (m *Manager) IsWaitingForInput() bool

IsWaitingForInput returns true if Claude is waiting for user input.

func (*Manager) Kill

func (m *Manager) Kill() error

Kill forcefully terminates the Claude process.

func (*Manager) PID

func (m *Manager) PID() int

PID returns the process ID if running.

func (*Manager) SendPTYInput

func (m *Manager) SendPTYInput(input string) error

SendPTYInput sends input to the PTY (for interactive responses). This allows remote clients to respond to permission prompts. For regular text input, a carriage return is automatically appended. For special keys (escape sequences, control characters), the input is sent as-is.

func (*Manager) SendResponse

func (m *Manager) SendResponse(toolUseID string, response string, isError bool) error

SendResponse sends a user response to Claude's stdin for interactive prompts.

func (*Manager) SessionID

func (m *Manager) SessionID() string

SessionID returns the session ID for this manager.

func (*Manager) SetLogDir

func (m *Manager) SetLogDir(dir string)

SetLogDir enables file logging to the specified directory.

func (*Manager) SetOnPTYComplete

func (m *Manager) SetOnPTYComplete(callback func(sessionID string))

SetOnPTYComplete sets a callback that's called when PTY streaming finishes. Used by session manager to emit claude_message with stop_reason.

func (*Manager) SetSessionID

func (m *Manager) SetSessionID(newID string)

SetSessionID updates the session ID for this manager. This is called when the real session ID is detected from Claude's session file. Thread-safe: uses mutex to protect concurrent access.

func (*Manager) SetWorkDir

func (m *Manager) SetWorkDir(dir string)

SetWorkDir sets the working directory for Claude CLI.

func (*Manager) Start

func (m *Manager) Start(ctx context.Context, prompt string) error

Start spawns Claude CLI with the given prompt (new session).

func (*Manager) StartWithPTY

func (m *Manager) StartWithPTY(ctx context.Context, prompt string, mode SessionMode, sessionID string, yoloMode bool) error

StartWithPTY spawns Claude CLI with a pseudo-terminal for true interactive support. This allows permission prompts to work interactively from remote clients. The PTY makes Claude think it's running in a real terminal. If prompt is empty, Claude starts in interactive mode waiting for user input.

func (*Manager) StartWithSession

func (m *Manager) StartWithSession(ctx context.Context, prompt string, mode SessionMode, sessionID string, permissionMode string) error

StartWithSession spawns Claude CLI with session control. permissionMode controls how Claude handles permissions: - "" or "default": Use skipPermissions config setting - "acceptEdits": Auto-accept file edits - "bypassPermissions": Skip all permission checks - "plan": Plan mode only

func (*Manager) State

func (m *Manager) State() events.ClaudeState

State returns the current state.

func (*Manager) Stop

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

Stop gracefully terminates the running Claude process.

func (*Manager) WorkspaceID

func (m *Manager) WorkspaceID() string

WorkspaceID returns the workspace ID for this manager.

type PTYParser

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

PTYParser detects and parses interactive prompts from terminal output.

func NewPTYParser

func NewPTYParser() *PTYParser

NewPTYParser creates a new PTY parser.

func (*PTYParser) GetState

func (p *PTYParser) GetState() PTYState

GetState returns the current PTY state.

func (*PTYParser) IsWaitingForInput

func (p *PTYParser) IsWaitingForInput() bool

IsWaitingForInput returns true if in a state that requires user input.

func (*PTYParser) ProcessLine

func (p *PTYParser) ProcessLine(rawLine string) (*PTYPermissionPrompt, PTYState)

ProcessLine processes a single line of PTY output. Returns a permission prompt if one is detected, nil otherwise.

func (*PTYParser) Reset

func (p *PTYParser) Reset()

Reset clears the parser state.

func (*PTYParser) SetState

func (p *PTYParser) SetState(state PTYState)

SetState sets the PTY state.

type PTYPermissionPrompt

type PTYPermissionPrompt struct {
	Type        PermissionType `json:"type"`
	Target      string         `json:"target"`      // filename or command
	Description string         `json:"description"` // full description
	Preview     string         `json:"preview"`     // file content preview or command
	Options     []PromptOption `json:"options"`
}

PTYPermissionPrompt represents a detected permission request.

type PTYState

type PTYState string

PTYState represents the current state of PTY interaction.

const (
	PTYStateIdle       PTYState = "idle"
	PTYStateThinking   PTYState = "thinking"
	PTYStatePermission PTYState = "permission"
	PTYStateQuestion   PTYState = "question"
	PTYStateError      PTYState = "error"
)

type ParsedLine

type ParsedLine struct {
	RawText   string `json:"raw_text"`   // Original text with ANSI codes
	CleanText string `json:"clean_text"` // Text with ANSI codes stripped
}

ParsedLine contains both raw and clean versions of a terminal line.

type PermissionType

type PermissionType string

PermissionType identifies what Claude wants to do.

const (
	PermissionTypeWriteFile   PermissionType = "write_file"
	PermissionTypeEditFile    PermissionType = "edit_file"
	PermissionTypeDeleteFile  PermissionType = "delete_file"
	PermissionTypeBashCommand PermissionType = "bash_command"
	PermissionTypeMCP         PermissionType = "mcp_tool"
	PermissionTypeTrustFolder PermissionType = "trust_folder"
	PermissionTypeUnknown     PermissionType = "unknown"
)

type PromptOption

type PromptOption struct {
	Key         string `json:"key"`                   // "1", "2", "n", etc.
	Label       string `json:"label"`                 // "Yes", "Yes to all", "No"
	Description string `json:"description,omitempty"` // optional extra info
	Selected    bool   `json:"selected"`              // true if this option has the cursor (❯)
}

PromptOption represents a choice in a permission prompt.

type SessionInfo

type SessionInfo struct {
	SessionID    string    `json:"session_id"`
	Summary      string    `json:"summary"`
	MessageCount int       `json:"message_count"`
	LastUpdated  time.Time `json:"last_updated"`
	Branch       string    `json:"branch,omitempty"`
}

SessionInfo represents information about a stored Claude session.

func ListSessions

func ListSessions(repoPath string) ([]SessionInfo, error)

ListSessions returns all available sessions for a repository.

type SessionMessage

type SessionMessage struct {
	Type      string          `json:"type"`
	UUID      string          `json:"uuid,omitempty"`
	SessionID string          `json:"session_id,omitempty"` // snake_case for API response
	Timestamp string          `json:"timestamp,omitempty"`
	GitBranch string          `json:"git_branch,omitempty"` // snake_case for API response
	Message   json.RawMessage `json:"message"`
}

SessionMessage represents a single message in a session for API response. Uses snake_case for consistency with WebSocket events.

func GetSessionMessages

func GetSessionMessages(repoPath, sessionID string) ([]SessionMessage, error)

GetSessionMessages returns all messages for a specific session. Returns raw JSON messages to preserve token usage and other metadata.

type SessionMode

type SessionMode string

SessionMode defines how to handle conversation sessions.

const (
	// SessionModeNew starts a new conversation (default).
	SessionModeNew SessionMode = "new"
	// SessionModeContinue continues a conversation by session ID.
	// Requires session_id parameter.
	SessionModeContinue SessionMode = "continue"
)

Jump to

Keyboard shortcuts

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