@magpie-community/opencode-trae-auth 0.1.5 → 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 +9 -6
  2. package/index.mjs +46 -4
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -105,21 +105,24 @@ magpie's usage card shows the account's credits:
105
105
  - **Source:** `api.trae.cn/trae/api/v2/pay/ide_user_ent_usage`.
106
106
  - **What it adds up:** each entitlement pack's `credits_limit` (-1 means
107
107
  unlimited) and what the pack has used.
108
- - **What it shows:** credits left as the balance, and the share used as a
109
- window.
108
+ - **What it shows:** one Credits window: the credits used of the packs'
109
+ total, with the share, which magpie shows as used or left like
110
+ WorkBuddy's.
110
111
 
111
112
  ## Daily check-in
112
113
 
113
114
  Trae CN gives credits for a daily check-in (每日签到). magpie can press it
114
115
  once a day for each account (Settings, or the switch on the usage card).
115
116
  It asks Trae CN's own pages through
116
- this plugin's fetch, which sends them as the account: its Cloud-IDE-JWT
117
- (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.
118
121
 
119
122
  - **Status:** `POST api.trae.cn/trae/api/v2/ug/checkin_credits/status`,
120
- body `{}`: `enable`, `checked_in`, `credits`.
123
+ body `{"req_source":1}`: `enable`, `checked_in`, `credits`.
121
124
  - **Claim:** `POST api.trae.cn/trae/api/v2/ug/checkin_credits/claim`,
122
- 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.
123
126
  - **What comes back:** Trae's answer as it is. A 401 or code 1001 marks
124
127
  the account for a new sign-in. Code 9095 means this device has checked
125
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()
@@ -881,8 +920,11 @@ export const TraeCNAuthPlugin = async ({ client }) => {
881
920
  const out = { plan: r.v.is_credits_billing ? "Credits" : "Free", user: a.name || a.uid, signIn }
882
921
  if (c.unlimited) out.balance = "unlimited"
883
922
  else if (c.limit > 0) {
884
- out.balance = `${round(Math.max(0, c.limit - c.used))} of ${round(c.limit)} credits left`
885
- const w = { name: "Credits", used: Math.max(0, Math.min(100, (c.used / c.limit) * 100)) }
923
+ // the count rides on the window (amount of limit, in credits), as
924
+ // WorkBuddy's does, so magpie says it in the window's row, used or
925
+ // left, in its own words and number format, rather than as an
926
+ // English balance line beside it (yetone/magpie#694)
927
+ const w = { name: "Credits", used: Math.max(0, Math.min(100, (c.used / c.limit) * 100)), amount: round(c.used), limit: round(c.limit), unit: "credits" }
886
928
  if (c.until) w.resetsAt = new Date(c.until).toISOString()
887
929
  out.windows = [w]
888
930
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-trae-auth",
3
- "version": "0.1.5",
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",