@magpie-community/opencode-qoder-auth 0.2.46 → 0.2.48
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 +16 -0
- package/index.mjs +95 -22
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -68,6 +68,14 @@ Refreshing:
|
|
|
68
68
|
- Only one refresh runs at a time. A request that reads the sign-in before
|
|
69
69
|
magpie saved a renewal uses the renewed pair, and a renewal handed a pair
|
|
70
70
|
a request already spent gives what that request got.
|
|
71
|
+
- A job token's end is what Qoder says: `expire_time` or `expires_at` (a
|
|
72
|
+
date, or an epoch in seconds or milliseconds), then `expires_in`
|
|
73
|
+
(milliseconds), then a day.
|
|
74
|
+
- A chat Qoder answers 401 or 403 with no other code ("Login expired") is
|
|
75
|
+
sent again once: with the pair saved since, when another request, magpie
|
|
76
|
+
or another process renewed it meanwhile, else after one refresh, under the
|
|
77
|
+
same lock. A token renewed less than 5 minutes ago and refused again isn't
|
|
78
|
+
renewed again; the refusal is the answer.
|
|
71
79
|
- The device token, which reads usage, has no end on record: it is rotated
|
|
72
80
|
when Qoder refuses it. On a [Qoder CN](#qoder-cn) device-token account it
|
|
73
81
|
is the chat token too, and is renewed as above.
|
|
@@ -208,6 +216,14 @@ app's Fast from 2026-10-12:
|
|
|
208
216
|
|
|
209
217
|
Every model speaks chat completions (`@ai-sdk/openai-compatible`).
|
|
210
218
|
|
|
219
|
+
Every model a site ships also says, either way, whether it takes images:
|
|
220
|
+
`attachment` and `modalities.input` are set on every `config` entry, image
|
|
221
|
+
or no image. A kind left out is not "text only" to magpie but "the vendor
|
|
222
|
+
didn't say", and magpie then falls back to models.dev's vote by bare model
|
|
223
|
+
ID — where a same-named model of another vendor may take images, so this
|
|
224
|
+
one gets sent them. QwenWork's three models and Qoder CN's Lite shipped no
|
|
225
|
+
image key; since 0.2.47 they say text alone.
|
|
226
|
+
|
|
211
227
|
## Qoder CN
|
|
212
228
|
|
|
213
229
|
`qoder-cn` is for accounts on [qoder.cn](https://qoder.cn), the China site.
|
package/index.mjs
CHANGED
|
@@ -56,6 +56,7 @@ const SIGN_IN_TIMEOUT = 15 * 60 * 1000
|
|
|
56
56
|
const REFRESH_LEAD = 5 * 60 * 1000 // a chat token this close to its end is refreshed before a request
|
|
57
57
|
const RENEW_LEAD = 10 * 60 * 1000 // and this close, magpie renews it ahead of time (auth.refresh)
|
|
58
58
|
const KEEP_SPENT_MS = 60 * 60 * 1000 // how long what a spent refresh token gave is remembered
|
|
59
|
+
const RELOGIN_GAP = 5 * 60 * 1000 // a token renewed this recently that Qoder refuses isn't renewed again
|
|
59
60
|
const DAY = 24 * 60 * 60 * 1000
|
|
60
61
|
const CHAT = "@ai-sdk/openai-compatible"
|
|
61
62
|
|
|
@@ -234,9 +235,33 @@ async function openapi(site, path, { method = "GET", token, body, query } = {})
|
|
|
234
235
|
return { status: res.status, json, text }
|
|
235
236
|
}
|
|
236
237
|
|
|
237
|
-
// expiresAt is when a job token Qoder gave
|
|
238
|
-
//
|
|
239
|
-
|
|
238
|
+
// expiresAt is when a job token Qoder gave runs out, read as Qoder's own
|
|
239
|
+
// client reads it, for a refreshed token as for an exchanged PAT's: its
|
|
240
|
+
// expire_time or expires_at first (a date, or an epoch in seconds or
|
|
241
|
+
// milliseconds), then expires_in (milliseconds), then a day. A refreshed
|
|
242
|
+
// token was read for expires_in alone, so an end Qoder said as a date was
|
|
243
|
+
// put down as a day away, and renewed after it had run out (plugins#474).
|
|
244
|
+
// An end already past is no end said.
|
|
245
|
+
function expiresAt(jt) {
|
|
246
|
+
for (const k of ["expire_time", "expires_at"]) {
|
|
247
|
+
const at = whenSaid(jt?.[k])
|
|
248
|
+
if (at > Date.now()) return at
|
|
249
|
+
}
|
|
250
|
+
if (jt?.expires_in > 0) return Date.now() + jt.expires_in
|
|
251
|
+
return Date.now() + DAY
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// whenSaid is the time a token's end field names, in milliseconds: a date,
|
|
255
|
+
// or an epoch in seconds or milliseconds; 0 for none (absent, 0, unreadable).
|
|
256
|
+
function whenSaid(v) {
|
|
257
|
+
if (typeof v === "number" || /^\d+$/.test(String(v ?? "").trim())) {
|
|
258
|
+
const n = Number(v)
|
|
259
|
+
if (!(n > 0)) return 0
|
|
260
|
+
return n < 1e12 ? n * 1000 : n
|
|
261
|
+
}
|
|
262
|
+
const at = Date.parse(String(v ?? "").trim())
|
|
263
|
+
return isNaN(at) ? 0 : at
|
|
264
|
+
}
|
|
240
265
|
|
|
241
266
|
// deviceExpiry is when a device token used for chat runs out: its
|
|
242
267
|
// expires_at (a date) or expires_in (seconds), as Qoder's CLI reads it; a
|
|
@@ -382,18 +407,6 @@ async function deviceSignIn(site) {
|
|
|
382
407
|
}
|
|
383
408
|
}
|
|
384
409
|
|
|
385
|
-
// exchangeExpiry is when a job token from /api/v1/jobToken/exchange runs
|
|
386
|
-
// out, as Qoder's own client reads it: expire_time or expires_at (a date)
|
|
387
|
-
// first, then expires_in (milliseconds), then a day.
|
|
388
|
-
function exchangeExpiry(jt) {
|
|
389
|
-
for (const k of ["expire_time", "expires_at"]) {
|
|
390
|
-
const at = Date.parse(String(jt?.[k] ?? "").trim())
|
|
391
|
-
if (!isNaN(at)) return at
|
|
392
|
-
}
|
|
393
|
-
if (jt?.expires_in > 0) return Date.now() + jt.expires_in
|
|
394
|
-
return Date.now() + DAY
|
|
395
|
-
}
|
|
396
|
-
|
|
397
410
|
// exchangeToken is the job token from an exchange response, as Qoder's
|
|
398
411
|
// client reads it: token, device_token or access_token.
|
|
399
412
|
function exchangeToken(jt) {
|
|
@@ -413,7 +426,7 @@ async function patSignIn(site, pat) {
|
|
|
413
426
|
const token = exchangeToken(r.json)
|
|
414
427
|
if (!token) throw new Error(`${site.name} PAT exchange: empty token in response`)
|
|
415
428
|
const refresh = String(r.json?.refresh_token ?? r.json?.refreshToken ?? "").trim()
|
|
416
|
-
const chat = { access: token, refresh, expires:
|
|
429
|
+
const chat = { access: token, refresh, expires: expiresAt(r.json) }
|
|
417
430
|
let email = ""
|
|
418
431
|
let name = ""
|
|
419
432
|
let uid = ""
|
|
@@ -674,10 +687,20 @@ async function fetchListing(site, cred) {
|
|
|
674
687
|
}
|
|
675
688
|
|
|
676
689
|
function configModel(m) {
|
|
690
|
+
const images = !!m.images
|
|
677
691
|
return {
|
|
678
692
|
name: m.name,
|
|
679
693
|
limit: { context: m.context ?? 0, output: 0 },
|
|
680
|
-
|
|
694
|
+
// Said either way, image or no image. A kind left out is not "text only"
|
|
695
|
+
// to magpie: it is "the vendor didn't say", and magpie then falls back to
|
|
696
|
+
// models.dev's vote by bare model id — where a same-named model of
|
|
697
|
+
// another vendor may take images, so a text-only one here is sent them
|
|
698
|
+
// (the same mistake codearts 0.2.3 made, fixed there in 0.2.4). The list
|
|
699
|
+
// a site ships is what magpie has before anyone signs in, and what it
|
|
700
|
+
// keeps when a listing can't be fetched (provider.models falls back to
|
|
701
|
+
// it), so those models are the ones most likely to be asked for an image.
|
|
702
|
+
attachment: images,
|
|
703
|
+
modalities: { input: images ? ["text", "image"] : ["text"], output: ["text"] },
|
|
681
704
|
...(m.efforts?.length ? { reasoning: true, variants: Object.fromEntries(m.efforts.map((e) => [e, { reasoningEffort: e }])) } : {}),
|
|
682
705
|
tool_call: true,
|
|
683
706
|
}
|
|
@@ -1175,9 +1198,11 @@ function failure(status, text) {
|
|
|
1175
1198
|
// lapsed: only a refused refresh did (errorResponse says kept). Qoder's
|
|
1176
1199
|
// own words go with it: a 401 right after signing in isn't a lapsed
|
|
1177
1200
|
// sign-in, and its reason is what says what is (Yayoi_no_yume on X).
|
|
1201
|
+
// lapsed: the job token the call carried was refused, which the loader
|
|
1202
|
+
// renews once and asks again with (plugins#474) before it says so.
|
|
1178
1203
|
if (status === 401 || status === 403) {
|
|
1179
1204
|
const said = msg && msg !== STATUS_CODES[status] ? ` (Qoder said ${status}: ${msg.slice(0, 300)})` : ""
|
|
1180
|
-
return { status: 401, message: "the sign-in lapsed — sign in again" + said }
|
|
1205
|
+
return { status: 401, message: "the sign-in lapsed — sign in again" + said, lapsed: true }
|
|
1181
1206
|
}
|
|
1182
1207
|
if (status === 429 || msg.toLowerCase().includes("quota")) return { status: 429, message: "usage limit reached: " + msg }
|
|
1183
1208
|
return { status, message: msg }
|
|
@@ -2491,6 +2516,32 @@ const makePlugin = (site) => async ({ client }) => {
|
|
|
2491
2516
|
return next
|
|
2492
2517
|
})
|
|
2493
2518
|
|
|
2519
|
+
// relogin is the account to ask again with once Qoder refused the job
|
|
2520
|
+
// token refused (a chat's 401/403 "Login expired"), or undefined to say
|
|
2521
|
+
// the refusal as it is. Under the lock, as fresh: the sign-in as saved
|
|
2522
|
+
// now (another request, magpie's auth.refresh or another process may
|
|
2523
|
+
// have renewed it meanwhile, and then that is what is used, with no
|
|
2524
|
+
// refresh), else the job token renewed once and saved. Qoder has been
|
|
2525
|
+
// seen to refuse a job token it had put down as a day away for minutes
|
|
2526
|
+
// before magpie renewed it (plugins#474), and a request then failed
|
|
2527
|
+
// until the renewal came. A token this process renewed only minutes ago
|
|
2528
|
+
// that is refused too isn't renewed again: a refusal that isn't about the
|
|
2529
|
+
// token's end would otherwise spend a refresh token on every request.
|
|
2530
|
+
const relogin = (getAuth, refused) =>
|
|
2531
|
+
locked(async () => {
|
|
2532
|
+
const stored = await getAuth()
|
|
2533
|
+
if (stored?.type !== "oauth" || !stored.access || !stored.uid) throw new SignInGone(`${site.name}: not signed in`)
|
|
2534
|
+
siteOf(site, stored)
|
|
2535
|
+
const a = latest(stored)
|
|
2536
|
+
if (a.access !== refused) return a
|
|
2537
|
+
const now = Date.now()
|
|
2538
|
+
for (const v of spent.values()) if (v.got.access === refused && now - v.at < RELOGIN_GAP) return undefined
|
|
2539
|
+
const next = { ...a, ...(await renew(a)) }
|
|
2540
|
+
await client.auth.set({ path: { id: ID }, body: next })
|
|
2541
|
+
renewals.add(next)
|
|
2542
|
+
return next
|
|
2543
|
+
})
|
|
2544
|
+
|
|
2494
2545
|
const models = async (cred, again = false) => {
|
|
2495
2546
|
const key = listingKey(cred)
|
|
2496
2547
|
let l = listings.get(key)
|
|
@@ -2727,7 +2778,10 @@ const makePlugin = (site) => async ({ client }) => {
|
|
|
2727
2778
|
})
|
|
2728
2779
|
|
|
2729
2780
|
// ask is the answer to a chat completion on the live account cred.
|
|
2730
|
-
|
|
2781
|
+
// asked is what ask asked with: the account it ended on, which a refused
|
|
2782
|
+
// job token's renewal (relogin, given getAuth) changes.
|
|
2783
|
+
const ask = async (chat, cred, init, named = "", fallback = false, getAuth, asked = {}) => {
|
|
2784
|
+
asked.cred = cred
|
|
2731
2785
|
let m
|
|
2732
2786
|
try {
|
|
2733
2787
|
m = (await models(cred)).find((x) => x.key === chat.model) ?? (await models(cred, true)).find((x) => x.key === chat.model)
|
|
@@ -2750,8 +2804,9 @@ const makePlugin = (site) => async ({ client }) => {
|
|
|
2750
2804
|
// resend can be queued again before it is answered.
|
|
2751
2805
|
const deadline = QUEUE.now() + QUEUE.budget
|
|
2752
2806
|
const ask = { requestSetId: body.request_set_id, requestId: body.request_id, sessionId: body.session_id, modelKey: m.key }
|
|
2753
|
-
|
|
2754
|
-
|
|
2807
|
+
let relogged = false
|
|
2808
|
+
for (let sent = 0, waited = 0; ; sent++) {
|
|
2809
|
+
if (sent) {
|
|
2755
2810
|
body.request_id = body.chat_record_id = randomUUID()
|
|
2756
2811
|
ask.requestId = body.request_id
|
|
2757
2812
|
}
|
|
@@ -2771,6 +2826,21 @@ const makePlugin = (site) => async ({ client }) => {
|
|
|
2771
2826
|
}
|
|
2772
2827
|
const f = !res.ok ? failure(res.status, (await res.text()).slice(0, 1 << 20)) : open.first.value?.error
|
|
2773
2828
|
if (f) call.abort()
|
|
2829
|
+
// a refused job token: renewed once (or the one renewed meanwhile)
|
|
2830
|
+
// and the request sent again, before the refusal is said
|
|
2831
|
+
if (f?.lapsed && getAuth && !relogged) {
|
|
2832
|
+
relogged = true
|
|
2833
|
+
let next
|
|
2834
|
+
try {
|
|
2835
|
+
next = await relogin(getAuth, cred.access)
|
|
2836
|
+
} catch (e) {
|
|
2837
|
+
return signedInError(e)
|
|
2838
|
+
}
|
|
2839
|
+
if (next) {
|
|
2840
|
+
cred = asked.cred = next
|
|
2841
|
+
continue
|
|
2842
|
+
}
|
|
2843
|
+
}
|
|
2774
2844
|
// an answered call moves its task on, as Qoder's client moves it
|
|
2775
2845
|
if (!f) task.answered = true
|
|
2776
2846
|
// with another account, provider or group member after this one
|
|
@@ -2782,6 +2852,7 @@ const makePlugin = (site) => async ({ client }) => {
|
|
|
2782
2852
|
} catch (e) {
|
|
2783
2853
|
return errorResponse({ status: 502, message: e?.message ?? String(e) })
|
|
2784
2854
|
}
|
|
2855
|
+
waited++
|
|
2785
2856
|
}
|
|
2786
2857
|
}
|
|
2787
2858
|
|
|
@@ -2850,7 +2921,9 @@ const makePlugin = (site) => async ({ client }) => {
|
|
|
2850
2921
|
}
|
|
2851
2922
|
const h = new Headers(input instanceof Request ? input.headers : undefined)
|
|
2852
2923
|
new Headers(init.headers ?? {}).forEach((v, k) => h.set(k, v))
|
|
2853
|
-
|
|
2924
|
+
const asked = {}
|
|
2925
|
+
const res = await ask(chat, cred, init, h.get(SESSION)?.trim().slice(0, 128) ?? "", h.get(FALLBACK) === "1", getAuth, asked)
|
|
2926
|
+
return signed(res, renewed(cred) || (asked.cred !== cred && renewed(asked.cred)))
|
|
2854
2927
|
},
|
|
2855
2928
|
}
|
|
2856
2929
|
},
|
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.48",
|
|
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",
|