Documentation
¶
Overview ¶
Package client provides a standard PlugKit host implementation. It allows launching and communicating with external plugin processes using CBOR-encoded messages over standard input and output.
Index ¶
- func HandleMessage[In any, Out any](c WrappedMessageHandler, name string, handler func(In) (Out, error))
- type SmartPlugClient
- func (c *SmartPlugClient) HandleMessageType(name string, handler func(any) (any, error))
- func (c *SmartPlugClient) RespondRaw(t string, v any) error
- func (c *SmartPlugClient) RunCommand(name codes.MessageCode, v any) (codes.PluginExitReason, any, error)
- func (c *SmartPlugClient) SetCommand(command string)
- func (c *SmartPlugClient) StartLocal() error
- type WrappedMessageHandler
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func HandleMessage ¶ added in v0.1.0
func HandleMessage[In any, Out any](c WrappedMessageHandler, name string, handler func(In) (Out, error))
HandleMessage is a generic convenience wrapper for HandleMessageType. It allows registering a strongly-typed handler function without manual wrapping.
The handler should have the form: func(In) (Out, error), where In is the expected input type (after CBOR decoding), and Out is the return type. Internally, the handler is wrapped using WrapHandler to conform to the PlugKit internal interface.
Types ¶
type SmartPlugClient ¶ added in v0.1.0
type SmartPlugClient struct {
Handlers map[string]func(any) (any, error)
// contains filtered or unexported fields
}
SmartPlugClient represents a PlugKit host instance. It manages the lifecycle and communication of a plugin process.
Communication is handled via stdin/stdout pipes using CBOR encoding. Messages are exchanged as Envelope structures with a defined message type and payload.
func NewSmartClient ¶ added in v0.1.0
func NewSmartClient(name string) *SmartPlugClient
NewSmartClient creates a new SmartPlugClient instance with the given plugin command. The plugin is not started automatically — use StartLocal() to launch it.
func (*SmartPlugClient) HandleMessageType ¶ added in v0.1.0
func (c *SmartPlugClient) HandleMessageType(name string, handler func(any) (any, error))
HandleMessageType registers a handler for a specific incoming message type.
Handlers are called when a message of the given type is received from the plugin.
func (*SmartPlugClient) RespondRaw ¶ added in v0.1.0
func (c *SmartPlugClient) RespondRaw(t string, v any) error
RespondRaw sends a raw response to the plugin with a specified type and payload.
This method bypasses the MessageCode abstraction and can be used for ad-hoc messages.
func (*SmartPlugClient) RunCommand ¶ added in v0.1.0
func (c *SmartPlugClient) RunCommand(name codes.MessageCode, v any) (codes.PluginExitReason, any, error)
RunCommand sends a command (message) to the plugin and waits for its response.
The message is wrapped in an Envelope with the given message code and payload. Based on the plugin's response, this method returns an appropriate PluginExitReason, a handler result (if any), and an error.
The second return value (of type any) represents the value returned by a matching message handler registered via HandleMessageType. If the plugin responds with a recognized message type and the corresponding handler executes successfully, its return value is passed back to the caller.
If no matching handler is found or the handler fails, the returned result will be nil. In general, it's recommended that plugins return a single, well-defined response type, or exit with an appropriate error code when things go wrong. Supporting multiple possible response types is possible, but may require custom decoding logic on the client side.
func (*SmartPlugClient) SetCommand ¶ added in v0.1.0
func (c *SmartPlugClient) SetCommand(command string)
SetCommand sets the executable path or name of the plugin binary. This must be set before calling StartLocal().
func (*SmartPlugClient) StartLocal ¶ added in v0.1.0
func (c *SmartPlugClient) StartLocal() error
StartLocal starts the plugin process using the provided command.
It sets up CBOR encoding/decoding over stdin/stdout pipes. Returns an error if the plugin cannot be started or the communication setup fails.