diff --git a/internal/gui/assets/plugins/routing/client.js b/internal/gui/assets/plugins/routing/client.js index 28c3eaae2..4b90e54ba 100644 --- a/internal/gui/assets/plugins/routing/client.js +++ b/internal/gui/assets/plugins/routing/client.js @@ -1573,7 +1573,7 @@ function routingPage(part) { const card = () => { const box = el("div", "rt-done"); const model = w ? (tr.auto?.length ? `${w.model} → ${tr.auto[tr.auto.length - 1].model}` : w.model) : tr.model || ""; - box.append(el("b", "who", w ? who(w) : tr.id), el("span", "mdl", model || ""), el("span", "effort-of", tr.effort || "")); + box.append(el("b", "who", w ? (w.kind === "key" ? where(w) : who(w)) : tr.id), el("span", "mdl", model || ""), el("span", "effort-of", tr.effort || "")); const v = speedOf(r, tr.ms, tr.ttft, tr.firstText, tr.flow, tr.firstReasoning || 0, tr.reasoningHidden || false); const { prompt, read } = promptOf(r); for (const [value, label] of [ @@ -2816,10 +2816,11 @@ function routingPage(part) { function makeRow(w) { const li = el("li"), b = el("b"); b.append(icon(w.icon || (w.preset ? w.preset : "generic"))); - const name = el("span", "who", who(w)); + // a key's row is headed by its provider's name (prov), set in rebuild + const prov = el("span", "prov", ""), name = el("span", "who", who(w)); // the provider's name heads the card; a row names what differs - const sub = el("span", "", rowSub(w, false)); - b.append(name, " ", sub, el("code", "mdl", w.fixed ? `${w.model}:${w.fixed}` : w.model)); + const sub = el("span", "", rowSub(w, false, false)); + b.append(prov, name, " ", sub, el("code", "mdl", w.fixed ? `${w.model}:${w.fixed}` : w.model)); if (w.fixed) b.title = t("{level} reasoning, fixed on this model in the group", { level: w.fixed }); if (w.fast) { // the group sends this member in its vendor's fast mode @@ -2842,13 +2843,21 @@ function routingPage(part) { st.onpointerleave = () => delete st.dataset.cut; li.append(el("i", "dot"), b, st, bar, tg); li.title = w.id; - return { li, wire: path(), st, bi, tg, w, subEl: sub }; + return { li, wire: path(), st, bi, tg, w, provEl: prov, subEl: sub }; } + // keyLed: a key's row is headed by its provider's name, the key's + // after it, muted: a key's name is often only its masked secret + // ("sk-4…ab10"), which says nothing of whose it is, so two relays' keys + // each serving one model read alike (iamleehom on X). Under a fold, + // whose heading names the provider, the key's name leads the row. + const keyLed = (w, folded) => w.kind === "key" && !folded; // rowSub is what a row says after the name: a fallback's provider, an - // account's plan; and the provider of a key or account named as one of - // another provider's on the stage is (twin), so two providers' Key-01 - // rows, each of the same model, are told apart (iamleehom on Discord) - const rowSub = (w, twin) => w.fallback ? w.name : w.kind === "provider" ? "" : twin ? [w.name || w.provider, planName(w.plan)].filter(Boolean).join(" · ") : planName(w.plan) || ""; + // account's plan; and the provider of an account (or a folded key) named + // as one of another provider's on the stage is (twin), so two + // providers' Key-01 rows, each of the same model, are told apart + // (iamleehom on Discord); a key-led row says its provider before it + const rowSub = (w, twin, folded) => keyLed(w, folded) ? planName(w.plan) || "" + : w.fallback ? w.name : w.kind === "provider" ? "" : twin ? [w.name || w.provider, planName(w.plan)].filter(Boolean).join(" · ") : planName(w.plan) || ""; // sync puts the staged routes on the stage: each agent on the left, and // the accounts they weighed, each route's in the order it weighed them — @@ -2949,13 +2958,6 @@ function routingPage(part) { // its way down: its route's heading, then the groups in the group row.path = ways ? [wayAt.get(id), ...(row.w.via || [])] : row.w.via || []; } - // a name more than one provider's rows go by says the provider too - const provsOf = new Map(); - for (const id of ids) { - const w = rows.get(id).w; - if (w.kind !== "provider") { const n = who(w); provsOf.set(n, (provsOf.get(n) || new Set()).add(w.provider)); } - } - for (const id of ids) { const row = rows.get(id); setText(row.subEl, rowSub(row.w, provsOf.get(who(row.w))?.size > 1)); } // a group in the group heads its models, which it routes by its own // routing, set in under it // keys or accounts side by side that fold together, FOLD_AT or more @@ -2977,6 +2979,20 @@ function routingPage(part) { else { flush(); if (folding(w)) run = [id]; } } flush(); + // a row's name and what it says after it: a key named by its provider, + // unless a fold over it names that; and a name more than one + // provider's rows go by says the provider too + const provsOf = new Map(); + for (const id of ids) { + const w = rows.get(id).w; + if (w.kind !== "provider" && !keyLed(w, foldOf.has(id))) { const n = who(w); provsOf.set(n, (provsOf.get(n) || new Set()).add(w.provider)); } + } + for (const id of ids) { + const row = rows.get(id), folded = foldOf.has(id), led = keyLed(row.w, folded); + setText(row.provEl, led ? row.w.name || row.w.provider : ""); + setText(row.subEl, rowSub(row.w, !led && provsOf.get(who(row.w))?.size > 1, folded)); + row.li.classList.toggle("keyled", led); + } // how many each group in the group routes to, which folds it when LONG const under = new Map(); for (const id of ids) { diff --git a/internal/gui/assets/plugins/routing/routing.css b/internal/gui/assets/plugins/routing/routing.css index 1acc6bc15..7b7b5a14f 100644 --- a/internal/gui/assets/plugins/routing/routing.css +++ b/internal/gui/assets/plugins/routing/routing.css @@ -119,10 +119,14 @@ .rt-accts li:not(.rt-sub) > b > * { flex: none; } .rt-accts li:not(.rt-sub) > b > span:not(.ic) { flex: 0 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; } .rt-accts li:not(.rt-sub) > b > span.who { flex-shrink: 100; } /* the name first, the plan after it */ -.rt-accts li:not(.rt-sub) > b > span:not(.ic):not(.who):not(:empty) { margin-left: .3em; } +.rt-accts li:not(.rt-sub) > b > span:not(.ic):not(.who):not(.prov):not(:empty) { margin-left: .3em; } +/* a key's row: its provider's name heads it and keeps its room; the key's + name follows, muted */ +.rt-accts li.keyled:not(.rt-sub) > b > span.who { margin-left: .3em; } .rt-accts li:not(.rt-sub) > b > .ic { align-self: center; } .rt-accts b > span:not(.ic) { font-weight: 450; color: var(--muted); } -.rt-accts b > span.who { color: var(--fg); font-weight: 560; } +.rt-accts b > span.who, .rt-accts li.keyled b > span.prov { color: var(--fg); font-weight: 560; } +.rt-accts li.keyled b > span.who { color: var(--muted); font-weight: 450; } .rt-accts b .ic { display: inline-flex; width: 16px; height: 16px; margin-right: 7px; vertical-align: -3px; } .rt-accts b .ic img, .rt-accts b .ic svg { width: 16px; height: 16px; } .rt-accts b .fb { margin-left: 6px; font-size: 10.5px; font-weight: 560; padding: 0 6px; border-radius: 999px; border: 1px solid var(--line); color: var(--muted); } diff --git a/internal/gui/tests/routing-key-provider.test.cjs b/internal/gui/tests/routing-key-provider.test.cjs new file mode 100644 index 000000000..8a3d3cdae --- /dev/null +++ b/internal/gui/tests/routing-key-provider.test.cjs @@ -0,0 +1,128 @@ +// Run with Node's test runner and Playwright on the module path; see README.md. +// iamleehom on X ("为什么路由中不显示供应商名称了?"): two OpenAI-compatible +// providers' keys, neither named, served gpt-6-astra under Smart, and the +// stage's rows read "sk-4…ab10" and "sk-a…eb0c": the masked keys where the +// provider's name was looked for, the same OpenAI logo on both. A key's row +// is named by its provider, the key after it, muted; the provider's name +// keeps its room at a narrow width, the key's gives it up; a fold of a +// provider's keys names the provider once, and the keys under it by their +// own names; the answered try's card names the provider too. Every +// language, both engines, a wide and a narrow width; 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(__dirname, "../assets"); +const now = new Date(); +const ago = (s) => new Date(now.getTime() - s * 1e3).toISOString(); +const M = "gpt-6-astra"; +const key = (provider, name, k) => ({ id: `${provider}#${k}`, provider, name, icon: "openai", who: k, kind: "key", model: M }); +const order = [key("relay-east", "Relay East", "sk-4…ab10"), key("relay-west", "Relay West", "sk-a…eb0c")]; +const group = { id: "astra", name: "Astra", routing: "", members: ["relay-east/" + M, "relay-west/" + M] }; +const first = { id: 1, seq: 1, time: ago(30), agent: "codex", model: "astra", provider: "relay-east", done: true, status: 200, ms: 900, tokens: 1200, group, order, + tries: [{ id: order[0].id, model: M, start: ago(30), done: true, status: 200, ms: 900 }] }; +// one provider's six keys, folded (FOLD_AT) and opened +const many = Array.from({ length: 6 }, (_, i) => key("pool", "Pool", `sk-${i}…000${i}`)); +const pooled = { id: 2, seq: 2, time: ago(10), agent: "codex", model: "pool/" + M, provider: "pool", done: true, status: 200, ms: 700, tokens: 900, order: many, + tries: [{ id: many[0].id, model: M, start: ago(10), done: true, status: 200, ms: 700 }] }; + +// live: routes go out on the long poll as the test sends them +function serve(lang, routes, live) { + return async (request) => { + const url = new URL(request.request().url()); + const json = (data) => request.fulfill({ json: data }); + if (url.pathname === "/boot.js") return request.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${lang}",theme:"light",web:true};` }); + if (url.pathname === "/wails/runtime.js") return request.fulfill({ contentType: "text/javascript", body: "export const Window = {};" }); + if (url.pathname === "/api/state") return json({ agents: [{ id: "codex", name: "Codex", path: "/test/config.toml", fields: [] }], profiles: [], settings: { lang, theme: "light" } }); + if (url.pathname === "/api/gateway/trace") { + if (url.searchParams.get("wait")) { + if (!live) await new Promise((r) => setTimeout(r, 20e3)); + else if (live.queued.length) return json(live.queued.shift()); + else { live.pending.push(request); return; } + } + return json({ mine: true, now: now.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({ groups: [], pools: [] }); + if (url.pathname === "/api/providers") return json({ providers: [], excluded: [], gateway: { running: true, window: true, url: "http://127.0.0.1:3425" } }); + 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 request.fulfill({ body: await fs.readFile(file).catch(() => ""), contentType }); + }; +} + +async function open(browser, lang, width, routes, live) { + const context = await browser.newContext({ viewport: { width, height: 900 }, reducedMotion: "reduce" }); + const page = await context.newPage(); + page.setDefaultTimeout(5000); + page.setDefaultNavigationTimeout(60000); + const errors = []; + page.on("pageerror", (e) => errors.push(e.message)); + await page.route("**/*", serve(lang, routes, live)); + await page.goto("http://magpie.test/?view=routing"); + return { context, page, errors }; +} + +const rowsOf = (page) => page.locator(".rt-accts > li:not(.rt-fold):not(.rt-sub)").filter({ has: page.locator("b span.who") }); +const read = (rows) => rows.evaluateAll((ls) => ls.map((li) => { + const b = li.querySelector("b"), prov = b.querySelector("span.prov"), who = b.querySelector("span.who"); + return { prov: prov.textContent, name: who.textContent, provCut: prov.scrollWidth > prov.clientWidth + 1 }; +})); + +for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) { + test(`${engine}: a key's row is named by its provider`, async (t) => { + const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" })); + t.after(() => browser.close()); + for (const lang of ["en", "zh", "zh-TW", "ja", "de"]) { + for (const width of [1100, 440]) { + await t.test(`${lang} ${width}px: two providers' unnamed keys`, async () => { + const { context, page, errors } = await open(browser, lang, width, [first]); + t.after(() => context.close()); + const rows = rowsOf(page); + await rows.nth(order.length - 1).waitFor(); + const seen = await read(rows); + assert.deepEqual(seen.map((r) => [r.prov, r.name]), [["Relay East", "sk-4…ab10"], ["Relay West", "sk-a…eb0c"]]); + if (width === 1100) for (const r of seen) assert.equal(r.provCut, false, `${r.prov} is whole`); + assert.equal(await page.evaluate(() => document.documentElement.scrollWidth > innerWidth), false, "nothing runs off the page"); + assert.deepEqual(errors, []); + }); + await t.test(`${lang} ${width}px: the answer's card names the provider`, async () => { + const live = { pending: [], queued: [], seq: 0 }; + const { context, page, errors } = await open(browser, lang, width, [], live); + t.after(() => context.close()); + const send = (route) => { + const d = { mine: true, now: new Date().toISOString(), seq: ++live.seq, totals: { requests: live.seq, rerouted: 0, errors: 0 }, routes: [route] }; + const r = live.pending.shift(); + if (r) r.fulfill({ json: d }); else live.queued.push(d); + }; + await page.locator(".rt-cap").waitFor({ state: "attached" }); + const going = { ...first, time: new Date().toISOString(), done: false, status: 0, ms: 0, tokens: 0, + tries: [{ ...first.tries[0], start: new Date().toISOString(), done: false, status: 0, ms: 0 }] }; + send(going); + await rowsOf(page).nth(order.length - 1).waitFor(); + send({ ...going, done: true, status: 200, ms: 900, tokens: 1200, tries: [{ ...going.tries[0], done: true, status: 200, ms: 900 }] }); + const who = page.locator(".rt-cap .rt-done > .who"); + await who.waitFor(); + assert.equal(await who.textContent(), "Relay East · sk-4…ab10"); + assert.deepEqual(errors, []); + }); + await t.test(`${lang} ${width}px: a fold names the provider once`, async () => { + const { context, page, errors } = await open(browser, lang, width, [pooled]); + t.after(() => context.close()); + const fold = page.locator(".rt-accts > li.rt-fold"); + await fold.waitFor(); + assert.equal(await fold.locator("b span.who").textContent(), "Pool"); + if ((await fold.getAttribute("aria-expanded")) !== "true") await fold.click(); + const rows = rowsOf(page); + await rows.nth(many.length - 1).waitFor(); + const seen = await read(rows); + assert.deepEqual(seen.map((r) => [r.prov, r.name]), many.map((k) => ["", k.who])); + assert.deepEqual(errors, []); + }); + } + } + }); +} diff --git a/internal/gui/tests/routing-twin-keys.test.cjs b/internal/gui/tests/routing-twin-keys.test.cjs index 662f7bc78..f4799487a 100644 --- a/internal/gui/tests/routing-twin-keys.test.cjs +++ b/internal/gui/tests/routing-twin-keys.test.cjs @@ -4,8 +4,10 @@ // alike (the same icon, key name and model), so which provider a row was // of couldn't be told. A key or account named as one of another // provider's on the stage says its provider after the name; a name only -// one provider has says nothing more, as before. Every language, both -// engines, a wide and a narrow width; no backend, the API is faked here. +// one provider has says nothing more, as before. Since iamleehom on X, a +// key's row is named by its provider and the key's name follows it. Every +// language, both engines, a wide and a narrow width; no backend, the API +// is faked here. const assert = require("node:assert/strict"); const fs = require("node:fs/promises"); const path = require("node:path"); @@ -67,12 +69,17 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", await rows.nth(order.length - 1).waitFor(); const seen = await rows.evaluateAll((ls) => ls.map((li) => { const b = li.querySelector("b"), sub = b.querySelector("span.who + span"); - return [b.querySelector("span.who").textContent, sub.textContent, sub.getBoundingClientRect().width]; + return [b.querySelector("span.prov").textContent, b.querySelector("span.who").textContent, sub.textContent]; })); - assert.deepEqual(seen.map(([n, s]) => [n, s]), [ - ["Key-01", "Relay A"], ["Key-02", "Relay A"], ["Key-01", "Relay B"], ["Key-02", "Relay B"], ["Key-09", ""], + // a key's row is headed by its provider since iamleehom on X, + // the key's name after it, so the twin rule has nothing to add + assert.deepEqual(seen, [ + ["Relay A", "Key-01", ""], ["Relay A", "Key-02", ""], ["Relay B", "Key-01", ""], ["Relay B", "Key-02", ""], ["Solo", "Key-09", ""], ]); - if (width === 1100) for (const [n, s, w] of seen) if (s) assert.ok(w > 0, `${n} of ${s} is drawn`); + if (width === 1100) { + const drawn = await rows.evaluateAll((ls) => ls.map((li) => li.querySelector("b span.prov").getBoundingClientRect().width)); + for (const w of drawn) assert.ok(w > 0, "each provider's name is drawn"); + } // no row is alike another any more const texts = await rows.evaluateAll((ls) => ls.map((li) => li.querySelector("b").textContent)); assert.equal(new Set(texts).size, texts.length, texts.join(" | "));