diff --git a/packages/doubao-work/README.md b/packages/doubao-work/README.md index e84f88b..66b7cfd 100644 --- a/packages/doubao-work/README.md +++ b/packages/doubao-work/README.md @@ -52,6 +52,10 @@ cached list doesn't have (at most once a minute); it is kept in `models.json` un macOS, `%LOCALAPPDATA%\magpie-doubao-work` on Windows) and read before anything else. The three models the plugin was written with stay as the fallback for when the menu cannot be read. +A list never read (an account that signed in before the plugin read menus) is +read when magpie lists the models, once a minute at most, and only from an app +the plugin has started before. Rows whose names differ only in Chinese get +distinct ids: Kimi K3.1 Pro 高速 is `kimi-k3.1-pro-fast`. Third-party models you added to 豆包工作 yourself (your own API key) are not served here: the plugin lists only what the app's model menu offers from diff --git a/packages/doubao-work/doubao-work.test.mjs b/packages/doubao-work/doubao-work.test.mjs index 5bea5f4..0223a05 100644 --- a/packages/doubao-work/doubao-work.test.mjs +++ b/packages/doubao-work/doubao-work.test.mjs @@ -282,6 +282,12 @@ test("idFor names a model from what its menu row says", () => { expect(idFor("纯中文模型").startsWith("model-")).toBe(true) }) +test("Kimi K3.1 Pro and Kimi K3.1 Pro 高速 are two models, and an unspellable suffix never merges two rows", () => { + expect(idFor("Kimi K3.1 Pro 高速")).toBe("kimi-k3.1-pro-fast") + const t = tableFrom([{ label: "Kimi K3.1 Pro" }, { label: "Kimi K3.1 Pro 高速" }, { label: "星火 4.0 快" }, { label: "星火 4.0 慢" }]) + expect(Object.values(t).map((m) => m.label)).toEqual(["Kimi K3.1 Pro", "Kimi K3.1 Pro 高速", "星火 4.0 快", "星火 4.0 慢"]) +}) + test("tableFrom keeps every model row, and falls back when nothing was read", () => { const t = tableFrom([{ label: "自动" }, { label: "GLM 5.3" }, { label: "GLM 5.3" }, { label: "" }]) expect(Object.keys(t)).toEqual(["auto", "glm-5.3"]) @@ -656,3 +662,37 @@ test("the plugin asks Doubao's menu again for a model its list lacks, and names expect((await loader.fetch(req({ model: "auto", messages: [{ role: "user", content: "hi" }] }))).status).toBe(200) expect(asked).toEqual(["kimi-k3", "gpt-9"]) }) + +test("a list never read from the menu is read when the plugin lists models, once a minute at most", async () => { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), "doubao-models-")) + const labels = ["自动", "豆包 2.1 Lite", "豆包 2.1 Pro", "DeepSeek V4.1 Flash", "Kimi K3.1 Pro", "Kimi K3.1 Pro 高速", "Kimi K3", "GLM 5.3", "GLM 5.3 Flash", "DeepSeek V4 Pro"] + const page = fakePage([], { steps: { menu: { rows: labels.map((label) => ({ label })) } } }) + const d = new Doubao({ app: { start: async () => 1, dataDir: dir }, openPage: async () => page, poll: 1 }) + const p = I.createPlugin({ doubao: d }) + const listed = await p.provider.models({}, {}) + expect(Object.values(listed).map((m) => m.name)).toEqual(labels) + await p.provider.models({}, {}) + expect(page.calls.filter((c) => c === MENU).length).toBe(1) + // a menu that fails is not asked again within the minute + const bad = fakePage([], { steps: { menu: { error: "no model menu" } } }) + const dir2 = fs.mkdtempSync(path.join(os.tmpdir(), "doubao-models-")) + const d2 = new Doubao({ app: { start: async () => 1, dataDir: dir2 }, openPage: async () => bad, poll: 1 }) + await d2.ensureList(); await d2.ensureList() + expect(bad.calls.filter((c) => c === MENU).length).toBe(1) + expect(Object.keys(d2.modelList())).toEqual(Object.keys(MODELS)) + // an app never started by the plugin (no data folder) is not opened + const d3 = new Doubao({ app: { start: async () => { throw new Error("opened") }, dataDir: path.join(dir, "none") }, openPage: async () => page }) + await d3.ensureList() + fs.rmSync(dir, { recursive: true, force: true }); fs.rmSync(dir2, { recursive: true, force: true }) +}) + +test("picking a model from the full list presses that row in the app", async () => { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), "doubao-models-")) + fs.writeFileSync(path.join(dir, "models.json"), JSON.stringify({ at: Date.now(), models: tableFrom([{ label: "自动" }, { label: "Kimi K3.1 Pro 高速" }, { label: "DeepSeek V4 Pro" }]) })) + const page = fakePage([{ sends: 1, reply: true, text: "ok", reasoning: "", finished: true, reason: "succeed" }]) + const d = new Doubao({ app: { start: async () => 1, dataDir: dir }, openPage: async () => page, poll: 1 }) + expect(Object.keys(d.modelList())).toEqual(["auto", "kimi-k3.1-pro-fast", "deepseek-v4-pro"]) + await d.ask("x", { model: "kimi-k3.1-pro-fast" }) + expect(page.calls.some((c) => c.includes('"Kimi K3.1 Pro 高速"'))).toBe(true) + fs.rmSync(dir, { recursive: true, force: true }) +}) diff --git a/packages/doubao-work/doubao.mjs b/packages/doubao-work/doubao.mjs index b8b0d66..eec4ce5 100644 --- a/packages/doubao-work/doubao.mjs +++ b/packages/doubao-work/doubao.mjs @@ -40,6 +40,7 @@ export function idFor(label) { if (!text || text === "自动") return "auto" let id = text .replace(/豆包/g, "doubao ") + .replace(/高速/g, " fast ") .toLowerCase() .replace(/[^a-z0-9.]+/g, "-") .replace(/-{2,}/g, "-") @@ -61,8 +62,11 @@ export function tableFrom(rows) { for (const r of rows ?? []) { const label = String(r?.label ?? "").trim() if (!label) continue - const id = idFor(label) - if (out[id]) continue + let id = idFor(label) + if (out[id]?.label === label) continue + // two rows whose names differ only in what idFor can't spell (a + // Chinese suffix) must not become one model + for (let n = 2; out[id]; n++) id = idFor(label) + "-" + n out[id] = { label, name: label } } if (!Object.keys(out).length) return { ...MODELS } @@ -392,6 +396,7 @@ export class Doubao { this.app = app Object.assign(this, { openPage, poll, idle, total, fresh }) this.missAt = 0 + this.readAt = 0 this.cacheFile = this.app?.dataDir ? path.join(this.app.dataDir, "models.json") : "" this.table = null this.tableAt = 0 @@ -451,6 +456,22 @@ export class Doubao { return table } + // ensureList reads the menu once when it has never been read, for an + // account that signed in before the plugin read menus (or whose sign-in + // read failed): without it the list stays at the three built-in models + // until the first request. Only an app this plugin has started before + // (its data folder exists) is opened for it; a failed read is tried + // again after a minute. It queues behind a running ask. + async ensureList({ signal } = {}) { + this.modelList() + if (this.tableAt || !this.app?.start || !this.cacheFile) return + if (!fs.existsSync(path.dirname(this.cacheFile)) || Date.now() - this.readAt < 60000) return + this.readAt = Date.now() + const run = this.queue.then(() => this.tableAt || this.discover({ signal })) + this.queue = run.catch(() => {}) + try { await run } catch {} + } + // 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 diff --git a/packages/doubao-work/index.mjs b/packages/doubao-work/index.mjs index 3c9307d..53749a4 100644 --- a/packages/doubao-work/index.mjs +++ b/packages/doubao-work/index.mjs @@ -61,6 +61,9 @@ export function createPlugin({ doubao = new Doubao(), newId, pick = choose } = { provider: { id: ID, async models() { + // a list never read from the menu is read now, so an account that + // signed in before the plugin read menus isn't left with three + if (typeof doubao?.ensureList === "function") await doubao.ensureList({ signal: AbortSignal.timeout(25000) }) return models(modelTable(doubao)) }, }, diff --git a/packages/doubao-work/package.json b/packages/doubao-work/package.json index af55138..b0e0428 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.7", + "version": "0.1.8", "type": "module", "main": "./opencode.mjs", "files": [