Files
magpie/internal/provider/copilot_accounts.go
T
yetone 6546033109 kiro: removing kiro-cli's or the Kiro IDE's own sign-in hides it in magpie, and it shows again when Kiro signs in anew (Discord 碳碳双键: 本机没安装 kiro,但旧的 Kiro 数据还在,magpie 列出一个"Kiro account"(登录已失效,请重新添加此账号),添加新账号并设为首位后,旧的数据无法移除) — magpie reads Kiro's own sign-in from kiro-cli's database or ~/.aws/sso/cache and lists it among the accounts it signed in; Remove on it said "that is kiro-cli's or the Kiro IDE's own sign-in; run kiro-cli logout or sign out in the IDE", which with neither installed left a dead account there for good. The same refusal was shared by every agent whose own sign-in magpie only reads (forgetSideLogin: Kiro, Devin, Grok, Copilot, ZCode, WorkBuddy and WorkBuddy AI, Command Code, Gemini CLI, Antigravity). Now Remove on the agent's own sign-in hides it: logins.json's entry for it keeps a Hidden mark, and it is left out of the account list and so of the gateway's tries (the first and the ones behind it both come from that list); the agent's files are never deleted or written. It shows again when that sign-in changes: for Kiro, by a mark that is a digest of where the sign-in is (kiro-cli's row or the IDE's file) and its refresh token (never the token itself), so a new kiro-cli login or IDE sign-in, to any account, brings it back; for the others, which have no such mark, when it is another account. Removing it while it is first is allowed now, the next account being put first (saved, so it stays first when the own one comes back); removed when it was the only one, the agent has no account left. One of magpie's own accounts is removed with its home as before, and still can't be while it is first. The error and its per-agent sign-out hints are gone. Logins carry own, and the accounts list shows Remove on the agent's own sign-in also while it is first, its title "magpie stops showing and using Kiro's own sign-in; its files are left as they are, and it shows again when Kiro signs in anew" (magpie 不再显示和使用 Kiro 自己的登录;它的文件保持原样,Kiro 重新登录后会再次出现). Tested with a new internal/provider/side_logins_hide_test.go in a sandbox HOME: an expired kiro-cli sign-in ("Kiro account") behind a Google one magpie signed in is removed, stays gone across reads, isn't the account used nor among AlsoOn, kiro-cli's database byte-for-byte and mtime as it was, the refresh token nowhere in magpie's files; a new kiro-cli sign-in brings it back behind the one first; put first and removed, the next is first; one of magpie's removed takes its home and leaves the other's; the only account removed leaves Kiro with none; Grok's CLI sign-in hidden with auth.json untouched, back once it is another account. The agents' tests that expected the refusal (Command Code, Copilot, Devin, Grok, WorkBuddy, ZCode) no longer do. A new internal/gui/tests/own-signin-remove.test.cjs in Chromium and WebKit, en and zh: kiro-cli's sign-in behind another and alone first both have Remove with that title and post login/forget, magpie's first account has none. go build -tags nogui (and GOOS=linux, GOOS=windows), go test -tags nogui ./... and the whole GUI suite (138) pass. Not tried with a real Kiro, kiro-cli or the Kiro IDE.
2026-09-29 23:52:33 +08:00

174 lines
4.9 KiB
Go

