script

package
v0.4.8 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package script defines a language-agnostic script execution interface. Implementations (e.g. script/jsrt for JavaScript, script/luart for Lua via gopher-lua) provide concrete runtimes that satisfy the Runtime interface. Env assembly primitives and their ordering protocol live here; concrete host APIs are built with script/bindings. See bindings/doc.go for bridge layout and conventions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SignalToError added in v0.3.3

func SignalToError(sig *Signal) error

SignalToError maps a script control signal into a Go error that fulfils the appropriate errdefs / engine classification, so host code (scriptnode, future scriptengine, …) can share one consistent translation step instead of branching on sig.Type by hand.

Mapping rules:

  • nil signal, Type "done", or unrecognised Type return nil.
  • Type "error": errdefs wrapper chosen by Kind; unknown / empty Kind degrades to errdefs.Internal (the value is preserved in the wrapped message so observability surfaces the typo).
  • Type "interrupt": engine.Interrupted with Cause taken from Kind (unknown values fall through to engine.CauseCustom) and Detail taken from Message.

Returning nil for an unknown Type matches the existing scriptnode behaviour — only "error" / "interrupt" are observable side-effects.

Types

type BindingFunc added in v0.4.7

type BindingFunc func(ctx context.Context) (name string, value any)

BindingFunc creates a named binding for script execution. The returned name becomes the global variable name in the script scope.

type Env

type Env struct {
	// Config is script-level configuration, accessible as a global in the script.
	Config map[string]any

	// Bindings maps names to host objects injected into the script's global scope.
	// Each value is typically a map[string]any of Go functions callable from the script.
	Bindings map[string]any
}

Env carries per-execution configuration and host bindings.

func BuildEnv added in v0.4.7

func BuildEnv(ctx context.Context, config map[string]any, fns ...BindingFunc) *Env

BuildEnv creates a script Env from ordinary binding funcs.

type EnvBuilder added in v0.4.7

type EnvBuilder struct {
	// contains filtered or unexported fields
}

EnvBuilder assembles per-execution script environments.

Ordinary bindings run first in the order they were added. Late bindings run after ordinary bindings and receive the Env being built, including the final bindings map. If multiple bindings return the same name, the later binding wins. This preserves the map-based Env semantics while making order-sensitive bindings such as runtime.execScript explicit.

func NewEnvBuilder added in v0.4.7

func NewEnvBuilder(config map[string]any) *EnvBuilder

NewEnvBuilder creates an EnvBuilder using config as the script config.

func (*EnvBuilder) Add added in v0.4.7

func (b *EnvBuilder) Add(fns ...BindingFunc) *EnvBuilder

Add appends ordinary binding functions. They execute in insertion order.

func (*EnvBuilder) AddLate added in v0.4.7

func (b *EnvBuilder) AddLate(fns ...LateBindingFunc) *EnvBuilder

AddLate appends late binding functions. They execute after ordinary bindings.

func (*EnvBuilder) Build added in v0.4.7

func (b *EnvBuilder) Build(ctx context.Context) *Env

Build evaluates all bindings and returns a fresh Env.

type ErrorKind added in v0.3.3

type ErrorKind string

ErrorKind enumerates the errdefs categories scripts may set on signal.error({ kind, message }). Only categories that can plausibly originate from script logic are exposed — auth, rate-limit, timeout and similar wire-level classifications stay in the Go layer where they are produced.

Unknown kinds passed by a script degrade to ErrorKindInternal in SignalToError so a typo in script land cannot escape as an unclassified error.

const (
	// ErrorKindValidation marks user-input / config validation
	// failures. Maps to [errdefs.Validation] (HTTP 400).
	ErrorKindValidation ErrorKind = "validation"

	// ErrorKindNotFound marks a lookup the script performed that
	// returned nothing. Maps to [errdefs.NotFound] (HTTP 404).
	ErrorKindNotFound ErrorKind = "not_found"

	// ErrorKindBudgetExceeded marks a per-run / per-tenant budget
	// (token, cost, iteration count) the script self-detected.
	// Maps to [errdefs.BudgetExceeded] (HTTP 429).
	ErrorKindBudgetExceeded ErrorKind = "budget_exceeded"

	// ErrorKindPolicyDenied marks a policy-layer refusal raised from
	// inside the script (tool allow-list, role check, …). Maps to
	// [errdefs.PolicyDenied] (HTTP 403).
	ErrorKindPolicyDenied ErrorKind = "policy_denied"

	// ErrorKindNotAvailable marks a transient unavailability of a
	// dependency the script needed (e.g. a knowledge store down).
	// Maps to [errdefs.NotAvailable] (HTTP 503).
	ErrorKindNotAvailable ErrorKind = "not_available"

	// ErrorKindInternal is the default fallback for uncategorised
	// script-side errors. Maps to [errdefs.Internal] (HTTP 500).
	ErrorKindInternal ErrorKind = "internal"
)

type LateBindingFunc added in v0.4.7

type LateBindingFunc func(ctx context.Context, env *Env) (name string, value any)

LateBindingFunc creates a named binding after ordinary bindings are built. The Env argument exposes the final bindings map so late bindings can capture the same map passed to the runtime.

type Runtime

type Runtime interface {
	Exec(ctx context.Context, name, source string, env *Env) (*Signal, error)
}

Runtime executes scripts with injected host bindings. Implementations must be safe for concurrent use.

type Signal

type Signal struct {
	Type    string         `json:"type"`
	Kind    string         `json:"kind,omitempty"`
	Message string         `json:"message,omitempty"`
	Detail  map[string]any `json:"detail,omitempty"`
}

Signal represents a control signal from a script back to the host.

Type is always one of "interrupt" / "error" / "done" — the three outcomes a script can choose to surface besides "ran to completion".

Kind is a per-Type sub-classifier:

  • For Type "error" it carries an errdefs category name (see ErrorKind). SignalToError maps it onto the matching errdefs wrapper; unknown values degrade to ErrorKindInternal so a typo in script land cannot escape as an unclassified error.
  • For Type "interrupt" it carries an engine.Cause string. Unknown values degrade to engine.CauseCustom for the same reason.
  • For Type "done" Kind is unused.

Message is the human-readable detail. Detail is freeform structured metadata scripts may attach; it is preserved across host translation but is not inspected by SignalToError today.

Directories

Path Synopsis
Package bindings assembles host capabilities into a script.Env for any script.Runtime implementation (jsrt, luart, etc.).
Package bindings assembles host capabilities into a script.Env for any script.Runtime implementation (jsrt, luart, etc.).
Package jsrt provides a goja-based JavaScript implementation of script.Runtime.
Package jsrt provides a goja-based JavaScript implementation of script.Runtime.
Package luart provides a pure-Go Lua 5.1 implementation of script.Runtime using github.com/yuin/gopher-lua (no CGO).
Package luart provides a pure-Go Lua 5.1 implementation of script.Runtime using github.com/yuin/gopher-lua (no CGO).

Jump to

Keyboard shortcuts

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