routing

package
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2026 License: AGPL-3.0 Imports: 9 Imported by: 0 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 routing turns the model name a client asked for into the model and accounts that serve it: model mapping rules, the catalog with admin overrides, and access checks.

Index

Constants

View Source
const (
	AliasSourceLive    = "live"    // ask each account, so a line only resolves to what it can use
	AliasSourceCatalog = "catalog" // built-in and custom models only
)

Where aliases learn which models exist.

View Source
const (
	PayloadDefault  = "default"  // set the field only when the request doesn't have it
	PayloadOverride = "override" // always set the field
	PayloadRemove   = "remove"   // delete the field
)

Payload rule modes.

View Source
const (
	StrategyRoundRobin    = "round_robin"    // rotate, least recently used first
	StrategyFill          = "fill"           // always the first account until it's limited
	StrategyMostRemaining = "most_remaining" // the account with the most quota left
	StrategyRandom        = "random"
)

Account selection strategies within a provider's highest available priority tier.

View Source
const (
	FormatAnthropic  = "anthropic"
	FormatOpenAI     = "openai"
	FormatResponses  = "responses"
	FormatOpenRouter = "openrouter"
)

Client API formats, used to scope mapping rules.

Variables

View Source
var (
	ErrUnknownModel  = errors.New("unknown model")
	ErrModelDisabled = errors.New("model is disabled")
	ErrNotAllowed    = errors.New("model not allowed for this key")
)

Functions

This section is empty.

Types

type Alias added in v0.9.0

type Alias struct {
	ID      string `json:"id"`
	Enabled bool   `json:"enabled"`
	Name    string `json:"name"`
	// Match is a glob over model names, e.g. "gpt-*-sol".
	Match string `json:"match"`
	// Pin holds the alias at one model instead of the newest.
	Pin string `json:"pin"`
	// Effort applies when the request doesn't set one.
	Effort string `json:"effort"`
}

type AliasCandidate added in v0.9.0

type AliasCandidate struct {
	ID       string `json:"id"`
	Name     string `json:"name"`
	Provider string `json:"provider"`
	// Accounts is how many accounts list it; -1 when that isn't known.
	Accounts int  `json:"accounts"`
	Usable   bool `json:"usable"`
}

AliasCandidate is one model an alias could mean, for the dashboard.

type AliasSettings added in v0.9.0

type AliasSettings struct {
	Enabled bool   `json:"enabled"`
	Source  string `json:"source"`
	// RefreshMinutes is how often each account's model list is fetched.
	RefreshMinutes int `json:"refreshMinutes"`
	// Fallback uses the next-newest model when nothing can serve the newest right now.
	Fallback bool `json:"fallback"`
	// Preview counts models upstream lists but doesn't generally offer yet.
	Preview bool `json:"preview"`
	// List shows aliases in /v1/models.
	List  bool    `json:"list"`
	Lines []Alias `json:"lines"`
}

AliasSettings let a client ask for a model line ("sol") and get its newest model.

func DefaultAliases added in v0.9.0

func DefaultAliases() AliasSettings

func (AliasSettings) Names added in v0.9.0

func (a AliasSettings) Names() []string

Names lists the aliases clients can currently use.

func (AliasSettings) RefreshInterval added in v0.9.0

func (a AliasSettings) RefreshInterval() time.Duration

RefreshInterval is how often accounts should report their models; 0 when nothing uses them.

type AliasStatus added in v0.9.0

type AliasStatus struct {
	ID         string           `json:"id"`
	Name       string           `json:"name"`
	Model      *models.Model    `json:"model,omitempty"`
	Error      string           `json:"error,omitempty"`
	Candidates []AliasCandidate `json:"candidates"`
}

AliasStatus says what an alias means right now, ignoring mapping rules and key limits.

type CustomModel

type CustomModel struct {
	ID            string `json:"id"`
	Provider      string `json:"provider"`
	Name          string `json:"name"`
	ContextLength int    `json:"contextLength"`
}

type ImageSettings

type ImageSettings struct {
	Enabled bool `json:"enabled"`
	// Model is the Responses model that runs the tool; requests are routed and checked
	// against key allow-lists as this model.
	Model string `json:"model"`
	// ToolModel is the image model used when the request doesn't name a gpt-image one.
	ToolModel string `json:"toolModel"`
	// AutoTool adds the image_generation tool to Responses API requests sent to Codex.
	AutoTool bool `json:"autoTool"`
}

ImageSettings control /v1/images, served by Codex's image_generation tool.

type KeyRules

type KeyRules struct {
	Rules        []Rule `json:"rules"`
	IgnoreGlobal bool   `json:"ignoreGlobal"`
	// IgnoreAliases makes names like "sol" unknown for this key.
	IgnoreAliases bool `json:"ignoreAliases"`
}

KeyRules are a key's own mapping rules, checked before the global ones.

type ListedAlias added in v0.9.0

type ListedAlias struct {
	Model  models.Model // named after the alias
	Target models.Model // what it stands for today
}

ListedAlias is an alias a client can use right now, dressed up as a model.

type ModelOverride

type ModelOverride struct {
	Disabled bool   `json:"disabled"`
	Name     string `json:"name"`
	// Accounts limits which accounts may serve the model; empty means any.
	Accounts []int64 `json:"accounts"`
	Strategy string  `json:"strategy"`
	// Effort applies when the request doesn't set one.
	Effort string `json:"effort"`
}

