From b87a3ce360feca25bccb3531da7a21f0e76179aa Mon Sep 17 00:00:00 2001 From: yetone Date: Sun, 27 Sep 2026 20:59:38 +0800 Subject: [PATCH] =?UTF-8?q?agents:=20Qoder=20is=20known=20as=20Qoder,=20no?= =?UTF-8?q?t=20Bun=20=E2=80=94=20Qoder=20CLI=20is=20a=20Bun=20single-file?= =?UTF-8?q?=20build=20whose=20requests=20carry=20Bun's=20User-Agent=20and?= =?UTF-8?q?=20nothing=20of=20Qoder's,=20so=20magpie's=20provider=20in=20Qo?= =?UTF-8?q?der=20(and=20Qoder=20CN)=20now=20carries=20the=20key=20magpie-q?= =?UTF-8?q?oder=20(magpie-qoder-cn),=20which=20the=20gateway=20knows=20an?= =?UTF-8?q?=20agent=20by=20before=20its=20User-Agent:=20usage,=20the=20rou?= =?UTF-8?q?ting=20view=20and=20the=20models=20it=20is=20shown=20say=20Qode?= =?UTF-8?q?r.=20A=20Qoder=20wired=20before=20with=20the=20key=20magpie=20i?= =?UTF-8?q?s=20still=20magpie's=20and=20gets=20the=20new=20key=20on=20the?= =?UTF-8?q?=20next=20sync,=20as=20magpie=20starts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/agent/qoder.go | 14 +++++++++++--- internal/agent/qoder_test.go | 18 +++++++++++++++++- internal/gateway/lan_test.go | 1 + 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/internal/agent/qoder.go b/internal/agent/qoder.go index 672ea80e0..9d9f9685a 100644 --- a/internal/agent/qoder.go +++ b/internal/agent/qoder.go @@ -61,7 +61,7 @@ func qoderSite(home string, b qoderBuild) *Agent { get := func(k string) string { v, _ := edit.GetJSON(path, k); return v } onMagpie := func() bool { _, ok := cutMagpie(get("model.name")) - return ok && get(slot+".apiKey") == gateway.Token + return ok && qoderKeyed(get(slot+".apiKey"), b.id) } return &Agent{ ID: b.id, Name: b.name, Icon: "qoder", Aliases: b.aliases, @@ -100,7 +100,7 @@ func qoderSite(home string, b qoderBuild) *Agent { } // out of magpie: its provider goes, and the model the user // had comes back when none is asked for - if onMagpie() || get(slot+".apiKey") == gateway.Token { + if onMagpie() || qoderKeyed(get(slot+".apiKey"), b.id) { if err := edit.DelJSON(path, slot); err != nil { return err } @@ -163,5 +163,13 @@ func qoderProvider(agent, model string) map[string]any { ms = append(ms, e) } return map[string]any{"displayName": "magpie", "protocol": "openai", "baseUrl": gatewayV1(), - "apiKey": gateway.Token, "model": model, "models": ms} + "apiKey": gateway.TokenFor(agent), "model": model, "models": ms} +} + +// qoderKeyed: key is one magpie gives Qoder's provider — its own, +// gateway.TokenFor, since Qoder's requests carry Bun's User-Agent and +// nothing of Qoder's, or gateway.Token, which it was given before; a sync +// gives it the new one. +func qoderKeyed(key, agent string) bool { + return key == gateway.TokenFor(agent) || key == gateway.Token } diff --git a/internal/agent/qoder_test.go b/internal/agent/qoder_test.go index b1d0eca3f..2f1895211 100644 --- a/internal/agent/qoder_test.go +++ b/internal/agent/qoder_test.go @@ -9,8 +9,10 @@ import ( "github.com/tidwall/jsonc" + "github.com/yetone/magpie/internal/edit" "github.com/yetone/magpie/internal/gateway" "github.com/yetone/magpie/internal/provider" + "github.com/yetone/magpie/internal/usage" ) func TestQoder(t *testing.T) { @@ -66,7 +68,7 @@ func testQoder(t *testing.T, mk func(string) *Agent, rel string) { } c, raw := read() p := c.Providers[magpieID] - if !strings.Contains(raw, "// the user's") || c.Model["name"] != "magpie/deepseek/pro" || p["baseUrl"] != gatewayV1() || p["apiKey"] != gateway.Token || + if !strings.Contains(raw, "// the user's") || c.Model["name"] != "magpie/deepseek/pro" || p["baseUrl"] != gatewayV1() || p["apiKey"] != gateway.TokenFor(a.ID) || p["protocol"] != "openai" || p["model"] != "deepseek/pro" || c.Providers["mine"]["apiKey"] != "sk-m" || c.Theme != "dark" { t.Fatalf("magpie:\n%s", raw) } @@ -108,6 +110,20 @@ func testQoder(t *testing.T, mk func(string) *Agent, rel string) { t.Fatalf("own:\n%s", raw) } + // wired before Qoder had a key of its own: still magpie's, and a sync + // gives it Qoder's key, which the gateway knows it by (not Bun's UA) + f.Set("magpie/deepseek/pro") + edit.SetJSON(path, edit.KV{Path: "providers." + magpieID + ".apiKey", Value: gateway.Token}) + if a.Check() != "" { + t.Fatalf("old key: %q", a.Check()) + } + if err := a.Sync(); err != nil { + t.Fatal(err) + } + if c, raw = read(); c.Providers[magpieID]["apiKey"] != gateway.TokenFor(a.ID) || usage.AgentOf(a.ID) != a.ID { + t.Fatalf("synced key:\n%s", raw) + } + // no settings before: a new file with magpie's provider os.Remove(path) if err := f.Set("magpie/deepseek/pro"); err != nil { diff --git a/internal/gateway/lan_test.go b/internal/gateway/lan_test.go index 85ba2d49e..7ee085c6e 100644 --- a/internal/gateway/lan_test.go +++ b/internal/gateway/lan_test.go @@ -74,6 +74,7 @@ func TestAgentOf(t *testing.T) { for _, c := range []struct{ auth, key, ua, want string }{ {"Bearer " + TokenFor("alma"), "", "ai-sdk/openai/2.0.52 ai-sdk/provider-utils/3.0.12 runtime/node.js/v22", "alma"}, {"", TokenFor("alma"), "ai-sdk/anthropic/2.0.1", "alma"}, + {"Bearer " + TokenFor("qoder"), "", "Bun/1.4.2", "qoder"}, {"Bearer " + Token, "", "claude-cli/2.1.0 (external, cli)", "claude-cli"}, {"Bearer " + Token, "", "ai-sdk/openai/2.0.52", "ai-sdk"}, {"Bearer " + Token + "-", "", "codex_cli_rs/0.40.0", "codex_cli_rs"},