@magpie-community/opencode-zcode-auth 0.1.57 → 0.1.58
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 +22 -0
- package/index.mjs +59 -12
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -144,6 +144,14 @@ plan's own in-flight cap (3008/3009/3010) does not: such a request is asked
|
|
|
144
144
|
again instead (see [A few at once](#a-few-at-once)). A stream's body is
|
|
145
145
|
never read or buffered on the way.
|
|
146
146
|
|
|
147
|
+
Since 0.1.58 the trial entries are marked free in the model list: they
|
|
148
|
+
spend the gift, which costs nothing. magpie then reads a coding plan
|
|
149
|
+
model's weekly or monthly limit (code 1310) as that model's, not the
|
|
150
|
+
account's, so the account's `-Trial` entries are still asked while the
|
|
151
|
+
plan's model rests (plugins#438). In magpie's pickers the trial entries
|
|
152
|
+
carry the Free mark, and a routing group that prefers free models picks
|
|
153
|
+
them first.
|
|
154
|
+
|
|
147
155
|
**Free first.** Picking `-Trial` by hand keeps the choice the user's,
|
|
148
156
|
but an agent that always asks the plain model never sees the gift.
|
|
149
157
|
`freeFirst`, on by default since 0.1.42, turns the preference around:
|
|
@@ -251,6 +259,20 @@ already refused needs; when both are on, the 405 hold's answer wins.
|
|
|
251
259
|
refuses past three in flight: 0.1.55 drew 24 refusals in it, this version
|
|
252
260
|
none.
|
|
253
261
|
|
|
262
|
+
Some accounts are served fewer than three at a time (plugins#438: one
|
|
263
|
+
account, one agent, refused at two in flight). Since 0.1.58 a request
|
|
264
|
+
refused again at its 1s re-ask while others of the same account were in
|
|
265
|
+
flight beside it narrows that account's gate to one fewer than were in
|
|
266
|
+
flight, at least one, for five minutes, after which it is back to three.
|
|
267
|
+
Such a refusal is this computer asking too much at once, which the
|
|
268
|
+
narrowed gate now stops, so it counts toward no hold; up to 0.1.57 those
|
|
269
|
+
refusals armed the hold at about five requests a minute, and armed it
|
|
270
|
+
again within a minute of its end. A request refused again alone, nothing
|
|
271
|
+
else of its account in flight, still counts as before: someone else holds
|
|
272
|
+
the slots. An account whose slots are taken elsewhere is narrowed to one
|
|
273
|
+
within a few refusals and then refused alone, so the firewall hold still
|
|
274
|
+
comes before the 405.
|
|
275
|
+
|
|
254
276
|
## Two accounts at once
|
|
255
277
|
|
|
256
278
|
Three ZCode accounts used at once from one address can get the address
|
package/index.mjs
CHANGED
|
@@ -2004,12 +2004,46 @@ const BUSY_HINT =
|
|
|
2004
2004
|
// the queue. A wait that runs out is answered startQueueAnswer's 429,
|
|
2005
2005
|
// nothing sent. The gate lives in this process's memory alone, by account
|
|
2006
2006
|
// (gateKey).
|
|
2007
|
-
|
|
2007
|
+
//
|
|
2008
|
+
// An account ZCode serves fewer than that (plugins#438: one account, one
|
|
2009
|
+
// agent, never more than two in flight, and refused at two) is narrowed:
|
|
2010
|
+
// a request refused again at its 1s re-ask while others of this computer
|
|
2011
|
+
// were in flight beside it (busyCap) tells the cap is below the gate, and
|
|
2012
|
+
// the account's gate goes down to one fewer than were in flight, at least
|
|
2013
|
+
// one, for NARROW_FOR. Such a cap is this computer asking too much at once,
|
|
2014
|
+
// which the narrowed gate now stops, so it counts toward no firewall hold.
|
|
2015
|
+
// narrow is kept apart from lanes, which go when idle; now is its clock,
|
|
2016
|
+
// injectable for tests.
|
|
2017
|
+
const NARROW_FOR = 5 * 60_000
|
|
2018
|
+
const startGate = { inflight: 3, wait: 120_000, lanes: new Map(), narrow: new Map(), now: () => Date.now() }
|
|
2019
|
+
|
|
2020
|
+
// limitOf is how many of the account's Start Plan requests go at once: the
|
|
2021
|
+
// gate's number, or less while the account is narrowed.
|
|
2022
|
+
function limitOf(key) {
|
|
2023
|
+
const n = startGate.narrow.get(key)
|
|
2024
|
+
if (!n) return startGate.inflight
|
|
2025
|
+
if (startGate.now() >= n.until) {
|
|
2026
|
+
startGate.narrow.delete(key)
|
|
2027
|
+
return startGate.inflight
|
|
2028
|
+
}
|
|
2029
|
+
return Math.min(startGate.inflight, n.limit)
|
|
2030
|
+
}
|
|
2031
|
+
|
|
2032
|
+
// narrowed lowers the account's gate below the requests that were in flight
|
|
2033
|
+
// when one was refused again (busy, its own included), and says whether it
|
|
2034
|
+
// did: a lone request refused again is not this computer's doing.
|
|
2035
|
+
function narrowed(key) {
|
|
2036
|
+
const busy = startGate.lanes.get(key)?.busy ?? 0
|
|
2037
|
+
if (busy < 2) return false
|
|
2038
|
+
const limit = Math.max(1, Math.min(limitOf(key), busy - 1))
|
|
2039
|
+
startGate.narrow.set(key, { limit, until: startGate.now() + NARROW_FOR })
|
|
2040
|
+
return true
|
|
2041
|
+
}
|
|
2008
2042
|
|
|
2009
2043
|
// startFull says the account's Start Plan slots are all taken, or others wait.
|
|
2010
2044
|
const startFull = (key) => {
|
|
2011
2045
|
const l = startGate.lanes.get(key)
|
|
2012
|
-
return !!l && (l.busy >=
|
|
2046
|
+
return !!l && (l.busy >= limitOf(key) || l.queue.length > 0)
|
|
2013
2047
|
}
|
|
2014
2048
|
|
|
2015
2049
|
// startSlot waits for one of the account's Start Plan slots: { release },
|
|
@@ -2019,7 +2053,7 @@ function startSlot(key, signal) {
|
|
|
2019
2053
|
let l = lanes.get(key)
|
|
2020
2054
|
if (!l) lanes.set(key, (l = { busy: 0, queue: [] }))
|
|
2021
2055
|
const next = () => {
|
|
2022
|
-
while (l.busy <
|
|
2056
|
+
while (l.busy < limitOf(key) && l.queue.length) l.queue.shift()()
|
|
2023
2057
|
if (!l.busy && !l.queue.length && lanes.get(key) === l) lanes.delete(key)
|
|
2024
2058
|
}
|
|
2025
2059
|
const take = () => {
|
|
@@ -2035,7 +2069,7 @@ function startSlot(key, signal) {
|
|
|
2035
2069
|
}
|
|
2036
2070
|
}
|
|
2037
2071
|
if (signal?.aborted) return Promise.reject(signal.reason ?? new DOMException("The operation was aborted.", "AbortError"))
|
|
2038
|
-
if (l.busy <
|
|
2072
|
+
if (l.busy < limitOf(key) && !l.queue.length) return Promise.resolve(take())
|
|
2039
2073
|
return new Promise((resolve, reject) => {
|
|
2040
2074
|
const leave = () => {
|
|
2041
2075
|
clearTimeout(timer)
|
|
@@ -2103,10 +2137,11 @@ function released(res, slot, signal) {
|
|
|
2103
2137
|
// startQueueAnswer is a request that waited its whole turn behind the
|
|
2104
2138
|
// account's others and was never sent: a 429 magpie reads as a rate limit,
|
|
2105
2139
|
// so it tries the next account, and a short Retry-After.
|
|
2106
|
-
function startQueueAnswer() {
|
|
2140
|
+
function startQueueAnswer(key) {
|
|
2141
|
+
const n = limitOf(key)
|
|
2107
2142
|
const error = {
|
|
2108
2143
|
type: "rate_limit_error",
|
|
2109
|
-
message: `ZCode's free Start Plan serves this account ${
|
|
2144
|
+
message: `ZCode's free Start Plan serves this account ${n} request${n === 1 ? "" : "s"} at a time; this one waited ${Math.round(startGate.wait / 1000)}s behind the others and was not sent. Run fewer agents or subagents on ZCode at once, or try again`,
|
|
2110
2145
|
}
|
|
2111
2146
|
return new Response(JSON.stringify({ type: "error", error }), {
|
|
2112
2147
|
status: 429, statusText: STATUS_TEXT[429], headers: { "content-type": "application/json", "retry-after": "5" },
|
|
@@ -2124,7 +2159,14 @@ function startQueueAnswer() {
|
|
|
2124
2159
|
// busy or another client on the account; a request refused on its first ask
|
|
2125
2160
|
// and again a second later (busyCap) is that, sustained, and BUSY_N of them
|
|
2126
2161
|
// in one minute hold the Start Plan (zcode.z.ai only, process-wide, every
|
|
2127
|
-
// account) off BUSY_HOLD. A cap
|
|
2162
|
+
// account) off BUSY_HOLD. A sustained cap of a request with others of the
|
|
2163
|
+
// same account in flight beside it counts for nothing either: it narrows the
|
|
2164
|
+
// account's gate (startGate), which then sends no more than ZCode serves
|
|
2165
|
+
// (plugins#438: an account served one at a time, two in flight from one
|
|
2166
|
+
// agent, held again and again at ~5 requests/min). #801's caps came with up
|
|
2167
|
+
// to ~10 in flight, which the gate now never sends, and an account whose
|
|
2168
|
+
// slots are taken elsewhere is narrowed to one within a few caps and then
|
|
2169
|
+
// refused alone, which counts as before. A cap the 1s re-ask got past counts for nothing:
|
|
2128
2170
|
// on 0.1.55 every capped request counted, a lone late slot as much as a
|
|
2129
2171
|
// saturated address, and busy afternoons held users for nothing. Replay of
|
|
2130
2172
|
// both days: N=5, H=3min froze 2–7 minutes before the first 405 and spared
|
|
@@ -2147,7 +2189,8 @@ const busyHeld = () => busyHold.now() < busyHold.until
|
|
|
2147
2189
|
// Only a run arms a hold; the window is cleared on arming, and a cap that
|
|
2148
2190
|
// lands while the hold is on counts toward nothing — the hold's own 429s and
|
|
2149
2191
|
// the first caps after it cannot keep re-arming it.
|
|
2150
|
-
function busyCap() {
|
|
2192
|
+
function busyCap(key) {
|
|
2193
|
+
if (key != null && narrowed(key)) return
|
|
2151
2194
|
if (busyHeld()) return
|
|
2152
2195
|
const now = busyHold.now()
|
|
2153
2196
|
busyHold.hits.push(now)
|
|
@@ -2789,7 +2832,11 @@ export async function ZCodeAuthPlugin({ client, magpie } = {}, options = {}) {
|
|
|
2789
2832
|
}
|
|
2790
2833
|
const id = giftID(raw)
|
|
2791
2834
|
if (out[id]) continue
|
|
2792
|
-
|
|
2835
|
+
// a trial entry is the gift's, which costs nothing: marked free,
|
|
2836
|
+
// so magpie reads a GLM Coding Plan model's weekly or monthly
|
|
2837
|
+
// limit (1310) as that model's, and the account's trial entries,
|
|
2838
|
+
// spent from the gift's own buckets, are still asked (plugins#438)
|
|
2839
|
+
out[id] = { ...model({ ...about(raw), id }, url), free: true }
|
|
2793
2840
|
}
|
|
2794
2841
|
// a fallback tells magpie to keep the list it was told last, which
|
|
2795
2842
|
// predates the gift read just made: an account whose gift lists
|
|
@@ -2980,7 +3027,7 @@ export async function ZCodeAuthPlugin({ client, magpie } = {}, options = {}) {
|
|
|
2980
3027
|
if (start) {
|
|
2981
3028
|
own.slot = await startSlot(gateKey, opts.signal)
|
|
2982
3029
|
own.signal = opts.signal
|
|
2983
|
-
if (!own.slot) return kept(startQueueAnswer())
|
|
3030
|
+
if (!own.slot) return kept(startQueueAnswer(gateKey))
|
|
2984
3031
|
// a hold may have armed while it waited
|
|
2985
3032
|
if (edgeHeld()) return kept(edgeHeldAnswer())
|
|
2986
3033
|
if (busyHeld()) return kept(busyHeldAnswer())
|
|
@@ -3029,7 +3076,7 @@ export async function ZCodeAuthPlugin({ client, magpie } = {}, options = {}) {
|
|
|
3029
3076
|
// Only Start Plan caps count toward the firewall hold: the
|
|
3030
3077
|
// coding plan is served by another host and was never refused
|
|
3031
3078
|
// with the IP's 405.
|
|
3032
|
-
res = await askBusy(url, opts, h, start ? busyCap : undefined)
|
|
3079
|
+
res = await askBusy(url, opts, h, start ? () => busyCap(gateKey) : undefined)
|
|
3033
3080
|
} catch (e) {
|
|
3034
3081
|
// free first moved the request onto the gift, so a gift that
|
|
3035
3082
|
// cannot be asked at all — the network, the host — is answered
|
|
@@ -3239,4 +3286,4 @@ export async function ZCodeAuthPlugin({ client, magpie } = {}, options = {}) {
|
|
|
3239
3286
|
// config sets what zcodeConfig has read, for tests: null is ZCode's config
|
|
3240
3287
|
// out of reach, undefined forgets it
|
|
3241
3288
|
const config = (cfg) => (cfgCache = cfg === undefined ? null : { cfg, at: Date.now() })
|
|
3242
|
-
export const _internal = { edge, EDGE_HOLD, busyHold, startGate, 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, queryOf, PROMPT, marked, keyDevice, sessionOf, busy, BUSY_WAITS, DEFAULT_CONCURRENCY, DEFAULT_ACCOUNTS }
|
|
3289
|
+
export const _internal = { edge, EDGE_HOLD, busyHold, startGate, NARROW_FOR, limitOf, 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, queryOf, 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.58",
|
|
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",
|