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. 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.
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 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 latency; 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
PausedUntil time.Time
Latency time.Duration
// 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
}
Health is the client's breaker state.