Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

gateway: GET / counts the catalog once a change, not on every ask

yetone318697dcea4e
diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go
index 4ce78f92f..417bbdef2 100644
--- a/internal/gateway/gateway.go
+++ b/internal/gateway/gateway.go
@@ -722,8 +722,15 @@ func responsesOverHTTP(w http.ResponseWriter, r *http.Request) {
writeError(w, provider.Responses, http.StatusMethodNotAllowed, "POST /v1/responses")
}
+// catalogSize is how many models the catalog has, as info says, kept by
+// provider.StampNow: every magpie on this computer asks another which
+// it is (ServedBy, a window's every 15s, each look at its state while it
+// doesn't serve), and each question built the whole catalog to count it,
+// 5.4 GB of a day's 36 GB of allocations.
+var catalogSize stamped[struct{}, int]
+
func (s *Server) info(w http.ResponseWriter, r *http.Request) {
- out := map[string]any{"name": "magpie", "version": Version, "models": len(provider.Catalog()), "window": Window,
+ out := map[string]any{"name": "magpie", "version": Version, "models": catalogSize.get(struct{}{}, func() int { return len(provider.Catalog()) }), "window": Window,
"apis": []string{"/v1/chat/completions", "/v1/responses", "/v1/messages", "/v1/systemone", "/v1beta/models/{model}:generateContent", "/v1/images/generations", "/v1/images/edits", "/v1/videos", "/v1/embeddings", "/v1/rerank", "/v1/audio/transcriptions", "/v1/audio/translations", "/v1/magpie/quotas", "/v1/magpie/quotas/history", "/v1/magpie/usage", "/v1/magpie/usage/requests", "/v1/magpie/route", "/v1/magpie/trace", SearchMCPPath, VisionMCPPath, ToolsMCPPath}}
// which process serves, for another magpie on this computer to name it
// on its Routing page (#2606); not told to another machine
diff --git a/internal/gateway/info_count_test.go b/internal/gateway/info_count_test.go
new file mode 100644
index 000000000..1f97f3bb9
--- /dev/null
+++ b/internal/gateway/info_count_test.go
@@ -0,0 +1,49 @@
+package gateway
+
+import (
+ "net/http"
+ "net/http/httptest"
+ "testing"
+
+ "github.com/yetone/magpie/internal/provider"
+)
+
+// info's count of the models is kept, and counted again once a provider
+// is added. Another magpie asks every 15s or so, after the catalog a
+// request held (provider.Hold) has gone: info is asked here outside one.
+func TestInfoCountsModelsOnceAChange(t *testing.T) {
+ var s *Server
+ count := func() float64 {
+ r := httptest.NewRequest(http.MethodGet, "/", nil)
+ r.RemoteAddr = "127.0.0.1:5555"
+ w := httptest.NewRecorder()
+ s.info(w, r)
+ var out struct{ Models float64 }
+ if err := decodeJSON(w.Body.Bytes(), &out); err != nil {
+ t.Fatal(err)
+ }
+ return out.Models
+ }
+ // asked again with nothing changed, it costs the same whatever the
+ // lists' length
+ allocs := func(n int) float64 {
+ manyEyes(t, n, true)
+ s = New()
+ if m := count(); m != float64(len(provider.Catalog())) {
+ t.Fatalf("info says %v models of %d", m, len(provider.Catalog()))
+ }
+ return testing.AllocsPerRun(20, func() { count() })
+ }
+ few, many := allocs(4), allocs(400)
+ t.Logf("allocations a GET /: %.0f with 4 models listed, %.0f with 400", few, many)
+ if many > few+50 {
+ t.Fatalf("GET / made %.0f allocations with 400 models listed, %.0f with 4: the catalog is built again", many, few)
+ }
+ n := count()
+ if err := provider.Save(provider.Provider{ID: "more", Name: "More", Chat: "http://more.invalid/v1", Key: "k", Models: []string{"a", "b"}}); err != nil {
+ t.Fatal(err)
+ }
+ if m := count(); m != float64(len(provider.Catalog())) || m == n {
+ t.Fatalf("info says %v models after a provider was added, of %d", m, len(provider.Catalog()))
+ }
+}