@magpie-community/opencode-zcode-auth 0.1.3 → 0.1.5

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 (2) hide show
  1. package/index.mjs +23 -6
  2. package/package.json +1 -1
package/index.mjs CHANGED
@@ -120,7 +120,9 @@ const jwtExpired = (jwt) => {
120
120
  const t = jwtExpiry(jwt)
121
121
  return t > 0 && Date.now() > t
122
122
  }
123
- const EXPIRED = "ZCode's sign-in has expired; sign in again"
123
+ // EXPIRED is the built-in's words (errZCodeExpired) for a Start Plan
124
+ // sign-in past its end.
125
+ const EXPIRED = "ZCode's sign-in has expired; sign in to ZCode again (or add the account again in magpie)"
124
126
 
125
127
  const num = (v) => {
126
128
  const n = typeof v === "string" ? parseFloat(v) : v
@@ -236,7 +238,7 @@ async function teamKey(s) {
236
238
  teamKeys.set(id, { key, at: Date.now() })
237
239
  return key
238
240
  } catch (e) {
239
- const err = new Error(`ZCode's team plan: ${e.message} — sign in again`)
241
+ const err = new Error(`ZCode's team plan: ${e.message} — sign in to ZCode again`)
240
242
  teamKeys.set(id, { err, at: Date.now() })
241
243
  throw err
242
244
  }
@@ -280,13 +282,18 @@ const isNum = (v) => typeof v === "number" && Number.isFinite(v)
280
282
 
281
283
  // limitWindows are /api/monitor/usage/quota/limit's limits as windows: what
282
284
  // is used of the whole when both are told (the whole less what remains, or
283
- // the current value), else the percentage given.
285
+ // the current value), else the percentage given. TIME_LIMIT is the month's
286
+ // MCP tool calls, which ZCode shows but never stops the models on, so it is
287
+ // set aside, as is a limit whose whole is told as 0: no cap (an older
288
+ // plan's), which the vendor may still give as 100% used.
284
289
  function limitWindows(d) {
285
290
  const out = []
286
291
  for (const x of d?.limits ?? []) {
287
292
  const span = spanOf(Math.trunc(Number(x?.unit) || 0), x?.number)
288
293
  const w = { name: windowName(span), used: 0 }
294
+ if (String(x?.type ?? "").toUpperCase() === "TIME_LIMIT") (w.name = "MCP · Month"), (w.aside = true)
289
295
  if (isNum(x?.percentage)) w.used = x.percentage
296
+ if (x?.usage === 0) (w.used = 0), (w.aside = true)
290
297
  if (isNum(x?.usage) && x.usage > 0) {
291
298
  const total = x.usage
292
299
  if (isNum(x.remaining)) {
@@ -1093,21 +1100,31 @@ export async function ZCodeAuthPlugin({ client }) {
1093
1100
  h.delete("authorization")
1094
1101
  h.set("x-api-key", key)
1095
1102
  h.set("Authorization", "Bearer " + key)
1096
- return fetch(url, { ...opts, headers: h })
1103
+ const res = await fetch(url, { ...opts, headers: h })
1104
+ // the plan's answer goes on as it came, the account kept: the
1105
+ // built-in never marked a ZCode account lapsed nor cleared one
1106
+ const kept = new Headers(res.headers)
1107
+ kept.delete("content-length")
1108
+ kept.delete("content-encoding")
1109
+ kept.set("X-Magpie-Sign-In", "kept")
1110
+ return new Response(res.body, { status: res.status, statusText: res.statusText, headers: kept })
1097
1111
  },
1098
1112
  }
1099
1113
  },
1100
- // the plan's allowance, as magpie's built-in showed it
1114
+ // the plan's allowance, as magpie's built-in showed it. Each read
1115
+ // keeps the sign-in (signIn): the built-in's marked no account lapsed,
1116
+ // its expired Start Plan sign-in among them, and cleared none
1101
1117
  async usage(getAuth, provider) {
1102
1118
  const auth = await getAuth()
1103
1119
  const s = stateOf(auth)
1104
- if (!s) return { error: "not signed in" }
1120
+ if (!s) return { error: "not signed in", signIn: "kept" }
1105
1121
  // every card starts from the plan the sign-in saved, as magpie's
1106
1122
  // built-in did: a reply without a level, a team detail that
1107
1123
  // failed or an error keeps it
1108
1124
  const saved = (out) => {
1109
1125
  const plan = s.plan || auth?.plan
1110
1126
  if (plan && !out.plan) out.plan = plan
1127
+ out.signIn = "kept"
1111
1128
  return out
1112
1129
  }
1113
1130
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-zcode-auth",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
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",