client

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2026 License: GPL-3.0 Imports: 16 Imported by: 0

Documentation

Overview

Package client implements the interactive Client for multi-turn conversations with Claude.

The client package provides a stateful, bidirectional interface to the Claude CLI that maintains session state across multiple exchanges. Unlike the one-shot Query() function, Client enables:

  • Multi-turn conversations with persistent context
  • Interruption of Claude's processing
  • Dynamic configuration changes (model, permissions)
  • Hook system integration for tool permission management

The Client uses the protocol package for bidirectional control message handling and manages its own goroutines for message reading and routing.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client implements the interactive client interface.

func New

func New() *Client

New creates a new interactive client.

The client is not connected after creation. Call Start() with options to connect.

func (*Client) Close

func (c *Client) Close() error

Close terminates the session and cleans up resources.

After Close(), the client cannot be reused - create a new client with New(). This method is safe to call multiple times.

func (*Client) GetMCPStatus

func (c *Client) GetMCPStatus(ctx context.Context) (*mcp.Status, error)

GetMCPStatus queries the CLI for live MCP server connection status. Returns the status of all configured MCP servers.

func (*Client) GetServerInfo

func (c *Client) GetServerInfo() map[string]any

GetServerInfo returns server initialization info including available commands. Returns nil if not connected or not in streaming mode.

func (*Client) Interrupt

func (c *Client) Interrupt(ctx context.Context) error

Interrupt sends an interrupt signal to stop Claude's current processing.

This uses the protocol controller to send a control_request with subtype "interrupt".

func (*Client) Query

func (c *Client) Query(ctx context.Context, prompt string, sessionID ...string) error

Query sends a user prompt to Claude.

This method sends a user_message to the CLI and returns immediately. Use Receive() to get the responses. Optional sessionID defaults to "default" for multi-session support.

func (*Client) ReceiveMessages

func (c *Client) ReceiveMessages(ctx context.Context) iter.Seq2[message.Message, error]

ReceiveMessages returns an iterator that yields messages indefinitely. Messages are yielded as they arrive until EOF, an error occurs, or context is cancelled. Unlike ReceiveResponse, this iterator does not stop at ResultMessage.

func (*Client) ReceiveResponse

func (c *Client) ReceiveResponse(ctx context.Context) iter.Seq2[message.Message, error]

ReceiveResponse returns an iterator that yields messages until a ResultMessage is received. Messages are yielded as they arrive for streaming consumption. The iterator stops after yielding the ResultMessage.

func (*Client) RewindFiles

func (c *Client) RewindFiles(ctx context.Context, userMessageID string) error

RewindFiles rewinds tracked files to their state at a specific user message.

The userMessageID should be the ID of a previous user message in the conversation. The CLI must support file checkpointing for this to work.

func (*Client) SendToolResult added in v0.0.3

func (c *Client) SendToolResult(ctx context.Context, toolUseID, content string, isError bool) error

SendToolResult sends a tool_result message back to Claude for a pending tool_use. This allows the SDK to intercept tool calls (e.g. AskUserQuestion), gather a result externally, and feed it back so Claude continues processing.

func (*Client) SetModel

func (c *Client) SetModel(ctx context.Context, model *string) error

SetModel changes the AI model during conversation.

Pass nil to use the default model.

func (*Client) SetPermissionMode

func (c *Client) SetPermissionMode(ctx context.Context, mode string) error

SetPermissionMode changes the permission mode during conversation. Valid modes: "default", "acceptEdits", "plan", "bypassPermissions".

func (*Client) Start

func (c *Client) Start(ctx context.Context, options *config.Options) error

Start establishes a connection to the Claude CLI.

This method spawns the CLI subprocess and sets up bidirectional communication. For interactive sessions, no initial prompt is sent - use Query() to send prompts.

Returns CLINotFoundError if the CLI binary cannot be located, or CLIConnectionError if the process fails to start.

func (*Client) StartWithPrompt

func (c *Client) StartWithPrompt(
	ctx context.Context,
	prompt string,
	options *config.Options,
) error

StartWithPrompt establishes a connection and immediately sends an initial prompt.

This is a convenience method equivalent to calling Start() followed by Query(). The prompt is sent to the "default" session.

func (*Client) StartWithStream

func (c *Client) StartWithStream(
	ctx context.Context,
	messages iter.Seq[message.StreamingMessage],
	options *config.Options,
) error

StartWithStream establishes a connection and streams initial messages.

This method starts the client in streaming mode and consumes messages from the provided iterator. Messages are sent to the CLI via stdin. The iterator runs in a separate goroutine; use context cancellation to abort message streaming. EndInput is called automatically when the iterator completes.

Jump to

Keyboard shortcuts

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