Files
magpie/internal/cli/library_project_cli.go
yetone 06db81530e library: a project's choices exported from the CLI and applied at another folder (#7278)
jinpwng asked to carry one project's Library choices, with what they
need, to a new path (a new checkout, another machine) from the CLI.

- `magpie library project export <dir> [file|-]` writes ExportProject's
  JSON (kind magpie-project, version 1): the project's skills, servers,
  subagents, hooks and plugins by agent, its Claude Code plugins, Pi
  packages, servers and skills turned off, and its instructions, plus
  need: each hook's and subagent's definition, each skill's and plugin's
  remote source; a plugin's own hooks and subagents go by their plugin.
  No ownership record (Placed, HookRecs, AgentFiles, PiRecs, ClaudeWrote,
  InstrFiles), key, sign-in or other project goes in; a server goes by
  name only, since its settings can hold a key. The file is 0600.
- `apply <file|-> <dir>` (also `import`) makes the folder a project,
  saves a hook or subagent the library here lacks from the export (for no
  agent of its own), and gives each choice through the setters the page
  uses: the agents the folder has for it kept, the export's added, so a
  second apply changes nothing and the folder's own entries stay. A Claude
  plugin or Pi package the folder has already is left as it is;
  instructions only when it has none. What this library lacks (a skill,
  server or plugin) is named with its source and exits 1; nothing is
  fetched.

Run (mote): go test ./internal/library -run 'TestProjectExportApply'
-count=1: ok; the full suite is in the push record.
Real CLI (built binary, two sandbox HOMEs with stub claude and pi on
PATH): in A, project add, hook bark and subagent checker for claude, Pi
npm:pi-demo, export; A deleted; in B (its own "echo mine" Stop hook in
.claude/settings.local.json) apply saved bark and checker and printed
"has what the export has", a second import printed the same and added
nothing; B's settings.local.json held SessionStart "echo bark" beside
"echo mine", .claude/agents/checker.md was written and .pi/settings.json
listed npm:pi-demo.
Broken by hand: with the hook save in ApplyProject skipped,
TestProjectExportApply is red.
Not tried: the reporter's own projects; a Windows path in an export.
2026-11-20 12:06:44 +08:00

