providers: the page finds each routing group's members once

providersState asked GroupFinder for a group's members once for every
agent on that group, and again for the gateway's group list, and each
ask built every member anew (a Member is 1408 bytes). groupsOnce keeps
what was found for the page's build; the page only reads it.
This commit is contained in:
yetone
2026-11-10 03:48:24 +08:00
parent 0ec28be0d4
commit fda52fe5dd
2 changed files with 54 additions and 1 deletions
+32
View File
@@ -0,0 +1,32 @@
package gui
import (
"testing"
"github.com/yetone/magpie/internal/provider"
)
// The Providers page finds each group's members once, however many agents
// are on it, and the gateway's list takes the same: each find built every
// member, a kilobyte and more each, again.
func TestGroupsFoundOncePerPage(t *testing.T) {
calls := map[string]int{}
find := groupsOnce(func(id string) (provider.Group, []provider.Member, bool) {
calls[id]++
if id != "group/a" {
return provider.Group{}, nil, false
}
return provider.Group{ID: "a"}, []provider.Member{{Model: "m"}}, true
})
for range 3 {
if g, ms, ok := find("group/a"); !ok || g.ID != "a" || len(ms) != 1 || ms[0].Model != "m" {
t.Fatalf("group/a: %v %v %v", g, ms, ok)
}
if _, _, ok := find("group/gone"); ok {
t.Fatal("a group not found was found")
}
}
if calls["group/a"] != 1 || calls["group/gone"] != 1 {
t.Errorf("finds: %v, want each once", calls)
}
}
+22 -1
View File
@@ -736,6 +736,27 @@ func agentUses(agents []*agent.Agent, findGroup func(string) (provider.Group, []
return out
}
// groupsOnce is find with each group's members found once: every agent
// on a group, and the gateway's list after them, asked for the same
// group's again, a Member over a kilobyte each (8 MB of forty polls of
// the Providers page with real accounts). The page only reads them.
func groupsOnce(find func(string) (provider.Group, []provider.Member, bool)) func(string) (provider.Group, []provider.Member, bool) {
type found struct {
g provider.Group
ms []provider.Member
ok bool
}
seen := map[string]found{}
return func(id string) (provider.Group, []provider.Member, bool) {
if f, ok := seen[id]; ok {
return f.g, f.ms, f.ok
}
g, ms, ok := find(id)
seen[id] = found{g, ms, ok}
return g, ms, ok
}
}
// presetOwnClient says a provider added from pr goes as a client of its
// vendor's own (provider.OwnClient): OpenCode's, Cline's, Kilo's, another
// magpie.
@@ -1237,7 +1258,7 @@ func providersState() providersJSON {
}
s.Excluded = append(s.Excluded, e)
}
findGroup := provider.GroupFinder()
findGroup := groupsOnce(provider.GroupFinder())
uses := agentUses(agents, findGroup)
desktop := desktopNumbers(uses)
have := map[string]bool{}