@magpie-community/opencode-qoder-auth 0.2.74 → 0.2.76
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/README.md +2 -1
- package/index.mjs +111 -14
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -162,7 +162,8 @@ itself.
|
|
|
162
162
|
- an MD5 signature;
|
|
163
163
|
- the machine id and the client's headers.
|
|
164
164
|
- `Cosy-Version`: the worker runtime version that Qoder's desktop app
|
|
165
|
-
ships (1.1.
|
|
165
|
+
ships (1.1.96-hotfix.8 in app 0.5.20, since 0.2.75, #1560; before
|
|
166
|
+
that 1.1.69 in app 0.4.8, from 0.2.23, #281), because
|
|
166
167
|
`Cosy-ClientType` says the desktop app.
|
|
167
168
|
|
|
168
169
|
**Reading the reply**
|
package/index.mjs
CHANGED
|
@@ -48,10 +48,14 @@ const MODELS_PATH = "/algo/api/v2/model/list?Encode=1"
|
|
|
48
48
|
// talks through — the worker runtime bundled in the app — because the
|
|
49
49
|
// envelope also says Cosy-ClientType: 10 (desktop) and carries that app's
|
|
50
50
|
// machine identity; the CLI's channel publishes a different, later number
|
|
51
|
-
// that no desktop build runs. Taken from app 0.
|
|
52
|
-
// 2026-
|
|
53
|
-
//
|
|
54
|
-
|
|
51
|
+
// that no desktop build runs. Taken from app 0.5.20 (both feeds, Qoder
|
|
52
|
+
// and Qoder CN, 2026-11-27), whose bundled worker runtime's
|
|
53
|
+
// runtime-info.json names 1.1.96-hotfix.8, the number its envelope and its
|
|
54
|
+
// chat's business.version say. 0.4.8 named 1.1.69: sending that, every
|
|
55
|
+
// request of one Qoder CN account was queued again after each wait, while
|
|
56
|
+
// the app answered the same account at once (plugins#1560). Raise it when
|
|
57
|
+
// the app moves, not when the CLI does.
|
|
58
|
+
const COSY_VERSION = "1.1.96-hotfix.8"
|
|
55
59
|
const SIGN_IN_TIMEOUT = 15 * 60 * 1000
|
|
56
60
|
const REFRESH_LEAD = 5 * 60 * 1000 // a chat token this close to its end is refreshed before a request
|
|
57
61
|
const RENEW_LEAD = 10 * 60 * 1000 // and this close, magpie renews it ahead of time (auth.refresh)
|
|
@@ -1543,6 +1547,13 @@ function modelConfig(m, thinking) {
|
|
|
1543
1547
|
// 0.4.3 and QwenWork 1.0.46 build their record without it, so a site that
|
|
1544
1548
|
// ships such a client says businessModelKey: false.
|
|
1545
1549
|
//
|
|
1550
|
+
// APP_CHAT is what Qoder's desktop app (0.5.20, its worker runtime's
|
|
1551
|
+
// remote chat body) says of every chat request it sends, whatever the
|
|
1552
|
+
// prompt: a streamed free-input turn, version "3". It goes with
|
|
1553
|
+
// COSY_VERSION, so only to the sites that speak as that app; QwenWork and
|
|
1554
|
+
// Lingma, with versions of their own, send their own clients' bodies.
|
|
1555
|
+
const APP_CHAT = { stream: true, chat_task: "FREE_INPUT", is_reply: true, is_retry: false, source: 1, version: "3", aliyun_user_type: "" }
|
|
1556
|
+
|
|
1546
1557
|
// qoderBody is the plaintext agent_chat_generation takes for a chat
|
|
1547
1558
|
// completion request, for model m, in the session the agent named (named,
|
|
1548
1559
|
// "" when none) and the task it is a call of (taskOf's, a new one when
|
|
@@ -1572,6 +1583,8 @@ function qoderBody(chat, m, named = "", task, site) {
|
|
|
1572
1583
|
request_set_id: task.id,
|
|
1573
1584
|
chat_record_id: request,
|
|
1574
1585
|
session_id: session,
|
|
1586
|
+
// what the app's request says of itself beside its version
|
|
1587
|
+
...(site?.cosyVersion ? {} : APP_CHAT),
|
|
1575
1588
|
parameters: params,
|
|
1576
1589
|
business: { product: site?.businessProduct ?? "app", version: site?.cosyVersion ?? COSY_VERSION, type: "agent", id: task.id, name: task.name ?? "", begin_at: task.begin, stage: task.answered ? "processing" : "start", ...(site?.businessModelKey === false ? {} : { model_key: m.key }) },
|
|
1577
1590
|
agent_id: "agent_common",
|
|
@@ -1800,6 +1813,9 @@ const QUEUE = {
|
|
|
1800
1813
|
// Qoder's client waits it (#807).
|
|
1801
1814
|
stuck: 5 * 60 * 1000,
|
|
1802
1815
|
now: () => Date.now(),
|
|
1816
|
+
// a line for magpie's log (the plugin host's stderr, app.log's
|
|
1817
|
+
// "plugin: " lines): each step of a queued request's wait
|
|
1818
|
+
log: (line) => console.warn(line),
|
|
1803
1819
|
// every calls fn each ms until the returned stop is called
|
|
1804
1820
|
every: (fn, ms) => {
|
|
1805
1821
|
const t = setInterval(fn, ms)
|
|
@@ -1830,6 +1846,23 @@ function queuePause(seconds) {
|
|
|
1830
1846
|
|
|
1831
1847
|
class QueueEnd extends Error {}
|
|
1832
1848
|
|
|
1849
|
+
// queueSays is a queue state as Qoder gave it, its fields by Qoder's names,
|
|
1850
|
+
// for magpie's log and the refusal a wait ends with: what the queue said
|
|
1851
|
+
// (plugins#1560: the reporter had no way to see it).
|
|
1852
|
+
const QUEUE_FIELDS = ["isQueued", "serviceAvailable", "queueType", "queueCount", "retryAfterSeconds", "waitTime", "subCode", "modelKey"]
|
|
1853
|
+
function queueSays(st) {
|
|
1854
|
+
if (!st || typeof st !== "object") return "nothing of the queue"
|
|
1855
|
+
const said = QUEUE_FIELDS.filter((k) => st[k] !== undefined && st[k] !== null && st[k] !== "").map((k) => `${k} ${st[k]}`)
|
|
1856
|
+
return said.length ? said.join(", ") : "nothing of the queue"
|
|
1857
|
+
}
|
|
1858
|
+
|
|
1859
|
+
// queueLog logs a step of the wait of ask, the request of site
|
|
1860
|
+
function queueLog(site, ask, text) {
|
|
1861
|
+
try {
|
|
1862
|
+
QUEUE.log(`${site?.id ?? "qoder"}: ${ask.modelKey} in request set ${String(ask.requestSetId).slice(0, 8)}: ${text}`)
|
|
1863
|
+
} catch {}
|
|
1864
|
+
}
|
|
1865
|
+
|
|
1833
1866
|
// queueState is the queue's state in a status reply, found as Qoder's
|
|
1834
1867
|
// client finds it: the object, or one under data/result/message/body, as
|
|
1835
1868
|
// an object or a JSON string, that says isQueued or serviceAvailable.
|
|
@@ -1852,7 +1885,7 @@ function queueState(text) {
|
|
|
1852
1885
|
if (isQueued !== undefined || serviceAvailable !== undefined) {
|
|
1853
1886
|
const num = (v) => (v == null || v === "" || !Number.isFinite(Number(v)) ? undefined : Number(v))
|
|
1854
1887
|
const str = (v) => (typeof v === "string" && v ? v : undefined)
|
|
1855
|
-
return { isQueued, serviceAvailable, modelKey: str(o.modelKey), queueType: str(o.queueType), retryAfterSeconds: num(o.retryAfterSeconds), queueCount: num(o.queueCount), subCode: str(o.subCode) }
|
|
1888
|
+
return { isQueued, serviceAvailable, modelKey: str(o.modelKey), queueType: str(o.queueType), retryAfterSeconds: num(o.retryAfterSeconds), queueCount: num(o.queueCount), waitTime: num(o.waitTime), subCode: str(o.subCode) }
|
|
1856
1889
|
}
|
|
1857
1890
|
for (const k of ["data", "result", "message", "body"]) {
|
|
1858
1891
|
const v = o[k]
|
|
@@ -1941,6 +1974,7 @@ function stuckKey(st) {
|
|
|
1941
1974
|
// (false). A wait ended by QUEUE.stuck sets ask.stuck. A cancelled request
|
|
1942
1975
|
// throws.
|
|
1943
1976
|
async function queueWait(api, cred, ask, first, signal, deadline, site) {
|
|
1977
|
+
const began = QUEUE.now()
|
|
1944
1978
|
let q = first ?? {}
|
|
1945
1979
|
let asked = false
|
|
1946
1980
|
let failed = 0
|
|
@@ -1948,21 +1982,31 @@ async function queueWait(api, cred, ask, first, signal, deadline, site) {
|
|
|
1948
1982
|
let since = QUEUE.now()
|
|
1949
1983
|
for (;;) {
|
|
1950
1984
|
if (signal?.aborted) throw signal.reason ?? new DOMException("aborted", "AbortError")
|
|
1951
|
-
|
|
1985
|
+
const ranOut = () => (queueLog(site, ask, `the wait ran out after ${Math.round((QUEUE.now() - began) / 1000)}s`), false)
|
|
1986
|
+
if (QUEUE.now() >= deadline) return ranOut()
|
|
1952
1987
|
if (asked) await QUEUE.pause(Math.min(queuePause(q.retryAfterSeconds), Math.max(0, deadline - QUEUE.now())), signal)
|
|
1953
1988
|
asked = true
|
|
1954
|
-
if (QUEUE.now() >= deadline) return
|
|
1989
|
+
if (QUEUE.now() >= deadline) return ranOut()
|
|
1955
1990
|
let st
|
|
1956
1991
|
try {
|
|
1957
1992
|
st = await queueStatus(api, cred, { ...ask, modelKey: q.modelKey ?? ask.modelKey, queueType: q.queueType ?? ask.queueType }, signal, deadline, site)
|
|
1958
1993
|
} catch (e) {
|
|
1959
1994
|
if (signal?.aborted) throw signal.reason ?? e
|
|
1960
|
-
if (e instanceof QueueEnd)
|
|
1961
|
-
|
|
1995
|
+
if (e instanceof QueueEnd) {
|
|
1996
|
+
queueLog(site, ask, `the wait ends: ${e.message}`)
|
|
1997
|
+
return false
|
|
1998
|
+
}
|
|
1999
|
+
queueLog(site, ask, `queue status ask failed: ${e?.message ?? e}`)
|
|
2000
|
+
if (++failed >= QUEUE.failures) {
|
|
2001
|
+
queueLog(site, ask, `the wait ends: ${failed} status asks failed in a row`)
|
|
2002
|
+
return false
|
|
2003
|
+
}
|
|
1962
2004
|
q = { ...q, retryAfterSeconds: undefined }
|
|
1963
2005
|
continue
|
|
1964
2006
|
}
|
|
1965
2007
|
failed = 0
|
|
2008
|
+
ask.last = st
|
|
2009
|
+
queueLog(site, ask, `queue status: ${queueSays(st)}${queueReady(st) ? "; through, sending it again" : ""}`)
|
|
1966
2010
|
q = { ...st, modelKey: st.modelKey ?? q.modelKey, queueType: st.queueType ?? q.queueType }
|
|
1967
2011
|
const key = stuckKey(q)
|
|
1968
2012
|
if (!key || key !== same) {
|
|
@@ -1970,6 +2014,7 @@ async function queueWait(api, cred, ask, first, signal, deadline, site) {
|
|
|
1970
2014
|
since = QUEUE.now()
|
|
1971
2015
|
} else if (QUEUE.now() - since >= QUEUE.stuck && !queueReady(st)) {
|
|
1972
2016
|
ask.stuck = true
|
|
2017
|
+
queueLog(site, ask, `the wait ends: the queue hasn't moved in ${Math.round(QUEUE.stuck / 60_000)} minutes with nothing ahead`)
|
|
1973
2018
|
return false
|
|
1974
2019
|
}
|
|
1975
2020
|
if (queueReady(st)) {
|
|
@@ -1977,6 +2022,7 @@ async function queueWait(api, cred, ask, first, signal, deadline, site) {
|
|
|
1977
2022
|
return QUEUE.now() < deadline
|
|
1978
2023
|
}
|
|
1979
2024
|
if (st.serviceAvailable === false || st.isQueued === true) continue
|
|
2025
|
+
queueLog(site, ask, "the wait ends: the status is neither queued nor through")
|
|
1980
2026
|
return false
|
|
1981
2027
|
}
|
|
1982
2028
|
}
|
|
@@ -2626,9 +2672,25 @@ const NAMELESS = "the model's native tool call has no name; it is not sent on as
|
|
|
2626
2672
|
// opened is a chat reply's events and the first of them, read before
|
|
2627
2673
|
// anything is answered: a refusal in the stream (Qoder's queue among them)
|
|
2628
2674
|
// comes first.
|
|
2629
|
-
async function opened(res, tools) {
|
|
2630
|
-
const
|
|
2631
|
-
|
|
2675
|
+
async function opened(res, tools, onReader) {
|
|
2676
|
+
const reader = res.body.getReader()
|
|
2677
|
+
onReader?.(reader)
|
|
2678
|
+
const body = async function* () {
|
|
2679
|
+
while (true) {
|
|
2680
|
+
const { done, value } = await reader.read()
|
|
2681
|
+
if (done) {
|
|
2682
|
+
return
|
|
2683
|
+
}
|
|
2684
|
+
yield value
|
|
2685
|
+
}
|
|
2686
|
+
}()
|
|
2687
|
+
const it = events(body, tools)
|
|
2688
|
+
try {
|
|
2689
|
+
const first = await it.next()
|
|
2690
|
+
return { it, first }
|
|
2691
|
+
} finally {
|
|
2692
|
+
onReader?.(undefined)
|
|
2693
|
+
}
|
|
2632
2694
|
}
|
|
2633
2695
|
|
|
2634
2696
|
async function answer({ it, first }, chat, servedName = chat.model) {
|
|
@@ -3926,13 +3988,28 @@ const makePlugin = (site) => async ({ client }) => {
|
|
|
3926
3988
|
const callSignal = signal ? AbortSignal.any([signal, call.signal]) : call.signal
|
|
3927
3989
|
let res, open
|
|
3928
3990
|
let silent = false
|
|
3991
|
+
let pendingRead
|
|
3929
3992
|
const hush = setTimeout(() => {
|
|
3930
3993
|
silent = true
|
|
3931
3994
|
call.abort()
|
|
3995
|
+
pendingRead?.cancel().catch(() => {})
|
|
3932
3996
|
}, QUEUE.silent)
|
|
3933
3997
|
try {
|
|
3934
3998
|
res = await fetch(CHAT_URL, { method: "POST", headers, body: wire, signal: callSignal })
|
|
3935
|
-
if (
|
|
3999
|
+
if (silent)
|
|
4000
|
+
return errorResponse({
|
|
4001
|
+
status: 504,
|
|
4002
|
+
message: `${siteOf(site, cred).name ?? "Qoder"} sent nothing for ${Math.round(QUEUE.silent / 1000)}s after the request (no first event); the call was dropped`,
|
|
4003
|
+
signIn: "",
|
|
4004
|
+
})
|
|
4005
|
+
if (res.ok)
|
|
4006
|
+
open = await opened(
|
|
4007
|
+
res,
|
|
4008
|
+
chat.tool_choice !== "none" ? (chat.tools ?? []).filter((t) => t?.type === "function") : [],
|
|
4009
|
+
(reader) => {
|
|
4010
|
+
pendingRead = reader
|
|
4011
|
+
},
|
|
4012
|
+
)
|
|
3936
4013
|
} catch (e) {
|
|
3937
4014
|
if (silent) {
|
|
3938
4015
|
const who = siteOf(site, cred).name ?? "Qoder"
|
|
@@ -3941,7 +4018,14 @@ const makePlugin = (site) => async ({ client }) => {
|
|
|
3941
4018
|
return errorResponse({ status: 502, message: e.message })
|
|
3942
4019
|
} finally {
|
|
3943
4020
|
clearTimeout(hush)
|
|
4021
|
+
pendingRead = undefined
|
|
3944
4022
|
}
|
|
4023
|
+
if (silent)
|
|
4024
|
+
return errorResponse({
|
|
4025
|
+
status: 504,
|
|
4026
|
+
message: `${siteOf(site, cred).name ?? "Qoder"} sent nothing for ${Math.round(QUEUE.silent / 1000)}s after the request (no first event); the call was dropped`,
|
|
4027
|
+
signIn: "",
|
|
4028
|
+
})
|
|
3945
4029
|
const f = !res.ok ? failure(res.status, (await res.text()).slice(0, 1 << 20)) : open.first.value?.error
|
|
3946
4030
|
if (f) call.abort()
|
|
3947
4031
|
// a refused job token: renewed once (or the one renewed meanwhile)
|
|
@@ -3985,7 +4069,20 @@ const makePlugin = (site) => async ({ client }) => {
|
|
|
3985
4069
|
void release(task, lease)
|
|
3986
4070
|
return r
|
|
3987
4071
|
}
|
|
3988
|
-
|
|
4072
|
+
const said = siteOf(site, cred)
|
|
4073
|
+
queueLog(said, ask, `queued (${QUEUED}) on send ${sent + 1}: ${queueSays(f.queue)}`)
|
|
4074
|
+
if (fallback) {
|
|
4075
|
+
queueLog(said, ask, "not waited: another candidate follows")
|
|
4076
|
+
return giveUp(errorResponse(f))
|
|
4077
|
+
}
|
|
4078
|
+
// the queue said it was through each time, and each resend was
|
|
4079
|
+
// queued again: said in the refusal, with the queue's last status,
|
|
4080
|
+
// so the reason is readable where the error is (plugins#1560)
|
|
4081
|
+
if (waited >= QUEUE.recoveries) {
|
|
4082
|
+
const again = `Qoder's queue let it through ${waited} times and queued each resend again; its last status: ${queueSays(ask.last)}`
|
|
4083
|
+
queueLog(said, ask, `the wait ends: ${again}`)
|
|
4084
|
+
return giveUp(errorResponse({ ...f, message: `${f.message} (${again})` }))
|
|
4085
|
+
}
|
|
3989
4086
|
if (chat.stream && !held) {
|
|
3990
4087
|
held = holdQueued(f, init.signal)
|
|
3991
4088
|
signal = held.signal
|
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.76",
|
|
4
4
|
"description": "Qoder and QwenWork (千问办公) subscriptions in OpenCode and magpie: qoder.com, qoder.cn, gateway.qwenwork.cn and the enterprise flagship, VPC and dedicated domains",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./opencode.mjs",
|