@magpie-community/opencode-trae-auth 0.1.6 → 0.1.7

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 +6 -4
  2. package/index.mjs +41 -2
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -114,13 +114,15 @@ magpie's usage card shows the account's credits:
114
114
  Trae CN gives credits for a daily check-in (每日签到). magpie can press it
115
115
  once a day for each account (Settings, or the switch on the usage card).
116
116
  It asks Trae CN's own pages through
117
- this plugin's fetch, which sends them as the account: its Cloud-IDE-JWT
118
- (renewed first when near its end) and its device id.
117
+ this plugin's fetch, which sends them as the account, as Trae CN's IDE
118
+ does: its Cloud-IDE-JWT (renewed first when near its end) and its
119
+ device's headers (`x-device-id`, `x-device-brand`, `x-device-type`,
120
+ `x-os-version`, `x-app-version`), and no others.
119
121
 
120
122
  - **Status:** `POST api.trae.cn/trae/api/v2/ug/checkin_credits/status`,
121
- body `{}`: `enable`, `checked_in`, `credits`.
123
+ body `{"req_source":1}`: `enable`, `checked_in`, `credits`.
122
124
  - **Claim:** `POST api.trae.cn/trae/api/v2/ug/checkin_credits/claim`,
123
- body `{}`, only while it is on and today's isn't in.
125
+ body `{"req_source":1}`, only while it is on and today's isn't in.
124
126
  - **What comes back:** Trae's answer as it is. A 401 or code 1001 marks
125
127
  the account for a new sign-in. Code 9095 means this device has checked
126
128
  in today; nothing sends another device id to get round it.
package/index.mjs CHANGED
@@ -366,6 +366,42 @@ function ideHeaders(a, extra = {}) {
366
366
  }
367
367
  }
368
368
 
369
+ // ugHeaders are the headers Trae CN's IDE sends its growth pages
370
+ // (api.trae.cn/trae/api/v2/ug/…, the daily check-in) with, and no others:
371
+ // its eb()/fb() (Trae CN 3.3.104's out/main.js) send Content-Type, the
372
+ // Cloud-IDE-JWT and the device (x-device-id, x-device-brand,
373
+ // x-device-type, x-os-version, x-app-version), none of the chat's
374
+ // x-uid, x-app-id, x-ide-token and the like
375
+ function ugHeaders(a, extra = {}) {
376
+ return {
377
+ "Content-Type": "application/json",
378
+ Authorization: `Cloud-IDE-JWT ${a.token}`,
379
+ "x-device-id": a.deviceId,
380
+ "x-device-brand": BRAND,
381
+ "x-device-type": "windows",
382
+ "x-os-version": "Windows 10",
383
+ "x-app-version": CLIENT_VERSION,
384
+ ...extra,
385
+ }
386
+ }
387
+
388
+ // ugBody is the body the IDE posts a growth page: {req_source: 1}, its
389
+ // own (2 is SOLO Lite's), where magpie gave none or {}; anything else
390
+ // given goes as it is
391
+ function ugBody(body) {
392
+ const s = typeof body === "string" ? body.trim() : ""
393
+ if (s === "" || s === "{}") return JSON.stringify({ req_source: 1 })
394
+ return s
395
+ }
396
+
397
+ const ugPage = (url) => {
398
+ try {
399
+ return new URL(url).pathname.startsWith("/trae/api/v2/ug/")
400
+ } catch {
401
+ return false
402
+ }
403
+ }
404
+
369
405
  // Trae's chat takes text only and has no turn for a tool call or its
370
406
  // result. The agent's tools are named to it natively and in a system
371
407
  // prompt that asks for each call as a tagged block; earlier calls and
@@ -848,10 +884,13 @@ export const TraeCNAuthPlugin = async ({ client }) => {
848
884
  if (e instanceof Expired) return errorResponse(401, `${a0.name || a0.uid}: ${e.message}`, "expired")
849
885
  throw e
850
886
  }
887
+ // the check-in goes as the IDE sends it (ugHeaders, ugBody); Trae's
888
+ // other pages with the chat's headers
889
+ const ug = ugPage(url)
851
890
  const res = await fetch(url, {
852
891
  method,
853
- headers: ideHeaders(a, { Accept: "application/json" }),
854
- body: method === "GET" || method === "HEAD" ? undefined : typeof body === "string" ? body : "{}",
892
+ headers: ug ? ugHeaders(a, { Accept: "application/json" }) : ideHeaders(a, { Accept: "application/json" }),
893
+ body: method === "GET" || method === "HEAD" ? undefined : ug ? ugBody(body) : typeof body === "string" ? body : "{}",
855
894
  signal: signal ?? AbortSignal.timeout(20_000),
856
895
  })
857
896
  const text = await res.text()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-trae-auth",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
4
4
  "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",
5
5
  "type": "module",
6
6
  "main": "./index.mjs",