@magpie-community/opencode-zcode-auth 0.1.44 → 0.1.46

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 +18 -10
  2. package/index.mjs +52 -50
  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,
@@ -273,12 +274,14 @@ the next read shows it as an ordinary gift bucket.
273
274
  With ZCode turned on in magpie's Settings → Daily check-in (it is off
274
275
  until you turn it on), magpie has the plugin claim each account's gift
275
276
  plans once a day (`auth.checkin`; `magpie accounts checkin` asks now). It
276
- reads both lists above (the banners' feed and the preview, each plan
277
- once) and posts each plan to
277
+ reads the banner feed above — the v3.17 client replaced the old preview
278
+ polling with the feed alone, and the feed is gated on the client's
279
+ version, so the plugin asks as the current client (3.17.2) sends — and
280
+ posts each plan the feed offers to
278
281
  `POST /api/v1/zcode-plan/billing/claim` (`{"plan_id": …}`) with the
279
282
  account's ZCode session, as the app does. An answer of nothing to claim
280
283
  carries an `until` half an hour out, so magpie's loop asks again then,
281
- and again, all day while neither list names a plan — two GETs, nothing
284
+ and again, all day while the feed names no plan — one GET, nothing
282
285
  posted: ZCode drops plans into the feed on its own schedule (a weekend
283
286
  banner starting at 19:00 Beijing, say), and one that arrives
284
287
  after the day's ask used to sit unclaimed past its short life. The
@@ -291,20 +294,25 @@ after one was claimed, is seen the next day. The answer is judged by
291
294
  the body's code, not the HTTP status:
292
295
 
293
296
  - nothing listed: *inactive* (ZCode answers an account with nothing to
294
- claim `{"code":0,"msg":""}`, with no list at all). A list that doesn't
295
- read is *failed*, never nothing to claim. A plan just claimed may show
296
- in neither `billing/preview` nor `billing/current` until its
297
- `effective_at`, so neither list is read as "claimed or not";
297
+ claim `{"code":0,"msg":""}`, with no list at all). A feed that doesn't
298
+ read is *failed*, never nothing to claim;
298
299
  - code 0: *claimed* (the next request uses the plan);
299
300
  - code 1003 "target already exists" (HTTP 200): *done*, claimed before;
300
301
  - code 3007 "captcha verify failed": *captcha*, the plan is claimed in
