diff --git a/packages/trae/index.mjs b/packages/trae/index.mjs index b0ccecc..c8a1386 100644 --- a/packages/trae/index.mjs +++ b/packages/trae/index.mjs @@ -39,12 +39,15 @@ const DAY = 24 * 3600 * 1000 // 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"] } } +// a reply limit of 0 is one Trae doesn't say: magpie then takes models.dev's +// for the model (magpie's catalog), where a made-up 32K told agents a far +// smaller one than DeepSeek V4.1 Flash's (ARNO on magpie's Discord) +const MODEL = { attachment: false, tool_call: true, reasoning: true, temperature: true, limit: { context: 128_000, output: 0 }, modalities: { input: ["text"], output: ["text"] } } // what Trae CN's chat_v3 is known to serve; the live list replaces it const MODELS = { "glm-5.2": { name: "GLM-5.2", ...MODEL, limit: { context: 200_000, output: 32_000 } }, "glm-5": { name: "GLM-5", ...MODEL }, - "kimi-k2.6": { name: "Kimi K2.6", ...MODEL, limit: { context: 256_000, output: 32_000 } }, + "kimi-k2.6": { name: "Kimi K2.6", ...MODEL, limit: { context: 256_000, output: 0 } }, "qwen-3.7-plus": { name: "Qwen 3.7 Plus", ...MODEL }, "DeepSeek-V4-Pro": { name: "DeepSeek V4 Pro", ...MODEL }, "DeepSeek-V4-Flash": { name: "DeepSeek V4 Flash", ...MODEL }, @@ -444,8 +447,10 @@ 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 -function chatBody(req, fn, modelName) { +// 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) { const session = randomUUID() const body = { messages: traeMessages(req), @@ -458,7 +463,7 @@ function chatBody(req, fn, modelName) { session_id: session, } const max = req.max_completion_tokens ?? req.max_tokens - if (Number.isFinite(max) && max > 0) body.max_tokens = Math.floor(max) + if (Number.isFinite(max) && max > 0) body.max_tokens = most > 0 ? Math.min(Math.floor(max), most) : Math.floor(max) 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) @@ -1197,7 +1202,7 @@ export const TraeCNAuthPlugin = async ({ client }) => { 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 }) + if (dev) modelNames.set(id, { fn, name: dev.model_name, most: Number(dev.max_tokens) || 0 }) else modelNames.delete(id) } return [...out.values()].map((x) => x.m) @@ -1208,7 +1213,7 @@ export const TraeCNAuthPlugin = async ({ client }) => { const was = provider.models?.[id] ?? {} // 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 out = Number(devModel(m)?.max_tokens) || was.limit?.output || 0 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" } } } @@ -1281,7 +1286,7 @@ export const TraeCNAuthPlugin = async ({ client }) => { 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 : "")), + body: JSON.stringify(chatBody(req, fn, named?.fn === fn ? named.name : "", named?.fn === fn ? named.most : 0)), signal: init.signal ?? r0?.signal, }) if (!res.ok) { diff --git a/packages/trae/package.json b/packages/trae/package.json index 106213e..f40f2ee 100644 --- a/packages/trae/package.json +++ b/packages/trae/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-trae-auth", - "version": "0.1.11", + "version": "0.1.12", "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 b52c71e..69e2cad 100644 --- a/packages/trae/usage.test.mjs +++ b/packages/trae/usage.test.mjs @@ -211,3 +211,35 @@ test("a model chat_v3 gives another's name is named and asked as SOLO lists it ( // named alike everywhere: kept on chat_v3, as before expect(await ask("DeepSeek-V4-Flash-Official")).toBe("chat_v3 DeepSeek-V4-Flash-Official__dev") }) + +// ARNO on magpie's Discord: Trae CN's DeepSeek V4.1 Flash said a 32K reply +// limit, one Trae never gave: a model whose list names no max_tokens says +// none (0), and magpie tells agents models.dev's. One Trae gives is kept, +// and a request asking more than it is held to it. +test("a reply limit is Trae's own or none, and a request is held to Trae's", async () => { + f = fakeTrae() + f.route("POST /api/ide/v1/batch_get_detail_param", () => json({ function_configs: [ + { function: "chat_v3", config_info_list: [ + { config_name: "glm-5.2", usage: "chat_completion", model_detail_list: [{ model_name: "glm-5.2__dev", max_tokens: 32000 }] }, + { config_name: "deepseek-v4.1-flash", usage: "chat_completion", context_window_tokens: { dev: 200000 }, model_detail_list: [{ model_name: "deepseek-v4.1-flash__dev" }] }, + { config_name: "kimi-k2.6", usage: "chat_completion" }, + ] }, + ] })) + f.route("POST /api/agent/v3/llm_utils_chat", (r) => sse([["output", { response: String(r.json.max_tokens) }], ["done", {}]])) + const hooks = await TraeCNAuthPlugin({ client: {} }) + const p = await given(hooks) + expect(p.models["kimi-k2.6"].limit.output).toBe(0) + expect(p.models["DeepSeek-V4-Flash"].limit.output).toBe(0) + const live = await hooks.provider.models(p, { auth: signedIn() }) + expect(live["glm-5.2"].limit.output).toBe(32000) + expect(live["deepseek-v4.1-flash"].limit).toEqual({ context: 200000, output: 0 }) + expect(live["kimi-k2.6"].limit.output).toBe(0) + const opts = await hooks.auth.loader(async () => signedIn()) + const ask = async (model, max_tokens) => { + const res = await opts.fetch(opts.baseURL + "/chat/completions", { method: "POST", body: JSON.stringify({ model, max_tokens, messages: [{ role: "user", content: "hi" }] }) }) + return (await res.json()).choices[0].message.content + } + expect(await ask("glm-5.2", 131072)).toBe("32000") + expect(await ask("glm-5.2", 8000)).toBe("8000") + expect(await ask("deepseek-v4.1-flash", 384000)).toBe("384000") +})