Files
magpie/internal/gateway/plugin_cline_pin_test.go
T
yetone d2c434b5a6 gateway: ClinePass pins glm-5.3-flash through OpenRouter's provider.only (sdytman on Discord)
sdytman (#feedback, 10-20) found that Cline doesn't send every model
through Vercel's AI Gateway: glm-5.3-flash goes through OpenRouter
directly, which ignores providerOptions.gateway.only, so the Upstream
tick (dd70c5e6a) didn't pin it. OpenRouter reads a top-level
provider.only with its own slug, "z-ai" for Z.AI
(openrouter.ai/api/v1/providers; its glm-5.3-flash endpoints list
"Z.AI z-ai/fp8"). deepseek-v4.1-flash and deepseek-v4.1-flash-1017 do go
through Vercel, so they keep providerOptions.gateway.only ["deepseek"].

provider.ClinePin now returns the upstream and whether the model's route
is OpenRouter, from a table whose first entry is glm-5.3-flash by exact
name (glm-5.3-flashx and glm-5.3 stay on Vercel's "zai" as before; which
router Cline uses isn't visible from its feed or either router's model
list, both of which list every one of these models). clinePin appends
"provider":{"only":["z-ai"]} at the body's end for such a model, so the
cached prefix is unchanged, and leaves a body naming provider or
providerOptions itself as sent. Applies to the Cline plugin's provider
too, since magpie writes its requests before the plugin sends them.

Not tried with a real request: there is no Cline account here.

Broken by hand, each red: no glm-5.3-flash entry (gets providerOptions
zai), a client's own provider field not checked (two pin keys), and
providerOptions for every model (flash has no provider.only). Not tried
with a real ClinePass request: no account here.

Run: gofmt -l (none); go vet ./... on darwin, GOOS=linux -tags nogui and
GOOS=windows: clean. `set -o pipefail; go test -timeout 45m ./...` at
1ebda9026 (this batch on 318697dce, movers then at qoder 0.2.21,
workbuddy 0.1.26, no devin raise): 65 packages ok, exit 0. Rebased onto
a9304efe0 (codex images, codex list tag, sessions decoders): gofmt, the
three vets and go test of internal/gateway, internal/provider,
internal/plugin and internal/sessions ok. Then the mover mins were raised
to qoder 0.2.23, workbuddy 0.1.27 and devin 0.1.21 (the only change
since, three strings): gofmt, the three vets of internal/provider and
go test ./internal/provider/ ok at the head of this batch.
2026-10-21 01:11:05 +08:00

104 lines
4.0 KiB
Go

package gateway
import (
"encoding/json"
"io"
"net/http"
"sync"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// The Cline plugin's provider has ClinePass's Upstream tick too (ARNO on
// Discord): saved on it, a DeepSeek model's request reaches the plugin
// pinned to DeepSeek's own API and a GLM model's to Z.ai's (sdytman on
// Discord), on Chat and Anthropic Messages alike, glm-5.3-flash by
// OpenRouter's provider.only since Cline sends it there, and its other
// models' requests go as sent.
func TestClinePluginPinsDeepSeekAndGLM(t *testing.T) {
t.Setenv("FAKE_DEEPSEEK", "1")
t.Setenv("FAKE_GLM", "1")
var mu sync.Mutex
var sent []map[string]any
pid := besideFake(t, "cline", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var q map[string]any
b, _ := io.ReadAll(r.Body)
json.Unmarshal(b, &q)
mu.Lock()
sent = append(sent, q)
mu.Unlock()
if q["stream"] == true {
w.Header().Set("Content-Type", "text/event-stream")
io.WriteString(w, sse(`data: {"id":"c1","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"role":"assistant","content":"hi"},"finish_reason":"stop"}]}`, `data: [DONE]`))
return
}
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, `{"id":"c1","object":"chat.completion","choices":[{"index":0,"message":{"role":"assistant","content":"hi"},"finish_reason":"stop"}],"usage":{"prompt_tokens":3,"completion_tokens":1}}`)
}))
p, err := provider.Find(pid)
if err != nil {
t.Fatal(err)
}
ds, _ := p.ClinePin("deepseek-v4-pro")
glm, _ := p.ClinePin("glm-5.3")
if !p.ClinePinnable() || ds != "" || glm != "" {
t.Fatalf("the Cline plugin's provider: pinnable %v, pinned unasked %q %q", p.ClinePinnable(), ds, glm)
}
ask := func() []map[string]any {
mu.Lock()
sent = nil
mu.Unlock()
for _, req := range []struct{ path, body string }{
{"/v1/chat/completions", `{"model":"` + pid + `/deepseek-v4-pro","messages":[{"role":"user","content":"hi"}]}`},
{"/v1/messages", `{"model":"` + pid + `/deepseek-v4-pro","max_tokens":10,"messages":[{"role":"user","content":"hi"}]}`},
{"/v1/chat/completions", `{"model":"` + pid + `/glm-5.3","messages":[{"role":"user","content":"hi"}]}`},
{"/v1/messages", `{"model":"` + pid + `/glm-5.3","max_tokens":10,"messages":[{"role":"user","content":"hi"}]}`},
{"/v1/chat/completions", `{"model":"` + pid + `/fake-1","messages":[{"role":"user","content":"hi"}]}`},
{"/v1/chat/completions", `{"model":"` + pid + `/glm-5.3-flash","messages":[{"role":"user","content":"hi"}]}`},
} {
postProto(t, New(), req.path, req.body)
}
mu.Lock()
defer mu.Unlock()
if len(sent) != 6 {
t.Fatalf("the plugin sent %d requests", len(sent))
}
return append([]map[string]any(nil), sent...)
}
for i, q := range ask() {
if q["providerOptions"] != nil {
t.Fatalf("request %d pinned with the tick off: %v", i, q["providerOptions"])
}
if q["provider"] != nil {
t.Fatalf("request %d pinned with the tick off: provider %v", i, q["provider"])
}
}
// the tick, saved as the editor saves it, is kept on the account
p.PinUpstream = true
if err := provider.Save(*p); err != nil {
t.Fatal(err)
}
if p, err = provider.Find(pid); err != nil || !p.PinUpstream {
t.Fatalf("the tick wasn't kept: %+v, %v", p, err)
}
got := ask()
for i, q := range got[:4] {
want := `{"gateway":{"only":["deepseek"]}}`
if i >= 2 {
want = `{"gateway":{"only":["zai"]}}`
}
po, _ := json.Marshal(q["providerOptions"])
if string(po) != want {
t.Fatalf("request %d reached the plugin with providerOptions %s, want %s", i, po, want)
}
}
if got[4]["providerOptions"] != nil || got[4]["provider"] != nil {
t.Fatalf("fake-1 pinned: %v %v", got[4]["providerOptions"], got[4]["provider"])
}
if po, _ := json.Marshal(got[5]["provider"]); string(po) != `{"only":["z-ai"]}` || got[5]["providerOptions"] != nil {
t.Fatalf("glm-5.3-flash reached the plugin with provider %s and providerOptions %v, want provider.only [z-ai] alone", po, got[5]["providerOptions"])
}
}