@magpie-community/opencode-qoder-auth 0.2.42 → 0.2.44

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 +13 -0
  2. package/index.mjs +65 -6
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -390,6 +390,19 @@ Qoder CN's `runtime-info` for environment `0` (the international one is
390
390
  - `<openapi>` is `openapi.qoder.sh` (Qoder) or `openapi.qoder.com.cn`
391
391
  (Qoder CN). Other pages on those hosts are still refused.
392
392
 
393
+ **When it is asked (0.2.43).** The daily 100 credits are a campaign of
394
+ their own each day, listed from 10:00 (UTC+8) to 09:59 the next day
395
+ (`startAt`/`endAt`; the campaign's own text says "Daily reset: 10:00
396
+ (UTC+8)", a live list on 2026-10-28). A check-in before 10:00 meets the day
397
+ before's campaign: it claims that one if it is still open. Every answer
398
+ that knows the window carries `until`, the next campaign's opening (a day
399
+ after this one's start, or its end when it runs longer) plus a minute, and
400
+ magpie (with `until` support) asks again then, so today's credits are
401
+ claimed a minute or two after 10:00 (UTC+8), whatever the computer's time
402
+ zone, with nothing to set. Between the old campaign's end at 09:59 and
403
+ 10:00 the check-in is `failed` with `until` 10:01. An older magpie ignores
404
+ `until` and asks once a Beijing calendar day, as before.
405
+
393
406
  Taken from [wallechfox/qoder-checkin](https://github.com/wallechfox/qoder-checkin),
394
407
  which a user reports works, and Qoder desktop 0.4.3's campaign requests.
395
408
  International campaign discovery was checked with a real account on
package/index.mjs CHANGED
@@ -2089,6 +2089,36 @@ function gfmt(n) {
2089
2089
 
2090
2090
  const RFC3339 = /^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d(\.\d+)?(Z|[+-]\d\d:\d\d)$/i
2091
2091
 
2092
+ // Qoder's daily credits are a campaign of their own each day, listed from
2093
+ // 10:00 (UTC+8) to 09:59 the next day: its startAt and endAt, and its own
2094
+ // words, "Daily reset: 10:00 (UTC+8)" (a live list, 2026-10-28; sdytman on
2095
+ // Discord: qoder每天10点开始签到). So the check-in's day is the campaign's,
2096
+ // not the calendar's in any zone: a check-in before 10:00 meets the day
2097
+ // before's campaign, and the next one opens a day after that one's start,
2098
+ // a minute after its end. A check-in says when (until, RFC 3339), and
2099
+ // magpie asks again then, whatever the machine's time zone.
2100
+ const DAY_MS = 86_400_000
2101
+ // a minute's room for a computer clock that runs ahead of Qoder's
2102
+ const OPEN_ROOM_MS = 60_000
2103
+
2104
+ // at is a time Qoder gives, in seconds or milliseconds, as milliseconds.
2105
+ const at = (v) => {
2106
+ const n = Number(v)
2107
+ if (v === null || v === undefined || v === "" || !Number.isFinite(n) || n <= 0) return undefined
2108
+ return n > 1e12 ? n : n * 1000
2109
+ }
2110
+
2111
+ // nextOpens is when the campaign after c is expected to open: a day after
2112
+ // c's start, or c's end when it runs longer than a day.
2113
+ const nextOpens = (c) => {
2114
+ const start = at(c?.startAt), end = at(c?.endAt)
2115
+ if (end === undefined) return undefined
2116
+ return start === undefined ? end : Math.max(end, start + DAY_MS)
2117
+ }
2118
+
2119
+ // askAgain is a check-in's until: when to ask again, with room.
2120
+ const askAgain = (t) => (Number.isFinite(t) ? { until: new Date(t + OPEN_ROOM_MS).toISOString() } : {})
2121
+
2092
2122
  // when is a time Qoder gives: an RFC 3339 string, else seconds or
2093
2123
  // milliseconds, as a number or its text.
2094
2124
  function when(v) {
@@ -2098,7 +2128,20 @@ function when(v) {
2098
2128
  return new Date(Math.trunc(n < 1e12 ? n * 1000 : n)).toISOString()
2099
2129
  }
2100
2130
 
2101
- // parseUsage is the plan and each pool of credits the envelope tells.
2131
+ // SPENT_IN_TURN are the pools Qoder spends one after another before an
2132
+ // account stops: the plan's credits, the account's own add-on credits and,
2133
+ // on a team, the organisation's shared add-on credits (docs.qoder.com/Credits
2134
+ // and account/teams/shared-add-on-credits: the soonest to expire first, the
2135
+ // plan's before add-on on the same day). The envelope's own
2136
+ // totalUsagePercentage counts them as one too. So the account has left what
2137
+ // they have left together: a plan used up beside add-on credits is no
2138
+ // account used up (magpie-community/plugins#454).
2139
+ const SPENT_IN_TURN = new Set(["Credits", "Add-on credits", "Shared credits"])
2140
+
2141
+ // parseUsage is the plan and each pool of credits the envelope tells. When
2142
+ // more than one of the pools spent in turn holds credits, "Total credits"
2143
+ // is what they hold together, the one routing counts, and each pool is
2144
+ // shown beside it, set aside.
2102
2145
  function parseUsage(env) {
2103
2146
  if (env.displayMode === "enterprise") return { plan: "Enterprise" }
2104
2147
  const u = env.qoderUsage
@@ -2118,11 +2161,22 @@ function parseUsage(env) {
2118
2161
  if (total == null || total <= 0 || (b.used == null && b.remaining == null)) return
2119
2162
  const used = b.used != null ? b.used : total - b.remaining
2120
2163
  if (used < 0) return
2121
- out.windows.push({ name: b.name || name, used: Math.min(100, (100 * used) / total), display: `${gfmt(used)} / ${gfmt(total)} ${b.unit || "credits"}` })
2164
+ const unit = b.unit || "credits"
2165
+ const w = { name: b.name || name, used: Math.min(100, (100 * used) / total), display: `${gfmt(used)} / ${gfmt(total)} ${unit}` }
2166
+ out.windows.push(w)
2167
+ if (SPENT_IN_TURN.has(name)) turn.push({ w, used: Math.min(used, total), total, unit })
2122
2168
  }
2169
+ const turn = []
2123
2170
  add(field("userQuota", "user_quota"), "Credits")
2124
2171
  add(field("addOnQuota", "add_on_quota"), "Add-on credits")
2125
2172
  add(field("orgResourcePackage", "org_resource_package"), "Shared credits")
2173
+ // one count only when every pool counts in the same unit
2174
+ if (turn.length > 1 && turn.every((p) => p.unit === turn[0].unit)) {
2175
+ const used = turn.reduce((a, p) => a + p.used, 0)
2176
+ const total = turn.reduce((a, p) => a + p.total, 0)
2177
+ for (const p of turn) p.w.aside = true
2178
+ out.windows.unshift({ name: "Total credits", used: Math.min(100, (100 * used) / total), display: `${gfmt(used)} / ${gfmt(total)} ${turn[0].unit}` })
2179
+ }
2126
2180
  const dedicated = field("dedicatedResourcePackages", "dedicated_resource_packages")
2127
2181
  for (const b of Array.isArray(dedicated) ? dedicated : []) add(b, "Dedicated credits")
2128
2182
  return out
@@ -2586,16 +2640,21 @@ const makePlugin = (site) => async ({ client }) => {
2586
2640
  }
2587
2641
  if (listRes.status !== 200) return { outcome: "failed", message: `campaigns: HTTP ${listRes.status}` }
2588
2642
  const now = Date.now()
2589
- const active = (c) => (!c.startAt || c.startAt * 1000 <= now) && (!c.endAt || c.endAt * 1000 > now)
2643
+ const active = (c) => (at(c.startAt) ?? -Infinity) <= now && now < (at(c.endAt) ?? Infinity)
2590
2644
  const campaigns = (Array.isArray(list.campaigns) ? list.campaigns : []).filter((x) => x?.actionType === "CLAIM_BENEFIT")
2591
2645
  const campaign = campaigns.find((x) => x.claimStatus === "CLAIMABLE")
2592
2646
  if (!campaign) {
2593
2647
  const done = campaigns.find((x) => x.claimStatus === "CLAIMED" && active(x))
2594
2648
  if (done) {
2595
2649
  const amount = Number(done.benefit?.amount)
2596
- return { outcome: "done", ...(Number.isFinite(amount) && amount > 0 ? { credit: amount } : {}) }
2650
+ return { outcome: "done", ...(Number.isFinite(amount) && amount > 0 ? { credit: amount } : {}), ...askAgain(nextOpens(done)) }
2651
+ }
2652
+ // the next day's credits open at a time the list says: ask then,
2653
+ // not half an hour later
2654
+ if (campaigns.length) {
2655
+ const next = Math.min(...campaigns.flatMap((c) => [at(c.startAt), nextOpens(c)]).filter((t) => t > now))
2656
+ return { outcome: "failed", message: "campaigns: today's credits aren't offered yet", ...askAgain(next) }
2597
2657
  }
2598
- if (campaigns.length) return { outcome: "failed", message: "campaigns: today's credits aren't offered yet" }
2599
2658
  // Asked without Qoder CN's identity and offered nothing: what an
2600
2659
  // international list says to a request without one, so say why
2601
2660
  // (no Qoder CN on this machine) or ask again (it didn't answer).
@@ -2620,7 +2679,7 @@ const makePlugin = (site) => async ({ client }) => {
2620
2679
  if (claimRes.status !== 200) return { outcome: "failed", message: `claim: HTTP ${claimRes.status}` }
2621
2680
  if (said === "CLAIMED") {
2622
2681
  const amount = Number(claim?.benefit?.amount ?? claim?.data?.benefit?.amount ?? campaign.benefit?.amount)
2623
- return { outcome: "claimed", ...(Number.isFinite(amount) && amount > 0 ? { credit: amount } : {}) }
2682
+ return { outcome: "claimed", ...(Number.isFinite(amount) && amount > 0 ? { credit: amount } : {}), ...askAgain(nextOpens(campaign)) }
2624
2683
  }
2625
2684
  return { outcome: "failed", message: `claim: ${said || "unknown answer"}` }
2626
2685
  } catch (e) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-qoder-auth",
3
- "version": "0.2.42",
3
+ "version": "0.2.44",
4
4
  "description": "Qoder and QwenWork (千问办公) subscriptions in OpenCode and magpie: qoder.com, qoder.cn, gateway.qwenwork.cn and the enterprise flagship (VPC)",
5
5
  "type": "module",
6
6
  "main": "./index.mjs",