@magpie-community/opencode-zcode-auth 0.1.32 → 0.1.34

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 +17 -3
  2. package/index.mjs +62 -16
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -145,8 +145,10 @@ declares — `maxConcurrency` on the auth hook, and `magpie.maxConcurrency`
145
145
  in its `package.json`, both three. A request the vendor refuses anyway
146
146
  waits a moment (a quarter of a second, then longer — about ten seconds of
147
147
  waits in all) and asks again, the same request byte for byte. A cap that
148
- outlasts the waits goes on as it came, ZCode's words followed by what it
149
- is, so magpie can move that request along instead of resting the account.
148
+ outlasts the waits goes on as a 429, ZCode's words followed by what they
149
+ mean, so the agent reads why. magpie still reads it as a rate limit: the
150
+ account rests a minute and the request goes to the next member of its
151
+ routing group, as it did before.
150
152
 
151
153
  Three is a default, not a cap the plugin enforces. A plan's real cap
152
154
  follows its tier — Z.ai serves a Max plan more at once than a Pro, and a
@@ -164,7 +166,19 @@ and end. The card's plan and term stay the coding plan's. A gift read
164
166
  that fails leaves just the coding card — magpie hides a card's windows
165
167
  behind an error, so a gift hiccup must not black out a working Coding
166
168
  Plan. A gift that goes out of date while requests use it simply stops
167
- showing.
169
+ showing. Beside the coding plan's windows each gift bucket is named
170
+ after its plan ("Start Plan · GLM-5.1-Trial"), one plan or several, so
171
+ the two allowances can't be read as one.
172
+
173
+ **Gift plans that start later.** A plan on the balance whose `starts_at`
174
+ is still to come (a Start Plan taking effect at 23:00 today) serves
175
+ nothing yet: no request goes to it and its buckets don't count. The card
176
+ names it in a line set aside, "ZCode gift plan not started yet", with
177
+ the plan's name and its start in this computer's time as the display
178
+ ("Start Plan · 2026-10-29 23:00"), soonest first. An account whose only
179
+ gift plan starts later gets a card with that line, not "ended or never
180
+ started". The status ZCode gives such a plan has not been seen, so any
181
+ status but `expired` is taken; `starts_at` decides it.
168
182
 
169
183
  **Resets.** A GLM Coding Plan of the account's own, and a team seat,
170
184
  show the resets the vendor's usage page lists
