library: a plugin that is a Pi package, given to a project for Pi, is one entry of its .pi/settings.json (#7243)
A library plugin whose folder has a package.json with a `pi` object, given to a project with Pi among its agents, is now a wanted package of the project (projectPiWanted): its library folder, written as one entry through the same pipeline as a package given by source (#7249). Applied again it stays one entry; withdrawn (or the plugin turned off or removed) only the entry magpie wrote goes. A declaration the project has of the same folder, filtered or not, is left byte for byte and not reported, as it loads already. Run (mote): gofmt, go vet, go test ./internal/library (ok); the new TestProjectPluginPiPackage covers not-for-Pi, reapply, another project untouched, withdraw, a filtered native entry byte for byte, and withdrawal beside the project's other packages. Not tried: a real Pi resolving the package's skills and extension.
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"sort"
|
||||
@@ -109,7 +110,8 @@ func piIndex(list []json.RawMessage, source string) int {
|
||||
// syncProjectPi writes the packages the project wants into its
|
||||
// .pi/settings.json and takes out what magpie wrote that it no longer does.
|
||||
func (l *Library) syncProjectPi(p *Project, fail func(what string, err error)) {
|
||||
if len(p.Pi) == 0 && len(p.PiRecs) == 0 {
|
||||
want, viaPlugin := l.projectPiWanted(p)
|
||||
if len(want) == 0 && len(p.PiRecs) == 0 {
|
||||
return
|
||||
}
|
||||
abs := filepath.Join(p.Dir, filepath.FromSlash(piSettingsRel))
|
||||
@@ -133,9 +135,9 @@ func (l *Library) syncProjectPi(p *Project, fail func(what string, err error)) {
|
||||
fail("pi:"+rec.Source, fmt.Errorf("%s has been changed since magpie wrote %s: it's left as it is", piSettingsRel, rec.Source))
|
||||
continue
|
||||
}
|
||||
state, want := p.Pi[rec.Source]
|
||||
state, wanted := want[rec.Source]
|
||||
switch {
|
||||
case !want:
|
||||
case !wanted:
|
||||
changed = true
|
||||
if rec.Orig != nil {
|
||||
list[i] = rec.Orig
|
||||
@@ -152,8 +154,8 @@ func (l *Library) syncProjectPi(p *Project, fail func(what string, err error)) {
|
||||
}
|
||||
recs = append(recs, rec)
|
||||
}
|
||||
srcs := make([]string, 0, len(p.Pi))
|
||||
for s := range p.Pi {
|
||||
srcs := make([]string, 0, len(want))
|
||||
for s := range want {
|
||||
srcs = append(srcs, s)
|
||||
}
|
||||
sort.Strings(srcs)
|
||||
@@ -162,10 +164,13 @@ func (l *Library) syncProjectPi(p *Project, fail func(what string, err error)) {
|
||||
continue
|
||||
}
|
||||
if piIndex(list, s) >= 0 {
|
||||
if viaPlugin[s] {
|
||||
continue // the project declares it already: it loads as it is
|
||||
}
|
||||
fail("pi:"+s, fmt.Errorf("%s has %s already: it's the project's own, left as it is until it is adopted", piSettingsRel, s))
|
||||
continue
|
||||
}
|
||||
e := piEntryFor(s, p.Pi[s], nil)
|
||||
e := piEntryFor(s, want[s], nil)
|
||||
list = append(list, e)
|
||||
recs = append(recs, PiRec{Source: s, Entry: e})
|
||||
changed = true
|
||||
@@ -294,3 +299,40 @@ func (p *Project) piViews() []ProjectPiView {
|
||||
sort.Slice(out, func(i, j int) bool { return out[i].Source < out[j].Source })
|
||||
return out
|
||||
}
|
||||
|
||||
// piManifest says whether a folder is a Pi package: a package.json with a
|
||||
// `pi` object (skills, extensions, prompts, themes).
|
||||
func piManifest(dir string) bool {
|
||||
b, err := os.ReadFile(filepath.Join(dir, "package.json"))
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return gjson.GetBytes(b, "pi").IsObject()
|
||||
}
|
||||
|
||||
// projectPiWanted are the packages the project has: those it was given by
|
||||
// source, and, for each library plugin it gives Pi that is a Pi package, the
|
||||
// plugin's folder (#7243), one entry however often it is applied. viaPlugin
|
||||
// marks the latter. A source given by hand wins.
|
||||
func (l *Library) projectPiWanted(p *Project) (want map[string]string, viaPlugin map[string]bool) {
|
||||
want = map[string]string{}
|
||||
viaPlugin = map[string]bool{}
|
||||
for s, st := range p.Pi {
|
||||
want[s] = st
|
||||
}
|
||||
for name, agents := range p.Plugins {
|
||||
b := l.plugin(name)
|
||||
if b == nil || b.Off || !slices.Contains(agents, "pi") {
|
||||
continue
|
||||
}
|
||||
dir := pluginDir(name)
|
||||
if !piManifest(dir) {
|
||||
continue
|
||||
}
|
||||
if _, ok := want[dir]; !ok {
|
||||
want[dir] = "on"
|
||||
viaPlugin[dir] = true
|
||||
}
|
||||
}
|
||||
return want, viaPlugin
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package library
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
@@ -98,3 +99,65 @@ func TestProjectPiPackages(t *testing.T) {
|
||||
}
|
||||
|
||||
func squash(s string) string { return strings.Join(strings.Fields(s), "") }
|
||||
|
||||
// A plugin that is a Pi package, given to a project for Pi (#7243), is one
|
||||
// entry of its .pi/settings.json: its folder. Applied again it stays one;
|
||||
// withdrawn, only that entry goes; a declaration the project has of the same
|
||||
// package is left byte for byte.
|
||||
func TestProjectPluginPiPackage(t *testing.T) {
|
||||
h := sandbox(t)
|
||||
pluginAgents(t, h)
|
||||
src := filepath.Join(t.TempDir(), "acme")
|
||||
writePlugin(t, src)
|
||||
if err := os.WriteFile(filepath.Join(src, "package.json"), []byte(`{"name":"acme-kit","pi":{"skills":["./skills"],"extensions":["./extension.ts"]}}`), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ok(t)(InstallPlugins(src, nil, []string{}))
|
||||
dir := pluginDir("acme-kit")
|
||||
a, b := filepath.Join(t.TempDir(), "a"), filepath.Join(t.TempDir(), "b")
|
||||
for _, d := range []string{a, b} {
|
||||
gitInit(t, d)
|
||||
ok(t)(AddProject(d))
|
||||
}
|
||||
file := filepath.Join(a, ".pi/settings.json")
|
||||
count := func(p string) int { return strings.Count(read(t, p), dir) }
|
||||
|
||||
// not for Pi: no package
|
||||
ok(t)(ProjectPlugin(a, "acme-kit", []string{"claude"}))
|
||||
gone(t, file)
|
||||
ok(t)(ProjectPlugin(a, "acme-kit", []string{"pi"}))
|
||||
if count(file) != 1 {
|
||||
t.Fatalf("one entry for the plugin:\n%s", read(t, file))
|
||||
}
|
||||
ok(t)(ProjectPlugin(a, "acme-kit", []string{"pi"}))
|
||||
ok(t)(ProjectPlugin(a, "acme-kit", []string{"pi", "claude"}))
|
||||
if count(file) != 1 {
|
||||
t.Fatalf("applied again:\n%s", read(t, file))
|
||||
}
|
||||
gone(t, filepath.Join(b, ".pi"))
|
||||
ok(t)(ProjectPlugin(a, "acme-kit", nil))
|
||||
gone(t, file)
|
||||
|
||||
// the project's own filtered declaration of the same folder
|
||||
own := "{\n \"theme\": \"dark\",\n \"packages\": [{\"source\": " + strconv.Quote(dir) + ", \"skills\": [\"demo\"]}]\n}\n"
|
||||
write(t, file, own)
|
||||
ok(t)(ProjectPlugin(a, "acme-kit", []string{"pi"}))
|
||||
if read(t, file) != own {
|
||||
t.Fatalf("assignment changed the native entry:\n%s", read(t, file))
|
||||
}
|
||||
ok(t)(ProjectPlugin(a, "acme-kit", nil))
|
||||
if read(t, file) != own {
|
||||
t.Fatalf("withdrawal changed the native entry:\n%s", read(t, file))
|
||||
}
|
||||
|
||||
// beside the project's other packages: only the managed entry goes
|
||||
write(t, file, "{\"packages\": [\"npm:pi-own\"]}\n")
|
||||
ok(t)(ProjectPlugin(a, "acme-kit", []string{"pi"}))
|
||||
if s := read(t, file); count(file) != 1 || !strings.Contains(s, "npm:pi-own") {
|
||||
t.Fatalf("beside:\n%s", s)
|
||||
}
|
||||
ok(t)(ProjectPlugin(a, "acme-kit", nil))
|
||||
if s := read(t, file); strings.Contains(s, dir) || !strings.Contains(s, "npm:pi-own") {
|
||||
t.Fatalf("withdrawn:\n%s", s)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user