Documentation
¶
Overview ¶
Package claude provides a Go SDK for interacting with Claude Code CLI. It allows you to programmatically execute Claude queries and process responses.
Index ¶
- func Exec(ctx context.Context, args []string) (*bytes.Buffer, error)
- func IsClaudeAvailable() bool
- type AbortError
- type ArgumentBuilder
- type AssistantMessage
- type Client
- type CommandExecutor
- type ConfigError
- type DefaultCommandExecutor
- type DefaultMessageParser
- type MCPHTTPServerConfig
- type MCPSSEServerConfig
- type MCPServerConfig
- type MCPServerStatus
- type MCPStdioServerConfig
- type Message
- type MessageOrError
- type MessageParser
- type MessageStream
- type Options
- type ParseError
- type PermissionMode
- type PermissionRequestMessage
- type ProcessError
- type ResultMessage
- type SystemMessage
- type Usage
- type UserMessage
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsClaudeAvailable ¶
func IsClaudeAvailable() bool
IsClaudeAvailable checks if the Claude CLI is available in the system PATH
Types ¶
type AbortError ¶
type AbortError struct {
Message string
}
AbortError represents an operation that was aborted
func (*AbortError) Error ¶
func (e *AbortError) Error() string
type ArgumentBuilder ¶
type ArgumentBuilder struct{}
ArgumentBuilder builds command line arguments for Claude CLI
func (*ArgumentBuilder) BuildArgs ¶
func (b *ArgumentBuilder) BuildArgs(opts *Options) []string
BuildArgs constructs command line arguments from options
func (*ArgumentBuilder) Validate ¶
func (b *ArgumentBuilder) Validate(opts *Options) error
Validate checks if options are valid
type AssistantMessage ¶
type AssistantMessage struct { Type string `json:"type"` Message json.RawMessage `json:"message"` ParentToolUseID *string `json:"parent_tool_use_id"` SessionID string `json:"session_id"` }
AssistantMessage represents a message from the assistant
type Client ¶
type Client interface { Query(ctx context.Context, prompt string, opts *Options) (*ResultMessage, error) QueryStream(ctx context.Context, prompt string, opts *Options) (*MessageStream, error) }
Client interface for Claude Code interaction
func NewClientWithExecutor ¶
func NewClientWithExecutor(executor CommandExecutor) Client
NewClientWithExecutor creates a new Client with a custom executor
type CommandExecutor ¶
type CommandExecutor interface { Execute(ctx context.Context, name string, args []string, stdin string, workingDir string) ([]byte, error) ExecuteStream(ctx context.Context, name string, args []string, stdin string, workingDir string) (io.ReadCloser, error) }
CommandExecutor is an interface for executing commands
type ConfigError ¶
ConfigError represents a configuration error
func (*ConfigError) Error ¶
func (e *ConfigError) Error() string
type DefaultCommandExecutor ¶
type DefaultCommandExecutor struct{}
DefaultCommandExecutor implements CommandExecutor using os/exec
func (*DefaultCommandExecutor) Execute ¶
func (e *DefaultCommandExecutor) Execute(ctx context.Context, name string, args []string, stdin string, workingDir string) ([]byte, error)
Execute runs a command and returns its output
func (*DefaultCommandExecutor) ExecuteStream ¶
func (e *DefaultCommandExecutor) ExecuteStream(ctx context.Context, name string, args []string, stdin string, workingDir string) (io.ReadCloser, error)
ExecuteStream runs a command and returns a stream of its output
type DefaultMessageParser ¶
type DefaultMessageParser struct{}
DefaultMessageParser implements MessageParser
func (*DefaultMessageParser) ParseMessage ¶
func (p *DefaultMessageParser) ParseMessage(line string) (Message, error)
ParseMessage parses a JSON line into a Message
type MCPHTTPServerConfig ¶
MCPHTTPServerConfig represents an HTTP-based MCP server
func (MCPHTTPServerConfig) ToArg ¶
func (c MCPHTTPServerConfig) ToArg() string
ToArg converts the config to a CLI argument string
type MCPSSEServerConfig ¶
MCPSSEServerConfig represents an SSE-based MCP server
func (MCPSSEServerConfig) ToArg ¶
func (c MCPSSEServerConfig) ToArg() string
ToArg converts the config to a CLI argument string
type MCPServerConfig ¶
type MCPServerConfig interface { ToArg() string // contains filtered or unexported methods }
MCPServerConfig is an interface for MCP server configurations
type MCPServerStatus ¶
type MCPServerStatus struct { Name string `json:"name"` Status string `json:"status"` Error string `json:"error,omitempty"` }
MCPServerStatus represents the status of an MCP server
type MCPStdioServerConfig ¶
MCPStdioServerConfig represents a stdio-based MCP server
func (MCPStdioServerConfig) ToArg ¶
func (c MCPStdioServerConfig) ToArg() string
ToArg converts the config to a CLI argument string
type Message ¶
type Message interface {
// contains filtered or unexported methods
}
Message is an interface for all message types from Claude Code CLI
func ParseMessage ¶
ParseMessage parses a JSON line from the Claude Code CLI into a Message
type MessageOrError ¶
MessageOrError wraps a Message or an error
type MessageParser ¶
MessageParser interface for parsing messages
type MessageStream ¶
type MessageStream struct { Messages <-chan MessageOrError // contains filtered or unexported fields }
MessageStream represents a stream of messages from Claude
func QueryStream ¶
QueryStream executes a Claude Code query and returns a channel of messages
type Options ¶
type Options struct { // Tools configuration AllowedTools []string DisallowedTools []string // System prompt configuration CustomSystemPrompt string AppendSystemPrompt string // Working directory for the Claude Code CLI WorkingDir string // Token and turn limits MaxThinkingTokens *int MaxTurns *int // MCP server configuration MCPServers map[string]MCPServerConfig // Path to the Claude Code CLI executable PathToClaudeCodeExecutable string // Default: "claude" // Permission handling PermissionMode PermissionMode PermissionPromptToolName string // Session continuation Continue bool Resume string // Model configuration Model string FallbackModel string }
Options configures the behavior of Claude Code SDK
type ParseError ¶
ParseError represents an error parsing messages from the CLI
func (*ParseError) Error ¶
func (e *ParseError) Error() string
type PermissionMode ¶
type PermissionMode string
PermissionMode represents how the SDK handles permissions for tool use
const ( // PermissionDefault uses the default permission mode PermissionDefault PermissionMode = "default" // PermissionAcceptEdits automatically accepts file edits PermissionAcceptEdits PermissionMode = "acceptEdits" // PermissionBypassPermissions bypasses all permission prompts PermissionBypassPermissions PermissionMode = "bypassPermissions" // PermissionPlan uses plan mode PermissionPlan PermissionMode = "plan" )
PermissionMode constants define how Claude Code handles permissions for tool use
type PermissionRequestMessage ¶
type PermissionRequestMessage struct { Type string `json:"type"` SessionID string `json:"session_id"` Subtype string `json:"subtype"` }
PermissionRequestMessage represents a permission request for tool use
type ProcessError ¶
ProcessError represents an error from the Claude Code CLI process
func (*ProcessError) Error ¶
func (e *ProcessError) Error() string
type ResultMessage ¶
type ResultMessage struct { Type string `json:"type"` Subtype string `json:"subtype"` DurationMS int64 `json:"duration_ms"` DurationAPIMS int64 `json:"duration_api_ms"` IsError bool `json:"is_error"` NumTurns int `json:"num_turns"` Result string `json:"result,omitempty"` SessionID string `json:"session_id"` TotalCostUSD float64 `json:"total_cost_usd"` Usage Usage `json:"usage"` }
ResultMessage represents the final result of a Claude Code session
type SystemMessage ¶
type SystemMessage struct { Type string `json:"type"` Subtype string `json:"subtype"` APIKeySource string `json:"apiKeySource"` CWD string `json:"cwd"` SessionID string `json:"session_id"` Tools []string `json:"tools"` MCPServers []MCPServerStatus `json:"mcp_servers"` Model string `json:"model"` PermissionMode string `json:"permissionMode"` }
SystemMessage represents system information from Claude Code CLI
type Usage ¶
type Usage struct { CacheCreationInputTokens int `json:"cache_creation_input_tokens,omitempty"` CacheReadInputTokens int `json:"cache_read_input_tokens,omitempty"` InputTokens int `json:"input_tokens"` OutputTokens int `json:"output_tokens"` }
Usage represents token usage information
type UserMessage ¶
type UserMessage struct { Type string `json:"type"` Message json.RawMessage `json:"message"` ParentToolUseID *string `json:"parent_tool_use_id"` SessionID string `json:"session_id"` }
UserMessage represents a message from the user