From 04ce09cc9c54de5e75035dbe3be17d462e93b636 Mon Sep 17 00:00:00 2001 From: sk-dev-ai Date: Fri, 4 Sep 2026 10:52:11 +0530 Subject: [PATCH 1/3] feat: expose per-model metadata (context window, reasoning, tool call, modalities) in /v1/models fetchProtocolCapabilities already parses the full opencode catalog but discards everything except protocol. Capture limit/reasoning/tool_call/ structured_output/modalities per model and expose them in /v1/models so auto-discovering clients (e.g. jcode, which defaults every model to 200k when context_window is absent) get correct context and feature flags. Purely additive: routing/decision logic untouched, legacy response shape preserved, cache schema bumped to v2 (old v1 caches load fine). --- gateway.go | 4 +-- models.go | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 83 insertions(+), 7 deletions(-) diff --git a/gateway.go b/gateway.go index 1b836b6..6a4f8cf 100644 --- a/gateway.go +++ b/gateway.go @@ -214,7 +214,7 @@ func (g *Gateway) handleModels(w http.ResponseWriter, _ *http.Request) { if _, err := g.catalog.Route(model, len(g.cfg.ZenKeys) > 0, len(g.cfg.GoKeys) > 0, g.cfg.Anonymous); err != nil { continue } - data = append(data, map[string]any{"id": model, "object": "model", "created": now, "owned_by": "opencode"}) + data = append(data, map[string]any{"id": model, "object": "model", "created": now, "owned_by": "opencode", "metadata": g.catalog.Metadata(model)}) } writeJSON(w, http.StatusOK, map[string]any{"object": "list", "data": data}) } @@ -827,7 +827,7 @@ func (g *Gateway) StartModelRefresh(ctx context.Context) { g.logger.Warn("OpenCode capability catalog refresh failed", "component", "models", "event", "capability_refresh_failed", "error", capabilitiesErr) } if zen != nil || goModels != nil { - g.catalog.ReplaceWithCapabilities(zen, goModels, capabilities.Protocols, capabilities.Unsupported) + g.catalog.ReplaceWithCapabilities(zen, goModels, capabilities.Protocols, capabilities.Unsupported, capabilities.Metadata) if ctx.Err() == nil { if err := g.catalog.SaveCache(); err != nil { g.logger.Warn("model catalog cache write failed", "component", "models", "event", "catalog_cache_write_failed", "error", err) diff --git a/models.go b/models.go index 2eb536a..9ea2001 100644 --- a/models.go +++ b/models.go @@ -88,6 +88,7 @@ type modelCatalog struct { // catalog. protocols remains the user-configured override map. nativeProtocols map[Tier]map[string]Protocol unsupported map[Tier]map[string]bool + modelMeta map[string]ModelMetadata updatedAt time.Time prefer Tier metadata *modelMetadataStore @@ -107,7 +108,7 @@ type modelCatalogSnapshot struct { Stale bool `json:"stale"` } -const modelCatalogCacheSchemaVersion = 1 +const modelCatalogCacheSchemaVersion = 2 var modelCatalogCacheWriteMu sync.Mutex @@ -118,6 +119,7 @@ type modelCatalogCache struct { Go []string `json:"go"` NativeProtocols map[Tier]map[string]Protocol `json:"native_protocols"` Unsupported map[Tier]map[string]bool `json:"unsupported"` + Metadata map[string]ModelMetadata `json:"metadata,omitempty"` } func newModelCatalog(prefer Tier, overrides map[string]string) *modelCatalog { @@ -146,10 +148,10 @@ func (c *modelCatalog) SetRefreshInterval(interval time.Duration) { } func (c *modelCatalog) Replace(zen, goModels []string) { - c.ReplaceWithCapabilities(zen, goModels, nil, nil) + c.ReplaceWithCapabilities(zen, goModels, nil, nil, nil) } -func (c *modelCatalog) ReplaceWithCapabilities(zen, goModels []string, native map[Tier]map[string]Protocol, unsupported map[Tier]map[string]bool) { +func (c *modelCatalog) ReplaceWithCapabilities(zen, goModels []string, native map[Tier]map[string]Protocol, unsupported map[Tier]map[string]bool, metadata map[string]ModelMetadata) { c.mu.Lock() defer c.mu.Unlock() if zen != nil { @@ -172,6 +174,9 @@ func (c *modelCatalog) ReplaceWithCapabilities(zen, goModels []string, native ma } } } + if metadata != nil { + c.modelMeta = cloneModelMeta(metadata) + } c.updatedAt = time.Now().UTC() c.cacheSource = "live" c.stale = false @@ -200,12 +205,14 @@ func (c *modelCatalog) CopyState(source *modelCatalog) { unsupported[tier][model] = value } } + meta := cloneModelMeta(source.modelMeta) updatedAt := source.updatedAt cacheSource := source.cacheSource stale := source.stale source.mu.RUnlock() c.mu.Lock() c.zen, c.goModels, c.nativeProtocols, c.unsupported, c.updatedAt = zen, goModels, native, unsupported, updatedAt + c.modelMeta = meta c.cacheSource, c.stale = cacheSource, stale c.mu.Unlock() } @@ -223,6 +230,7 @@ func (c *modelCatalog) LoadCache(path string) error { c.goModels = toSet(cache.Go) c.nativeProtocols = cloneTierProtocols(cache.NativeProtocols) c.unsupported = cloneTierBools(cache.Unsupported) + c.modelMeta = cloneModelMeta(cache.Metadata) c.updatedAt = cache.UpdatedAt.UTC() c.cacheSource = "disk" c.stale = true @@ -246,6 +254,7 @@ func (c *modelCatalog) SaveCache() error { Go: sortedSetKeys(c.goModels), NativeProtocols: cloneTierProtocols(c.nativeProtocols), Unsupported: cloneTierBools(c.unsupported), + Metadata: cloneModelMeta(c.modelMeta), } c.mu.RUnlock() if path == "" { @@ -440,6 +449,16 @@ func (c *modelCatalog) Supported(model string) bool { return c.supportedLocked(model) } +// Metadata returns the rich per-model metadata (context window, reasoning, +// tool call, modalities) captured from the opencode catalog. The zero value +// is returned for models the catalog does not describe (or before the first +// capability refresh). +func (c *modelCatalog) Metadata(model string) ModelMetadata { + c.mu.RLock() + defer c.mu.RUnlock() + return c.modelMeta[model] +} + func (c *modelCatalog) supportedLocked(model string) bool { if len(c.zen) == 0 && len(c.goModels) == 0 { return true @@ -492,6 +511,17 @@ func cloneBools(source map[string]bool) map[string]bool { return result } +func cloneModelMeta(source map[string]ModelMetadata) map[string]ModelMetadata { + if source == nil { + return nil + } + result := make(map[string]ModelMetadata, len(source)) + for id, md := range source { + result[id] = md + } + return result +} + func sortedSetKeys(source map[string]bool) []string { result := make([]string, 0, len(source)) for model, available := range source { @@ -675,6 +705,7 @@ func saveModelCatalogCache(path string, cache modelCatalogCache) error { type protocolCapabilities struct { Protocols map[Tier]map[string]Protocol Unsupported map[Tier]map[string]bool + Metadata map[string]ModelMetadata } type capabilityProvider struct { @@ -685,8 +716,51 @@ type capabilityProvider struct { } type capabilityModel struct { - ID string `json:"id"` - Provider *capabilityModelProvider `json:"provider"` + ID string `json:"id"` + Provider *capabilityModelProvider `json:"provider"` + Limit *capabilityModelLimit `json:"limit"` + Reasoning bool `json:"reasoning"` + ToolCall bool `json:"tool_call"` + StructuredOutput bool `json:"structured_output"` + InputModalities []string `json:"input"` + OutputModalities []string `json:"output"` +} + +type capabilityModelLimit struct { + Context int `json:"context"` + Input int `json:"input"` + Output int `json:"output"` +} + +// ModelMetadata carries the per-model capability fields surfaced through +// /v1/models so consumers (harnesses like Pi or jcode) get real context +// windows and feature flags from the catalog instead of guessing. It is +// purely additive: routing does not depend on any of these fields. +type ModelMetadata struct { + ContextWindow int `json:"context_window,omitempty"` + MaxInput int `json:"max_input,omitempty"` + MaxOutput int `json:"max_output,omitempty"` + Reasoning bool `json:"reasoning,omitempty"` + ToolCall bool `json:"tool_call,omitempty"` + StructuredOutput bool `json:"structured_output,omitempty"` + InputModalities []string `json:"input_modalities,omitempty"` + OutputModalities []string `json:"output_modalities,omitempty"` +} + +func (m *capabilityModel) metadata() ModelMetadata { + md := ModelMetadata{ + Reasoning: m.Reasoning, + ToolCall: m.ToolCall, + StructuredOutput: m.StructuredOutput, + InputModalities: m.InputModalities, + OutputModalities: m.OutputModalities, + } + if m.Limit != nil { + md.ContextWindow = m.Limit.Context + md.MaxInput = m.Limit.Input + md.MaxOutput = m.Limit.Output + } + return md } type capabilityModelProvider struct { @@ -723,6 +797,7 @@ func fetchProtocolCapabilities(ctx context.Context, client *http.Client, endpoin result := protocolCapabilities{ Protocols: map[Tier]map[string]Protocol{TierZen: {}, TierGo: {}}, Unsupported: map[Tier]map[string]bool{TierZen: {}, TierGo: {}}, + Metadata: map[string]ModelMetadata{}, } for providerID, provider := range providers { tier, ok := capabilityTier(providerID, provider.API) @@ -742,6 +817,7 @@ func fetchProtocolCapabilities(ctx context.Context, client *http.Client, endpoin } else { result.Unsupported[tier][modelID] = true } + result.Metadata[modelID] = model.metadata() } } // The machine catalog is the primary source. The upstream endpoint tables From f3593e37909273fafb67714c439a6a014ab4e458 Mon Sep 17 00:00:00 2001 From: sk-dev-ai Date: Fri, 4 Sep 2026 10:56:13 +0530 Subject: [PATCH 2/3] feat: emit top-level context_window and capability flags in /v1/models Discovery clients (jcode, Pi, etc.) read context_length/context_window and supported_reasoning_levels at the TOP LEVEL of each /v1/models entry, not inside a nested metadata object. Emit both: keep the rich nested metadata object AND mirror context_window/context_length/max_input/max_output/ reasoning/tool_call/structured_output at the top level so standard OpenAI model-discovery clients get correct context windows without config. --- config.json.models.catalog.json | 973 ++++++++++++++++++++++++++++++++ gateway.go | 29 +- 2 files changed, 1001 insertions(+), 1 deletion(-) create mode 100644 config.json.models.catalog.json diff --git a/config.json.models.catalog.json b/config.json.models.catalog.json new file mode 100644 index 0000000..47d25dc --- /dev/null +++ b/config.json.models.catalog.json @@ -0,0 +1,973 @@ +{ + "schema_version": 2, + "updated_at": "2026-09-04T05:22:53.853695398Z", + "zen": [ + "big-pickle", + "claude-fable-5", + "claude-fable-5-1", + "claude-haiku-4-5", + "claude-opus-4-5", + "claude-opus-4-6", + "claude-opus-4-7", + "claude-opus-4-8", + "claude-opus-5", + "claude-sonnet-4", + "claude-sonnet-4-5", + "claude-sonnet-4-6", + "claude-sonnet-5", + "deepseek-v4-flash", + "deepseek-v4-flash-free", + "deepseek-v4-pro", + "gemini-3-flash", + "gemini-3.1-pro", + "gemini-3.5-flash", + "gemini-3.5-flash-lite", + "gemini-3.6-flash", + "gemini-3.7-flash", + "gemini-3.8-flash", + "glm-5", + "glm-5.1", + "glm-5.2", + "gpt-5", + "gpt-5-codex", + "gpt-5-nano", + "gpt-5.1", + "gpt-5.1-codex", + "gpt-5.1-codex-max", + "gpt-5.1-codex-mini", + "gpt-5.2", + "gpt-5.2-codex", + "gpt-5.3-codex", + "gpt-5.3-codex-spark", + "gpt-5.4", + "gpt-5.4-mini", + "gpt-5.4-nano", + "gpt-5.4-pro", + "gpt-5.5", + "gpt-5.5-pro", + "gpt-5.6-luna", + "gpt-5.6-sol", + "gpt-5.6-terra", + "grok-4.5", + "grok-4.6", + "grok-build-0.1", + "kimi-k2.5", + "kimi-k2.6", + "kimi-k2.7-code", + "kimi-k3", + "laguna-s-2.1-free", + "ling-3.0-flash-fin-free", + "mimo-v2.5-free", + "minimax-m2.5", + "minimax-m2.7", + "minimax-m3", + "muse-spark-1.2", + "muse-spark-1.2-contributor-free", + "muse-spark-1.3-contributor-free", + "nemotron-3-ultra-free", + "nemotron-3.5-lightning-free", + "qwen3.5-plus", + "qwen3.6-plus" + ], + "go": [], + "native_protocols": { + "go": { + "deepseek-v4-flash": "chat", + "deepseek-v4-flash-vision-exp": "chat", + "deepseek-v4-pro": "chat", + "glm-5": "chat", + "glm-5.1": "chat", + "glm-5.2": "chat", + "glm-5.3": "chat", + "glm-5.3-flash": "chat", + "gpt-5.6-luna": "responses", + "grok-4.5": "responses", + "grok-4.6": "responses", + "hy3": "chat", + "hy4-preview": "chat", + "kimi-k2.5": "chat", + "kimi-k2.6": "chat", + "kimi-k2.7-code": "chat", + "kimi-k3": "chat", + "longcat-2.0": "chat", + "mimo-v2-omni": "chat", + "mimo-v2-pro": "chat", + "mimo-v2.5": "chat", + "mimo-v2.5-pro": "chat", + "minimax-m2.5": "anthropic", + "minimax-m2.7": "anthropic", + "minimax-m3": "anthropic", + "muse-spark-1.2-contributor": "responses", + "muse-spark-1.3-contributor": "responses", + "omen-alpha": "chat", + "ox-alpha-free": "chat", + "qwen3.5-plus": "chat", + "qwen3.6-plus": "anthropic", + "qwen3.7-max": "anthropic", + "qwen3.7-plus": "anthropic", + "qwen3.8-flash": "anthropic", + "qwen3.8-max": "anthropic" + }, + "zen": { + "big-pickle": "chat", + "claude-3-5-haiku": "anthropic", + "claude-fable-5": "anthropic", + "claude-fable-5-1": "anthropic", + "claude-haiku-4-5": "anthropic", + "claude-opus-4-1": "anthropic", + "claude-opus-4-5": "anthropic", + "claude-opus-4-6": "anthropic", + "claude-opus-4-7": "anthropic", + "claude-opus-4-8": "anthropic", + "claude-opus-5": "anthropic", + "claude-sonnet-4": "anthropic", + "claude-sonnet-4-5": "anthropic", + "claude-sonnet-4-6": "anthropic", + "claude-sonnet-5": "anthropic", + "deepseek-v4-flash": "chat", + "deepseek-v4-flash-free": "chat", + "deepseek-v4-pro": "chat", + "glm-4.6": "chat", + "glm-4.7": "chat", + "glm-4.7-free": "chat", + "glm-5": "chat", + "glm-5-free": "chat", + "glm-5.1": "chat", + "glm-5.2": "chat", + "gpt-5": "responses", + "gpt-5-codex": "responses", + "gpt-5-nano": "responses", + "gpt-5.1": "responses", + "gpt-5.1-codex": "responses", + "gpt-5.1-codex-max": "responses", + "gpt-5.1-codex-mini": "responses", + "gpt-5.2": "responses", + "gpt-5.2-codex": "responses", + "gpt-5.3-codex": "responses", + "gpt-5.3-codex-spark": "responses", + "gpt-5.4": "responses", + "gpt-5.4-mini": "responses", + "gpt-5.4-nano": "responses", + "gpt-5.4-pro": "responses", + "gpt-5.5": "responses", + "gpt-5.5-pro": "responses", + "gpt-5.6-luna": "responses", + "gpt-5.6-sol": "responses", + "gpt-5.6-terra": "responses", + "grok-4.5": "responses", + "grok-4.6": "responses", + "grok-build-0.1": "responses", + "grok-code": "chat", + "hy3-free": "chat", + "hy3-preview-free": "chat", + "kimi-k2": "chat", + "kimi-k2-thinking": "chat", + "kimi-k2.5": "chat", + "kimi-k2.5-free": "chat", + "kimi-k2.6": "chat", + "kimi-k2.7-code": "chat", + "kimi-k3": "chat", + "laguna-s-2.1-free": "chat", + "ling-2.6-flash-free": "chat", + "ling-3.0-flash-fin-free": "chat", + "ling-3.0-flash-free": "chat", + "ling-3.0-tiny-free": "chat", + "longcat-2.0-free": "chat", + "mimo-v2-flash-free": "chat", + "mimo-v2-omni-free": "chat", + "mimo-v2-pro-free": "chat", + "mimo-v2.5-free": "chat", + "minimax-m2.1": "chat", + "minimax-m2.1-free": "anthropic", + "minimax-m2.5": "chat", + "minimax-m2.5-free": "anthropic", + "minimax-m2.7": "chat", + "minimax-m3": "chat", + "minimax-m3-free": "anthropic", + "muse-spark-1.2": "responses", + "muse-spark-1.2-contributor-free": "responses", + "muse-spark-1.3-contributor-free": "responses", + "nemotron-3-super-free": "chat", + "nemotron-3-ultra-free": "chat", + "nemotron-3.5-lightning-free": "chat", + "north-mini-code-free": "chat", + "qwen3-coder": "chat", + "qwen3.5-plus": "anthropic", + "qwen3.6-plus": "anthropic", + "qwen3.6-plus-free": "anthropic", + "qwen3.7-max": "anthropic", + "qwen3.7-plus": "anthropic", + "ring-2.6-1t-free": "chat", + "trinity-large-preview-free": "chat", + "x-preview-f-free": "chat" + } + }, + "unsupported": { + "go": {}, + "zen": { + "gemini-3-flash": true, + "gemini-3-pro": true, + "gemini-3.1-pro": true, + "gemini-3.5-flash": true, + "gemini-3.5-flash-lite": true, + "gemini-3.6-flash": true, + "gemini-3.7-flash": true, + "gemini-3.8-flash": true + } + }, + "metadata": { + "big-pickle": { + "context_window": 200000, + "max_input": 160000, + "max_output": 32000, + "reasoning": true, + "tool_call": true, + "structured_output": true + }, + "claude-3-5-haiku": { + "context_window": 200000, + "max_output": 8192, + "tool_call": true + }, + "claude-fable-5": { + "context_window": 1000000, + "max_output": 128000, + "reasoning": true, + "tool_call": true + }, + "claude-fable-5-1": { + "context_window": 1000000, + "max_output": 128000, + "reasoning": true, + "tool_call": true + }, + "claude-haiku-4-5": { + "context_window": 200000, + "max_output": 64000, + "reasoning": true, + "tool_call": true + }, + "claude-opus-4-1": { + "context_window": 200000, + "max_output": 32000, + "reasoning": true, + "tool_call": true + }, + "claude-opus-4-5": { + "context_window": 200000, + "max_output": 64000, + "reasoning": true, + "tool_call": true + }, + "claude-opus-4-6": { + "context_window": 1000000, + "max_output": 128000, + "reasoning": true, + "tool_call": true + }, + "claude-opus-4-7": { + "context_window": 1000000, + "max_output": 128000, + "reasoning": true, + "tool_call": true + }, + "claude-opus-4-8": { + "context_window": 1000000, + "max_output": 128000, + "reasoning": true, + "tool_call": true + }, + "claude-opus-5": { + "context_window": 1000000, + "max_output": 128000, + "reasoning": true, + "tool_call": true + }, + "claude-sonnet-4": { + "context_window": 1000000, + "max_output": 64000, + "reasoning": true, + "tool_call": true + }, + "claude-sonnet-4-5": { + "context_window": 1000000, + "max_output": 64000, + "reasoning": true, + "tool_call": true + }, + "claude-sonnet-4-6": { + "context_window": 1000000, + "max_output": 64000, + "reasoning": true, + "tool_call": true + }, + "claude-sonnet-5": { + "context_window": 1000000, + "max_output": 128000, + "reasoning": true, + "tool_call": true + }, + "deepseek-v4-flash": { + "context_window": 1000000, + "max_output": 384000, + "reasoning": true, + "tool_call": true, + "structured_output": true + }, + "deepseek-v4-flash-free": { + "context_window": 200000, + "max_output": 128000, + "reasoning": true, + "tool_call": true, + "structured_output": true + }, + "deepseek-v4-flash-vision-exp": { + "context_window": 1000000, + "max_output": 384000, + "reasoning": true, + "tool_call": true, + "structured_output": true + }, + "deepseek-v4-pro": { + "context_window": 1000000, + "max_output": 384000, + "reasoning": true, + "tool_call": true, + "structured_output": true + }, + "gemini-3-flash": { + "context_window": 1048576, + "max_output": 65536, + "reasoning": true, + "tool_call": true, + "structured_output": true + }, + "gemini-3-pro": { + "context_window": 1048576, + "max_output": 65536, + "reasoning": true, + "tool_call": true, + "structured_output": true + }, + "gemini-3.1-pro": { + "context_window": 1048576, + "max_output": 65536, + "reasoning": true, + "tool_call": true, + "structured_output": true + }, + "gemini-3.5-flash": { + "context_window": 1048576, + "max_output": 65536, + "reasoning": true, + "tool_call": true, + "structured_output": true + }, + "gemini-3.5-flash-lite": { + "context_window": 1048576, + "max_output": 65536, + "reasoning": true, + "tool_call": true, + "structured_output": true + }, + "gemini-3.6-flash": { + "context_window": 1048576, + "max_output": 65536, + "reasoning": true, + "tool_call": true, + "structured_output": true + }, + "gemini-3.7-flash": { + "context_window": 1048576, + "max_output": 65536, + "reasoning": true, + "tool_call": true, + "structured_output": true + }, + "gemini-3.8-flash": { + "context_window": 1048576, + "max_output": 65536, + "reasoning": true, + "tool_call": true, + "structured_output": true + }, + "glm-4.6": { + "context_window": 204800, + "max_output": 131072, + "reasoning": true, + "tool_call": true + }, + "glm-4.7": { + "context_window": 204800, + "max_output": 131072, + "reasoning": true, + "tool_call": true + }, + "glm-4.7-free": { + "context_window": 204800, + "max_output": 131072, + "reasoning": true, + "tool_call": true + }, + "glm-5": { + "context_window": 204800, + "max_output": 131072, + "reasoning": true, + "tool_call": true + }, + "glm-5-free": { + "context_window": 204800, + "max_output": 131072, + "reasoning": true, + "tool_call": true + }, + "glm-5.1": { + "context_window": 204800, + "max_output": 131072, + "reasoning": true, + "tool_call": true + }, + "glm-5.2": { + "context_window": 1000000, + "max_output": 131072, + "reasoning": true, + "tool_call": true, + "structured_output": true + }, + "glm-5.3": { + "context_window": 1000000, + "max_output": 131072, + "reasoning": true, + "tool_call": true, + "structured_output": true + }, + "glm-5.3-flash": { + "context_window": 1000000, + "max_output": 131072, + "reasoning": true, + "tool_call": true, + "structured_output": true + }, + "gpt-5": { + "context_window": 400000, + "max_input": 272000, + "max_output": 128000, + "reasoning": true, + "tool_call": true, + "structured_output": true + }, + "gpt-5-codex": { + "context_window": 400000, + "max_input": 272000, + "max_output": 128000, + "reasoning": true, + "tool_call": true, + "structured_output": true + }, + "gpt-5-nano": { + "context_window": 400000, + "max_input": 272000, + "max_output": 128000, + "reasoning": true, + "tool_call": true, + "structured_output": true + }, + "gpt-5.1": { + "context_window": 400000, + "max_input": 272000, + "max_output": 128000, + "reasoning": true, + "tool_call": true, + "structured_output": true + }, + "gpt-5.1-codex": { + "context_window": 400000, + "max_input": 272000, + "max_output": 128000, + "reasoning": true, + "tool_call": true, + "structured_output": true + }, + "gpt-5.1-codex-max": { + "context_window": 400000, + "max_input": 272000, + "max_output": 128000, + "reasoning": true, + "tool_call": true, + "structured_output": true + }, + "gpt-5.1-codex-mini": { + "context_window": 400000, + "max_input": 272000, + "max_output": 128000, + "reasoning": true, + "tool_call": true, + "structured_output": true + }, + "gpt-5.2": { + "context_window": 400000, + "max_input": 272000, + "max_output": 128000, + "reasoning": true, + "tool_call": true, + "structured_output": true + }, + "gpt-5.2-codex": { + "context_window": 400000, + "max_input": 272000, + "max_output": 128000, + "reasoning": true, + "tool_call": true, + "structured_output": true + }, + "gpt-5.3-codex": { + "context_window": 400000, + "max_input": 272000, + "max_output": 128000, + "reasoning": true, + "tool_call": true, + "structured_output": true + }, + "gpt-5.3-codex-spark": { + "context_window": 128000, + "max_input": 128000, + "max_output": 128000, + "reasoning": true, + "tool_call": true, + "structured_output": true + }, + "gpt-5.4": { + "context_window": 1050000, + "max_input": 922000, + "max_output": 128000, + "reasoning": true, + "tool_call": true, + "structured_output": true + }, + "gpt-5.4-mini": { + "context_window": 400000, + "max_input": 272000, + "max_output": 128000, + "reasoning": true, + "tool_call": true, + "structured_output": true + }, + "gpt-5.4-nano": { + "context_window": 400000, + "max_input": 272000, + "max_output": 128000, + "reasoning": true, + "tool_call": true, + "structured_output": true + }, + "gpt-5.4-pro": { + "context_window": 1050000, + "max_input": 922000, + "max_output": 128000, + "reasoning": true, + "tool_call": true + }, + "gpt-5.5": { + "context_window": 1050000, + "max_input": 922000, + "max_output": 128000, + "reasoning": true, + "tool_call": true, + "structured_output": true + }, + "gpt-5.5-pro": { + "context_window": 1050000, + "max_input": 922000, + "max_output": 128000, + "reasoning": true, + "tool_call": true + }, + "gpt-5.6-luna": { + "context_window": 1050000, + "max_input": 922000, + "max_output": 128000, + "reasoning": true, + "tool_call": true, + "structured_output": true + }, + "gpt-5.6-sol": { + "context_window": 1050000, + "max_input": 922000, + "max_output": 128000, + "reasoning": true, + "tool_call": true, + "structured_output": true + }, + "gpt-5.6-terra": { + "context_window": 1050000, + "max_input": 922000, + "max_output": 128000, + "reasoning": true, + "tool_call": true, + "structured_output": true + }, + "grok-4.5": { + "context_window": 500000, + "max_output": 500000, + "reasoning": true, + "tool_call": true, + "structured_output": true + }, + "grok-4.6": { + "context_window": 500000, + "max_output": 500000, + "reasoning": true, + "tool_call": true, + "structured_output": true + }, + "grok-build-0.1": { + "context_window": 256000, + "max_output": 256000, + "reasoning": true, + "tool_call": true, + "structured_output": true + }, + "grok-code": { + "context_window": 256000, + "max_output": 256000, + "reasoning": true, + "tool_call": true + }, + "hy3": { + "context_window": 256000, + "max_input": 192000, + "max_output": 128000, + "reasoning": true, + "tool_call": true + }, + "hy3-free": { + "context_window": 190000, + "max_input": 192000, + "max_output": 64000, + "reasoning": true, + "tool_call": true, + "structured_output": true + }, + "hy3-preview-free": { + "context_window": 256000, + "max_output": 64000, + "reasoning": true, + "tool_call": true + }, + "hy4-preview": { + "context_window": 1024000, + "max_output": 64000, + "reasoning": true, + "tool_call": true + }, + "kimi-k2": { + "context_window": 262144, + "max_output": 262144, + "tool_call": true + }, + "kimi-k2-thinking": { + "context_window": 262144, + "max_output": 262144, + "reasoning": true, + "tool_call": true + }, + "kimi-k2.5": { + "context_window": 262144, + "max_output": 65536, + "reasoning": true, + "tool_call": true + }, + "kimi-k2.5-free": { + "context_window": 262144, + "max_output": 262144, + "reasoning": true, + "tool_call": true + }, + "kimi-k2.6": { + "context_window": 262144, + "max_output": 65536, + "reasoning": true, + "tool_call": true + }, + "kimi-k2.7-code": { + "context_window": 262144, + "max_output": 262144, + "reasoning": true, + "tool_call": true, + "structured_output": true + }, + "kimi-k3": { + "context_window": 1048576, + "max_output": 131072, + "reasoning": true, + "tool_call": true, + "structured_output": true + }, + "laguna-s-2.1-free": { + "context_window": 256000, + "max_output": 32000, + "reasoning": true, + "tool_call": true + }, + "ling-2.6-flash-free": { + "context_window": 262100, + "max_output": 32800, + "tool_call": true + }, + "ling-3.0-flash-fin-free": { + "context_window": 262144, + "max_output": 32768, + "reasoning": true, + "tool_call": true + }, + "ling-3.0-flash-free": { + "context_window": 262144, + "max_output": 32768, + "reasoning": true, + "tool_call": true + }, + "ling-3.0-tiny-free": { + "context_window": 262144, + "max_output": 32768, + "reasoning": true, + "tool_call": true + }, + "longcat-2.0": { + "context_window": 1000000, + "max_output": 131072, + "reasoning": true, + "tool_call": true + }, + "longcat-2.0-free": { + "context_window": 1000000, + "max_output": 131072, + "reasoning": true, + "tool_call": true + }, + "mimo-v2-flash-free": { + "context_window": 262144, + "max_output": 65536, + "reasoning": true, + "tool_call": true + }, + "mimo-v2-omni": { + "context_window": 262144, + "max_output": 128000, + "reasoning": true, + "tool_call": true + }, + "mimo-v2-omni-free": { + "context_window": 262144, + "max_output": 64000, + "reasoning": true, + "tool_call": true + }, + "mimo-v2-pro": { + "context_window": 1048576, + "max_output": 128000, + "reasoning": true, + "tool_call": true + }, + "mimo-v2-pro-free": { + "context_window": 1048576, + "max_output": 64000, + "reasoning": true, + "tool_call": true + }, + "mimo-v2.5": { + "context_window": 1000000, + "max_output": 128000, + "reasoning": true, + "tool_call": true + }, + "mimo-v2.5-free": { + "context_window": 200000, + "max_output": 32000, + "reasoning": true, + "tool_call": true + }, + "mimo-v2.5-pro": { + "context_window": 1048576, + "max_output": 128000, + "reasoning": true, + "tool_call": true + }, + "minimax-m2.1": { + "context_window": 204800, + "max_output": 131072, + "reasoning": true, + "tool_call": true + }, + "minimax-m2.1-free": { + "context_window": 204800, + "max_output": 131072, + "reasoning": true, + "tool_call": true + }, + "minimax-m2.5": { + "context_window": 204800, + "max_output": 131072, + "reasoning": true, + "tool_call": true + }, + "minimax-m2.5-free": { + "context_window": 204800, + "max_output": 131072, + "reasoning": true, + "tool_call": true + }, + "minimax-m2.7": { + "context_window": 204800, + "max_output": 131072, + "reasoning": true, + "tool_call": true + }, + "minimax-m3": { + "context_window": 512000, + "max_output": 128000, + "reasoning": true, + "tool_call": true + }, + "minimax-m3-free": { + "context_window": 200000, + "max_output": 32000, + "reasoning": true, + "tool_call": true + }, + "muse-spark-1.2": { + "context_window": 1048576, + "max_output": 131072, + "reasoning": true, + "tool_call": true, + "structured_output": true + }, + "muse-spark-1.2-contributor": { + "context_window": 1048576, + "max_output": 131072, + "reasoning": true, + "tool_call": true, + "structured_output": true + }, + "muse-spark-1.2-contributor-free": { + "context_window": 1048576, + "max_output": 131072, + "reasoning": true, + "tool_call": true, + "structured_output": true + }, + "muse-spark-1.3-contributor": { + "context_window": 1048576, + "max_output": 131072, + "reasoning": true, + "tool_call": true, + "structured_output": true + }, + "muse-spark-1.3-contributor-free": { + "context_window": 1048576, + "max_output": 131072, + "reasoning": true, + "tool_call": true, + "structured_output": true + }, + "nemotron-3-super-free": { + "context_window": 204800, + "max_output": 128000, + "reasoning": true, + "tool_call": true + }, + "nemotron-3-ultra-free": { + "context_window": 1000000, + "max_output": 128000, + "reasoning": true, + "tool_call": true + }, + "nemotron-3.5-lightning-free": { + "context_window": 262144, + "max_output": 262144, + "reasoning": true, + "tool_call": true, + "structured_output": true + }, + "north-mini-code-free": { + "context_window": 256000, + "max_output": 64000, + "reasoning": true, + "tool_call": true, + "structured_output": true + }, + "ox-alpha-free": { + "context_window": 1000000, + "max_output": 131072, + "reasoning": true, + "tool_call": true, + "structured_output": true + }, + "qwen3-coder": { + "context_window": 262144, + "max_output": 65536, + "tool_call": true + }, + "qwen3.5-plus": { + "context_window": 262144, + "max_output": 65536, + "reasoning": true, + "tool_call": true + }, + "qwen3.6-plus": { + "context_window": 262144, + "max_output": 65536, + "reasoning": true, + "tool_call": true + }, + "qwen3.6-plus-free": { + "context_window": 262144, + "max_output": 65536, + "reasoning": true, + "tool_call": true + }, + "qwen3.7-max": { + "context_window": 1000000, + "max_output": 65536, + "reasoning": true, + "tool_call": true + }, + "qwen3.7-plus": { + "context_window": 1000000, + "max_output": 65536, + "reasoning": true, + "tool_call": true + }, + "qwen3.8-flash": { + "context_window": 1000000, + "max_output": 131072, + "reasoning": true, + "tool_call": true, + "structured_output": true + }, + "qwen3.8-max": { + "context_window": 1000000, + "max_output": 131072, + "reasoning": true, + "tool_call": true, + "structured_output": true + }, + "ring-2.6-1t-free": { + "context_window": 262000, + "max_output": 66000, + "reasoning": true, + "tool_call": true + }, + "trinity-large-preview-free": { + "context_window": 131072, + "max_output": 131072, + "tool_call": true + }, + "x-preview-f-free": { + "context_window": 1000000, + "max_output": 131072, + "reasoning": true, + "tool_call": true, + "structured_output": true + } + } +} diff --git a/gateway.go b/gateway.go index 6a4f8cf..84faf2e 100644 --- a/gateway.go +++ b/gateway.go @@ -214,7 +214,34 @@ func (g *Gateway) handleModels(w http.ResponseWriter, _ *http.Request) { if _, err := g.catalog.Route(model, len(g.cfg.ZenKeys) > 0, len(g.cfg.GoKeys) > 0, g.cfg.Anonymous); err != nil { continue } - data = append(data, map[string]any{"id": model, "object": "model", "created": now, "owned_by": "opencode", "metadata": g.catalog.Metadata(model)}) + md := g.catalog.Metadata(model) + entry := map[string]any{ + "id": model, "object": "model", "created": now, "owned_by": "opencode", + "metadata": md, + } + // Top-level OpenAI-standard fields: discovery clients (jcode, Pi, …) + // read context/reasoning at the top level of each model entry. + if md.ContextWindow > 0 { + entry["context_window"] = md.ContextWindow + entry["context_length"] = md.ContextWindow + } + if md.MaxInput > 0 { + entry["max_input"] = md.MaxInput + } + if md.MaxOutput > 0 { + entry["max_output"] = md.MaxOutput + } + if md.Reasoning { + entry["reasoning"] = true + entry["supports_reasoning"] = true + } + if md.ToolCall { + entry["tool_call"] = true + } + if md.StructuredOutput { + entry["structured_output"] = true + } + data = append(data, entry) } writeJSON(w, http.StatusOK, map[string]any{"object": "list", "data": data}) } From ce89cc01cbde6b45f52ba3381079431de32b96f0 Mon Sep 17 00:00:00 2001 From: sk-dev-ai Date: Sat, 5 Sep 2026 12:29:36 +0530 Subject: [PATCH 3/3] fix: nested modalities, per-tier metadata, drop committed cache - capabilityModel now reads the nested modalities.{input,output} object the catalog actually serves; the old top-level input/output tags never matched and left both arrays permanently empty. - Metadata is stored per tier and /v1/models selects by route.Tier, so same-named Zen/Go models (glm-5, minimax-m3, ...) advertise the limits of the tier that will serve them. Anonymous routes resolve to TierZen, keeping the keyless path on Zen metadata. - Cache schema 2 -> 3; stale flat caches are refused and fall back to live refresh. - Untrack config.json.models.catalog.json and ignore *.models.catalog.json: it is a runtime snapshot, not source. - Add models_test.go fixtures: nested modalities, tier-split limits, tier selection, v3 round-trip, v2 rejection. --- .gitignore | 1 + config.json.models.catalog.json | 973 -------------------------------- gateway.go | 7 +- models.go | 80 +-- models_test.go | 194 +++++++ 5 files changed, 244 insertions(+), 1011 deletions(-) delete mode 100644 config.json.models.catalog.json create mode 100644 models_test.go diff --git a/.gitignore b/.gitignore index 4c43a74..ba2eec1 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ opencode2api.exe *.log .tmp/ .gocache/ +*.models.catalog.json diff --git a/config.json.models.catalog.json b/config.json.models.catalog.json deleted file mode 100644 index 47d25dc..0000000 --- a/config.json.models.catalog.json +++ /dev/null @@ -1,973 +0,0 @@ -{ - "schema_version": 2, - "updated_at": "2026-09-04T05:22:53.853695398Z", - "zen": [ - "big-pickle", - "claude-fable-5", - "claude-fable-5-1", - "claude-haiku-4-5", - "claude-opus-4-5", - "claude-opus-4-6", - "claude-opus-4-7", - "claude-opus-4-8", - "claude-opus-5", - "claude-sonnet-4", - "claude-sonnet-4-5", - "claude-sonnet-4-6", - "claude-sonnet-5", - "deepseek-v4-flash", - "deepseek-v4-flash-free", - "deepseek-v4-pro", - "gemini-3-flash", - "gemini-3.1-pro", - "gemini-3.5-flash", - "gemini-3.5-flash-lite", - "gemini-3.6-flash", - "gemini-3.7-flash", - "gemini-3.8-flash", - "glm-5", - "glm-5.1", - "glm-5.2", - "gpt-5", - "gpt-5-codex", - "gpt-5-nano", - "gpt-5.1", - "gpt-5.1-codex", - "gpt-5.1-codex-max", - "gpt-5.1-codex-mini", - "gpt-5.2", - "gpt-5.2-codex", - "gpt-5.3-codex", - "gpt-5.3-codex-spark", - "gpt-5.4", - "gpt-5.4-mini", - "gpt-5.4-nano", - "gpt-5.4-pro", - "gpt-5.5", - "gpt-5.5-pro", - "gpt-5.6-luna", - "gpt-5.6-sol", - "gpt-5.6-terra", - "grok-4.5", - "grok-4.6", - "grok-build-0.1", - "kimi-k2.5", - "kimi-k2.6", - "kimi-k2.7-code", - "kimi-k3", - "laguna-s-2.1-free", - "ling-3.0-flash-fin-free", - "mimo-v2.5-free", - "minimax-m2.5", - "minimax-m2.7", - "minimax-m3", - "muse-spark-1.2", - "muse-spark-1.2-contributor-free", - "muse-spark-1.3-contributor-free", - "nemotron-3-ultra-free", - "nemotron-3.5-lightning-free", - "qwen3.5-plus", - "qwen3.6-plus" - ], - "go": [], - "native_protocols": { - "go": { - "deepseek-v4-flash": "chat", - "deepseek-v4-flash-vision-exp": "chat", - "deepseek-v4-pro": "chat", - "glm-5": "chat", - "glm-5.1": "chat", - "glm-5.2": "chat", - "glm-5.3": "chat", - "glm-5.3-flash": "chat", - "gpt-5.6-luna": "responses", - "grok-4.5": "responses", - "grok-4.6": "responses", - "hy3": "chat", - "hy4-preview": "chat", - "kimi-k2.5": "chat", - "kimi-k2.6": "chat", - "kimi-k2.7-code": "chat", - "kimi-k3": "chat", - "longcat-2.0": "chat", - "mimo-v2-omni": "chat", - "mimo-v2-pro": "chat", - "mimo-v2.5": "chat", - "mimo-v2.5-pro": "chat", - "minimax-m2.5": "anthropic", - "minimax-m2.7": "anthropic", - "minimax-m3": "anthropic", - "muse-spark-1.2-contributor": "responses", - "muse-spark-1.3-contributor": "responses", - "omen-alpha": "chat", - "ox-alpha-free": "chat", - "qwen3.5-plus": "chat", - "qwen3.6-plus": "anthropic", - "qwen3.7-max": "anthropic", - "qwen3.7-plus": "anthropic", - "qwen3.8-flash": "anthropic", - "qwen3.8-max": "anthropic" - }, - "zen": { - "big-pickle": "chat", - "claude-3-5-haiku": "anthropic", - "claude-fable-5": "anthropic", - "claude-fable-5-1": "anthropic", - "claude-haiku-4-5": "anthropic", - "claude-opus-4-1": "anthropic", - "claude-opus-4-5": "anthropic", - "claude-opus-4-6": "anthropic", - "claude-opus-4-7": "anthropic", - "claude-opus-4-8": "anthropic", - "claude-opus-5": "anthropic", - "claude-sonnet-4": "anthropic", - "claude-sonnet-4-5": "anthropic", - "claude-sonnet-4-6": "anthropic", - "claude-sonnet-5": "anthropic", - "deepseek-v4-flash": "chat", - "deepseek-v4-flash-free": "chat", - "deepseek-v4-pro": "chat", - "glm-4.6": "chat", - "glm-4.7": "chat", - "glm-4.7-free": "chat", - "glm-5": "chat", - "glm-5-free": "chat", - "glm-5.1": "chat", - "glm-5.2": "chat", - "gpt-5": "responses", - "gpt-5-codex": "responses", - "gpt-5-nano": "responses", - "gpt-5.1": "responses", - "gpt-5.1-codex": "responses", - "gpt-5.1-codex-max": "responses", - "gpt-5.1-codex-mini": "responses", - "gpt-5.2": "responses", - "gpt-5.2-codex": "responses", - "gpt-5.3-codex": "responses", - "gpt-5.3-codex-spark": "responses", - "gpt-5.4": "responses", - "gpt-5.4-mini": "responses", - "gpt-5.4-nano": "responses", - "gpt-5.4-pro": "responses", - "gpt-5.5": "responses", - "gpt-5.5-pro": "responses", - "gpt-5.6-luna": "responses", - "gpt-5.6-sol": "responses", - "gpt-5.6-terra": "responses", - "grok-4.5": "responses", - "grok-4.6": "responses", - "grok-build-0.1": "responses", - "grok-code": "chat", - "hy3-free": "chat", - "hy3-preview-free": "chat", - "kimi-k2": "chat", - "kimi-k2-thinking": "chat", - "kimi-k2.5": "chat", - "kimi-k2.5-free": "chat", - "kimi-k2.6": "chat", - "kimi-k2.7-code": "chat", - "kimi-k3": "chat", - "laguna-s-2.1-free": "chat", - "ling-2.6-flash-free": "chat", - "ling-3.0-flash-fin-free": "chat", - "ling-3.0-flash-free": "chat", - "ling-3.0-tiny-free": "chat", - "longcat-2.0-free": "chat", - "mimo-v2-flash-free": "chat", - "mimo-v2-omni-free": "chat", - "mimo-v2-pro-free": "chat", - "mimo-v2.5-free": "chat", - "minimax-m2.1": "chat", - "minimax-m2.1-free": "anthropic", - "minimax-m2.5": "chat", - "minimax-m2.5-free": "anthropic", - "minimax-m2.7": "chat", - "minimax-m3": "chat", - "minimax-m3-free": "anthropic", - "muse-spark-1.2": "responses", - "muse-spark-1.2-contributor-free": "responses", - "muse-spark-1.3-contributor-free": "responses", - "nemotron-3-super-free": "chat", - "nemotron-3-ultra-free": "chat", - "nemotron-3.5-lightning-free": "chat", - "north-mini-code-free": "chat", - "qwen3-coder": "chat", - "qwen3.5-plus": "anthropic", - "qwen3.6-plus": "anthropic", - "qwen3.6-plus-free": "anthropic", - "qwen3.7-max": "anthropic", - "qwen3.7-plus": "anthropic", - "ring-2.6-1t-free": "chat", - "trinity-large-preview-free": "chat", - "x-preview-f-free": "chat" - } - }, - "unsupported": { - "go": {}, - "zen": { - "gemini-3-flash": true, - "gemini-3-pro": true, - "gemini-3.1-pro": true, - "gemini-3.5-flash": true, - "gemini-3.5-flash-lite": true, - "gemini-3.6-flash": true, - "gemini-3.7-flash": true, - "gemini-3.8-flash": true - } - }, - "metadata": { - "big-pickle": { - "context_window": 200000, - "max_input": 160000, - "max_output": 32000, - "reasoning": true, - "tool_call": true, - "structured_output": true - }, - "claude-3-5-haiku": { - "context_window": 200000, - "max_output": 8192, - "tool_call": true - }, - "claude-fable-5": { - "context_window": 1000000, - "max_output": 128000, - "reasoning": true, - "tool_call": true - }, - "claude-fable-5-1": { - "context_window": 1000000, - "max_output": 128000, - "reasoning": true, - "tool_call": true - }, - "claude-haiku-4-5": { - "context_window": 200000, - "max_output": 64000, - "reasoning": true, - "tool_call": true - }, - "claude-opus-4-1": { - "context_window": 200000, - "max_output": 32000, - "reasoning": true, - "tool_call": true - }, - "claude-opus-4-5": { - "context_window": 200000, - "max_output": 64000, - "reasoning": true, - "tool_call": true - }, - "claude-opus-4-6": { - "context_window": 1000000, - "max_output": 128000, - "reasoning": true, - "tool_call": true - }, - "claude-opus-4-7": { - "context_window": 1000000, - "max_output": 128000, - "reasoning": true, - "tool_call": true - }, - "claude-opus-4-8": { - "context_window": 1000000, - "max_output": 128000, - "reasoning": true, - "tool_call": true - }, - "claude-opus-5": { - "context_window": 1000000, - "max_output": 128000, - "reasoning": true, - "tool_call": true - }, - "claude-sonnet-4": { - "context_window": 1000000, - "max_output": 64000, - "reasoning": true, - "tool_call": true - }, - "claude-sonnet-4-5": { - "context_window": 1000000, - "max_output": 64000, - "reasoning": true, - "tool_call": true - }, - "claude-sonnet-4-6": { - "context_window": 1000000, - "max_output": 64000, - "reasoning": true, - "tool_call": true - }, - "claude-sonnet-5": { - "context_window": 1000000, - "max_output": 128000, - "reasoning": true, - "tool_call": true - }, - "deepseek-v4-flash": { - "context_window": 1000000, - "max_output": 384000, - "reasoning": true, - "tool_call": true, - "structured_output": true - }, - "deepseek-v4-flash-free": { - "context_window": 200000, - "max_output": 128000, - "reasoning": true, - "tool_call": true, - "structured_output": true - }, - "deepseek-v4-flash-vision-exp": { - "context_window": 1000000, - "max_output": 384000, - "reasoning": true, - "tool_call": true, - "structured_output": true - }, - "deepseek-v4-pro": { - "context_window": 1000000, - "max_output": 384000, - "reasoning": true, - "tool_call": true, - "structured_output": true - }, - "gemini-3-flash": { - "context_window": 1048576, - "max_output": 65536, - "reasoning": true, - "tool_call": true, - "structured_output": true - }, - "gemini-3-pro": { - "context_window": 1048576, - "max_output": 65536, - "reasoning": true, - "tool_call": true, - "structured_output": true - }, - "gemini-3.1-pro": { - "context_window": 1048576, - "max_output": 65536, - "reasoning": true, - "tool_call": true, - "structured_output": true - }, - "gemini-3.5-flash": { - "context_window": 1048576, - "max_output": 65536, - "reasoning": true, - "tool_call": true, - "structured_output": true - }, - "gemini-3.5-flash-lite": { - "context_window": 1048576, - "max_output": 65536, - "reasoning": true, - "tool_call": true, - "structured_output": true - }, - "gemini-3.6-flash": { - "context_window": 1048576, - "max_output": 65536, - "reasoning": true, - "tool_call": true, - "structured_output": true - }, - "gemini-3.7-flash": { - "context_window": 1048576, - "max_output": 65536, - "reasoning": true, - "tool_call": true, - "structured_output": true - }, - "gemini-3.8-flash": { - "context_window": 1048576, - "max_output": 65536, - "reasoning": true, - "tool_call": true, - "structured_output": true - }, - "glm-4.6": { - "context_window": 204800, - "max_output": 131072, - "reasoning": true, - "tool_call": true - }, - "glm-4.7": { - "context_window": 204800, - "max_output": 131072, - "reasoning": true, - "tool_call": true - }, - "glm-4.7-free": { - "context_window": 204800, - "max_output": 131072, - "reasoning": true, - "tool_call": true - }, - "glm-5": { - "context_window": 204800, - "max_output": 131072, - "reasoning": true, - "tool_call": true - }, - "glm-5-free": { - "context_window": 204800, - "max_output": 131072, - "reasoning": true, - "tool_call": true - }, - "glm-5.1": { - "context_window": 204800, - "max_output": 131072, - "reasoning": true, - "tool_call": true - }, - "glm-5.2": { - "context_window": 1000000, - "max_output": 131072, - "reasoning": true, - "tool_call": true, - "structured_output": true - }, - "glm-5.3": { - "context_window": 1000000, - "max_output": 131072, - "reasoning": true, - "tool_call": true, - "structured_output": true - }, - "glm-5.3-flash": { - "context_window": 1000000, - "max_output": 131072, - "reasoning": true, - "tool_call": true, - "structured_output": true - }, - "gpt-5": { - "context_window": 400000, - "max_input": 272000, - "max_output": 128000, - "reasoning": true, - "tool_call": true, - "structured_output": true - }, - "gpt-5-codex": { - "context_window": 400000, - "max_input": 272000, - "max_output": 128000, - "reasoning": true, - "tool_call": true, - "structured_output": true - }, - "gpt-5-nano": { - "context_window": 400000, - "max_input": 272000, - "max_output": 128000, - "reasoning": true, - "tool_call": true, - "structured_output": true - }, - "gpt-5.1": { - "context_window": 400000, - "max_input": 272000, - "max_output": 128000, - "reasoning": true, - "tool_call": true, - "structured_output": true - }, - "gpt-5.1-codex": { - "context_window": 400000, - "max_input": 272000, - "max_output": 128000, - "reasoning": true, - "tool_call": true, - "structured_output": true - }, - "gpt-5.1-codex-max": { - "context_window": 400000, - "max_input": 272000, - "max_output": 128000, - "reasoning": true, - "tool_call": true, - "structured_output": true - }, - "gpt-5.1-codex-mini": { - "context_window": 400000, - "max_input": 272000, - "max_output": 128000, - "reasoning": true, - "tool_call": true, - "structured_output": true - }, - "gpt-5.2": { - "context_window": 400000, - "max_input": 272000, - "max_output": 128000, - "reasoning": true, - "tool_call": true, - "structured_output": true - }, - "gpt-5.2-codex": { - "context_window": 400000, - "max_input": 272000, - "max_output": 128000, - "reasoning": true, - "tool_call": true, - "structured_output": true - }, - "gpt-5.3-codex": { - "context_window": 400000, - "max_input": 272000, - "max_output": 128000, - "reasoning": true, - "tool_call": true, - "structured_output": true - }, - "gpt-5.3-codex-spark": { - "context_window": 128000, - "max_input": 128000, - "max_output": 128000, - "reasoning": true, - "tool_call": true, - "structured_output": true - }, - "gpt-5.4": { - "context_window": 1050000, - "max_input": 922000, - "max_output": 128000, - "reasoning": true, - "tool_call": true, - "structured_output": true - }, - "gpt-5.4-mini": { - "context_window": 400000, - "max_input": 272000, - "max_output": 128000, - "reasoning": true, - "tool_call": true, - "structured_output": true - }, - "gpt-5.4-nano": { - "context_window": 400000, - "max_input": 272000, - "max_output": 128000, - "reasoning": true, - "tool_call": true, - "structured_output": true - }, - "gpt-5.4-pro": { - "context_window": 1050000, - "max_input": 922000, - "max_output": 128000, - "reasoning": true, - "tool_call": true - }, - "gpt-5.5": { - "context_window": 1050000, - "max_input": 922000, - "max_output": 128000, - "reasoning": true, - "tool_call": true, - "structured_output": true - }, - "gpt-5.5-pro": { - "context_window": 1050000, - "max_input": 922000, - "max_output": 128000, - "reasoning": true, - "tool_call": true - }, - "gpt-5.6-luna": { - "context_window": 1050000, - "max_input": 922000, - "max_output": 128000, - "reasoning": true, - "tool_call": true, - "structured_output": true - }, - "gpt-5.6-sol": { - "context_window": 1050000, - "max_input": 922000, - "max_output": 128000, - "reasoning": true, - "tool_call": true, - "structured_output": true - }, - "gpt-5.6-terra": { - "context_window": 1050000, - "max_input": 922000, - "max_output": 128000, - "reasoning": true, - "tool_call": true, - "structured_output": true - }, - "grok-4.5": { - "context_window": 500000, - "max_output": 500000, - "reasoning": true, - "tool_call": true, - "structured_output": true - }, - "grok-4.6": { - "context_window": 500000, - "max_output": 500000, - "reasoning": true, - "tool_call": true, - "structured_output": true - }, - "grok-build-0.1": { - "context_window": 256000, - "max_output": 256000, - "reasoning": true, - "tool_call": true, - "structured_output": true - }, - "grok-code": { - "context_window": 256000, - "max_output": 256000, - "reasoning": true, - "tool_call": true - }, - "hy3": { - "context_window": 256000, - "max_input": 192000, - "max_output": 128000, - "reasoning": true, - "tool_call": true - }, - "hy3-free": { - "context_window": 190000, - "max_input": 192000, - "max_output": 64000, - "reasoning": true, - "tool_call": true, - "structured_output": true - }, - "hy3-preview-free": { - "context_window": 256000, - "max_output": 64000, - "reasoning": true, - "tool_call": true - }, - "hy4-preview": { - "context_window": 1024000, - "max_output": 64000, - "reasoning": true, - "tool_call": true - }, - "kimi-k2": { - "context_window": 262144, - "max_output": 262144, - "tool_call": true - }, - "kimi-k2-thinking": { - "context_window": 262144, - "max_output": 262144, - "reasoning": true, - "tool_call": true - }, - "kimi-k2.5": { - "context_window": 262144, - "max_output": 65536, - "reasoning": true, - "tool_call": true - }, - "kimi-k2.5-free": { - "context_window": 262144, - "max_output": 262144, - "reasoning": true, - "tool_call": true - }, - "kimi-k2.6": { - "context_window": 262144, - "max_output": 65536, - "reasoning": true, - "tool_call": true - }, - "kimi-k2.7-code": { - "context_window": 262144, - "max_output": 262144, - "reasoning": true, - "tool_call": true, - "structured_output": true - }, - "kimi-k3": { - "context_window": 1048576, - "max_output": 131072, - "reasoning": true, - "tool_call": true, - "structured_output": true - }, - "laguna-s-2.1-free": { - "context_window": 256000, - "max_output": 32000, - "reasoning": true, - "tool_call": true - }, - "ling-2.6-flash-free": { - "context_window": 262100, - "max_output": 32800, - "tool_call": true - }, - "ling-3.0-flash-fin-free": { - "context_window": 262144, - "max_output": 32768, - "reasoning": true, - "tool_call": true - }, - "ling-3.0-flash-free": { - "context_window": 262144, - "max_output": 32768, - "reasoning": true, - "tool_call": true - }, - "ling-3.0-tiny-free": { - "context_window": 262144, - "max_output": 32768, - "reasoning": true, - "tool_call": true - }, - "longcat-2.0": { - "context_window": 1000000, - "max_output": 131072, - "reasoning": true, - "tool_call": true - }, - "longcat-2.0-free": { - "context_window": 1000000, - "max_output": 131072, - "reasoning": true, - "tool_call": true - }, - "mimo-v2-flash-free": { - "context_window": 262144, - "max_output": 65536, - "reasoning": true, - "tool_call": true - }, - "mimo-v2-omni": { - "context_window": 262144, - "max_output": 128000, - "reasoning": true, - "tool_call": true - }, - "mimo-v2-omni-free": { - "context_window": 262144, - "max_output": 64000, - "reasoning": true, - "tool_call": true - }, - "mimo-v2-pro": { - "context_window": 1048576, - "max_output": 128000, - "reasoning": true, - "tool_call": true - }, - "mimo-v2-pro-free": { - "context_window": 1048576, - "max_output": 64000, - "reasoning": true, - "tool_call": true - }, - "mimo-v2.5": { - "context_window": 1000000, - "max_output": 128000, - "reasoning": true, - "tool_call": true - }, - "mimo-v2.5-free": { - "context_window": 200000, - "max_output": 32000, - "reasoning": true, - "tool_call": true - }, - "mimo-v2.5-pro": { - "context_window": 1048576, - "max_output": 128000, - "reasoning": true, - "tool_call": true - }, - "minimax-m2.1": { - "context_window": 204800, - "max_output": 131072, - "reasoning": true, - "tool_call": true - }, - "minimax-m2.1-free": { - "context_window": 204800, - "max_output": 131072, - "reasoning": true, - "tool_call": true - }, - "minimax-m2.5": { - "context_window": 204800, - "max_output": 131072, - "reasoning": true, - "tool_call": true - }, - "minimax-m2.5-free": { - "context_window": 204800, - "max_output": 131072, - "reasoning": true, - "tool_call": true - }, - "minimax-m2.7": { - "context_window": 204800, - "max_output": 131072, - "reasoning": true, - "tool_call": true - }, - "minimax-m3": { - "context_window": 512000, - "max_output": 128000, - "reasoning": true, - "tool_call": true - }, - "minimax-m3-free": { - "context_window": 200000, - "max_output": 32000, - "reasoning": true, - "tool_call": true - }, - "muse-spark-1.2": { - "context_window": 1048576, - "max_output": 131072, - "reasoning": true, - "tool_call": true, - "structured_output": true - }, - "muse-spark-1.2-contributor": { - "context_window": 1048576, - "max_output": 131072, - "reasoning": true, - "tool_call": true, - "structured_output": true - }, - "muse-spark-1.2-contributor-free": { - "context_window": 1048576, - "max_output": 131072, - "reasoning": true, - "tool_call": true, - "structured_output": true - }, - "muse-spark-1.3-contributor": { - "context_window": 1048576, - "max_output": 131072, - "reasoning": true, - "tool_call": true, - "structured_output": true - }, - "muse-spark-1.3-contributor-free": { - "context_window": 1048576, - "max_output": 131072, - "reasoning": true, - "tool_call": true, - "structured_output": true - }, - "nemotron-3-super-free": { - "context_window": 204800, - "max_output": 128000, - "reasoning": true, - "tool_call": true - }, - "nemotron-3-ultra-free": { - "context_window": 1000000, - "max_output": 128000, - "reasoning": true, - "tool_call": true - }, - "nemotron-3.5-lightning-free": { - "context_window": 262144, - "max_output": 262144, - "reasoning": true, - "tool_call": true, - "structured_output": true - }, - "north-mini-code-free": { - "context_window": 256000, - "max_output": 64000, - "reasoning": true, - "tool_call": true, - "structured_output": true - }, - "ox-alpha-free": { - "context_window": 1000000, - "max_output": 131072, - "reasoning": true, - "tool_call": true, - "structured_output": true - }, - "qwen3-coder": { - "context_window": 262144, - "max_output": 65536, - "tool_call": true - }, - "qwen3.5-plus": { - "context_window": 262144, - "max_output": 65536, - "reasoning": true, - "tool_call": true - }, - "qwen3.6-plus": { - "context_window": 262144, - "max_output": 65536, - "reasoning": true, - "tool_call": true - }, - "qwen3.6-plus-free": { - "context_window": 262144, - "max_output": 65536, - "reasoning": true, - "tool_call": true - }, - "qwen3.7-max": { - "context_window": 1000000, - "max_output": 65536, - "reasoning": true, - "tool_call": true - }, - "qwen3.7-plus": { - "context_window": 1000000, - "max_output": 65536, - "reasoning": true, - "tool_call": true - }, - "qwen3.8-flash": { - "context_window": 1000000, - "max_output": 131072, - "reasoning": true, - "tool_call": true, - "structured_output": true - }, - "qwen3.8-max": { - "context_window": 1000000, - "max_output": 131072, - "reasoning": true, - "tool_call": true, - "structured_output": true - }, - "ring-2.6-1t-free": { - "context_window": 262000, - "max_output": 66000, - "reasoning": true, - "tool_call": true - }, - "trinity-large-preview-free": { - "context_window": 131072, - "max_output": 131072, - "tool_call": true - }, - "x-preview-f-free": { - "context_window": 1000000, - "max_output": 131072, - "reasoning": true, - "tool_call": true, - "structured_output": true - } - } -} diff --git a/gateway.go b/gateway.go index 84faf2e..87003be 100644 --- a/gateway.go +++ b/gateway.go @@ -211,10 +211,13 @@ func (g *Gateway) handleModels(w http.ResponseWriter, _ *http.Request) { if g.cfg.Anonymous && len(g.cfg.ZenKeys) == 0 && len(g.cfg.GoKeys) == 0 && !g.catalog.anonymousDecision(model).Allowed { continue } - if _, err := g.catalog.Route(model, len(g.cfg.ZenKeys) > 0, len(g.cfg.GoKeys) > 0, g.cfg.Anonymous); err != nil { + route, err := g.catalog.Route(model, len(g.cfg.ZenKeys) > 0, len(g.cfg.GoKeys) > 0, g.cfg.Anonymous) + if err != nil { continue } - md := g.catalog.Metadata(model) + // Tier-scoped metadata: the advertised context window must match + // the tier that will serve the request (anonymous ⇒ Zen). + md := g.catalog.MetadataForTier(model, route.Tier) entry := map[string]any{ "id": model, "object": "model", "created": now, "owned_by": "opencode", "metadata": md, diff --git a/models.go b/models.go index 9ea2001..37dfb34 100644 --- a/models.go +++ b/models.go @@ -88,7 +88,7 @@ type modelCatalog struct { // catalog. protocols remains the user-configured override map. nativeProtocols map[Tier]map[string]Protocol unsupported map[Tier]map[string]bool - modelMeta map[string]ModelMetadata + modelMeta map[Tier]map[string]ModelMetadata updatedAt time.Time prefer Tier metadata *modelMetadataStore @@ -108,18 +108,18 @@ type modelCatalogSnapshot struct { Stale bool `json:"stale"` } -const modelCatalogCacheSchemaVersion = 2 +const modelCatalogCacheSchemaVersion = 3 var modelCatalogCacheWriteMu sync.Mutex type modelCatalogCache struct { - SchemaVersion int `json:"schema_version"` - UpdatedAt time.Time `json:"updated_at"` - Zen []string `json:"zen"` - Go []string `json:"go"` - NativeProtocols map[Tier]map[string]Protocol `json:"native_protocols"` - Unsupported map[Tier]map[string]bool `json:"unsupported"` - Metadata map[string]ModelMetadata `json:"metadata,omitempty"` + SchemaVersion int `json:"schema_version"` + UpdatedAt time.Time `json:"updated_at"` + Zen []string `json:"zen"` + Go []string `json:"go"` + NativeProtocols map[Tier]map[string]Protocol `json:"native_protocols"` + Unsupported map[Tier]map[string]bool `json:"unsupported"` + Metadata map[Tier]map[string]ModelMetadata `json:"metadata,omitempty"` } func newModelCatalog(prefer Tier, overrides map[string]string) *modelCatalog { @@ -151,7 +151,7 @@ func (c *modelCatalog) Replace(zen, goModels []string) { c.ReplaceWithCapabilities(zen, goModels, nil, nil, nil) } -func (c *modelCatalog) ReplaceWithCapabilities(zen, goModels []string, native map[Tier]map[string]Protocol, unsupported map[Tier]map[string]bool, metadata map[string]ModelMetadata) { +func (c *modelCatalog) ReplaceWithCapabilities(zen, goModels []string, native map[Tier]map[string]Protocol, unsupported map[Tier]map[string]bool, metadata map[Tier]map[string]ModelMetadata) { c.mu.Lock() defer c.mu.Unlock() if zen != nil { @@ -449,14 +449,17 @@ func (c *modelCatalog) Supported(model string) bool { return c.supportedLocked(model) } -// Metadata returns the rich per-model metadata (context window, reasoning, -// tool call, modalities) captured from the opencode catalog. The zero value -// is returned for models the catalog does not describe (or before the first -// capability refresh). -func (c *modelCatalog) Metadata(model string) ModelMetadata { +// MetadataForTier returns the rich per-model metadata (context window, +// reasoning, tool call, modalities) captured from the opencode catalog for +// the tier that will actually serve the request. Same-named models can carry +// different limits per tier, so callers must pass route.Tier — never a +// tier-blind lookup. Anonymous routes always resolve to TierZen, which keeps +// the keyless path on Zen metadata. The zero value is returned for models +// the catalog does not describe (or before the first capability refresh). +func (c *modelCatalog) MetadataForTier(model string, tier Tier) ModelMetadata { c.mu.RLock() defer c.mu.RUnlock() - return c.modelMeta[model] + return c.modelMeta[tier][model] } func (c *modelCatalog) supportedLocked(model string) bool { @@ -511,13 +514,12 @@ func cloneBools(source map[string]bool) map[string]bool { return result } -func cloneModelMeta(source map[string]ModelMetadata) map[string]ModelMetadata { - if source == nil { - return nil - } - result := make(map[string]ModelMetadata, len(source)) - for id, md := range source { - result[id] = md +func cloneModelMeta(source map[Tier]map[string]ModelMetadata) map[Tier]map[string]ModelMetadata { + result := map[Tier]map[string]ModelMetadata{TierZen: {}, TierGo: {}} + for _, tier := range []Tier{TierZen, TierGo} { + for id, md := range source[tier] { + result[tier][id] = md + } } return result } @@ -705,7 +707,7 @@ func saveModelCatalogCache(path string, cache modelCatalogCache) error { type protocolCapabilities struct { Protocols map[Tier]map[string]Protocol Unsupported map[Tier]map[string]bool - Metadata map[string]ModelMetadata + Metadata map[Tier]map[string]ModelMetadata } type capabilityProvider struct { @@ -716,14 +718,18 @@ type capabilityProvider struct { } type capabilityModel struct { - ID string `json:"id"` - Provider *capabilityModelProvider `json:"provider"` - Limit *capabilityModelLimit `json:"limit"` - Reasoning bool `json:"reasoning"` - ToolCall bool `json:"tool_call"` - StructuredOutput bool `json:"structured_output"` - InputModalities []string `json:"input"` - OutputModalities []string `json:"output"` + ID string `json:"id"` + Provider *capabilityModelProvider `json:"provider"` + Limit *capabilityModelLimit `json:"limit"` + Reasoning bool `json:"reasoning"` + ToolCall bool `json:"tool_call"` + StructuredOutput bool `json:"structured_output"` + Modalities *capabilityModelModalities `json:"modalities"` +} + +type capabilityModelModalities struct { + Input []string `json:"input"` + Output []string `json:"output"` } type capabilityModelLimit struct { @@ -752,8 +758,10 @@ func (m *capabilityModel) metadata() ModelMetadata { Reasoning: m.Reasoning, ToolCall: m.ToolCall, StructuredOutput: m.StructuredOutput, - InputModalities: m.InputModalities, - OutputModalities: m.OutputModalities, + } + if m.Modalities != nil { + md.InputModalities = m.Modalities.Input + md.OutputModalities = m.Modalities.Output } if m.Limit != nil { md.ContextWindow = m.Limit.Context @@ -797,7 +805,7 @@ func fetchProtocolCapabilities(ctx context.Context, client *http.Client, endpoin result := protocolCapabilities{ Protocols: map[Tier]map[string]Protocol{TierZen: {}, TierGo: {}}, Unsupported: map[Tier]map[string]bool{TierZen: {}, TierGo: {}}, - Metadata: map[string]ModelMetadata{}, + Metadata: map[Tier]map[string]ModelMetadata{TierZen: {}, TierGo: {}}, } for providerID, provider := range providers { tier, ok := capabilityTier(providerID, provider.API) @@ -817,7 +825,7 @@ func fetchProtocolCapabilities(ctx context.Context, client *http.Client, endpoin } else { result.Unsupported[tier][modelID] = true } - result.Metadata[modelID] = model.metadata() + result.Metadata[tier][modelID] = model.metadata() } } // The machine catalog is the primary source. The upstream endpoint tables diff --git a/models_test.go b/models_test.go new file mode 100644 index 0000000..b5586e3 --- /dev/null +++ b/models_test.go @@ -0,0 +1,194 @@ +package main + +import ( + "context" + "encoding/json" + "io" + "net/http" + "os" + "path/filepath" + "strings" + "testing" + "time" +) + +// fixtureCatalog mirrors the real models.opencode.ai/api.json shape: providers +// keyed by ID, each model carrying limit/reasoning/flags plus a NESTED +// modalities object. tier-split-model intentionally carries different limits +// per tier, reproducing the glm-5/minimax-m3 class of bug where a tier-blind +// metadata map advertises the wrong tier's context window. +const fixtureCatalog = `{ + "opencode": { + "id": "opencode", + "api": "https://opencode.ai/zen/v1", + "npm": "@ai-sdk/openai-compatible", + "models": { + "tier-split-model": { + "id": "tier-split-model", + "limit": {"context": 1000000, "input": 990000, "output": 128000}, + "reasoning": true, + "tool_call": true, + "structured_output": false, + "modalities": {"input": ["text", "image"], "output": ["text"]} + }, + "zen-only-model": { + "limit": {"context": 200000, "output": 32000}, + "reasoning": false, + "tool_call": true, + "structured_output": true, + "modalities": {"input": ["text"], "output": ["text"]} + } + } + }, + "opencode-go": { + "id": "opencode-go", + "api": "https://opencode.ai/go/v1", + "npm": "@ai-sdk/openai-compatible", + "models": { + "tier-split-model": { + "id": "tier-split-model", + "limit": {"context": 200000, "input": 190000, "output": 32000}, + "reasoning": true, + "tool_call": true, + "structured_output": false, + "modalities": {"input": ["text"], "output": ["text"]} + } + } + } +}` + +// fixtureRoundTripper serves the catalog fixture for the capability endpoint +// and fails everything else immediately, keeping protocol-docs fallback +// fetches hermetic instead of hitting the network. +type fixtureRoundTripper struct { + catalogURL string +} + +func (f fixtureRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { + if req.URL.String() != f.catalogURL { + return nil, &testDNSError{url: req.URL.String()} + } + return &http.Response{ + StatusCode: http.StatusOK, + Body: io.NopCloser(strings.NewReader(fixtureCatalog)), + Header: make(http.Header), + }, nil +} + +type testDNSError struct{ url string } + +func (e *testDNSError) Error() string { return "blocked test request to " + e.url } + +func testParseCapabilities(t *testing.T) protocolCapabilities { + t.Helper() + const catalogURL = "https://example.invalid/api.json" + client := &http.Client{Transport: fixtureRoundTripper{catalogURL: catalogURL}} + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + caps, err := fetchProtocolCapabilities(ctx, client, catalogURL) + if err != nil { + t.Fatalf("fetchProtocolCapabilities: %v", err) + } + return caps +} + +func TestNestedModalitiesParsed(t *testing.T) { + caps := testParseCapabilities(t) + md := caps.Metadata[TierZen]["tier-split-model"] + if len(md.InputModalities) != 2 || md.InputModalities[0] != "text" || md.InputModalities[1] != "image" { + t.Fatalf("zen input modalities = %v, want [text image]", md.InputModalities) + } + if len(md.OutputModalities) != 1 || md.OutputModalities[0] != "text" { + t.Fatalf("zen output modalities = %v, want [text]", md.OutputModalities) + } + if !md.Reasoning || !md.ToolCall || md.StructuredOutput { + t.Fatalf("zen flags = %+v, want reasoning+tool_call", md) + } +} + +func TestTierSplitMetadataKeptSeparate(t *testing.T) { + caps := testParseCapabilities(t) + zen := caps.Metadata[TierZen]["tier-split-model"] + goMd := caps.Metadata[TierGo]["tier-split-model"] + if zen.ContextWindow != 1000000 { + t.Fatalf("zen context = %d, want 1000000", zen.ContextWindow) + } + if goMd.ContextWindow != 200000 { + t.Fatalf("go context = %d, want 200000", goMd.ContextWindow) + } + if zen.MaxOutput != 128000 || goMd.MaxOutput != 32000 { + t.Fatalf("max_output zen=%d go=%d, want 128000/32000", zen.MaxOutput, goMd.MaxOutput) + } +} + +func TestMetadataForTierSelection(t *testing.T) { + caps := testParseCapabilities(t) + cat := newModelCatalog(TierZen, nil) + cat.ReplaceWithCapabilities( + []string{"tier-split-model"}, []string{"tier-split-model"}, + caps.Protocols, caps.Unsupported, caps.Metadata, + ) + if got := cat.MetadataForTier("tier-split-model", TierZen).ContextWindow; got != 1000000 { + t.Fatalf("zen selection = %d, want 1000000", got) + } + if got := cat.MetadataForTier("tier-split-model", TierGo).ContextWindow; got != 200000 { + t.Fatalf("go selection = %d, want 200000", got) + } + if got := cat.MetadataForTier("missing-model", TierZen).ContextWindow; got != 0 { + t.Fatalf("unknown model = %d, want zero value", got) + } +} + +func TestMetadataCacheRoundTripV3(t *testing.T) { + caps := testParseCapabilities(t) + cat := newModelCatalog(TierZen, nil) + cat.SetCachePath(filepath.Join(t.TempDir(), "cache.models.catalog.json")) + cat.ReplaceWithCapabilities( + []string{"tier-split-model", "zen-only-model"}, []string{"tier-split-model"}, + caps.Protocols, caps.Unsupported, caps.Metadata, + ) + if err := cat.SaveCache(); err != nil { + t.Fatalf("SaveCache: %v", err) + } + restored := newModelCatalog(TierZen, nil) + if err := restored.LoadCache(cat.cachePath); err != nil { + t.Fatalf("LoadCache: %v", err) + } + if got := restored.MetadataForTier("tier-split-model", TierGo).ContextWindow; got != 200000 { + t.Fatalf("restored go context = %d, want 200000", got) + } + restoredMd := restored.MetadataForTier("tier-split-model", TierZen) + if len(restoredMd.InputModalities) != 2 || restoredMd.MaxOutput != 128000 { + t.Fatalf("restored zen metadata = %+v, want modalities+128k output", restoredMd) + } +} + +func TestLegacyV2CacheRejected(t *testing.T) { + // A pre-tier-split cache (flat metadata map, schema 2) must be refused + // rather than misread: flat entries cannot be attributed to a tier. + path := filepath.Join(t.TempDir(), "v2.models.catalog.json") + legacy := map[string]any{ + "schema_version": 2, + "updated_at": time.Now().UTC().Format(time.RFC3339Nano), + "zen": []string{"tier-split-model"}, + "go": []string{}, + "metadata": map[string]any{ + "tier-split-model": map[string]any{"context_window": 1000000}, + }, + } + raw, err := json.Marshal(legacy) + if err != nil { + t.Fatal(err) + } + if err := os.WriteFile(path, raw, 0600); err != nil { + t.Fatal(err) + } + cat := newModelCatalog(TierZen, nil) + // The flat v2 metadata shape cannot even decode into the per-tier v3 + // struct, so rejection surfaces as a decode error before the explicit + // version gate. Either way the stale cache is refused and the gateway + // falls back to a live refresh — it must never be half-read. + if err := cat.LoadCache(path); err == nil { + t.Fatal("LoadCache accepted schema v2, want rejection") + } +}