@magpie-community/opencode-zcode-auth 0.1.48 → 0.1.50
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 +23 -4
- package/index.mjs +68 -20
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -147,8 +147,12 @@ but an agent that always asks the plain model never sees the gift.
|
|
|
147
147
|
the plain model spends the account's own live gift first, and falls
|
|
148
148
|
back to the coding plan when the gift cannot serve it — a bucket that
|
|
149
149
|
is spent, a model the gift does not serve, no ZCode session, or the
|
|
150
|
-
minute's rest a refusal just earned.
|
|
151
|
-
|
|
150
|
+
minute's rest a refusal just earned. In magpie, the switch is in the
|
|
151
|
+
provider's editor: Providers › ZCode, **Free quota first** under Plugin
|
|
152
|
+
options, just above Advanced (since 0.1.49). It is on by default; off
|
|
153
|
+
keeps the plain models on the plan. The JSON below still works, and a
|
|
154
|
+
`freeFirst: false` there keeps it off whatever the switch says. Turn it
|
|
155
|
+
off for the whole plugin in magpie's Plugins › Options
|
|
152
156
|
|
|
153
157
|
```json
|
|
154
158
|
{ "freeFirst": false }
|
|
@@ -172,7 +176,12 @@ from the plan in the same request — any gift failure is, its own
|
|
|
172
176
|
waited-out busy cap included, and the replay waits out the plan's busy
|
|
173
177
|
cap as any plan request does — and a spent answer or a block page rests
|
|
174
178
|
the model a minute and drops the route's balance read, so the requests
|
|
175
|
-
behind it go straight to the plan.
|
|
179
|
+
behind it go straight to the plan. Only a spent answer holds the
|
|
180
|
+
model's `-Trial` entry too; a refusal for another reason (a 405/3012)
|
|
181
|
+
keeps free first off the gift for the minute but leaves the `-Trial`
|
|
182
|
+
entry asking ZCode, which answers it as it came. Before 0.1.49 the two
|
|
183
|
+
rests were one, and a refused free-first request made the next `-Trial`
|
|
184
|
+
request answer a local "quota exhausted" 429 without asking. A gift that cannot be asked at all —
|
|
176
185
|
the network, the host — is answered from the plan the same way, the
|
|
177
186
|
route read dropped for the next request, while a caller's own abort
|
|
178
187
|
stays an abort. The agent sees the plan's answer
|
|
@@ -285,7 +294,9 @@ and again, all day while the feed names no plan — one GET, nothing
|
|
|
285
294
|
posted: ZCode drops plans into the feed on its own schedule (a weekend
|
|
286
295
|
banner starting at 19:00 Beijing, say), and one that arrives
|
|
287
296
|
after the day's ask used to sit unclaimed past its short life. The
|
|
288
|
-
other answers stand for the day
|
|
297
|
+
other answers stand for the day — but an *ineligible* (code 1004, below)
|
|
298
|
+
is read again in half an hour, as an inactive is: a claimed plan leaves
|
|
299
|
+
the feed, so
|
|
289
300
|
asking again half an hour later would find nothing and flip the card
|
|
290
301
|
from "Checked in today" to "No check-in event now"; a captcha plan
|
|
291
302
|
re-asked makes ZCode demand its captcha again; a done plan re-asked
|
|
@@ -304,6 +315,14 @@ the body's code, not the HTTP status:
|
|
|
304
315
|
(11-03: seven accounts' ZCODE GLOBAL BUILD all answered 3007). Once
|
|
305
316
|
claimed there, the gift lands on the account and freeFirst spends it
|
|
306
317
|
like any other;
|
|
318
|
+
- code 1004 "…not eligible for this plan yet" (HTTP 400): *ineligible*,
|
|
319
|
+
ZCode is not offering this account the plan at that moment. It is read
|
|
320
|
+
again half an hour out, since a plan can be offered later in the day
|
|
321
|
+
(11-04: an account's GLOBAL BUILD banner appeared 28 minutes after it
|
|
322
|
+
signed in). When *every* account answers 1004, check `APP_VERSION`
|
|
323
|
+
first: six accounts all answered 1004 under 3.16.3's header, and four
|
|
324
|
+
of them claimed with code 0 under 3.17.2's, one night, with nothing
|
|
325
|
+
else changed;
|
|
307
326
|
- anything else, an HTTP error without a code, or a reply that isn't
|
|
308
327
|
JSON: *failed*, which magpie asks again later that day. It is never
|
|
309
328
|
taken as claimed, and a failed claim leaves the account's sign-in
|
package/index.mjs
CHANGED
|
@@ -23,6 +23,8 @@ import { isAbsolute, join } from "node:path"
|
|
|
23
23
|
import PROMPT from "./prompt.mjs"
|
|
24
24
|
|
|
25
25
|
const PROVIDER = "zcode"
|
|
26
|
+
// FREE_FIRST is magpie's switch for free first in the provider's editor
|
|
27
|
+
const FREE_FIRST = "free_first"
|
|
26
28
|
const ZCODE = "https://zcode.z.ai"
|
|
27
29
|
const ZAI_API = "https://api.z.ai"
|
|
28
30
|
const BIGMODEL_API = "https://bigmodel.cn" // BigModel's business API, where ZCode asks it
|
|
@@ -274,6 +276,17 @@ async function plansOf(s) {
|
|
|
274
276
|
// blocked remembers the models a gift said it has none left for: a key+jwt+
|
|
275
277
|
// model for a minute, so one turn that is refused does not ask again.
|
|
276
278
|
const blocked = new Map()
|
|
279
|
+
// sidelined remembers, the same way, the models whose gift turned free
|
|
280
|
+
// first away for another reason (a 405/3012, a block page): free first
|
|
281
|
+
// leaves them on the plan for the minute, but a -Trial entry, the user's
|
|
282
|
+
// own pick of the gift, still asks ZCode and gets its real answer. Once
|
|
283
|
+
// one rest served both, and a refusal under free first answered the
|
|
284
|
+
// Trial entry locally as a quota "exhausted" it never was (Can4ry_ on X).
|
|
285
|
+
const sidelined = new Map()
|
|
286
|
+
const rest = (map, k) => {
|
|
287
|
+
for (const [x, until] of map) if (Date.now() >= until) map.delete(x)
|
|
288
|
+
map.set(k, Date.now() + 60_000)
|
|
289
|
+
}
|
|
277
290
|
|
|
278
291
|
// giftServes: the gift's buckets name their models lowercased ("glm-5.3-
|
|
279
292
|
// flash"); what is asked for arrives as the plan lists it ("GLM-5.3-Flash").
|
|
@@ -281,6 +294,7 @@ const giftServes = (gift, m) => (gift.models ?? []).some((x) => x.toLowerCase()
|
|
|
281
294
|
const giftRested = (s, m) => Date.now() < (blocked.get(s.key + "\0" + s.jwt + "\0" + m.toLowerCase()) ?? 0)
|
|
282
295
|
const giftUsable = (s, gift, m) => !jwtExpired(s.jwt) && giftServes(gift, m) &&
|
|
283
296
|
Date.now() >= (blocked.get(s.key + "\0" + s.jwt + "\0" + m.toLowerCase()) ?? 0)
|
|
297
|
+
const giftSidelined = (s, m) => Date.now() < (sidelined.get(s.key + "\0" + s.jwt + "\0" + m.toLowerCase()) ?? 0)
|
|
284
298
|
|
|
285
299
|
// spentUp is the gift saying it has nothing left to spend: its own no-
|
|
286
300
|
// resource-package code 1113, its "exceed quota limit" 1005 — a spent gift
|
|
@@ -695,6 +709,12 @@ async function claimHint(s) {
|
|
|
695
709
|
// (seen 10-06, #30).
|
|
696
710
|
const CLAIMED_ALREADY = 1003
|
|
697
711
|
const CAPTCHA = 3007
|
|
712
|
+
// NOT_ELIGIBLE is /billing/claim's body code for a plan the account is not
|
|
713
|
+
// offered at that moment: HTTP 400 {"code":1004,…}. A stale APP_VERSION
|
|
714
|
+
// answers it for every account at once (11-04: six accounts all answered
|
|
715
|
+
// 1004 under 3.16.3's header, and four of them claimed with code 0 under
|
|
716
|
+
// 3.17.2's, one night, with nothing else changed).
|
|
717
|
+
const NOT_ELIGIBLE = 1004
|
|
698
718
|
const CAPTCHA_WORDS = /captcha/i
|
|
699
719
|
|
|
700
720
|
// The marketing feed and the claim are gated on the client's version:
|
|
@@ -729,6 +749,7 @@ async function claimOne(s, planID) {
|
|
|
729
749
|
const msg = typeof env?.msg === "string" ? env.msg : ""
|
|
730
750
|
if (code === CAPTCHA || CAPTCHA_WORDS.test(msg)) return { outcome: "captcha", why: msg || `code ${code}` }
|
|
731
751
|
if (code === CLAIMED_ALREADY) return { outcome: "done" }
|
|
752
|
+
if (code === NOT_ELIGIBLE) return { outcome: "ineligible", why: msg || `code ${code}` }
|
|
732
753
|
if (code === 0 && res.ok) return { outcome: "claimed" }
|
|
733
754
|
if (!Number.isFinite(code)) return { outcome: "failed", why: `HTTP ${res.status}, unreadable reply ${String(text ?? "").trim().slice(0, 120) || "(empty)"}` }
|
|
734
755
|
return { outcome: "failed", why: `${msg || "error"} (HTTP ${res.status}, code ${code})` }
|
|
@@ -833,7 +854,9 @@ const recheck = () => new Date(Date.now() + RECHECK_MS).toISOString()
|
|
|
833
854
|
// toClaim, the app's banners and its preview), as auth.checkin's answer:
|
|
834
855
|
// inactive when neither lists one, claimed when one was claimed now,
|
|
835
856
|
// done when each was claimed before, captcha when ZCode wants the app's
|
|
836
|
-
// captcha for one (it is claimed in the ZCode app),
|
|
857
|
+
// captcha for one (it is claimed in the ZCode app), ineligible when
|
|
858
|
+
// ZCode is not offering one at that moment (code 1004, read again later
|
|
859
|
+
// in the day), failed when a list
|
|
837
860
|
// or a claim can't be read. A failed one is asked again later in the
|
|
838
861
|
// day, when a plan claimed now answers 1003. An inactive stands only
|
|
839
862
|
// until RECHECK_MS from now, so a plan dropped into the feed later in
|
|
@@ -853,6 +876,7 @@ async function claimDaily(s) {
|
|
|
853
876
|
const said = [
|
|
854
877
|
claimed.length ? `claimed ${claimed.join(", ")}` : "",
|
|
855
878
|
...got.filter((g) => g.outcome === "captcha").map((g) => `${g.name}: claim it in the ZCode app (${g.why})`),
|
|
879
|
+
...got.filter((g) => g.outcome === "ineligible").map((g) => `${g.name}: ${g.why}`),
|
|
856
880
|
...got.filter((g) => g.outcome === "failed").map((g) => `${g.name}: ${g.why}`),
|
|
857
881
|
...unread,
|
|
858
882
|
].filter(Boolean).join("; ")
|
|
@@ -862,6 +886,10 @@ async function claimDaily(s) {
|
|
|
862
886
|
if (unread.length || got.some((g) => g.outcome === "failed")) return { outcome: "failed", message: said }
|
|
863
887
|
if (got.some((g) => g.outcome === "captcha")) return { outcome: "captcha", message: said }
|
|
864
888
|
if (claimed.length) return { outcome: "claimed", message: said }
|
|
889
|
+
// a plan not offered now can be offered later in the day (11-04: an
|
|
890
|
+
// account's GLOBAL BUILD banner showed 28 minutes after it signed in),
|
|
891
|
+
// so an ineligible is read again half an hour out, as an inactive is
|
|
892
|
+
if (got.some((g) => g.outcome === "ineligible")) return { outcome: "ineligible", message: said, until: recheck() }
|
|
865
893
|
return { outcome: "done", message: `already claimed: ${named("done").join(", ")}` }
|
|
866
894
|
}
|
|
867
895
|
|
|
@@ -2278,7 +2306,7 @@ function sessionOf(id, device) {
|
|
|
2278
2306
|
|
|
2279
2307
|
// ---- the plugin ------------------------------------------------------------------
|
|
2280
2308
|
|
|
2281
|
-
export async function ZCodeAuthPlugin({ client }, options = {}) {
|
|
2309
|
+
export async function ZCodeAuthPlugin({ client, magpie } = {}, options = {}) {
|
|
2282
2310
|
const log = (message) => {
|
|
2283
2311
|
try {
|
|
2284
2312
|
client?.app?.log?.({ body: { service: "zcode-auth", level: "error", message } })
|
|
@@ -2289,9 +2317,14 @@ export async function ZCodeAuthPlugin({ client }, options = {}) {
|
|
|
2289
2317
|
// provider carries no freeFirst of its own. It is on unless one of them
|
|
2290
2318
|
// says false: a gift ZCode hands out (each weekend, sgfvdsg86265 on X)
|
|
2291
2319
|
// lapses unused behind an agent that asks the plain model, and every
|
|
2292
|
-
// failure of the gift is answered from the plan.
|
|
2320
|
+
// failure of the gift is answered from the plan. magpie's switch in the
|
|
2321
|
+
// provider's editor (FREE_FIRST, auth.options below) turns it off too;
|
|
2322
|
+
// it is on by default, so a JSON freeFirst: false a user wrote before
|
|
2323
|
+
// the switch existed keeps it off. A key magpie doesn't report (an older
|
|
2324
|
+
// magpie, OpenCode) leaves it as the JSON says.
|
|
2293
2325
|
const freeFirst = (providerOptions) =>
|
|
2294
|
-
|
|
2326
|
+
magpie?.options?.(PROVIDER)?.[FREE_FIRST] !== false &&
|
|
2327
|
+
(providerOptions?.freeFirst !== undefined ? providerOptions.freeFirst !== false : options?.freeFirst !== false)
|
|
2295
2328
|
const oauth = (site) => ({
|
|
2296
2329
|
type: "oauth",
|
|
2297
2330
|
label: `ZCode: ${SITES[site].name}${site === "bigmodel" ? " (智谱)" : ""} GLM Coding Plan`,
|
|
@@ -2374,6 +2407,23 @@ export async function ZCodeAuthPlugin({ client }, options = {}) {
|
|
|
2374
2407
|
// how many accounts take requests at once, the others in reserve
|
|
2375
2408
|
// (DEFAULT_ACCOUNTS); a magpie that doesn't read it uses them all
|
|
2376
2409
|
maxAccounts: DEFAULT_ACCOUNTS,
|
|
2410
|
+
// magpie's own switches (OpenCode never reads auth.options), read at
|
|
2411
|
+
// each request through input.magpie
|
|
2412
|
+
options: [
|
|
2413
|
+
{
|
|
2414
|
+
key: FREE_FIRST,
|
|
2415
|
+
label: "Free quota first",
|
|
2416
|
+
description: "When on, the plain GLM models spend ZCode's free Start Plan quota (the -Trial entries' quota) before the GLM Coding Plan, and go to the plan when it is spent or refused. Off: only the -Trial entries use it. freeFirst: false in the options JSON turns it off too.",
|
|
2417
|
+
labels: { zh: "优先用免费额度", "zh-TW": "優先用免費額度", ja: "無料枠を優先", de: "Gratis-Kontingent zuerst" },
|
|
2418
|
+
descriptions: {
|
|
2419
|
+
zh: "开启后,普通的 GLM 模型先用 ZCode 的免费 Start Plan 额度(即 -Trial 条目的额度),用完或被拒时再用 GLM Coding Plan。关闭后只有 -Trial 条目会用它。选项 JSON 里的 freeFirst: false 也会关闭它。",
|
|
2420
|
+
"zh-TW": "開啟後,一般的 GLM 模型先用 ZCode 的免費 Start Plan 額度(即 -Trial 項目的額度),用完或被拒時再用 GLM Coding Plan。關閉後只有 -Trial 項目會用它。選項 JSON 裡的 freeFirst: false 也會關閉它。",
|
|
2421
|
+
ja: "オンにすると、通常の GLM モデルは GLM Coding Plan より先に ZCode の無料 Start Plan 枠(-Trial 項目の枠)を使い、使い切るか拒否されるとプランに戻ります。オフでは -Trial 項目だけが使います。オプション JSON の freeFirst: false でもオフになります。",
|
|
2422
|
+
de: "Ist dies an, nutzen die normalen GLM-Modelle zuerst ZCodes freies Start-Plan-Kontingent (das der -Trial-Einträge) und erst danach den GLM Coding Plan. Aus: nur die -Trial-Einträge nutzen es. freeFirst: false im Optionen-JSON schaltet es auch aus.",
|
|
2423
|
+
},
|
|
2424
|
+
default: true,
|
|
2425
|
+
},
|
|
2426
|
+
],
|
|
2377
2427
|
async loader(getAuth, provider) {
|
|
2378
2428
|
const s0 = stateOf(await getAuth())
|
|
2379
2429
|
if (!s0) return {}
|
|
@@ -2421,13 +2471,13 @@ export async function ZCodeAuthPlugin({ client }, options = {}) {
|
|
|
2421
2471
|
const raw = giftModel(gift, m)
|
|
2422
2472
|
const available = raw && giftUsable(s, gift, raw)
|
|
2423
2473
|
if (!available) {
|
|
2424
|
-
const error = { type: raw ? "rate_limit_error" : "invalid_request_error", message: raw ?
|
|
2474
|
+
const error = { type: raw ? "rate_limit_error" : "invalid_request_error", message: raw ? `${giftRested(s, raw) ? `ZCode said the last request for ${raw} was over its quota, so it is not asked again for a minute: ` : ""}ZCode's limited quota for ${raw} is exhausted or temporarily unavailable` : `ZCode has no active limited quota for ${m}` }
|
|
2425
2475
|
return kept(new Response(JSON.stringify({ type: "error", error }), { status: raw ? 429 : 400, headers: { "content-type": "application/json" } }))
|
|
2426
2476
|
}
|
|
2427
2477
|
orig = opts.body = JSON.stringify({ ...JSON.parse(orig), model: wireModel(raw) })
|
|
2428
2478
|
m = raw
|
|
2429
2479
|
forcedGift = true
|
|
2430
|
-
} else if (!alone && !edgeHeld() && freeFirst(provider?.options) && s.jwt && known && giftUsable(s, known, m)) {
|
|
2480
|
+
} else if (!alone && !edgeHeld() && freeFirst(provider?.options) && s.jwt && known && giftUsable(s, known, m) && !giftSidelined(s, m)) {
|
|
2431
2481
|
// free first: the plain model spends the account's own live
|
|
2432
2482
|
// gift before it spends the coding plan, under the name the
|
|
2433
2483
|
// gift lists. The gift is the route's cache alone (a failed
|
|
@@ -2455,13 +2505,14 @@ export async function ZCodeAuthPlugin({ client }, options = {}) {
|
|
|
2455
2505
|
const askedUrl = url
|
|
2456
2506
|
const place = workPlace(h)
|
|
2457
2507
|
const sid = sessionOf(h.get("x-session-id"), s.device)
|
|
2458
|
-
// restModel is
|
|
2459
|
-
// model rests a minute
|
|
2460
|
-
//
|
|
2461
|
-
//
|
|
2462
|
-
|
|
2463
|
-
|
|
2464
|
-
|
|
2508
|
+
// restModel is free first's gift turning this model away: the
|
|
2509
|
+
// model rests a minute and the route is dropped, so the requests
|
|
2510
|
+
// behind it re-read the balance and go straight to the plan. A
|
|
2511
|
+
// quota answer rests it in blocked, which a -Trial entry obeys
|
|
2512
|
+
// too; any other refusal (spent false) in sidelined, which only
|
|
2513
|
+
// free first reads.
|
|
2514
|
+
const restModel = (spent) => {
|
|
2515
|
+
rest(spent ? blocked : sidelined, s.key + "\0" + s.jwt + "\0" + asked.toLowerCase())
|
|
2465
2516
|
routes.delete(s.key + "\0" + s.jwt)
|
|
2466
2517
|
}
|
|
2467
2518
|
// replayOnPlan is free first's answer to a gift that refused
|
|
@@ -2552,7 +2603,7 @@ export async function ZCodeAuthPlugin({ client }, options = {}) {
|
|
|
2552
2603
|
// a block page holds every request off zcode.z.ai a while
|
|
2553
2604
|
if (EDGE_PAGE.test(text)) edge.until = Date.now() + EDGE_HOLD
|
|
2554
2605
|
if (freeGift) {
|
|
2555
|
-
restModel()
|
|
2606
|
+
restModel(false)
|
|
2556
2607
|
return done(kept(await replayOnPlan()))
|
|
2557
2608
|
}
|
|
2558
2609
|
if (s.key && !forcedGift) routes.delete(s.key + "\0" + s.jwt)
|
|
@@ -2580,10 +2631,7 @@ export async function ZCodeAuthPlugin({ client }, options = {}) {
|
|
|
2580
2631
|
const text = await res.text()
|
|
2581
2632
|
const spent = spentUp(text, res.status)
|
|
2582
2633
|
if (!spent && res.status < 400) return done(kept(res, text))
|
|
2583
|
-
if (spent)
|
|
2584
|
-
for (const [k, until] of blocked) if (Date.now() >= until) blocked.delete(k)
|
|
2585
|
-
blocked.set(s.key + "\0" + s.jwt + "\0" + modelOf(orig).toLowerCase(), Date.now() + 60_000)
|
|
2586
|
-
}
|
|
2634
|
+
if (spent) rest(blocked, s.key + "\0" + s.jwt + "\0" + modelOf(orig).toLowerCase())
|
|
2587
2635
|
return spent && res.status < 400 ? kept(new Response(text, { status: 429, headers: res.headers })) : done(kept(res, text))
|
|
2588
2636
|
}
|
|
2589
2637
|
}
|
|
@@ -2603,7 +2651,7 @@ export async function ZCodeAuthPlugin({ client }, options = {}) {
|
|
|
2603
2651
|
const cap = busy(res.status, text)
|
|
2604
2652
|
const spent = !cap && spentUp(text, res.status)
|
|
2605
2653
|
if (spent) {
|
|
2606
|
-
restModel()
|
|
2654
|
+
restModel(true)
|
|
2607
2655
|
return done(kept(await replayOnPlan()))
|
|
2608
2656
|
}
|
|
2609
2657
|
if (res.status >= 400) {
|
|
@@ -2736,4 +2784,4 @@ export async function ZCodeAuthPlugin({ client }, options = {}) {
|
|
|
2736
2784
|
// config sets what zcodeConfig has read, for tests: null is ZCode's config
|
|
2737
2785
|
// out of reach, undefined forgets it
|
|
2738
2786
|
const config = (cfg) => (cfgCache = cfg === undefined ? null : { cfg, at: Date.now() })
|
|
2739
|
-
export const _internal = { edge, EDGE_HOLD, config, entry, limitWindows, termOf, startUsage, routes, blocked, plansOf, giftOf, dualUsage, claimHint, claimDaily, spentUp, giftServes, modelOf, teamKeys, ownSignIn, stateOf, located, dress, PROMPT, marked, keyDevice, sessionOf, busy, BUSY_WAITS, DEFAULT_CONCURRENCY, DEFAULT_ACCOUNTS }
|
|
2787
|
+
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 }
|
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.50",
|
|
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",
|