diff --git a/internal/gui/assets/plugins/routing/client.js b/internal/gui/assets/plugins/routing/client.js index 224aaa4b8..3939577b8 100644 --- a/internal/gui/assets/plugins/routing/client.js +++ b/internal/gui/assets/plugins/routing/client.js @@ -1047,10 +1047,24 @@ function routingPage(part) { : t("{by} was asked which of {kinds} turn {turn} is, and said “{intent}” ({took}).", { by, kinds, turn: r.rule.turn, intent: c.intent, took: when + sure })) + told; return effort ? `${said} ${effort}` : said; } + // intentOf is the intent a rule's condition asks for, as the gateway + // writes it (intent "…"); undefined for another condition + const intentOf = (c) => /^intent "(.*)"$/.exec(c)?.[1].replace(/\\"/g, '"').replace(/\\\\/g, "\\"); + // smartFor is the intent the decision model said the turn is, when a + // rule by that intent put the request's first where it is, in the group + // or a group in it: the row routing chose says it where an account's + // allowance goes (joey_xue on Discord) + function smartFor(r) { + for (const x of [r.rule, ...(r.nested || []).map((n) => n.rule)]) { + if (!x?.use || x.unready) continue; + for (const c of x.when || []) { const m = intentOf(c); if (m) return m; } + } + return ""; + } // a rule's condition as the gateway writes it, in the page's words function condText(c) { let m; - if ((m = /^intent "(.*)"$/.exec(c))) return t("asks for “{intent}”", { intent: m[1].replace(/\\"/g, '"').replace(/\\\\/g, "\\") }); + if ((m = intentOf(c)) != null) return t("asks for “{intent}”", { intent: m }); if ((m = /^tokens ≥ (\d+)$/.exec(c))) return t("≥ {n} tokens", { n: Number(m[1]).toLocaleString(intlLang()) }); if (c === "images") return t("has an image"); if (c === "reasoning") return t("reasoning on"); @@ -2515,18 +2529,21 @@ function routingPage(part) { // rest with it (#1960) const r = src().get(row.rid) || pinned || cur, { answered, rests, gave, went } = findings(r); const w = row.w, rest = rests.get(w.id) || rests.get(w.id + "\u0000" + (w.model || "")), resting = rest && at(rest.until) > n; + const smart = r.order[0] && id === seat(r.order[0]) && smartFor(r), said = smart ? t("picked for “{intent}”", { intent: smart }) : ""; let s, b; if (w.unlisted) s = unlistedWord(w); else if (resting) s = `${failWord(rest.why)} · ${restWhen(rest)}`; else if (w.window) s = t("request longer than its {n} window · passed over", { n: ctxShort(w.window) }); // a row another request weighed, answering that one: the header // tells of this request, so it says the answer is not this one's - else if (trying.has(id)) s = r.id !== cur.id && agents.size <= 1 ? t("answering another request…") : t("answering…"); - else if (answered.has(id)) s = agents.size > 1 ? t("answered {agent}", { agent: agentName(r.agent) }) : t("answered this request"); + else if (trying.has(id)) s = (r.id !== cur.id && agents.size <= 1 ? t("answering another request…") : t("answering…")) + (said && " · " + said); + else if (answered.has(id)) s = (agents.size > 1 ? t("answered {agent}", { agent: agentName(r.agent) }) : t("answered this request")) + (said && " · " + said); else if (gave.has(id)) s = t("{status} · {fail} · passed to {agent}", { status: gave.get(id).status, fail: failWord(gave.get(id).fail), agent: agentName(r.agent) }); else if (went.has(id)) s = t("{status} · {fail} · went on to the next", { status: went.get(id).status, fail: failWord(went.get(id).fail) }); else if (w.noURL) s = noURLWord(w); else if (w.sunk && !lifted(sunkKey(w), at(w.sunk))) s = t("rate limited at {time} · at the back", { time: clock(w.sunk) }); + // the row smart routing's intent put first says the intent instead + else if (said) s = said; // a model its vendor serves at no cost is no share of the account's // allowance: the windows are what the rest of it costs, so "100% // used" and its bar said the seat that always can answer was spent diff --git a/internal/gui/assets/plugins/routing/i18n.js b/internal/gui/assets/plugins/routing/i18n.js index 3a72ab5f9..bfb003a53 100644 --- a/internal/gui/assets/plugins/routing/i18n.js +++ b/internal/gui/assets/plugins/routing/i18n.js @@ -532,6 +532,7 @@ magpie.strings({ "answering…": "回答中…", "answering another request…": "在回答另一个请求…", "answered this request": "回答了这个请求", + "picked for “{intent}”": "按意图“{intent}”选中", "answered {agent}": "回答了 {agent}", "{n} agents at once": "{n} 个 Agent 同时在发", "In order: member by member, the first model the group names until it can't answer, each over its own accounts or keys as its provider routes them.": "按顺序:逐个成员,先用组内第一个模型,无法响应再换下一个;各成员按其供应商的路由在自己的账号或 Key 间分配。", @@ -1121,6 +1122,7 @@ magpie.strings({ "answering…": "回答中…", "answering another request…": "在回答另一個請求…", "answered this request": "回答了這個請求", + "picked for “{intent}”": "按意圖「{intent}」選中", "answered {agent}": "回答了 {agent}", "{n} agents at once": "{n} 個 Agent 同時在發", "In order: member by member, the first model the group names until it can't answer, each over its own accounts or keys as its provider routes them.": "按順序:逐個成員,先用組內第一個模型,無法回應再換下一個;各成員按其供應商的路由在自己的帳號或 Key 間分配。", @@ -1708,6 +1710,7 @@ magpie.strings({ "answering…": "応答中…", "answering another request…": "別のリクエストに応答中…", "answered this request": "このリクエストに応答", + "picked for “{intent}”": "インテント「{intent}」で選択", "answered {agent}": "{agent} に応答", "{n} agents at once": "{n} 個のエージェントが同時に送信中", "In order: member by member, the first model the group names until it can't answer, each over its own accounts or keys as its provider routes them.": "順番:メンバーを 1 つずつ、グループが最初に挙げるモデルから、応答できなくなるまで使います。各メンバーはそのプロバイダのルーティングに従って自分のアカウントやキーに振り分けます。", @@ -2297,6 +2300,7 @@ magpie.strings({ "answering…": "antwortet…", "answering another request…": "beantwortet eine andere Anfrage…", "answered this request": "hat diese Anfrage beantwortet", + "picked for “{intent}”": "für „{intent}“ gewählt", "answered {agent}": "hat {agent} geantwortet", "{n} agents at once": "{n} Agenten gleichzeitig", "In order: member by member, the first model the group names until it can't answer, each over its own accounts or keys as its provider routes them.": "Der Reihe nach: Mitglied für Mitglied, zuerst das erste Modell der Gruppe, bis es nicht mehr antworten kann; jedes über seine eigenen Konten oder Schlüssel, so wie sein Anbieter sie weiterleitet.", diff --git a/internal/gui/tests/routing-smart-intent.test.cjs b/internal/gui/tests/routing-smart-intent.test.cjs new file mode 100644 index 000000000..5b08b0025 --- /dev/null +++ b/internal/gui/tests/routing-smart-intent.test.cjs @@ -0,0 +1,124 @@ +// Run with Node's test runner and Playwright on the module path; see README.md. +// When smart routing's decision model said which intent a turn is, and a +// rule by that intent put the request's first where it is, that row says +// the intent where an account's allowance goes (joey_xue on Discord); one +// answering or that answered keeps saying so, the intent after it. A +// route a rule put first by no intent, and every other row, still say +// what they have left. In every language, and at a phone's width. +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 models = [ + { id: "a/fast", name: "fast", providerName: "A", icon: "generic", context: 256000, ready: true }, + { id: "b/opus", name: "opus", providerName: "B", icon: "generic", context: 1000000, ready: true }, +]; +const groups = () => ({ + models, + groups: [{ id: "main", name: "Main", members: ["a/fast", "b/opus"], routing: "order", ready: true, classifier: "c/cls", + memberInfo: [{ id: "a/fast", ready: true, context: 256000 }, { id: "b/opus", ready: true, context: 1000000 }], + rules: [{ use: "b/opus", intent: "complex" }] }], + pools: [], +}); +const now = Date.now(); +// how: "waiting" (not tried yet), "answered", or "tokens" (a rule by size, no intent) +const routeOf = (how) => ({ + id: 7, seq: 7, time: new Date(now - 2000).toISOString(), agent: "claude", model: "main", provider: "b", + ...(how === "answered" ? { done: true, status: 200, ms: 900 } : {}), tokens: 9000, + group: { id: "main", name: "Main", members: ["a/fast", "b/opus"] }, + order: [{ id: "b", provider: "b", name: "B", kind: "account", model: "opus", known: true, used: 10 }, + { id: "a", provider: "a", name: "A", kind: "account", model: "fast", known: true, used: 20 }], + tries: how === "answered" ? [{ id: "b", model: "opus", start: new Date(now - 2000).toISOString(), done: true, ms: 800, status: 200 }] : [], + rule: how === "tokens" + ? { n: 1, use: "b/opus", when: ["tokens ≥ 8000"], turn: 3, tokens: 9000 } + : { n: 1, use: "b/opus", when: ['intent "complex"'], turn: 3, tokens: 9000, + classified: { by: "c/cls", intents: ["complex"], intent: "complex", ms: 300 } }, +}); + +const words = { + en: { picked: "picked for “complex”", answered: "answered this request" }, + zh: { picked: "按意图“complex”选中", answered: "回答了这个请求" }, + "zh-TW": { picked: "按意圖「complex」選中", answered: "回答了這個請求" }, + ja: { picked: "インテント「complex」で選択", answered: "このリクエストに応答" }, + de: { picked: "für „complex“ gewählt", answered: "hat diese Anfrage beantwortet" }, +}; + +function serve(lang, route) { + const state = { agents: [{ id: "claude", name: "Claude Code", path: "/test/claude", fields: [] }], profiles: [], settings: { lang, theme: "light" } }; + let first = true; + 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:true};` }); + 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/gateway/trace") { + if (url.searchParams.get("wait")) return new Promise(() => {}); // nothing more comes + const routes = first ? [route] : []; + first = false; + return json({ mine: true, now: new Date().toISOString(), seq: 7, totals: { requests: 1, 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()); + 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 }); + }; +} + +async function open(t, engine, lang, width, how) { + const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" })); + t.after(() => browser.close()); + const page = await (await browser.newContext({ viewport: { width, height: 1400 } })).newPage(); + page.setDefaultTimeout(5000); + const errors = []; + page.on("pageerror", (e) => errors.push(e.message)); + await page.route("**/*", serve(lang, routeOf(how))); + await page.goto("http://magpie.test/?view=routing"); + await page.locator(".rt-group").first().waitFor(); + return { page, errors }; +} + +// what each account's row says, by its id (the row's tooltip begins with it) +const said = (page) => page.evaluate(() => Object.fromEntries([...document.querySelectorAll(".rt-accts li")] + .map((li) => [li.title.split("\n")[0], li.querySelector(":scope > em")?.textContent || ""]))); +const until = (page, f) => page.waitForFunction(f).catch(() => {}); + +for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) { + for (const lang of Object.keys(words)) { + const w = words[lang]; + for (const width of [1100, 390]) { + test(`${engine} ${lang} ${width}px: the row smart routing chose says the intent, not what it has left`, async (t) => { + const { page, errors } = await open(t, engine, lang, width, "waiting"); + await until(page, () => [...document.querySelectorAll(".rt-accts li")].some((li) => li.title.startsWith("a") && /\d/.test(li.querySelector(":scope > em")?.textContent || ""))); + await until(page, (p) => [...document.querySelectorAll(".rt-accts li em")].some((e) => e.textContent === p), w.picked); + const s = await said(page); + assert.equal(s.b, w.picked, `the chosen row: ${JSON.stringify(s)}`); + assert(/\d/.test(s.a) && !s.a.includes(w.picked), `the other row still says what it has left: ${JSON.stringify(s)}`); + const wide = await page.evaluate(() => document.documentElement.scrollWidth - document.documentElement.clientWidth); + assert(wide <= 0, `no sideways scroll: ${wide}px`); + assert.deepEqual(errors, []); + }); + } + test(`${engine} ${lang}: the row that answered says so, then the intent`, async (t) => { + const { page, errors } = await open(t, engine, lang, 1100, "answered"); + const want = `${w.answered} · ${w.picked}`; + await until(page, (p) => [...document.querySelectorAll(".rt-accts li em")].some((e) => e.textContent === p), want); + const s = await said(page); + assert.equal(s.b, want, JSON.stringify(s)); + assert.deepEqual(errors, []); + }); + test(`${engine} ${lang}: a rule by no intent leaves the row saying what it has left`, async (t) => { + const { page, errors } = await open(t, engine, lang, 1100, "tokens"); + await until(page, () => [...document.querySelectorAll(".rt-accts li")].filter((li) => /\d/.test(li.querySelector(":scope > em")?.textContent || "")).length >= 2); + const s = await said(page); + assert(/\d/.test(s.b) && !s.b.includes(w.picked), JSON.stringify(s)); + assert.deepEqual(errors, []); + }); + } +}