301
- the ZCode app (on 10-06 ZCode wanted the app's Aliyun captcha for a
302
- claim; whether it still does for a fresh plan is not known yet);
302
+ the ZCode app — the claim is gated on the Aliyun captcha the app's
303
+ renderer answers, and a claim from outside the app is refused it
304
+ (11-03: seven accounts' ZCODE GLOBAL BUILD all answered 3007). Once
305
+ claimed there, the gift lands on the account and freeFirst spends it
306
+ like any other;
303
307
  - anything else, an HTTP error without a code, or a reply that isn't
304
308
  JSON: *failed*, which magpie asks again later that day. It is never
305
309
  taken as claimed, and a failed claim leaves the account's sign-in
306
310
  alone.
307
311
 
312
+ The rewards center in the client (奖励中心, the embedded
313
+ `zcode.z.ai/{cn|en}/rewards` page) is the referral program's website,
314
+ separate from these gift plans; its rewards are claimed on that page.
315
+
308
316
  An account added with a GLM Coding Plan key has no ZCode session to claim
309
317
  with (*ineligible*).
310
318
 
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.
@@ -167,7 +171,12 @@ const giftSpent = (x) => {
167
171
  // the gift when any of them has a live bucket serving it — a bucket past
168
172
  // its plan's own end, or one whose remaining_units says none, is skipped
169
173
  // rather than asked and refused. models are those live buckets' names.
170
- async function startPlan(jwt, device) {
174
+ //
175
+ // coming (sign-in only) also takes a plan that has not started yet, as
176
+ // ZCode's own check does (resolveStartPlanBalanceAvailability's "pending"
177
+ // counts as entitled): {name, models: [], coming: true}. Requests and the
178
+ // card never ask for it: a plan not started serves nothing yet.
179
+ async function startPlan(jwt, device, coming = false) {
171
180
  if (!jwt) throw new Error("not signed in to ZCode")
172
181
  if (jwtExpired(jwt)) throw new Error(EXPIRED)
173
182
  const b = (await call("GET", `${ZCODE}/api/v1/zcode-plan/billing/balance?app_version=${APP_VERSION}`, { auth: "Bearer " + jwt, device })) ?? {}
@@ -198,7 +207,11 @@ async function startPlan(jwt, device) {
198
207
  }
199
208
  }
200
209
  }
201
- return any ? { name: name || "Start Plan", models: [...ms], allModels: [...all.values()], ttl } : null
210
+ if (any) return { name: name || "Start Plan", models: [...ms], allModels: [...all.values()], ttl }
211
+ if (!coming) return null
212
+ const next = plans.filter((p) => notStarted(p, now) && !(num(p?.ends_at) > 0 && num(p?.ends_at) <= now))
213
+ .sort((a, b) => startNum(a.starts_at) - startNum(b.starts_at))[0]
214
+ return next ? { name: String(next.name ?? "").trim() || "Start Plan", models: [], allModels: [], ttl, coming: true } : null
202
215
  }
203
216
 
204
217
  // plansOf says where an account's requests go and what more it can spend:
@@ -644,8 +657,8 @@ async function giftOf(s) {
644
657
  const CLAIM_HINT = "Gift plans to claim in the ZCode app"
645
658
 
646
659
  // claimHint is the card's line for gift plans ZCode is holding for the
647
- // account but has not put on it: the app's banners (marketing/touch) and
648
- // GET /billing/preview list them (toClaim). The
660
+ // account but has not put on it: the app's banners (marketing/touch)
661
+ // list them (toClaim). The
649
662
  // card only names them: they are claimed in the ZCode app, or by the
650
663
  // daily claim (claimDaily) when the user turned it on; on 10-06 a claim
651
664
  // without the app's Aliyun captcha attestation was refused (3007). A window, set aside (it is no
@@ -678,9 +691,11 @@ const CLAIMED_ALREADY = 1003
678
691
  const CAPTCHA = 3007
679
692
  const CAPTCHA_WORDS = /captcha/i
680
693
 
681
- // zcodeHeaders are what the ZCode app sends on its billing calls beside
682
- // the session (#503's capture): its version and platform.
683
- const zcodeHeaders = () => ({ "X-ZCode-App-Version": APP_VERSION, "X-Platform": platform() })
694
+ // The marketing feed and the claim are gated on the client's version:
695
+ // under an older version's header the feed answers empty (11-03, #678:
696
+ // the day's banner showed for 3.17.2 and for no older one), so they go,
697
+ // as every call does, as the current client (APP_VERSION) sends them.
698
+ const marketingHeaders = () => ({ "X-ZCode-App-Version": APP_VERSION, "X-Platform": platform(), "X-Client-Language": clientLanguage() })
684
699
 
685
700
  // claimOne asks /billing/claim for one plan and says what its body says:
686
701
  // "claimed" (code 0), "done" (code 1003, claimed before), "captcha" (the
@@ -693,7 +708,7 @@ async function claimOne(s, planID) {
693
708
  try {
694
709
  res = await fetch(`${ZCODE}/api/v1/zcode-plan/billing/claim`, {
695
710
  method: "POST",
696
- headers: { "Content-Type": "application/json", Accept: "application/json", "User-Agent": UA, Authorization: "Bearer " + s.jwt, "X-Device-Mid": s.device || anyDevice, ...zcodeHeaders() },
711
+ headers: { "Content-Type": "application/json", Accept: "application/json", "User-Agent": UA, Authorization: "Bearer " + s.jwt, "X-Device-Mid": s.device || anyDevice, ...marketingHeaders() },
697
712
  body: JSON.stringify({ plan_id: planID }),
698
713
  })
699
714
  text = await res.text()
@@ -713,39 +728,29 @@ async function claimOne(s, planID) {
713
728
  return { outcome: "failed", why: `${msg || "error"} (HTTP ${res.status}, code ${code})` }
714
729
  }
715
730
 
716
- // previewPlans is /billing/preview's plan list. An account with nothing to
717
- // claim is answered {"code":0,"msg":"","logid":…}: no data at all, not
718
- // "plans": [] (the owner's account read 10-30 for hykql's report on
719
- // Discord; the list is left out when it is empty). call has already judged the code, so a reply
720
- // without data, or data without plans, is a list of none. Only data that
721
- // isn't an object, or plans that are there and aren't a list, don't read
722
- // (null, unknown).
723
- function previewPlans(d) {
724
- if (d == null) return []
725
- if (typeof d !== "object" || Array.isArray(d)) return null
726
- if (d.plans == null) return []
727
- return Array.isArray(d.plans) ? d.plans : null
728
- }
729
-
730
731
  // TOUCH is ZCode's marketing feed: the ZCode app's claim banners come
731
- // from it, not from /billing/preview. A banner plan ("ZCODE WEEKEND
732
- // BUILD", 10-30) is in neither /billing/preview nor /billing/current
732
+ // from it. A banner plan ("ZCODE WEEKEND BUILD", 10-30) is served only
733
733
  // while the app shows its banner, and the app claims it through the same
734
734
  // /billing/claim (#503's capture of 10-30, kongbai01203: GET
735
735
  // /api/v1/marketing/touch?seq=<n, counting up from 0> with the session,
736
736
  // X-Device-Mid, X-Client-Language and X-ZCode-App-Version, answered
737
737
  // data.deliveries[].banner.buttons[].action = {"type":"claim_zcode_plan",
738
738
  // "args":{"plan_id":…}} and the plan's name in
739
- // banner.background.args.zcode_plan.name).
739
+ // banner.background.args.zcode_plan.name). The v3.17 client reads this
740
+ // feed alone: the old preview polling is gone, and the rewards center's
741
+ // gifts are the rewards website's, claimed in the client.
740
742
  const TOUCH = "/api/v1/marketing/touch"
741
743
  const CLAIM_ACTION = "claim_zcode_plan"
742
744
  let touchSeq = 0
745
+ // clientLanguage is the language ZCode names to marketing/touch: zh-CN or
746
+ // en-US, its two UI languages, never another (marketingTouchService's
747
+ // localeSchema), so a computer set to zh-Hans-CN or en-GB asks as ZCode does.
743
748
  const clientLanguage = () => {
749
+ let l = ""
744
750
  try {
745
- return Intl.DateTimeFormat().resolvedOptions().locale || "en-US"
746
- } catch {
747
- return "en-US"
748
- }
751
+ l = Intl.DateTimeFormat().resolvedOptions().locale || ""
752
+ } catch {}
753
+ return /^zh\b/i.test(l) ? "zh-CN" : "en-US"
749
754
  }
750
755
 
751
756
  // touchPlans is the plans marketing/touch's deliveries offer to claim:
@@ -775,30 +780,27 @@ function touchPlans(d) {
775
780
  }
776
781
 
777
782
  // toClaim is every gift plan the account can claim: what the app's
778
- // marketing feed offers (its banners) and what /billing/preview lists,
779
- // once each by plan_id. unread names a list that didn't read: its
780
- // plans are unknown, never none.
783
+ // marketing feed offers (its banners). The v3.17 client replaced the old
784
+ // preview polling with the feed alone (docs/marketing-touch-client.md in
785
+ // zai-org/ZCode), so the preview is read no more; a rewards center's gift
786
+ // lives on the rewards website, outside this API, and is claimed in the
787
+ // client. unread names a feed that didn't read: its plans are unknown,
788
+ // never none.
781
789
  async function toClaim(s) {
782
790
  const plans = [], unread = []
783
791
  const add = (p) => {
784
792
  if (!plans.some((q) => q.plan_id === p.plan_id)) plans.push(p)
785
793
  }
786
- const ask = async (what, url, headers, read) => {
787
- let d
788
- try {
789
- d = await call("GET", url, { auth: "Bearer " + s.jwt, device: s.device, headers })
790
- } catch (e) {
791
- return unread.push(`${what}: ${e?.message ?? String(e)}`)
792
- }
793
- const got = read(d)
794
- if (!got) return unread.push(`${what}: the plan list doesn't read`)
795
- got.forEach(add)
796
- }
797
- await ask("gift plans to claim", `${ZCODE}${TOUCH}?seq=${touchSeq++}`, { "X-ZCode-App-Version": APP_VERSION, "X-Client-Language": clientLanguage() }, touchPlans)
798
- await ask("gift plans to claim", `${ZCODE}/api/v1/zcode-plan/billing/preview?app_version=${APP_VERSION}&platform=${platform()}`, zcodeHeaders(), (d) => {
799
- const l = previewPlans(d)
800
- return l && l.filter((p) => typeof p?.plan_id === "string" && p.plan_id.trim()).map((p) => ({ plan_id: p.plan_id.trim(), name: first(typeof p.name === "string" ? p.name : "", p.plan_id.trim()) }))
801
- })
794
+ let d
795
+ try {
796
+ d = await call("GET", `${ZCODE}${TOUCH}?seq=${touchSeq++}`, { auth: "Bearer " + s.jwt, device: s.device, headers: marketingHeaders() })
797
+ } catch (e) {
798
+ unread.push(`gift plans to claim: ${e?.message ?? String(e)}`)
799
+ return { plans, unread }
800
+ }
801
+ const got = touchPlans(d)
802
+ if (!got) unread.push("gift plans to claim: the plan list doesn't read")
803
+ else got.forEach(add)
802
804
  return { plans, unread }
803
805
  }
804
806
 
@@ -1226,7 +1228,7 @@ async function signedIn(site, token, jwt, device, onTeam = false) {
1226
1228
  let berr = null
1227
1229
  if (jwt) {
1228
1230
  try {
1229
- const sp = await startPlan(jwt, device)
1231
+ const sp = await startPlan(jwt, device, true)
1230
1232
  if (sp) return { ...(err || !s?.key ? { base: SITES[site].base } : s), jwt, plan: sp.name }
1231
1233
  } catch (e) {
1232
1234
  berr = e
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-zcode-auth",
3
- "version": "0.1.44",
3
+ "version": "0.1.46",
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",