@magpie-community/opencode-zcode-auth 0.1.68 → 0.1.70

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 +16 -14
  2. package/index.mjs +73 -30
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -379,20 +379,22 @@ polling with the feed alone, and the feed is gated on the client's
379
379
  version, so the plugin asks as the current client (3.17.2) sends — and
380
380
  posts each plan the feed offers to
381
381
  `POST /api/v1/zcode-plan/billing/claim` (`{"plan_id": …}`) with the
382
- account's ZCode session, as the app does. An answer of nothing to claim
383
- carries an `until` half an hour out, so magpie's loop asks again then,
384
- and again, all day while the feed names no plan — one GET, nothing
385
- posted: ZCode drops plans into the feed on its own schedule (a weekend
386
- banner starting at 19:00 Beijing, say), and one that arrives
387
- after the day's ask used to sit unclaimed past its short life. The
388
- other answers stand for the day — but an *ineligible* (code 1004, below)
389
- is read again in half an hour, as an inactive is: a claimed plan leaves
390
- the feed, so
391
- asking again half an hour later would find nothing and flip the card
392
- from "Checked in today" to "No check-in event now"; a captcha plan
393
- re-asked makes ZCode demand its captcha again; a done plan re-asked
394
- posts a claim ZCode already answered. So a second plan the same day,
395
- after one was claimed, is seen the next day. The answer is judged by
382
+ account's ZCode session, as the app does. ZCode drops plans into the
383
+ feed on its own schedule through the day (a weekend banner starting at
384
+ 19:00 Beijing, and another at 19:30, say), and one that arrives after the
385
+ day's ask used to sit unclaimed past its short life. So an answer of
386
+ nothing to claim, a claim, a done and an ineligible each carry an
387
+ `until` half an hour out, and magpie's loop asks again then, all day —
388
+ one GET of the feed each time. A recheck posts only plans the account
389
+ has no answer for yet that day: a plan claimed, answered 1003 or held
390
+ for the captcha is never posted again, so its captcha is not asked for
391
+ again and no claim ZCode already answered is repeated. With nothing new
392
+ after the day's claim, the answer is *done*: the card keeps "Checked in
393
+ today" rather than "No check-in event now" (a claimed plan leaves the
394
+ feed). An ineligible (code 1004, below) is posted again on the next
395
+ recheck, as it may be offered later. The day's answers live in memory
396
+ and end with the Beijing day: after a magpie restart, a plan still in
397
+ the feed is posted once more and answers 1003, *done*. The answer is judged by
396
398
  the body's code, not the HTTP status:
397
399
 
