diff --git a/internal/agent/alma.go b/internal/agent/alma.go index f5e0e9d8d..dd5f3a082 100644 --- a/internal/agent/alma.go +++ b/internal/agent/alma.go @@ -241,9 +241,16 @@ func almaModels() (ids []string, known []map[string]any) { for _, m := range magpieModels("alma") { ids = append(ids, m.ID) k := map[string]any{"id": m.ID, "name": m.Name} - if caps := almaCaps(m); len(caps) > 0 { - k["capabilityOverrides"] = caps - } + caps := almaCaps(m) + // every chat model magpie lists takes tools, as its /v1/models + // says (gateway's toolsTold). Alma looks function calling up on + // models.dev by the id, finds none for a routing group's name or a + // plugin's model, and puts the tools in the prompt instead + // (hermesToolMiddleware): no native tools went out, and DeepSeek + // answered with its DSML call text, shown raw (ethanchi on + // Discord). One the user set in Alma is kept (almaUserFirst). + caps["functionCalling"] = true + k["capabilityOverrides"] = caps known = append(known, k) } // the model magpie draws with, which Alma offers for its image @@ -317,10 +324,16 @@ func almaCaps(m catalog.Model) map[string]any { return caps } +// almaUserFirst are the overrides whose value a model already has in Alma +// is kept over magpie's: magpie never wrote them before saying so, so one +// there is the user's own (Model Options › Tool Calling), and magpie only +// fills it in where it is missing. +var almaUserFirst = []string{"functionCalling"} + // almaWithOverrides is known with each model's overrides laid over the ones // it has in Alma: the overrides sent take the place of a model's, so one -// the user set there that magpie says nothing of (functionCalling, -// pricing) is sent back as it was. +// the user set there that magpie says nothing of (pricing) is sent back as +// it was, and one of almaUserFirst stays as the user set it. func almaWithOverrides(p *almaProvider, known []map[string]any) []map[string]any { had := map[string]map[string]any{} for _, k := range p.Known { @@ -345,6 +358,9 @@ func almaWithOverrides(p *almaProvider, known []map[string]any) []map[string]any merged[key] = v } for key, v := range mine { + if _, set := o[key]; set && slices.Contains(almaUserFirst, key) { + continue + } merged[key] = v } kk := map[string]any{} @@ -370,6 +386,9 @@ func almaSameModels(p *almaProvider, ids []string, known []map[string]any) bool mine, _ := k["capabilityOverrides"].(map[string]any) had, _ := p.Known[i]["capabilityOverrides"].(map[string]any) for key, v := range mine { + if _, set := had[key]; set && slices.Contains(almaUserFirst, key) { + continue + } if !sameJSONValue(had[key], v) { return false } diff --git a/internal/agent/alma_caps_test.go b/internal/agent/alma_caps_test.go index f39faebb4..0ac5f8362 100644 --- a/internal/agent/alma_caps_test.go +++ b/internal/agent/alma_caps_test.go @@ -58,7 +58,7 @@ func TestAlmaToldReasoning(t *testing.T) { return nil } js := func(v any) string { b, _ := json.Marshal(v); return string(b) } - if got := js(caps("think/gpt-5.5")); got != `{"contextWindow":400000,"maxOutputTokens":128000,"reasoning":true,"reasoningLevels":["low","medium","high","xhigh"],"vision":true}` { + if got := js(caps("think/gpt-5.5")); got != `{"contextWindow":400000,"functionCalling":true,"maxOutputTokens":128000,"reasoning":true,"reasoningLevels":["low","medium","high","xhigh"],"vision":true}` { t.Fatalf("gpt-5.5: %s", got) } if c := caps("think/switch-only"); c["reasoning"] != true || c["reasoningLevels"] != nil { @@ -154,3 +154,84 @@ func TestAlmaToldPricing(t *testing.T) { t.Fatalf("unpriced: %v", p) } } + +// Alma is told every one of magpie's chat models calls tools, as the +// gateway's /v1/models says (toolsTold): Alma looks function calling up on +// models.dev by the model's id, found nothing for a routing group's name or +// a plugin's model, and put the tools in the prompt (hermesToolMiddleware), +// so DeepSeek wrote its DSML call text out, shown raw (ethanchi on +// Discord). A Tool Calling the user set in Alma, on or off, is kept, and +// isn't written over again at every sync; one taken away is told again. +func TestAlmaToldTools(t *testing.T) { + syncHome(t) + if err := provider.Save(provider.Provider{ID: "relay2", Name: "Relay Two", Chat: "https://example.test/v1", Key: "key", + Models: []string{"DeepSeek", "kimi"}}); err != nil { + t.Fatal(err) + } + f := startAlma(t) + a := alma() + if err := os.MkdirAll(a.Dir, 0o755); err != nil { + t.Fatal(err) + } + if err := a.Apply("model", "magpie/relay2/DeepSeek"); err != nil { + t.Fatal(err) + } + var mine map[string]any + for _, p := range f.providers { + if p["name"] == "magpie" { + mine = p + } + } + if mine == nil { + t.Fatalf("no magpie provider: %v", f.providers) + } + model := func(id string) map[string]any { + t.Helper() + for _, m := range mine["availableModels"].([]any) { + if o := m.(map[string]any); o["id"] == id { + return o + } + } + t.Fatalf("%s not in %v", id, mine["availableModels"]) + return nil + } + tools := func(id string) any { + t.Helper() + c, _ := model(id)["capabilityOverrides"].(map[string]any) + return c["functionCalling"] + } + ids, _ := mine["models"].([]any) + if len(ids) == 0 { + t.Fatal("no models told") + } + for _, id := range ids { + if got := tools(id.(string)); got != true { + t.Fatalf("%s: functionCalling %v, want true", id, got) + } + } + f.takeWrites() + + // the user turned Tool Calling off for one, and Alma lost another's + // overrides: the user's kept, the other told again, and once told + // nothing is written again + model("relay2/DeepSeek")["capabilityOverrides"].(map[string]any)["functionCalling"] = false + delete(model("relay2/kimi"), "capabilityOverrides") + if err := a.Sync(); err != nil { + t.Fatal(err) + } + if w := f.takeWrites(); len(w) != 1 { + t.Fatalf("sync wrote %v", w) + } + if got := tools("relay2/DeepSeek"); got != false { + t.Fatalf("user's off: functionCalling %v", got) + } + if got := tools("relay2/kimi"); got != true { + t.Fatalf("kimi told again: functionCalling %v", got) + } + if err := a.Sync(); err != nil { + t.Fatal(err) + } + if w := f.takeWrites(); len(w) != 0 { + t.Fatalf("second sync wrote %v", w) + } +}