Files
magpie/internal/provider/migrate_qoder.go
T
yetone 3bec6c0e40 providers: a built-in subscription moves onto its community plugin and back, its accounts with it (the user: 慢慢下掉内置的那些 Subscription Provider…不要让用户一升级完之后,突然发现他的所有服务都不可用了) — eleven built-ins (Zed, Qoder, Factory, MiMo, Devin, Grok, Command Code Plan, Cursor, Kiro, WorkBuddy, ZCode) each get a mover to their @magpie-community package. A move installs the plugin (updated when older than the mover needs), hands it every account in the plugin's shape, checks the plugin lists models for them, and only then lets the plugin serve the same provider id, so agents' model ids don't change; any failure rolls back and the built-in keeps serving. The built-in's accounts are kept as a backup, and "Run built-in again" (magpie plugin move-back <id>) takes the plugin's current sign-ins back, never copying a rotating token: they are taken out of the plugin in one step, and an account the built-in renewed meanwhile keeps its newer token. An account only the plugin can hold (Cursor's browser sign-in) stays with it. The provider editor shows "Runs on" with the button, a failed move's reason, and the CLI has magpie plugin move / move-back. Automatic moves (Retiring) are wired in but the list stays empty until the packages are on npm. Checked with real accounts in a sandbox HOME: Command Code Plan and Devin moved, answered a chat through the gateway on the plugin, moved back and answered again on the built-in. TestMove*, TestPutBackKeepsNewer, the movers' round-trip tests and plugin-move.test.cjs (Chromium, WebKit, en/zh) are new. go vet, go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui ./... pass; click-scroll, old-webkit pass.
2026-09-30 21:21:58 +08:00

86 lines
2.6 KiB
Go

package provider
import (
"encoding/json"
"errors"
"time"
)
// Qoder's accounts on @magpie-community/opencode-qoder-auth, as its device
// sign-in keeps one: the job token pair as access and refresh (the refresh
// token is spent on use, so it moves rather than being copied), the device
// token pair and machine id beside them.
func init() {
movers["qoder"] = &mover{
pkg: "@magpie-community/opencode-qoder-auth",
agents: []string{"qoder"},
out: func() ([]Moving, error) {
// no refresh of the built-in's runs while the pairs are read
ls := qoderLogins()
qoderMu.Lock()
defer qoderMu.Unlock()
var out []Moving
for _, l := range ls {
c, ok, _ := qoderCurrent(l.saved)
if !ok {
continue
}
out = append(out, Moving{User: l.User, First: l.Active, On: l.On, Lapsed: l.Lapsed != "", Auth: map[string]any{
"type": "oauth",
"access": c.Token,
"refresh": c.RefreshToken,
"expires": c.ExpiresAt,
"accountId": l.User,
"uid": c.UID,
"email": c.Email,
"name": c.Name,
"machineId": c.MachineID,
"deviceToken": c.DeviceToken,
"deviceRefresh": c.DeviceRefresh,
}})
}
return out, nil
},
back: func(ls []savedLogin, user string, auth map[string]any) ([]savedLogin, string, error) {
access, uid := str(auth["access"]), str(auth["uid"])
if access == "" || uid == "" {
return ls, "", errors.New("Qoder: an unreadable plugin sign-in")
}
if user == "" {
user = firstNonEmpty(str(auth["accountId"]), str(auth["email"]), uid)
}
i := backInto(&ls, "qoder", user)
c, _ := qoderSaved(ls[i])
c.UID, c.Token, c.RefreshToken, c.ExpiresAt = uid, access, str(auth["refresh"]), num(auth["expires"])
c.Email = firstNonEmpty(str(auth["email"]), c.Email)
c.Name = firstNonEmpty(str(auth["name"]), c.Name)
c.MachineID = firstNonEmpty(str(auth["machineId"]), c.MachineID)
c.DeviceToken = firstNonEmpty(str(auth["deviceToken"]), c.DeviceToken)
c.DeviceRefresh = firstNonEmpty(str(auth["deviceRefresh"]), c.DeviceRefresh)
b, err := json.Marshal(c)
if err != nil {
return ls, "", err
}
ls[i].Auth, ls[i].Lapsed, ls[i].Seen = b, "", time.Now().UTC().Truncate(time.Second)
return ls, ls[i].User, nil
},
}
}
// num is a JSON number read into any (a float64, or an int64 before it
// was written out) as an int64.
func num(v any) int64 {
switch n := v.(type) {
case float64:
return int64(n)
case int64:
return n
case int:
return int64(n)
case json.Number:
i, _ := n.Int64()
return i
}
return 0
}