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>
45 lines
1.8 KiB
JavaScript
45 lines
1.8 KiB
JavaScript
import { expect, test } from "bun:test"
|
|
import { testConnection } from "./probe.mjs"
|
|
|
|
const json = (body, status = 200) => new Response(JSON.stringify(body), { status, headers: { "content-type": "application/json" } })
|
|
const fake = (routes) => async (url) => {
|
|
const path = new URL(url).pathname
|
|
const handler = routes[path]
|
|
if (!handler) throw new Error(`unexpected ${path}`)
|
|
return handler()
|
|
}
|
|
const mem = { serverUrl: "https://mem.example.com", apiKey: "nmem_probe" }
|
|
|
|
test("the connection test checks reachability, the key and the Space", async () => {
|
|
const result = await testConnection(mem, fake({
|
|
"/health": () => json({ status: "ok" }),
|
|
"/agent/working-memory": () => json({ exists: true, content: "## Focus Areas\n- Quartz release" }),
|
|
}))
|
|
expect(result.ok).toBe(true)
|
|
expect(result.ms).toBeGreaterThanOrEqual(0)
|
|
})
|
|
|
|
test("the connection test reports fixed codes, not Mem's text", async () => {
|
|
const unauthorized = await testConnection(mem, fake({
|
|
"/health": () => json({ status: "ok" }),
|
|
"/agent/working-memory": () => json({ detail: "invalid key" }, 401),
|
|
}))
|
|
expect(unauthorized).toEqual(expect.objectContaining({ ok: false, code: "unauthorized" }))
|
|
|
|
const unbound = await testConnection({ ...mem, spaceId: "space-x" }, fake({
|
|
"/health": () => json({ status: "ok" }),
|
|
"/agent/working-memory": () => json({ detail: "unknown space" }, 400),
|
|
}))
|
|
expect(unbound.ok).toBe(false)
|
|
expect(unbound.code).toBe("unbound")
|
|
|
|
const down = await testConnection(mem, async () => { throw new TypeError("fetch failed") })
|
|
expect(down).toEqual(expect.objectContaining({ ok: false, code: "unavailable" }))
|
|
})
|
|
|
|
test("the connection test refuses an invalid endpoint", async () => {
|
|
const result = await testConnection({ serverUrl: "ftp://mem.example.com" }, fake({}))
|
|
expect(result.ok).toBe(false)
|
|
expect(result.code).toBe("invalid")
|
|
})
|