diff --git a/packages/trae/index.mjs b/packages/trae/index.mjs index 70eed08..11ad04f 100644 --- a/packages/trae/index.mjs +++ b/packages/trae/index.mjs @@ -930,7 +930,8 @@ export const TraeCNAuthPlugin = async ({ client }) => { // 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. + // names a __dev model (one Trae serves) and isn't misnamed, else the + // first that names one, else the first. const liveModels = async (a) => { let lists = null try { @@ -948,13 +949,35 @@ export const TraeCNAuthPlugin = async ({ client }) => { if (g.status === "fulfilled") lists[FUNCTIONS[i]] = g.value }) } + // a function can give a model another's name: chat_v3 calls + // deepseek-v4.1-flash "DeepSeek-V4-Flash 正式版", as it does + // DeepSeek-V4-Flash-Official, where SOLO's lists call it + // DeepSeek-V4.1-Flash (yetone/magpie#681) — Trae's usage page then books + // it under that name too. Such an entry is misnamed: its name is another + // model's in the same list, and other lists name it otherwise. + const nameOf = (m) => String(m.display_config?.display_name || m.display_name || m.display_model_name || m.config_name) + const names = new Map() // id → the names its lists give it + for (const fn of FUNCTIONS) { + for (const m of lists[fn] ?? []) { + if (!chatModel(m)) continue + const id = String(m.config_name) + names.set(id, (names.get(id) ?? new Set()).add(nameOf(m))) + } + } + const misnamed = (fn, m) => { + const id = String(m.config_name), name = nameOf(m) + return names.get(id).size > 1 && (lists[fn] ?? []).some((o) => chatModel(o) && String(o.config_name) !== id && nameOf(o) === name) + } + // a function's entry is worth more when it names a __dev model, then + // when it isn't misnamed; the first of the best is taken + const worth = (fn, m) => (devModel(m) ? 2 : 0) + (misnamed(fn, m) ? 0 : 1) const out = new Map() for (const fn of FUNCTIONS) { for (const m of lists[fn] ?? []) { if (!chatModel(m)) continue const id = String(m.config_name) const was = out.get(id) - if (!was || (!devModel(was.m) && devModel(m))) out.set(id, { m, fn }) + if (!was || worth(fn, m) > was.worth) out.set(id, { m, fn, worth: worth(fn, m) }) } } for (const [id, { m, fn }] of out) { diff --git a/packages/trae/package.json b/packages/trae/package.json index 23f1be5..e9637ff 100644 --- a/packages/trae/package.json +++ b/packages/trae/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-trae-auth", - "version": "0.1.4", + "version": "0.1.5", "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 9362057..2fcbd74 100644 --- a/packages/trae/usage.test.mjs +++ b/packages/trae/usage.test.mjs @@ -173,3 +173,41 @@ test("when the list can't be read, the known models stand", async () => { const p = await given(hooks) expect(await hooks.provider.models(p, { auth: signedIn() })).toBe(p.models) }) + +// as Trae CN answered the reporter (yetone/magpie#681): chat_v3 lists +// deepseek-v4.1-flash with its __dev model but under DeepSeek-V4-Flash-Official's +// name, and Trae's usage page books it under that name; SOLO's lists name it +// DeepSeek-V4.1-Flash +const SAME_NAME = { function_configs: [ + { function: "chat_v3", config_info_list: [ + { config_name: "deepseek-v4.1-flash", usage: "chat_completion", display_config: { display_name: "DeepSeek-V4-Flash 正式版" }, model_detail_list: [{ model_name: "deepseek-v4.1-flash__dev" }, { model_name: "deepseek-v4.1-flash__max" }] }, + { config_name: "DeepSeek-V4-Flash-Official", usage: "chat_completion", display_config: { display_name: "DeepSeek-V4-Flash 正式版" }, model_detail_list: [{ model_name: "DeepSeek-V4-Flash-Official__dev" }] }, + ] }, + { function: "solo_work_lite", config_info_list: [ + { config_name: "deepseek-v4.1-flash", usage: "chat_completion", display_config: { display_name: "DeepSeek-V4.1-Flash" }, model_detail_list: [{ model_name: "deepseek-v4.1-flash__dev" }] }, + { config_name: "DeepSeek-V4-Flash-Official", usage: "chat_completion", display_config: { display_name: "DeepSeek-V4-Flash 正式版" }, model_detail_list: [{ model_name: "DeepSeek-V4-Flash-Official__dev" }] }, + ] }, + { function: "solo_agent", config_info_list: [ + { config_name: "deepseek-v4.1-flash", usage: "chat_completion", display_config: { display_name: "DeepSeek-V4.1-Flash" }, model_detail_list: [{ model_name: "deepseek-v4.1-flash__dev" }] }, + ] }, +] } + +test("a model chat_v3 gives another's name is named and asked as SOLO lists it (yetone/magpie#681)", async () => { + f = fakeTrae() + f.route("POST /api/ide/v1/batch_get_detail_param", () => json(SAME_NAME)) + 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() }) + expect(live["deepseek-v4.1-flash"].name).toBe("DeepSeek-V4.1-Flash") + expect(live["DeepSeek-V4-Flash-Official"].name).toBe("DeepSeek-V4-Flash 正式版") + 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 + } + // the first list that names it rightly, with its __dev model + expect(await ask("deepseek-v4.1-flash")).toBe("solo_work_lite deepseek-v4.1-flash__dev") + // named alike everywhere: kept on chat_v3, as before + expect(await ask("DeepSeek-V4-Flash-Official")).toBe("chat_v3 DeepSeek-V4-Flash-Official__dev") +})