decisions

package
v2.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: AGPL-3.0 Imports: 7 Imported by: ? modules, 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 decisions holds the wire types for decision models ("System One" models): models that answer typed questions about a piece of evidence with calibrated probabilities instead of generating text.

Two wire formats are understood:

  • OpenAI Decisions (POST /v1/decisions): the canonical client contract the gateway exposes on its unified /v1 ingress. Questions are an ordered array; answers come back as an array in the same order.
  • System One (POST /v1/systemone): TypeSafe Jev and wire-compatible self-hosts (llama.cpp serving Kev, OpenJev, Laya, ...). Questions and answers are maps keyed by question name.

The package is a dependency-free leaf so the proxy, the vendors, the plugin SDK and the microgateway can all share one parser and one translator.

Index

Constants

View Source
const (
	MaxSystemOneChoices = 255
	MaxSystemOneLevels  = 10
)

System One limits (TypeSafe API reference).

View Source
const (
	OpenAIEndpoint    = "/v1/decisions"
	SystemOneEndpoint = "/v1/systemone"
)

Upstream endpoints, relative to a vendor's API base.

View Source
const (
	TypePredicate = "predicate"
	TypeChoice    = "choice"
	TypeScore     = "score"
	// TypeRefusal marks an answer the model declined to give.
	TypeRefusal = "refusal"
)

Question and answer types of the OpenAI (canonical) format.

View Source
const (
	SystemOneTypeNoul   = "noul"
	SystemOneTypeChoice = "choice"
	SystemOneTypeScore  = "score"
)

Question types of the System One format. Choice and score share their names with the OpenAI format; noul is System One's predicate.

View Source
const KindDecision = "decision"

