Files
magpie/internal/gateway/omarchy.go
T
yetone 8f385e113b site: usemagpie.ai draws itself in Omarchy's look on Omarchy, in the theme Omarchy has on (owner: 整个官网和文档页面能否智能识别是不是 Omarchy 在访问,如果是 Omarchy 在访问,那么整个页面风格能否变成 Omarchy 风格?而且能够适配当前 Omarchy 就更牛逼了) — every page (home, /import, the docs in en/zh/ja) loads omarchy.js in a Linux desktop browser only; it tells Omarchy by its font (a page may load local("omarchy"), which Omarchy installs system-wide and nothing else has), remembers it, and from then on paints before the page shows: the theme's palette, the system monospace (Omarchy's font), square corners, no shadows or glass, a solid bar, buttons and what's chosen in the accent, images framed in it. Until magpie tells the theme it takes Omarchy's default (tokyo-night, or catppuccin-latte on a light desktop). magpie's gateway answers GET /v1/magpie/omarchy (name, mode, stamp and the CSS variables magpie's own windows use) to this machine only, readable only by usemagpie.ai (CORS + Private Network Access; MAGPIE_SITE_ORIGIN adds an origin to try the site locally), 404 off Omarchy. The page asks it by itself only once the browser allows it to reach this computer; a chip in the corner (Omarchy's logo and the theme) asks once on a click, lists the 22 built-in themes to pick one, and turns the look off; ?omarchy=1|0|<theme> sets it anywhere. Followed while the page shows: a theme set in Omarchy's menu comes through in about 3 s. Tried in the omarchy VM's Chromium 152 with this magpie (nogui serve): detection, the home page and zh docs in tokyo-night, the chip's menu, following the computer, and omarchy-theme-set catppuccin-latte repainting the open page light. TestOmarchyThemeForTheSite covers 404 off Omarchy, the theme and its CORS for usemagpie.ai only, LAN refused and the preflight. go vet, builds (darwin, GOOS=linux, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass. The permission prompt from the public site wasn't clicked through in a real browser.
2026-10-03 16:01:53 +08:00

57 lines
1.9 KiB
Go

package gateway
import (
"net/http"
"os"
"github.com/yetone/magpie/internal/omarchy"
"github.com/yetone/magpie/internal/provider"
)
// siteOrigins are the pages that may read the Omarchy theme: usemagpie.ai,
// which on an Omarchy machine draws itself in that machine's theme
// (site/public/omarchy.js) and asks the magpie running there which one it
// is; MAGPIE_SITE_ORIGIN adds one (the site served locally to try it).
var siteOrigins = map[string]bool{"https://usemagpie.ai": true, "https://www.usemagpie.ai": true}
func siteOrigin(o string) bool {
return siteOrigins[o] || o != "" && o == os.Getenv("MAGPIE_SITE_ORIGIN")
}
// onOmarchy is whether this magpie runs on Omarchy, asked once.
var onOmarchy = omarchy.Detect()
// omarchyTheme is Omarchy's current theme as magpie's own windows draw it
// (name, mode, stamp, CSS variables: palette, font, corners, border), for
// usemagpie.ai's pages on the same machine. It answers this machine only,
// and the browser lets only usemagpie.ai read it; a magpie elsewhere says
// 404.
func (s *Server) omarchyTheme(w http.ResponseWriter, r *http.Request) {
if o := r.Header.Get("Origin"); siteOrigin(o) {
w.Header().Set("Access-Control-Allow-Origin", o)
w.Header().Set("Vary", "Origin")
if r.Header.Get("Access-Control-Request-Private-Network") == "true" {
w.Header().Set("Access-Control-Allow-Private-Network", "true")
}
}
if r.Method == http.MethodOptions {
w.Header().Set("Access-Control-Allow-Methods", "GET")
w.WriteHeader(http.StatusNoContent)
return
}
if !local(r) {
writeError(w, provider.Chat, http.StatusForbidden, "magpie tells its Omarchy theme to this machine only")
return
}
th, ok := omarchy.Theme{}, false
if onOmarchy {
th, ok = omarchy.Current()
}
if !ok {
writeError(w, provider.Chat, http.StatusNotFound, "magpie isn't running on Omarchy")
return
}
w.Header().Set("Cache-Control", "no-store")
writeJSON(w, 200, th)
}