diff --git a/packages/trae/README.md b/packages/trae/README.md index da4541b..bf1a900 100644 --- a/packages/trae/README.md +++ b/packages/trae/README.md @@ -251,8 +251,13 @@ which levels it takes — its `reasoning_effort_config`, with the options names as `reasoning_effort`, so a level an agent picks is sent as Trae spells it: the plugin offers each model's own levels as `variants` (`low`/`high`/`xhigh` for `light`/`high`/`extra_high`; `medium` and `max` -keep their names, offered only where the entry names them, as for DeepSeek -V4.1 Flash's `max` in Trae's app), and sends nothing +keep their names where the entry names them). A model whose top level is +`extra_high` and that names no `max` — TRAE CN's DeepSeek-V4.1-Flash, +GLM-5.3 and Kimi-K3, 轻/高/极高 in Trae's app — is offered `max` too, sent +as `extra_high` (as `xhigh` is): 极高 is that model's max, so in a magpie +routing group it ranges up to max beside members that have one, and a +group set to max sends it `extra_high`. An entry naming Trae's own `max` +keeps it, with `extra_high` as `xhigh`. The plugin sends nothing for a level the model doesn't name, or for a model whose entry names none — Trae then serves it at its own default. An entry naming levels this map can't spell offers none, rather than being sent one Trae would reject. A diff --git a/packages/trae/index.mjs b/packages/trae/index.mjs index 444dda7..314293b 100644 --- a/packages/trae/index.mjs +++ b/packages/trae/index.mjs @@ -2996,6 +2996,15 @@ const MAX = "-max" // 更大上下文 (Max) 1M-window switch, which is the model id + MAX below, not a // level (#434's screenshot). // +// TRAE CN's 极高 (extra_high) is the top of a model that names no max of its +// own — DeepSeek-V4.1-Flash, GLM-5.3 and Kimi-K3 offer 轻/高/极高 — so it is +// that model's max too (@avalongarden6 on X, 11-09). Offered as xhigh alone, +// such a member ranged 低–很高 beside WorkBuddy's and Qoder's …–最高 in a +// routing group, and the group, which offers the levels its members share, +// lost max for every member. So such a model is offered max as well, and max +// is sent as extra_high (topsAtXhigh). An entry that names Trae's own max +// keeps it: max is max and extra_high is xhigh. +// // An entry without the config takes no level: Trae serves it at its own // default and nothing is sent. The plugin used to send none for every model, // which read as "Trae takes no level" — it takes them, per model, and an @@ -3020,19 +3029,27 @@ const levelsOf = (m) => { // spelling here would make the body say "light", which is not one of the // levels this plugin offers, and the field would then be dropped. {} when the // model takes none. -const variantsOf = (levels) => Object.fromEntries((levels ?? []).map((level) => [level, { reasoningEffort: level }])) +const variantsOf = (levels) => Object.fromEntries(offered(levels).map((level) => [level, { reasoningEffort: level }])) + +// topsAtXhigh: the entry's top level is extra_high, with no max of its own +const topsAtXhigh = (levels) => Array.isArray(levels) && levels.includes("xhigh") && !levels.includes("max") + +// offered is the levels agents are offered for a model: its own, and max +// beside an extra_high that is its top (sent as extra_high, effortOf) +const offered = (levels) => (topsAtXhigh(levels) ? [...levels, "max"] : levels ?? []) // effortOf is the name to send Trae for an effort the request carries, or // undefined when the model doesn't take that one: a level Trae has no entry for // is rejected, so nothing is sent instead. The effort arrives in either // spelling — this plugin's own (low/high/xhigh) from its variants, or Trae's // (light/extra_high) from a client that read them off the list — and both name -// the same level. +// the same level. max is sent as extra_high to a model whose top that is. const effortOf = (levels, effort) => { if (typeof effort !== "string" || !levels) return undefined const mine = Object.values(LEVELS).includes(effort) ? effort : Object.prototype.hasOwnProperty.call(LEVELS, effort) ? LEVELS[effort] : undefined + if (mine === "max" && topsAtXhigh(levels)) return "extra_high" if (mine === undefined || !levels.includes(mine)) return undefined return Object.keys(LEVELS).find((k) => LEVELS[k] === mine) } @@ -3823,4 +3840,4 @@ const makePlugin = (site) => async ({ client }) => { export const TraeCNAuthPlugin = makePlugin(SITES["trae-cn"]) export const TraeGlobalAuthPlugin = makePlugin(SITES["trae-global"]) -export const _internal = { ownClient, clientStoragePaths, clientDevice, clientProfiles, deviceNote, clientDecode, thisMachine, traeHost, accountOf, HOSTS: SITES["trae-cn"].hosts, MODELS: SITES["trae-cn"].models, TextTools, NativeCalls, looseJSON, glmCall, nameCall, toolNamed, unwrapped, traeMessages, chatBody, nativeTools, moonshotSchema, credits, dollarUsageOf, whenOf, newDevice, ugDevice, proofOf, SITES, SESSION, conversationID, LEVELS, levelsOf, variantsOf, effortOf, traeRateOf } +export const _internal = { ownClient, clientStoragePaths, clientDevice, clientProfiles, deviceNote, clientDecode, thisMachine, traeHost, accountOf, HOSTS: SITES["trae-cn"].hosts, MODELS: SITES["trae-cn"].models, TextTools, NativeCalls, looseJSON, glmCall, nameCall, toolNamed, unwrapped, traeMessages, chatBody, nativeTools, moonshotSchema, credits, dollarUsageOf, whenOf, newDevice, ugDevice, proofOf, SITES, SESSION, conversationID, LEVELS, levelsOf, variantsOf, effortOf, offered, traeRateOf } diff --git a/packages/trae/package.json b/packages/trae/package.json index d300d54..1897bea 100644 --- a/packages/trae/package.json +++ b/packages/trae/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-trae-auth", - "version": "0.2.32", + "version": "0.2.33", "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", diff --git a/packages/trae/reasoning.test.mjs b/packages/trae/reasoning.test.mjs index 84cbf46..99fbd63 100644 --- a/packages/trae/reasoning.test.mjs +++ b/packages/trae/reasoning.test.mjs @@ -40,12 +40,15 @@ async function live(configs) { test("an entry's levels are offered as the variants agents pick, named as OpenCode names them", async () => { const ms = await live([entry("deepseek-v4.1-flash", ["light", "high", "extra_high"], { default_level: "high" })]) const m = ms["deepseek-v4.1-flash"] - expect(Object.keys(m.variants)).toEqual(["low", "high", "xhigh"]) + // extra_high is this model's top, so it is offered as max too (sent as + // extra_high): a routing group with it in can offer max + expect(Object.keys(m.variants)).toEqual(["low", "high", "xhigh", "max"]) // the value is what OpenCode writes into the body, so it is this plugin's // name for the level; effortOf turns it into Trae's on the way out expect(m.variants.low).toEqual({ reasoningEffort: "low" }) expect(m.variants.high).toEqual({ reasoningEffort: "high" }) expect(m.variants.xhigh).toEqual({ reasoningEffort: "xhigh" }) + expect(m.variants.max).toEqual({ reasoningEffort: "max" }) // the model reasons, as it did before the levels came expect(m.capabilities.reasoning).toBe(true) }) @@ -65,8 +68,10 @@ test("max and medium an entry names are offered and sent as Trae names them (#43 const levels = ["low", "medium", "high", "xhigh", "max"] expect(chatBody(req("max"), "chat_v3", "", 0, null, "s", false, levels).reasoning_effort).toBe("max") expect(chatBody(req("medium"), "chat_v3", "", 0, null, "s", false, levels).reasoning_effort).toBe("medium") - // a model that names no max still sends nothing for it - expect(chatBody(req("max"), "chat_v3", "", 0, null, "s", false, ["low", "high", "xhigh"]).reasoning_effort).toBeUndefined() + // a model that names no max and tops at extra_high is sent extra_high for it + expect(chatBody(req("max"), "chat_v3", "", 0, null, "s", false, ["low", "high", "xhigh"]).reasoning_effort).toBe("extra_high") + // one whose top is lower still sends nothing for it + expect(chatBody(req("max"), "chat_v3", "", 0, null, "s", false, ["low", "high"]).reasoning_effort).toBeUndefined() }) test("max reaches Trae's request for a model whose entry names it (#434)", async () => { @@ -107,7 +112,7 @@ test("the levels an agent picks are sent as Trae's own names, and left out when expect(chatBody(req("high"), "chat_v3", "", 0, null, "s", false, levels).reasoning_effort).toBe("high") expect(chatBody(req("xhigh"), "chat_v3", "", 0, null, "s", false, levels).reasoning_effort).toBe("extra_high") // a level the model doesn't take is not sent: Trae has no entry for it - expect(chatBody(req("max"), "chat_v3", "", 0, null, "s", false, levels).reasoning_effort).toBeUndefined() + expect(chatBody(req("medium"), "chat_v3", "", 0, null, "s", false, levels).reasoning_effort).toBeUndefined() // no effort asked, or a model with no levels: nothing sent expect(chatBody(req(), "chat_v3", "", 0, null, "s", false, levels).reasoning_effort).toBeUndefined() expect(chatBody(req("high"), "chat_v3", "", 0, null, "s", false, []).reasoning_effort).toBeUndefined() @@ -146,7 +151,7 @@ test("every variant's own value survives the round trip to Trae's request", asyn const variants = ms["deepseek-v4.1-flash"].variants const opts = await hooks.auth.loader(async () => signedIn()) - const expectSent = { low: "light", high: "high", xhigh: "extra_high" } + const expectSent = { low: "light", high: "high", xhigh: "extra_high", max: "extra_high" } for (const [name, v] of Object.entries(variants)) { await opts.fetch(opts.baseURL + "/chat/completions", { method: "POST", @@ -181,8 +186,8 @@ test("a model in the fallback table keeps the levels its entry names", async () const ms = await hooks.provider.models(provider, { auth: signedIn() }) // glm-5.2 is in the fallback table, so its config entry is the empty one - expect(Object.keys(ms["glm-5.2"].variants)).toEqual(["high", "xhigh"]) - expect(Object.keys(ms["glm-5.3"].variants)).toEqual(["low", "high", "xhigh"]) + expect(Object.keys(ms["glm-5.2"].variants)).toEqual(["high", "xhigh", "max"]) + expect(Object.keys(ms["glm-5.3"].variants)).toEqual(["low", "high", "xhigh", "max"]) }) test("a request for a model with no levels carries no reasoning_effort", async () => { @@ -384,7 +389,7 @@ test("of two entries a model has, the one naming levels is kept, so its levels a ] })) const hooks = await TraeCNAuthPlugin({ client: {} }) const ms = await hooks.provider.models({ models: {} }, { auth: signedIn() }) - expect(Object.keys(ms["glm-5.3"].variants)).toEqual(["low", "high", "xhigh"]) + expect(Object.keys(ms["glm-5.3"].variants)).toEqual(["low", "high", "xhigh", "max"]) }) test("a name the lists agree on still outranks an entry's levels", async () => { @@ -405,3 +410,62 @@ test("a name the lists agree on still outranks an entry's levels", async () => { expect(Object.keys(ms["glm-5.3"].variants)).toEqual([]) expect(ms["glm-5.3"].name).toBe("GLM-5.3") }) + +// @avalongarden6 on X (11-09): TRAE CN's app offers DeepSeek-V4.1-Flash, +// GLM-5.3 and Kimi-K3 轻/高/极高, and 极高 is the model's top. magpie ranged +// the Trae member 低–很高 beside WorkBuddy's and Qoder's …–最高, and a +// routing group, which offers the levels its members share, lost max. A +// model whose top is extra_high is offered max, sent as extra_high. +test("a model whose top is extra_high is offered max, and max and xhigh both go as extra_high", async () => { + const { offered } = _internal + for (const name of ["deepseek-v4.1-flash", "glm-5.3", "kimi-k3"]) { + const ms = await live([entry(name, ["light", "high", "extra_high"], { default_level: "high" })]) + expect(Object.keys(ms[name].variants)).toEqual(["low", "high", "xhigh", "max"]) + f.close() + } + const levels = ["low", "high", "xhigh"] + expect(offered(levels)).toEqual(["low", "high", "xhigh", "max"]) + expect(effortOf(levels, "max")).toBe("extra_high") + expect(effortOf(levels, "xhigh")).toBe("extra_high") + expect(effortOf(levels, "extra_high")).toBe("extra_high") + // low and high are as they were + expect(effortOf(levels, "low")).toBe("light") + expect(effortOf(levels, "light")).toBe("light") + expect(effortOf(levels, "high")).toBe("high") + // a model that tops lower is offered no max, and sent none for it + expect(offered(["low", "high"])).toEqual(["low", "high"]) + expect(effortOf(["low", "high"], "max")).toBeUndefined() + // nor one with no levels + expect(offered([])).toEqual([]) + expect(offered(null)).toEqual([]) + expect(effortOf([], "max")).toBeUndefined() +}) + +test("an entry that names Trae's own max keeps it: max is max, extra_high is xhigh", () => { + const { offered } = _internal + const levels = levelsOf({ reasoning_effort_config: { options: ["light", "high", "extra_high", "max"] } }) + expect(offered(levels)).toEqual(["low", "high", "xhigh", "max"]) + expect(effortOf(levels, "max")).toBe("max") + expect(effortOf(levels, "xhigh")).toBe("extra_high") + expect(effortOf(levels, "low")).toBe("light") +}) + +// a routing group set to max: magpie fits max to the member's own levels +// (catalog.NearestEffort over the variants), finds max among them and sends +// the plugin max, which must reach Trae as extra_high +test("max asked of a model whose top is extra_high reaches Trae as extra_high", async () => { + f = fakeTrae() + f.route("POST /api/ide/v1/batch_get_detail_param", () => json({ function_configs: [ + { function: "chat_v3", config_info_list: [entry("kimi-k3", ["light", "high", "extra_high"], { default_level: "high" })] }, + ] })) + f.route("POST /api/agent/v3/llm_utils_chat", () => sse([["output", { content: "ok" }], ["finish", { finish_reason: "stop" }]])) + const hooks = await TraeCNAuthPlugin({ client: {} }) + const opts = await hooks.auth.loader(async () => signedIn()) + await opts.fetch(opts.baseURL + "/chat/completions", { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ model: "kimi-k3", stream: true, messages: [{ role: "user", content: "hi" }], reasoning_effort: "max" }), + }) + const sent = f.seen.find((r) => r.path === "/api/agent/v3/llm_utils_chat") + expect(sent.json.reasoning_effort).toBe("extra_high") +})