An OpenCode Zen provider added with no key asks with public, the key
OpenCode sends signed out (normalize). On a Zen provider that already had
a key of the user's own, Add another key said only "paste an API key", and
with nothing pasted AddKey refused before it knew which provider it was
("paste the key to add"), so there was no way to learn of public or add it
as the anonymous fallback behind the user's own key.
- provider: Provider.AnonymousKey is the key a provider is asked with when
the user has none (OpenCode Zen's public, by its preset, as normalize
already decided; "" for every other, Kilo included). AddKey looks the
provider up first; nothing pasted adds that key, and asked again says
"<name> already has public, the key its free models answer with none of
yours" rather than "already has this key". Every other provider still
wants a key pasted. normalize reads the same method.
- gui: /api/providers names anonKey on a provider that has such a key and
doesn't have it yet. Its Add another key field reads "paste an API key,
or leave empty for public: free models only" (title too, where a narrow
field cuts it short), and the row added is marked as any new key's is.
zh, zh-TW, ja and de translations.
Verified:
- TestZenAddKeyEmptyIsPublic (provider) fails on the old code with
"paste the key to add"; TestZenAddAnotherKeyEmptyAddsPublic (gui,
through /api/keys/add and /api/providers) fails with `Zen with its own
key offers ""`. Both pass with the change.
- zen-anon-key.test.cjs: 40 pass in chromium and webkit, en/zh/zh-TW/ja/de,
at 900 and 560px; on the old app.js the placeholder test fails
('paste an API key' vs the new hint).
- gui-de, gui-ja, gui-zh-tw, i18n-keys, key-import, provider-many-keys,
nokey-editor, key-copy: 46 pass.
- Not tried against Zen itself: whether a gateway falling from a spent key
to public on a paid model rests public is unchanged behavior, the same as
for public typed by hand.
125 lines
3.6 KiB
Go
125 lines
3.6 KiB
Go
package provider
|
|
|
|
import (
|
|
"encoding/json"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/catalog"
|
|
)
|
|
|
|
// OpenCode Zen's free models are listed: magpie asks them as OpenCode does
|
|
// (gateway/zenfree.go), so they answer.
|
|
func TestOpenCodeZenListsFreeModels(t *testing.T) {
|
|
p, err := FromPreset("opencode-zen")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got := p.planModels([]catalog.Model{{ID: "mimo-v2.6-flash-free"}, {ID: "claude-opus-5-5"}, {ID: "gpt-5.5"}})
|
|
if len(got) != 3 || got[0].ID != "mimo-v2.6-flash-free" {
|
|
t.Fatalf("got %v", got)
|
|
}
|
|
if !p.OpenCodeFree("mimo-v2.6-flash-free") || p.OpenCodeFree("gpt-5.5") {
|
|
t.Fatal("OpenCodeFree")
|
|
}
|
|
if (Provider{Preset: "together"}).OpenCodeFree("a-free") {
|
|
t.Fatal("OpenCodeFree on another provider")
|
|
}
|
|
}
|
|
|
|
// The Test button's probe of a free model is streamed and offers bash and
|
|
// read, each in the protocol's own tool shape.
|
|
func TestZenFreeProbe(t *testing.T) {
|
|
for proto, key := range map[Protocol]string{Chat: "function", Responses: "name", Anthropic: "input_schema"} {
|
|
var m map[string]any
|
|
if err := json.Unmarshal([]byte(zenFreeProbe(proto, `{"model":"x-free","max_tokens":1}`)), &m); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
tools, _ := m["tools"].([]any)
|
|
if m["stream"] != true || len(tools) != 2 || m["model"] != "x-free" {
|
|
t.Fatalf("%v: %v", proto, m)
|
|
}
|
|
if _, ok := tools[0].(map[string]any)[key]; !ok {
|
|
t.Fatalf("%v: tool %v", proto, tools[0])
|
|
}
|
|
}
|
|
}
|
|
|
|
// #572: an OpenCode Zen provider added with no key asks with the one
|
|
// OpenCode sends signed out, which Zen's free models answer; a key of the
|
|
// user's own is kept.
|
|
func TestOpenCodeZenNeedsNoKey(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"))
|
|
p, err := FromPreset("opencode-zen")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
p.Models = []string{"mimo-v2.6-flash-free"}
|
|
id, err := Add(p)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, err := Find(id)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got.Key != OpenCodeAnonymousKey {
|
|
t.Fatalf("key %q", got.Key)
|
|
}
|
|
p.Name, p.Key = "Zen paid", "sk-own"
|
|
if id, err = Add(p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got, _ = Find(id); got.Key != "sk-own" {
|
|
t.Fatalf("own key %q", got.Key)
|
|
}
|
|
// another provider still needs one
|
|
if _, err := Add(Provider{Name: "Relay", Chat: "https://api.relay.example/v1", Models: []string{"m"}}); err == nil {
|
|
t.Fatal("a relay with no key")
|
|
}
|
|
}
|
|
|
|
// #1795: Add another key on an OpenCode Zen provider with a key of the
|
|
// user's own, with nothing pasted, adds public behind it, as a new Zen
|
|
// provider saved with no key has it; asked again, it says it has it. Kilo,
|
|
// whose free models need no key too, has no such key to add.
|
|
func TestZenAddKeyEmptyIsPublic(t *testing.T) {
|
|
isolate(t)
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
zen, err := FromPreset("opencode-zen")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
zen.ID, zen.Key = "zen", "sk-own"
|
|
if err := Save(zen); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := AddKey("zen", "", "", ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
p, _ := Find("zen")
|
|
if on := p.KeysOn(); len(on) != 2 || on[0].Key != "sk-own" || on[1].Key != OpenCodeAnonymousKey {
|
|
t.Fatalf("keys on %+v", on)
|
|
}
|
|
if err := AddKey("zen", "", "", ""); err == nil || !strings.Contains(err.Error(), "already has public") {
|
|
t.Fatalf("public twice: %v", err)
|
|
}
|
|
kilo, err := FromPreset("kilo")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
kilo.ID, kilo.Key = "kilo", "sk-kilo"
|
|
if err := Save(kilo); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := AddKey("kilo", "", "", ""); err == nil {
|
|
t.Fatal("Kilo took an empty key")
|
|
}
|
|
}
|