diff --git a/internal/gui/assets/app.css b/internal/gui/assets/app.css index 25ceee610..fb4c27c4d 100644 --- a/internal/gui/assets/app.css +++ b/internal/gui/assets/app.css @@ -3411,6 +3411,15 @@ body.window .ag-sk-icon { width: 22px; height: 22px; border-radius: 6px; } .sheet .kind { display: flex; align-items: baseline; gap: 8px; padding: 22px 8px 6px; font-size: 12px; } .sheet .kind b { font-size: 13px; font-weight: 600; color: var(--fg); } .sheet .kind span { color: var(--faint); } +/* Partners' heading row: the heading, and at its end the way to join them */ +.sheet .kind-row { display: flex; flex-wrap: wrap; align-items: baseline; justify-content: space-between; column-gap: 12px; } +.sheet .sponsor-me { display: inline-flex; align-items: center; gap: 5px; height: 22px; margin: 0 8px 0 auto; padding: 0 9px 0 8px; border: 1px solid var(--line); border-radius: 11px; background: var(--card); color: var(--fg-2); font: inherit; font-size: 12px; line-height: 1; white-space: nowrap; cursor: pointer; transition: color .15s ease, border-color .15s ease, transform .12s cubic-bezier(.23, 1, .32, 1); } +.sheet .sponsor-me svg { flex: none; color: var(--accent); } +.sheet .sponsor-me:active { transform: scale(.97); } +.sheet .sponsor-me:focus-visible { outline: none; border-color: var(--accent); color: var(--fg); } +@media (hover: hover) and (pointer: fine) { + .sheet .sponsor-me:hover { color: var(--fg); border-color: color-mix(in srgb, var(--accent) 45%, var(--line)); } +} /* Partners' light (shinePartners): a hairline runs once round the heading and, together, round the partners' tiles below it, each over a breath of the accent, then fades; nothing with reduced motion. The two outlines diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js index b2d3cd2eb..4bd4b665f 100644 --- a/internal/gui/assets/app.js +++ b/internal/gui/assets/app.js @@ -288,6 +288,9 @@ const CHEV_R = "m6.5 4.5 3 3.5-3 3.5"; const CHECK = "m3.5 8.5 3 3 6-7"; const CROSS = "M4.5 4.5l7 7M11.5 4.5l-7 7"; const PLUS = "M8 3.5v9M3.5 8h9"; +const HEART = "M8 13.2S2.5 9.9 2.5 6.1A2.8 2.8 0 0 1 8 5a2.8 2.8 0 0 1 5.5 1.1C13.5 9.9 8 13.2 8 13.2Z"; +// internal/gui/providers.go's sponsorMail, the one mailto: /api/open opens +const SPONSOR_MAIL = "mailto:yetoneful@gmail.com?subject=Sponsoring%20magpie"; const OUT = "M6.5 3.5h-3v9h9v-3M9 3.5h3.5V7M12.5 3.5 7.5 8.5"; const PLAY_ICON = "M5.5 3.75v8.5L12.25 8z"; const COPY_ICON = "M5.5 5.5V3.5h7v7h-2M3.5 5.5h7v7h-7z"; @@ -7193,7 +7196,17 @@ function renderAdd() { if (partners.length) { any = true; const grid = section("Partners", "magpie's sponsors"); - shinePartners(grid.previousElementSibling, grid); + const k = grid.previousElementSibling; + // the heading's row ends in the way to join them, the address + // usemagpie.ai's own "Become a sponsor" writes to + const head = el("div", "kind-row"); + const join = el("button", "sponsor-me"); + join.type = "button"; + join.append(svg(HEART, 12, 1.4), el("span", "", t("Become a sponsor"))); + join.onclick = () => (web ? (location.href = SPONSOR_MAIL) : api("open", { url: SPONSOR_MAIL })); + k.replaceWith(head); + head.append(k, join); + shinePartners(k, grid); // a new one's tile keeps a dot until opened (partnerTile) newPartnerTiles ??= new Set(); for (const p of partners) if (p.new && !p.added) newPartnerTiles.add(p.id); diff --git a/internal/gui/assets/i18n.js b/internal/gui/assets/i18n.js index a7fa8a06a..a09e4caa8 100644 --- a/internal/gui/assets/i18n.js +++ b/internal/gui/assets/i18n.js @@ -1990,6 +1990,7 @@ const I18N = { "sign in, no key": "登录即可,无需密钥", "Partners": "合作伙伴", "magpie's sponsors": "赞助 magpie 的服务", + "Become a sponsor": "我要赞助", "New in Partners: {names}": "合作伙伴有新成员:{names}", "New": "新", "Relays": "中转", @@ -7129,6 +7130,7 @@ const I18N = { "sign in, no key": "登入即可,無需金鑰", "Partners": "合作夥伴", "magpie's sponsors": "贊助 magpie 的服務", + "Become a sponsor": "我要贊助", "New in Partners: {names}": "合作夥伴有新成員:{names}", "New": "新", "Relays": "中轉", @@ -12242,6 +12244,7 @@ const I18N = { "sign in, no key": "サインインのみ、キー不要", "Partners": "パートナー", "magpie's sponsors": "magpie のスポンサー", + "Become a sponsor": "スポンサーになる", "New in Partners: {names}": "新しいパートナー:{names}", "New": "新着", "Relays": "中継サービス", @@ -17386,6 +17389,7 @@ const I18N = { "sign in, no key": "anmelden, kein Schlüssel", "Partners": "Partner", "magpie's sponsors": "Sponsoren von magpie", + "Become a sponsor": "Sponsor werden", "New in Partners: {names}": "Neu bei den Partnern: {names}", "New": "Neu", "Relays": "Relays", diff --git a/internal/gui/providers.go b/internal/gui/providers.go index fe3362f9e..f1743c726 100644 --- a/internal/gui/providers.go +++ b/internal/gui/providers.go @@ -1506,6 +1506,11 @@ func moveContext(r *http.Request) (context.Context, context.CancelFunc) { return context.WithTimeout(context.WithoutCancel(r.Context()), 10*time.Minute) } +// sponsorMail is where the Add sheet's "Become a sponsor" writes to, the +// same address and subject as usemagpie.ai's; /api/open opens this one +// mailto: link and no other +const sponsorMail = "mailto:yetoneful@gmail.com?subject=Sponsoring%20magpie" + // appLink hands an app's import link over once something here opens it // (agent.OpenAppLink); tests stand in for the system's answer. var appLink = agent.OpenAppLink @@ -3476,8 +3481,9 @@ func providerRoutes(mux *http.ServeMux, w Windows) { var in struct{ URL string } _ = json.NewDecoder(r.Body).Decode(&in) // and System Settings' Login Items, where Open at login is allowed - // again: that one pane, not System Settings' scheme at large - if strings.HasPrefix(in.URL, "https://") || strings.HasPrefix(in.URL, "http://") || strings.HasPrefix(in.URL, agent.CindyScheme) || strings.HasPrefix(in.URL, agent.LodyScheme) || strings.HasPrefix(in.URL, agent.AionUiScheme) || strings.HasPrefix(in.URL, agent.CherryStudioScheme) || in.URL == autostart.LoginItemsURL { + // again: that one pane, not System Settings' scheme at large; and + // the Add sheet's "Become a sponsor", that one address, not mailto: + if in.URL == sponsorMail || strings.HasPrefix(in.URL, "https://") || strings.HasPrefix(in.URL, "http://") || strings.HasPrefix(in.URL, agent.CindyScheme) || strings.HasPrefix(in.URL, agent.LodyScheme) || strings.HasPrefix(in.URL, agent.AionUiScheme) || strings.HasPrefix(in.URL, agent.CherryStudioScheme) || in.URL == autostart.LoginItemsURL { open := w.OpenURL // an app's import link is handed over only when something // here opens it: the system says nothing when nothing does, diff --git a/internal/gui/sponsor_mail_test.go b/internal/gui/sponsor_mail_test.go new file mode 100644 index 000000000..69fe8d49b --- /dev/null +++ b/internal/gui/sponsor_mail_test.go @@ -0,0 +1,50 @@ +package gui + +import ( + "encoding/json" + "net/http" + "net/http/httptest" + "os" + "strings" + "testing" +) + +// The Add sheet's "Become a sponsor" opens a mail to the owner, the same +// one usemagpie.ai's link writes; /api/open opens that one mailto: link, +// not any address a page names. +func TestSponsorMailOpensThatAddressOnly(t *testing.T) { + sandboxHome(t) + w := &importWindows{} + mux := http.NewServeMux() + providerRoutes(mux, w) + for _, link := range []string{ + sponsorMail, + "mailto:someone@example.com", + "mailto:yetoneful@gmail.com", + sponsorMail + "&body=hello", + "MAILTO:yetoneful@gmail.com?subject=Sponsoring%20magpie", + } { + w.opened = "" + body, _ := json.Marshal(map[string]string{"url": link}) + mux.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest("POST", "/api/open", strings.NewReader(string(body)))) + if want := link == sponsorMail; (w.opened == link) != want { + t.Errorf("%s: opened %q", link, w.opened) + } + } + + // the page asks for the same link the server lets through + app, err := os.ReadFile("assets/app.js") + if err != nil { + t.Fatal(err) + } + if !strings.Contains(string(app), `"`+sponsorMail+`"`) { + t.Errorf("app.js doesn't open %s", sponsorMail) + } + site, err := os.ReadFile("../../site/public/index.html") + if err != nil { + t.Fatal(err) + } + if !strings.Contains(string(site), `href="`+sponsorMail+`"`) { + t.Errorf("usemagpie.ai's sponsor link is no longer %s", sponsorMail) + } +} diff --git a/internal/gui/tests/add-partners-shine.test.cjs b/internal/gui/tests/add-partners-shine.test.cjs index 87fdf4332..cc9511925 100644 --- a/internal/gui/tests/add-partners-shine.test.cjs +++ b/internal/gui/tests/add-partners-shine.test.cjs @@ -70,7 +70,7 @@ const block = (page) => page.locator("#addSheet .kind").first().evaluate(async ( .filter((a) => !/^shine-/.test(a.animationName) && a.effect?.getComputedTiming().iterations !== Infinity) .map((a) => a.finished.catch(() => {}))); const r = (e) => e.getBoundingClientRect(); - const grid = k.nextElementSibling, kr = k.querySelector("i.ring"), gr = grid.querySelector(":scope > i.ring"); + const grid = (k.closest(".kind-row") || k).nextElementSibling, kr = k.querySelector("i.ring"), gr = grid.querySelector(":scope > i.ring"); const g = r(grid), a = kr && r(kr), b = gr && r(gr); // the heading's words, not its box: a lit heading is as wide as its words // each from the sheet's corner, which may still be sliding in diff --git a/internal/gui/tests/add-sponsor-me.test.cjs b/internal/gui/tests/add-sponsor-me.test.cjs new file mode 100644 index 000000000..7f31b5fc2 --- /dev/null +++ b/internal/gui/tests/add-sponsor-me.test.cjs @@ -0,0 +1,104 @@ +// Run with Node's test runner and Playwright on the module path; see README.md. +// The Add sheet's Partners heading ends in "Become a sponsor" (yetone: 在右边 +// 加上一个优雅精致的我要赞助的按钮): on the heading's own line, at the sheet's +// right, clear of the heading's light, and a click asks magpie to open the +// mail usemagpie.ai's "Become a sponsor" writes, that one link. +// English, Chinese, Japanese and German, at 900px and 440px; no backend. +const assert = require("node:assert/strict"); +const fs = require("node:fs/promises"); +const path = require("node:path"); +const { test } = require("node:test"); +const { chromium, webkit } = require("playwright"); + +const assets = path.resolve(__dirname, "../assets"); +const MAIL = "mailto:yetoneful@gmail.com?subject=Sponsoring%20magpie"; +const partners = [ + { id: "acme", name: "Acme AI", icon: "generic", kind: "partner", sponsored: true, chat: "https://api.acme.example.com/v1" }, + { id: "beta", name: "Beta Relay", icon: "generic", kind: "partner", sponsored: true, chat: "https://api.beta.example.com/v1" }, +]; +const presets = [{ id: "openai", name: "OpenAI", icon: "openai", kind: "vendor", chat: "https://api.openai.example.com/v1" }]; +const providers = [{ id: "openai", name: "OpenAI", icon: "openai", preset: "openai", models: [], agents: [], key: { set: true, masked: "sk-…ab12" } }]; + +function server(lang, opened, withPartners = true) { + return async (route) => { + const req = route.request(); + const url = new URL(req.url()); + const json = (data) => route.fulfill({ json: data }); + if (url.pathname === "/api/open") { opened.push(JSON.parse(req.postData() || "{}").url); return route.fulfill({ status: 204, body: "" }); } + if (url.pathname === "/api/partner") return route.fulfill({ status: 204, body: "" }); + if (url.pathname === "/boot.js") return route.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = ${JSON.stringify({ lang, theme: "light", web: false })};` }); + if (url.pathname === "/wails/runtime.js") return route.fulfill({ contentType: "text/javascript", body: "export const Window = {};" }); + if (url.pathname === "/api/state") return json({ agents: [], profiles: [], settings: { lang, theme: "light" } }); + if (url.pathname === "/api/providers") return json({ providers, presets: [...(withPartners ? partners : []), ...presets], excluded: [], gateway: { running: true, window: true } }); + if (url.pathname === "/api/groups") return json({ groups: [], pools: [] }); + if (url.pathname === "/api/gateway/trace") return json({ routes: [] }); + if (url.pathname.startsWith("/api/")) return json({}); + const file = path.join(assets, url.pathname === "/" ? "index.html" : url.pathname); + const contentType = { ".html": "text/html", ".js": "text/javascript", ".css": "text/css", ".svg": "image/svg+xml", ".png": "image/png" }[path.extname(file)]; + try { await route.fulfill({ body: await fs.readFile(file), contentType }); } catch { await route.fulfill({ status: 404, body: "" }); } + }; +} + +const L = { en: "Become a sponsor", zh: "我要赞助", ja: "スポンサーになる", de: "Sponsor werden" }; + +// where the button sits against the heading's words, its light and the sheet +const where = (page) => page.locator("#addSheet .sponsor-me").evaluate((b) => { + const r = (e) => e.getBoundingClientRect(); + const row = b.closest(".kind-row"), k = row.querySelector(".kind"), sheet = r(b.closest("#addSheet")); + const me = r(b), words = r(k.querySelector("b")), ring = k.querySelector("i.ring"); + const o = ring ? r(ring) : r(k); + return { + text: b.textContent, icon: !!b.querySelector("svg"), + oneLine: me.top < words.bottom && me.bottom > words.top, + right: Math.round(sheet.right - me.right), inSheet: me.left >= sheet.left && me.right <= sheet.right, + clear: me.left >= o.right, + gridNext: row.nextElementSibling?.classList.contains("grid"), + }; +}); + +for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) { + test(engine + ": Partners' heading ends in Become a sponsor", async (t) => { + assert(["chromium", "webkit"].includes(engine), "BROWSER must be chromium or webkit"); + const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" })); + t.after(() => browser.close()); + for (const lang of Object.keys(L)) for (const width of [900, 440]) { + await t.test(lang + " " + width, async () => { + const page = await (await browser.newContext({ viewport: { width, height: 800 } })).newPage(); + const errors = [], opened = []; + page.on("pageerror", (e) => errors.push(e.message)); + await page.route("**/*", server(lang, opened)); + await page.goto("http://magpie.test/?view=providers"); + await page.locator("#addProvider").click(); + await page.locator("#addSheet .kind.lit").first().waitFor(); + + assert.equal(await page.locator("#addSheet .sponsor-me").count(), 1); + const w = await where(page); + assert.equal(w.text, L[lang]); + assert(w.icon); + assert(w.oneLine, "on the heading's line"); + assert(w.inSheet, "in the sheet"); + assert(w.right <= 32, "at the sheet's right: " + w.right + "px off"); + assert(w.clear, "clear of the heading's light"); + assert(w.gridNext, "the partners' tiles follow the heading's row"); + + const asked = page.waitForResponse((r) => new URL(r.url()).pathname === "/api/open"); + await page.locator("#addSheet .sponsor-me").click(); + await asked; + assert.deepEqual(opened, [MAIL]); + // the sheet stays open + assert(await page.locator("#addSheet").isVisible()); + assert.deepEqual(errors, []); + await page.context().close(); + }); + } + await t.test("no partners, no button", async () => { + const page = await (await browser.newContext({ viewport: { width: 900, height: 800 } })).newPage(); + await page.route("**/*", server("en", [], false)); + await page.goto("http://magpie.test/?view=providers"); + await page.locator("#addProvider").click(); + await page.locator("#addSheet .tile").first().waitFor(); + assert.equal(await page.locator("#addSheet .sponsor-me").count(), 0); + await page.context().close(); + }); + }); +}