@magpie-community/opencode-zcode-auth 0.1.29 → 0.1.30

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 +6 -1
  2. package/index.mjs +46 -4
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -25,7 +25,12 @@ over Anthropic's Messages API. Provider id: `zcode`.
25
25
  agent's system prompt at the end of the third (a fourth block is
26
26
  refused, #343), the day in a `<system-reminder>` turn before the agent's
27
27
  turns, and the sign-in's device id in `metadata.user_id`. GLM Coding
28
- Plan and team requests go as the agent sent them. zcode.z.ai also
28
+ Plan and team requests go as the agent sent them, but for that same
29
+ `metadata.user_id` when the agent sent none: ZCode puts it on every
30
+ request, and the plan's endpoint keeps a prompt cache on the machine it
31
+ picks by it, so without it the cache is mostly missed (#387). An
32
+ account known only by its key gets a device id made from the key, the
33
+ same on every start. zcode.z.ai also
29
34
  answers 405 code 3012 to every request of an account its risk control
30
35
  blocked, ZCode's own app's included (plugins#348). That answer comes
31
36
  back as ZCode's words, then "ZCode's risk control blocked this account
package/index.mjs CHANGED
@@ -50,6 +50,12 @@ const siteOf = (s) => (s === "bigmodel" ? "bigmodel" : "zai")
50
50
  const platform = () => `${process.platform}-${process.arch}` // darwin-arm64, win32-x64, linux-x64, as ZCode names them
51
51
  const uuid = () => crypto.randomUUID()
52
52
  const anyDevice = uuid() // X-Device-Mid for calls made signed out (the model list)
53
+ // keyDevice is a device id for an account known only by its key: the same
54
+ // key gives the same id on every start, as a sign-in's saved device does
55
+ const keyDevice = (key) => {
56
+ const h = createHash("sha256").update("zcode-device\0" + key).digest("hex")
57
+ return `${h.slice(0, 8)}-${h.slice(8, 12)}-4${h.slice(13, 16)}-${((parseInt(h[16], 16) & 3) | 8).toString(16)}${h.slice(17, 20)}-${h.slice(20, 32)}`
58
+ }
53
59
  const first = (...ss) => ss.map((s) => (typeof s === "string" ? s.trim() : "")).find((s) => s) ?? ""
54
60
 
55
61
  // STATUS_TEXT is Go's http.StatusText, which magpie's built-in said a
@@ -1265,11 +1271,13 @@ function stateOf(auth) {
1265
1271
  }
1266
1272
  }
1267
1273
  const site = siteOf(s.site)
1268
- return { ...s, site, base: s.base || SITES[site].base, key: s.key ?? (s.jwt ? "" : auth.access), device: s.device || anyDevice }
1274
+ const key = s.key ?? (s.jwt ? "" : auth.access)
1275
+ return { ...s, site, base: s.base || SITES[site].base, key, device: s.device || (key ? keyDevice(key) : anyDevice) }
1269
1276
  }
1270
1277
  if (auth?.type === "api") {
1271
1278
  const site = siteOf(auth.metadata?.site)
1272
- return { site, base: SITES[site].base, key: String(auth.key ?? "").trim(), device: anyDevice }
1279
+ const key = String(auth.key ?? "").trim()
1280
+ return { site, base: SITES[site].base, key, device: key ? keyDevice(key) : anyDevice }
1273
1281
  }
1274
1282
  return null
1275
1283
  }
@@ -1764,7 +1772,32 @@ function dress(text, site, device, place) {
1764
1772
  }
1765
1773
  if (Array.isArray(b.tools)) for (const t of b.tools) if (t && typeof t === "object") delete t.cache_control
1766
1774
  const meta = b.metadata && typeof b.metadata === "object" ? b.metadata : {}
1767
- b.metadata = { ...meta, user_id: JSON.stringify({ device_id: device, account_uuid: "", session_id: "" }) }
1775
+ b.metadata = { ...meta, user_id: userIdOf(device) }
1776
+ return JSON.stringify(b)
1777
+ }
1778
+
1779
+ // userIdOf is metadata.user_id as ZCode's client (3.14.3, glm/zcode.cjs
1780
+ // MLs and vWr) puts it on every Anthropic-format request, a coding plan's
1781
+ // too: the machine's device id, no account uuid. The plan's endpoint keeps
1782
+ // a prompt cache on the machine it picks by it, so a request without it
1783
+ // lands anywhere and its cache is mostly missed (#387).
1784
+ const userIdOf = (device) => JSON.stringify({ device_id: device, account_uuid: "", session_id: "" })
1785
+
1786
+ // marked is a coding plan's Messages request body with metadata.user_id
1787
+ // as ZCode sends it, when the agent sent none of its own. Anything else,
1788
+ // a body that isn't a Messages request among them, goes as it is.
1789
+ function marked(text, device) {
1790
+ if (typeof text !== "string" || !device || !text.includes("messages")) return text
1791
+ let b
1792
+ try {
1793
+ b = JSON.parse(text)
1794
+ } catch {
1795
+ return text
1796
+ }
1797
+ if (!b || typeof b !== "object" || Array.isArray(b) || !Array.isArray(b.messages)) return text
1798
+ const meta = b.metadata && typeof b.metadata === "object" && !Array.isArray(b.metadata) ? b.metadata : {}
1799
+ if (typeof meta.user_id === "string" && meta.user_id.trim()) return text
1800
+ b.metadata = { ...meta, user_id: userIdOf(device) }
1768
1801
  return JSON.stringify(b)
1769
1802
  }
1770
1803
 
@@ -1917,6 +1950,15 @@ export async function ZCodeAuthPlugin({ client }) {
1917
1950
  } else {
1918
1951
  if (!key) throw new Error("ZCode's sign-in has no key; sign in again")
1919
1952
  swap(s.base)
1953
+ // a Messages request names the device, as ZCode's does (#387);
1954
+ // count_tokens and other paths ZCode never sends go as they are
1955
+ if (opts.body != null && /\/v1\/messages$/.test(new URL(url).pathname)) {
1956
+ const text = typeof opts.body === "string" ? opts.body : await new Response(opts.body).text()
1957
+ const next = marked(text, s.device)
1958
+ if (next !== text) h.delete("content-length")
1959
+ opts.body = next
1960
+ delete opts.duplex
1961
+ }
1920
1962
  if (renamed) h.delete("content-length")
1921
1963
  h.delete("authorization")
1922
1964
  h.set("x-api-key", key)
@@ -2070,4 +2112,4 @@ export async function ZCodeAuthPlugin({ client }) {
2070
2112
  // config sets what zcodeConfig has read, for tests: null is ZCode's config
2071
2113
  // out of reach, undefined forgets it
2072
2114
  const config = (cfg) => (cfgCache = cfg === undefined ? null : { cfg, at: Date.now() })
2073
- export const _internal = { config, entry, limitWindows, termOf, startUsage, routes, blocked, plansOf, giftOf, dualUsage, claimHint, spentUp, giftServes, modelOf, teamKeys, ownSignIn, stateOf, dress, PROMPT }
2115
+ export const _internal = { config, entry, limitWindows, termOf, startUsage, routes, blocked, plansOf, giftOf, dualUsage, claimHint, spentUp, giftServes, modelOf, teamKeys, ownSignIn, stateOf, dress, PROMPT, marked, keyDevice }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-zcode-auth",
3
- "version": "0.1.29",
3
+ "version": "0.1.30",
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",