trae 0.2.17: Trae CN's daily check-in through the plugin (#178)

Trae CN's daily check-in moves into the plugin as auth.checkin. It asks
/trae/api/v2/ug/checkin_credits/status and then /claim through ownRequest,
so it carries the same JWT renewal, IDE headers, {req_source: 1} body and
16-digit ugDevice (#194) as every other ug page. Trae Global answers
inactive. 9095 on the claim is ineligible, the same as magpie's built-in
traeRefused. 9074 on the claim is an answer for the day: ineligible, with a
hint to check in in the Trae CN app. The built-in calls that own-app, which
the plugin protocol doesn't have. trae 0.2.17.

Run at the reviewed head 4e9bb87 (on top of origin/main 63a3d2a..c397e96):
- bun test packages/trae: 160 pass, 0 fail. The global.test.mjs and
  stream.test.mjs failures the author saw did not reproduce.
- bun scripts/check.mjs trae: ok.
- Broken by hand, one at a time, and each turned native-checkin.test.mjs red:
  9074 mapped to failed; the ugDevice line in ownRequest removed; the
  unreadable-claim guard removed.
- magpie's plugin host (yetone/magpie 85eef5d87 build, sandbox HOME, a copy
  of the package pointed at a fake Trae on 127.0.0.1:18764), via
  `magpie accounts checkin --json`. The results were claimed 100, 9074
  ineligible with the app hint, 9095 ineligible, and checked_in done 100
  with no claim sent. Status and claim both went out with
  x-device-id 2866487253502651 and {"req_source":1}.
