diff --git a/internal/catalog/catalog.go b/internal/catalog/catalog.go index b0e55b844..5b968ae8c 100644 --- a/internal/catalog/catalog.go +++ b/internal/catalog/catalog.go @@ -660,6 +660,7 @@ func loadGen() (map[string]mdProvider, int) { } var m map[string]mdProvider if json.Unmarshal(b, &m) == nil && len(m) > 0 { + shareCatalog(m) mdev = m votes, reasons := map[string]int{}, map[string]int{} named := map[string]map[string]int{} diff --git a/internal/catalog/catalog_share.go b/internal/catalog/catalog_share.go new file mode 100644 index 000000000..80f01f7c9 --- /dev/null +++ b/internal/catalog/catalog_share.go @@ -0,0 +1,71 @@ +package catalog + +import ( + "slices" + "strconv" + "strings" +) + +// shareCatalog has what the loaded catalog's models repeat held once: the +// JSON gives each of ~9k models its own copy of "text", "image", its +// family, its release date, its levels and its SDK's name, and the catalog +// is kept while anything reads it (#2278). A list is shared clipped, so an +// append to one copies it rather than writing into another model's. +func shareCatalog(m map[string]mdProvider) { + strs := map[string]string{} + str := func(s string) string { + if s == "" { + return "" + } + if k, ok := strs[s]; ok { + return k + } + strs[s] = s + return s + } + lists := map[string][]string{} + list := func(l []string) []string { + if l == nil { + return nil + } + k := strconv.Itoa(len(l)) + "\x00" + strings.Join(l, "\x00") + if v, ok := lists[k]; ok { + return v + } + for i := range l { + l[i] = str(l[i]) + } + l = slices.Clip(l) + lists[k] = l + return l + } + sdks := map[string]any{} + for pid, p := range m { + p.Env = list(p.Env) + for id, x := range p.Models { + if x.ID == id { + x.ID = id // the map's key already holds it + } + x.Name, x.ReleaseDate, x.Family, x.Canonical = str(x.Name), str(x.ReleaseDate), str(x.Family), str(x.Canonical) + x.Modalities.Input, x.Modalities.Output = list(x.Modalities.Input), list(x.Modalities.Output) + for i := range x.Reasoning { + r := &x.Reasoning[i] + r.Type, r.Values = str(r.Type), list(r.Values) + } + if x.Provider != nil { + x.Provider = sharedOf(sdks, x.Provider.NPM, x.Provider) + } + p.Models[id] = x + } + m[pid] = p + } +} + +// sharedOf is the value first kept under k, keeping v when there is none. +func sharedOf[T any](kept map[string]any, k string, v T) T { + if was, ok := kept[k]; ok { + return was.(T) + } + kept[k] = v + return v +} diff --git a/internal/catalog/catalog_share_test.go b/internal/catalog/catalog_share_test.go new file mode 100644 index 000000000..0a62acb17 --- /dev/null +++ b/internal/catalog/catalog_share_test.go @@ -0,0 +1,57 @@ +package catalog + +import ( + "testing" + "unsafe" +) + +// The loaded catalog holds what its models repeat once: two models with +// the same modalities, family, release date, levels and SDK share them. +func TestLoadedCatalogSharesRepeats(t *testing.T) { + writeCatalog(t, `{ + "openai": {"models": { + "a": {"id":"a","name":"A","family":"gpt","release_date":"2026-01-01", + "modalities":{"input":["text","image"],"output":["text"]}, + "reasoning_options":[{"type":"effort","values":["low","high"]}], + "provider":{"npm":"@ai-sdk/openai"}}, + "b": {"id":"b","name":"B","family":"gpt","release_date":"2026-01-01", + "modalities":{"input":["text","image"],"output":["text"]}, + "reasoning_options":[{"type":"effort","values":["low","high"]}], + "provider":{"npm":"@ai-sdk/openai"}} + }}, + "other": {"models": { + "c": {"id":"c","name":"C","family":"gpt","modalities":{"input":["text"],"output":["text"]}} + }} + }`) + m, _ := loadGen() + a, b, c := m["openai"].Models["a"], m["openai"].Models["b"], m["other"].Models["c"] + same := func(x, y string) bool { return unsafe.StringData(x) == unsafe.StringData(y) } + if !same(a.Family, b.Family) || !same(a.Family, c.Family) || !same(a.ReleaseDate, b.ReleaseDate) { + t.Error("each model has its own copy of its family and release date") + } + if &a.Modalities.Input[0] != &b.Modalities.Input[0] || &a.Modalities.Output[0] != &b.Modalities.Output[0] { + t.Error("each model has its own modalities list") + } + if !same(a.Modalities.Output[0], c.Modalities.Input[0]) { + t.Error("each list has its own copy of a modality") + } + if &a.Reasoning[0].Values[0] != &b.Reasoning[0].Values[0] || !same(a.Reasoning[0].Type, b.Reasoning[0].Type) { + t.Error("each model has its own levels") + } + if a.Provider != b.Provider { + t.Error("each model has its own SDK") + } + // a shared list is clipped: an append copies it + in := a.Modalities.Input + if cap(in) != len(in) { + t.Fatalf("shared list has room past its end: len %d cap %d", len(in), cap(in)) + } + _ = append(in, "pdf") + if len(b.Modalities.Input) != 2 { + t.Error("an append to one model's list changed another's") + } + // what is read through it is as before + if a.Name != "A" || b.Name != "B" || c.Modalities.Input[0] != "text" || a.Reasoning[0].Values[1] != "high" || a.Provider.NPM != "@ai-sdk/openai" { + t.Errorf("values changed: %+v %+v %+v", a, b, c) + } +}