diff --git a/docs/subsystems/gui-shell.md b/docs/subsystems/gui-shell.md index 593bad95a..9f6b6b933 100644 --- a/docs/subsystems/gui-shell.md +++ b/docs/subsystems/gui-shell.md @@ -394,6 +394,12 @@ Routing's New group buttons share `newGroup`, which asks before replacing a dirty group draft; Cancel retains the draft and Discard opens the new editor. `newGroupWith` applies the same guard when creating a group from a model and waits for `show` to accept navigation before creating its draft. +The Routing page's head also has Routing groups (`#rtGoGroups`), since the +groups sit below every request. Its click goes through `scrollOnPurpose`, +brings `.rt-gsec` to the top of the view and focuses it; the page's layout is +unchanged. `fitGo` gives it only the head's spare room: named, its arrow +alone, or left out, so nothing else in the head moves or wraps for it +(`routing-groups-jump.test.cjs`). The Plugins page updates the npm search-results section when a search answers, leaving suggested plugin cards in place so a click begun before the answer still diff --git a/internal/gui/assets/plugins/routing/client.js b/internal/gui/assets/plugins/routing/client.js index dc7f80ca3..2645e6962 100644 --- a/internal/gui/assets/plugins/routing/client.js +++ b/internal/gui/assets/plugins/routing/client.js @@ -21,6 +21,7 @@ magpie.plugin("routing", (ctx) => { title: "Routing", html: `
Routinglive, as the gateway decides +
@@ -5869,6 +5870,38 @@ function routingPage(part) { if (!groups) await loadGroups(); if (groups) newGroup(ev); }; + // the head's Routing groups: the groups sit below the stage, the story + // and every request, a long scroll down with many models (shihh on + // Discord). Its click brings them to the top of the view, and the focus + // with them, so Tab goes on from there; the page is laid out as before + gsec.tabIndex = -1; + const headGo = $("#rtGoGroups"); + if (headGo) headGo.onclick = (ev) => { + if (!window.scrollOnPurpose?.(ev)) return; + gsec.scrollIntoView({ block: "start", behavior: still() ? "auto" : "smooth" }); + gsec.focus({ preventScroll: true }); + }; + // fitGo: it takes only room the head has spare, so nothing else in the + // head moves or wraps for it: named where its name fits, its arrow alone + // where only that does, and left out of a head with no room (a phone's) + const goHead = headGo?.parentElement, goGrow = goHead?.querySelector(".grow"); + function fitGo() { + if (!goGrow || !goHead.offsetParent) return; + headGo.dataset.fit = "none"; + const room = goGrow.getBoundingClientRect().width, gap = parseFloat(getComputedStyle(goHead).columnGap) || 0; + for (const fit of ["full", "icon"]) { + headGo.dataset.fit = fit; + if (room >= headGo.getBoundingClientRect().width + 6 + gap) return; + } + headGo.dataset.fit = "none"; + } + let goFitting = 0; + const fitGoSoon = () => { if (!goFitting) goFitting = requestAnimationFrame(() => { goFitting = 0; fitGo(); }); }; + if (goHead) { + const ro = new ResizeObserver(fitGoSoon); + for (const n of [goHead, ...goHead.querySelectorAll(".label, .note")]) ro.observe(n); + new MutationObserver(fitGoSoon).observe(document.documentElement, { attributes: true, attributeFilter: ["lang"] }); + } const askedGroup = document.body.classList.contains("window") && params.get("newgroup"); if (askedGroup) loadGroups().then(() => window.newGroupWith(askedGroup, "")); else loadGroups(); diff --git a/internal/gui/assets/plugins/routing/i18n.js b/internal/gui/assets/plugins/routing/i18n.js index 78999c8af..6f68daca5 100644 --- a/internal/gui/assets/plugins/routing/i18n.js +++ b/internal/gui/assets/plugins/routing/i18n.js @@ -2,6 +2,7 @@ // kernel adds them to i18n.js's (magpie.strings). magpie.strings({ zh: { + "Go to the routing groups, below the requests": "跳到请求下方的路由组", "Filter, or type an agent's name…": "筛选,或输入 Agent 名称…", "the name its User-Agent begins with": "User-Agent 开头的名称", "heard from in the last 30 days": "近 30 天有请求", @@ -543,6 +544,7 @@ magpie.strings({ "{h} h {m} min": "{h} 小时 {m} 分钟", }, "zh-TW": { + "Go to the routing groups, below the requests": "跳到請求下方的路由組", "Filter, or type an agent's name…": "篩選,或輸入 Agent 名稱…", "the name its User-Agent begins with": "User-Agent 開頭的名稱", "heard from in the last 30 days": "近 30 天有請求", @@ -1084,6 +1086,7 @@ magpie.strings({ "{h} h {m} min": "{h} 小時 {m} 分鐘", }, ja: { + "Go to the routing groups, below the requests": "リクエストの下にあるルーティンググループへ移動", "Filter, or type an agent's name…": "絞り込み、またはエージェント名を入力…", "the name its User-Agent begins with": "User-Agent の先頭の名前", "heard from in the last 30 days": "過去 30 日にリクエストあり", @@ -1625,6 +1628,7 @@ magpie.strings({ "These routing groups will no longer be available to agents.": "これらのルーティンググループはエージェントから利用できなくなります。", }, de: { + "Go to the routing groups, below the requests": "Zu den Weiterleitungsgruppen unter den Anfragen springen", "Filter, or type an agent's name…": "Filtern oder Namen eines Agenten eingeben…", "the name its User-Agent begins with": "der Name, mit dem sein User-Agent beginnt", "heard from in the last 30 days": "in den letzten 30 Tagen gesehen", diff --git a/internal/gui/assets/plugins/routing/routing.css b/internal/gui/assets/plugins/routing/routing.css index 687fd5f4c..83c32873f 100644 --- a/internal/gui/assets/plugins/routing/routing.css +++ b/internal/gui/assets/plugins/routing/routing.css @@ -187,8 +187,13 @@ #view-routing .row-head { margin: 14px 0 6px; padding: 0 2px; gap: 0; } #view-routing .row-head .note { font-size: 11px; color: var(--faint); margin-left: 8px; } #view-routing .row-head .rt-livedot + .note { margin-left: 8px; } -.rt-mask, .rt-newgroup { display: inline-flex; align-items: center; gap: 5px; } -.rt-mask svg, .rt-newgroup svg { width: 13px; height: 13px; flex: none; } +.rt-mask, .rt-newgroup, .rt-gogroups { display: inline-flex; align-items: center; gap: 5px; } +.rt-mask svg, .rt-newgroup svg, .rt-gogroups svg { width: 13px; height: 13px; flex: none; } +/* the routing groups, far below with many models: the head goes to them */ +.rt-gogroups { margin-right: 6px; flex: none; } +.rt-gogroups[data-fit="icon"] > span { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; } +.rt-gogroups[data-fit="none"] { display: none; } +.rt-gsec:focus { outline: none; } /* the routing groups' New group, at the top too: the groups sit below the requests, out of sight on a first look (#944) */ .rt-newgroup { margin-right: 6px; } diff --git a/internal/gui/tests/routing-groups-jump.test.cjs b/internal/gui/tests/routing-groups-jump.test.cjs new file mode 100644 index 000000000..307895612 --- /dev/null +++ b/internal/gui/tests/routing-groups-jump.test.cjs @@ -0,0 +1,162 @@ +// Run with Node's test runner and Playwright on the module path; see README.md. +// The routing groups sit at the Routing page's foot, below the stage, the +// story and every request: with many models a long scroll down (shihh on +// Discord). The page's head has a Routing groups beside New group, with a +// down arrow and a tooltip saying where they are; its click brings the +// groups' heading to the top of the view (or the view to its foot) and the +// focus into them, and nothing else moves: the groups stay where they were +// in the page, and a click no reader made scrolls nothing. It takes only +// room the head has spare: named where that fits, its arrow alone (still +// named to a screen reader) where only that does, left out where nothing +// does, and every other part of the head is where it is without it. Every +// language, Chromium and WebKit, wide, a phone's width and a narrow window. +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_ROUTING_ASSETS || path.join(__dirname, "../assets")); +const models = [ + { id: "a/m", name: "m", providerName: "A", icon: "generic" }, + { id: "b/m", name: "m", providerName: "B", icon: "generic" }, +]; +const info = (ids) => ids.map((id) => ({ id, ready: true })); +const found = { id: "auto-m", name: "Model M", members: ["a/m", "b/m"], auto: true, ready: true, memberInfo: info(["a/m", "b/m"]) }; + +const words = { + en: { btn: "Routing groups", tip: "Go to the routing groups, below the requests" }, + zh: { btn: "路由组", tip: "跳到请求下方的路由组" }, + "zh-TW": { btn: "路由組", tip: "跳到請求下方的路由組" }, + ja: { btn: "ルーティンググループ", tip: "リクエストの下にあるルーティンググループへ移動" }, + de: { btn: "Weiterleitungsgruppen", tip: "Zu den Weiterleitungsgruppen unter den Anfragen springen" }, +}; + +// requests enough to put the groups well below the window's fold +const now = Date.now(); +const routes = Array.from({ length: 60 }, (_, i) => ({ + id: "r" + i, seq: i + 1, at: new Date(now - i * 60000).toISOString(), agent: "claude", asked: "group/auto-m", + model: "a/m", provider: "a", status: 200, ms: 900, ttft: 300, in: 100, out: 20, done: true, +})); + +function serve(lang, web) { + const state = { agents: [{ id: "claude", name: "Claude Code", path: "/test/claude", fields: [] }], profiles: [], settings: { lang, theme: "light" } }; + return async (r) => { + const url = new URL(r.request().url()); + const json = (data) => r.fulfill({ json: data }); + if (url.pathname === "/boot.js") return r.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${lang}",theme:"light",web:${web}};` }); + if (url.pathname === "/wails/runtime.js") return r.fulfill({ contentType: "text/javascript", body: "export const Window = {};" }); + if (url.pathname === "/api/state") return json(state); + if (url.pathname === "/api/plugins") return json({ plugins: [] }); + if (url.pathname === "/api/gateway/trace") { + if (url.searchParams.get("wait")) return new Promise(() => {}); // nothing more comes + return json({ mine: true, now: new Date().toISOString(), seq: routes.length, totals: { requests: routes.length, rerouted: 0, errors: 0 }, routes }); + } + if (url.pathname === "/api/gateway/history") return json({ cut: false, days: [], routes: [] }); + if (url.pathname === "/api/groups") return json({ models, pools: [], deciders: [], found: true, groups: [found] }); + if (url.pathname === "/api/providers") return json({ providers: [], presets: [], excluded: [], gateway: { running: true, window: true, url: "http://127.0.0.1:3999" } }); + 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 r.fulfill({ body: await fs.readFile(file), contentType }); + }; +} + +for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) { + for (const lang of Object.keys(words)) { + for (const { width, web } of [{ width: 1000, web: true }, { width: 480, web: true }, { width: 640, web: false }]) { + const w = words[lang]; + test(`${engine} ${lang} ${width}px${web ? " web" : " window"}: Routing groups in the Routing page's head goes to them`, async (t) => { + const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" })); + const page = await (await browser.newContext({ viewport: { width, height: 600 }, reducedMotion: "reduce" })).newPage(); + t.after(async () => { + if (process.env.ARTIFACT_DIR) { + await fs.mkdir(process.env.ARTIFACT_DIR, { recursive: true }); + await page.screenshot({ path: path.join(process.env.ARTIFACT_DIR, `${engine}-${lang}-${width}${web ? "-web" : ""}-routing-groups-jump.png`) }); + } + await browser.close(); + }); + page.setDefaultTimeout(5000); + const errors = []; + page.on("pageerror", (e) => errors.push(e.message)); + await page.route("**/*", serve(lang, web)); + await page.goto("http://magpie.test/?view=routing"); + await page.locator(".rt-group", { hasText: "Model M" }).waitFor({ state: "attached" }); + await page.locator(".rt-reqs > *").first().waitFor({ state: "attached" }); + + const view = page.locator("#view-routing"); + const go = page.locator("#view-routing > .row-head #rtGoGroups"); + await go.waitFor({ state: "attached" }); + await page.waitForFunction(() => document.getElementById("rtGoGroups").dataset.fit); + await page.waitForTimeout(100); // the fit settles in the frame after a resize + const fit = await go.getAttribute("data-fit"); + if (width === 1000) assert.equal(fit, "full", "a wide head names it"); + assert.equal(await go.getAttribute("title"), w.tip); + assert.equal(await go.evaluate((e) => e.tagName), "BUTTON", "a button, no native select"); + assert.equal(await go.locator("svg").count(), 1, "a down arrow"); + // nothing else in the head moves for it, or wraps, at this width (the + // spacer gives it its room) + const apart = await page.evaluate(() => { + const b = document.getElementById("rtGoGroups"), h = b.parentElement; + const at = () => JSON.stringify([h.getBoundingClientRect().height, ...[...h.children].filter((c) => c !== b && !c.classList.contains("grow")).map((c) => { const r = c.getBoundingClientRect(); return [c.className, Math.round(r.left), Math.round(r.top), Math.round(r.width), Math.round(r.height)]; })]); + const withIt = at(); + b.style.setProperty("display", "none", "important"); + const without = at(); + b.style.removeProperty("display"); + return { withIt, without, over: h.scrollWidth - h.clientWidth }; + }); + assert.equal(apart.withIt, apart.without, "the head is laid out as without it"); + assert(apart.over <= 0, `nothing overflows the head (${apart.over}px)`); + const vw = await page.evaluate(() => document.documentElement.scrollWidth - innerWidth); + assert(vw <= 0, `no sideways scroll (${vw}px)`); + if (fit === "none") { + assert.equal(await go.isVisible(), false, "left out where the head has no room"); + assert.deepEqual(errors, []); + return; + } + await go.waitFor(); + // named on screen, or to a screen reader where only the arrow shows + assert.equal(await page.getByRole("button", { name: w.btn, exact: true }).count(), 1); + const label = go.locator("span"); + if (fit === "full") assert.equal((await label.textContent()).trim(), w.btn); + else assert((await label.boundingBox()).width <= 1, "the arrow alone"); + const border = await go.evaluate((e) => { const c = getComputedStyle(e); return c.borderLeftWidth !== c.borderRightWidth || c.borderLeftColor !== c.borderRightColor; }); + assert.equal(border, false, "no left-border accent"); + const hb = await go.boundingBox(); + assert(hb.x >= 0 && hb.x + hb.width <= width && hb.y >= 0, "in sight on arrival"); + + // the layout as before: the groups after every request, below the fold + assert.equal(await page.evaluate(() => { + const more = document.getElementById("rtMore"), g = more.querySelector(".rt-gsec"), reqs = more.querySelector(".rt-reqs"); + return g.parentElement === more && more.lastElementChild === g && !!(reqs.compareDocumentPosition(g) & Node.DOCUMENT_POSITION_FOLLOWING); + }), true, "the groups stay at the page's foot, after the requests"); + const vh = await view.evaluate((v) => v.getBoundingClientRect().bottom); + const gTop = () => page.locator(".rt-gsec").evaluate((e) => e.getBoundingClientRect().top); + assert(await gTop() > vh, "the groups are below the fold on arrival"); + assert.equal(await view.evaluate((v) => v.scrollTop), 0, "the page opens at its top"); + + // a click no reader made (code's own) scrolls nothing + await go.evaluate((b) => b.click()); + await page.waitForTimeout(300); + assert.equal(await view.evaluate((v) => v.scrollTop), 0, "an untrusted click doesn't move the page"); + + // the reader's click: the groups' heading at the top of the view, the focus in them + await go.click(); + // at the top of the view, or the view at its foot where the page ends first + await page.waitForFunction(() => { + const v = document.getElementById("view-routing"), g = document.querySelector(".rt-gsec"); + const top = g.getBoundingClientRect().top - v.getBoundingClientRect().top; + return (top >= -1 && top < 40) || (top > 0 && v.scrollTop > 0 && v.scrollTop + v.clientHeight >= v.scrollHeight - 1); + }); + assert.equal(await page.evaluate(() => document.querySelector(".rt-gsec").contains(document.activeElement)), true, "the focus goes with them"); + const gh = await page.locator(".rt-gsec > .row-head .label").first().boundingBox(); + assert(gh && gh.y >= 0 && gh.y + gh.height <= vh, "the groups' heading is in sight"); + // and stays there: a moment later the view hasn't been pulled away + const at = await view.evaluate((v) => v.scrollTop); + await page.waitForTimeout(400); + assert.equal(await view.evaluate((v) => v.scrollTop), at, "the view stays where the click took it"); + assert.deepEqual(errors, []); + }); + } + } +}