398
400
  - nothing listed: *inactive* (ZCode answers an account with nothing to
package/index.mjs CHANGED
@@ -980,25 +980,49 @@ async function toClaim(s) {
980
980
  return { plans, unread, rate: null }
981
981
  }
982
982
 
983
- // RECHECK is how long an "nothing to claim" stands before magpie's loop
984
- // asks again: ZCode drops gift plans into the feed during the day (a
985
- // weekend banner, say) on its own schedule, and the day's ask
986
- // may have run hours before, so an inactive settled for the day lets a
987
- // plan sit unclaimed past its short life (the weekend plan of
988
- // testdata/touch-1030-wk.json starts 19:00 Beijing). `until` wakes the
989
- // loop again in half an hour, and it asks again all day while neither
990
- // list names a plan — two GETs and no claim posted. The other settled
991
- // answers stand for the day: a claimed plan leaves the feed, so asking
992
- // a claimed account again half an hour later finds nothing and flips
993
- // its card from "Checked in today" to "No check-in event now"; a
994
- // captcha plan re-asked makes ZCode demand its captcha again each time,
995
- // and a done plan re-asked posts a claim it already answered 1003. The
996
- // same shape as Qoder's campaign that opens mid-day, asked again on
997
- // magpie's side there (yetone/magpie#2696). A failed needs no until:
998
- // magpie asks a failed again after 30 minutes of its own.
983
+ // RECHECK is how long an answer that leaves the day open stands before
984
+ // magpie's loop asks again: ZCode drops gift plans into the feed during
985
+ // the day on its own schedule (the weekend plan of
986
+ // testdata/touch-1030-wk.json starts 19:00 Beijing; @aaron0x10 on X,
987
+ // 11-22: a second one follows at 19:30), and the day's ask may have run
988
+ // hours before, so an answer settled for the day lets a plan sit
989
+ // unclaimed past its short life. `until` wakes the loop again in half an
990
+ // hour, and it asks again all day — one GET each time. That holds after a
991
+ // claim too (claimed, done): the recheck posts only plans this account
992
+ // has no answer for today (dayAnswers), so a plan claimed, answered 1003
993
+ // or held for the captcha is never posted again, and one that has left
994
+ // the feed is still the day's claim — answered done, so the card keeps
995
+ // "Checked in today" instead of flipping to "No check-in event now". A
996
+ // captcha needs no until: magpie leaves it for the day, as it waits on
997
+ // the user. A failed needs none either: magpie asks a failed again after
998
+ // 30 minutes of its own. The same shape as Qoder's campaign that opens
999
+ // mid-day, asked again on magpie's side there (yetone/magpie#2696).
999
1000
  const RECHECK_MS = 30 * 60_000
1000
1001
  const recheck = () => new Date(Date.now() + RECHECK_MS).toISOString()
1001
1002
 
1003
+ // dayAnswers is what each account's plans answered today (Beijing):
1004
+ // holdKey -> { day, plans: Map(plan_id -> { name, outcome }) }, for the
1005
+ // answers that settle a plan for the day — claimed, done (1003) and
1006
+ // captcha (3007). A recheck posts only plans not in it, so none of those
1007
+ // is asked again; an ineligible or failed one is not kept, and is asked
1008
+ // again. It lives in this process's memory: a magpie restart forgets it,
1009
+ // and the next check-in posts a plan still in the feed once more (1003,
1010
+ // done, as before).
1011
+ const dayAnswers = new Map()
1012
+ const SETTLES = new Set(["claimed", "done", "captcha"])
1013
+ const uniq = (xs) => [...new Set(xs)]
1014
+ const beijingDay = () => new Date(Date.now() + 8 * 3600_000).toISOString().slice(0, 10)
1015
+ function answersToday(s) {
1016
+ const k = holdKey(s), day = beijingDay()
1017
+ let a = dayAnswers.get(k)
1018
+ if (!a || a.day !== day) dayAnswers.set(k, (a = { day, plans: new Map() }))
1019
+ return a.plans
1020
+ }
1021
+ // resetDayAnswers is for tests: it forgets every account's answers today.
1022
+ export function resetDayAnswers() {
1023
+ dayAnswers.clear()
1024
+ }
1025
+
1002
1026
  // claimDaily claims every gift plan ZCode offers the account (#503:
1003
1027
  // toClaim, the app's banners and its preview), as auth.checkin's answer:
1004
1028
  // inactive when neither lists one, claimed when one was claimed now,
@@ -1016,9 +1040,10 @@ const recheck = () => new Date(Date.now() + RECHECK_MS).toISOString()
1016
1040
  // v0.1.3165 honors a failed's `until` itself; older versions wait for
1017
1041
  // this hold, which is why it stays.
1018
1042
  // A failed one is asked again later in the
1019
- // day, when a plan claimed now answers 1003. An inactive stands only
1020
- // until RECHECK_MS from now, so a plan dropped into the feed later in
1021
- // the day is still claimed; the other settled answers stand for the day
1043
+ // day. An inactive, ineligible, claimed or done stands only until
1044
+ // RECHECK_MS from now, so a plan dropped into the feed later in the day
1045
+ // is still claimed; a plan answered today (dayAnswers) is not posted
1046
+ // again, and after the day's claim an ask that finds nothing new is done
1022
1047
  // (see RECHECK).
1023
1048
  // Nothing here marks the account: a failed claim keeps its sign-in.
1024
1049
  async function claimDaily(s) {
@@ -1035,14 +1060,23 @@ async function claimDaily(s) {
1035
1060
  const message = unread.join("; ") || "ZCode rate limited this account"
1036
1061
  return { outcome: "failed", message, until: noteRateLimit(holdKey(s), rate, message) }
1037
1062
  }
1063
+ // the plans this account answered for already today: never posted again
1064
+ const today = answersToday(s)
1065
+ const checkedToday = () => [...today.values()].filter((a) => a.outcome === "claimed" || a.outcome === "done").map((a) => a.name)
1066
+ const fresh = plans.filter((p) => !today.has(p.plan_id))
1038
1067
  // the feed answered and no claim is read: the wait is over. It is not
1039
1068
  // forgotten before the claims below, so a claim ZCode keeps limiting
1040
1069
  // waits longer each time (see noteRateLimit)
1041
- if (!plans.length) {
1070
+ if (!fresh.length) {
1042
1071
  rateWait.delete(holdKey(s))
1043
- return unread.length
1044
- ? { outcome: "failed", message: unread.join("; ") }
1045
- : { outcome: "inactive", message: "no ZCode gift plan to claim", until: recheck() }
1072
+ if (unread.length) return { outcome: "failed", message: unread.join("; ") }
1073
+ // nothing new since the day's claim: still checked in today, and
1074
+ // looked at again half an hour out (see RECHECK)
1075
+ const had = checkedToday()
1076
+ if (had.length) return { outcome: "done", message: `already claimed: ${uniq(had).join(", ")}`, until: recheck() }
1077
+ // only plans held for the captcha, still listed: named again, not posted
1078
+ if (plans.length) return { outcome: "captcha", message: plans.map((p) => `${p.name}: claim it in the ZCode app (${today.get(p.plan_id).why})`).join("; ") }
1079
+ return { outcome: "inactive", message: "no ZCode gift plan to claim", until: recheck() }
1046
1080
  }
1047
1081
  // a claim ZCode guards with its security check is not posted: the app
1048
1082
  // passes it before each claim and sends the proof in a header, which
@@ -1059,11 +1093,15 @@ async function claimDaily(s) {
1059
1093
  }
1060
1094
  if (guarded !== false) {
1061
1095
  rateWait.delete(holdKey(s))
1062
- 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("; ") }
1063
- return { outcome: "captcha", message: plans.map((p) => `${p.name}: ${CAPTCHA_CLAIM}`).join("; ") }
1096
+ if (guarded === null) return { outcome: "failed", message: [...fresh.map((p) => `${p.name}: not claimed, ZCode's security check for claims can't be read`), ...unread].join("; ") }
1097
+ return { outcome: "captcha", message: fresh.map((p) => `${p.name}: ${CAPTCHA_CLAIM}`).join("; ") }
1064
1098
  }
1065
1099
  const got = []
1066
- for (const p of plans) got.push({ name: p.name, ...(await claimOne(s, p.plan_id)) })
1100
+ for (const p of fresh) {
1101
+ const g = { name: p.name, ...(await claimOne(s, p.plan_id)) }
1102
+ if (SETTLES.has(g.outcome)) today.set(p.plan_id, { name: p.name, outcome: g.outcome, why: g.why })
1103
+ got.push(g)
1104
+ }
1067
1105
  const named = (o) => got.filter((g) => g.outcome === o).map((g) => g.name)
1068
1106
  const claimed = named("claimed")
1069
1107
  const said = [
@@ -1082,12 +1120,16 @@ async function claimDaily(s) {
1082
1120
  // a list that didn't read may hold a plan not claimed: asked again
1083
1121
  if (unread.length || got.some((g) => g.outcome === "failed")) return { outcome: "failed", message: said }
1084
1122
  if (got.some((g) => g.outcome === "captcha")) return { outcome: "captcha", message: said }
1085
- if (claimed.length) return { outcome: "claimed", message: said }
1123
+ // claimed and done keep looking for the day's later plans (see RECHECK)
1124
+ if (claimed.length) return { outcome: "claimed", message: said, until: recheck() }
1125
+ const had = checkedToday()
1126
+ const notOffered = got.filter((g) => g.outcome === "ineligible").map((g) => `${g.name}: ${g.why}`)
1086
1127
  // a plan not offered now can be offered later in the day (11-04: an
1087
1128
  // account's GLOBAL BUILD banner showed 28 minutes after it signed in),
1088
- // so an ineligible is read again half an hour out, as an inactive is
1089
- if (got.some((g) => g.outcome === "ineligible")) return { outcome: "ineligible", message: said, until: recheck() }
1090
- return { outcome: "done", message: `already claimed: ${named("done").join(", ")}` }
1129
+ // so an ineligible is read again half an hour out, as an inactive is;
1130
+ // after the day's claim the card stays checked in
1131
+ if (!had.length) return { outcome: "ineligible", message: said, until: recheck() }
1132
+ return { outcome: "done", message: [`already claimed: ${uniq(had).join(", ")}`, ...notOffered].join("; "), until: recheck() }
1091
1133
  }
1092
1134
 
1093
1135
  // GIFT_SIGN_IN is the card's line for an account whose gift plans can't
@@ -1532,6 +1574,7 @@ async function authorize(site, log, onTeam = false) {
1532
1574
  continue // a hiccup: ask again
1533
1575
  }
1534
1576
  const token = site === "bigmodel" ? first(got.bigmodel?.access_token, got.bigmodel?.accessToken) : first(got.zai?.access_token)
1577
+ if (got.status === "ready" && !token) return fail(`${SITES[site].name} sign-in: the poll answered ready without an OAuth access token; sign in again`)
1535
1578
  if (got.status === "pending" || !got.status) continue
1536
1579
  if (got.status === "failed") return fail(`the sign-in was declined on ${SITES[site].name}`)
1537
1580
  if (got.status !== "ready" || !token) return fail("ZCode sign-in: unexpected answer " + got.status)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-zcode-auth",
3
- "version": "0.1.68",
3
+ "version": "0.1.70",
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",