diff --git a/internal/gateway/history.go b/internal/gateway/history.go index 50617a056..b5e24f515 100644 --- a/internal/gateway/history.go +++ b/internal/gateway/history.go @@ -424,8 +424,15 @@ func tally(d dayFile, c counted) counted { // was one answered under Google's name for the deployment serving its // Gemini model (gemini-3.8-flash as gemini-3.8-flash-n) or xAI's for a Grok // one (grok-4.7 as grok-4.7-build), usage.ServingName, nor one answered -// under the name its provider's list gives it (usage.ListedAs). +// under the name its provider's list gives it (usage.ListedAs). Its +// agent is named again as usage.AgentOf names it now, as the ledger's +// records are: Kimi Code's "kimi-code-cli", kept before AgentOf knew it, +// is Kimi Code ("kimi") on the Routing page and the Context tab too, its +// icon and name with it, not a card of its own drawn as the cube (jiabubu). func (r *Route) routedAgain() { + if r.Agent != "" { + r.Agent = usage.AgentOf(r.Agent) + } for i := range r.Tries { if tr := &r.Tries[i]; tr.Swapped && usage.GroupRouted(tr.Model, tr.Served) { tr.Swapped, tr.Routed = false, true diff --git a/internal/gui/history_agent_test.go b/internal/gui/history_agent_test.go new file mode 100644 index 000000000..2cb56d8c0 --- /dev/null +++ b/internal/gui/history_agent_test.go @@ -0,0 +1,79 @@ +package gui + +import ( + "bytes" + "encoding/json" + "os" + "path/filepath" + "testing" + "time" + + "github.com/yetone/magpie/internal/gateway" +) + +// A route kept before usage.AgentOf knew Kimi Code 2.x's User-Agent +// ("kimi-code-cli/…") carries that raw name. The ledger names its records +// again as it reads them, so the Usage page's request list showed Kimi +// Code; the Routing page's request list and the Context tab's agent cards +// read the kept routes and drew a "kimi-code-cli" of their own with the +// cube (jiabubu on Discord). Both read the routes as AgentOf names them +// now: "kimi", Kimi Code's id, which /api/state's clients give its icon. +func TestKeptRoutesNameTheirAgentAgain(t *testing.T) { + t.Setenv("XDG_CONFIG_HOME", t.TempDir()) + t.Setenv("XDG_CACHE_HOME", t.TempDir()) + mux := keptMux(t) + traceRoutes(mux) + ctxKept.Lock() + ctxKept.days, ctxKept.key = map[string]ctxDay{}, "" + ctxKept.Unlock() + + dir := gateway.HistoryDir() + if err := os.MkdirAll(dir, 0o700); err != nil { + t.Fatal(err) + } + now := time.Now() + day := now.Format("2006-01-02") + var b bytes.Buffer + for i, agent := range []string{"kimi-code-cli", "kimi", "kimi-code-desktop", "claude-desktop", "Python-urllib"} { + r := ctxRoute(int64(i+1), agent, "s", 30000, 1000, 200000, 0) + r.Time = now.Add(-time.Duration(5-i) * time.Minute) + j, _ := json.Marshal(r) + b.Write(append(j, '\n')) + } + if err := os.WriteFile(filepath.Join(dir, day+".jsonl"), b.Bytes(), 0o600); err != nil { + t.Fatal(err) + } + + var hist struct { + Routes []gateway.Route `json:"routes"` + } + getJSON(t, mux, "/api/gateway/history?day="+day, &hist) + var got []string + for _, r := range hist.Routes { + got = append(got, r.Agent) + } + want := []string{"kimi", "kimi", "kimi-code-desktop", "claude-desktop", "Python-urllib"} + if len(got) != len(want) { + t.Fatalf("history's agents %v, want %v", got, want) + } + for i := range want { + if got[i] != want[i] { + t.Fatalf("history's agents %v, want %v", got, want) + } + } + + var ctx ctxJSON + getJSON(t, mux, "/api/context?days=1", &ctx) + cards := map[string]int{} + for _, a := range ctx.Agents { + cards[a.Agent] = a.Calls + } + if cards["kimi-code-cli"] != 0 || cards["kimi"] != 2 { + t.Fatalf("the Context tab's agent cards: %v, want kimi's two requests on one card", cards) + } + for _, s := range ctx.Sessions { + if s.Agent == "kimi-code-cli" { + t.Fatalf("a session under the raw name: %+v", s) + } + } +}