diff --git a/packages/trae/README.md b/packages/trae/README.md index 5843e6c..a109592 100644 --- a/packages/trae/README.md +++ b/packages/trae/README.md @@ -44,17 +44,31 @@ agent's `POST https://trae-api-cn.mchost.guru/api/agent/v3/llm_utils_chat`: `x-app-id`, the device headers and `x-uid`. - **Answer:** Trae always answers in its own SSE events: - `output` gives `response`/`content` and `reasoning_content`/`reasoning`. + Text under `message`, `delta` or an event with no name is the answer's + too. - `token_usage`, `done` and `error` mark usage, the end and failures. + `done`'s `finish_reason` `length`/`max_tokens` is OpenAI's `length`. - Queue events are ignored. + - A `{"reasoning_content": …}` object written into the text is reasoning, + and reasoning Trae sends again is sent once. - The plugin turns these into an OpenAI stream or a single body. + The plugin turns these into an OpenAI stream or a single body. A reply + that fails (an `error` event, or Trae's stream breaking off) ends with the + error and `[DONE]`, with no finish. - **Tools:** - Trae's chat has no turn for a tool call or its result, so the tools are named twice: - natively in `tools`, with `parameters` as a JSON string; - in a system prompt that asks for each call as a `{"name", "arguments"}` block. - - Calls from either source become OpenAI `tool_calls`. + - GLM also writes its own template, `namek + v` (a string kept as written, other + types read as JSON), and sometimes a whole call into a native call's + name; both are read as the call. + - Calls from either source become OpenAI `tool_calls`. A name matching a + requested tool but for case or punctuation (`Read`) goes on as the + request's (`read`); any other goes on as written, so the agent can say + the tool doesn't exist. - Earlier calls and their results go back in as text. - Images aren't sent. @@ -65,8 +79,19 @@ time, when the batch gives none). The lists are put together: SOLO and the TRAE agent list models `chat_v3` doesn't (deepseek-v4.1-flash is the TRAE agent's, `solo_agent`). Left out are the IDE's helpers (`usage` other than `chat_completion`: summary, fast_apply…), configs switched off and the -custom-model slots. Context is the list's `context_window_tokens.dev` -(`max` is Max mode's), output the `__dev` model's `max_tokens`. When no +custom-model slots. Context is the list's `context_window_tokens.dev`, +output the `__dev` model's `max_tokens`. A config with a `__max` model and +a bigger `context_window_tokens.max` is listed again as its Max, +`-max` ("… (Max)"), with that window and the `__max` model's +`max_tokens`; it asks the config's `__max` model, with `max_tokens` set +and `user_message_context.model_info.prompt_max_tokens` the window less +it, as Max mode does. + +Reasoning: the models reason, but Trae's request takes no effort or +thinking level, so the plugin lists no variants and a level an agent +picks does nothing. + +When no list can be read, the plugin uses the models Trae CN's `chat_v3` is known to serve: GLM-5.2, GLM-5, Kimi K2.6, Qwen 3.7 Plus, DeepSeek V4 Pro and DeepSeek V4 Flash. diff --git a/packages/trae/index.mjs b/packages/trae/index.mjs index 7724566..bfe8549 100644 --- a/packages/trae/index.mjs +++ b/packages/trae/index.mjs @@ -449,21 +449,28 @@ const nativeTools = (tools) => // chatBody is the request for llm_utils_chat; modelName is the model the // function's list names for the config (its __dev one), when it named one, // and most the max_tokens it gives that model (0: none given), which a -// request asking more is held to -function chatBody(req, fn, modelName, most = 0) { +// request asking more is held to. A Max model (max: {base, window}) asks +// the config base by its __max model, max_tokens always set, and gives +// the prompt the room the window has beside it, as the IDE's Max mode does. +function chatBody(req, fn, modelName, most = 0, max = null) { const session = randomUUID() + const config = max?.base ?? req.model const body = { messages: traeMessages(req), function: fn, - config_name: req.model, - model: req.model, + config_name: config, + model: config, ...(modelName ? { model_name: modelName } : {}), stream: true, // Trae answers in SSE either way request_id: session, session_id: session, } - const max = req.max_completion_tokens ?? req.max_tokens - if (Number.isFinite(max) && max > 0) body.max_tokens = most > 0 ? Math.min(Math.floor(max), most) : Math.floor(max) + const asked = req.max_completion_tokens ?? req.max_tokens + if (Number.isFinite(asked) && asked > 0) body.max_tokens = most > 0 ? Math.min(Math.floor(asked), most) : Math.floor(asked) + if (max?.window) { + if (!body.max_tokens && most > 0) body.max_tokens = most + body.user_message_context = { model_info: { prompt_max_tokens: max.window - (body.max_tokens || 0) } } + } if (typeof req.temperature === "number") body.temperature = req.temperature if (Array.isArray(req.tools) && req.tools.length && req.tool_choice !== "none") { body.tools = nativeTools(req.tools) @@ -543,14 +550,21 @@ const callId = () => "call_" + randomBytes(12).toString("hex") + (callSeq++).toS // once whole even when DeepSeek closes it with its own tags in place of // , or DeepSeek's (<|DSML|invoke name="…">, magpie#823); the // tags left around a call are dropped. +// +// GLM writes a call as its own template has it, name +// kv, which is read +// too; and a {"reasoning_content": …} object in the text is the model's +// reasoning, not its answer (歧路亡羊 on magpie's Discord). class TextTools { - constructor() { + constructor(tools = []) { this.buf = "" this.after = false + this.tools = tools } push(s, end = false) { this.buf += s let text = "" + let reasoning = "" const calls = [] for (;;) { if (this.after) { @@ -566,6 +580,27 @@ class TextTools { } const i = this.buf.indexOf(OPEN) const d = this.buf.search(DSML_AT) + const r = this.buf.search(REASON_AT) + if (r >= 0 && (i < 0 || r < i) && (d < 0 || r < d)) { + const o = objectEnd(this.buf, r) + if (o === -1 && !end) { + // not whole yet: held, the text before it sent + text += this.buf.slice(0, r) + this.buf = this.buf.slice(r) + break + } + const v = o > 0 ? looseJSON(this.buf.slice(r, o)) : null + if (v && typeof v.reasoning_content === "string") { + text += this.buf.slice(0, r) + reasoning += v.reasoning_content + this.buf = this.buf.slice(o) + continue + } + // not one after all: text, and what follows read on + text += this.buf.slice(0, r + 1) + this.buf = this.buf.slice(r + 1) + continue + } if (i < 0 && d < 0) break if (d >= 0 && (i < 0 || d < i)) { text += this.buf.slice(0, d) @@ -620,30 +655,81 @@ class TextTools { this.after = true continue } - const j = this.buf.indexOf(CLOSE, i + OPEN.length) - if (j < 0) break + let j = this.buf.indexOf(CLOSE, i + OPEN.length) + if (j < 0) { + // GLM's call, cut short at the end, is still the call + if (!end || !glmCall(this.buf.slice(i + OPEN.length), this.tools)) break + j = this.buf.length + } text += this.buf.slice(0, i) const raw = this.buf.slice(i + OPEN.length, j).trim() this.buf = this.buf.slice(j + CLOSE.length) - const w = looseJSON(raw) ?? {} - if (w.name) calls.push({ name: String(w.name), arguments: callArgs(w) }) - else text += OPEN + raw + CLOSE + const w = looseJSON(raw) ?? glmCall(raw, this.tools) ?? {} + if (w.name) { + calls.push({ name: String(w.name), arguments: callArgs(w) }) + this.after = true + } else text += OPEN + raw + CLOSE } if (end) { text += this.buf this.buf = "" } else { // keep back an opened block, or what may be the start of one - const at = [this.buf.indexOf(OPEN), this.buf.search(DSML_AT)].filter((x) => x >= 0) + const at = [this.buf.indexOf(OPEN), this.buf.search(DSML_AT), this.buf.search(REASON_AT)].filter((x) => x >= 0) let keep = at.length ? this.buf.length - Math.min(...at) : 0 if (!keep) for (let k = Math.min(MARK_MAX - 1, this.buf.length); k > 0; k--) if (MARKS.some((m) => m.startsWith(this.buf.slice(-k)))) { keep = k; break } text += this.buf.slice(0, this.buf.length - keep) this.buf = this.buf.slice(this.buf.length - keep) } - return { text, calls } + return { text, calls, reasoning } } } +// GLM's call template: the tool's name, then each argument as +// kv +const GLM_ARG = /\s*([\s\S]*?)\s*<\/arg_key>\s*([\s\S]*?)<\/arg_value>/g + +// glmCall is a call GLM wrote in its template ({name, arguments}), null +// when s isn't one. A value is its text where the tool's schema says +// string (or the text isn't JSON), else its JSON, as GLM's template writes +// a value that isn't a string. A name alone is a call only to a tool the +// request has. +function glmCall(s, tools = []) { + s = String(s ?? "").replace(/<\/tool_call>\s*$/, "") + const k = s.indexOf("") + const name = (k < 0 ? s : s.slice(0, k)).trim() + if (!/^[\w.:-]{1,128}$/.test(name)) return null + if (k < 0 && !tools.some((t) => (t.function ?? t)?.name === name)) return null + const props = (tools.find((t) => (t.function ?? t)?.name === name)?.function ?? {}).parameters?.properties ?? {} + const args = {} + for (const m of s.matchAll(GLM_ARG)) { + const raw = m[2] + if (props[m[1]]?.type === "string") { + args[m[1]] = raw + continue + } + try { + args[m[1]] = JSON.parse(raw.trim()) + } catch { + args[m[1]] = raw + } + } + if (k >= 0 && !Object.keys(args).length) return null + return { name, arguments: args } +} + +// toolNamed is the request's own name for a tool a model named otherwise +// (Read for read, list-files for list_files); name itself when the request +// has none like it. A tool it has none of goes on as named: the agent tells +// the model there's no such tool, which it can act on, where a call +// dropped here would end the turn with nothing. +function toolNamed(name, tools) { + const names = tools.map((t) => (t.function ?? t)?.name).filter(Boolean) + if (!names.length || names.includes(name)) return name + const norm = (n) => String(n).toLowerCase().replace(/[^a-z0-9]/g, "") + return names.find((n) => norm(n) === norm(name)) ?? name +} + // DeepSeek's tags, with its full-width bar or a plain one const DSML = String.raw`<\s*\/?\s*[||][\s||]*DSML[\s||]*` const DSML_AT = new RegExp(DSML) @@ -656,7 +742,9 @@ const DSML_PARAM = new RegExp(String.raw`<\s*[||][\s||]*DSML[\s||]*paramet // next call's const LEFTOVER = new RegExp(String.raw`^(?:\s+|<\/tool_calls?>||` + DSML + String.raw`(?![\s||]*invoke\b)[^<>]*>)+`) // what text may be the start of, so held back -const MARKS = [OPEN, CLOSE, "<|DSML|", "<|DSML|", " m.length)) // dsmlArgs is the arguments of a DSML invoke as JSON: a parameter marked @@ -793,8 +881,15 @@ class NativeCalls { if (next.startsWith(prev) || done(prev)) o.arguments = next // the arguments so far else o.arguments = prev + next // a piece } - take() { + take(tools = []) { return this.calls.filter((c) => c.name).map(({ id, name, arguments: a }) => { + // a name that holds the whole call, as JSON or in GLM's template: + // that call, its arguments the name's when it gave none of its own + const w = name.trimStart().startsWith("{") ? looseJSON(name) : name.includes("") ? glmCall(name, tools) : null + if (w?.name) { + name = String(w.name) + if (!a || a.trim() === "{}") a = callArgs(w) + } const v = a && !done(a) ? looseJSON(a) : null return { id: id || callId(), name, arguments: v ? JSON.stringify(v) : a || "{}" } }) @@ -812,11 +907,48 @@ function done(a) { } } +// the events that carry the answer: output and message, and the names +// Trae has put a reply's text under otherwise (delta, or none at all); +// any other is queueing, metadata or timing +const ANSWER = new Set(["", "output", "message", "delta", "content", "text", "answer", "chunk", "output_delta", "message_delta"]) + +// finishOf is OpenAI's finish_reason for the one Trae's last event gives, +// "" for none it says or one it has no word for +function finishOf(d) { + const f = String(d?.finish_reason ?? d?.stop_reason ?? d?.finishReason ?? "").toLowerCase() + if (["length", "max_tokens", "max_output_tokens", "max_length"].includes(f)) return "length" + if (f === "content_filter" || f === "sensitive") return "content_filter" + return "" +} + +// Thoughts is the reasoning sent on so far: a piece Trae sends again (the +// same one twice in a row, or the reasoning so far, again or with more +// after it) sends only what is new +class Thoughts { + constructor() { + this.all = "" + this.last = "" + } + add(r) { + const last = this.last + this.last = r + if (r === last && r.length >= 8) return "" + if (this.all.length >= 16 && r.length >= 16 && this.all.endsWith(r)) return "" + if (this.all.length >= 16 && r.startsWith(this.all)) r = r.slice(this.all.length) + this.all += r + return r + } +} + // parts turns Trae's events into what the answer is made of: text, -// reasoning, tool calls, the token counts, the finish, or an error. -async function* parts(events) { - const tt = new TextTools() +// reasoning, tool calls, the token counts, the finish, or an error. tools +// are the request's, a call to one named otherwise going on under its name. +async function* parts(events, tools = []) { + const tt = new TextTools(tools) const nc = new NativeCalls() + const th = new Thoughts() + const call = (c) => ({ call: { ...c, name: toolNamed(c.name, tools) } }) + let finish = "" for await (const { event, data } of events) { const name = eventName(event) const d = data && typeof data === "object" ? data : {} @@ -834,27 +966,38 @@ async function* parts(events) { if (name === "done" || name === "response_done" || name === "stream_done") { const u = tokensOf(d.usage) if (u) yield { usage: u } + finish = finishOf(d) || finish break } - if (name && name !== "output" && name !== "message") continue // queueing, metadata, timing - const reasoning = d.reasoning_content ?? d.reasoning ?? "" - if (typeof reasoning === "string" && reasoning) yield { reasoning } - let text = typeof d.response === "string" ? d.response : typeof d.content === "string" ? d.content : "" + if (!ANSWER.has(name)) continue + const delta = d.delta && typeof d.delta === "object" ? d.delta : {} + const reasoning = d.reasoning_content ?? d.reasoning ?? delta.reasoning_content ?? "" + if (typeof reasoning === "string" && reasoning) { + const r = th.add(reasoning) + if (r) yield { reasoning: r } + } + let text = typeof d.response === "string" ? d.response : typeof d.content === "string" ? d.content : typeof delta.content === "string" ? delta.content : typeof d.delta === "string" ? d.delta : "" // the IDE's own progress notes, not the model's if (/^(Building prompt:|Completed building prompt)/.test(text)) text = "" if (text) { const r = tt.push(text) + const t = r.reasoning && th.add(r.reasoning) + if (t) yield { reasoning: t } if (r.text) yield { text: r.text } - for (const c of r.calls) yield { call: { ...c, id: callId() } } + for (const c of r.calls) yield call({ ...c, id: callId() }) } for (const tc of Array.isArray(d.tool_calls) ? d.tool_calls : []) nc.push(tc) const u = tokensOf(d.usage) if (u) yield { usage: u } + finish = finishOf(d) || finish } const r = tt.push("", true) + const t = r.reasoning && th.add(r.reasoning) + if (t) yield { reasoning: t } if (r.text) yield { text: r.text } - for (const c of r.calls) yield { call: { ...c, id: callId() } } - for (const c of nc.take()) yield { call: c } + for (const c of r.calls) yield call({ ...c, id: callId() }) + for (const c of nc.take(tools)) yield call(c) + if (finish) yield { finish } } // first reads events up to the first that is the answer's, so an answer @@ -899,45 +1042,71 @@ async function openai(req, it) { let reasoning = "" const calls = [] let usage = null + let finish = "" for await (const p of it) { if (p.error) return failure(502, p.code, p.error) if (p.text) text += p.text if (p.reasoning) reasoning += p.reasoning if (p.call) calls.push(p.call) if (p.usage) usage = p.usage + if (p.finish) finish = p.finish } const message = { role: "assistant", content: text || (calls.length ? null : "") } if (reasoning) message.reasoning_content = reasoning if (calls.length) message.tool_calls = calls.map((c) => ({ id: c.id, type: "function", function: { name: c.name, arguments: c.arguments } })) - const body = { id, object: "chat.completion", created, model, choices: [{ index: 0, message, finish_reason: calls.length ? "tool_calls" : "stop" }] } + const body = { id, object: "chat.completion", created, model, choices: [{ index: 0, message, finish_reason: calls.length ? "tool_calls" : finish || "stop" }] } if (usage) body.usage = usage return new Response(JSON.stringify(body), { status: 200, headers: { "content-type": "application/json" } }) } const enc = new TextEncoder() const chunk = (delta, finish = null, extra = {}) => enc.encode(`data: ${JSON.stringify({ id, object: "chat.completion.chunk", created, model, choices: [{ index: 0, delta, finish_reason: finish }], ...extra })}\n\n`) + // a reply that failed (an error event, or Trae's stream breaking off) + // ends with the error and [DONE], not a finish that says it stopped; the + // stream is closed whatever happens, and an agent that goes away stops + // the reading of Trae's + const error = (message, code = null) => enc.encode(`data: ${JSON.stringify({ error: { message: `Trae CN: ${message}`, type: "api_error", code } })}\n\n`) + let gone = false const stream = new ReadableStream({ async start(ctl) { let calls = 0 let usage = null - ctl.enqueue(chunk({ role: "assistant", content: "" })) + let finish = "" + let failed = false + const put = (b) => { + if (!gone) ctl.enqueue(b) + } try { + put(chunk({ role: "assistant", content: "" })) for await (const p of it) { + if (gone) break if (p.error) { - ctl.enqueue(enc.encode(`data: ${JSON.stringify({ error: { message: `Trae CN: ${p.error}`, type: "api_error", code: p.code ?? null } })}\n\n`)) + put(error(p.error, p.code ?? null)) + failed = true break } - if (p.text) ctl.enqueue(chunk({ content: p.text })) - if (p.reasoning) ctl.enqueue(chunk({ reasoning_content: p.reasoning })) - if (p.call) ctl.enqueue(chunk({ tool_calls: [{ index: calls++, id: p.call.id, type: "function", function: { name: p.call.name, arguments: p.call.arguments } }] })) + if (p.text) put(chunk({ content: p.text })) + if (p.reasoning) put(chunk({ reasoning_content: p.reasoning })) + if (p.call) put(chunk({ tool_calls: [{ index: calls++, id: p.call.id, type: "function", function: { name: p.call.name, arguments: p.call.arguments } }] })) if (p.usage) usage = p.usage + if (p.finish) finish = p.finish + } + if (!failed) { + put(chunk({}, calls ? "tool_calls" : finish || "stop")) + if (usage) put(enc.encode(`data: ${JSON.stringify({ id, object: "chat.completion.chunk", created, model, choices: [], usage })}\n\n`)) } - ctl.enqueue(chunk({}, calls ? "tool_calls" : "stop")) - if (usage) ctl.enqueue(enc.encode(`data: ${JSON.stringify({ id, object: "chat.completion.chunk", created, model, choices: [], usage })}\n\n`)) - ctl.enqueue(enc.encode("data: [DONE]\n\n")) } catch (e) { - ctl.enqueue(enc.encode(`data: ${JSON.stringify({ error: { message: `Trae CN: ${e?.message ?? e}`, type: "api_error", code: null } })}\n\n`)) + try { + put(error(e?.message ?? e)) + } catch {} } - ctl.close() + try { + put(enc.encode("data: [DONE]\n\n")) + if (!gone) ctl.close() + } catch {} + }, + cancel() { + gone = true + it.return?.() }, }) return new Response(stream, { status: 200, headers: { "content-type": "text/event-stream", "cache-control": "no-cache" } }) @@ -964,6 +1133,20 @@ function chatModel(m) { // devModel is the model an entry serves requests with: its __dev one const devModel = (m) => (Array.isArray(m?.model_detail_list) ? m.model_detail_list : []).find((d) => /__dev$/.test(String(d?.model_name ?? ""))) +// maxModel is a config's Max mode: its __max model and the window +// context_window_tokens.max gives it, when that is bigger than dev's; a +// config without one has no Max +const maxModel = (m) => { + const d = (Array.isArray(m?.model_detail_list) ? m.model_detail_list : []).find((d) => /__max$/.test(String(d?.model_name ?? ""))) + const window = Number(m?.context_window_tokens?.max) || 0 + const dev = Number(m?.context_window_tokens?.dev) || 0 + return d && window > dev ? { name: String(d.model_name), most: Number(d.max_tokens) || 0, window } : null +} + +// MAX is the suffix of a model's Max: deepseek-v4.1-flash-max is +// deepseek-v4.1-flash in Max mode +const MAX = "-max" + // ---- usage ------------------------------------------------------------------------ // credits adds up the account's entitlement packs as the IDE shows them: @@ -1219,6 +1402,11 @@ export const TraeCNAuthPlugin = async ({ client }) => { const dev = devModel(m) if (dev) modelNames.set(id, { fn, name: dev.model_name, most: Number(dev.max_tokens) || 0 }) else modelNames.delete(id) + const max = maxModel(m) + if (max && !out.has(id + MAX)) { + listedBy.set(id + MAX, fn) + modelNames.set(id + MAX, { fn, name: max.name, most: max.most, max: { base: id, window: max.window } }) + } } return [...out.values()].map((x) => x.m) } @@ -1253,7 +1441,16 @@ export const TraeCNAuthPlugin = async ({ client }) => { try { const ms = await liveModels(await fresh(async () => auth)) if (!ms.length) return provider.models - return Object.fromEntries(ms.map((m) => [String(m.config_name), modelOf(provider, m)])) + const ids = new Set(ms.map((m) => String(m.config_name))) + return Object.fromEntries(ms.flatMap((m) => { + const id = String(m.config_name) + const own = modelOf(provider, m) + const max = maxModel(m) + if (!max || ids.has(id + MAX)) return [[id, own]] + // its Max, a model of its own: the window and output Max mode gives + const was = provider.models?.[id + MAX] ?? {} + return [[id, own], [id + MAX, { ...own, ...was, id: id + MAX, name: own.name + " (Max)", limit: { context: max.window, output: max.most || own.limit.output }, api: { ...own.api, id: id + MAX } }]] + })) } catch (e) { if (e instanceof Expired) throw e return provider.models @@ -1296,12 +1493,19 @@ export const TraeCNAuthPlugin = async ({ client }) => { // served this account last, then the rest const fns = [...new Set([listedBy.get(String(req.model)), fnOf.get(who), ...FUNCTIONS].filter(Boolean))] let last - const named = modelNames.get(String(req.model)) - for (const fn of fns) { + let named = modelNames.get(String(req.model)) + if (!named && String(req.model).endsWith(MAX)) { + // a Max asked before the list was read here: read it + await liveModels(a).catch(() => {}) + named = modelNames.get(String(req.model)) + if (named) fns.unshift(named.fn) + } + const max = named?.max ?? null + for (const fn of [...new Set(fns)]) { const res = await fetch(apiOf(a) + "/api/agent/v3/llm_utils_chat", { method: "POST", headers: ideHeaders(a, { Accept: "text/event-stream", "X-Request-ID": randomUUID() }), - body: JSON.stringify(chatBody(req, fn, named?.fn === fn ? named.name : "", named?.fn === fn ? named.most : 0)), + body: JSON.stringify(chatBody(req, fn, named?.fn === fn || max ? named.name : "", named?.fn === fn || max ? named.most : 0, max)), signal: init.signal ?? r0?.signal, }) if (!res.ok) { @@ -1319,7 +1523,7 @@ export const TraeCNAuthPlugin = async ({ client }) => { if (wrongFunction(e.code)) continue break } - const it = parts(sse(res.body))[Symbol.asyncIterator]() + const it = parts(sse(res.body), Array.isArray(req.tools) ? req.tools : [])[Symbol.asyncIterator]() const { held, done } = await first(it) const err = held.find((p) => p.error) if (err) { @@ -1343,4 +1547,4 @@ export const TraeCNAuthPlugin = async ({ client }) => { } // for tests -export const _internal = { HOSTS, MODELS, TextTools, NativeCalls, looseJSON, traeMessages, chatBody, credits, whenOf, newDevice } +export const _internal = { HOSTS, MODELS, TextTools, NativeCalls, looseJSON, glmCall, toolNamed, traeMessages, chatBody, credits, whenOf, newDevice } diff --git a/packages/trae/package.json b/packages/trae/package.json index d72d719..1923289 100644 --- a/packages/trae/package.json +++ b/packages/trae/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-trae-auth", - "version": "0.1.13", + "version": "0.1.14", "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", "type": "module", "main": "./index.mjs", diff --git a/packages/trae/stream.test.mjs b/packages/trae/stream.test.mjs new file mode 100644 index 0000000..0367452 --- /dev/null +++ b/packages/trae/stream.test.mjs @@ -0,0 +1,176 @@ +// What 歧路亡羊 found on magpie's Discord (2026-10-05): 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 { TraeCNAuthPlugin, _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 TraeCNAuthPlugin({ 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 / call is a call, split over events, a string kept as written and a number read as one", async () => { + stream([ + ["output", { response: "Reading.\nread\npath\n007.txt\nlimit\n20\n" }], + ["output", { response: 'bashcommandecho "a\nb"' }], + ["done", {}], + ]) + const c = await chunks(await ask({ model: "glm-5", 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: "bashcommandls", arguments: "{}" } }] }], + ["done", {}], + ]) + const c = await chunks(await ask({ model: "glm-5", 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: '{"name":"Read","arguments":{"path":"a"}}' }], ["done", {}]]) + const c = await chunks(await ask({ model: "glm-5", 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: "glm-5", 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: "glm-5", 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: "glm-5", 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: "glm-5", 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: "glm-5", 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: "glm-5", stream: true, messages: [{ role: "user", content: "hi" }] })) + expect(textOf(c)).toBe("half") + expect(finishOf(c)).toEqual([]) + expect(c.at(-2).error.message).toContain("Trae CN") + expect(c.at(-1)).toBe("[DONE]") +}) + +const BATCH = { function_configs: [ + { function: "solo_agent", config_info_list: [ + { config_name: "deepseek-v4.1-flash", usage: "chat_completion", display_config: { display_name: "DeepSeek-V4.1-Flash" }, context_window_tokens: { dev: 116000, max: 1000000 }, model_detail_list: [{ model_name: "deepseek-v4.1-flash__dev", max_tokens: 16000 }, { model_name: "deepseek-v4.1-flash__max", max_tokens: 64000 }] }, + { config_name: "glm-5.2", usage: "chat_completion", context_window_tokens: { dev: 200000 }, model_detail_list: [{ model_name: "glm-5.2__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 TraeCNAuthPlugin({ client: {} }) + const p = { models: { ..._internal.MODELS } } + const live = await hooks.provider.models(p, { auth: signedIn() }) + expect(Object.keys(live)).toEqual(["deepseek-v4.1-flash", "deepseek-v4.1-flash-max", "glm-5.2"]) + expect(live["deepseek-v4.1-flash"].limit).toEqual({ context: 116000, output: 16000 }) + expect(live["deepseek-v4.1-flash-max"].limit).toEqual({ context: 1000000, output: 64000 }) + expect(live["deepseek-v4.1-flash-max"].name).toBe("DeepSeek-V4.1-Flash (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: "deepseek-v4.1-flash-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", "deepseek-v4.1-flash", "deepseek-v4.1-flash", "deepseek-v4.1-flash__max", 64000]) + expect(b.user_message_context).toEqual({ model_info: { prompt_max_tokens: 1000000 - 64000 } }) + await send({ model: "deepseek-v4.1-flash-max" }) + b = f.seen.at(-1).json + expect([b.model_name, b.max_tokens, b.user_message_context.model_info.prompt_max_tokens]).toEqual(["deepseek-v4.1-flash__max", 64000, 936000]) + // the model itself is asked as before + await send({ model: "deepseek-v4.1-flash" }) + b = f.seen.at(-1).json + expect([b.model_name, b.user_message_context]).toEqual(["deepseek-v4.1-flash__dev", undefined]) +}) diff --git a/packages/trae/usage.test.mjs b/packages/trae/usage.test.mjs index 69e2cad..5b24fe8 100644 --- a/packages/trae/usage.test.mjs +++ b/packages/trae/usage.test.mjs @@ -99,7 +99,9 @@ test("the lists are asked in one batch, as TRAE SOLO CN asks them, and DeepSeek expect(r.headers.get("x-ide-version-code")).toBe("20260917") expect(r.headers.get("x-app-version-code")).toBe("20260917") // the IDE's helpers, custom-model slots, configs switched off and other functions' models aren't models to pick - expect(Object.keys(live)).toEqual(["glm-5.2", "deepseek-v4.1-flash"]) + // and DeepSeek's Max a model of its own (glm-5.2 names no __max model) + expect(Object.keys(live)).toEqual(["glm-5.2", "deepseek-v4.1-flash", "deepseek-v4.1-flash-max"]) + expect(live["deepseek-v4.1-flash-max"].limit).toEqual({ context: 1000000, output: 128000 }) const m = live["deepseek-v4.1-flash"] expect(m.name).toBe("DeepSeek-V4.1-Flash") expect(m.limit).toEqual({ context: 200000, output: 64000 })