Adds @magpie-community/gateway-nowledge-mem, an asynchronous gateway hook by IceCodeNew (#526): before each request it recalls from the user's Nowledge Mem (and the Context Bundle / Working Memory on a session's first turn), and, only when `capture` is turned on, imports completed conversations as Mem Threads. It applies to direct local requests and the `magpie-key` sharing key this computer writes into the user's own agents; other gateway keys, remote and unknown origins are never included. A page sets the connection and runs a real connection test through a loopback service guarded by Host, Origin and a per-start token. The package writes nothing into magpie's Library. registry.json gains one compact entry. Reviewed head 3f4ba91 (f9eb686 plus a maintainer commit adding a test that starts the service and expects 403 for a foreign Host, Origin and token). Run at 3f4ba91's tree on mote (macOS, Bun 1.4.2, HOME=$(mktemp -d)): repo `bun test` 3861 pass, 9 skip, 0 fail; `bun scripts/check.mjs` exit 0; packages/nowledge-mem 53 pass, 1 skip. Broken by hand: each of the service's four guards in handle() turns the new test red; earlier rounds broke the origin gate, the block escaping, the secret check and the cleartext-HTTP guard, each red. Not tried: a real Mem server, a real magpie with the plugin installed, Windows. Co-authored-by: IceCodeNew <32576256+IceCodeNew@users.noreply.github.com>
43 lines
3.7 KiB
JavaScript
43 lines
3.7 KiB
JavaScript
// The page's connection test, run by the small loopback service the page
|
|
// navigates to. Magpie's page sandbox has no network (connect-src 'none'), so
|
|
// the page cannot call Mem itself; the service tests the saved plugin options,
|
|
// so no URL or key crosses the page and no page can aim it at another host.
|
|
import { MemFailure, request, resolveOptions } from "./mem.mjs";
|
|
|
|
const texts = {
|
|
en: { testing: "Testing the connection…", connected: "Connected", failed: "Connection failed", back: "Back to settings", noReturn: "No settings page to return to.", noService: "The local test service did not answer.", unauthorized: "key rejected", unbound: "Space not found", unavailable: "unreachable", invalid: "invalid address", notfound: "Working Memory not supported", empty: "no answer", run: "Test" },
|
|
zh: { testing: "正在测试连接…", connected: "连接正常", failed: "连接失败", back: "返回设置", noReturn: "没有可返回的设置页面。", noService: "本地测试服务没有响应。", unauthorized: "密钥被拒绝", unbound: "Space 不存在", unavailable: "无法连接", invalid: "地址无效", notfound: "不支持 Working Memory", empty: "没有返回内容", run: "测试" },
|
|
"zh-TW": { testing: "正在測試連線…", connected: "連線正常", failed: "連線失敗", back: "返回設定", noReturn: "沒有可返回的設定頁面。", noService: "本機測試服務沒有回應。", unauthorized: "金鑰被拒絕", unbound: "Space 不存在", unavailable: "無法連線", invalid: "位址無效", notfound: "不支援 Working Memory", empty: "沒有傳回內容", run: "測試" },
|
|
ja: { testing: "接続を確認しています…", connected: "接続できました", failed: "接続に失敗しました", back: "設定に戻る", noReturn: "戻る設定ページがありません。", noService: "ローカルのテストサービスが応答しません。", unauthorized: "キーが拒否されました", unbound: "Space が見つかりません", unavailable: "接続できません", invalid: "アドレスが無効です", notfound: "Working Memory 非対応", empty: "応答がありません", run: "テスト" },
|
|
de: { testing: "Verbindung wird geprüft…", connected: "Verbunden", failed: "Verbindung fehlgeschlagen", back: "Zurück zu den Einstellungen", noReturn: "Keine Einstellungsseite zum Zurückkehren.", noService: "Der lokale Testdienst antwortet nicht.", unauthorized: "Schlüssel abgelehnt", unbound: "Space nicht gefunden", unavailable: "nicht erreichbar", invalid: "ungültige Adresse", notfound: "Working Memory nicht unterstützt", empty: "keine Antwort", run: "Test" },
|
|
};
|
|
|
|
export function textsFor(lang) {
|
|
return texts[lang] ?? texts.en;
|
|
}
|
|
|
|
// testConnection makes the two requests the page promises: reachability
|
|
// through the public health route, then the key and Space through Working
|
|
// Memory. It reports fixed codes, never Mem's own text.
|
|
export async function testConnection(raw = {}, fetcher = fetch, timeoutMs = 5000) {
|
|
const started = Date.now();
|
|
try {
|
|
const o = resolveOptions({ ...raw, recall: true, capture: false });
|
|
const health = await request(o, { method: "GET", route: "health", timeoutMs, fetch: fetcher });
|
|
const route = `agent/working-memory${o.spaceId ? `?space_id=${encodeURIComponent(o.spaceId)}` : ""}`;
|
|
await request(o, { method: "GET", route, timeoutMs, fetch: fetcher });
|
|
return {
|
|
ok: true,
|
|
ms: Date.now() - started,
|
|
version: typeof health.body?.version === "string" ? health.body.version : undefined,
|
|
};
|
|
}
|
|
catch (error) {
|
|
return {
|
|
ok: false,
|
|
code: error instanceof MemFailure ? error.code : "unavailable",
|
|
ms: Date.now() - started,
|
|
};
|
|
}
|
|
}
|