Files
magpie/internal/library/project_plugins.go
T
yetone 0417417e57 library: a project takes a whole Claude plugin, and gives it back (jinpwng on X)
A project can now have a library Claude plugin for chosen agents
(projects/plugin). Its skills, MCP servers and subagents go where the
project's own do (withPlugins adds them for a sync and puts the project's
own sets back after), and its hooks are written entry by entry into
.claude/settings.local.json and .codex/hooks.json (HookRecs). Taking the
plugin out deletes it from Plugins, and only what magpie placed or wrote
comes out; an entry the user changed stays. Removing a plugin from the
library drops it from every project. The Library plugins tab gets a
Project plugins section with per-agent chips, in en, zh, zh-TW, ja, de.

Run, on mote at the pushed head (rebased on origin/main 5d4f9862c):
- go test -count=1 ./internal/library ./internal/gui: both ok
  (204.9s, 181.3s); the same two ok at the earlier base 352b124b3.
- gofmt -l on internal/library and internal/gui: clean.

Not run before this push (mote and mica were both at load 27 to 51 under
other sessions' full suites; pushed at the owner's word to ship at once):
the full go test ./..., Linux and Windows vet, the whole GUI suite in
Chromium and WebKit. They are queued behind those runs and their result
will be filed as an issue per red. Not tried with a real project and a
real plugin outside the tests.
2026-11-19 23:48:47 +08:00

263 lines
7.7 KiB
Go

