Files
plugins/packages/powercontext/page/settings.js
T
Tsukikage 1e4c8a2b91 powercontext 0.2.0: PowerContext recall and opt-in capture as a gateway hook (#117)
PowerContext as a gateway hook package (@magpie-community/gateway-powercontext 0.2.0): recall from a user-deployed PowerContext service before a request, and opt-in capture of completed conversations, with a connection page that uses only the options/theme bridge. By @Tsukikage7.

Reviewed at c61036e9 (author) plus bff430f9 (maintainer merge of main: README, registry.json and scripts/check.mjs conflicts, both sides kept; check.mjs takes main's identical hooks skip).
All four points of the 10-25 review are fixed: the timing tests give the healthy side 1000 ms and move the backoff with a stubbed Date.now(); the empty page panels are gone; Scope ID is optional on the page as in the hook; both READMEs describe the npm install and the registry lists it.

Run on mote (macOS, Bun 1.4.2, temp HOME) at bff430f9:
- bun scripts/check.mjs: exit 0. Full bun test: 3423 pass, 8 skip, 0 fail.
- packages/powercontext: 107 pass, 5 runs in a row, then 8 runs in parallel, 0 fail each.
- Broken by hand: putting the Scope-required check back in page/settings.js turns the optional-Scope page test red; making the recall backoff expire at once (powercontext.hooks.mjs:336) turns 4 tests red (hung scope/prepare/body and connection-failure backoff).
- Earlier round (10-25): the real package in magpie's Bun host behind the gateway handler, against fake PowerContext and upstream.
Not tried: the page in a browser at this head (the author reports 40 Chromium/WebKit cases), a real PowerContext server.
2026-10-29 21:13:37 +08:00

132 lines
6.2 KiB
JavaScript

// Only the official configuration bridge is used; no service requests or host actions.
const {languageFor, languageTags, translate} = powercontextI18n;
const $ = (id) => document.getElementById(id);
let language = 'en', busy = false, savedOptions = null, pendingCapture = null;
const t = (key, parameters) => translate(language, key, parameters);
function message(id, key, error = false) {
const el = $(id);
el.dataset.message = key;
el.textContent = t(key);
el.dataset.error = String(error);
el.hidden = false;
}
const result = (key, error = false) => message("result", key, error);
function summary() {
if (!savedOptions) return;
$("summary-url").textContent = savedOptions.serverUrl || t("notConfigured");
$("summary-scope").textContent = savedOptions.scopeId || t("automaticScope");
const capture = $("summary-capture");
capture.checked = pendingCapture ?? savedOptions.capture === true;
$("capture-state").textContent = savedOptions.capture === true ? t("on") : t("off");
capture.title = t("captureHelp");
}
function fill(options) {
$("url").value = options.serverUrl || ""; $("scope").value = options.scopeId || "";
$("auth").value = (options.authorization || "").replace(/^Bearer\s+/i, "");
$("auth").type = "password";
paintSecret();
}
function paintSecret() {
const visible = $("auth").type === "text";
const label = t(visible ? "hideKey" : "showKey");
$("show-auth").setAttribute("aria-label", label);
$("show-auth").setAttribute("title", label);
$("show-auth").setAttribute("aria-pressed", String(visible));
}
$("show-auth").onclick = () => { $("auth").type = $("auth").type === "password" ? "text" : "password"; paintSecret(); };
$("settings").onclose = () => { $("auth").value = ""; $("auth").type = "password"; paintSecret(); };
function close() { if (!busy) $("settings").close(); }
$("configure").onclick = () => { fill(savedOptions); $("result").hidden = true; $("settings").showModal(); };
$("close").onclick = close;
$("cancel").onclick = close;
$("deployment").onclick = () => { $("docs-feedback").hidden = true; $("deployment-dialog").showModal(); };
$("deployment-close").onclick = () => $("deployment-dialog").close();
$("deployment-done").onclick = () => $("deployment-dialog").close();
$("settings").oncancel = (event) => { if (busy) event.preventDefault(); };
function paint() {
language = languageFor(magpie.theme?.lang || navigator.language);
document.documentElement.lang = languageTags[language];
$("close").setAttribute("aria-label", t("close"));
$("deployment-close").setAttribute("aria-label", t("close"));
paintSecret();
summary();
document.querySelectorAll("[data-message]").forEach((el) => { el.textContent = t(el.dataset.message); });
document.querySelectorAll("[data-t]").forEach((el) => { el.textContent = t(el.dataset.t); });
}
magpie.ready.then(async () => {
paint();
magpie.onTheme(paint);
try {
const options = await magpie.options.get();
savedOptions = options; fill(options); summary();
message("configured", options.serverUrl ? "loaded" : "notConfigured");
$("save").disabled = false; $("configure").disabled = false; $("summary-capture").disabled = false;
} catch { message("configured", "loadFailed", true); }
}).catch(() => { paint(); message("configured", "bridgeFailed", true); });
$("copy-docs").onclick = () => {
const selection = window.getSelection(), range = document.createRange();
range.selectNodeContents($("docs")); selection.removeAllRanges(); selection.addRange(range);
let copied = false;
// The released sandbox has no clipboard bridge; use user-initiated copy with a selection fallback.
try { copied = document.execCommand("copy"); } catch { /* Keep the address selected for manual copying. */ }
if (copied) selection.removeAllRanges();
message("docs-feedback", copied ? "copied" : "copyFallback");
};
const save = async (event) => {
event.preventDefault(); if (busy) return;
let url;
try { url = new URL($("url").value.trim()); } catch { /* validated below */ }
if (!url || !["http:","https:"].includes(url.protocol) || url.username || url.password || url.search || url.hash || (url.protocol === "http:" && !["localhost","127.0.0.1","[::1]"].includes(url.hostname))) {
result("invalidUrl", true); $("url").focus(); return;
}
const scopeId = $("scope").value.trim();
const token = $("auth").value.trim().replace(/^Bearer\s+/i, "");
if (token && /[\s\u0000-\u001f\u007f]/.test(token)) {
result("invalidKey", true); $("auth").focus(); return;
}
busy = true; $("save").disabled = true; $("close").disabled = true; $("cancel").disabled = true;
try {
const previous = await magpie.options.get();
// Read again before merging to preserve options not exposed by this form.
const next = {...previous, serverUrl:url.href};
if (scopeId) next.scopeId = scopeId;
else delete next.scopeId;
if (token) next.authorization = `Bearer ${token}`;
else delete next.authorization;
await magpie.options.set(next);
savedOptions = next; summary();
message("configured", "configurationSaved");
result("saveSucceeded");
} catch { result("saveFailed", true); }
finally { busy = false; $("save").disabled = false; $("close").disabled = false; $("cancel").disabled = false; }
};
$("save").onclick = save;
$("form").onsubmit = (event) => event.preventDefault();
$("form").onkeydown = (event) => {
if (event.key === "Enter" && event.target.tagName === "INPUT" && event.target.type !== "checkbox") save(event);
};
$("summary-capture").onchange = async () => {
if (busy || !savedOptions) return;
const enabled = $("summary-capture").checked;
pendingCapture = enabled;
busy = true;
$("summary-capture").disabled = true; $("configure").disabled = true;
message("capture-feedback", "saving");
try {
const previous = await magpie.options.get();
const next = {...previous, capture:enabled};
await magpie.options.set(next);
savedOptions = next;
message("capture-feedback", enabled ? "captureEnabled" : "captureDisabled");
} catch {
// A rejected bridge response may follow a completed write; reconcile when possible.
try { savedOptions = await magpie.options.get(); } catch { /* Retain the last confirmed configuration. */ }
message("capture-feedback", "captureFailed", true);
} finally {
pendingCapture = null; summary(); busy = false;
$("summary-capture").disabled = false; $("configure").disabled = false;
}
};