diff --git a/packages/trae/README.md b/packages/trae/README.md index 7c9943e..63cf7e5 100644 --- a/packages/trae/README.md +++ b/packages/trae/README.md @@ -246,11 +246,13 @@ custom-model slots. An enterprise's own built-in models since 0.2.41 (#1258), unless the admin switched one off (`status` false); a personal custom model (`config_source` 3) is not, its key being in the user's IDE only. A chat with an enterprise model carries `config_source` -2, `is_custom_model` true and the entry's `provider` at the top of the -body, as Trae CN 3.4.20's own `llm_utils_chat` request does (0.2.42; 0.2.41 +2, `is_custom_model` true, and the `model_name` and `provider` of its +`model_detail_list` entry (its `__dev` one, else the first) at the top of the +body, as Trae CN 3.4.20's own `llm_utils_chat` request does (0.2.43; 0.2.41 put them under `user_message_context.model_info`, and an enterprise seat got -4001 "the param is invalid" on every function) — **not verified with a real -enterprise seat**. A refused chat's error names +4001 "the param is invalid" on every function; 0.2.42 read `provider` off the +config, where Trae's list doesn't carry it, sent no `model_name`, and got +4023 "something went wrong") — **not verified with a real enterprise seat**. A refused chat's error names the model, the functions tried and the host, and says when the account's list doesn't have the model. Context is the list's `context_window_tokens.dev`, output the `__dev` model's `max_tokens`. A model with a `__max` model and diff --git a/packages/trae/entmodels.test.mjs b/packages/trae/entmodels.test.mjs index dfef731..40cdc7c 100644 --- a/packages/trae/entmodels.test.mjs +++ b/packages/trae/entmodels.test.mjs @@ -98,3 +98,51 @@ test("a refused chat names the model, the functions tried and the host", async ( expect(msg).toContain("model Acme-Qwen-72B asked through chat_v3") expect(msg).toContain("this account's model list doesn't have Acme-Qwen-72B") }) + +// the reporter's next error (#1258, 0.2.42, console.enterprise.trae.cn): with +// config_source sent, both of their enterprise models got 4023 on every +// function. Trae's list types carry provider and model_name on each +// model_detail_list item, not on the config, and 0.2.42 sent neither. The +// entries' detail values are inferred from the config names they gave +// (provider/model); their real list entry hasn't been seen. +const WENT_WRONG = "We're sorry, something went wrong. Please try again later or contact our support team for assistance." +const listed = [ + { config_name: "AI/deekseek-v4.1-flash", usage: "chat_completion", config_source: 2, is_preset: false, is_custom_model: true, status: true, + display_config: { display_name: "deekseek-v4.1-flash", is_custom_model: true }, + model_detail_list: [{ model_name: "deekseek-v4.1-flash", provider: "AI", max_tokens: 8192 }] }, + { config_name: "volc/deepseek-v4-1-flash", usage: "chat_completion", config_source: 2, is_preset: false, is_custom_model: true, status: true, + display_config: { display_name: "deepseek-v4-1-flash", is_custom_model: true }, + model_detail_list: [{ model_name: "deepseek-v4-1-flash", provider: "volc", max_tokens: 8192 }] }, +] + +test("a chat with an enterprise model sends its detail's model_name and provider", async () => { + const hooks = await plugin([preset, ...listed]) + await hooks.provider.models({ models: {} }, { auth: signedIn() }) + // Trae as the reporter met it on 0.2.42: an enterprise config without the + // model and provider it is served by went wrong + f.route("POST /api/agent/v3/llm_utils_chat", (r) => { + const e = listed.find((m) => m.config_name === r.json.config_name) + const d = e?.model_detail_list[0] + if (e && (r.json.model_name !== d.model_name || r.json.provider !== d.provider)) return sse([["error", { code: 4023, message: WENT_WRONG }]]) + return sse([["output", { response: "ok" }], ["done", {}]]) + }) + const opts = await hooks.auth.loader(async () => signedIn()) + for (const m of listed) { + const res = await opts.fetch(opts.baseURL + "/chat/completions", { method: "POST", body: JSON.stringify({ model: m.config_name, messages: [{ role: "user", content: "hi" }] }) }) + expect(res.status).toBe(200) + expect((await res.json()).choices[0].message.content).toBe("ok") + } + const chats = f.seen.filter((r) => r.path === "/api/agent/v3/llm_utils_chat") + expect(chats.length).toBe(2) + expect(chats[0].json).toMatchObject({ config_name: "AI/deekseek-v4.1-flash", config_source: 2, is_custom_model: true, model_name: "deekseek-v4.1-flash", provider: "AI" }) + expect(chats[1].json).toMatchObject({ config_name: "volc/deepseek-v4-1-flash", config_source: 2, is_custom_model: true, model_name: "deepseek-v4-1-flash", provider: "volc" }) +}) + +test("an enterprise model's __dev detail is the one asked by; a preset's body gains nothing", () => { + const two = { ...listed[1], model_detail_list: [{ model_name: "deepseek-v4-1-flash__max", provider: "volc" }, { model_name: "deepseek-v4-1-flash__dev", provider: "volc" }] } + const body = _internal.chatBody({ model: two.config_name, messages: [{ role: "user", content: "hi" }] }, "chat_v3", "", 0, null, "s", false, null, two) + expect(body.model_name).toBe("deepseek-v4-1-flash__dev") + expect(body.provider).toBe("volc") + const p = _internal.chatBody({ model: "glm-5", messages: [{ role: "user", content: "hi" }] }, "chat_v3", "", 0, null, "s", false, null, preset) + for (const k of ["model_name", "provider", "config_source", "is_custom_model"]) expect(k in p).toBe(false) +}) diff --git a/packages/trae/index.mjs b/packages/trae/index.mjs index 4ddbd8a..cb40173 100644 --- a/packages/trae/index.mjs +++ b/packages/trae/index.mjs @@ -1649,10 +1649,22 @@ function chatBody(req, fn, modelName, most = 0, max = null, session = "", sees = // under user_message_context.model_info, the agent task's shape). Not yet // seen answering from a real enterprise seat: a Trae preset is sent as // before. + // + // model_name and provider are the entry's model detail's: Trae's list + // types (libharness CloudModelConfigInfo / CloudModelDetailInfo) carry + // provider on each model_detail_list item, beside its model_name, and + // not on the config, and an enterprise model's detail has no __dev name + // for modelName to come from. 0.2.42 read provider off the config and + // sent no model_name, so both were missing, and the reporter's seat got + // 4023 "We're sorry, something went wrong" on every function (#1258, + // AI/deekseek-v4.1-flash and volc/deepseek-v4-1-flash). if (enterpriseModel(entry)) { body.config_source = ENTERPRISE body.is_custom_model = true - if (entry.provider) body.provider = String(entry.provider) + const detail = enterpriseDetail(entry) + if (!body.model_name && detail?.model_name) body.model_name = String(detail.model_name) + const provider = entry.provider || detail?.provider + if (provider) body.provider = String(provider) } if (typeof req.temperature === "number") body.temperature = req.temperature if (Array.isArray(req.tools) && req.tools.length && req.tool_choice !== "none") { @@ -3085,6 +3097,9 @@ const wrongFunction = (code) => ["4001", "4023", "1005"].includes(String(code)) // One the admin switched off (status false) stays out, as in the IDE. const ENTERPRISE = 2 const enterpriseModel = (m) => Number(m?.config_source) === ENTERPRISE +// enterpriseDetail is the model detail an enterprise model is asked by: its +// __dev one when it names one, else the first that names a model +const enterpriseDetail = (m) => devModel(m) ?? (Array.isArray(m?.model_detail_list) ? m.model_detail_list : []).find((d) => d?.model_name) function chatModel(m, site) { const id = String(m?.config_name ?? "") if (!id) return false diff --git a/packages/trae/package.json b/packages/trae/package.json index 9a6bdaf..9a1cf43 100644 --- a/packages/trae/package.json +++ b/packages/trae/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-trae-auth", - "version": "0.2.42", + "version": "0.2.43", "description": "OpenCode provider plugins for Trae: Trae CN (trae.cn) and Trae Global (trae.ai), free tier included, with Trae's own browser sign-in", "type": "module", "main": "./opencode.mjs",