page-agent-console 1.1.0, by qingnian108 (#569). QA fixes B1-B21, a reworked UI, a new logo, and self-heal after a plugin host restart. In this version: - Accent: a fixed neutral grey (charcoal in light mode, light grey in dark mode). Magpie's accent tokens are dropped on purpose. Surfaces, text, lines, fonts and semantic colours still follow magpie. - Every native <select> (composer effort/mode/Cursor fast, Settings, Debug) is now the page's own SelectMenu, a button plus a listbox. - The registry summary again discloses the 127.0.0.1 service and its lifecycle, that only its own page gets the token, the ~46 MB first install, and that Claude Code isn't bundled (en, zh). - session.js is written once at bind with beat:false, and beats every 4s only after the first /open. A new host deletes a dead host's session.js at startup (owner pid in session-owner.json, outside the page folder). - The daemon swallows console EPIPE after a hard host kill and handles a lifeline close once. Run at 930c6e2 on macOS (mote, Node 22), in a fresh copy: - npm ci, then npm run check (tsc daemon + web): ok - npx vitest run: 46 files, 288 passed, 17 skipped - npm run verify:build: app.js, app.css, index.html and daemon/dist/main.js(.map) rebuild byte-identical - bun scripts/check.mjs agent-console: ok - Playwright Chromium and WebKit, light and dark, sandbox HOME, page served under /_magpie/plugin/<id>/page with a stub bridge passing accent #ff0000: - accent: --accent and the primary button stay grey (rgb(38,38,38) light, rgb(229,229,229) dark); no <select> anywhere. - Settings menus: click opens the menu inside the window; ArrowDown+Enter picks, closes and refocuses the trigger; the pick persists through the daemon. - Esc closes the menu, not the dialog, and then closes the dialog. Clicking an option keeps the dialog open; a click outside closes the menu. - A host that was never opened writes beat:false and doesn't rewrite session.js. SIGKILL it, start a new host: the stale file is removed and the page reaches the new daemon. - After an open, session.js beats. SIGKILL the host: the page heals in ~5.3s and stays. A daemon-only crash heals in ~5.3s. - Daemon logs: no uncaughtException or EPIPE, one lifeline line per daemon exit. Not tried: the composer pickers in a browser (they need a thread with an installed agent; same SelectMenu component), magpie's real sandboxed frame, Windows and Linux.
124 lines
5.5 KiB
JavaScript
124 lines
5.5 KiB
JavaScript
// Bootstrap page: the only network action of this sandboxed page (no allow-same-origin, connect-src 'none') is navigating itself elsewhere.
|
|
//
|
|
// Handoff: once the plugin host binds the gate port, it writes {port, key} into session.js in this directory.
|
|
// key is only the gate key, not the daemon token: Magpie serves this directory at /_magpie/plugin/<path that changes every launch>/
|
|
// without checking the origin, so any page that knows the path can read session.js via <script src>; nothing that unlocks the daemon goes there.
|
|
// This page navigates to the gate at http://127.0.0.1:<port>/open?k=<key>; the gate starts the daemon and 302s to the daemon page,
|
|
// with the daemon token only in the #t= fragment.
|
|
// The window's theme, language and color tokens are passed along too, so that page matches magpie.
|
|
(async () => {
|
|
let theme = "";
|
|
let lang = "";
|
|
let tokens = null;
|
|
|
|
try {
|
|
const t = await window.magpie?.ready;
|
|
if (t) {
|
|
if (typeof t.dark === "boolean") theme = t.dark ? "dark" : "light";
|
|
if (typeof t.lang === "string") lang = t.lang;
|
|
if (t.tokens && typeof t.tokens === "object") tokens = t.tokens;
|
|
}
|
|
} catch { /* no bridge: use defaults */ }
|
|
|
|
const zh = !lang || lang.toLowerCase().startsWith("zh");
|
|
const said = document.getElementById("said");
|
|
const brand = document.querySelector(".box b");
|
|
const retry = document.getElementById("retry");
|
|
if (said) said.textContent = zh ? "正在启动本地服务…" : "Starting local service…";
|
|
if (brand) brand.textContent = zh ? "Agent 控制台" : "Agent Console";
|
|
|
|
// ts: when the host wrote the file. beat: true once the host refreshes it every few seconds (from the first page open on);
|
|
// a beating file whose ts stopped moving belongs to a host that was killed (Magpie restarting it) and its gate is gone, so wait
|
|
// for the new host's file instead of navigating to a dead port. A file that doesn't beat (the page was never opened) can be old
|
|
// and still live; a dead host's one is deleted by the next host when it starts.
|
|
const STALE_MS = 12000;
|
|
// ac_lost: set by the daemon page when it lost its connection and came back here. The old host's file was last refreshed before
|
|
// that moment, so only a session.js refreshed after it (a live host's) is used.
|
|
const here = new URL(location.href);
|
|
const lostAt = Number(here.searchParams.get("ac_lost")) || 0;
|
|
here.searchParams.delete("ac_lost");
|
|
here.hash = "";
|
|
function validSession(s) {
|
|
return (
|
|
s &&
|
|
typeof s === "object" &&
|
|
Number.isInteger(s.port) &&
|
|
s.port > 0 &&
|
|
s.port <= 65535 &&
|
|
typeof s.key === "string" &&
|
|
/^[0-9a-f]{64}$/.test(s.key) &&
|
|
fresh(s)
|
|
);
|
|
}
|
|
function fresh(s) {
|
|
if (typeof s.ts !== "number") return true; // a host from before ts
|
|
if (lostAt && !(s.ts > lostAt)) return false;
|
|
if (s.beat === false) return true;
|
|
return Math.abs(Date.now() - s.ts) < STALE_MS;
|
|
}
|
|
|
|
// Reload an uncached copy of session.js (the key changes after the plugin host restarts).
|
|
// Returns null when the file doesn't exist (the host isn't up yet or the port isn't bound yet).
|
|
function loadFreshSession(attempt) {
|
|
return new Promise((resolve) => {
|
|
globalThis.AgentConsoleSession = undefined;
|
|
const script = document.createElement("script");
|
|
script.src = `session.js?r=${Date.now()}-${attempt}`;
|
|
script.onload = () => {
|
|
script.remove();
|
|
resolve(validSession(globalThis.AgentConsoleSession) ? globalThis.AgentConsoleSession : null);
|
|
};
|
|
script.onerror = () => {
|
|
script.remove();
|
|
resolve(null);
|
|
};
|
|
document.head.appendChild(script);
|
|
});
|
|
}
|
|
|
|
// The copy loaded statically by index.html (possibly cached) is only a fallback
|
|
const preloaded = validSession(globalThis.AgentConsoleSession) ? globalThis.AgentConsoleSession : null;
|
|
let session = await loadFreshSession(0);
|
|
if (!session && preloaded) session = preloaded;
|
|
// Right after the host starts (or restarts) the file may be missing or stale: retry every 500ms for about 30 seconds
|
|
for (let attempt = 1; !session && attempt <= 60; attempt++) {
|
|
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
session = await loadFreshSession(attempt);
|
|
}
|
|
|
|
if (!session) {
|
|
if (said) {
|
|
said.textContent = zh
|
|
? "本地服务还没有就绪。请稍后重试;如果一直这样,请在 Magpie 里重新加载插件。"
|
|
: "The local service is not ready yet. Try again in a moment; if this persists, reload the plugin in Magpie.";
|
|
}
|
|
if (retry) {
|
|
retry.textContent = zh ? "重试" : "Retry";
|
|
retry.hidden = false;
|
|
retry.onclick = () => location.reload();
|
|
}
|
|
return;
|
|
}
|
|
|
|
const q = new URLSearchParams();
|
|
q.set("k", session.key);
|
|
if (theme) q.set("theme", theme);
|
|
if (lang) q.set("lang", lang);
|
|
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 { /* fine if it can't be passed along */ }
|
|
}
|
|
// This page's own URL: when Magpie restarts the plugin host (new gate key, the old daemon exits), the daemon page comes back here
|
|
// to read the fresh session.js. The gate checks it and puts it only in the # fragment.
|
|
try {
|
|
const back = here.toString();
|
|
if (back.length <= 2048) q.set("back", back);
|
|
} catch { /* no self-heal, the page still works */ }
|
|
|
|
location.replace("http://127.0.0.1:" + session.port + "/open?" + q.toString());
|
|
})();
|