diff --git a/internal/gui/assets/context.css b/internal/gui/assets/context.css index 519849232..6754668f1 100644 --- a/internal/gui/assets/context.css +++ b/internal/gui/assets/context.css @@ -22,11 +22,19 @@ i.dot { display: inline-block; flex: none; width: 8px; height: 8px; border-radiu i.dot.free { background: var(--k-free); box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--fg) 10%, transparent); } .ctx-card { position: relative; background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 14px 16px 12px; display: flex; flex-direction: column; gap: 14px; min-width: 0; font-variant-numeric: tabular-nums; } -.ctx-head { display: flex; align-items: center; gap: 8px; min-width: 0; } +.ctx-head { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 8px; min-width: 0; } +/* the route and the state, kept together at the end of the head: beside + the title while the whole model id fits there, else on a line of their + own, where the id ends in an ellipsis */ +.ctx-end { display: flex; flex: 1 1 auto; justify-content: flex-end; align-items: center; gap: 8px; min-width: 0; } .ctx-head .grow, .ctx-foot .grow, .ctx-contents-head .grow, .ctx-agent-head .grow, .ctx-agent-foot .grow, .ctx-sess-head .grow { flex: 1; } -.ctx-title { font-size: 13px; font-weight: 600; color: var(--fg); white-space: nowrap; } +.ctx-title { flex: none; font-size: 13px; font-weight: 600; color: var(--fg); white-space: nowrap; } .ctx-route, .ctx-who, .ctx-what { display: inline-flex; align-items: center; gap: 5px; min-width: 0; } -.ctx-route { gap: 6px; font-size: 12px; } +/* the title never gives way: the model id does, down to its ellipsis, and + past that the route is cut at its own edge, so nothing in it is drawn + over the title or the state (packinggoods on Discord: in a narrow window + the provider's icon sat on the end of "Context window") */ +.ctx-route { gap: 6px; font-size: 12px; overflow: hidden; } .ctx-who { flex: none; color: var(--fg-2); font-weight: 500; white-space: nowrap; } .ctx-who .ic { width: 14px; height: 14px; } .ctx-what .ic { flex: none; width: 12px; height: 12px; } @@ -140,7 +148,7 @@ i.dot.free { background: var(--k-free); box-shadow: inset 0 0 0 1px color-mix(in /* Routing: the card under the story of the request */ .rt-log .ctx-card { margin-top: 12px; } /* folded: its head alone, how full it is beside the title */ -.ctx-fold { display: inline-flex; align-items: center; gap: 6px; min-width: 0; height: 22px; margin: -2px 0 -2px -4px; padding: 0 6px 0 4px; border: 0; border-radius: 6px; background: none; color: inherit; font: inherit; cursor: pointer; } +.ctx-fold { display: inline-flex; flex: none; align-items: center; gap: 6px; height: 22px; margin: -1px 0 -1px -4px; padding: 0 6px 0 4px; border: 0; border-radius: 6px; background: none; color: inherit; font: inherit; cursor: pointer; } .ctx-fold:hover { background: var(--pill); } .ctx-fold .tw { display: inline-flex; width: 10px; flex: none; color: var(--faint); } .ctx-fold .tw svg { transition: transform .2s var(--ease, ease); } @@ -152,11 +160,21 @@ i.dot.free { background: var(--k-free); box-shadow: inset 0 0 0 1px color-mix(in .ctx-card.shut > :not(.ctx-head) { display: none; } /* its head alone, the space under it is the space over it */ .ctx-card.shut { padding-bottom: 14px; } -:where(.ctx-card.shut.max420) .ctx-what { display: none; } /* folded, the head is one line of a fixed height whatever a live request puts in it, so nothing under it moves; a thin bar of what fills the window, a part each, takes the room between the figure and the model */ -.ctx-card.shut .ctx-head { height: 20px; white-space: nowrap; } +.ctx-card.shut .ctx-head { height: 20px; white-space: nowrap; flex-wrap: nowrap; } +/* folded, the bar takes the room and the model id gives it up first. A + narrower card keeps the agent's icon alone, a narrow one no route and + the state's dot without its word; only past that is the figure cut, so + nothing is drawn over another */ +.ctx-card.shut .ctx-end { flex: 0 1 auto; } +.ctx-card.shut.max520 :is(.ctx-who-n, .ctx-sep, .ctx-what) { display: none; } +.ctx-card.shut.max420 .ctx-route { display: none; } +.ctx-card.shut.max420 .ctx-end { flex: none; } +.ctx-card.shut.max420 .ctx-state { padding: 0 7px; } +.ctx-card.shut.max420 .ctx-state-w { display: none; } +.ctx-card.shut.max420 .ctx-short { flex: 0 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; } .ctx-stack { display: none; flex: 1 1 0; min-width: 24px; max-width: 360px; height: 6px; border-radius: 3px; background: var(--k-free); overflow: hidden; } .ctx-card.shut .ctx-stack { display: flex; } .ctx-stack i { flex: none; height: 100%; background: var(--k); transition: width .3s var(--ease, ease); } diff --git a/internal/gui/assets/context.js b/internal/gui/assets/context.js index da4c4a136..f907f2f35 100644 --- a/internal/gui/assets/context.js +++ b/internal/gui/assets/context.js @@ -790,8 +790,13 @@ if (r.agent) route.append(el("span", "ctx-sep", "›")); route.append(what); } - head.append(route); - head.append(st); + // the route and the state go together: where the title leaves them + // too little room they take a line of their own, the model id cut + // to fit there, and nothing is drawn over the title (packinggoods on + // Discord: in a narrow window the provider's icon sat on it) + const end = el("span", "ctx-end"); + end.append(route, st); + head.append(end); // how full, and the cache const top = el("div", "ctx-top"); diff --git a/internal/gui/tests/ctx-head-narrow.test.cjs b/internal/gui/tests/ctx-head-narrow.test.cjs new file mode 100644 index 000000000..052744e1f --- /dev/null +++ b/internal/gui/tests/ctx-head-narrow.test.cjs @@ -0,0 +1,162 @@ +// Run with Node's test runner and Playwright on the module path; see README.md. +// Routing: at every window width the context window card's head keeps its +// parts apart (packinggoods on Discord: in a narrow window the provider's +// icon was drawn over the end of "Context window", the model id beside it). +// The title is never cut or run under its neighbour; the long model id is +// what gives way, ending in an ellipsis, and the icon before it stays clear +// of the title and of the state pill. Open and folded, with and without the +// agent, a live request and a counted one, every GUI language, Chromium and +// WebKit. +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 iso = (ms) => new Date(ms).toISOString(); +// the reporter's model: a remote magpie used as a provider +const model = "remote-magpie/claude/claude-opus-5-5"; +const promptOf = (window) => ({ + window, tokens: 186000, counted: false, turns: 1, + parts: [ + { kind: "system", tokens: 4000, items: [{ name: "prompt", tokens: 4000 }] }, + { kind: "chat", tokens: 182000, items: [{ name: "turn", tag: "turns", n: 1, tokens: 182000 }] }, + ], +}); +function req(agent, live, window) { + const prompt = promptOf(window); + const seat = { id: "acc@me", provider: "remote-magpie", name: "Remote", who: "me@example.com", kind: "account", model, known: true }; + const r = { + id: 101, seq: 1010, time: iso(Date.now() - 5000), agent, session: "s-1", model, provider: "remote-magpie", + order: [seat], tries: [{ id: seat.id, model, start: iso(Date.now() - 5000), done: !live, status: live ? 0 : 200, ms: 3000 }], + done: !live, status: live ? 0 : 200, ms: 3000, tokens: 900, usage: live ? [] : [{ in: 12000, out: 900 }], prompt, + }; + if (!agent) delete r.agent; + return r; +} + +function serve(lang, r) { + const state = { agents: [{ id: "claude", name: "Claude Code", icon: "claudecode-color", path: "/test/claude", fields: [] }], profiles: [], settings: { lang, theme: "light" } }; + 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:true};` }); + 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/gateway/trace") { + if (url.searchParams.get("wait")) return; // no more requests come in + return json({ mine: true, now: new Date().toISOString(), seq: r.seq, totals: { requests: 1, rerouted: 0, errors: 0 }, routes: [r] }); + } + if (url.pathname === "/api/gateway/history") return json({ cut: false, days: [], routes: [] }); + if (url.pathname === "/api/groups") return json({ groups: [], models: [], pools: [] }); + 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 route.fulfill({ body: await fs.readFile(file).catch(() => ""), contentType }); + }; +} + +// the head's drawn parts, each as the box its ink takes: the title's text +// (a range, so text run out of its box counts where it is drawn), each icon, +// the model id as drawn (clipped to its box), the state pill +const parts = (page) => page.evaluate(() => { + const assert = (ok, m) => { if (!ok) throw new Error(m); }; + const head = document.querySelector(".rt-ctx .ctx-head"); + const card = head.closest(".ctx-card"); + const box = (r) => ({ l: r.left, r: r.right, t: r.top, b: r.bottom, w: r.width }); + // what of a box is drawn: cut to every ancestor in the head that clips + const drawn = (e) => { + let r = e.getBoundingClientRect(), l = r.left, rt = r.right, t = r.top, b = r.bottom; + for (let a = e.parentElement; a && a !== head.parentElement; a = a.parentElement) { + if (getComputedStyle(a).overflowX === "visible") continue; + const c = a.getBoundingClientRect(); + l = Math.max(l, c.left); rt = Math.min(rt, c.right); t = Math.max(t, c.top); b = Math.min(b, c.bottom); + } + return { l, r: rt, t, b, w: Math.max(0, rt - l), full: r.width }; + }; + const shown = (e) => !!e && e.getClientRects().length > 0 && e.getBoundingClientRect().width > 0; + const out = []; + const title = head.querySelector(".ctx-title"); + if (title) { + const rg = document.createRange(); + rg.selectNodeContents(title); + // the text against the box it sits in: the fold's button when folding + // is on, the head otherwise + const tr = rg.getBoundingClientRect(), holder = (title.closest(".ctx-fold") || head).getBoundingClientRect(); + out.push({ n: "title", ...box(tr), cut: tr.right > holder.right + 0.5 || tr.left < holder.left - 0.5 }); + } + for (const [n, s] of [["agent icon", ".ctx-who .ic"], ["agent name", ".ctx-who-n"], ["lab icon", ".ctx-what .ic"], ["model", ".ctx-model"], ["short", ".ctx-short"], ["state", ".ctx-state"]]) { + const e = head.querySelector(s); + if (!shown(e)) continue; + const d = drawn(e); + // an icon is drawn whole or not at all, never cut by the route's edge + if (n.endsWith("icon") && d.w > 0.5) assert(d.w >= d.full - 0.5, `the ${n} is cut: ${JSON.stringify(d)}`); + if (d.w > 0.5) out.push({ n, ...d }); + } + const hr = head.getBoundingClientRect(); + return { parts: out, head: box(hr), card: box(card.getBoundingClientRect()), shut: card.classList.contains("shut"), vw: innerWidth, model: head.querySelector(".ctx-model")?.textContent }; +}); + +const overlaps = (a, b) => Math.min(a.r, b.r) - Math.max(a.l, b.l) > 0.5 && Math.min(a.b, b.b) - Math.max(a.t, b.t) > 0.5; + +function check(seen, where) { + const ps = seen.parts; + const title = ps.find((p) => p.n === "title"); + assert.ok(title && !title.cut, `${where}: the title is whole ${JSON.stringify(title)}`); + for (let i = 0; i < ps.length; i++) { + for (let j = i + 1; j < ps.length; j++) { + assert.ok(!overlaps(ps[i], ps[j]), `${where}: ${ps[i].n} and ${ps[j].n} overlap ${JSON.stringify([ps[i], ps[j]])}`); + } + assert.ok(ps[i].r <= seen.card.r + 0.5, `${where}: ${ps[i].n} runs past the card ${JSON.stringify(ps[i])} ${JSON.stringify(seen.card)}`); + } + assert.equal(seen.model, model); + assert.ok(ps.some((p) => p.n === "state"), `${where}: the state pill is drawn`); +} + +const langs = ["en", "zh", "zh-TW", "ja", "de"]; +// the reporter's window (~640px of content beside the sidebar) and every +// width around it, down to a phone's +const widths = [1100, 980, 900, 860, 820, 780, 740, 700, 660, 620, 580, 540, 500, 460, 420, 380, 340]; + +for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) { + test(`${engine}: the context window's head keeps its parts apart at every width`, async (t) => { + const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" })); + t.after(() => browser.close()); + for (const lang of langs) { + // the reporter's (no agent, live, the window not known) and its neighbours + for (const [agent, live, window] of [[null, true, 0], ["claude", true, 0], ["claude", false, 0], ["claude", false, 272000], [null, true, 272000]]) { + await t.test(`${lang} ${agent || "no agent"} ${live ? "live" : "counted"} ${window ? "window " + window : "window not known"}`, async () => { + const context = await browser.newContext({ viewport: { width: widths[0], height: 900 } }); + t.after(() => context.close()); + const page = await context.newPage(); + await page.addInitScript(() => { try { localStorage.setItem("magpie.ctxShut", "0"); } catch {} }); + page.setDefaultTimeout(5000); + page.setDefaultNavigationTimeout(60000); + const errors = []; + page.on("pageerror", (e) => errors.push(e.message)); + await page.route("**/*", serve(lang, req(agent, live, window))); + await page.goto("http://magpie.test/?view=routing"); + await page.locator(".rt-ctx .ctx-head .ctx-route").waitFor(); + for (const shut of [false, true]) { + if (shut) { + await page.setViewportSize({ width: widths[0], height: 900 }); + await page.locator(".rt-ctx .ctx-fold").click(); + await page.locator(".rt-ctx .ctx-card.shut").waitFor(); + } + for (const w of widths) { + await page.setViewportSize({ width: w, height: 900 }); + // the card's width classes follow a ResizeObserver: a frame on + await page.evaluate(() => new Promise((ok) => requestAnimationFrame(() => requestAnimationFrame(ok)))); + const seen = await parts(page); + assert.equal(seen.shut, shut); + check(seen, `${shut ? "folded" : "open"} at ${w}px`); + } + } + assert.deepEqual(errors, []); + }); + } + } + }); +}