Files
magpie/internal/provider/key_models_refresh.go
T
yetone 7661a0aa72 provider: Fetch models ticks each key's own models, the first key's too (young02036)
young02036 on Discord (#feedback, v0.1.2965): "点击供应商的获取模型,给这个
供应商的所有账号默认勾选上可用模型,和新增账号之后默认勾选上可用模型这个功能
好像失效了".

Cause: no later change broke it; the two commits that built it each left
keys out. 0431793db (v0.1.2674) gives a key its own list only when it is
added beside others and its list is short, and says "the keys it had keep
their lists". 1a67ca614 (v0.1.2942) carries on each Fetch only what a key's
list gained and lost, and only for a key that already had a list of its
own ("a key serving every model still does"). So every key on All models
stayed there through every Fetch: the provider's first key, keys added
before per-key lists, and a key whose list wasn't read in the 8s when it
was added. In the reporter's setup that is the key the provider was made
with, so the feature reads as gone.

Fix (refreshKeyModels): a key on All models whose fetched list leaves out
a model the provider's other keys list is given that list. A key whose
list has every model stays on All models, so later additions still reach
it; a lone key stays too. Keys with lists of their own work as before
(un-ticks and ticks kept).

The user's choice is never overridden: a key the user set to All models
(`accountmodels` with none, the picker's All models) is now remembered in
Provider.AccountModelsAll and left alone by Fetch. The editor's save keeps
it, a key replaced in place moves it (moveKeyed), accountSettings and
clone carry it, normalize drops a mark for a key that has a list.
Not covered: a key set back to All models before this release has no mark
and is narrowed once to its own list on the next Fetch; the user can
press All models again and it stays.

Tests:
- TestFetchTicksEachKeysListOnAllModels: keys saved with no lists are
  ticked on Fetch; a key set to All models before and after stays so
  through two fetches; an un-tick survives; a key whose list failed stays
  All models and gets its list once it reads.
- TestFetchLeavesALoneKeyOnAllModels.
- internal/gui TestFetchModelsTicksEveryKeysModels through the real
  routes: provider/save, provider/models, keys/add, accountmodels, the
  editor's save, against an httptest relay with a list per key.
- TestFetchRefreshesEachKeysModels and TestNewKeyTakesItsOwnModels pinned
  the old rule (sk-b / sk-old left on All models); they now expect the
  key's own list.

Broken by hand (mote): the old `was == nil -> continue` put back: the
new provider test, the GUI test (sk-a, the first key: [] want
[m1 m2 m3 m4]) and the two updated tests go red. The AccountModelsAll
check removed: sk-c/sk-d set to All models get narrowed, red. The editor
save not keeping AccountModelsAll: the GUI test's sk-c red.

Run (mote, at this tree): gofmt -l clean; go vet of the touched packages,
GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./... ok;
go test -tags nogui -count=1 -timeout 45m ./internal/provider ./internal/gui
./internal/cli ./internal/gateway ./internal/tui: all ok (provider 223s,
gui 138s, cli 106s, gateway 797s, tui 26s).
GUI: account-models, account-models-available, account-refresh,
gateway-key-models, gateway-key-models-redraw (each in Chromium and
WebKit, as the files run): 94 of 94 pass.

Rebased on origin/main 8e97e26d6 (google.go, migrate_zcode.go): ./internal/provider
run again at this head, ok (157s).

Not tried: a real relay with per-group keys; Windows and Linux test runs
(nothing OS-specific changed). No app.js change.
2026-11-05 00:11:02 +08:00

180 lines
5.3 KiB
Go

package provider
// Each key's own models, again on every fetch (young02036 on Discord): a
// provider of several keys, each at a group of a relay's that serves some
// of its models, had each key's list read only when it was added
// (PrefillNewKeys). Fetch models merged every key's list into the
// provider's, marking each model with the keys that list it, but a key's
// own models stayed what its list had when it was added: a model its
// group took on later was never sent to it, one its group gave up still
// was.
//
// A fetch now carries what each key's list gained and lost since the
// fetch before into that key's models: a model its list has now and
// didn't before is ticked for it, one it had and has no more is taken
// out. A model the user un-ticked for a key that its list had before and
// still has stays un-ticked, and one the user ticked that its list never
// had stays ticked: magpie's default never overrides what the user set.
//
// A key with no models of its own yet (All models: one added before its
// own list was asked for, the provider's first key, one whose list wasn't
// read in time when it was added) is given its own list, as a key added
// now is (PrefillNewKeys), when that list leaves out a model the
// provider's other keys list (young02036 on Discord: Fetch models ticks
// the models each key can serve). A key the user set back to every model
// (AccountModelsAll) stays so, and one whose list has every model the
// others list serves every model still, so the vendor's later additions
// reach it. A key with no marks now (one key on, one switched off, a
// list that failed with nothing kept) is unknown, and its models stay as
// they are.
import (
"slices"
"strings"
"github.com/yetone/magpie/internal/catalog"
)
// keyMarks are, by key fingerprint, the provider's own ids of the models
// its fetched list marks each key as seeing (catalog.Model.Keys).
func keyMarks(id string) map[string]map[string]bool {
ms, _, _ := catalog.Live(id)
ms = append(ms, catalog.LiveDrawers(id)...)
ms = append(ms, catalog.LiveVideomakers(id)...)
out := map[string]map[string]bool{}
for _, m := range ms {
for _, k := range m.Keys {
if out[k] == nil {
out[k] = map[string]bool{}
}
out[k][m.ID] = true
}
}
return out
}
// refreshKeyModels carries what each of the provider id's keys' lists
// gained and lost between before and now into the models it serves
// (AccountModels), gives a key with none of its own its list now when
// that is short of the others', and answers how many keys' models changed.
func refreshKeyModels(id string, before, now map[string]map[string]bool) int {
if len(now) == 0 {
return 0
}
p, err := Find(id)
if err != nil {
return 0
}
// every model the keys' lists have between them
every := map[string]bool{}
for _, set := range now {
for m := range set {
every[m] = true
}
}
var am map[string][]string
n := 0
for _, k := range p.KeysOn() {
kid := keyID(k.Key)
was := p.AccountModels[accountKey(kid)]
o, w := before[kid], now[kid]
if len(w) == 0 {
continue
}
var list []string
if was == nil {
// a key on every model, by magpie's default and not the
// user's, whose list leaves some of the others' out
if p.AccountModelsAll[accountKey(kid)] || len(w) >= len(every) {
continue
}
for m := range w {
list = append(list, p.ListedModelID(m))
}
slices.Sort(list)
} else {
if len(o) == 0 {
continue
}
for _, m := range was {
own := p.OwnModelID(m)
if o[own] && !w[own] {
continue // its list had it and has no more
}
list = append(list, m)
}
var gained []string
for m := range w {
if !o[m] {
gained = append(gained, m)
}
}
slices.Sort(gained)
for _, m := range gained {
if !slices.ContainsFunc(list, func(x string) bool { return x == m || p.OwnModelID(x) == m }) {
list = append(list, p.ListedModelID(m))
}
}
}
if len(list) == 0 || slices.Equal(list, was) {
continue // nothing left would read as every model: keep it
}
if am == nil {
am = map[string][]string{}
}
am[accountKey(kid)] = list
n++
}
if n == 0 {
return 0
}
// read again: the user may have set a key's models while the lists
// were asked for, and what they set stands
q, err := Find(id)
if err != nil {
return 0
}
merged := make(map[string][]string, len(q.AccountModels))
for k, v := range q.AccountModels {
merged[k] = v
}
n = 0
for k, v := range am {
if !slices.Equal(q.AccountModels[k], p.AccountModels[k]) || q.AccountModelsAll[k] != p.AccountModelsAll[k] {
continue
}
merged[k] = v
n++
}
if n == 0 {
return 0
}
q.AccountModels = merged
if Save(*q) != nil {
return 0
}
return n
}
// KeyListed is the models the provider id's last fetched list marks its
// key ref (KeyID) as seeing, by the ids the provider lists them by, for
// the editor's Select all available (young02036 on Discord); nil when no
// list marks that key (one key on, a fetch not asked per key), which is
// unknown, not none.
func KeyListed(id, ref string) []string {
p, err := Find(id)
if err != nil {
return nil
}
seen := keyMarks(id)[strings.ToLower(strings.TrimSpace(ref))]
if len(seen) == 0 {
return nil
}
out := make([]string, 0, len(seen))
for m := range seen {
out = append(out, p.ListedModelID(m))
}
slices.Sort(out)
return out
}