@magpie-community/opencode-qoder-auth 0.2.47 → 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 +8 -0
- package/index.mjs +84 -21
- 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.
|
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 = ""
|
|
@@ -1185,9 +1198,11 @@ function failure(status, text) {
|
|
|
1185
1198
|
// lapsed: only a refused refresh did (errorResponse says kept). Qoder's
|
|
1186
1199
|
// own words go with it: a 401 right after signing in isn't a lapsed
|
|
1187
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.
|
|
1188
1203
|
if (status === 401 || status === 403) {
|
|
1189
1204
|
const said = msg && msg !== STATUS_CODES[status] ? ` (Qoder said ${status}: ${msg.slice(0, 300)})` : ""
|
|
1190
|
-
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 }
|
|
1191
1206
|
}
|
|
1192
1207
|
if (status === 429 || msg.toLowerCase().includes("quota")) return { status: 429, message: "usage limit reached: " + msg }
|
|
1193
1208
|
return { status, message: msg }
|
|
@@ -2501,6 +2516,32 @@ const makePlugin = (site) => async ({ client }) => {
|
|
|
2501
2516
|
return next
|
|
2502
2517
|
})
|
|
2503
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
|
+
|
|
2504
2545
|
const models = async (cred, again = false) => {
|
|
2505
2546
|
const key = listingKey(cred)
|
|
2506
2547
|
let l = listings.get(key)
|
|
@@ -2737,7 +2778,10 @@ const makePlugin = (site) => async ({ client }) => {
|
|
|
2737
2778
|
})
|
|
2738
2779
|
|
|
2739
2780
|
// ask is the answer to a chat completion on the live account cred.
|
|
2740
|
-
|
|
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
|
|
2741
2785
|
let m
|
|
2742
2786
|
try {
|
|
2743
2787
|
m = (await models(cred)).find((x) => x.key === chat.model) ?? (await models(cred, true)).find((x) => x.key === chat.model)
|
|
@@ -2760,8 +2804,9 @@ const makePlugin = (site) => async ({ client }) => {
|
|
|
2760
2804
|
// resend can be queued again before it is answered.
|
|
2761
2805
|
const deadline = QUEUE.now() + QUEUE.budget
|
|
2762
2806
|
const ask = { requestSetId: body.request_set_id, requestId: body.request_id, sessionId: body.session_id, modelKey: m.key }
|
|
2763
|
-
|
|
2764
|
-
|
|
2807
|
+
let relogged = false
|
|
2808
|
+
for (let sent = 0, waited = 0; ; sent++) {
|
|
2809
|
+
if (sent) {
|
|
2765
2810
|
body.request_id = body.chat_record_id = randomUUID()
|
|
2766
2811
|
ask.requestId = body.request_id
|
|
2767
2812
|
}
|
|
@@ -2781,6 +2826,21 @@ const makePlugin = (site) => async ({ client }) => {
|
|
|
2781
2826
|
}
|
|
2782
2827
|
const f = !res.ok ? failure(res.status, (await res.text()).slice(0, 1 << 20)) : open.first.value?.error
|
|
2783
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
|
+
}
|
|
2784
2844
|
// an answered call moves its task on, as Qoder's client moves it
|
|
2785
2845
|
if (!f) task.answered = true
|
|
2786
2846
|
// with another account, provider or group member after this one
|
|
@@ -2792,6 +2852,7 @@ const makePlugin = (site) => async ({ client }) => {
|
|
|
2792
2852
|
} catch (e) {
|
|
2793
2853
|
return errorResponse({ status: 502, message: e?.message ?? String(e) })
|
|
2794
2854
|
}
|
|
2855
|
+
waited++
|
|
2795
2856
|
}
|
|
2796
2857
|
}
|
|
2797
2858
|
|
|
@@ -2860,7 +2921,9 @@ const makePlugin = (site) => async ({ client }) => {
|
|
|
2860
2921
|
}
|
|
2861
2922
|
const h = new Headers(input instanceof Request ? input.headers : undefined)
|
|
2862
2923
|
new Headers(init.headers ?? {}).forEach((v, k) => h.set(k, v))
|
|
2863
|
-
|
|
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)))
|
|
2864
2927
|
},
|
|
2865
2928
|
}
|
|
2866
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",
|