Documentation
¶
Overview ¶
Package binding compiles restricted Go input and output types into immutable conversion plans and owns process-neutral value conversion.
One shared type compiler builds a post-order node arena for each direction. Inputs and outputs accept the same recursive structural universe (scalars of every integer and float width, pointers, slices, string-keyed maps, and nested structs) with direction-specific exceptions: inputs reject arrays, pointer-to-pointer, and custom unmarshalers; outputs reject custom marshalers. Root input field names must be Starlark identifiers because they are keyword arguments; every other field name only needs to be a valid JSON field name.
The input arena projects to InputSchema, a process-neutral description the worker validates once and uses to bind Starlark keyword arguments. The parent re-binds the worker's canonical map against the same arena to build the typed handler input and the authoritative canonical arguments.
Typed handler values, Starlark values, and JSON-shaped maps share one allowed matrix: nil, bool, string, int64, finite float64, []any, and map[string]any. ValidateValue, FromStarlark, and ToStarlark enforce that matrix plus positive depth and materialization limits. json.Number and other numeric types are rejected. Plan.InputShape and Plan.OutputShape remain flat FieldShape slices whose Type strings carry nested list, dict, struct, and nullable notation.
Index ¶
- Variables
- func FromStarlark(value starlark.Value, maxDepth int, maxNodes int) (any, error)
- func ToStarlark(value any, maxDepth int, maxNodes int) (starlark.Value, error)
- func ValidIdentifier(name string) bool
- func ValidateValue(value any, maxDepth int, maxNodes int) error
- type FieldShape
- type InputField
- type InputKind
- type InputNode
- type InputSchema
- type Plan
- func (plan *Plan) BindValue(arguments map[string]any) (any, map[string]any, error)
- func (plan *Plan) ConvertOutput(output any, maxDepth int, maxNodes int) (map[string]any, error)
- func (plan *Plan) InputSchema() InputSchema
- func (plan *Plan) InputShape() []FieldShape
- func (plan *Plan) InputType() reflect.Type
- func (plan *Plan) OutputShape() []FieldShape
- func (plan *Plan) OutputType() reflect.Type
- func (plan *Plan) Signature(capabilityName string) string
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidPlan classifies an input or output type that cannot form a restricted binding plan. ErrInvalidPlan = errors.New("invalid binding plan") // ErrInvalidArguments classifies Starlark arguments rejected before authorization. ErrInvalidArguments = errors.New("invalid capability arguments") // ErrUnsupportedValue classifies a Go or Starlark value outside the supported conversion surface. ErrUnsupportedValue = errors.New("unsupported value") // ErrValueLimit classifies converted output that exceeds a configured depth or byte limit. ErrValueLimit = errors.New("converted value limit exceeded") )
Functions ¶
func FromStarlark ¶
FromStarlark converts a Starlark value into a process-neutral MCP value.
maxDepth and maxNodes must be positive. Active cycles are rejected, and destination containers are allocated only after their child counts fit the remaining materialization budget.
func ToStarlark ¶
ToStarlark converts a process-neutral MCP value into a Starlark value.
maxDepth and maxNodes must be positive. Active cycles are rejected, and destination containers are allocated only after their child counts fit the remaining materialization budget.
func ValidIdentifier ¶
ValidIdentifier reports whether name is accepted as an identifier by the pinned Starlark scanner.
func ValidateValue ¶
ValidateValue reports whether value is a process-neutral MCP value within the supplied limits.
Accepted values are nil, bool, string, int64, finite float64, []any, and map[string]any, recursively. json.Number and other numeric types are rejected. maxDepth and maxNodes must be positive. Active cycles are rejected.
Types ¶
type FieldShape ¶
type FieldShape struct {
// Name is the field's exact Starlark and JSON name.
Name string `json:"name"`
// Type is the compact Starlark-facing value notation.
Type string `json:"type"`
// Required reports whether the caller or handler must provide the field.
Required bool `json:"required"`
}
FieldShape is one model-facing field in a supported capability input or output structure.
type InputField ¶ added in v0.2.2
type InputField struct {
// Name is the exact Starlark keyword or dict key.
Name string `json:"name"`
// Node is the arena index of the field's type.
Node int `json:"node"`
}
InputField is one named struct member in an input schema.
type InputKind ¶ added in v0.2.2
type InputKind string
InputKind identifies one node kind in a process-neutral input schema.
const ( // InputString accepts a Starlark str. InputString InputKind = "str" // InputInt accepts a Starlark int within the node's inclusive IntMin..IntMax range. InputInt InputKind = "int" // InputBool accepts a Starlark bool. InputBool InputKind = "bool" // InputFloat accepts a finite Starlark float or int; Float32 narrows the range. InputFloat InputKind = "float" // InputList accepts a Starlark list or tuple of Elem values. InputList InputKind = "list" // InputMap accepts a Starlark dict with str keys and Elem values. InputMap InputKind = "dict" // InputStruct accepts a Starlark dict whose keys are a subset of the declared // fields and include every required field. The root struct binds keyword arguments. InputStruct InputKind = "struct" // InputOptional accepts None or an Elem value; a struct member may also be omitted. InputOptional InputKind = "optional" )
type InputNode ¶ added in v0.2.2
type InputNode struct {
// Kind selects the accepted Starlark value shape.
Kind InputKind `json:"kind"`
// Elem is the element node for list, dict, and optional kinds.
Elem int `json:"elem,omitempty"`
// Fields are the declaration-ordered members of a struct node.
Fields []InputField `json:"fields,omitempty"`
// IntMin is the inclusive lower bound of an int node.
IntMin int64 `json:"int_min,omitempty"`
// IntMax is the inclusive upper bound of an int node.
IntMax int64 `json:"int_max,omitempty"`
// Float32 reports that a float node must fit the float32 range.
Float32 bool `json:"float32,omitempty"`
}
InputNode is one schema node.
type InputSchema ¶ added in v0.2.2
type InputSchema struct {
// Root is the arena index of the keyword-argument struct.
Root int `json:"root"`
// Nodes is the post-order node arena.
Nodes []InputNode `json:"nodes"`
}
InputSchema is the process-neutral input description the worker uses to bind keyword arguments without access to Go types.
Nodes form a post-order arena: every Elem and field Node index refers to an earlier node, which makes the graph acyclic by construction. Root is a struct node whose fields are the keyword arguments.
func (InputSchema) Bind ¶ added in v0.2.2
func (schema InputSchema) Bind( args starlark.Tuple, kwargs []starlark.Tuple, maxDepth int, maxNodes int, ) (map[string]any, error)
Bind binds keyword-only Starlark arguments against a validated schema.
It returns only a fresh canonical JSON-shaped map. Explicit Starlark None and omission both leave an optional member absent at every struct level. Callers must not treat the result as the authoritative authorization map; parent re-binding constructs that.
maxDepth and maxNodes must be positive and bound the canonical map exactly as FromStarlark bounds a value: the argument map is depth 1, optional wrappers add no depth, and every container's child count is charged against the node budget before the container is materialized. Starlark values that alias one container many times therefore fail with ErrValueLimit instead of being copied. Argument mistakes wrap ErrInvalidArguments.
func (InputSchema) Clone ¶ added in v0.2.2
func (schema InputSchema) Clone() InputSchema
Clone returns a deep copy that shares no slices with schema.
func (InputSchema) Validate ¶ added in v0.2.2
func (schema InputSchema) Validate() error
Validate reports whether schema is a well-formed acyclic arena with a struct root.
Root members must be Starlark identifiers; nested members must be valid JSON field names. Callers validate once when loading a schema; Bind trusts it.
type Plan ¶
type Plan struct {
// contains filtered or unexported fields
}
Plan is an immutable compiled input, output, signature, and canonical-argument plan.
func Compile ¶
Compile creates an immutable restricted plan for exact input and output struct types.
func CompileFor ¶
CompileFor compiles the exact generic input and output types once.
func (*Plan) BindValue ¶
BindValue reconstructs the exact registered Go input and a fresh canonical map.
arguments is the normalized map produced by worker-side binding. The parent re-checks every value against the compiled schema, builds the typed input by reflection, and returns a canonical copy that shares no container with arguments. An optional struct member that is absent or nil stays a nil pointer and is absent from the canonical map. Work is linear in the size of arguments: diagnostic paths are rendered only on error. Invalid maps are classified with ErrInvalidArguments.
func (*Plan) ConvertOutput ¶
ConvertOutput converts the plan's exact handler output to a process-neutral object.
maxDepth and maxNodes must be positive. The root struct is depth 1. Pointers add no depth. Nested structs, lists, and maps add one depth through their value node. Destination maps, lists, and sorted map-key slices are allocated only after their child counts fit the remaining materialization budget.
func (*Plan) InputSchema ¶ added in v0.2.2
func (plan *Plan) InputSchema() InputSchema
InputSchema returns a fresh process-neutral schema for worker-side binding.
func (*Plan) InputShape ¶
func (plan *Plan) InputShape() []FieldShape
InputShape returns a fresh model-facing description of the root input fields.
Type carries the full nested notation; Required is false only for pointer fields.
func (*Plan) OutputShape ¶
func (plan *Plan) OutputShape() []FieldShape
OutputShape returns a fresh model-facing description of the compiled output fields.
func (*Plan) OutputType ¶
OutputType returns the exact Go output type compiled into the plan.