Files
magpie/internal/agent/pi_extension_test.go
T
yetone a8d6a861a4 agent: Pi's codemode gets magpie's decision and image models (extensions/magpie.ts)
wangbinyq on Discord (#feedback) asked for magpie's System One and image
models in Pi's config, since Pi's codemode can call classifier and image
models (models.classify, models.generateImages; Pi docs/models.md "Use
classifier models").

Pi's models.json can't hold them: its model entries have no type, so
every one is a chat model (ModelDefinitionSchema, modelFromJson in
provider-composer.ts, Pi 1.4.0 / c9f4c7b9f). A custom provider's
classifier and image models come from an extension alone
(docs/custom-provider.md, "Register mixed-operation models"). So while
models.json has providers.magpie, magpie now writes
extensions/magpie.ts in Pi's agent folder, which Pi loads by itself:

- provider magpie-systemone: provider.OfferedDeciders, labelled as
  /v1/models labels them, on Pi's own typesafe-system-one client
  (builtinProviders' typesafe .classify), which posts the TypeSafe
  System One body to the gateway's /v1/systemone. Text input only.
- provider magpie-images: gateway.ImageModels (new; mediaModels with
  labels, the Settings > Images default first, none while it is off),
  drawn by POST /v1/images/generations with the input's images as data
  URLs, b64_json back as image blocks.

Separate ids because an extension's models replace those models.json
gives the same provider id: registered as "magpie", Pi 1.4.0 listed no
chat models at all ("No models available").

The file is rewritten at each Sync and on a pick, and removed with
providers.magpie (pick of none, Disconnect, a Sync that finds it gone).
A magpie.ts whose first line isn't magpie's mark is the user's and is
never written over or removed. Pi only, not OmO or Prime Agent. On a Pi
older than its classifier models the extension returns without
registering anything. Codemode itself is not turned on: that is the
user's defaultTools.

Run at the pushed head (on mote):
- At e7185daeb plus this change: go build ./..., go vet (darwin, GOOS=linux,
  GOOS=windows) of agent and gateway, gofmt -l clean; go test -timeout 45m
  of agent, backup, cli, davsync, gateway, gui, library, profile, stats,
  tui, uninstall. All ok but three reds that are the same on origin/main
  1747559d0 and filed: cli TestEveryRouteHasACommandOrIsNamed (#4826), gui
  TestMainWindowCorners/macOS_26_SDK (#3949), gateway
  TestCodexPoolSaysTheClientsVersion (full-run only, passes alone, #4807).
- Rebased on 1aca55909: go build ./..., go vet linux/windows of agent and
  gateway, go test ./internal/agent/ ok (181s), gateway -run
  'Image|Media|Draw|Decide|SystemOne' ok.

Real Pi in a sandbox HOME on mote (npm @earendil-works/pi-coding-agent),
against a fake gateway, with the extension magpie generated
(piExtensionFor from the test fixture):
- 1.4.0: `pi -p --tools +codemode`, the script listed
  magpie-systemone/mixed/typesafe/jev and magpie-images/art/gpt-image-2
  (in=text,image); classify answered {approved: bool 0.93}; a drawing
  with an image input came back as image/png. The fake logged
  /v1/systemone {model: mixed/typesafe/jev, noul question} and
  /v1/images/generations {model: art/gpt-image-2, images: [data URL]},
  both with Bearer magpie; the chat model from models.json still ran.
- 0.87.1 and 1.0.0: start and chat with the file in place, no error.

Broken by hand: the user's-own check removed -> LeavesTheUsersOwn red
(file written over, then removed); the Pi-only gate removed ->
PiOnly red; the extension write made a no-op -> ListsDecisionAndImage
Models and FollowsSync red.

Not tried: a real magpie gateway with real decision or image vendors
behind it (the fake answers in TypeSafe's wire shape, as magpie's
/v1/systemone does); classifier image input (left off); a WSL Pi. A
change of decision or image models reaches the file at the agent's next
Sync (a catalog change) or pick; whether a Settings > Images change
alone starts a Sync was not checked.
2026-11-02 15:43:43 +08:00

177 lines
5.8 KiB
Go

package agent
import (
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/provider"
)
// piExtensionHome is a sandbox home with a provider that decides
// (typesafe/jev) and one that draws (gpt-image-2, which takes images too).
func piExtensionHome(t *testing.T) string {
t.Helper()
home := syncHome(t)
// no request reaches these: the extension only names them
if err := provider.Save(provider.Provider{ID: "mixed", Name: "Mixed", Key: "k", Chat: "http://127.0.0.1:9/v1", Decide: "http://127.0.0.1:9/v1", Models: []string{"deepseek-v4.1-flash", "typesafe/jev"}}); err != nil {
t.Fatal(err)
}
art := provider.Provider{ID: "art", Name: "Art", Responses: "http://127.0.0.1:9/v1", Key: "k"}
if err := provider.Save(art); err != nil {
t.Fatal(err)
}
if err := catalog.SaveLive("art", art.Responses, []catalog.Model{{ID: "text"}, {ID: "gpt-image-2", Name: "GPT Image 2", Draws: true, Images: true}}); err != nil {
t.Fatal(err)
}
return home
}
type piExtensionData struct {
BaseURL string `json:"baseUrl"`
APIKey string `json:"apiKey"`
Classifiers []piExtensionModel `json:"classifiers"`
Images []piExtensionModel `json:"images"`
}
// readPiExtension is the data in the extension magpie wrote at path.
func readPiExtension(t *testing.T, path string) piExtensionData {
t.Helper()
src := readFile(path)
if !strings.HasPrefix(src, piExtensionMark) {
t.Fatalf("%s is not magpie's: %.200s", path, src)
}
_, rest, ok := strings.Cut(src, "const MAGPIE = ")
line, _, _ := strings.Cut(rest, ";\n")
var d piExtensionData
if !ok || json.Unmarshal([]byte(line), &d) != nil {
t.Fatalf("no MAGPIE data in %s: %.400s", path, rest)
}
return d
}
func hasModel(ms []piExtensionModel, id string) (piExtensionModel, bool) {
for _, m := range ms {
if m.ID == id {
return m, true
}
}
return piExtensionModel{}, false
}
// Pi's codemode reaches classifier and image models, which models.json
// can't define (wangbinyq on Discord): picking a model of magpie's writes
// extensions/magpie.ts with magpie's decision and image models, and
// taking Pi off magpie removes it.
func TestPiExtensionListsDecisionAndImageModels(t *testing.T) {
home := piExtensionHome(t)
a := pi(home)
path := piExtensionPath(a.Dir)
if err := a.Field("model").Set(magpieID + "/mixed/deepseek-v4.1-flash"); err != nil {
t.Fatal(err)
}
d := readPiExtension(t, path)
if d.BaseURL != here(home).v1() {
t.Errorf("baseUrl = %q, want %q", d.BaseURL, here(home).v1())
}
if want := mustGet(t, filepath.Join(a.Dir, "models.json"), "providers.magpie.apiKey"); d.APIKey != strings.Trim(want, `"`) {
t.Errorf("apiKey = %q, models.json's is %s", d.APIKey, want)
}
if _, ok := hasModel(d.Classifiers, "mixed/typesafe/jev"); !ok {
t.Errorf("classifiers = %+v, want mixed/typesafe/jev", d.Classifiers)
}
if _, ok := hasModel(d.Classifiers, "mixed/deepseek-v4.1-flash"); ok {
t.Errorf("a chat model listed as a classifier: %+v", d.Classifiers)
}
img, ok := hasModel(d.Images, "art/gpt-image-2")
if !ok {
t.Fatalf("images = %+v, want art/gpt-image-2", d.Images)
}
if strings.Join(img.Input, ",") != "text,image" || img.Name != "GPT Image 2 · Art" {
t.Errorf("image model = %+v", img)
}
if !strings.Contains(readFile(path), `pi.registerProvider("magpie-systemone"`) || !strings.Contains(readFile(path), `"typesafe-system-one"`) {
t.Errorf("extension source: %.300s", readFile(path))
}
if err := a.Field("model").Set(""); err != nil {
t.Fatal(err)
}
if _, err := os.Stat(path); !os.IsNotExist(err) {
t.Errorf("extension left after Pi went off magpie: %v", err)
}
}
// The catalog sync rewrites the extension as magpie's models change, and
// removes it once models.json has no provider magpie.
func TestPiExtensionFollowsSync(t *testing.T) {
home := piExtensionHome(t)
a := pi(home)
path := piExtensionPath(a.Dir)
if err := a.Field("model").Set(magpieID + "/mixed/deepseek-v4.1-flash"); err != nil {
t.Fatal(err)
}
// the one provider that draws removed: no image model is listed
if err := provider.Delete("art"); err != nil {
t.Fatal(err)
}
if err := a.Sync(); err != nil {
t.Fatal(err)
}
if d := readPiExtension(t, path); len(d.Images) != 0 || len(d.Classifiers) == 0 {
t.Errorf("after the drawing provider went: %+v", d)
}
models := filepath.Join(a.Dir, "models.json")
if err := edit.DelJSON(models, "providers.magpie"); err != nil {
t.Fatal(err)
}
if err := a.Sync(); err != nil {
t.Fatal(err)
}
if _, err := os.Stat(path); !os.IsNotExist(err) {
t.Errorf("extension left with no provider magpie: %v", err)
}
}
// A magpie.ts the user wrote themselves is theirs: never written over,
// never removed.
func TestPiExtensionLeavesTheUsersOwn(t *testing.T) {
home := piExtensionHome(t)
a := pi(home)
path := piExtensionPath(a.Dir)
os.MkdirAll(filepath.Dir(path), 0o755)
own := "// my own magpie helpers\nexport default function (pi) {}\n"
os.WriteFile(path, []byte(own), 0o644)
if err := a.Field("model").Set(magpieID + "/mixed/deepseek-v4.1-flash"); err != nil {
t.Fatal(err)
}
if err := a.Sync(); err != nil {
t.Fatal(err)
}
if got := readFile(path); got != own {
t.Errorf("the user's file written over: %.200s", got)
}
if err := a.Field("model").Set(""); err != nil {
t.Fatal(err)
}
if got := readFile(path); got != own {
t.Errorf("the user's file removed or changed: %q", got)
}
}
// OmO keeps Pi's files but may not load Pi's extensions: it gets none.
func TestPiExtensionPiOnly(t *testing.T) {
home := piExtensionHome(t)
a := omo(home)
if err := a.Field("model").Set(magpieID + "/mixed/deepseek-v4.1-flash"); err != nil {
t.Fatal(err)
}
if _, err := os.Stat(piExtensionPath(a.Dir)); !os.IsNotExist(err) {
t.Errorf("OmO got Pi's extension: %v", err)
}
}