QwenWork (千问办公) is served from the qoder package as two more provider ids, as the ruling on #313/#314 asked: `qwenwork` (gateway.qwenwork.cn, the desktop client's device flow and account-context usage) and `qwenwork-enterprise` (the enterprise flagship on the enterprise's own domain, asked at sign-in or preset by QWENWORK_ENT_DOMAIN). Chat, the model list and the queue are signed for the site (ClientType 6, scene qwork, cosy 1.1.59, product qoder_work). No PAT and no check-in for QwenWork, so Qoder's desktop identity never reaches it. Qoder and Qoder CN are unchanged. Reviewed at c543f1b (on main 8d687ad; main has no qoder or registry change since), by @LittlefishStudent. Run at c543f1b, sandbox HOME and TMPDIR, network pointed at a dead proxy: - packages/qoder `bun test`: 223 pass, 0 fail. `bun scripts/check.mjs qoder`: ok. - Broken by hand, one at a time: the queue status signed without its site, 1 red; the usage 401 rotation removed, 1 red; the site's system prompt ignored, 1 red; the enterpriseHost routing removed, 6 red. - Read every call of the changed signatures (cosyHeaders, userBlob, queueStatus, queueWait, qoderBody): each passes the site, and a Qoder site has none of the new fields, so its requests are as before (the earlier review's recording probe found them byte-identical). - magpie origin/main 4dfd88206's plugin host, sandbox HOME (scripts/try.sh qoder): qoder 17 models, qoder-cn 4, qwenwork 3, qwenwork-enterprise 3. Not tried against the real API here: no QwenWork account in the sandbox. The author ran sign-in, chats and usage against a real deployment.
333 lines
17 KiB
JavaScript
333 lines
17 KiB
JavaScript
// QwenWork on the qoder protocol: the sign-in shapes, and — with fetch
|
|
// mocked and every outgoing request recorded — that each hook the QwenWork
|
|
// sites set actually reaches the wire: the model list and a chat go out
|
|
// signed for the site (its own client type, scene, version, product and RSA
|
|
// key), the enterprise account's every call goes to its own domain with
|
|
// nothing to gateway.qwenwork.cn, usage reads its account-context, and the
|
|
// 503 catalog falls back where a 500 doesn't. Delete a hook and a test
|
|
// here goes red.
|
|
import { test, expect } from "bun:test"
|
|
|
|
const client = { app: { log: async () => {} }, auth: { set: async () => {} }, tui: { showToast: async () => {} }, config: { get: async () => ({ data: {} }) } }
|
|
const load = async (fresh = "") => {
|
|
const mod = await import(`./index.mjs${fresh}`)
|
|
const pub = await mod.QwenWorkAuthPlugin({ client, directory: process.cwd(), worktree: process.cwd(), $: undefined, project: {} })
|
|
const ent = await mod.QwenWorkEnterpriseAuthPlugin({ client, directory: process.cwd(), worktree: process.cwd(), $: undefined, project: {} })
|
|
return { mod, pub, ent }
|
|
}
|
|
|
|
// the URLs fetch saw, with the headers each was sent with
|
|
const seen = []
|
|
let answer = async () => new Response("{}", { status: 404 })
|
|
const mock = () => {
|
|
const saved = globalThis.fetch
|
|
globalThis.fetch = async (url, init) => {
|
|
seen.push({ url: String(url), headers: { ...(init?.headers ?? {}) } })
|
|
return answer(String(url), init)
|
|
}
|
|
return () => { globalThis.fetch = saved; seen.length = 0 }
|
|
}
|
|
const header = (i, name) => seen[i]?.headers[name]
|
|
|
|
// a signed-in account on the enterprise domain
|
|
const entCred = (domain = "acme.example.com") => ({ type: "oauth", uid: "u1", email: "alice@acme.example.com", name: "Alice", machineId: "m1", access: "a", refresh: "r", expires: Date.now() + 3_600_000, deviceChat: true, deviceToken: "dt", deviceRefresh: "dtr", vpc: domain, enterpriseHost: domain })
|
|
|
|
test("the enterprise sign-in asks for a domain and runs on it, with no redirect_uri", async () => {
|
|
const { ent } = await load()
|
|
const m = ent.auth.methods[0]
|
|
expect(m.prompts.some((p) => p.key === "domain")).toBe(true)
|
|
const flow = await m.authorize({ domain: "acme.example.com" })
|
|
expect(flow.url.startsWith("https://acme.example.com/device/selectAccounts?")).toBe(true)
|
|
expect(flow.url.includes("redirect_uri")).toBe(false)
|
|
// the public cloud keeps its own
|
|
const { pub } = await load()
|
|
const pubFlow = await pub.auth.methods[0].authorize()
|
|
expect(pubFlow.url.includes("redirect_uri=qwenwork-cn%3A%2F%2F")).toBe(true)
|
|
})
|
|
|
|
test("the domain input refuses loopback, intranet, ports, paths and IPs", async () => {
|
|
const { ent } = await load()
|
|
const validate = ent.auth.methods[0].prompts.find((p) => p.key === "domain").validate
|
|
for (const bad of ["not a domain", "1.2.3.4", "localhost", "foo.localhost", "foo.local", "foo.internal", "https://a.example:8443", "https://a.example/path", "http://a.example", "https://u:p@a.example"]) {
|
|
expect(validate(bad)).toBeString()
|
|
}
|
|
for (const good of ["your-company.vpc.qwenwork.cn", "https://your-company.vpc.qwenwork.cn", "https://your-company.vpc.qwenwork.cn/"]) {
|
|
expect(validate(good)).toBeUndefined()
|
|
}
|
|
})
|
|
|
|
test("QWENWORK_ENT_DOMAIN presets the domain", async () => {
|
|
process.env.QWENWORK_ENT_DOMAIN = "preset.example.com"
|
|
try {
|
|
const { ent } = await load("?preset")
|
|
const m = ent.auth.methods[0]
|
|
expect(m.prompts.length).toBe(0)
|
|
const flow = await m.authorize({})
|
|
expect(flow.url.startsWith("https://preset.example.com/")).toBe(true)
|
|
} finally {
|
|
delete process.env.QWENWORK_ENT_DOMAIN
|
|
}
|
|
})
|
|
|
|
test("the enterprise sign-in completes on the enterprise's own host, naming the account with its domain", async () => {
|
|
const { ent } = await load()
|
|
const flow = await ent.auth.methods[0].authorize({ domain: "acme.example.com" })
|
|
const restore = mock()
|
|
answer = async (url) => {
|
|
if (url.includes("/api/v1/deviceToken/poll")) return new Response(JSON.stringify({ token: "dt", refresh_token: "dtr", user_id: "u1" }), { status: 200 })
|
|
if (url.includes("/api/v1/adapter/user/account-context")) return new Response(JSON.stringify({ data: { user: { id: "u1", name: "Alice", email: "alice@acme.example.com" } } }), { status: 200 })
|
|
return new Response("{}", { status: 404 })
|
|
}
|
|
try {
|
|
const acct = await flow.callback()
|
|
expect(acct.accountId).toBe("alice@acme.example.com (acme.example.com)")
|
|
expect(acct.enterpriseHost).toBe("acme.example.com")
|
|
expect(seen.every((r) => r.url.startsWith("https://acme.example.com/"))).toBe(true)
|
|
expect(seen.some((r) => r.url.includes("/api/v1/me/jobToken"))).toBe(false) // deviceOnly: no job token
|
|
} finally {
|
|
restore()
|
|
}
|
|
})
|
|
|
|
test("the model list goes out signed for the site: client type, scene, version, product and its own key", async () => {
|
|
const { ent } = await load()
|
|
const restore = mock()
|
|
answer = async () => new Response(JSON.stringify({ qwork: [{ key: "flash", display_name: "标准", is_vl: false, is_reasoning: false, max_input_tokens: 4096 }] }), { status: 200 })
|
|
try {
|
|
const models = await ent.provider.models({ models: {} }, { auth: entCred() })
|
|
expect(Object.keys(models)).toContain("flash")
|
|
// QwenWork's RSA key is byte-equal to Qoder's (checked: it is), so the
|
|
// blob's key can't tell them apart — the headers can
|
|
expect(header(0, "Cosy-ClientType")).toBe("6")
|
|
expect(header(0, "Cosy-Scene")).toBe("qwork")
|
|
expect(header(0, "Cosy-Business-Product")).toBe("qoder_work")
|
|
expect(header(0, "Cosy-Version")).toBe("1.1.59")
|
|
expect(seen[0].url.startsWith("https://acme.example.com/")).toBe(true)
|
|
} finally {
|
|
restore()
|
|
}
|
|
})
|
|
|
|
test("a chat goes out signed and written for the site: headers and body", async () => {
|
|
const { mod, pub } = await load()
|
|
const restore = mock()
|
|
// the full answer an SSE chat round needs: skipped, as ask() streams; a
|
|
// 400 from upstream exercises the signing path and returns an error
|
|
answer = async (url) => {
|
|
if (url.includes("/model/list")) return new Response(JSON.stringify({ qwork: [{ key: "flash", display_name: "标准", is_vl: false, is_reasoning: false, max_input_tokens: 4096 }] }), { status: 200 })
|
|
return new Response(JSON.stringify({ code: "400", message: "no" }), { status: 400, headers: { "Content-Type": "application/json" } })
|
|
}
|
|
try {
|
|
const cred = { type: "oauth", uid: "u1", email: "a@b.c", name: "A", machineId: "m1", access: "a", refresh: "r", expires: Date.now() + 3_600_000, deviceChat: true, deviceToken: "dt", deviceRefresh: "dtr" }
|
|
const l = await pub.auth.loader(async () => cred, { models: {} })
|
|
const chatRes = await l.fetch("https://gateway.qwenwork.cn/v1/chat/completions", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ model: "flash", messages: [{ role: "user", content: "hi" }], max_tokens: 5 }), // bare: the gateway strips the provider prefix
|
|
})
|
|
expect([400, 502]).toContain(chatRes.status) // the mocked upstream's answer, surfaced
|
|
const chatReq = seen.find((r) => r.url.includes("/agent_chat_generation"))
|
|
expect(chatReq).toBeDefined()
|
|
// signed for the site
|
|
expect(chatReq.headers["Cosy-ClientType"]).toBe("6")
|
|
expect(chatReq.headers["Cosy-Scene"]).toBe("qwork")
|
|
expect(chatReq.headers["Cosy-Business-Product"]).toBe("qoder_work")
|
|
expect(chatReq.headers["Cosy-Version"]).toBe("1.1.59")
|
|
} finally {
|
|
restore()
|
|
}
|
|
})
|
|
|
|
test("the chat body carries the site's session type, business product and system prompt", async () => {
|
|
const { pub } = await load()
|
|
const restore = mock()
|
|
answer = async (url) => {
|
|
if (url.includes("/model/list")) return new Response(JSON.stringify({ qwork: [{ key: "flash", display_name: "标准", is_vl: false, is_reasoning: false, max_input_tokens: 4096 }] }), { status: 200 })
|
|
return new Response(JSON.stringify({ code: "400", message: "no" }), { status: 400, headers: { "Content-Type": "application/json" } })
|
|
}
|
|
// capture the body too
|
|
const savedBody = globalThis.fetch
|
|
const bodies = []
|
|
globalThis.fetch = async (url, init) => {
|
|
seen.push({ url: String(url), headers: { ...(init?.headers ?? {}) } })
|
|
if (init?.body) bodies.push(String(init.body))
|
|
return answer(String(url), init)
|
|
}
|
|
try {
|
|
const cred = { type: "oauth", uid: "u1", email: "a@b.c", name: "A", machineId: "m1", access: "a", refresh: "r", expires: Date.now() + 3_600_000, deviceChat: true, deviceToken: "dt", deviceRefresh: "dtr" }
|
|
const l = await pub.auth.loader(async () => cred, { models: {} })
|
|
await l.fetch("https://gateway.qwenwork.cn/v1/chat/completions", {
|
|
method: "POST", headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ model: "flash", messages: [{ role: "user", content: "hi" }], max_tokens: 5 }),
|
|
})
|
|
const { decodeBody } = (await import("./index.mjs"))._internal
|
|
const chatBody = bodies.map((b) => { try { return JSON.parse(decodeBody(b)) } catch { return null } }).find((x) => x && x.session_type)
|
|
expect(chatBody).toBeDefined()
|
|
expect(chatBody.session_type).toBe("qoder_work") // not "app"
|
|
expect(chatBody.business.product).toBe("qoder_work")
|
|
expect(chatBody.business.version).toBe("1.1.59")
|
|
expect(chatBody.system?.[0]?.text).toContain("You are QwenWork")
|
|
expect(chatBody.system?.[0]?.text).not.toContain("Qoder")
|
|
} finally {
|
|
globalThis.fetch = savedBody
|
|
restore()
|
|
}
|
|
})
|
|
|
|
test("usage reads the account-context on the account's own host, and nothing reaches the public cloud", async () => {
|
|
const { ent } = await load()
|
|
const restore = mock()
|
|
answer = async (url) => {
|
|
if (url.startsWith("https://acme.example.com/")) return new Response(JSON.stringify({ data: { plan: { name: "Enterprise" }, quota: { total: 100, remaining: 60 } } }), { status: 200 })
|
|
return new Response("{}", { status: 404 })
|
|
}
|
|
try {
|
|
const usage = await ent.auth.usage(async () => entCred())
|
|
expect(usage.plan).toBe("Enterprise")
|
|
expect(usage.windows[0].display).toBe("40 / 100 credits")
|
|
expect(seen.some((r) => r.url.includes("gateway.qwenwork.cn"))).toBe(false)
|
|
} finally {
|
|
restore()
|
|
}
|
|
})
|
|
|
|
test("usage renews an expired account rather than marking it lapsed", async () => {
|
|
const { ent } = await load()
|
|
const restore = mock()
|
|
answer = async (url) => {
|
|
if (url.includes("/api/v1/deviceToken/refresh")) return new Response(JSON.stringify({ token: "dt2", refresh_token: "dtr2", expires_in: 3600 }), { status: 200 })
|
|
if (url.startsWith("https://acme.example.com/")) return new Response(JSON.stringify({ data: { plan: { name: "Enterprise" }, quota: { total: 100, remaining: 60 } } }), { status: 200 })
|
|
return new Response("{}", { status: 404 })
|
|
}
|
|
try {
|
|
const expired = { ...entCred(), expires: Date.now() - 1_000 }
|
|
const usage = await ent.auth.usage(async () => expired)
|
|
expect(usage.signIn).not.toBe("expired")
|
|
expect(usage.plan).toBe("Enterprise")
|
|
} finally {
|
|
restore()
|
|
}
|
|
})
|
|
|
|
test("a 503 catalog falls back to the shipped list; a 500 doesn't", async () => {
|
|
const { ent } = await load()
|
|
const restore = mock()
|
|
answer = async (url) => {
|
|
if (url.includes("/model/list")) return new Response(JSON.stringify({ code: "503", message: "Model catalog is unavailable" }), { status: 503 })
|
|
return new Response("{}", { status: 200 })
|
|
}
|
|
try {
|
|
const models = await ent.provider.models({ models: {} }, { auth: entCred() })
|
|
expect(Object.keys(models)).toContain("flash")
|
|
expect(seen.every((r) => r.url.startsWith("https://acme.example.com/"))).toBe(true)
|
|
} finally {
|
|
restore()
|
|
}
|
|
answer = async () => new Response("nope", { status: 500 })
|
|
try {
|
|
const models = await ent.provider.models({ models: {} }, { auth: entCred() })
|
|
expect(Object.keys(models)).not.toContain("flash")
|
|
} finally {
|
|
restore()
|
|
}
|
|
})
|
|
|
|
test("the public plugin offers no PAT and no check-in", async () => {
|
|
const { pub, ent } = await load()
|
|
expect(pub.auth.methods.some((m) => m.label.includes("PAT"))).toBe(false)
|
|
expect(ent.auth.methods.some((m) => m.label.includes("PAT"))).toBe(false)
|
|
expect(pub.auth.checkin).toBeUndefined()
|
|
})
|
|
|
|
|
|
test("usage rotates a refused device token and stays signed in; a refused refresh expires", async () => {
|
|
const { ent } = await load()
|
|
const restore = mock()
|
|
let accountAnswers = 0
|
|
let refreshes = 0
|
|
let refuseRefresh = false
|
|
answer = async (url) => {
|
|
if (url.includes("/api/v1/deviceToken/refresh")) {
|
|
refreshes += 1
|
|
if (refuseRefresh) return new Response(JSON.stringify({ code: "401" }), { status: 401 })
|
|
return new Response(JSON.stringify({ token: "dt2", refresh_token: "dtr2", expires_in: 3600 }), { status: 200 })
|
|
}
|
|
if (url.includes("/api/v1/adapter/user/account-context")) {
|
|
accountAnswers += 1
|
|
// the live device token is refused once, the rotated one succeeds
|
|
if (accountAnswers === 1) return new Response("{}", { status: 401 })
|
|
return new Response(JSON.stringify({ data: { plan: { name: "Enterprise" }, quota: { total: 100, remaining: 60 } } }), { status: 200 })
|
|
}
|
|
return new Response("{}", { status: 404 })
|
|
}
|
|
// a live access token: fresh() doesn't renew; the 401 has to be rotated through
|
|
const live = { ...entCred(), access: "dt", expires: Date.now() + 3_600_000 }
|
|
try {
|
|
const usage = await ent.auth.usage(async () => live)
|
|
expect(usage.signIn).not.toBe("expired")
|
|
expect(usage.plan).toBe("Enterprise")
|
|
expect(refreshes).toBe(1) // the rotation refreshed the device token
|
|
expect(accountAnswers).toBe(2) // refused, then read
|
|
// and when the refresh itself is refused, the account is expired
|
|
refuseRefresh = true
|
|
accountAnswers = 0
|
|
const gone = await ent.auth.usage(async () => ({ ...live, deviceToken: "dt3", expires: Date.now() + 3_600_000 }))
|
|
expect(gone.signIn).toBe("expired")
|
|
} finally {
|
|
restore()
|
|
}
|
|
})
|
|
|
|
test("the device token refresh is asked with target: 'c'", async () => {
|
|
const { ent } = await load()
|
|
const restore = mock()
|
|
const bodies = []
|
|
const saved = globalThis.fetch
|
|
globalThis.fetch = async (url, init) => {
|
|
seen.push({ url: String(url), headers: { ...(init?.headers ?? {}) } })
|
|
if (init?.body) bodies.push(String(init.body))
|
|
if (String(url).includes("/api/v1/deviceToken/refresh")) return new Response(JSON.stringify({ token: "dt2", refresh_token: "dtr2", expires_in: 3600 }), { status: 200 })
|
|
if (String(url).includes("/api/v1/adapter/user/account-context")) return new Response(JSON.stringify({ data: { plan: { name: "Enterprise" }, quota: { total: 100, remaining: 60 } } }), { status: 200 })
|
|
return new Response("{}", { status: 404 })
|
|
}
|
|
try {
|
|
// an expired access token: fresh() renews through refreshDevice
|
|
const expired = { ...entCred(), expires: Date.now() - 1_000 }
|
|
const usage = await ent.auth.usage(async () => expired)
|
|
expect(usage.plan).toBe("Enterprise")
|
|
const refreshBody = bodies.map((b) => { try { return JSON.parse(b) } catch { return null } }).find((x) => x && x.refresh_token)
|
|
expect(refreshBody).toBeDefined()
|
|
expect(refreshBody.target).toBe("c") // QwenWork's refresh target, not Qoder's absent one
|
|
} finally {
|
|
globalThis.fetch = saved
|
|
restore()
|
|
}
|
|
})
|
|
|
|
test("a queued chat's status request is signed for the site", async () => {
|
|
const { pub } = await load()
|
|
const restore = mock()
|
|
// the first chat answer is a queue refusal, as Qoder's own flow sees one;
|
|
// the queue status asked afterwards must carry the site's headers
|
|
answer = async (url) => {
|
|
if (url.includes("/model/list")) return new Response(JSON.stringify({ qwork: [{ key: "flash", display_name: "标准", is_vl: false, is_reasoning: false, max_input_tokens: 4096 }] }), { status: 200 })
|
|
if (url.includes("/agent_chat_generation")) return new Response(JSON.stringify({ code: "10605", message: "request queued", data: { isQueued: true, queueType: "global", modelKey: "flash", retryAfterSeconds: 0 } }), { status: 403, headers: { "Content-Type": "application/json" } })
|
|
if (url.includes("/queue/status")) return new Response(JSON.stringify({ isQueued: false, retryAfterSeconds: 0 }), { status: 200, headers: { "Content-Type": "application/json" } })
|
|
return new Response("{}", { status: 404 })
|
|
}
|
|
try {
|
|
const cred = { type: "oauth", uid: "u1", email: "a@b.c", name: "A", machineId: "m1", access: "a", refresh: "r", expires: Date.now() + 3_600_000, deviceChat: true, deviceToken: "dt", deviceRefresh: "dtr" }
|
|
const l = await pub.auth.loader(async () => cred, { models: {} })
|
|
await l.fetch("https://gateway.qwenwork.cn/v1/chat/completions", {
|
|
method: "POST", headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ model: "flash", messages: [{ role: "user", content: "hi" }], max_tokens: 5 }),
|
|
})
|
|
const statusReq = seen.find((r) => r.url.includes("/queue/status"))
|
|
expect(statusReq).toBeDefined()
|
|
expect(statusReq.headers["Cosy-ClientType"]).toBe("6")
|
|
expect(statusReq.headers["Cosy-Scene"]).toBe("qwork")
|
|
expect(statusReq.headers["Cosy-Version"]).toBe("1.1.59")
|
|
} finally {
|
|
restore()
|
|
}
|
|
}) |