@magpie-community/opencode-zcode-auth 0.1.45 → 0.1.47

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.
Files changed (3) hide show
  1. package/README.md +2 -1
  2. package/index.mjs +37 -16
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -239,7 +239,8 @@ names it in a line set aside, "ZCode gift plan not started yet", with
239
239
  the plan's name and its start in this computer's time as the display
240
240
  ("Start Plan · 2026-10-29 23:00"), soonest first. An account whose only
241
241
  gift plan starts later gets a card with that line, not "ended or never
242
- started". The status ZCode gives such a plan has not been seen, so any
242
+ started", and signs in with it too, as ZCode counts such a plan as the
243
+ account's (its "pending" Start Plan). The status ZCode gives such a plan has not been seen, so any
243
244
  status but `expired` is taken; `starts_at` decides it.
244
245
 
245
246
  **Resets.** A GLM Coding Plan of the account's own, and a team seat,
package/index.mjs CHANGED
@@ -29,7 +29,11 @@ const BIGMODEL_API = "https://bigmodel.cn" // BigModel's business API, where ZCo
29
29
  const ZAI_BASE = "https://api.z.ai/api/anthropic"
30
30
  const BIGMODEL_BASE = "https://open.bigmodel.cn/api/anthropic"
31
31
  const START_BASE = ZCODE + "/api/v1/zcode-plan/anthropic"
32
- const APP_VERSION = "3.16.3" // the ZCode whose sign-in this makes
32
+ // APP_VERSION is the ZCode this signs in and asks as, on every call. ZCode
33
+ // gates by it: its marketing feed answers empty to an older one (#678),
34
+ // and its Start Plan balance "judges capability by the real app_version"
35
+ // (zaiStartPlanBilling.ts in zai-org/ZCode@a9fe401, 3.17.2).
36
+ const APP_VERSION = "3.17.2"
33
37
  const UA = "ZCode/" + APP_VERSION
34
38
 
35
39
  // ZCode's models before its config is read; the Start Plan has all but GLM-5.3.
@@ -148,6 +152,11 @@ const uniqueModels = (names) => {
148
152
  return [...byName.values()]
149
153
  }
150
154
  const giftNames = (g) => g?.allModels ?? g?.models ?? []
155
+ // wireModel is a gift model as ZCode sends it: the buckets name it lowercased
156
+ // ("glm-5.3-flash"), the app's request carries its registry id
157
+ // ("GLM-5.3-Flash"; normalizeOfficialGlmModelId's spelling). Names ZCode
158
+ // does not know go as the bucket wrote them.
159
+ const wireModel = (m) => MODELS.find((x) => x.id.toLowerCase() === String(m).toLowerCase())?.id ?? m
151
160
  const giftModel = (g, id) => giftNames(g).find((m) => giftID(m).toLowerCase() === id.toLowerCase())
152
161
  const bucketModels = (x) => (Array.isArray(x.capabilities) ? x.capabilities : [])
153
162
  .map((c) => String(c ?? "").trim()).filter((c) => c.startsWith("model:") || !c.includes(":"))
@@ -167,7 +176,12 @@ const giftSpent = (x) => {
167
176
  // the gift when any of them has a live bucket serving it — a bucket past
168
177
  // its plan's own end, or one whose remaining_units says none, is skipped
169
178
  // rather than asked and refused. models are those live buckets' names.
170
- async function startPlan(jwt, device) {
179
+ //
180
+ // coming (sign-in only) also takes a plan that has not started yet, as
181
+ // ZCode's own check does (resolveStartPlanBalanceAvailability's "pending"
182
+ // counts as entitled): {name, models: [], coming: true}. Requests and the
183
+ // card never ask for it: a plan not started serves nothing yet.
184
+ async function startPlan(jwt, device, coming = false) {
171
185
  if (!jwt) throw new Error("not signed in to ZCode")
172
186
  if (jwtExpired(jwt)) throw new Error(EXPIRED)
173
187
  const b = (await call("GET", `${ZCODE}/api/v1/zcode-plan/billing/balance?app_version=${APP_VERSION}`, { auth: "Bearer " + jwt, device })) ?? {}
@@ -198,7 +212,11 @@ async function startPlan(jwt, device) {
198
212
  }
199
213
  }
200
214
  }
201
- return any ? { name: name || "Start Plan", models: [...ms], allModels: [...all.values()], ttl } : null
215
+ if (any) return { name: name || "Start Plan", models: [...ms], allModels: [...all.values()], ttl }
216
+ if (!coming) return null
217
+ const next = plans.filter((p) => notStarted(p, now) && !(num(p?.ends_at) > 0 && num(p?.ends_at) <= now))
218
+ .sort((a, b) => startNum(a.starts_at) - startNum(b.starts_at))[0]
219
+ return next ? { name: String(next.name ?? "").trim() || "Start Plan", models: [], allModels: [], ttl, coming: true } : null
202
220
  }
203
221
 
204
222
  // plansOf says where an account's requests go and what more it can spend:
@@ -260,6 +278,7 @@ const blocked = new Map()
260
278
  // giftServes: the gift's buckets name their models lowercased ("glm-5.3-
261
279
  // flash"); what is asked for arrives as the plan lists it ("GLM-5.3-Flash").
262
280
  const giftServes = (gift, m) => (gift.models ?? []).some((x) => x.toLowerCase() === m.toLowerCase())
