diff --git a/internal/gui/assets/app.css b/internal/gui/assets/app.css index fb190fecd..cc51cfc5f 100644 --- a/internal/gui/assets/app.css +++ b/internal/gui/assets/app.css @@ -3355,10 +3355,15 @@ body.window .ag-sk-icon { width: 22px; height: 22px; border-radius: 6px; } .tile .nm .badge { flex: none; } .tile .have { flex: none; width: 6px; height: 6px; border-radius: 3px; background: var(--green); } .tile .cnt { flex: none; font-size: 11px; color: var(--muted); font-variant-numeric: tabular-nums; } -.sheet .custom-foot { display: flex; align-items: center; gap: 8px; margin-top: 14px; padding: 10px 2px 0; border-top: 1px solid var(--line-2); } -.sheet .custom-foot .custom { display: inline-flex; align-items: center; gap: 7px; height: 30px; padding: 0 10px 0 6px; border-radius: 8px; color: var(--accent); font-size: 13px; } -.sheet .custom-foot .custom:hover, .sheet .custom-foot .custom.on { background: var(--accent-soft); } -.sheet .custom-foot .hint { font-size: 12px; color: var(--muted); } +.sheet .row-head .custom { display: inline-flex; align-items: center; gap: 4px; color: var(--accent); } +.sheet .row-head .custom:hover, .sheet .row-head .custom.on { color: var(--accent); background: var(--accent-soft); } +/* narrow: the search and Custom provider on a line of their own */ +@media (max-width: 760px) { + .sheet .row-head { flex-wrap: wrap; height: auto; row-gap: 8px; } + .sheet .row-head::after { content: ""; order: 1; flex-basis: 100%; } + .sheet .row-head .find { order: 2; flex: 1; width: auto; min-width: 0; } + .sheet .row-head .custom { order: 2; } +} /* the provider editor, in a dialog */ /* the label column is as wide as its longest label, so that label starts at the padding and the fields end the same way on the right (the diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js index bcc810cc1..9f328fb42 100644 --- a/internal/gui/assets/app.js +++ b/internal/gui/assets/app.js @@ -7120,7 +7120,7 @@ function shinePartners(k, grid) { seen.observe(k); } -// The add sheet: presets first (a key is all they need), custom last. +// The add sheet: presets, and a custom provider beside its search (jackutan on X). let presetQuery = "", addReturnPick = null; function renderAdd() { const sheet = $("#addSheet"); @@ -7155,7 +7155,12 @@ function renderAdd() { if (q.value) { q.value = presetQuery = ""; drawTiles(); } else if (providers.providers.length) closeAddSheet(); }; q.oninput = () => { presetQuery = q.value; drawTiles(); }; - head.append(q); + const c = el("button", "text custom" + (editing?.custom ? " on" : "")); + c.append(svg(PLUS, 11, 1.8), t("Custom provider")); + c.dataset.pick = "custom"; + c.title = t("any OpenAI or Anthropic compatible URL"); + c.onclick = () => { editing = { custom: true }; draft = null; renderProviders(); }; + head.append(q, c); const imp = el("button", "text", t("Import…")); imp.title = t("Bring over providers set up in other apps"); imp.onclick = openImportApps; @@ -7269,20 +7274,11 @@ function renderAdd() { const none = el("div", "none"); none.append(t("Nothing called “{q}”. ", { q: presetQuery.trim() })); const b = el("button", "link", t("Add it as a custom provider")); - b.onclick = () => { editing = { custom: true }; draft = null; renderProviders(); }; + b.onclick = c.onclick; const pl = el("button", "link", t("look for a plugin")); pl.onclick = () => openPlugins(presetQuery.trim()); none.append(b, t(", or "), pl); tiles.append(none); - } else if (!f) { - // a vendor not listed: one line under them all - const foot = el("div", "custom-foot"); - const c = el("button", "custom" + (editing?.custom ? " on" : "")); - c.append(svg(PLUS, 13, 1.8), el("span", "", t("Custom provider"))); - c.dataset.pick = "custom"; - c.onclick = () => { editing = { custom: true }; draft = null; renderProviders(); }; - foot.append(c, el("span", "hint", t("any OpenAI or Anthropic compatible URL"))); - tiles.append(foot); } }; drawTiles(); diff --git a/internal/gui/tests/add-sheet-custom.test.cjs b/internal/gui/tests/add-sheet-custom.test.cjs new file mode 100644 index 000000000..ea810c18e --- /dev/null +++ b/internal/gui/tests/add-sheet-custom.test.cjs @@ -0,0 +1,114 @@ +// Run with Node's test runner and Playwright on the module path; see README.md. +// The add sheet's custom provider is in sight as the sheet opens (jackutan on +// X: hard to find): a button beside the search, above every preset, kept +// while searching, where it was one line under every section and gone while +// searching. A search that finds nothing still offers "Add it as a custom +// provider". Every language, wide and narrow; the API is faked. +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(process.env.MAGPIE_ASSETS || path.join(__dirname, "../assets")); + +const preset = (id, name, kind) => ({ id, name, icon: "openai", kind, chat: `https://api.${id}.example.com/v1`, added: false }); +// enough presets that the sections run past a short window +const presets = []; +for (const kind of ["vendor", "relay", "local"]) for (let i = 0; i < 18; i++) presets.push(preset(kind + i, kind[0].toUpperCase() + kind.slice(1) + " " + i, kind)); +const providers = [{ id: "openrouter", name: "OpenRouter", icon: "openai", preset: "relay0", models: [], agents: [], key: { set: true, masked: "sk-…ab12" } }]; + +function server(lang) { + return 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({ agents: [], profiles: [], settings: { lang, theme: "light" } }); + if (url.pathname === "/api/providers") return json({ providers, presets, excluded: [], gateway: { running: true, window: true } }); + if (url.pathname === "/api/groups") return json({ groups: [], pools: [] }); + 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)]; + try { await route.fulfill({ body: await fs.readFile(file), contentType }); } catch { await route.fulfill({ status: 404, body: "" }); } + }; +} + +const L = { + en: { custom: "Custom provider", hint: "any OpenAI or Anthropic compatible URL", none: "Add it as a custom provider" }, + zh: { custom: "自定义供应商", hint: "任意 OpenAI / Anthropic 兼容的 URL", none: "添加为自定义供应商" }, + "zh-TW": { custom: "自定義供應商", hint: "任意 OpenAI / Anthropic 相容的 URL", none: "新增為自定義供應商" }, + ja: { custom: "カスタムプロバイダ", hint: "OpenAI または Anthropic 互換の任意の URL", none: "カスタムプロバイダとして追加" }, + de: { custom: "Benutzerdefinierter Anbieter", hint: "jede OpenAI- oder Anthropic-kompatible URL", none: "Als benutzerdefinierten Anbieter hinzufügen" }, +}; + +for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) { + test(engine + ": the custom provider is in sight as the add sheet opens", async (t) => { + assert(["chromium", "webkit"].includes(engine), "BROWSER must be chromium or webkit"); + const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" })); + t.after(() => browser.close()); + for (const lang of Object.keys(L)) for (const width of [980, 761, 420]) { + await t.test(lang + " " + width, async () => { + const w = L[lang]; + const ctx = await browser.newContext({ viewport: { width, height: 560 }, reducedMotion: "reduce" }); + t.after(() => ctx.close()); + const page = await ctx.newPage(); + const errors = []; + page.on("pageerror", (e) => errors.push(e.message)); + await page.route("**/*", server(lang)); + await page.goto("http://magpie.test/?view=providers"); + await page.locator("#addProvider").click(); + const sheet = page.locator("#addSheet"); + await sheet.locator(".tile").first().waitFor(); + + // beside the search, above the first preset, in the window as it opens + const custom = sheet.locator(".row-head .custom"); + assert.equal(await custom.count(), 1); + assert.equal((await custom.textContent()).trim(), w.custom); + assert.equal(await custom.getAttribute("title"), w.hint); + const box = await page.evaluate(() => { + const r = (e) => e.getBoundingClientRect(); + const c = document.querySelector("#addSheet .row-head .custom"), head = c.parentElement, find = head.querySelector(".find"); + const tile = document.querySelector("#addSheet .tile"); + const els = [...head.children].map(r); + return { + c: r(c), find: r(find), tile: r(tile), head: r(head), sheet: r(document.querySelector("#addSheet")), vw: innerWidth, vh: innerHeight, + overlap: els.some((a, i) => els.some((b, j) => i < j && a.width && b.width && a.left < b.right - 0.5 && b.left < a.right - 0.5 && a.top < b.bottom - 0.5 && b.top < a.bottom - 0.5)), + out: [...head.children].filter((e) => r(e).width && (r(e).left < r(head).left - 1 || r(e).right > Math.min(r(head).right, innerWidth) + 1)).map((e) => e.textContent || e.className), + wraps: c.scrollWidth > c.clientWidth + 1, lines: Math.round(r(c).height), + }; + }); + assert(box.c.top >= 0 && box.c.bottom <= box.vh, "in the window: " + JSON.stringify(box.c)); + assert(box.c.left >= box.sheet.left - 1 && box.c.right <= Math.min(box.sheet.right, box.vw) + 1, "inside the sheet: " + JSON.stringify([box.c, box.sheet])); + assert(box.c.bottom <= box.tile.top, "above the presets"); + assert(Math.abs((box.c.top + box.c.bottom) / 2 - (box.find.top + box.find.bottom) / 2) <= 2, "on the search's line"); + assert(!box.overlap, "the head's controls apart"); + assert(box.out.length === 0, "the head's controls inside the sheet: " + box.out); + assert(!box.wraps && box.lines <= 26, "one line, unclipped: " + box.lines); + if (process.env.ARTIFACT_DIR) await page.screenshot({ path: path.join(process.env.ARTIFACT_DIR, `${engine}-${lang}-${width}.png`) }); + // no second entry at the foot + assert.equal(await sheet.locator(".custom-foot").count(), 0); + + // kept while a search finds something + await sheet.locator(".find").fill(lang === "en" ? "relay 1" : "Relay 1"); + await sheet.locator(".tile").first().waitFor(); + assert(await custom.isVisible()); + // and one that finds nothing still offers it in its line + await sheet.locator(".find").fill("zzqq"); + const none = sheet.locator(".none button", { hasText: w.none }); + await none.waitFor(); + assert(await custom.isVisible()); + await none.click(); + await page.locator(".editor.new").waitFor(); + await page.keyboard.press("Escape"); + await page.locator("#modal").waitFor({ state: "hidden" }); + + // the head's button opens the custom editor + await sheet.locator(".find").fill(""); + await custom.click(); + await page.locator(".editor.new").waitFor(); + assert.deepEqual(errors, []); + }); + } + }); +} diff --git a/internal/gui/tests/add-sheet.test.cjs b/internal/gui/tests/add-sheet.test.cjs index 0c540aaf5..157327f62 100644 --- a/internal/gui/tests/add-sheet.test.cjs +++ b/internal/gui/tests/add-sheet.test.cjs @@ -8,8 +8,8 @@ // subscription's account count beside it when more than one. A vendor's // global and China presets are one row with nothing added to it, the // regions' hosts in its title, its editor switching between them with -// the key kept. A custom provider is one line -// under them all, gone while searching. English and Chinese; no backend, +// the key kept. A custom provider is a button +// beside the search, kept while searching. English and Chinese; no backend, // the API is faked here. const assert = require("node:assert/strict"); const fs = require("node:fs/promises"); @@ -137,13 +137,13 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", assert.match(kt, new RegExp(w.china + " api\\.moonshot\\.cn")); assert.match(kt, /api\.moonshot\.example\.com/); - // the custom provider: one line at the foot, gone while searching - const foot = sheet.locator(".custom-foot"); - assert.equal(await foot.locator(".custom").textContent(), w.custom); - assert.equal(await foot.locator(".hint").textContent(), w.customHint); + // the custom provider: beside the search, kept while searching + const custom = sheet.locator(".row-head .custom"); + assert.equal(await custom.textContent(), w.custom); + assert.equal(await custom.getAttribute("title"), w.customHint); await sheet.locator(".find").fill("deep"); assert.deepEqual(await rows.locator(".n").allTextContents(), ["DeepSeek"]); - assert.equal(await foot.count(), 0); + assert.equal(await custom.count(), 1); await sheet.locator(".find").fill("china"); assert.deepEqual(await rows.locator(".n").allTextContents(), ["Kimi"]); await sheet.locator(".find").fill(""); diff --git a/internal/gui/tests/model-id-space.test.cjs b/internal/gui/tests/model-id-space.test.cjs index b1d9cfa0e..fa4122b45 100644 --- a/internal/gui/tests/model-id-space.test.cjs +++ b/internal/gui/tests/model-id-space.test.cjs @@ -85,7 +85,7 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", const posts = []; const { page, errors } = await open(t, posts); await page.locator("#addProvider").click(); - await page.locator("#addSheet .custom-foot .custom").click(); + await page.locator("#addSheet .row-head .custom").click(); const ed = page.locator(".editor.new"); await ed.locator(".add-models").waitFor(); const L = await page.evaluate(() => ({ name: t("e.g. My Relay"), add: t("Add") })); diff --git a/internal/gui/tests/provider-add-gemini.test.cjs b/internal/gui/tests/provider-add-gemini.test.cjs index a86fbeae0..f4cb46099 100644 --- a/internal/gui/tests/provider-add-gemini.test.cjs +++ b/internal/gui/tests/provider-add-gemini.test.cjs @@ -57,7 +57,7 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", await page.goto("http://magpie.test/?view=providers"); const S = L[lang]; await page.locator("#addProvider").click(); - await page.locator("#addSheet .custom-foot .custom").click(); + await page.locator("#addSheet .row-head .custom").click(); const ed = page.locator(".editor.new"); await ed.locator(".base-url").waitFor(); await ed.locator(`input[placeholder="${S.name}"]`).fill("Gem Relay"); diff --git a/internal/gui/tests/provider-add-models.test.cjs b/internal/gui/tests/provider-add-models.test.cjs index 5c971a348..9fb000704 100644 --- a/internal/gui/tests/provider-add-models.test.cjs +++ b/internal/gui/tests/provider-add-models.test.cjs @@ -66,7 +66,7 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", hint: zh ? "可选:用「获取模型」从供应商列表中勾选,或直接填写模型 ID;不选则保存后自动获取模型列表。" : "Optional: Fetch models to pick from the vendor's list, or type ids; none picked, magpie asks for the list after saving.", }; await page.locator("#addProvider").click(); - await page.locator("#addSheet .custom-foot .custom").click(); + await page.locator("#addSheet .row-head .custom").click(); const ed = page.locator(".editor.new"); await ed.locator(".add-models").waitFor(); const field = ed.locator(".add-models .detect-row input"); diff --git a/internal/gui/tests/provider-curl-paste.test.cjs b/internal/gui/tests/provider-curl-paste.test.cjs index b6d426616..4f83ad7ae 100644 --- a/internal/gui/tests/provider-curl-paste.test.cjs +++ b/internal/gui/tests/provider-curl-paste.test.cjs @@ -135,7 +135,7 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", await page.goto("http://magpie.test/?view=providers"); await page.locator("#addProvider").click(); - await page.locator("#addSheet .custom-foot .custom").click(); + await page.locator("#addSheet .row-head .custom").click(); await baseURL.waitFor(); await ed.locator("input[placeholder]").first().fill("Kimi"); diff --git a/internal/gui/tests/provider-detect-draft.test.cjs b/internal/gui/tests/provider-detect-draft.test.cjs index 17939e548..3d0c11c4f 100644 --- a/internal/gui/tests/provider-detect-draft.test.cjs +++ b/internal/gui/tests/provider-detect-draft.test.cjs @@ -83,7 +83,7 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", await page.goto("http://magpie.test/?view=providers"); const T = (s) => tr(lang, s); await page.locator("#addProvider").click(); - await page.locator("#addSheet .custom-foot .custom").click(); + await page.locator("#addSheet .row-head .custom").click(); const ed = page.locator(".editor.new"); await ed.locator(".detect").waitFor(); const detect = ed.getByRole("button", { name: T("Detect APIs"), exact: true }); diff --git a/internal/gui/tests/provider-retrieval-only.test.cjs b/internal/gui/tests/provider-retrieval-only.test.cjs index 1e07c0dfb..bd26653dc 100644 --- a/internal/gui/tests/provider-retrieval-only.test.cjs +++ b/internal/gui/tests/provider-retrieval-only.test.cjs @@ -56,7 +56,7 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", // the add form's Fetch models await page.locator("#addProvider").click(); - await page.locator("#addSheet .custom-foot .custom").click(); + await page.locator("#addSheet .row-head .custom").click(); const ed = page.locator(".editor.new"); await ed.locator(".add-models").waitFor(); await ed.locator('input[type="url"][placeholder="https://…/v1"]').first().fill("http://embed.test/v1"); diff --git a/internal/gui/tests/provider-url-each-api.test.cjs b/internal/gui/tests/provider-url-each-api.test.cjs index a7be42417..ded7b8d08 100644 --- a/internal/gui/tests/provider-url-each-api.test.cjs +++ b/internal/gui/tests/provider-url-each-api.test.cjs @@ -64,7 +64,7 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", const addNew = async (name) => { await page.goto("http://magpie.test/?view=providers"); await page.locator("#addProvider").click(); - await page.locator("#addSheet .custom-foot .custom").click(); + await page.locator("#addSheet .row-head .custom").click(); await baseURL.waitFor(); await ed.locator('input[placeholder]').first().fill(name); }; diff --git a/internal/gui/tests/system-one.test.cjs b/internal/gui/tests/system-one.test.cjs index f2baf880c..d21bf2b9f 100644 --- a/internal/gui/tests/system-one.test.cjs +++ b/internal/gui/tests/system-one.test.cjs @@ -120,7 +120,7 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", // a custom System One: probed at its address, saved as decide await page.goto("http://magpie.test/?view=providers"); await page.locator("#addProvider").click(); - await sheet.locator(".custom-foot .custom").click(); + await sheet.locator(".row-head .custom").click(); await ed.waitFor(); await ed.locator("input").first().fill("My Decider"); await ed.locator(".segs .opt", { hasText: "System One" }).click();