subprocess

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: May 30, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (

	// MaxMessageSize bounds the encoded JSON-RPC message size accepted by the transport.
	MaxMessageSize = 10 * 1024 * 1024
)

Variables

This section is empty.

Functions

func MergeEnvironment

func MergeEnvironment(base map[string]string, extra map[string]string) []string

MergeEnvironment returns the current environment with base and extra assignments appended.

func NormalizeWaitError

func NormalizeWaitError(prefix string, err error) error

NormalizeWaitError wraps subprocess wait errors with a caller-provided prefix.

func ValidateInitializeResponse

func ValidateInitializeResponse(request InitializeRequest, response InitializeResponse) error

ValidateInitializeResponse checks the negotiated protocol version and capability subset.

Types

type InitializeRequest

type InitializeRequest struct {
	ProtocolVersion           string   `json:"protocol_version"`
	SupportedProtocolVersions []string `json:"supported_protocol_versions,omitempty"`
	GrantedCapabilities       []string `json:"granted_capabilities,omitempty"`
}

InitializeRequest describes the protocol version and capability contract offered by the host.

type InitializeResponse

type InitializeResponse struct {
	ProtocolVersion      string   `json:"protocol_version"`
	AcceptedCapabilities []string `json:"accepted_capabilities,omitempty"`
}

InitializeResponse describes the negotiated protocol version and accepted capability contract.

func Initialize

func Initialize(
	ctx context.Context,
	transport *Transport,
	requestID json.RawMessage,
	request InitializeRequest,
) (InitializeResponse, error)

Initialize sends an initialize request and validates the selected protocol version.

type LaunchConfig

type LaunchConfig struct {
	Command           []string
	Env               []string
	WorkingDir        string
	WaitDelay         time.Duration
	WaitErrorPrefix   string
	SignalGracePeriod time.Duration
}

LaunchConfig describes how to start and manage a subprocess.

type LockedBuffer

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

LockedBuffer is a concurrency-safe stderr capture buffer.

func (*LockedBuffer) String

func (b *LockedBuffer) String() string

String returns the current buffer contents as a string.

func (*LockedBuffer) Write

func (b *LockedBuffer) Write(p []byte) (int, error)

Write appends bytes to the buffer.

type Message

type Message struct {
	JSONRPC string           `json:"jsonrpc"`
	ID      *json.RawMessage `json:"id,omitempty"`
	Method  string           `json:"method,omitempty"`
	Params  json.RawMessage  `json:"params,omitempty"`
	Result  json.RawMessage  `json:"result,omitempty"`
	Error   *RequestError    `json:"error,omitempty"`
}

Message is a line-framed JSON-RPC 2.0 envelope.

type Process

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

Process manages a spawned subprocess and its lifetime.

func Launch

func Launch(ctx context.Context, cfg LaunchConfig) (*Process, error)

Launch starts a subprocess with the provided configuration.

func (*Process) CloseInput

func (p *Process) CloseInput()

CloseInput closes the subprocess stdin pipe.

func (*Process) Done

func (p *Process) Done() <-chan struct{}

Done returns a channel that closes when the subprocess exits.

func (*Process) Forced

func (p *Process) Forced() bool

Forced reports whether shutdown escalated beyond cooperative stdin closure.

func (*Process) Kill

func (p *Process) Kill() error

Kill force-terminates the subprocess immediately.

func (*Process) Shutdown

func (p *Process) Shutdown(timeout time.Duration) error

Shutdown attempts cooperative exit first, then escalates to SIGTERM and SIGKILL.

func (*Process) StderrBuffer

func (p *Process) StderrBuffer() *LockedBuffer

StderrBuffer returns the subprocess stderr capture buffer.

func (*Process) Stdin

func (p *Process) Stdin() io.WriteCloser

Stdin returns the subprocess stdin pipe.

func (*Process) Stdout

func (p *Process) Stdout() io.ReadCloser

Stdout returns the subprocess stdout pipe.

func (*Process) Wait

func (p *Process) Wait() error

Wait blocks until the subprocess exits and returns the normalized wait error.

type RequestError

type RequestError struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
	Data    any    `json:"data,omitempty"`
}

RequestError represents a JSON-RPC error object.

func NewInternalError

func NewInternalError(data any) *RequestError

NewInternalError creates a JSON-RPC internal error.

func NewInvalidParams

func NewInvalidParams(data any) *RequestError

NewInvalidParams creates a JSON-RPC invalid params error.

func NewInvalidRequest

func NewInvalidRequest(data any) *RequestError

NewInvalidRequest creates a JSON-RPC invalid request error.

func NewMethodNotFound

func NewMethodNotFound(method string) *RequestError

NewMethodNotFound creates a JSON-RPC method-not-found error.

func NewParseError

func NewParseError(data any) *RequestError

NewParseError creates a JSON-RPC parse error.

func (*RequestError) Error

func (e *RequestError) Error() string

Error returns a stable human-readable JSON-RPC error string.

type Transport

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

Transport reads and writes line-delimited JSON-RPC messages.

func NewTransport

func NewTransport(reader io.Reader, writer io.Writer) *Transport

NewTransport constructs a line-delimited JSON-RPC transport.

func (*Transport) ReadMessage

func (t *Transport) ReadMessage() (Message, error)

ReadMessage reads the next non-empty JSON-RPC message from the transport.

func (*Transport) WriteMessage

func (t *Transport) WriteMessage(message Message) error

WriteMessage writes one JSON-RPC message and appends a single trailing newline.

Jump to

Keyboard shortcuts

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