Writing Operators (extend)
Audience: embedders adding an operator Pulse does not ship.
When to use it
Reach for extend when no built-in operator answers your question and a
request-level workaround (an expression, a computed attribute) would be
awkward. You implement one interface from
github.com/frankbardon/pulse/extend,
wrap it in the matching registration on pulse.Options.Extensions, and
Pulse adapts it at pulse.New. From then on predict, the manifest, MCP
and the runtime treat it exactly like a built-in.
extend imports only the public encoding, types and linalg
packages, never the engine, so an operator package stays small.
One aggregator
package spread
import (
"github.com/frankbardon/pulse"
"github.com/frankbardon/pulse/encoding"
"github.com/frankbardon/pulse/extend"
"github.com/frankbardon/pulse/types"
)
// Spread is the largest value minus the smallest.
type Spread struct{}
func (Spread) Aggregate(rows extend.Rows, field string) (float64, error) {
lo, hi, seen := 0.0, 0.0, false
for i := 0; i < rows.Len(); i++ {
v, ok := rows.At(i).NumericValue(field) // false for null
if !ok {
continue
}
if !seen || v < lo {
lo = v
}
if !seen || v > hi {
hi = v
}
seen = true
}
return hi - lo, nil
}
// New builds an instance that serves AGG_ACME_SPREAD.
func New(dataDir string) (*pulse.Pulse, error) {
return pulse.New(pulse.Options{DataDir: dataDir, Extensions: pulse.Extensions{
Aggregators: []pulse.AggregatorRegistration{{
Name: "AGG_ACME_SPREAD", // <CATEGORY>_<NAMESPACE>_<NAME>
Description: "Largest value minus the smallest.",
Factory: func(*types.Aggregation, *encoding.Schema) (extend.Aggregator, error) {
return Spread{}, nil
},
}},
}})
}
A request then names "type": "AGG_ACME_SPREAD" like any aggregator.
Rules that bite
- A
RecordorRowsis valid only during the call that received it: copy values out, never keep the handle (package doc, “Reuse contract”). NumericValueis false for null, for set fields and for vectors; a categorical’sNumericValueis its dictionary index.dateis epoch days,datetimeepoch seconds.- Streaming and shard merging are opt-in: set the registration’s
Streamable(andMergeable) flag and implement the matching sibling interface (OnlineAggregator,MergeableAggregator, …). Without them the operator runs buffered.
Where the detail lives
- Extension Points: every registration shape, naming policy, validation, component schemas, guidance metadata, embedder skills and examples.
- Per-category recipes: Adding an Aggregator and its siblings, Adding a Regression Model, Adding an Overlay Kind, Adding a Post-Fit Operator.
- GoDoc:
extend,pulse.Extensions.