@magpie-community/opencode-zcode-auth 0.1.51 → 0.1.53
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 +11 -0
- package/index.mjs +121 -13
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -323,6 +323,17 @@ the body's code, not the HTTP status:
|
|
|
323
323
|
first: six accounts all answered 1004 under 3.16.3's header, and four
|
|
324
324
|
of them claimed with code 0 under 3.17.2's, one night, with nothing
|
|
325
325
|
else changed;
|
|
326
|
+
- HTTP 429 "Too Many Requests", or a body code 3002 "请求过于频繁": *failed*,
|
|
327
|
+
and the account is then **held in the plugin**: until the wait is over, a
|
|
328
|
+
check-in answers that same failed (`until` and all) and asks ZCode nothing.
|
|
329
|
+
magpie asks a failed again after 30 minutes of its own whatever `until`
|
|
330
|
+
says, so the hold has to live here (magpie from v0.1.3165 honors a failed's
|
|
331
|
+
`until` itself; older versions lean on this hold). A `Retry-After` ZCode
|
|
332
|
+
sent is honored up to six hours; without one the account's own wait doubles
|
|
333
|
+
from half an hour, and a read that comes through forgets it. The hold
|
|
334
|
+
belongs to one account and lives in memory — a magpie restart forgets it.
|
|
335
|
+
(11-04, #503: one owner read 180 of them over four days at magpie's
|
|
336
|
+
30-minute retry, across several accounts.);
|
|
326
337
|
- anything else, an HTTP error without a code, or a reply that isn't
|
|
327
338
|
JSON: *failed*, which magpie asks again later that day. It is never
|
|
328
339
|
taken as claimed, and a failed claim leaves the account's sign-in
|
package/index.mjs
CHANGED
|
@@ -96,8 +96,11 @@ async function call(method, url, { auth, body, headers, device, signal } = {}) {
|
|
|
96
96
|
} catch {}
|
|
97
97
|
const code = env?.code == null ? "" : String(env.code)
|
|
98
98
|
if (!res.ok) {
|
|
99
|
-
|
|
100
|
-
|
|
99
|
+
const said = env?.msg ? (code && code !== "0" ? `${env.msg} (${res.status}, code ${code})` : `${env.msg} (${res.status})`) : (STATUS_TEXT[res.status] ?? `${res.status} ${res.statusText}`.trim())
|
|
100
|
+
const err = new ZError(said, res.status)
|
|
101
|
+
err.code = Number(code) || 0 // 3002 and the like: so a caller can tell a rate limit from a failure
|
|
102
|
+
err.retryAfterMs = retryAfterMs(res) // 0 unless the reply carried a Retry-After
|
|
103
|
+
throw err
|
|
101
104
|
}
|
|
102
105
|
if (!env || typeof env !== "object") throw new ZError("not JSON", res.status)
|
|
103
106
|
if (code && code !== "0" && code !== "200") throw new ZError(env.msg || `error ${code}`, res.status)
|
|
@@ -312,6 +315,19 @@ function spentUp(text, status) {
|
|
|
312
315
|
return c1 === "1113" || c1 === "1005" || c2 === "1113" || c2 === "1005"
|
|
313
316
|
}
|
|
314
317
|
|
|
318
|
+
// spentSaid is an answer that carries spentUp's code inside a 200, as a
|
|
319
|
+
// spent bucket's can, said as the 429 it is (plugins#817: a Start Plan
|
|
320
|
+
// account's GLM-5.3 out for the day answered 200 "exceed quota limit",
|
|
321
|
+
// which magpie took for a reply that did not stream and rested the whole
|
|
322
|
+
// account, GLM-5.3-Flash's own bucket with it). Any other answer goes on
|
|
323
|
+
// as it came; a stream's body is never read here.
|
|
324
|
+
async function spentSaid(res) {
|
|
325
|
+
if (res.status >= 400 || !(res.headers.get("content-type") ?? "").includes("application/json")) return res
|
|
326
|
+
const text = await res.text()
|
|
327
|
+
const status = spentUp(text, res.status) ? 429 : res.status
|
|
328
|
+
return new Response(text, { status, statusText: status === 429 ? "Too Many Requests" : res.statusText, headers: res.headers })
|
|
329
|
+
}
|
|
330
|
+
|
|
315
331
|
// ---- keys ------------------------------------------------------------------------
|
|
316
332
|
|
|
317
333
|
function teamHeaders(base, org, project) {
|
|
@@ -715,6 +731,62 @@ const CAPTCHA = 3007
|
|
|
715
731
|
// 1004 under 3.16.3's header, and four of them claimed with code 0 under
|
|
716
732
|
// 3.17.2's, one night, with nothing else changed).
|
|
717
733
|
const NOT_ELIGIBLE = 1004
|
|
734
|
+
// RATE_LIMITED is Z.ai's "请求过于频繁" body code, its answer (with HTTP
|
|
735
|
+
// 429) to a client that asked too often. #503 (MiniFullC, 11-04) read 180
|
|
736
|
+
// "gift plans to claim: Too Many Requests" over four days at magpie's
|
|
737
|
+
// 30-minute retry, so asking again the same half hour keeps the limit on.
|
|
738
|
+
// magpie asks a failed again once retryAfter() has passed, whatever the
|
|
739
|
+
// answer's `until` says (the contract reads `until` as when an answer
|
|
740
|
+
// stops standing, not as the earliest time to ask again), so the account
|
|
741
|
+
// is held here instead: while the wait lasts, claimDaily answers the same
|
|
742
|
+
// failed again, `until` and all, and asks ZCode nothing. A Retry-After
|
|
743
|
+
// ZCode sent is honored up to RATE_LIMIT_MAX_MS; without one the
|
|
744
|
+
// account's own wait doubles from RECHECK_MS, and a read that comes
|
|
745
|
+
// through forgets it.
|
|
746
|
+
const RATE_LIMITED = 3002
|
|
747
|
+
const RATE_LIMIT_MAX_MS = 6 * 60 * 60_000
|
|
748
|
+
const rateWait = new Map() // s.key -> { ms, until, message }: the hold this account is under
|
|
749
|
+
// resetRateWait is for tests: it forgets the holds, and with `expire` it
|
|
750
|
+
// back-dates them, standing where the wait has passed. The plugin itself
|
|
751
|
+
// forgets a hold by reading through or by waiting it out, never by calling
|
|
752
|
+
// this.
|
|
753
|
+
export function resetRateWait(expire = false) {
|
|
754
|
+
if (!expire) {
|
|
755
|
+
rateWait.clear()
|
|
756
|
+
return
|
|
757
|
+
}
|
|
758
|
+
for (const [k, h] of rateWait) rateWait.set(k, { ...h, until: new Date(Date.now() - 1).toISOString() })
|
|
759
|
+
}
|
|
760
|
+
// holdKey names the account a wait belongs to. stateOf gives key: "" to a
|
|
761
|
+
// sign-in that has a jwt and no API key — the Start-Plan-only sign-ins
|
|
762
|
+
// that claim gift plans, #503's case — so the key alone would put every
|
|
763
|
+
// one of them under a single hold, and one account's 429 would answer for
|
|
764
|
+
// all of them. The jwt tells them apart; a renewed token starting its own
|
|
765
|
+
// wait is fine.
|
|
766
|
+
const holdKey = (s) => s.key || s.jwt
|
|
767
|
+
// retryAfterMs is a Retry-After header, in seconds or as an HTTP date, in
|
|
768
|
+
// ms; 0 when there is none or it doesn't read.
|
|
769
|
+
function retryAfterMs(res) {
|
|
770
|
+
const v = first(res?.headers?.get?.("retry-after"))
|
|
771
|
+
if (!v) return 0
|
|
772
|
+
const secs = Number(v)
|
|
773
|
+
if (Number.isFinite(secs) && secs > 0) return secs * 1000
|
|
774
|
+
const at = Date.parse(v)
|
|
775
|
+
return Number.isFinite(at) ? Math.max(0, at - Date.now()) : 0
|
|
776
|
+
}
|
|
777
|
+
// noteRateLimit remembers the wait this account is under, with the failed
|
|
778
|
+
// that wait answers with, and says when it ends: a Retry-After ZCode sent
|
|
779
|
+
// is taken as it is, up to the cap; without one the wait doubles from
|
|
780
|
+
// RECHECK_MS (and never falls below it), so a client ZCode keeps turning
|
|
781
|
+
// away asks less and less.
|
|
782
|
+
function noteRateLimit(key, saidMs, message) {
|
|
783
|
+
const last = rateWait.get(key)?.ms ?? 0
|
|
784
|
+
const want = saidMs > 0 ? saidMs : Math.max(RECHECK_MS, last * 2)
|
|
785
|
+
const ms = Math.min(RATE_LIMIT_MAX_MS, want)
|
|
786
|
+
const until = new Date(Date.now() + ms).toISOString()
|
|
787
|
+
rateWait.set(key, { ms, until, message })
|
|
788
|
+
return until
|
|
789
|
+
}
|
|
718
790
|
const CAPTCHA_WORDS = /captcha/i
|
|
719
791
|
|
|
720
792
|
// The marketing feed and the claim are gated on the client's version:
|
|
@@ -747,6 +819,8 @@ async function claimOne(s, planID) {
|
|
|
747
819
|
} catch {}
|
|
748
820
|
const code = env && typeof env === "object" && env.code != null && String(env.code).trim() !== "" ? Number(env.code) : NaN
|
|
749
821
|
const msg = typeof env?.msg === "string" ? env.msg : ""
|
|
822
|
+
// a rate limit is no claim: it is said as one, and carries how long to wait
|
|
823
|
+
if (res.status === 429 || code === RATE_LIMITED) return { outcome: "failed", why: `${msg || STATUS_TEXT[429]} (HTTP ${res.status}${Number.isFinite(code) ? `, code ${code}` : ""})`, rate: retryAfterMs(res) }
|
|
750
824
|
if (code === CAPTCHA || CAPTCHA_WORDS.test(msg)) return { outcome: "captcha", why: msg || `code ${code}` }
|
|
751
825
|
if (code === CLAIMED_ALREADY) return { outcome: "done" }
|
|
752
826
|
if (code === NOT_ELIGIBLE) return { outcome: "ineligible", why: msg || `code ${code}` }
|
|
@@ -829,12 +903,14 @@ async function toClaim(s) {
|
|
|
829
903
|
d = await call("GET", `${ZCODE}${TOUCH}?seq=${touchSeq++}`, { auth: "Bearer " + s.jwt, device: s.device, headers: marketingHeaders() })
|
|
830
904
|
} catch (e) {
|
|
831
905
|
unread.push(`gift plans to claim: ${e?.message ?? String(e)}`)
|
|
832
|
-
|
|
906
|
+
// rate is the ms ZCode asked to wait (0 when it sent no Retry-After),
|
|
907
|
+
// null when this was not a rate limit at all
|
|
908
|
+
return { plans, unread, rate: e?.status === 429 || e?.code === RATE_LIMITED ? (e.retryAfterMs || 0) : null }
|
|
833
909
|
}
|
|
834
910
|
const got = touchPlans(d)
|
|
835
911
|
if (!got) unread.push("gift plans to claim: the plan list doesn't read")
|
|
836
912
|
else got.forEach(add)
|
|
837
|
-
return { plans, unread }
|
|
913
|
+
return { plans, unread, rate: null }
|
|
838
914
|
}
|
|
839
915
|
|
|
840
916
|
// RECHECK is how long an "nothing to claim" stands before magpie's loop
|
|
@@ -863,7 +939,16 @@ const recheck = () => new Date(Date.now() + RECHECK_MS).toISOString()
|
|
|
863
939
|
// captcha for one (it is claimed in the ZCode app), ineligible when
|
|
864
940
|
// ZCode is not offering one at that moment (code 1004, read again later
|
|
865
941
|
// in the day), failed when a list
|
|
866
|
-
// or a claim can't be read. A
|
|
942
|
+
// or a claim can't be read. A rate-limited read (HTTP 429, or code 3002)
|
|
943
|
+
// is not asked again either: the account is held here until the wait ends
|
|
944
|
+
// (a Retry-After ZCode sent, else the account's own wait doubling from
|
|
945
|
+
// RECHECK_MS, out to RATE_LIMIT_MAX_MS — see RATE_LIMITED), and every ask
|
|
946
|
+
// inside the wait answers the failed it already gave, without a request.
|
|
947
|
+
// The hold belongs to one account (`s.key || s.jwt`) and lives in this
|
|
948
|
+
// process's memory alone: a magpie restart forgets it. magpie from
|
|
949
|
+
// v0.1.3165 honors a failed's `until` itself; older versions wait for
|
|
950
|
+
// this hold, which is why it stays.
|
|
951
|
+
// A failed one is asked again later in the
|
|
867
952
|
// day, when a plan claimed now answers 1003. An inactive stands only
|
|
868
953
|
// until RECHECK_MS from now, so a plan dropped into the feed later in
|
|
869
954
|
// the day is still claimed; the other settled answers stand for the day
|
|
@@ -872,9 +957,26 @@ const recheck = () => new Date(Date.now() + RECHECK_MS).toISOString()
|
|
|
872
957
|
async function claimDaily(s) {
|
|
873
958
|
if (!s?.jwt) return { outcome: "ineligible", message: "ZCode's gift plans are claimed with a ZCode sign-in; this account has none" }
|
|
874
959
|
if (jwtExpired(s.jwt)) return { outcome: "failed", message: EXPIRED }
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
960
|
+
// a rate limit this account is still under: the same failed again, and
|
|
961
|
+
// not one request (magpie asks a failed again after 30 minutes of its
|
|
962
|
+
// own, so holding it is the plugin's job — see RATE_LIMITED)
|
|
963
|
+
const held = rateWait.get(holdKey(s))
|
|
964
|
+
if (held && Date.now() < Date.parse(held.until)) return { outcome: "failed", message: held.message, until: held.until }
|
|
965
|
+
const { plans, unread, rate = null } = await toClaim(s)
|
|
966
|
+
// a rate-limited read: held here until the wait is over
|
|
967
|
+
if (rate !== null) {
|
|
968
|
+
const message = unread.join("; ") || "ZCode rate limited this account"
|
|
969
|
+
return { outcome: "failed", message, until: noteRateLimit(holdKey(s), rate, message) }
|
|
970
|
+
}
|
|
971
|
+
// the feed answered and no claim is read: the wait is over. It is not
|
|
972
|
+
// forgotten before the claims below, so a claim ZCode keeps limiting
|
|
973
|
+
// waits longer each time (see noteRateLimit)
|
|
974
|
+
if (!plans.length) {
|
|
975
|
+
rateWait.delete(holdKey(s))
|
|
976
|
+
return unread.length
|
|
977
|
+
? { outcome: "failed", message: unread.join("; ") }
|
|
978
|
+
: { outcome: "inactive", message: "no ZCode gift plan to claim", until: recheck() }
|
|
979
|
+
}
|
|
878
980
|
const got = []
|
|
879
981
|
for (const p of plans) got.push({ name: p.name, ...(await claimOne(s, p.plan_id)) })
|
|
880
982
|
const named = (o) => got.filter((g) => g.outcome === o).map((g) => g.name)
|
|
@@ -888,6 +990,10 @@ async function claimDaily(s) {
|
|
|
888
990
|
].filter(Boolean).join("; ")
|
|
889
991
|
// a plan claimed now is used by the next request, not ten minutes on
|
|
890
992
|
if (claimed.length) routes.delete(s.key + "\0" + s.jwt)
|
|
993
|
+
// a claim ZCode rate limited: the plan may still be there, later
|
|
994
|
+
const limitedClaim = got.find((g) => g.rate !== undefined)
|
|
995
|
+
if (limitedClaim) return { outcome: "failed", message: said, until: noteRateLimit(holdKey(s), limitedClaim.rate, said) }
|
|
996
|
+
rateWait.delete(holdKey(s)) // nothing in this check-in was rate limited: the wait is over
|
|
891
997
|
// a list that didn't read may hold a plan not claimed: asked again
|
|
892
998
|
if (unread.length || got.some((g) => g.outcome === "failed")) return { outcome: "failed", message: said }
|
|
893
999
|
if (got.some((g) => g.outcome === "captcha")) return { outcome: "captcha", message: said }
|
|
@@ -2615,7 +2721,7 @@ export async function ZCodeAuthPlugin({ client, magpie } = {}, options = {}) {
|
|
|
2615
2721
|
if (EDGE_PAGE.test(text)) edge.until = Date.now() + EDGE_HOLD
|
|
2616
2722
|
if (freeGift) {
|
|
2617
2723
|
restModel(false)
|
|
2618
|
-
return done(kept(await replayOnPlan()))
|
|
2724
|
+
return done(kept(await spentSaid(await replayOnPlan())))
|
|
2619
2725
|
}
|
|
2620
2726
|
if (s.key && !forcedGift) routes.delete(s.key + "\0" + s.jwt)
|
|
2621
2727
|
return kept(startBlocked(res, text))
|
|
@@ -2663,18 +2769,20 @@ export async function ZCodeAuthPlugin({ client, magpie } = {}, options = {}) {
|
|
|
2663
2769
|
const spent = !cap && spentUp(text, res.status)
|
|
2664
2770
|
if (spent) {
|
|
2665
2771
|
restModel(true)
|
|
2666
|
-
return done(kept(await replayOnPlan()))
|
|
2772
|
+
return done(kept(await spentSaid(await replayOnPlan())))
|
|
2667
2773
|
}
|
|
2668
2774
|
if (res.status >= 400) {
|
|
2669
2775
|
if (!cap && (res.status === 401 || res.status === 403)) routes.delete(s.key + "\0" + s.jwt)
|
|
2670
|
-
return done(kept(await replayOnPlan()))
|
|
2776
|
+
return done(kept(await spentSaid(await replayOnPlan())))
|
|
2671
2777
|
}
|
|
2672
2778
|
return done(kept(res, text))
|
|
2673
2779
|
}
|
|
2674
2780
|
}
|
|
2675
2781
|
// the plan's answer goes on as it came, the account kept: the
|
|
2676
|
-
// built-in never marked a ZCode account lapsed nor cleared one
|
|
2677
|
-
|
|
2782
|
+
// built-in never marked a ZCode account lapsed nor cleared one.
|
|
2783
|
+
// A spent bucket's 200 says 429 (spentSaid), as a trial
|
|
2784
|
+
// entry's does, so magpie rests the model, not the account
|
|
2785
|
+
return done(kept(await spentSaid(res)))
|
|
2678
2786
|
},
|
|
2679
2787
|
}
|
|
2680
2788
|
},
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@magpie-community/opencode-zcode-auth",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.53",
|
|
4
4
|
"description": "OpenCode provider plugin: signs in to Z.ai's or BigModel's (智谱) GLM Coding Plan as ZCode does, including team seats and ZCode's Start Plan, and claims its gift plans daily",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./opencode.mjs",
|