diff --git a/internal/gateway/desktop.go b/internal/gateway/desktop.go index 5828bf9c2..ff35a685c 100644 --- a/internal/gateway/desktop.go +++ b/internal/gateway/desktop.go @@ -58,20 +58,81 @@ func desktopAccepts(id string) bool { // its display_name and description carry the model's name and magpie id. const desktopAlias = "anthropic/magpie-" -func aliasFor(id string) string { +// Claude Desktop offers a thinking-effort picker only for a model it knows: +// in a gateway's mode it reads no effort from /v1/models (IIt keeps id, +// display_name, description, supports_1m and anthropic_family_tier), and +// its signed model catalog can't be a gateway's, so its levels come from +// tIt in its app.asar (index.chunk-D3OyLXgG.js, 2.7032): +// +// let t=IC(e), n=HFt[t] ?? (UFt.test(t) ? VFt : void 0) +// +// HFt its table of Claude models (claude-sonnet-4-6, claude-opus-4-8, …), +// UFt /^(?:claude-)?(?:fable|mythos)(?:-|$)/ with VFt low, medium, high, +// xhigh and max (high recommended, thinking always on), and IC the id +// lowercased with a Bedrock-style ".anthropic." prefix and a date +// taken off. No provider/model id is either, so no model magpie served had +// the picker (ARNO). A model with reasoning levels is listed by one of +// these instead: +// +// - a Claude model (claude-opus-4-8 at any provider): +// magpie-.anthropic.claude-opus-4-8, which IC reads as +// claude-opus-4-8 and Claude Code as Claude Opus 4.8, as before; +// - any other: mythos-magpie-, which UFt matches. Nothing in it +// says haiku, sonnet or opus, so Desktop's small_fast pick is as it +// was, and Claude Code (which knows claude-mythos-… only) takes it for +// a model it doesn't know and sends its effort as output_config.effort. +// +// The effort chosen reaches the gateway as thinking plus +// output_config.effort and is fitted to the model's own levels there. +const ( + desktopEffortAlias = "mythos-magpie-" + desktopClaudeInfix = ".anthropic." +) + +// desktopClaude is a model id that is Anthropic's own Claude model, as tIt +// knows it: claude--, a date taken off. +var ( + desktopClaude = regexp.MustCompile(`^claude-(?:opus|sonnet|haiku|fable|mythos)-\d+(?:-\d+)?$`) + desktopDated = regexp.MustCompile(`-\d{8}$`) +) + +func aliasNumber(id string) string { h := fnv.New64a() h.Write([]byte(id)) - return fmt.Sprintf("%s%010d", desktopAlias, h.Sum64()%1e10) + return fmt.Sprintf("%010d", h.Sum64()%1e10) } -// claudeLooking is a model's id as Claude Desktop is shown it: as it is when -// it already reads as a Claude model's, else its alias (unprefixed serves it -// again). -func claudeLooking(id string) string { - if desktopAccepts(id) && !strings.HasPrefix(id, desktopAlias) { - return id +func aliasFor(id string) string { return desktopAlias + aliasNumber(id) } + +// claudeModel is the Claude model an entry is, lowercased and without a +// date or a vendor's "anthropic/" in front, or "" when it is none. +func claudeModel(e provider.Entry) string { + m := strings.ToLower(e.Model) + if i := strings.LastIndex(m, "/"); i >= 0 { + m = m[i+1:] } - return aliasFor(id) + m = desktopDated.ReplaceAllString(m, "") + if desktopClaude.MatchString(m) { + return m + } + return "" +} + +// claudeLooking is a model's id as Claude Desktop is shown it: one that +// gets its effort picker when the model has reasoning levels, else as it is +// when it already reads as a Claude model's, else its alias (unprefixed +// serves each again). +func claudeLooking(e provider.Entry) string { + if len(e.Efforts) > 0 { + if m := claudeModel(e); m != "" { + return "magpie-" + aliasNumber(e.ID) + desktopClaudeInfix + m + } + return desktopEffortAlias + aliasNumber(e.ID) + } + if desktopAccepts(e.ID) && !strings.HasPrefix(e.ID, desktopAlias) { + return e.ID + } + return aliasFor(e.ID) } // desktopModels is /v1/models as Claude Desktop is shown it: every model by @@ -90,7 +151,7 @@ func desktopModels(entries []provider.Entry) []map[string]any { name += " (" + e.ID + ")" } m["display_name"] = name - if m["id"] = claudeLooking(e.ID); m["id"] != e.ID { + if m["id"] = claudeLooking(e); m["id"] != e.ID { m["description"] = e.ID + " in magpie" } data = append(data, m) @@ -105,14 +166,27 @@ func desktopName(e provider.Entry) string { return e.ID } -// aliased is the catalog id an anthropic/magpie- alias stands for. +// aliased is the catalog id an alias Claude Desktop was given stands for: +// anthropic/magpie- (as it was listed before too), +// mythos-magpie- or magpie-.anthropic., with +// or without Claude Code's "[1m]". func aliased(id string) (string, bool) { id = strings.TrimSuffix(id, "[1m]") - if !strings.HasPrefix(id, desktopAlias) { + var number string + if n, ok := strings.CutPrefix(id, desktopAlias); ok { + number = n + } else if n, ok := strings.CutPrefix(id, desktopEffortAlias); ok { + number = n + } else if rest, ok := strings.CutPrefix(id, "magpie-"); ok { + if i := strings.Index(rest, desktopClaudeInfix); i > 0 { + number = rest[:i] + } + } + if len(number) != 10 { return "", false } for _, e := range provider.Catalog() { - if aliasFor(e.ID) == id { + if aliasNumber(e.ID) == number { return e.ID, true } } diff --git a/internal/gateway/desktop_effort_test.go b/internal/gateway/desktop_effort_test.go new file mode 100644 index 000000000..484203c5f --- /dev/null +++ b/internal/gateway/desktop_effort_test.go @@ -0,0 +1,167 @@ +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-.anthropic., any other by +// mythos-magpie-; 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}, + {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", "m2"}}); err != nil { + t.Fatal(err) + } + for _, ref := range []string{"fake/m1", "fake/claude-opus-4-8"} { + 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"` + Description string `json:"description"` + } `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[strings.TrimSuffix(m.Description, " in magpie")] = 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/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) + } + } +} diff --git a/internal/gateway/desktop_test.go b/internal/gateway/desktop_test.go index c6587e478..0a0898a5f 100644 --- a/internal/gateway/desktop_test.go +++ b/internal/gateway/desktop_test.go @@ -45,7 +45,7 @@ func TestAnthropicPrefixedModel(t *testing.T) { if !strings.Contains(rec.Body.String(), `"id":"`+aliasFor("fake/m1")+`"`) { t.Fatalf("Claude Desktop's list: %s", rec.Body) } - if claudeLooking("devin/claude-opus-5-5") != "devin/claude-opus-5-5" { + if claudeLooking(provider.Entry{ID: "devin/claude-opus-5-5"}) != "devin/claude-opus-5-5" { t.Fatal("a Claude id was prefixed") } if got := unprefixed("claude-sonnet-4-5"); got != "claude-sonnet-4-5" { @@ -67,8 +67,8 @@ func TestClaudeDesktopAliases(t *testing.T) { } } for _, id := range []string{"deepseek/deepseek-flash", "moonshot/kimi-k2.5", "openai/gpt-5.5", "codex/gpt-5-codex", "zai/glm-5"} { - a := claudeLooking(id) - if !desktopAccepts(a) || !strings.HasPrefix(a, desktopAlias) || a != claudeLooking(id) { + a := claudeLooking(provider.Entry{ID: id}) + if !desktopAccepts(a) || !strings.HasPrefix(a, desktopAlias) || a != claudeLooking(provider.Entry{ID: id}) { t.Errorf("%s listed as %s", id, a) } } diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go index caa74eb8e..3d18b4700 100644 --- a/internal/gateway/gateway.go +++ b/internal/gateway/gateway.go @@ -385,7 +385,8 @@ func (s *Server) model(w http.ResponseWriter, r *http.Request) { } // unprefixed is the model magpie serves by an id Claude Desktop was given -// for it (claudeLooking): anthropic/magpie-, or, as it listed them +// for it (claudeLooking): anthropic/magpie-, mythos-magpie-, +// magpie-.anthropic., or, as it listed them // before, "anthropic/" put in front of magpie's id. An id that is magpie's // as it stands (a provider named anthropic) is left alone. func unprefixed(id string) string { @@ -1663,13 +1664,17 @@ func conversationID(in http.Header, body []byte) string { // "low"|"medium"|"high"|"xhigh"|"max"`. var effortLevelsNamed = regexp.MustCompile(`(?i)\blow\b\W+(?:medium|high)\b`) -// bodyEffort is the reasoning effort a Chat or Responses request asks for. +// bodyEffort is the reasoning effort a Chat or Responses request asks for, +// or an Anthropic one in its output_config. func bodyEffort(proto provider.Protocol, body []byte) string { var v struct { ReasoningEffort string `json:"reasoning_effort"` Reasoning *struct { Effort string `json:"effort"` } `json:"reasoning"` + OutputConfig *struct { + Effort string `json:"effort"` + } `json:"output_config"` } if json.Unmarshal(body, &v) != nil { return "" @@ -1679,14 +1684,27 @@ func bodyEffort(proto provider.Protocol, body []byte) string { return v.ReasoningEffort case proto == provider.Responses && v.Reasoning != nil: return v.Reasoning.Effort + case proto == provider.Anthropic && v.OutputConfig != nil: + return v.OutputConfig.Effort } return "" } -// withBodyEffort asks a Chat or Responses request for effort instead, -// keeping the rest of its reasoning settings. +// withBodyEffort asks a Chat, Responses or Anthropic request for effort +// instead, keeping the rest of its reasoning settings. Anthropic's is +// output_config.effort, which Claude Desktop sends at any of low…max +// whatever the model takes. func withBodyEffort(proto provider.Protocol, body []byte, effort string) []byte { switch proto { + case provider.Anthropic: + var v struct { + OutputConfig map[string]any `json:"output_config"` + } + if json.Unmarshal(body, &v) != nil || v.OutputConfig == nil { + return body + } + v.OutputConfig["effort"] = effort + return withFields(body, map[string]any{"output_config": v.OutputConfig}) case provider.Chat: return withFields(body, map[string]any{"reasoning_effort": effort}) case provider.Responses: