From bafdd6954b114afa67207facaa3a124c3e55fc9b Mon Sep 17 00:00:00 2001 From: yetone Date: Sat, 3 Oct 2026 04:44:37 +0800 Subject: [PATCH] =?UTF-8?q?agents:=20Empryo=20(#612)=20=E2=80=94=20picking?= =?UTF-8?q?=20a=20magpie=20model=20in=20Empryo=20adds=20a=20"magpie"=20pro?= =?UTF-8?q?vider=20at=20the=20gateway=20to=20~/.empryo/config.json's=20pro?= =?UTF-8?q?viders=20(baseURL=20+=20modelsAPI,=20no=20envVar)=20and=20sets?= =?UTF-8?q?=20defaultModel=20to=20magpie/;=20the=20user's=20?= =?UTF-8?q?defaultModel=20is=20stashed=20and=20put=20back=20on=20leaving,?= =?UTF-8?q?=20magpie's=20provider=20goes=20again,=20and=20a=20file=20magpi?= =?UTF-8?q?e=20made=20is=20removed=20when=20empty.=20The=20user's=20own=20?= =?UTF-8?q?providers=20and=20keys=20are=20left=20alone.=20The=20gateway=20?= =?UTF-8?q?now=20answers=20GET=20/v1=20(and=20/v1/)=20with=20its=20info,?= =?UTF-8?q?=20as=20Empryo=20lists=20a=20keyless=20provider=20only=20when?= =?UTF-8?q?=20its=20baseURL=20answers.=20Also=20on=20WSL.=20Built=20from?= =?UTF-8?q?=20Empryo's=20docs=20and=20its=20public=20v2=20code,=20not=20ru?= =?UTF-8?q?n=20against=20the=20Empryo=20binary.=20TestEmpryo,=20TestBaseUR?= =?UTF-8?q?LAnswers=20(GET=20/v1=20was=20404=20before).?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + internal/agent/agents.go | 1 + internal/agent/empryo.go | 177 +++++++++++++++++++++ internal/agent/empryo_test.go | 101 ++++++++++++ internal/agent/wsl.go | 2 + internal/gateway/empryo_test.go | 18 +++ internal/gateway/gateway.go | 4 + internal/gui/assets/icons/empryo-color.svg | 1 + site/public/icons/empryo-color.svg | 1 + site/public/index.html | 1 + 10 files changed, 307 insertions(+) create mode 100644 internal/agent/empryo.go create mode 100644 internal/agent/empryo_test.go create mode 100644 internal/gateway/empryo_test.go create mode 100644 internal/gui/assets/icons/empryo-color.svg create mode 100644 site/public/icons/empryo-color.svg diff --git a/README.md b/README.md index 3063121d4..c66cec8c0 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,7 @@ and there is a terminal version (`magpie tui`) and a plain CLI. | Hermes Agent | `~/.hermes/config.yaml` (`$HERMES_HOME`) | model | | Kimi Code | `~/.kimi/config.toml` (`$KIMI_SHARE_DIR`) | model (a `magpie` provider; magpie's models in Kimi's /model) | | Muse Code | `~/.config/muse/settings.json` (`$XDG_CONFIG_HOME`) | model (endpoint_transport to the gateway, auth none; magpie's models in Muse's list) | +| Empryo | `~/.empryo/config.json` | defaultModel (a `magpie` provider at the gateway in `providers`) | | MiniMax Code (mcode) | `~/.minimax/config.yaml` (`$MINIMAX_DATA_DIR`) | model (a `magpie` custom provider; magpie's models in its /model) | | Droid (Factory) | `~/.factory/settings.json` (`$FACTORY_HOME_OVERRIDE`) | model (magpie's models as BYOK `customModels`, in Droid's /model) | | Cline (CLI) | `~/.cline/data/settings/providers.json` (`$CLINE_DIR`) | model, effort (magpie takes its openai-compatible provider) | diff --git a/internal/agent/agents.go b/internal/agent/agents.go index 4e5384407..81325db53 100644 --- a/internal/agent/agents.go +++ b/internal/agent/agents.go @@ -81,6 +81,7 @@ func All() []*Agent { hermes(home), kimi(home), muse(cfg), + empryo(home), miniMax(home), droid(home), cline(home), diff --git a/internal/agent/empryo.go b/internal/agent/empryo.go new file mode 100644 index 000000000..04507980f --- /dev/null +++ b/internal/agent/empryo.go @@ -0,0 +1,177 @@ +package agent + +// Empryo keeps its settings in ~/.empryo/config.json (a project's +// .empryo/config.json goes over it). Its own providers are a "providers" +// array of {id, name, baseURL, envVar, models, modelsAPI}, OpenAI-compatible +// endpoints it lists models from (modelsAPI, else baseURL's /models) and +// asks /chat/completions of; with no envVar it sends no key of the user's. +// The model sessions start on is defaultModel, "/", +// split at the first "/". +// +// magpie adds a provider of its own, id "magpie", at the gateway, and sets +// defaultModel to magpie/; the defaultModel the user had is +// stashed and put back when magpie steps out, and the "magpie" provider +// goes again then. The user's providers and other keys are left as they +// are. With no envVar Empryo only lists a provider whose baseURL answers +// (2xx, 401 or 403), which the gateway's GET /v1 does. + +import ( + "encoding/json" + "path/filepath" + "strings" + + "github.com/tidwall/gjson" + + "github.com/yetone/magpie/internal/edit" +) + +const ( + empryoProviders = "providers" + empryoModel = "defaultModel" +) + +func empryo(home string) *Agent { return empryoAt(here(home)) } + +// empryoIn is Empryo in a WSL distro (see wsl.go). +func empryoIn(at place) *Agent { return empryoAt(at) } + +// empryoAt is Empryo with its home at at.home, reaching the gateway as at +// does. +func empryoAt(at place) *Agent { + gatewayV1 := at.v1 + dir := filepath.Join(at.home, ".empryo") + path := filepath.Join(dir, "config.json") + keyModel := "empryo:" + path + ":" + empryoModel + // keyNew marks a file magpie made, which goes again once magpie's + // provider and model are taken out of it and nothing is left + keyNew := "empryo:" + path + ":new" + model := func() string { v, _ := edit.GetJSON(path, empryoModel); return v } + // ours is magpie's provider entry, "" where there is none + ours := func() string { + raw, err := edit.Read(path) + if err != nil { + return "" + } + for _, p := range gjson.GetBytes(raw, empryoProviders).Array() { + if p.Get("id").String() == magpieID { + return p.Raw + } + } + return "" + } + // on: magpie's — the model is one of magpie's provider + on := func() bool { + ref, ok := strings.CutPrefix(model(), magpieID+"/") + return ok && isMagpie(ref) + } + // providers sets the providers array to the user's ones plus, with + // add, magpie's; with neither left the key goes + providers := func(add bool) error { + raw, err := edit.Read(path) + if err != nil && isFile(path) { + return err + } + var list []json.RawMessage + for _, p := range gjson.GetBytes(raw, empryoProviders).Array() { + if p.Get("id").String() != magpieID { + list = append(list, json.RawMessage(p.Raw)) + } + } + if add { + mine, _ := json.Marshal(map[string]any{ + "id": magpieID, "name": "Magpie", + "baseURL": gatewayV1(), "modelsAPI": gatewayV1() + "/models", + }) + list = append(list, mine) + } + if len(list) == 0 { + if !isFile(path) { + return nil + } + return edit.DelJSON(path, empryoProviders) + } + return edit.SetJSON(path, edit.KV{Path: empryoProviders, Value: list}) + } + put := func(v string) error { + if v == "" { + if !isFile(path) { + return nil + } + return edit.DelJSON(path, empryoModel) + } + return edit.SetJSON(path, edit.KV{Path: empryoModel, Value: v}) + } + return atomic(&Agent{ + ID: "empryo", Name: "Empryo", Icon: "empryo-color", Aliases: []string{"soulforge"}, + Bin: "empryo", Dir: dir, Path: path, + Notice: func() string { + if Running(`(^|/)empryo( |$)`) { + return "Empryo reads its config at start-up — restart open empryo sessions to use this." + } + return "" + }, + Check: func() string { + if !on() { + return "" + } + raw := ours() + if raw == "" { + return "Empryo's magpie provider (config.json) is gone, so it no longer reaches magpie" + } + return wiringOff("Empryo", path, func(k string) (string, bool) { + r := gjson.Get(raw, k) + return r.String(), r.Exists() + }, "baseURL", gatewayV1()) + }, + Fields: []Field{{ + Key: "model", Label: "model", + Get: model, + Set: func(v string) error { + if ref, ok := strings.CutPrefix(v, magpieID+"/"); ok && isMagpie(ref) { + // what the user had, unless magpie stashed it already + if !on() { + forget(keyModel, keyNew) + stash(map[string]string{keyModel: model()}) + if !isFile(path) { + stash(map[string]string{keyNew: "1"}) + } + } + if err := providers(true); err != nil { + return err + } + return put(v) + } + made := false + if on() || ours() != "" { + made = unstash(keyNew) != "" + // back to what the user had + if v == "" { + v = unstash(keyModel) + } else { + forget(keyModel) + } + if err := providers(false); err != nil { + return err + } + } + if err := put(v); err != nil { + return err + } + if made { + if raw, err := edit.Read(path); err == nil && len(gjson.ParseBytes(raw).Map()) == 0 { + return edit.Remove(path) + } + } + return nil + }, + Options: func(cur map[string]string) []Option { + var own []Option + if c := cur["model"]; c != "" && !usesMagpie(c) { + _, id, _ := strings.Cut(c, "/") + own = append(own, Option{Value: c, Icon: modelIcon("", id)}) + } + return append(group("Empryo", own), viaMagpie("empryo", magpieID+"/")...) + }, + }}, + }, path) +} diff --git a/internal/agent/empryo_test.go b/internal/agent/empryo_test.go new file mode 100644 index 000000000..059ae8e59 --- /dev/null +++ b/internal/agent/empryo_test.go @@ -0,0 +1,101 @@ +package agent + +import ( + "encoding/json" + "os" + "path/filepath" + "reflect" + "strings" + "testing" +) + +func TestEmpryo(t *testing.T) { + home, _ := museHome(t) + path := filepath.Join(home, ".empryo", "config.json") + os.MkdirAll(filepath.Dir(path), 0o755) + // a user's own: a provider of theirs, their model and a theme + orig := `{ + "providers": [{"id": "lmstudio", "name": "LM Studio", "baseURL": "http://localhost:1234/v1"}], + "defaultModel": "anthropic/claude-sonnet-5-5", + "theme": "dark" +} +` + os.WriteFile(path, []byte(orig), 0o644) + read := func() map[string]any { + t.Helper() + var m map[string]any + b, _ := os.ReadFile(path) + if err := json.Unmarshal(b, &m); err != nil { + t.Fatalf("%v\n%s", err, b) + } + return m + } + var want map[string]any + json.Unmarshal([]byte(orig), &want) + + a := empryo(home) + if a.Path != path { + t.Fatalf("path: %s", a.Path) + } + f := a.Field("model") + if f.Get() != "anthropic/claude-sonnet-5-5" || a.Check() != "" { + t.Fatalf("own: %q %q", f.Get(), a.Check()) + } + var vals []string + for _, o := range f.Options(a.Values()) { + vals = append(vals, o.Value) + } + if !strings.Contains(strings.Join(vals, " "), "anthropic/claude-sonnet-5-5 magpie/deepseek/pro") { + t.Fatalf("options: %v", vals) + } + + for range 2 { // twice: one magpie provider, and the user's model kept + if err := f.Set("magpie/deepseek/pro"); err != nil { + t.Fatal(err) + } + } + m := read() + ps, _ := m["providers"].([]any) + if len(ps) != 2 || !reflect.DeepEqual(ps[0], want["providers"].([]any)[0]) || + !reflect.DeepEqual(ps[1], map[string]any{"id": "magpie", "name": "Magpie", "baseURL": gatewayV1(), "modelsAPI": gatewayV1() + "/models"}) { + t.Fatalf("providers: %v", m["providers"]) + } + if m["defaultModel"] != "magpie/deepseek/pro" || m["theme"] != "dark" { + t.Fatalf("set: %v", m) + } + if f.Get() != "magpie/deepseek/pro" || a.Check() != "" { + t.Fatalf("on: %q %q", f.Get(), a.Check()) + } + + // magpie's provider pointed elsewhere + b, _ := os.ReadFile(path) + os.WriteFile(path, []byte(strings.Replace(string(b), gatewayV1(), "http://elsewhere/v1", 1)), 0o644) + if !strings.Contains(a.Check(), "http://elsewhere/v1") { + t.Fatalf("check: %q", a.Check()) + } + os.WriteFile(path, b, 0o644) + + // stepping out puts back what the user had + if err := f.Set(""); err != nil { + t.Fatal(err) + } + if m := read(); !reflect.DeepEqual(m, want) { + t.Fatalf("restore: %v", m) + } + + // a file magpie made goes again + os.Remove(path) + if err := f.Set("magpie/deepseek/flash"); err != nil { + t.Fatal(err) + } + if m := read(); m["defaultModel"] != "magpie/deepseek/flash" { + t.Fatalf("new: %v", m) + } + if err := f.Set(""); err != nil { + t.Fatal(err) + } + if _, err := os.Stat(path); !os.IsNotExist(err) { + b, _ := os.ReadFile(path) + t.Fatalf("made file left: %s", b) + } +} diff --git a/internal/agent/wsl.go b/internal/agent/wsl.go index 9434dd3ce..d9cf5e22b 100644 --- a/internal/agent/wsl.go +++ b/internal/agent/wsl.go @@ -318,6 +318,8 @@ var wslKinds = []wslKind{ return append(miniMaxOwnOptions("", cur["model"]), viaMagpie("minimax-code", magpieID+"/")...) } }}, + {id: "empryo", name: "Empryo", dir: ".empryo", bin: "empryo", in: empryoIn, + restart: "reads its config at start-up — restart open empryo sessions to use this."}, {id: "muse", name: "Muse Code", dir: ".config/muse", bin: "muse", in: museIn, restart: "reads its settings at start-up — restart open muse sessions to use this."}, {id: "qoder", name: "Qoder", dir: ".qoder", bin: "qodercli", in: qoderIn, diff --git a/internal/gateway/empryo_test.go b/internal/gateway/empryo_test.go new file mode 100644 index 000000000..eeca84028 --- /dev/null +++ b/internal/gateway/empryo_test.go @@ -0,0 +1,18 @@ +package gateway + +import ( + "net/http/httptest" + "testing" +) + +// Empryo lists a provider with no key of its own only when its baseURL +// answers a GET (2xx, 401 or 403). +func TestBaseURLAnswers(t *testing.T) { + for _, p := range []string{"/v1", "/v1/"} { + rec := httptest.NewRecorder() + New().Handler().ServeHTTP(rec, httptest.NewRequest("GET", p, nil)) + if rec.Code != 200 { + t.Fatalf("GET %s: %d %s", p, rec.Code, rec.Body) + } + } +} diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go index e99e24124..815bb5dd2 100644 --- a/internal/gateway/gateway.go +++ b/internal/gateway/gateway.go @@ -431,6 +431,10 @@ func (s *Server) ListenAndServe(ctx context.Context) error { func (s *Server) Handler() http.Handler { mux := http.NewServeMux() mux.HandleFunc("GET /{$}", s.info) + // an OpenAI-compatible base URL answers too: Empryo lists a provider with + // no key of its own only when its baseURL does + mux.HandleFunc("GET /v1", s.info) + mux.HandleFunc("GET /v1/{$}", s.info) mux.HandleFunc("GET /v1/models", s.models) mux.HandleFunc("GET /models", s.models) mux.HandleFunc("GET /v1/models/{id...}", s.model) diff --git a/internal/gui/assets/icons/empryo-color.svg b/internal/gui/assets/icons/empryo-color.svg new file mode 100644 index 000000000..035a7a14a --- /dev/null +++ b/internal/gui/assets/icons/empryo-color.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/site/public/icons/empryo-color.svg b/site/public/icons/empryo-color.svg new file mode 100644 index 000000000..035a7a14a --- /dev/null +++ b/site/public/icons/empryo-color.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/site/public/index.html b/site/public/index.html index d829c4166..086955d77 100644 --- a/site/public/index.html +++ b/site/public/index.html @@ -466,6 +466,7 @@ footer a:hover { color: var(--ink); }
  • Hermes Agent
  • Kimi Code
  • Muse Code
  • +
  • Empryo
  • MiniMax Code
  • Droid
  • Grok Build