diff --git a/README.md b/README.md index 1680b8c..1fb3ddf 100644 --- a/README.md +++ b/README.md @@ -1606,7 +1606,7 @@ The gateway picks the highest tier below the first attempted tier, and goes lowe - **Estimate**: the prompt estimate counts one token per four bytes of these inputs: message text, the system prompt, the `tools` schema, OpenAI `tool_calls` arguments, and Anthropic `tool_use` input. Each image adds 1000 tokens. Very large structured payloads count as a lower bound. - **Learned ratio**: after 50 served requests with provider-reported usage, the gateway learns bytes per token for each upstream model. The ratio stays between 1.5 and 6.0 and lives in memory on each instance. - **No fit**: when the check would skip every target, the gateway uses the model with the largest known window instead of returning `404`. -- **Vision**: a request with an image skips models whose resolved vision flag is `false`: from `modelMeta`, else from discovery. +- **Vision**: a request with an image skips models whose resolved vision flag is `false`: from `modelMeta`, else from discovery. A target that is a model alias uses the alias's own `modelMeta` entry first, as `GET /v1/models` does. This also applies to its `contextTokens`. - **Advertised window**: `GET /v1/models` lists a router when the group may use its name and at least one target. With `contextEscalation` on, the entry advertises the largest known window of all targets in `context_window`, `context_length`, `max_model_len`, and `max_context_length`. With `contextEscalation: false`, it advertises the smallest known window. Targets with an unknown window do not count. When no target has a known window, the fields are omitted. - **Override**: a `modelMeta` entry keyed by the router name replaces the value. It needs `contextTokens` above zero. The gateway logs a one-time warning when the override exceeds the largest known target window. diff --git a/registry.go b/registry.go index 084cd9a..ee3bdfd 100644 --- a/registry.go +++ b/registry.go @@ -2489,6 +2489,15 @@ func (m *modelRegistry) resolveMetaForAliasName(alias string) resolvedModelMeta return m.resolveMetaForAlias(alias, providerName, bareTarget) } +// candidateMeta resolves a router candidate's metadata as /v1/models shows +// the target: through the alias when target is one. +func (m *modelRegistry) candidateMeta(target, providerName, model string) resolvedModelMeta { + if _, isAlias := m.aliases[target]; isAlias { + return m.resolveMetaForAlias(target, providerName, model) + } + return m.resolveMetaFor(providerName, model) +} + // resolveMetaForAlias resolves alias's metadata (feature v0.23) via // resolveAliasModelMeta: target-inherited unless the alias itself has // its own modelMeta entry — see that function's own doc comment. diff --git a/routes_unified_router_test.go b/routes_unified_router_test.go index 8e08246..45bfc60 100644 --- a/routes_unified_router_test.go +++ b/routes_unified_router_test.go @@ -245,6 +245,28 @@ func TestRouter_TargetSkipping(t *testing.T) { }, content: "hi", wantStatus: 200, wantModel: "alpha/a1", wantA1Hit: 1, }, + { + name: "alias vision false is skipped for image requests", + mut: func(c *Config) { + c.ModelAliases = map[string]string{"a1-text": "alpha/a1"} + c.ModelMeta = map[string]*ModelMetaConfig{"a1-text": {Vision: &no}} + twoTiers(c, "a1-text", "b1") + }, + content: []any{ + map[string]any{"type": "text", "text": "hi"}, + map[string]any{"type": "image_url", "image_url": map[string]any{"url": "data:image/png;base64,AAAA"}}, + }, + wantStatus: 200, wantModel: "beta/b1", + }, + { + name: "alias small context window skipped when escalation on", + mut: func(c *Config) { + c.ModelAliases = map[string]string{"a1-small": "alpha/a1"} + c.ModelMeta = map[string]*ModelMetaConfig{"a1-small": {ContextTokens: 50}} + twoTiers(c, "a1-small", "b1") + }, + content: longPrompt, wantStatus: 200, wantModel: "beta/b1", + }, { name: "small context window skipped when escalation on", mut: func(c *Config) { diff --git a/smart_router_plan.go b/smart_router_plan.go index 3178905..b6d73fa 100644 --- a/smart_router_plan.go +++ b/smart_router_plan.go @@ -327,7 +327,7 @@ func (g *Gateway) routerTargetCandidates(rt *smartRouter, tier, target string, f ordered := g.orderedFailoverCandidates(primary, extra) usable := make([]resolveCandidate, 0, len(ordered)) for _, cand := range ordered { - if g.routerCandidateSkipped(cand, tier, rt, facts, skips) { + if g.routerCandidateSkipped(cand, target, tier, rt, facts, skips) { continue } usable = append(usable, cand) @@ -336,8 +336,9 @@ func (g *Gateway) routerTargetCandidates(rt *smartRouter, tier, target string, f return capFailoverCandidates(usable, g.failover.maxAttempts) } -func (g *Gateway) routerCandidateSkipped(cand resolveCandidate, tier string, rt *smartRouter, facts routerRequestFacts, skips *routerSkips) bool { - if facts.hasImage && !visionCapable(g.registry.resolveMetaFor(cand.providerName, cand.upstreamModel).Vision) { +func (g *Gateway) routerCandidateSkipped(cand resolveCandidate, target, tier string, rt *smartRouter, facts routerRequestFacts, skips *routerSkips) bool { + meta := g.registry.candidateMeta(target, cand.providerName, cand.upstreamModel) + if facts.hasImage && !visionCapable(meta.Vision) { skips.vision++ return true } @@ -347,7 +348,6 @@ func (g *Gateway) routerCandidateSkipped(cand resolveCandidate, tier string, rt } // Checked last so the fallback candidate already passed vision and cost. if rt.contextEscalation { - meta := g.registry.resolveMetaFor(cand.providerName, cand.upstreamModel) if meta.ContextKnown && !contextFits(facts, g.tokenCalib.promptTokens(cand.providerName, cand.upstreamModel, facts.promptBytes), int64(meta.ContextTokens)) { skips.context++ if skips.largest == nil || meta.ContextTokens > skips.largestCtx {