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