diff --git a/packages/qoder/index.mjs b/packages/qoder/index.mjs index e4d48a8..a1df7b5 100644 --- a/packages/qoder/index.mjs +++ b/packages/qoder/index.mjs @@ -1314,6 +1314,9 @@ const makePlugin = (site) => async ({ client }) => { } // each account's listing, the model configs a request carries const listings = new Map() + // A public and a VPC account can name the same uid but have different + // catalogs. Keep each deployment's listing apart. + const listingKey = (cred) => `${cred.vpc ?? ""}\0${cred.uid}` // the accounts fresh renewed: the built-in took the lapse mark off on a // renewed job token (qoderPersist), whatever the request then met const renewals = new WeakSet() @@ -1322,7 +1325,7 @@ const makePlugin = (site) => async ({ client }) => { // says "renewed" for it const unsaid = new Set() const renewed = (cred) => { - const was = unsaid.delete(cred.uid) + const was = unsaid.delete(listingKey(cred)) return renewals.has(cred) || was } @@ -1343,10 +1346,11 @@ const makePlugin = (site) => async ({ client }) => { }) const models = async (cred, again = false) => { - let l = listings.get(cred.uid) + const key = listingKey(cred) + let l = listings.get(key) if (!l || again) { l = modelInfos(await fetchListing(siteOf(site, cred), cred)) - listings.set(cred.uid, l) + listings.set(key, l) } return l } @@ -1559,7 +1563,7 @@ const makePlugin = (site) => async ({ client }) => { if (e?.expired) throw Object.assign(new Error(e.message), { signIn: "expired" }) return provider.models } - if (renewals.has(cred)) unsaid.add(cred.uid) + if (renewals.has(cred)) unsaid.add(listingKey(cred)) try { const ms = await models(cred, true) if (!ms.length) return provider.models diff --git a/packages/qoder/package.json b/packages/qoder/package.json index 22bff7d..c980544 100644 --- a/packages/qoder/package.json +++ b/packages/qoder/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-qoder-auth", - "version": "0.2.10", + "version": "0.2.11", "description": "Qoder subscriptions in OpenCode and magpie: Qoder (qoder.com) and Qoder CN (qoder.cn)", "type": "module", "main": "./index.mjs", diff --git a/packages/qoder/vpc.test.mjs b/packages/qoder/vpc.test.mjs index 3e3e4ec..1d0ed1c 100644 --- a/packages/qoder/vpc.test.mjs +++ b/packages/qoder/vpc.test.mjs @@ -233,6 +233,54 @@ test("a VPC account's chat and list go to its gateway host, not the public one", noPublic() }) +test("public and VPC accounts with the same uid keep separate model listings", async () => { + toServer() + const publicGateway = "gateway.qoder.com.cn" + route = (at) => { + if (at === publicGateway + "/algo/api/v2/model/list") return json({ chat: [{ ...MODEL, display_name: "Public Performance" }] }) + if (at === GATEWAY + "/algo/api/v2/model/list") return json({ chat: [{ ...MODEL, display_name: "VPC Performance" }] }) + if (at === publicGateway + "/algo/api/v2/service/pro/sse/agent_chat_generation" || at === GATEWAY + "/algo/api/v2/service/pro/sse/agent_chat_generation") + return sse({ choices: [{ index: 0, delta: { content: "OK" }, finish_reason: "stop" }] }) + return new Response("", { status: 404 }) + } + const publicAccount = store(signedIn({ vpc: undefined, accountId: "alice@corp.example" })) + const enterpriseAccount = store(signedIn()) + const hooks = await QoderCNAuthPlugin({ client: publicAccount.client }) + const publicLoader = await hooks.auth.loader(publicAccount.get) + const enterpriseLoader = await hooks.auth.loader(enterpriseAccount.get) + expect((await publicLoader.fetch(publicLoader.baseURL + "/chat/completions", { method: "POST", body: JSON.stringify(chat) })).status).toBe(200) + expect((await enterpriseLoader.fetch(enterpriseLoader.baseURL + "/chat/completions", { method: "POST", body: JSON.stringify(chat) })).status).toBe(200) + expect(seen.map((x) => x.host + x.path)).toEqual([ + publicGateway + "/algo/api/v2/model/list", + publicGateway + "/algo/api/v2/service/pro/sse/agent_chat_generation", + GATEWAY + "/algo/api/v2/model/list", + GATEWAY + "/algo/api/v2/service/pro/sse/agent_chat_generation", + ]) +}) + +test("public and VPC accounts with the same uid keep separate renewal marks", async () => { + toServer() + const publicOpenapi = "openapi.qoder.com.cn" + const publicGateway = "gateway.qoder.com.cn" + route = (at) => { + if (at === publicOpenapi + "/api/v1/deviceToken/refresh") + return json({ device_token: "dt-public-2", refresh_token: "drt-public-2", expires_at: new Date(Date.now() + 7_200_000).toISOString() }) + if (at === publicGateway + "/algo/api/v2/model/list" || at === GATEWAY + "/algo/api/v2/model/list") return json({ chat: [MODEL] }) + if (at === publicGateway + "/algo/api/v2/service/pro/sse/agent_chat_generation" || at === GATEWAY + "/algo/api/v2/service/pro/sse/agent_chat_generation") + return sse({ choices: [{ index: 0, delta: { content: "OK" }, finish_reason: "stop" }] }) + return new Response("", { status: 404 }) + } + const publicAccount = store(signedIn({ vpc: undefined, accountId: "alice@corp.example", expires: Date.now() + 60_000 })) + const enterpriseAccount = store(signedIn()) + const hooks = await QoderCNAuthPlugin({ client: publicAccount.client }) + await hooks.provider.models({ models: {} }, { auth: publicAccount.auth() }) + const enterpriseLoader = await hooks.auth.loader(enterpriseAccount.get) + const enterpriseResponse = await enterpriseLoader.fetch(enterpriseLoader.baseURL + "/chat/completions", { method: "POST", body: JSON.stringify(chat) }) + expect(enterpriseResponse.headers.get("X-Magpie-Sign-In")).toBe("kept") + const publicLoader = await hooks.auth.loader(publicAccount.get) + const publicResponse = await publicLoader.fetch(publicLoader.baseURL + "/chat/completions", { method: "POST", body: JSON.stringify(chat) }) + expect(publicResponse.headers.get("X-Magpie-Sign-In")).toBe("renewed") +}) test("a VPC device token is renewed on its openapi host with the reporter's refresh shape", async () => { toServer() route = (at, { body }) => {