gui: a model picker's row says how fast its provider answered this week, fastest first under a search

axuhere on X: with several providers serving one model, the list and the
model picker should show each one's speed and sort by it. Each row of an
agent's model picker now carries a quiet tag from the usage ledger's last
seven days: tokens a second, or the first token's wait when only that was
timed (title: "first token in Xs, N tokens a second"). Under a search,
rows that match equally go fastest first by about how long a 200-token
reply takes, the untimed last. Without a search the rows keep catalog
order, grouped by provider. "Fastest first" for a routing group already
existed as Speed first (d9a8148d7); nothing added there.

- GET /api/model-speeds (internal/gui/speeds.go): usage.SummarizeTop(Week)
  models by provider/model, the catalog ref an option carries; a model
  whose calls went to more than one host is left out.
- app.js: readSpeeds (once a minute, on a model picker opening, re-filters
  keeping the cursor), speedRank, speedTag. Existing i18n keys only.
  app.js 1,304,233 under its 1,311,735 budget, no raise.
- menu-scroll's strict fixture answers /api/model-speeds.

Run, at this head (rebased on bb3565309) unless said:
- mote: go test -run 'Budget|TestModelSpeedsByRef' ./internal/gui ok;
  go vet ./internal/gui darwin ok, GOOS=windows ok.
- picker-speeds.test.cjs: Chromium 1000/440px x en, zh, zh-TW, ja, de 10/10
  on mote; WebKit 10/10 on the Linux box (wsl-run), because WebKit launch
  hangs on mote at present (many pw_run.sh from several sessions, 0 WebKit
  processes). i18n-keys 4/4.
- Broken by hand: speeds.go without TPS -> TestModelSpeedsByRef red;
  filter() without the speedRank tie-break -> picker-speeds red
  ("fastest first, the untimed last").
