@magpie-community/opencode-zcode-auth 0.1.10 → 0.1.12

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 +12 -0
  2. package/index.mjs +46 -5
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -92,6 +92,18 @@ behind an error, so a gift hiccup must not black out a working Coding
92
92
  Plan. A gift that goes out of date while requests use it simply stops
93
93
  showing.
94
94
 
95
+ **Gift plans ZCode holds but has not granted.** ZCode hands out one-off
96
+ trust-build / gift plans (a day's ZCode Trust Build, say) that are not
97
+ on the account until the ZCode app claims them. The plugin reads the
98
+ app's own list of them (`GET /api/v1/zcode-plan/billing/preview`) with
99
+ the card and names them there — "1 to claim · claim it in the ZCode app"
100
+ — set aside, so the line stops nothing and routing, usage caps and the
101
+ menu bar pass it over. The plugin never claims one: the claim needs the
102
+ Aliyun captcha attestation only the app's renderer can make. A preview
103
+ that fails, or lists nothing, adds no line at all, and a card that is an
104
+ error keeps its error. Claim the plan in the ZCode app (open it once);
105
+ the next read shows it as an ordinary gift bucket.
106
+
95
107
  ## Models
96
108
 
97
109
  The models are the ones ZCode offers for the account's plan. They come
package/index.mjs CHANGED
@@ -366,7 +366,9 @@ const isNum = (v) => typeof v === "number" && Number.isFinite(v)
366
366
  // the current value), else the percentage given. TIME_LIMIT is the month's
367
367
  // MCP tool calls, which ZCode shows but never stops the models on, so it is
368
368
  // set aside, as is a limit whose whole is told as 0: no cap (an older
369
- // plan's), which the vendor may still give as 100% used.
369
+ // plan's), which the vendor may still give as 100% used. A window whose
370
+ // count is told carries it as amount of limit, so magpie's card says the
371
+ // count as used or as left, as it says the share beside it (#659).
370
372
  function limitWindows(d) {
371
373
  const out = []
372
374
  for (const x of d?.limits ?? []) {
@@ -380,9 +382,13 @@ function limitWindows(d) {
380
382
  if (isNum(x.remaining)) {
381
383
  const used = total - x.remaining
382
384
  w.used = (100 * used) / total
385
+ w.amount = used
386
+ w.limit = total
383
387
  w.display = `${compact(used)} / ${compact(total)}`
384
388
  } else if (isNum(x.currentValue)) {
385
389
  if (!isNum(x.percentage)) w.used = (100 * x.currentValue) / total
390
+ w.amount = x.currentValue
391
+ w.limit = total
386
392
  w.display = `${compact(x.currentValue)} / ${compact(total)}`
387
393
  }
388
394
  }
@@ -508,7 +514,11 @@ async function giftOf(s) {
508
514
  const w = { name: base, used: 0, _plan: str(owner.name), _spent: giftSpent(x) }
509
515
  if (used === undefined) used = total !== undefined && left !== undefined ? total - left : 0
510
516
  if (total > 0) {
517
+ // the bucket's count as amount of limit, as the Coding windows' is
518
+ // (#659): magpie says it as used or as left, as it says the share
511
519
  w.used = (100 * used) / total
520
+ w.amount = used
521
+ w.limit = total
512
522
  w.display = `${compact(used)} / ${compact(total)}`
513
523
  }
514
524
  if (w._spent) w.used = Math.max(100, w.used)
@@ -531,6 +541,30 @@ async function giftOf(s) {
531
541
  return out
532
542
  }
533
543
 
544
+ // claimHint is the card's line for gift plans ZCode is holding for the
545
+ // account but has not put on it: GET /billing/preview lists them, and
546
+ // only the ZCode app claims one — its claim takes the Aliyun captcha
547
+ // attestation the app's own renderer makes, so the plugin reads the list
548
+ // and asks the user to claim it there. A window, set aside (it is no
549
+ // allowance: using it up stops nothing), so the card shows it and
550
+ // routing, caps and the menu bar pass it over. null when there is
551
+ // nothing to claim, the preview failing or listing none among it: a
552
+ // failed read is no line at all, never a card or a refusal of its own.
553
+ async function claimHint(s) {
554
+ if (!s.jwt || jwtExpired(s.jwt)) return null
555
+ let d
556
+ try {
557
+ d = await call("GET", `${ZCODE}/api/v1/zcode-plan/billing/preview?app_version=${APP_VERSION}&platform=${platform()}`, { auth: "Bearer " + s.jwt, device: s.device })
558
+ } catch {
559
+ return null
560
+ }
561
+ const plans = (Array.isArray(d?.plans) ? d.plans : []).filter((p) => typeof p?.plan_id === "string" && p.plan_id.trim())
562
+ if (!plans.length) return null
563
+ const n = plans.length
564
+ const name = first(...plans.map((p) => (typeof p?.name === "string" ? p.name : "")), "ZCode gift plan")
565
+ return { name, used: 0, aside: true, display: `${n} to claim · claim ${n === 1 ? "it" : "them"} in the ZCode app` }
566
+ }
567
+
534
568
  // startUsage is a gift-only account's card: its buckets, or the words
535
569
  // for having none to show. The card's header already names the plan, so
536
570
  // the windows keep their own names.
@@ -1523,9 +1557,16 @@ export async function ZCodeAuthPlugin({ client }) {
1523
1557
  return saved(await teamUsage(s, key))
1524
1558
  }
1525
1559
  const { start, gift } = await plansOf(s)
1526
- if (start) return saved(await startUsage(s))
1527
- if (gift) return saved(await dualUsage(s))
1528
- return saved(await codingUsage(s))
1560
+ const card = saved(start ? await startUsage(s) : gift ? await dualUsage(s) : await codingUsage(s))
1561
+ // a gift plan ZCode holds for the account but has not put on it:
1562
+ // named on the card, claimed in the ZCode app (its claim needs
1563
+ // the app's captcha attestation, see claimHint). A card that is
1564
+ // an error keeps its error and asks nothing
1565
+ if (!card.error) {
1566
+ const hint = await claimHint(s)
1567
+ if (hint) card.windows = [...(card.windows ?? []), hint]
1568
+ }
1569
+ return card
1529
1570
  } catch (e) {
1530
1571
  return saved({ error: e?.message ?? String(e) })
1531
1572
  }
@@ -1555,4 +1596,4 @@ export async function ZCodeAuthPlugin({ client }) {
1555
1596
  }
1556
1597
 
1557
1598
  // for tests
1558
- export const _internal = { entry, limitWindows, termOf, startUsage, routes, blocked, plansOf, giftOf, dualUsage, spentUp, giftServes, modelOf, teamKeys, ownSignIn, stateOf, dress, PROMPT }
1599
+ export const _internal = { entry, limitWindows, termOf, startUsage, routes, blocked, plansOf, giftOf, dualUsage, claimHint, spentUp, giftServes, modelOf, teamKeys, ownSignIn, stateOf, dress, PROMPT }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-zcode-auth",
3
- "version": "0.1.10",
3
+ "version": "0.1.12",
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",