diff --git a/internal/gateway/desktop_probe_test.go b/internal/gateway/desktop_probe_test.go index 398fcdef2..f4d2dc73c 100644 --- a/internal/gateway/desktop_probe_test.go +++ b/internal/gateway/desktop_probe_test.go @@ -158,8 +158,6 @@ func TestOwnCacheKey(t *testing.T) { }{ {provider.Provider{ID: "relay", Chat: "https://relay.example/v1"}, true}, {provider.Provider{ID: "openai", Preset: "openai", Responses: "https://api.openai.com/v1"}, true}, - {provider.Provider{ID: "mistral", Preset: "mistral", Chat: "https://api.mistral.ai/v1"}, true}, - {provider.Provider{ID: "mistral-own", Chat: "https://api.mistral.ai/v1", ExtraBody: provider.ExtraBody{"prompt_cache_key": json.RawMessage(`"k"`)}}, false}, {provider.Provider{ID: "deepseek", Preset: "deepseek", Chat: "https://api.deepseek.com/v1"}, false}, {provider.Provider{ID: "codex", Account: &provider.Account{}, Responses: "https://chatgpt.com/backend-api/codex"}, false}, {provider.Provider{ID: "relay-own", Chat: "https://relay.example/v1", ExtraBody: provider.ExtraBody{"prompt_cache_key": json.RawMessage(`"${sessionId}"`)}}, false}, diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go index 052d51508..a265240cf 100644 --- a/internal/gateway/gateway.go +++ b/internal/gateway/gateway.go @@ -6463,9 +6463,6 @@ func (s *Server) passthrough(w http.ResponseWriter, r *http.Request, p provider. } upstream := provider.UpstreamNameIn(wiresOf(r.Context()), p.ID, model) body = rewriteModel(body, upstream) - if proto == provider.Chat { - body = withMistralCacheKey(p, r.Header, body) - } if proto == provider.Responses { // Claude's blocks magpie gave a Responses client go back to Claude // alone; to this API the item is its summary, as before @@ -8569,9 +8566,7 @@ const cacheKeyField = "prompt_cache_key" // client's never has one): OpenAI's API routes a conversation by it to // where its prompt is cached, and a relay in front of it can refuse a // request without it ("prompt_cache_key is required", CyberCaius's -// AnyRouter on Discord). Only OpenAI's own API with a key, Mistral's -// (its prompt_cache_key keeps a conversation on the server that holds its -// cache; daoche_wuzui on X), and a provider +// AnyRouter on Discord). Only OpenAI's own API with a key, and a provider // the user added by its address, which may be such a relay: a vendor's // preset, a signed-in account (the ChatGPT backend gets its own, as Codex // CLI sends it), a plugin, Bedrock and Vertex are sent nothing they didn't @@ -8589,49 +8584,13 @@ func ownCacheKey(p provider.Provider) bool { if _, ok := p.ExtraBody["!"+cacheKeyField]; ok { return false } - if p.AddedByAddress() || isMistral(p) { + if p.AddedByAddress() { return true } h := p.VendorHost() return h == "api.openai.com" || strings.HasSuffix(h, ".api.openai.com") } -func isMistral(p provider.Provider) bool { - return p.Preset == "mistral" || p.Host() == "api.mistral.ai" -} - -// withMistralCacheKey gives a Chat body bound for Mistral the -// prompt_cache_key Mistral routes a conversation by to where its prompt is -// cached (daoche_wuzui on X: poor cache hits). The key is a hash of the -// session or conversation, never the id itself, and a key the client sent, -// or the user's extraBody, is the one kept. A body with no conversation to -// name gets none. -func withMistralCacheKey(p provider.Provider, in http.Header, body []byte) []byte { - if !isMistral(p) || !ownCacheKey(p) || gjson.GetBytes(body, cacheKeyField).Exists() { - return body - } - id := cmp.Or(sessionOf(in), conversationID(in, body)) - if id == "" { - return body - } - sum := sha256.Sum256([]byte("mistral\x00" + id)) - // written in ahead of the first member, so the client's own bytes - // (and the order its messages were sent in, for the cache) stay as sent - i := bytes.IndexByte(body, '{') - if i < 0 || len(bytes.TrimSpace(body[:i])) > 0 { - return body - } - rest := bytes.TrimLeft(body[i+1:], " \t\r\n") - sep := "," - if len(rest) == 0 || rest[0] == '}' { - sep = "" - } - out := make([]byte, 0, len(body)+96) - out = append(out, body[:i+1]...) - out = append(out, `"`+cacheKeyField+`":"`+hex.EncodeToString(sum[:])+`"`+sep...) - return append(out, body[i+1:]...) -} - // cacheKeyOf is the prompt cache key of the conversation req is a turn // of: the session the client names (sessionOf), else its first user // message, which every later turn repeats, hashed into a UUID's shape, so diff --git a/internal/gateway/mistral_cache_key_test.go b/internal/gateway/mistral_cache_key_test.go deleted file mode 100644 index 1a8d57d7a..000000000 --- a/internal/gateway/mistral_cache_key_test.go +++ /dev/null @@ -1,50 +0,0 @@ -package gateway - -import ( - "strings" - "testing" - - "github.com/tidwall/gjson" - "github.com/yetone/magpie/internal/provider" -) - -// Mistral reads prompt_cache_key to keep a conversation on the server that -// holds its cache (daoche_wuzui on X). A turn without one gets an opaque -// hash of its conversation; a client's own key and other vendors are left -// as they were. -func TestMistralPromptCacheKey(t *testing.T) { - const msg = `{"model":"m1","messages":[{"role":"user","content":"hello there"}]}` - run := func(preset, body, session string) string { - f := &fake{t: t, reply: deepseekReply} - up := setup(t, provider.Chat, f) - p, _ := provider.Find("fake") - p.Preset, p.Chat = preset, up.URL+"/v1" - if err := provider.Save(*p); err != nil { - t.Fatal(err) - } - h := map[string]string{} - if session != "" { - h["X-Session-Id"] = session - } - rec, _ := postWith(t, "/v1/chat/completions", body, h) - if rec != 200 { - t.Fatalf("status %d", rec) - } - return string(f.got) - } - a := gjson.Get(run("mistral", msg, "sess-secret-1"), "prompt_cache_key").String() - b := gjson.Get(run("mistral", msg, "sess-secret-1"), "prompt_cache_key").String() - c := gjson.Get(run("mistral", msg, "sess-secret-2"), "prompt_cache_key").String() - if a == "" || a != b || a == c { - t.Errorf("keys %q %q %q: want the same for one session, another for another", a, b, c) - } - if strings.Contains(a, "sess-secret") { - t.Errorf("key %q holds the session id", a) - } - if got := gjson.Get(run("mistral", strings.Replace(msg, `"messages"`, `"prompt_cache_key":"mine","messages"`, 1), "s"), "prompt_cache_key").String(); got != "mine" { - t.Errorf("client key became %q", got) - } - if got := run("deepseek", msg, "s"); strings.Contains(got, "prompt_cache_key") { - t.Errorf("another vendor got a key: %s", got) - } -} diff --git a/internal/gateway/mistral_extra_fields_test.go b/internal/gateway/mistral_extra_fields_test.go index b804d6f3d..3924e847c 100644 --- a/internal/gateway/mistral_extra_fields_test.go +++ b/internal/gateway/mistral_extra_fields_test.go @@ -14,7 +14,7 @@ import ( // field it doesn't know: 422 extra_forbidden, every such field listed. func mistralRefuse(body []byte) (int, string) { known := []string{"model", "messages", "stream", "stream_options", "max_tokens", "temperature", "top_p", "tools", "tool_choice", - "parallel_tool_calls", "response_format", "stop", "random_seed", "presence_penalty", "frequency_penalty", "n", "reasoning_effort", "prompt_cache_key"} + "parallel_tool_calls", "response_format", "stop", "random_seed", "presence_penalty", "frequency_penalty", "n", "reasoning_effort"} var m map[string]json.RawMessage json.Unmarshal(body, &m) var detail []string diff --git a/internal/gui/app.go b/internal/gui/app.go index 08dced406..e7ad58e69 100644 --- a/internal/gui/app.go +++ b/internal/gui/app.go @@ -707,7 +707,13 @@ func Run(version string, showMain bool, link string) error { h.main.OnWindowEvent(events.Windows.WebViewNavigationCompleted, func(*application.WindowEvent) { markReady() }) } else { h.app.Event.OnApplicationEvent(events.Common.ApplicationStarted, func(*application.ApplicationEvent) { - plainTitlebar(h.main) // Linux: the page's header is the title bar; the Mac: a plain one's corners + if runtime.GOOS != "darwin" { + plainTitlebar(h.main) // Linux: the page's header is the title bar + } + // The Mac's is done as the window is first shown (reveal): it + // makes a window of its own (toolbarRounder), which deadlocks the + // main thread while a hidden background launch (an update's + // relaunch) is still inside FBSWorkspaceScenesClient on macOS 26. nameWindow(h.panel, panelTitle) ownFrame(h.panel) // KDE: no title bar of KWin's on it (#1283) markReady() diff --git a/internal/gui/lightweight.go b/internal/gui/lightweight.go index b4f9aff01..d139339b4 100644 --- a/internal/gui/lightweight.go +++ b/internal/gui/lightweight.go @@ -311,6 +311,9 @@ func (h *host) openMain(url string) { // can do that without showing it first (showsMinimised); elsewhere it is // shown quietly, as any window a restart by itself brings back. func (h *host) reveal(w *application.WebviewWindow, quiet, minimised bool) { + if runtime.GOOS == "darwin" { + plainTitlebar(w) // the Mac's, not shown yet; not at start (app.go) + } if minimised && showsMinimised { showMinimised(w, h.placeAt(w)) return diff --git a/internal/gui/titlebar_start_test.go b/internal/gui/titlebar_start_test.go new file mode 100644 index 000000000..5d3337ab2 --- /dev/null +++ b/internal/gui/titlebar_start_test.go @@ -0,0 +1,40 @@ +//go:build !nogui + +package gui + +import ( + "os" + "strings" + "testing" +) + +// The Mac's plain title bar makes a window of its own (toolbarRounder). Done +// in ApplicationStarted, it deadlocked the main thread of an update's hidden +// relaunch on macOS 26, which is still inside FBSWorkspaceScenesClient then +// (eating_dream on X). It is done as the main window is first shown instead. +func TestMacTitleBarNotDoneAtStart(t *testing.T) { + app, err := os.ReadFile("app.go") + if err != nil { + t.Fatal(err) + } + i := strings.LastIndex(string(app), "events.Common.ApplicationStarted, func(") + if i < 0 { + t.Fatal("no ApplicationStarted handler in app.go") + } + body := string(app)[i:] + body = body[:strings.Index(body, "markReady()")] + if strings.Contains(strings.ReplaceAll(body, `if runtime.GOOS != "darwin" { + plainTitlebar(h.main)`, ""), "plainTitlebar(") { + t.Error("ApplicationStarted calls plainTitlebar on the Mac") + } + lw, err := os.ReadFile("lightweight.go") + if err != nil { + t.Fatal(err) + } + r := string(lw) + r = r[strings.Index(r, "func (h *host) reveal("):] + r = r[:strings.Index(r, "\n}\n")] + if !strings.Contains(r, "plainTitlebar(w)") { + t.Error("reveal doesn't give the Mac's main window its plain title bar") + } +}