@magpie-community/opencode-zcode-auth 0.1.53 → 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 +78 -6
- 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
|
@@ -1931,6 +1931,56 @@ const BUSY_WAITS = [250, 500, 900, 1600, 2800, 4000]
|
|
|
1931
1931
|
const BUSY_HINT =
|
|
1932
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"
|
|
1933
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
|
+
|
|
1934
1984
|
// busy says a 429 is that in-flight cap: its own code (3008, 3009), or its
|
|
1935
1985
|
// own words when the envelope carries no code.
|
|
1936
1986
|
function busy(status, text) {
|
|
@@ -1977,17 +2027,26 @@ function busyAnswer(res, text) {
|
|
|
1977
2027
|
// account is saturated rather than busy, and the cap goes back as
|
|
1978
2028
|
// busyAnswer's 429. The plan path's patience, shared with free first's
|
|
1979
2029
|
// replay, whose plan has a cap of its own.
|
|
1980
|
-
async function askBusy(u, o, hs) {
|
|
2030
|
+
async function askBusy(u, o, hs, onCap) {
|
|
1981
2031
|
let res = await fetch(u, { ...o, headers: hs })
|
|
2032
|
+
let counted = false
|
|
1982
2033
|
for (let i = 0; res.status === 429; i++) {
|
|
1983
2034
|
const text = await res.text()
|
|
1984
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 }
|
|
1985
2043
|
if (!cap || i >= BUSY_WAITS.length) {
|
|
1986
2044
|
res = cap ? busyAnswer(res, text) : new Response(text, { status: res.status, statusText: res.statusText, headers: res.headers })
|
|
1987
2045
|
break
|
|
1988
2046
|
}
|
|
1989
2047
|
const w = BUSY_WAITS[i]
|
|
1990
2048
|
await sleep(Math.round(w * (0.75 + Math.random() * 0.5)))
|
|
2049
|
+
if (onCap && busyHeld()) { res = busyHeldAnswer(); break }
|
|
1991
2050
|
res = await fetch(u, { ...o, headers: hs })
|
|
1992
2051
|
}
|
|
1993
2052
|
return res
|
|
@@ -2594,7 +2653,7 @@ export async function ZCodeAuthPlugin({ client, magpie } = {}, options = {}) {
|
|
|
2594
2653
|
orig = opts.body = JSON.stringify({ ...JSON.parse(orig), model: wireModel(raw) })
|
|
2595
2654
|
m = raw
|
|
2596
2655
|
forcedGift = true
|
|
2597
|
-
} 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)) {
|
|
2598
2657
|
// free first: the plain model spends the account's own live
|
|
2599
2658
|
// gift before it spends the coding plan, under the name the
|
|
2600
2659
|
// gift lists. The gift is the route's cache alone (a failed
|
|
@@ -2602,7 +2661,11 @@ export async function ZCodeAuthPlugin({ client, magpie } = {}, options = {}) {
|
|
|
2602
2661
|
// the request on the plan with no read per request; a
|
|
2603
2662
|
// gift-only account is already on the gift and is left as
|
|
2604
2663
|
// it was. A refusal past this check is replayed on the plan
|
|
2605
|
-
// 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.
|
|
2606
2669
|
const raw = giftNames(known).find((x) => x.toLowerCase() === m.toLowerCase())
|
|
2607
2670
|
orig = opts.body = JSON.stringify({ ...JSON.parse(orig), model: wireModel(raw) })
|
|
2608
2671
|
m = raw
|
|
@@ -2657,8 +2720,14 @@ export async function ZCodeAuthPlugin({ client, magpie } = {}, options = {}) {
|
|
|
2657
2720
|
}
|
|
2658
2721
|
// a block page held this computer off zcode.z.ai (edge): the
|
|
2659
2722
|
// Start Plan and a picked gift are answered here; free first
|
|
2660
|
-
// 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.
|
|
2661
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())
|
|
2662
2731
|
if (start) {
|
|
2663
2732
|
if (jwtExpired(s.jwt)) throw new Error(EXPIRED)
|
|
2664
2733
|
if (!s.jwt) throw new Error("ZCode's sign-in has no key; sign in again")
|
|
@@ -2698,7 +2767,10 @@ export async function ZCodeAuthPlugin({ client, magpie } = {}, options = {}) {
|
|
|
2698
2767
|
// the number declared above
|
|
2699
2768
|
let res
|
|
2700
2769
|
try {
|
|
2701
|
-
|
|
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)
|
|
2702
2774
|
} catch (e) {
|
|
2703
2775
|
// free first moved the request onto the gift, so a gift that
|
|
2704
2776
|
// cannot be asked at all — the network, the host — is answered
|
|
@@ -2903,4 +2975,4 @@ export async function ZCodeAuthPlugin({ client, magpie } = {}, options = {}) {
|
|
|
2903
2975
|
// config sets what zcodeConfig has read, for tests: null is ZCode's config
|
|
2904
2976
|
// out of reach, undefined forgets it
|
|
2905
2977
|
const config = (cfg) => (cfgCache = cfg === undefined ? null : { cfg, at: Date.now() })
|
|
2906
|
-
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",
|