Files
magpie/internal/gui/lastproviders.go
T
yetone 92fbabb47e gui: the Providers page draws the last list at once and the fresh one behind it
#1519 (fatkun): the Providers page asked /api/providers each time it
opened, and after a start that read takes seconds (every account's
sign-in, every agent's model), so the page showed skeletons that long.

Now, as the Agents page draws the last state:
- the app reads /api/providers once in the background after the start
  (prefetchProviders), and the page's first opening uses that read, or
  waits on it, instead of asking again. The sidebar's gateway line
  reuses the same read (sideGwRead), so a start still makes one read.
- the server keeps the list it last built in the cache folder
  (providers-state.json, 0600; lastproviders.go) without the gateway's
  recent calls and lanes or any provider's headers, proxy or extra body.
  /api/providers?last=1 serves it at once; the page draws it while the
  fresh read is out, and the fresh one replaces it.
- a row clicked on a kept list waits for the fresh one rather than
  opening an editor that would be drawn again under the reader's hands.

Tests:
- TestLastKnownProviders (internal/gui): red when the stripping of
  Headers/Proxy was broken by hand ("the kept list holds hdr-secret-1519
  / proxy-secret-1519"), green restored.
- providers-last-known.test.cjs (new): the kept list drawn before the
  fresh read answers, then replaced; a row click waits. With
  kept.last = false by hand the row-click case was red in all 5
  languages.
- sidebar.test.cjs: the narrow case now guards one /api/providers read
  in total, still one after widening to 1200px (the sidebar reuses it).
  Red on the old assets (0 reads), green on the new, both engines.

Run:
  go test -timeout 45m ./internal/gui
  ok  	github.com/yetone/magpie/internal/gui	528.667s
  gofmt -l internal/: nothing; go vet ./internal/gui, GOOS=linux and
  GOOS=windows go vet -tags nogui ./...: exit 0
  GUI, chromium and webkit, every language, wide and narrow:
  providers-last-known 20/20, sidebar 8/8, provider-proxy 4/4,
  agents-last-known 3/3, last-page 1/1 in each engine.
  provider-advanced-fold: 1-3 of 25 red per run, the same on the old
  assets (#2552).
  Chromium full GUI suite (before the kept.last and sidebar-test edits),
  under load ~200: 5681 tests, 55 red in 23 files; each was rerun alone
  and either passed or was as red on the old assets. routing-model-rest
  runs, old vs new: old 4, 2, 7 red, new 3, 3 red; routing-request-
  metrics two runs: old 1, 0 red, new 0, 2 red, each red at 23-29s
  (load ~240; the same test red on old).

Not verified: a real cold start on Windows; the webkit full suite (59
provider/sidebar/start files run: 516 tests, the reds there pass alone or
are #1863).
2026-10-20 20:23:56 +08:00

93 lines
3.0 KiB
Go

package gui
import (
"bytes"
"encoding/json"
"os"
"path/filepath"
"sync"
"github.com/yetone/magpie/internal/appdir"
"github.com/yetone/magpie/internal/edit"
)
// The last-known providers list (#1519, fatkun): the Providers page asked
// /api/providers each time it opened, and after a start that read took
// seconds (every account's sign-in, every agent's model), so the page
// showed skeletons for that long. As the Agents page draws the last state
// (laststate.go), it now draws the list built last, kept here and in the
// cache folder for the next start, and the fresh one replaces it the
// moment it is in.
//
// What is kept leaves out what the page never needs before the fresh list
// is in and what can hold a secret: the gateway's recent calls and lanes,
// each provider's headers, proxies and extra body. The page opens no
// provider's editor from the kept list; it waits for the fresh one.
var lastProviders struct {
sync.Mutex
kept []byte
read bool
}
type providersFile struct {
Version string `json:"version"`
Providers providersJSON `json:"providers"`
}
func lastProvidersPath() string { return filepath.Join(appdir.Cache(), "providers-state.json") }
// keepProviders remembers s as the last-known list, written only when what
// is kept changed.
func keepProviders(s providersJSON) {
s.Gateway.Calls, s.Gateway.Lanes = nil, nil
s.Moved, s.Added, s.Had, s.Removed, s.Fetching = nil, 0, 0, 0, false
ps := make([]providerJSON, len(s.Providers))
for i, p := range s.Providers {
p.Headers, p.ExtraBody, p.Proxy, p.AccountProxies = nil, nil, "", nil
ps[i] = p
}
s.Providers = ps
b, err := json.Marshal(providersFile{Version: Version, Providers: s})
if err != nil {
return
}
lastProviders.Lock()
defer lastProviders.Unlock()
if !lastProviders.read {
lastProviders.kept, _ = os.ReadFile(lastProvidersPath())
lastProviders.read = true
}
if bytes.Equal(b, lastProviders.kept) {
return
}
lastProviders.kept = b
// only this user's to read: it names their accounts
if _, err := os.Stat(lastProvidersPath()); os.IsNotExist(err) {
os.MkdirAll(filepath.Dir(lastProvidersPath()), 0o700)
os.WriteFile(lastProvidersPath(), nil, 0o600)
}
_ = edit.WriteAtomic(lastProvidersPath(), b)
}
// lastKnownProviders is the last list built, by this run or the one
// before it, as kept; false when none is kept, or another version kept it.
func lastKnownProviders() (json.RawMessage, bool) {
lastProviders.Lock()
if !lastProviders.read {
lastProviders.kept, _ = os.ReadFile(lastProvidersPath())
lastProviders.read = true
}
b := lastProviders.kept
lastProviders.Unlock()
var f struct {
Version string `json:"version"`
Providers json.RawMessage `json:"providers"`
}
if len(b) == 0 || json.Unmarshal(b, &f) != nil || f.Version != Version || len(f.Providers) == 0 || f.Providers[0] != '{' {
return nil, false
}
// marked, for the page to tell it from a fresh one
return append(json.RawMessage(`{"last":true,`), f.Providers[1:]...), true
}