Files
plugins/packages/nowledge-mem/page/validate.js
T
SadWood 5b370f3e62 nowledge-mem 0.1.5: one Settings dialog for every option on released hosts (#588, SadWood)
The Nowledge Mem page's dialog now sets every option the hook reads, on Magpie's released bridge (options.get/set only), by SadWood.

- One Settings dialog: connection, key (an empty field keeps the saved key; Remove saved key / Undo), API key variable, recall and remote recall, Context Bundle, apps without memory, capture and remote capture, the human-dialogue filter (a switch, Cindy only with it on, tool results only with it off) and the advanced numbers. Remote HTTP needs the Advanced exception.
- A × beside a field that holds an explicit value returns it to the default. Saving re-reads the options, writes only edited fields, and refuses (keeping the draft) when one of them, or a gate's remote grant or the endpoint behind a newly allowed HTTP exception, changed elsewhere.
- page/validate.js is shared by the page and resolveOptions, so both refuse the same values.
- The footer wraps at narrow widths (German Save was cut off at 380px).
- No native select. Every language has every key; the English fallback loop is gone.

Reviewed at 75a09d19e, run merged onto main 04f3608 (main's later 24cd0b9 touches packages/cursor only). Mac mini, Bun 1.4.2, temp HOME:
- packages/nowledge-mem: 130 pass, 2 skip (live Mem, Linux-only Chrome layout), 0 fail.
- whole repo bun test: 4699 pass, 12 skip, 0 fail. bun scripts/check.mjs: exit 0.
- PR CI at this head ran the Linux Chrome layout test (pass, 30 layouts) and 4587 pass.
- Browser harness: page/ framed with sandbox="allow-scripts", magpie's CSP and magpie's real pagebridge.js; Chromium and WebKit, en/zh/zh-TW/ja/de, 320/380/900px, light/dark (60 runs). No request beyond the page's own files, no console or page errors, the key never in the DOM, no select, no overflow, key field password again after show/close/reopen, recallRemote shown but disabled while recall is off. Flows (en 380, both engines): editing Space gives one options.set keeping custom, apiKey, requestTimeoutMs:900, skipAgents and recallRemote; http://100.64.0.5 refused until the HTTP exception is on, then saved; a Space changed elsewhere conflicts and writes nothing; Bearer key replaces and empties the field; Remove saved key deletes apiKey; turning recall on keeps recallRemote.

Broken by hand:
- Main's footer CSS back: the harness flags 12 runs (en 320, de 320/380, both engines).
- settings.js dependency check off: "enabling a global gate conflicts…" and "enabling HTTP exception conflicts…" go red.
- validateExplicitOptions removed from resolveOptions: two hook option tests go red.

Not tried: a real Mem server.
2026-11-02 05:14:03 +08:00

34 lines
2.0 KiB
JavaScript

// Shared by the gateway hook and the sandboxed settings page. This classic
// script validates explicit settings; the hook resolves environment fallback.
// Keep the global available for later importers: ES modules are cached.
globalThis.__nowledgeMemValidateOptions = (raw = {}) => {
const fail = () => { throw new Error('invalidOption'); };
const text = (name) => {
const v = raw[name];
if (v === undefined || v === null || v === '') return undefined;
if (typeof v !== 'string' || !v.trim()) fail();
return v;
};
for (const name of ['spaceId','agentId','hostAgentId']) text(name);
const skip = raw.skipAgents;
if (skip != null && typeof skip !== 'string' && (!Array.isArray(skip) || skip.some((name) => typeof name !== 'string'))) fail();
for (const name of ['recall','contextBundle','capture','recallRemote','captureRemote','captureTools','captureCindy'])
if (raw[name] !== undefined && typeof raw[name] !== 'boolean') fail();
for (const [name,fallback,min,max] of [['maxMemories',5,1,20],['maxBytes',6000,512,32768],['requestTimeoutMs',1500,50,5000],['recallBackoffMs',30000,100,300000]]) {
const n = raw[name] ?? fallback;
if (!Number.isInteger(n) || n < min || n > max) fail();
}
if (raw.captureFilter !== undefined && raw.captureFilter !== 'off' && raw.captureFilter !== 'human') fail();
const endpoint = text('serverUrl');
if (endpoint) {
let u; try { u = new URL(endpoint); } catch { throw new Error('invalidUrl'); }
const local = ['localhost','127.0.0.1','[::1]'].includes(u.hostname);
if (!['http:','https:'].includes(u.protocol) || u.username || u.password || u.search || u.hash || (u.protocol === 'http:' && !local && raw.allowInsecureHttp !== true)) throw new Error('invalidUrl');
}
const env = text('apiKeyEnv') ?? 'NMEM_API_KEY';
if (!/^NMEM_[A-Za-z0-9_]+$/.test(env)) fail();
const secret = text('apiKey');
const key = secret?.replace(/^Bearer\s+/i,'').trim();
if (key && /[\s\u0000-\u001f\u007f]/.test(key)) throw new Error('invalidKey');
};