Claude Desktop's Code tab runs Claude Code on the id magpie lists a model by. claudeLooking lists a Claude model with reasoning levels as magpie-<n>.anthropic.<model>, which Desktop and Claude Code read as that Claude model, but claudeModel matched dashed versions only. Copilot writes a version with a dot, so each of its Claude models fell to mythos-magpie-<n>, a model Claude Code doesn't know: Opus 5.5 ran at 32000 output tokens and effort high (128000 and medium for the model it knows), and Haiku 4.5, the haiku tier, was sent output_config.effort, which Copilot answers with 400 "model claude-haiku-4.5 does not support reasoning effort". claudeModel now dashes the version first (provider.ClaudeDashed). A request is still served by <n> alone (aliased), so Copilot still gets its own claude-opus-5.5. An id with a suffix (claude-sonnet-4.5-thinking) and other vendors' ids are listed as before. This is mdwsk88's fix from mdwsk88/magpie@51a5ad80, reviewed line by line and taken with its tests. Sibling: ClaudeDashed and ClaudeUndashed knew opus, sonnet, haiku and fable but not mythos, while desktopClaude, claudeLine and claudeVersion know it. Claude Code 2.1.296 (read from its binary) matches claude-mythos-5-1 by substring before claude-mythos-5, so a dotted claude-mythos-5.1 would read as Mythos 5; it is now dashed like the others, in Claude Code's settings and in Desktop's list. The other matchers of Claude ids (agent's claudeName, gateway's claudeFamily, claudeVersion, claudeLine, provider.sameModel) already take a dot. ClaudeFast's list is dashed but applies to api.anthropic.com alone, which never writes a dot. Run (go1.26.8 toolchain, sandbox HOME, private GOCACHE/GOMODCACHE, -tags nogui): - With the old desktop.go and claudedash.go put back, the new tests fail: TestClaudeDesktopEffortIDs "copilot/claude-opus-5.5 listed as mythos-magpie-5238445297, want ^magpie-\d{10}\.anthropic\.claude-opus-5-5$" (and the opus-4.6, sonnet-5.5, haiku-4.5, dated sonnet-4.5 and mythos-5.1 rows), TestClaudeDesktopDottedClaudeReachesItsModel "listed as mythos-magpie-9584025798", TestClaudeDashedOnlyClaudeVersions "ClaudeDashed("claude-mythos-5.1") = "claude-mythos-5.1"". Green with the change. - go test ./internal/gateway ./internal/provider: ok; -run 'Desktop|Claude' over internal/agent too: ok. -race -count=3 -run TestClaudeDesktop ./internal/gateway: ok. - gofmt -l clean; go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./...: clean. Not run: a real Copilot account or the real Claude Desktop through this build (the reporter's own run used a stub upstream and real Claude Code 2.1.295); Windows and WSL; the full go test ./... (machine loaded). Co-Authored-By: mdwsk88 <11055210+mdwsk88@users.noreply.github.com>
227 lines
11 KiB
Go
227 lines
11 KiB
Go
package gateway
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http/httptest"
|
|
"regexp"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// desktopPicker is Claude Desktop's tIt (index.chunk-D3OyLXgG.js, 2.7032):
|
|
// whether the model id it was given gets a thinking-effort picker. IC is
|
|
// qS of the lowercased id; HFt's keys that have effort levels, and UFt.
|
|
func desktopPicker(id string) bool {
|
|
e := strings.ToLower(id)
|
|
t := regexp.MustCompile(`^arn:aws[a-z-]*:bedrock:[^/]+/`).ReplaceAllString(e, "")
|
|
t = regexp.MustCompile(`^(?:[a-z][a-z0-9-]*\.)?anthropic\.`).ReplaceAllString(t, "")
|
|
stripped := t != e || regexp.MustCompile(`^claude-(?:[a-z]+-)?\d`).MatchString(t)
|
|
t = regexp.MustCompile(`\[[^\]]+\]$`).ReplaceAllString(t, "")
|
|
if stripped {
|
|
t = regexp.MustCompile(`-v\d+(?::\d+)?$`).ReplaceAllString(t, "")
|
|
}
|
|
t = regexp.MustCompile(`@\d{8}$`).ReplaceAllString(t, "")
|
|
t = regexp.MustCompile(`-\d{8}$`).ReplaceAllString(t, "")
|
|
switch t {
|
|
case "claude-sonnet-4-6", "claude-sonnet-5", "claude-opus-4-6", "claude-opus-4-7", "claude-opus-4-8", "claude-opus-5":
|
|
return true
|
|
}
|
|
return regexp.MustCompile(`^(?:claude-)?(?:fable|mythos)(?:-|$)`).MatchString(t)
|
|
}
|
|
|
|
// Desktop's small_fast pick (_$n): an id with haiku, sonnet or opus in it
|
|
func desktopSmallFast(id string) bool {
|
|
for _, w := range []string{"haiku", "sonnet", "opus"} {
|
|
if strings.Contains(id, w) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// a model with reasoning levels is listed to Claude Desktop by an id it
|
|
// offers its effort picker for (ARNO: 接入claude desktop后无法设置思考强度):
|
|
// a Claude model by magpie-<n>.anthropic.<model>, any other by
|
|
// mythos-magpie-<n>, and so a routing group whatever model leads it (else
|
|
// Desktop names it from its catalog, "Opus 5.5", not by the group's name);
|
|
// one without levels as before. Each is kept by Desktop, served again by
|
|
// magpie, with or without [1m], and none but a Claude model becomes
|
|
// Desktop's small_fast pick.
|
|
func TestClaudeDesktopEffortIDs(t *testing.T) {
|
|
levels := []string{"low", "medium", "high"}
|
|
for _, c := range []struct {
|
|
e provider.Entry
|
|
want string // the id's form
|
|
picker bool
|
|
}{
|
|
{provider.Entry{ID: "zai/glm-5.3", Model: "glm-5.3", Efforts: levels}, `^mythos-magpie-\d{10}$`, true},
|
|
{provider.Entry{ID: "x/claude-sonnet-5-thinking", Model: "claude-sonnet-5-thinking", Efforts: levels}, `^mythos-magpie-\d{10}$`, true},
|
|
{provider.Entry{ID: "or/anthropic/claude-opus-4-8", Model: "anthropic/claude-opus-4-8", Efforts: levels}, `^magpie-\d{10}\.anthropic\.claude-opus-4-8$`, true},
|
|
{provider.Entry{ID: "x/Claude-Sonnet-4-6-20260101", Model: "Claude-Sonnet-4-6-20260101", Efforts: levels}, `^magpie-\d{10}\.anthropic\.claude-sonnet-4-6$`, true},
|
|
{provider.Entry{ID: "x/claude-haiku-4-5", Model: "claude-haiku-4-5", Efforts: levels}, `^magpie-\d{10}\.anthropic\.claude-haiku-4-5$`, false},
|
|
// Copilot, and the relays that follow it, write a Claude version with a
|
|
// dot; Desktop and Claude Code read a Claude model by its dashed id
|
|
{provider.Entry{ID: "copilot/claude-opus-4.6", Model: "claude-opus-4.6", Efforts: levels}, `^magpie-\d{10}\.anthropic\.claude-opus-4-6$`, true},
|
|
{provider.Entry{ID: "copilot/claude-opus-5.5", Model: "claude-opus-5.5", Efforts: levels}, `^magpie-\d{10}\.anthropic\.claude-opus-5-5$`, false},
|
|
{provider.Entry{ID: "copilot/claude-sonnet-5.5", Model: "claude-sonnet-5.5", Efforts: levels}, `^magpie-\d{10}\.anthropic\.claude-sonnet-5-5$`, false},
|
|
{provider.Entry{ID: "x/claude-mythos-5.1", Model: "claude-mythos-5.1", Efforts: levels}, `^magpie-\d{10}\.anthropic\.claude-mythos-5-1$`, true},
|
|
{provider.Entry{ID: "copilot/claude-haiku-4.5", Model: "claude-haiku-4.5", Efforts: []string{"none", "high"}}, `^magpie-\d{10}\.anthropic\.claude-haiku-4-5$`, false},
|
|
{provider.Entry{ID: "x/Claude-Sonnet-4.5-20250929", Model: "Claude-Sonnet-4.5-20250929", Efforts: levels}, `^magpie-\d{10}\.anthropic\.claude-sonnet-4-5$`, false},
|
|
{provider.Entry{ID: "x/claude-sonnet-4.5-thinking", Model: "claude-sonnet-4.5-thinking", Efforts: levels}, `^mythos-magpie-\d{10}$`, true},
|
|
{provider.Entry{ID: "group/coding", Model: "claude-opus-5-5", Group: "coding", Efforts: levels}, `^mythos-magpie-\d{10}$`, true},
|
|
{provider.Entry{ID: "zai/glm-4.5-air", Model: "glm-4.5-air"}, `^anthropic/magpie-\d{10}$`, false},
|
|
{provider.Entry{ID: "x/claude-opus-4-8", Model: "claude-opus-4-8"}, `^x/claude-opus-4-8$`, false},
|
|
} {
|
|
id := claudeLooking(c.e)
|
|
if !regexp.MustCompile(c.want).MatchString(id) {
|
|
t.Errorf("%s listed as %s, want %s", c.e.ID, id, c.want)
|
|
}
|
|
if !desktopAccepts(id) {
|
|
t.Errorf("%s: Desktop would drop %s", c.e.ID, id)
|
|
}
|
|
if desktopPicker(id) != c.picker {
|
|
t.Errorf("%s as %s: picker %v", c.e.ID, id, !c.picker)
|
|
}
|
|
if strings.HasPrefix(id, desktopEffortAlias) && desktopSmallFast(id) {
|
|
t.Errorf("%s: %s would be Desktop's small_fast model", c.e.ID, id)
|
|
}
|
|
}
|
|
if desktopPicker("x/claude-opus-4-8") || desktopPicker(aliasFor("zai/glm-5.3")) {
|
|
t.Fatal("the ids listed before had a picker")
|
|
}
|
|
|
|
setup(t, provider.Anthropic, &fake{})
|
|
if err := provider.Save(provider.Provider{ID: "fake", Name: "Fake", Key: "k", Anthropic: "http://127.0.0.1:1",
|
|
Models: []string{"m1", "claude-opus-4-8", "claude-opus-5.5", "m2"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, ref := range []string{"fake/m1", "fake/claude-opus-4-8", "fake/claude-opus-5.5"} {
|
|
if err := provider.SetModelEfforts(ref, []string{"low", "high"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
req := httptest.NewRequest("GET", "/v1/models", nil)
|
|
req.Header.Set("x-api-key", Token+"-claude-desktop")
|
|
rec := httptest.NewRecorder()
|
|
New().Handler().ServeHTTP(rec, req)
|
|
var list struct {
|
|
Data []struct {
|
|
ID string `json:"id"`
|
|
} `json:"data"`
|
|
}
|
|
if err := json.Unmarshal(rec.Body.Bytes(), &list); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got := map[string]string{}
|
|
for _, m := range list.Data {
|
|
got[DesktopCatalogID(m.ID)] = m.ID
|
|
}
|
|
for id, want := range map[string]string{
|
|
"fake/m1": desktopEffortAlias + aliasNumber("fake/m1"),
|
|
"fake/claude-opus-4-8": "magpie-" + aliasNumber("fake/claude-opus-4-8") + ".anthropic.claude-opus-4-8",
|
|
"fake/claude-opus-5.5": "magpie-" + aliasNumber("fake/claude-opus-5.5") + ".anthropic.claude-opus-5-5",
|
|
"fake/m2": aliasFor("fake/m2"),
|
|
} {
|
|
if got[id] != want {
|
|
t.Errorf("%s listed as %q, want %q (%s)", id, got[id], want, rec.Body)
|
|
}
|
|
for _, asked := range []string{want, want + "[1m]"} {
|
|
if real, ok := aliased(asked); !ok || real != id {
|
|
t.Errorf("aliased(%s) = %q %v", asked, real, ok)
|
|
}
|
|
}
|
|
}
|
|
for _, bad := range []string{"mythos-magpie-0000000000", "magpie-0000000000.anthropic.claude-opus-4-8", "magpie-12.anthropic.claude-opus-4-8", "mythos-5"} {
|
|
if _, ok := aliased(bad); ok {
|
|
t.Errorf("aliased(%s) resolved", bad)
|
|
}
|
|
}
|
|
}
|
|
|
|
// the effort picked in Claude Desktop reaches the model, fitted to its
|
|
// levels: Desktop's Claude Code sends thinking plus output_config.effort
|
|
func TestClaudeDesktopEffortReachesModel(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"}`)}
|
|
setup(t, provider.Anthropic, f)
|
|
if err := provider.SetModelEfforts("fake/m1", []string{"low", "high"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
alias := desktopEffortAlias + aliasNumber("fake/m1")
|
|
for _, c := range []struct{ asked, sent string }{
|
|
{"low", "low"}, {"high", "high"}, {"max", "high"}, {"xhigh", "high"},
|
|
} {
|
|
body := `{"model":"` + alias + `","max_tokens":32000,"stream":true,"thinking":{"type":"adaptive"},"output_config":{"effort":"` + c.asked + `"},` +
|
|
`"tools":[{"name":"Bash","input_schema":{"type":"object"}}],"messages":[{"role":"user","content":"hi"}]}`
|
|
req := httptest.NewRequest("POST", "/v1/messages", strings.NewReader(body))
|
|
req.Header.Set("x-api-key", Token+"-claude-desktop")
|
|
rec := httptest.NewRecorder()
|
|
New().Handler().ServeHTTP(rec, req)
|
|
if rec.Code != 200 {
|
|
t.Fatalf("%s: %d %s", c.asked, rec.Code, rec.Body)
|
|
}
|
|
var sent struct {
|
|
Model string `json:"model"`
|
|
OutputConfig struct {
|
|
Effort string `json:"effort"`
|
|
} `json:"output_config"`
|
|
}
|
|
if err := json.Unmarshal(f.got, &sent); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if sent.Model != "m1" || sent.OutputConfig.Effort != c.sent {
|
|
t.Errorf("%s: sent %s at %q, want m1 at %q", c.asked, sent.Model, sent.OutputConfig.Effort, c.sent)
|
|
}
|
|
}
|
|
}
|
|
|
|
// a Claude model a provider spells with a dot is listed to Desktop by its
|
|
// dashed id, so Claude Code knows it for the Claude model it is, and a turn
|
|
// on that id reaches the provider's own spelling (Copilot's claude-opus-5.5
|
|
// listed as mythos-magpie-<n> ran as a model Claude Code doesn't know: 32000
|
|
// output tokens, effort high, and claude-haiku-4.5 asked for an effort Copilot
|
|
// answers with 400)
|
|
func TestClaudeDesktopDottedClaudeReachesItsModel(t *testing.T) {
|
|
f := &fake{reply: sse(
|
|
`event: message_start`+"\n"+`data: {"type":"message_start","message":{"id":"m","type":"message","role":"assistant","model":"claude-opus-5.5","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{"claude-opus-5.5"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := provider.SetModelEfforts("fake/claude-opus-5.5", []string{"low", "medium", "high"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
listed := claudeLooking(provider.Entry{ID: "fake/claude-opus-5.5", Model: "claude-opus-5.5", Efforts: []string{"low", "medium", "high"}})
|
|
if want := "magpie-" + aliasNumber("fake/claude-opus-5.5") + ".anthropic.claude-opus-5-5"; listed != want {
|
|
t.Fatalf("listed as %q, want %q", listed, want)
|
|
}
|
|
for _, asked := range []string{listed, listed + "[1m]"} {
|
|
body := `{"model":"` + asked + `","max_tokens":32000,"stream":true,"thinking":{"type":"adaptive"},"output_config":{"effort":"high"},` +
|
|
`"tools":[{"name":"Bash","input_schema":{"type":"object"}}],"messages":[{"role":"user","content":"hi"}]}`
|
|
req := httptest.NewRequest("POST", "/v1/messages", strings.NewReader(body))
|
|
req.Header.Set("x-api-key", Token+"-claude-desktop")
|
|
rec := httptest.NewRecorder()
|
|
New().Handler().ServeHTTP(rec, req)
|
|
if rec.Code != 200 {
|
|
t.Fatalf("%s: %d %s", asked, rec.Code, rec.Body)
|
|
}
|
|
var sent struct {
|
|
Model string `json:"model"`
|
|
OutputConfig struct {
|
|
Effort string `json:"effort"`
|
|
} `json:"output_config"`
|
|
}
|
|
if err := json.Unmarshal(f.got, &sent); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if sent.Model != "claude-opus-5.5" || sent.OutputConfig.Effort != "high" {
|
|
t.Errorf("%s: sent %s at %q, want claude-opus-5.5 at high", asked, sent.Model, sent.OutputConfig.Effort)
|
|
}
|
|
}
|
|
}
|