diff --git a/internal/agent/agents.go b/internal/agent/agents.go index 93413d4c9..9aa79e64d 100644 --- a/internal/agent/agents.go +++ b/internal/agent/agents.go @@ -150,6 +150,11 @@ var others = []*Agent{ // ("pi-desktop/0.1.0"), it is told apart from Pi by that name, which // Pi's "pi-" would take otherwise (#1966, dgmico) {ID: "pi-desktop", Name: "PI-Desktop", Icon: "pi", UA: []string{"pi-desktop"}}, + // Kimi Code Desktop runs Kimi Code's engine on ~/.kimi-code, so it + // reaches magpie through Kimi Code's config, under a client name of + // its own ("kimi-code-desktop/…", jiabubu on Discord). Its icon is the + // app's own, from KimiCode-1.2.1-win-x64.exe's build/icon.ico. + {ID: "kimi-code-desktop", Name: "Kimi Code Desktop", Icon: "kimi-code-desktop", UA: []string{"kimi-code-desktop"}}, // Raft (slock's raft-computer) runs Pi in each of its agents' folders; // Sessions lists those as Raft's (internal/sessions/raft.go), while // their requests say "pi (…)" and are Pi's diff --git a/internal/agent/kimi.go b/internal/agent/kimi.go index 3a0028888..43db92b40 100644 --- a/internal/agent/kimi.go +++ b/internal/agent/kimi.go @@ -295,7 +295,9 @@ func kimiIn(at place) *Agent { } return atomic(&Agent{ ID: "kimi", Name: "Kimi Code", Icon: "kimi", Aliases: []string{"kimi-code", "kimi-cli"}, Spelled: prefixed, - UA: []string{"kimicli"}, + // kimi-cli (Python) says KimiCLI/…; Kimi Code 2.x, kimi-code-cli/… + // (jiabubu on Discord: its requests showed the raw name) + UA: []string{"kimicli", "kimi-code-cli"}, Bin: "kimi", Dir: dir, Path: path, ListOnly: true, Sync: func() error { diff --git a/internal/agent/useragent_test.go b/internal/agent/useragent_test.go index ae8e1e4a0..dd85ae17a 100644 --- a/internal/agent/useragent_test.go +++ b/internal/agent/useragent_test.go @@ -33,6 +33,11 @@ func TestAgentOf(t *testing.T) { "minimax-code": "minimax-code", // what magpie's entry sends "MiniMaxCode": "minimax-code", // 0.6.8's own (#1605) "": "other", + // Kimi Code 2.x and its desktop app, as jiabubu's requests named + // them; the old Python kimi-cli's KimiCLI/ + "kimi-code-cli/2.4.1": "kimi", + "kimi-code-desktop/1.2.1": "kimi-code-desktop", + "KimiCLI/1.4.0": "kimi", } for ua, want := range cases { if got := usage.AgentOf(ua); got != want { diff --git a/internal/gui/assets/app.css b/internal/gui/assets/app.css index 40eb7948a..4cedc5a2b 100644 --- a/internal/gui/assets/app.css +++ b/internal/gui/assets/app.css @@ -174,7 +174,7 @@ code { font: var(--code-style, normal) var(--code-weight, 400) 11.5px var(--code they read a size bigger than a line logo beside them, so they are drawn at the size Alma's and Cindy's tiles have of their own. icon-tile.test.cjs measures every file in icons/ and keeps this list whole. */ -.ic:is([data-icon="aihubmix-color"], [data-icon="atomcode"], [data-icon="cherryin-color"], [data-icon="codebuddy-color"], [data-icon="codex-color"], [data-icon="crush"], [data-icon="geminicli-color"], [data-icon="hanako"], [data-icon="hunyuan-color"], [data-icon="kilo"], [data-icon="kiro-color"], [data-icon="lody"], [data-icon="mistermorph-color"], [data-icon="mlx-serve"], [data-icon="monica-color"], [data-icon="penguin-color"], [data-icon="phala-color"], [data-icon="pipellm-color"], [data-icon="qoder"], [data-icon="snow"], [data-icon="targon-color"], [data-icon="typesafe"], [data-icon="workbuddy-color"], [data-icon="yuanbao-color"], [data-icon="zcode"], [data-icon="zoo-code"]) > * { width: 82%; height: 82%; } +.ic:is([data-icon="aihubmix-color"], [data-icon="atomcode"], [data-icon="cherryin-color"], [data-icon="codebuddy-color"], [data-icon="codex-color"], [data-icon="crush"], [data-icon="geminicli-color"], [data-icon="hanako"], [data-icon="hunyuan-color"], [data-icon="kilo"], [data-icon="kimi-code-desktop"], [data-icon="kiro-color"], [data-icon="lody"], [data-icon="mistermorph-color"], [data-icon="mlx-serve"], [data-icon="monica-color"], [data-icon="penguin-color"], [data-icon="phala-color"], [data-icon="pipellm-color"], [data-icon="qoder"], [data-icon="snow"], [data-icon="targon-color"], [data-icon="typesafe"], [data-icon="workbuddy-color"], [data-icon="yuanbao-color"], [data-icon="zcode"], [data-icon="zoo-code"]) > * { width: 82%; height: 82%; } .ic.generic { color: var(--muted); } /* maskSpan's shelf: a hidden user of every mask picture loaded, so a logo drawn again paints at once (#1847) */ .mask-shelf { position: fixed; top: 0; left: 0; width: 0; height: 0; overflow: hidden; visibility: hidden; pointer-events: none; } diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js index 8916e6b02..e929f1d8d 100644 --- a/internal/gui/assets/app.js +++ b/internal/gui/assets/app.js @@ -327,11 +327,10 @@ function maskSpan(src, failed) { if (failed) load.then((ok) => ok || failed()); return m; } -const pngIcons = new Set(["crush", "zcode", "alma", "hanako", "cindy", "lody", "typesafe", "atomcode", "snow", "mlx-serve", "mothx", "zoo-code"]); +const pngIcons = new Set(["crush", "zcode", "alma", "hanako", "cindy", "lody", "typesafe", "atomcode", "snow", "mlx-serve", "mothx", "zoo-code", "kimi-code-desktop"]); // lazy: a picture in a long list (Plugins' rows) is asked for near the -// screen. Each SVG an draws is a webview document, ~0.6 MB, kept -// while the page is open (#909) +// screen: each SVG an draws is a ~0.6 MB webview document, kept (#909) function icon(name, lazy) { const kept = keptIcons?.get(name || "")?.shift(); if (kept) { kept.removeAttribute("title"); return kept; } diff --git a/internal/gui/assets/icons/kimi-code-desktop.png b/internal/gui/assets/icons/kimi-code-desktop.png new file mode 100644 index 000000000..2adae5e01 Binary files /dev/null and b/internal/gui/assets/icons/kimi-code-desktop.png differ diff --git a/internal/gui/client_icons_test.go b/internal/gui/client_icons_test.go new file mode 100644 index 000000000..e88dce829 --- /dev/null +++ b/internal/gui/client_icons_test.go @@ -0,0 +1,77 @@ +package gui + +import ( + "os" + "regexp" + "strings" + "testing" + + "github.com/yetone/magpie/internal/agent" + "github.com/yetone/magpie/internal/gateway" +) + +// genericIcon are the clients magpie has no logo for yet, drawn with the +// generic cube on purpose (#4487). Each leaves this list when its real +// logo, from the app or its vendor, is added. +var genericIcon = map[string]bool{"ante": true, "pencil": true, "openpencil": true, "newmax": true, "kirocrew": true, "raft": true} + +// Every client a request can be recorded as is drawn by its own name and +// icon in the Routing page's request list, Usage and Sessions, which all +// look the request's client up in /api/state's clients (agent.Clients): +// one not there shows its raw id and the cube (jiabubu on Discord: +// kimi-code-cli and kimi-code-desktop). This holds the table to the ids +// the gateway records, and each entry's icon to a file app.js's icon() +// loads: a PNG only when pngIcons names it, else an SVG. +func TestEveryClientHasNameAndIcon(t *testing.T) { + js, err := os.ReadFile("assets/app.js") + if err != nil { + t.Fatal(err) + } + m := regexp.MustCompile(`const pngIcons = new Set\(\[([^\]]*)\]\)`).FindSubmatch(js) + if m == nil { + t.Fatal("app.js's pngIcons isn't found") + } + png := map[string]bool{} + for _, q := range regexp.MustCompile(`"([^"]+)"`).FindAllSubmatch(m[1], -1) { + png[string(q[1])] = true + } + exists := func(f string) bool { _, err := os.Stat("assets/icons/" + f); return err == nil } + + clients := map[string]bool{} + for _, a := range agent.Clients() { + clients[a.ID] = true + if strings.TrimSpace(a.Name) == "" { + t.Errorf("%s has no name", a.ID) + } + switch { + case a.Icon == "generic": + if !genericIcon[a.ID] { + t.Errorf("%s is drawn with the generic cube: give it its real logo", a.ID) + } + case a.Icon == "" || strings.HasPrefix(a.Icon, "file:") || strings.ContainsAny(a.Icon, ":/"): + t.Errorf("%s's icon %q isn't one of magpie's", a.ID, a.Icon) + case png[a.Icon]: + if !exists(a.Icon + ".png") { + t.Errorf("%s's icon: pngIcons names %s, with no icons/%[2]s.png", a.ID, a.Icon) + } + case !exists(a.Icon + ".svg"): + if exists(a.Icon + ".png") { + t.Errorf("%s's icon %s is a PNG app.js's pngIcons doesn't name, so it is drawn as the cube", a.ID, a.Icon) + } else { + t.Errorf("%s's icon %s has no file in icons/", a.ID, a.Icon) + } + } + } + for id := range genericIcon { + if !clients[id] { + t.Errorf("genericIcon names %s, which is no client", id) + } + } + // the ids the gateway gives a request by more than its User-Agent + // (gateway.agentOf), and those it names by one: each is a client + for _, id := range []string{"claude-desktop", gateway.ScienceID, "cursor-local", "codex", "magpie", "kimi", "kimi-code-desktop"} { + if !clients[id] { + t.Errorf("the gateway records requests as %s, which isn't among the clients", id) + } + } +} diff --git a/internal/gui/tests/routing-client-icons.test.cjs b/internal/gui/tests/routing-client-icons.test.cjs new file mode 100644 index 000000000..6d2860a37 --- /dev/null +++ b/internal/gui/tests/routing-client-icons.test.cjs @@ -0,0 +1,102 @@ +// Run with Node's test runner and Playwright on the module path; see README.md. +// The Routing page's request list draws each request's client by its own +// icon and name, from /api/state's clients (agent.Clients): Kimi Code and +// Kimi Code Desktop showed the cube and their raw ids, kimi-code-cli and +// kimi-code-desktop (jiabubu on Discord). Kimi Code Desktop's logo is a +// PNG, which icon() draws only when app.js's pngIcons names it; one it +// doesn't is asked for as an SVG mask and falls back to the cube. +// TestEveryClientHasNameAndIcon holds the Go table to the files. +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 day = [now.getFullYear(), now.getMonth() + 1, now.getDate()].map((n) => String(n).padStart(2, "0")).join("-"); +const at = (i) => new Date(now.getTime() - (i + 1) * 60e3).toISOString(); +// as agent.Clients() gives them +const clients = [ + { id: "kimi", name: "Kimi Code", icon: "kimi" }, + { id: "kimi-code-desktop", name: "Kimi Code Desktop", icon: "kimi-code-desktop" }, + { id: "workbuddy", name: "WorkBuddy", icon: "workbuddy-color" }, +]; +const routes = clients.map((c, i) => ({ + id: 100 + i, seq: 100 + i, time: at(i), agent: c.id, model: "kimi-k3", provider: "kimi-code", + order: [], tries: [], done: true, status: 200, ms: 900, tokens: 100, +})); + +function serve(lang) { + const state = { agents: [{ id: "kimi", name: "Kimi Code", icon: "kimi", path: "/test/config.toml", fields: [] }], clients, profiles: [], settings: { lang, theme: "light" } }; + return async (route) => { + const url = new URL(route.request().url()); + const json = (data) => route.fulfill({ json: data }); + if (url.pathname === "/boot.js") return route.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${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 json(state); + if (url.pathname === "/api/gateway/trace") { + if (url.searchParams.get("wait")) await new Promise((r) => setTimeout(r, 20e3)); + return json({ mine: true, now: now.toISOString(), seq: 1, totals: { requests: 0, rerouted: 0, errors: 0 }, routes: [] }); + } + if (url.pathname === "/api/gateway/history") { + const d = url.searchParams.get("day"); + return json({ cut: false, days: [{ day, requests: routes.length }], routes: d ? routes : [] }); + } + if (url.pathname === "/api/groups") return json({ groups: [] }); + if (url.pathname === "/api/providers") return json({ providers: [], gateway: { running: true, window: true } }); + 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)]; + try { + await route.fulfill({ body: await fs.readFile(file), contentType }); + } catch { + await route.fulfill({ status: 404, body: "" }); + } + }; +} + +for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) { + for (const lang of ["en", "zh"]) { + test(`${engine} ${lang}: a request's client is drawn by its own icon and name`, async (t) => { + const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" })); + const context = await browser.newContext({ viewport: { width: 1100, height: 760 }, 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)); + t.after(async () => { + if (process.env.ARTIFACT_DIR) { + await fs.mkdir(process.env.ARTIFACT_DIR, { recursive: true }); + await page.screenshot({ path: path.join(process.env.ARTIFACT_DIR, `${engine}-${lang}-client-icons.png`) }); + } + await browser.close(); + }); + await page.goto("http://magpie.test/?view=routing"); + await page.locator(".rt-days .rt-day").nth(1).click(); + await page.locator(".rt-req").nth(clients.length - 1).waitFor(); + // every picture asked for has come, or failed + await page.waitForFunction(() => [...document.querySelectorAll(".rt-req .asked .ic img")].every((i) => i.complete)); + + const rows = await page.locator(".rt-req").evaluateAll((bs) => bs.map((b) => { + const ic = b.querySelector(".asked .ic"), img = ic?.querySelector("img"); + return { title: b.title, icon: ic?.dataset.icon, generic: !!ic?.classList.contains("generic") || ic?.dataset.icon === "generic", src: img?.getAttribute("src") || "", loaded: !!img && img.naturalWidth > 0 }; + })); + for (const c of clients) { + const r = rows.find((x) => x.title.startsWith(c.name + " · ")); + assert(r, `${c.name}: ${JSON.stringify(rows)}`); + assert.equal(r.icon, c.icon, JSON.stringify(r)); + assert(!r.generic, JSON.stringify(r)); + } + // its own logo, the PNG, drawn + const desk = rows.find((x) => x.title.startsWith("Kimi Code Desktop · ")); + assert.equal(desk.src, "icons/kimi-code-desktop.png", JSON.stringify(desk)); + assert(desk.loaded, JSON.stringify(desk)); + // no raw client id stands for a name + assert(!rows.some((x) => /^kimi-code-(cli|desktop) · /.test(x.title)), JSON.stringify(rows)); + assert.deepEqual(errors, []); + }); + } +}