provider: one key added again for another API of the same host is a provider of its own (#3388)
energetic_wolf_99424 (Discord #help) has one relay key that works in both the OpenAI and the Anthropic format. They added a custom provider with it in one format; adding a second custom provider with the same key in the other format was refused with "<name> is already added with that key". The same refusal blocked OpenCode Go beside OpenCode Zen with one account's key (#3388, found by ggbdpq, verified by Edward4226). Add's duplicate check (add in provider.go) used sameProvider, which judges by key + host + headers only. It now also needs servesAll: the new provider brings no API, and no address of one, that the provider already there lacks (Chat, Responses, Anthropic and Gemini compared slot by slot after normalize). So the same key at the same addresses is still refused as the one already here, and another format, or another product path on the host, is added under its own id. The GUI, the CLI (`magpie provider add`) and the TUI all go through provider.Add, so all three are covered. AddCopy never had the check. Two providers sharing one key already existed (AddCopy, another host), so nothing keyed by the key (KeyID, usage by provider id, resting) meets a new state. Not changed: the app import still calls an entry "Already added" when the provider there has the key at that host, even if the entry brings an API it lacks; filed as #4904. Run at this head (rebased on1829cd52f), on mote: - go test ./internal/provider -count=1: ok (191s). - gofmt -l clean; go vet -tags nogui ./internal/provider ./internal/cli ./internal/tui ./internal/gui for darwin, linux and windows: ok. - Before the rebase (base07f4d46c5, same diff): go test ./internal/cli ./internal/tui ./internal/gui: tui ok; cli red only on TestEveryRouteHasACommandOrIsNamed (#4826, checkin-account route), gui red only on TestMainWindowCorners/macOS_26_SDK (#3949, macOS 27). Broken by hand: dropping `&& servesAll(...)` turns TestAddSameKeyOtherFormat red with the reporter's refusal ("Relay is already added with that key (relay); ..."). Not tried: the real GUI Add dialog, a real relay key in both formats, a real OpenCode key. Reported by energetic_wolf_99424; #3388 by ggbdpq.
This commit is contained in:
@@ -1185,11 +1185,15 @@ func add(p Provider, once bool) (string, error) {
|
||||
// nothing: the id is the site's instead
|
||||
p.ID = hostID(p)
|
||||
}
|
||||
// the same key on the same host with the same headers is the one
|
||||
// already here, not another: adding it twice would only split its usage.
|
||||
// A copy the user asked for is taken (AddCopy).
|
||||
// the same key on the same host with the same headers, at the same
|
||||
// addresses, is the one already here, not another: adding it twice
|
||||
// would only split its usage. One key a relay takes in both formats,
|
||||
// added once for its OpenAI API and once for its Anthropic API, is two
|
||||
// providers the user asked for (energetic_wolf_99424 on Discord: the
|
||||
// second was refused as the key already used). A copy the user asked
|
||||
// for is taken (AddCopy).
|
||||
for _, h := range All() {
|
||||
if once && h.Account == nil && sameProvider(h, normalize(p)) {
|
||||
if once && h.Account == nil && sameProvider(h, normalize(p)) && servesAll(h, normalize(p)) {
|
||||
return "", fmt.Errorf("%s is already added with that key (%s); magpie provider key %s <key> changes its key", h.Name, h.ID, h.ID)
|
||||
}
|
||||
}
|
||||
@@ -1204,6 +1208,17 @@ func add(p Provider, once bool) (string, error) {
|
||||
return p.ID, nil
|
||||
}
|
||||
|
||||
// servesAll says h is asked at every address p is added with, each for the
|
||||
// same API: p brings no API, and no address of one, that h lacks.
|
||||
func servesAll(h, p Provider) bool {
|
||||
for _, u := range [][2]string{{h.Chat, p.Chat}, {h.Responses, p.Responses}, {h.Anthropic, p.Anthropic}, {h.Gemini, p.Gemini}} {
|
||||
if u[1] != "" && u[0] != u[1] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// AddCopy adds p, a copy the user made of the provider from (#268), beside
|
||||
// it: what the form doesn't carry — the keys, the balance token, how
|
||||
// requests spread over the keys, where they fall back to — is from's where
|
||||
|
||||
@@ -110,3 +110,66 @@ func TestAddSecondOfPreset(t *testing.T) {
|
||||
t.Fatalf("edit: %+v, %d providers", a, len(All()))
|
||||
}
|
||||
}
|
||||
|
||||
// One key a relay takes in both formats is added once for each: the
|
||||
// second, at the same host with the same key, is another API of it, not
|
||||
// the one already here (energetic_wolf_99424 on Discord: the Anthropic
|
||||
// one was refused as the key already used). The same API at the same
|
||||
// address again is still the one already here.
|
||||
func TestAddSameKeyOtherFormat(t *testing.T) {
|
||||
isolate(t)
|
||||
home := t.TempDir()
|
||||
t.Setenv("HOME", home)
|
||||
t.Setenv("USERPROFILE", home)
|
||||
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
|
||||
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
|
||||
const key = "sk-both"
|
||||
openai, err := Add(Provider{Name: "Relay", Chat: "https://api.relay.example/v1", Key: key, Models: []string{"gpt-5"}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
claude, err := Add(Provider{Name: "Relay Claude", Anthropic: "https://api.relay.example", Key: key, Models: []string{"claude-sonnet-4-5"}})
|
||||
if err != nil {
|
||||
t.Fatalf("the same key in the Anthropic format was refused: %v", err)
|
||||
}
|
||||
if openai == claude {
|
||||
t.Fatalf("both saved as %q", openai)
|
||||
}
|
||||
if p, err := Find(claude); err != nil || p.Anthropic != "https://api.relay.example" || p.Key != key || p.Chat != "" {
|
||||
t.Fatalf("anthropic one: %+v %v", p, err)
|
||||
}
|
||||
if p, err := Find(openai); err != nil || p.Chat != "https://api.relay.example/v1" || p.Anthropic != "" {
|
||||
t.Fatalf("openai one changed: %+v %v", p, err)
|
||||
}
|
||||
// the same again, in either format, is the one already here
|
||||
for _, dup := range []Provider{
|
||||
{Name: "Relay again", Chat: "https://api.relay.example/v1/", Key: key},
|
||||
{Name: "Claude again", Anthropic: "https://api.relay.example/v1", Key: key},
|
||||
} {
|
||||
if id, err := Add(dup); err == nil {
|
||||
t.Fatalf("%s was added as %q", dup.Name, id)
|
||||
}
|
||||
}
|
||||
if n := len(All()); n != 2 {
|
||||
t.Fatalf("%d providers, want 2", n)
|
||||
}
|
||||
// two products of one vendor at one host, one account's key for both
|
||||
// (#3388, ggbdpq's case): OpenCode Go beside OpenCode Zen
|
||||
zen, _ := FromPreset("opencode-zen")
|
||||
zen.Key = "sk-opencode"
|
||||
if _, err := Add(zen); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
gop, _ := FromPreset("opencode-go")
|
||||
gop.Key = "sk-opencode"
|
||||
id, err := Add(gop)
|
||||
if err != nil {
|
||||
t.Fatalf("Go beside Zen, same key: %v", err)
|
||||
}
|
||||
if p, err := Find(id); err != nil || p.Chat != "https://opencode.ai/zen/go/v1" {
|
||||
t.Fatalf("go: %+v %v", p, err)
|
||||
}
|
||||
if _, err := Add(gop); err == nil {
|
||||
t.Fatal("a second Go with the same key was added")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user