gateway: Claude Desktop's titles go to the model its session is on (ARNO: 给claude desktop配置了自定义模型后偶尔还是会请求到claude自己的模型)
Desktop in its third-party mode asks for a session's title and branch name in one tool-less request whose model is its "small_fast" pick from the gateway's /v1/models: the first id with haiku in it, else sonnet, else opus (_$n in its app.asar, 2.7032), and the session's model only when no id has one. So with any Claude model in magpie's catalog, {"model":"claude-sonnet-5-thinking","max_tokens":200,"system":"You write short session titles. …"} went to that Claude model, whatever the user picked; Claude Code in the Code tab likewise asks for its own claude-haiku-… by name for small tasks, which magpie didn't serve for Desktop. Desktop has no config key for its small model (inferenceModels' anthropicFamilyTier / anthropic_family_tier only tags a listed model as a tier, and magpie doesn't know the user's pick), so the gateway does it: a request on the magpie-claude-desktop key (or Desktop's User-Agent) that carries tools is a session turn and its model is remembered (and kept in claude-desktop.model beside settings.json, for a title asked before the first turn after a restart); a small tool-less request (max_tokens at most 4096: a title asks 200, a turn tens of thousands) for a model whose id reads as Claude's, or any request for a model magpie doesn't serve, goes to that model instead. Requests from other agents, tool-less ones on non-Claude models, a tool-less chat turn on a Claude model the user picked, and a session that is itself on the Claude model are sent as asked. standIn's "magpie serves no such model" check is now unserved(), shared by both.
Tested: a gateway test against a fake Anthropic provider on the claude-desktop key (title before any turn as asked; after a turn on an aliased model the sonnet title and an unserved claude-haiku-4-5-20251001, with and without tools, go to it; a non-Claude tool-less request, Claude Code's key and a sonnet session untouched; the pick read back from disk after a reset), hasTools cases; go test -tags nogui ./... passes, CGO_ENABLED=0 go build -tags nogui for darwin, linux and windows.
This commit is contained in:
@@ -1,13 +1,19 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"hash/fnv"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/yetone/magpie/internal/provider"
|
||||
"github.com/yetone/magpie/internal/settings"
|
||||
)
|
||||
|
||||
// Claude Desktop, pointed at a third-party gateway, keeps a model only when
|
||||
@@ -120,3 +126,79 @@ func isClaudeDesktop(r *http.Request) bool {
|
||||
ua := r.Header.Get("User-Agent")
|
||||
return strings.HasPrefix(ua, "Mozilla/") && strings.Contains(ua, " Claude/")
|
||||
}
|
||||
|
||||
// Claude Desktop sends some requests on a model of its own choosing rather
|
||||
// than the one its session is on. A session's title (and branch name) is
|
||||
// asked for by one tool-less request whose model is its "small_fast" pick
|
||||
// from the gateway's list — the first id with haiku in it, else sonnet,
|
||||
// else opus (_$n in its app.asar, 2.7032), the session's model only when
|
||||
// none has one — so {"model":"claude-sonnet-5-thinking","max_tokens":200,
|
||||
// "system":"You write short session titles. …"} went to a Claude model the
|
||||
// user never picked. Claude Code in its Code tab asks for its own
|
||||
// claude-haiku-… by name for small tasks too.
|
||||
//
|
||||
// A session's turns carry tools; the model the latest one is for is the one
|
||||
// the user picked. A small tool-less request (a title's max_tokens is 200,
|
||||
// a turn's tens of thousands) for a model whose id reads as Claude's, and
|
||||
// any request for a model magpie doesn't serve, goes to that model instead,
|
||||
// so a chat the user started on a Claude model of their own stays on it. It is kept on disk, so a title asked for before the first
|
||||
// turn after a restart goes there too.
|
||||
var desktopPicked struct {
|
||||
sync.Mutex
|
||||
model string
|
||||
from string // the file it was read from
|
||||
}
|
||||
|
||||
func desktopPickedPath() string { return filepath.Join(settings.Dir(), "claude-desktop.model") }
|
||||
|
||||
// desktopTurn is the model a Claude Desktop request for asked is served by.
|
||||
func desktopTurn(asked string, body []byte) string {
|
||||
if asked == "" {
|
||||
return asked
|
||||
}
|
||||
tools := hasTools(body)
|
||||
desktopPicked.Lock()
|
||||
defer desktopPicked.Unlock()
|
||||
if path := desktopPickedPath(); desktopPicked.from != path {
|
||||
b, _ := os.ReadFile(path)
|
||||
desktopPicked.model, desktopPicked.from = strings.TrimSpace(string(b)), path
|
||||
}
|
||||
picked := desktopPicked.model
|
||||
if asked == picked {
|
||||
return asked
|
||||
}
|
||||
if unserved(asked) || !tools && small(body) && desktopAccepts(asked) {
|
||||
if picked != "" {
|
||||
return picked
|
||||
}
|
||||
return asked
|
||||
}
|
||||
if tools {
|
||||
desktopPicked.model = asked
|
||||
if os.MkdirAll(settings.Dir(), 0o755) == nil {
|
||||
os.WriteFile(desktopPickedPath(), []byte(asked+"\n"), 0o600)
|
||||
}
|
||||
}
|
||||
return asked
|
||||
}
|
||||
|
||||
// small: the request asks for a short answer (max_tokens at most 4096), as
|
||||
// Desktop's title does, not a session's turn.
|
||||
func small(body []byte) bool {
|
||||
var v struct {
|
||||
MaxTokens int `json:"max_tokens"`
|
||||
}
|
||||
return json.Unmarshal(body, &v) == nil && v.MaxTokens > 0 && v.MaxTokens <= 4096
|
||||
}
|
||||
|
||||
// hasTools: the request offers the model tools (Anthropic's, Chat's and
|
||||
// Responses' "tools" alike).
|
||||
func hasTools(body []byte) bool {
|
||||
if !bytes.Contains(body, []byte(`"tools"`)) {
|
||||
return false
|
||||
}
|
||||
var v struct {
|
||||
Tools []json.RawMessage `json:"tools"`
|
||||
}
|
||||
return json.Unmarshal(body, &v) == nil && len(v.Tools) > 0
|
||||
}
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/yetone/magpie/internal/provider"
|
||||
)
|
||||
|
||||
// Claude Desktop's session titles (and Claude Code's small tasks in its
|
||||
// Code tab) go to the model its session is on, not to the Claude model it
|
||||
// picks for them or names by itself (ARNO)
|
||||
func TestClaudeDesktopAuxiliaryModel(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-thinking", "m2"}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
send := func(key, model string, tools bool) string {
|
||||
t.Helper()
|
||||
body := `{"model":"` + model + `","max_tokens":200,"stream":true,"system":"You write short session titles.","messages":[{"role":"user","content":"hi"}]}`
|
||||
if tools {
|
||||
body = `{"model":"` + model + `","max_tokens":32000,"stream":true,"tools":[{"name":"Bash","input_schema":{"type":"object"}}],"messages":[{"role":"user","content":"hi"}]}`
|
||||
}
|
||||
f.got = nil
|
||||
req := httptest.NewRequest("POST", "/v1/messages", strings.NewReader(body))
|
||||
req.Header.Set("x-api-key", key)
|
||||
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)
|
||||
}
|
||||
desktop, other := Token+"-claude-desktop", Token+"-claude"
|
||||
|
||||
// nothing picked yet: as asked
|
||||
if got := send(desktop, "claude-sonnet-5-thinking", false); got != "claude-sonnet-5-thinking" {
|
||||
t.Fatalf("before any turn: sent %q", got)
|
||||
}
|
||||
// a turn (tools) on a non-Claude model, listed to Desktop by its alias
|
||||
if got := send(desktop, aliasFor("fake/m1"), true); got != "m1" {
|
||||
t.Fatalf("turn: sent %q", got)
|
||||
}
|
||||
// the title goes to the session's model
|
||||
if got := send(desktop, "claude-sonnet-5-thinking", false); got != "m1" {
|
||||
t.Fatalf("title: sent %q, want m1", got)
|
||||
}
|
||||
// a chat turn without tools on a Claude model the user picked stays on it
|
||||
f.got = nil
|
||||
chat := httptest.NewRequest("POST", "/v1/messages", strings.NewReader(`{"model":"claude-sonnet-5-thinking","max_tokens":32000,"stream":true,"messages":[{"role":"user","content":"hi"}]}`))
|
||||
chat.Header.Set("x-api-key", desktop)
|
||||
New().Handler().ServeHTTP(httptest.NewRecorder(), chat)
|
||||
if got := modelOf(f.got); got != "claude-sonnet-5-thinking" {
|
||||
t.Fatalf("tool-less chat turn: sent %q", got)
|
||||
}
|
||||
// a Claude model magpie doesn't serve, tools or not, goes there too
|
||||
for _, tools := range []bool{false, true} {
|
||||
if got := send(desktop, "claude-haiku-4-5-20251001", tools); got != "m1" {
|
||||
t.Fatalf("unserved haiku (tools %v): sent %q", tools, got)
|
||||
}
|
||||
}
|
||||
// a tool-less request for a model that doesn't read as Claude's is as asked
|
||||
if got := send(desktop, aliasFor("fake/m2"), false); got != "m2" {
|
||||
t.Fatalf("tool-less m2: sent %q", got)
|
||||
}
|
||||
// another agent's requests are untouched
|
||||
if got := send(other, "claude-sonnet-5-thinking", false); got != "claude-sonnet-5-thinking" {
|
||||
t.Fatalf("Claude Code's request: sent %q", got)
|
||||
}
|
||||
// a session on the Claude model itself keeps it for its title
|
||||
if got := send(desktop, "claude-sonnet-5-thinking", true); got != "claude-sonnet-5-thinking" {
|
||||
t.Fatalf("turn on sonnet: sent %q", got)
|
||||
}
|
||||
if got := send(desktop, "claude-sonnet-5-thinking", false); got != "claude-sonnet-5-thinking" {
|
||||
t.Fatalf("title on a sonnet session: sent %q", got)
|
||||
}
|
||||
|
||||
// kept across a restart
|
||||
send(desktop, "fake/m2", true)
|
||||
if b, _ := os.ReadFile(desktopPickedPath()); strings.TrimSpace(string(b)) != "fake/m2" {
|
||||
t.Fatalf("kept %q", b)
|
||||
}
|
||||
desktopPicked.Lock()
|
||||
desktopPicked.model, desktopPicked.from = "", ""
|
||||
desktopPicked.Unlock()
|
||||
if got := send(desktop, "claude-sonnet-5-thinking", false); got != "m2" {
|
||||
t.Fatalf("title after a restart: sent %q, want m2", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHasTools(t *testing.T) {
|
||||
for body, want := range map[string]bool{
|
||||
`{"model":"x"}`: false,
|
||||
`{"model":"x","tools":[]}`: false,
|
||||
`{"model":"x","tools":[{"name":"a"}]}`: true,
|
||||
`{"system":"no \"tools\" here"}`: false,
|
||||
`{"tools":[{"type":"function"}],"x":1}`: true,
|
||||
} {
|
||||
if hasTools([]byte(body)) != want {
|
||||
t.Errorf("hasTools(%s) = %v", body, !want)
|
||||
}
|
||||
}
|
||||
}
|
||||
+28
-12
@@ -63,20 +63,9 @@ var StandIn func(agent, model string) string
|
||||
// standIn is StandIn's model for one magpie shows no entry for: not a
|
||||
// catalog id or model, nor a ready provider's "provider/model".
|
||||
func standIn(agent, asked string) string {
|
||||
if StandIn == nil || asked == "" || strings.HasPrefix(asked, provider.GroupPrefix) {
|
||||
if StandIn == nil || !unserved(asked) {
|
||||
return ""
|
||||
}
|
||||
id := strings.TrimSuffix(asked, "[1m]")
|
||||
for _, e := range provider.Catalog() {
|
||||
if e.ID == id || e.Model == id {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
if pid, _, ok := strings.Cut(id, "/"); ok {
|
||||
if p, err := provider.Find(pid); err == nil && p.Ready() {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
m := StandIn(agent, asked)
|
||||
if m == asked {
|
||||
return ""
|
||||
@@ -84,6 +73,30 @@ func standIn(agent, asked string) string {
|
||||
return m
|
||||
}
|
||||
|
||||
// unserved: magpie shows no entry for the model — not a catalog id or
|
||||
// model, nor a ready provider's "provider/model" (a routing group's id is
|
||||
// taken as served).
|
||||
func unserved(asked string) bool {
|
||||
if asked == "" || strings.HasPrefix(asked, provider.GroupPrefix) {
|
||||
return false
|
||||
}
|
||||
if _, ok := provider.GroupFor(asked); ok {
|
||||
return false
|
||||
}
|
||||
id := strings.TrimSuffix(asked, "[1m]")
|
||||
for _, e := range provider.Catalog() {
|
||||
if e.ID == id || e.Model == id {
|
||||
return false
|
||||
}
|
||||
}
|
||||
if pid, _, ok := strings.Cut(id, "/"); ok {
|
||||
if p, err := provider.Find(pid); err == nil && p.Ready() {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// Version is set by main.
|
||||
var Version = "dev"
|
||||
|
||||
@@ -617,6 +630,9 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro
|
||||
// the group's, as "group/<id>" is, rather than one provider's that
|
||||
// serves it: the Routing view shows the group it went to
|
||||
asked := call.Model
|
||||
if call.Agent == "claude-desktop" {
|
||||
asked = desktopTurn(asked, body)
|
||||
}
|
||||
if id, ok := provider.GroupFor(asked); ok {
|
||||
asked = id
|
||||
} else if m := standIn(call.Agent, asked); m != "" {
|
||||
|
||||
Reference in New Issue
Block a user