Files
magpie/internal/gui/importapps.go
T
yetone 0431793db4 provider: a key added beside others takes the models its own list has
Young on Discord: a provider of several keys, each of a relay group that
serves some of its models, had every key added or imported allow every
model, so the user narrowed each by hand, and requests went to a key
that can't serve the model.

A key added now (GUI Add key, Import keys, several keys pasted in the
provider's key field, and Import local configurations, CC Switch's
entries folded into one provider included) is asked for its own
/models, with that key alone, within the add's 8s wait and beside the
provider's own Fetch. What it answers becomes its AccountModels when it
leaves out a model the provider's other keys serve.

Only a new key with no models of its own is given a list: the keys a
provider had keep theirs (none, or the user's), and a pick the user
makes while the list is asked is kept. A key whose list can't be told
keeps every model, as before, and no empty list is written: a refused
key, an empty list, one past the wait, a base that didn't answer (its
part would be the provider's old list), a vendor with no list
(Preset.NoList, plan-only presets, Cline and Kilo's feeds), a provider
of one key, and a key that lists every model the others do (a list
would only hold back what the vendor adds later).

The CLI has no key add (`provider key` replaces the key), so it is
unchanged.

Run on mote at the pushed head (on origin/main fa6725ee7):
- go test -count=1 -timeout 45m ./internal/provider/: ok
- go test -count=1 -timeout 45m ./internal/gui/: ok but
  TestMainWindowCorners, red the same 3 of 3 on origin/main b3eb26400
  on mote's macOS 27 (#3949)
- GOOS=linux go vet -tags nogui ./internal/provider/ ./internal/gui/
  and GOOS=windows go vet ./...: clean
- gofmt -l internal/provider internal/gui: clean
Tests use a fake relay that answers per key (httptest), no network.

Broken by hand, each red:
- give existing keys a list too (drop the before check):
  TestNewKeyTakesItsOwnModels (2 keys given a list, want 1)
- take a list with a base that didn't answer:
  TestNewKeyHalfAnsweredKeepsAll
- give a list even when it has every model, and skip the Save:
  TestNewKeyTakesItsOwnModels / TestImportedKeysTakeTheirOwnModels

Not tried: a real relay account with per-group keys; the "replace key"
action (it keeps the key's slot); a model the vendor later adds to a
prefilled key's group is added by hand in the key's models, as with
any list. No GUI asset changed.
2026-11-02 11:09:11 +08:00

92 lines
2.9 KiB
Go

package gui
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"net/http"
"github.com/yetone/magpie/internal/provider"
)
// Bringing over the providers another app (CC Switch, Alma) has set up:
// the page lists them, keys masked, and posts back the ones the user
// picked; the keys are read again here, never sent to the page.
func importFingerprint(source string, it provider.AppImport) string {
p := it.Provider
p.ID = "" // settle's generated ID can change when another source is added
b, _ := json.Marshal(struct {
Source, Ref string
Provider provider.Provider
}{source, it.Ref, p})
sum := sha256.Sum256(b)
return hex.EncodeToString(sum[:])
}
func importAppsRoutes(mux *http.ServeMux) {
// Opaque fingerprints let the window remember dismissed configurations
// without keeping their credentials, endpoints or paths in localStorage.
mux.HandleFunc("GET /api/importapps/discovery", func(rw http.ResponseWriter, r *http.Request) {
type candidate struct {
Fingerprint string `json:"fingerprint"`
Source string `json:"source"`
}
out := []candidate{}
// only the apps looked in unasked: OpenCode waits for the picker
for _, s := range provider.DiscoverSources() {
for _, it := range s.Items {
// Match openImportApps' default selection: disabled entries and
// ID collisions without an add-key option need a manual choice.
if it.Skip == "" && it.Status != "same" && it.Off == "" && (it.Status != "taken" || it.KeyOf != "" || it.Reserved) {
out = append(out, candidate{importFingerprint(s.ID, it), s.Name})
}
}
}
writeJSON(rw, out)
})
mux.HandleFunc("GET /api/importapps", func(rw http.ResponseWriter, r *http.Request) {
type item struct {
provider.AppImport
Fingerprint string `json:"fingerprint"`
}
type source struct {
provider.AppSource
Items []item `json:"items"`
}
sources := []source{}
for _, s := range provider.ImportSources() {
out := source{AppSource: s, Items: []item{}}
for _, it := range s.Items {
fingerprint := importFingerprint(s.ID, it)
p := &it.Provider
p.Key, p.Keys = provider.Mask(p.Key), nil
out.Items = append(out.Items, item{it, fingerprint})
}
sources = append(sources, out)
}
writeJSON(rw, sources)
})
mux.HandleFunc("POST /api/importapps", func(rw http.ResponseWriter, r *http.Request) {
var in struct{ Picks []provider.AppPick }
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
fail(rw, err)
return
}
had := provider.KeysHeld()
added, err := provider.ImportFromApps(in.Picks)
// a key imported beside others is given the models its own list
// has, as one added by hand is (PrefillNewKeys); those an import
// that stopped part way added too
provider.PrefillNewKeys(r.Context(), had)
if err != nil {
fail(rw, err)
return
}
writeJSON(rw, struct {
Added []string `json:"added"`
State any `json:"state"`
}{added, providersState()})
})
}