This commit is contained in:
Euraxluo
2026-10-17 18:06:54 +08:00
committed by GitHub
parent c397e96b77
commit 1d58133eaa
4 changed files with 132 additions and 2 deletions
+5 -1
View File
@@ -281,7 +281,11 @@ an empty answer.
Trae CN gives credits for a daily check-in (每日签到); Trae Global has no
such page, so this is CN only. magpie can press it
once a day for each account (Settings, or the switch on the usage card).
once a day for each account (Settings, or the switch on the usage card),
through `auth.checkin`: `claimed` when Trae grants the credits, `done`
when the status or Trae's 9095 says this device has already claimed,
`inactive` when the page is disabled, and `failed` for a turned-away
sign-in or Trae's 9074 busy answer.
It asks Trae CN's own pages through
this plugin's fetch, which sends them as the account, as Trae CN's IDE
(3.3.104) sends them: its Cloud-IDE-JWT (renewed first when near its
+38
View File
@@ -113,6 +113,10 @@ const SITES = {
// Trae CN's account info carries no region: its one deployment is the
// one it is asked through
regionOf: () => "",
checkin: {
status: { path: "/trae/api/v2/ug/checkin_credits/status" },
claim: { path: "/trae/api/v2/ug/checkin_credits/claim" },
},
// where usage is asked, and which page it is (CN: credits)
usage: {
kind: "credits",
@@ -2530,6 +2534,39 @@ const makePlugin = (site) => async ({ client }) => {
return new Response(text, { status: res.status, headers })
}
// checkin asks Trae CN's own pages as magpie's check-in host does, with
// the same account, device and renewal as any own page. Trae Global has
// no such page.
const checkin = async (getAuth) => {
if (!site.checkin) return { outcome: "inactive", message: `${site.realm} has no daily check-in` }
const a0 = fromAuth(await getAuth(), site)
if (!a0) return { outcome: "failed", message: "not signed in" }
const base = site.hosts.auth + site.checkin.status.path
const status = await ownRequest(getAuth, a0, base, "POST", "{}")
const statusText = await status.text()
let body
try { body = JSON.parse(statusText) } catch {}
if (!body || typeof body !== "object" || Array.isArray(body)) return { outcome: "failed", message: `check-in status: unreadable reply ${statusText.trim().slice(0, 120) || "(empty)"}` }
const e = errorOf(body)
if (status.status === 401 || lapsedCode(e.code, site)) return { outcome: "failed", message: e.message || `${a0.name || a0.uid}: Trae's sign-in has expired` }
if (status.status !== 200 || (e.code && String(e.code) !== "0")) return { outcome: "failed", message: e.message || statusLine(status.status) }
if (body?.checked_in) return { outcome: "done", ...(Number(body.credits) > 0 ? { credit: Number(body.credits) } : {}) }
if (!body?.enable) return { outcome: "inactive" }
const claim = await ownRequest(getAuth, a0, site.hosts.auth + site.checkin.claim.path, "POST", "{}")
const claimText = await claim.text()
let answer
try { answer = JSON.parse(claimText) } catch {}
const ce = errorOf(answer)
if (claim.status === 401 || lapsedCode(ce.code, site)) return { outcome: "failed", message: ce.message || `${a0.name || a0.uid}: Trae's sign-in has expired` }
if (String(ce.code) === "9095") return { outcome: "ineligible", message: ce.message || "this device has checked in today" }
if (String(ce.code) === "9074") return { outcome: "ineligible", message: `${ce.message} — check in in the Trae CN app` }
if (claim.status !== 200 || (ce.code && String(ce.code) !== "0")) return { outcome: "failed", message: ce.message || statusLine(claim.status) }
if (!answer || typeof answer !== "object" || Array.isArray(answer)) return { outcome: "failed", message: `check-in claim: unreadable reply ${claimText.trim().slice(0, 120) || "(empty)"}` }
const credit = Number(answer.credits) + Number(answer.extra_credits || 0)
const fallback = Number(body.credits) + Number(body.extra_credits || 0)
return { outcome: "claimed", ...(credit > 0 ? { credit } : fallback > 0 ? { credit: fallback } : {}) }
}
const usage = async (getAuth) => {
let a
try {
@@ -2773,6 +2810,7 @@ const makePlugin = (site) => async ({ client }) => {
auth: {
provider: site.id,
usage,
checkin,
// magpie renews the token LEAD_MS before its end, once, before its
// requests, models and usage ask; the check before each request
// stays for OpenCode, which doesn't call this
+88
View File
@@ -0,0 +1,88 @@
import "./nonet.mjs"
import { afterEach, expect, test } from "bun:test"
import { TraeCNAuthPlugin, TraeGlobalAuthPlugin } from "./index.mjs"
import { fakeTrae, json, signedIn } from "./fake.mjs"
let f
afterEach(() => f?.close())
const STATUS = "/trae/api/v2/ug/checkin_credits/status"
const CLAIM = "/trae/api/v2/ug/checkin_credits/claim"
async function checkin(plugin = TraeCNAuthPlugin, auth = signedIn(), client = {}) {
const hooks = await plugin({ client })
return hooks.auth.checkin(async () => auth)
}
test("a claimable day is claimed through Trae's own pages", async () => {
f = fakeTrae()
f.route("POST " + STATUS, () => json({ code: 0, enable: true, checked_in: false }))
f.route("POST " + CLAIM, () => json({ code: 0, credits: 100 }))
expect(await checkin()).toEqual({ outcome: "claimed", credit: 100 })
expect(f.seen.map((r) => r.path)).toEqual([STATUS, CLAIM])
for (const r of f.seen) {
expect(r.json).toEqual({ req_source: 1 })
expect(r.headers.get("authorization")).toBe("Cloud-IDE-JWT jwt-1")
}
})
test("an already-checked day is done without claiming", async () => {
f = fakeTrae()
f.route("POST " + STATUS, () => json({ code: 0, enable: true, checked_in: true, credits: 100 }))
expect(await checkin()).toEqual({ outcome: "done", credit: 100 })
expect(f.seen.map((r) => r.path)).toEqual([STATUS])
})
test("a disabled day is inactive", async () => {
f = fakeTrae()
f.route("POST " + STATUS, () => json({ code: 0, enable: false }))
expect(await checkin()).toEqual({ outcome: "inactive" })
expect(f.seen.map((r) => r.path)).toEqual([STATUS])
})
test("the device's already-checked answer is ineligible", async () => {
f = fakeTrae()
f.route("POST " + STATUS, () => json({ code: 0, enable: true, checked_in: false }))
f.route("POST " + CLAIM, () => json({ code: 9095, message: "this device has checked in today", credits: 100 }))
expect(await checkin()).toEqual({ outcome: "ineligible", message: "this device has checked in today" })
})
test("Trae's busy answer is failed without another device", async () => {
f = fakeTrae()
f.route("POST " + STATUS, () => json({ code: 0, enable: true, checked_in: false }))
f.route("POST " + CLAIM, () => json({ code: 9074, message: "当前参与用户太多" }))
const out = await checkin()
expect(out.outcome).toBe("ineligible")
expect(out.message).toContain("当前参与用户太多")
expect(out.message).toContain("check in in the Trae CN app")
// the check-in rides ugDevice like every other ug page: the 19-digit
// sign-in device becomes the account's own 16-digit shape (#194)
expect(f.seen.map((r) => r.headers.get("x-device-id"))).toEqual(["2866487253502651", "2866487253502651"])
})
test("an unreadable status reply is failed", async () => {
f = fakeTrae()
f.route("POST " + STATUS, () => new Response("<html>gateway</html>"))
const out = await checkin()
expect(out).toMatchObject({ outcome: "failed" })
expect(out.message).toContain("unreadable")
})
test("an unreadable claim reply is failed, never claimed", async () => {
f = fakeTrae()
f.route("POST " + STATUS, () => json({ code: 0, enable: true, checked_in: false }))
f.route("POST " + CLAIM, () => new Response(""))
const out = await checkin()
expect(out).toMatchObject({ outcome: "failed" })
expect(out.message).toContain("unreadable")
})
test("an expired sign-in is failed and marked", async () => {
f = fakeTrae()
f.route("POST " + STATUS, () => json({ code: 1001, message: "not able to authenticate you" }))
const out = await checkin()
expect(out).toMatchObject({ outcome: "failed" })
})
test("Trae Global has no check-in", async () => {
expect(await checkin(TraeGlobalAuthPlugin)).toEqual({ outcome: "inactive", message: "Trae Global has no daily check-in" })
})
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-trae-auth",
"version": "0.2.16",
"version": "0.2.17",
"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",
"type": "module",
"main": "./index.mjs",