Files
magpie/internal/library/claude_plugins.go
T
yetone 9afec644cc sync: Claude Code's own plugins go with the library (AXmake110 on X)
GitHub sync and backups carried the skills a Claude plugin brought into
magpie's library, but not the plugins installed in Claude Code itself
through its /plugin marketplaces, so a second computer had none of them.

The library bundle now carries ClaudePlugins: each marketplace's source
(from plugins/known_marketplaces.json and settings.json's
extraKnownMarketplaces, only `source` and `autoUpdate` kept) and the
plugin ids enabledPlugins has on, under CLAUDE_CONFIG_DIR when set.
installLocation, installPath and plugins/{marketplaces,cache} stay, as
Claude Code fetches them again. A marketplace from a local folder or
file, or with a user/token in a URL or request headers, is not carried,
and neither are its plugins. claude-plugins-official itself is not
carried (Claude Code adds it), its plugins are.

On the other computer they are added to settings.json's
extraKnownMarketplaces and enabledPlugins, which Claude Code installs
from at its next interactive start: tried with Claude Code 2.1.319 in a
sandbox CLAUDE_CONFIG_DIR, commit-commands@claude-code-plugins was
cloned and installed from those two settings alone. settings.json is
patched in place (comments and other keys kept), written to a temp file
and renamed in, and the old file is kept with the backups. Claude Code's
own known_marketplaces.json and installed_plugins.json are never
written. Only adding: a marketplace known here under that name, and a
plugin with any value here (false too), stay as they are. An unreadable
settings.json is neither carried as "none" nor written over. A computer
with no such plugins carries nil, so its library hash is as before. The
davsync library part counts it as the user's (not unset, #5642), and the
preview lists each marketplace and plugin.

Run (mote):
- go test -tags nogui ./... -timeout 45m -count=1 at 5d12d65dc (on
  origin/main 442964d70): ok, 101 packages, no FAIL
- rebased on becc1ef15 (bili path change in internal/library), then at
  this head: go test ./internal/library ./internal/davsync
  ./internal/backup . -count=1: ok
- go vet ./internal/library ./internal/davsync ./internal/backup, and
  GOOS=linux / GOOS=windows go vet of library and davsync: ok
- gofmt -l: clean
Broken by hand: carriedClaudePlugins and putClaudePlugins returning nil,
the unset clause and the preview branch removed: TestClaudePluginsCollected,
TestClaudePluginsPut, TestClaudePluginsInLibraryPart and
TestClaudePluginsInPreview red. A first version added x@evil for a
carried marketplace that wasn't portable; TestClaudePluginsPut caught it.
The first full run was red only on TestNoEagerPackageRegexps (a
package-level regexp); now lazyre.New.

Not tried: a real two-computer GitHub sync with a real marketplace;
`claude -p` and `claude plugin list` do not install declared plugins,
only an interactive start does. Not yet: a plugin removed or turned off
on one computer is not removed on the others (add-only). GUI sync labels
unchanged (the preview shows each item by name), so no GUI run.
2026-11-15 02:46:01 +08:00

233 lines
7.9 KiB
Go

