cline 0.1.10: pinUpstream pins glm-5.3-flash through OpenRouter's provider.only (sdytman on Discord)

The plugin's side of magpie d2c434b5a. Cline sends glm-5.3-flash through
OpenRouter directly, not Vercel's AI Gateway, and OpenRouter ignores
providerOptions.gateway.only, so pinned() gave it ["zai"] where nothing
reads it. OpenRouter reads a top-level provider.only with its own slug,
"z-ai" for Z.AI. MAKERS is now magpie's clineMakers table: glm-5.3-flash
by exact name -> provider.only ["z-ai"]; deepseek* -> providerOptions
["deepseek"] (deepseek-v4.1-flash and -1017 go through Vercel); glm* ->
providerOptions ["zai"] (glm-5.3-flashx, glm-5.3 stay there). pinned()
returns {upstream, openRouter} or null. pinBody appends the field at the
body's end so the cached prefix is unchanged, and leaves a body naming
provider or providerOptions itself as sent.

Tests: "pinBody pins glm-5.3-flash by OpenRouter's provider.only" and the
loader test now sending glm-5.3-flash; both red on the old index.mjs
(providerOptions zai, no provider), green with the change. Not tried with
a real ClinePass request: no account here.

Run on origin/main 6fd976a: TMPDIR=<scratch> HOME=<scratch>/home bun test
(whole repo) 1919 pass 0 fail across 173 files; bun scripts/check.mjs ok.
This commit is contained in:
yetone
2026-10-21 01:52:44 +08:00
parent 4af310cc57
commit 7a74416a4d
4 changed files with 86 additions and 32 deletions
+6 -2
View File
@@ -69,8 +69,12 @@ Cline's gateway picks among the providers that serve a model. With the
not the `cline-free/` ones) carries `"providerOptions": {"gateway": {"only":
["deepseek"]}}`, so it is answered by DeepSeek's own API only, and one for a
GLM model (`cline-pass/glm-…`) carries `["zai"]`, so it is answered by Z.ai's
own API only — the same as magpie's built-in ClinePass tick. Other models, and a request with
`providerOptions` of its own, go as sent. It is off by default. Turn it on:
own API only — the same as magpie's built-in ClinePass tick. Cline sends
`glm-5.3-flash` through OpenRouter rather than Vercel's gateway, and OpenRouter
ignores `providerOptions`, so that model's request carries OpenRouter's own
`"provider": {"only": ["z-ai"]}` instead (since 0.1.10). Other models, and a
request with `provider` or `providerOptions` of its own, go as sent. It is off
by default. Turn it on:
- in magpie: the provider's editor › Upstream › tick "DeepSeek and GLM
models only from their makers' own APIs";
+38 -21
View File
@@ -774,39 +774,56 @@ function usageOf(me, balance, limits) {
// ---- the plugin --------------------------------------------------------------------
// MAKERS maps a model name's start to its maker's own upstream on Cline's
// gateway (Vercel's AI Gateway, whose provider slugs these are:
// ai-gateway.vercel.sh/v1/models/<model>/endpoints lists "deepseek" for
// deepseek/deepseek-v4-pro and "zai" for zai/glm-5.3)
// MAKERS 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/<model>/endpoints lists "deepseek" for
// deepseek/deepseek-v4-pro and "zai" for zai/glm-5.3). Some go through
// OpenRouter directly, which ignores providerOptions and reads only a
// top-level provider.only with OpenRouter's own slug
// (openrouter.ai/api/v1/providers: Z.AI is "z-ai"): sdytman (#feedback,
// 10-20) found glm-5.3-flash among them, while deepseek-v4.1-flash and
// deepseek-v4.1-flash-1017 go through Vercel. An exact entry is a model
// name; the others match a name's start, the first match winning. magpie's
// built-in ClinePass has the same table (internal/provider/cline.go).
const MAKERS = [
["deepseek", "deepseek"],
["glm", "zai"],
{ name: "glm-5.3-flash", exact: true, upstream: "z-ai", openRouter: true },
{ name: "deepseek", upstream: "deepseek" },
{ name: "glm", upstream: "zai" },
]
// pinned is the upstream a DeepSeek or GLM model of Cline's is pinned to when
// the pinUpstream option is on (magpie's built-in ClinePass has the same
// tick): DeepSeek's own API for DeepSeek's, Z.ai's for GLM. Cline's gateway
// otherwise picks among the providers serving the model, and some answer
// worse than the maker's own API, which also keeps the prompt cache. A
// cline-free/ model is left to Cline, as is every other maker's model.
// pinned is where a DeepSeek or GLM model of Cline's is pinned when the
// pinUpstream option is on (magpie's built-in ClinePass has the same tick):
// { upstream, openRouter }, DeepSeek's own API for DeepSeek's, Z.ai's for
// GLM, openRouter true for a model Cline sends through OpenRouter. Cline's
// routers otherwise pick among the providers serving the model, and some
// answer worse than the maker's own API, which also keeps the prompt cache.
// A cline-free/ model is left to Cline, as is every other maker's model:
// null.
function pinned(on, model) {
if (!on || typeof model !== "string" || model.startsWith("cline-free/")) return ""
if (!on || typeof model !== "string" || model.startsWith("cline-free/")) return null
const name = model.slice(model.lastIndexOf("/") + 1).toLowerCase()
return MAKERS.find(([prefix]) => name.startsWith(prefix))?.[1] ?? ""
const m = MAKERS.find((m) => (m.exact ? name === m.name : name.startsWith(m.name)))
return m ? { upstream: m.upstream, openRouter: !!m.openRouter } : null
}
// pinBody is the request's body with providerOptions.gateway.only set to the
// pinned upstream, written in at the end so the rest of the body — the
// prefix a cache matches — is the client's byte for byte. A body that has
// providerOptions of its own is the client's to keep.
// pinBody is the request's body with the pinned upstream written in at the
// end, so the rest of the body — the prefix a cache matches — is the
// client's byte for byte: providerOptions.gateway.only for a model Cline
// sends through Vercel's gateway, provider.only for one it sends through
// OpenRouter. A body that has provider or providerOptions of its own is the
// client's to keep.
function pinBody(body, chat, on) {
const up = pinned(on, chat?.model)
if (!up || chat.providerOptions !== undefined) return body
const pin = pinned(on, chat?.model)
if (!pin || chat.providerOptions !== undefined || chat.provider !== undefined) return body
const end = body.lastIndexOf("}")
if (end < 0) return body
const head = body.slice(0, end)
const sep = /[{,]\s*$/.test(head) ? "" : ","
return head + sep + `"providerOptions":${JSON.stringify({ gateway: { only: [up] } })}` + body.slice(end)
const field = pin.openRouter
? `"provider":${JSON.stringify({ only: [pin.upstream] })}`
: `"providerOptions":${JSON.stringify({ gateway: { only: [pin.upstream] } })}`
return head + sep + field + body.slice(end)
}
// isOn is whether an option is set: true, or "true" in a config written by hand
+41 -8
View File
@@ -1144,20 +1144,48 @@ test("pinBody pins a DeepSeek model to DeepSeek's API, keeping the body's prefix
`{"model":"deepseek/deepseek-v4-pro","providerOptions":{"gateway":{"only":["fireworks"]}}}`,
`{"model":"cline-pass/glm-5.3","providerOptions":{"gateway":{"order":["novita"]}}}`,
]) expect(pinBody(own, JSON.parse(own), true)).toBe(own)
expect(pinned(true, "DeepSeek/DeepSeek-V4-Pro")).toBe("deepseek")
expect(pinned(true, "DeepSeek/DeepSeek-V4-Pro")).toEqual({ upstream: "deepseek", openRouter: false })
expect(JSON.parse(pinBody(`{}`, {}, true))).toEqual({})
})
test("pinBody pins a GLM model to Z.ai's API, keeping the body's prefix", () => {
const { pinBody, pinned } = _internal
for (const m of ["cline-pass/glm-5.3", "cline-pass/glm-5.3-flash"]) {
const body = `{"model":"${m}","messages":[{"role":"user","content":"hi"}],"stream":true}`
const out = pinBody(body, JSON.parse(body), true)
expect(out.startsWith(body.slice(0, -1))).toBe(true)
expect(JSON.parse(out).providerOptions).toEqual({ gateway: { only: ["zai"] } })
expect(pinBody(body, JSON.parse(body), false)).toBe(body)
const body = `{"model":"cline-pass/glm-5.3","messages":[{"role":"user","content":"hi"}],"stream":true}`
const out = pinBody(body, JSON.parse(body), true)
expect(out.startsWith(body.slice(0, -1))).toBe(true)
expect(JSON.parse(out).providerOptions).toEqual({ gateway: { only: ["zai"] } })
expect(JSON.parse(out).provider).toBeUndefined()
expect(pinBody(body, JSON.parse(body), false)).toBe(body)
expect(pinned(true, "z-ai/GLM-5.3")).toEqual({ upstream: "zai", openRouter: false })
// glm-5.3-flashx is not glm-5.3-flash: it stays on Vercel's gateway
expect(pinned(true, "cline-pass/glm-5.3-flashx")).toEqual({ upstream: "zai", openRouter: false })
})
// sdytman (#feedback, 10-20): Cline sends glm-5.3-flash through OpenRouter,
// not Vercel's AI Gateway, and OpenRouter ignores providerOptions.gateway.only.
// It reads a top-level provider.only with its own slug, "z-ai". DeepSeek's
// v4.1 flash models go through Vercel and keep providerOptions.
test("pinBody pins glm-5.3-flash by OpenRouter's provider.only, keeping the body's prefix", () => {
const { pinBody, pinned } = _internal
const body = `{"model":"cline-pass/glm-5.3-flash","messages":[{"role":"user","content":"hi"}],"stream":true}`
const out = pinBody(body, JSON.parse(body), true)
expect(out.startsWith(body.slice(0, -1))).toBe(true)
expect(JSON.parse(out).provider).toEqual({ only: ["z-ai"] })
expect(JSON.parse(out).providerOptions).toBeUndefined()
expect(pinBody(body, JSON.parse(body), false)).toBe(body)
expect(pinned(true, "cline-pass/GLM-5.3-Flash")).toEqual({ upstream: "z-ai", openRouter: true })
for (const m of ["cline-pass/deepseek-v4.1-flash", "cline-pass/deepseek-v4.1-flash-1017"]) {
const ds = `{"model":"${m}"}`
const got = JSON.parse(pinBody(ds, JSON.parse(ds), true))
expect(got.providerOptions).toEqual({ gateway: { only: ["deepseek"] } })
expect(got.provider).toBeUndefined()
}
expect(pinned(true, "z-ai/GLM-5.3")).toBe("zai")
// the client's own provider or providerOptions is kept, with nothing added beside it
for (const own of [
`{"model":"cline-pass/glm-5.3-flash","provider":{"order":["novita"]}}`,
`{"model":"cline-pass/glm-5.3-flash","providerOptions":{"gateway":{"order":["novita"]}}}`,
`{"model":"cline-pass/deepseek-v4-pro","provider":{"order":["novita"]}}`,
]) expect(pinBody(own, JSON.parse(own), true)).toBe(own)
})
for (const [how, plugin, provider] of [
@@ -1171,14 +1199,19 @@ for (const [how, plugin, provider] of [
[chatUrl, () => Response.json({ ok: true })],
[chatUrl, () => Response.json({ ok: true })],
[chatUrl, () => Response.json({ ok: true })],
[chatUrl, () => Response.json({ ok: true })],
])
const l = await hooks.auth.loader(async () => ({ type: "api", key: "ck" }), provider)
await l.fetch(chatUrl, chatInit({ model: "cline-pass/deepseek-v4-pro", messages: [] }))
await l.fetch(chatUrl, chatInit({ model: "cline-pass/glm-5.3", messages: [] }))
await l.fetch(chatUrl, chatInit({ model: "cline-pass/kimi-k3", messages: [] }))
await l.fetch(chatUrl, chatInit({ model: "cline-pass/glm-5.3-flash", messages: [] }))
expect(JSON.parse(calls[0].init.body).providerOptions).toEqual({ gateway: { only: ["deepseek"] } })
expect(JSON.parse(calls[1].init.body).providerOptions).toEqual({ gateway: { only: ["zai"] } })
expect(JSON.parse(calls[2].init.body).providerOptions).toBeUndefined()
expect(JSON.parse(calls[2].init.body).provider).toBeUndefined()
expect(JSON.parse(calls[3].init.body).provider).toEqual({ only: ["z-ai"] })
expect(JSON.parse(calls[3].init.body).providerOptions).toBeUndefined()
})
}
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-cline-auth",
"version": "0.1.9",
"version": "0.1.10",
"description": "Cline (cline.bot) on your own account — usage billing, ClinePass and the free models — in OpenCode and magpie",
"type": "module",
"main": "./index.mjs",