@magpie-community/opencode-zcode-auth 0.1.42 → 0.1.43

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 +8 -1
  2. package/index.mjs +35 -2
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -38,7 +38,14 @@ over Anthropic's Messages API. Provider id: `zcode`.
38
38
  for unusual activity (code 3012); …", which magpie reads as the account
39
39
  restricted: it rests, and the group's other providers are used. Only
40
40
  ZCode support lifts such a block. A block page from ZCode's edge
41
- firewall says this network's address was blocked instead.
41
+ firewall says this network's address was blocked instead. Since 0.1.43
42
+ (plugins#649) such a page also holds zcode.z.ai off for three minutes,
43
+ for every account of the plugin, since they share the address: the
44
+ Start Plan and a picked `-Trial` entry are answered at once with the
45
+ same 405 and words, so magpie still uses the group's other providers,
46
+ and free first keeps a dual account on its coding plan. The first
47
+ request after the three minutes asks again. A GLM Coding Plan key, on
48
+ api.z.ai, is never held.
42
49
  - The Start Plan's token can't be refreshed. When it runs out, sign in
43
50
  again.
44
51
  - Every request, on any plan, names its conversation in `x-session-id`,
package/index.mjs CHANGED
@@ -1899,6 +1899,33 @@ const START_REFUSED = /unusual activity|request has been blocked|"code"\s*:\s*"?
1899
1899
  // or code 3012.
1900
1900
  const startRefused = (status, text) => status >= 400 && (status === 405 || START_REFUSED.test(text))
1901
1901
 
1902
+ // edge is the hold a block page puts on this computer's address: zcode.z.ai
1903
+ // (the Start Plan, a gift) is not asked again until edge.until, and its
1904
+ // requests are answered here with the same 405 and START_EDGE, so magpie
1905
+ // still reads them as the network's (provider.EdgeBlocked) and asks the
1906
+ // group's next provider. Every account of this process goes out from the
1907
+ // one address, so the hold is the process's, not an account's. Asking on
1908
+ // while the address is blocked, from every account and every session, only
1909
+ // knocks again on a firewall that is counting (plugins#649: two accounts,
1910
+ // three sessions and their subagents, and ZCode's own app blocked too; a
1911
+ // block in #369 lasted about seven minutes). EDGE_HOLD is short enough that
1912
+ // a user who switched network or proxy waits little for it.
1913
+ const EDGE_HOLD = 3 * 60_000
1914
+ const edge = { until: 0 }
1915
+ const edgeHeld = () => Date.now() < edge.until
1916
+
1917
+ function hhmm(t) {
1918
+ const d = new Date(t)
1919
+ return String(d.getHours()).padStart(2, "0") + ":" + String(d.getMinutes()).padStart(2, "0") + ":" + String(d.getSeconds()).padStart(2, "0")
1920
+ }
1921
+
1922
+ // edgeHeldAnswer is a held request's answer: the block page's status and
1923
+ // meaning, and until when this computer holds off.
1924
+ function edgeHeldAnswer() {
1925
+ const error = { type: "permission_error", message: `405 ${STATUS_TEXT[405]} — ${START_EDGE} (ZCode's Start Plan is not asked again from here until ${hhmm(edge.until)}, plugins#649)` }
1926
+ return new Response(JSON.stringify({ type: "error", error }), { status: 405, statusText: STATUS_TEXT[405], headers: { "content-type": "application/json" } })
1927
+ }
1928
+
1902
1929
  // startBlocked is the refusal's answer: what the vendor said (its status
1903
1930
  // for a block page), then what it means. A block page is the network's;
1904
1931
  // anything else, ZCode's JSON 3012 or a bare 405, the account's.
@@ -2382,7 +2409,7 @@ export async function ZCodeAuthPlugin({ client }, options = {}) {
2382
2409
  orig = opts.body = JSON.stringify({ ...JSON.parse(orig), model: raw })
2383
2410
  m = raw
2384
2411
  forcedGift = true
2385
- } else if (!alone && freeFirst(provider?.options) && s.jwt && known && giftUsable(s, known, m)) {
2412
+ } else if (!alone && !edgeHeld() && freeFirst(provider?.options) && s.jwt && known && giftUsable(s, known, m)) {
2386
2413
  // free first: the plain model spends the account's own live
2387
2414
  // gift before it spends the coding plan, under the name the
2388
2415
  // gift lists. The gift is the route's cache alone (a failed
@@ -2442,6 +2469,10 @@ export async function ZCodeAuthPlugin({ client }, options = {}) {
2442
2469
  delete opts.duplex
2443
2470
  return askBusy(u, opts, h2)
2444
2471
  }
2472
+ // a block page held this computer off zcode.z.ai (edge): the
2473
+ // Start Plan and a picked gift are answered here; free first
2474
+ // already left the gift for the plan
2475
+ if (start && edgeHeld()) return kept(edgeHeldAnswer())
2445
2476
  if (start) {
2446
2477
  if (jwtExpired(s.jwt)) throw new Error(EXPIRED)
2447
2478
  if (!s.jwt) throw new Error("ZCode's sign-in has no key; sign in again")
@@ -2500,6 +2531,8 @@ export async function ZCodeAuthPlugin({ client }, options = {}) {
2500
2531
  // a key account sent to the Start Plan (its plan list read
2501
2532
  // none) reads its plans again on the next request, so a
2502
2533
  // coding plan that is there after all is used (#369)
2534
+ // a block page holds every request off zcode.z.ai a while
2535
+ if (EDGE_PAGE.test(text)) edge.until = Date.now() + EDGE_HOLD
2503
2536
  if (freeGift) {
2504
2537
  restModel()
2505
2538
  return done(kept(await replayOnPlan()))
@@ -2685,4 +2718,4 @@ export async function ZCodeAuthPlugin({ client }, options = {}) {
2685
2718
  // config sets what zcodeConfig has read, for tests: null is ZCode's config
2686
2719
  // out of reach, undefined forgets it
2687
2720
  const config = (cfg) => (cfgCache = cfg === undefined ? null : { cfg, at: Date.now() })
2688
- export const _internal = { config, entry, limitWindows, termOf, startUsage, routes, blocked, plansOf, giftOf, dualUsage, claimHint, claimDaily, spentUp, giftServes, modelOf, teamKeys, ownSignIn, stateOf, located, dress, PROMPT, marked, keyDevice, sessionOf, busy, BUSY_WAITS, DEFAULT_CONCURRENCY, DEFAULT_ACCOUNTS }
2721
+ export const _internal = { edge, EDGE_HOLD, config, entry, limitWindows, termOf, startUsage, routes, blocked, plansOf, giftOf, dualUsage, claimHint, claimDaily, spentUp, giftServes, modelOf, teamKeys, ownSignIn, stateOf, located, dress, 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.42",
3
+ "version": "0.1.43",
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": "./index.mjs",