Documentation
¶
Overview ¶
Package state holds the process-wide adaptive router singleton and the route-event fan-out: every route decision is written to stderr as one `[router] <json>` NDJSON line and delivered to the process-local subscribers. A panicking subscriber never breaks telemetry.
Index ¶
- Variables
- func AdaptiveConfig(cfg RouterConfig) adaptive.AdaptiveRouterConfig
- func AdaptiveRouter(r Router) (*adaptive.AdaptiveModelRouter, bool)
- func EmitRouteEvent(event RouteEvent)
- func OnRouteEvent(listener RouteEventListener) func()
- func ResetListenersForTesting()
- func ResetRouterForTesting()
- func SetRouterFactory(f func(cfg RouterConfig) Router) func()
- type RouteEvent
- type RouteEventListener
- type Router
- type RouterConfig
Constants ¶
This section is empty.
Variables ¶
var Stderr io.Writer = os.Stderr
Stderr receives the NDJSON lines; it is a variable so tests can capture them. They go to stderr so they never mix with the event stream on stdout.
Functions ¶
func AdaptiveConfig ¶
func AdaptiveConfig(cfg RouterConfig) adaptive.AdaptiveRouterConfig
AdaptiveConfig coerces the opaque RouterConfig into adaptive's struct. Both a typed struct and a decoded JSON map work; anything else yields the zero config, which the router fills with its own defaults.
func AdaptiveRouter ¶
func AdaptiveRouter(r Router) (*adaptive.AdaptiveModelRouter, bool)
AdaptiveRouter narrows GetRouter()'s opaque handle. It returns false when a test has installed a different factory through SetRouterFactory, which is the only way the singleton can be anything else.
func EmitRouteEvent ¶
func EmitRouteEvent(event RouteEvent)
EmitRouteEvent writes one NDJSON line on stderr, then fans the event out. Listener panics are swallowed so they cannot break router telemetry.
func OnRouteEvent ¶
func OnRouteEvent(listener RouteEventListener) func()
OnRouteEvent registers a process-local subscriber and returns its unsubscribe.
func ResetListenersForTesting ¶
func ResetListenersForTesting()
ResetListenersForTesting drops every subscriber.
func ResetRouterForTesting ¶
func ResetRouterForTesting()
ResetRouterForTesting drops the singleton so the next GetRouter() rebuilds it.
func SetRouterFactory ¶
func SetRouterFactory(f func(cfg RouterConfig) Router) func()
SetRouterFactory replaces the router constructor. Returns a restore func.
Types ¶
type RouteEvent ¶
type RouteEvent struct {
Slot string `json:"slot"`
Tier string `json:"tier"`
Model string `json:"model"`
PreviousModel string `json:"previous_model"`
Switched bool `json:"switched"`
Reason string `json:"reason"`
Score float64 `json:"score"`
ElapsedS float64 `json:"elapsed_s"`
Attempts float64 `json:"attempts"`
Successes float64 `json:"successes"`
Failures float64 `json:"failures"`
RateLimits float64 `json:"rate_limits"`
LatencyEwma float64 `json:"latency_ewma"`
ToksecEwma float64 `json:"toksec_ewma"`
Error string `json:"error"`
}
RouteEvent is the route decision record written to the `[router] ` NDJSON line and handed to subscribers.
func ToRouteEvent ¶
func ToRouteEvent(ev adaptive.AdaptiveRouteEvent) RouteEvent
ToRouteEvent converts an adaptive event into the emitted record.
type RouteEventListener ¶
type RouteEventListener func(event RouteEvent)
RouteEventListener receives every emitted route event.
type Router ¶
type Router any
Router is the opaque router handle the singleton stores; adaptivewire.go narrows it to *adaptive.AdaptiveModelRouter.
func GetRouter ¶
func GetRouter() Router
GetRouter returns the singleton, building a default-config one for code paths that run before the CLI bootstraps the router (tests, eager imports).
func InitRouter ¶
func InitRouter(cfg RouterConfig) Router
InitRouter unconditionally replaces the singleton.
func NewAdaptiveRouter ¶
func NewAdaptiveRouter(cfg RouterConfig) Router
NewAdaptiveRouter builds the real router and wires its event hook to EmitRouteEvent unless the config already carries one.
type RouterConfig ¶
type RouterConfig any
RouterConfig is the router configuration, opaque at this boundary.
var DefaultRouterConfig RouterConfig = map[string]any{}
DefaultRouterConfig is the empty config GetRouter falls back to; the router fills in its default pools.