Documentation
¶
Overview ¶
Package routerconfig is the inference-router plugin's configuration: its shape, its validation, and the host rules the plugin routes by.
It is a package of its own, importing only the standard library, so that agentop can check a config before writing it without linking the plugin. Linking the plugin would pull the plugin registry, and SPIFFE and gRPC with it, into a terminal UI. One copy of these rules is what keeps `agentop server` from writing a config the proxy then refuses on reload.
Index ¶
- Constants
- Variables
- func CheckAPI(api map[string]string) error
- func CheckAgent(name string) error
- func CheckAuthHeader(name string) error
- func CheckHeaders(h map[string]string, authHeader string) error
- func CheckKey(key string) error
- func CheckName(name string) error
- func CheckSessionID(id string) error
- func CheckWord(word string) error
- func FetchModels(ctx context.Context, client *http.Client, ep Endpoint, a Auth) (ids []string, header string, err error)
- func HeaderOf(m map[string]string) http.Header
- func Hostname(hostport string) string
- func Key(hostport string) string
- func NewModelsClient() *http.Client
- func Resolve(word string, ids []string) string
- func SetAuth(h http.Header, name, key string)
- func SyntheticSessionID(id string) bool
- type Auth
- type Config
- type Endpoint
- type Server
Constants ¶
const NoAgent = "unknown"
NoAgent is the agent name of a request that carried no User-Agent. It is pipeline.UnknownClientLabel, copied rather than imported to keep this package free of the pipeline; a test in the plugin package holds the two equal.
It cannot be routed: requests under it come from no one agent, so routing it would send every unlabelled request — a health probe, a curl — to a server.
Variables ¶
var APIProtocols = []string{"anthropic-messages", "chat-completions", "responses"}
APIProtocols are the protocol names an api key can have. It is translate.Names(), copied rather than imported to keep this package free of core/translate; a test in the plugin package holds the two equal.
Functions ¶
func CheckAPI ¶
CheckAPI reports whether api maps known protocols to paths a request can be sent to. Keys are checked in sorted order, so the same map always gives the same first error. Each error starts with "api". The path rule is pipeline.Context.SetRedirectPath's.
func CheckAgent ¶
CheckAgent reports whether name can be routed. Agent names are the ones pipeline.AgentName produces and agentop shows, such as claude-code and opencode.
func CheckAuthHeader ¶
CheckAuthHeader reports whether name can carry a server's key. "" is valid: it means the agent's header, or the target protocol's usual one.
func CheckHeaders ¶
CheckHeaders reports whether h can be set on every request to a server whose auth_header is authHeader. Names are checked in sorted order for a stable first error. The errors never repeat a value.
func CheckKey ¶
CheckKey reports whether key can be sent as a header value. The error never repeats the key.
func CheckSessionID ¶
CheckSessionID reports whether id can name a session in the sessions map. The ids the session API serves are UUIDs, but an id need only be printable, non-empty and free of whitespace to be one worth routing: refusing anything stricter would block a future agent's id shape. Synthetic ids are refused here, not later.
func CheckWord ¶
CheckWord reports whether word can name a server's main or helper model. A word is matched against a model's name with everything up to the last "/" dropped, so one containing a "/" could never match; whitespace never appears in a model's name.
func FetchModels ¶
func FetchModels(ctx context.Context, client *http.Client, ep Endpoint, a Auth) (ids []string, header string, err error)
FetchModels is the model list a server serves to a.Key, and the auth header that worked. Its errors quote no part of the URL or the key.
It asks GET /v1/models, and on 403 or 404 GET /v1/model/info (LiteLLM's list, read from data[].model_name), with a.Header or authorization. When a.Header is "" and each answer was 401 or 403, it asks the two again with x-api-key. The first 200 wins.
The list is a lower bound. A LiteLLM server also serves names it does not list, aliases of listed models, so it is good for resolving a main or helper word and for showing what a server offers, and not for judging whether a name the agent asked for is served. The router lets the server's own answer judge that.
func HeaderOf ¶
HeaderOf is m as an http.Header, each pair through Header.Set, and nil for an empty m.
func Key ¶
Key is what a request's host[:port] is matched to a server by: the host, lowercased, and on a loopback host its port too, since one machine serves many things there — a model server on one port, an MCP or dev server on another. A port a scheme defaults, 80 or 443, counts as none, as a server URL drops it.
func NewModelsClient ¶
NewModelsClient is the client FetchModels is meant to be given: the system's roots, as the TLS bridge's upstream client trusts, and no proxy, so a proxy's own fetch never goes back through itself.
func Resolve ¶
Resolve is the model word names among ids, a server's model list, and "" when it names none. Of the ids whose name — everything up to the last "/" dropped — contains word, case ignored, it is the one with the highest version, and of those the shortest name, so a model listed under several spellings resolves the same way every time.
A version is the numbers in the name, read left to right, a trailing date left out: claude-opus-5-5 beats claude-opus-5, which beats claude-opus-4-8. Only numbers standing alone count, so a size such as the 550B in NVIDIA-Nemotron-3-Ultra-550B is not taken for one.
The newest is what a server's main and helper words resolve to, so that a server adding a newer model needs no edit to be used. The router and agentop both resolve with this, so what agentop shows is what the router sends.
func SetAuth ¶
SetAuth puts key on h in the header name: "" or authorization (case ignored) gives Authorization: Bearer <key>, any other name gives <name>: <key>.
func SyntheticSessionID ¶
SyntheticSessionID reports whether id is one of the session store's synthetic buckets — "default" and "pending:<agent>" — which hold many conversations, not one a move could target.
Types ¶
type Auth ¶
type Auth struct {
// Key is the server's key.
Key string
// Header is the server's auth_header. With "", FetchModels tries
// authorization, then x-api-key.
Header string
// Headers are the server's headers, sent on each request.
Headers http.Header
}
Auth is how FetchModels sends a server its key.
type Config ¶
type Config struct {
Servers map[string]Server `json:"servers" required:"true" description:"Inference servers, by name."`
Agents map[string]string `json:"agents" description:"Agents to route, by agent name, to a server name. Unlisted agents are not routed."`
// Sessions moves one running session to a server, by the session's id, beating
// the pin that session already holds. "" stops routing that session. agentop
// writes these; see CheckSessionID for what an id may look like.
Sessions map[string]string `` /* 135-byte string literal not displayed */
// KeyRoutes sends a request that carries the key of one server to another
// server. The router does not follow a second entry.
KeyRoutes map[string]string `` /* 162-byte string literal not displayed */
}
Config is the plugin's config: block.
func Decode ¶
func Decode(raw json.RawMessage) (Config, error)
Decode reads raw, the entry's config: block as JSON, refusing unknown fields, and validates it. Unknown fields are refused because the likeliest one is a guess at a feature this design leaves out on purpose — a `default:` server — and a silently ignored guess would route nothing while looking configured.
type Endpoint ¶
type Endpoint struct {
// Scheme is "http" or "https".
Scheme string
// Host is the lowercased host, with the port only when it is not the scheme's
// default. It is the redirect target's host.
Host string
// Hostname is the lowercased host without any port.
Hostname string
// Path is the URL's path prefix with no trailing "/", or "" for none.
Path string
}
Endpoint is a server URL reduced to what routing compares.
func ParseURL ¶
ParseURL validates a server URL and returns its Endpoint. It accepts what pipeline.Context.Redirect accepts — http or https, a host, no user info, no query or fragment — and a path prefix of plain segments, with one trailing "/" dropped. It also checks two things more: a port must be a number from 1 to 65535, and the scheme's default port is dropped, so a server at https://x:443 is the host a request for x names, and the redirect a routed request for x still gets records no requested host.
Its errors are fixed text that names the problem and quotes no part of raw, not even redacted. They reach the logs and the unauthenticated /reload/status, and a key pasted into a mistyped URL can land anywhere in it: user info, query, fragment, a path segment, or the path or port url.Parse files it under when the "//" is wrong. Redacting shape by shape kept missing one.
func (Endpoint) PlaintextRemote ¶
PlaintextRemote reports an http endpoint on another machine. A request routed there leaves the proxy decrypted, with its key and its prompt, across the network.
type Server ¶
type Server struct {
URL string `` /* 151-byte string literal not displayed */
Key string `json:"key" required:"true" description:"API key sent to this server in place of the client's."`
Main string `` /* 231-byte string literal not displayed */
Helper string `` /* 152-byte string literal not displayed */
API map[string]string `` /* 167-byte string literal not displayed */
AuthHeader string `` /* 213-byte string literal not displayed */
Headers map[string]string `` /* 128-byte string literal not displayed */
}
Server is one inference server.