Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.
provider: asking a plugin for its model id replaces just the model (#4004)
yetone4391ff0f5610
diff --git a/internal/provider/plugins.go b/internal/provider/plugins.goindex 714002fb5..317e86f54 100644--- a/internal/provider/plugins.go+++ b/internal/provider/plugins.go@@ -13,6 +13,7 @@ import ("strings""time"+ "github.com/tidwall/gjson""github.com/yetone/magpie/internal/catalog""github.com/yetone/magpie/internal/plugin""github.com/yetone/magpie/internal/probelog"@@ -681,9 +682,13 @@ func modelAPIID(m plugin.Model) string {return m.ID}+// withModel is body asking for model.func withModel(body []byte, model string) []byte {+ if b, ok := replaceModel(body, model); ok {+ return b+ }var v map[string]json.RawMessage- if json.Unmarshal(body, &v) != nil {+ if json.Unmarshal(body, &v) != nil || v == nil {return body}v["model"], _ = json.Marshal(model)@@ -693,6 +698,32 @@ func withModel(body []byte, model string) []byte {return body}+// replaceModel is body with its one top-level "model", a string, replaced+// by model and every other byte as it came; false when body isn't JSON or+// has no such "model". Decoding a body and marshalling it again to ask+// a plugin for its model id allocated ten times a 45 MB request (#4004).+func replaceModel(body []byte, model string) ([]byte, bool) {+ if !gjson.ValidBytes(body) {+ return nil, false+ }+ n := 0+ for _, k := range gjson.GetBytes(body, "@keys").Array() {+ if k.Str == "model" {+ n+++ }+ }+ was := gjson.GetBytes(body, "model")+ at := was.Index+ if n != 1 || was.Type != gjson.String || at <= 0 {+ return nil, false+ }+ name, _ := json.Marshal(model)+ out := make([]byte, 0, len(body)-len(was.Raw)+len(name))+ out = append(out, body[:at]...)+ out = append(out, name...)+ return append(out, body[at+len(was.Raw):]...), true+}+func firstOf(ss ...string) string {for _, s := range ss {if s != "" {diff --git a/internal/provider/plugins_withmodel_test.go b/internal/provider/plugins_withmodel_test.gonew file mode 100644index 000000000..8d57cbbe2--- /dev/null+++ b/internal/provider/plugins_withmodel_test.go@@ -0,0 +1,53 @@+package provider++import (+ "encoding/json"+ "reflect"+ "runtime"+ "strings"+ "testing"+)++// withModel asks for the plugin's model and leaves the rest of the+// request as it was, a "model" deeper in the body included.+func TestWithModelAsksForTheModel(t *testing.T) {+ for _, c := range []struct{ in, want string }{+ {`{"model":"a","messages":[{"role":"user","content":"<x>","model":"keep"}]}`, `{"model":"b","messages":[{"role":"user","content":"<x>","model":"keep"}]}`},+ {`{ "stream" : true , "model" : "a" }`, `{ "stream" : true , "model" : "b" }`},+ {`{"model":"a\"q","x":1}`, `{"model":"b","x":1}`},+ {`{"x":1}`, `{"model":"b","x":1}`},+ {`{"model":1,"x":1}`, `{"model":"b","x":1}`},+ {`{"model":"a","model":"c","x":1}`, `{"model":"b","x":1}`},+ {`{"model":"a",`, `{"model":"a",`},+ {`null`, `null`},+ } {+ got := string(withModel([]byte(c.in), "b"))+ if got == c.want {+ continue+ }+ var g, w any+ if json.Unmarshal([]byte(got), &g) != nil || json.Unmarshal([]byte(c.want), &w) != nil || !reflect.DeepEqual(g, w) {+ t.Errorf("withModel(%s) = %s, want %s", c.in, got, c.want)+ }+ }+}++// A 45 MB request is copied once to ask for another model, not decoded+// and marshalled again (#4004).+func TestWithModelCopiesOnce(t *testing.T) {+ body := []byte(`{"model":"a","messages":[{"role":"tool","content":"` + strings.Repeat("output <line>\\n", 3<<20) + `"}]}`)+ runtime.GC()+ var m runtime.MemStats+ runtime.ReadMemStats(&m)+ before := m.TotalAlloc+ out := withModel(body, "plugin-model")+ runtime.ReadMemStats(&m)+ n := m.TotalAlloc - before+ t.Logf("%d MiB body: withModel allocated %d MiB", len(body)>>20, n>>20)+ if n > uint64(len(body))*11/10 {+ t.Errorf("withModel allocated %d MiB for a %d MiB body", n>>20, len(body)>>20)+ }+ if !strings.HasPrefix(string(out), `{"model":"plugin-model","messages":`) {+ t.Errorf("withModel wrote %.60s", out)+ }+}