subprocess

package
v0.0.5 Latest Latest
Warning

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

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

Documentation

Overview

Package subprocess provides subprocess-based transport for the Codex CLI.

This package implements the Transport interface by spawning the Codex CLI as a child process and communicating via stdin/stdout. It handles process lifecycle management, message buffering, and error handling.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AppServerAdapter

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

AppServerAdapter bridges the control_request/control_response protocol used by Controller/Session to the JSON-RPC 2.0 protocol spoken by codex app-server. It implements config.Transport.

func NewAppServerAdapter

func NewAppServerAdapter(
	log *slog.Logger,
	opts *config.Options,
) *AppServerAdapter

NewAppServerAdapter creates a new adapter that wraps an AppServerTransport.

func (*AppServerAdapter) Close

func (a *AppServerAdapter) Close() error

Close shuts down the adapter and inner transport.

func (*AppServerAdapter) EndInput

func (a *AppServerAdapter) EndInput() error

EndInput is a no-op for app-server sessions which stay alive for multi-turn interaction.

func (*AppServerAdapter) IsReady

func (a *AppServerAdapter) IsReady() bool

IsReady delegates to the inner transport.

func (*AppServerAdapter) ReadMessages

func (a *AppServerAdapter) ReadMessages(
	_ context.Context,
) (<-chan map[string]any, <-chan error)

ReadMessages returns channels populated by the adapter read loop with messages in exec-event format that message.Parse() understands.

func (*AppServerAdapter) SendMessage

func (a *AppServerAdapter) SendMessage(ctx context.Context, data []byte) error

SendMessage intercepts outgoing control protocol messages and translates them into JSON-RPC calls on the inner transport.

func (*AppServerAdapter) Start

func (a *AppServerAdapter) Start(ctx context.Context) error

Start initializes the inner transport (JSON-RPC handshake) and starts the adapter read loop that translates notifications into exec-event format.

type AppServerTransport

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

AppServerTransport manages a codex app-server subprocess communicating via JSON-RPC 2.0 over stdin/stdout.

func NewAppServerTransport

func NewAppServerTransport(log *slog.Logger, opts *config.Options) *AppServerTransport

NewAppServerTransport creates a new app server transport.

func (*AppServerTransport) Close

func (t *AppServerTransport) Close() error

Close terminates the app server subprocess.

func (*AppServerTransport) IsReady

func (t *AppServerTransport) IsReady() bool

IsReady reports whether the transport has completed the initialize handshake.

func (*AppServerTransport) Notifications

func (t *AppServerTransport) Notifications() <-chan *RPCNotification

Notifications returns the channel of incoming notifications.

func (*AppServerTransport) Requests

func (t *AppServerTransport) Requests() <-chan *RPCIncomingRequest

Requests returns the channel of incoming server-to-client requests.

func (*AppServerTransport) SendRequest

func (t *AppServerTransport) SendRequest(
	ctx context.Context,
	method string,
	params any,
) (*RPCResponse, error)

SendRequest sends a JSON-RPC request and waits for the matching response.

func (*AppServerTransport) SendResponse

func (t *AppServerTransport) SendResponse(
	id int64,
	result json.RawMessage,
	rpcErr *RPCError,
) error

SendResponse sends a JSON-RPC response back to the server for a given request ID.

func (*AppServerTransport) Start

func (t *AppServerTransport) Start(ctx context.Context) error

Start discovers the Codex CLI binary, spawns the app-server subprocess, and performs the initialize handshake.

type CLITransport

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

CLITransport implements Transport by spawning a Codex CLI subprocess.

func NewCLITransport

func NewCLITransport(
	log *slog.Logger,
	prompt string,
	options *config.Options,
) *CLITransport

NewCLITransport creates a new CLI transport for one-shot exec mode.

func NewCLITransportWithMode

func NewCLITransportWithMode(
	log *slog.Logger,
	prompt string,
	options *config.Options,
	isStreaming bool,
) *CLITransport

NewCLITransportWithMode creates a new CLI transport with explicit mode control.

When isStreaming is true, the transport spawns `codex app-server` and keeps stdin open for bidirectional JSON-RPC. When false, it spawns `codex exec` for one-shot queries.

func (*CLITransport) Close

func (t *CLITransport) Close() error

Close terminates the CLI 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.

func (*CLITransport) ReadMessages

func (t *CLITransport) ReadMessages(
	ctx context.Context,
) (<-chan map[string]any, <-chan error)

ReadMessages reads JSON messages from the CLI stdout.

func (*CLITransport) SendMessage

func (t *CLITransport) SendMessage(ctx context.Context, data []byte) error

SendMessage sends a JSON message to the CLI stdin.

func (*CLITransport) Start

func (t *CLITransport) Start(ctx context.Context) error

Start starts the CLI subprocess.

type RPCError

type RPCError struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
}

RPCError contains error information from a JSON-RPC response.

func (*RPCError) Error

func (e *RPCError) Error() string

Error implements the error interface.

type RPCIncomingRequest

type RPCIncomingRequest struct {
	JSONRPC string          `json:"jsonrpc"`
	Method  string          `json:"method"`
	ID      int64           `json:"id"`
	Params  json.RawMessage `json:"params,omitempty"`
}

RPCIncomingRequest is a JSON-RPC 2.0 request received from the server.

type RPCNotification

type RPCNotification struct {
	JSONRPC string          `json:"jsonrpc"`
	Method  string          `json:"method"`
	Params  json.RawMessage `json:"params,omitempty"`
}

RPCNotification is a JSON-RPC 2.0 notification (no id field).

type RPCRequest

type RPCRequest struct {
	JSONRPC string `json:"jsonrpc"`
	Method  string `json:"method"`
	ID      int64  `json:"id"`
	Params  any    `json:"params,omitempty"`
}

RPCRequest is a JSON-RPC 2.0 request message.

type RPCResponse

type RPCResponse struct {
	JSONRPC string          `json:"jsonrpc"`
	ID      int64           `json:"id"`
	Result  json.RawMessage `json:"result,omitempty"`
	Error   *RPCError       `json:"error,omitempty"`
}

RPCResponse is a JSON-RPC 2.0 response message.

Jump to

Keyboard shortcuts

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