@magpie-community/opencode-zcode-auth 0.1.64 → 0.1.66

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 +14 -3
  2. package/index.mjs +79 -10
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -117,7 +117,14 @@ the plans again on the next request:
117
117
  (`/api/biz/subscription/list`), its requests go to that plan. A model
118
118
  the account's gift plans also serve is listed again as
119
119
  `<model>-Trial`; asking that entry spends the gift (see below).
120
- - Otherwise they go to the gift plans.
120
+ - Otherwise they go to the gift plans. A model the gift plans' balance
121
+ doesn't name (an agent left on GLM-5.2 while the gift serves only
122
+ GLM-5.3-Flash) would spend none of the free quota, so it is answered
123
+ with the models the balance names and not sent. The balance is the one
124
+ the model list read; a model it doesn't name has it read again first.
125
+ When the plan list can't be read, an account whose last read found no
126
+ plan stays on the gift plans rather than sending its key to the coding
127
+ plan's host, which bills a key with no plan pay-as-you-go.
121
128
 
122
129
  Every request carries the key as both `x-api-key` and
123
130
  `Authorization: Bearer`.
@@ -350,8 +357,12 @@ menu bar pass it over. The name is a fixed sentence magpie can
350
357
  translate; the count and the plan's own name are the display. Reading
351
358
  the card never claims one. A preview that fails, or lists nothing, adds
352
359
  no line at all, and a card that is an error keeps its error. Claim the
353
- plan in the ZCode app (open it once), or let the daily claim below do it;
354
- the next read shows it as an ordinary gift bucket.
360
+ plan in the ZCode app (open it once); while ZCode asks its security check
361
+ for a claim, as it does now, the daily claim below posts none. The next
362
+ read shows it as an ordinary gift bucket: its `-Trial` entry is listed and
363
+ free first spends it. Until then a `-Trial` pick that finds no gift is
364
+ refused with the held plan's name and "claim it in the ZCode app", not
365
+ only "no active limited quota" (heduian and bg5eau on X, 11-14).
355
366
 
356
367
  ## The daily claim
357
368
 
package/index.mjs CHANGED
@@ -263,13 +263,20 @@ async function plansOf(s) {
263
263
  // Plan's 405 until the route was read again (#369)
264
264
  if (start) sure = !(s.key && r && !r.start)
265
265
  } catch {
266
- // the plan list could not be read: a key account takes the dual route
267
- // (start stays false, the gift is kept, a minute's ttl), so a gift
268
- // refusal still replays to the coding plan rather than blacking out
269
- try {
270
- gift = await startPlan(s.jwt, s.device)
271
- sure = false
272
- } catch {}
266
+ // the plan list could not be read. An account the last read found with
267
+ // no coding plan stays on the Start Plan for the minute: the dual route
268
+ // would send its key to the coding plan's host, which bills a key with
269
+ // no plan pay-as-you-go while the free quota sits unspent (bg5eau on
270
+ // X). Any other key account takes the dual route (start stays false,
271
+ // the gift is kept, a minute's ttl), so a gift refusal still replays to
272
+ // the coding plan rather than blacking out
273
+ if (r?.start) start = true
274
+ else {
275
+ try {
276
+ gift = await startPlan(s.jwt, s.device)
277
+ } catch {}
278
+ }
279
+ sure = false
273
280
  }
274
281
  const out = { start, gift, at: Date.now(), ttl: Math.min(sure ? 600_000 : 60_000, gift?.ttl ?? 600_000) }
275
282
  routes.set(id, out)
@@ -715,6 +722,48 @@ async function claimHint(s) {
715
722
  return { name: CLAIM_HINT, used: 0, aside: true, display: `${n} · ${plan}` }
716
723
  }
717
724
 
725
+ // unclaimed names the gift plans ZCode holds for the account but has not
726
+ // put on it (toClaim), for a -Trial pick that finds no gift: the weekend
727
+ // plan (ZCODE WEEKEND BUILD) comes only by a claim, and the claim needs
728
+ // the ZCode app's captcha (claimDaily posts none), so until it is
729
+ // claimed in the app the pick's quota does not exist yet (heduian and
730
+ // bg5eau on X, 11-14). The refusal says where the quota is rather than
731
+ // only that there is none. Kept ten minutes per session, so an agent
732
+ // retrying the pick doesn't read the feed each time; a read that fails
733
+ // names none and is not kept.
734
+ // aloneSeen is a gift-only account's balance as last read (the model list
735
+ // reads it), kept ten minutes. A request reads no balance on its own: only
736
+ // a model the kept balance doesn't name has it read again (aloneGift), so
737
+ // a gift claimed since is not refused. A read that fails is not kept.
738
+ const aloneSeen = new Map()
739
+ const aloneNames = (s) => {
740
+ const c = aloneSeen.get(s.jwt)
741
+ return c && Date.now() - c.at < 600_000 ? giftNames(c.gift) : []
742
+ }
743
+ async function aloneGift(s) {
744
+ let gift
745
+ try {
746
+ gift = await startPlan(s.jwt, s.device)
747
+ } catch {
748
+ return null
749
+ }
750
+ aloneSeen.set(s.jwt, { at: Date.now(), gift })
751
+ return gift
752
+ }
753
+
754
+ const unclaimedSeen = new Map()
755
+ async function unclaimed(s) {
756
+ if (!s.jwt || jwtExpired(s.jwt)) return []
757
+ const k = s.jwt
758
+ const c = unclaimedSeen.get(k)
759
+ if (c && Date.now() - c.at < 600_000) return c.names
760
+ const got = await toClaim(s).catch(() => null)
761
+ if (!got || got.unread.length) return []
762
+ const names = uniqueModels(got.plans.map((p) => first(p.name, p.plan_id)).filter(Boolean))
763
+ unclaimedSeen.set(k, { at: Date.now(), names })
764
+ return names
765
+ }
766
+
718
767
  // ---- the daily claim ---------------------------------------------------------------
