gateway: the Providers page's drawers come from the list it has

Each /api/providers poll asked gateway.Drawers for each provider's image
models, and Drawers read the provider's model list again (p.Available)
though providerInfoWith already had it in the kept Listing. In a
real-account heap profile (20 warm rounds of drift, state and providers)
Drawers was 14.4 MB of what was allocated.

DrawersOf takes the list as a func, called only on the paths that read
it (a Google sign-in's, a plugin's), and the Providers page passes its
Listing's Available, which is p.Available() kept (Provider.Listing).
Drawers itself is DrawersOf(p, p.Available) for every other caller.

Real use: the owner's real-account sandbox, offline (proxy to :9), 20
warm rounds of /api/drift, /api/state and /api/providers, at this head:
- Drawers 14.4 MB -> DrawersOf 3.1 MB.
- /api/state and /api/drift bytes identical to 6712e367e's (cmp);
  /api/providers differs only in each provider's "fetched" time.

Run on mote (mote-run, Go 1.26.8):
- gofmt clean; go vet ok, also GOOS=windows and GOOS=linux.
- TestDrawersOfTheCallersList alone, 3 times: ok.
- Broken by hand (DrawersOf reading p.Available()): red, "drawers [],
  want gemini-3.1-flash-image alone".
- go test -timeout 45m ./internal/gateway: ok (669s). ./internal/gui:
  FAIL on TestSettingsSaveKeepsWhatItDoesNotSend (#4514) and
  TestMainWindowCorners (#3949), both red on main and filed. That run
  was at dd7442f28; this head is it rebased on 6712e367e (the stash and a
  Claude account-switch change, neither in gateway's draw path), built
  and run for the numbers above.
This commit is contained in:
yetone
2026-11-01 05:35:21 +08:00
parent 6712e367e8
commit 6a7a690be0
3 changed files with 36 additions and 4 deletions
+9 -3
View File
@@ -266,7 +266,13 @@ func drawsCodex(p provider.Provider) bool {
// own picks named for images, and those the user said draw (#1399), less
// those they said make videos; a ChatGPT account's GPT Image, a Grok
// subscription's Grok Imagine, a WorkBuddy plan's those its config lists.
func Drawers(p provider.Provider) []catalog.Model {
func Drawers(p provider.Provider) []catalog.Model { return DrawersOf(p, p.Available) }
// DrawersOf is Drawers with p's model list from avail, called only when
// it is wanted: the Providers page has each provider's list at hand, and
// reading it again for each was 15 MB of what twenty window polls
// allocated. What avail gives is only read.
func DrawersOf(p provider.Provider, avail func() []catalog.Model) []catalog.Model {
if drawsWorkBuddy(p) {
return wbDrawers(p)
}
@@ -284,7 +290,7 @@ func Drawers(p provider.Provider) []catalog.Model {
// the image models a Google sign-in lists (Antigravity's
// gemini-3.1-flash-image), which draw on Code Assist (#757)
var out []catalog.Model
for _, m := range p.Available() {
for _, m := range avail() {
if catalog.DrawsID(m.ID) {
m.Provider = p.ID
out = append(out, m)
@@ -302,7 +308,7 @@ func Drawers(p provider.Provider) []catalog.Model {
// when it says it gives images or is named for them (XYenon's
// Google AI Studio plugin).
var out []catalog.Model
for _, m := range p.Available() {
for _, m := range avail() {
if p.ModelKind(m.ID) == provider.KindVideo {
continue
}
+26
View File
@@ -0,0 +1,26 @@
package gateway
import (
"testing"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/provider"
)
// The image models a Google sign-in can draw with come from the model list
// the caller has (the Providers page's), read once, not from a list read
// again for them.
func TestDrawersOfTheCallersList(t *testing.T) {
p := provider.Provider{ID: "antigravity", Account: &provider.Account{Agent: "antigravity"}}
calls := 0
got := DrawersOf(p, func() []catalog.Model {
calls++
return []catalog.Model{{ID: "gemini-3-pro"}, {ID: agDrawer}}
})
if len(got) != 1 || got[0].ID != agDrawer || got[0].Provider != "antigravity" {
t.Fatalf("drawers %+v, want %s alone", got, agDrawer)
}
if calls != 1 {
t.Fatalf("the list was read %d times", calls)
}
}
+1 -1
View File
@@ -930,7 +930,7 @@ func providerInfoWith(p provider.Provider, ls provider.Listing, agents []agentUs
out.Exposed = len(exposed)
// its image models aren't among those agents chat with; the editor
// lists them apart, as Settings → Images is where one is picked
for _, m := range gateway.Drawers(p) {
for _, m := range gateway.DrawersOf(p, func() []catalog.Model { return ls.Available }) {
out.DrawIDs = append(out.DrawIDs, m.ID)
}
out.Draws = len(out.DrawIDs)