From b26f16ee24c399e4ff2bbb60d1b97ceac4643c63 Mon Sep 17 00:00:00 2001 From: yetone Date: Mon, 16 Nov 2026 00:18:36 +0800 Subject: [PATCH] agent: Alma has one magpie provider, whichever side turned it on realLemonawa on X: Alma has its own Magpie preset, and magpie's Alma agent adds a provider of its own, so turning on both left two. Alma's preset card claims the provider whose id is "magpie" (Alma origin/main: forPredef / getProviderForPredefined, provPredefBody in desktop-native settings_providers_detail.go). magpie's provider is now made under that id (with the preset's icon), so Alma's card shows it set up and offers no second one. When a provider has that id already (a user's own elsewhere), magpie makes one as before and leaves it alone. almaMagpie already took a Magpie set up in Alma first (same gateway host) as magpie's, so the row showed connected and nothing was added. What was wrong was switching off: almaSet("") DELETEd whichever provider it had matched, the user's own preset included. Now a provider magpie didn't make (name not "magpie") is kept: only the default model and image refs that point at it are taken back, as before, and it is turned off again only if it was off when magpie turned it on (alma.wasoff in the stash). Sync no longer rewrites its model list after the switch-off. Tests: TestAlmaOneMagpie (internal/agent/alma_test.go), against the fake Alma, whose POST now honours an id and answers a taken id with Alma's 500 "UNIQUE constraint failed": - set up in Alma first: connected, no second provider, switch-off sends no DELETE, the preset stays on, sync after off writes nothing - turned off in Alma: enabled on connect, disabled again on switch-off - none in Alma: created under id "magpie", removed on switch-off - preset id taken by another host: left alone, a new one made Red on the old alma.go: "the user's own Magpie was removed: [PUT /api/settings DELETE /api/providers/magpie]". Broken by hand: without the id, "none in Alma" fails ("not under the preset's id"); without the Sync skip, "sync after off wrote [PUT /api/providers/magpie/models]". Real Alma (alma-server from /Applications/Alma.app, sandbox HOME and ALMA_DATA_DIR, port 23917, never the live one): the preset's own POST body creates id "magpie"; a second POST with that id is refused 500 UNIQUE constraint failed; PUT {"enabled":false} turns it off; DELETE 204; magpie's POST body with id "magpie" and type openai is kept as sent. Run at this tree (rebased on origin/main 0ffb21f83), on mica: go test -tags nogui -count=1 ./internal/agent ok (199s); go vet -tags nogui ./internal/agent for darwin, linux and windows ok; gofmt -l clean. The full suite is queued on mote behind another session's run and was not waited for; a red there gets its own issue. Not tried: magpie's agent end to end against a running Alma (the calls were replayed with curl). A user who already has both (magpie's old random-id provider and Alma's preset) keeps both: threads name ":", so neither is merged or removed for them, and magpie's old provider keeps its id. --- internal/agent/alma.go | 57 ++++++++++++++- internal/agent/alma_test.go | 135 +++++++++++++++++++++++++++++++++++- 2 files changed, 188 insertions(+), 4 deletions(-) diff --git a/internal/agent/alma.go b/internal/agent/alma.go index 8bc4669da..69011f963 100644 --- a/internal/agent/alma.go +++ b/internal/agent/alma.go @@ -14,7 +14,10 @@ package agent // imageGen.model the same ("" for Auto) // // magpie is one provider there, named magpie, of type openai at the -// gateway's /v1; choosing one of its models makes it Alma's default model, +// gateway's /v1, under the id of Alma's own Magpie preset (almaPresetID); +// a Magpie the user set up in Alma first is used in its place, and kept +// when magpie is switched off (almaMade). Choosing one of its models +// makes it Alma's default model, // and the model magpie draws with Alma's image generation model when Alma // has none picked (almaSetImage). // Alma not running is no error: there is nothing to read or keep current. @@ -490,8 +493,16 @@ func almaWire() (string, error) { p := almaMagpie(ps) if p == nil { var made almaProvider - if err := almaDo("POST", "/api/providers", map[string]any{"name": magpieID, "type": "openai", - "apiKey": almaKey(), "baseURL": gatewayV1(), "enabled": true}, &made); err != nil { + body := map[string]any{"name": magpieID, "type": "openai", + "apiKey": almaKey(), "baseURL": gatewayV1(), "enabled": true, "icon": almaPresetID} + // under the id of Alma's own Magpie preset, so Alma's Providers + // page shows it as that preset, set up, and offers no second one + // (realLemonawa on X); a provider of that id already there, at + // another address, is the user's, and keeps it + if !slices.ContainsFunc(ps, func(o almaProvider) bool { return o.ID == almaPresetID }) { + body["id"] = almaPresetID + } + if err := almaDo("POST", "/api/providers", body, &made); err != nil { return "", err } if made.ID == "" { @@ -506,6 +517,11 @@ func almaWire() (string, error) { } p = &made } else if p.BaseURL != gatewayV1() || !p.Enabled { + if !p.Enabled && !almaMade(p) { + // a Magpie of the user's own in Alma, turned off: off again + // when magpie is (almaSet) + stash(map[string]string{almaWasOff: p.ID}) + } if err := almaDo("PUT", "/api/providers/"+p.ID, map[string]any{"baseURL": gatewayV1(), "apiKey": almaKey(), "enabled": true}, nil); err != nil { return "", err @@ -516,6 +532,24 @@ func almaWire() (string, error) { return p.ID, almaSyncModels(p) } +// almaPresetID is the id of Alma's own Magpie provider, its preset on +// the Providers page and in its quick setup: {"id":"magpie","name":"Magpie", +// "type":"custom","apiFormat":"openai-chat","icon":"magpie","apiKey": +// "magpie-alma","baseURL":"http://127.0.0.1:3425/v1"} (provPredefBody in +// Alma's desktop-native/internal/app/settings_providers_detail.go, and +// NewProvidersSettings.tsx). Alma takes a provider of this id as that +// preset set up, and offers it again while none has it. +const almaPresetID = "magpie" + +// almaWasOff, in the stash, is the id of the user's own Magpie provider in +// Alma that magpie turned on to use it, for turning it off again. +const almaWasOff = "alma.wasoff" + +// almaMade says p is the provider magpie made in Alma: magpie names its +// own magpie, and Alma's preset is Magpie. One the user made, the preset or +// one of their own pointed at the gateway, magpie uses, and never removes. +func almaMade(p *almaProvider) bool { return p.Name == magpieID } + // almaKey is the key magpie's provider in Alma is given. Alma's requests // carry the AI SDK's User-Agent and nothing of Alma's, so the key is how // the gateway knows them for Alma's. @@ -700,6 +734,18 @@ func almaSet(v string) (err error) { return err } } + if !almaMade(p) { + // the user's own Magpie in Alma (its preset, set up there + // first): Alma no longer starts on it, and it stays, as it + // was before magpie used it + if stashShared()[almaWasOff] == p.ID { + if err := almaDo("PUT", "/api/providers/"+p.ID, map[string]any{"enabled": false}, nil); err != nil { + return err + } + forget(almaWasOff) + } + return nil + } return almaDo("DELETE", "/api/providers/"+p.ID, nil, nil) } if ref, ok := strings.CutPrefix(v, magpieID+"/"); ok { @@ -833,6 +879,11 @@ func alma() *Agent { if p == nil { return nil } + if !almaMade(p) && stashShared()["alma"+disconnectedKey] == "1" { + // the user's own Magpie in Alma, with magpie switched off + // for Alma: left as the user keeps it + return nil + } if err := almaKeyed(p); err != nil { return err } diff --git a/internal/agent/alma_test.go b/internal/agent/alma_test.go index c14d9a789..3e565d71d 100644 --- a/internal/agent/alma_test.go +++ b/internal/agent/alma_test.go @@ -7,6 +7,7 @@ import ( "net/http/httptest" "os" "path/filepath" + "slices" "strings" "sync" "testing" @@ -73,9 +74,17 @@ func (f *fakeAlma) ServeHTTP(w http.ResponseWriter, r *http.Request) { case r.URL.Path == "/api/providers" && r.Method == "GET": reply(200, f.providers) case r.URL.Path == "/api/providers" && r.Method == "POST": + // as Alma: the id given (its presets'), else one made up; an id + // taken fails the insert + if id, _ := body["id"].(string); id != "" && f.provider(id) != nil { + reply(500, map[string]any{"error": "Failed to create provider: UNIQUE constraint failed: providers.id"}) + return + } f.n++ f.keys = append(f.keys, fmt.Sprint(body["apiKey"])) - body["id"] = fmt.Sprintf("p%d", f.n) + if id, _ := body["id"].(string); id == "" { + body["id"] = fmt.Sprintf("p%d", f.n) + } body["models"], body["availableModels"] = []any{}, []any{} f.providers = append(f.providers, body) reply(201, body) @@ -747,3 +756,127 @@ func TestInTrash(t *testing.T) { } } } + +// almaPreset is Alma's own Magpie provider as its Providers page or quick +// setup makes it (provPredefBody in Alma's desktop-native), at this +// gateway, with the one model its quick setup turned on. +func almaPreset(enabled bool, ref string) map[string]any { + return map[string]any{"id": "magpie", "name": "Magpie", "type": "custom", "apiFormat": "openai-chat", "icon": "magpie", + "apiKey": "magpie-alma", "baseURL": gatewayV1(), "enabled": enabled, "models": []any{ref}, + "availableModels": []any{map[string]any{"id": ref, "name": ref}}} +} + +// Alma's own Magpie and magpie's Alma agent are one setup (realLemonawa on +// X): magpie switched on with the preset set up in Alma first uses it, and +// adds no second; set up in Alma first, the agent reads as on; switched off, +// the preset stays as the user had it; and magpie's own provider takes the +// preset's id, so Alma doesn't offer the preset again beside it. +func TestAlmaOneMagpie(t *testing.T) { + syncHome(t) + f := startAlma(t) + a := alma() + if err := os.MkdirAll(a.Dir, 0o755); err != nil { + t.Fatal(err) + } + fl := a.Field("model") + var ref string + for _, o := range fl.Options(a.Values()) { + if o.Ref != "" { + ref = o.Ref + break + } + } + if ref == "" { + t.Fatal("no magpie model offered") + } + + t.Run("set up in Alma first", func(t *testing.T) { + f.mu.Lock() + f.providers = append(f.providers, almaPreset(true, ref)) + f.settings["chat"].(map[string]any)["defaultModel"] = "magpie:" + ref + f.mu.Unlock() + if !a.Wired() || fl.Get() != "magpie/"+ref || a.Check() != "" { + t.Fatalf("Alma on its Magpie reads as off: wired %v, model %q, check %q", a.Wired(), fl.Get(), a.Check()) + } + if err := a.Apply("model", "magpie/"+ref); err != nil { + t.Fatal(err) + } + if len(f.providers) != 2 || f.provider("magpie") == nil { + t.Fatalf("a second magpie in Alma: %v", f.providers) + } + f.takeWrites() + if err := a.Disconnect(); err != nil { + t.Fatal(err) + } + if w := f.takeWrites(); slices.Contains(w, "DELETE /api/providers/magpie") { + t.Fatalf("the user's own Magpie was removed: %v", w) + } + p := f.provider("magpie") + if p == nil || p["name"] != "Magpie" || p["enabled"] != true { + t.Fatalf("preset after off: %v", p) + } + if d := f.settings["chat"].(map[string]any)["defaultModel"]; d == "magpie:"+ref { + t.Fatalf("still on magpie after off: %v", d) + } + // switched off: magpie keeps out of the user's provider + p["models"] = []any{"few"} + f.takeWrites() + if err := a.Sync(); err != nil { + t.Fatal(err) + } + if w := f.takeWrites(); len(w) != 0 { + t.Fatalf("sync after off wrote %v", w) + } + }) + + t.Run("turned off in Alma", func(t *testing.T) { + f.mu.Lock() + f.provider("magpie")["enabled"] = false + f.mu.Unlock() + if err := a.Apply("model", "magpie/"+ref); err != nil { + t.Fatal(err) + } + if p := f.provider("magpie"); p["enabled"] != true || len(f.providers) != 2 { + t.Fatalf("providers: %v", f.providers) + } + if err := a.Disconnect(); err != nil { + t.Fatal(err) + } + if p := f.provider("magpie"); p == nil || p["enabled"] != false { + t.Fatalf("the user's Magpie, off before, after magpie is off: %v", p) + } + }) + + t.Run("none in Alma", func(t *testing.T) { + f.mu.Lock() + f.providers = f.providers[:1] + f.mu.Unlock() + if err := a.Apply("model", "magpie/"+ref); err != nil { + t.Fatal(err) + } + p := f.provider("magpie") + if len(f.providers) != 2 || p == nil || p["name"] != "magpie" || p["baseURL"] != gatewayV1() { + t.Fatalf("magpie's provider not under the preset's id: %v", f.providers) + } + if err := a.Disconnect(); err != nil { + t.Fatal(err) + } + if len(f.providers) != 1 || f.providers[0]["id"] != "own" { + t.Fatalf("magpie's own provider stays after off: %v", f.providers) + } + }) + + t.Run("preset id taken elsewhere", func(t *testing.T) { + other := almaPreset(true, ref) + other["baseURL"] = "http://192.168.1.9:3425/v1" + f.mu.Lock() + f.providers = append(f.providers[:1], other) + f.mu.Unlock() + if err := a.Apply("model", "magpie/"+ref); err != nil { + t.Fatal(err) + } + if len(f.providers) != 3 || other["baseURL"] != "http://192.168.1.9:3425/v1" { + t.Fatalf("providers: %v", f.providers) + } + }) +}