From 3acb0fc3770bfe7f8ecf9b174d9935adc3b5ea42 Mon Sep 17 00:00:00 2001 From: yetone Date: Mon, 5 Oct 2026 21:15:36 +0800 Subject: [PATCH] trae 0.2.1: the daily check-in is sent as Trae CN's IDE sends it, with a client's User-Agent, not Bun's (yetone/magpie#808, whqtian on Discord) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Trae CN's check-in answered magpie with 9074 「当前参与用户太多,请稍后再试」 every time: whqtian's three accounts never got in (the fourth only when checked in by hand in Trae), Moody-Sin's never did. magpie retried every 30 minutes and got 9074 again. The plugin sent the check-in with Bun's own User-Agent (Bun/1.x), the chat's headers (x-uid, x-ide-token, versions…) and {}. Trae CN's IDE 3.3.104 (main.js, read from the official linux build) sends /trae/api/v2/ug/checkin_credits/status and /claim with only its Cloud-IDE-JWT and its device (fb: x-device-id, x-device-brand, x-device-type, x-os-version, x-app-version) through its own client, body {req_source: 1}; qilimixingkong/trae-checkin found that a check-in without a real client's User-Agent is taken for a script and answered 9074. A growth page (/trae/api/v2/ug/…) now goes with those headers, a TRAE SOLO CN Electron User-Agent and {req_source: 1} for an empty body; Trae's other pages still go with the chat's headers. 0.1.7 tried the IDE's headers and body without a User-Agent and was put back in 0.1.9; the User-Agent is what neither had. No device id is changed to get round a 9074 or a 9095. No Trae CN account was at hand, so it isn't tried against a real one: api.trae.cn answers the new request without a token with its usual 1001. checkin.test.mjs checks the body, the IDE's headers, the User-Agent and none of the chat's (fails on 0.2.0), a body given as is, and Trae's other pages unchanged; bun test (66) and bun scripts/check.mjs trae pass. --- packages/trae/README.md | 17 +++++++---- packages/trae/checkin.test.mjs | 30 ++++++++++++++++++-- packages/trae/index.mjs | 52 ++++++++++++++++++++++++++++++---- packages/trae/package.json | 2 +- 4 files changed, 87 insertions(+), 14 deletions(-) diff --git a/packages/trae/README.md b/packages/trae/README.md index abe9912..542e8dd 100644 --- a/packages/trae/README.md +++ b/packages/trae/README.md @@ -192,16 +192,23 @@ 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). 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 +(3.3.104) sends them: its Cloud-IDE-JWT (renewed first when near its +end), its device (`x-device-id`, `x-device-brand`, `x-device-type`, +`x-os-version`, `x-app-version`) and a client's User-Agent (TRAE SOLO +CN's Electron shell). Since 0.2.1: Bun's own User-Agent and the chat's +headers got 9074 「当前参与用户太多,请稍后再试」 every time +(yetone/magpie#808). - **Status:** `POST api.trae.cn/trae/api/v2/ug/checkin_credits/status`, - body `{}`: `enable`, `checked_in`, `credits`. + body `{"req_source":1}` (magpie's `{}` is sent as that): `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. + the same, 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. + in today; nothing sends another device id to get round it, nor to get + round a 9074. Only `api.trae.cn`'s `/trae/api/` pages are sent this way; any other URL that isn't a chat request is still refused (400). Needs 0.1.4 or later. diff --git a/packages/trae/checkin.test.mjs b/packages/trae/checkin.test.mjs index 5b49e49..4357c27 100644 --- a/packages/trae/checkin.test.mjs +++ b/packages/trae/checkin.test.mjs @@ -28,13 +28,39 @@ 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({}) + // as Trae CN's IDE sends it (yetone/magpie#808): {req_source: 1}, its + // device headers and a client's User-Agent, not Bun's + expect(r.json).toEqual({ req_source: 1 }) 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") + expect(r.headers.get("x-device-type")).toBe("Windows") + expect(r.headers.get("x-os-version")).toBe("10.0.22631") + expect(r.headers.get("x-app-version")).toBe("0.1.69") + expect(r.headers.get("x-device-brand")).toBeTruthy() + expect(r.headers.get("user-agent")).toMatch(/^Mozilla\/5\.0 \(Windows NT 10\.0; Win64; x64\) .*TRAE-SOLO-CN\/0\.1\.69 Chrome\/[\d.]+ Electron\/[\d.]+ Safari\/537\.36$/) + expect(r.headers.get("user-agent")).not.toMatch(/Bun/) + // not the chat's + for (const h of ["x-uid", "x-ide-token", "x-cloudide-token", "x-ide-version", "x-machine-id"]) expect(r.headers.has(h)).toBe(false) } }) +test("a check-in's own body is sent as given", async () => { + f = fakeTrae() + f.route("POST /trae/api/v2/ug/checkin_credits/status", () => json({ code: 0, enable: true, checked_in: true })) + const o = await fetcher() + await o.fetch(f.origin + "/trae/api/v2/ug/checkin_credits/status", { method: "POST", body: '{"req_source":2}' }) + expect(f.seen[0].json).toEqual({ req_source: 2 }) +}) + +test("Trae's other pages still go with the chat's headers", async () => { + f = fakeTrae() + f.route("POST /trae/api/v2/pay/ide_user_ent_usage", () => json({ code: 0 })) + const o = await fetcher() + await o.fetch(f.origin + "/trae/api/v2/pay/ide_user_ent_usage", { method: "POST", body: "{}" }) + expect(f.seen[0].json).toEqual({}) + expect(f.seen[0].headers.get("x-uid")).toBe("u-1") +}) + test("a token Trae turns away marks the sign-in", async () => { f = fakeTrae() f.route("POST /trae/api/v2/ug/checkin_credits/status", () => json({ code: 1001, enable: false, checked_in: false, message: "not able to authenticate you" })) diff --git a/packages/trae/index.mjs b/packages/trae/index.mjs index 48c1f0c..d0f5db8 100644 --- a/packages/trae/index.mjs +++ b/packages/trae/index.mjs @@ -529,6 +529,42 @@ function ideHeaders(a, site, extra = {}) { } } +// ugUA is the User-Agent a growth page (/trae/api/v2/ug/…: the daily +// check-in) is sent with: TRAE SOLO CN's Electron shell, as its version +// is named. Bun's own (Bun/1.x) reads as a script, and Trae answers a +// script's check-in with 9074 「当前参与用户太多,请稍后再试」 however +// seldom it asks (yetone/magpie#808; qilimixingkong/trae-checkin found +// the same). +const ugUA = (site) => + `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) TRAE-SOLO-CN/${site.clientVersion} Chrome/120.0.6099.291 Electron/28.2.10 Safari/537.36` + +// ugHeaders are the headers Trae CN's IDE (3.3.104) sends a growth page +// with, and only those: its JWT, then its device (fb: x-device-id, +// x-device-brand, x-device-type, x-os-version, x-app-version), not the +// chat's tokens and versions. +function ugHeaders(a, site, extra = {}) { + return { + "Content-Type": "application/json", + Authorization: `Cloud-IDE-JWT ${a.token}`, + "User-Agent": ugUA(site), + "x-device-id": a.deviceId, + "x-device-brand": site.brand, + "x-device-type": "Windows", + "x-os-version": "10.0.22631", + "x-app-version": site.clientVersion, + ...extra, + } +} + +// ugPage: url is one of Trae's growth pages (the check-in) +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 @@ -1461,14 +1497,18 @@ const makePlugin = (site) => async ({ client }) => { if (e instanceof Expired) return errorResponse(401, `${a0.name || a0.uid}: ${e.message}`, "expired") throw e } - // Trae's pages, the check-in among them, go with the chat's headers and - // the body given ({}): 0.1.7 sent the check-in with the IDE's growth - // headers and {req_source: 1} instead, and a check-in that worked on - // 0.1.6 failed from then on (yetone/magpie#808) + // a growth page (the check-in) goes as the IDE sends it: its headers, + // a client's User-Agent, and {req_source: 1} for an empty body. Every + // check-in sent with Bun's User-Agent came back 9074, with the chat's + // headers and {} (0.1.6, 0.1.9) as with the IDE's (0.1.7) + // (yetone/magpie#808). Other pages go with the chat's headers. + const ug = ugPage(url) + let sent = method === "GET" || method === "HEAD" ? undefined : typeof body === "string" ? body : "{}" + if (ug && sent !== undefined && /^\s*(\{\s*\})?\s*$/.test(sent)) sent = JSON.stringify({ req_source: 1 }) const res = await fetch(url, { method, - headers: ideHeaders(a, site, { Accept: "application/json" }), - body: method === "GET" || method === "HEAD" ? undefined : typeof body === "string" ? body : "{}", + headers: ug ? ugHeaders(a, site, { Accept: "application/json" }) : ideHeaders(a, site, { Accept: "application/json" }), + body: sent, signal: signal ?? AbortSignal.timeout(20_000), }) const text = await res.text() diff --git a/packages/trae/package.json b/packages/trae/package.json index bae84c2..92875dd 100644 --- a/packages/trae/package.json +++ b/packages/trae/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-trae-auth", - "version": "0.2.0", + "version": "0.2.1", "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",