Files
magpie/internal/gui/speeds.go
T
yetone 1198cff601 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.
2026-10-29 17:09:13 +08:00

45 lines
1.5 KiB
Go

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())
})
}