614 lines
20 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package cli
import (
"encoding/json"
"fmt"
"io"
"os"
"slices"
"sort"
"strings"
"github.com/yetone/magpie/internal/library"
)
const libraryProjectUsage = `magpie library project [--json] the projects (Library › Projects) and the skills each has
magpie library project add <dir> add a folder whose agents can be given library skills
magpie library project rm <dir> [--keep-files] take it off; --keep-files leaves what magpie put there, yours from then on
magpie library project skills <dir> [--json] its skills, for which agents, and where each is placed
magpie library project skill add <dir> <skill…> [agents=a,b…] give it skills: by default for the agents its skills
are for already, or, with none yet, every agent here that reads a project's skills
magpie library project skill rm <dir> <skill…> take skills out of it
magpie library project skill agents <dir> <skill> <a,b…|none> the agents it has the skill for
magpie library project copy <dir> on|off copies of its skills in place of links to the library's
magpie library project server [list] <dir> [--json] its MCP servers and the agents each is for
magpie library project server add <dir> <server…> [agents=a,b…] give it library servers: by default for the agents
its servers are for already, or, with none yet, every agent here that reads a
project's MCP file
magpie library project server rm <dir> <server…> take servers out of it
magpie library project server agents <dir> <server> <a,b…|none> the agents it has the server for
magpie library project subagent [list] <dir> [--json] its subagents and the agents each is for
magpie library project subagent add <dir> <subagent…> [agents=a,b…] the same for library subagents
magpie library project subagent rm <dir> <subagent…> take subagents out of it
magpie library project subagent agents <dir> <subagent> <a,b…|none> the agents it has the subagent for
magpie library project hook [list] <dir> [--json] its own hooks and the agents each is for
magpie library project hook add <dir> <hook…> [agents=a,b…] give it library hooks, one at a time, written into
.claude/settings.local.json and .codex/hooks.json beside what's there
magpie library project hook rm <dir> <hook…> take them out, only the entries magpie wrote
magpie library project hook agents <dir> <hook> <a,b…|none> the agents it has the hook for
magpie library project pi [list] <dir> [--json] its packages for Pi (.pi/settings.json) and the project's own
magpie library project pi add|on <dir> <source…> give it packages for Pi: npm:<name>[@version] or a git source; Pi
installs them when it next starts there
magpie library project pi off <dir> <source…> keep them listed but loading nothing; on turns them on again
magpie library project pi rm <dir> <source…> take them out, only what magpie wrote
magpie library project pi adopt <dir> <source…> switch a package the project declares itself, left as written
magpie library project instructions <dir> [--json] its own instructions, the agents they're for and the files
magpie library project instructions <dir> text <text…>|--file <f>|- set them ("" takes them out); written, between
magpie's two marker lines, into the project file each agent reads: by default for
the agents the shared instructions are on for, else every agent here that reads one
magpie library project instructions <dir> agents <a,b…|none> the agents whose project file they're written into
magpie library project export <dir> [file] what the project has, and the hooks, subagents and sources that
takes, as JSON (to the file, or printed): no key, sign-in or other project
magpie library project apply <file|-> <dir> give a folder (made a project) what an export has: added to what
it has already, so again changes nothing; what this library lacks is named
<dir> may be relative, or "." for the folder you're in. A skill that couldn't be
placed (a folder there that isn't magpie's is left as it is) is named, and the
command exits with status 1.
`
// libraryProjectCmd is magpie library project …: the window's Library ›
// Projects page from a terminal (#5193), through the same library calls
// its page makes.
func libraryProjectCmd(args []string) error {
usage := func() error { return fmt.Errorf("usage:\n %s", libraryProjectUsage) }
if len(args) == 0 || len(args) == 1 && (args[0] == "list" || args[0] == "--json") || len(args) == 2 && args[0] == "list" && args[1] == "--json" {
return listProjects("", slices.Contains(args, "--json"))
}
switch sub, rest := args[0], args[1:]; sub {
case "help", "-h", "--help":
fmt.Print(" " + libraryProjectUsage)
return nil
case "add":
if len(rest) != 1 || strings.HasPrefix(rest[0], "-") {
return usage()
}
dir, err := library.ProjectPath(rest[0])
if err != nil {
return err
}
if _, err := library.AddProject(dir); err != nil {
return err
}
fmt.Println(green.Render("✓"), dir, "is a project")
fmt.Println(muted.Render(" magpie library project skill add " + quoteArg(dir) + " <skill…> gives it skills"))
return nil
case "rm", "remove":
keep := false
var dirs []string
for _, a := range rest {
switch {
case a == "--keep-files":
keep = true
case strings.HasPrefix(a, "-"):
return usage()
default:
dirs = append(dirs, a)
}
}
if len(dirs) != 1 {
return usage()
}
dir, err := projectDir(dirs[0])
if err != nil {
return err
}
if _, err := library.RemoveProject(dir, keep); err != nil {
return err
}
// what couldn't be taken out keeps the project listed
if p, _ := library.ProjectAt(dir); p != "" {
return fmt.Errorf("%s stays a project: magpie couldn't take everything it placed out of it (magpie library project skills %s)", dir, quoteArg(dir))
}
if keep {
fmt.Println(green.Render("✓"), dir, "is no project now; what magpie put there is left as it is")
} else {
fmt.Println(green.Render("✓"), dir, "is no project now; what magpie put there is taken out")
}
return nil
case "skills":
asJSON := slices.Contains(rest, "--json")
rest = slices.DeleteFunc(slices.Clone(rest), func(a string) bool { return a == "--json" })
if len(rest) != 1 || strings.HasPrefix(rest[0], "-") {
return usage()
}
dir, err := projectDir(rest[0])
if err != nil {
return err
}
return listProjects(dir, asJSON)
case "copy":
if len(rest) != 2 || strings.HasPrefix(rest[0], "-") || rest[1] != "on" && rest[1] != "off" {
return usage()
}
dir, err := projectDir(rest[0])
if err != nil {
return err
}
res, err := library.ProjectCopy(dir, rest[1] == "on")
if err != nil {
return err
}
if err := projectOutcome(dir, nil, res); err != nil {
return err
}
if rest[1] == "on" {
fmt.Println(green.Render("✓"), dir, "gets copies of its skills, made again whenever the library's skill changes")
} else {
fmt.Println(green.Render("✓"), dir, "gets links to the library's skills")
}
return nil
case "skill":
if len(rest) == 0 {
return usage()
}
return projectSkillCmd(rest[0], rest[1:], usage)
case "server", "mcp":
return projectKindCmd(projectServers, rest, usage)
case "subagent":
return projectKindCmd(projectSubagents, rest, usage)
case "hook":
return projectKindCmd(projectHooks, rest, usage)
case "pi":
return projectPiCmd(rest, usage)
case "instructions", "instr":
return projectInstrCmd(rest, usage)
case "export":
return projectExportCmd(rest, usage)
case "apply", "import":
return projectApplyCmd(rest, usage)
}
return fmt.Errorf("no library project command %q\n %s", args[0], libraryProjectUsage)
}
func projectSkillCmd(verb string, rest []string, usage func() error) error {
if len(rest) < 2 || strings.HasPrefix(rest[0], "-") {
return usage()
}
dir, err := projectDir(rest[0])
if err != nil {
return err
}
v, err := library.Read(nil)
if err != nil {
return err
}
p := projectView(v, dir)
if p == nil {
return fmt.Errorf("%s isn't a project", dir)
}
switch verb {
case "add":
var names, agents []string
given := false
for _, a := range rest[1:] {
if s, ok := strings.CutPrefix(a, "agents="); ok {
if agents, err = projectAgents(s); err != nil {
return err
}
given = true
continue
}
if strings.HasPrefix(a, "-") {
return usage()
}
names = addOnce(names, a)
}
if len(names) == 0 {
return usage()
}
if !given {
agents = defaultProjectAgents(v, p)
}
if len(agents) == 0 {
return fmt.Errorf("no agent here reads a project's skills: name them with agents=a,b…")
}
res, err := library.ProjectSkills(dir, names, agents)
if err != nil {
return err
}
return projectOutcome(dir, names, res)
case "rm", "remove":
var names []string
for _, a := range rest[1:] {
if strings.HasPrefix(a, "-") {
return usage()
}
if _, ok := p.Skills[a]; !ok {
return fmt.Errorf("%s has no skill %s", dir, a)
}
names = addOnce(names, a)
}
res, err := library.ProjectSkills(dir, names, nil)
if err != nil {
return err
}
if err := projectOutcome(dir, nil, res); err != nil {
return err
}
fmt.Println(green.Render("✓"), strings.Join(names, ", "), "taken out of", dir)
return nil
case "agents":
if len(rest) != 3 || strings.HasPrefix(rest[1], "-") || strings.HasPrefix(rest[2], "-") {
return usage()
}
agents, err := projectAgents(rest[2])
if err != nil {
return err
}
res, err := library.ProjectSkill(dir, rest[1], agents)
if err != nil {
return err
}
if len(agents) == 0 {
if err := projectOutcome(dir, nil, res); err != nil {
return err
}
fmt.Println(green.Render("✓"), rest[1], "taken out of", dir)
return nil
}
return projectOutcome(dir, []string{rest[1]}, res)
}
return fmt.Errorf("no library project skill command %q\n %s", verb, libraryProjectUsage)
}
// addOnce adds s to a list once, in the order given.
func addOnce(list []string, s string) []string {
if slices.Contains(list, s) {
return list
}
return append(list, s)
}
// projectDir is the listed folder of the project typed, or why there's none.
func projectDir(typed string) (string, error) {
dir, err := library.ProjectAt(typed)
if err != nil {
return "", err
}
if dir == "" {
abs, _ := library.ProjectPath(typed)
return "", fmt.Errorf("%s isn't a project: magpie library project add %s adds it", abs, quoteArg(abs))
}
return dir, nil
}
// projectAgents is a comma list of agents (none for no agent), each by its
// id, refused when magpie knows of no folder it reads a project's skills
// from.
func projectAgents(s string) ([]string, error) {
ids, err := libraryAgents(s, "skills")
if err != nil {
return nil, err
}
for _, id := range ids {
if library.ProjectSkillsDir(id) == "" {
return nil, fmt.Errorf("magpie knows of no folder %s reads a project's skills from", id)
}
}
return ids, nil
}
// defaultProjectAgents is who a skill added with no agents= is for, as on
// the page's Skills menu: the agents the project's skills are for already,
// or, in a project with none yet, every agent here that reads a project's
// skills.
func defaultProjectAgents(v *library.View, p *library.ProjectView) []string {
var all []string
for _, a := range v.Agents {
if a.ProjectSkills != "" && !a.Gone && !a.Unseen {
all = append(all, a.ID)
}
}
var used []string
for _, ids := range p.Skills {
for _, id := range ids {
if slices.Contains(all, id) && !slices.Contains(used, id) {
used = append(used, id)
}
}
}
if len(used) > 0 {
sort.Strings(used)
return used
}
var out []string
for _, a := range v.Agents {
if slices.Contains(all, a.ID) && a.Asleep == "" {
out = append(out, a.ID)
}
}
return out
}
func projectView(v *library.View, dir string) *library.ProjectView {
for i := range v.Projects {
if v.Projects[i].Dir == dir {
return &v.Projects[i]
}
}
return nil
}
// projectOutcome says, for each skill named, where it was placed or why
// it wasn't, and the project's own problem; one not placed is an error,
// for a script to tell "installed" from "left as it is" by the status.
func projectOutcome(dir string, names []string, res *library.Result) error {
v, err := library.Read(res.Problems)
if err != nil {
return err
}
p := projectView(v, dir)
if p == nil {
return fmt.Errorf("%s isn't a project", dir)
}
if why := p.Problems[""]; why != "" {
fmt.Println(amber.Render("!"), dir+":", why)
}
var unplaced []string
for _, n := range names {
placed, why := skillPlaces(p, n)
if why != "" {
unplaced = append(unplaced, n)
fmt.Println(amber.Render("!"), n, muted.Render("not placed:"), why)
continue
}
fmt.Println(green.Render("✓"), n, "→", strings.Join(p.Skills[n], ", "), muted.Render(strings.Join(placed, " ")))
}
if len(unplaced) > 0 {
return fmt.Errorf("%s not placed in %s", strings.Join(unplaced, ", "), dir)
}
return nil
}
// skillPlaces are the entries of a project's skill magpie placed, and,
// for one its agents should have and don't, why.
func skillPlaces(p *library.ProjectView, name string) (placed []string, why string) {
want := map[string]bool{}
for _, id := range p.Skills[name] {
if d := library.ProjectSkillsDir(id); d != "" {
want[d+"/"+name] = true
}
}
for e := range want {
if slices.Contains(p.Placed, e) {
placed = append(placed, e)
}
}
sort.Strings(placed)
if why = p.Problems[name]; why == "" && len(placed) < len(want) {
why = "not in the project (magpie library sync says why)"
}
return placed, why
}
// listProjects prints the projects, or one, with their skills.
func listProjects(dir string, asJSON bool) error {
v, err := library.Read(nil)
if err != nil {
return err
}
ps := v.Projects
if dir != "" {
p := projectView(v, dir)
if p == nil {
return fmt.Errorf("%s isn't a project", dir)
}
ps = []library.ProjectView{*p}
}
if asJSON {
e := json.NewEncoder(os.Stdout)
e.SetIndent("", " ")
if dir != "" {
return e.Encode(ps[0])
}
return e.Encode(ps)
}
if len(ps) == 0 {
fmt.Println(muted.Render("No projects yet · magpie library project add <dir>"))
return nil
}
for _, p := range ps {
head := bold.Render(p.Name) + " " + muted.Render(p.Dir)
if p.Missing {
head += " " + amber.Render("(the folder is gone)")
} else if p.Copy {
head += " " + muted.Render("(copies)")
}
fmt.Println(head)
names := make([]string, 0, len(p.Skills))
for n := range p.Skills {
names = append(names, n)
}
sort.Strings(names)
if len(names) == 0 {
fmt.Println(" ", muted.Render("no skills yet · magpie library project skill add "+quoteArg(p.Dir)+" <skill…>"))
}
for _, n := range names {
placed, why := skillPlaces(&p, n)
if why != "" {
fmt.Printf(" %s %s → %s %s\n", amber.Render("!"), bold.Render(n), strings.Join(p.Skills[n], ", "), muted.Render(why))
continue
}
fmt.Printf(" %s → %s %s\n", bold.Render(n), strings.Join(p.Skills[n], ", "), muted.Render(strings.Join(placed, " ")))
}
}
return nil
}
// quoteArg is a path as a shell takes it back.
func quoteArg(s string) string {
if s != "" && !strings.ContainsAny(s, " \t'\"$`\\!*?[]{}()&;|<>#~") {
return s
}
return "'" + strings.ReplaceAll(s, "'", `'\''`) + "'"
}
// projectInstrCmd is magpie library project instructions …: the project's
// own instructions, as the Instructions page's project card sets them.
func projectInstrCmd(rest []string, usage func() error) error {
asJSON := slices.Contains(rest, "--json")
rest = slices.DeleteFunc(slices.Clone(rest), func(a string) bool { return a == "--json" })
if len(rest) == 0 || strings.HasPrefix(rest[0], "-") {
return usage()
}
dir, err := projectDir(rest[0])
if err != nil {
return err
}
if len(rest) == 1 {
return showProjectInstr(dir, asJSON)
}
var c library.ProjectInstr
switch rest[1] {
case "text":
var text string
switch args := rest[2:]; {
case len(args) == 1 && args[0] == "-":
b, err := io.ReadAll(os.Stdin)
if err != nil {
return err
}
text = string(b)
case len(args) == 2 && args[0] == "--file":
b, err := os.ReadFile(args[1])
if err != nil {
return err
}
text = string(b)
case len(args) > 0 && !strings.HasPrefix(args[0], "-"):
text = strings.Join(args, " ")
default:
return usage()
}
c.Text = &text
// the first text a project gets goes to the agents the shared
// instructions are on for, as the page's card does it
v, err := library.Read(nil)
if err != nil {
return err
}
if p := projectView(v, dir); p != nil && len(p.InstrAgents) == 0 && strings.TrimSpace(text) != "" {
ids := defaultInstrAgents(v)
if len(ids) == 0 {
return fmt.Errorf("no agent here reads a project's instructions file magpie knows of")
}
c.Agents = &ids
}
case "agents":
if len(rest) != 3 {
return usage()
}
ids, err := projectInstrAgents(rest[2])
if err != nil {
return err
}
c.Agents = &ids
default:
return usage()
}
res, err := library.ProjectInstructions(dir, c)
if err != nil {
return err
}
v, err := library.Read(res.Problems)
if err != nil {
return err
}
p := projectView(v, dir)
if p == nil {
return fmt.Errorf("%s isn't a project", dir)
}
if why := p.Problems["instructions"]; why != "" {
return fmt.Errorf("%s: %s", dir, why)
}
if p.Instr == "" || len(p.InstrAgents) == 0 {
fmt.Println(green.Render("✓"), dir, "has no instructions of its own in its files now")
return nil
}
fmt.Println(green.Render("✓"), dir, "→", strings.Join(p.InstrAgents, ", "), muted.Render(strings.Join(p.InstrFiles, " ")))
return nil
}
func showProjectInstr(dir string, asJSON bool) error {
v, err := library.Read(nil)
if err != nil {
return err
}
p := projectView(v, dir)
if p == nil {
return fmt.Errorf("%s isn't a project", dir)
}
if asJSON {
e := json.NewEncoder(os.Stdout)
e.SetIndent("", " ")
return e.Encode(map[string]any{"dir": p.Dir, "text": p.Instr, "agents": p.InstrAgents, "files": p.InstrFiles})
}
if p.Instr == "" {
fmt.Println(muted.Render("No instructions of its own · magpie library project instructions " + quoteArg(dir) + " text <text…>"))
return nil
}
if len(p.InstrAgents) == 0 {
fmt.Println(muted.Render("for no agent · magpie library project instructions " + quoteArg(dir) + " agents <a,b…>"))
} else {
fmt.Println(bold.Render(strings.Join(p.InstrAgents, ", ")), muted.Render(strings.Join(p.InstrFiles, " ")))
}
if why := p.Problems["instructions"]; why != "" {
fmt.Println(amber.Render("!"), why)
}
fmt.Println(p.Instr)
return nil
}
// projectInstrAgents is a comma list of agents (none for no agent), each
// by its id or name, refused when magpie knows of no file it reads a
// project's instructions from.
func projectInstrAgents(s string) ([]string, error) {
out := []string{}
for _, a := range agentList(s) {
id := strings.ToLower(a)
if library.ProjectInstrFile(id) == "" {
if t, err := library.Takes(a, "skills"); err == nil {
id = t
}
}
if library.ProjectInstrFile(id) == "" {
return nil, fmt.Errorf("magpie knows of no file %s reads a project's instructions from", a)
}
out = addOnce(out, id)
}
return out, nil
}
// defaultInstrAgents is who a project's first instructions are for: the
// agents here that read a project's instructions and have the shared ones
// on, else every one of them here.
func defaultInstrAgents(v *library.View) []string {
var all, on []string
for _, a := range v.Agents {
if a.ProjectInstructions != "" && !a.Gone && !a.Unseen && a.Asleep == "" {
all = append(all, a.ID)
}
}
if v.Instructions != nil {
for _, a := range v.Instructions.Agents {
if a.On && slices.Contains(all, a.Agent) {
on = append(on, a.Agent)
}
}
}
if len(on) > 0 {
return on
}
return all
}