Files
plugins/packages/trae/fake.mjs
T
如是andyetone 42783f0033 trae: one package, two realms — Trae Global folds into packages/trae (#23)
* trae: one package, two realms — Trae Global folds into packages/trae

The review asked for this and it is the right shape: the two deployments
share one implementation. 86 of the 89 top-level names were already the
same code; what differed was a table's worth of values (hosts, client,
versions, usage page, fallback models, error codes) and two realm-only
branches. A separate copy meant every request, stream and tool-call fix
had to be made twice, and it had already drifted (the 0.1.15 Max fix
came back as a bug there).

packages/trae now holds a SITES table with two entries — trae-cn and
trae-global — and makePlugin(site) builds a provider from one. Exported
as TraeCNAuthPlugin and TraeGlobalAuthPlugin. The CN values are moved
verbatim, not re-derived: same hosts, client id, app id, IDE/client
versions, plugin version, brand, functions, fallback models, credits
page, and its own check-in pages. The international entry carries what
only it has: the second (US) deployment and the region that selects it,
the dollar billing's entitlements page, its own model names, and its
extra lapse/quota codes (20101, 4011).

The CN plugin needs the stream fix too (the review's point), so the
AbortController the loader now builds per request, openai()'s abort, and
sse()'s finally are in this one implementation for both realms — as is
the wrong-function retry releasing the read it leaves behind.

fake.mjs takes a realm id, defaulting to CN so every test written before
the fold is unchanged. The global tests move to global.test.mjs, plus
new ones for what only the international realm does: the US/SG routing
of a request and of a saved sign-in, its own error codes and error name,
the dollar usage page, and its fallback model list. chatModel gains the
picker's invisible-entry filter (paygo/auto twins, search agents) and
the config hook names the realm's own provider.

The acceptance test: packages/trae/*.test.mjs' 48 CN tests are untouched
and green — that is the proof the refactor changed no CN behaviour, with
its constants verified line by line against HEAD. bun test: 63 pass
(48 CN + 15 global); bun scripts/check.mjs trae: ok.

registry.json: one entry, providers [trae-cn, trae-global]. README: both
realms, each realm's endpoints, usage page and codes; the verification
range stated honestly (CN never run live; Global on one Free SG account,
no Pro and no US account).

* trae: Trae CN's model lists stay whole; only Global hides is_invisible_to_user

The fold filtered is_invisible_to_user for both realms, which took 40 of
Trae CN's 76 models out of its list. CN is served as before; Global (a
site flag, hidesInvisible) keeps the filter. Also: the unsupported-request
error names the realm, and two duplicated/merged comments are tidied.

---------

Co-authored-by: yetone <yetoneful@gmail.com>
2026-10-05 17:18:12 +08:00

57 lines
2.4 KiB
JavaScript

// A fake of Trae for the tests: one local server standing in for the
// realm's authorization page host and its other hosts, which the plugin's
// HOSTS are pointed at. route(method, path) answers a request; seen lists
// what came. The CN realm is the default, so a test written before the two
// realms shared a package is unchanged; pass "trae-global" for the
// international one, whose hosts and usage page differ.
import { _internal } from "./index.mjs"
const HOST_KEYS = ["web", "auth", "api", "us", "pay", "usPay"]
export function fakeTrae(siteId = "trae-cn") {
const routes = new Map()
const seen = []
const server = Bun.serve({
port: 0,
hostname: "127.0.0.1",
async fetch(req) {
const url = new URL(req.url)
const text = await req.text()
const r = { method: req.method, path: url.pathname, query: url.searchParams, headers: req.headers, text }
if (text) try { r.json = JSON.parse(text) } catch {}
seen.push(r)
const h = routes.get(req.method + " " + url.pathname)
if (!h) return new Response("no route " + req.method + " " + url.pathname, { status: 404 })
return h(r)
},
})
const origin = `http://127.0.0.1:${server.port}`
const hosts = _internal.SITES[siteId].hosts
const was = { ...hosts }
Object.assign(hosts, { web: origin, auth: origin, api: origin, us: origin, pay: origin, usPay: origin })
return {
origin,
site: _internal.SITES[siteId],
seen,
route: (key, h) => routes.set(key, h),
close() {
server.stop(true)
for (const k of HOST_KEYS) if (was[k] === undefined) delete hosts[k]
Object.assign(hosts, was)
},
}
}
export const json = (v, status = 200) => new Response(JSON.stringify(v), { status, headers: { "content-type": "application/json" } })
// sse is an answer in Trae's events: [event, data] pairs
export const sse = (events, status = 200) =>
new Response(events.map(([e, d]) => `event: ${e}\ndata: ${typeof d === "string" ? d : JSON.stringify(d)}\n\n`).join(""), { status, headers: { "content-type": "text/event-stream" } })
export const jwtOf = (claims) => ["e30", Buffer.from(JSON.stringify(claims)).toString("base64url"), "sig"].join(".")
export const signedIn = (over = {}) => ({
type: "oauth", access: "jwt-1", refresh: "r-1", expires: Date.now() + 3600_000, accountId: "Ann", uid: "u-1",
clientId: "ono9krqynydwx5", deviceId: "1234567890123456789", machineId: "ab".repeat(16), ...over,
})