diff --git a/packages/cline/README.md b/packages/cline/README.md index 147f724..256950d 100644 --- a/packages/cline/README.md +++ b/packages/cline/README.md @@ -53,6 +53,21 @@ apart too. The model list is read from needs a sign-in); usage reads `/api/v1/users/me` and the account's `/balance` (its balance is counted in millionths of a dollar). +### DeepSeek models only through DeepSeek's own API + +Cline's gateway picks among the providers that serve a model. With the +`pinUpstream` option on, a request for a DeepSeek model (`cline-pass/deepseek-…`, +not the `cline-free/` ones) carries `"providerOptions": {"gateway": {"only": +["deepseek"]}}`, so it is answered by DeepSeek'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: + +- in magpie: the provider's editor › Upstream › tick "DeepSeek models only + through DeepSeek's own upstream"; +- in OpenCode: `"provider": {"cline": {"options": {"pinUpstream": true}}}` in + `opencode.json`, or the plugin's own options: + `"plugin": [["@magpie-community/opencode-cline-auth", {"pinUpstream": true}]]`. + ## Models The list is Cline's own, merged from the two feeds its clients read: the diff --git a/packages/cline/index.mjs b/packages/cline/index.mjs index f373695..794ec42 100644 --- a/packages/cline/index.mjs +++ b/packages/cline/index.mjs @@ -674,7 +674,34 @@ function usageOf(me, balance) { // ---- the plugin -------------------------------------------------------------------- -export const ClinePlugin = async ({ client } = {}) => { +// pinned is the upstream a DeepSeek model of Cline's is pinned to when the +// pinUpstream option is on (magpie's built-in ClinePass has the same tick): +// Cline's gateway otherwise picks among the providers serving the model, and +// some answer DeepSeek's models worse than DeepSeek's own API. A cline-free/ +// model is left to Cline, as is every model that isn't DeepSeek's. +function pinned(on, model) { + if (!on || typeof model !== "string" || model.startsWith("cline-free/")) return "" + return model.slice(model.lastIndexOf("/") + 1).toLowerCase().startsWith("deepseek") ? "deepseek" : "" +} + +// 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. +function pinBody(body, chat, on) { + const up = pinned(on, chat?.model) + if (!up || chat.providerOptions !== 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) +} + +// isOn is whether an option is set: true, or "true" in a config written by hand +const isOn = (v) => v === true || v === "true" + +export const ClinePlugin = async ({ client } = {}, options = {}) => { // the accounts fresh renewed this run: the sign-in mark comes off on the // renewed token, whatever the request then met const renewals = new WeakSet() @@ -845,8 +872,11 @@ export const ClinePlugin = async ({ client } = {}) => { return out }), - async loader(getAuth) { + async loader(getAuth, provider) { const a = await getAuth() + // pinUpstream: the plugin's own option, or the provider's in + // OpenCode's config (provider.cline.options.pinUpstream) + const pin = isOn(options?.pinUpstream) || isOn(provider?.options?.pinUpstream) if (a?.type !== "oauth" && a?.type !== "api") return {} return { baseURL: API, @@ -862,6 +892,7 @@ export const ClinePlugin = async ({ client } = {}) => { body = await bodyText(input, init) const chat = JSON.parse(body) if (!chat || typeof chat !== "object" || Array.isArray(chat)) throw new Error("not a chat completion") + body = pinBody(body, chat, pin) } catch { return errorResponse({ status: 400, message: "a request that isn't a chat completion" }) } @@ -939,6 +970,8 @@ export const _internal = { failure, errorResponse, bodyText, + pinned, + pinBody, unwrapped, clientHeaders, deviceAuthorize, diff --git a/packages/cline/index.test.mjs b/packages/cline/index.test.mjs index 3cc2937..98b7816 100644 --- a/packages/cline/index.test.mjs +++ b/packages/cline/index.test.mjs @@ -885,3 +885,52 @@ test("an auth entry is read the same however it was stored", () => { expect(authOf({ type: "api", key: "k" }).type).toBe("api") expect(authOf(null).type).toBe("oauth") }) + +// ---- pinUpstream: DeepSeek's models only through DeepSeek's own API ------------- + +test("pinBody pins a DeepSeek model to DeepSeek's API, keeping the body's prefix", () => { + const { pinBody, pinned } = _internal + const body = `{"model":"cline-pass/deepseek-v4.1-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).providerOptions).toEqual({ gateway: { only: ["deepseek"] } }) + // off, another vendor's model, a free model, or the client's own options: as sent + expect(pinBody(body, JSON.parse(body), false)).toBe(body) + const glm = `{"model":"cline-pass/glm-5.3"}` + expect(pinBody(glm, JSON.parse(glm), true)).toBe(glm) + const free = `{"model":"cline-free/deepseek-v4.1-flash"}` + expect(pinBody(free, JSON.parse(free), true)).toBe(free) + const own = `{"model":"deepseek/deepseek-v4-pro","providerOptions":{"gateway":{"only":["fireworks"]}}}` + expect(pinBody(own, JSON.parse(own), true)).toBe(own) + expect(pinned(true, "DeepSeek/DeepSeek-V4-Pro")).toBe("deepseek") + expect(JSON.parse(pinBody(`{}`, {}, true))).toEqual({}) +}) + +for (const [how, plugin, provider] of [ + ["the plugin's own option", { pinUpstream: true }, undefined], + ["OpenCode's provider.cline.options", undefined, { id: "cline", options: { pinUpstream: true } }], +]) { + test(`pinUpstream set as ${how} pins DeepSeek requests, and only them`, async () => { + const { client: c } = client() + const hooks = await ClinePlugin({ client: c }, plugin) + serve([ + [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/kimi-k3", messages: [] })) + expect(JSON.parse(calls[0].init.body).providerOptions).toEqual({ gateway: { only: ["deepseek"] } }) + expect(JSON.parse(calls[1].init.body).providerOptions).toBeUndefined() + }) +} + +test("without pinUpstream a DeepSeek request goes as sent", async () => { + const { client: c } = client() + const hooks = await ClinePlugin({ client: c }) + serve([[chatUrl, () => Response.json({ ok: true })]]) + const l = await hooks.auth.loader(async () => ({ type: "api", key: "ck" }), { id: "cline", options: {} }) + const sent = { model: "cline-pass/deepseek-v4-pro", messages: [] } + await l.fetch(chatUrl, chatInit(sent)) + expect(calls[0].init.body).toBe(JSON.stringify(sent)) +}) diff --git a/packages/cline/package.json b/packages/cline/package.json index 021de41..b9d65a8 100644 --- a/packages/cline/package.json +++ b/packages/cline/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-cline-auth", - "version": "0.1.2", + "version": "0.1.3", "description": "Cline (cline.bot) on your own account — usage billing, ClinePass and the free models — in OpenCode and magpie", "type": "module", "main": "./index.mjs",