Lists Magic_zero's (Magiczerowxy) magpie-chat as @magpie-community/page-chat:
a chat page in magpie's window over magpie's own gateway, models, MCP
servers, instructions and skills. Not a provider or a relay: it signs in
to nothing and every request goes through magpie's gateway.
- chat.test.mjs starts the service on a free port against a fake
gateway in a temp config folder: no token (Origin null and none), a
wrong token, another Origin and a rebound Host are refused and nothing
reaches the gateway; with the token a chat streams with the caller key;
the icon redirect has a Location; boot.js makes no request; app.js
sends the token.
- scripts/check.mjs takes the OpenCode v1 default export {id, server}
as magpie's host does, and doesn't start a page's server (it would bind
a port and write config); layerfs's tools are still checked.
- package.json declares the port option (3477) both parts read; boot.js's
comment says so instead of a fallback to 3478 that never existed.
- README in the repo's style with credit, what the service reads and
sends, who can use it, and the changes from the original.
- registry.json entry and a row in the Pages table.
Run on mote (bun 1.4.2) at this tree:
- cd packages/chat && HOME=$(mktemp -d) bun test: 11 pass.
- HOME=$(mktemp -d) bun test (whole repo): 3119 pass, 0 fail, 3127
tests across 261 files; bun scripts/check.mjs: exit 0, chat and
layerfs ok.
Broken by hand: with the token check off, "a page without the token
can't chat" went red; with the Host check off, "a DNS-rebound Host is
refused" went red.
Not tried: the page in a real magpie window (sandboxed frame navigation
and the host calling server()).
Co-authored-by: Magiczerowxy <magiczerowxy@users.noreply.github.com>
56 lines
2.5 KiB
JavaScript
56 lines
2.5 KiB
JavaScript
// 带路页:把它自己(也就是 magpie 页面那个框架)导航到插件的服务。
|
||
//
|
||
// 这里必须"直接跳",不能先探测:
|
||
// 页面的 CSP 是 connect-src 'none',fetch/XHR/WebSocket 全被禁止 ——
|
||
// 连 no-cors 的请求都会被直接拒绝。所以「先探一下服务在不在」这条路走不通,
|
||
// alive() 永远返回 false,页面会永远停在等待页。
|
||
// magpie 文档也明说了:这个页面唯一能做的联网动作就是把自己跳转到别处。
|
||
//
|
||
// 端口用插件选项里的 port(默认 3477),服务端(index.mjs)读的是同一个值。
|
||
//
|
||
// 顺手把窗口的主题和设计令牌一起带上(magpie 的 bridge 已经把它们算好了),
|
||
// 这样对话页的配色和字体跟窗口完全一致,连用户的自定义主题也算在内。
|
||
(async () => {
|
||
const DEFAULT_PORT = 3477;
|
||
let port = DEFAULT_PORT;
|
||
let theme = "";
|
||
let tokens = null;
|
||
|
||
try {
|
||
const t = await window.magpie?.ready; // { dark, lang, tokens }
|
||
if (t) {
|
||
if (typeof t.dark === "boolean") theme = t.dark ? "dark" : "light";
|
||
if (t.tokens && typeof t.tokens === "object") tokens = t.tokens;
|
||
}
|
||
} catch { /* 没有 bridge 就退回默认主题 */ }
|
||
|
||
try {
|
||
const o = await window.magpie?.options?.get?.();
|
||
if (o && Number.isFinite(Number(o.port)) && Number(o.port) > 0) port = Number(o.port);
|
||
} catch { /* 没有 options 就用默认端口 */ }
|
||
|
||
// 字体不在这里传。
|
||
//
|
||
// 试过从 magpie 页面的 CSS 变量里读 --ui-font 再带过去,但时机靠不住:
|
||
// magpie 是设置加载完之后才把 --ui-font 写到 :root 上的,这里 await 完
|
||
// magpie.ready 立刻读,经常读到空。而且页面 CSP 是 connect-src 'none',
|
||
// 读不到也没法重试。现在改成服务端直接读 magpie 的 settings.json,
|
||
// 由 /api/fonts.css 提供给对话页 —— 那边什么时候读都准。
|
||
|
||
const q = new URLSearchParams();
|
||
if (theme) q.set("theme", theme);
|
||
if (true) {
|
||
// 只留非空的令牌,压成一个短串;令牌本身就是 magpie 自己的 CSS 变量值。
|
||
const slim = {};
|
||
for (const [k, v] of Object.entries(tokens || {})) {
|
||
const s = String(v ?? "").trim();
|
||
if (s) slim[k] = s;
|
||
}
|
||
if (Object.keys(slim).length) {
|
||
try { q.set("tokens", btoa(unescape(encodeURIComponent(JSON.stringify(slim))))); } catch { /* 编码不了就只带主题 */ }
|
||
}
|
||
}
|
||
|
||
location.replace("http://127.0.0.1:" + port + "/?" + q.toString());
|
||
})();
|