diff --git a/packages/trae/README.md b/packages/trae/README.md index ba10ed3..8a5aa46 100644 --- a/packages/trae/README.md +++ b/packages/trae/README.md @@ -32,9 +32,10 @@ agent's `POST https://trae-api-cn.mchost.guru/api/agent/v3/llm_utils_chat`: - **Request:** - The messages go as `{role, content: [{type: "text", text}]}`. - The model goes as `config_name` and `model`. - - The function is `chat_v3`, the classic IDE's. If Trae answers 4001, 4023 - or 1005, the plugin tries `solo_work_lite` (SOLO's) once and remembers - which one worked. + - The function is the one whose model list has the model (`chat_v3`, the + classic IDE's, first; else `solo_work_lite`, SOLO's). If Trae answers + 4001, 4023 or 1005, the plugin tries the other once and remembers which + one worked. - **Headers:** the IDE's: `Authorization: Cloud-IDE-JWT …`, `X-Cloudide-Token`, `x-app-id`, `x-ide-version`, the device headers and `x-uid`. @@ -54,8 +55,9 @@ agent's `POST https://trae-api-cn.mchost.guru/api/agent/v3/llm_utils_chat`: - Earlier calls and their results go back in as text. - Images aren't sent. -The model list is the account's own, from `/api/ide/v1/get_detail_param`. -When that can't be read, the plugin uses the models Trae CN's `chat_v3` is +The model list is the account's own, from `/api/ide/v1/get_detail_param`, +asked for both functions and put together: SOLO lists models `chat_v3` +doesn't (DeepSeek-V4.1-Flash). When neither can be read, the plugin uses the models Trae CN's `chat_v3` is known to serve: GLM-5.2, GLM-5, Kimi K2.6, Qwen 3.7 Plus, DeepSeek V4 Pro and DeepSeek V4 Flash. diff --git a/packages/trae/index.mjs b/packages/trae/index.mjs index 6f6e29b..0d9cc76 100644 --- a/packages/trae/index.mjs +++ b/packages/trae/index.mjs @@ -727,6 +727,8 @@ export const TraeCNAuthPlugin = async ({ client }) => { const renewed = new Map() // the chat function that served each account last const fnOf = new Map() + // the first chat function whose model list has each model + const listedBy = new Map() const save = async (auth) => { try { @@ -814,10 +816,10 @@ export const TraeCNAuthPlugin = async ({ client }) => { return out } - // liveModels is the account's model list, as the IDE asks for it - const liveModels = async (a) => { + // listOf is one function's model list, as the IDE asks for it + const listOf = async (a, fn) => { const r = await post(a, apiOf(a) + "/api/ide/v1/get_detail_param", { - function: FUNCTIONS[0], config_names: null, need_prompt: false, current_config_info: null, poly_prompt: true, mode_type: null, agent_type: null, + function: fn, config_names: null, need_prompt: false, current_config_info: null, poly_prompt: true, mode_type: null, agent_type: null, }) if (r.status === 401 || lapsedCode(errorOf(r.v).code)) throw new Expired("Trae CN's sign-in has expired; sign in again") if (r.status !== 200) throw new Error(`Trae CN models: ${statusLine(r.status)}`) @@ -825,6 +827,28 @@ export const TraeCNAuthPlugin = async ({ client }) => { return list.filter((m) => m?.config_name) } + // liveModels is the account's model list: every chat function's, since + // SOLO lists models the classic IDE's chat_v3 doesn't (DeepSeek-V4.1-Flash + // came to SOLO first, yetone/magpie#681); a model listed by one function + // only is asked through that one. One function's list failing leaves the + // others'; all failing is the error. + const liveModels = async (a) => { + const got = await Promise.allSettled(FUNCTIONS.map((fn) => listOf(a, fn))) + const expired = got.find((g) => g.status === "rejected" && g.reason instanceof Expired) + if (expired) throw expired.reason + if (got.every((g) => g.status === "rejected")) throw got[0].reason + const out = new Map() + got.forEach((g, i) => { + if (g.status !== "fulfilled") return + for (const m of g.value) { + const id = String(m.config_name) + if (!out.has(id)) out.set(id, { m, fn: FUNCTIONS[i] }) + } + }) + for (const [id, { fn }] of out) listedBy.set(id, fn) + return [...out.values()].map((x) => x.m) + } + const modelOf = (provider, m) => { const id = String(m.config_name) const was = provider.models?.[id] ?? {} @@ -889,7 +913,9 @@ export const TraeCNAuthPlugin = async ({ client }) => { } const signIn = a.renewed ? "renewed" : "kept" const who = a.uid || a.name - const fns = fnOf.has(who) ? [fnOf.get(who), ...FUNCTIONS.filter((f) => f !== fnOf.get(who))] : FUNCTIONS + // the function that lists the model first, then the one that + // served this account last, then the rest + const fns = [...new Set([listedBy.get(String(req.model)), fnOf.get(who), ...FUNCTIONS].filter(Boolean))] let last for (const fn of fns) { const res = await fetch(apiOf(a) + "/api/agent/v3/llm_utils_chat", { diff --git a/packages/trae/package.json b/packages/trae/package.json index 62e57f7..6e5426a 100644 --- a/packages/trae/package.json +++ b/packages/trae/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-trae-auth", - "version": "0.1.0", + "version": "0.1.1", "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 56bfb17..81579fa 100644 --- a/packages/trae/usage.test.mjs +++ b/packages/trae/usage.test.mjs @@ -3,7 +3,7 @@ import "./nonet.mjs" import { afterEach, expect, test } from "bun:test" import { TraeCNAuthPlugin, _internal } from "./index.mjs" -import { fakeTrae, json, signedIn } from "./fake.mjs" +import { fakeTrae, json, signedIn, sse } from "./fake.mjs" let f afterEach(() => f?.close()) @@ -58,7 +58,38 @@ test("the config declares the known models; the live list replaces them", async const live = await hooks.provider.models(p, { auth: signedIn() }) expect(Object.keys(live)).toEqual(["glm-5.2", "DeepSeek-V4-Pro"]) expect(live["glm-5.2"].limit.context).toBe(200000) - expect(f.seen[0].json.function).toBe("chat_v3") + expect(f.seen.map((r) => r.json.function).sort()).toEqual(["chat_v3", "solo_work_lite"]) +}) + +test("SOLO's models are listed too, and asked through SOLO's function (yetone/magpie#681)", async () => { + f = fakeTrae() + f.route("POST /api/ide/v1/get_detail_param", (r) => json({ config_info_list: r.json.function === "chat_v3" + ? [{ config_name: "glm-5.2", display_name: "GLM-5.2" }, { config_name: "DeepSeek-V4-Flash-Official", display_name: "DeepSeek-V4-Flash-Official" }] + : [{ config_name: "glm-5.2", display_name: "GLM-5.2" }, { config_name: "DeepSeek-V4.1-Flash", display_name: "DeepSeek-V4.1-Flash", context_window_size: { max: [1000000] } }] })) + f.route("POST /api/agent/v3/llm_utils_chat", (r) => sse([["output", { response: r.json.function }], ["done", {}]])) + const hooks = await TraeCNAuthPlugin({ client: {} }) + const p = await given(hooks) + const live = await hooks.provider.models(p, { auth: signedIn() }) + expect(Object.keys(live)).toEqual(["glm-5.2", "DeepSeek-V4-Flash-Official", "DeepSeek-V4.1-Flash"]) + expect(live["DeepSeek-V4.1-Flash"].name).toBe("DeepSeek-V4.1-Flash") + expect(live["DeepSeek-V4.1-Flash"].limit.context).toBe(1000000) + 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 + } + expect(await ask("DeepSeek-V4.1-Flash")).toBe("solo_work_lite") + expect(await ask("glm-5.2")).toBe("chat_v3") +}) + +test("one function's list failing leaves the other's", async () => { + f = fakeTrae() + f.route("POST /api/ide/v1/get_detail_param", (r) => r.json.function === "chat_v3" + ? json({ config_info_list: [{ config_name: "glm-5.2" }] }) + : new Response("down", { status: 503 })) + const hooks = await TraeCNAuthPlugin({ client: {} }) + const p = await given(hooks) + expect(Object.keys(await hooks.provider.models(p, { auth: signedIn() }))).toEqual(["glm-5.2"]) }) test("when the list can't be read, the known models stand", async () => {