agents: Kimi Code offers a model's thinking levels, and the level picked reaches the model (#333) — the new Kimi Code (2.x) builds its thinking picker from a model's support_efforts and default_effort, which magpie never wrote, so it offered thinking only on or off. magpie now writes the model's levels (none left out: Kimi Code's Off is that) and default_effort high when the model has it, to ~/.kimi-code only; the old kimi-cli's schema has neither. Kimi Code sends the level inside thinking ({"type":"enabled","effort":…}), not as reasoning_effort, so the gateway moves it there on a Chat request that doesn't say reasoning_effort itself, and fits it like any other agent's. Checked with the real kimi CLI (2.1.1) in a sandbox HOME against a fake upstream: kimi doctor passes, /model shows Off Low Medium High Max, a pick reaches upstream as reasoning_effort; unknown values don't make it reject the file. TestKimiEfforts and TestKimiCodeEffortReachesChatVendor fail without the change. go vet, go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui ./... pass.

This commit is contained in:
yetone
2026-09-30 23:02:50 +08:00
parent f5f10bda4d
commit adde88ef23
5 changed files with 196 additions and 0 deletions
+27
View File
@@ -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")
+82
View File
@@ -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)
}
}
+24
View File
@@ -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"
+3
View File
@@ -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)
}
}
+60
View File
@@ -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)
}