Files
plugins/packages/trae-global/stream.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

239 lines
13 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// What 歧路亡羊 found on magpie's Discord (2026-10-05) and the CN plugin
// fixed in 0.1.9–0.1.14, the international plugin taking the same machinery:
// GLM's own tool-call tags, a call written into a native call's name, answer
// text under an event the stream didn't name as output, reasoning written
// into the text or sent twice, the finish of a reply that failed or was cut
// at its limit, and Trae's Max context.
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 deltasOf = (c) => c.filter((x) => x !== "[DONE]" && x.choices?.length).map((x) => x.choices[0].delta)
const callsOf = (c) => deltasOf(c).flatMap((d) => d.tool_calls ?? []).map((t) => [t.function.name, JSON.parse(t.function.arguments)])
const textOf = (c) => deltasOf(c).map((d) => d.content ?? "").join("")
const reasoningOf = (c) => deltasOf(c).map((d) => d.reasoning_content ?? "").join("")
const finishOf = (c) => c.filter((x) => x !== "[DONE]" && x.choices?.[0]?.finish_reason).map((x) => x.choices[0].finish_reason)
const TOOLS = [
{ type: "function", function: { name: "read", parameters: { type: "object", properties: { path: { type: "string" }, limit: { type: "number" } } } } },
{ type: "function", function: { name: "bash", parameters: { type: "object", properties: { command: { type: "string" } } } } },
]
const stream = (events) => {
f?.close()
f = fakeTrae()
f.route("POST /api/agent/v3/llm_utils_chat", () => sse(events))
}
test("GLM's own <arg_key>/<arg_value> call is a call, split over events, a string kept as written and a number read as one", async () => {
stream([
["output", { response: "Reading.\n<tool_call>read\n<arg_key>path</arg_key>\n<arg_va" }],
["output", { response: "lue>007.txt</arg_value>\n<arg_key>limit</arg_key>\n<arg_value>20</arg_value>\n</tool_call>" }],
["output", { response: '<tool_call>bash<arg_key>command</arg_key><arg_value>echo "a\nb"</arg_value></tool_call>' }],
["done", {}],
])
const c = await chunks(await ask({ model: "gpt-5.4", stream: true, tools: TOOLS, messages: [{ role: "user", content: "hi" }] }))
expect(textOf(c)).toBe("Reading.\n")
expect(callsOf(c)).toEqual([["read", { path: "007.txt", limit: 20 }], ["bash", { command: 'echo "a\nb"' }]])
expect(finishOf(c)).toEqual(["tool_calls"])
})
test("a native call whose name holds the whole call, as JSON or GLM's tags, is that call", async () => {
stream([
["output", { tool_calls: [{ id: "n1", function: { name: '{"name":"read","arguments":{"path":"a"}}', arguments: "" } }] }],
["output", { tool_calls: [{ id: "n2", function: { name: "bash<arg_key>command</arg_key><arg_value>ls</arg_value>", arguments: "{}" } }] }],
["done", {}],
])
const c = await chunks(await ask({ model: "gpt-5.4", stream: true, tools: TOOLS, messages: [{ role: "user", content: "hi" }] }))
expect(callsOf(c)).toEqual([["read", { path: "a" }], ["bash", { command: "ls" }]])
})
test("a call to a tool named otherwise than the request's (case, dashes) goes on under the request's name", async () => {
stream([["output", { response: '<tool_call>{"name":"Read","arguments":{"path":"a"}}</tool_call>' }], ["done", {}]])
const c = await chunks(await ask({ model: "gpt-5.4", stream: true, tools: TOOLS, messages: [{ role: "user", content: "hi" }] }))
expect(callsOf(c)).toEqual([["read", { path: "a" }]])
})
test("answer text under an event not named output (delta, or none) is the answer's", async () => {
f = fakeTrae()
f.route("POST /api/agent/v3/llm_utils_chat", () =>
new Response('event: output\ndata: {"response":"He"}\n\nevent: delta\ndata: {"response":"ll"}\n\ndata: {"content":"o"}\n\nevent: request_wait_in_queue\ndata: {"position":1}\n\nevent: done\ndata: {}\n\n', { headers: { "content-type": "text/event-stream" } }))
const c = await chunks(await ask({ model: "gpt-5.4", stream: true, messages: [{ role: "user", content: "hi" }] }))
expect(textOf(c)).toBe("Hello")
})
test("reasoning written into the text as JSON is reasoning, and reasoning sent again isn't sent twice", async () => {
stream([
["output", { reasoning_content: "The user wants a greeting, so" }],
["output", { reasoning_content: "The user wants a greeting, so" }],
["output", { reasoning_content: "The user wants a greeting, so I say hi." }],
["output", { response: "Hi there." }],
["output", { response: '{"reasoning_' }],
["output", { response: 'content":"The user wants a greeting, so I say hi."}' }],
["output", { response: '{"reasoning_content":"one more thought"}' }],
["done", {}],
])
const c = await chunks(await ask({ model: "gpt-5.4", stream: true, messages: [{ role: "user", content: "hi" }] }))
expect(textOf(c)).toBe("Hi there.")
expect(reasoningOf(c)).toBe("The user wants a greeting, so I say hi.one more thought")
})
test("text holding braces that aren't reasoning goes through whole", () => {
const t = new _internal.TextTools()
const a = t.push('x = {"reasoning": 1} and {"a":')
const b = t.push("2}", true)
expect(a.text + b.text).toBe('x = {"reasoning": 1} and {"a":2}')
})
test("a reply that fails mid-way ends with the error and [DONE], not a stop", async () => {
stream([["output", { response: "partial" }], ["error", { code: 500, message: "server broke" }]])
const c = await chunks(await ask({ model: "gpt-5.4", stream: true, messages: [{ role: "user", content: "hi" }] }))
expect(textOf(c)).toBe("partial")
expect(finishOf(c)).toEqual([])
expect(c.at(-2).error.message).toContain("server broke")
expect(c.at(-1)).toBe("[DONE]")
})
test("a reply cut at its limit says length", async () => {
stream([["output", { response: "a lot" }], ["done", { finish_reason: "length" }]])
const c = await chunks(await ask({ model: "gpt-5.4", stream: true, messages: [{ role: "user", content: "hi" }] }))
expect(finishOf(c)).toEqual(["length"])
stream([["output", { response: "a lot" }], ["done", { finish_reason: "max_tokens" }]])
const b = await (await ask({ model: "gpt-5.4", messages: [{ role: "user", content: "hi" }] })).json()
expect(b.choices[0].finish_reason).toBe("length")
})
test("a stream that breaks off ends with an error and [DONE]", async () => {
f = fakeTrae()
f.route("POST /api/agent/v3/llm_utils_chat", () => {
let n = 0
const body = new ReadableStream({
async pull(ctl) {
if (n++ === 0) return ctl.enqueue(new TextEncoder().encode('event: output\ndata: {"response":"half"}\n\n'))
await new Promise((r) => setTimeout(r, 30)) // the headers and the first event are out
ctl.error(new Error("connection reset"))
},
})
return new Response(body, { headers: { "content-type": "text/event-stream" } })
})
const c = await chunks(await ask({ model: "gpt-5.4", stream: true, messages: [{ role: "user", content: "hi" }] }))
expect(textOf(c)).toBe("half")
expect(finishOf(c)).toEqual([])
expect(c.at(-2).error.message).toContain("Trae Global")
expect(c.at(-1)).toBe("[DONE]")
})
// The lists needn't agree (trae 0.1.15, ARNO on magpie's Discord): chat_v3's
// entry names the __max model, while the SOLO entry the model is asked
// through names only __dev and gives the windows. Its Max is found across
// them, and asked through the function that names the __max model.
const SPLIT = { function_configs: [
{ function: "chat_v3", config_info_list: [
{ config_name: "gpt-5.4", usage: "chat_completion", display_config: { display_name: "GPT-5.4 Official" }, model_detail_list: [{ model_name: "gpt-5.4__dev" }, { model_name: "gpt-5.4__max", max_tokens: 128000 }] },
{ config_name: "GPT-5.4-Official", usage: "chat_completion", display_config: { display_name: "GPT-5.4 Official" }, model_detail_list: [{ model_name: "GPT-5.4-Official__dev" }] },
] },
{ function: "solo_work_lite", config_info_list: [
{ config_name: "gpt-5.4", usage: "chat_completion", display_config: { display_name: "GPT-5.4" }, context_window_tokens: { dev: 200000, max: 1000000 }, model_detail_list: [{ model_name: "gpt-5.4__dev", max_tokens: 64000 }] },
] },
] }
test("a Max the lists split between them is listed, and asked where its __max model is named", async () => {
f = fakeTrae()
f.route("POST /api/ide/v1/batch_get_detail_param", () => json(SPLIT))
f.route("POST /api/agent/v3/llm_utils_chat", () => sse([["output", { response: "ok" }], ["done", {}]]))
const hooks = await TraeGlobalAuthPlugin({ client: {} })
const p = { models: { ..._internal.MODELS } }
const live = await hooks.provider.models(p, { auth: signedIn() })
expect(Object.keys(live)).toEqual(["gpt-5.4", "gpt-5.4-max", "GPT-5.4-Official"])
expect(live["gpt-5.4"].limit).toEqual({ context: 200000, output: 64000 })
expect(live["gpt-5.4"].name).toBe("GPT-5.4")
expect(live["gpt-5.4-max"].limit).toEqual({ context: 1000000, output: 128000 })
expect(live["gpt-5.4-max"].name).toBe("GPT-5.4 (Max)")
const opts = await hooks.auth.loader(async () => signedIn())
const send = (body) => opts.fetch(opts.baseURL + "/chat/completions", { method: "POST", body: JSON.stringify({ messages: [{ role: "user", content: "hi" }], ...body }) })
await send({ model: "gpt-5.4-max" })
const b = f.seen.at(-1).json
expect([b.function, b.config_name, b.model_name, b.max_tokens, b.user_message_context.model_info.prompt_max_tokens]).toEqual(["chat_v3", "gpt-5.4", "gpt-5.4__max", 128000, 872000])
})
const BATCH = { function_configs: [
{ function: "solo_agent", config_info_list: [
{ config_name: "gpt-5.4", usage: "chat_completion", display_config: { display_name: "GPT-5.4" }, context_window_tokens: { dev: 272000, max: 400000 }, model_detail_list: [{ model_name: "gpt-5.4__dev", max_tokens: 32000 }, { model_name: "gpt-5.4__max", max_tokens: 128000 }] },
{ config_name: "gemini-3-flash", usage: "chat_completion", context_window_tokens: { dev: 200000 }, model_detail_list: [{ model_name: "gemini-3-flash__dev", max_tokens: 32000 }] },
] },
] }
test("a model with a Max context is listed a second time, as its Max, which asks Trae's __max model with the prompt it has room for", async () => {
f = fakeTrae()
f.route("POST /api/ide/v1/batch_get_detail_param", () => json(BATCH))
f.route("POST /api/agent/v3/llm_utils_chat", () => sse([["output", { response: "ok" }], ["done", {}]]))
const hooks = await TraeGlobalAuthPlugin({ client: {} })
const p = { models: { ..._internal.MODELS } }
const live = await hooks.provider.models(p, { auth: signedIn() })
expect(Object.keys(live)).toEqual(["gpt-5.4", "gpt-5.4-max", "gemini-3-flash"])
expect(live["gpt-5.4"].limit).toEqual({ context: 272000, output: 32000 })
expect(live["gpt-5.4-max"].limit).toEqual({ context: 400000, output: 128000 })
expect(live["gpt-5.4-max"].name).toBe("GPT-5.4 (Max)")
const opts = await hooks.auth.loader(async () => signedIn())
const send = (body) => opts.fetch(opts.baseURL + "/chat/completions", { method: "POST", body: JSON.stringify({ messages: [{ role: "user", content: "hi" }], ...body }) })
await send({ model: "gpt-5.4-max", max_tokens: 100000 })
let b = f.seen.at(-1).json
expect([b.function, b.config_name, b.model, b.model_name, b.max_tokens]).toEqual(["solo_agent", "gpt-5.4", "gpt-5.4", "gpt-5.4__max", 100000])
expect(b.user_message_context).toEqual({ model_info: { prompt_max_tokens: 400000 - 100000 } })
await send({ model: "gpt-5.4-max" })
b = f.seen.at(-1).json
expect([b.model_name, b.max_tokens, b.user_message_context.model_info.prompt_max_tokens]).toEqual(["gpt-5.4__max", 128000, 272000])
// the model itself is asked as before
await send({ model: "gpt-5.4" })
b = f.seen.at(-1).json
expect([b.model_name, b.user_message_context]).toEqual(["gpt-5.4__dev", undefined])
})
test("an answer the agent walks away from lets Trae's connection go", async () => {
// the agent reads a tool call's name and leaves the rest of the stream
// (pi/magpie cancels it to make the call): the read to Trae must close,
// not hang on and leave the reused host one Trae has dropped. The fake
// sends nothing more, so only aborting Trae's own fetch can release it —
// a generator's return can't break a read already waiting.
let cancelled = false
f = fakeTrae()
f.route("POST /api/agent/v3/llm_utils_chat", () => {
let n = 0
const body = new ReadableStream({
async pull(ctl) {
if (n++ === 0) return ctl.enqueue(new TextEncoder().encode('event: output\ndata: {"response":"working on it"}\n\n'))
await new Promise((r) => setTimeout(r, 10_000)) // nothing more comes
},
cancel() { cancelled = true },
})
return new Response(body, { headers: { "content-type": "text/event-stream" } })
})
const res = await ask({ model: "gpt-5.4", stream: true, messages: [{ role: "user", content: "hi" }] })
const reader = res.body.getReader()
await reader.read() // the opening role chunk
await reader.read() // "working on it"
await reader.cancel() // the agent goes away, as making a tool call does
await new Promise((r) => setTimeout(r, 100))
expect(cancelled).toBe(true)
})