package library
import (
"encoding/json"
"errors"
"log"
"maps"
"os"
"path/filepath"
"slices"
"strings"
"time"
"github.com/tidwall/gjson"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/lazyre"
)
// Claude Code's own plugins, the ones installed in Claude Code through its
// /plugin marketplaces (AXmake110 on X), go with the library: the
// marketplaces it knows and the plugins turned on for the user, so a
// computer that syncs ends up with the same plugins. They are not the
// library's plugin bundles (plugin.go), which magpie keeps and gives to
// every agent itself.
//
// Claude Code keeps them in its folder (claudeDir, CLAUDE_CONFIG_DIR too):
//
// plugins/known_marketplaces.json {name: {source: {source: "github", repo}, installLocation, lastUpdated}}
// plugins/installed_plugins.json {version: 2, plugins: {"p@m": [{scope, installPath, version, …}]}}
// settings.json enabledPlugins: {"p@m": true}, extraKnownMarketplaces: {name: {source}}
//
// What is carried is each marketplace's source and the user's enabled
// plugin ids. installLocation, installPath and the downloaded code in
// plugins/marketplaces and plugins/cache are this computer's, and Claude
// Code fetches them again from the sources. On the other computer they
// are written where Claude Code reads a declared setup from:
// settings.json's extraKnownMarketplaces and enabledPlugins. At its next
// interactive start Claude Code clones the marketplaces and installs the
// plugins into its own files (tried with Claude Code 2.1.319 in a sandbox
// CLAUDE_CONFIG_DIR: commit-commands@claude-code-plugins installed from
// those two settings alone; `claude -p` does not install them).
//
// Only adding: a marketplace or plugin the other computer has under that
// name, enabled or turned off there, stays as it is there, and a plugin
// removed on one computer is not removed on the others.
type ClaudePlugins struct {
// Marketplaces are by name, each as extraKnownMarketplaces holds one:
// {"source": {…}} and its autoUpdate when set.
Marketplaces map[string]json.RawMessage `json:"marketplaces,omitempty"`
// Enabled are the plugins turned on for the user, as plugin@marketplace.
Enabled []string `json:"enabled,omitempty"`
}
// claudeOfficial is the marketplace Claude Code adds by itself: its
// plugins are carried, the marketplace is not (a settings entry under its
// name adds nothing, as Claude Code says).
const claudeOfficial = "claude-plugins-official"
func claudeSettings() string { return filepath.Join(claudeDir(), "settings.json") }
func claudeMarketplaces() string {
return filepath.Join(claudeDir(), "plugins", "known_marketplaces.json")
}
func claudeInstalled() string { return filepath.Join(claudeDir(), "plugins", "installed_plugins.json") }
// readClaudeJSON is a file of Claude Code's as JSON: nil when there is
// none or it is empty, an error when it can't be read as an object.
func readClaudeJSON(path string) ([]byte, error) {
doc, err := readHooksFile(path)
if errors.Is(err, os.ErrNotExist) {
return nil, nil
}
return doc, err
}
// userinfo is a URL with a user (and maybe a password or token) in it.
var userinfo = lazyre.New(`(?i)[a-z][a-z0-9+.-]*://[^/\s"@]+@`)
// portableMarketplace is a marketplace's entry as it goes, or false when it
// stays on this computer: a folder or a file of this computer's, or a
// source with a sign-in in it (a token in a git URL, a request header).
func portableMarketplace(entry gjson.Result) (json.RawMessage, bool) {
src := entry.Get("source")
if !src.IsObject() {
return nil, false
}
switch src.Get("source").String() {
case "", "directory", "file":
return nil, false
}
if src.Get("path").Exists() || src.Get("headers").Exists() || userinfo.Regexp().MatchString(src.Raw) {
return nil, false
}
out := map[string]json.RawMessage{"source": json.RawMessage(src.Raw)}
if a := entry.Get("autoUpdate"); a.IsBool() {
out["autoUpdate"] = json.RawMessage(a.Raw)
}
j, err := json.Marshal(out)
if err != nil {
return nil, false
}
return j, true
}
// carriedClaudePlugins is this computer's Claude Code plugins as they go,
// nil when there are none, or when a file of them can't be read: unknown
// is never carried as none.
func carriedClaudePlugins() *ClaudePlugins {
known, err := readClaudeJSON(claudeMarketplaces())
if err != nil {
return nil
}
set, err := readClaudeJSON(claudeSettings())
if err != nil {
return nil
}
cp := &ClaudePlugins{Marketplaces: map[string]json.RawMessage{}}
add := func(r gjson.Result) {
r.ForEach(func(k, v gjson.Result) bool {
name := k.String()
if _, ok := cp.Marketplaces[name]; ok || name == claudeOfficial {
return true
}
if j, ok := portableMarketplace(v); ok {
cp.Marketplaces[name] = j
}
return true
})
}
add(gjson.GetBytes(set, "extraKnownMarketplaces"))
add(gjson.ParseBytes(known))
gjson.GetBytes(set, "enabledPlugins").ForEach(func(k, v gjson.Result) bool {
id := k.String()
_, m, ok := strings.Cut(id, "@")
if !ok || v.Type != gjson.True {
return true
}
if _, carried := cp.Marketplaces[m]; carried || m == claudeOfficial {
cp.Enabled = append(cp.Enabled, id)
}
return true
})
slices.Sort(cp.Enabled)
if len(cp.Enabled) == 0 && len(cp.Marketplaces) == 0 {
return nil
}
if len(cp.Marketplaces) == 0 {
cp.Marketplaces = nil
}
return cp
}
// claudePluginsChanged is when Claude Code last added or took away a
// marketplace or a plugin here, zero when it never did.
func claudePluginsChanged() time.Time {
var t time.Time
for _, p := range []string{claudeMarketplaces(), claudeInstalled()} {
if fi, err := os.Stat(p); err == nil && fi.ModTime().After(t) {
t = fi.ModTime()
}
}
return t
}
// putClaudePlugins adds to Claude Code's settings.json the marketplaces and
// enabled plugins cp carries that it doesn't have yet, written the way
// Claude Code writes it (a temp file renamed in, every other setting as
// it was); the file it replaces is kept with the change's backups. A
// marketplace this computer knows under that name, and a plugin it has
// any value for (turned off here too), is left as it is. A settings.json
// or known_marketplaces.json that can't be read is not written to.
func putClaudePlugins(cp *ClaudePlugins, kept *backups) error {
p := claudeSettings()
set, err := readClaudeJSON(p)
if err != nil {
log.Printf("library: Claude Code's plugins not added: %v", err)
return nil
}
known, err := readClaudeJSON(claudeMarketplaces())
if err != nil {
log.Printf("library: Claude Code's plugins not added: %v", err)
return nil
}
have := func(name string) bool {
return name == claudeOfficial || gjson.GetBytes(set, "extraKnownMarketplaces."+gjson.Escape(name)).Exists() ||
gjson.GetBytes(known, gjson.Escape(name)).Exists()
}
var kvs []edit.KV
came := map[string]bool{} // the carried marketplaces fit to add
for _, name := range slices.Sorted(maps.Keys(cp.Marketplaces)) {
var v any
if strings.TrimSpace(name) == "" || json.Unmarshal(cp.Marketplaces[name], &v) != nil {
continue
}
if _, ok := portableMarketplace(gjson.ParseBytes(cp.Marketplaces[name])); !ok {
continue
}
came[name] = true
if !have(name) {
kvs = append(kvs, edit.KV{Path: "extraKnownMarketplaces." + gjson.Escape(name), Value: v})
}
}
for _, id := range cp.Enabled {
_, m, ok := strings.Cut(id, "@")
if !ok || gjson.GetBytes(set, "enabledPlugins."+gjson.Escape(id)).Exists() {
continue
}
if !came[m] && !have(m) {
continue
}
kvs = append(kvs, edit.KV{Path: "enabledPlugins." + gjson.Escape(id), Value: true})
}
if len(kvs) == 0 {
return nil
}
raw, err := edit.Read(p)
if err != nil && !errors.Is(err, os.ErrNotExist) {
return err
}
out, err := edit.PatchJSON(raw, kvs, nil)
if err != nil {
log.Printf("library: Claude Code's plugins not added to %s: %v", p, err)
return nil
}
if err := kept.keep("claude", p); err != nil {
return err
}
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
return err
}
return edit.WriteAtomic(p, out)
}