@magpie-community/opencode-zcode-auth 0.1.28 → 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.
- package/README.md +6 -1
- package/index.mjs +57 -9
- 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
|
|
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
|
|
@@ -1001,6 +1007,10 @@ async function signedIn(site, token, jwt, device, onTeam = false) {
|
|
|
1001
1007
|
// TEAM marks a team seat's account name beside the account's own.
|
|
1002
1008
|
const TEAM = "team"
|
|
1003
1009
|
const who = (u) => first(String(u?.email ?? "").replace(/@phone\.local$/, ""), u?.name, u?.user_id, "ZCode")
|
|
1010
|
+
// uidOf is the account's user id, kept beside its name: magpie tells two
|
|
1011
|
+
// accounts of one name apart by it, a name being no one's own (wakkana on
|
|
1012
|
+
// Discord, WorkBuddy's)
|
|
1013
|
+
const uidOf = (u) => (u?.user_id === undefined || u?.user_id === null ? "" : String(u.user_id).trim())
|
|
1004
1014
|
const sleep = (ms) => new Promise((r) => setTimeout(r, ms))
|
|
1005
1015
|
|
|
1006
1016
|
// authorize opens ZCode's sign-in flow on site; its callback polls it.
|
|
@@ -1059,6 +1069,7 @@ async function authorize(site, log, onTeam = false) {
|
|
|
1059
1069
|
// the key doesn't run out; ZCode's token, when it is all there is, does
|
|
1060
1070
|
expires: s.key || isTeam(s) ? 0 : jwtExpiry(s.jwt),
|
|
1061
1071
|
accountId: onTeam ? `${who(got.user)} (${TEAM})` : who(got.user),
|
|
1072
|
+
...(uidOf(got.user) ? { uid: uidOf(got.user) } : {}),
|
|
1062
1073
|
}
|
|
1063
1074
|
}
|
|
1064
1075
|
},
|
|
@@ -1220,7 +1231,7 @@ function ownSignIn() {
|
|
|
1220
1231
|
} catch {}
|
|
1221
1232
|
}
|
|
1222
1233
|
}
|
|
1223
|
-
return { ...s, user: who(info) }
|
|
1234
|
+
return { ...s, user: who(info), uid: uidOf(info) }
|
|
1224
1235
|
}
|
|
1225
1236
|
|
|
1226
1237
|
// asZCode is the sign-in "as ZCode": what it reads is ZCode's own.
|
|
@@ -1232,9 +1243,9 @@ function asZCode() {
|
|
|
1232
1243
|
async callback() {
|
|
1233
1244
|
const own = ownSignIn()
|
|
1234
1245
|
if (!own) return { type: "failed", error: "ZCode isn't signed in on this computer; sign in to ZCode, or sign in to Z.ai or BigModel here" }
|
|
1235
|
-
const { user, ...s } = own
|
|
1246
|
+
const { user, uid, ...s } = own
|
|
1236
1247
|
const state = { source: "zcode", device: anyDevice, ...s }
|
|
1237
|
-
return { type: "success", refresh: JSON.stringify(state), access: s.key || s.jwt || "", expires: 0, accountId: user }
|
|
1248
|
+
return { type: "success", refresh: JSON.stringify(state), access: s.key || s.jwt || "", expires: 0, accountId: user, ...(uid ? { uid } : {}) }
|
|
1238
1249
|
},
|
|
1239
1250
|
}
|
|
1240
1251
|
}
|
|
@@ -1253,17 +1264,20 @@ function stateOf(auth) {
|
|
|
1253
1264
|
// send this account's requests as that one, and an expired session
|
|
1254
1265
|
// says sign in again (EXPIRED) rather than borrowing another's.
|
|
1255
1266
|
const own = ownSignIn()
|
|
1256
|
-
|
|
1257
|
-
|
|
1267
|
+
// (by user id where both have one: another account may share the name)
|
|
1268
|
+
if (own && own.user === String(auth.accountId ?? "") && (!own.uid || !auth.uid || own.uid === String(auth.uid))) {
|
|
1269
|
+
const { user, uid: _uid, ...now } = own
|
|
1258
1270
|
s = { source: "zcode", device: s.device, plan: s.plan, ...now }
|
|
1259
1271
|
}
|
|
1260
1272
|
}
|
|
1261
1273
|
const site = siteOf(s.site)
|
|
1262
|
-
|
|
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) }
|
|
1263
1276
|
}
|
|
1264
1277
|
if (auth?.type === "api") {
|
|
1265
1278
|
const site = siteOf(auth.metadata?.site)
|
|
1266
|
-
|
|
1279
|
+
const key = String(auth.key ?? "").trim()
|
|
1280
|
+
return { site, base: SITES[site].base, key, device: key ? keyDevice(key) : anyDevice }
|
|
1267
1281
|
}
|
|
1268
1282
|
return null
|
|
1269
1283
|
}
|
|
@@ -1758,7 +1772,32 @@ function dress(text, site, device, place) {
|
|
|
1758
1772
|
}
|
|
1759
1773
|
if (Array.isArray(b.tools)) for (const t of b.tools) if (t && typeof t === "object") delete t.cache_control
|
|
1760
1774
|
const meta = b.metadata && typeof b.metadata === "object" ? b.metadata : {}
|
|
1761
|
-
b.metadata = { ...meta, user_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) }
|
|
1762
1801
|
return JSON.stringify(b)
|
|
1763
1802
|
}
|
|
1764
1803
|
|
|
@@ -1911,6 +1950,15 @@ export async function ZCodeAuthPlugin({ client }) {
|
|
|
1911
1950
|
} else {
|
|
1912
1951
|
if (!key) throw new Error("ZCode's sign-in has no key; sign in again")
|
|
1913
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
|
+
}
|
|
1914
1962
|
if (renamed) h.delete("content-length")
|
|
1915
1963
|
h.delete("authorization")
|
|
1916
1964
|
h.set("x-api-key", key)
|
|
@@ -2064,4 +2112,4 @@ export async function ZCodeAuthPlugin({ client }) {
|
|
|
2064
2112
|
// config sets what zcodeConfig has read, for tests: null is ZCode's config
|
|
2065
2113
|
// out of reach, undefined forgets it
|
|
2066
2114
|
const config = (cfg) => (cfgCache = cfg === undefined ? null : { cfg, at: Date.now() })
|
|
2067
|
-
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.
|
|
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",
|