@magpie-community/opencode-zcode-auth 0.1.52 → 0.1.54
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 +17 -0
- package/index.mjs +98 -11
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -213,6 +213,23 @@ number written into the plugin would hold a Max account to a Lite one's
|
|
|
213
213
|
with no way to raise it. A user who knows their plan's cap sets it on the
|
|
214
214
|
account or the provider in magpie, and magpie's queue follows that.
|
|
215
215
|
|
|
216
|
+
A run of caps is also the edge firewall's countdown: two days of logs
|
|
217
|
+
(11-05 and 11-06) show ~18 requests/min on the Start Plan bringing 3008
|
|
218
|
+
caps in a run, then 3–7 minutes later a 405 block page that refuses the
|
|
219
|
+
whole IP for 1–2 hours. The waits absorb most caps, so neither magpie nor
|
|
220
|
+
the agent sees the address is saturated and nobody slows the arrival rate.
|
|
221
|
+
Five cap-hit requests in one minute now hold Start Plan requests off
|
|
222
|
+
`zcode.z.ai` for three minutes, process-wide and for every account, and
|
|
223
|
+
answer them here with a 429 and `Retry-After`, so magpie still rests the
|
|
224
|
+
account and asks the group's next provider. A coding plan, served by
|
|
225
|
+
another host, is never held. The window clears when the hold is armed, so
|
|
226
|
+
the hold's own 429s cannot keep it armed, and a request already in
|
|
227
|
+
flight stops asking too: once the hold is armed, its remaining waits
|
|
228
|
+
answer locally with the hold's 429 instead of asking the counting
|
|
229
|
+
firewall again. This is distinct from the network's 405 block page hold
|
|
230
|
+
(plugins#649), which is what an IP already refused needs; when both are
|
|
231
|
+
on, the 405 hold's answer wins.
|
|
232
|
+
|
|
216
233
|
## Two accounts at once
|
|
217
234
|
|
|
218
235
|
Three ZCode accounts used at once from one address can get the address
|
package/index.mjs
CHANGED
|
@@ -315,6 +315,19 @@ function spentUp(text, status) {
|
|
|
315
315
|
return c1 === "1113" || c1 === "1005" || c2 === "1113" || c2 === "1005"
|
|
316
316
|
}
|
|
317
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
|
+
|
|
318
331
|
// ---- keys ------------------------------------------------------------------------
|
|
319
332
|
|
|
320
333
|
function teamHeaders(base, org, project) {
|
|
@@ -1918,6 +1931,56 @@ const BUSY_WAITS = [250, 500, 900, 1600, 2800, 4000]
|
|
|
1918
1931
|
const BUSY_HINT =
|
|
1919
1932
|
"ZCode serves one account a few requests at a time, and every agent and session of this computer asks through magpie; this request came behind the others, and the same request a moment later is served"
|
|
1920
1933
|
|
|
1934
|
+
// busyHold is the firewall's countdown. Two days of logs (11-05 23:06–23:15
|
|
1935
|
+
// and 11-06 06:23–06:27) show the same shape: sustained ~18 requests/min on
|
|
1936
|
+
// the Start Plan brings code-3008 caps in a run, and 3–7.5 minutes later the
|
|
1937
|
+
// edge firewall (errors.aliyun.com) refuses the whole IP with 405 for 1–2
|
|
1938
|
+
// hours. askBusy's per-request waits absorb ~90% of those caps, so neither
|
|
1939
|
+
// magpie nor the agent sees that the address is saturated and nobody slows
|
|
1940
|
+
// the arrival rate down. A run of BUSY_N caps in one minute holds the Start
|
|
1941
|
+
// Plan (zcode.z.ai only, process-wide, every account) off BUSY_HOLD. Replay
|
|
1942
|
+
// of both days: N=5, H=3min freezes 2–7 minutes before the first 405 and
|
|
1943
|
+
// spares 37–52 requests. A held request answers 429 with Retry-After, so
|
|
1944
|
+
// magpie still reads a rate limit, rests the account and fails over; it is
|
|
1945
|
+
// not the 405/BlockedHint of an IP already refused. The coding plan, on
|
|
1946
|
+
// another host, is never held. The hold lives in this process's memory
|
|
1947
|
+
// alone: magpie restarting forgets it. Only model requests are held —
|
|
1948
|
+
// balance reads, the usage card and check-in still reach zcode.z.ai during
|
|
1949
|
+
// it (the check-in holds its own reads per account on a 429).
|
|
1950
|
+
const BUSY_N = 5
|
|
1951
|
+
const BUSY_WINDOW = 60_000
|
|
1952
|
+
const BUSY_HOLD = 3 * 60_000
|
|
1953
|
+
const busyHold = { hits: [], until: 0, now: () => Date.now() }
|
|
1954
|
+
// busyHold.now is the clock, injectable for tests: a hold's window, its
|
|
1955
|
+
// length and its expiry all read through it.
|
|
1956
|
+
const busyHeld = () => busyHold.now() < busyHold.until
|
|
1957
|
+
|
|
1958
|
+
// busyCap records one concurrency cap of a Start Plan request. Only a run
|
|
1959
|
+
// arms a hold; the window is cleared on arming, and a cap that lands while
|
|
1960
|
+
// the hold is on counts toward nothing — the hold's own 429s and the first
|
|
1961
|
+
// caps after it cannot keep re-arming it.
|
|
1962
|
+
function busyCap() {
|
|
1963
|
+
if (busyHeld()) return
|
|
1964
|
+
const now = busyHold.now()
|
|
1965
|
+
busyHold.hits.push(now)
|
|
1966
|
+
while (busyHold.hits.length && now - busyHold.hits[0] > BUSY_WINDOW) busyHold.hits.shift()
|
|
1967
|
+
if (busyHold.hits.length >= BUSY_N) {
|
|
1968
|
+
busyHold.until = now + BUSY_HOLD
|
|
1969
|
+
busyHold.hits.length = 0
|
|
1970
|
+
}
|
|
1971
|
+
}
|
|
1972
|
+
|
|
1973
|
+
function busyHeldAnswer() {
|
|
1974
|
+
const wait = Math.max(1, Math.ceil((busyHold.until - busyHold.now()) / 1000))
|
|
1975
|
+
const error = {
|
|
1976
|
+
type: "rate_limit_error",
|
|
1977
|
+
message: `ZCode is busy: its concurrency cap was hit ${BUSY_N} times in a minute, so this computer's Start Plan requests are held for ${wait}s to keep the provider's network firewall from blocking this IP; wait a while, or use a GLM Coding Plan model`,
|
|
1978
|
+
}
|
|
1979
|
+
return new Response(JSON.stringify({ type: "error", error }), {
|
|
1980
|
+
status: 429, statusText: STATUS_TEXT[429], headers: { "content-type": "application/json", "retry-after": String(wait) },
|
|
1981
|
+
})
|
|
1982
|
+
}
|
|
1983
|
+
|
|
1921
1984
|
// busy says a 429 is that in-flight cap: its own code (3008, 3009), or its
|
|
1922
1985
|
// own words when the envelope carries no code.
|
|
1923
1986
|
function busy(status, text) {
|
|
@@ -1964,17 +2027,26 @@ function busyAnswer(res, text) {
|
|
|
1964
2027
|
// account is saturated rather than busy, and the cap goes back as
|
|
1965
2028
|
// busyAnswer's 429. The plan path's patience, shared with free first's
|
|
1966
2029
|
// replay, whose plan has a cap of its own.
|
|
1967
|
-
async function askBusy(u, o, hs) {
|
|
2030
|
+
async function askBusy(u, o, hs, onCap) {
|
|
1968
2031
|
let res = await fetch(u, { ...o, headers: hs })
|
|
2032
|
+
let counted = false
|
|
1969
2033
|
for (let i = 0; res.status === 429; i++) {
|
|
1970
2034
|
const text = await res.text()
|
|
1971
2035
|
const cap = busy(429, text)
|
|
2036
|
+
// one request's retries are one saturation signal, not one per wait
|
|
2037
|
+
if (cap && !counted) { counted = true; onCap?.() }
|
|
2038
|
+
// the hold armed: a request already in flight stops asking too. The
|
|
2039
|
+
// second check catches the asks that capped before the hold armed and
|
|
2040
|
+
// woke from their wait after it. Keying on onCap keeps the coding plan
|
|
2041
|
+
// and free first's replay (no onCap) as they are.
|
|
2042
|
+
if (cap && onCap && busyHeld()) { res = busyHeldAnswer(); break }
|
|
1972
2043
|
if (!cap || i >= BUSY_WAITS.length) {
|
|
1973
2044
|
res = cap ? busyAnswer(res, text) : new Response(text, { status: res.status, statusText: res.statusText, headers: res.headers })
|
|
1974
2045
|
break
|
|
1975
2046
|
}
|
|
1976
2047
|
const w = BUSY_WAITS[i]
|
|
1977
2048
|
await sleep(Math.round(w * (0.75 + Math.random() * 0.5)))
|
|
2049
|
+
if (onCap && busyHeld()) { res = busyHeldAnswer(); break }
|
|
1978
2050
|
res = await fetch(u, { ...o, headers: hs })
|
|
1979
2051
|
}
|
|
1980
2052
|
return res
|
|
@@ -2581,7 +2653,7 @@ export async function ZCodeAuthPlugin({ client, magpie } = {}, options = {}) {
|
|
|
2581
2653
|
orig = opts.body = JSON.stringify({ ...JSON.parse(orig), model: wireModel(raw) })
|
|
2582
2654
|
m = raw
|
|
2583
2655
|
forcedGift = true
|
|
2584
|
-
} else if (!alone && !edgeHeld() && freeFirst(provider?.options) && s.jwt && known && giftUsable(s, known, m) && !giftSidelined(s, m)) {
|
|
2656
|
+
} else if (!alone && !edgeHeld() && !busyHeld() && freeFirst(provider?.options) && s.jwt && known && giftUsable(s, known, m) && !giftSidelined(s, m)) {
|
|
2585
2657
|
// free first: the plain model spends the account's own live
|
|
2586
2658
|
// gift before it spends the coding plan, under the name the
|
|
2587
2659
|
// gift lists. The gift is the route's cache alone (a failed
|
|
@@ -2589,7 +2661,11 @@ export async function ZCodeAuthPlugin({ client, magpie } = {}, options = {}) {
|
|
|
2589
2661
|
// the request on the plan with no read per request; a
|
|
2590
2662
|
// gift-only account is already on the gift and is left as
|
|
2591
2663
|
// it was. A refusal past this check is replayed on the plan
|
|
2592
|
-
// in the same request (replayOnPlan below).
|
|
2664
|
+
// in the same request (replayOnPlan below). A busy hold (or
|
|
2665
|
+
// a block page) keeps the plain model off the gift: the
|
|
2666
|
+
// coding plan is never held, and free first must not move
|
|
2667
|
+
// the request onto a route the hold answers 429 without
|
|
2668
|
+
// asking.
|
|
2593
2669
|
const raw = giftNames(known).find((x) => x.toLowerCase() === m.toLowerCase())
|
|
2594
2670
|
orig = opts.body = JSON.stringify({ ...JSON.parse(orig), model: wireModel(raw) })
|
|
2595
2671
|
m = raw
|
|
@@ -2644,8 +2720,14 @@ export async function ZCodeAuthPlugin({ client, magpie } = {}, options = {}) {
|
|
|
2644
2720
|
}
|
|
2645
2721
|
// a block page held this computer off zcode.z.ai (edge): the
|
|
2646
2722
|
// Start Plan and a picked gift are answered here; free first
|
|
2647
|
-
// already left the gift for the plan
|
|
2723
|
+
// already left the gift for the plan. Checked before the busy
|
|
2724
|
+
// hold: an IP the firewall already refused keeps that behavior
|
|
2725
|
+
// (the 405 magpie reads as the network's), hold or no hold.
|
|
2648
2726
|
if (start && edgeHeld()) return kept(edgeHeldAnswer())
|
|
2727
|
+
// a run of busy caps held this computer off zcode.z.ai before the
|
|
2728
|
+
// edge firewall could block the IP: a held request is answered
|
|
2729
|
+
// here, with a 429 magpie still reads as a rate limit
|
|
2730
|
+
if (start && busyHeld()) return kept(busyHeldAnswer())
|
|
2649
2731
|
if (start) {
|
|
2650
2732
|
if (jwtExpired(s.jwt)) throw new Error(EXPIRED)
|
|
2651
2733
|
if (!s.jwt) throw new Error("ZCode's sign-in has no key; sign in again")
|
|
@@ -2685,7 +2767,10 @@ export async function ZCodeAuthPlugin({ client, magpie } = {}, options = {}) {
|
|
|
2685
2767
|
// the number declared above
|
|
2686
2768
|
let res
|
|
2687
2769
|
try {
|
|
2688
|
-
|
|
2770
|
+
// Only Start Plan caps count toward the firewall hold: the
|
|
2771
|
+
// coding plan is served by another host and was never refused
|
|
2772
|
+
// with the IP's 405.
|
|
2773
|
+
res = await askBusy(url, opts, h, start ? busyCap : undefined)
|
|
2689
2774
|
} catch (e) {
|
|
2690
2775
|
// free first moved the request onto the gift, so a gift that
|
|
2691
2776
|
// cannot be asked at all — the network, the host — is answered
|
|
@@ -2708,7 +2793,7 @@ export async function ZCodeAuthPlugin({ client, magpie } = {}, options = {}) {
|
|
|
2708
2793
|
if (EDGE_PAGE.test(text)) edge.until = Date.now() + EDGE_HOLD
|
|
2709
2794
|
if (freeGift) {
|
|
2710
2795
|
restModel(false)
|
|
2711
|
-
return done(kept(await replayOnPlan()))
|
|
2796
|
+
return done(kept(await spentSaid(await replayOnPlan())))
|
|
2712
2797
|
}
|
|
2713
2798
|
if (s.key && !forcedGift) routes.delete(s.key + "\0" + s.jwt)
|
|
2714
2799
|
return kept(startBlocked(res, text))
|
|
@@ -2756,18 +2841,20 @@ export async function ZCodeAuthPlugin({ client, magpie } = {}, options = {}) {
|
|
|
2756
2841
|
const spent = !cap && spentUp(text, res.status)
|
|
2757
2842
|
if (spent) {
|
|
2758
2843
|
restModel(true)
|
|
2759
|
-
return done(kept(await replayOnPlan()))
|
|
2844
|
+
return done(kept(await spentSaid(await replayOnPlan())))
|
|
2760
2845
|
}
|
|
2761
2846
|
if (res.status >= 400) {
|
|
2762
2847
|
if (!cap && (res.status === 401 || res.status === 403)) routes.delete(s.key + "\0" + s.jwt)
|
|
2763
|
-
return done(kept(await replayOnPlan()))
|
|
2848
|
+
return done(kept(await spentSaid(await replayOnPlan())))
|
|
2764
2849
|
}
|
|
2765
2850
|
return done(kept(res, text))
|
|
2766
2851
|
}
|
|
2767
2852
|
}
|
|
2768
2853
|
// the plan's answer goes on as it came, the account kept: the
|
|
2769
|
-
// built-in never marked a ZCode account lapsed nor cleared one
|
|
2770
|
-
|
|
2854
|
+
// built-in never marked a ZCode account lapsed nor cleared one.
|
|
2855
|
+
// A spent bucket's 200 says 429 (spentSaid), as a trial
|
|
2856
|
+
// entry's does, so magpie rests the model, not the account
|
|
2857
|
+
return done(kept(await spentSaid(res)))
|
|
2771
2858
|
},
|
|
2772
2859
|
}
|
|
2773
2860
|
},
|
|
@@ -2888,4 +2975,4 @@ export async function ZCodeAuthPlugin({ client, magpie } = {}, options = {}) {
|
|
|
2888
2975
|
// config sets what zcodeConfig has read, for tests: null is ZCode's config
|
|
2889
2976
|
// out of reach, undefined forgets it
|
|
2890
2977
|
const config = (cfg) => (cfgCache = cfg === undefined ? null : { cfg, at: Date.now() })
|
|
2891
|
-
export const _internal = { edge, EDGE_HOLD, config, entry, limitWindows, termOf, startUsage, routes, blocked, sidelined, plansOf, giftOf, dualUsage, claimHint, claimDaily, spentUp, giftServes, modelOf, teamKeys, ownSignIn, stateOf, located, dress, PROMPT, marked, keyDevice, sessionOf, busy, BUSY_WAITS, DEFAULT_CONCURRENCY, DEFAULT_ACCOUNTS }
|
|
2978
|
+
export const _internal = { edge, EDGE_HOLD, busyHold, BUSY_N, BUSY_WINDOW, BUSY_HOLD, busyCap, busyHeld, config, entry, limitWindows, termOf, startUsage, routes, blocked, sidelined, plansOf, giftOf, dualUsage, claimHint, claimDaily, spentUp, giftServes, modelOf, teamKeys, ownSignIn, stateOf, located, dress, PROMPT, marked, keyDevice, sessionOf, busy, BUSY_WAITS, DEFAULT_CONCURRENCY, DEFAULT_ACCOUNTS }
|
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.54",
|
|
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",
|