diff --git a/packages/trae/README.md b/packages/trae/README.md index a109592..91d6761 100644 --- a/packages/trae/README.md +++ b/packages/trae/README.md @@ -80,10 +80,12 @@ 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`, -output the `__dev` model's `max_tokens`. A config with a `__max` model and +output the `__dev` model's `max_tokens`. A model with a `__max` model and a bigger `context_window_tokens.max` is listed again as its Max, `-max` ("… (Max)"), with that window and the `__max` model's -`max_tokens`; it asks the config's `__max` model, with `max_tokens` set +`max_tokens`; the two can come from different functions' lists (chat_v3 +can name the `__max` model, a SOLO list the windows). It asks the +`__max` model through the function that names it, with `max_tokens` set and `user_message_context.model_info.prompt_max_tokens` the window less it, as Max mode does. diff --git a/packages/trae/index.mjs b/packages/trae/index.mjs index bfe8549..1215ba3 100644 --- a/packages/trae/index.mjs +++ b/packages/trae/index.mjs @@ -1133,14 +1133,21 @@ function chatModel(m) { // 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 ?? ""))) -// maxModel is a config's Max mode: its __max model and the window -// context_window_tokens.max gives it, when that is bigger than dev's; a -// config without one has no Max -const maxModel = (m) => { - const d = (Array.isArray(m?.model_detail_list) ? m.model_detail_list : []).find((d) => /__max$/.test(String(d?.model_name ?? ""))) - const window = Number(m?.context_window_tokens?.max) || 0 - const dev = Number(m?.context_window_tokens?.dev) || 0 - return d && window > dev ? { name: String(d.model_name), most: Number(d.max_tokens) || 0, window } : null +// maxModel is a model's Max mode, from the entries its lists give it (the +// one it is asked through first): the first __max model one names, and the +// window context_window_tokens.max gives it, when that is bigger than dev's. +// The lists needn't agree: chat_v3 can name the __max model while the SOLO +// list the model is asked through names only __dev, and the window can be +// another entry's again. A model with no __max, or no bigger window, has no +// Max. +const maxModel = (...ms) => { + const details = (m) => (Array.isArray(m?.model_detail_list) ? m.model_detail_list : []) + const at = ms.find((m) => details(m).some((d) => /__max$/.test(String(d?.model_name ?? "")))) + if (!at) return null + const d = details(at).find((d) => /__max$/.test(String(d?.model_name ?? ""))) + const first = (k) => [at, ...ms].map((m) => Number(m?.context_window_tokens?.[k]) || 0).find((n) => n > 0) || 0 + const window = first("max") + return window > first("dev") ? { name: String(d.model_name), most: Number(d.max_tokens) || 0, window, entry: at } : null } // MAX is the suffix of a model's Max: deepseek-v4.1-flash-max is @@ -1186,6 +1193,8 @@ export const TraeCNAuthPlugin = async ({ client }) => { // model it names there const listedBy = new Map() const modelNames = new Map() + // the Max each model the list last read has (null: none) + const maxes = new Map() const save = async (auth) => { try { @@ -1402,10 +1411,18 @@ export const TraeCNAuthPlugin = async ({ client }) => { const dev = devModel(m) if (dev) modelNames.set(id, { fn, name: dev.model_name, most: Number(dev.max_tokens) || 0 }) else modelNames.delete(id) - const max = maxModel(m) - if (max && !out.has(id + MAX)) { - listedBy.set(id + MAX, fn) - modelNames.set(id + MAX, { fn, name: max.name, most: max.most, max: { base: id, window: max.window } }) + if (out.has(id + MAX)) continue + // its Max is asked through the function whose entry names the __max model + const entries = [{ m, fn }, ...FUNCTIONS.flatMap((f) => (lists[f] ?? []).filter((o) => o !== m && chatModel(o) && String(o.config_name) === id).map((o) => ({ m: o, fn: f })))] + const max = maxModel(...entries.map((e) => e.m)) + maxes.set(id, max) + if (max) { + const by = entries.find((e) => e.m === max.entry).fn + listedBy.set(id + MAX, by) + modelNames.set(id + MAX, { fn: by, name: max.name, most: max.most, max: { base: id, window: max.window } }) + } else { + listedBy.delete(id + MAX) + modelNames.delete(id + MAX) } } return [...out.values()].map((x) => x.m) @@ -1445,7 +1462,7 @@ export const TraeCNAuthPlugin = async ({ client }) => { return Object.fromEntries(ms.flatMap((m) => { const id = String(m.config_name) const own = modelOf(provider, m) - const max = maxModel(m) + const max = maxes.get(id) if (!max || ids.has(id + MAX)) return [[id, own]] // its Max, a model of its own: the window and output Max mode gives const was = provider.models?.[id + MAX] ?? {} diff --git a/packages/trae/package.json b/packages/trae/package.json index 1923289..292f101 100644 --- a/packages/trae/package.json +++ b/packages/trae/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-trae-auth", - "version": "0.1.14", + "version": "0.1.15", "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/stream.test.mjs b/packages/trae/stream.test.mjs index 0367452..71fc867 100644 --- a/packages/trae/stream.test.mjs +++ b/packages/trae/stream.test.mjs @@ -174,3 +174,47 @@ test("a model with a Max context is listed a second time, as its Max, which asks b = f.seen.at(-1).json expect([b.model_name, b.user_message_context]).toEqual(["deepseek-v4.1-flash__dev", undefined]) }) + +// ARNO on magpie's Discord: 0.1.14 listed no deepseek-v4.1-flash-max. Trae CN +// lists the model in several functions (yetone/magpie#681), and they needn't +// agree: chat_v3's entry names the __max model, while the SOLO entry the model +// is asked through names only __dev and gives the windows. Its Max is found +// across them, and asked through the function that names the __max model. +const SPLIT = { 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", max_tokens: 128000 }] }, + { 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" }, context_window_tokens: { dev: 200000, max: 1000000 }, model_detail_list: [{ model_name: "deepseek-v4.1-flash__dev", max_tokens: 64000 }] }, + ] }, +] } + +test("a Max the lists split between them is listed, and asked where its __max model is named", async () => { + f = fakeTrae() + f.route("POST /api/ide/v1/batch_get_detail_param", () => json(SPLIT)) + f.route("POST /api/agent/v3/llm_utils_chat", () => sse([["output", { response: "ok" }], ["done", {}]])) + const hooks = await TraeCNAuthPlugin({ client: {} }) + const p = { models: { ..._internal.MODELS } } + const live = await hooks.provider.models(p, { auth: signedIn() }) + expect(Object.keys(live)).toEqual(["deepseek-v4.1-flash", "deepseek-v4.1-flash-max", "DeepSeek-V4-Flash-Official"]) + expect(live["deepseek-v4.1-flash"].limit).toEqual({ context: 200000, output: 64000 }) + expect(live["deepseek-v4.1-flash-max"].limit).toEqual({ context: 1000000, output: 128000 }) + expect(live["deepseek-v4.1-flash-max"].name).toBe("DeepSeek-V4.1-Flash (Max)") + const opts = await hooks.auth.loader(async () => signedIn()) + const send = (body) => opts.fetch(opts.baseURL + "/chat/completions", { method: "POST", body: JSON.stringify({ messages: [{ role: "user", content: "hi" }], ...body }) }) + await send({ model: "deepseek-v4.1-flash-max" }) + let b = f.seen.at(-1).json + expect([b.function, b.config_name, b.model_name, b.max_tokens, b.user_message_context.model_info.prompt_max_tokens]).toEqual(["chat_v3", "deepseek-v4.1-flash", "deepseek-v4.1-flash__max", 128000, 872000]) + await send({ model: "deepseek-v4.1-flash" }) + b = f.seen.at(-1).json + expect([b.function, b.model_name, b.user_message_context]).toEqual(["solo_work_lite", "deepseek-v4.1-flash__dev", undefined]) +}) + +test("a __max model with no bigger window in any list is no Max", async () => { + f = fakeTrae() + f.route("POST /api/ide/v1/batch_get_detail_param", () => json({ function_configs: [SPLIT.function_configs[0]] })) + const hooks = await TraeCNAuthPlugin({ client: {} }) + const live = await hooks.provider.models({ models: { ..._internal.MODELS } }, { auth: signedIn() }) + expect(Object.keys(live)).toEqual(["deepseek-v4.1-flash", "DeepSeek-V4-Flash-Official"]) +})