@magpie-community/opencode-zcode-auth 0.1.58 → 0.1.59

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 +2 -0
  2. package/index.mjs +73 -1
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -165,6 +165,8 @@ keeps the plain models on the plan. The JSON below still works, and a
165
165
  `freeFirst: false` there keeps it off whatever the switch says. Turn it
166
166
  off for the whole plugin in magpie's Plugins › Options
167
167
 
168
+ Since 0.1.59 a reply the gift answered says its model as the picker lists it (`GLM-5.3-Flash-Trial`), whether free first or a `-Trial` pick sent the request, so the Usage page, the sessions and the request log show which route spent the turn; a reply the coding plan answered, a free-first replay among them, keeps the plan's name.
169
+
168
170
  ```json
169
171
  { "freeFirst": false }
170
172
  ```
package/index.mjs CHANGED
@@ -2575,6 +2575,69 @@ function searchNamed(res) {
2575
2575
  })
2576
2576
  }
2577
2577
 
2578
+ // trialNamed is a gift's answer with its model said as the picker lists it
2579
+ // (GLM-5.3-Flash-Trial): the gift answers under the plain name, as the coding
2580
+ // plan does, so the Usage page, the sessions and the request log named the
2581
+ // plan's model whichever route served it, and a user with free first on
2582
+ // could not tell whether the free quota was spent (WJaunita45604 on X). A
2583
+ // failed answer, or a body that is not JSON or an event stream, goes on as it
2584
+ // came.
2585
+ function trialNamed(res, name) {
2586
+ if (!res.body || res.status >= 400) return res
2587
+ const type = res.headers.get("content-type") ?? ""
2588
+ const set = (d) => {
2589
+ if (d && typeof d === "object") {
2590
+ if (typeof d.model === "string") d.model = name
2591
+ if (typeof d.message?.model === "string") d.message.model = name
2592
+ return true
2593
+ }
2594
+ return false
2595
+ }
2596
+ const line = (l) => {
2597
+ if (!l.startsWith("data:") || !l.includes('"model"')) return l
2598
+ try {
2599
+ const d = JSON.parse(l.slice(5))
2600
+ return set(d) ? "data: " + JSON.stringify(d) : l
2601
+ } catch {
2602
+ return l
2603
+ }
2604
+ }
2605
+ const h = new Headers(res.headers)
2606
+ if (type.includes("text/event-stream")) {
2607
+ const dec = new TextDecoder()
2608
+ const enc = new TextEncoder()
2609
+ let held = ""
2610
+ const body = res.body.pipeThrough(
2611
+ new TransformStream({
2612
+ transform(chunk, ctl) {
2613
+ held += dec.decode(chunk, { stream: true })
2614
+ const i = held.lastIndexOf("\n")
2615
+ if (i < 0) return
2616
+ ctl.enqueue(enc.encode(held.slice(0, i + 1).split("\n").map(line).join("\n")))
2617
+ held = held.slice(i + 1)
2618
+ },
2619
+ flush(ctl) {
2620
+ held += dec.decode()
2621
+ if (held) ctl.enqueue(enc.encode(line(held)))
2622
+ },
2623
+ }),
2624
+ )
2625
+ return new Response(body, { status: res.status, statusText: res.statusText, headers: h })
2626
+ }
2627
+ if (!type.includes("json")) return res
2628
+ return res.text().then((text) => {
2629
+ let out = text
2630
+ if (text.includes('"model"')) {
2631
+ try {
2632
+ const v = JSON.parse(text)
2633
+ if (set(v)) out = JSON.stringify(v)
2634
+ } catch {}
2635
+ }
2636
+ h.delete("content-length")
2637
+ return new Response(out, { status: res.status, statusText: res.statusText, headers: h })
2638
+ })
2639
+ }
2640
+
2578
2641
  // startURL is a Start Plan request's URL without the query the agent put
2579
2642
  // on it: Claude Code asks /v1/messages?beta=true, which magpie passes on,
2580
2643
  // and ZCode asks /v1/messages alone.
@@ -2917,9 +2980,15 @@ export async function ZCodeAuthPlugin({ client, magpie } = {}, options = {}) {
2917
2980
  // says it is spent can spend nothing and replay what was asked for
2918
2981
  let start = alone
2919
2982
  let orig, forcedGift = false, freeGift = false
2983
+ // the gift's -Trial name while the gift is the route that answers
2984
+ // (a free-first request replayed on the plan clears it)
2985
+ let giftName = ""
2920
2986
  let asked = ""
2921
2987
  let renamed = false
2922
- const done = (res) => (renamed ? searchNamed(res) : res)
2988
+ const done = (res) => {
2989
+ if (giftName) res = trialNamed(res, giftName)
2990
+ return renamed ? searchNamed(res) : res
2991
+ }
2923
2992
  if (opts.body != null) {
2924
2993
  orig = opts.body = typeof opts.body === "string" ? opts.body : await new Response(opts.body).text()
2925
2994
  delete opts.duplex
@@ -2943,6 +3012,7 @@ export async function ZCodeAuthPlugin({ client, magpie } = {}, options = {}) {
2943
3012
  orig = opts.body = JSON.stringify({ ...JSON.parse(orig), model: wireModel(raw) })
2944
3013
  m = raw
2945
3014
  forcedGift = true
3015
+ giftName = giftID(raw)
2946
3016
  } else if (!alone && !edgeHeld() && !busyHeld() && !startFull(gateKey) && freeFirst(provider?.options) && s.jwt && known && giftUsable(s, known, m) && !giftSidelined(s, m)) {
2947
3017
  // free first: the plain model spends the account's own live
2948
3018
  // gift before it spends the coding plan, under the name the
@@ -2962,6 +3032,7 @@ export async function ZCodeAuthPlugin({ client, magpie } = {}, options = {}) {
2962
3032
  orig = opts.body = JSON.stringify({ ...JSON.parse(orig), model: wireModel(raw) })
2963
3033
  m = raw
2964
3034
  freeGift = true
3035
+ giftName = giftID(raw)
2965
3036
  }
2966
3037
  start = alone || forcedGift || freeGift
2967
3038
  }
@@ -2994,6 +3065,7 @@ export async function ZCodeAuthPlugin({ client, magpie } = {}, options = {}) {
2994
3065
  const replayOnPlan = async () => {
2995
3066
  // the gift is done with: its slot goes to the next request
2996
3067
  own.slot?.release()
3068
+ giftName = ""
2997
3069
  let u = askedUrl
2998
3070
  for (const b of [START_BASE, ZAI_BASE, BIGMODEL_BASE]) {
2999
3071
  if (u.startsWith(b)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-zcode-auth",
3
- "version": "0.1.58",
3
+ "version": "0.1.59",
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",