Documentation
¶
Overview ¶
Package config holds the router configuration: tiers, activities, routes, policy, classifiers, and the JSON file under the XDG config dir they load from. It is a leaf package: nothing in internal/ is imported here.
Index ¶
- Constants
- Variables
- func EndpointPlaceholders(endpoint string) ([]string, error)
- func ExpandHome(path, home string) string
- func Path() (string, error)
- func ProjectModes(cwd func(threadID string) string) func(threadID string) (RoutingMode, error)
- func ReadEnvKey(path, name string) (string, error)
- type Activity
- type Cache
- type Classifier
- type Classifiers
- type Config
- type Context
- type Effort
- type ModeConfig
- type Modes
- type Policy
- type PolicyPatch
- type Price
- type Proxy
- type Route
- type RoutingMode
- type Tier
Constants ¶
const ( DirName = "codex-router" FileName = "config.json" )
File locations under the XDG config dir.
const ( APISystemOne = "system-one" APIOpenAIDecisions = "openai-decisions" APIOllama = "ollama" )
Classifier wire adapters.
const ClassifierOff = "off"
ClassifierOff as Config.Classifier turns the classifier off: no prompt leaves the machine, and only pins and the context guard change the model.
const ProjectFile = ".codex/router.json"
ProjectFile is a project's routing mode file, relative to a directory between a session's cwd and its repository root.
Variables ¶
var ( ErrEnvFileMode = errors.New("open to group or others; chmod 600 it") ErrKeyNotInEnv = errors.New("key not in file") )
Env file errors. Their text never holds a value from the file.
var Activities = []Activity{ ActivityCode, ActivityDebug, ActivityExplore, ActivityPlan, ActivityReview, ActivityOps, ActivityDocs, }
Activities lists the seven routable activities.
var Efforts = []Effort{EffortLow, EffortMedium, EffortHigh, EffortXHigh}
Efforts lists efforts weakest first.
var RoutingModes = []RoutingMode{ModeCost, ModeBalanced, ModeQuality}
RoutingModes lists the modes, cheapest first.
var Tiers = []Tier{TierMicro, TierLow, TierMedium, TierHigh}
Tiers lists every tier by rank.
Functions ¶
func EndpointPlaceholders ¶ added in v0.5.0
EndpointPlaceholders returns the NAMEs of the {NAME} placeholders in endpoint, in order of first use. A brace that is not part of a well-formed placeholder is an error.
func ExpandHome ¶ added in v0.3.0
ExpandHome turns a leading "~/" into home. Other paths are returned as is.
func Path ¶
Path returns the config file location: $XDG_CONFIG_HOME/codex-router/config.json, else ~/.config/codex-router/config.json. A relative XDG_CONFIG_HOME is ignored per the XDG spec.
func ProjectModes ¶ added in v0.9.0
func ProjectModes(cwd func(threadID string) string) func(threadID string) (RoutingMode, error)
ProjectModes is ProjectMode per thread, from each thread's cwd; nil when cwd is nil.
func ReadEnvKey ¶ added in v0.3.0
ReadEnvKey returns the value of name in the dotenv file at path: lines KEY=value or export KEY=value, values optionally in single or double quotes (no escapes), # comment lines, and a " #" comment after an unquoted value. The last assignment wins; an empty value counts as missing. Nothing is exported to the process env. A file that group or others can read is refused, as a shared key file is a leaked key.
Types ¶
type Activity ¶
type Activity string
Activity names what a turn produces. ActivityUncertain is a classifier answer, never a config key.
type Classifier ¶
type Classifier struct {
Label string `json:"label"`
API string `json:"api"`
Endpoint string `json:"endpoint"`
Model string `json:"model"`
KeyEnv string `json:"keyEnv,omitempty"`
EnvFile string `json:"envFile,omitempty"`
TimeoutMs int `json:"timeoutMs"`
}
Classifier describes one classifier endpoint. API names the wire adapter ("system-one", "openai-decisions" or "ollama"). KeyEnv names the environment variable holding the bearer key; empty means no key. Endpoint may hold {NAME} placeholders, filled from the variable NAME. EnvFile is a dotenv file (absolute or ~/ path) read for KeyEnv and each placeholder when the process env does not set it: service units do not see the shell's env.
type Classifiers ¶ added in v0.3.2
type Classifiers map[string]Classifier
Classifiers maps a name to a classifier. A config file's entry is decoded over the entry of the same name, so it names only the fields it changes.
func (*Classifiers) UnmarshalJSON ¶ added in v0.3.2
func (cs *Classifiers) UnmarshalJSON(b []byte) error
UnmarshalJSON decodes each entry over the one already in cs. Unknown fields are errors, as in the rest of the file.
type Config ¶
type Config struct {
Baseline Tier `json:"baseline"`
Routes map[Tier]Route `json:"routes"`
Activities map[Activity]map[Tier]Route `json:"activities,omitempty"`
Prices map[string]Price `json:"prices,omitempty"`
Policy Policy `json:"policy"`
// Mode is the profile's routing mode; empty means not set (balanced).
Mode RoutingMode `json:"mode,omitempty"`
Modes Modes `json:"modes"`
Classifier string `json:"classifier"`
Classifiers Classifiers `json:"classifiers"`
Context Context `json:"context"`
Cache Cache `json:"cache"`
Proxy Proxy `json:"proxy"`
CodexBin string `json:"codexBin,omitempty"`
}
Config is the whole router configuration.
func Default ¶
func Default() Config
Default returns the built-in configuration, classifier Jev with Clef, OpenAI and a local Ollama option. Routes use two models of one compatibility group (plan D2): gpt-6-luna for micro and low, gpt-6.1-sol for medium and high. Codex caches per model, not per effort, so only a move between the two models costs cache. Tuned on live routing tests and benchmarks on 2026-10-15/16: Luna at high effort matched Sol on bounded review, debug, ops, code, explore and plan tasks at 5-10% of the list cost; xhigh on either model cost more and planned no better than high.
func Load ¶
Load reads the config file over Default(): keys the file omits keep their defaults, a map entry the file names replaces the default entry whole, and a classifiers entry changes only the fields it names. A missing file yields the defaults. Unknown keys and invalid values are errors.
func (Config) DecideTimeout ¶
DecideTimeout bounds one routing decision: the selected classifier's timeout plus a margin, so the proxy outlasts the classifier call.
func (Config) Effective ¶
func (c Config) Effective(mode RoutingMode) Policy
Effective returns Policy with mode's patch applied: a field the patch sets replaces the configured value. An empty mode is balanced.
type ModeConfig ¶ added in v0.9.0
type ModeConfig struct {
Policy PolicyPatch `json:"policy"`
}
ModeConfig is one mode's settings.
type Modes ¶ added in v0.9.0
type Modes map[RoutingMode]ModeConfig
Modes maps a mode to its settings. A config file's entry is decoded over the built-in entry of the same mode, so it names only the fields it changes.
func (*Modes) UnmarshalJSON ¶ added in v0.9.0
UnmarshalJSON decodes each entry over the one already in ms, so a file changes only the patch fields it names; null clears a built-in field. Unknown fields are errors.
type Policy ¶
type Policy struct {
UpgradeVotes int `json:"upgradeVotes"`
UpgradeBase float64 `json:"upgradeBase"`
UpgradeSlope float64 `json:"upgradeSlope"`
UpgradePivotTokens int `json:"upgradePivotTokens"`
JumpConfidence float64 `json:"jumpConfidence"`
DowngradeVotes int `json:"downgradeVotes"`
DowngradeMass float64 `json:"downgradeMass"`
DowngradeSlope float64 `json:"downgradeSlope"`
DowngradePivotTokens int `json:"downgradePivotTokens"`
ContinuationMass float64 `json:"continuationMass"`
EscalationHoldTurns int `json:"escalationHoldTurns"`
ReviewEveryRequests int `json:"reviewEveryRequests"`
MinDwellRequests int `json:"minDwellRequests"`
ActivityMass float64 `json:"activityMass"`
ContextFill float64 `json:"contextFill"`
// Deprecated: accepted for existing configs; has no effect.
DowngradeHorizonTurns int `json:"downgradeHorizonTurns,omitempty"`
}
Policy holds the switching thresholds. Taxes are in uncached tokens.
type PolicyPatch ¶ added in v0.9.0
type PolicyPatch struct {
UpgradeVotes *int `json:"upgradeVotes,omitempty"`
UpgradeBase *float64 `json:"upgradeBase,omitempty"`
UpgradeSlope *float64 `json:"upgradeSlope,omitempty"`
UpgradePivotTokens *int `json:"upgradePivotTokens,omitempty"`
JumpConfidence *float64 `json:"jumpConfidence,omitempty"`
DowngradeVotes *int `json:"downgradeVotes,omitempty"`
DowngradeMass *float64 `json:"downgradeMass,omitempty"`
DowngradeSlope *float64 `json:"downgradeSlope,omitempty"`
DowngradePivotTokens *int `json:"downgradePivotTokens,omitempty"`
ContinuationMass *float64 `json:"continuationMass,omitempty"`
EscalationHoldTurns *int `json:"escalationHoldTurns,omitempty"`
ReviewEveryRequests *int `json:"reviewEveryRequests,omitempty"`
MinDwellRequests *int `json:"minDwellRequests,omitempty"`
ActivityMass *float64 `json:"activityMass,omitempty"`
ContextFill *float64 `json:"contextFill,omitempty"`
}
PolicyPatch holds the Policy fields a mode replaces; a nil field keeps the configured value.
type Price ¶
type Price struct {
Input float64 `json:"input"`
CachedInput float64 `json:"cachedInput"`
Output float64 `json:"output"`
}
Price is a model's list price in USD per million tokens. Zero values mean unpriced: estimates then stay in tokens.
type Proxy ¶
type Proxy struct {
Port int `json:"port"`
}
Proxy holds listener settings. Port 0 binds an ephemeral port per session.
type Route ¶
Route is a model slug plus the effort the router sends for it. An empty Effort keeps the effort Codex sent.
type RoutingMode ¶ added in v0.9.0
type RoutingMode string
RoutingMode trades cost against answer quality: it moves the route a tier aims at and patches the policy. It is not route.Mode (on, paused, manual).
const ( ModeCost RoutingMode = "cost" ModeBalanced RoutingMode = "balanced" ModeQuality RoutingMode = "quality" )
func ProjectMode ¶ added in v0.9.0
func ProjectMode(cwd string) (RoutingMode, string, error)
ProjectMode returns the mode set by the project file nearest to cwd, and that file's path. The search runs from cwd up to the nearest directory holding .git, inclusive; outside a repository only cwd is searched. No file gives no mode, no path and no error. A file that does not parse, holds a key other than mode, or names an unknown mode is an error, with its path.