KindDecision is the request kind (models.RequestKind, the plugin context's request_kind) of decision-model traffic. Generative traffic has kind "".

Variables

View Source
var ErrImagesUnsupported = errors.New("image input is not supported by this decision model; send text input only")

ErrImagesUnsupported is returned when a request carrying image input is translated for an upstream that only accepts text state.

Functions

func InputText

func InputText(input json.RawMessage) (string, int, error)

InputText flattens an OpenAI-format input into plain text and counts its image parts. A string input is returned as is; message inputs have their text parts joined by blank lines.

func IsDecisionPath

func IsDecisionPath(path string) bool

IsDecisionPath reports whether an upstream request path targets a decision endpoint in either format. The path may carry any prefix (/llm/rest/{slug}, an Azure deployment path, ...); only the trailing endpoint is matched.

func KindForPath

func KindForPath(path string) string

KindForPath is the request kind of an upstream request path: KindDecision for a decision endpoint, "" otherwise.

func RenderAnswers

func RenderAnswers(resp *Response) string

RenderAnswers renders a response as compact text, one answer per line, for text-based response filters and logs.

func StateText

func StateText(raw json.RawMessage) string

StateText renders System One state (or instructions) as text: a string as is, an object or array as compact JSON.

func Validate

func Validate(req *Request, target Format) error

Validate checks an OpenAI-format request before it is forwarded to target. It enforces what the gateway relies on (named, typed questions it can count and translate) plus the target's documented limits; anything else is left to the upstream to judge.

Types

type Answer

type Answer struct {
	Type string `json:"type"`
	Name string `json:"name"`
	// Probability is the predicate's probability that the condition holds.
	Probability *float64 `json:"probability,omitempty"`
	// Choice is the selected option of a choice question.
	Choice string `json:"choice,omitempty"`
	// Score is the probability-weighted average level index of a score question.
	Score         *float64            `json:"score,omitempty"`
	Probabilities []AnswerProbability `json:"probabilities,omitempty"`
	Confidence    *float64            `json:"confidence,omitempty"`
}

Answer is one OpenAI-format answer. Which fields are set depends on Type.

type AnswerProbability

type AnswerProbability struct {
	Value       any     `json:"value"`
	Label       string  `json:"label,omitempty"`
	Probability float64 `json:"probability"`
}

AnswerProbability is one entry of a choice or score distribution. For a choice Value is the option string; for a score it is the level index and Label carries the level's label.

type Choice

type Choice struct {
	Value       string `json:"value"`
	Description string `json:"description,omitempty"`
}

Choice is one option of a choice question.

type Format

type Format string

Format names the wire format of a decision request or response body.

const (
	FormatOpenAI    Format = "openai"
	FormatSystemOne Format = "systemone"
)

func DetectResponseFormat

func DetectResponseFormat(body []byte) (Format, bool)

DetectResponseFormat reports the wire format of a decision response body from the shape of its answers field (an array for OpenAI, an object for System One).

func FormatForPath

func FormatForPath(path string) (Format, bool)

FormatForPath reports which decision wire format an upstream path speaks.

type InputTokensDetails

type InputTokensDetails struct {
	CachedTokens     int `json:"cached_tokens"`
	CacheWriteTokens int `json:"cache_write_tokens"`
}

InputTokensDetails is the cache share of a call's input tokens.

type Level

type Level struct {
	Label       string `json:"label"`
	Description string `json:"description,omitempty"`
}

Level is one rung of a score question.

type NamedQuestion

type NamedQuestion struct {
	Name     string
	Question SystemOneQuestion
}

NamedQuestion is a System One question with its map key.

type OrderedQuestions

type OrderedQuestions []NamedQuestion

OrderedQuestions is the System One questions map in wire order.

type Question

type Question struct {
	Type         string   `json:"type"`
	Name         string   `json:"name"`
	Instructions string   `json:"instructions,omitempty"`
	Choices      []Choice `json:"choices,omitempty"`
	// Levels are ordered lowest to highest.
	Levels []Level `json:"levels,omitempty"`
}

Question is one OpenAI-format question.

type Request

type Request struct {
	Model string `json:"model"`
	// Input is the shared evidence: a JSON string, or an array of user
	// messages whose content is a string or input_text/input_image parts.
	Input     json.RawMessage `json:"input,omitempty"`
	Questions []Question      `json:"questions"`
}

Request is an OpenAI-format decision request.

func ParseRequest

func ParseRequest(body []byte, format Format) (*Request, error)

ParseRequest parses a request body in the given format into the OpenAI shape.

func ToOpenAIRequest

func ToOpenAIRequest(so *SystemOneRequest) *Request

ToOpenAIRequest converts a System One request into the OpenAI shape, so plugins and filters can reason about one schema. State becomes a string input (objects and arrays are carried as compact JSON text).

type Response

type Response struct {
	ID      string   `json:"id,omitempty"`
	Object  string   `json:"object,omitempty"`
	Model   string   `json:"model,omitempty"`
	Answers []Answer `json:"answers"`
	Usage   *Usage   `json:"usage,omitempty"`
}

Response is an OpenAI-format decision response.

func FromSystemOne

func FromSystemOne(so *SystemOneResponse, req *Request) *Response

FromSystemOne translates a System One response into an OpenAI-format response, returning answers in the order of req's questions. A question with no answer, or an answer of an unexpected type, becomes a refusal.

func ParseResponse

func ParseResponse(body []byte, req *Request) (*Response, error)

ParseResponse parses a response body in either format into the OpenAI shape. req, when given, orders System One answers like its questions.

type Summary

type Summary struct {
	Questions int
	Answers   int
	Refusals  int
	// ByType counts answered questions per OpenAI answer type.
	ByType map[string]int
}

Summary counts what a decision exchange asked and answered.

func Summarize

func Summarize(reqBody []byte, reqFormat Format, respBody []byte) Summary

Summarize counts the questions in a request body (either format) and the answers and refusals in a response body. Either body may be nil.

type SystemOneAnswer

type SystemOneAnswer struct {
	Type          string          `json:"type"`
	Noul          *float64        `json:"noul,omitempty"`
	Choice        string          `json:"choice,omitempty"`
	Score         *float64        `json:"score,omitempty"`
	Legend        json.RawMessage `json:"legend,omitempty"`
	Probabilities json.RawMessage `json:"probabilities,omitempty"`
	Confidence    *float64        `json:"confidence,omitempty"`
}

SystemOneAnswer is one System One answer. Probabilities is an option->probability object for choice, and an array or index-keyed object for score.

type SystemOneQuestion

type SystemOneQuestion struct {
	Type         string          `json:"type"`
	Instructions json.RawMessage `json:"instructions,omitempty"`
	Criteria     json.RawMessage `json:"criteria,omitempty"`
}

SystemOneQuestion is one System One question. Instructions may be a string, an object or an array; Criteria depends on Type (an option->description object for choice, an array of levels for score, optional for noul).

type SystemOneRequest

type SystemOneRequest struct {
	Model     string
	State     json.RawMessage
	Questions OrderedQuestions
}

SystemOneRequest is a System One decision request. Questions keep their insertion order through OrderedQuestions: System One models are sensitive to option order, and a Go map would sort it.

func ToSystemOne

func ToSystemOne(req *Request) (*SystemOneRequest, error)

ToSystemOne translates an OpenAI-format request into a System One request. Question order and option order are preserved.

func (SystemOneRequest) MarshalJSON

func (r SystemOneRequest) MarshalJSON() ([]byte, error)

MarshalJSON writes the request with questions as an ordered object.

func (*SystemOneRequest) UnmarshalJSON

func (r *SystemOneRequest) UnmarshalJSON(data []byte) error

UnmarshalJSON reads a System One request, keeping question order.

type SystemOneResponse

type SystemOneResponse struct {
	Model   string                     `json:"model,omitempty"`
	Answers map[string]SystemOneAnswer `json:"answers"`
	Usage   *Usage                     `json:"usage,omitempty"`
}

SystemOneResponse is a System One decision response.

type Usage

type Usage struct {
	InputTokens  int `json:"input_tokens"`
	OutputTokens int `json:"output_tokens"`
	TotalTokens  int `json:"total_tokens,omitempty"`
	// InputTokensDetails is OpenAI's prompt-cache breakdown (seen on the live
	// API: cached_tokens and cache_write_tokens, both included in
	// InputTokens). System One reports none.
	InputTokensDetails *InputTokensDetails `json:"input_tokens_details,omitempty"`
}

Usage is the token usage of a decision call. It accepts the input_tokens/output_tokens spelling (System One, OpenAI Responses-style) and the prompt_tokens/completion_tokens spelling (OpenAI chat-style).

func UsageFromBody

func UsageFromBody(body []byte) (Usage, bool)

UsageFromBody reads the usage block of a decision response in either format.

func (*Usage) UnmarshalJSON

func (u *Usage) UnmarshalJSON(data []byte) error

UnmarshalJSON accepts either usage spelling.

type ValidationError

type ValidationError struct {
	Param   string
	Message string
}

ValidationError is a client error in a decision request. Param names the offending field in the OpenAI error envelope.

func (*ValidationError) Error

func (e *ValidationError) Error() string

Jump to

Keyboard shortcuts

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