diff --git a/internal/gui/assets/plugins/library/client.js b/internal/gui/assets/plugins/library/client.js index 243040900..3f6c35142 100644 --- a/internal/gui/assets/plugins/library/client.js +++ b/internal/gui/assets/plugins/library/client.js @@ -1479,7 +1479,7 @@ magpie.plugin("library", (ctx) => { const sub = el("div", "sub mono", serverLine(s)); sub.title = serverLine(s); who.append(sub, toolsEl(s.name)); - row.append(mark(s.icon, s.transport === "stdio" ? GLYPH.cmd : GLYPH.web), who, + row.append(mark(s.icon, s.transport === "stdio" ? GLYPH.cmd : GLYPH.web), who, ...removeAct(() => confirmRemoveServer(s)), agentChips(withGone(all, s.agents), s.agents, chipsChange("servers/agents", s.name, "servers", (x) => serverRow(x, all)), { problems: s.problems, blocked: sseBlocked(s), all: true })); if (picking) return pickRow(row, s.name); row.onclick = () => editServer(s); @@ -1487,6 +1487,56 @@ magpie.plugin("library", (ctx) => { return row; } + // A server's or a hook's own Remove on its row, as a skill's has (#2364, + // szoxidy: a server turned off for every agent stayed in the list, its + // only Remove inside the editor its row opens). It asks first in the + // page, naming the one it takes and what goes with it. While picking, + // the bar's Remove is the one, and the row keeps the room it had. + function removeAct(run) { + if (picking) return []; + const acts = el("div", "lib-rowacts"); + const rm = button("", "lib-icon danger lib-rm", run); + rm.append(svg(GLYPH.trash, 13, 1.4)); + rm.title = t("Remove"); + acts.append(rm); + return [acts]; + } + function confirmRemoveOne(kind, name, title, lines) { + const ed = el("div", "editor lib-editor lib-rmone"); + const head = el("div", "ehead"); + head.append(glyph(GLYPH.trash), el("b", "", title)); + ed.append(head); + for (const l of lines) if (l) ed.append(el("p", "lib-confirm", l)); + const bar = el("div", "bar"); + const go = button(t("Remove"), "primary danger-fill", async () => { + go.disabled = true; + if (await change(kind + "/remove", { name }, t("{name} removed", { name }))) closeLibModal(true); + else go.disabled = false; + }); + bar.append(el("span", "grow"), button(t("Cancel"), "", closeLibModal), go); + ed.append(bar); + modal = { save: () => go.click() }; + openLib(ed); + } + const takenFromOne = (x) => x.agents?.length + ? t("It is taken out of the library and out of {agents}.", { agents: x.agents.map(nameOf).join(", ") }) + : t("It is taken out of the library; no agent has it."); + function confirmRemoveServer(s) { + const projects = (lib.projects || []).filter((p) => p.servers?.[s.name]?.length).length; + confirmRemoveOne("servers", s.name, t("Remove the MCP server {name}?", { name: s.name }), [ + takenFromOne(s), + projects && t("It is taken out of the {n} projects magpie placed it in too.", { n: projects }), + s.signIn?.signedIn && t("magpie's sign-in to it is forgotten too."), + t("Servers your agents have that aren't in the library stay as they are."), + ]); + } + function confirmRemoveHook(h) { + confirmRemoveOne("hooks", h.name, t("Remove the hook {name}?", { name: h.name }), [ + takenFromOne(h), + t("Hooks your agents have that aren't in the library stay as they are, and so does one you changed in an agent's file."), + ]); + } + // Whether a server works, as magpie found by connecting to it: started // (or reached) and asked for its tools. Its row shows it as a dot and a // few words, the whole reason in its tooltip; a click checks it again. @@ -2265,7 +2315,7 @@ magpie.plugin("library", (ctx) => { const cmd = el("div", "sub mono lib-hookcmd", h.command); cmd.title = h.command; who.append(cmd); - row.append(glyph(GLYPH.hook), who, + row.append(glyph(GLYPH.hook), who, ...removeAct(() => confirmRemoveHook(h)), agentChips(withGone(all, h.agents), h.agents, chipsChange("hooks/agents", h.name, "hooks", (x) => hookRow(x, all)), { problems: h.problems, blocked: hookBlocked(h), all: true })); if (picking) return pickRow(row, h.name); row.onclick = () => editHook(h); diff --git a/internal/gui/assets/plugins/library/i18n.js b/internal/gui/assets/plugins/library/i18n.js index 1b26f97be..999583f1a 100644 --- a/internal/gui/assets/plugins/library/i18n.js +++ b/internal/gui/assets/plugins/library/i18n.js @@ -269,6 +269,12 @@ magpie.strings({ "Remove all {n} skills?": "删除全部 {n} 个技能?", "They are taken out of the library and out of {agents}.": "它们会从资源库中删除,并从 {agents} 中移除。", "They are taken out of the library; no agent has any of them.": "将从资源库中删除;没有 Agent 启用它们。", + "Remove the MCP server {name}?": "删除 MCP 服务器 {name}?", + "Remove the hook {name}?": "删除钩子 {name}?", + "It is taken out of the library and out of {agents}.": "它会从资源库中删除,并从 {agents} 中移除。", + "It is taken out of the library; no agent has it.": "它会从资源库中删除;目前没有 Agent 启用它。", + "It is taken out of the {n} projects magpie placed it in too.": "magpie 放进 {n} 个项目里的它也会一并移除。", + "magpie's sign-in to it is forgotten too.": "magpie 对它的登录也会一并清除。", "The skills magpie placed in {n} projects are taken away too.": "magpie 放进 {n} 个项目的这些技能也会一并移除。", "{n} folders are moved to magpie's backups (Backups, at the top of the Library), not deleted.": "{n} 个技能文件夹将移到 magpie 的备份(资源库顶部的「备份」),不会删除。", "{n} linked from folders of your own are only unlinked: those folders stay where they are.": "{n} 个链接自你文件夹的技能只取消链接,原文件夹保留。", @@ -836,6 +842,12 @@ magpie.strings({ "Remove all {n} skills?": "刪除全部 {n} 個技能?", "They are taken out of the library and out of {agents}.": "它們會從資源庫中刪除,並從 {agents} 中移除。", "They are taken out of the library; no agent has any of them.": "將從資源庫中刪除;沒有 Agent 啟用它們。", + "Remove the MCP server {name}?": "刪除 MCP 伺服器 {name}?", + "Remove the hook {name}?": "刪除鉤子 {name}?", + "It is taken out of the library and out of {agents}.": "它會從資源庫中刪除,並從 {agents} 中移除。", + "It is taken out of the library; no agent has it.": "它會從資源庫中刪除;目前沒有 Agent 啟用它。", + "It is taken out of the {n} projects magpie placed it in too.": "magpie 放進 {n} 個專案裡的它也會一併移除。", + "magpie's sign-in to it is forgotten too.": "magpie 對它的登入也會一併清除。", "The skills magpie placed in {n} projects are taken away too.": "magpie 放進 {n} 個專案的這些技能也會一併移除。", "{n} folders are moved to magpie's backups (Backups, at the top of the Library), not deleted.": "{n} 個技能資料夾將移到 magpie 的備份(資源庫頂部的「備份」),不會刪除。", "{n} linked from folders of your own are only unlinked: those folders stay where they are.": "{n} 個連結自你資料夾的技能只取消連結,原資料夾保留。", @@ -1398,6 +1410,12 @@ magpie.strings({ "Remove all {n} skills?": "{n} 個のスキルをすべて削除しますか?", "They are taken out of the library and out of {agents}.": "ライブラリから削除され、{agents} からも外されます。", "They are taken out of the library; no agent has any of them.": "ライブラリから削除されます。これらを持っているエージェントはありません。", + "Remove the MCP server {name}?": "MCP サーバー {name} を削除しますか?", + "Remove the hook {name}?": "フック {name} を削除しますか?", + "It is taken out of the library and out of {agents}.": "ライブラリから削除され、{agents} からも外されます。", + "It is taken out of the library; no agent has it.": "ライブラリから削除されます。これを持っているエージェントはありません。", + "It is taken out of the {n} projects magpie placed it in too.": "magpie が置いた {n} 個のプロジェクトからも外されます。", + "magpie's sign-in to it is forgotten too.": "magpie のこのサーバーへのサインインも削除されます。", "The skills magpie placed in {n} projects are taken away too.": "magpie が {n} 個のプロジェクトに置いたスキルも取り除かれます。", "{n} folders are moved to magpie's backups (Backups, at the top of the Library), not deleted.": "{n} 個のフォルダは削除されず、magpie のバックアップ(ライブラリ上部の「バックアップ」)に移されます。", "{n} linked from folders of your own are only unlinked: those folders stay where they are.": "自分のフォルダからリンクされた {n} 個はリンクが解除されるだけで、そのフォルダはそのまま残ります。", @@ -1963,6 +1981,12 @@ magpie.strings({ "Remove all {n} skills?": "Alle {n} Skills entfernen?", "They are taken out of the library and out of {agents}.": "Sie werden aus der Bibliothek und aus {agents} entfernt.", "They are taken out of the library; no agent has any of them.": "Sie werden aus der Bibliothek entfernt; kein Agent hat einen davon.", + "Remove the MCP server {name}?": "MCP-Server {name} entfernen?", + "Remove the hook {name}?": "Hook {name} entfernen?", + "It is taken out of the library and out of {agents}.": "Er wird aus der Bibliothek und aus {agents} entfernt.", + "It is taken out of the library; no agent has it.": "Er wird aus der Bibliothek entfernt; kein Agent hat ihn.", + "It is taken out of the {n} projects magpie placed it in too.": "Aus den {n} Projekten, in die magpie ihn gelegt hat, wird er ebenfalls entfernt.", + "magpie's sign-in to it is forgotten too.": "Die Anmeldung von magpie bei ihm wird ebenfalls vergessen.", "The skills magpie placed in {n} projects are taken away too.": "Die Skills, die magpie in {n} Projekten abgelegt hat, werden ebenfalls entfernt.", "{n} folders are moved to magpie's backups (Backups, at the top of the Library), not deleted.": "{n} Ordner werden in die Backups von magpie verschoben (Backups, oben in der Bibliothek), nicht gelöscht.", "{n} linked from folders of your own are only unlinked: those folders stay where they are.": "Bei {n} aus eigenen Ordnern verlinkten wird nur der Link entfernt: Diese Ordner bleiben, wo sie sind.", diff --git a/internal/gui/tests/library-row-remove.test.cjs b/internal/gui/tests/library-row-remove.test.cjs new file mode 100644 index 000000000..b01428e18 --- /dev/null +++ b/internal/gui/tests/library-row-remove.test.cjs @@ -0,0 +1,212 @@ +// Run with Node's test runner and Playwright on the module path; see README.md. +// #2364 (szoxidy): an MCP server turned off for every agent stayed in the +// Library's list, and its row had no Remove: the only one was inside the +// editor the row opens, and Remove all (or Select) was all the list +// offered. A server's row now has a skill's trash button, shown on hover, +// and so does a hook's (the same gap). Its click opens no editor; it asks +// first in the page (no window.confirm), naming the one it takes ("Remove +// the MCP server exa-2?") and what goes with it: the agents it is taken +// out of (none for one turned off), the projects magpie placed it in, its +// sign-in, and that the agents' own servers stay. Cancel posts nothing; +// Remove posts servers/remove (hooks/remove) with its name, the row goes, +// the others stay. Chromium and WebKit, 1100px and 420px, in en, zh, +// zh-TW, ja and de; no click moves the page, nothing runs off it sideways. +// No backend: the API is faked here. +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 { strings } = require("./strings.cjs"); + +const assets = path.resolve(__dirname, "../assets"); +const HOME = "/Users/szoxidy"; +const agent = (id, name) => ({ id, name, icon: "", skills: `${HOME}/.${id}/skills`, mcp: `${HOME}/.${id}/mcp.json` }); +const sv = (name, url, agents, more = {}) => ({ name, transport: "http", url, agents, ...more }); +const hk = (name, agents) => ({ name, event: "turn-end", matcher: "", command: "notify-send done", agents }); +const HOOK_AGENTS = [ + { id: "claude", file: `${HOME}/.claude/settings.json`, events: ["PreToolUse", "PostToolUse", "Notification", "Stop"] }, + { id: "codex", file: `${HOME}/.codex/hooks.json`, events: ["PreToolUse", "PostToolUse", "Stop"] }, +]; +const lib = (st) => ({ + dir: `${HOME}/.magpie/library`, backups: `${HOME}/.magpie/backups`, home: HOME, + agents: [agent("claude", "Claude Code"), agent("codex", "Codex")], + instructions: { agents: [] }, servers: st.servers, skills: [], foundServers: [], foundSkills: [], problems: [], + projects: [{ path: `${HOME}/work/app`, name: "app", servers: { exa: ["claude"] } }], + hooks: st.hooks, hookAgents: HOOK_AGENTS, +}); + +let I18N; +// the words the page says in lang; a string missing from a language fails +function tr(lang, key, vars = {}) { + let s = key; + if (lang !== "en") { + assert.equal(typeof I18N[lang][key], "string", `${lang} has no ${JSON.stringify(key)}`); + s = I18N[lang][key]; + } + return s.replace(/\{(\w+)\}/g, (m, k) => (k in vars ? String(vars[k]) : m)); +} + +function server(lang, state, posts) { + return async (route) => { + const req = route.request(), url = new URL(req.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({ agents: [], profiles: [], settings: { lang, theme: "light" } }); + if (url.pathname === "/api/library") return json(lib(state)); + // every server answers, so no row is checked again under a click + if (url.pathname === "/api/library/mcp/check") return json({ servers: Object.fromEntries(req.postDataJSON().names.map((n) => [n, { state: "ok", tools: 2 }])) }); + if (url.pathname.startsWith("/api/library/") && req.method() === "POST" && !url.pathname.endsWith("/reveal")) { + const body = req.postDataJSON(), what = url.pathname.replace("/api/library/", ""); + posts.push({ what, body }); + if (what === "servers/remove") state.servers = state.servers.filter((x) => x.name !== body.name); + if (what === "hooks/remove") state.hooks = state.hooks.filter((x) => x.name !== body.name); + return json({ ...lib(state), result: { changed: ["claude"] } }); + } + if (url.pathname === "/api/plugins") return json({ plugins: [] }); + if (url.pathname === "/api/usage/quotas") return json([]); + if (url.pathname === "/api/groups") return json({ groups: [] }); + if (url.pathname === "/api/providers") return json({ providers: [], gateway: { running: true } }); + if (url.pathname.startsWith("/api/")) return json({}); + if (url.host !== "magpie.test") return route.fulfill({ status: 404, body: "" }); + 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: "" }); } + }; +} + +// a click where the thing is, as the reader's: the pointer comes over the +// row first (the trash shows on hover), and nothing on the page scrolls +const press = async (page, loc) => { + await page.waitForFunction(() => !document.querySelector("#view-library").getAnimations({ subtree: true }).some((a) => a.effect?.getComputedTiming().endTime !== Infinity)); + const b = await loc.boundingBox(); + const scrolls = () => page.evaluate(() => [...document.querySelectorAll("*")].filter((e) => e.scrollTop).map((e) => (e.id || e.className) + ":" + e.scrollTop).join(" ")); + const top = await scrolls(); + await page.mouse.move(b.x + b.width / 2, b.y + b.height / 2); + await page.waitForTimeout(200); + await page.mouse.click(b.x + b.width / 2, b.y + b.height / 2); + await page.waitForTimeout(200); + assert.equal(await scrolls(), top, `${loc}'s click scrolled the page`); +}; +const settled = (page) => page.waitForFunction(() => !document.querySelector("#modal").getAnimations({ subtree: true }).length); +const sideways = (page) => page.evaluate(() => { + const over = []; + const w = document.documentElement.clientWidth; + if (document.documentElement.scrollWidth > w) over.push("page " + document.documentElement.scrollWidth); + const v = document.querySelector("#view-library"); + if (v.scrollWidth > v.clientWidth + 1) over.push("library " + v.scrollWidth + ">" + v.clientWidth); + for (const e of document.querySelectorAll("#view-library .lib-server, #view-library .lib-hook, #view-library .lib-rowacts, #modal .lib-editor, #modal .lib-editor .bar > *, #modal .lib-editor .lib-confirm")) { + const r = e.getBoundingClientRect(); + if (r.width && (r.right > w + 0.5 || r.left < -0.5)) over.push((e.className || e.tagName) + " " + Math.round(r.left) + "–" + Math.round(r.right)); + } + return over; +}); + +for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) { + test(engine + ": one MCP server, or one hook, out of the library from its row", async (t) => { + assert(["chromium", "webkit"].includes(engine), "BROWSER must be chromium or webkit"); + I18N = strings().I18N; + const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: process.env.PLAYWRIGHT_CHANNEL || "chromium" })); + t.after(() => browser.close()); + for (const width of [1100, 420]) for (const lang of ["en", "zh", "zh-TW", "ja", "de"]) { + await t.test(`${lang} at ${width}px`, async () => { + const w = (key, vars) => tr(lang, key, vars); + const state = { + servers: [sv("exa", "https://mcp.exa.ai/mcp?tools=web_search_exa,web_fetch_exa", ["claude", "codex"], { signIn: { signedIn: true } }), sv("exa-2", "https://mcp.exa.ai/mcp", [])], + hooks: [hk("notify", ["claude"]), hk("fmt", ["claude", "codex"])], + }; + const posts = [], errors = []; + const ctx = await browser.newContext({ viewport: { width, height: 900 }, reducedMotion: "reduce" }); + // the tab the page opens on: MCP servers, then Hooks after a reload (at + // 1100px the tabs are in the side bar) + await ctx.addInitScript(() => { try { localStorage.setItem("magpie.libTab", sessionStorage.getItem("tab") || "mcp"); } catch {} }); + const page = await ctx.newPage(); + page.setDefaultTimeout(5000); + page.on("pageerror", (e) => errors.push(e.message)); + page.on("dialog", (d) => { errors.push("a browser dialog: " + d.message()); d.dismiss(); }); + await page.route("**/*", server(lang, state, posts)); + await page.goto("http://magpie.test/"); + await page.locator('button[data-view="library"]').click(); + const v = page.locator("#view-library"); + const sheet = page.locator("#modal .lib-editor"); + // its name, and a tag after it (Signed in) when it has one + const srow = (name) => v.locator(".lib-server").filter({ has: page.locator(".name", { hasText: new RegExp("^" + name + "(?!-)") }) }); + await srow("exa-2").waitFor(); + + // the trash shows over the row the pointer is on, titled Remove + const rm = srow("exa-2").locator(".lib-rowacts .lib-icon.danger"); + assert.equal(await rm.count(), 1, "exa-2's row has no Remove"); + assert.equal(await rm.getAttribute("title"), w("Remove")); + + // one turned off for every agent: no agent has it; Cancel posts nothing + await press(page, rm); + await sheet.waitFor(); + await settled(page); + assert.equal(await page.locator("#modal .lib-srvedit, #modal .lib-editor input").count(), 0, "the click opened the editor"); + assert.equal((await sheet.locator(".ehead b").textContent()).trim(), w("Remove the MCP server {name}?", { name: "exa-2" })); + const lines = async () => (await sheet.locator(".lib-confirm").allTextContents()).map((s) => s.trim()); + assert.deepEqual(await lines(), [ + w("It is taken out of the library; no agent has it."), + w("Servers your agents have that aren't in the library stay as they are."), + ]); + assert.deepEqual(await sideways(page), [], "sideways with the sheet"); + await sheet.locator(".bar button", { hasText: w("Cancel") }).click(); + await sheet.waitFor({ state: "detached" }); + assert.deepEqual(posts, [], "Cancel posted"); + assert.equal(await v.locator(".lib-server").count(), 2); + + // one given to agents, placed in a project, signed in: each said + await press(page, srow("exa").locator(".lib-rowacts .lib-icon.danger")); + await sheet.waitFor(); + await settled(page); + assert.equal((await sheet.locator(".ehead b").textContent()).trim(), w("Remove the MCP server {name}?", { name: "exa" })); + assert.deepEqual(await lines(), [ + w("It is taken out of the library and out of {agents}.", { agents: "Claude Code, Codex" }), + w("It is taken out of the {n} projects magpie placed it in too.", { n: 1 }), + w("magpie's sign-in to it is forgotten too."), + w("Servers your agents have that aren't in the library stay as they are."), + ]); + await sheet.locator(".bar button", { hasText: w("Cancel") }).click(); + await sheet.waitFor({ state: "detached" }); + assert.deepEqual(posts, [], "Cancel posted"); + + // Remove: exa-2 alone goes + await press(page, srow("exa-2").locator(".lib-rowacts .lib-icon.danger")); + await sheet.waitFor(); + await settled(page); + await sheet.locator(".bar button.primary").click(); + await sheet.waitFor({ state: "detached" }); + assert.deepEqual(posts.shift(), { what: "servers/remove", body: { name: "exa-2" } }); + await page.locator("#status").filter({ hasText: w("{name} removed", { name: "exa-2" }) }).waitFor(); + await srow("exa-2").waitFor({ state: "detached" }); + assert.equal(await srow("exa").count(), 1, "exa went too"); + assert.deepEqual(await sideways(page), [], "sideways on the servers"); + + // a hook's row, the same + await page.evaluate(() => sessionStorage.setItem("tab", "hooks")); + await page.reload(); + await page.locator('button[data-view="library"]').click(); + const hrow = (name) => v.locator(".lib-hook").filter({ has: page.locator(".name", { hasText: new RegExp("^" + name + "$") }) }); + await hrow("fmt").waitFor(); + await press(page, hrow("fmt").locator(".lib-rowacts .lib-icon.danger")); + await sheet.waitFor(); + await settled(page); + assert.equal((await sheet.locator(".ehead b").textContent()).trim(), w("Remove the hook {name}?", { name: "fmt" })); + assert.deepEqual(await lines(), [ + w("It is taken out of the library and out of {agents}.", { agents: "Claude Code, Codex" }), + w("Hooks your agents have that aren't in the library stay as they are, and so does one you changed in an agent's file."), + ]); + assert.deepEqual(await sideways(page), [], "sideways with the hook's sheet"); + await sheet.locator(".bar button.primary").click(); + await sheet.waitFor({ state: "detached" }); + assert.deepEqual(posts.shift(), { what: "hooks/remove", body: { name: "fmt" } }); + await hrow("fmt").waitFor({ state: "detached" }); + assert.equal(await hrow("notify").count(), 1, "notify went too"); + assert.deepEqual(posts, []); + assert.deepEqual(errors, []); + await ctx.close(); + }); + } + }); +}