From 28879fdc18e1cd4b781da2bd171834bef221321d Mon Sep 17 00:00:00 2001 From: yetone Date: Thu, 12 Nov 2026 01:38:05 -0800 Subject: [PATCH] doubao-work 0.1.6: a model the app added since the last menu read is found at once (#1048) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The model list is read off the app's menu on sign-in and again only once the cached list is six hours old. A model Doubao added in between (the new DeepSeek, Kimi and GLM rows) was answered 404 "没有模型", whether picked from the list or typed by hand. Now a request that names a model the cached list doesn't have reads the menu again first, queued behind any running ask, at most once a minute so a made-up name doesn't open the menu each time. A menu that can't be read leaves the model unknown and the list as it was. The 404 now names the models the menu has. Run on a separate test Mac (Bun 1.4.2, temp HOME): - bun test packages/doubao-work: 39 pass, 0 fail (3 new). - bun scripts/check.mjs doubao-work: ok. Broken by hand, each red then restored: - the plugin never asks the menu again: 1 fail. - no once-a-minute limit: 1 fail. Not tried: the real 豆包工作 or 豆包 app (no install for a review); the reporter's menu may differ in a way the read doesn't handle, so #1048 stays open for them. Reported-by: lumiai101 --- packages/doubao-work/README.md | 3 +- packages/doubao-work/doubao-work.test.mjs | 60 +++++++++++++++++++++++ packages/doubao-work/doubao.mjs | 17 +++++++ packages/doubao-work/index.mjs | 3 +- packages/doubao-work/package.json | 2 +- 5 files changed, 82 insertions(+), 3 deletions(-) diff --git a/packages/doubao-work/README.md b/packages/doubao-work/README.md index 31e7acd..0019016 100644 --- a/packages/doubao-work/README.md +++ b/packages/doubao-work/README.md @@ -34,7 +34,8 @@ plan), DeepSeek V4.1 Flash (`deepseek-v4.1-flash`), GLM 5.3 (`glm-5.3`) and so on. Rows that open a submenu (推理强度) are not models and are skipped. The list is read when you sign in, and again on a request once the cached list -is more than six hours old; it is kept in `models.json` under +is more than six hours old, or at once when a request names a model the +cached list doesn't have (at most once a minute); it is kept in `models.json` under `DOUBAO_WORK_HOME` (`~/Library/Application Support/magpie-doubao-work` on macOS, `%LOCALAPPDATA%\magpie-doubao-work` on Windows) and read before anything else. The three models the plugin was written with stay as the diff --git a/packages/doubao-work/doubao-work.test.mjs b/packages/doubao-work/doubao-work.test.mjs index 6349f09..8e4420a 100644 --- a/packages/doubao-work/doubao-work.test.mjs +++ b/packages/doubao-work/doubao-work.test.mjs @@ -557,3 +557,63 @@ test("an abort between two polls stops the ask too", async () => { const d = new Doubao({ app: fakeApp, openPage: async () => page, poll: 1 }) await expect(d.ask("x", { signal: ac.signal })).rejects.toBeDefined() }) + +test("a model the app added since the last read is found at once, and a made-up one opens the menu once a minute at most (#1048)", async () => { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), "doubao-models-")) + // the list was read minutes ago, before Doubao added Kimi + fs.writeFileSync(path.join(dir, "models.json"), JSON.stringify({ + at: Date.now(), + models: { auto: MODELS.auto, "doubao-2.1-lite": MODELS["doubao-2.1-lite"] }, + })) + const rows = [{ label: "自动" }, { label: "豆包 2.1 Lite" }, { label: "Kimi K3.1 Pro" }, { label: "GLM 5.3 Flash" }] + const page = fakePage([], { steps: { menu: { rows } } }) + const d = new Doubao({ app: { start: async () => 1, dataDir: dir }, openPage: async () => page, poll: 1 }) + const menus = () => page.calls.filter((c) => c === MENU).length + expect(await d.known("doubao-2.1-lite")).toBe(true) + expect(menus()).toBe(0) + expect(await d.known("kimi-k3.1-pro")).toBe(true) + expect(menus()).toBe(1) + expect(Object.keys(JSON.parse(fs.readFileSync(path.join(dir, "models.json"), "utf8")).models)).toContain("glm-5.3-flash") + // a made-up name in the same minute doesn't open the menu again + expect(await d.known("gpt-9")).toBe(false) + expect(menus()).toBe(1) + d.missAt -= 61000 + expect(await d.known("gpt-9")).toBe(false) + expect(menus()).toBe(2) + expect(await d.known("gpt-10")).toBe(false) + expect(menus()).toBe(2) + fs.rmSync(dir, { recursive: true, force: true }) +}) + +test("a menu that can't be read leaves the model unknown, not the list emptied (#1048)", async () => { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), "doubao-models-")) + fs.writeFileSync(path.join(dir, "models.json"), JSON.stringify({ at: Date.now(), models: { auto: MODELS.auto } })) + const page = fakePage([], { steps: { menu: { error: "no model menu" } } }) + const d = new Doubao({ app: { start: async () => 1, dataDir: dir }, openPage: async () => page, poll: 1 }) + expect(await d.known("kimi-k3")).toBe(false) + expect(Object.keys(d.modelList())).toEqual(["auto"]) + fs.rmSync(dir, { recursive: true, force: true }) +}) + +test("the plugin asks Doubao's menu again for a model its list lacks, and names the models it has (#1048)", async () => { + let table = { auto: MODELS.auto } + const asked = [] + const doubao = { + modelList: () => table, + known: async (model) => { + asked.push(model) + table = { auto: MODELS.auto, "kimi-k3": { label: "Kimi K3", name: "Kimi K3" } } + return !!table[model] + }, + ask: async () => ({ text: "ok", reasoning: "" }), + } + const loader = await I.createPlugin({ doubao, newId: ids }).auth.loader(async () => ({})) + expect((await loader.fetch(req({ model: "doubao-work/kimi-k3", messages: [{ role: "user", content: "hi" }] }))).status).toBe(200) + const r = await loader.fetch(req({ model: "gpt-9", messages: [] })) + expect(r.status).toBe(404) + expect((await r.json()).error.message).toBe("没有模型 gpt-9。豆包的模型菜单里有:auto、kimi-k3。") + expect(asked).toEqual(["kimi-k3", "gpt-9"]) + // a model the list has never asks the menu + expect((await loader.fetch(req({ model: "auto", messages: [{ role: "user", content: "hi" }] }))).status).toBe(200) + expect(asked).toEqual(["kimi-k3", "gpt-9"]) +}) diff --git a/packages/doubao-work/doubao.mjs b/packages/doubao-work/doubao.mjs index 29a7e95..2a39575 100644 --- a/packages/doubao-work/doubao.mjs +++ b/packages/doubao-work/doubao.mjs @@ -282,6 +282,7 @@ export class Doubao { } this.app = app Object.assign(this, { openPage, poll, idle, total, fresh }) + this.missAt = 0 this.cacheFile = this.app?.dataDir ? path.join(this.app.dataDir, "models.json") : "" this.table = null this.tableAt = 0 @@ -341,6 +342,22 @@ export class Doubao { return table } + // known says whether Doubao offers model. A model the list read last + // doesn't have sends the plugin back to the menu at once (Doubao adds + // models between reads, #1048), at most once a minute, so a client + // asking for a name Doubao never had doesn't open the menu each time. + // The read waits its turn behind a running ask: both press the same + // controls. + async known(model, { signal } = {}) { + if (this.modelList()[model]) return true + if (!this.app?.start || Date.now() - this.missAt < 60000) return false + this.missAt = Date.now() + const run = this.queue.then(() => this.discover({ signal })) + this.queue = run.catch(() => {}) + try { await run } catch { return false } + return !!this.modelList()[model] + } + async connect(signal) { if (this.page && !this.page.closed) return this.page const port = await this.app.start({ signal }) diff --git a/packages/doubao-work/index.mjs b/packages/doubao-work/index.mjs index 2736c9c..3c9307d 100644 --- a/packages/doubao-work/index.mjs +++ b/packages/doubao-work/index.mjs @@ -114,7 +114,8 @@ export function createPlugin({ doubao = new Doubao(), newId, pick = choose } = { return json(400, "请求不是 JSON。", "invalid_request_error") } const model = String(body.model ?? "auto").replace(/^doubao-work\//, "") - if (!modelTable(doubao)[model]) return json(404, `没有模型 ${model}。`, "model_not_found") + const known = modelTable(doubao)[model] || (typeof doubao?.known === "function" && (await doubao.known(model, { signal }))) + if (!known) return json(404, `没有模型 ${model}。豆包的模型菜单里有:${Object.keys(modelTable(doubao)).join("、")}。`, "model_not_found") const prompt = flatten(body) const id = "chatcmpl-" + Math.random().toString(36).slice(2) const promptTokens = (r) => usageOf(r?.usage) || Math.ceil(prompt.length / 3) diff --git a/packages/doubao-work/package.json b/packages/doubao-work/package.json index dc1f8d6..9212e40 100644 --- a/packages/doubao-work/package.json +++ b/packages/doubao-work/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-doubao-work-auth", - "version": "0.1.5", + "version": "0.1.6", "type": "module", "main": "./opencode.mjs", "files": [