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) + } + }) +}