From 709e4dc79becb1a5e18736ad49b0c7d58e352b77 Mon Sep 17 00:00:00 2001 From: yetone Date: Sun, 4 Oct 2026 13:12:51 +0800 Subject: [PATCH] =?UTF-8?q?trae=200.1.7:=20the=20daily=20check-in=20is=20s?= =?UTF-8?q?ent=20as=20Trae=20CN's=20IDE=20sends=20it=20(Discord,=20ARNO=20?= =?UTF-8?q?on=20magpie=20v0.1.813:=20trae-cn=E7=9A=84=E7=AD=BE=E5=88=B0?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E4=BC=9A=E5=A4=B1=E8=B4=A5)=20=E2=80=94=20ow?= =?UTF-8?q?nRequest=20sent=20api.trae.cn's=20check-in=20pages=20with=20the?= =?UTF-8?q?=20chat's=20headers=20(x-uid,=20x-app-id,=20X-Cloudide-Token,?= =?UTF-8?q?=20x-ide-token,=20x-machine-id,=20x-ide-version=E2=80=A6=20as?= =?UTF-8?q?=20TRAE=20SOLO=20CN's=20ai-agent=20sends=20llm=5Futils=5Fchat)?= =?UTF-8?q?=20and=20the=20body=20magpie=20gave=20({}).=20Trae=20CN=203.3.1?= =?UTF-8?q?04's=20own=20client=20(out/main.js,=20eb()/fb(),=20from=20the?= =?UTF-8?q?=20linux=20tarball=20the=20version=20API=20names)=20posts=20/tr?= =?UTF-8?q?ae/api/v2/ug/checkin=5Fcredits/status=20and=20/claim=20with=20{?= =?UTF-8?q?req=5Fsource:=201}=20(2=20only=20for=20SOLO=20Lite/enterprise)?= =?UTF-8?q?=20and=20only=20Content-Type,=20Authorization:=20Cloud-IDE-JWT?= =?UTF-8?q?=20and=20the=20device's=20x-device-id,=20x-device-brand,=20x-de?= =?UTF-8?q?vice-type,=20x-os-version,=20x-app-version.=20Pages=20under=20/?= =?UTF-8?q?trae/api/v2/ug/=20now=20go=20with=20those=20headers=20(ugHeader?= =?UTF-8?q?s)=20and=20{req=5Fsource:1}=20where=20none=20or=20{}=20was=20gi?= =?UTF-8?q?ven=20(ugBody);=20Trae's=20other=20pages=20are=20sent=20as=20be?= =?UTF-8?q?fore.=20Trae's=209074=20(=E5=BD=93=E5=89=8D=E5=8F=82=E4=B8=8E?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E5=A4=AA=E5=A4=9A)=20is=20a=20device-level?= =?UTF-8?q?=20risk=20control=20that=20both=20open-source=20check-in=20clie?= =?UTF-8?q?nts=20say=20trips=20on=20requests=20that=20don't=20look=20like?= =?UTF-8?q?=20the=20client's;=20whether=20that=20is=20what=20ARNO=20saw=20?= =?UTF-8?q?is=20not=20known=20(no=20error=20text,=20and=20no=20Trae=20CN?= =?UTF-8?q?=20account=20to=20try=20with).=20checkin.test.mjs=20gains=20"th?= =?UTF-8?q?e=20check-in=20is=20sent=20as=20Trae=20CN's=20IDE=20sends=20it"?= =?UTF-8?q?=20(body=20and=20header=20set;=20fails=20on=200.1.6);=20bun=20t?= =?UTF-8?q?est=20(30=20pass)=20and=20bun=20scripts/check.mjs=20trae=20unde?= =?UTF-8?q?r=20a=20temp=20HOME=20pass;=20the=20plugin's=20fetch=20against?= =?UTF-8?q?=20the=20real=20api.trae.cn=20with=20a=20placeholder=20token=20?= =?UTF-8?q?answers=20200=20code=201001=20marked=20expired,=20so=20the=20ro?= =?UTF-8?q?ute=20is=20reached.=20Not=20tried=20with=20a=20real=20Trae=20CN?= =?UTF-8?q?=20account.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/trae/README.md | 10 ++++---- packages/trae/checkin.test.mjs | 21 +++++++++++++++-- packages/trae/index.mjs | 43 ++++++++++++++++++++++++++++++++-- packages/trae/package.json | 2 +- 4 files changed, 67 insertions(+), 9 deletions(-) diff --git a/packages/trae/README.md b/packages/trae/README.md index 5843e6c..49d4c37 100644 --- a/packages/trae/README.md +++ b/packages/trae/README.md @@ -114,13 +114,15 @@ magpie's usage card shows the account's credits: Trae CN gives credits for a daily check-in (每日签到). magpie can press it once a day for each account (Settings, or the switch on the usage card). It asks Trae CN's own pages through -this plugin's fetch, which sends them as the account: its Cloud-IDE-JWT -(renewed first when near its end) and its device id. +this plugin's fetch, which sends them as the account, as Trae CN's IDE +does: its Cloud-IDE-JWT (renewed first when near its end) and its +device's headers (`x-device-id`, `x-device-brand`, `x-device-type`, +`x-os-version`, `x-app-version`), and no others. - **Status:** `POST api.trae.cn/trae/api/v2/ug/checkin_credits/status`, - body `{}`: `enable`, `checked_in`, `credits`. + body `{"req_source":1}`: `enable`, `checked_in`, `credits`. - **Claim:** `POST api.trae.cn/trae/api/v2/ug/checkin_credits/claim`, - body `{}`, only while it is on and today's isn't in. + body `{"req_source":1}`, only while it is on and today's isn't in. - **What comes back:** Trae's answer as it is. A 401 or code 1001 marks the account for a new sign-in. Code 9095 means this device has checked in today; nothing sends another device id to get round it. diff --git a/packages/trae/checkin.test.mjs b/packages/trae/checkin.test.mjs index 5b49e49..04eaa93 100644 --- a/packages/trae/checkin.test.mjs +++ b/packages/trae/checkin.test.mjs @@ -28,10 +28,27 @@ test("a check-in page is sent as the account, its answer back as it is", async ( expect(f.seen.map((r) => r.path)).toEqual(["/trae/api/v2/ug/checkin_credits/status", "/trae/api/v2/ug/checkin_credits/claim"]) for (const r of f.seen) { expect(r.method).toBe("POST") - expect(r.json).toEqual({}) expect(r.headers.get("authorization")).toBe("Cloud-IDE-JWT jwt-1") expect(r.headers.get("x-device-id")).toBe("1234567890123456789") - expect(r.headers.get("x-uid")).toBe("u-1") + } +}) + +// Trae CN's IDE (3.3.104, out/main.js eb()/fb()) posts the check-in with +// {req_source: 1} and only the JWT and the device's headers; the chat's +// own (x-uid, x-app-id, x-ide-token …) aren't among them +test("the check-in is sent as Trae CN's IDE sends it", async () => { + f = fakeTrae() + f.route("POST /trae/api/v2/ug/checkin_credits/status", () => json({ code: 0, enable: true, checked_in: false, credits: 100 })) + f.route("POST /trae/api/v2/ug/checkin_credits/claim", () => json({ code: 0 })) + const o = await fetcher() + await o.fetch(f.origin + "/trae/api/v2/ug/checkin_credits/status", { method: "POST", body: "{}" }) + await o.fetch(f.origin + "/trae/api/v2/ug/checkin_credits/claim", { method: "POST" }) + expect(f.seen.length).toBe(2) + for (const r of f.seen) { + expect(r.json).toEqual({ req_source: 1 }) + const names = [...r.headers.keys()].filter((k) => k === "authorization" || k.startsWith("x-")).sort() + expect(names).toEqual(["authorization", "x-app-version", "x-device-brand", "x-device-id", "x-device-type", "x-os-version"]) + expect(r.headers.get("content-type")).toBe("application/json") } }) diff --git a/packages/trae/index.mjs b/packages/trae/index.mjs index 2ce6e2b..c5fd16c 100644 --- a/packages/trae/index.mjs +++ b/packages/trae/index.mjs @@ -366,6 +366,42 @@ function ideHeaders(a, extra = {}) { } } +// ugHeaders are the headers Trae CN's IDE sends its growth pages +// (api.trae.cn/trae/api/v2/ug/…, the daily check-in) with, and no others: +// its eb()/fb() (Trae CN 3.3.104's out/main.js) send Content-Type, the +// Cloud-IDE-JWT and the device (x-device-id, x-device-brand, +// x-device-type, x-os-version, x-app-version), none of the chat's +// x-uid, x-app-id, x-ide-token and the like +function ugHeaders(a, extra = {}) { + return { + "Content-Type": "application/json", + Authorization: `Cloud-IDE-JWT ${a.token}`, + "x-device-id": a.deviceId, + "x-device-brand": BRAND, + "x-device-type": "windows", + "x-os-version": "Windows 10", + "x-app-version": CLIENT_VERSION, + ...extra, + } +} + +// ugBody is the body the IDE posts a growth page: {req_source: 1}, its +// own (2 is SOLO Lite's), where magpie gave none or {}; anything else +// given goes as it is +function ugBody(body) { + const s = typeof body === "string" ? body.trim() : "" + if (s === "" || s === "{}") return JSON.stringify({ req_source: 1 }) + return s +} + +const ugPage = (url) => { + try { + return new URL(url).pathname.startsWith("/trae/api/v2/ug/") + } catch { + return false + } +} + // Trae's chat takes text only and has no turn for a tool call or its // result. The agent's tools are named to it natively and in a system // prompt that asks for each call as a tagged block; earlier calls and @@ -848,10 +884,13 @@ export const TraeCNAuthPlugin = async ({ client }) => { if (e instanceof Expired) return errorResponse(401, `${a0.name || a0.uid}: ${e.message}`, "expired") throw e } + // the check-in goes as the IDE sends it (ugHeaders, ugBody); Trae's + // other pages with the chat's headers + const ug = ugPage(url) const res = await fetch(url, { method, - headers: ideHeaders(a, { Accept: "application/json" }), - body: method === "GET" || method === "HEAD" ? undefined : typeof body === "string" ? body : "{}", + headers: ug ? ugHeaders(a, { Accept: "application/json" }) : ideHeaders(a, { Accept: "application/json" }), + body: method === "GET" || method === "HEAD" ? undefined : ug ? ugBody(body) : typeof body === "string" ? body : "{}", signal: signal ?? AbortSignal.timeout(20_000), }) const text = await res.text() diff --git a/packages/trae/package.json b/packages/trae/package.json index de8a813..15c26ad 100644 --- a/packages/trae/package.json +++ b/packages/trae/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-trae-auth", - "version": "0.1.6", + "version": "0.1.7", "description": "OpenCode provider plugin for Trae CN (trae.cn): your Trae CN account's models, free tier included, with Trae's own browser sign-in", "type": "module", "main": "./index.mjs",