package library
import (
"errors"
"fmt"
"path/filepath"
"slices"
"sort"
)
// A project's plugins (jinpwng on X): a plugin the library keeps is given
// to a project whole, for some of its agents, and taken out of it whole.
// Its skills, MCP servers and subagents go where the project's own do
// (projects.go, project_mcp.go, project_subagents.go), beside them: for
// a sync, syncProject adds them to the project's own sets (withPlugins),
// and puts those back as they were after. So a part the project also has
// of its own stays when the plugin goes, and taking the plugin out is
// only deleting it from Plugins: what magpie placed or wrote for it,
// and nothing else, comes out on that sync, by the records each part
// keeps already (Placed, Wrote, AgentFiles). Its hooks are written in the
// project's own hooks file (projectHookFiles), entry by entry, as the
// agent's own are (hookWrite): an entry the user changed since is theirs
// and stays.
// projectHookFiles are the files, in a project, an agent reads hooks
// from that magpie writes a project's plugins' hooks into: Claude Code's
// .claude/settings.local.json (its local project settings, which it
// keeps out of git itself), and Codex's .codex/hooks.json, which
// codex-rs/hooks loads from a project's .codex folder in a project the
// user trusts, each hook in it once the user has reviewed it in /hooks.
var projectHookFiles = map[string]string{
"claude": claudeSettingsRel,
"codex": ".codex/hooks.json",
}
// ProjectHooksFile is the file in a project an agent reads hooks from:
// "" for one magpie can't give a project's hooks to.
func ProjectHooksFile(agent string) string { return projectHookFiles[agent] }
// projectPluginTakes says whether an agent takes any part of a plugin in
// a project.
func projectPluginTakes(id string) bool {
return ProjectSkillsDir(id) != "" || ProjectMCPFile(id) != "" || ProjectSubagentsDir(id) != "" || ProjectHooksFile(id) != ""
}
func projectHookSpec(p *Project, id string) *hookSpec {
rel := projectHookFiles[id]
abs := filepath.Join(p.Dir, filepath.FromSlash(rel))
switch id {
case "claude":
return claudeHookSpec(abs)
case "codex":
return codexHookSpec(abs)
}
return nil
}
// withPlugins are the project's skills, servers and subagents with the
// parts of its plugins added, each for those of the plugin's agents that
// take it in a project: new maps, the project's own left as they are. A
// plugin turned off, or gone from the library, gives nothing; a part
// taken out of the library one by one, or one that is another's by now,
// isn't the plugin's.
func (l *Library) withPlugins(p *Project) (skills, servers, subagents map[string][]string) {
if len(p.Plugins) == 0 {
return p.Skills, p.Servers, p.Subagents
}
clone := func(m map[string][]string) map[string][]string {
out := make(map[string][]string, len(m))
for k, v := range m {
out[k] = slices.Clone(v)
}
return out
}
skills, servers, subagents = clone(p.Skills), clone(p.Servers), clone(p.Subagents)
add := func(m map[string][]string, name string, agents []string, takes func(string) bool) {
for _, a := range agents {
if takes(a) {
m[name] = set(m[name], a, true)
}
}
}
for name, agents := range p.Plugins {
b := l.plugin(name)
if b == nil || b.Off {
continue
}
for _, n := range b.Skills {
if s := l.skill(n); s != nil && s.Bundle == b.Name {
add(skills, n, agents, func(a string) bool { return ProjectSkillsDir(a) != "" })
}
}
for _, n := range b.MCP {
if s := l.server(n); s != nil && s.Bundle == b.Name {
add(servers, n, agents, func(a string) bool { return ProjectMCPFile(a) != "" })
}
}
for _, n := range b.Subagents {
if s := l.subagent(n); s != nil && s.Bundle == b.Name {
add(subagents, n, agents, func(a string) bool { return ProjectSubagentsDir(a) != "" })
}
}
}
return skills, servers, subagents
}
// projectHooks are, by agent, the hooks the project's plugins give it.
func (l *Library) projectHooks(p *Project) map[string][]*Hook {
out := map[string][]*Hook{}
for _, name := range slices.Sorted(func(yield func(string) bool) {
for k := range p.Plugins {
if !yield(k) {
return
}
}
}) {
b := l.plugin(name)
if b == nil || b.Off {
continue
}
for _, n := range b.Hooks {
h := l.hook(n)
if h == nil || h.Bundle != b.Name {
continue
}
for _, a := range p.Plugins[name] {
if ProjectHooksFile(a) != "" && hookTakes(h, a) && !slices.Contains(out[a], h) {
out[a] = append(out[a], h)
}
}
}
}
return out
}
// syncProjectHooks writes the hooks of the project's plugins in each
// agent's project hooks file, and takes out those magpie wrote there that
// no plugin gives any more. A file magpie made is taken away again once
// nothing is left in it.
func (l *Library) syncProjectHooks(p *Project, fail func(what string, err error)) {
want := l.projectHooks(p)
ids := map[string]bool{}
for id := range want {
ids[id] = true
}
for id := range p.HookRecs {
ids[id] = true
}
for _, id := range slices.Sorted(func(yield func(string) bool) {
for k := range ids {
if !yield(k) {
return
}
}
}) {
rel := projectHookFiles[id]
sp := projectHookSpec(p, id)
if sp == nil {
// an agent magpie wrote hooks for once and no longer can:
// its records go, the entries stay the user's
delete(p.HookRecs, id)
continue
}
if err := p.inside(sp.path); err != nil {
for _, h := range want[id] {
fail("hook:"+h.Name, err)
}
continue
}
made := false
if len(want[id]) > 0 && !exists(sp.path) {
if err := p.mkdirs(filepath.Dir(sp.path)); err != nil {
for _, h := range want[id] {
fail("hook:"+h.Name, err)
}
continue
}
made = true
}
w := &hookWrite{id: id, sp: sp, want: want[id], recs: p.HookRecs[id], fail: fail}
if id == "codex" {
w.rekeyCfg = filepath.Join(codexDir(), "config.toml")
}
recs, _, ok := w.run()
if !ok {
continue
}
if made && exists(sp.path) && !slices.Contains(p.MadeFiles, rel) {
p.MadeFiles = append(p.MadeFiles, rel)
sort.Strings(p.MadeFiles)
}
if len(recs) == 0 {
delete(p.HookRecs, id)
// Claude Code's file holds the skills the project turns off
// too (syncSkillsOff)
if id != "claude" || len(p.Overrides) == 0 {
p.dropMadeFile(rel)
}
continue
}
if p.HookRecs == nil {
p.HookRecs = map[string][]HookEntry{}
}
p.HookRecs[id] = recs
}
if len(p.HookRecs) == 0 {
p.HookRecs = nil
}
}
// ProjectPlugin sets which agents get a library plugin in a project: each
// of its parts goes to those of them that take it there. None takes the
// plugin out of the project, and with it only what magpie wrote for it.
func ProjectPlugin(dir, name string, agents []string) (*Result, error) {
for _, a := range agents {
if err := checkAgent(a); err != nil {
return nil, err
}
}
return change(func(l *Library) error {
p := l.project(dir)
if p == nil {
return fmt.Errorf("no project %s", dir)
}
if name == "" {
return errors.New("no plugin given")
}
var ids []string
for _, id := range agents {
if !projectPluginTakes(id) {
return fmt.Errorf("magpie knows of nowhere %s reads a project's skills, servers, subagents or hooks from", id)
}
ids = set(ids, id, true)
}
if len(ids) == 0 {
delete(p.Plugins, name)
if len(p.Plugins) == 0 {
p.Plugins = nil
}
return nil
}
if l.plugin(name) == nil {
return fmt.Errorf("no plugin called %s", name)
}
if p.Plugins == nil {
p.Plugins = map[string][]string{}
}
p.Plugins[name] = ids
return nil
})
}
// dropProjectPlugin takes a plugin out of every project, and their
// worktrees: it is leaving the library.
func (l *Library) dropProjectPlugin(name string) {
for _, p := range l.Projects {
delete(p.Plugins, name)
if len(p.Plugins) == 0 {
p.Plugins = nil
}
}
}