mirror of
https://github.com/jasonxu114514/opencode2api.git
synced 2026-12-05 08:02:53 +00:00
Merge remote-tracking branch 'origin/pr-17'
This commit is contained in:
commit
bd9ced463a
3 files changed
+129
-14
No files matched your search
@@ -7,3 +7,4 @@ opencode2api.exe
|
||||
*.log
|
||||
.tmp/
|
||||
.gocache/
|
||||
*.models.catalog.json
|
||||
+33
-3
@@ -211,10 +211,40 @@ 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
|
||||
}
|
||||
data = append(data, map[string]any{"id": model, "object": "model", "created": now, "owned_by": "opencode"})
|
||||
// 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,
|
||||
}
|
||||
// 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})
|
||||
}
|
||||
@@ -944,7 +974,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)
|
||||
|
||||
@@ -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[Tier]map[string]ModelMetadata
|
||||
updatedAt time.Time
|
||||
prefer Tier
|
||||
metadata *modelMetadataStore
|
||||
@@ -107,17 +108,18 @@ type modelCatalogSnapshot struct {
|
||||
Stale bool `json:"stale"`
|
||||
}
|
||||
|
||||
const modelCatalogCacheSchemaVersion = 1
|
||||
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"`
|
||||
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 {
|
||||
@@ -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[Tier]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,19 @@ func (c *modelCatalog) Supported(model string) bool {
|
||||
return c.supportedLocked(model)
|
||||
}
|
||||
|
||||
// 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[tier][model]
|
||||
}
|
||||
|
||||
func (c *modelCatalog) supportedLocked(model string) bool {
|
||||
if len(c.zen) == 0 && len(c.goModels) == 0 {
|
||||
return true
|
||||
@@ -492,6 +514,16 @@ func cloneBools(source map[string]bool) map[string]bool {
|
||||
return result
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func sortedSetKeys(source map[string]bool) []string {
|
||||
result := make([]string, 0, len(source))
|
||||
for model, available := range source {
|
||||
@@ -675,6 +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[Tier]map[string]ModelMetadata
|
||||
}
|
||||
|
||||
type capabilityProvider struct {
|
||||
@@ -685,8 +718,57 @@ 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"`
|
||||
Modalities *capabilityModelModalities `json:"modalities"`
|
||||
}
|
||||
|
||||
type capabilityModelModalities struct {
|
||||
Input []string `json:"input"`
|
||||
Output []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,
|
||||
}
|
||||
if m.Modalities != nil {
|
||||
md.InputModalities = m.Modalities.Input
|
||||
md.OutputModalities = m.Modalities.Output
|
||||
}
|
||||
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 +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[Tier]map[string]ModelMetadata{TierZen: {}, TierGo: {}},
|
||||
}
|
||||
for providerID, provider := range providers {
|
||||
tier, ok := capabilityTier(providerID, provider.API)
|
||||
@@ -742,6 +825,7 @@ func fetchProtocolCapabilities(ctx context.Context, client *http.Client, endpoin
|
||||
} else {
|
||||
result.Unsupported[tier][modelID] = true
|
||||
}
|
||||
result.Metadata[tier][modelID] = model.metadata()
|
||||
}
|
||||
}
|
||||
// The machine catalog is the primary source. The upstream endpoint tables
|
||||
|
||||
Reference in new issue
Block a user