Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

3.3 KB
Raw Blame
package access

import (
	"errors"
	"fmt"
	"slices"
	"strings"

	"github.com/yetone/magpie/internal/provider"
)

// CleanAccounts is a key's account list as kept (#905): each entry
// "<provider>/<account>", an account named by its stable id — or by the
// name it is shown by, resolved to that id, so the command line reads
// while the list keeps what a rename doesn't move — and a key by its
// fingerprint; trimmed, told apart in lower case as accounts are, each
// once, in the order given. Every account the entry names must be one
// its provider has now, so a typo can't quietly hold a key to nothing;
// one gone later — a key rotated, an account signed out — is kept as it
// is, matching nothing.
func CleanAccounts(in []string) ([]string, error) {
	var out []string
	for _, a := range in {
		a = strings.TrimSpace(a)
		if a == "" {
			continue
		}
		pid, ref, ok := strings.Cut(a, "/")
		if !ok || pid == "" || ref == "" {
			return nil, errors.New("A key's account is <provider>/<account> or <provider>/<key id>, like codex/[email protected]: " + a)
		}
		if len(a) > 300 {
			return nil, errors.New("A key's account is at most 300 characters")
		}
		p, err := provider.Find(pid)
		if err != nil {
			return nil, err
		}
		id := ""
		for _, r := range p.AccountIDs() {
			if strings.EqualFold(r.ID, ref) || strings.EqualFold(r.User, ref) {
				id = r.ID
				break
			}
		}
		if id == "" {
			if p.Account == nil {
				return nil, fmt.Errorf("%s has no key %q", p.Name, ref)
			}
			return nil, fmt.Errorf("%s has no account %q", p.Name, ref)
		}
		kept := p.ID + "/" + id
		if !slices.ContainsFunc(out, func(o string) bool { return strings.EqualFold(o, kept) }) {
			out = append(out, kept)
		}
	}
	if len(out) > 500 {
		return nil, errors.New("A key can list at most 500 accounts")
	}
	return out, nil
}

// AllowsAccount says the provider pid's account or key ref is among the
// key's accounts — "<provider>/<ref>", told apart in lower case, as
// accounts are. The list holds a key to some accounts **of the providers
// it names**: a provider it names no account of, the key uses as it
// always did, its every account and key. Any, for a key that lists none.
func (who Identity) AllowsAccount(pid, ref string) bool {
	if len(who.Accounts) == 0 {
		return true
	}
	pid = strings.ToLower(strings.TrimSpace(pid))
	key := pid + "/" + strings.ToLower(strings.TrimSpace(ref))
	named := false
	for _, a := range who.Accounts {
		if a = strings.ToLower(strings.TrimSpace(a)); a == key {
			return true
		} else if p, _, ok := strings.Cut(a, "/"); ok && p == pid {
			named = true
		}
	}
	return !named
}

// ReplaceAccount moves every key's account list from one of provider
// pid's keys to the fingerprint it has now (#1869: the key's value
// replaced in place), so a key held to it is held to it still. It
// returns how many keys it moved.
func ReplaceAccount(pid, was, now string) (int, error) {
	if was == now {
		return 0, nil
	}
	mu.Lock()
	defer mu.Unlock()
	keys, err := load()
	if err != nil {
		return 0, err
	}
	old := strings.ToLower(pid + "/" + was)
	moved := 0
	for i := range keys {
		for j, a := range keys[i].Accounts {
			if strings.ToLower(strings.TrimSpace(a)) == old {
				keys[i].Accounts[j] = pid + "/" + now
				moved++
			}
		}
	}
	if moved == 0 {
		return 0, nil
	}
	return moved, save(keys)
}