@magpie-community/opencode-zcode-auth 0.1.54 → 0.1.55
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 +11 -1
- package/index.mjs +86 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -31,7 +31,10 @@ over Anthropic's Messages API. Provider id: `zcode`.
|
|
|
31
31
|
picks by it, so without it the cache is mostly missed (#387). An
|
|
32
32
|
account known only by its key gets a device id made from the key, the
|
|
33
33
|
same on every start. Every request also carries the `x-session-id`
|
|
34
|
-
header below.
|
|
34
|
+
header below. ZCode's headers are the desktop app's (3.17.2):
|
|
35
|
+
`X-Title: Z Code@electron`, and an `x-query-id` per user turn, a UUID
|
|
36
|
+
that stays the same through the turn's tool results and is new with
|
|
37
|
+
the next user message, as ZCode makes one per query. zcode.z.ai also
|
|
35
38
|
answers 405 code 3012 to every request of an account its risk control
|
|
36
39
|
blocked, ZCode's own app's included (plugins#348). That answer comes
|
|
37
40
|
back as ZCode's words, then "ZCode's risk control blocked this account
|
|
@@ -326,6 +329,13 @@ the body's code, not the HTTP status:
|
|
|
326
329
|
read is *failed*, never nothing to claim;
|
|
327
330
|
- code 0: *claimed* (the next request uses the plan);
|
|
328
331
|
- code 1003 "target already exists" (HTTP 200): *done*, claimed before;
|
|
332
|
+
- ZCode's own client config (`GET /api/v1/client/configs`, read at most
|
|
333
|
+
every ten minutes) says whether a claim needs its security check
|
|
334
|
+
(`captcha.enabled`). While it does, which is so since 11-07, nothing is
|
|
335
|
+
posted: every plan answers *captcha*, "claim it in the ZCode app", since
|
|
336
|
+
a claim without the app's proof only spends one of ZCode's refusals. A
|
|
337
|
+
config that doesn't read answers *failed*, and nothing is posted then
|
|
338
|
+
either;
|
|
329
339
|
- code 3007 "captcha verify failed": *captcha*, the plan is claimed in
|
|
330
340
|
the ZCode app — the claim is gated on the Aliyun captcha the app's
|
|
331
341
|
renderer answers, and a claim from outside the app is refused it
|
package/index.mjs
CHANGED
|
@@ -789,6 +789,23 @@ function noteRateLimit(key, saidMs, message) {
|
|
|
789
789
|
}
|
|
790
790
|
const CAPTCHA_WORDS = /captcha/i
|
|
791
791
|
|
|
792
|
+
// claimNeedsCaptcha says whether ZCode's claims want its security check
|
|
793
|
+
// now, from what client/configs says: its captcha (Aliyun's, which the
|
|
794
|
+
// ZCode app passes before every claim, invisibly as a rule, and sends as
|
|
795
|
+
// X-Aliyun-Captcha-Verify-Param, 3.17.2's requestVerificationClaimHeaders)
|
|
796
|
+
// switched on. No captcha in the configs is no check, as the app reads it
|
|
797
|
+
// (readRequestVerificationConfig gives null, and the claim goes without
|
|
798
|
+
// the header); one that isn't an object is unknown, null.
|
|
799
|
+
// skip_model_request is the same check for Start Plan requests, which it
|
|
800
|
+
// spares (true on 11-07); it is not read here.
|
|
801
|
+
function claimNeedsCaptcha(c) {
|
|
802
|
+
const k = c?.configs?.captcha
|
|
803
|
+
if (k == null) return false
|
|
804
|
+
if (typeof k !== "object" || Array.isArray(k)) return null
|
|
805
|
+
return k.enabled !== false
|
|
806
|
+
}
|
|
807
|
+
const CAPTCHA_CLAIM = "claim it in the ZCode app: ZCode asks its security check before a claim, which only the app passes, so the daily check-in leaves it to the app"
|
|
808
|
+
|
|
792
809
|
// The marketing feed and the claim are gated on the client's version:
|
|
793
810
|
// under an older version's header the feed answers empty (11-03, #678:
|
|
794
811
|
// the day's banner showed for 3.17.2 and for no older one), so they go,
|
|
@@ -977,6 +994,24 @@ async function claimDaily(s) {
|
|
|
977
994
|
? { outcome: "failed", message: unread.join("; ") }
|
|
978
995
|
: { outcome: "inactive", message: "no ZCode gift plan to claim", until: recheck() }
|
|
979
996
|
}
|
|
997
|
+
// a claim ZCode guards with its security check is not posted: the app
|
|
998
|
+
// passes it before each claim and sends the proof in a header, which
|
|
999
|
+
// this plugin can't make, and a claim without it only gets 3007 again
|
|
1000
|
+
// (young02036 on Discord, 11-07: asked every day, never claimed). The
|
|
1001
|
+
// plans are named, to claim in the ZCode app. A config that doesn't
|
|
1002
|
+
// read leaves it unknown: nothing is posted, and it is asked again.
|
|
1003
|
+
let guarded
|
|
1004
|
+
try {
|
|
1005
|
+
guarded = claimNeedsCaptcha(await clientConfigs())
|
|
1006
|
+
} catch (e) {
|
|
1007
|
+
guarded = null
|
|
1008
|
+
unread.push(`ZCode's client config: ${e?.message ?? String(e)}`)
|
|
1009
|
+
}
|
|
1010
|
+
if (guarded !== false) {
|
|
1011
|
+
rateWait.delete(holdKey(s))
|
|
1012
|
+
if (guarded === null) return { outcome: "failed", message: [...plans.map((p) => `${p.name}: not claimed, ZCode's security check for claims can't be read`), ...unread].join("; ") }
|
|
1013
|
+
return { outcome: "captcha", message: plans.map((p) => `${p.name}: ${CAPTCHA_CLAIM}`).join("; ") }
|
|
1014
|
+
}
|
|
980
1015
|
const got = []
|
|
981
1016
|
for (const p of plans) got.push({ name: p.name, ...(await claimOne(s, p.plan_id)) })
|
|
982
1017
|
const named = (o) => got.filter((g) => g.outcome === o).map((g) => g.name)
|
|
@@ -1833,7 +1868,7 @@ function localConfig() {
|
|
|
1833
1868
|
// remoteConfig is the release of ZCode's provider config zcode.z.ai names now.
|
|
1834
1869
|
async function remoteConfig() {
|
|
1835
1870
|
const signal = AbortSignal.timeout(8000)
|
|
1836
|
-
const c = await
|
|
1871
|
+
const c = await clientConfigs()
|
|
1837
1872
|
const url = c?.configs?.builtin_provider_config_json
|
|
1838
1873
|
if (!String(url ?? "").startsWith("https://")) throw new Error("ZCode's configs name no provider config")
|
|
1839
1874
|
const res = await fetch(url, { headers: { "User-Agent": UA }, signal })
|
|
@@ -1841,6 +1876,23 @@ async function remoteConfig() {
|
|
|
1841
1876
|
return res.json()
|
|
1842
1877
|
}
|
|
1843
1878
|
|
|
1879
|
+
// clientConfigs is what zcode.z.ai tells this client version
|
|
1880
|
+
// (/api/v1/client/configs): the provider config's address, and the
|
|
1881
|
+
// captcha its claims and requests need (claimNeedsCaptcha). A read that
|
|
1882
|
+
// came through is kept for 10 minutes; a failed one is not kept, and
|
|
1883
|
+
// throws.
|
|
1884
|
+
let clientCache = null
|
|
1885
|
+
async function clientConfigs() {
|
|
1886
|
+
if (clientCache && Date.now() - clientCache.at < 600_000) return clientCache.c
|
|
1887
|
+
const c = await call("GET", `${ZCODE}/api/v1/client/configs?app_version=${APP_VERSION}&platform=${platform()}`, { signal: AbortSignal.timeout(8000) })
|
|
1888
|
+
clientCache = { c, at: Date.now() }
|
|
1889
|
+
return c
|
|
1890
|
+
}
|
|
1891
|
+
// resetClientConfigs is for tests: the next read asks zcode.z.ai again.
|
|
1892
|
+
export function resetClientConfigs() {
|
|
1893
|
+
clientCache = null
|
|
1894
|
+
}
|
|
1895
|
+
|
|
1844
1896
|
// config is ZCode's provider config, the later of zcode.z.ai's and an
|
|
1845
1897
|
// installed ZCode's, kept for 10 minutes.
|
|
1846
1898
|
let cfgCache = null
|
|
@@ -2182,7 +2234,12 @@ function startHeaders(h, jwt) {
|
|
|
2182
2234
|
out.set("HTTP-Referer", ZCODE)
|
|
2183
2235
|
out.set("User-Agent", UA + " ai-sdk/anthropic/3.0.81")
|
|
2184
2236
|
out.set("X-ZCode-App-Version", APP_VERSION)
|
|
2185
|
-
|
|
2237
|
+
// the desktop app's agent names itself electron: 3.17.2's
|
|
2238
|
+
// zcode-protocol-entrypoint passes sourceTitle "electron", and a bare
|
|
2239
|
+
// run under app-server says the same (Mvc in glm/zcode.cjs); "cli" is
|
|
2240
|
+
// the zcode command line's alone, which sends no X-Device-Mid on the
|
|
2241
|
+
// app's calls and no desktop prompt texts, as this plugin does
|
|
2242
|
+
out.set("X-Title", "Z Code@electron")
|
|
2186
2243
|
out.set("X-Release-Channel", "production")
|
|
2187
2244
|
const o = intl()
|
|
2188
2245
|
out.set("X-Client-Language", o.locale || "unknown")
|
|
@@ -2201,6 +2258,30 @@ function startHeaders(h, jwt) {
|
|
|
2201
2258
|
return out
|
|
2202
2259
|
}
|
|
2203
2260
|
|
|
2261
|
+
// queryOf is the request's x-query-id, as 3.17.2's
|
|
2262
|
+
// createModelRequestAttributionHeaders (zft in glm/zcode.cjs) sends it:
|
|
2263
|
+
// the user's query, "query_" + a UUID with the prefix cut, the same for
|
|
2264
|
+
// every request of one turn (the tool calls the model makes on the way)
|
|
2265
|
+
// and a new one for the next thing the user says. The agent's turns are
|
|
2266
|
+
// counted: a user message that carries text, not tool results alone,
|
|
2267
|
+
// starts a query. A body that doesn't read gives none, and none is sent.
|
|
2268
|
+
function queryOf(text, session) {
|
|
2269
|
+
let b
|
|
2270
|
+
try {
|
|
2271
|
+
b = JSON.parse(text)
|
|
2272
|
+
} catch {
|
|
2273
|
+
return ""
|
|
2274
|
+
}
|
|
2275
|
+
if (!Array.isArray(b?.messages)) return ""
|
|
2276
|
+
let n = 0
|
|
2277
|
+
for (const m of b.messages) {
|
|
2278
|
+
if (m?.role !== "user") continue
|
|
2279
|
+
const c = m.content
|
|
2280
|
+
if (typeof c === "string" ? c.trim() : Array.isArray(c) && c.some((x) => x && x.type !== "tool_result")) n++
|
|
2281
|
+
}
|
|
2282
|
+
return n ? uuidOf("zcode-query\0" + session + "\0" + n) : ""
|
|
2283
|
+
}
|
|
2284
|
+
|
|
2204
2285
|
// Claude Code's tool search, as ZCode's plans take it. Claude Code
|
|
2205
2286
|
// (2.1.310) offers a tool named ToolSearch, names its deferred tools in a
|
|
2206
2287
|
// system turn, and sends a deferred tool's schema with defer_loading only
|
|
@@ -2739,6 +2820,8 @@ export async function ZCodeAuthPlugin({ client, magpie } = {}, options = {}) {
|
|
|
2739
2820
|
if (opts.body != null) {
|
|
2740
2821
|
const text = orig ?? (typeof opts.body === "string" ? opts.body : await new Response(opts.body).text())
|
|
2741
2822
|
opts.body = dress(text, s.site, s.device, place, sid)
|
|
2823
|
+
const q = queryOf(text, sid)
|
|
2824
|
+
if (q) h.set("x-query-id", q)
|
|
2742
2825
|
delete opts.duplex
|
|
2743
2826
|
h.delete("content-length")
|
|
2744
2827
|
}
|
|
@@ -2975,4 +3058,4 @@ export async function ZCodeAuthPlugin({ client, magpie } = {}, options = {}) {
|
|
|
2975
3058
|
// config sets what zcodeConfig has read, for tests: null is ZCode's config
|
|
2976
3059
|
// out of reach, undefined forgets it
|
|
2977
3060
|
const config = (cfg) => (cfgCache = cfg === undefined ? null : { cfg, at: Date.now() })
|
|
2978
|
-
export const _internal = { edge, EDGE_HOLD, busyHold, BUSY_N, BUSY_WINDOW, BUSY_HOLD, busyCap, busyHeld, config, entry, limitWindows, termOf, startUsage, routes, blocked, sidelined, plansOf, giftOf, dualUsage, claimHint, claimDaily, spentUp, giftServes, modelOf, teamKeys, ownSignIn, stateOf, located, dress, PROMPT, marked, keyDevice, sessionOf, busy, BUSY_WAITS, DEFAULT_CONCURRENCY, DEFAULT_ACCOUNTS }
|
|
3061
|
+
export const _internal = { edge, EDGE_HOLD, busyHold, BUSY_N, BUSY_WINDOW, BUSY_HOLD, busyCap, busyHeld, config, entry, limitWindows, termOf, startUsage, routes, blocked, sidelined, plansOf, giftOf, dualUsage, claimHint, claimDaily, spentUp, giftServes, modelOf, teamKeys, ownSignIn, stateOf, located, dress, queryOf, PROMPT, marked, keyDevice, sessionOf, busy, BUSY_WAITS, DEFAULT_CONCURRENCY, DEFAULT_ACCOUNTS }
|
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.55",
|
|
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",
|