package/index.mjs CHANGED
@@ -182,7 +182,7 @@ async function startPlan(jwt, device) {
182
182
  for (const p of plans) {
183
183
  const st = String(p?.status ?? "").trim().toLowerCase()
184
184
  const end = num(p?.ends_at)
185
- if (st !== "active" || (end > 0 && end <= now)) continue
185
+ if (st !== "active" || (end > 0 && end <= now) || notStarted(p, now)) continue
186
186
  any = true
187
187
  if (end > 0) ttl = Math.min(ttl, (end - now) * 1000)
188
188
  if (!name) name = String(p?.name ?? "").trim()
@@ -520,6 +520,28 @@ function startNum(v) {
520
520
  return undefined
521
521
  }
522
522
 
523
+ // notStarted is a plan whose starts_at is still to come (a Start Plan
524
+ // that takes effect at 23:00 today, windy on Discord): it serves nothing
525
+ // yet, so no request is routed to it and its buckets don't count, but the
526
+ // card names it with its start. ZCode's balance gives every plan a
527
+ // starts_at in Unix seconds; which status a plan not started yet carries
528
+ // is not known, so any but "expired" is taken.
529
+ function notStarted(p, now) {
530
+ const at = startNum(p?.starts_at)
531
+ return at > now && String(p?.status ?? "").trim().toLowerCase() !== "expired"
532
+ }
533
+
534
+ // PENDING is the card's line for a gift plan that has not started yet: a
535
+ // fixed sentence, so magpie translates it; the plan's name and its start,
536
+ // in this computer's time, are the display. Set aside: it stops nothing.
537
+ const PENDING = "ZCode gift plan not started yet"
538
+ const two = (n) => String(n).padStart(2, "0")
539
+ const localTime = (sec) => {
540
+ const d = new Date(Math.trunc(sec) * 1000)
541
+ return `${d.getFullYear()}-${two(d.getMonth() + 1)}-${two(d.getDate())} ${two(d.getHours())}:${two(d.getMinutes())}`
542
+ }
543
+ const pendingLines = (g) => (g?.pending ?? []).map((p) => ({ name: PENDING, used: 0, aside: true, display: `${p.name} · ${localTime(p.start)}` }))
544
+
523
545
  // giftOf reads the balance and returns the gift's card fields: its plans and
524
546
  // windows. Several plans can be active at once, so every live plan's buckets
525
547
  // show, each keeping its own models and end; the plan and end put on the
@@ -551,21 +573,28 @@ async function giftOf(s) {
551
573
  return keep
552
574
  })
553
575
 
576
+ // a plan not started yet is named with its start, soonest first; its
577
+ // buckets, if the balance lists any, wait for it
578
+ const pending = plans.filter((p) => notStarted(p, now))
579
+ .map((p) => ({ name: first(str(p.name), "Start Plan"), start: startNum(p.starts_at) }))
580
+ .sort((a, b) => a.start - b.start)
581
+
554
582
  // every live plan counts as the gift: no plan-name filter decides it,
555
583
  // the balance says what the session can spend
556
584
  let active = null
557
585
  for (const p of plans) {
558
- if (p.status.trim().toLowerCase() !== "active") continue
586
+ if (p.status.trim().toLowerCase() !== "active" || notStarted(p, now)) continue
559
587
  const end = startNum(p.ends_at)
560
588
  if (end > 0 && end <= now) continue
561
589
  if (active && !(end > 0 && (!startNum(active.end) || end < startNum(active.end)))) continue
562
590
  active = { name: first(str(p.name), "Start Plan"), end, until: end > 0 ? new Date(Math.trunc(end) * 1000).toISOString() : "" }
563
591
  }
564
- if (!active) return null
565
- const out = { plan: active.name, windows: [] }
566
- if (active.until) Object.assign(out, { until: active.until, renew: "off" })
592
+ if (!active && !pending.length) return null
593
+ // with no live plan, the one starting soonest names the card
594
+ const out = { plan: active?.name ?? pending[0].name, windows: [], live: !!active, pending }
595
+ if (active?.until) Object.assign(out, { until: active.until, renew: "off" })
567
596
  const live = plans.filter((p) => {
568
- if (p.status.trim().toLowerCase() !== "active") return false
597
+ if (p.status.trim().toLowerCase() !== "active" || notStarted(p, now)) return false
569
598
  const end = startNum(p.ends_at)
570
599
  return !(end > 0 && end <= now)
571
600
  })
@@ -678,6 +707,9 @@ async function startUsage(s) {
678
707
  }
679
708
  return { ...w, models: [...models, ...models.map(giftID)] }
680
709
  }))
710
+ g.windows.push(...pendingLines(g))
711
+ delete g.live
712
+ delete g.pending
681
713
  return g
682
714
  }
683
715
 
@@ -715,6 +747,12 @@ function bestBuckets(windows) {
715
747
  return best
716
748
  }
717
749
 
750
+ // giftReadable: ZCode's session can read the gift beside a coding plan,
751
+ // so the card asks for it even when no gift is live for requests: a plan
752
+ // that starts later is named on the card (windy, Discord), not left out
753
+ // because nothing could be spent yet.
754
+ const giftReadable = (s) => !!s.jwt && !jwtExpired(s.jwt)
755
+
718
756
  // dualUsage shows both pools but only the pool a request can use counts
719
757
  // for its model. Live gift models don't spend the Coding windows; spent
720
758
  // or blocked gift buckets stay visible without holding up the Coding Plan.
