pi: with the pi-magpie extension installed, magpie's models.json block goes (KenyonCode on X)
pi-magpie (npm, tinysnake) serves provider "magpie" from the live catalog and warns at every start while models.json still names providers.magpie (one baseUrl for every model, a frozen list). magpie wrote that block itself, so the user was told to delete it by hand. When Pi's settings.json "packages" lists pi-magpie, Pi's Sync and model pick now drop magpie's own block (only one pointing at this gateway with magpie's key; a block the user wrote stays) after keeping models.json.before-pi-magpie, through the atomic JSON delete, the rest of the file untouched. Check no longer calls the missing block a broken wiring. Without the extension nothing changes. Run: TestPiMagpieExtensionTakesOverTheBlock (sandbox HOME) and ./internal/agent on mote, vet darwin/linux nogui/windows. Broken by hand: viaExt forced false -> the test goes red with the block still there. Not tried: a real Pi with the extension installed.
This commit is contained in:
@@ -1544,7 +1544,20 @@ func piLikeKeyed(at place, id, name, dir string, key func() string) *Agent {
|
||||
if id == "pi" {
|
||||
extension = func() error { return piExtensionSynced(dir, modelsPath, at.v1(), key()) }
|
||||
}
|
||||
// the pi-magpie extension serves provider "magpie" live, so Pi's
|
||||
// models.json block is not written and magpie's own stale one goes
|
||||
// (pimagpie.go)
|
||||
viaExt := func() bool { return id == "pi" && piMagpieInstalled(path) }
|
||||
writeMagpie := func() error {
|
||||
if viaExt() {
|
||||
if err := dropPiBlock(modelsPath, func() string {
|
||||
return wiringOff(name, modelsPath, func(k string) (string, bool) { return edit.GetJSON(modelsPath, "providers."+magpieID+"."+k) },
|
||||
"baseUrl", at.v1(), "apiKey", key())
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
return extension()
|
||||
}
|
||||
if err := edit.SetJSON(modelsPath, edit.KV{Path: "providers." + magpieID, Value: block()}); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -1567,12 +1580,19 @@ func piLikeKeyed(at place, id, name, dir string, key func() string) *Agent {
|
||||
if p, _ := get("defaultProvider"); p != magpieID {
|
||||
return ""
|
||||
}
|
||||
if viaExt() {
|
||||
return ""
|
||||
}
|
||||
return wiringOff(name, modelsPath, func(k string) (string, bool) { return edit.GetJSON(modelsPath, "providers."+magpieID+"."+k) },
|
||||
"baseUrl", at.v1(), "apiKey", key())
|
||||
},
|
||||
ListOnly: true,
|
||||
Sync: func() error {
|
||||
if err := syncJSON(modelsPath, "providers."+magpieID, block); err != nil {
|
||||
if viaExt() {
|
||||
if err := writeMagpie(); err != nil { // drops the stale block
|
||||
return err
|
||||
}
|
||||
} else if err := syncJSON(modelsPath, "providers."+magpieID, block); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := extension(); err != nil {
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"regexp"
|
||||
|
||||
"github.com/yetone/magpie/internal/edit"
|
||||
)
|
||||
|
||||
// piMagpieExt matches a Pi package source for the pi-magpie extension:
|
||||
// npm:pi-magpie, npm:pi-magpie@0.1.6, or a path or git source ending in it.
|
||||
var piMagpieExt = regexp.MustCompile(`(^|[:/\\])pi-magpie(@[^/\\]*)?(#.*)?$`)
|
||||
|
||||
// piMagpieInstalled says Pi's settings.json lists the pi-magpie extension
|
||||
// in "packages". It serves provider "magpie" from magpie's live catalog,
|
||||
// and warns at every start while models.json also names providers.magpie:
|
||||
// that block gives every model one baseUrl and a frozen list. A settings
|
||||
// file that can't be read is unknown, so not installed.
|
||||
func piMagpieInstalled(settings string) bool {
|
||||
raw, err := os.ReadFile(settings)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
var s struct {
|
||||
Packages []json.RawMessage `json:"packages"`
|
||||
}
|
||||
if json.Unmarshal(edit.TrimBOM(raw), &s) != nil {
|
||||
return false
|
||||
}
|
||||
for _, p := range s.Packages {
|
||||
var src string
|
||||
if json.Unmarshal(p, &src) != nil {
|
||||
var o struct {
|
||||
Source string `json:"source"`
|
||||
}
|
||||
if json.Unmarshal(p, &o) != nil {
|
||||
continue
|
||||
}
|
||||
src = o.Source
|
||||
}
|
||||
if piMagpieExt.MatchString(src) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// dropPiBlock takes magpie's own providers.magpie out of models.json, the
|
||||
// rest of the file as it was, after keeping a copy of the file beside it
|
||||
// (models.json.before-pi-magpie, the first one kept). off is wiringOff for
|
||||
// the block: "" says it points at this gateway with magpie's key, so it is
|
||||
// magpie's; any other block is the user's and stays.
|
||||
func dropPiBlock(modelsPath string, off func() string) error {
|
||||
if _, has := edit.GetJSON(modelsPath, "providers."+magpieID); !has || off() != "" {
|
||||
return nil
|
||||
}
|
||||
if raw, err := os.ReadFile(modelsPath); err == nil {
|
||||
if _, e := os.Stat(modelsPath + ".before-pi-magpie"); e != nil {
|
||||
if err := edit.WriteAtomic(modelsPath+".before-pi-magpie", raw); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return edit.DelJSON(modelsPath, "providers."+magpieID)
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/yetone/magpie/internal/provider"
|
||||
)
|
||||
|
||||
// With the pi-magpie extension installed, magpie's own providers.magpie
|
||||
// block in models.json is taken out (a copy kept), the user's other
|
||||
// providers stay, and a block that is not magpie's stays too.
|
||||
func TestPiMagpieExtensionTakesOverTheBlock(t *testing.T) {
|
||||
home := syncHome(t)
|
||||
dir := filepath.Join(home, ".pi", "agent")
|
||||
os.MkdirAll(dir, 0o755)
|
||||
models := filepath.Join(dir, "models.json")
|
||||
settings := filepath.Join(dir, "settings.json")
|
||||
os.WriteFile(settings, []byte(`{"packages":[]}`), 0o644)
|
||||
os.WriteFile(models, []byte(`{"providers":{"mine":{"baseUrl":"http://x/v1"}}}`), 0o644)
|
||||
if err := provider.Save(provider.Provider{ID: "p", Name: "P", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"m"}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// plain Pi: the block is written
|
||||
if err := pi(home).Field("model").Set("magpie/p/m"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(readFile(models), `"magpie"`) {
|
||||
t.Fatalf("without the extension the block is written: %s", readFile(models))
|
||||
}
|
||||
// the extension installed: the next sync drops it
|
||||
os.WriteFile(settings, []byte(`{"packages":["npm:pi-magpie"],"defaultProvider":"magpie"}`), 0o644)
|
||||
if err := pi(home).Sync(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got := readFile(models)
|
||||
if strings.Contains(got, `"magpie"`) || !strings.Contains(got, `"mine"`) {
|
||||
t.Fatalf("block not dropped or mine lost: %s", got)
|
||||
}
|
||||
if _, err := os.Stat(models + ".before-pi-magpie"); err != nil {
|
||||
t.Fatalf("no backup: %v", err)
|
||||
}
|
||||
if msg := pi(home).Check(); msg != "" {
|
||||
t.Fatalf("check: %s", msg)
|
||||
}
|
||||
// a block the user wrote (another address) is left alone
|
||||
os.WriteFile(models, []byte(`{"providers":{"magpie":{"baseUrl":"http://example.com/v1","apiKey":"theirs"}}}`), 0o644)
|
||||
if err := pi(home).Sync(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(readFile(models), "example.com") {
|
||||
t.Fatalf("user's block removed: %s", readFile(models))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user