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

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 +35 -4
  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
@@ -531,6 +531,30 @@ async function giftOf(s) {
531
531
  return out
532
532
  }
533
533
 
534
+ // claimHint is the card's line for gift plans ZCode is holding for the
535
+ // account but has not put on it: GET /billing/preview lists them, and
536
+ // only the ZCode app claims one — its claim takes the Aliyun captcha
537
+ // attestation the app's own renderer makes, so the plugin reads the list
538
+ // and asks the user to claim it there. A window, set aside (it is no
539
+ // allowance: using it up stops nothing), so the card shows it and
540
+ // routing, caps and the menu bar pass it over. null when there is
541
+ // nothing to claim, the preview failing or listing none among it: a
542
+ // failed read is no line at all, never a card or a refusal of its own.
543
+ async function claimHint(s) {
544
+ if (!s.jwt || jwtExpired(s.jwt)) return null
545
+ let d
546
+ try {
547
+ d = await call("GET", `${ZCODE}/api/v1/zcode-plan/billing/preview?app_version=${APP_VERSION}&platform=${platform()}`, { auth: "Bearer " + s.jwt, device: s.device })
548
+ } catch {
549
+ return null
550
+ }
551
+ const plans = (Array.isArray(d?.plans) ? d.plans : []).filter((p) => typeof p?.plan_id === "string" && p.plan_id.trim())
552
+ if (!plans.length) return null
553
+ const n = plans.length
554
+ const name = first(...plans.map((p) => (typeof p?.name === "string" ? p.name : "")), "ZCode gift plan")
555
+ return { name, used: 0, aside: true, display: `${n} to claim · claim ${n === 1 ? "it" : "them"} in the ZCode app` }
556
+ }
557
+
534
558
  // startUsage is a gift-only account's card: its buckets, or the words
535
559
  // for having none to show. The card's header already names the plan, so
536
560
  // the windows keep their own names.
@@ -1523,9 +1547,16 @@ export async function ZCodeAuthPlugin({ client }) {
1523
1547
  return saved(await teamUsage(s, key))
1524
1548
  }
1525
1549
  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))
1550
+ const card = saved(start ? await startUsage(s) : gift ? await dualUsage(s) : await codingUsage(s))
1551
+ // a gift plan ZCode holds for the account but has not put on it:
1552
+ // named on the card, claimed in the ZCode app (its claim needs
1553
+ // the app's captcha attestation, see claimHint). A card that is
1554
+ // an error keeps its error and asks nothing
1555
+ if (!card.error) {
1556
+ const hint = await claimHint(s)
1557
+ if (hint) card.windows = [...(card.windows ?? []), hint]
1558
+ }
1559
+ return card
1529
1560
  } catch (e) {
1530
1561
  return saved({ error: e?.message ?? String(e) })
1531
1562
  }
@@ -1555,4 +1586,4 @@ export async function ZCodeAuthPlugin({ client }) {
1555
1586
  }
1556
1587
 
1557
1588
  // for tests
1558
- export const _internal = { entry, limitWindows, termOf, startUsage, routes, blocked, plansOf, giftOf, dualUsage, spentUp, giftServes, modelOf, teamKeys, ownSignIn, stateOf, dress, PROMPT }
1589
+ 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.11",
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",