Files
plugins/packages/trae-global/request.test.mjs
T
如是 228628689f Add Trae Global (trae.ai) plugin - trae-global provider (#22)
* Add Trae Global (trae.ai) plugin — trae-global provider

Trae international (trae.ai, ByteDance's AI IDE international build) as a
provider plugin: sign in with Trae's own browser flow, free tier included,
models served through the international llm_utils_chat gateway.

Same plugin contract and request/response machinery as the Trae CN
plugin (packages/trae), a separate deployment: different domains, OAuth
client, model catalog, plus a US region (coreva) routed by the account's
AIRegion. The usage card reads the dollar billing's entitlements
(basic+bonus allowance) from the pay host, with the Free plan's /month
allowance riding it.

bun test: 41 pass; bun scripts/check.mjs trae-global: ok

* trae-global: a stream the agent walks away from closes Trae's connection

The bug: an answer whose tool call the agent acts on is cancelled mid-read,
and the read to Trae was left hanging. A generator's return cannot break a
read that is already waiting on reader.read(), so the SSE reader kept its
half of the connection open, and the host the next ask reused was one Trae
had dropped — a stream that runs, then dies, exactly around tool calls.

The fix: the answer's fetch is what gets stopped. The loader builds one
AbortController per request, follows the caller's abort signal into it, and
openai() aborts it when the stream is cancelled. sse() also lets its reader
go in a finally, for the paths that end the generator rather than the fetch.

stream.test.mjs: the agent reads, then cancels with nothing more coming, so
only aborting Trae's fetch can release the read — red before, green after.
bun test (42 pass); bun scripts/check.mjs trae-global: ok

* trae-global: the review's three blockers — planLocked, the 0.1.15 Max, usage's error code

planLocked is gone. It read display_contact_config's identity_list and
assumed every account is Free (identity 0), so a paying account lost
exactly the models it pays for — gpt-5.5, the gpt-5.6 family, gpt-6,
glm-5.2 — and could never pick them. The plugin is never told the
account's own identity (not at sign-in, not in the entitlement answer),
so nothing is filtered on identity now: the list is the account's own,
the IDE greys out what the plan locks, and a locked model answers Trae's
own 1005 when asked. The picker's invisible entries (paygo/auto twins,
search agents) stay out, as before.

maxModel takes the 0.1.15 fix: a model's Max is found across every list
that names it, not just the one entry the model is listed through —
chat_v3 can name the __max model while the SOLO list names only __dev,
and the window can be another entry's again. Without it the same
"no -max model" bug the CN plugin fixed in 0.1.15 comes back here.
maxes holds each model's Max for the models hook, as CN's does.

usage: a 200 carrying an error code (e.code !== "0") now says Trae's own
message instead of "no entitlement pack in the answer", as CN's does.

README: the verification range is stated honestly — one Free account,
SG realm, no Pro and no US account tried; paid-account behaviour is
implemented from the interface's shape, not confirmed on a live one.
registry.json marks Trae Global experimental, as Trae CN is.

usage.test.mjs: the locked-model test now asserts the paying account's
list, and a new one asks a paid account's gpt-5.5 through the gateway.
stream.test.mjs: a Max split across lists is listed and asked where its
__max model is named. Each red before its fix, green after.
bun test (46 pass); bun scripts/check.mjs trae-global: ok
2026-10-05 16:10:35 +08:00

238 lines
13 KiB
JavaScript

// Model requests: OpenAI chat completions translated for the IDE agent's
// llm_utils_chat, and its SSE events back into an OpenAI answer.
import "./nonet.mjs"
import { afterEach, expect, test } from "bun:test"
import { TraeGlobalAuthPlugin, _internal } from "./index.mjs"
import { fakeTrae, json, signedIn, sse } from "./fake.mjs"
let f
afterEach(() => f?.close())
async function ask(body, auth = signedIn()) {
const hooks = await TraeGlobalAuthPlugin({ client: {} })
const opts = await hooks.auth.loader(async () => auth)
return opts.fetch(opts.baseURL + "/chat/completions", { method: "POST", body: JSON.stringify(body) })
}
async function chunks(res) {
const out = []
for (const line of (await res.text()).split("\n")) {
if (!line.startsWith("data: ")) continue
const d = line.slice(6)
out.push(d === "[DONE]" ? d : JSON.parse(d))
}
return out
}
const TOOLS = [{ type: "function", function: { name: "read", description: "Read a file", parameters: { type: "object", properties: { path: { type: "string" } } } } }]
test("the request is the IDE's: its headers, the device, text parts, the tools natively and in the prompt", async () => {
f = fakeTrae()
f.route("POST /api/agent/v3/llm_utils_chat", () => sse([["output", { response: "ok" }], ["done", {}]]))
await ask({ model: "gpt-5.4", max_tokens: 100, tools: TOOLS, messages: [{ role: "system", content: "be brief" }, { role: "user", content: [{ type: "text", text: "hi" }] }] })
const r = f.seen[0]
expect(r.headers.get("authorization")).toBe("Cloud-IDE-JWT jwt-1")
expect(r.headers.get("x-cloudide-token")).toBe("jwt-1")
expect(r.headers.get("x-device-id")).toBe("1234567890123456789")
expect(r.headers.get("x-machine-id")).toBe("ab".repeat(16))
expect(r.headers.get("x-uid")).toBe("u-1")
expect(r.headers.get("accept")).toBe("text/event-stream")
const b = r.json
expect(b.function).toBe("chat_v3")
expect(b.config_name).toBe("gpt-5.4")
expect(b.model).toBe("gpt-5.4")
expect(b.max_tokens).toBe(100)
expect(b.stream).toBe(true)
expect(b.messages.map((m) => m.role)).toEqual(["system", "system", "user"])
expect(b.messages[0].content[0].text).toContain("<tool_call>")
expect(b.messages[2].content).toEqual([{ type: "text", text: "hi" }])
expect(b.tools[0].function.name).toBe("read")
expect(typeof b.tools[0].function.parameters).toBe("string")
})
test("a US account's request goes to the US chat host", async () => {
f = fakeTrae()
f.route("POST /api/agent/v3/llm_utils_chat", () => sse([["output", { response: "us" }], ["done", {}]]))
const hooks = await TraeGlobalAuthPlugin({ client: {} })
const opts = await hooks.auth.loader(async () => signedIn({ region: "US-East" }))
expect(opts.baseURL).toBe(f.origin + "/v1") // the US host, which the fake stands in for
const res = await opts.fetch(opts.baseURL + "/chat/completions", { method: "POST", body: JSON.stringify({ model: "gpt-5.4", messages: [{ role: "user", content: "hi" }] }) })
expect((await res.json()).choices[0].message.content).toBe("us")
})
test("tool history goes in as text", () => {
const m = _internal.traeMessages({ messages: [
{ role: "user", content: "read a" },
{ role: "assistant", content: null, tool_calls: [{ id: "c1", type: "function", function: { name: "read", arguments: "{\"path\":\"a\"}" } }] },
{ role: "tool", tool_call_id: "c1", content: "AAA" },
{ role: "tool", tool_call_id: "c2", content: "BBB" },
] })
expect(m.map((x) => x.role)).toEqual(["user", "assistant", "user"])
expect(m[1].content[0].text).toBe('<tool_call>{"name":"read","arguments":{"path":"a"}}</tool_call>')
expect(m[2].content[0].text).toContain("Result of read (call c1):\nAAA")
expect(m[2].content[0].text).toContain("BBB")
})
test("a stream: text, reasoning, a tool block split across events, a native call, usage", async () => {
f = fakeTrae()
f.route("POST /api/agent/v3/llm_utils_chat", () => sse([
["request_wait_in_queue", { position: 3 }],
["output", { reasoning_content: "think" }],
["output", { response: "Let me look. <tool" }],
["output", { response: '_call>{"name":"read","arguments":{"path":"a"}}</tool_call>' }],
["output", { tool_calls: [{ id: "n1", function: { name: "read", arguments: { path: "b" } } }] }],
["token_usage", { usage: { prompt_tokens: 10, completion_tokens: 5 } }],
["done", { finish_reason: "stop" }],
]))
const res = await ask({ model: "gpt-5.4", stream: true, tools: TOOLS, messages: [{ role: "user", content: "hi" }] })
expect(res.status).toBe(200)
expect(res.headers.get("content-type")).toBe("text/event-stream")
expect(res.headers.get("X-Magpie-Sign-In")).toBe("kept")
const c = await chunks(res)
const deltas = c.filter((x) => x !== "[DONE]" && x.choices?.length).map((x) => x.choices[0].delta)
expect(deltas.map((d) => d.content ?? "").join("")).toBe("Let me look. ")
expect(deltas.find((d) => d.reasoning_content).reasoning_content).toBe("think")
const calls = deltas.flatMap((d) => d.tool_calls ?? [])
expect(calls.map((t) => [t.index, t.function.name, t.function.arguments])).toEqual([[0, "read", '{"path":"a"}'], [1, "read", '{"path":"b"}']])
expect(calls[1].id).toBe("n1")
expect(c.at(-3).choices[0].finish_reason).toBe("tool_calls")
expect(c.at(-2).usage).toEqual({ prompt_tokens: 10, completion_tokens: 5, total_tokens: 15 })
expect(c.at(-1)).toBe("[DONE]")
})
test("a native call streamed over several events goes on whole (#799)", async () => {
f = fakeTrae()
f.route("POST /api/agent/v3/llm_utils_chat", () => sse([
// the arguments so far, each event
["output", { tool_calls: [{ id: "b1", index: 0, function: { name: "bash", arguments: "" } }] }],
["output", { tool_calls: [{ id: "b1", index: 0, function: { name: "bash", arguments: '{"command":"echo \\"T' } }] }],
["output", { tool_calls: [{ id: "b1", index: 0, function: { name: "bash", arguments: '{"command":"echo \\"Trae\\""}' } }] }],
// only the new piece, the name only at first
["output", { tool_calls: [{ id: "r1", index: 1, function: { name: "read", arguments: '{"filePath":"C:/Users/j' } }] }],
["output", { tool_calls: [{ index: 1, function: { arguments: 'oe/a.txt"}' } }] }],
// a whole call sent again
["output", { tool_calls: [{ id: "r1", index: 1, function: { name: "read", arguments: '{"filePath":"C:/Users/joe/a.txt"}' } }] }],
["done", {}],
]))
const res = await ask({ model: "gpt-5.4", stream: true, tools: TOOLS, messages: [{ role: "user", content: "hi" }] })
const c = await chunks(res)
const calls = c.filter((x) => x !== "[DONE]" && x.choices?.length).flatMap((x) => x.choices[0].delta.tool_calls ?? [])
expect(calls.map((t) => [t.id, t.function.name, JSON.parse(t.function.arguments)])).toEqual([
["b1", "bash", { command: 'echo "Trae"' }],
["r1", "read", { filePath: "C:/Users/joe/a.txt" }],
])
})
test("no stream asked: one chat completion", async () => {
f = fakeTrae()
f.route("POST /api/agent/v3/llm_utils_chat", () => sse([["output", { content: "Hel" }], ["output", { content: "lo" }], ["done", {}]]))
const res = await ask({ model: "gpt-5.4", messages: [{ role: "user", content: "hi" }] })
const b = await res.json()
expect(b.object).toBe("chat.completion")
expect(b.choices[0].message).toEqual({ role: "assistant", content: "Hello" })
expect(b.choices[0].finish_reason).toBe("stop")
})
test("a text like a tag that isn't one goes through whole", () => {
const t = new _internal.TextTools()
expect(t.push("a <to").text).toBe("a ")
expect(t.push("p> b").text).toBe("<top> b")
expect(t.push("", true).text).toBe("")
})
test("a model chat_v3 doesn't know is asked of SOLO's function, which is kept for the account", async () => {
f = fakeTrae()
f.route("POST /api/agent/v3/llm_utils_chat", (r) => (r.json.function === "chat_v3"
? sse([["error", { code: 4023, message: "model is unknown" }]])
: sse([["output", { response: "solo" }], ["done", {}]])))
const hooks = await TraeGlobalAuthPlugin({ client: {} })
const opts = await hooks.auth.loader(async () => signedIn())
const body = JSON.stringify({ model: "gemini-3.1-pro", messages: [{ role: "user", content: "hi" }] })
const res = await opts.fetch(opts.baseURL + "/chat/completions", { method: "POST", body })
expect((await res.json()).choices[0].message.content).toBe("solo")
expect(f.seen.map((r) => r.json.function)).toEqual(["chat_v3", "solo_work_lite"])
await opts.fetch(opts.baseURL + "/chat/completions", { method: "POST", body })
expect(f.seen.at(-1).json.function).toBe("solo_work_lite")
})
test("a function that doesn't take the model leaves its read behind with it", async () => {
// chat_v3 answers "unknown model" and would keep the stream open:
// the plugin moves to solo_work_lite, and chat_v3's read must close
// with it, or the connection it left is one Trae has to drop
let cancelled = false
f = fakeTrae()
f.route("POST /api/agent/v3/llm_utils_chat", (r) => {
if (r.json.function !== "chat_v3") return sse([["output", { response: "solo" }], ["done", {}]])
let n = 0
const body = new ReadableStream({
async pull(ctl) {
if (n++ === 0) return ctl.enqueue(new TextEncoder().encode('event: error\ndata: {"code":4023,"message":"model is unknown"}\n\n'))
await new Promise((r) => setTimeout(r, 10_000)) // the stream would stay open
},
cancel() { cancelled = true },
})
return new Response(body, { headers: { "content-type": "text/event-stream" } })
})
const hooks = await TraeGlobalAuthPlugin({ client: {} })
const opts = await hooks.auth.loader(async () => signedIn())
const res = await opts.fetch(opts.baseURL + "/chat/completions", { method: "POST", body: JSON.stringify({ model: "gemini-3.1-pro", messages: [{ role: "user", content: "hi" }] }) })
expect((await res.json()).choices[0].message.content).toBe("solo")
await new Promise((r) => setTimeout(r, 50))
expect(cancelled).toBe(true)
})
test("signed out (1001, or a 401) is a 401 that marks the account", async () => {
f = fakeTrae()
f.route("POST /api/agent/v3/llm_utils_chat", () => sse([["error", { code: 1001, message: "not login" }]]))
let res = await ask({ model: "gpt-5.4", messages: [{ role: "user", content: "hi" }] })
expect(res.status).toBe(401)
expect(res.headers.get("X-Magpie-Sign-In")).toBe("expired")
expect((await res.json()).error.message).toContain("sign in again")
f.route("POST /api/agent/v3/llm_utils_chat", () => json({ code: 401, message: "unauthorized" }, 401))
res = await ask({ model: "gpt-5.4", messages: [{ role: "user", content: "hi" }] })
expect(res.status).toBe(401)
expect(res.headers.get("X-Magpie-Sign-In")).toBe("expired")
})
test("out of quota (4008) is a 429, the account kept", async () => {
f = fakeTrae()
f.route("POST /api/agent/v3/llm_utils_chat", () => sse([["error", { code: 4008, message: "quota exceeded" }]]))
const res = await ask({ model: "gpt-5.4", stream: true, messages: [{ role: "user", content: "hi" }] })
expect(res.status).toBe(429)
expect(res.headers.get("X-Magpie-Sign-In")).toBe("kept")
expect((await res.json()).error.message).toBe("Trae Global: quota exceeded")
})
test("a tool block with a file's content written out raw, or its arguments beside the name, goes on whole (#799)", async () => {
f = fakeTrae()
const block = '<tool_call>{"name":"write","arguments":{"path":"a.py","content":"import sys\n\tprint(\\"hi\\")\n"}}</tool_call>'
f.route("POST /api/agent/v3/llm_utils_chat", () => sse([
["output", { response: "Writing it. " + block }],
["output", { response: '<tool_call>{"name":"read","path":"a.py"}</tool_call>' }],
["done", {}],
]))
const res = await ask({ model: "gpt-5.4", stream: true, tools: TOOLS, messages: [{ role: "user", content: "hi" }] })
const c = await chunks(res)
const deltas = c.filter((x) => x !== "[DONE]" && x.choices?.length).map((x) => x.choices[0].delta)
expect(deltas.map((d) => d.content ?? "").join("")).toBe("Writing it. ")
const calls = deltas.flatMap((d) => d.tool_calls ?? [])
expect(calls.map((t) => [t.function.name, JSON.parse(t.function.arguments)])).toEqual([
["write", { path: "a.py", content: 'import sys\n\tprint("hi")\n' }],
["read", { path: "a.py" }],
])
})
test("a native call's pieces that say neither id nor name join the call being written (#799)", () => {
const nc = new _internal.NativeCalls()
nc.push({ id: "w1", function: { name: "write", arguments: '{"path":"a.py",' } })
nc.push({ index: 0, function: { arguments: '"content":"x\ny"' } })
nc.push({ function: { arguments: "}" } })
expect(nc.take().map((t) => [t.id, t.name, JSON.parse(t.arguments)])).toEqual([["w1", "write", { path: "a.py", content: "x\ny" }]])
})
test("looseJSON reads raw control characters in strings only", () => {
expect(_internal.looseJSON('{"a":"x\ny","b":"\\"q\\""}')).toEqual({ a: "x\ny", b: '"q"' })
expect(_internal.looseJSON('{"a":\n1}')).toEqual({ a: 1 })
expect(_internal.looseJSON('{"a":"x')).toBe(null)
})