Documentation
¶
Overview ¶
Package classifier asks an external model which tier and activity a prompt needs. It holds the shared contract (criteria, instructions, advice shape), one adapter per wire protocol (system-one, ollama), and a client with a total deadline, two attempts, and a breaker. Configured builds the client the config selects, from the selected name, its definitions and an env lookup, and says why it is off. Input carries only the prompt and clipped dialogue: Codex headers cannot reach an adapter by construction. Ported from claude-model-router lib/classifier-*.mjs.
Index ¶
Constants ¶
const ( RetryDelay = 100 * time.Millisecond ActivityStepMin = 300 * time.Millisecond FailuresToPause = 3 PauseDuration = 60 * time.Second MaxAnswerChars = 16 * 1024 MaxAttempts = 2 )
Retry and breaker rules shared by every protocol.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ActivityAdvice ¶
ActivityAdvice is the activity answer, or nil when it was not given.
type ActivityStep ¶
type ActivityStep interface {
BuildActivity(model string, in Input) ([]byte, error)
ParseActivity(body []byte) *ActivityAdvice
}
ActivityStep is an Adapter whose protocol asks the activity in a second request. The Client sends it only with ActivityStepMin of the deadline left; ParseActivity returns nil for any answer it cannot read.
type Adapter ¶
type Adapter interface {
Build(model string, in Input) ([]byte, error)
Parse(body []byte) (Advice, error)
}
Adapter builds a request body for a protocol and parses its answer.
func Ollama ¶
func Ollama() Adapter
Ollama is Ollama's native chat API with a one-token answer: the model writes one letter per option, and the probability of each letter among the top candidates is the option's probability. It asks no continuation; the activity is a second request with the same system message and state prefix, so Ollama reuses the cached prefix.
func OpenAIDecisions ¶ added in v0.5.0
func OpenAIDecisions() Adapter
OpenAIDecisions is the OpenAI Decisions protocol (POST /v1/decisions): the state travels as JSON text in `input`, and the questions come back as named answers. The route and the activity are choices, the continuation a predicate.
type Advice ¶
type Advice struct {
Choice Choice
Probabilities map[Choice]float64
Continuation *float64
Activity *ActivityAdvice
Latency time.Duration
}
Advice is the shared answer shape every adapter returns. Continuation is nil when the protocol does not ask it.
type Asker ¶
Asker is what the router depends on; Client implements it.
func Configured ¶ added in v0.7.2
func Configured(name string, defs map[string]config.Classifier, getenv func(string) string) (Asker, string)
Configured returns the classifier that name selects among defs, ready to ask its endpoint, or nil and the reason it is off. The key and each {NAME} placeholder of the endpoint come from the env var of that name, read with getenv, else from the classifier's envFile. A value found in neither turns it off: the router still runs and pins still work. The reason never holds a value.
type Choice ¶
type Choice string
Choice is a route answer: a config.Tier or ChoiceUncertain.
const ChoiceUncertain Choice = "uncertain"
ChoiceUncertain is the route answer for a request that is unclear or not a task.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client asks one classifier endpoint. It allows one request at a time: a call waits, under its context, while another is in flight, and a request abandoned at the deadline holds the slot until it settles, so a late answer never overlaps the next turn's. now measures the budget, the breaker and the service latency, which excludes time waiting for the slot; the context carries the same timeout as a duration, so an injected clock never makes the request expire early or late.
func New ¶
func New(cfg config.Classifier, key string, adapter Adapter, doer Doer, now func() time.Time) *Client
New returns a client for cfg. key is the bearer key read from cfg.KeyEnv; it is the only credential the client ever sends.
type Health ¶
type Health struct {
Failures int `json:"Failures"`
PausedUntil time.Time `json:"PausedUntil"`
Latency time.Duration `json:"Latency"`
QueueWait time.Duration `json:"QueueWait"`
QueueTimeouts uint64 `json:"QueueTimeouts"`
// LastError is why the last call failed, as a short code such as
// "timeout" or "http 503". Never the key or a response body. Empty after
// a success.
LastError string `json:"LastError"`
}
Health is the client's breaker state. A Snapshot carries it on the control wire, so its JSON names are spelled out and pinned by internal/control's protocol-v2 fixtures.