@magpie-community/opencode-trae-auth 0.2.16 → 0.2.17

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 +5 -1
  2. package/index.mjs +38 -0
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -281,7 +281,11 @@ an empty answer.
281
281
 
282
282
  Trae CN gives credits for a daily check-in (每日签到); Trae Global has no
283
283
  such page, so this is CN only. magpie can press it
284
- once a day for each account (Settings, or the switch on the usage card).
284
+ once a day for each account (Settings, or the switch on the usage card),
285
+ through `auth.checkin`: `claimed` when Trae grants the credits, `done`
286
+ when the status or Trae's 9095 says this device has already claimed,
287
+ `inactive` when the page is disabled, and `failed` for a turned-away
288
+ sign-in or Trae's 9074 busy answer.
285
289
  It asks Trae CN's own pages through
286
290
  this plugin's fetch, which sends them as the account, as Trae CN's IDE
287
291
  (3.3.104) sends them: its Cloud-IDE-JWT (renewed first when near its
package/index.mjs CHANGED
@@ -113,6 +113,10 @@ const SITES = {
113
113
  // Trae CN's account info carries no region: its one deployment is the
114
114
  // one it is asked through
115
115
  regionOf: () => "",
116
+ checkin: {
117
+ status: { path: "/trae/api/v2/ug/checkin_credits/status" },
118
+ claim: { path: "/trae/api/v2/ug/checkin_credits/claim" },
119
+ },
116
120
  // where usage is asked, and which page it is (CN: credits)
117
121
  usage: {
118
122
  kind: "credits",
@@ -2530,6 +2534,39 @@ const makePlugin = (site) => async ({ client }) => {
2530
2534
  return new Response(text, { status: res.status, headers })
2531
2535
  }
2532
2536
 
2537
+ // checkin asks Trae CN's own pages as magpie's check-in host does, with
2538
+ // the same account, device and renewal as any own page. Trae Global has
2539
+ // no such page.
2540
+ const checkin = async (getAuth) => {
2541
+ if (!site.checkin) return { outcome: "inactive", message: `${site.realm} has no daily check-in` }
2542
+ const a0 = fromAuth(await getAuth(), site)
2543
+ if (!a0) return { outcome: "failed", message: "not signed in" }
2544
+ const base = site.hosts.auth + site.checkin.status.path
2545
+ const status = await ownRequest(getAuth, a0, base, "POST", "{}")
2546
+ const statusText = await status.text()
2547
+ let body
2548
+ try { body = JSON.parse(statusText) } catch {}
2549
+ if (!body || typeof body !== "object" || Array.isArray(body)) return { outcome: "failed", message: `check-in status: unreadable reply ${statusText.trim().slice(0, 120) || "(empty)"}` }
2550
+ const e = errorOf(body)
2551
+ if (status.status === 401 || lapsedCode(e.code, site)) return { outcome: "failed", message: e.message || `${a0.name || a0.uid}: Trae's sign-in has expired` }
2552
+ if (status.status !== 200 || (e.code && String(e.code) !== "0")) return { outcome: "failed", message: e.message || statusLine(status.status) }
2553
+ if (body?.checked_in) return { outcome: "done", ...(Number(body.credits) > 0 ? { credit: Number(body.credits) } : {}) }
2554
+ if (!body?.enable) return { outcome: "inactive" }
2555
+ const claim = await ownRequest(getAuth, a0, site.hosts.auth + site.checkin.claim.path, "POST", "{}")
2556
+ const claimText = await claim.text()
2557
+ let answer
2558
+ try { answer = JSON.parse(claimText) } catch {}
2559
+ const ce = errorOf(answer)
2560
+ if (claim.status === 401 || lapsedCode(ce.code, site)) return { outcome: "failed", message: ce.message || `${a0.name || a0.uid}: Trae's sign-in has expired` }
2561
+ if (String(ce.code) === "9095") return { outcome: "ineligible", message: ce.message || "this device has checked in today" }
2562
+ if (String(ce.code) === "9074") return { outcome: "ineligible", message: `${ce.message} — check in in the Trae CN app` }
2563
+ if (claim.status !== 200 || (ce.code && String(ce.code) !== "0")) return { outcome: "failed", message: ce.message || statusLine(claim.status) }
2564
+ if (!answer || typeof answer !== "object" || Array.isArray(answer)) return { outcome: "failed", message: `check-in claim: unreadable reply ${claimText.trim().slice(0, 120) || "(empty)"}` }
2565
+ const credit = Number(answer.credits) + Number(answer.extra_credits || 0)
2566
+ const fallback = Number(body.credits) + Number(body.extra_credits || 0)
2567
+ return { outcome: "claimed", ...(credit > 0 ? { credit } : fallback > 0 ? { credit: fallback } : {}) }
2568
+ }
2569
+
2533
2570
  const usage = async (getAuth) => {
2534
2571
  let a
2535
2572
  try {
@@ -2773,6 +2810,7 @@ const makePlugin = (site) => async ({ client }) => {
2773
2810
  auth: {
2774
2811
  provider: site.id,
2775
2812
  usage,
2813
+ checkin,
2776
2814
  // magpie renews the token LEAD_MS before its end, once, before its
2777
2815
  // requests, models and usage ask; the check before each request
2778
2816
  // stays for OpenCode, which doesn't call this
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-trae-auth",
3
- "version": "0.2.16",
3
+ "version": "0.2.17",
4
4
  "description": "OpenCode provider plugins for Trae: Trae CN (trae.cn) and Trae Global (trae.ai), free tier included, with Trae's own browser sign-in",
5
5
  "type": "module",
6
6
  "main": "./index.mjs",