719
768
 
720
769
  // CLAIMED_ALREADY is /billing/claim's body code for a plan the account
@@ -2970,7 +3019,7 @@ export async function ZCodeAuthPlugin({ client, magpie } = {}, options = {}) {
2970
3019
  const s = await located(stateOf(auth))
2971
3020
  if (!s) return provider.models
2972
3021
  const { start, gift: known } = await plansOf(s).catch(() => ({ start: false, gift: null }))
2973
- const gift = start ? await startPlan(s.jwt, s.device).catch(() => null) : known
3022
+ const gift = start ? await aloneGift(s) : known
2974
3023
  const base = start ? START_BASE : s.base
2975
3024
  const cfg = await zcodeConfig().catch(() => null)
2976
3025
  let ms = modelsOf(cfg, planID(base))
@@ -3119,7 +3168,8 @@ export async function ZCodeAuthPlugin({ client, magpie } = {}, options = {}) {
3119
3168
  const raw = giftModel(gift, m)
3120
3169
  const available = raw && giftUsable(s, gift, raw)
3121
3170
  if (!available) {
3122
- const error = { type: raw ? "rate_limit_error" : "invalid_request_error", message: raw ? `${giftRested(s, raw) ? `ZCode said the last request for ${raw} was over its quota, so it is not asked again for a minute: ` : ""}ZCode's limited quota for ${raw} is exhausted or temporarily unavailable` : `ZCode has no active limited quota for ${m}` }
3171
+ const held = raw ? [] : await unclaimed(s)
3172
+ const error = { type: raw ? "rate_limit_error" : "invalid_request_error", message: raw ? `${giftRested(s, raw) ? `ZCode said the last request for ${raw} was over its quota, so it is not asked again for a minute: ` : ""}ZCode's limited quota for ${raw} is exhausted or temporarily unavailable` : `ZCode has no active limited quota for ${m}${held.length ? `. ZCode holds ${held.join(", ")} for this account unclaimed: claim it in the ZCode app (its claim needs the app's security check), and ${m} spends it` : ""}` }
3123
3173
  return kept(new Response(JSON.stringify({ type: "error", error }), { status: raw ? 429 : 400, headers: { "content-type": "application/json" } }))
3124
3174
  }
3125
3175
  orig = opts.body = JSON.stringify({ ...JSON.parse(orig), model: wireModel(raw) })
@@ -3146,6 +3196,25 @@ export async function ZCodeAuthPlugin({ client, magpie } = {}, options = {}) {
3146
3196
  m = raw
3147
3197
  freeGift = true
3148
3198
  giftName = giftID(raw)
3199
+ } else if (alone && /\/v1\/messages$/.test(new URL(url).pathname)) {
3200
+ // a gift-only account's Start Plan serves the models its
3201
+ // balance names, and ZCode's app offers only those
3202
+ // (resolveStartPlanBalanceAvailability). Any other model
3203
+ // spends none of the free quota, so it is not sent: an
3204
+ // agent left on another model would be billed while the
3205
+ // gift stayed unspent (bg5eau on X). The balance is the
3206
+ // one the model list read (aloneSeen); a model it doesn't
3207
+ // name has it read again before the refusal. One not read
3208
+ // yet, one that fails, or one naming none leaves the
3209
+ // request as it was.
3210
+ const named = (ns) => ns.some((x) => x.toLowerCase() === m.toLowerCase())
3211
+ const kept0 = aloneNames(s)
3212
+ const names = kept0.length && !named(kept0) ? giftNames(await aloneGift(s)) : []
3213
+ if (names.length && !named(names)) {
3214
+ const shown = uniqueModels(names.map(wireModel))
3215
+ const error = { type: "invalid_request_error", message: `ZCode's Start Plan on this account serves ${shown.join(", ")}; ${m} is not among them and would spend none of its free quota, so it was not sent. Pick ${shown.length > 1 ? "one of those" : shown[0]} for this account` }
3216
+ return kept(new Response(JSON.stringify({ type: "error", error }), { status: 400, headers: { "content-type": "application/json" } }))
3217
+ }
3149
3218
  }
3150
3219
  start = alone || forcedGift || freeGift
3151
3220
  }
@@ -3501,4 +3570,4 @@ export async function ZCodeAuthPlugin({ client, magpie } = {}, options = {}) {
3501
3570
  // config sets what zcodeConfig has read, for tests: null is ZCode's config
3502
3571
  // out of reach, undefined forgets it
3503
3572
  const config = (cfg) => (cfgCache = cfg === undefined ? null : { cfg, at: Date.now() })
3504
- export const _internal = { wallAnswer, unreached, planFull, notePlan, planFullUntil, locatedKeys, lastKeys, edge, EDGE_HOLD, busyHold, startGate, NARROW_FOR, limitOf, 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 }
3573
+ export const _internal = { wallAnswer, unreached, planFull, notePlan, planFullUntil, locatedKeys, lastKeys, edge, EDGE_HOLD, busyHold, startGate, NARROW_FOR, limitOf, BUSY_N, BUSY_WINDOW, BUSY_HOLD, busyCap, busyHeld, config, entry, limitWindows, termOf, startUsage, routes, blocked, sidelined, plansOf, giftOf, dualUsage, claimHint, unclaimed, unclaimedSeen, aloneGift, aloneSeen, aloneNames, 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.64",
3
+ "version": "0.1.66",
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",