plugins

package
v1.35.0 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: Apache-2.0 Imports: 15 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
const (
	GenericBinaryPrefix = "nucleus-plugin-"
	DefaultProbeTimeout = 2 * time.Second
)
View Source
const (
	EnvelopeVersionV1 = "v1"

	CapabilityMailSend       = "mail.send"
	CapabilityQueuePublish   = "queue.publish"
	CapabilityWebhookDeliver = "webhook.deliver"
)
View Source
const (
	ExitCodeSuccess    = 0
	ExitCodeValidation = 10
	ExitCodeTransient  = 20
	ExitCodeRejected   = 30
	ExitCodeTimeout    = 40
	ExitCodeInternal   = 50
)

Variables

This section is empty.

Functions

func IsExecutableFile

func IsExecutableFile(path string, entry os.DirEntry) (bool, error)

func ParseProviderFromBinary

func ParseProviderFromBinary(name, prefix string) (string, bool)

func ProbeCapabilities

func ProbeCapabilities(ctx context.Context, binaryPath string, timeout time.Duration) ([]string, error)

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

type Allowance struct {
	Provider     string
	Capabilities []string
}

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

type Failure struct {
	ExitCode  int
	Code      string
	Message   string
	Retriable bool
}

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

func Fail(exitCode int, code, format string, args ...any) *Failure

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.

func (*Failure) Error added in v1.32.0

func (f *Failure) Error() string

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) CollectInventory(pathEnv string, builtinMailProviders []string, probeTimeout time.Duration) []Descriptor

func (LocalHost) ExecuteRequest

func (LocalHost) ExecuteRequest(ctx context.Context, binaryPath string, request RequestEnvelope, timeout time.Duration) (ResponseEnvelope, error)

func (LocalHost) ProbeCapabilities

func (LocalHost) ProbeCapabilities(ctx context.Context, binaryPath string, timeout time.Duration) ([]string, 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 MailSendPayload struct {
	From    string            `json:"from"`
	To      []string          `json:"to"`
	Subject string            `json:"subject"`
	Body    string            `json:"body"`
	Headers map[string]string `json:"headers,omitempty"`
}

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

func (p Policy) AllowCommand(name string) error

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

func (p Policy) AllowPlugin(provider, capability string) error

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

func (p Policy) ListsCommands() bool

ListsCommands is ListsPlugins for the external commands.

func (Policy) ListsPlugins added in v1.32.0

func (p Policy) ListsPlugins() bool

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.

func (Policy) String added in v1.32.0

func (p Policy) String() string

String renders the policy the way its configuration reads, for logs and diagnostics.

type QueuePublishOutput

type QueuePublishOutput struct {
	Accepted  bool   `json:"accepted"`
	MessageID string `json:"message_id,omitempty"`
}

type QueuePublishPayload

type QueuePublishPayload struct {
	Topic   string            `json:"topic"`
	Key     string            `json:"key,omitempty"`
	Body    json.RawMessage   `json:"body"`
	Headers map[string]string `json:"headers,omitempty"`
}

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

func NewRequestEnvelope(provider, capability string, timeout time.Duration, payload any, metadata map[string]string) (RequestEnvelope, error)

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 ResponseError struct {
	Code    string `json:"code"`
	Message string `json:"message"`
}

type ResponseMetrics

type ResponseMetrics struct {
	DurationMS int64 `json:"duration_ms,omitempty"`
}

type Source

type Source string
const (
	SourceBuiltinMail     Source = "builtin_mail"
	SourceExternalGeneric Source = "external_generic"
)

type WebhookDeliverOutput

type WebhookDeliverOutput struct {
	Accepted   bool `json:"accepted"`
	StatusCode int  `json:"status_code,omitempty"`
}

type WebhookDeliverPayload

type WebhookDeliverPayload struct {
	URL       string            `json:"url"`
	Method    string            `json:"method,omitempty"`
	Headers   map[string]string `json:"headers,omitempty"`
	Body      string            `json:"body,omitempty"`
	TimeoutMS int64             `json:"timeout_ms,omitempty"`
}

Jump to

Keyboard shortcuts

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