decide

package
v0.20.0 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package decide provides the `decide` action: it evaluates a state against a registered decision backend and returns structured answers. Backends are registered by the application before the pipeline runs; the .nflow file only names the backend.

Typical use:

{ backend: "http-judge", state: .state, questions: { verdict: { type: "label", labels: ["ok","bad"] } } } -> decide.run

Index

Constants

View Source
const ID = "decide"

Variables

View Source
var DecideAction = action.New("decide.run", func(ctx context.Context, req Request) (Response, error) {
	if req.Backend == "" {
		return Response{}, xerr.BadRequest("decide: backend is required")
	}
	if len(req.Questions) == 0 {
		return Response{}, xerr.BadRequest("decide: questions map is required")
	}

	backend, exists := Lookup(req.Backend)
	if !exists {
		return Response{}, xerr.NotFound("decide: backend " + req.Backend + " not registered")
	}

	wireQuestions := make(map[string]any, len(req.Questions))
	for key, question := range req.Questions {
		wireQuestions[key] = question
	}

	result, err := backend.Decide(ctx, req.State, wireQuestions)
	if err != nil {
		return Response{}, xerr.Unavailable("decide: backend "+req.Backend+" execution failed", err)
	}
	return result, nil
}).
	Description("Evaluates state with a decision backend").
	Tag("ai", "decision").
	Build()

DecideAction evaluates a state against a registered backend.

Functions

func Bundle

func Bundle(_ map[string]string) core.Bundle

func LoadConfigFile

func LoadConfigFile(path string) error

LoadConfigFile reads a YAML config and registers every declared HTTP backend into the process-wide registry.

func Register

func Register(backend Backend) error

Register adds a backend to the process-wide registry.

Types

type Answer

type Answer struct {
	Value      any     `json:"value"`
	Confidence float64 `json:"confidence"`
	Label      string  `json:"label,omitempty"`
	Score      float64 `json:"score,omitempty"`
}

Answer is one question's resolved value.

type Backend

type Backend interface {
	Name() string
	Decide(ctx context.Context, state, questions map[string]any) (Result, error)
	Close() error
}

Backend is the contract every decision source implements.

func Lookup

func Lookup(name string) (Backend, bool)

Lookup resolves a backend by name from the process-wide registry.

func NewHTTPBackend

func NewHTTPBackend(configuration HTTPConfig) (Backend, error)

NewHTTPBackend builds an HTTP decision source. A zero Timeout defaults to 30 seconds.

type FileConfig

type FileConfig struct {
	Backends map[string]struct {
		Kind     string            `yaml:"kind"`
		Endpoint string            `yaml:"endpoint"`
		Timeout  time.Duration     `yaml:"timeout"`
		Headers  map[string]string `yaml:"headers,omitempty"`
	} `yaml:"backends"`
}

FileConfig is the YAML shape accepted by LoadConfigFile.

type HTTPConfig

type HTTPConfig struct {
	Name     string            `yaml:"name"`
	Endpoint string            `yaml:"endpoint"`
	Timeout  time.Duration     `yaml:"timeout"`
	Headers  map[string]string `yaml:"headers"`
}

HTTPConfig configures an HTTP-backed decision source.

type Question

type Question struct {
	Type     string   `json:"type"`
	Labels   []string `json:"labels,omitempty"`
	Rubric   []string `json:"rubric,omitempty"`
	Question string   `json:"question,omitempty"`
}

Question describes one decision to make.

type Registry

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

Registry is an in-process backend table. It is a plain struct, not a package-level variable, so tests can create isolated instances.

func NewRegistry

func NewRegistry() *Registry

NewRegistry returns an empty registry.

func (*Registry) Lookup

func (r *Registry) Lookup(name string) (Backend, bool)

Lookup returns the named backend.

func (*Registry) Names

func (r *Registry) Names() []string

Names returns every registered backend name, sorted.

func (*Registry) Register

func (r *Registry) Register(backend Backend) error

Register adds a backend. A duplicate name is an error, not a silent override.

type Request

type Request struct {
	Backend   string              `json:"backend" cli:"backend,b"`
	State     map[string]any      `json:"state,omitempty"`
	Questions map[string]Question `json:"questions"`
	TimeoutMS int64               `json:"timeout_ms,omitempty"`
}

Request is the decide action's input.

type Response

type Response = Result

Response is the decide action's output. It mirrors Result so callers never need to import the backend contract.

type Result

type Result struct {
	Answers map[string]Answer `json:"answers"`
	Routing map[string]any    `json:"routing,omitempty"`
}

Result is the backend's response.

Jump to

Keyboard shortcuts

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