@magpie-community/opencode-zcode-auth 0.1.2 → 0.1.4

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 +32 -12
  2. package/package.json +1 -1
package/index.mjs CHANGED
@@ -39,7 +39,6 @@ const MODELS = [
39
39
  { id: "GLM-5-Turbo", context: 200_000, output: 64_000, efforts: ["none", "high"] },
40
40
  ]
41
41
  const START_MODELS = MODELS.filter((m) => m.id !== "GLM-5.3")
42
- const OUTPUT = 131_072 // when ZCode's config names none
43
42
 
44
43
  const SITES = {
45
44
  zai: { name: "Z.ai", api: ZAI_API, base: ZAI_BASE, subscribe: "z.ai/subscribe" },
@@ -121,7 +120,9 @@ const jwtExpired = (jwt) => {
121
120
  const t = jwtExpiry(jwt)
122
121
  return t > 0 && Date.now() > t
123
122
  }
124
- 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)"
125
126
 
126
127
  const num = (v) => {
127
128
  const n = typeof v === "string" ? parseFloat(v) : v
@@ -237,7 +238,7 @@ async function teamKey(s) {
237
238
  teamKeys.set(id, { key, at: Date.now() })
238
239
  return key
239
240
  } catch (e) {
240
- 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`)
241
242
  teamKeys.set(id, { err, at: Date.now() })
242
243
  throw err
243
244
  }
@@ -975,7 +976,8 @@ const entry = (m) => ({
975
976
  tool_call: true,
976
977
  attachment: !!m.image,
977
978
  modalities: { input: m.image ? ["text", "image"] : ["text"], output: ["text"] },
978
- limit: { context: m.context || 200_000, output: m.output || OUTPUT },
979
+ // what ZCode's config names, else 0 (unknown), as magpie's built-in
980
+ limit: { context: m.context || 0, output: m.output || 0 },
979
981
  cost: { input: 0, output: 0, cache_read: 0, cache_write: 0 },
980
982
  variants: variants(m.efforts),
981
983
  })
@@ -1093,15 +1095,33 @@ export async function ZCodeAuthPlugin({ client }) {
1093
1095
  h.delete("authorization")
1094
1096
  h.set("x-api-key", key)
1095
1097
  h.set("Authorization", "Bearer " + key)
1096
- return fetch(url, { ...opts, headers: h })
1098
+ const res = await fetch(url, { ...opts, headers: h })
1099
+ // the plan's answer goes on as it came, the account kept: the
1100
+ // built-in never marked a ZCode account lapsed nor cleared one
1101
+ const kept = new Headers(res.headers)
1102
+ kept.delete("content-length")
1103
+ kept.delete("content-encoding")
1104
+ kept.set("X-Magpie-Sign-In", "kept")
1105
+ return new Response(res.body, { status: res.status, statusText: res.statusText, headers: kept })
1097
1106
  },
1098
1107
  }
1099
1108
  },
1100
- // the plan's allowance, as magpie's built-in showed it
1109
+ // the plan's allowance, as magpie's built-in showed it. Each read
1110
+ // keeps the sign-in (signIn): the built-in's marked no account lapsed,
1111
+ // its expired Start Plan sign-in among them, and cleared none
1101
1112
  async usage(getAuth, provider) {
1102
1113
  const auth = await getAuth()
1103
1114
  const s = stateOf(auth)
1104
- if (!s) return { error: "not signed in" }
1115
+ if (!s) return { error: "not signed in", signIn: "kept" }
1116
+ // every card starts from the plan the sign-in saved, as magpie's
1117
+ // built-in did: a reply without a level, a team detail that
1118
+ // failed or an error keeps it
1119
+ const saved = (out) => {
1120
+ const plan = s.plan || auth?.plan
1121
+ if (plan && !out.plan) out.plan = plan
1122
+ out.signIn = "kept"
1123
+ return out
1124
+ }
1105
1125
  try {
1106
1126
  if (isTeam(s)) {
1107
1127
  let key = s.key
@@ -1112,12 +1132,12 @@ export async function ZCodeAuthPlugin({ client }) {
1112
1132
  await client?.auth?.set?.({ path: { id: provider?.id ?? PROVIDER }, body: next }).catch?.(() => {})
1113
1133
  }
1114
1134
  }
1115
- return await teamUsage(s, key)
1135
+ return saved(await teamUsage(s, key))
1116
1136
  }
1117
- if (await onStart(s)) return await startUsage(s)
1118
- return await codingUsage(s)
1137
+ if (await onStart(s)) return saved(await startUsage(s))
1138
+ return saved(await codingUsage(s))
1119
1139
  } catch (e) {
1120
- return { error: e?.message ?? String(e) }
1140
+ return saved({ error: e?.message ?? String(e) })
1121
1141
  }
1122
1142
  },
1123
1143
  methods: [
@@ -1145,4 +1165,4 @@ export async function ZCodeAuthPlugin({ client }) {
1145
1165
  }
1146
1166
 
1147
1167
  // for tests
1148
- export const _internal = { limitWindows, termOf, startUsage, routes, teamKeys, ownSignIn, stateOf }
1168
+ export const _internal = { entry, limitWindows, termOf, startUsage, routes, teamKeys, ownSignIn, stateOf }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-zcode-auth",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
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",