Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.
icons: a big picture is sent to the window scaled to 256px (#909, #1956, #2459)
yetone88ac42843dbc
diff --git a/go.mod b/go.modindex 20b2c58f0..2a94a1d0c 100644--- a/go.mod+++ b/go.mod@@ -16,6 +16,7 @@ require (github.com/tidwall/gjson v1.19.0github.com/tidwall/jsonc v0.3.3github.com/wailsapp/wails/v3 v3.0.0-beta.24+ golang.org/x/image v0.43.0golang.org/x/net v0.56.0golang.org/x/sys v0.46.0golang.org/x/term v0.44.0diff --git a/go.sum b/go.sumindex a4ed75dfb..81a58133a 100644--- a/go.sum+++ b/go.sum@@ -96,6 +96,8 @@ go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw=go.yaml.in/yaml/v3 v3.0.5/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg=golang.org/x/exp v0.0.0-20260410095643-746e56fc9e2f h1:W3F4c+6OLc6H2lb//N1q4WpJkhzJCK5J6kUi1NTVXfM=golang.org/x/exp v0.0.0-20260410095643-746e56fc9e2f/go.mod h1:J1xhfL/vlindoeF/aINzNzt2Bket5bjo9sdOYzOsU80=+golang.org/x/image v0.43.0 h1:FLxcP4ec2350nTfOC8ysKtqYSIFbk/QGjw1ZHNP4tsY=+golang.org/x/image v0.43.0/go.mod h1:rrpelvGFt+kLPAjPM4HeWPgrl0FtafueU//e5N0qk/Q=golang.org/x/mod v0.37.0 h1:vF1DjpVEshcIqoEaauuHebaLk1O1forxjxBaVn884JQ=golang.org/x/mod v0.37.0/go.mod h1:m8S8VeM9r4dzDwjrKO0a1sZP3YjeMamRRlD+fmR2Q/0=golang.org/x/net v0.56.0 h1:Rw8j/hFzGvJUZwNBXnAtf5sVDVt+65SK2C7IxCxZt5o=diff --git a/internal/gui/providers.go b/internal/gui/providers.goindex af49b1275..7e8dc0337 100644--- a/internal/gui/providers.go+++ b/internal/gui/providers.go@@ -1137,11 +1137,15 @@ func providerRoutes(mux *http.ServeMux, w Windows) {writeJSON(rw, map[string]string{"icon": icon})})mux.HandleFunc("GET /api/icons/{name}", func(rw http.ResponseWriter, r *http.Request) {- f := provider.IconFile(r.PathValue("name"))+ // a big picture comes scaled to what the window draws (#909)+ f, ctype := provider.IconServed(r.PathValue("name"))if f == "" {http.NotFound(rw, r)return}+ if ctype != "" {+ rw.Header().Set("Content-Type", ctype)+ }// an SVG is only ever drawn as an image, never runrw.Header().Set("Content-Security-Policy", "default-src 'none'; style-src 'unsafe-inline'")rw.Header().Set("Cache-Control", "max-age=31536000, immutable")diff --git a/internal/provider/icon.go b/internal/provider/icon.goindex dd2aea8f2..66b755396 100644--- a/internal/provider/icon.go+++ b/internal/provider/icon.go@@ -302,4 +302,5 @@ func pruneIcons(f file) {os.Remove(filepath.Join(IconDir(), e.Name()))}}+ pruneSmallIcons()}diff --git a/internal/provider/icon_small.go b/internal/provider/icon_small.gonew file mode 100644index 000000000..8c0acc5db--- /dev/null+++ b/internal/provider/icon_small.go@@ -0,0 +1,125 @@+package provider++import (+ "bytes"+ "image"+ _ "image/jpeg"+ "image/png"+ "os"+ "path/filepath"+ "strings"+ "sync"++ xdraw "golang.org/x/image/draw"+ _ "golang.org/x/image/webp"++ "github.com/yetone/magpie/internal/appdir"+)++// iconSide is the longest side, in pixels, of a picture the window is sent.+// The window draws an icon at 20 to 64 points, so 256 is sharp at a 250%+// display; a webview keeps every picture on the page decoded at its own+// size, and Plugins' 1254px icons cost 6 MB each (#909, #1956, #2459).+const iconSide = 256++// iconBigPixels is the most pixels a picture is decoded for scaling: past+// it (a small file can hold a huge PNG) the picture is sent as it is.+const iconBigPixels = 64 << 20++var smallIconMu sync.Mutex++// smallIconDir is where the scaled copies are kept: a cache, made again+// from the icons when it is gone.+func smallIconDir() string { return filepath.Join(appdir.Cache(), "icons-small") }++// IconServed is the file to send for a stored picture and its type, ""+// to go by its name; path is "" for a name IconFile refuses. A PNG, JPEG+// or WebP longer than iconSide on a side is sent as a PNG copy scaled+// down to it, made on its first ask and kept; one that can't be read or+// scaled is sent as it is.+func IconServed(name string) (path, ctype string) {+ f := IconFile(name)+ if f == "" {+ return "", ""+ }+ switch filepath.Ext(name) {+ case ".png", ".jpg", ".webp":+ default:+ return f, ""+ }+ small := filepath.Join(smallIconDir(), name+".png")+ if _, err := os.Stat(small); err == nil {+ return small, "image/png"+ }+ smallIconMu.Lock()+ defer smallIconMu.Unlock()+ if _, err := os.Stat(small); err == nil {+ return small, "image/png"+ }+ b, err := scaledIcon(f)+ if err != nil || b == nil {+ return f, ""+ }+ if err := os.MkdirAll(smallIconDir(), 0o755); err != nil {+ return f, ""+ }+ tmp, err := os.CreateTemp(smallIconDir(), ".small-*")+ if err != nil {+ return f, ""+ }+ _, werr := tmp.Write(b)+ if cerr := tmp.Close(); werr == nil {+ werr = cerr+ }+ if werr != nil || os.Rename(tmp.Name(), small) != nil {+ os.Remove(tmp.Name())+ return f, ""+ }+ return small, "image/png"+}++// scaledIcon is the picture at f scaled to iconSide as a PNG, nil when it+// is that small already or too big to decode.+func scaledIcon(f string) ([]byte, error) {+ data, err := os.ReadFile(f)+ if err != nil {+ return nil, err+ }+ cfg, _, err := image.DecodeConfig(bytes.NewReader(data))+ if err != nil {+ return nil, err+ }+ w, h := cfg.Width, cfg.Height+ if max(w, h) <= iconSide || w*h > iconBigPixels {+ return nil, nil+ }+ src, _, err := image.Decode(bytes.NewReader(data))+ if err != nil {+ return nil, err+ }+ sw, sh := iconSide, max(1, h*iconSide/w)+ if h > w {+ sw, sh = max(1, w*iconSide/h), iconSide+ }+ dst := image.NewNRGBA(image.Rect(0, 0, sw, sh))+ xdraw.CatmullRom.Scale(dst, dst.Bounds(), src, src.Bounds(), xdraw.Src, nil)+ var out bytes.Buffer+ if err := png.Encode(&out, dst); err != nil {+ return nil, err+ }+ return out.Bytes(), nil+}++// pruneSmallIcons drops the scaled copies of pictures no longer kept.+func pruneSmallIcons() {+ ents, _ := os.ReadDir(smallIconDir())+ for _, e := range ents {+ name, ok := strings.CutSuffix(e.Name(), ".png")+ if !ok || !iconName.MatchString(name) {+ continue+ }+ if _, err := os.Stat(filepath.Join(IconDir(), name)); os.IsNotExist(err) {+ os.Remove(filepath.Join(smallIconDir(), e.Name()))+ }+ }+}diff --git a/internal/provider/icon_small_test.go b/internal/provider/icon_small_test.gonew file mode 100644index 000000000..12ce9c992--- /dev/null+++ b/internal/provider/icon_small_test.go@@ -0,0 +1,94 @@+package provider++import (+ "bytes"+ "image"+ "image/color"+ "image/png"+ "os"+ "path/filepath"+ "strings"+ "testing"+)++func iconHome(t *testing.T) {+ home := t.TempDir()+ t.Setenv("HOME", home)+ t.Setenv("USERPROFILE", home)+ t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))+ t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))+ t.Setenv("LOCALAPPDATA", filepath.Join(home, "AppData", "Local"))+}++func pngOf(t *testing.T, w, h int) []byte {+ m := image.NewNRGBA(image.Rect(0, 0, w, h))+ for y := range h {+ for x := range w {+ m.Set(x, y, color.NRGBA{uint8(x), uint8(y), 200, 255})+ }+ }+ var b bytes.Buffer+ if err := png.Encode(&b, m); err != nil {+ t.Fatal(err)+ }+ return b.Bytes()+}++func storedName(t *testing.T, data []byte) string {+ ref, err := StoreIcon(data)+ if err != nil {+ t.Fatal(err)+ }+ return strings.TrimPrefix(ref, "file:")+}++// a webview keeps a picture decoded at its own size: a 1254px icon drawn+// at 20 points held 6 MB in the Plugins page's renderer (#909)+func TestBigIconServedScaled(t *testing.T) {+ iconHome(t)+ name := storedName(t, pngOf(t, 1254, 600))+ f, ctype := IconServed(name)+ if ctype != "image/png" || f == IconFile(name) {+ t.Fatalf("IconServed = %q, %q: want a scaled copy", f, ctype)+ }+ b, err := os.ReadFile(f)+ if err != nil {+ t.Fatal(err)+ }+ cfg, err := png.DecodeConfig(bytes.NewReader(b))+ if err != nil {+ t.Fatal(err)+ }+ if cfg.Width != iconSide || cfg.Height != 600*iconSide/1254 {+ t.Errorf("scaled to %dx%d, want %dx%d", cfg.Width, cfg.Height, iconSide, 600*iconSide/1254)+ }+ if again, _ := IconServed(name); again != f {+ t.Errorf("second ask = %q, want the kept copy %q", again, f)+ }+ // the original is gone: its copy goes with it+ os.Remove(IconFile(name))+ pruneSmallIcons()+ if _, err := os.Stat(f); !os.IsNotExist(err) {+ t.Errorf("copy of a dropped icon kept: %v", err)+ }+}++func TestSmallIconServedAsItIs(t *testing.T) {+ iconHome(t)+ small := storedName(t, pngOf(t, 64, 64))+ if f, ctype := IconServed(small); f != IconFile(small) || ctype != "" {+ t.Errorf("64px icon = %q, %q: want the file itself", f, ctype)+ }+ svg := storedName(t, []byte(`<?xml version="1.0"?><svg xmlns="http://www.w3.org/2000/svg" width="1024" height="1024"/>`))+ if f, ctype := IconServed(svg); f != IconFile(svg) || ctype != "" {+ t.Errorf("svg = %q, %q: want the file itself", f, ctype)+ }+ // a file that says PNG and doesn't decode is still sent+ bad := storedName(t, []byte("\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR"))+ if f, _ := IconServed(bad); f != IconFile(bad) {+ t.Errorf("undecodable png = %q, want the file itself", f)+ }+ if f, _ := IconServed("../providers.json"); f != "" {+ t.Errorf("a name outside the folder = %q", f)+ }+}