diff --git a/packages/trae/README.md b/packages/trae/README.md index f6ba0c6..70b4dfc 100644 --- a/packages/trae/README.md +++ b/packages/trae/README.md @@ -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 diff --git a/packages/trae/index.mjs b/packages/trae/index.mjs index f926515..ee78185 100644 --- a/packages/trae/index.mjs +++ b/packages/trae/index.mjs @@ -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 diff --git a/packages/trae/native-checkin.test.mjs b/packages/trae/native-checkin.test.mjs new file mode 100644 index 0000000..90472a6 --- /dev/null +++ b/packages/trae/native-checkin.test.mjs @@ -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("gateway")) + 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" }) +}) diff --git a/packages/trae/package.json b/packages/trae/package.json index 506eca0..57fc9fd 100644 --- a/packages/trae/package.json +++ b/packages/trae/package.json @@ -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",