@@ -726,29 +764,32 @@ async function dualUsage(s, out) {
726
764
  const cached = routes.get(s.key + "\0" + s.jwt)
727
765
  try {
728
766
  const g = await giftOf(s)
729
- const gift = g ? { name: g.plan, models: [...new Set(g.windows.filter((w) => !w._spent)
767
+ const gift = g?.live ? { name: g.plan, models: [...new Set(g.windows.filter((w) => !w._spent)
730
768
  .flatMap((w) => w.models ?? []).map((m) => m.toLowerCase()))],
731
769
  allModels: uniqueModels(g.windows.flatMap((w) => w.models ?? [])) } : null
732
770
  if (cached) {
733
771
  cached.gift = gift
734
- const ends = g ? [g.until, ...g.windows.map((w) => w.resetsAt)].map((t) => Date.parse(t)).filter(Number.isFinite) : []
772
+ // a plan that starts later is read again when it starts
773
+ const ends = g ? [g.until, ...g.windows.map((w) => w.resetsAt)].map((t) => Date.parse(t))
774
+ .concat(g.pending.map((p) => p.start * 1000)).filter(Number.isFinite) : []
735
775
  if (ends.length) cached.ttl = Math.min(cached.ttl, Math.max(0, Math.min(...ends) - cached.at))
736
776
  }
737
- if (g) {
777
+ if (g?.live) {
738
778
  // the gift's models are its trial entries alone: a plain model stays
739
779
  // on the coding plan, and the user picks the gift by its entry
740
780
  const notModels = gift.allModels.map((m) => giftID(m).toLowerCase())
741
781
  if (notModels.length) out.windows = out.windows.map((w) => w.aside ? w : { ...w, notModels })
742
782
  // Several buckets may serve one model: one spent sibling cannot block it.
743
783
  const best = bestBuckets(g.windows)
744
- const label = byPlan(g.windows)
784
+ // beside the Coding Plan's windows every gift bucket is named after
785
+ // its plan, one plan or several: "Start Plan · GLM-5.1-Trial" can't
786
+ // be read as the Coding Plan's own (windy, Discord)
787
+ const label = (name, plan) => first(plan, "Start Plan") + " · " + name
745
788
  out.windows.push(...countedFirst(g.windows.map(({ _plan: p, _spent, ...w }, i) => {
746
789
  const selected = gift.allModels.filter((m) => best.get(m.toLowerCase()) === i)
747
790
  const models = selected.map(giftID)
748
791
  const scoped = models.length ? { ...w, models } : { ...w, aside: true }
749
- // the card names a bucket's model as the picker does (GLM-5.3-Flash-Trial);
750
- // no plan prefix while one plan gives them all — the card's section
751
- // header already says the account; with several, byPlan names each
792
+ // the card names a bucket's model as the picker does (GLM-5.3-Flash-Trial)
752
793
  let name = w.name
753
794
  for (const m of selected) {
754
795
  const at = name.toLowerCase().indexOf(m.toLowerCase())
@@ -757,6 +798,7 @@ async function dualUsage(s, out) {
757
798
  return { ...scoped, name: label(name, p) }
758
799
  })))
759
800
  }
801
+ if (g) out.windows.push(...pendingLines(g))
760
802
  } catch {
761
803
  if (cached) {
762
804
  cached.gift = null
@@ -2006,7 +2048,11 @@ export async function ZCodeAuthPlugin({ client }) {
2006
2048
  id: PROVIDER,
2007
2049
  // the account's plan's models, as ZCode's config lists them now
2008
2050
  async models(provider, { auth } = {}) {
2009
- const s = stateOf(auth)
2051
+ // a ZCode 3.16 sign-in holds only where its key is: read the key
2052
+ // first, as a request does, or plansOf takes the keyless sign-in
2053
+ // for a Start Plan one and lists that plan's models (#439). A key
2054
+ // that can't be read throws, and magpie keeps the list it had.
2055
+ const s = await located(stateOf(auth))
2010
2056
  if (!s) return provider.models
2011
2057
  const { start, gift: known } = await plansOf(s).catch(() => ({ start: false, gift: null }))
2012
2058
  const gift = start ? await startPlan(s.jwt, s.device).catch(() => null) : known
@@ -2256,8 +2302,8 @@ export async function ZCodeAuthPlugin({ client }) {
2256
2302
  }
2257
2303
  }
2258
2304
  const team = await teamUsage(s, key)
2259
- card = saved(gift ? await dualUsage(s, team) : team)
2260
- } else card = saved(start ? await startUsage(s) : gift ? await dualUsage(s) : await codingUsage(s))
2305
+ card = saved(gift || giftReadable(s) ? await dualUsage(s, team) : team)
2306
+ } else card = saved(start ? await startUsage(s) : gift || giftReadable(s) ? await dualUsage(s) : await codingUsage(s))
2261
2307
  // a gift plan ZCode holds for the account but has not put on it:
2262
2308
  // named on the card, claimed in the ZCode app (its claim needs
2263
2309
  // the app's captcha attestation, see claimHint). A card that is
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-zcode-auth",
3
- "version": "0.1.32",
3
+ "version": "0.1.34",
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",
5
5
  "type": "module",
6
6
  "main": "./index.mjs",