Files
magpie/internal/provider/kiro_accounts.go
T
yetone fa9daa6945 docs: every built-in subscription a plugin serves once moved says so in its code, and AGENTS.md says where a fix for it goes (user: 对内置的订阅 provider 在代码层面和文档层面加上注释,防止又有别的 agent 在改内置订阅 provider 的代码而不知道生效的其实是对应的插件代码) — a moved Cursor, Devin, Grok, Command Code plan, Kiro, Zed, Qoder, Factory, Xiaomi MiMo, ZCode or WorkBuddy (provider.Moved; since v0.1.642 also the default for a new sign-in, provider.Adopt) is signed in, listed, requested and metered by its @magpie-community plugin, and none of the built-in's code in internal/provider/<name>*.go, internal/gateway/<name>.go, internal/zed or internal/qoder serves it; a fix made there alone never reaches those users. Each of those files now opens with a PLUGIN-SERVED notice naming the subscription's id, its plugin package, where the plugin lives (magpie-community/plugins, packages/<name>) and the mover whose min to raise so moved users get the fixed plugin (keepMovedCurrent); Qoder's adds that Qoder CN isn't moved and still runs there. AGENTS.md (CLAUDE.md imports it) lists the moved built-ins with their packages and movers, how to tell whether a user's is moved (move.state "plugin", onPlugins), the order a fix goes in (the plugin, publish, raise min, the built-in only for those not moved), the magpie code that does run for a moved one (internal/plugin, plugins.go, plugin_usage.go, pluginsignin.go, migrate*.go, app.js's subOf/pluginSubs/startPluginSignIn), and the parity tests. movers in migrate.go and SUBS in app.js point there too. TestMovedBuiltinsSayTheirPlugin fails for a mover whose code carries no PLUGIN-SERVED notice naming its id and package (it failed while a wrapped package name was split). Comments and docs only, no behaviour changed. go vet ./internal/..., go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui -count=1 ./... under a temp HOME pass (TestProvidersAnswerWhileListsComeIn and TestClaudeSignedOut failed once on their timing under load, pass alone; untouched).
2026-10-02 19:37:02 +08:00

182 lines
5.7 KiB
Go

