@magpie-community/opencode-zcode-auth 0.1.61 → 0.1.64
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 +13 -0
- package/index.mjs +58 -12
- package/opencode2.mjs +25 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -275,6 +275,19 @@ the slots. An account whose slots are taken elsewhere is narrowed to one
|
|
|
275
275
|
within a few refusals and then refused alone, so the firewall hold still
|
|
276
276
|
comes before the 405.
|
|
277
277
|
|
|
278
|
+
Since 0.1.64 the request that brought about such a narrowing is not
|
|
279
|
+
failed either. Up to 0.1.63 it went back to magpie as a 429 after its
|
|
280
|
+
re-asks, so two sessions on an account served one at a time showed a 429
|
|
281
|
+
every time the narrowing ran out (JJLIN070414 on X), and with one account
|
|
282
|
+
the agent was told it. Now it gives its slot back at once, without the 2s
|
|
283
|
+
re-ask, and waits at the head of the queue until one of the account's
|
|
284
|
+
other requests has ended, then is sent again, as ZCode's own client
|
|
285
|
+
(its dynamic-workflow governor) cuts its cap and queues a capped request
|
|
286
|
+
rather than failing it. The waits share the gate's two minutes, after
|
|
287
|
+
which it is answered here with the queue's 429. A request refused alone,
|
|
288
|
+
or free first's gift (which goes to the coding plan), is answered as
|
|
289
|
+
before.
|
|
290
|
+
|
|
278
291
|
## Two accounts at once
|
|
279
292
|
|
|
280
293
|
Three ZCode accounts used at once from one address can get the address
|
package/index.mjs
CHANGED
|
@@ -1804,7 +1804,11 @@ function ownSignIn() {
|
|
|
1804
1804
|
else s = { ...s, jwt, base: s.base || ZAI_BASE, site: s.base === BIGMODEL_BASE ? "bigmodel" : "zai" }
|
|
1805
1805
|
if (!s.key && !s.jwt && !isTeam(s)) return null
|
|
1806
1806
|
let info = {}
|
|
1807
|
-
|
|
1807
|
+
// The JWT belongs to the app's active sign-in. Cached profiles from
|
|
1808
|
+
// another provider must not name it, and 3.18's China sign-in is zai_cn.
|
|
1809
|
+
const active = decrypt(key, store["oauth:active_provider"])
|
|
1810
|
+
const families = [...new Set([active, "zai", "zai_cn", "bigmodel"].filter((x) => ["zai", "zai_cn", "bigmodel"].includes(x)))]
|
|
1811
|
+
for (const name of families.map((x) => `oauth:${x}:user_info`)) {
|
|
1808
1812
|
const v = decrypt(key, store[name])
|
|
1809
1813
|
if (v && !first(info.email, info.name, info.user_id)) {
|
|
1810
1814
|
try {
|
|
@@ -2040,7 +2044,11 @@ function kept(res, text) {
|
|
|
2040
2044
|
// this computer through this one process, so the plugin does what that
|
|
2041
2045
|
// client does: the Start Plan gets a few requests of an account in flight at
|
|
2042
2046
|
// once (startGate), the rest wait their turn here instead of being sent to be
|
|
2043
|
-
// refused, and a refusal anyway is asked again twice (BUSY_WAITS).
|
|
2047
|
+
// refused, and a refusal anyway is asked again twice (BUSY_WAITS). A refusal
|
|
2048
|
+
// of a request sent beside others of the same account lowers that account's
|
|
2049
|
+
// gate and waits its turn again (askBusy's requeue), as ZCode's own
|
|
2050
|
+
// dynamic-workflow governor cuts its cap and queues a capped subagent
|
|
2051
|
+
// request rather than failing it.
|
|
2044
2052
|
//
|
|
2045
2053
|
// The coding plan keeps no gate of the plugin's: its cap depends on its tier
|
|
2046
2054
|
// (Z.ai serves Max more at once than Pro, and Lite fewer) and is higher
|
|
@@ -2064,6 +2072,10 @@ const BUSY_CODES = new Set(["3008", "3009", "3010"])
|
|
|
2064
2072
|
// requests a capped account could serve on the wire, and the agents' own
|
|
2065
2073
|
// retries came on top.
|
|
2066
2074
|
const BUSY_WAITS = [1000, 2000]
|
|
2075
|
+
// REQUEUE_MAX bounds how often one request is handed back to the gate by a
|
|
2076
|
+
// narrowing (askBusy): each narrowing lowers the gate, three to one at most
|
|
2077
|
+
// takes two, so more is a fault of the count, not ZCode.
|
|
2078
|
+
const REQUEUE_MAX = 4
|
|
2067
2079
|
// The hint names neither "plan" nor "limit": after the vendor's "…limit
|
|
2068
2080
|
// exceeded" either would read to magpie as a plan used up.
|
|
2069
2081
|
const BUSY_HINT =
|
|
@@ -2125,8 +2137,10 @@ const startFull = (key) => {
|
|
|
2125
2137
|
}
|
|
2126
2138
|
|
|
2127
2139
|
// startSlot waits for one of the account's Start Plan slots: { release },
|
|
2128
|
-
// null when startGate.wait ran out first, or the
|
|
2129
|
-
|
|
2140
|
+
// null when the wait (startGate.wait unless given) ran out first, or the
|
|
2141
|
+
// agent's abort thrown. A request handed back by a narrowing (requeued) was
|
|
2142
|
+
// first in line already, so it waits at the head of the queue.
|
|
2143
|
+
function startSlot(key, signal, { wait = startGate.wait, front = false } = {}) {
|
|
2130
2144
|
const lanes = startGate.lanes
|
|
2131
2145
|
let l = lanes.get(key)
|
|
2132
2146
|
if (!l) lanes.set(key, (l = { busy: 0, queue: [] }))
|
|
@@ -2168,9 +2182,10 @@ function startSlot(key, signal) {
|
|
|
2168
2182
|
leave()
|
|
2169
2183
|
next()
|
|
2170
2184
|
resolve(null)
|
|
2171
|
-
},
|
|
2185
|
+
}, Math.max(0, wait))
|
|
2172
2186
|
signal?.addEventListener?.("abort", aborted, { once: true })
|
|
2173
|
-
l.queue.
|
|
2187
|
+
if (front) l.queue.unshift(go)
|
|
2188
|
+
else l.queue.push(go)
|
|
2174
2189
|
})
|
|
2175
2190
|
}
|
|
2176
2191
|
|
|
@@ -2266,10 +2281,12 @@ const busyHeld = () => busyHold.now() < busyHold.until
|
|
|
2266
2281
|
// busyCap records one sustained concurrency cap of a Start Plan request.
|
|
2267
2282
|
// Only a run arms a hold; the window is cleared on arming, and a cap that
|
|
2268
2283
|
// lands while the hold is on counts toward nothing — the hold's own 429s and
|
|
2269
|
-
// the first caps after it cannot keep re-arming it.
|
|
2284
|
+
// the first caps after it cannot keep re-arming it. It says true when the
|
|
2285
|
+
// cap narrowed the account instead: the request is then requeued, not
|
|
2286
|
+
// failed (see askBusy).
|
|
2270
2287
|
function busyCap(key) {
|
|
2271
|
-
if (key != null && narrowed(key)) return
|
|
2272
|
-
if (busyHeld()) return
|
|
2288
|
+
if (key != null && narrowed(key)) return true
|
|
2289
|
+
if (busyHeld()) return false
|
|
2273
2290
|
const now = busyHold.now()
|
|
2274
2291
|
busyHold.hits.push(now)
|
|
2275
2292
|
while (busyHold.hits.length && now - busyHold.hits[0] > BUSY_WINDOW) busyHold.hits.shift()
|
|
@@ -2277,6 +2294,7 @@ function busyCap(key) {
|
|
|
2277
2294
|
busyHold.until = now + BUSY_HOLD
|
|
2278
2295
|
busyHold.hits.length = 0
|
|
2279
2296
|
}
|
|
2297
|
+
return false
|
|
2280
2298
|
}
|
|
2281
2299
|
|
|
2282
2300
|
// BUSY_HOLD_MARK marks an answer the hold made up: a request the gate or the
|
|
@@ -2345,8 +2363,13 @@ function busyAnswer(res, text) {
|
|
|
2345
2363
|
// 1s and then 2s, as ZCode's own client does. Past them the cap goes back as
|
|
2346
2364
|
// busyAnswer's 429. A request refused again at its 1s re-ask is a sustained
|
|
2347
2365
|
// cap, told to onCap once (the firewall hold's count); one the re-ask got
|
|
2348
|
-
// past is not.
|
|
2349
|
-
//
|
|
2366
|
+
// past is not. When onCap says the cap narrowed the account (others of this
|
|
2367
|
+
// computer were in flight on it), asking again in 2s would only be refused
|
|
2368
|
+
// beside them again: the busyAnswer goes back at once marked `requeue`, and
|
|
2369
|
+
// the Start Plan path waits for one of those others to end and sends it
|
|
2370
|
+
// again (JJLIN070414 on X: two sessions on one account, each pair's second
|
|
2371
|
+
// request failed 429 where ZCode's own client queues it). The plan path's
|
|
2372
|
+
// patience, shared with free first's replay, whose plan has a cap of its own.
|
|
2350
2373
|
async function askBusy(u, o, hs, onCap) {
|
|
2351
2374
|
let res = await fetch(u, { ...o, headers: hs })
|
|
2352
2375
|
let caps = 0
|
|
@@ -2354,7 +2377,11 @@ async function askBusy(u, o, hs, onCap) {
|
|
|
2354
2377
|
const text = await res.text()
|
|
2355
2378
|
const cap = busy(429, text)
|
|
2356
2379
|
// one request's refusals are one saturation signal, not one per ask
|
|
2357
|
-
if (cap && ++caps === 2
|
|
2380
|
+
if (cap && ++caps === 2 && onCap?.() === true) {
|
|
2381
|
+
res = busyAnswer(res, text)
|
|
2382
|
+
res.requeue = true
|
|
2383
|
+
break
|
|
2384
|
+
}
|
|
2358
2385
|
// the hold armed: a request already in flight stops asking too. The
|
|
2359
2386
|
// second check catches the asks that capped before the hold armed and
|
|
2360
2387
|
// woke from their wait after it. Keying on onCap keeps the coding plan
|
|
@@ -3246,6 +3273,25 @@ export async function ZCodeAuthPlugin({ client, magpie } = {}, options = {}) {
|
|
|
3246
3273
|
// coding plan is served by another host and was never refused
|
|
3247
3274
|
// with the IP's 405.
|
|
3248
3275
|
res = await askBusy(url, opts, h, start ? () => busyCap(gateKey) : undefined)
|
|
3276
|
+
// a cap this computer brought on itself, with others of the
|
|
3277
|
+
// account in flight beside it, narrowed the account's gate:
|
|
3278
|
+
// the request gives its slot back and waits, first in line,
|
|
3279
|
+
// until one of those others has ended, then is sent again —
|
|
3280
|
+
// queued, as ZCode's own client queues its subagents' caps,
|
|
3281
|
+
// not failed. Free first's gift goes to the plan instead,
|
|
3282
|
+
// which serves it now. The waits share one startGate.wait;
|
|
3283
|
+
// each turn the gate is lower or the request is alone, where
|
|
3284
|
+
// a cap is ZCode's or another client's and goes back as it
|
|
3285
|
+
// did.
|
|
3286
|
+
const until = Date.now() + startGate.wait
|
|
3287
|
+
for (let turn = 0; res.requeue && !freeGift && turn < REQUEUE_MAX; turn++) {
|
|
3288
|
+
own.slot?.release()
|
|
3289
|
+
own.slot = await startSlot(gateKey, opts.signal, { wait: until - Date.now(), front: true })
|
|
3290
|
+
if (!own.slot) return kept(startQueueAnswer(gateKey))
|
|
3291
|
+
if (edgeHeld()) return kept(edgeHeldAnswer())
|
|
3292
|
+
if (busyHeld()) return kept(busyHeldAnswer())
|
|
3293
|
+
res = await askBusy(url, opts, h, () => busyCap(gateKey))
|
|
3294
|
+
}
|
|
3249
3295
|
} catch (e) {
|
|
3250
3296
|
// free first moved the request onto the gift, so a gift that
|
|
3251
3297
|
// cannot be asked at all — the network, the host — is answered
|
package/opencode2.mjs
CHANGED
|
@@ -575,7 +575,31 @@ async function setupPlugin(ctx, fn, live) {
|
|
|
575
575
|
}
|
|
576
576
|
} catch {}
|
|
577
577
|
})()
|
|
578
|
-
|
|
578
|
+
// A plugin still reading its list in the background when the first read
|
|
579
|
+
// returned (agy-cli: `agy models` and a session probe take seconds, and
|
|
580
|
+
// the first read gets its static list) says so through
|
|
581
|
+
// provider.warming(): the list is read once more when that ends, once,
|
|
582
|
+
// and no later than a minute on.
|
|
583
|
+
const warmed = async () => {
|
|
584
|
+
let w
|
|
585
|
+
try {
|
|
586
|
+
w = typeof hooks.provider?.warming === "function" ? hooks.provider.warming() : null
|
|
587
|
+
} catch {
|
|
588
|
+
return
|
|
589
|
+
}
|
|
590
|
+
if (!w || typeof w.then !== "function") return
|
|
591
|
+
let timer
|
|
592
|
+
await Promise.race([
|
|
593
|
+
Promise.resolve(w).catch(() => {}),
|
|
594
|
+
new Promise((r) => {
|
|
595
|
+
timer = setTimeout(r, 60_000)
|
|
596
|
+
timer.unref?.()
|
|
597
|
+
}),
|
|
598
|
+
])
|
|
599
|
+
clearTimeout(timer)
|
|
600
|
+
if (live.on) await refresh()
|
|
601
|
+
}
|
|
602
|
+
void refresh().then(warmed, () => {})
|
|
579
603
|
}
|
|
580
604
|
|
|
581
605
|
export async function setup(ctx, mod = v1) {
|
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.64",
|
|
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",
|