package provider
// Several Copilot subscriptions. Copilot's editors and its CLI keep one
// GitHub account, which magpie only ever reads. Each further account is
// signed in by magpie itself, with GitHub's device code under the Copilot
// editors' own OAuth app, and its GitHub token kept in logins.json; the
// editors' own account is remembered there too, with nothing of its token,
// so it can stand behind another or be turned off.
import (
"context"
"encoding/json"
"errors"
"io"
"net/http"
"os"
"path/filepath"
"strings"
)
// copilotConfigDir is where the Copilot editors keep their sign-in.
func copilotConfigDir() string {
if cfg := os.Getenv("XDG_CONFIG_HOME"); cfg != "" {
return cfg
}
home, _ := os.UserHomeDir()
return filepath.Join(home, ".config")
}
// copilotSaved is the sign-in of an account magpie keeps.
func copilotSaved(l savedLogin) (copilotApp, bool) {
var app copilotApp
if l.own() || json.Unmarshal(l.Auth, &app) != nil || app.Token == "" {
return copilotApp{}, false
}
app.User = l.User
return app, true
}
type copilotLoginApp struct {
Login
app copilotApp
}
// copilotLogins lists the Copilot accounts, the first in use first, then
// the rest as they were added.
func copilotLogins(cfg string) []copilotLoginApp {
own, _ := copilotLogin(cfg)
ownUser := copilotOwnUser(cfg)
var out []copilotLoginApp
for _, l := range sideLogins("copilot", ownUser, func(l savedLogin) bool {
_, ok := copilotSaved(l)
return ok
}) {
app := own
if !l.saved.own() {
app, _ = copilotSaved(l.saved)
}
app.User = l.User
out = append(out, copilotLoginApp{l.Login, app})
}
return out
}
// copilotOwnUser is who the editors' or CLI's Copilot sign-in is, "" for none.
func copilotOwnUser(cfg string) string {
own, ok := copilotLogin(cfg)
if !ok {
return ""
}
return firstNonEmpty(own.User, "GitHub")
}
func copilotSide() []sideLogin {
var out []sideLogin
for _, c := range copilotLogins(copilotConfigDir()) {
out = append(out, sideLogin{Login: c.Login})
}
return out
}
func copilotLoginList() []Login { return loginsOf(copilotSide()) }
func switchCopilotLogin(user string) error {
return switchSideLogin("copilot", user, copilotSide())
}
func setCopilotLoginOn(user string, on bool) error {
return setSideLoginOn("copilot", user, on, copilotSide())
}
func forgetCopilotLogin(user string) error {
return forgetSideLogin("copilot", user, copilotSide(), nil)
}
// addCopilotLogin keeps an account magpie just signed in.
func addCopilotLogin(user, plan, token string) error {
auth, _ := json.Marshal(map[string]string{"oauth_token": token})
return addSideLogin(savedLogin{Agent: "copilot", User: user, Plan: plan, Auth: auth}, copilotOwnUser(copilotConfigDir()), func(savedLogin) {})
}
// copilotAccount is the Copilot account in use first.
func copilotAccount(cfg string) (Provider, bool) {
ls := copilotLogins(cfg)
if len(ls) == 0 {
return Provider{}, false
}
return copilotProvider(ls[0].app, ls[0].Plan), true
}
// copilotAlsoOn is the Copilot accounts in use behind the first.
func copilotAlsoOn() []Provider {
var out []Provider
for _, c := range copilotLogins(copilotConfigDir()) {
if !c.Active && c.On {
out = append(out, copilotProvider(c.app, c.Plan))
}
}
return out
}
// copilotPlans names Copilot's plans as GitHub sells them.
var copilotPlans = map[string]string{
"free": "Free", "individual": "Pro", "individual_pro": "Pro+",
"business": "Business", "enterprise": "Enterprise",
}
// copilotUser is who a GitHub token belongs to and which Copilot plan it
// has; no plan, no Copilot.
func copilotUser(ctx context.Context, token string) (user, plan string, err error) {
get := func(u string, v any) (int, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil)
if err != nil {
return 0, err
}
req.Header.Set("Authorization", "token "+token)
req.Header.Set("Accept", "application/json")
res, err := http.DefaultClient.Do(req)
if err != nil {
return 0, err
}
defer res.Body.Close()
b, _ := io.ReadAll(io.LimitReader(res.Body, 1<<20))
if res.StatusCode != 200 {
return res.StatusCode, errors.New(APIError(b, res.Status))
}
return 200, json.Unmarshal(b, v)
}
var gh struct {
Login string `json:"login"`
}
if _, err := get(GitHubUserURL, &gh); err != nil || gh.Login == "" {
return "", "", errors.New("GitHub didn't say whose account this is")
}
var cp struct {
Plan string `json:"copilot_plan"`
}
if code, err := get(CopilotUserURL, &cp); err != nil {
if code == 401 || code == 403 || code == 404 {
return gh.Login, "", errors.New(gh.Login + " has no Copilot subscription")
}
return gh.Login, "", errors.New("Copilot: " + err.Error())
}
plan = copilotPlans[strings.ToLower(cp.Plan)]
if plan == "" && cp.Plan != "" {
plan = strings.ToUpper(cp.Plan[:1]) + cp.Plan[1:]
}
return gh.Login, plan, nil
}
// GitHubUserURL says whose a GitHub token is; a var so tests can point it
// elsewhere.
var GitHubUserURL = "https://api.github.com/user"