From 6596a31c84190ec506e7981d1d8edc4ced2be6ce Mon Sep 17 00:00:00 2001 From: yetone Date: Sun, 4 Oct 2026 01:13:59 +0800 Subject: [PATCH] trae 0.1.3: the model lists are asked as TRAE SOLO CN asks them, in one batch, as client 0.1.69 (yetone/magpie#681) 0.1.2 still showed no DeepSeek V4.1 Flash: its lists came from get_detail_param, one function at a time, sent as the Trae CN IDE 3.3.65 (version code 20260401). The reporter's TRAE SOLO CN 0.1.69 (version code 20260917, same app id) asks POST /api/ide/v1/batch_get_detail_param on trae-api-cn.mchost.guru (its ai-agent log, get_model_list -> batch_get_model_detail_param; 290 configs cached), and picks deepseek-v4.1-flash from it; two other open tools that list it (smart-open/TraeWorkAssistant, zhengwuji/Jet-Hub) ask the same batch, as a 0.1.5x SOLO client, and find it under solo_agent with model_name deepseek-v4.1-flash__dev. Now the lists come from batch_get_detail_param (functions chat_v3, solo_work_lite, solo_agent, solo_agent_lite; agent_type "", access_type 0, show_custom_model), with get_detail_param per function when the batch gives none; the model host is told the client is 0.1.69 / 20260917 (x-ide-version, x-app-version and their codes). A model several functions list is asked through the first whose entry names a __dev model, and that model goes as model_name. The IDE's helpers (usage other than chat_completion: summary, fast_apply...), configs switched off and custom-model slots are left out of the list; names come from display_config.display_name, context from context_window_tokens.dev (max is Max mode's), output from the __dev model's max_tokens. usage.test.mjs: the batch test (one ask, its body and version headers, DeepSeek-V4.1-Flash listed with 200000/64000 and asked through solo_agent as deepseek-v4.1-flash__dev) and the signed-out batch test fail on 0.1.2 and pass; bun test (24 pass) and scripts/check.mjs trae pass. Not tried with a real Trae CN account (none here). --- packages/trae/README.md | 33 ++++++---- packages/trae/index.mjs | 120 +++++++++++++++++++++++++++++------ packages/trae/package.json | 2 +- packages/trae/usage.test.mjs | 72 ++++++++++++++++++++- 4 files changed, 192 insertions(+), 35 deletions(-) diff --git a/packages/trae/README.md b/packages/trae/README.md index bfb0d5c..39b97ec 100644 --- a/packages/trae/README.md +++ b/packages/trae/README.md @@ -31,14 +31,17 @@ agent's `POST https://trae-api-cn.mchost.guru/api/agent/v3/llm_utils_chat`: - **Request:** - The messages go as `{role, content: [{type: "text", text}]}`. - - The model goes as `config_name` and `model`. - - The function is the one whose model list has the model (`chat_v3`, the - classic IDE's, first; else `solo_work_lite`, SOLO's Work mode; else - `solo_agent`, the TRAE agent's). If Trae answers 4001, 4023 or 1005, - the plugin tries the others once and remembers which one worked. -- **Headers:** the IDE's: `Authorization: Cloud-IDE-JWT …`, - `X-Cloudide-Token`, `x-app-id`, `x-ide-version`, the device headers and - `x-uid`. + - The model goes as `config_name` and `model`, and as `model_name` the + `__dev` model the function's list names for it, when it names one. + - The function is the one whose model list has the model, the first that + names a `__dev` model for it (in the order `chat_v3`, the classic IDE's; + `solo_work_lite`, SOLO's Work mode; `solo_agent`, the TRAE agent's; + `solo_agent_lite`). If Trae answers 4001, 4023 or 1005, the plugin tries + the others once and remembers which one worked. +- **Headers:** TRAE SOLO CN 0.1.69's (`x-ide-version`/`x-app-version` + 0.1.69, version code 20260917; Trae offers a model only to clients new + enough for it): `Authorization: Cloud-IDE-JWT …`, `X-Cloudide-Token`, + `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`. - `token_usage`, `done` and `error` mark usage, the end and failures. @@ -55,10 +58,16 @@ agent's `POST https://trae-api-cn.mchost.guru/api/agent/v3/llm_utils_chat`: - Earlier calls and their results go back in as text. - Images aren't sent. -The model list is the account's own, from `/api/ide/v1/get_detail_param`, -asked for each of those functions and put together: SOLO and the TRAE -agent list models `chat_v3` doesn't (deepseek-v4.1-flash is the TRAE -agent's, `solo_agent`). When neither can be read, the plugin uses the models Trae CN's `chat_v3` is +The model list is the account's own, from +`/api/ide/v1/batch_get_detail_param`, every function's list in one ask, as +TRAE SOLO CN asks it (`/api/ide/v1/get_detail_param`, one function at a +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 +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 c811c51..a4d05dc 100644 --- a/packages/trae/index.mjs +++ b/packages/trae/index.mjs @@ -18,8 +18,13 @@ const HOSTS = { } const CLIENT_ID = "ono9krqynydwx5" // Trae CN's IDE const APP_ID = "6eefa01c-1036-4c7e-9ca5-d891f63bfcd8" -const IDE_VERSION = "3.3.65" +const IDE_VERSION = "3.3.65" // named to the authorization page const IDE_VERSION_CODE = "20260401" +// the client the model host is told it serves: TRAE SOLO CN 0.1.69, as its +// ai-agent names itself (same app id). Trae offers a model only to clients +// new enough for it, and an April IDE was offered no deepseek-v4.1-flash. +const CLIENT_VERSION = "0.1.69" +const CLIENT_VERSION_CODE = "20260917" const PLUGIN_VERSION = "2.3.24254" const BRAND = "ASUS TUF Gaming A15 FA507RM_FA507RM" const SIGN_IN_TIMEOUT = 10 * 60 * 1000 @@ -27,9 +32,9 @@ const EARLY_MS = 2 * 60 * 1000 // a token this close to its end is renewed befor const LEAD_MS = 10 * 60 * 1000 // and this close, magpie renews it ahead of time (auth.refresh) const DAY = 24 * 3600 * 1000 // the IDE's chat functions: the classic IDE's agent, then SOLO's Work -// mode, then the TRAE agent (solo_agent): Trae CN 3.3.104's agent-type map -// files its models under "trae", and only that one has deepseek-v4.1-flash -const FUNCTIONS = ["chat_v3", "solo_work_lite", "solo_agent"] +// mode, then the TRAE agent (solo_agent, which has deepseek-v4.1-flash), +// then SOLO Lite's agent +const FUNCTIONS = ["chat_v3", "solo_work_lite", "solo_agent", "solo_agent_lite"] const MODEL = { attachment: false, tool_call: true, reasoning: true, temperature: true, limit: { context: 128_000, output: 32_000 }, modalities: { input: ["text"], output: ["text"] } } // what Trae CN's chat_v3 is known to serve; the live list replaces it @@ -332,8 +337,10 @@ function ideHeaders(a, extra = {}) { "x-device-id": a.deviceId, "x-machine-id": a.machineId, "x-request-id": randomUUID(), - "x-ide-version": IDE_VERSION, - "x-ide-version-code": IDE_VERSION_CODE, + "x-app-version": CLIENT_VERSION, + "x-app-version-code": CLIENT_VERSION_CODE, + "x-ide-version": CLIENT_VERSION, + "x-ide-version-code": CLIENT_VERSION_CODE, "x-ide-version-type": "stable", "x-device-cpu": "AMD", "x-device-brand": BRAND, @@ -421,13 +428,16 @@ const nativeTools = (tools) => return { type: "function", function: { name: f.name, description: f.description ?? "", parameters: typeof f.parameters === "string" ? f.parameters : JSON.stringify(f.parameters ?? {}) } } }) -function chatBody(req, fn) { +// 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 +function chatBody(req, fn, modelName) { const session = randomUUID() const body = { messages: traeMessages(req), function: fn, config_name: req.model, model: req.model, + ...(modelName ? { model_name: modelName } : {}), stream: true, // Trae answers in SSE either way request_id: session, session_id: session, @@ -694,6 +704,24 @@ async function openai(req, it) { // a model or function this account's chat_v3 doesn't take: another may const wrongFunction = (code) => ["4001", "4023", "1005"].includes(String(code)) +// ---- the model lists ------------------------------------------------------------ + +// chatModel: an entry of a function's list that is a model to chat with. +// The lists also hold the IDE's own helpers (summary, fast_apply, title +// generation: usage other than chat_completion), configs switched off, and +// the slots of custom models, which need a provider bound in the IDE. +function chatModel(m) { + const id = String(m?.config_name ?? "") + if (!id || /^custom_model/i.test(id)) return false + if (m.usage && m.usage !== "chat_completion") return false + if (m.config_switch === false) return false + if (m.is_custom_model === true || m.display_config?.is_custom_model === true) return false + return true +} + +// 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 ?? ""))) + // ---- usage ------------------------------------------------------------------------ // credits adds up the account's entitlement packs as the IDE shows them: @@ -729,8 +757,10 @@ export const TraeCNAuthPlugin = async ({ client }) => { const renewed = new Map() // the chat function that served each account last const fnOf = new Map() - // the first chat function whose model list has each model + // the chat function whose model list has each model, and the __dev + // model it names there const listedBy = new Map() + const modelNames = new Map() const save = async (auth) => { try { @@ -818,6 +848,27 @@ export const TraeCNAuthPlugin = async ({ client }) => { return out } + // batchLists is every chat function's model list in one ask, as TRAE + // SOLO CN's ai-agent asks (batch_get_detail_param): {function: entries}. + // null when Trae answers it with no lists. + const batchLists = async (a) => { + const r = await post(a, apiOf(a) + "/api/ide/v1/batch_get_detail_param", { + functions: FUNCTIONS, agent_type: "", current_config_info: { config_name: "", is_custom_model: false }, + mode_type: 0, access_type: 0, ab_force_vids: "", ab_autotest_advanced_mode: 0, show_custom_model: true, + }) + if (r.status === 401 || lapsedCode(errorOf(r.v).code)) throw new Expired("Trae CN's sign-in has expired; sign in again") + if (r.status !== 200) throw new Error(`Trae CN models: ${statusLine(r.status)}`) + const groups = r.v.function_configs ?? r.v.data?.function_configs + if (!Array.isArray(groups)) return null + const out = {} + for (const g of groups) { + const fn = String(g?.function ?? "") + if (!FUNCTIONS.includes(fn) || !Array.isArray(g.config_info_list)) continue + out[fn] = [...(out[fn] ?? []), ...g.config_info_list.filter((m) => m?.config_name)] + } + return Object.keys(out).length ? out : null + } + // listOf is one function's model list, as the IDE asks for it const listOf = async (a, fn) => { const r = await post(a, apiOf(a) + "/api/ide/v1/get_detail_param", { @@ -834,28 +885,54 @@ export const TraeCNAuthPlugin = async ({ client }) => { // came to the TRAE agent's first, yetone/magpie#681); a model listed by one function // only is asked through that one. One function's list failing leaves the // others'; all failing is the error. + // + // The lists come from batch_get_detail_param, as TRAE SOLO CN asks them; + // when that gives none, from get_detail_param, one function at a time. + // A model several functions list is asked through the first whose entry + // names a __dev model (one Trae serves), else the first. const liveModels = async (a) => { - const got = await Promise.allSettled(FUNCTIONS.map((fn) => listOf(a, fn))) - const expired = got.find((g) => g.status === "rejected" && g.reason instanceof Expired) - if (expired) throw expired.reason - if (got.every((g) => g.status === "rejected")) throw got[0].reason + let lists = null + try { + lists = await batchLists(a) + } catch (e) { + if (e instanceof Expired) throw e + } + if (!lists) { + const got = await Promise.allSettled(FUNCTIONS.map((fn) => listOf(a, fn))) + const expired = got.find((g) => g.status === "rejected" && g.reason instanceof Expired) + if (expired) throw expired.reason + if (got.every((g) => g.status === "rejected")) throw got[0].reason + lists = {} + got.forEach((g, i) => { + if (g.status === "fulfilled") lists[FUNCTIONS[i]] = g.value + }) + } const out = new Map() - got.forEach((g, i) => { - if (g.status !== "fulfilled") return - for (const m of g.value) { + for (const fn of FUNCTIONS) { + for (const m of lists[fn] ?? []) { + if (!chatModel(m)) continue const id = String(m.config_name) - if (!out.has(id)) out.set(id, { m, fn: FUNCTIONS[i] }) + const was = out.get(id) + if (!was || (!devModel(was.m) && devModel(m))) out.set(id, { m, fn }) } - }) - for (const [id, { fn }] of out) listedBy.set(id, fn) + } + for (const [id, { m, fn }] of out) { + listedBy.set(id, fn) + const dev = devModel(m) + if (dev) modelNames.set(id, { fn, name: dev.model_name }) + else modelNames.delete(id) + } return [...out.values()].map((x) => x.m) } const modelOf = (provider, m) => { const id = String(m.config_name) const was = provider.models?.[id] ?? {} - const ctx = Number(m.context_window_size?.max?.[0] ?? m.context_window_size?.max ?? m.context_window_tokens?.max ?? m.prompt_max_tokens) || was.limit?.context || MODEL.limit.context - return { ...MODEL, ...was, id, providerID: ID, name: String(m.display_name || m.display_model_name || was.name || id), limit: { context: ctx, output: was.limit?.output ?? MODEL.limit.output }, api: was.api ?? { id, url: HOSTS.api, npm: "@ai-sdk/openai-compatible" } } + // context_window_tokens: dev is what a request gets, max only in Max mode + const ctx = Number(m.context_window_tokens?.dev ?? m.context_window_size?.max?.[0] ?? m.context_window_size?.max ?? m.context_window_tokens?.max ?? m.prompt_max_tokens) || was.limit?.context || MODEL.limit.context + const out = Number(devModel(m)?.max_tokens) || was.limit?.output || MODEL.limit.output + const name = m.display_config?.display_name || m.display_name || m.display_model_name || was.name || id + return { ...MODEL, ...was, id, providerID: ID, name: String(name), limit: { context: ctx, output: out }, api: was.api ?? { id, url: HOSTS.api, npm: "@ai-sdk/openai-compatible" } } } return { @@ -919,11 +996,12 @@ 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) { 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)), + body: JSON.stringify(chatBody(req, fn, named?.fn === fn ? named.name : "")), signal: init.signal ?? r0?.signal, }) if (!res.ok) { diff --git a/packages/trae/package.json b/packages/trae/package.json index 32c621b..2bf462b 100644 --- a/packages/trae/package.json +++ b/packages/trae/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-trae-auth", - "version": "0.1.2", + "version": "0.1.3", "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/usage.test.mjs b/packages/trae/usage.test.mjs index 878a2cb..9362057 100644 --- a/packages/trae/usage.test.mjs +++ b/packages/trae/usage.test.mjs @@ -58,7 +58,77 @@ test("the config declares the known models; the live list replaces them", async const live = await hooks.provider.models(p, { auth: signedIn() }) expect(Object.keys(live)).toEqual(["glm-5.2", "DeepSeek-V4-Pro"]) expect(live["glm-5.2"].limit.context).toBe(200000) - expect(f.seen.map((r) => r.json.function).sort()).toEqual(["chat_v3", "solo_agent", "solo_work_lite"]) + // no batch list (404 here): one function at a time + expect(f.seen[0].path).toBe("/api/ide/v1/batch_get_detail_param") + expect(f.seen.slice(1).map((r) => r.json.function).sort()).toEqual(["chat_v3", "solo_agent", "solo_agent_lite", "solo_work_lite"]) +}) + +// what batch_get_detail_param answered TRAE SOLO CN, cut down: each +// function's own list; deepseek-v4.1-flash is the TRAE agent's (solo_agent), +// where it names its __dev model; chat_v3 lists it without one +const BATCH = { function_configs: [ + { function: "chat_v3", config_info_list: [ + { config_name: "glm-5.2", usage: "chat_completion", display_config: { display_name: "GLM-5.2" }, context_window_tokens: { dev: 200000, max: 1000000 }, model_detail_list: [{ model_name: "glm-5.2__dev", max_tokens: 32000 }] }, + { config_name: "deepseek-v4.1-flash", usage: "chat_completion", display_config: { display_name: "DeepSeek-V4-Flash 正式版" } }, + { config_name: "summary", usage: "summary" }, + { config_name: "custom_model_200k", usage: "chat_completion" }, + { config_name: "kimi-k2", usage: "chat_completion", config_switch: false }, + ] }, + { function: "solo_agent", config_info_list: [ + { config_name: "glm-5.2", usage: "chat_completion", display_config: { display_name: "GLM-5.2" }, model_detail_list: [{ model_name: "glm-5.2__dev" }] }, + { config_name: "deepseek-v4.1-flash", usage: "chat_completion", display_config: { display_name: "DeepSeek-V4.1-Flash", multimodal: true }, context_window_tokens: { dev: 200000, max: 1000000 }, model_detail_list: [{ model_name: "deepseek-v4.1-flash__max", max_tokens: 128000 }, { model_name: "deepseek-v4.1-flash__dev", max_tokens: 64000 }] }, + ] }, + { function: "builder", config_info_list: [{ config_name: "builder-only", usage: "chat_completion" }] }, +] } + +test("the lists are asked in one batch, as TRAE SOLO CN asks them, and DeepSeek V4.1 Flash is the TRAE agent's (yetone/magpie#681)", async () => { + f = fakeTrae() + f.route("POST /api/ide/v1/batch_get_detail_param", () => json(BATCH)) + f.route("POST /api/agent/v3/llm_utils_chat", (r) => sse([["output", { response: r.json.function + " " + (r.json.model_name ?? "-") }], ["done", {}]])) + const hooks = await TraeCNAuthPlugin({ client: {} }) + const p = await given(hooks) + const live = await hooks.provider.models(p, { auth: signedIn() }) + // one ask, the client SOLO CN is + expect(f.seen.map((r) => r.path)).toEqual(["/api/ide/v1/batch_get_detail_param"]) + const r = f.seen[0] + expect(r.json).toEqual({ + functions: ["chat_v3", "solo_work_lite", "solo_agent", "solo_agent_lite"], agent_type: "", current_config_info: { config_name: "", is_custom_model: false }, + mode_type: 0, access_type: 0, ab_force_vids: "", ab_autotest_advanced_mode: 0, show_custom_model: true, + }) + expect(r.headers.get("x-ide-version")).toBe("0.1.69") + 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"]) + const m = live["deepseek-v4.1-flash"] + expect(m.name).toBe("DeepSeek-V4.1-Flash") + expect(m.limit).toEqual({ context: 200000, output: 64000 }) + expect(live["glm-5.2"].limit).toEqual({ context: 200000, output: 32000 }) + const opts = await hooks.auth.loader(async () => signedIn()) + const ask = async (model) => { + const res = await opts.fetch(opts.baseURL + "/chat/completions", { method: "POST", body: JSON.stringify({ model, messages: [{ role: "user", content: "hi" }] }) }) + return (await res.json()).choices[0].message.content + } + // asked through the function that serves it, with the model it names there + expect(await ask("deepseek-v4.1-flash")).toBe("solo_agent deepseek-v4.1-flash__dev") + expect(await ask("glm-5.2")).toBe("chat_v3 glm-5.2__dev") +}) + +test("a batch answered with no lists falls back to one function at a time", async () => { + f = fakeTrae() + f.route("POST /api/ide/v1/batch_get_detail_param", () => json({ code: 0 })) + f.route("POST /api/ide/v1/get_detail_param", (r) => json({ config_info_list: r.json.function === "solo_agent" ? [{ config_name: "deepseek-v4.1-flash" }] : [] })) + const hooks = await TraeCNAuthPlugin({ client: {} }) + const p = await given(hooks) + expect(Object.keys(await hooks.provider.models(p, { auth: signedIn() }))).toEqual(["deepseek-v4.1-flash"]) +}) + +test("signed out asking the batch marks the account", async () => { + f = fakeTrae() + f.route("POST /api/ide/v1/batch_get_detail_param", () => json({ code: 1001, message: "not login" }, 401)) + const hooks = await TraeCNAuthPlugin({ client: {} }) + const p = await given(hooks) + await expect(hooks.provider.models(p, { auth: signedIn() })).rejects.toThrow("sign in again") }) test("SOLO's and the TRAE agent's models are listed too, and asked through the function that lists them (yetone/magpie#681)", async () => {