package provider
// PLUGIN-SERVED (see AGENTS.md): Kiro ("kiro") is a deprecated built-in
// subscription served by its plugin, @magpie-community/opencode-kiro-auth,
// once moved onto it (provider.Moved; the default for a new sign-in). A
// moved one's sign-ins, models, requests and usage are all the plugin's,
// never this code's (only the move, in migrate*.go, still reads its
// accounts). A fix here alone doesn't reach those users; fix the plugin
// (github.com/magpie-community/plugins, packages/kiro) and raise the mover's
// min in internal/provider/migrate_kiro.go.
// Several Kiro subscriptions. kiro-cli and the Kiro IDE keep one account,
// which magpie only ever reads. Each account magpie signs in gets a home of
// its own holding its kiro-auth-token.json, refreshed there, so each refresh
// token has one holder. logins.json names those homes; Kiro's own account is
// remembered there too, with no home, so it can stand behind another or be
// turned off.
import (
"context"
"errors"
"os"
"path/filepath"
"strings"
"sync"
"time"
)
// kiroTokenFile is what an account's home holds, in the IDE's shape.
const kiroTokenFile = "kiro-auth-token.json"
// kiroAccountsDir holds the homes of the Kiro accounts magpie signed in.
func kiroAccountsDir() string { return filepath.Join(filepath.Dir(Path()), "kiro") }
// kiroLegacyPath is where magpie kept the one Kiro account it signed in
// before it kept several (v0.1.374).
func kiroLegacyPath() string { return filepath.Join(filepath.Dir(Path()), kiroTokenFile) }
// kiroLogin is a Kiro account and the home it is signed in in, "" for
// kiro-cli's or the IDE's.
type kiroLogin struct {
Login
Home string
}
// kiroOwnUser is who kiro-cli's or the IDE's sign-in is, "" for none; until
// Kiro has said, it is named for what it is.
func kiroOwnUser() string {
if _, ok := readKiroAt("", ""); !ok {
return ""
}
user, _ := kiroIdentity("", "")
return firstNonEmpty(user, "Kiro account")
}
// kiroLogins lists the Kiro accounts that are signed in, the first in use
// first, then the rest as they were added.
func kiroLogins() []kiroLogin {
kiroAdoptLegacy()
var out []kiroLogin
for _, l := range sideLogins("kiro", kiroOwnUser(), func(l savedLogin) bool {
_, ok := readKiroAt("", l.Home)
return l.Home != "" && ok
}) {
out = append(out, kiroLogin{l.Login, l.saved.Home})
}
return out
}
// kiroAdoptLegacy moves the account magpie kept alone into a home of its
// own, among the rest, once Kiro says whose it is; until it can, the file
// stays where it was and is tried again a few minutes on.
func kiroAdoptLegacy() {
kiroAdopt.Lock()
defer kiroAdopt.Unlock()
old := kiroLegacyPath()
if time.Since(kiroAdopt.tried) < 5*time.Minute {
return
}
if _, err := os.Stat(old); err != nil {
return
}
kiroAdopt.tried = time.Now()
home, err := newKiroHome()
if err != nil {
return
}
file := filepath.Join(home, kiroTokenFile)
if err := os.Rename(old, file); err != nil {
removeKiroHome(home)
return
}
if _, err := addKiroLogin(home); err != nil {
_ = os.Rename(file, old)
removeKiroHome(home)
}
}
var kiroAdopt struct {
sync.Mutex
tried time.Time
}
func kiroSide() []sideLogin {
var out []sideLogin
for _, k := range kiroLogins() {
out = append(out, sideLogin{Login: k.Login})
}
return out
}
// kiroLoginList is the Kiro accounts as Logins lists them.
func kiroLoginList() []Login { return loginsOf(kiroSide()) }
// switchKiroLogin puts a Kiro account first. kiro-cli's and the IDE's
// sign-in stay as they are: magpie only changes which account its gateway
// uses first.
func switchKiroLogin(user string) error { return switchSideLogin("kiro", user, kiroSide()) }
func setKiroLoginOn(user string, on bool) error {
return setSideLoginOn("kiro", user, on, kiroSide())
}
// forgetKiroLogin drops an account magpie signed in, with its home.
// kiro-cli's or the IDE's own is only hidden (forgetSideLogin).
func forgetKiroLogin(user string) error {
return forgetSideLogin("kiro", user, kiroSide(),
func(l savedLogin) { removeKiroHome(l.Home) })
}
// newKiroHome makes the home a further Kiro account is signed in in.
func newKiroHome() (string, error) {
home := filepath.Join(kiroAccountsDir(), strings.ToLower(strings.NewReplacer("-", "", "_", "").Replace(randomToken(9))))
return home, os.MkdirAll(home, 0o700)
}
// removeKiroHome deletes a home magpie made, and nothing else.
func removeKiroHome(home string) {
if rel, err := filepath.Rel(kiroAccountsDir(), home); err == nil && rel != "." && !strings.HasPrefix(rel, "..") {
_ = os.RemoveAll(home)
}
}
// addKiroLogin keeps the account just signed in in home, in use beside the
// others, once Kiro says whose it is. Signed in again, an account keeps
// the newer home.
func addKiroLogin(home string) (string, error) {
user, plan := askKiroIdentity("", home)
if user == "" {
return "", errors.New("signed in, but Kiro didn't say whose account it is; try again")
}
kiroSaidNow("", home, user, plan)
return user, addSideLogin(savedLogin{Agent: "kiro", User: user, Plan: plan, Home: home}, kiroOwnUser(),
func(l savedLogin) { removeKiroHome(l.Home) })
}
// kiroAlsoOn is the Kiro accounts in use behind the first; with a key
// saved on the provider, that key is all there is.
func kiroAlsoOn(p Provider) []Provider {
if p.Key != "" {
return nil
}
var out []Provider
for _, k := range kiroLogins() {
if k.Active || !k.On {
continue
}
out = append(out, kiroProvider("", kiroAcct(k)))
}
return out
}
// kiroLoginQuota is the allowance of one of the Kiro accounts.
func kiroLoginQuota(ctx context.Context, l Login) SubscriptionQuota {
for _, k := range kiroLogins() {
if strings.EqualFold(k.User, l.User) {
return kiroQuotaAt(ctx, "", k.Home)
}
}
return SubscriptionQuota{Provider: "kiro", Plan: l.Plan, Windows: []QuotaWindow{}, Error: "not signed in"}
}