From d2c434b5a6d1d100efdb5a9dee13bf32c3c55fe5 Mon Sep 17 00:00:00 2001 From: yetone Date: Tue, 20 Oct 2026 22:10:06 +0800 Subject: [PATCH] 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. --- internal/gateway/cline_pin.go | 38 ++++++++++------- internal/gateway/cline_pin_test.go | 50 +++++++++++++++++------ internal/gateway/plugin_cline_pin_test.go | 24 +++++++---- internal/plugin/testdata/fake/index.js | 6 ++- internal/provider/cline.go | 44 +++++++++++++------- 5 files changed, 114 insertions(+), 48 deletions(-) diff --git a/internal/gateway/cline_pin.go b/internal/gateway/cline_pin.go index c78cdc075..7c7673bde 100644 --- a/internal/gateway/cline_pin.go +++ b/internal/gateway/cline_pin.go @@ -9,31 +9,41 @@ import ( ) // clinePin is body with its DeepSeek or GLM model pinned to its maker's -// own API (DeepSeek's, Z.ai's) on the Cline API, when the provider asks it (provider.PinUpstream): -// providerOptions.gateway.only, which Cline's AI gateway reads to serve -// the model from that host alone, as ClinePass + ProviderPin set it -// (linux.do/t/topic/2918145). A body naming providerOptions itself is -// left as the client sent it. The field goes at the end, so the prefix a -// cache keys on doesn't change. +// own API (DeepSeek's, Z.ai's) on the Cline API, when the provider asks it +// (provider.PinUpstream). A model Cline sends through Vercel's AI Gateway +// gets providerOptions.gateway.only, which that gateway reads to serve the +// model from that host alone, as ClinePass + ProviderPin set it +// (linux.do/t/topic/2918145); one it sends through OpenRouter gets +// provider.only, OpenRouter's own field (sdytman on Discord). A body +// naming provider or providerOptions itself is left as the client sent +// it. The field goes at the end, so the prefix a cache keys on doesn't +// change. func clinePin(p provider.Provider, to provider.Protocol, body []byte) []byte { if to != provider.Chat { return body } - up := p.ClinePin(gjson.GetBytes(body, "model").String()) - if up == "" || gjson.GetBytes(body, "providerOptions").Exists() { + up, openRouter := p.ClinePin(gjson.GetBytes(body, "model").String()) + if up == "" || gjson.GetBytes(body, "providerOptions").Exists() || gjson.GetBytes(body, "provider").Exists() { return body } end := bytes.LastIndexByte(body, '}') - if end < 0 { + start := bytes.IndexByte(body, '{') + if end < 0 || start < 0 || start > end { return body } - opts, _ := json.Marshal(map[string]any{"gateway": map[string]any{"only": []string{up}}}) - out := make([]byte, 0, len(body)+len(opts)+24) + key, opts := "providerOptions", map[string]any{"gateway": map[string]any{"only": []string{up}}} + if openRouter { + key, opts = "provider", map[string]any{"only": []string{up}} + } + val, _ := json.Marshal(opts) + out := make([]byte, 0, len(body)+len(val)+24) out = append(out, body[:end]...) - if len(bytes.TrimSpace(body[bytes.IndexByte(body, '{')+1:end])) > 0 { + if len(bytes.TrimSpace(body[start+1:end])) > 0 { out = append(out, ',') } - out = append(out, `"providerOptions":`...) - out = append(out, opts...) + out = append(out, '"') + out = append(out, key...) + out = append(out, `":`...) + out = append(out, val...) return append(out, body[end:]...) } diff --git a/internal/gateway/cline_pin_test.go b/internal/gateway/cline_pin_test.go index e1a0e4f06..03a46ba90 100644 --- a/internal/gateway/cline_pin_test.go +++ b/internal/gateway/cline_pin_test.go @@ -16,9 +16,13 @@ import ( // ProviderPin does): each request for one carries // providerOptions.gateway.only ["deepseek"], on Chat and when an Anthropic // or Responses request is built into one. Its GLM models are pinned to -// Z.ai's the same way, ["zai"] (sdytman on Discord). Its other makers' -// models, Cline's free ones, a provider that doesn't pin, and a body -// naming providerOptions itself go as they were. +// Z.ai's the same way, ["zai"] (sdytman on Discord). glm-5.3-flash, which +// Cline sends through OpenRouter, not Vercel's gateway, is pinned by +// OpenRouter's top-level provider.only ["z-ai"] instead, while +// deepseek-v4.1-flash and its -1017 stay on providerOptions (sdytman, +// 10-20). Its other makers' models, Cline's free ones, a provider that +// doesn't pin, and a body naming provider or providerOptions itself go as +// they were. func TestClinePinsDeepSeekAndGLM(t *testing.T) { t.Setenv("XDG_CONFIG_HOME", t.TempDir()) t.Setenv("XDG_CACHE_HOME", t.TempDir()) @@ -27,7 +31,7 @@ func TestClinePinsDeepSeekAndGLM(t *testing.T) { t.Fatal(err) } cline.ID, cline.Key, cline.PinUpstream = "cline", "clp_key", true - cline.Models = []string{"cline-pass/deepseek-v4-pro", "cline-pass/glm-5.3", "cline-pass/glm-5.3-flash", "cline-pass/kimi-k3", "cline-pass/qwen3.8-max", "cline-free/deepseek-v4.1-flash", "cline-free/glm-5.3-flash"} + cline.Models = []string{"cline-pass/deepseek-v4-pro", "cline-pass/glm-5.3", "cline-pass/glm-5.3-flash", "cline-pass/deepseek-v4.1-flash", "cline-pass/deepseek-v4.1-flash-1017", "cline-pass/kimi-k3", "cline-pass/qwen3.8-max", "cline-free/deepseek-v4.1-flash", "cline-free/glm-5.3-flash"} plain := cline plain.ID, plain.Name, plain.PinUpstream = "cline2", "ClinePass 2", false for _, p := range []provider.Provider{cline, plain} { @@ -57,12 +61,15 @@ func TestClinePinsDeepSeekAndGLM(t *testing.T) { } return gjson.GetBytes(sent, "providerOptions").Raw } + orPin := func() string { return gjson.GetBytes(sent, "provider").Raw } chat := func(model, extra string) string { return ask("/v1/chat/completions", `{"model":"`+model+`","messages":[{"role":"user","content":"Say pong"}]`+extra+`}`) } const pinned = `{"gateway":{"only":["deepseek"]}}` - if got := chat("cline/cline-pass/deepseek-v4-pro", ""); got != pinned { - t.Errorf("chat: providerOptions %s, want %s (sent %s)", got, pinned, sent) + for _, m := range []string{"cline/cline-pass/deepseek-v4-pro", "cline/cline-pass/deepseek-v4.1-flash", "cline/cline-pass/deepseek-v4.1-flash-1017"} { + if got := chat(m, ""); got != pinned || orPin() != "" { + t.Errorf("%s: providerOptions %s provider %s, want providerOptions %s alone (sent %s)", m, got, orPin(), pinned, sent) + } } if got := ask("/v1/messages", `{"model":"cline/cline-pass/deepseek-v4-pro","max_tokens":64,"messages":[{"role":"user","content":"Say pong"}]}`); got != pinned { t.Errorf("messages: providerOptions %s, want %s", got, pinned) @@ -71,17 +78,27 @@ func TestClinePinsDeepSeekAndGLM(t *testing.T) { t.Errorf("responses: providerOptions %s, want %s", got, pinned) } const zai = `{"gateway":{"only":["zai"]}}` - for _, m := range []string{"cline/cline-pass/glm-5.3", "cline/cline-pass/glm-5.3-flash"} { - if got := chat(m, ""); got != zai { - t.Errorf("%s: providerOptions %s, want %s (sent %s)", m, got, zai, sent) - } + if got := chat("cline/cline-pass/glm-5.3", ""); got != zai || orPin() != "" { + t.Errorf("glm-5.3: providerOptions %s provider %s, want providerOptions %s alone (sent %s)", got, orPin(), zai, sent) + } + const zaiOR = `{"only":["z-ai"]}` + const flash = `{"model":"cline-pass/glm-5.3-flash","messages":[{"role":"user","content":"Say pong"}]` + if got := chat("cline/cline-pass/glm-5.3-flash", ""); got != "" || orPin() != zaiOR { + t.Errorf("glm-5.3-flash: providerOptions %s provider %s, want provider %s alone (sent %s)", got, orPin(), zaiOR, sent) + } else if !strings.HasPrefix(string(sent), flash) { + // the pin goes after everything the client sent, so a prompt + // cache keyed on the body's start still matches + t.Errorf("glm-5.3-flash: the body's start changed: %s", sent) + } + if ask("/v1/messages", `{"model":"cline/cline-pass/glm-5.3-flash","max_tokens":64,"messages":[{"role":"user","content":"Say pong"}]}`); orPin() != zaiOR { + t.Errorf("messages, glm-5.3-flash: provider %s, want %s (sent %s)", orPin(), zaiOR, sent) } if got := ask("/v1/messages", `{"model":"cline/cline-pass/glm-5.3","max_tokens":64,"messages":[{"role":"user","content":"Say pong"}]}`); got != zai { t.Errorf("messages, GLM: providerOptions %s, want %s", got, zai) } for _, m := range []string{"cline/cline-pass/kimi-k3", "cline/cline-pass/qwen3.8-max", "cline/cline-free/deepseek-v4.1-flash", "cline/cline-free/glm-5.3-flash", "cline2/cline-pass/deepseek-v4-pro", "cline2/cline-pass/glm-5.3"} { - if got := chat(m, ""); got != "" { - t.Errorf("%s: providerOptions %s, want none", m, got) + if got := chat(m, ""); got != "" || orPin() != "" { + t.Errorf("%s: providerOptions %s provider %s, want none", m, got, orPin()) } } for _, m := range []string{"cline/cline-pass/deepseek-v4-pro", "cline/cline-pass/glm-5.3"} { @@ -91,4 +108,13 @@ func TestClinePinsDeepSeekAndGLM(t *testing.T) { t.Errorf("%s: the client's own providerOptions became %s (sent %s)", m, got, sent) } } + // a client's own provider is kept as sent, and nothing is added beside it + for _, m := range []string{"cline/cline-pass/glm-5.3-flash", "cline/cline-pass/deepseek-v4-pro"} { + if got := chat(m, `,"provider":{"order":["novita"]}`); got != "" || orPin() != `{"order":["novita"]}` || strings.Count(string(sent), `"provider"`) != 1 { + t.Errorf("%s: the client's own provider became %s, providerOptions %s (sent %s)", m, orPin(), got, sent) + } + } + if got := chat("cline/cline-pass/glm-5.3-flash", `,"providerOptions":{"gateway":{"order":["novita"]}}`); got != `{"gateway":{"order":["novita"]}}` || orPin() != "" { + t.Errorf("glm-5.3-flash: the client's own providerOptions became %s, provider %s (sent %s)", got, orPin(), sent) + } } diff --git a/internal/gateway/plugin_cline_pin_test.go b/internal/gateway/plugin_cline_pin_test.go index 90c351d27..c2cf1e31c 100644 --- a/internal/gateway/plugin_cline_pin_test.go +++ b/internal/gateway/plugin_cline_pin_test.go @@ -13,8 +13,9 @@ import ( // 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, and its other models' -// requests go as sent. +// 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") @@ -39,8 +40,10 @@ func TestClinePluginPinsDeepSeekAndGLM(t *testing.T) { if err != nil { t.Fatal(err) } - if !p.ClinePinnable() || p.ClinePin("deepseek-v4-pro") != "" || p.ClinePin("glm-5.3") != "" { - t.Fatalf("the Cline plugin's provider: pinnable %v, pinned unasked %q %q", p.ClinePinnable(), p.ClinePin("deepseek-v4-pro"), p.ClinePin("glm-5.3")) + 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() @@ -52,12 +55,13 @@ func TestClinePluginPinsDeepSeekAndGLM(t *testing.T) { {"/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) != 5 { + if len(sent) != 6 { t.Fatalf("the plugin sent %d requests", len(sent)) } return append([]map[string]any(nil), sent...) @@ -66,6 +70,9 @@ func TestClinePluginPinsDeepSeekAndGLM(t *testing.T) { 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 @@ -87,7 +94,10 @@ func TestClinePluginPinsDeepSeekAndGLM(t *testing.T) { t.Fatalf("request %d reached the plugin with providerOptions %s, want %s", i, po, want) } } - if got[4]["providerOptions"] != nil { - t.Fatalf("fake-1 pinned: %v", got[4]["providerOptions"]) + 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"]) } } diff --git a/internal/plugin/testdata/fake/index.js b/internal/plugin/testdata/fake/index.js index 8f7154d09..dc83d038c 100644 --- a/internal/plugin/testdata/fake/index.js +++ b/internal/plugin/testdata/fake/index.js @@ -88,7 +88,11 @@ export const FakePlugin = async ({ client }) => ({ // $FAKE_DEEPSEEK: a DeepSeek model, as Cline's cline-pass/deepseek-v4-pro if (process.env.FAKE_DEEPSEEK) cfg.provider[ID].models["deepseek-v4-pro"] = { name: "DeepSeek V4 Pro", limit: { context: 1000, output: 100 } } // $FAKE_GLM: a GLM model, as Cline's cline-pass/glm-5.3 - if (process.env.FAKE_GLM) cfg.provider[ID].models["glm-5.3"] = { name: "GLM 5.3", limit: { context: 1000, output: 100 } } + if (process.env.FAKE_GLM) { + cfg.provider[ID].models["glm-5.3"] = { name: "GLM 5.3", limit: { context: 1000, output: 100 } } + // Cline sends glm-5.3-flash through OpenRouter, not Vercel's gateway + cfg.provider[ID].models["glm-5.3-flash"] = { name: "GLM 5.3 Flash", limit: { context: 1000, output: 100 } } + } // $FAKE_SEARCHES: the provider's upstream searches by itself (#2027), // as a plugin in front of Claude's API says if (process.env.FAKE_SEARCHES) cfg.provider[ID].searches = true diff --git a/internal/provider/cline.go b/internal/provider/cline.go index 38f66043a..221321aac 100644 --- a/internal/provider/cline.go +++ b/internal/provider/cline.go @@ -174,28 +174,44 @@ func (p Provider) ClinePinnable() bool { return p.IsCline() || p.PluginProvider() == "cline" } -// clineMakers maps a model name's start to its maker's own upstream on -// Cline's AI gateway (Vercel's AI Gateway, whose provider slugs these -// are: ai-gateway.vercel.sh/v1/models//endpoints lists "deepseek" -// for deepseek/deepseek-v4-pro and "zai" for zai/glm-5.3). -var clineMakers = []struct{ prefix, upstream string }{ - {"deepseek", "deepseek"}, - {"glm", "zai"}, // sdytman on Discord: GLM only through Z.ai +// clineMakers maps a model name to its maker's own upstream on the +// router Cline sends it through. Most go through Vercel's AI Gateway, +// pinned by providerOptions.gateway.only with Vercel's provider slug +// (ai-gateway.vercel.sh/v1/models//endpoints lists "deepseek" for +// deepseek/deepseek-v4-pro and "zai" for zai/glm-5.3). Some go through +// OpenRouter directly, which reads only a top-level provider.only with +// OpenRouter's slug (openrouter.ai/api/v1/providers: Z.AI is "z-ai"): +// sdytman (#feedback, 10-20) found glm-5.3-flash among them, and +// deepseek-v4.1-flash and deepseek-v4.1-flash-1017 going through Vercel. +// An exact entry is a model name; the others match a name's start, the +// first match winning. +var clineMakers = []struct { + name string + exact bool + upstream string + openRouter bool +}{ + {name: "glm-5.3-flash", exact: true, upstream: "z-ai", openRouter: true}, + {name: "deepseek", upstream: "deepseek"}, + {name: "glm", upstream: "zai"}, // sdytman on Discord: GLM only through Z.ai } // ClinePin is the upstream a request for model is pinned to on p -// (PinUpstream): "deepseek" for a DeepSeek model of the Cline API, "zai" -// for a GLM one, "" for anything else. Cline's free models are served +// (PinUpstream), and whether Cline sends the model through OpenRouter +// (pinned by provider.only) rather than Vercel's AI Gateway (pinned by +// providerOptions.gateway.only): "deepseek" for a DeepSeek model of the +// Cline API, "zai" for a GLM one, "z-ai" through OpenRouter for +// glm-5.3-flash, "" for anything else. Cline's free models are served // where Cline puts them, so they never are. -func (p Provider) ClinePin(model string) string { +func (p Provider) ClinePin(model string) (upstream string, openRouter bool) { if !p.PinUpstream || !p.ClinePinnable() || strings.HasPrefix(model, clineFreePrefix) { - return "" + return "", false } name := strings.ToLower(model[strings.LastIndex(model, "/")+1:]) for _, m := range clineMakers { - if strings.HasPrefix(name, m.prefix) { - return m.upstream + if m.exact && name == m.name || !m.exact && strings.HasPrefix(name, m.name) { + return m.upstream, m.openRouter } } - return "" + return "", false }