Documentation
¶
Overview ¶
Package plugins is the executable plugin contract of Nucleus, both sides of it.
A capability plugin is an executable named `nucleus-plugin-<provider>`. Asked `capabilities` (or `capabilities --json`), it lists the capabilities it serves (`mail.send`, `queue.publish`, `webhook.deliver`, or any other `domain.action`); run with no arguments, it reads one RequestEnvelope from stdin, writes one ResponseEnvelope to stdout and exits with one of the ExitCode* codes.
The host side — what Nucleus runs — is DiscoverExternal and DiscoverAllowed, ProbeCapabilities and ExecuteRequest, behind the Host interface; a Policy decides which executables may run at all.
The plugin side — what a plugin author writes — is Serve: a Plugin with one typed handler per capability, and Serve speaks the envelope and the exit codes for it. A complete plugin built on it, tested through the real runtime, is internal/fixtures/plugins/nucleus-plugin-maildir in the Nucleus repository. The contract itself is docs/reference/PLUGIN_SDK.md.
Index ¶
- Constants
- func IsExecutableFile(path string, entry os.DirEntry) (bool, error)
- func ParseProviderFromBinary(name, prefix string) (string, bool)
- func ProbeCapabilities(ctx context.Context, binaryPath string, timeout time.Duration) ([]string, error)
- func Serve(p Plugin)
- func ServeIO(ctx context.Context, p Plugin, args []string, stdin io.Reader, ...) int
- func SupportsCapability(desc Descriptor, capability string) bool
- type Allowance
- type Descriptor
- func BuiltinMailDescriptorsFromProviders(providers []string) []Descriptor
- func CollectInventory(pathEnv string, builtinMailProviders []string, probeTimeout time.Duration) []Descriptor
- func DiscoverAllowed(pathEnv string, probeTimeout time.Duration, policy Policy) []Descriptor
- func DiscoverExternal(pathEnv string, probeTimeout time.Duration) []Descriptor
- type ExecutionError
- type Failure
- type Host
- type LocalHost
- func (LocalHost) CollectInventory(pathEnv string, builtinMailProviders []string, probeTimeout time.Duration) []Descriptor
- func (LocalHost) ExecuteRequest(ctx context.Context, binaryPath string, request RequestEnvelope, ...) (ResponseEnvelope, error)
- func (LocalHost) ProbeCapabilities(ctx context.Context, binaryPath string, timeout time.Duration) ([]string, error)
- type MailSendOutput
- type MailSendPayload
- type Plugin
- type Policy
- type QueuePublishOutput
- type QueuePublishPayload
- type RefusedError
- type RequestEnvelope
- type ResponseEnvelope
- type ResponseError
- type ResponseMetrics
- type Source
- type WebhookDeliverOutput
- type WebhookDeliverPayload
Constants ¶
const ( GenericBinaryPrefix = "nucleus-plugin-" DefaultProbeTimeout = 2 * time.Second )
const ( EnvelopeVersionV1 = "v1" CapabilityMailSend = "mail.send" CapabilityQueuePublish = "queue.publish" CapabilityWebhookDeliver = "webhook.deliver" )
const ( ExitCodeSuccess = 0 ExitCodeValidation = 10 ExitCodeTransient = 20 ExitCodeRejected = 30 ExitCodeTimeout = 40 ExitCodeInternal = 50 )
Variables ¶
This section is empty.
Functions ¶
func ParseProviderFromBinary ¶
func ProbeCapabilities ¶
func Serve ¶ added in v1.32.0
func Serve(p Plugin)
Serve runs the plugin side of the contract on the process: it answers `capabilities` (and `capabilities --json`) with the capabilities p serves, and with no arguments reads one request envelope from stdin, calls the handler, writes the response envelope to stdout and exits with the contract's exit code. It does not return.
func main() {
plugins.Serve(plugins.Plugin{
MailSend: func(ctx context.Context, req plugins.RequestEnvelope, m plugins.MailSendPayload) (plugins.MailSendOutput, error) {
// deliver m
return plugins.MailSendOutput{ProviderRequestID: "…"}, nil
},
})
}
SIGINT and SIGTERM cancel the handler's context.
func ServeIO ¶ added in v1.32.0
func ServeIO(ctx context.Context, p Plugin, args []string, stdin io.Reader, stdout, stderr io.Writer) int
ServeIO is Serve with the arguments and the streams passed in; it returns the exit code instead of exiting. A test drives a plugin through it.
func SupportsCapability ¶
func SupportsCapability(desc Descriptor, capability string) bool
Types ¶
type Allowance ¶ added in v1.32.0
Allowance is one entry of Policy.Allowed: a provider and the capabilities it may run.
type Descriptor ¶
type Descriptor struct {
Provider string `json:"provider"`
Capabilities []string `json:"capabilities"`
Source Source `json:"source"`
BinaryPath string `json:"binary_path,omitempty"`
ProbeError string `json:"probe_error,omitempty"`
// Refused is why the Policy the inventory was collected under does not
// let the binary run. A refused binary is never executed, not even to
// read its capabilities, so Capabilities is empty.
Refused string `json:"refused,omitempty"`
}
func BuiltinMailDescriptorsFromProviders ¶
func BuiltinMailDescriptorsFromProviders(providers []string) []Descriptor
func CollectInventory ¶
func CollectInventory(pathEnv string, builtinMailProviders []string, probeTimeout time.Duration) []Descriptor
func DiscoverAllowed ¶ added in v1.32.0
func DiscoverAllowed(pathEnv string, probeTimeout time.Duration, policy Policy) []Descriptor
DiscoverAllowed is DiscoverExternal under a policy: a binary the policy does not let run is listed with Refused set and is never executed, not even to read its capabilities.
func DiscoverExternal ¶
func DiscoverExternal(pathEnv string, probeTimeout time.Duration) []Descriptor
DiscoverExternal finds every `nucleus-plugin-<provider>` executable on pathEnv and asks each for its capabilities. It is DiscoverAllowed under the zero Policy, which allows every one.
type ExecutionError ¶
type ExecutionError struct {
ExitCode int
Retriable bool
Code string
Message string
Stderr string
Cause error
}
func (*ExecutionError) Error ¶
func (e *ExecutionError) Error() string
func (*ExecutionError) Unwrap ¶
func (e *ExecutionError) Unwrap() error
type Failure ¶ added in v1.32.0
Failure is the error a handler returns to choose how the host sees a failed request: the process exit code, the machine-readable code and the message of the response's error, and whether the host may retry.
func Fail ¶ added in v1.32.0
Fail builds a Failure. exitCode is one of the contract's codes — ExitCodeValidation (10), ExitCodeTransient (20), ExitCodeRejected (30), ExitCodeTimeout (40), ExitCodeInternal (50); any other value becomes ExitCodeInternal. Retriable follows the code's documented meaning: 20, 40 and 50 are retriable.
type Host ¶
type Host interface {
CollectInventory(pathEnv string, builtinMailProviders []string, probeTimeout time.Duration) []Descriptor
ProbeCapabilities(ctx context.Context, binaryPath string, timeout time.Duration) ([]string, error)
ExecuteRequest(ctx context.Context, binaryPath string, request RequestEnvelope, timeout time.Duration) (ResponseEnvelope, error)
}
Host is the stable plugin runtime contract exposed by Nucleus. It allows callers to discover providers, probe capabilities, and execute capability requests independently from the underlying transport/runtime.
type LocalHost ¶
type LocalHost struct{}
LocalHost is the default Host implementation backed by local executables.
func (LocalHost) CollectInventory ¶
func (LocalHost) ExecuteRequest ¶
func (LocalHost) ExecuteRequest(ctx context.Context, binaryPath string, request RequestEnvelope, timeout time.Duration) (ResponseEnvelope, error)
type MailSendOutput ¶
type MailSendOutput struct {
Accepted bool `json:"accepted"`
ProviderRequestID string `json:"provider_request_id,omitempty"`
}
func DecodeMailSendOutput ¶
func DecodeMailSendOutput(raw json.RawMessage) (MailSendOutput, error)
type MailSendPayload ¶
type Plugin ¶ added in v1.32.0
type Plugin struct {
MailSend func(ctx context.Context, req RequestEnvelope, payload MailSendPayload) (MailSendOutput, error)
QueuePublish func(ctx context.Context, req RequestEnvelope, payload QueuePublishPayload) (QueuePublishOutput, error)
WebhookDeliver func(ctx context.Context, req RequestEnvelope, payload WebhookDeliverPayload) (WebhookDeliverOutput, error)
// Custom serves capabilities this package has no schema for (a
// `sms.send`, say), keyed by capability name. The handler
// decodes its own payload; its output is encoded as JSON.
Custom map[string]func(ctx context.Context, req RequestEnvelope, payload json.RawMessage) (any, error)
}
Plugin is the plugin side of the contract: one handler per capability the executable provides. Serve advertises exactly the capabilities whose handler is set, decodes each request's payload into the capability's type, and writes the response envelope and the exit code the host reads.
A handler that returns a nil error has accepted the request: Serve sets the output's Accepted field. To refuse or fail, return a *Failure (see Fail) — its exit code and code are what the host sees. Any other error is an internal failure (exit 50, retriable). Write logs to stderr: stdout carries the response envelope and nothing else.
type Policy ¶ added in v1.32.0
type Policy struct {
// DenyExternal refuses every external executable, whatever the lists
// say (`plugins.allow_external: false`).
DenyExternal bool
// Allowed, when it has entries, is the only set of capability plugins
// that run: a provider runs a capability only when an entry names the
// provider and lists the capability. Empty allows every provider until
// v2.0.0.
Allowed []Allowance
// Commands, when it has entries, are the only external commands the CLI
// dispatches. Empty allows every `nucleus-<name>` on PATH until v2.0.0.
Commands []string
}
Policy says which external executables may run: the capability plugins (`nucleus-plugin-<provider>`, run by the runtime and by `nucleus plugin`) and the external commands (`nucleus-<name>`, run by `nucleus <name>`).
The zero value allows every one of them, which is what Nucleus did before the policy existed and stays the default until v2.0.0. From v2.0.0 an external executable runs only when the configuration lists it (DEP-2026-014).
The configuration keys are `plugins.allow_external`, `plugins.allowed` and `plugins.commands`; app.PluginsConfig.Policy builds a Policy from them.
func (Policy) AllowCommand ¶ added in v1.32.0
AllowCommand reports whether the CLI may dispatch `nucleus <name>` to the external command `nucleus-<name>`. A refusal is a *RefusedError.
func (Policy) AllowPlugin ¶ added in v1.32.0
AllowPlugin reports whether the capability plugin of provider may run capability. An empty capability asks whether the executable may run at all — to be asked for its capabilities — which an entry for the provider allows whatever capabilities it lists. A refusal is a *RefusedError.
func (Policy) ListsCommands ¶ added in v1.32.0
ListsCommands is ListsPlugins for the external commands.
func (Policy) ListsPlugins ¶ added in v1.32.0
ListsPlugins reports whether the policy names the capability plugins that may run, rather than allowing every one it is not told to refuse. A plugin that runs under a policy that does not list plugins is the case DEP-2026-014 announces will be refused from v2.0.0.
type QueuePublishOutput ¶
type QueuePublishPayload ¶
type RefusedError ¶ added in v1.32.0
type RefusedError struct {
// Kind is "plugin" for a capability plugin, "command" for an external
// command.
Kind string
// Name is the provider of a plugin, or the name of a command.
Name string
// Capability is the capability that was asked for; empty when the
// provider is refused outright.
Capability string
// Key is the configuration key whose value refused it.
Key string
}
RefusedError is the answer of a Policy for an executable it does not allow. It names the executable and the configuration key that refused it.
func (*RefusedError) Error ¶ added in v1.32.0
func (e *RefusedError) Error() string
type RequestEnvelope ¶
type RequestEnvelope struct {
Version string `json:"version"`
RequestID string `json:"request_id"`
Timestamp string `json:"timestamp"`
Capability string `json:"capability"`
Provider string `json:"provider"`
TimeoutMS int64 `json:"timeout_ms"`
Metadata map[string]string `json:"metadata,omitempty"`
Payload json.RawMessage `json:"payload"`
}
func NewRequestEnvelope ¶
type ResponseEnvelope ¶
type ResponseEnvelope struct {
Version string `json:"version"`
RequestID string `json:"request_id,omitempty"`
OK bool `json:"ok"`
ProviderRequestID string `json:"provider_request_id,omitempty"`
Retriable bool `json:"retriable,omitempty"`
Output json.RawMessage `json:"output,omitempty"`
Error *ResponseError `json:"error,omitempty"`
Metrics *ResponseMetrics `json:"metrics,omitempty"`
}
func ExecuteRequest ¶
func ExecuteRequest(ctx context.Context, binaryPath string, request RequestEnvelope, timeout time.Duration) (ResponseEnvelope, error)
type ResponseError ¶
type ResponseMetrics ¶
type ResponseMetrics struct {
DurationMS int64 `json:"duration_ms,omitempty"`
}