type PayloadRule

type PayloadRule struct {
	ID      string `json:"id"`
	Enabled bool   `json:"enabled"`
	// Match is a model glob (bare or lab-prefixed); empty matches every model.
	Match    string          `json:"match"`
	Provider string          `json:"provider"` // "" any, "claude", "codex"
	Formats  []string        `json:"formats"`  // client API formats; empty = all
	Mode     string          `json:"mode"`
	Path     string          `json:"path"` // dot path, e.g. "service_tier" or "reasoning.effort"
	Value    json.RawMessage `json:"value"`
	Note     string          `json:"note"`
}

PayloadRule edits the final upstream request body, after all translation.

type Request

type Request struct {
	Model    string
	Format   string
	KeyRules KeyRules
	Allowed  []string
	// Providers optionally restricts which providers may serve it (OpenRouter's provider.only).
	Providers []string
	// Inventory reports the models accounts can use, for aliases. Nil means only the catalog is known.
	Inventory func() models.Inventory
	// Usable reports whether any of the accounts (or, when empty, any of the provider's) could
	// take a request for the model right now. Nil means assume so.
	Usable func(provider, model string, accounts []int64) bool
}

Request is what the resolver needs to know about an incoming call.

type Route

type Route struct {
	Requested string       `json:"requested"`
	Mapped    string       `json:"mapped,omitempty"` // set when a rule rewrote the name
	Rule      string       `json:"rule,omitempty"`   // "key" or "global"
	Alias     string       `json:"alias,omitempty"`  // set when the name was a line like "sol"
	Model     models.Model `json:"model"`
	Effort    string       `json:"effort,omitempty"`
	// DefaultEffort applies only when the request itself doesn't ask for a reasoning level.
	DefaultEffort string  `json:"defaultEffort,omitempty"`
	Accounts      []int64 `json:"accounts,omitempty"`
	Strategy      string  `json:"strategy"`
	Catalog       bool    `json:"inCatalog"`
}

type Rule

type Rule struct {
	ID      string `json:"id"`
	Enabled bool   `json:"enabled"`
	// Match is a case-insensitive glob over the requested name, with or without a lab prefix.
	Match  string `json:"match"`
	Target string `json:"target"`
	// Effort overrides the reasoning effort; empty keeps the requested one.
	Effort string `json:"effort"`
	// Formats limits the rule to some client API formats; empty means all.
	Formats []string `json:"formats"`
	Note    string   `json:"note"`
}

Rule rewrites a requested model name before resolution.

type Settings

type Settings struct {
	// Strategy is the default account selection; ProviderStrategy overrides it per provider.
	Strategy         string            `json:"strategy"`
	ProviderStrategy map[string]string `json:"providerStrategy"`
	// Affinity keeps related requests on the same account for StickyMinutes after their last
	// use, which keeps upstream prompt caches warm and thinking signatures valid:
	// "session" (per conversation), "key" (per API key) or "off". The binding is dropped if the
	// account becomes unavailable.
	Affinity string `json:"affinity"`
	// Sticky is the pre-affinity setting, kept so older saved settings still load.
	Sticky        bool `json:"sticky,omitempty"`
	StickyMinutes int  `json:"stickyMinutes"`

	// ModelList picks names in /v1/models: "bare" (claude-opus-5), "canonical"
	// (anthropic/claude-opus-5) or "both". OpenRouter-style paths always list canonical names.
	ModelList string `json:"modelList"`
	// UnknownModels decides whether names missing from the catalog are passed through to the
	// provider their family suggests ("claude-…" → Claude).
	UnknownModels bool `json:"unknownModels"`
	// ReasoningField is where Chat Completions responses put reasoning text:
	// "reasoning_content", "reasoning" (OpenRouter style) or "both".
	ReasoningField string `json:"reasoningField"`

	Models   map[string]ModelOverride `json:"models"` // keyed by canonical name
	Custom   []CustomModel            `json:"custom"`
	Mappings []Rule                   `json:"mappings"`
	Aliases  AliasSettings            `json:"aliases"`
	// PayloadRules edit upstream request bodies after translation.
	PayloadRules []PayloadRule `json:"payloadRules"`
	Images       ImageSettings `json:"images"`
}

func DefaultSettings

func DefaultSettings() Settings

func (Settings) AliasStatuses added in v0.9.0

func (s Settings) AliasStatuses(req Request) []AliasStatus

func (Settings) ApplyPayloadRules

func (s Settings) ApplyPayloadRules(body []byte, m models.Model, format string) ([]byte, bool)

ApplyPayloadRules runs every matching rule over an upstream body and reports whether it changed.

func (Settings) Catalog

func (s Settings) Catalog() []models.Model

Catalog is every model: built-ins, then custom ones, with display-name overrides applied.

func (Settings) ListedAliases added in v0.9.0

func (s Settings) ListedAliases(req Request) []ListedAlias

ListedAliases are the aliases to show in a model list, as seen by this request's key.

func (*Settings) Normalize

func (s *Settings) Normalize()

func (Settings) Resolve

func (s Settings) Resolve(req Request) (*Route, error)

Resolve maps a requested model to the model, provider and accounts that should serve it.

func (Settings) StrategyFor

func (s Settings) StrategyFor(m models.Model) string

StrategyFor returns the account strategy for a model: model override, provider override, default.

Jump to

Keyboard shortcuts

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