routerconfig

package
v0.0.0-...-53bd38a Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2026 License: Apache-2.0 Imports: 18 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 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

View Source
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

View Source
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

func CheckAPI(api map[string]string) error

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

func CheckAgent(name string) error

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

func CheckAuthHeader(name string) error

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

func CheckHeaders(h map[string]string, authHeader string) error

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

func CheckKey(key string) error

CheckKey reports whether key can be sent as a header value. The error never repeats the key.

func CheckName

func CheckName(name string) error

CheckName reports whether name can name a server.

func CheckSessionID

func CheckSessionID(id string) error

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

func CheckWord(word string) error

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

func HeaderOf(m map[string]string) http.Header

HeaderOf is m as an http.Header, each pair through Header.Set, and nil for an empty m.

func Hostname

func Hostname(hostport string) string

Hostname is a request's host[:port], lowercased and without the port.

func Key

func Key(hostport string) string

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

func NewModelsClient() *http.Client

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

func Resolve(word string, ids []string) string

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

func SetAuth(h http.Header, name, key string)

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

func SyntheticSessionID(id string) bool

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.

func (Config) Validate

func (c Config) Validate() error

Validate checks every rule the plugin routes by. Each map is checked in name order, so the same config always reports the same first error.

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

func ParseURL(raw string) (Endpoint, error)

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) Key

func (e Endpoint) Key() string

Key is what a request is matched to this endpoint by: Key of its Host.

func (Endpoint) PlaintextRemote

func (e Endpoint) PlaintextRemote() bool

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.

func (Endpoint) URL

func (e Endpoint) URL() string

URL is the endpoint as scheme://host[/path], the form agentop writes.

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.

Jump to

Keyboard shortcuts

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