Files
plugins/packages/nowledge-mem/mem.mjs
T
yetone debc0f7b53 nowledge-mem 0.1.4: a tailnet HTTP server from the page, and recallRemote/captureRemote opt-ins (#608)
pluoki (on X) runs Mem on a Mac Studio reached over Tailscale. Two limits:

- page/settings.js validUrl() refused any http: URL to a non-loopback host
  whatever allowInsecureHttp said, while mem.mjs honoured it, so
  http://mac-studio.tailnet.ts.net:14242 could only be set by hand in
  Options JSON and the page's Save and Test refused it. The dialog now has
  an "Allow HTTP to another machine" switch (Mem service section, same
  switch-field component as the others; help text: only on a network you
  trust, e.g. Tailscale) that reads and writes allowInsecureHttp, and
  validUrl follows it exactly as mem.mjs does; Test checks the saved
  option. Turning it off removes the option. All five languages (en, zh,
  zh-TW, ja, de) get the new strings; urlHelp and invalidUrl name the
  switch.
- The hook took only origin local / magpie-key. recallRemote and
  captureRemote (default false, typed booleans, in package.json's options
  and the README) let remote and gateway-key requests in, each on its own,
  as powercontext's do: captureRemote saves the conversation but recalls
  nothing, and neither turns capture on. Unlike powercontext's, a missing
  or unknown origin stays out even with both on. powercontext's page has
  no switches for these, so this page doesn't either; its "What is sent"
  text names the options. Sticky blocks were already keyed by origin.

Run (mote, at this tree on origin/main 53e3820):
- HOME=$(mktemp -d) bun test: 4557 pass, 0 fail, 9 skip (live checks).
- bun scripts/check.mjs: ok.
- The page in Playwright 1.62 chromium and webkit, served over local HTTP
  with a stub bridge, in en/zh/zh-TW/ja/de: the tailnet URL is refused
  with the switch off and saved with allowInsecureHttp: true with it on;
  screenshots checked (en webkit, de chromium).
- Broken by hand, each red then green: validUrl ignoring the option, and
  the page never writing it (tailnet page test red); captureRemote
  granting recall, the old local-only line, unknown origins counted as
  remote (new origin test red); both opt-ins on by default (new test plus
  "remote and unknown origins stay out" and the gateway-key test red).
Not tried: a real Mem over a real tailnet, or in the real magpie app; the
hook is driven as magpie's host calls it, and the page through its test
and the Playwright stub.
2026-11-02 02:38:06 +08:00

229 lines
8.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// The Nowledge Mem connection for the gateway hook: how the endpoint and
// credential are resolved, how a request is made and bounded, and the small
// helpers the hook needs. The page, the hook and Mem's own clients read the
// same options and environment, so they never disagree about where Mem is or
// which Space they speak to.
import { createHash } from "node:crypto";
// Bumped with package.json: Mem's own clients record it on imported Threads.
export const version = "0.1.4";
// The only report codes magpie's Plugins page shows; tools turn these into
// messages, never the service's own text.
export class MemFailure extends Error {
code;
status;
constructor(code, status = 0) {
super(`Nowledge Mem ${code}`);
this.code = code;
this.status = status;
}
}
// Mem search answers carry per-hit debug metadata: 20 hits can reach ~110 KiB.
const maxResponseBytes = 512 << 10;
export function resolveOptions(raw = {}, env = process.env) {
const text = (v) => {
if (v === undefined || v === null || v === "")
return undefined;
if (typeof v !== "string" || !v.trim())
throw new MemFailure("invalid");
return v;
};
const bool = (name, fallback) => {
if (raw[name] !== undefined && typeof raw[name] !== "boolean")
throw new MemFailure("invalid");
return raw[name] ?? fallback;
};
const integer = (name, fallback, min, max) => {
const value = raw[name] ?? fallback;
if (!Number.isInteger(value) || value < min || value > max)
throw new MemFailure("invalid");
return value;
};
// skipAgents names the apps whose requests this plugin leaves alone: a
// list, or one string of names split by commas or new lines.
let skip = raw.skipAgents ?? [];
if (typeof skip === "string")
skip = skip.split(/[,,\n]/u);
if (!Array.isArray(skip) || skip.some((name) => typeof name !== "string"))
throw new MemFailure("invalid");
const skipAgents = new Set(skip.map(agentKey).filter(Boolean));
const captureFilter = raw.captureFilter === undefined ? "off" : raw.captureFilter;
if (captureFilter !== "off" && captureFilter !== "human")
throw new MemFailure("invalid");
// The Mem endpoint, as its own clients resolve it: the option, then the
// portable NMEM_API_URL, then the local desktop default. A URL from the
// environment that isn't an HTTP(S) address is ignored rather than fatal.
const configured = text(raw.serverUrl);
let serverUrl = configured;
if (!configured) {
let fromEnv;
try { fromEnv = text(env.NMEM_API_URL); } catch { /* unusable */ }
for (const candidate of [fromEnv, "http://127.0.0.1:14242"]) {
try {
if (candidate && ["https:", "http:"].includes(new URL(candidate).protocol)) {
serverUrl = candidate;
break;
}
}
catch { /* next candidate */ }
}
}
if (!serverUrl)
throw new MemFailure("invalid");
let server;
try {
server = new URL(serverUrl);
}
catch {
throw new MemFailure("invalid");
}
// a credential is never sent in clear text to another machine, kept in a
// URL that logs show, or followed through a redirect
const loopback = ["localhost", "127.0.0.1", "[::1]"].includes(server.hostname);
if (server.username || server.password || server.search || server.hash || !["https:", "http:"].includes(server.protocol) ||
(server.protocol === "http:" && !loopback && raw.allowInsecureHttp !== true))
throw new MemFailure("invalid");
server.pathname = server.pathname.replace(/\/$/, "") + "/";
const apiKeyEnv = text(raw.apiKeyEnv) ?? "NMEM_API_KEY";
if (!/^NMEM_[A-Za-z0-9_]+$/u.test(apiKeyEnv))
throw new MemFailure("invalid");
const apiKey = bearer(text(raw.apiKey) ?? text(env[apiKeyEnv]));
if (apiKey && /[\s\u0000-\u001f\u007f]/u.test(apiKey))
throw new MemFailure("invalid");
return {
server, apiKey, apiKeyEnv,
spaceId: text(raw.spaceId),
agentId: text(raw.agentId),
hostAgentId: text(raw.hostAgentId),
skipAgents,
recall: bool("recall", true),
contextBundle: bool("contextBundle", true),
capture: bool("capture", false),
recallRemote: bool("recallRemote", false),
captureRemote: bool("captureRemote", false),
captureTools: bool("captureTools", true),
captureFilter,
captureCindy: bool("captureCindy", false),
maxMemories: integer("maxMemories", 5, 1, 20),
maxBytes: integer("maxBytes", 6000, 512, 32768),
requestTimeoutMs: integer("requestTimeoutMs", 1500, 50, 5000),
recallBackoffMs: integer("recallBackoffMs", 30_000, 100, 300_000),
};
}
// agentKey is an app's name as skipAgents compares it: magpie's id
// ("claude-code") and the name its pages show ("Claude Code") are one app.
export function agentKey(name) {
return String(name ?? "").toLowerCase().replace(/[^\p{L}\p{N}]+/gu, "");
}
// request sends to the configured Mem only, reads a bounded reply and decodes
// it strictly. A status in `expected` comes back without its body, so
// "nothing there" is told from a failure. Failures use Mem's small outcome
// vocabulary; the caller decides what each means for it.
export async function request(o, { method, route, payload, signal, timeoutMs, expected = [], fetch: fetcher = fetch }) {
const headers = { "User-Agent": `magpie-nowledge-mem/${version}`, Accept: "application/json" };
if (payload !== undefined)
headers["Content-Type"] = "application/json";
if (o.apiKey)
headers.Authorization = `Bearer ${o.apiKey}`;
const deadline = AbortSignal.timeout(timeoutMs);
const combined = signal ? AbortSignal.any([signal, deadline]) : deadline;
try {
combined.throwIfAborted();
let r;
try {
r = await fetcher(new URL(route, o.server), {
method, headers, signal: combined, redirect: "error",
body: payload === undefined ? undefined : JSON.stringify(payload),
});
}
catch {
throw new MemFailure("unavailable");
}
if (r.ok)
return { status: r.status, body: await json(r) };
await r.body?.cancel();
if (expected.includes(r.status))
return { status: r.status, body: undefined };
if (r.status === 401 || r.status === 403)
throw new MemFailure("unauthorized", r.status);
if (r.status === 400)
throw new MemFailure(o.spaceId ? "unbound" : "invalid", r.status);
if (r.status === 404)
throw new MemFailure("notfound", r.status);
if (r.status === 409 || r.status === 422)
throw new MemFailure("invalid", r.status);
throw new MemFailure("unavailable", r.status);
}
catch (error) {
// The client's own cancellation keeps its reason, not ours.
signal?.throwIfAborted();
if (deadline.aborted)
throw new MemFailure("unavailable");
throw error;
}
}
async function json(r) {
const reader = r.body?.getReader();
if (!reader)
throw new MemFailure("invalid", r.status);
const chunks = [];
let size = 0;
try {
for (;;) {
let part;
try { part = await reader.read(); }
catch { throw new MemFailure("unavailable", r.status); }
const { value, done } = part;
if (done)
break;
size += value.byteLength;
if (size > maxResponseBytes) {
await reader.cancel();
throw new MemFailure("invalid", r.status);
}
chunks.push(value);
}
}
finally {
reader.releaseLock();
}
try {
return JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(Buffer.concat(chunks)));
}
catch {
throw new MemFailure("invalid", r.status);
}
}
export function bearer(value) {
if (!value)
return undefined;
return value.replace(/^Bearer\s+/iu, "").trim() || undefined;
}
export function serviceKey(o) { return sha256(o.server.href, o.apiKey ?? "", o.spaceId ?? ""); }
export function sha256(...parts) { return createHash("sha256").update(parts.join("\0")).digest("hex"); }
export function utf8(text) { return Buffer.byteLength(text, "utf8"); }
// trimText cuts at a UTF-8 boundary and marks the cut, so a tool's output and
// a recall block stay inside their budgets.
export function trimText(text, max) {
if (utf8(text) <= max)
return text;
let out = "", bytes = 0;
for (const ch of text) {
const size = utf8(ch);
if (bytes + size > max - 3)
break;
out += ch;
bytes += size;
}
return `${out}…`;
}