While Pi's ~/.pi/agent/settings.json has a non-empty enabledModels (its Ctrl+P / /scoped-models list), a new session starts on defaultProvider/defaultModel only when that list takes the model in, else on the list's first entry, so a model picked in magpie outside the list was written and never used. Setting Pi's model now adds it to enabledModels as provider/model, as Pi's own picker does, when nothing in the list takes it in already: the same reference with or without a :level after it, or a glob (*, ?, [..], without case) that the reference or its model id fits. The user's entries, their order and comments stay; no list is made where there is none, and an empty one is left as it is. Turning the model back to Pi's own takes magpie/… entries out with magpie's provider, removing the key rather than leaving it empty. The model field shows what a new session starts on: the list's first model when the saved default isn't in the list and that entry names one model outright. The list itself isn't editable in magpie yet; Field is single-valued, and an editor needs a multi-value field kind in the API, GUI (a multi-select, reorderable picker), TUI and CLI. Pi's models.json now gives each magpie model the API its provider speaks natively, so the gateway passes the request through instead of translating Chat: Responses-native models keep openai-responses; a Messages-only provider's (a custom Anthropic-compatible endpoint, as Cleary's) get api anthropic-messages with baseUrl the gateway's root (Anthropic's SDK adds /v1/messages). A Claude from 4.6 on also gets compat.forceAdaptiveThinking, so Pi asks for adaptive thinking and an effort, not the budget such a Claude refuses (why these stayed on Chat before). Magpie's models stay under providers.magpie; other providers are untouched. Why Chat→Messages could 400 where Pi's direct Messages didn't: with reasoning on, buildAnthropic raised max_tokens to budget+4096 (28096 for high) past the client's own max_completion_tokens, which Pi sets to the model's output limit, so an upstream capped below that refused it. The budget now fits under a cap the client gives (at most the cap less 1024; no thinking when the cap is 2048 or less); with no cap it still raises max_tokens as before. Tested: go test -tags nogui ./... (new: TestPiTakes, TestPiModelJoinsEnabledModels, TestPiModelOffLeavesEnabledModels, TestThinkingBudgetFitsTheClientsCap; TestPiModelsAskedOnTheirNativeAPI extended), nogui builds for darwin, linux and windows. In a sandbox HOME: settings.json with enabledModels [xai/grok-4.6, openai-codex/gpt-5.6-sol, deepseek/deepseek-flash], magpie pi magpie/cleary/kimi-k3 appended the model and kept the rest, magpie pi default took it out again; models.json listed cleary's models on anthropic-messages, a Chat relay's on the provider's openai-completions, a Responses vendor's on openai-responses. Pi 0.87.1 loaded that models.json (--list-models) and, against a fake Messages endpoint standing in for the gateway (MAGPIE_ADDR on a spare port), posted to /v1/messages with the gateway key: kimi-k3 with a 15360-token budget under max_tokens 16384, claude-sonnet-5 with adaptive thinking and effort high.
113 lines
4.6 KiB
Go
113 lines
4.6 KiB
Go
package gateway
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
)
|
|
|
|
// A Chat client's max tokens is what the model can write — Pi sends the
|
|
// model's own output limit — so the thinking budget fits under it rather
|
|
// than max_tokens raised past it, which the upstream refuses with a 400.
|
|
// With no cap asked, max_tokens still makes room for the budget.
|
|
func TestThinkingBudgetFitsTheClientsCap(t *testing.T) {
|
|
for _, c := range []struct {
|
|
max int
|
|
effort string
|
|
wantMax float64
|
|
wantBudget float64
|
|
wantNoThink bool
|
|
}{
|
|
{16384, "high", 16384, 15360, false},
|
|
{32000, "high", 32000, 24000, false},
|
|
{8192, "xhigh", 8192, 7168, false},
|
|
{0, "high", 28096, 24000, false},
|
|
{2048, "high", 2048, 0, true},
|
|
} {
|
|
body, _ := json.Marshal(map[string]any{"model": "k", "max_completion_tokens": c.max, "reasoning_effort": c.effort,
|
|
"messages": []map[string]any{{"role": "user", "content": "hi"}}})
|
|
r, err := parseChat(body)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var out map[string]any
|
|
json.Unmarshal(buildAnthropic(r, "kimi-k3"), &out)
|
|
if out["max_tokens"] != c.wantMax {
|
|
t.Errorf("%d %s: max_tokens %v, want %v", c.max, c.effort, out["max_tokens"], c.wantMax)
|
|
}
|
|
th, _ := out["thinking"].(map[string]any)
|
|
if c.wantNoThink {
|
|
if th != nil {
|
|
t.Errorf("%d %s: thinking %v with no room for it", c.max, c.effort, th)
|
|
}
|
|
continue
|
|
}
|
|
if th["budget_tokens"] != c.wantBudget {
|
|
t.Errorf("%d %s: budget %v, want %v", c.max, c.effort, th["budget_tokens"], c.wantBudget)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestAdaptiveThinking(t *testing.T) {
|
|
for model, want := range map[string]bool{
|
|
"claude-opus-5-5": true, "claude-opus-5": true, "claude-sonnet-4-6": true,
|
|
"claude-opus-4-7": true, "anthropic.claude-sonnet-4.6-v1": true, "claude-sonnet-5": true,
|
|
"claude-sonnet-4-5-20250929": false, "claude-sonnet-4-20250514": false, "claude-opus-4-1": false,
|
|
"claude-3-7-sonnet-20250219": false, "deepseek-v4": false, "claude-haiku-4-5": false,
|
|
} {
|
|
if got := adaptiveOnly(model); got != want {
|
|
t.Errorf("adaptiveOnly(%q) = %v", model, got)
|
|
}
|
|
}
|
|
var out map[string]any
|
|
json.Unmarshal(buildAnthropic(&Request{Thinking: true, Effort: "xhigh"}, "claude-opus-5-5"), &out)
|
|
if th, _ := json.Marshal(out["thinking"]); string(th) != `{"type":"adaptive"}` {
|
|
t.Errorf("thinking = %s", th)
|
|
}
|
|
if oc, _ := json.Marshal(out["output_config"]); string(oc) != `{"effort":"max"}` {
|
|
t.Errorf("output_config = %s", oc)
|
|
}
|
|
json.Unmarshal(buildAnthropic(&Request{Thinking: true, Effort: "low"}, "claude-sonnet-4-5"), &out)
|
|
if th, _ := json.Marshal(out["thinking"]); string(th) != `{"budget_tokens":4096,"type":"enabled"}` {
|
|
t.Errorf("old model thinking = %s", th)
|
|
}
|
|
}
|
|
|
|
// Z.ai's GLM-5.2 and GLM-5.3 take their effort in output_config, as ZCode
|
|
// sends it, fitted to their levels; a budget alone leaves them at theirs
|
|
func TestGLMEffortInOutputConfig(t *testing.T) {
|
|
for model, want := range map[string]bool{
|
|
"GLM-5.3": true, "glm-5.3-flash": true, "GLM-5.2": true, "zai/glm-5.3": true,
|
|
"GLM-5-Turbo": false, "glm-5.1": false, "glm-5.30": false, "claude-opus-5-5": false,
|
|
} {
|
|
if got := effortInOutputConfig.MatchString(model); got != want {
|
|
t.Errorf("effortInOutputConfig(%q) = %v", model, got)
|
|
}
|
|
}
|
|
var out map[string]any
|
|
json.Unmarshal(buildAnthropic(&Request{Thinking: true, Effort: "max"}, "GLM-5.3"), &out)
|
|
if oc, _ := json.Marshal(out["output_config"]); string(oc) != `{"effort":"max"}` {
|
|
t.Errorf("output_config = %s", oc)
|
|
}
|
|
out = nil
|
|
json.Unmarshal(buildAnthropic(&Request{Thinking: true, Effort: "high"}, "GLM-5-Turbo"), &out)
|
|
if out["output_config"] != nil {
|
|
t.Errorf("GLM-5-Turbo asked output_config: %v", out["output_config"])
|
|
}
|
|
|
|
levels := []string{"low", "high", "max"}
|
|
for body, want := range map[string]string{
|
|
// Claude Code's budget for medium, the nearest of GLM-5.3's
|
|
`{"thinking":{"type":"enabled","budget_tokens":10000}}`: `{"output_config":{"effort":"high"},"thinking":{"budget_tokens":10000,"type":"enabled"}}`,
|
|
`{"thinking":{"type":"adaptive"},"output_config":{"effort":"low","format":"x"}}`: `{"output_config":{"effort":"low","format":"x"},"thinking":{"type":"adaptive"}}`,
|
|
`{"thinking":{"type":"adaptive"},"output_config":{"effort":"xhigh"}}`: `{"output_config":{"effort":"max"},"thinking":{"type":"adaptive"}}`,
|
|
`{"thinking":{"type":"disabled"}}`: `{"thinking":{"type":"disabled"}}`,
|
|
`{"max_tokens":5}`: `{"max_tokens":5}`,
|
|
} {
|
|
var v any
|
|
json.Unmarshal(withOutputEffort([]byte(body), levels), &v)
|
|
if got, _ := json.Marshal(v); string(got) != want {
|
|
t.Errorf("%s:\n got %s\n want %s", body, got, want)
|
|
}
|
|
}
|
|
}
|