cybercaius picked AgentRouter's gpt-6.1-sol in Claude Desktop, and the Routing page showed Claude Code's background calls in the same chat (3-9k tokens) going to leee/claude-haiku-5-5: "为什么Claude Desktop中模型 选择的是AgentRouter · gpt-6.1-sol,但在对话过程中还是会去请求leee供应商的 模型呢?" Cause: magpie's own default, not a pick of theirs. desktopModels tagged every Claude model magpie serves with its own tier in the /v1/models it gives Desktop (anthropic_family_tier,f55747ce8) whenever that tier had no pick. Desktop's Code tab hands a tagged model to Claude Code as ANTHROPIC_DEFAULT_HAIKU_MODEL, its small, fast model, so every haiku call (summaries, bash prefix checks and the like) asked for Leee's haiku by its catalog id and was served there, whatever the chat was on. Now: - Only a model the user picked for a tier (claude-desktop.tiers.json, the Agents page's opus/sonnet/haiku/fable) is tagged. Untagged, Claude Code asks for its own id of the tier (claude-haiku-4-5-…). - desktopTurn: a background call (no tools) or a subagent's turn for a Claude model by Claude's own id that is no catalog id (claudeOwnID) stays on the chat's provider (desktopPicked) even when another provider serves a model of that name; before, only an unserved one did. A lead's turn and a tier pick (a catalog id) go as asked. - servedBeside: the chat's provider's model of that name (also one listed by the bare name), else its newest model of the same tier, beside the chat's model first, else "" and the call goes to the chat's model. A chat on GPT gets its haiku calls on that GPT; a chat on a provider with haikus keeps them on a haiku there. - The tier picker's "Not set" note says so in en/zh/zh-TW/ja/de ("the chat's provider's model of the tier, else the chat's model"). - gateway-routing.md describes the rule. A value the user set is untouched: a tier pick stays tagged and served as picked (asserted in the new test). Tests: TestClaudeDesktopBackgroundStaysOnChatProvider (new): AgentRouter gpt-6.1-sol + Leee claude-haiku-5-5 + a third provider with two haikus; asserts nothing tagged unpicked, Claude Code's haiku calls (bare claude-haiku-5-5 served elsewhere, an unserved dated haiku, a subagent) go to agentrouter/gpt-6.1-sol, a chat on the third provider gets its haikus, and a picked haiku is tagged and kept. Red on5143bf593(old code, test file only): "auto/claude-haiku-5-5 tagged "haiku" with nothing picked". TestClaudeDesktopTiers expects no tag unpicked; TestClaudeDesktopClassifierOnRemoteMagpie's unserved claude-haiku-4-5 now goes to the remote's claude-haiku-5-5 (same provider, same tier), and an unserved fable still to the session's opus. Broken by hand (mote, new test): - elsewhere forced false: FAIL "chat on AgentRouter, claude-haiku-5-5 (subagent false) sent to "claude-haiku-5-5"" (and the subagent and Claudeish cases). - servedBeside's tier stand-in skipped: FAIL "chat on Claudeish, claude-haiku-4-5-20251001 sent to "claudeish/claude-opus-5-5"". Restored, it passes. The first run of this change also turned the old remote test's expectation red ("unserved: sent "work/claude-haiku-5-5""), the tier stand-in at work; changed on purpose, see above. Run (mote, this change on5143bf593): go test ./internal/gateway ./internal/agent/... -count=1 -timeout 45m: gateway ok (889s), agent ok (469s). Rebased onto origin/main after the run; the commits since touch no file here. GUI (wsl-run, magpie-desktop): desktop-tiers.test.cjs chromium+webkit, en+zh, 4/4 ok; i18n-keys.test.cjs 8/8 ok. gofmt -l clean. Not tried: a real Claude Desktop. Desktop reads /v1/models at start-up, so the change reaches it after Desktop is restarted.
117 lines
3.9 KiB
Go
117 lines
3.9 KiB
Go
package gateway
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http/httptest"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// Claude Desktop's Code tab runs a subagent on a tier only when the
|
|
// gateway's /v1/models tags a model with it; untagged, every subagent ran on
|
|
// the chat's model (WilianWeng). A model the user picks for a tier is tagged
|
|
// with it as its default, and Claude Code's own id of a tier no row carries
|
|
// goes to the tier's pick. A Claude model is not tagged with its own tier
|
|
// unpicked (cybercaius: another provider's haiku took Claude Code's small
|
|
// calls).
|
|
func TestClaudeDesktopTiers(t *testing.T) {
|
|
f := &fake{reply: sse(
|
|
`event: message_start`+"\n"+`data: {"type":"message_start","message":{"id":"m","type":"message","role":"assistant","model":"m1","content":[],"usage":{"input_tokens":1,"output_tokens":0}}}`,
|
|
`event: message_stop`+"\n"+`data: {"type":"message_stop"}`)}
|
|
up := setup(t, provider.Anthropic, f)
|
|
if err := provider.Save(provider.Provider{ID: "fake", Name: "Fake", Key: "k", Anthropic: up.URL,
|
|
Models: []string{"m1", "claude-sonnet-5", "m2"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
desktop := Token + "-claude-desktop"
|
|
type row struct {
|
|
Tier string `json:"anthropic_family_tier"`
|
|
Default bool `json:"is_family_default"`
|
|
}
|
|
rows := func() map[string]row {
|
|
t.Helper()
|
|
req := httptest.NewRequest("GET", "/v1/models?limit=1000", nil)
|
|
req.Header.Set("x-api-key", desktop)
|
|
rec := httptest.NewRecorder()
|
|
New().Handler().ServeHTTP(rec, req)
|
|
var list struct {
|
|
Data []struct {
|
|
ID string `json:"id"`
|
|
row
|
|
} `json:"data"`
|
|
}
|
|
if err := json.Unmarshal(rec.Body.Bytes(), &list); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
out := map[string]row{}
|
|
for _, d := range list.Data {
|
|
out[DesktopCatalogID(d.ID)] = d.row
|
|
}
|
|
return out
|
|
}
|
|
send := func(model string) string {
|
|
t.Helper()
|
|
f.got = nil
|
|
req := httptest.NewRequest("POST", "/v1/messages", strings.NewReader(`{"model":"`+model+`","max_tokens":32000,"stream":true,"tools":[{"name":"Bash","input_schema":{"type":"object"}}],"messages":[{"role":"user","content":"hi"}]}`))
|
|
req.Header.Set("x-api-key", desktop)
|
|
rec := httptest.NewRecorder()
|
|
New().Handler().ServeHTTP(rec, req)
|
|
if rec.Code != 200 {
|
|
t.Fatalf("%s: %d %s", model, rec.Code, rec.Body)
|
|
}
|
|
return modelOf(f.got)
|
|
}
|
|
|
|
// nothing picked: nothing is tagged
|
|
got := rows()
|
|
if r := got["fake/claude-sonnet-5"]; r.Tier != "" || r.Default {
|
|
t.Fatalf("claude-sonnet-5 unpicked: %+v", r)
|
|
}
|
|
if r := got["fake/m1"]; r.Tier != "" {
|
|
t.Fatalf("m1 unpicked: %+v", r)
|
|
}
|
|
|
|
// m1 picked for opus and sonnet, m2 for haiku
|
|
for tier, id := range map[string]string{"opus": "fake/m1", "sonnet": "fake/m1", "haiku": "fake/m2"} {
|
|
if err := SetDesktopTier(tier, id); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
got = rows()
|
|
if r := got["fake/m1"]; r.Tier != "opus" || !r.Default {
|
|
t.Fatalf("m1 picked for opus and sonnet: %+v", r)
|
|
}
|
|
if r := got["fake/m2"]; r.Tier != "haiku" || !r.Default {
|
|
t.Fatalf("m2 picked for haiku: %+v", r)
|
|
}
|
|
if r := got["fake/claude-sonnet-5"]; r.Tier != "" {
|
|
t.Fatalf("claude-sonnet-5 with sonnet picked: %+v", r)
|
|
}
|
|
// Claude Code's own ids of a tier go to its pick, sonnet's too, which
|
|
// no row is tagged with; a Claude model magpie serves stays as asked
|
|
for asked, want := range map[string]string{
|
|
"claude-sonnet-4-6": "m1", "claude-opus-4-8[1m]": "m1", "claude-3-5-haiku-20241022": "m2",
|
|
"claude-haiku-4-5-20251001": "m2", "claude-sonnet-5": "claude-sonnet-5",
|
|
} {
|
|
if got := send(asked); got != want {
|
|
t.Errorf("%s: sent %q, want %q", asked, got, want)
|
|
}
|
|
}
|
|
if DesktopCatalogID(aliasFor("fake/m1")) != "fake/m1" || DesktopCatalogID("fake/m2") != "fake/m2" {
|
|
t.Fatal("DesktopCatalogID")
|
|
}
|
|
|
|
// unpicked again, the file goes
|
|
for _, tier := range DesktopTierNames {
|
|
if err := SetDesktopTier(tier, ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if _, err := os.Stat(desktopTiersPath()); !os.IsNotExist(err) {
|
|
t.Fatalf("tiers file left: %v", err)
|
|
}
|
|
}
|