- 57 other GUI files that open a model picker, Chromium, at the pre-rebase
  tree, concurrency 4 on mote at load ~30-40: 185 pass, 180 fail. One was
  real (menu-scroll's "Unexpected API: /api/model-speeds", fixed here); the
  rest were 5000ms locator timeouts and 11 WebKit launch timeouts. A rerun
  of those files at concurrency 2 was still running at push.

Not tried: native Linux go vet (the WSL box lacks gtk4/webkitgtk dev
packages); the real app with a real ledger; the full GUI or Go suite.
This commit is contained in:
yetone
2026-10-29 17:09:13 +08:00
parent bb3565309d
commit 1198cff601
7 changed files with 236 additions and 3 deletions
+1 -1
View File
@@ -32,7 +32,7 @@ something else shows up as drift.
## Runtime path
1. **Connect.** The Agents page's switch calls `ConnectHow` (`POST /api/agents/{action}/{id}` in [`internal/gui/api.go`](../../internal/gui/api.go)). It stashes the current values (`<id>.connect.was`). It brings back the models used before a switch-off (`reconnect`), or calls `Join` where the agent keeps its own model (Codex signed in with ChatGPT). `reconnect` calls `Join` first too, so a Codex switched off on a routing group comes back joined, as the switch first connected it: put back on the group alone it was beside its sign-in only, and an own model picked on the Agents page took it off magpie (NancyTom18 on X, `TestCodexReconnectStaysJoined`). Otherwise it picks a model by the order `ConnectHow` documents and sets it through `Apply`.
2. **Pick.** `Pick` sets a field from the model picker. Picking one of magpie's models on an agent that isn't connected connects it first.
2. **Pick.** `Pick` sets a field from the model picker. Picking one of magpie's models on an agent that isn't connected connects it first. Each row of a model picker that a provider served with a timed reply this week says how fast: tokens a second, or the first token's wait when only that was timed (`speedTag` in app.js, from `GET /api/model-speeds`, `modelSpeeds` in [`internal/gui/speeds.go`](../../internal/gui/speeds.go): the usage ledger's mean TTFT and decode speed by `provider/model`, the catalog ref an option carries). Under a search, rows that match equally go fastest first by about how long a 200-token reply takes (`speedRank`), the untimed last, so a model several providers serve lists the fastest one first (axuhere on X). Without a search the rows keep catalog order, grouped by provider. A routing group that always asks the fastest provider is the Speed first strategy ([gateway routing](gateway-routing.md)).
3. **Notice.** After a change the page shows `Notice` when the agent reads its config only at start-up and needs a restart. A field that is `Live` (`Field.Live`: magpie's own setting, which the gateway reads on each request, Claude Desktop's `subagent_model` and `subagent_effort`) needs no restart, so `POST /api/set` and `magpie <agent> <field> <value>` leave the Notice out after it, unless the pick connected the agent too (`Pick` on an agent not connected). A stopped WSL distro's copy of a field keeps the flag (`asleep`).
4. **Drift.** As the Agents page is drawn, `Drift` compares the config with `applied.json`. It runs `Check` first, then each field. An agent pointed at magpie through a reverse proxy is still on magpie (#1985, davisjiahao: a company's nginx at `https://ai.corp.example/magpie`). `wiringOff`, where most agents' `Check` compares a URL, and the checks of their own (Claude Code's managed settings, Reasonix's provider) take a URL that isn't magpie's when it ends with magpie's path, the rest is an http or https address on another machine (`onAnotherMachine`) or one on this machine's loopback with a path of its own (`loopbackWithPath`: the reporter's compliance proxy at `http://127.0.0.1:8788/claude-code-anthropic/<route>`; `proxyBase`), and `GET <rest>/` there answers as the gateway's own `GET /` does (`{"name":"magpie"}`, `magpieAt` in [`reach.go`](../../internal/agent/reach.go), tried in the background like `reachProbe`). Something else there (a relay CC Switch put in) is `unwired`, as is an address on this machine's loopback without a path, which is magpie's on an older port and is never tried. Until the answer is known it isn't drift, so Reconnect isn't offered and doesn't write the proxy away; port moves (`OnGateway`, `Rewire`) re-set only `unwired` agents, so they leave it too. Codex keeps such an address by magpie's key, as for #816 (`codexKeptGateway`: http or https, any port, a path before magpie's, loopback with a path). A model picked in Claude Code keeps it too once magpie has answered there (`surelyMagpie`; `routed` and `gwAt` in `claude.go`), so a proxy every request must go through isn't bypassed; before that, or at a relay, a pick points Claude Code at this magpie. Tests stub `magpieAt` in `TestMain` as known and not magpie. When the config is right and points at an address off loopback (a WSL twin under NAT, or this machine's Codex at an address the user kept, #816), it tries that address ([`reach.go`](../../internal/agent/reach.go)): in the background, at most every 20 s, never waited for. When nothing answers there while the gateway answers on loopback, the drift is `unreachable` (#1013), with what has to listen there: Share on local network, the user's portproxy, Windows' firewall. Then it checks whether the agent's latest use (`LastUsed`, `Reached`) reached the gateway: a request Codex's log says went to the host its config keeps magpie at, or one the gateway saw from it after (`lastSeen`), reached it.
5. **Disconnect.** `Disconnect` puts back what the stash kept and forgets the record. It also marks the agent disconnected (`<id>.disconnected` in the stash, `Disconnected`) until the user connects it again (`connect`, or an `Apply` that leaves it wired): nothing magpie does by itself puts magpie back in meanwhile, neither a start's `Sync` nor Codex's account failover nor #701's magpie API wiring (paynezhuang on Discord: Codex had `openai_base_url` back after every update).
+1
View File
@@ -2480,6 +2480,7 @@ func Handler(w Windows, gw *gateway.Server) http.Handler {
rw.WriteHeader(http.StatusNoContent)
})
agentModelsAPI(mux)
speedsAPI(mux)
devListen(mux)
return noFramedAPI(held(mux))
}
+39 -1
View File
@@ -4954,6 +4954,7 @@ function openPicker(agent, field, anchor, ev, only) {
}
const modelPicker = (["model", "small", "large", MEMORIES, SUB_MODEL, AUTO_MODE, "executor", "planner", ...ROLE_MODELS].includes(field.label) || hermesBot(field)) && !only;
pick = { agent, field, options, groups, anchor, cursor: 0, free: !only && !field.menu, modelPicker, effortPicker, groupFilter: "all" };
if (modelPicker) readSpeeds();
anchor.classList.add("open");
const pop = $("#pop");
pop.classList.toggle("model-picker", modelPicker);
@@ -5203,7 +5204,9 @@ function filter(keep) {
else if (pick.modelPicker && pick.groupFilter !== "all") source = source.filter((o) => (o.group || o.home) === pick.groupFilter || o.reset);
const scored = source.map((o) => ({ o, i: pick.options.indexOf(o), s: score(q, o) })).filter((x) => x.s > 0);
// with a query, best matches first; without, catalog order keeps the groups together
if (q) scored.sort((a, b) => b.s - a.s || a.i - b.i);
// (and a model several providers serve, found by its name, fastest
// first: modelSpeeds, axuhere on X)
if (q) scored.sort((a, b) => b.s - a.s || (pick.modelPicker ? speedRank(a.o) - speedRank(b.o) : 0) || a.i - b.i);
pick.items = scored.map((x) => x.o);
const typed = $("#q").value.trim();
if (typed && pick.free && ["model", "small", "large", "executor", "planner", ...ROLE_MODELS].includes(pick.field.label) && !pick.items.some((o) => o.value === typed)) {
@@ -5444,6 +5447,39 @@ function pathTag(o) {
return tag;
}
// modelSpeeds: each catalog ref's speed this week from the usage ledger
// ({ttft ms, tps, n}, /api/model-speeds), read when a model picker opens,
// at most once a minute; a model several providers serve shows which of
// them answers it fastest (axuhere on X)
let modelSpeeds = {}, speedsRead = 0;
function readSpeeds() {
if (Date.now() - speedsRead < 60000) return;
speedsRead = Date.now();
api("model-speeds").then((s) => {
modelSpeeds = s || {};
if (pick?.modelPicker) { const o = pick.items?.[pick.cursor]; filter(o && ((x) => x === o)); }
}).catch(() => { speedsRead = 0; });
}
// speedRank: about how long a 200-token reply takes on o's provider, in
// seconds; Infinity when no reply was timed
function speedRank(o) {
const s = o.ref && modelSpeeds[o.ref];
if (!s || !(s.ttft || s.tps)) return Infinity;
return (s.ttft || 0) / 1000 + (s.tps ? 200 / s.tps : 0);
}
// speedTag: the quiet speed by a picker's model, tokens a second, or the
// first token's wait when only that was timed
function speedTag(o) {
const s = o.ref && modelSpeeds[o.ref];
if (!s || !(s.ttft || s.tps)) return null;
const secs = ((s.ttft || 0) / 1000).toFixed(1);
const tag = el("span", "badge ctx speed", s.tps ? t("{n} tok/s", { n: Math.round(s.tps) }) : secs + "s");
tag.title = s.tps && s.ttft ? t("first token in {s}s, {n} tokens a second", { s: secs, n: Math.round(s.tps) }) : s.ttft ? t("first token in {s}s", { s: secs }) : t("{n} tok/s", { n: Math.round(s.tps) });
return tag;
}
function renderList() {
const list = $("#list");
list.replaceChildren();
@@ -5469,6 +5505,8 @@ function renderList() {
if (rate) words.append(rate);
const ctx = contextTag(o.context, o.label);
if (ctx) words.append(ctx);
const speed = pick.modelPicker && speedTag(o);
if (speed) words.append(speed);
// an Aside account's note is its e-mail, or Aside's "Local Account",
// then whether Aside has it active: the words are magpie's, the e-mail not
let note = o.note && o.note !== (o.label || o.value) ? (own ? t(o.note) : asideAccount(pick.agent, pick.field) ? o.note.split(" · ").map((w) => t(w)).join(" · ") : o.note) : "";
+44
View File
@@ -0,0 +1,44 @@
package gui
import (
"net/http"
"github.com/yetone/magpie/internal/usage"
)
// speedJSON is how fast a model answered on one provider over the last
// seven days, from the usage ledger: the mean time to its first token and
// how fast it wrote after it, the numbers the Requests page shows per call.
type speedJSON struct {
TTFT int64 `json:"ttft,omitempty"` // ms
TPS float64 `json:"tps,omitempty"` // output tokens a second after the first
N int `json:"n"` // the timed replies they are of
}
// modelSpeeds is speedJSON by catalog ref ("provider/model", an option's
// ref in an agent's model picker), for the models with a timed reply this
// week. The model picker says it by each row, so a model several providers
// serve shows which of them answers it fastest (axuhere on X). It reads
// the summary the Usage page reads, cached, never the log on its own.
func modelSpeeds() map[string]speedJSON {
out := map[string]speedJSON{}
for _, g := range usage.SummarizeTop(usage.Week, 0).Models {
// a model whose calls went to more than one place is told apart
// by Host, and its numbers aren't the provider's of today
if g.Host != "" || g.Provider == "" || g.Model == "" || g.Timed == 0 {
continue
}
s := speedJSON{TTFT: g.MeanTTFT(), TPS: g.Speed(), N: g.Timed}
if s.TTFT <= 0 && s.TPS <= 0 {
continue
}
out[g.Provider+"/"+g.Model] = s
}
return out
}
func speedsAPI(mux *http.ServeMux) {
mux.HandleFunc("GET /api/model-speeds", func(rw http.ResponseWriter, r *http.Request) {
writeJSON(rw, modelSpeeds())
})
}
+55
View File
@@ -0,0 +1,55 @@
package gui
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/yetone/magpie/internal/usage"
)
// /api/model-speeds says how fast each provider answered a model this
// week, keyed by the ref an agent's model picker gives the row (axuhere on
// X: one model from several providers, at very different speeds). A model
// with no timed reply, and one older than the week, say nothing.
func TestModelSpeedsByRef(t *testing.T) {
sandboxHome(t)
now := time.Now()
for _, r := range []usage.Record{
// 200 tokens written in 2s after a 400ms first token: 100 tok/s
{Time: now.Add(-time.Hour), Provider: "quick", Model: "glm-5", Output: 200, Millis: 2400, TTFT: 400, Status: 200},
{Time: now.Add(-time.Hour), Provider: "quick", Model: "glm-5", Output: 200, Millis: 2600, TTFT: 600, Status: 200},
// 200 tokens in 10s after 3s: 20 tok/s
{Time: now.Add(-time.Hour), Provider: "slow", Model: "glm-5", Output: 200, Millis: 13000, TTFT: 3000, Status: 200},
// not streamed: no first token, no speed
{Time: now.Add(-time.Hour), Provider: "plain", Model: "glm-5", Output: 200, Millis: 5000, Status: 200},
// before the week
{Time: now.AddDate(0, 0, -9), Provider: "old", Model: "glm-5", Output: 200, Millis: 2400, TTFT: 400, Status: 200},
} {
usage.Append(r)
}
mux := http.NewServeMux()
speedsAPI(mux)
w := httptest.NewRecorder()
mux.ServeHTTP(w, httptest.NewRequest("GET", "/api/model-speeds", nil))
if w.Code != 200 {
t.Fatalf("%d %s", w.Code, w.Body)
}
var got map[string]speedJSON
if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil {
t.Fatal(err)
}
if q := got["quick/glm-5"]; q.TTFT != 500 || q.TPS < 99 || q.TPS > 101 || q.N != 2 {
t.Errorf("quick/glm-5 = %+v; want 500ms, 100 tok/s of 2", q)
}
if s := got["slow/glm-5"]; s.TTFT != 3000 || s.TPS < 19 || s.TPS > 21 {
t.Errorf("slow/glm-5 = %+v; want 3000ms, 20 tok/s", s)
}
for _, k := range []string{"plain/glm-5", "old/glm-5"} {
if s, ok := got[k]; ok {
t.Errorf("%s says %+v; want nothing", k, s)
}
}
}
+1 -1
View File
@@ -40,7 +40,7 @@ async function serve(route) {
if (url.pathname === "/api/gateway/trace") return json({ mine: false, now: date, totals: { requests: 0, rerouted: 0, errors: 0 } });
if (url.pathname === "/api/plugins/updates") return json({ waiting: [] });
if (url.pathname === "/api/usage/quotas" || url.pathname === "/api/usage/quotas/history" || url.pathname === "/api/agents/install" || url.pathname === "/api/plugins/agents") return json([]);
if (url.pathname === "/api/whatsnew") return json({});
if (url.pathname === "/api/whatsnew" || url.pathname === "/api/model-speeds") return json({});
if (url.pathname === "/api/upstream") return json({ vendors: [], providers: {} });
// the sidebar's plugin pages, and plugin panels in magpie's pages: none
if (url.pathname === "/api/plugin-pages" || url.pathname === "/api/plugin-panels") return json([]);
+95
View File
@@ -0,0 +1,95 @@
// Run with Node's test runner and Playwright on the module path; see README.md.
// A model several providers serve shows, by each provider's row in an
// agent's model picker, how fast it answered this week (/api/model-speeds,
// from the usage ledger), and a search for it lists the fastest first:
// axuhere on X asked for the speed beside each provider and a sort by it.
// A provider with no timed reply has no tag and comes last. In Chromium and
// WebKit, at a narrow window, in every language.
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 row = (group, icon, id, label) => ({ value: id, ref: id, label, group, icon });
const options = [
row("Z.ai", "zai", "zai/glm-5", "GLM-5"),
row("OpenRouter", "openrouter", "openrouter/glm-5", "GLM-5"),
row("SiliconFlow", "siliconcloud-color", "siliconflow/glm-5", "GLM-5"),
row("Kimi", "kimi", "kimi/k3", "Kimi K3"),
];
const speeds = {
"zai/glm-5": { ttft: 3000, tps: 20, n: 4 },
"siliconflow/glm-5": { ttft: 500, tps: 120, n: 9 },
"kimi/k3": { ttft: 1500, n: 1 },
};
const said = {
en: ["120 tok/s", "first token in 0.5s, 120 tokens a second", "first token in 1.5s"],
zh: ["120 token/秒", "首字 0.5 秒,每秒 120 个 token", "首字 1.5 秒"],
"zh-TW": ["120 token/秒", "首字 0.5 秒,每秒 120 個 token", "首字 1.5 秒"],
ja: ["120 トークン/秒", "最初のトークンまで 0.5 秒、毎秒 120 トークン", "最初のトークンまで 1.5 秒"],
de: ["120 Token/s", "erstes Token nach 0.5 s, 120 Tokens pro Sekunde", "erstes Token nach 1.5 s"],
};
function serve(lang) {
return async (route) => {
const url = new URL(route.request().url());
if (url.pathname === "/boot.js") return route.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:${JSON.stringify(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 route.fulfill({ json: { agents: [{ id: "codex", name: "Codex", path: "/test/config.toml", icon: "codex-color", wired: false, fields: [{ key: "model", label: "model", value: "kimi/k3", options }] }], profiles: [], settings: { lang, theme: "light" } } });
if (url.pathname === "/api/model-speeds") return route.fulfill({ json: speeds });
if (url.pathname === "/api/agents/cli") return route.fulfill({ json: { agents: {}, pending: false } });
if (url.pathname === "/api/groups") return route.fulfill({ json: { groups: [] } });
if (url.pathname === "/api/usage/quotas") return route.fulfill({ json: [] });
if (url.pathname.startsWith("/api/")) return route.fulfill({ 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), contentType });
};
}
const rows = (page) => page.evaluate(() => [...document.querySelectorAll("#list li:not(.group):not(.custom)")].map((li) => {
const tag = li.querySelector(".badge.speed");
const r = li.getBoundingClientRect(), b = tag?.getBoundingClientRect();
return { ref: li.querySelector(".n")?.textContent || li.querySelector(".v")?.textContent, text: li.textContent, tag: tag?.textContent || "", title: tag?.title || "", inside: !b || (b.width > 0 && b.left >= r.left && b.right <= r.right + 0.5) };
}));
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const width of [1000, 440]) {
for (const lang of Object.keys(said)) {
test(`${engine} ${width}px ${lang}: each provider's row says its speed, fastest first under a search`, async () => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
try {
const page = await browser.newPage({ viewport: { width, height: 760 } });
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
await page.route("http://magpie.test/**", serve(lang));
await page.goto("http://magpie.test/");
await page.locator('#nav [data-view="agents"]').click();
await page.locator('#agents [data-key="model"]').first().click();
await page.locator("#list .badge.speed").first().waitFor();
const [tps, both, first] = said[lang];
let all = await rows(page);
const k3 = all.find((r) => r.text.includes("Kimi K3"));
assert.equal(k3.tag, "1.5s", "only the first token timed: its wait");
assert.equal(k3.title, first);
await page.locator("#q").fill("glm-5");
// (with the "use as typed" row after them, left out here)
await page.waitForFunction(() => document.querySelectorAll("#list li:not(.group):not(.custom)").length === 3);
all = await rows(page);
// a search lists each row's provider as its note
assert.deepEqual(all.map((r) => r.ref), ["SiliconFlow", "Z.ai", "OpenRouter"], "fastest first, the untimed last");
assert.equal(all[0].tag, tps);
assert.equal(all[0].title, both);
assert.equal(all[2].tag, "", "no tag without a timed reply");
assert(all.every((r) => r.inside), `each tag inside its row: ${JSON.stringify(all)}`);
assert.deepEqual(errors, [], "page runtime errors");
} finally {
await browser.close();
}
});
}
}
}