library: a project turns on a plugin Claude Code installed, in its own settings (#7254)
Plugins Claude Code has installed itself (installed_plugins.json) are listed on a project's Claude plugins tab, each with one Claude chip. On writes enabledPlugins.<id> = true into the project's .claude/settings.local.json through the format-preserving JSON edit (comments and the user's keys stay); off takes out only a key magpie wrote that is still true. A key the user already had is neither written nor taken out; a file magpie made goes once the skill offs, hooks and plugin keys sharing it are all gone. Worktrees follow through syncTrees; RemoveProject lets go. Written fresh, not ported. Run (mote-run): go vet library/gui/cli; go test ./internal/library -run TestProjectClaudePlugin (new, two tests); project-claude-plugins, project-plugins and i18n-keys GUI files in chromium and webkit, en and zh; go test ./internal/gui reds only on testenv's docs-index guard (routing-groups pages newer than the search index, not from this change). Not tried: a real Claude Code reading the project's enabledPlugins; the whole GUI suite; a CLI for it; "off" for a plugin the user turned on user-wide.
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -5698,6 +5698,8 @@ magpie.plugin("library", (ctx) => {
|
||||
const plugins = lib.plugins || [];
|
||||
if (!plugins.length) {
|
||||
body.append(empty(t("No Claude plugins in the library yet"), t("Install one from a folder, a git address or a marketplace.json.")));
|
||||
// Claude Code's own plugins can still be turned on for a project
|
||||
if (!agentless() && lib.projects.length && (lib.claudeInstalled || []).length) renderProjects(body, "plugins");
|
||||
return;
|
||||
}
|
||||
const list = el("div", "list lib-list");
|
||||
@@ -7415,6 +7417,25 @@ magpie.plugin("library", (ctx) => {
|
||||
row.append(glyph(GLYPH.folder), w, agentChips(all, on, projectChange("projects/plugin", p.dir, b.name, say)));
|
||||
card.append(row);
|
||||
}
|
||||
// the plugins Claude Code has installed itself, which only Claude reads
|
||||
// a project's enabledPlugins for: one chip, on for this project alone
|
||||
const claude = all.find((a) => a.id === "claude");
|
||||
const installed = lib.claudeInstalled || [];
|
||||
if (claude && installed.length) {
|
||||
const mine = p.claudePlugins || [];
|
||||
for (const c of installed) {
|
||||
const row = el("div", "row lib-row lib-projskill lib-projplugin lib-projclaude");
|
||||
const w = el("div", "who");
|
||||
w.append(el("div", "name mono", c.id));
|
||||
row.append(glyph(GLYPH.folder), w);
|
||||
const say = (next) => next.length
|
||||
? t("{item} is in {name} for {agents}", { item: c.id, name: p.name, agents: nameOf("claude") })
|
||||
: t("{item} is out of {name}", { item: c.id, name: p.name });
|
||||
row.append(agentChips([claude], mine.includes(c.id) ? ["claude"] : [], projectChange("projects/claude-plugin", p.dir, c.id, say)));
|
||||
card.append(row);
|
||||
}
|
||||
card.append(el("p", "lib-aside lib-projfoot lib-projclaude-note", t("The plugins above are Claude Code's own, turned on for this project in .claude/settings.local.json. They stay installed through Claude Code.")));
|
||||
}
|
||||
card.append(el("p", "lib-aside lib-projfoot", t("Its skills, servers and subagents go where the project's own do, and its hooks into .claude/settings.local.json and .codex/hooks.json. A file there that isn't magpie's, or an entry you changed, is left as it is.")));
|
||||
if (plugins.some((b) => b.hooks?.length && (p.plugins?.[b.name] || []).includes("codex"))) card.append(el("p", "lib-aside lib-projfoot lib-projhooks-codex", t("Codex runs a project's hooks only in a project you trust, once you've reviewed them in /hooks.")));
|
||||
return card;
|
||||
|
||||
@@ -797,6 +797,7 @@ magpie.strings({
|
||||
"Pick the plugins it gets": "选择它要用的插件",
|
||||
"Its skills, servers and subagents go where the project's own do, and its hooks into .claude/settings.local.json and .codex/hooks.json. A file there that isn't magpie's, or an entry you changed, is left as it is.": "它的技能、服务器和子代理放在项目自己的那些位置,hooks 写进 .claude/settings.local.json 和 .codex/hooks.json。那里不是 magpie 写的文件、你改过的条目,都保持原样。",
|
||||
"Codex runs a project's hooks only in a project you trust, once you've reviewed them in /hooks.": "Codex 只在你信任的项目里运行项目的 hooks,并且要先在 /hooks 中审阅。",
|
||||
"The plugins above are Claude Code's own, turned on for this project in .claude/settings.local.json. They stay installed through Claude Code.": "上面的插件是 Claude Code 自带的,在 .claude/settings.local.json 里只为这个项目启用,仍由 Claude Code 安装。",
|
||||
"Give a project's agents some of these subagents as the project's own: magpie writes them into its .claude/agents and the like, and keeps them out of git.": "把这些子代理作为项目自己的给项目里的 Agent:magpie 写进它的 .claude/agents 等文件夹,并让它们不进 git。",
|
||||
"{name} added — pick the subagents it gets": "已添加 {name},选择它要用的子代理",
|
||||
"No subagents yet": "还没有子代理",
|
||||
@@ -1945,6 +1946,7 @@ magpie.strings({
|
||||
"Pick the plugins it gets": "選擇它要用的外掛",
|
||||
"Its skills, servers and subagents go where the project's own do, and its hooks into .claude/settings.local.json and .codex/hooks.json. A file there that isn't magpie's, or an entry you changed, is left as it is.": "它的技能、伺服器和子代理放在專案自己的那些位置,hooks 寫進 .claude/settings.local.json 和 .codex/hooks.json。那裡不是 magpie 寫的檔案、你改過的條目,都保持原樣。",
|
||||
"Codex runs a project's hooks only in a project you trust, once you've reviewed them in /hooks.": "Codex 只在你信任的專案裡執行專案的 hooks,並且要先在 /hooks 中審閱。",
|
||||
"The plugins above are Claude Code's own, turned on for this project in .claude/settings.local.json. They stay installed through Claude Code.": "上面的外掛是 Claude Code 自帶的,在 .claude/settings.local.json 裡只為這個專案啟用,仍由 Claude Code 安裝。",
|
||||
"Give a project's agents some of these subagents as the project's own: magpie writes them into its .claude/agents and the like, and keeps them out of git.": "把這些子代理作為專案自己的給專案裡的 Agent:magpie 寫進它的 .claude/agents 等資料夾,並讓它們不進 git。",
|
||||
"{name} added — pick the subagents it gets": "已新增 {name},選擇它要用的子代理",
|
||||
"No subagents yet": "還沒有子代理",
|
||||
@@ -3084,6 +3086,7 @@ magpie.strings({
|
||||
"Pick the plugins it gets": "渡すプラグインを選ぶ",
|
||||
"Its skills, servers and subagents go where the project's own do, and its hooks into .claude/settings.local.json and .codex/hooks.json. A file there that isn't magpie's, or an entry you changed, is left as it is.": "スキル、サーバー、サブエージェントはプロジェクト自身のものと同じ場所に、フックは .claude/settings.local.json と .codex/hooks.json に書き込まれます。magpie のものではないファイルや、あなたが変更した項目はそのまま残ります。",
|
||||
"Codex runs a project's hooks only in a project you trust, once you've reviewed them in /hooks.": "Codex は信頼したプロジェクトでのみ、/hooks で確認した後にプロジェクトのフックを実行します。",
|
||||
"The plugins above are Claude Code's own, turned on for this project in .claude/settings.local.json. They stay installed through Claude Code.": "上のプラグインは Claude Code 自身のもので、このプロジェクトに限り .claude/settings.local.json で有効にします。インストールは引き続き Claude Code が行います。",
|
||||
"Give a project's agents some of these subagents as the project's own: magpie writes them into its .claude/agents and the like, and keeps them out of git.": "これらのサブエージェントをプロジェクト専用として渡します。magpie が .claude/agents などに書き込み、git には入れません。",
|
||||
"{name} added — pick the subagents it gets": "{name} を追加しました。渡すサブエージェントを選んでください",
|
||||
"No subagents yet": "サブエージェントはまだありません",
|
||||
@@ -4228,6 +4231,7 @@ magpie.strings({
|
||||
"Pick the plugins it gets": "Wähle die Plugins, die es bekommt",
|
||||
"Its skills, servers and subagents go where the project's own do, and its hooks into .claude/settings.local.json and .codex/hooks.json. A file there that isn't magpie's, or an entry you changed, is left as it is.": "Skills, Server und Subagenten kommen dorthin, wo die eigenen des Projekts liegen, Hooks in .claude/settings.local.json und .codex/hooks.json. Eine Datei dort, die nicht von magpie ist, oder ein Eintrag, den du geändert hast, bleibt, wie er ist.",
|
||||
"Codex runs a project's hooks only in a project you trust, once you've reviewed them in /hooks.": "Codex führt die Hooks eines Projekts nur in einem Projekt aus, dem du vertraust, und erst, nachdem du sie in /hooks geprüft hast.",
|
||||
"The plugins above are Claude Code's own, turned on for this project in .claude/settings.local.json. They stay installed through Claude Code.": "Die Plugins darüber sind die von Claude Code selbst und gelten nur für dieses Projekt, aktiviert in .claude/settings.local.json. Installiert bleiben sie über Claude Code.",
|
||||
"Give a project's agents some of these subagents as the project's own: magpie writes them into its .claude/agents and the like, and keeps them out of git.": "Gib den Agenten eines Projekts einige dieser Subagenten als eigene: magpie schreibt sie in dessen .claude/agents und Ähnliches und hält sie aus git heraus.",
|
||||
"{name} added — pick the subagents it gets": "{name} hinzugefügt – wähle die Subagenten dafür",
|
||||
"No subagents yet": "Noch keine Subagenten",
|
||||
|
||||
@@ -952,6 +952,14 @@ func libraryRoutes(mux *http.ServeMux, w Windows) {
|
||||
res, err = library.ProjectServer(in.Dir, in.Name, in.Agents)
|
||||
case "projects/plugin":
|
||||
res, err = library.ProjectPlugin(in.Dir, in.Name, in.Agents)
|
||||
case "projects/claude-plugin":
|
||||
// a plugin Claude Code has installed, on for this project alone
|
||||
// (agents ["claude"]) or taken out of its settings ([])
|
||||
if len(in.Agents) > 1 || len(in.Agents) == 1 && in.Agents[0] != "claude" {
|
||||
http.Error(rw, "only Claude Code reads a project's enabledPlugins", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
res, err = library.ProjectClaudePlugin(in.Dir, in.Name, len(in.Agents) == 1)
|
||||
case "projects/server-off":
|
||||
// a Library server turned off in this project only
|
||||
res, err = library.ProjectServerOff(in.Dir, in.Name, in.Agents)
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
// Run with Node's test runner and Playwright on the module path; see README.md.
|
||||
// (#7254, jinpwng on X) A project's Claude plugins tab lists the plugins
|
||||
// Claude Code has installed itself, each with one Claude chip that posts to
|
||||
// projects/claude-plugin; no chip left takes it out. In English and Chinese.
|
||||
// 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 assets = path.resolve(__dirname, "../assets");
|
||||
const HOME = "/Users/emo";
|
||||
const DIR = HOME + "/code/app";
|
||||
const agent = (id, name, more = {}) => ({ id, name, icon: "", skills: `${HOME}/.${id}/skills`, mcp: `${HOME}/.${id}/mcp.json`, ...more });
|
||||
const plugin = (name, more) => ({ name, version: "1.0.0", description: "", agents: [], skills: [], mcp: [], hooks: [], subagents: [], ...more });
|
||||
const lib = (given) => ({
|
||||
dir: `${HOME}/.magpie/library`, backups: `${HOME}/.magpie/backups`, home: HOME,
|
||||
agents: [
|
||||
agent("claude", "Claude Code", { projectSkills: ".claude/skills", projectMCP: ".mcp.json", projectSubagents: ".claude/agents", projectHooks: ".claude/settings.local.json" }),
|
||||
agent("codex", "Codex", { projectMCP: ".codex/config.toml", projectSubagents: ".codex/agents", projectHooks: ".codex/hooks.json" }),
|
||||
agent("pi", "Pi", { projectSkills: ".agents/skills", projectMCP: ".pi/mcp.json" }),
|
||||
agent("goose", "Goose"),
|
||||
],
|
||||
instructions: { agents: [] }, servers: [], subagents: [], skills: [], foundServers: [], foundSkills: [],
|
||||
plugins: [], claudeInstalled: [{ id: "fmt@acme", on: false }, { id: "lint@acme", on: true }],
|
||||
projects: [{ dir: DIR, name: "app", copy: false, skills: {}, placed: [], servers: {}, subagents: {}, plugins: {}, claudePlugins: given, wrote: {} }],
|
||||
});
|
||||
|
||||
function server(lang, posts) {
|
||||
let given = [];
|
||||
return async (route) => {
|
||||
const req = route.request(), url = new URL(req.url());
|
||||
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 route.fulfill({ json: { agents: [], profiles: [], settings: { lang, theme: "light" } } });
|
||||
if (url.pathname === "/api/library") return route.fulfill({ json: lib(given) });
|
||||
if (url.pathname === "/api/library/projects/claude-plugin") {
|
||||
const b = req.postDataJSON();
|
||||
posts.push(b);
|
||||
if (posts.hold) await posts.hold;
|
||||
given = given.filter((x) => x !== b.name);
|
||||
if (b.agents.length) given.push(b.name);
|
||||
return route.fulfill({ json: { ...lib(given), result: { changed: [], problems: [] } } });
|
||||
}
|
||||
if (url.pathname === "/api/plugins") return route.fulfill({ json: { plugins: [] } });
|
||||
if (url.pathname === "/api/usage/quotas") return route.fulfill({ json: [] });
|
||||
if (url.pathname === "/api/groups") return route.fulfill({ json: { groups: [] } });
|
||||
if (url.pathname === "/api/providers") return route.fulfill({ json: { providers: [], gateway: { running: true } } });
|
||||
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 scrolled = (page) => page.evaluate(() => [window.scrollX, window.scrollY, ...[...document.querySelectorAll("*")].filter((e) => e.scrollTop || e.scrollLeft).map((e) => `${e.className}:${e.scrollTop},${e.scrollLeft}`)].join(" "));
|
||||
// a click where the element is, as the reader's: Playwright's own click
|
||||
// first scrolls it into view
|
||||
const press = async (page, loc) => {
|
||||
const b = await loc.boundingBox();
|
||||
await page.mouse.click(b.x + b.width / 2, b.y + b.height / 2);
|
||||
};
|
||||
// a chip: the chips fan out under the pointer first, as a reader's hand
|
||||
// does, and the chip is pressed where it is then
|
||||
// (a render after the last write may replace it under the pointer, so it
|
||||
// is found again until it stays)
|
||||
const box = async (loc) => {
|
||||
for (;;) {
|
||||
await loc.waitFor({ state: "visible" });
|
||||
const b = await loc.boundingBox();
|
||||
if (b) return b;
|
||||
}
|
||||
};
|
||||
const chip = async (page, loc) => {
|
||||
const a = await box(loc);
|
||||
await page.mouse.move(a.x + a.width / 2, a.y + a.height / 2);
|
||||
await page.waitForTimeout(350);
|
||||
const b = await box(loc);
|
||||
await page.mouse.click(b.x + b.width / 2, b.y + b.height / 2);
|
||||
};
|
||||
|
||||
|
||||
const words = {
|
||||
en: { on: "fmt@acme is in app for Claude Code", out: "fmt@acme is out of app", note: "The plugins above are Claude Code's own, turned on for this project in .claude/settings.local.json. They stay installed through Claude Code." },
|
||||
zh: { on: "fmt@acme 已在 app 中对 Claude Code 开启", out: "fmt@acme 已从 app 中移除", note: "上面的插件是 Claude Code 自带的,在 .claude/settings.local.json 里只为这个项目启用,仍由 Claude Code 安装。" },
|
||||
};
|
||||
|
||||
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
|
||||
test(engine + ": a project turns on a plugin Claude Code has installed", async (t) => {
|
||||
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
|
||||
const errors = [];
|
||||
t.after(async () => {
|
||||
if (errors.length) console.log(errors);
|
||||
await browser.close();
|
||||
});
|
||||
for (const lang of ["en", "zh"]) {
|
||||
await t.test(lang, async () => {
|
||||
const w = words[lang], posts = [];
|
||||
const ctx = await browser.newContext({ viewport: { width: 860, height: 700 } });
|
||||
await ctx.addInitScript(() => { try { localStorage.setItem("magpie.libTab", "plugins"); } catch {} });
|
||||
const page = await ctx.newPage();
|
||||
page.setDefaultTimeout(5000);
|
||||
page.setDefaultNavigationTimeout(60000);
|
||||
page.on("pageerror", (e) => errors.push(e.message));
|
||||
await page.route("**/*", server(lang, posts));
|
||||
await page.goto("http://magpie.test/");
|
||||
await page.locator('button[data-view="library"]').click();
|
||||
const card = page.locator("#view-library .lib-project");
|
||||
await card.waitFor();
|
||||
await press(page, card.locator(".lib-projhead"));
|
||||
const rows = card.locator(".lib-projclaude");
|
||||
await rows.first().waitFor();
|
||||
assert.equal(await rows.count(), 2);
|
||||
assert.deepEqual(await rows.first().locator(".lib-ag").evaluateAll((cs) => cs.map((c) => c.dataset.agent)), ["claude"]);
|
||||
assert.equal((await card.locator(".lib-projclaude-note").textContent()).trim(), w.note);
|
||||
const row = () => card.locator(".lib-projclaude").filter({ hasText: "fmt@acme" });
|
||||
await chip(page, row().locator('.lib-ag[data-agent="claude"]'));
|
||||
await page.waitForFunction(() => document.querySelectorAll('#view-library .lib-projclaude .lib-ag.on').length === 1);
|
||||
await page.waitForFunction((s) => document.querySelector("#status").textContent.trim() === s, w.on);
|
||||
await chip(page, row().locator('.lib-ag[data-agent="claude"]'));
|
||||
await page.waitForFunction(() => document.querySelectorAll('#view-library .lib-projclaude .lib-ag.on').length === 0);
|
||||
await page.waitForFunction((s) => document.querySelector("#status").textContent.trim() === s, w.out);
|
||||
assert.deepEqual(posts.map((b) => [b.dir, b.name, b.agents]), [[DIR, "fmt@acme", ["claude"]], [DIR, "fmt@acme", []]]);
|
||||
await ctx.close();
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
package library
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/tidwall/gjson"
|
||||
|
||||
"github.com/yetone/magpie/internal/edit"
|
||||
)
|
||||
|
||||
// A project can turn on a plugin Claude Code has installed for that folder
|
||||
// alone (#7254, jinpwng on X): enabledPlugins in the project's
|
||||
// .claude/settings.local.json, which Claude Code reads for the folder and
|
||||
// which wins over the user's settings.json. The plugin itself stays
|
||||
// installed through Claude Code's own CLI; magpie writes the one key
|
||||
// (claudeSettingsRel, the file the project's skill offs and hooks share) and
|
||||
// takes out only a key it wrote, still true (Project.ClaudePlugins is what
|
||||
// the project wants, ClaudeWrote what magpie wrote). An entry the user put
|
||||
// there is theirs: it is neither written over nor taken out.
|
||||
//
|
||||
// <project>/.claude/settings.local.json {"enabledPlugins": {"p@m": true}}
|
||||
|
||||
// ClaudePluginView is a plugin Claude Code has installed, as the project
|
||||
// card lists it: whether the user's own settings have it on.
|
||||
type ClaudePluginView struct {
|
||||
ID string `json:"id"`
|
||||
On bool `json:"on"`
|
||||
}
|
||||
|
||||
// ReadClaudeInstalled lists the plugins Claude Code's own record
|
||||
// (plugins/installed_plugins.json) has installed, with whether
|
||||
// settings.json turns each on.
|
||||
func ReadClaudeInstalled() []ClaudePluginView {
|
||||
raw, err := readClaudeJSON(claudeInstalled())
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
set, _ := readClaudeJSON(claudeSettings())
|
||||
var out []ClaudePluginView
|
||||
gjson.GetBytes(raw, "plugins").ForEach(func(k, _ gjson.Result) bool {
|
||||
id := k.String()
|
||||
if strings.Contains(id, "@") {
|
||||
out = append(out, ClaudePluginView{ID: id, On: gjson.GetBytes(set, "enabledPlugins."+gjson.Escape(id)).Type == gjson.True})
|
||||
}
|
||||
return true
|
||||
})
|
||||
sort.Slice(out, func(i, j int) bool { return out[i].ID < out[j].ID })
|
||||
return out
|
||||
}
|
||||
|
||||
func projectClaudeSettings(p *Project) string {
|
||||
return filepath.Join(p.Dir, filepath.FromSlash(claudeSettingsRel))
|
||||
}
|
||||
|
||||
// syncProjectClaudePlugins keeps enabledPlugins.<id> = true in the project's
|
||||
// .claude/settings.local.json for each plugin the project turns on, and
|
||||
// takes out the ones magpie wrote that it no longer does.
|
||||
func (l *Library) syncProjectClaudePlugins(p *Project, fail func(what string, err error)) {
|
||||
if len(p.ClaudePlugins) == 0 && len(p.ClaudeWrote) == 0 {
|
||||
return
|
||||
}
|
||||
abs := projectClaudeSettings(p)
|
||||
if err := p.inside(abs); err != nil {
|
||||
fail("claude-plugin", err)
|
||||
return
|
||||
}
|
||||
key := func(id string) string { return "enabledPlugins." + gjson.Escape(id) }
|
||||
var mine []string
|
||||
for _, id := range p.ClaudeWrote {
|
||||
if slices.Contains(p.ClaudePlugins, id) {
|
||||
if v, ok := edit.GetJSON(abs, key(id)); ok && v == "true" {
|
||||
mine = append(mine, id)
|
||||
}
|
||||
continue
|
||||
}
|
||||
if v, ok := edit.GetJSON(abs, key(id)); ok && v != "true" {
|
||||
continue // the user's own by now
|
||||
}
|
||||
if err := edit.DelJSON(abs, key(id)); err != nil {
|
||||
fail("claude-plugin:"+id, err)
|
||||
mine = append(mine, id)
|
||||
}
|
||||
}
|
||||
for _, id := range p.ClaudePlugins {
|
||||
if slices.Contains(mine, id) {
|
||||
continue
|
||||
}
|
||||
if v, ok := edit.GetJSON(abs, key(id)); ok {
|
||||
if v != "true" {
|
||||
fail("claude-plugin:"+id, fmt.Errorf("%s sets %s for this project already: it's left as it is", claudeSettingsRel, id))
|
||||
}
|
||||
continue // the user's own
|
||||
}
|
||||
if !exists(abs) {
|
||||
if err := p.mkdirs(filepath.Dir(abs)); err != nil {
|
||||
fail("claude-plugin:"+id, err)
|
||||
continue
|
||||
}
|
||||
if !slices.Contains(p.MadeFiles, claudeSettingsRel) {
|
||||
p.MadeFiles = append(p.MadeFiles, claudeSettingsRel)
|
||||
}
|
||||
}
|
||||
if err := edit.SetJSON(abs, edit.KV{Path: key(id), Value: true}); err != nil {
|
||||
fail("claude-plugin:"+id, err)
|
||||
continue
|
||||
}
|
||||
mine = append(mine, id)
|
||||
}
|
||||
sort.Strings(mine)
|
||||
p.ClaudeWrote = mine
|
||||
if len(mine) == 0 {
|
||||
p.ClaudeWrote = nil
|
||||
if len(p.Overrides) == 0 && len(p.HookRecs["claude"]) == 0 {
|
||||
p.dropMadeFile(claudeSettingsRel)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ProjectClaudePlugin sets whether a project turns on a plugin Claude Code
|
||||
// has installed, in its own settings; off takes the key magpie wrote out.
|
||||
func ProjectClaudePlugin(dir, id string, on bool) (*Result, error) {
|
||||
if i := strings.Index(id, "@"); i < 1 || i == len(id)-1 {
|
||||
return nil, fmt.Errorf("%q isn't a plugin id: it looks like name@marketplace", id)
|
||||
}
|
||||
return change(func(l *Library) error {
|
||||
p := l.project(dir)
|
||||
if p == nil {
|
||||
return fmt.Errorf("no project %s", dir)
|
||||
}
|
||||
p.ClaudePlugins = slices.DeleteFunc(p.ClaudePlugins, func(x string) bool { return x == id })
|
||||
if on {
|
||||
p.ClaudePlugins = append(p.ClaudePlugins, id)
|
||||
sort.Strings(p.ClaudePlugins)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
package library
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// A project turns on a plugin Claude Code has installed for its folder
|
||||
// alone (#7254, jinpwng on X): enabledPlugins in its
|
||||
// .claude/settings.local.json. The user's own keys, comments and entries
|
||||
// stay; only the key magpie wrote comes out, and a file it made goes.
|
||||
func TestProjectClaudePluginEnabledPlugins(t *testing.T) {
|
||||
h, a, b := pluginProjects(t)
|
||||
write(t, filepath.Join(h, ".claude", "plugins", "installed_plugins.json"),
|
||||
`{"version":2,"plugins":{"fmt@acme":[{"scope":"user"}],"lint@acme":[{"scope":"user"}]}}`)
|
||||
write(t, filepath.Join(h, ".claude", "settings.json"), `{"enabledPlugins":{"lint@acme":true}}`)
|
||||
userWas := read(t, filepath.Join(h, ".claude", "settings.json"))
|
||||
f := filepath.Join(a, ".claude", "settings.local.json")
|
||||
|
||||
if _, err := ProjectClaudePlugin(a, "nope", true); err == nil {
|
||||
t.Error("an id with no marketplace was taken")
|
||||
}
|
||||
v, _ := Read(nil)
|
||||
if len(v.ClaudeInstalled) != 2 || v.ClaudeInstalled[0].ID != "fmt@acme" || v.ClaudeInstalled[0].On || !v.ClaudeInstalled[1].On {
|
||||
t.Errorf("installed: %+v", v.ClaudeInstalled)
|
||||
}
|
||||
|
||||
// a file magpie made, with nothing else in it, goes with the key
|
||||
res := ok(t)(ProjectClaudePlugin(a, "fmt@acme", true))
|
||||
if len(res.Problems) > 0 {
|
||||
t.Fatalf("problems: %+v", res.Problems)
|
||||
}
|
||||
if got := read(t, f); !strings.Contains(got, `"fmt@acme": true`) {
|
||||
t.Errorf("settings.local.json:\n%s", got)
|
||||
}
|
||||
if g := read(t, filepath.Join(a, ".git", "info", "exclude")); !strings.Contains(g, "/.claude/settings.local.json\n") {
|
||||
t.Errorf("info/exclude:\n%s", g)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(b, ".claude")); err == nil {
|
||||
t.Error("another project got a file")
|
||||
}
|
||||
ok(t)(ProjectClaudePlugin(a, "fmt@acme", true)) // again: one key
|
||||
if n := strings.Count(read(t, f), "fmt@acme"); n != 1 {
|
||||
t.Errorf("%d keys:\n%s", n, read(t, f))
|
||||
}
|
||||
v, _ = Read(nil)
|
||||
if got := v.Projects[0].ClaudePlugins; len(got) != 1 || got[0] != "fmt@acme" {
|
||||
t.Errorf("view: %v", got)
|
||||
}
|
||||
ok(t)(ProjectClaudePlugin(a, "fmt@acme", false))
|
||||
gone(t, f)
|
||||
|
||||
// the user's file, with a comment, a key of theirs and a plugin they
|
||||
// turned on themselves: only magpie's key is added and taken out
|
||||
own := "{\n // mine\n \"permissions\": {\"allow\": [\"Bash(ls)\"]},\n \"enabledPlugins\": {\"lint@acme\": true}\n}\n"
|
||||
write(t, f, own)
|
||||
ok(t)(ProjectClaudePlugin(a, "fmt@acme", true))
|
||||
ok(t)(ProjectClaudePlugin(a, "lint@acme", true)) // theirs already: not written, not taken out
|
||||
got := read(t, f)
|
||||
if !strings.Contains(got, "// mine") || !strings.Contains(got, "Bash(ls)") || !strings.Contains(got, "fmt@acme") {
|
||||
t.Errorf("settings.local.json:\n%s", got)
|
||||
}
|
||||
ok(t)(ProjectClaudePlugin(a, "fmt@acme", false))
|
||||
ok(t)(ProjectClaudePlugin(a, "lint@acme", false))
|
||||
got = read(t, f)
|
||||
if strings.Contains(got, "fmt@acme") || !strings.Contains(got, `"lint@acme": true`) || !strings.Contains(got, "// mine") {
|
||||
t.Errorf("after taking out:\n%s", got)
|
||||
}
|
||||
|
||||
// a key the user changed since is theirs: it stays when the project lets go
|
||||
ok(t)(ProjectClaudePlugin(a, "fmt@acme", true))
|
||||
write(t, f, strings.Replace(read(t, f), `"fmt@acme": true`, `"fmt@acme": false`, 1))
|
||||
ok(t)(ProjectClaudePlugin(a, "fmt@acme", false))
|
||||
if got := read(t, f); !strings.Contains(got, `"fmt@acme": false`) {
|
||||
t.Errorf("the user's value was taken:\n%s", got)
|
||||
}
|
||||
if read(t, filepath.Join(h, ".claude", "settings.json")) != userWas {
|
||||
t.Error("Claude Code's own settings.json was written")
|
||||
}
|
||||
|
||||
// taking the project away takes magpie's key with it
|
||||
ok(t)(ProjectClaudePlugin(a, "lint@acme", true))
|
||||
ok(t)(RemoveProject(a, false))
|
||||
if got := read(t, f); strings.Count(got, "lint@acme") != 1 || strings.Contains(got, "// mine") == false {
|
||||
t.Errorf("after the project went:\n%s", got)
|
||||
}
|
||||
}
|
||||
|
||||
// The file the project's skill offs and hooks share goes only once none of
|
||||
// them is left in it.
|
||||
func TestProjectClaudePluginSharesTheFile(t *testing.T) {
|
||||
_, a, _ := pluginProjects(t)
|
||||
f := filepath.Join(a, ".claude", "settings.local.json")
|
||||
ok(t)(ProjectPlugin(a, "acme-kit", []string{"claude"})) // hooks into the file
|
||||
ok(t)(ProjectClaudePlugin(a, "fmt@acme", true))
|
||||
ok(t)(ProjectPlugin(a, "acme-kit", nil))
|
||||
if !strings.Contains(read(t, f), "fmt@acme") {
|
||||
t.Errorf("the key went with the hooks:\n%s", read(t, f))
|
||||
}
|
||||
ok(t)(ProjectClaudePlugin(a, "fmt@acme", false))
|
||||
gone(t, f)
|
||||
}
|
||||
@@ -193,7 +193,7 @@ func (l *Library) syncProjectHooks(p *Project, fail func(what string, err error)
|
||||
delete(p.HookRecs, id)
|
||||
// Claude Code's file holds the skills the project turns off
|
||||
// too (syncSkillsOff)
|
||||
if id != "claude" || len(p.Overrides) == 0 {
|
||||
if id != "claude" || len(p.Overrides) == 0 && len(p.ClaudeWrote) == 0 {
|
||||
p.dropMadeFile(rel)
|
||||
}
|
||||
continue
|
||||
|
||||
@@ -415,7 +415,7 @@ func (l *Library) syncSkillsOff(p *Project, fail func(what string, err error)) {
|
||||
sort.Strings(mine)
|
||||
p.Overrides = mine
|
||||
// the file holds the project's plugins' hooks too (project_plugins.go)
|
||||
if len(mine) == 0 && len(p.HookRecs["claude"]) == 0 {
|
||||
if len(mine) == 0 && len(p.HookRecs["claude"]) == 0 && len(p.ClaudeWrote) == 0 {
|
||||
p.dropMadeFile(claudeSettingsRel)
|
||||
}
|
||||
}
|
||||
@@ -461,7 +461,7 @@ func (l *Library) syncTrees(p *Project, res *Result, hubs map[string]*Server) {
|
||||
t = &Project{Dir: d}
|
||||
}
|
||||
t.Copy, t.Skills, t.Servers, t.ServersOff, t.SkillsOff, t.Subagents = p.Copy, p.Skills, p.Servers, p.ServersOff, p.SkillsOff, p.Subagents
|
||||
t.Instr, t.InstrAgents, t.Plugins = p.Instr, p.InstrAgents, p.Plugins
|
||||
t.Instr, t.InstrAgents, t.Plugins, t.ClaudePlugins = p.Instr, p.InstrAgents, p.Plugins, p.ClaudePlugins
|
||||
l.syncFolder(t, res, hubs)
|
||||
trees = append(trees, t)
|
||||
}
|
||||
@@ -475,7 +475,7 @@ func (l *Library) syncTrees(p *Project, res *Result, hubs map[string]*Server) {
|
||||
// no longer one of the repository's worktrees, or no longer the
|
||||
// project's: what magpie put there comes out
|
||||
t.Skills, t.Servers, t.ServersOff, t.SkillsOff, t.Subagents = nil, nil, nil, nil, nil
|
||||
t.Instr, t.InstrAgents, t.Plugins = "", nil, nil
|
||||
t.Instr, t.InstrAgents, t.Plugins, t.ClaudePlugins = "", nil, nil, nil
|
||||
l.syncFolder(t, res, nil)
|
||||
if t.holds() {
|
||||
trees = append(trees, t)
|
||||
@@ -498,7 +498,7 @@ func (p *Project) tree(dir string) *Project {
|
||||
|
||||
// holds says whether magpie has anything of its own in the folder still.
|
||||
func (p *Project) holds() bool {
|
||||
return len(p.Placed) > 0 || len(p.Wrote) > 0 || len(p.CodexOff) > 0 || len(p.Overrides) > 0 || len(p.MadeFiles) > 0 || len(p.AgentFiles) > 0 || len(p.InstrFiles) > 0 || len(p.HookRecs) > 0
|
||||
return len(p.Placed) > 0 || len(p.Wrote) > 0 || len(p.CodexOff) > 0 || len(p.Overrides) > 0 || len(p.MadeFiles) > 0 || len(p.AgentFiles) > 0 || len(p.InstrFiles) > 0 || len(p.HookRecs) > 0 || len(p.ClaudeWrote) > 0
|
||||
}
|
||||
|
||||
// ignored are the files and folders of the project magpie put there, which
|
||||
|
||||
@@ -77,6 +77,12 @@ type Project struct {
|
||||
// agent, the hook entries magpie wrote in the project's file for them
|
||||
Plugins map[string][]string `json:"plugins,omitempty"`
|
||||
HookRecs map[string][]HookEntry `json:"hookRecs,omitempty"`
|
||||
// ClaudePlugins are the plugins Claude Code has installed that the
|
||||
// project turns on for itself, ClaudeWrote the enabledPlugins keys
|
||||
// magpie wrote for them in .claude/settings.local.json, the only
|
||||
// ones it takes out (project_claude_plugins.go)
|
||||
ClaudePlugins []string `json:"claudePlugins,omitempty"`
|
||||
ClaudeWrote []string `json:"claudeWrote,omitempty"`
|
||||
}
|
||||
|
||||
// projectSkillsDirs is where, in a project, each agent reads skills of its
|
||||
@@ -151,7 +157,7 @@ func (l *Library) syncProjects(res *Result) {
|
||||
|
||||
func (l *Library) syncProject(p *Project, res *Result) {
|
||||
if fi, err := os.Stat(p.Dir); err != nil || !fi.IsDir() {
|
||||
if len(p.Skills) > 0 || len(p.Servers) > 0 || len(p.Subagents) > 0 || len(p.Plugins) > 0 || len(p.ServersOff) > 0 || len(p.SkillsOff) > 0 || p.Instr != "" && len(p.InstrAgents) > 0 || p.holds() {
|
||||
if len(p.Skills) > 0 || len(p.Servers) > 0 || len(p.Subagents) > 0 || len(p.Plugins) > 0 || len(p.ClaudePlugins) > 0 || len(p.ServersOff) > 0 || len(p.SkillsOff) > 0 || p.Instr != "" && len(p.InstrAgents) > 0 || p.holds() {
|
||||
res.Problems = append(res.Problems, Problem{Agent: p.Dir, What: "project:", Error: "the folder is gone"})
|
||||
}
|
||||
return
|
||||
@@ -246,6 +252,7 @@ func (l *Library) syncFolder(p *Project, res *Result, hubs map[string]*Server) {
|
||||
l.syncProjectSubagents(p, func(what string, err error) { fail(what, err) })
|
||||
l.syncProjectInstr(p, fail)
|
||||
l.syncProjectHooks(p, fail)
|
||||
l.syncProjectClaudePlugins(p, fail)
|
||||
p.tidy()
|
||||
}
|
||||
|
||||
@@ -545,7 +552,7 @@ func RemoveProject(dir string, keep bool) (*Result, error) {
|
||||
l.Projects = slices.DeleteFunc(l.Projects, func(x *Project) bool { return x == p })
|
||||
return nil
|
||||
}
|
||||
p.Skills, p.Servers, p.ServersOff, p.SkillsOff, p.Subagents, p.InstrAgents, p.Plugins = nil, nil, nil, nil, nil, nil, nil
|
||||
p.Skills, p.Servers, p.ServersOff, p.SkillsOff, p.Subagents, p.InstrAgents, p.Plugins, p.ClaudePlugins = nil, nil, nil, nil, nil, nil, nil, nil
|
||||
l.syncProject(p, &Result{})
|
||||
if !p.holds() && p.Claude == nil && !slices.ContainsFunc(p.Trees, (*Project).holds) {
|
||||
l.Projects = slices.DeleteFunc(l.Projects, func(x *Project) bool { return x == p })
|
||||
@@ -642,7 +649,10 @@ type ProjectView struct {
|
||||
// Plugins are the library's plugins the project has, for the agents
|
||||
// named
|
||||
Plugins map[string][]string `json:"plugins"`
|
||||
Missing bool `json:"missing,omitempty"` // the folder is gone
|
||||
// ClaudePlugins are the plugins Claude Code has installed that the
|
||||
// project turns on for itself
|
||||
ClaudePlugins []string `json:"claudePlugins"`
|
||||
Missing bool `json:"missing,omitempty"` // the folder is gone
|
||||
// Problems are, by skill ("" for the project's own), what couldn't be done
|
||||
Problems map[string]string `json:"problems,omitempty"`
|
||||
}
|
||||
@@ -684,6 +694,7 @@ func projectViews(l *Library, problems []Problem) []ProjectView {
|
||||
for k, a := range p.Plugins {
|
||||
v.Plugins[k] = append([]string{}, a...)
|
||||
}
|
||||
v.ClaudePlugins = append([]string{}, p.ClaudePlugins...)
|
||||
for _, t := range p.Trees {
|
||||
v.Trees = append(v.Trees, t.Dir)
|
||||
}
|
||||
|
||||
@@ -568,13 +568,16 @@ type View struct {
|
||||
NewSkills []NewSkill `json:"newSkills"`
|
||||
// CheckFolders is whether a check looks in folders the user added
|
||||
// skills from, which the page then offers with no remote skill
|
||||
CheckFolders bool `json:"checkFolders,omitempty"`
|
||||
Projects []ProjectView `json:"projects"`
|
||||
Instructions *InstructionsView `json:"instructions"`
|
||||
Dir string `json:"dir"`
|
||||
Backups string `json:"backups"`
|
||||
SkillGroups []SkillGroup `json:"skillGroups"`
|
||||
CopySkills bool `json:"copySkills"` // the library gives skills as copies (#896)
|
||||
CheckFolders bool `json:"checkFolders,omitempty"`
|
||||
Projects []ProjectView `json:"projects"`
|
||||
// ClaudeInstalled are the plugins Claude Code has installed itself,
|
||||
// which a project can turn on for its folder (project_claude_plugins.go)
|
||||
ClaudeInstalled []ClaudePluginView `json:"claudeInstalled"`
|
||||
Instructions *InstructionsView `json:"instructions"`
|
||||
Dir string `json:"dir"`
|
||||
Backups string `json:"backups"`
|
||||
SkillGroups []SkillGroup `json:"skillGroups"`
|
||||
CopySkills bool `json:"copySkills"` // the library gives skills as copies (#896)
|
||||
// SharedSkills: every skill on here is in ~/.agents/skills too
|
||||
// (skills_shared.go), at SharedDir
|
||||
SharedSkills bool `json:"sharedSkills"`
|
||||
@@ -869,6 +872,9 @@ func readView(problems []Problem, glance bool) (*View, error) {
|
||||
v.NewSkills = newSkills(l)
|
||||
v.CheckFolders = l.addedFromFolders()
|
||||
v.Projects = projectViews(l, problems)
|
||||
if v.ClaudeInstalled = ReadClaudeInstalled(); v.ClaudeInstalled == nil {
|
||||
v.ClaudeInstalled = []ClaudePluginView{}
|
||||
}
|
||||
v.SkillGroups = l.skillGroups()
|
||||
v.CheckDue = checkDue(l)
|
||||
if glance {
|
||||
|
||||
Reference in New Issue
Block a user