Files
plugins/packages/nowledge-mem/page/validate.js
T
yetone dda4afdc36 nowledge-mem 0.1.6: requestTimeoutMs and the hook budget go up to 30s (#926)
A deep Mem search can take longer than 5s, and the page refused any
requestTimeoutMs over 5000 or gatewayTimeoutMs over 10000. Both now go up
to 30000; the defaults (1500 and magpie's 1000) are unchanged. magpie
v0.1.3538 (yetone/magpie#5890) runs a hook up to its own 30s budget;
the README says an older magpie cuts anything over 10s.

- validate.js, settings page and labels (en, zh, zh-TW, ja, de): 50-30000
  and 100-30000.
- nowledge-mem-agent checked: its 10000 ms values are fixed internal
  timeouts, not user options with a cap; unchanged.
- slow-recall.test.mjs: a fixture server answers /memories/search after
  6100 ms; requestTimeoutMs 15000 recalls it, 30001 is invalid, and a
  caller abort stops the fetch at once (the server sees the abort) and
  keeps the request as it came.

Run (wsl-run, magpie-desktop): bun test packages/nowledge-mem
packages/nowledge-mem-agent: 161 pass, 2 skip (live Mem and the
Playwright layout check, skipped as on main), 0 fail; bun
scripts/check.mjs: rc 0. The README's magpie version was filled in after
that run.
Not tried: a real Nowledge Mem server with a slow deep search.

Reported-by: aboat0625-cpu
2026-11-10 15:31:40 +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,30000],['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');
};