Files
magpie/internal/gui/importapps.go
T
yetone 4976561743 provider, gui: a provider imported under a subscription's name comes in beside it, and a colliding name can be changed in the picker (#1487, Dudung1018)
Dudung1018's CC Switch had third-party providers named as magpie's
subscriptions are: a Pi proxy "workbuddy", an OpenCode relay "Claude".
Reproduced in a sandbox GUI with a CC Switch db in cc-switch's real
schema:
- the default import answered 400 "workbuddy is the id of the workbuddy
  subscription; pick another name", after some picks had already been
  written. That is why the reporter later saw "already added" and couldn't
  find where the key went.
- the "Replace it" choice on a row named "claude" wrote the relay's models
  as the Claude subscription's picks, and dropped its key and address.

What changed:
- importapps: a row whose id is a subscription's (signed in or not), or
  magpie's own, is marked reserved. It is picked like a new one and added
  under a free id beside the subscription. Replace is refused for it, and
  key mode never joins an account or a provider at another address.
- the picker has a name field on every row whose name collides. A renamed
  row is added under its new name, and Replace is no longer offered for it.
  "Add as another key" names the provider the key went to.
- import links (magpie://import, magpie import) named or id'd as a
  subscription get a free id.
- the new sub-line is in en, zh, zh-TW, ja and de.

What was run:
- sandbox GUI on the old build (the 400, the partial write, the Replace
  case), then on the new one. The same picks gave 200 ["Relay 1487",
  "Claude (findcg)", "workbuddy"]: findcg got one more key, and relay-1487
  and workbuddy-2 were added at their own addresses.
- importapps_reserved_test.go: 4 tests on the real cc-switch schema, all
  red on the old code.
- import-collision.test.cjs: chromium and webkit, in en/zh/zh-TW/ja/de, at
  900 and 390px, 20/20 passing and red on the old assets.
  provider-discovery.test.cjs still passes.
- go test -tags nogui ./... green, gofmt -l, and GOOS=linux and windows go
  vet for internal/provider and internal/gui.
2026-10-10 23:17:22 +08:00

86 lines
2.6 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{}
for _, s := range provider.ImportSources() {
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
}
added, err := provider.ImportFromApps(in.Picks)
if err != nil {
fail(rw, err)
return
}
writeJSON(rw, struct {
Added []string `json:"added"`
State any `json:"state"`
}{added, providersState()})
})
}