decide

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2026 License: MIT Imports: 7 Imported by: 1 module, 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 decide defines the contract for decision models: a request carries a shared state and a set of typed questions, evaluated in parallel and in isolation, and returns typed answers with calibrated probabilities. It is the decision-model counterpart of provider.Provider for chat.

The wire implementation lives in decide/openrouter. A caller may also supply its own Decider; Roles is the decorator that maps an application role such as "oracle" to a model id, a timeout, and an optional fallback.

Index

Constants

View Source
const (
	// KindChoice picks one option from Criteria.
	KindChoice = "choice"
	// KindNoul reports the probability that a yes/no condition holds.
	KindNoul = "noul"
	// KindScore places the state on an ordered set of Levels.
	KindScore = "score"
)

The question kinds the Decisions API documents. KindChoice is the default when Question.Kind is empty. An unknown kind is carried through unchanged, so a new wire kind does not need an API change here.

Variables

This section is empty.

Functions

func ValidateResponse

func ValidateResponse(req Request, resp *Response) error

ValidateResponse checks a response against the request that produced it. It rejects a value outside the question's option set or level range, a missing or unexpected answer, and a confidence or probability outside [0,1]. The wire client treats a failure as a malformed response and does not pass it through to the caller.

Types

type Answer

type Answer struct {
	Kind          string             `json:"kind,omitempty"`
	Choice        string             `json:"choice,omitempty"`
	Noul          float64            `json:"noul"`
	Score         float64            `json:"score"`
	Legend        map[string]string  `json:"legend,omitempty"`
	Probabilities map[string]float64 `json:"probabilities,omitempty"`
	Confidence    float64            `json:"confidence"`
}

Answer is one question's result. The fields that matter depend on the question kind: a choice answer names one option from Criteria, a noul answer is a probability, and a score answer is a position on Levels with an optional legend and per-level probabilities keyed by their index.

type Catalog

type Catalog interface {
	List(ctx context.Context) ([]provider.Model, error)
}

Catalog reads a live decision-model roster. decide/openrouter's Client implements it, so a benchmark can accept a catalog without naming a vendor.

type Clock

type Clock = roles.Clock

Clock is the time seam for a role's timeout. It is the shared roles.Clock, so a caller can inject one clock for every primitive.

type Decider

type Decider interface {
	Decide(ctx context.Context, req Request) (*Response, error)
}

Decider is a decision backend. Implementations must honor ctx cancellation and must return a non-nil error on failure.

type Question

type Question struct {
	Kind         string            `json:"kind,omitempty"`
	Instructions any               `json:"instructions,omitempty"`
	Criteria     map[string]string `json:"criteria,omitempty"`
	Levels       []string          `json:"levels,omitempty"`
}

Question is one typed question. Kind defaults to KindChoice when empty.

A choice question uses Criteria, whose keys are the option set. A noul question may use Criteria with exactly the keys "true" and "false". A score question uses Levels, ordered lowest first; its wire criteria are that list. Instructions may be a string, or an object/array when the model accepts structured instructions.

type Request

type Request struct {
	// Model is a catalog id or a configured role name. Roles resolves a role;
	// a direct id must contain a slash so an unknown role is never mistaken for
	// a model id.
	Model string `json:"model"`
	// State is the shared evidence: a string, or []Source for a multi-source
	// comparison.
	State any `json:"state"`
	// Questions are keyed by the caller's name for the answer. The map must not
	// be empty.
	Questions map[string]Question `json:"questions"`
}

Request is one decision call. Every question is evaluated in parallel and in isolation against the same State, so a caller should fan out questions in one request rather than making one call per question.

func (Request) Validate

func (r Request) Validate() error

Validate checks a request before it reaches the wire, so a malformed question is a local error instead of a 400 from the provider.

type Response

type Response struct {
	// Answers is keyed by the request's question name.
	Answers map[string]Answer `json:"answers"`
	// Usage is the call's token and cost accounting. The Decisions API reports
	// one cost per call, not per question.
	Usage provider.Usage `json:"usage"`
	// Model is the build that actually answered, when the server reports it.
	// An alias moves silently, so a caller that records a benchmark result
	// should record this too.
	Model string `json:"model,omitempty"`
}

Response is one decision call's result.

type Role

type Role = roles.Role

Role maps one application role to a model id and its bounds. Timeout is per attempt; zero means only the caller's context bounds the call. Fallback is tried when the primary fails and the caller's context is still live.

type Roles

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

Roles resolves a role name to a model id and applies the role's timeout and fallback. A request whose Model contains a slash is treated as a direct model id and passes through; a bare unknown name is a configuration error naming the role, never a 400 from the wire. The resolver itself lives in roles, because every primitive that maps an application name to a model needs the same rules.

func NewRoles

func NewRoles(cfg RolesConfig) *Roles

NewRoles builds the decorator. It does not validate the table, because a bad role is a configuration error the caller should see when it is used, not a crash at startup.

func (*Roles) Decide

func (r *Roles) Decide(ctx context.Context, req Request) (*Response, error)

Decide resolves req.Model, then calls the decider with the role's timeout and fallback. Usage from a failed attempt is never lost: it is summed into the response, which is returned even alongside the first error.

type RolesConfig

type RolesConfig struct {
	Decider Decider
	// Roles maps an application role name to its model and bounds.
	Roles map[string]Role
	// Clock drives a role's timeout; nil means the wall clock.
	Clock Clock
	// AllowAliases permits ids that end in "-latest" or start with "~".
	// False is the default: an alias moves without notice, so a decision that
	// fires on every turn could change model and nobody would see it.
	AllowAliases bool
}

RolesConfig builds a Roles decorator.

type Source

type Source struct {
	Source string `json:"source"`
	Text   string `json:"text"`
}

Source is one entry of a multi-source State, for a comparison where each source must stay separate on the wire instead of being concatenated.

Directories

Path Synopsis
Package openrouter implements decide.Decider over OpenRouter's decision surfaces.
Package openrouter implements decide.Decider over OpenRouter's decision surfaces.

Jump to

Keyboard shortcuts

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