From 1a8048592ee0db66e0a3e46516e0cfd7e34ea175 Mon Sep 17 00:00:00 2001 From: yetone Date: Wed, 28 Oct 2026 13:00:08 +0800 Subject: [PATCH] providers: a search for a model keeps the providers serving it and names the model under each (frankdeng, Neo) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit frankdeng (Discord #general) wanted to type a model (opus 5.5) on the Providers page and see who serves it; Neo had two providers serving opus5.5 and couldn't tell which. The search already matched model ids, but as one blob of the provider's text: "opus 5.5" kept a provider with claude-opus-4-1 and gpt-5.5, and one with Opus 5.5 switched off, and no row said which model matched. Now the words a row doesn't say itself must all be one model's that is on, separators aside ("opus 5.5", "opus5.5", "Opus-5.5" find claude-opus-5-5 and opus-5.5), and the row gets a muted line "Serves " (the existing .sub style, clipped with "…"). A search on the provider's own name, host, account or key names no model. Changed: a provider whose only match is a model it has switched off no longer shows. "Serves {models}" is in zh, zh-TW, ja and de. Size budgets not raised: renderProviders no longer sets the lists' visibility and the fold's state, which findProviders (called at its end) set again; findProviders' two branches for that are one. Core scripts 1,526,548 of 1,526,556; app.js 1,309,717 of its budget. Run on mote, at this head (rebased on 8c5c83c9f): - go build: ok. go test -tags nogui -run TestCoreScriptBudget ./internal/gui: ok. Before the rebase, go test -tags nogui ./internal/gui (whole package): ok. - node --test provider-find-model, provider-find, i18n-keys: 28 pass, 0 fail (Chromium and WebKit, en zh zh-TW ja de, 380px wide). Broken by hand: with the model match taken out (hits always empty) the new test fails on "opus 5.5" (actual [], expected anthropic, relay-a, relay-off with their lines); against origin/main's app.js it fails with relay-b and relay-c shown and no "Serves" line. Not run: full GUI suite, full Go suite (shipped at once per owner). A run of the 231 GUI files that reach the Providers list is still going on mote; reds from it get their own issues. Not tried: the real app with real providers. --- internal/gui/assets/app.js | 43 ++---- internal/gui/assets/i18n.js | 4 + .../gui/tests/provider-find-model.test.cjs | 145 ++++++++++++++++++ 3 files changed, 166 insertions(+), 26 deletions(-) create mode 100644 internal/gui/tests/provider-find-model.test.cjs diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js index b36cdeac2..92045bf0e 100644 --- a/internal/gui/assets/app.js +++ b/internal/gui/assets/app.js @@ -6109,14 +6109,7 @@ function renderProviders() { const onList = $("#providers"), offList = $("#offProviders"); onList.replaceChildren(); offList.replaceChildren(); - const offs = providers.providers.filter((p) => p.off).length; - onList.hidden = offs === providers.providers.length; - $("#offHead").hidden = !offs; - offList.hidden = !offs || offFolded; - const fold = $("#foldOff"); - fold.setAttribute("aria-expanded", String(!offFolded)); - fold.title = t(offFolded ? "Show the providers turned off" : "Fold the providers turned off away"); - $("#offCount").textContent = offs ? String(offs) : ""; + $("#foldOff").title = t(offFolded ? "Show the providers turned off" : "Fold the providers turned off away"); let dialog = null; // the editor, if one is open for (const p of providers.providers) { const list = p.off ? offList : onList; @@ -6283,18 +6276,20 @@ function openProviderMenu(anchor, p) { // Providers' search (#1614, ququdu: "供应商很多,然后经常需要翻找"): the // words typed keep the rows that say them all, on and off alike, matched on // the row's own text and on what it doesn't show: its id, preset, addresses, -// every model's id and name, each account's user and plan, and the keys' -// names. The ones turned off show unfolded while searching. Escape or -// clearing the box brings back every row, the fold as it was. +// each account's user and plan, and the keys' names. The words it doesn't +// say are one model's it serves, separators aside ("opus 5.5" finds +// claude-opus-5-5), named under the row (frankdeng, Neo). The ones turned +// off show unfolded while searching. Escape or clearing the box brings back +// every row, the fold as it was. let pvQuery = ""; function providerText(p, row) { const a = p.account; return [row.textContent, p.id, p.name, p.preset, p.host, p.chat, p.responses, p.anthropic, p.gemini, - ...(p.models || []).flatMap((m) => [m.id, m.name]), a?.user, a?.agentName, a?.plan, ...(a?.logins || []).flatMap((l) => [l.user, l.plan]), ...Object.values(p.accountNames || {}), ...(p.keyList || []).map((k) => k.name), ].filter(Boolean).join(" ").toLowerCase(); } +const pvSq = (s) => (s || "").toLowerCase().replace(/[\s._-]+/g, ""); function findProviders() { const view = $("#view-providers"), q = $("#pvQ"), none = $("#pvNone"); q.setAttribute("aria-label", t("Find a provider")); @@ -6304,26 +6299,22 @@ function findProviders() { let on = 0, off = 0; for (const row of view.querySelectorAll("#providers > .row.provider, #offProviders > .row.provider")) { const p = byId.get(row.dataset.id); - const hit = !p || words.every((w) => providerText(p, row).includes(w)); + row.querySelector(".pv-hits")?.remove(); + const own = p ? providerText(p, row) : "", rest = words.filter((w) => !own.includes(w)); + const hits = rest.length ? (p?.models || []).filter((m) => m.on && rest.every((w) => (pvSq(m.id) + "|" + pvSq(m.name)).includes(pvSq(w)))).map((m) => m.id) : []; + const hit = !p || !rest.length || hits.length; + if (hits.length) row.querySelector(".who").append(el("div", "sub pv-hits", t("Serves {models}", { models: hits.join(", ") }))); row.hidden = !hit; if (hit) p?.off ? off++ : on++; } - const offs = providers.providers.filter((p) => p.off).length; // while searching, the ones off that match show, whatever the fold says - const fold = $("#foldOff"); + const fold = $("#foldOff"), offs = pvQuery ? off : providers.providers.filter((p) => p.off).length; fold.disabled = !!pvQuery; fold.setAttribute("aria-expanded", String(!!pvQuery || !offFolded)); - if (pvQuery) { - $("#providers").hidden = !on; - $("#offHead").hidden = !off; - $("#offProviders").hidden = !off; - $("#offCount").textContent = off ? String(off) : ""; - } else { - $("#providers").hidden = offs === providers.providers.length; - $("#offHead").hidden = !offs; - $("#offProviders").hidden = !offs || offFolded; - $("#offCount").textContent = offs ? String(offs) : ""; - } + $("#providers").hidden = pvQuery ? !on : offs === providers.providers.length; + $("#offHead").hidden = !offs; + $("#offProviders").hidden = !offs || !pvQuery && offFolded; + $("#offCount").textContent = offs || ""; none.hidden = !pvQuery || on + off > 0; none.textContent = none.hidden ? "" : t("No provider matches “{q}”", { q: q.value.trim() }); } diff --git a/internal/gui/assets/i18n.js b/internal/gui/assets/i18n.js index 77f28d93d..549a27383 100644 --- a/internal/gui/assets/i18n.js +++ b/internal/gui/assets/i18n.js @@ -2789,6 +2789,7 @@ const I18N = { "No setting matches “{q}”": "没有和“{q}”匹配的设置", "Find a provider": "查找供应商", "No provider matches “{q}”": "没有和“{q}”匹配的供应商", + "Serves {models}": "提供 {models}", "Move to top": "移到顶部", "Move to bottom": "移到底部", "right-click to move it to the top or the bottom": "右键可移到顶部或底部", @@ -6811,6 +6812,7 @@ const I18N = { "No setting matches “{q}”": "沒有和「{q}」匹配的設定", "Find a provider": "尋找供應商", "No provider matches “{q}”": "沒有和「{q}」匹配的供應商", + "Serves {models}": "提供 {models}", "Move to top": "移到頂部", "Move to bottom": "移到底部", "right-click to move it to the top or the bottom": "按右鍵可移到頂部或底部", @@ -10837,6 +10839,7 @@ const I18N = { "No setting matches “{q}”": "「{q}」に一致する設定はありません", "Find a provider": "プロバイダを検索", "No provider matches “{q}”": "「{q}」に一致するプロバイダはありません", + "Serves {models}": "{models} を提供", "Move to top": "一番上へ移動", "Move to bottom": "一番下へ移動", "right-click to move it to the top or the bottom": "右クリックで一番上・一番下へ移動", @@ -14873,6 +14876,7 @@ const I18N = { "No setting matches “{q}”": "Keine Einstellung passt zu „{q}“", "Find a provider": "Anbieter suchen", "No provider matches “{q}”": "Kein Anbieter passt zu „{q}“", + "Serves {models}": "Bietet {models}", "Move to top": "Ganz nach oben", "Move to bottom": "Ganz nach unten", "right-click to move it to the top or the bottom": "Rechtsklick, um ihn ganz nach oben oder unten zu verschieben", diff --git a/internal/gui/tests/provider-find-model.test.cjs b/internal/gui/tests/provider-find-model.test.cjs new file mode 100644 index 000000000..549b4058e --- /dev/null +++ b/internal/gui/tests/provider-find-model.test.cjs @@ -0,0 +1,145 @@ +// Run with Node's test runner and Playwright on the module path; see README.md. +// frankdeng (Discord): type a model on the Providers page and see which +// providers serve it; Neo had two providers serving Opus 5.5 and couldn't +// tell which. The search box keeps the rows with a model on that has every +// word the row doesn't say itself, written with or without its separators +// ("opus 5.5", "opus5.5" find claude-opus-5-5 and opus-5.5), and names the +// models under the row. Words spread over two models don't count, nor a +// model turned off. A search on the provider's own name names no model. +// Every language, Chromium and WebKit, in a narrow window; no backend. +const assert = require("node:assert/strict"); +const fs = require("node:fs/promises"); +const path = require("node:path"); +const { test } = require("node:test"); +const { chromium, webkit } = require("playwright"); + +const assets = path.resolve(__dirname, "../assets"); +const prov = (id, name, models, extra = {}) => ({ + id, name, icon: "generic", host: id + ".example.com", chat: "https://" + id + ".example.com/v1", responses: "", anthropic: "", catalog: "", + models, agents: [], fallback: [], headers: {}, + key: { set: true, masked: "sk-…" + id }, keyList: [], balanceToken: { takes: false, set: false }, proxy: "", ...extra, +}); +const m = (id, name, on = true) => ({ id, name, on }); + +function fixture(lang) { + const providers = { + providers: [ + prov("anthropic", "Anthropic", [m("claude-opus-5-5", "Claude Opus 5.5"), m("claude-sonnet-5", "Claude Sonnet 5")]), + prov("relay-a", "Relay A", [m("opus-5.5", "opus-5.5"), m("opus-5.5-thinking", "Opus 5.5 Thinking"), m("gpt-5.5", "GPT-5.5")]), + // opus in one model, 5.5 in another: it serves no Opus 5.5 + prov("relay-b", "Relay B", [m("claude-opus-4-1", "Claude Opus 4.1"), m("gpt-5.5", "GPT-5.5")]), + // Opus 5.5 listed but turned off: not served + prov("relay-c", "Relay C", [m("claude-opus-5-5", "Claude Opus 5.5", false), m("glm-5", "GLM-5")]), + prov("relay-off", "Relay Off", [m("claude-opus-5-5", "Claude Opus 5.5")], { off: true }), + ], + presets: [], excluded: [], gateway: { running: true, window: true }, + }; + const state = { agents: [], profiles: [], settings: { lang, theme: "light" } }; + const route = async (route) => { + const url = new URL(route.request().url()); + const json = (data) => route.fulfill({ json: data }); + if (url.pathname === "/boot.js") return route.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${lang}",theme:"light",web:false};` }); + if (url.pathname === "/wails/runtime.js") return route.fulfill({ contentType: "text/javascript", body: "export const Window = {};" }); + if (url.pathname === "/api/state") return json(state); + if (url.pathname === "/api/providers") return json(providers); + if (url.pathname === "/api/groups") return json({ groups: [] }); + if (url.pathname === "/api/plugins") return json({ plugins: [] }); + if (url.pathname === "/api/usage/quotas") return json([]); + if (url.pathname.startsWith("/api/")) return json({}); + const file = path.join(assets, url.pathname === "/" ? "index.html" : url.pathname); + const contentType = { ".html": "text/html", ".js": "text/javascript", ".css": "text/css", ".svg": "image/svg+xml", ".png": "image/png" }[path.extname(file)]; + await route.fulfill({ body: await fs.readFile(file), contentType }); + }; + return { route }; +} + +// each row shown, on and off, with the models named under it +const shown = (page) => page.evaluate(() => [...document.querySelectorAll("#providers > .row.provider, #offProviders > .row.provider")] + .filter((r) => r.getClientRects().length) + .map((r) => [r.dataset.id, [...r.querySelectorAll(".pv-hits")].map((h) => h.textContent).join("|")])); +// the line names a model inside the row, clipped with "…" when long, and nothing runs off to the side +const overflow = (page) => page.evaluate(() => { + const out = [], de = document.documentElement, v = document.querySelector("#view-providers"); + if (de.scrollWidth > de.clientWidth) out.push("page " + de.scrollWidth + ">" + de.clientWidth); + if (v.scrollWidth > v.clientWidth) out.push("view " + v.scrollWidth + ">" + v.clientWidth); + for (const h of document.querySelectorAll(".pv-hits")) { + if (!h.getClientRects().length) continue; + const r = h.getBoundingClientRect(), row = h.closest(".row").getBoundingClientRect(); + if (r.left < row.left - 0.5 || r.right > row.right + 0.5) out.push("line " + h.textContent); + if (getComputedStyle(h).textOverflow !== "ellipsis") out.push("not clipped " + h.textContent); + } + return out; +}); + +const LANGS = ["en", "zh", "zh-TW", "ja", "de"]; +for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) { + test(engine + ": the Providers page finds the providers serving a model, and names it", { timeout: 180000 }, async (t) => { + assert(["chromium", "webkit"].includes(engine), "BROWSER must be chromium or webkit"); + const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" })); + const errors = []; + t.after(async () => { + await browser.close(); + assert.deepEqual(errors, []); + }); + for (const lang of LANGS) { + await t.test(lang, async () => { + const ctx = await browser.newContext({ viewport: { width: 380, height: 560 }, reducedMotion: "reduce" }); + const page = await ctx.newPage(), f = fixture(lang); + page.setDefaultTimeout(5000); + page.on("pageerror", (e) => errors.push(e.message)); + await page.route("**/*", f.route); + await page.goto("http://magpie.test/?view=providers"); + await page.locator("#providers > .row.provider").nth(3).waitFor(); + const tr = (s, vars) => page.evaluate(([s, vars]) => t(s, vars), [s, vars]); + const serves = (models) => tr("Serves {models}", { models }); + if (lang !== "en") assert.notEqual(await serves("x"), "Serves x", "no translation"); + const q = page.locator("#pvQ"); + const type = async (s) => { await q.fill(s); await page.waitForTimeout(60); }; + const y0 = await page.evaluate(() => [scrollX, scrollY, document.querySelector("#view-providers").scrollTop]); + + // "opus 5.5", as frankdeng typed it, and "opus5.5", "Opus-5.5", as + // Neo did: the ones serving it, each saying which model + const opus = [ + ["anthropic", await serves("claude-opus-5-5")], + ["relay-a", await serves("opus-5.5, opus-5.5-thinking")], + ["relay-off", await serves("claude-opus-5-5")], + ]; + for (const typed of ["opus 5.5", "opus5.5", "Opus-5.5", "claude opus 5 5"]) { + await type(typed); + assert.deepEqual(await shown(page), typed === "claude opus 5 5" ? [opus[0], opus[2]] : opus, typed); + } + assert.deepEqual(await overflow(page), []); + + // a model's own name; its provider's name and a model's together + await type("Claude Sonnet"); + assert.deepEqual(await shown(page), [["anthropic", await serves("claude-sonnet-5")]]); + await type("relay a thinking"); + assert.deepEqual(await shown(page), [["relay-a", await serves("opus-5.5-thinking")]]); + + // the provider's own name names no model + await type("relay"); + assert.deepEqual(await shown(page), [["relay-a", ""], ["relay-b", ""], ["relay-c", ""], ["relay-off", ""]]); + + // a word no model or provider has: nothing, and no line left behind + await type("opus 9"); + assert.deepEqual(await shown(page), []); + assert.equal(await page.locator(".pv-hits").count(), 0); + + // Escape: every row back, none naming a model + await type("gpt 5.5"); + assert.deepEqual(await shown(page), [["relay-a", await serves("gpt-5.5")], ["relay-b", await serves("gpt-5.5")]]); + await q.focus(); + await page.keyboard.press("Escape"); + assert.deepEqual(await shown(page), [["anthropic", ""], ["relay-a", ""], ["relay-b", ""], ["relay-c", ""]]); + assert.equal(await page.locator(".pv-hits").count(), 0); + assert.deepEqual(await page.evaluate(() => [scrollX, scrollY, document.querySelector("#view-providers").scrollTop]), y0); + if (process.env.ARTIFACT_DIR) { + await fs.mkdir(process.env.ARTIFACT_DIR, { recursive: true }); + await type("opus 5.5"); + await page.screenshot({ path: path.join(process.env.ARTIFACT_DIR, `${engine}-provider-find-model-${lang}.png`) }); + } + await ctx.close(); + }); + } + }); +}