@magpie-community/opencode-zcode-auth 0.1.38 → 0.1.39

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 +11 -4
  2. package/index.mjs +94 -31
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -233,8 +233,11 @@ left, shows no count.
233
233
  **Gift plans ZCode holds but has not granted.** ZCode hands out one-off
234
234
  trust-build / gift plans (a day's ZCode Trust Build, say) that are not
235
235
  on the account until the ZCode app claims them. The plugin reads the
236
- app's own list of them (`GET /api/v1/zcode-plan/billing/preview`) with
237
- the card and names them there — the window is named "Gift plans to
236
+ app's own lists of them with the card and names them there: its claim
237
+ banners' feed (`GET /api/v1/marketing/touch`, each delivery's
238
+ `claim_zcode_plan` button) and `GET /api/v1/zcode-plan/billing/preview`.
239
+ A banner plan (a weekend's ZCODE WEEKEND BUILD, 10-30) is in the feed
240
+ alone, never in the preview (#503) — the window is named "Gift plans to
238
241
  claim in the ZCode app" and the display shows "1 · ZCode Trust Build"
239
242
  — set aside, so the line stops nothing and routing, usage caps and the
240
243
  menu bar pass it over. The name is a fixed sentence magpie can
@@ -249,13 +252,17 @@ the next read shows it as an ordinary gift bucket.
249
252
  With ZCode turned on in magpie's Settings → Daily check-in (it is off
250
253
  until you turn it on), magpie has the plugin claim each account's gift
251
254
  plans once a day (`auth.checkin`; `magpie accounts checkin` asks now). It
252
- reads the app's preview list and posts each plan it lists to
255
+ reads both lists above (the banners' feed and the preview, each plan
256
+ once) and posts each plan to
253
257
  `POST /api/v1/zcode-plan/billing/claim` (`{"plan_id": …}`) with the
254
258
  account's ZCode session, as the app does. The answer is judged by the
255
259
  body's code, not the HTTP status:
256
260
 
257
261
  - nothing listed: *inactive* (ZCode answers an account with nothing to
258
- claim `{"code":0,"msg":""}`, with no list at all);
262
+ claim `{"code":0,"msg":""}`, with no list at all). A list that doesn't
263
+ read is *failed*, never nothing to claim. A plan just claimed may show
264
+ in neither `billing/preview` nor `billing/current` until its
265
+ `effective_at`, so neither list is read as "claimed or not";
259
266
  - code 0: *claimed* (the next request uses the plan);
260
267
  - code 1003 "target already exists" (HTTP 200): *done*, claimed before;
261
268
  - code 3007 "captcha verify failed": *captcha*, the plan is claimed in
