nb5p runs a gateway of their own behind magpie and saw nearly every request arrive as "magpie/<version>". The agent's User-Agent now goes on as the agent sent it, wherever magpie isn't itself the client. What each path sends now: - Key+URL providers (official vendor keys and custom OpenAI/Anthropic compatible ones), on chat, Anthropic, Responses, Gemini, count_tokens, embeddings, images and videos: the agent's User-Agent, as sent. Before, this was the opt-in passUserAgent (#1851) and the default was magpie's. The opt-out is magpieUserAgent: `magpie provider set <id> user-agent=magpie`, or untick "Send the agent's own User-Agent" in the editor. passUserAgent is no longer a field, so a file that set it true reads the same as the new default. A provider someone turned back off after opting in can't be told apart from one never touched, and now sends the agent's too. - When the agent sent no User-Agent, and on magpie's own requests (classifier magpie-router/1, vision, search, the image tool, model lists): magpie's, unchanged. - A header.User-Agent the user set: still wins over both. - Subscription accounts (Claude, Codex, Copilot, Google, ZCode…), plugins, and OpenCode/Cline/Kilo: unchanged. Their signing sets the official client's UA (provider.PassesUserAgent still refuses an Account or OwnClient). - Claude Code and Codex header copies: unchanged. - Remote magpie: the near magpie still asks as magpie/<v>, because the far end trusts X-Magpie-Agent/Via only from that. It now adds X-Magpie-User-Agent (passOnUA, on forwardOnce, embeddings, draw and video; the classifier's decision request names none). The far end takes it only from a magpie/ request (callerFrom) and strips it on arrival (withCaller). agentUA/callerUA then hand it to userAgentFor, so the far vendor sees the agent's. A client that writes the header itself, straight to a magpie, is ignored. magpie's own UAs are never relayed (magpiesOwn). Vendor check: invalid-key requests with magpie/, claude-cli, a Chrome UA and pi's went to Anthropic, OpenAI, Gemini, DeepSeek, Moonshot, Kimi For Coding, Zhipu, OpenRouter, xAI, DashScope, MiniMax, Volcengine Ark, Groq and Mistral. Every vendor gave the same auth error for every UA, so no edge-level UA gating was seen. Kimi For Coding's agent allow-list sits behind a valid key, which wasn't tried. Run (mote): - go test -run 'UserAgent|RemoteMagpie|ZCode|Blocked' ./internal/gateway: ok - go test ./internal/provider ./internal/cli ./internal/gui: ok - node --test provider-user-agent, provider-native-compaction and i18n-keys (chromium and webkit; en/zh/zh-TW/ja/de; 900 and 440 wide): 123 of 124 pass. The red is i18n-keys "a plugin's words are its own" (plugins' words said in files this change doesn't touch), already filed as #5327/#5332, #5426 and #5482. - gofmt -l clean; go vet; GOOS=linux -tags nogui and GOOS=windows vet: ok at the rebased head (onf13d5f16f). Targeted gateway, provider, cli and gui packages (UserAgent|RemoteMagpie|ZCode|Blocked|Budget) ok there too; whole packages ran at the pre-rebase base4461957b9. Broken by hand: - PassesUserAgent back to opt-in: TestUserAgentPassedOn/default, TestUserAgentFor, TestRemoteMagpieRelaysUserAgent, TestPassesUserAgent and TestProviderUserAgentPair go red. - agentUA's relay off, or passOnUA a no-op: TestRemoteMagpieRelaysUserAgent goes red; the far vendor is asked as magpie/dev. - withCaller's Del(X-Magpie-User-Agent) removed: stays green. Nothing copies incoming headers to a vendor today, so the Del only guards against that. Not tried: a real remote magpie on two machines, a real valid-key vendor request, and nb5p's own gateway. Reported-by: nb5p on Discord
166 lines
6.5 KiB
Go
166 lines
6.5 KiB
Go
package gui
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// A provider's Routing and Stays are picked in its editor and made with its
|
|
// Save (01huadalang on Discord: each click saved at once); a Save that
|
|
// doesn't carry them keeps them, where the Stays set on the Routing page
|
|
// used to be lost at the editor's next Save, which never sent it.
|
|
func TestProviderSaveRouting(t *testing.T) {
|
|
t.Setenv("HOME", t.TempDir())
|
|
t.Setenv("USERPROFILE", os.Getenv("HOME"))
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
t.Setenv("XDG_CACHE_HOME", t.TempDir())
|
|
mux := http.NewServeMux()
|
|
providerRoutes(mux, nil)
|
|
post := func(action, body string) {
|
|
t.Helper()
|
|
w := httptest.NewRecorder()
|
|
mux.ServeHTTP(w, httptest.NewRequest("POST", "/api/provider/"+action, strings.NewReader(body)))
|
|
if w.Code != 200 {
|
|
t.Fatalf("%s: %d %s", action, w.Code, w.Body)
|
|
}
|
|
}
|
|
is := func(routing, affinity string) {
|
|
t.Helper()
|
|
p, err := provider.Find("relay")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if p.Routing != routing || p.Affinity != affinity {
|
|
t.Fatalf("routing %q, affinity %q; want %q, %q", p.Routing, p.Affinity, routing, affinity)
|
|
}
|
|
}
|
|
const form = `"id":"relay","name":"Relay","chat":"http://127.0.0.1:1/v1","models":["sol"]`
|
|
post("save", `{`+form+`,"key":"k","new":true}`)
|
|
is("", "")
|
|
// the editor's picks, made with its Save
|
|
post("save", `{`+form+`,"from":"relay","routing":"rotate","affinity":"session"}`)
|
|
is(provider.Rotate, provider.AffinitySession)
|
|
// a Save that doesn't carry them keeps them
|
|
post("save", `{`+form+`,"from":"relay"}`)
|
|
is(provider.Rotate, provider.AffinitySession)
|
|
// set on their own, from the Routing page, and kept by the next Save
|
|
post("route", `{"id":"relay","routing":"order"}`)
|
|
post("affinity", `{"id":"relay","affinity":"turn"}`)
|
|
is(provider.Ordered, provider.AffinityTurn)
|
|
post("save", `{`+form+`,"from":"relay"}`)
|
|
is(provider.Ordered, provider.AffinityTurn)
|
|
// Smart and Auto picked are "" sent, not left out
|
|
post("save", `{`+form+`,"from":"relay","routing":"","affinity":""}`)
|
|
is("", "")
|
|
}
|
|
|
|
// Sink (01huadalang: one rate limited with quota left goes to the back)
|
|
// is picked in the editor and made with its Save, or set on its own from
|
|
// the Routing page, and a Save that doesn't carry it keeps it.
|
|
func TestProviderSaveSink(t *testing.T) {
|
|
t.Setenv("HOME", t.TempDir())
|
|
t.Setenv("USERPROFILE", os.Getenv("HOME"))
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
t.Setenv("XDG_CACHE_HOME", t.TempDir())
|
|
mux := http.NewServeMux()
|
|
providerRoutes(mux, nil)
|
|
post := func(action, body string) {
|
|
t.Helper()
|
|
w := httptest.NewRecorder()
|
|
mux.ServeHTTP(w, httptest.NewRequest("POST", "/api/provider/"+action, strings.NewReader(body)))
|
|
if w.Code != 200 {
|
|
t.Fatalf("%s: %d %s", action, w.Code, w.Body)
|
|
}
|
|
}
|
|
is := func(want bool) {
|
|
t.Helper()
|
|
p, err := provider.Find("relay")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if p.Sink != want {
|
|
t.Fatalf("sink %v, want %v", p.Sink, want)
|
|
}
|
|
if got := providerInfo(*p, nil).Sink; got != want {
|
|
t.Fatalf("listed sink %v, want %v", got, want)
|
|
}
|
|
}
|
|
const form = `"id":"relay","name":"Relay","chat":"http://127.0.0.1:1/v1","models":["sol"]`
|
|
post("save", `{`+form+`,"key":"k","new":true}`)
|
|
is(false)
|
|
post("save", `{`+form+`,"from":"relay","routing":"order","sink":true}`)
|
|
is(true)
|
|
post("save", `{`+form+`,"from":"relay"}`)
|
|
is(true)
|
|
post("sink", `{"id":"relay","sink":false}`)
|
|
is(false)
|
|
post("sink", `{"id":"relay","sink":true}`)
|
|
post("save", `{`+form+`,"from":"relay"}`)
|
|
is(true)
|
|
}
|
|
|
|
// A decision-only preset's provider (TypeSafe Jev) has no chat URL, so its
|
|
// editor's Save is rebuilt from the preset: what the editor picks beside
|
|
// the key — Routing, Stays, Rate limited, Unlisted, each key's proxy — is
|
|
// kept, not put back to Smart (fflake33 on Discord: In order saved, and
|
|
// reopened as Smart). The body is the editor's, as app.js sends it; its
|
|
// decision API is a closed local port, so the list fetched after a new key
|
|
// reaches no network.
|
|
func TestDecidePresetSaveKeepsRouting(t *testing.T) {
|
|
t.Setenv("HOME", t.TempDir())
|
|
t.Setenv("USERPROFILE", os.Getenv("HOME"))
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
t.Setenv("XDG_CACHE_HOME", t.TempDir())
|
|
mux := http.NewServeMux()
|
|
providerRoutes(mux, nil)
|
|
post := func(action, body string) {
|
|
t.Helper()
|
|
w := httptest.NewRecorder()
|
|
mux.ServeHTTP(w, httptest.NewRequest("POST", "/api/provider/"+action, strings.NewReader(body)))
|
|
if w.Code != 200 {
|
|
t.Fatalf("%s: %d %s", action, w.Code, w.Body)
|
|
}
|
|
}
|
|
post("save", `{"id":"typesafe","name":"TypeSafe Jev","preset":"typesafe","key":"jev-key-1","decide":"http://127.0.0.1:1/v1","models":[],"headers":{},"new":true}`)
|
|
if _, _, err := provider.AddKeys("typesafe", []string{"jev-key-2"}, ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
p, err := provider.Find("typesafe")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if p.Chat != "" || p.Decide == "" || len(p.KeysOn()) != 2 {
|
|
t.Fatalf("not a decision-only provider of two keys: %+v", p)
|
|
}
|
|
second := p.KeyList()[1].ID
|
|
form := `"id":"typesafe","from":"typesafe","name":"TypeSafe Jev","preset":"typesafe","key":"","decide":"http://127.0.0.1:1/v1","models":[],"headers":{},"fallback":[],"noModels":false,"searches":false,"pinUpstream":false,"unredacted":false,"passUserAgent":true,"contexts":{},"proxy":"","maxConcurrency":0`
|
|
post("save", `{`+form+`,"unlisted":true,"accountProxies":{"`+second+`":"http://127.0.0.1:7890"},"routing":"order","affinity":"session","sink":true}`)
|
|
p, err = provider.Find("typesafe")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if p.Routing != provider.Ordered || p.Affinity != provider.AffinitySession || !p.Sink || !p.Unlisted || p.AccountProxies[second] != "http://127.0.0.1:7890" {
|
|
t.Fatalf("after Save: routing %q, affinity %q, sink %v, unlisted %v, proxies %v", p.Routing, p.Affinity, p.Sink, p.Unlisted, p.AccountProxies)
|
|
}
|
|
if got := providerInfo(*p, nil).Routing; got != provider.Ordered {
|
|
t.Fatalf("the editor reopens on %q, want order", got)
|
|
}
|
|
if p.Key != "jev-key-1" || len(p.KeysOn()) != 2 {
|
|
t.Fatalf("keys changed by the Save: %q, %d on", p.Key, len(p.KeysOn()))
|
|
}
|
|
// a Save that doesn't carry them keeps them; Smart picked is "" sent
|
|
post("save", `{`+form+`}`)
|
|
if p, _ = provider.Find("typesafe"); p.Routing != provider.Ordered || p.Affinity != provider.AffinitySession || !p.Sink {
|
|
t.Fatalf("a Save without them: %q %q %v", p.Routing, p.Affinity, p.Sink)
|
|
}
|
|
post("save", `{`+form+`,"routing":"","affinity":"","sink":false}`)
|
|
if p, _ = provider.Find("typesafe"); p.Routing != "" || p.Affinity != "" || p.Sink {
|
|
t.Fatalf("Smart picked: %q %q %v", p.Routing, p.Affinity, p.Sink)
|
|
}
|
|
}
|