From adde88ef23643dbda6ca55b2d77b2022b68c362c Mon Sep 17 00:00:00 2001 From: yetone Date: Wed, 30 Sep 2026 22:58:50 +0800 Subject: [PATCH] =?UTF-8?q?agents:=20Kimi=20Code=20offers=20a=20model's=20?= =?UTF-8?q?thinking=20levels,=20and=20the=20level=20picked=20reaches=20the?= =?UTF-8?q?=20model=20(#333)=20=E2=80=94=20the=20new=20Kimi=20Code=20(2.x)?= =?UTF-8?q?=20builds=20its=20thinking=20picker=20from=20a=20model's=20supp?= =?UTF-8?q?ort=5Fefforts=20and=20default=5Feffort,=20which=20magpie=20neve?= =?UTF-8?q?r=20wrote,=20so=20it=20offered=20thinking=20only=20on=20or=20of?= =?UTF-8?q?f.=20magpie=20now=20writes=20the=20model's=20levels=20(none=20l?= =?UTF-8?q?eft=20out:=20Kimi=20Code's=20Off=20is=20that)=20and=20default?= =?UTF-8?q?=5Feffort=20high=20when=20the=20model=20has=20it,=20to=20~/.kim?= =?UTF-8?q?i-code=20only;=20the=20old=20kimi-cli's=20schema=20has=20neithe?= =?UTF-8?q?r.=20Kimi=20Code=20sends=20the=20level=20inside=20thinking=20({?= =?UTF-8?q?"type":"enabled","effort":=E2=80=A6}),=20not=20as=20reasoning?= =?UTF-8?q?=5Feffort,=20so=20the=20gateway=20moves=20it=20there=20on=20a?= =?UTF-8?q?=20Chat=20request=20that=20doesn't=20say=20reasoning=5Feffort?= =?UTF-8?q?=20itself,=20and=20fits=20it=20like=20any=20other=20agent's.=20?= =?UTF-8?q?Checked=20with=20the=20real=20kimi=20CLI=20(2.1.1)=20in=20a=20s?= =?UTF-8?q?andbox=20HOME=20against=20a=20fake=20upstream:=20kimi=20doctor?= =?UTF-8?q?=20passes,=20/model=20shows=20Off=20Low=20Medium=20High=20Max,?= =?UTF-8?q?=20a=20pick=20reaches=20upstream=20as=20reasoning=5Feffort;=20u?= =?UTF-8?q?nknown=20values=20don't=20make=20it=20reject=20the=20file.=20Te?= =?UTF-8?q?stKimiEfforts=20and=20TestKimiCodeEffortReachesChatVendor=20fai?= =?UTF-8?q?l=20without=20the=20change.=20go=20vet,=20go=20build=20-tags=20?= =?UTF-8?q?nogui=20(darwin,=20GOOS=3Dlinux),=20GOOS=3Dwindows=20go=20build?= =?UTF-8?q?,=20go=20test=20-tags=20nogui=20./...=20pass.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/agent/kimi.go | 27 +++++++++ internal/agent/kimi_efforts_test.go | 82 ++++++++++++++++++++++++++++ internal/gateway/chat.go | 24 ++++++++ internal/gateway/gateway.go | 3 + internal/gateway/kimi_effort_test.go | 60 ++++++++++++++++++++ 5 files changed, 196 insertions(+) create mode 100644 internal/agent/kimi_efforts_test.go create mode 100644 internal/gateway/kimi_effort_test.go diff --git a/internal/agent/kimi.go b/internal/agent/kimi.go index 9f07d8c62..fe370c9d3 100644 --- a/internal/agent/kimi.go +++ b/internal/agent/kimi.go @@ -17,6 +17,7 @@ package agent import ( "os" "path/filepath" + "slices" "strconv" "strings" @@ -81,11 +82,37 @@ func kimiModelTables(legacy bool) []edit.Table { if len(caps) > 0 { kvs = append(kvs, edit.KV{Path: "capabilities", Value: edit.Raw("[" + strings.Join(caps, ", ") + "]")}) } + if !legacy { + kvs = append(kvs, kimiEfforts(m.Efforts)...) + } out = append(out, edit.Table{Name: "models." + strconv.Quote(magpieID+"/"+m.ID), KVs: kvs}) } return out } +// kimiEfforts are a thinking model's levels for the new Kimi Code's +// thinking picker (#333): support_efforts, and default_effort high where +// the model has it, as Kimi Code takes for its own models (else it starts +// on the middle one). Without them it offers thinking on or off only and +// asks for no level at all. none is left out: Kimi Code's own off turns +// thinking off. kimi-cli has neither key. +func kimiEfforts(efforts []string) []edit.KV { + var levels []string + for _, e := range efforts { + if e != "none" { + levels = append(levels, strconv.Quote(e)) + } + } + if len(levels) == 0 { + return nil + } + kvs := []edit.KV{{Path: "support_efforts", Value: edit.Raw("[" + strings.Join(levels, ", ") + "]")}} + if slices.Contains(efforts, "high") { + kvs = append(kvs, edit.KV{Path: "default_effort", Value: "high"}) + } + return kvs +} + func kimi(home string) *Agent { dir, legacy := KimiDir(home) path := filepath.Join(dir, "config.toml") diff --git a/internal/agent/kimi_efforts_test.go b/internal/agent/kimi_efforts_test.go new file mode 100644 index 000000000..e9c79d31a --- /dev/null +++ b/internal/agent/kimi_efforts_test.go @@ -0,0 +1,82 @@ +package agent + +import ( + "os" + "path/filepath" + "reflect" + "testing" + + "github.com/pelletier/go-toml/v2" + + "github.com/yetone/magpie/internal/catalog" + "github.com/yetone/magpie/internal/provider" +) + +// #333: the new Kimi Code is told each thinking model's levels +// (support_efforts, default_effort high where there is one), or its picker +// has thinking on and off only; none is its own off, so it isn't a level, +// and kimi-cli, which knows neither key, gets neither. +func TestKimiEfforts(t *testing.T) { + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config")) + t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache")) + t.Setenv("KIMI_SHARE_DIR", "") + t.Setenv("KIMI_CODE_HOME", "") + if err := provider.Save(provider.Provider{ + ID: "think", Name: "Think", Chat: "https://example.test/v1", Key: "key", + Models: []string{"deep", "wide", "plain"}, + }); err != nil { + t.Fatal(err) + } + if err := catalog.SaveLive("think", "https://example.test/v1", []catalog.Model{ + {ID: "deep", Efforts: []string{"none", "high", "max"}}, + {ID: "wide", Efforts: []string{"low", "medium"}}, + {ID: "plain"}, + }); err != nil { + t.Fatal(err) + } + type model struct { + Capabilities []string `toml:"capabilities"` + SupportEfforts []string `toml:"support_efforts"` + DefaultEffort string `toml:"default_effort"` + } + read := func(path string) map[string]model { + t.Helper() + b, err := os.ReadFile(path) + if err != nil { + t.Fatal(err) + } + var c struct{ Models map[string]model } + if err := toml.Unmarshal(b, &c); err != nil { + t.Fatalf("%v\n%s", err, b) + } + return c.Models + } + + path := filepath.Join(home, ".kimi-code", "config.toml") + writeFile(t, path, "") + if err := kimi(home).Field("model").Set("magpie/think/deep"); err != nil { + t.Fatal(err) + } + ms := read(path) + for k, want := range map[string]model{ + "magpie/think/deep": {[]string{"thinking", "tool_use"}, []string{"high", "max"}, "high"}, + "magpie/think/wide": {[]string{"thinking", "tool_use"}, []string{"low", "medium"}, ""}, + "magpie/think/plain": {[]string{"tool_use"}, nil, ""}, + } { + if got := ms[k]; !reflect.DeepEqual(got, want) { + t.Errorf("%s: %+v, want %+v", k, got, want) + } + } + + os.RemoveAll(filepath.Join(home, ".kimi-code")) + legacy := filepath.Join(home, ".kimi", "config.toml") + writeFile(t, legacy, "") + if err := kimi(home).Field("model").Set("magpie/think/deep"); err != nil { + t.Fatal(err) + } + if got := read(legacy)["magpie/think/deep"]; got.SupportEfforts != nil || got.DefaultEffort != "" { + t.Errorf("kimi-cli's: %+v", got) + } +} diff --git a/internal/gateway/chat.go b/internal/gateway/chat.go index eb43bf6a0..125537690 100644 --- a/internal/gateway/chat.go +++ b/internal/gateway/chat.go @@ -382,6 +382,30 @@ func geminiCompat(host, model string) bool { return ip != nil && (ip.IsLoopback() || ip.IsPrivate()) } +// thinkingEffort moves the level the new Kimi Code (2.x) asks a Chat +// request for, inside its thinking switch ({"type":"enabled","effort": +// "high"}, #333), to reasoning_effort: where kimi-cli put it, beside +// thinking's type, and where the gateway and every other Chat API read it. +// A request that says reasoning_effort itself is left as it is. +func thinkingEffort(body []byte) []byte { + if !bytes.Contains(body, []byte(`"effort"`)) { + return body + } + var v struct { + ReasoningEffort *string `json:"reasoning_effort"` + Thinking map[string]any `json:"thinking"` + } + if json.Unmarshal(body, &v) != nil || v.ReasoningEffort != nil { + return body + } + effort, _ := v.Thinking["effort"].(string) + if effort == "" { + return body + } + delete(v.Thinking, "effort") + return withFields(body, map[string]any{"reasoning_effort": effort, "thinking": v.Thinking}) +} + // thinkingConfigField is Gemini's thinking_config as unfit remembers a // provider that refused it. const thinkingConfigField = "thinking_config" diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go index 238852af3..f70ac340b 100644 --- a/internal/gateway/gateway.go +++ b/internal/gateway/gateway.go @@ -564,6 +564,9 @@ func (s *Server) handle(from provider.Protocol) http.HandlerFunc { writeError(w, from, 400, err.Error()) return } + if from == provider.Chat { + body = thinkingEffort(body) + } s.serve(w, r, from, body) } } diff --git a/internal/gateway/kimi_effort_test.go b/internal/gateway/kimi_effort_test.go new file mode 100644 index 000000000..a5abe0b9d --- /dev/null +++ b/internal/gateway/kimi_effort_test.go @@ -0,0 +1,60 @@ +package gateway + +import ( + "encoding/json" + "os" + "path/filepath" + "testing" + + "github.com/yetone/magpie/internal/catalog" + "github.com/yetone/magpie/internal/provider" +) + +// #333: the new Kimi Code asks for its thinking level inside thinking +// ({"type":"enabled","effort":…}); it reaches the vendor as reasoning_effort, +// fitted to the model's levels as any other agent's is, and thinking keeps +// the rest. A request with no level, or its own reasoning_effort, goes as it +// was. +func TestKimiCodeEffortReachesChatVendor(t *testing.T) { + f := &fake{t: t, reply: sse( + `data: {"id":"c1","model":"glm-5.3-flash","choices":[{"delta":{"role":"assistant","content":"hi"}}]}`, + `data: {"id":"c1","choices":[{"delta":{},"finish_reason":"stop"}]}`, + `data: [DONE]`)} + up := setup(t, provider.Chat, f) + os.MkdirAll(filepath.Dir(catalog.CachePath()), 0o755) + os.WriteFile(catalog.CachePath(), []byte(`{"zai":{"models":{"glm-5.3-flash":{"id":"glm-5.3-flash","reasoning_options":[{"type":"effort","values":["low","high","max"]}]}}}}`), 0o644) + catalog.Reset() + t.Cleanup(catalog.Reset) + if err := provider.Save(provider.Provider{ID: "volc", Name: "Volc", Key: "k", Chat: up.URL + "/v1", Models: []string{"glm-5.3-flash"}}); err != nil { + t.Fatal(err) + } + for _, c := range []struct { + fields, effort string + thinking map[string]any + }{ + {`"thinking":{"type":"enabled","effort":"medium","keep":"all"}`, "high", map[string]any{"type": "enabled", "keep": "all"}}, + {`"thinking":{"type":"enabled","effort":"max"}`, "max", map[string]any{"type": "enabled"}}, + {`"thinking":{"type":"enabled","keep":"all"}`, "", map[string]any{"type": "enabled", "keep": "all"}}, + {`"thinking":{"type":"disabled"}`, "", map[string]any{"type": "disabled"}}, + {`"reasoning_effort":"low","thinking":{"type":"enabled","effort":"max"}`, "low", map[string]any{"type": "enabled", "effort": "max"}}, + } { + code, body := post(t, "/v1/chat/completions", `{"model":"volc/glm-5.3-flash","stream":true,"messages":[{"role":"user","content":"hi"}],`+c.fields+`}`) + if code != 200 { + t.Fatalf("status %d: %s", code, body) + } + var got struct { + Effort string `json:"reasoning_effort"` + Thinking map[string]any `json:"thinking"` + } + json.Unmarshal(f.got, &got) + if got.Effort != c.effort || !jsonEqual(got.Thinking, c.thinking) { + t.Errorf("%s: sent %s", c.fields, f.got) + } + } +} + +func jsonEqual(a, b any) bool { + x, _ := json.Marshal(a) + y, _ := json.Marshal(b) + return string(x) == string(y) +}