@magpie-community/opencode-zcode-auth 0.1.65 → 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 +8 -1
  2. package/index.mjs +55 -9
  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`.
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)
@@ -724,6 +731,26 @@ async function claimHint(s) {
724
731
  // only that there is none. Kept ten minutes per session, so an agent
725
732
  // retrying the pick doesn't read the feed each time; a read that fails
726
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
+
727
754
  const unclaimedSeen = new Map()
728
755
  async function unclaimed(s) {
729
756
  if (!s.jwt || jwtExpired(s.jwt)) return []
@@ -2992,7 +3019,7 @@ export async function ZCodeAuthPlugin({ client, magpie } = {}, options = {}) {
2992
3019
  const s = await located(stateOf(auth))
2993
3020
  if (!s) return provider.models
2994
3021
  const { start, gift: known } = await plansOf(s).catch(() => ({ start: false, gift: null }))
2995
- const gift = start ? await startPlan(s.jwt, s.device).catch(() => null) : known
3022
+ const gift = start ? await aloneGift(s) : known
2996
3023
  const base = start ? START_BASE : s.base
2997
3024
  const cfg = await zcodeConfig().catch(() => null)
2998
3025
  let ms = modelsOf(cfg, planID(base))
@@ -3169,6 +3196,25 @@ export async function ZCodeAuthPlugin({ client, magpie } = {}, options = {}) {
3169
3196
  m = raw
3170
3197
  freeGift = true
3171
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
+ }
3172
3218
  }
3173
3219
  start = alone || forcedGift || freeGift
3174
3220
  }
@@ -3524,4 +3570,4 @@ export async function ZCodeAuthPlugin({ client, magpie } = {}, options = {}) {
3524
3570
  // config sets what zcodeConfig has read, for tests: null is ZCode's config
3525
3571
  // out of reach, undefined forgets it
3526
3572
  const config = (cfg) => (cfgCache = cfg === undefined ? null : { cfg, at: Date.now() })
3527
- 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, 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.65",
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",