@magpie-community/opencode-zcode-auth 0.1.41 → 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 +17 -9
  2. package/index.mjs +41 -5
  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`,
@@ -136,14 +143,15 @@ never read or buffered on the way.
136
143
 
137
144
  **Free first.** Picking `-Trial` by hand keeps the choice the user's,
138
145
  but an agent that always asks the plain model never sees the gift.
139
- `freeFirst` turns the preference around: the plain model spends the
140
- account's own live gift first, and falls back to the coding plan when
141
- the gift cannot serve it — a bucket that is spent, a model the gift
142
- does not serve, no ZCode session, or the minute's rest a refusal just
143
- earned. Turn it on for the whole plugin in magpie's Plugins › Options
146
+ `freeFirst`, on by default since 0.1.42, turns the preference around:
147
+ the plain model spends the account's own live gift first, and falls
148
+ back to the coding plan when the gift cannot serve it — a bucket that
149
+ is spent, a model the gift does not serve, no ZCode session, or the
150
+ minute's rest a refusal just earned. Turn it off for the whole plugin
151
+ in magpie's Plugins › Options
144
152
 
145
153
  ```json
146
- { "freeFirst": true }
154
+ { "freeFirst": false }
147
155
  ```
148
156
 
149
157
  or on the provider alone, under `provider.zcode.options` in the config
@@ -271,8 +279,8 @@ once) and posts each plan to
271
279
  account's ZCode session, as the app does. An answer of nothing to claim
272
280
  carries an `until` half an hour out, so magpie's loop asks again then,
273
281
  and again, all day while neither list names a plan — two GETs, nothing
274
- posted: ZCode drops plans into the feed on its own schedule (a reset
275
- card, a weekend banner starting at 19:00 Beijing), and one that arrives
282
+ posted: ZCode drops plans into the feed on its own schedule (a weekend
283
+ banner starting at 19:00 Beijing, say), and one that arrives
276
284
  after the day's ask used to sit unclaimed past its short life. The
277
285
  other answers stand for the day: a claimed plan leaves the feed, so
278
286
  asking again half an hour later would find nothing and flip the card
package/index.mjs CHANGED
@@ -804,7 +804,7 @@ async function toClaim(s) {
804
804
 
805
805
  // RECHECK is how long an "nothing to claim" stands before magpie's loop
806
806
  // asks again: ZCode drops gift plans into the feed during the day (a
807
- // reset card, a weekend banner) on its own schedule, and the day's ask
807
+ // weekend banner, say) on its own schedule, and the day's ask
808
808
  // may have run hours before, so an inactive settled for the day lets a
809
809
  // plan sit unclaimed past its short life (the weekend plan of
810
810
  // testdata/touch-1030-wk.json starts 19:00 Beijing). `until` wakes the
@@ -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.
@@ -2241,9 +2268,12 @@ export async function ZCodeAuthPlugin({ client }, options = {}) {
2241
2268
  }
2242
2269
  // freeFirst reads the option from the provider config's options first,
2243
2270
  // and from the plugin's own (magpie's Plugins › Options) when the
2244
- // provider carries no freeFirst of its own.
2271
+ // provider carries no freeFirst of its own. It is on unless one of them
2272
+ // says false: a gift ZCode hands out (each weekend, sgfvdsg86265 on X)
2273
+ // lapses unused behind an agent that asks the plain model, and every
2274
+ // failure of the gift is answered from the plan.
2245
2275
  const freeFirst = (providerOptions) =>
2246
- providerOptions?.freeFirst !== undefined ? providerOptions.freeFirst === true : options?.freeFirst === true
2276
+ providerOptions?.freeFirst !== undefined ? providerOptions.freeFirst !== false : options?.freeFirst !== false
2247
2277
  const oauth = (site) => ({
2248
2278
  type: "oauth",
2249
2279
  label: `ZCode: ${SITES[site].name}${site === "bigmodel" ? " (智谱)" : ""} GLM Coding Plan`,
@@ -2379,7 +2409,7 @@ export async function ZCodeAuthPlugin({ client }, options = {}) {
2379
2409
  orig = opts.body = JSON.stringify({ ...JSON.parse(orig), model: raw })
2380
2410
  m = raw
2381
2411
  forcedGift = true
2382
- } 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)) {
2383
2413
  // free first: the plain model spends the account's own live
2384
2414
  // gift before it spends the coding plan, under the name the
2385
2415
  // gift lists. The gift is the route's cache alone (a failed
@@ -2439,6 +2469,10 @@ export async function ZCodeAuthPlugin({ client }, options = {}) {
2439
2469
  delete opts.duplex
2440
2470
  return askBusy(u, opts, h2)
2441
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())
2442
2476
  if (start) {
2443
2477
  if (jwtExpired(s.jwt)) throw new Error(EXPIRED)
2444
2478
  if (!s.jwt) throw new Error("ZCode's sign-in has no key; sign in again")
@@ -2497,6 +2531,8 @@ export async function ZCodeAuthPlugin({ client }, options = {}) {
2497
2531
  // a key account sent to the Start Plan (its plan list read
2498
2532
  // none) reads its plans again on the next request, so a
2499
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
2500
2536
  if (freeGift) {
2501
2537
  restModel()
2502
2538
  return done(kept(await replayOnPlan()))
@@ -2682,4 +2718,4 @@ export async function ZCodeAuthPlugin({ client }, options = {}) {
2682
2718
  // config sets what zcodeConfig has read, for tests: null is ZCode's config
2683
2719
  // out of reach, undefined forgets it
2684
2720
  const config = (cfg) => (cfgCache = cfg === undefined ? null : { cfg, at: Date.now() })
2685
- 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.41",
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",