281
+ const giftRested = (s, m) => Date.now() < (blocked.get(s.key + "\0" + s.jwt + "\0" + m.toLowerCase()) ?? 0)
263
282
  const giftUsable = (s, gift, m) => !jwtExpired(s.jwt) && giftServes(gift, m) &&
264
283
  Date.now() >= (blocked.get(s.key + "\0" + s.jwt + "\0" + m.toLowerCase()) ?? 0)
265
284
 
@@ -679,11 +698,10 @@ const CAPTCHA = 3007
679
698
  const CAPTCHA_WORDS = /captcha/i
680
699
 
681
700
  // The marketing feed and the claim are gated on the client's version:
682
- // under an older version's header the feed answers empty (11-03: the
683
- // day's banner showed for 3.17.2 and for no older one), so they go as
684
- // the current client (3.17.2) sends them.
685
- const MARKETING_VERSION = "3.17.2"
686
- const marketingHeaders = () => ({ "X-ZCode-App-Version": MARKETING_VERSION, "X-Platform": platform(), "X-Client-Language": clientLanguage() })
701
+ // under an older version's header the feed answers empty (11-03, #678:
702
+ // the day's banner showed for 3.17.2 and for no older one), so they go,
703
+ // as every call does, as the current client (APP_VERSION) sends them.
704
+ const marketingHeaders = () => ({ "X-ZCode-App-Version": APP_VERSION, "X-Platform": platform(), "X-Client-Language": clientLanguage() })
687
705
 
688
706
  // claimOne asks /billing/claim for one plan and says what its body says:
689
707
  // "claimed" (code 0), "done" (code 1003, claimed before), "captcha" (the
@@ -730,12 +748,15 @@ async function claimOne(s, planID) {
730
748
  const TOUCH = "/api/v1/marketing/touch"
731
749
  const CLAIM_ACTION = "claim_zcode_plan"
732
750
  let touchSeq = 0
751
+ // clientLanguage is the language ZCode names to marketing/touch: zh-CN or
752
+ // en-US, its two UI languages, never another (marketingTouchService's
753
+ // localeSchema), so a computer set to zh-Hans-CN or en-GB asks as ZCode does.
733
754
  const clientLanguage = () => {
755
+ let l = ""
734
756
  try {
735
- return Intl.DateTimeFormat().resolvedOptions().locale || "en-US"
736
- } catch {
737
- return "en-US"
738
- }
757
+ l = Intl.DateTimeFormat().resolvedOptions().locale || ""
758
+ } catch {}
759
+ return /^zh\b/i.test(l) ? "zh-CN" : "en-US"
739
760
  }
740
761
 
741
762
  // touchPlans is the plans marketing/touch's deliveries offer to claim:
@@ -1213,7 +1234,7 @@ async function signedIn(site, token, jwt, device, onTeam = false) {
1213
1234
  let berr = null
1214
1235
  if (jwt) {
1215
1236
  try {
1216
- const sp = await startPlan(jwt, device)
1237
+ const sp = await startPlan(jwt, device, true)
1217
1238
  if (sp) return { ...(err || !s?.key ? { base: SITES[site].base } : s), jwt, plan: sp.name }
1218
1239
  } catch (e) {
1219
1240
  berr = e
@@ -2390,10 +2411,10 @@ export async function ZCodeAuthPlugin({ client }, options = {}) {
2390
2411
  const raw = giftModel(gift, m)
2391
2412
  const available = raw && giftUsable(s, gift, raw)
2392
2413
  if (!available) {
2393
- const error = { type: raw ? "rate_limit_error" : "invalid_request_error", message: raw ? `ZCode's limited quota for ${raw} is exhausted or temporarily unavailable` : `ZCode has no active limited quota for ${m}` }
2414
+ const error = { type: raw ? "rate_limit_error" : "invalid_request_error", message: raw ? `ZCode's limited quota for ${raw} is exhausted or temporarily unavailable${giftRested(s, raw) ? " (ZCode answered 429 to the last request for it, so it is not asked again for a minute)" : ""}` : `ZCode has no active limited quota for ${m}` }
2394
2415
  return kept(new Response(JSON.stringify({ type: "error", error }), { status: raw ? 429 : 400, headers: { "content-type": "application/json" } }))
2395
2416
  }
2396
- orig = opts.body = JSON.stringify({ ...JSON.parse(orig), model: raw })
2417
+ orig = opts.body = JSON.stringify({ ...JSON.parse(orig), model: wireModel(raw) })
2397
2418
  m = raw
2398
2419
  forcedGift = true
2399
2420
  } else if (!alone && !edgeHeld() && freeFirst(provider?.options) && s.jwt && known && giftUsable(s, known, m)) {
@@ -2406,7 +2427,7 @@ export async function ZCodeAuthPlugin({ client }, options = {}) {
2406
2427
  // it was. A refusal past this check is replayed on the plan
2407
2428
  // in the same request (replayOnPlan below).
2408
2429
  const raw = giftNames(known).find((x) => x.toLowerCase() === m.toLowerCase())
2409
- orig = opts.body = JSON.stringify({ ...JSON.parse(orig), model: raw })
2430
+ orig = opts.body = JSON.stringify({ ...JSON.parse(orig), model: wireModel(raw) })
2410
2431
  m = raw
2411
2432
  freeGift = true
2412
2433
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-zcode-auth",
3
- "version": "0.1.45",
3
+ "version": "0.1.47",
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",