package/index.mjs CHANGED
@@ -644,7 +644,8 @@ async function giftOf(s) {
644
644
  const CLAIM_HINT = "Gift plans to claim in the ZCode app"
645
645
 
646
646
  // claimHint is the card's line for gift plans ZCode is holding for the
647
- // account but has not put on it: GET /billing/preview lists them. The
647
+ // account but has not put on it: the app's banners (marketing/touch) and
648
+ // GET /billing/preview list them (toClaim). The
648
649
  // card only names them: they are claimed in the ZCode app, or by the
649
650
  // daily claim (claimDaily) when the user turned it on; on 10-06 a claim
650
651
  // without the app's Aliyun captcha attestation was refused (3007). A window, set aside (it is no
@@ -657,16 +658,10 @@ const CLAIM_HINT = "Gift plans to claim in the ZCode app"
657
658
  // own.
658
659
  async function claimHint(s) {
659
660
  if (!s.jwt || jwtExpired(s.jwt)) return null
660
- let d
661
- try {
662
- d = await call("GET", `${ZCODE}/api/v1/zcode-plan/billing/preview?app_version=${APP_VERSION}&platform=${platform()}`, { auth: "Bearer " + s.jwt, device: s.device })
663
- } catch {
664
- return null
665
- }
666
- const plans = (previewPlans(d) ?? []).filter((p) => typeof p?.plan_id === "string" && p.plan_id.trim())
661
+ const { plans } = await toClaim(s)
667
662
  if (!plans.length) return null
668
663
  const n = plans.length
669
- const plan = first(...plans.map((p) => (typeof p?.name === "string" ? p.name : "")), "ZCode gift plan")
664
+ const plan = first(...plans.map((p) => p.name), "ZCode gift plan")
670
665
  return { name: CLAIM_HINT, used: 0, aside: true, display: `${n} · ${plan}` }
671
666
  }
672
667
 
@@ -691,8 +686,8 @@ const zcodeHeaders = () => ({ "X-ZCode-App-Version": APP_VERSION, "X-Platform":
691
686
  // "claimed" (code 0), "done" (code 1003, claimed before), "captcha" (the
692
687
  // claim wants the app's captcha), else "failed" with why. Only a body
693
688
  // that reads decides: an HTTP error without one, or text that isn't
694
- // JSON, is failed (unknown), never claimed. The code 0 body hasn't been
695
- // seen yet, so nothing but its code is read.
689
+ // JSON, is failed (unknown), never claimed. Only the code is read (#503
690
+ // has a real code 0 reply, "Plan claimed successfully.").
696
691
  async function claimOne(s, planID) {
697
692
  let res, text
698
693
  try {
@@ -732,41 +727,109 @@ function previewPlans(d) {
732
727
  return Array.isArray(d.plans) ? d.plans : null
733
728
  }
734
729
 
735
- // claimDaily claims every gift plan ZCode's preview lists for the account
736
- // (#503), as auth.checkin's answer: inactive when it lists none, claimed
737
- // when one was claimed now, done when each was claimed before, captcha
738
- // when ZCode wants the app's captcha for one (it is claimed in the ZCode
739
- // app), failed when the list or a claim can't be read. A failed one is
740
- // asked again later in the day, when a plan claimed now answers 1003.
730
+ // 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
733
+ // while the app shows its banner, and the app claims it through the same
734
+ // /billing/claim (#503's capture of 10-30, kongbai01203: GET
735
+ // /api/v1/marketing/touch?seq=<n, counting up from 0> with the session,
736
+ // X-Device-Mid, X-Client-Language and X-ZCode-App-Version, answered
737
+ // data.deliveries[].banner.buttons[].action = {"type":"claim_zcode_plan",
738
+ // "args":{"plan_id":…}} and the plan's name in
739
+ // banner.background.args.zcode_plan.name).
740
+ const TOUCH = "/api/v1/marketing/touch"
741
+ const CLAIM_ACTION = "claim_zcode_plan"
742
+ let touchSeq = 0
743
+ const clientLanguage = () => {
744
+ try {
745
+ return Intl.DateTimeFormat().resolvedOptions().locale || "en-US"
746
+ } catch {
747
+ return "en-US"
748
+ }
749
+ }
750
+
751
+ // touchPlans is the plans marketing/touch's deliveries offer to claim:
752
+ // [{plan_id, name}], [] for a feed with no deliveries, null for one that
753
+ // doesn't read. Each delivery is searched for its claim_zcode_plan
754
+ // actions and its zcode_plan's name, wherever the banner keeps them, so
755
+ // a button moved inside the banner is still found.
756
+ function touchPlans(d) {
757
+ if (d == null) return []
758
+ if (typeof d !== "object" || Array.isArray(d)) return null
759
+ if (d.deliveries == null) return []
760
+ if (!Array.isArray(d.deliveries)) return null
761
+ const out = []
762
+ for (const dv of d.deliveries) {
763
+ const ids = [], names = []
764
+ const walk = (v, depth) => {
765
+ if (!v || typeof v !== "object" || depth > 12) return
766
+ if (Array.isArray(v)) return v.forEach((x) => walk(x, depth + 1))
767
+ if (v.type === CLAIM_ACTION && typeof v.args?.plan_id === "string" && v.args.plan_id.trim()) ids.push(v.args.plan_id.trim())
768
+ if (v.zcode_plan && typeof v.zcode_plan === "object" && typeof v.zcode_plan.name === "string") names.push(v.zcode_plan.name)
769
+ for (const k of Object.keys(v)) walk(v[k], depth + 1)
770
+ }
771
+ walk(dv, 0)
772
+ for (const id of ids) out.push({ plan_id: id, name: first(names[0] ?? "", id) })
773
+ }
774
+ return out
775
+ }
776
+
777
+ // 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.
781
+ async function toClaim(s) {
782
+ const plans = [], unread = []
783
+ const add = (p) => {
784
+ if (!plans.some((q) => q.plan_id === p.plan_id)) plans.push(p)
785
+ }
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
+ })
802
+ return { plans, unread }
803
+ }
804
+
805
+ // claimDaily claims every gift plan ZCode offers the account (#503:
806
+ // toClaim, the app's banners and its preview), as auth.checkin's answer:
807
+ // inactive when neither lists one, claimed when one was claimed now,
808
+ // done when each was claimed before, captcha when ZCode wants the app's
809
+ // captcha for one (it is claimed in the ZCode app), failed when a list
810
+ // or a claim can't be read. A failed one is asked again later in the
811
+ // day, when a plan claimed now answers 1003.
741
812
  // Nothing here marks the account: a failed claim keeps its sign-in.
742
813
  async function claimDaily(s) {
743
814
  if (!s?.jwt) return { outcome: "ineligible", message: "ZCode's gift plans are claimed with a ZCode sign-in; this account has none" }
744
815
  if (jwtExpired(s.jwt)) return { outcome: "failed", message: EXPIRED }
745
- let d
746
- try {
747
- d = await call("GET", `${ZCODE}/api/v1/zcode-plan/billing/preview?app_version=${APP_VERSION}&platform=${platform()}`, { auth: "Bearer " + s.jwt, device: s.device, headers: zcodeHeaders() })
748
- } catch (e) {
749
- return { outcome: "failed", message: `gift plans to claim: ${e?.message ?? String(e)}` }
750
- }
751
- const listed = previewPlans(d)
752
- if (!listed) return { outcome: "failed", message: "gift plans to claim: the plan list doesn't read" }
753
- const plans = listed.filter((p) => typeof p?.plan_id === "string" && p.plan_id.trim())
816
+ const { plans, unread } = await toClaim(s)
817
+ if (!plans.length && unread.length) return { outcome: "failed", message: unread.join("; ") }
754
818
  if (!plans.length) return { outcome: "inactive", message: "no ZCode gift plan to claim" }
755
819
  const got = []
756
- for (const p of plans) {
757
- const name = first(typeof p.name === "string" ? p.name : "", p.plan_id)
758
- got.push({ name, ...(await claimOne(s, p.plan_id.trim())) })
759
- }
820
+ for (const p of plans) got.push({ name: p.name, ...(await claimOne(s, p.plan_id)) })
760
821
  const named = (o) => got.filter((g) => g.outcome === o).map((g) => g.name)
761
822
  const claimed = named("claimed")
762
823
  const said = [
763
824
  claimed.length ? `claimed ${claimed.join(", ")}` : "",
764
825
  ...got.filter((g) => g.outcome === "captcha").map((g) => `${g.name}: claim it in the ZCode app (${g.why})`),
765
826
  ...got.filter((g) => g.outcome === "failed").map((g) => `${g.name}: ${g.why}`),
827
+ ...unread,
766
828
  ].filter(Boolean).join("; ")
767
829
  // a plan claimed now is used by the next request, not ten minutes on
768
830
  if (claimed.length) routes.delete(s.key + "\0" + s.jwt)
769
- if (got.some((g) => g.outcome === "failed")) return { outcome: "failed", message: said }
831
+ // a list that didn't read may hold a plan not claimed: asked again
832
+ if (unread.length || got.some((g) => g.outcome === "failed")) return { outcome: "failed", message: said }
770
833
  if (got.some((g) => g.outcome === "captcha")) return { outcome: "captcha", message: said }
771
834
  if (claimed.length) return { outcome: "claimed", message: said }
772
835
  return { outcome: "done", message: `already claimed: ${named("done").join(", ")}` }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-zcode-auth",
3
- "version": "0.1.38",
3
+ "version": "0.1.39",
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": "./index.mjs",