@magpie-community/opencode-qoder-auth 0.2.40 → 0.2.42
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.mjs +98 -17
- package/package.json +1 -1
package/index.mjs
CHANGED
|
@@ -38,8 +38,9 @@ import { createCipheriv, createHash, publicEncrypt, randomBytes, randomUUID, con
|
|
|
38
38
|
import { execFile } from "node:child_process"
|
|
39
39
|
import { readdir, stat } from "node:fs/promises"
|
|
40
40
|
import { STATUS_CODES } from "node:http"
|
|
41
|
-
import { homedir } from "node:os"
|
|
41
|
+
import { homedir, hostname } from "node:os"
|
|
42
42
|
import { join, resolve } from "node:path"
|
|
43
|
+
import { domainToASCII } from "node:url"
|
|
43
44
|
|
|
44
45
|
const CHAT_PATH = "/algo/api/v2/service/pro/sse/agent_chat_generation?FetchKeys=llm_model_result&AgentId=agent_common&Encode=1"
|
|
45
46
|
const MODELS_PATH = "/algo/api/v2/model/list?Encode=1"
|
|
@@ -73,10 +74,33 @@ const hexID = () => randomUUID().replaceAll("-", "")
|
|
|
73
74
|
|
|
74
75
|
// ---- the COSY envelope --------------------------------------------------------
|
|
75
76
|
|
|
76
|
-
// machineOS names the machine as Qoder's desktop
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
77
|
+
// machineOS names the machine as Qoder's desktop client does: Node's
|
|
78
|
+
// process.platform, so x86_64_win32 on Windows, aarch64_darwin on a Mac
|
|
79
|
+
// (its worker runtime's COSY_MACHINE_OS, Qoder 0.4.3). Only x64 and arm64
|
|
80
|
+
// are renamed; any other arch goes as Node names it.
|
|
81
|
+
function machineOS(arch = process.arch, platform = process.platform) {
|
|
82
|
+
return ({ x64: "x86_64", arm64: "aarch64" }[arch] ?? arch) + "_" + platform
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// machineHostname is the Cosy-MachineHostname Qoder's client puts on a chat
|
|
86
|
+
// request: os.hostname(), sent as it is when it is printable ASCII, else
|
|
87
|
+
// its punycode, else made safe and marked with a hash of the original, at
|
|
88
|
+
// most 96 characters (its zei). "" (none is sent) for an empty name.
|
|
89
|
+
const HOST_SAFE = /^[\x21-\x7e](?:[\x20-\x7e]*[\x21-\x7e])?$/u
|
|
90
|
+
function machineHostname(name = hostname()) {
|
|
91
|
+
const hash = (v) => createHash("sha256").update(v, "utf8").digest("hex").slice(0, 8)
|
|
92
|
+
const cap = (v) => {
|
|
93
|
+
if (v.length <= 96) return v
|
|
94
|
+
const head = v.slice(0, 96 - 8 - 1).replace(/[-\s]+$/u, "")
|
|
95
|
+
return head ? `${head}-${hash(v)}` : `unknown-${hash(v)}`
|
|
96
|
+
}
|
|
97
|
+
const v = String(name ?? "").trim()
|
|
98
|
+
if (!v) return ""
|
|
99
|
+
if (HOST_SAFE.test(v)) return cap(v)
|
|
100
|
+
const ascii = domainToASCII(v)
|
|
101
|
+
if (ascii && HOST_SAFE.test(ascii)) return cap(ascii)
|
|
102
|
+
const safe = v.replace(/[^\x21-\x7e]+/gu, "-").replace(/-{2,}/gu, "-").replace(/^-+|-+$/gu, "")
|
|
103
|
+
return cap(safe ? `${safe}-${hash(v)}` : `unknown-${hash(v)}`)
|
|
80
104
|
}
|
|
81
105
|
|
|
82
106
|
// userBlob is the account as the envelope carries it: AES-128-CBC under a
|
|
@@ -98,7 +122,9 @@ function cosyHeaders(url, u, body, site, ts = Math.floor(Date.now() / 1000)) {
|
|
|
98
122
|
const cosyVersion = site?.cosyVersion ?? COSY_VERSION
|
|
99
123
|
const blob = userBlob(u, site?.rsaKey)
|
|
100
124
|
const payload = Buffer.from(
|
|
101
|
-
|
|
125
|
+
// the request id is a dashed UUID, as Qoder's signer makes it; its
|
|
126
|
+
// ideVersion is "" too
|
|
127
|
+
JSON.stringify({ version: "v1", requestId: randomUUID(), info: blob.info, cosyVersion, ideVersion: "" }),
|
|
102
128
|
).toString("base64")
|
|
103
129
|
let path = new URL(url).pathname
|
|
104
130
|
if (path.startsWith("/algo")) path = path.slice(5)
|
|
@@ -127,6 +153,25 @@ function cosyHeaders(url, u, body, site, ts = Math.floor(Date.now() / 1000)) {
|
|
|
127
153
|
}
|
|
128
154
|
}
|
|
129
155
|
|
|
156
|
+
// chatHeaders are the headers of a chat (agent_chat_generation) call, as
|
|
157
|
+
// Qoder's client builds them (its prepareInferRequest, then Vai): no
|
|
158
|
+
// Cosy-ClientIp; Cosy-MachineToken and Cosy-MachineType are its device
|
|
159
|
+
// identity's (the runtime-info the check-in reads), or left off when it has
|
|
160
|
+
// none, rather than the machine id cosyHeaders puts there; and the
|
|
161
|
+
// machine's hostname. Every other call keeps cosyHeaders' set, which is
|
|
162
|
+
// its prepareRequest's: there ClientIp, MachineId and MachineToken are all
|
|
163
|
+
// the machine id (#443).
|
|
164
|
+
function chatHeaders(url, u, body, site, model, identity = {}) {
|
|
165
|
+
const h = cosyHeaders(url, u, body, site)
|
|
166
|
+
delete h["Cosy-ClientIp"]
|
|
167
|
+
delete h["Cosy-MachineToken"]
|
|
168
|
+
delete h["Cosy-MachineType"]
|
|
169
|
+
for (const key of ["Cosy-MachineToken", "Cosy-MachineType"]) if (identity[key]) h[key] = identity[key]
|
|
170
|
+
const host = machineHostname()
|
|
171
|
+
if (host) h["Cosy-MachineHostname"] = host
|
|
172
|
+
return { ...h, Accept: "text/event-stream", "Cache-Control": "no-cache", "X-Model-Key": model.key, "X-Model-Source": model.source }
|
|
173
|
+
}
|
|
174
|
+
|
|
130
175
|
// ---- the body codec -----------------------------------------------------------
|
|
131
176
|
|
|
132
177
|
// Base64 through the client's shuffled alphabet ('$' pads), then the first
|
|
@@ -747,6 +792,28 @@ function callID(at, k, c) {
|
|
|
747
792
|
return "call_" + createHash("sha256").update(`${at}\0${k}\0${c.function?.name ?? ""}\0${said}`).digest("hex").slice(0, 32)
|
|
748
793
|
}
|
|
749
794
|
|
|
795
|
+
// reasoningOf is the thinking an assistant turn of the chat carries back:
|
|
796
|
+
// reasoning_content (DeepSeek's spelling, which magpie and most clients
|
|
797
|
+
// send), else reasoning (OpenRouter's), else reasoning_details' text, ""
|
|
798
|
+
// when it carries none.
|
|
799
|
+
function reasoningOf(m) {
|
|
800
|
+
let text = ""
|
|
801
|
+
for (const k of ["reasoning_content", "reasoning"]) if (!text && typeof m[k] === "string") text = m[k]
|
|
802
|
+
if (!text && Array.isArray(m.reasoning_details)) text = m.reasoning_details.map((d) => (typeof d?.text === "string" ? d.text : typeof d?.summary === "string" ? d.summary : "")).join("")
|
|
803
|
+
// A blank one (magpie's gateway sends " " for a tool turn with no
|
|
804
|
+
// thinking) is no thinking at all.
|
|
805
|
+
return text.trim() ? text : ""
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
// An assistant turn's thinking goes back in reasoning_content, as Qoder's
|
|
809
|
+
// own client sends it (qoder-agent-sdk, Qoder.app 0.x: its converter sets
|
|
810
|
+
// reasoning_content on every earlier assistant turn that had thinking).
|
|
811
|
+
// It was left out from the plugin's first version on, so a model behind
|
|
812
|
+
// Qoder never saw what it had thought on the turns before (uk.jent on
|
|
813
|
+
// Discord): on Qoder, DeepSeek-V4-Pro in a tool loop now recalls what its
|
|
814
|
+
// thinking before the call said, and said it had none without it. A turn
|
|
815
|
+
// with thinking and nothing else is still left out, as that client leaves
|
|
816
|
+
// it.
|
|
750
817
|
function qoderMessages(msgs) {
|
|
751
818
|
const out = []
|
|
752
819
|
const names = {}
|
|
@@ -805,7 +872,8 @@ function qoderMessages(msgs) {
|
|
|
805
872
|
const args = c.function?.arguments
|
|
806
873
|
return { id, type: "function", function: { name: c.function?.name, arguments: typeof args === "string" ? args : JSON.stringify(args ?? {}) } }
|
|
807
874
|
})
|
|
808
|
-
|
|
875
|
+
const think = reasoningOf(m)
|
|
876
|
+
out.push({ role: "assistant", content: textOf(m.content), ...(think ? { reasoning_content: think } : {}), tool_calls: calls })
|
|
809
877
|
const open = calls.map((c) => c.id)
|
|
810
878
|
const kept = new Set()
|
|
811
879
|
for (let j = i + 1; j < msgs.length && msgs[j].role === "tool"; j++) {
|
|
@@ -819,7 +887,8 @@ function qoderMessages(msgs) {
|
|
|
819
887
|
if (m.role === "assistant") blocks = blocks.filter((b) => b.type === "text")
|
|
820
888
|
if (!blocks.length) continue
|
|
821
889
|
if (m.role === "user" && seen.length) [blocks, seen] = [[...seen, ...blocks], []]
|
|
822
|
-
|
|
890
|
+
const think = m.role === "assistant" ? reasoningOf(m) : ""
|
|
891
|
+
out.push({ role: m.role, content: blocks, ...(think ? { reasoning_content: think } : {}) })
|
|
823
892
|
// a result after the assistant's own words answers no call before it
|
|
824
893
|
turn = null
|
|
825
894
|
}
|
|
@@ -2235,12 +2304,30 @@ const makePlugin = (site) => async ({ client }) => {
|
|
|
2235
2304
|
campaignMachines.set(uid, entry)
|
|
2236
2305
|
}
|
|
2237
2306
|
try {
|
|
2238
|
-
|
|
2307
|
+
const got = await entry.promise
|
|
2308
|
+
if (campaignMachines.get(uid) === entry) readIdentities.set(uid, { at: entry.at, headers: got })
|
|
2309
|
+
return got
|
|
2239
2310
|
} catch (e) {
|
|
2240
2311
|
if (campaignMachines.get(uid) === entry) campaignMachines.delete(uid)
|
|
2241
2312
|
throw e
|
|
2242
2313
|
}
|
|
2243
2314
|
}
|
|
2315
|
+
// chatIdentity: the device identity a chat carries, as Qoder's client puts
|
|
2316
|
+
// its native identity on when it has one. Only one this process has
|
|
2317
|
+
// already read (a check-in) is used, so a chat never waits on, or is the
|
|
2318
|
+
// first to run, Qoder's helper; one an hour old is read again behind the
|
|
2319
|
+
// chat, as the desktop client renews its own hourly. {}: none yet.
|
|
2320
|
+
const readIdentities = new Map()
|
|
2321
|
+
const chatIdentity = (uid) => {
|
|
2322
|
+
const known = uid ? readIdentities.get(uid) : undefined
|
|
2323
|
+
if (!known) return {}
|
|
2324
|
+
const now = Date.now()
|
|
2325
|
+
if (now - (known.tried ?? known.at) >= CAMPAIGN_MACHINE_TTL) {
|
|
2326
|
+
known.tried = now
|
|
2327
|
+
machineForCampaign(uid).catch(() => {})
|
|
2328
|
+
}
|
|
2329
|
+
return known.headers
|
|
2330
|
+
}
|
|
2244
2331
|
// campaignIdentity: the headers a campaigns request carries, and why
|
|
2245
2332
|
// there are none where the site goes on without them (Qoder CN).
|
|
2246
2333
|
const campaignIdentity = async (uid) => {
|
|
@@ -2581,13 +2668,7 @@ const makePlugin = (site) => async ({ client }) => {
|
|
|
2581
2668
|
ask.requestId = body.request_id
|
|
2582
2669
|
}
|
|
2583
2670
|
const wire = encodeBody(JSON.stringify(body))
|
|
2584
|
-
const headers =
|
|
2585
|
-
...cosyHeaders(CHAT_URL, cred, wire, siteOf(site, cred)),
|
|
2586
|
-
Accept: "text/event-stream",
|
|
2587
|
-
"Cache-Control": "no-cache",
|
|
2588
|
-
"X-Model-Key": m.key,
|
|
2589
|
-
"X-Model-Source": m.source,
|
|
2590
|
-
}
|
|
2671
|
+
const headers = chatHeaders(CHAT_URL, cred, wire, siteOf(site, cred), m, chatIdentity(cred.uid))
|
|
2591
2672
|
// a refused call's stream is closed: Qoder keeps a 10605 stream open
|
|
2592
2673
|
// after its one event, and a connection held open while the call is
|
|
2593
2674
|
// waited out and sent again is one more the account holds
|
|
@@ -2856,4 +2937,4 @@ export async function QwenWorkEnterpriseAuthPlugin({ client }) {
|
|
|
2856
2937
|
export const protocol = { makePlugin, deviceSignIn, openapi, siteOf }
|
|
2857
2938
|
|
|
2858
2939
|
// for tests
|
|
2859
|
-
export const _internal = { typedUnions, Splitter, campaignRuntime, RUNTIMES, campaignPage, campaignTarget, vpcInstance, vpcSite, parseUsage, gfmt, when, failure, modelInfo, qoderBody, qoderMessages, cacheMarked, sessionOf, namedSession, taskOf, TASKS, TASK_MAX, TASK_IDLE, SESSION, SITES, decodeBody, events, QUEUE, queueState }
|
|
2940
|
+
export const _internal = { chatHeaders, machineOS, machineHostname, cosyHeaders, typedUnions, Splitter, campaignRuntime, RUNTIMES, campaignPage, campaignTarget, vpcInstance, vpcSite, parseUsage, gfmt, when, failure, modelInfo, qoderBody, qoderMessages, cacheMarked, sessionOf, namedSession, taskOf, TASKS, TASK_MAX, TASK_IDLE, SESSION, SITES, decodeBody, events, QUEUE, queueState }
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@magpie-community/opencode-qoder-auth",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.42",
|
|
4
4
|
"description": "Qoder and QwenWork (千问办公) subscriptions in OpenCode and magpie: qoder.com, qoder.cn, gateway.qwenwork.cn and the enterprise flagship (VPC)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./index.mjs",
|