@magpie-community/opencode-zcode-auth 0.1.48 → 0.1.49
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 +12 -3
- package/index.mjs +53 -19
- 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
|
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
|
|
@@ -2278,7 +2292,7 @@ function sessionOf(id, device) {
|
|
|
2278
2292
|
|
|
2279
2293
|
// ---- the plugin ------------------------------------------------------------------
|
|
2280
2294
|
|
|
2281
|
-
export async function ZCodeAuthPlugin({ client }, options = {}) {
|
|
2295
|
+
export async function ZCodeAuthPlugin({ client, magpie } = {}, options = {}) {
|
|
2282
2296
|
const log = (message) => {
|
|
2283
2297
|
try {
|
|
2284
2298
|
client?.app?.log?.({ body: { service: "zcode-auth", level: "error", message } })
|
|
@@ -2289,9 +2303,14 @@ export async function ZCodeAuthPlugin({ client }, options = {}) {
|
|
|
2289
2303
|
// provider carries no freeFirst of its own. It is on unless one of them
|
|
2290
2304
|
// says false: a gift ZCode hands out (each weekend, sgfvdsg86265 on X)
|
|
2291
2305
|
// lapses unused behind an agent that asks the plain model, and every
|
|
2292
|
-
// failure of the gift is answered from the plan.
|
|
2306
|
+
// failure of the gift is answered from the plan. magpie's switch in the
|
|
2307
|
+
// provider's editor (FREE_FIRST, auth.options below) turns it off too;
|
|
2308
|
+
// it is on by default, so a JSON freeFirst: false a user wrote before
|
|
2309
|
+
// the switch existed keeps it off. A key magpie doesn't report (an older
|
|
2310
|
+
// magpie, OpenCode) leaves it as the JSON says.
|
|
2293
2311
|
const freeFirst = (providerOptions) =>
|
|
2294
|
-
|
|
2312
|
+
magpie?.options?.(PROVIDER)?.[FREE_FIRST] !== false &&
|
|
2313
|
+
(providerOptions?.freeFirst !== undefined ? providerOptions.freeFirst !== false : options?.freeFirst !== false)
|
|
2295
2314
|
const oauth = (site) => ({
|
|
2296
2315
|
type: "oauth",
|
|
2297
2316
|
label: `ZCode: ${SITES[site].name}${site === "bigmodel" ? " (智谱)" : ""} GLM Coding Plan`,
|
|
@@ -2374,6 +2393,23 @@ export async function ZCodeAuthPlugin({ client }, options = {}) {
|
|
|
2374
2393
|
// how many accounts take requests at once, the others in reserve
|
|
2375
2394
|
// (DEFAULT_ACCOUNTS); a magpie that doesn't read it uses them all
|
|
2376
2395
|
maxAccounts: DEFAULT_ACCOUNTS,
|
|
2396
|
+
// magpie's own switches (OpenCode never reads auth.options), read at
|
|
2397
|
+
// each request through input.magpie
|
|
2398
|
+
options: [
|
|
2399
|
+
{
|
|
2400
|
+
key: FREE_FIRST,
|
|
2401
|
+
label: "Free quota first",
|
|
2402
|
+
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.",
|
|
2403
|
+
labels: { zh: "优先用免费额度", "zh-TW": "優先用免費額度", ja: "無料枠を優先", de: "Gratis-Kontingent zuerst" },
|
|
2404
|
+
descriptions: {
|
|
2405
|
+
zh: "开启后,普通的 GLM 模型先用 ZCode 的免费 Start Plan 额度(即 -Trial 条目的额度),用完或被拒时再用 GLM Coding Plan。关闭后只有 -Trial 条目会用它。选项 JSON 里的 freeFirst: false 也会关闭它。",
|
|
2406
|
+
"zh-TW": "開啟後,一般的 GLM 模型先用 ZCode 的免費 Start Plan 額度(即 -Trial 項目的額度),用完或被拒時再用 GLM Coding Plan。關閉後只有 -Trial 項目會用它。選項 JSON 裡的 freeFirst: false 也會關閉它。",
|
|
2407
|
+
ja: "オンにすると、通常の GLM モデルは GLM Coding Plan より先に ZCode の無料 Start Plan 枠(-Trial 項目の枠)を使い、使い切るか拒否されるとプランに戻ります。オフでは -Trial 項目だけが使います。オプション JSON の freeFirst: false でもオフになります。",
|
|
2408
|
+
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.",
|
|
2409
|
+
},
|
|
2410
|
+
default: true,
|
|
2411
|
+
},
|
|
2412
|
+
],
|
|
2377
2413
|
async loader(getAuth, provider) {
|
|
2378
2414
|
const s0 = stateOf(await getAuth())
|
|
2379
2415
|
if (!s0) return {}
|
|
@@ -2421,13 +2457,13 @@ export async function ZCodeAuthPlugin({ client }, options = {}) {
|
|
|
2421
2457
|
const raw = giftModel(gift, m)
|
|
2422
2458
|
const available = raw && giftUsable(s, gift, raw)
|
|
2423
2459
|
if (!available) {
|
|
2424
|
-
const error = { type: raw ? "rate_limit_error" : "invalid_request_error", message: raw ?
|
|
2460
|
+
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
2461
|
return kept(new Response(JSON.stringify({ type: "error", error }), { status: raw ? 429 : 400, headers: { "content-type": "application/json" } }))
|
|
2426
2462
|
}
|
|
2427
2463
|
orig = opts.body = JSON.stringify({ ...JSON.parse(orig), model: wireModel(raw) })
|
|
2428
2464
|
m = raw
|
|
2429
2465
|
forcedGift = true
|
|
2430
|
-
} else if (!alone && !edgeHeld() && freeFirst(provider?.options) && s.jwt && known && giftUsable(s, known, m)) {
|
|
2466
|
+
} else if (!alone && !edgeHeld() && freeFirst(provider?.options) && s.jwt && known && giftUsable(s, known, m) && !giftSidelined(s, m)) {
|
|
2431
2467
|
// free first: the plain model spends the account's own live
|
|
2432
2468
|
// gift before it spends the coding plan, under the name the
|
|
2433
2469
|
// gift lists. The gift is the route's cache alone (a failed
|
|
@@ -2455,13 +2491,14 @@ export async function ZCodeAuthPlugin({ client }, options = {}) {
|
|
|
2455
2491
|
const askedUrl = url
|
|
2456
2492
|
const place = workPlace(h)
|
|
2457
2493
|
const sid = sessionOf(h.get("x-session-id"), s.device)
|
|
2458
|
-
// restModel is
|
|
2459
|
-
// model rests a minute
|
|
2460
|
-
//
|
|
2461
|
-
//
|
|
2462
|
-
|
|
2463
|
-
|
|
2464
|
-
|
|
2494
|
+
// restModel is free first's gift turning this model away: the
|
|
2495
|
+
// model rests a minute and the route is dropped, so the requests
|
|
2496
|
+
// behind it re-read the balance and go straight to the plan. A
|
|
2497
|
+
// quota answer rests it in blocked, which a -Trial entry obeys
|
|
2498
|
+
// too; any other refusal (spent false) in sidelined, which only
|
|
2499
|
+
// free first reads.
|
|
2500
|
+
const restModel = (spent) => {
|
|
2501
|
+
rest(spent ? blocked : sidelined, s.key + "\0" + s.jwt + "\0" + asked.toLowerCase())
|
|
2465
2502
|
routes.delete(s.key + "\0" + s.jwt)
|
|
2466
2503
|
}
|
|
2467
2504
|
// replayOnPlan is free first's answer to a gift that refused
|
|
@@ -2552,7 +2589,7 @@ export async function ZCodeAuthPlugin({ client }, options = {}) {
|
|
|
2552
2589
|
// a block page holds every request off zcode.z.ai a while
|
|
2553
2590
|
if (EDGE_PAGE.test(text)) edge.until = Date.now() + EDGE_HOLD
|
|
2554
2591
|
if (freeGift) {
|
|
2555
|
-
restModel()
|
|
2592
|
+
restModel(false)
|
|
2556
2593
|
return done(kept(await replayOnPlan()))
|
|
2557
2594
|
}
|
|
2558
2595
|
if (s.key && !forcedGift) routes.delete(s.key + "\0" + s.jwt)
|
|
@@ -2580,10 +2617,7 @@ export async function ZCodeAuthPlugin({ client }, options = {}) {
|
|
|
2580
2617
|
const text = await res.text()
|
|
2581
2618
|
const spent = spentUp(text, res.status)
|
|
2582
2619
|
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
|
-
}
|
|
2620
|
+
if (spent) rest(blocked, s.key + "\0" + s.jwt + "\0" + modelOf(orig).toLowerCase())
|
|
2587
2621
|
return spent && res.status < 400 ? kept(new Response(text, { status: 429, headers: res.headers })) : done(kept(res, text))
|
|
2588
2622
|
}
|
|
2589
2623
|
}
|
|
@@ -2603,7 +2637,7 @@ export async function ZCodeAuthPlugin({ client }, options = {}) {
|
|
|
2603
2637
|
const cap = busy(res.status, text)
|
|
2604
2638
|
const spent = !cap && spentUp(text, res.status)
|
|
2605
2639
|
if (spent) {
|
|
2606
|
-
restModel()
|
|
2640
|
+
restModel(true)
|
|
2607
2641
|
return done(kept(await replayOnPlan()))
|
|
2608
2642
|
}
|
|
2609
2643
|
if (res.status >= 400) {
|
|
@@ -2736,4 +2770,4 @@ export async function ZCodeAuthPlugin({ client }, options = {}) {
|
|
|
2736
2770
|
// config sets what zcodeConfig has read, for tests: null is ZCode's config
|
|
2737
2771
|
// out of reach, undefined forgets it
|
|
2738
2772
|
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 }
|
|
2773
|
+
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.49",
|
|
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",
|