Documentation
¶
Overview ¶
Package subprocess provides subprocess-based transport for the Claude CLI.
This package implements the Transport interface by spawning the Claude CLI as a child process and communicating via stdin/stdout. It handles process lifecycle management, message buffering, and error handling.
Index ¶
- type CLITransport
- func (t *CLITransport) Close() error
- func (t *CLITransport) CloseStdin() error
- func (t *CLITransport) EndInput() error
- func (t *CLITransport) IsReady() bool
- func (t *CLITransport) ReadMessages(ctx context.Context) (<-chan map[string]any, <-chan error)
- func (t *CLITransport) SendMessage(ctx context.Context, data []byte) error
- func (t *CLITransport) Start(ctx context.Context) error
- func (t *CLITransport) WritePrompt(ctx context.Context) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CLITransport ¶
type CLITransport struct {
// contains filtered or unexported fields
}
CLITransport implements Transport by spawning a Claude CLI subprocess.
func NewCLITransport ¶
NewCLITransport creates a new CLI transport with the given prompt and options.
The logger is used for operation tracking and debugging. It will receive debug, info, warn, and error messages during transport operations.
CLI discovery is deferred to Start(), which searches for the Claude CLI binary in the following order:
- The explicit path in options.CliPath (if provided)
- The system PATH
- Common installation directories (/usr/local/bin, /usr/bin, ~/.local/bin)
Start() returns CLINotFoundError if the CLI binary cannot be located.
func NewCLITransportWithMode ¶
func NewCLITransportWithMode( log *slog.Logger, prompt string, options *config.Options, isStreaming bool, ) *CLITransport
NewCLITransportWithMode creates a new CLI transport with explicit streaming mode control.
When isStreaming is true, the transport uses --input-format stream-json and keeps stdin open for sending multiple messages. The prompt is not passed on the command line in streaming mode - instead, messages are sent via stdin using SendMessage.
When isStreaming is false (default), the transport uses --print mode and writes the one-shot prompt to stdin after the process starts.
CLI discovery is deferred to Start() where it can use the caller's context.
func (*CLITransport) Close ¶
func (t *CLITransport) Close() error
Close terminates the CLI process.
This forcefully kills the CLI process using SIGKILL. It's safe to call Close multiple times or on an already-terminated process.
func (*CLITransport) CloseStdin ¶
func (t *CLITransport) CloseStdin() error
CloseStdin closes the stdin pipe to signal end of input in streaming mode.
This is used in streaming mode to indicate that no more messages will be sent. The CLI process will continue processing any pending input and then exit normally.
func (*CLITransport) EndInput ¶
func (t *CLITransport) EndInput() error
EndInput ends the input stream (closes stdin for process transports).
This signals to the CLI that no more input will be sent. The CLI process will continue processing any pending input and then exit normally.
func (*CLITransport) IsReady ¶
func (t *CLITransport) IsReady() bool
IsReady checks if the transport is ready for communication.
Returns true if the CLI process is running and stdin is open.
func (*CLITransport) ReadMessages ¶
ReadMessages reads JSON messages from the CLI stdout.
This method starts a goroutine that reads line-delimited JSON from the CLI process stdout. Each line is parsed as a JSON object and sent to the messages channel.
The goroutine exits when:
- The CLI process terminates
- The context is cancelled
- An unrecoverable error occurs
Parse errors for individual messages are sent to the error channel but do not stop message processing. The goroutine closes both channels when it exits.
func (*CLITransport) SendMessage ¶
func (t *CLITransport) SendMessage(ctx context.Context, data []byte) error
SendMessage sends a JSON message to the CLI stdin.
The data should be a complete JSON message followed by a newline. This method is safe for concurrent use and respects context cancellation even during blocking writes.
If context is cancelled during a blocked write, stdin is closed to unblock the goroutine (safe since Go 1.9+). Subsequent calls will return ErrStdinClosed.
func (*CLITransport) Start ¶
func (t *CLITransport) Start(ctx context.Context) error
Start starts the CLI subprocess.
This method discovers the Claude CLI binary, builds command arguments, and spawns the process with the configured environment variables. It sets up stdin, stdout, and stderr pipes for communication.
Returns CLINotFoundError if the CLI binary cannot be located, or ConnectionError if the process fails to start.
func (*CLITransport) WritePrompt ¶ added in v0.0.4
func (t *CLITransport) WritePrompt(ctx context.Context) error
WritePrompt writes the stored prompt to stdin for one-shot (--print) mode. In --print mode the Claude CLI reads the prompt from stdin when no positional argument is provided. This avoids OS ARG_MAX limits for large prompts.
This method is a no-op in streaming mode (where messages are sent via SendMessage).