dsh-lm-memory 0.19.0 → 0.19.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/server/viewer.mjs CHANGED
@@ -604,6 +604,15 @@ let t; $("#q").addEventListener("input", (e) => { clearTimeout(t); t = setTimeou
604
604
  load();
605
605
  </script></body></html>`;
606
606
 
607
+ const CONSOLE_PAGE = (() => {
608
+ try { return fs.readFileSync(new URL("./console.html", import.meta.url), "utf8"); }
609
+ catch { return null; }
610
+ })();
611
+ const tailLines = (file, n) => {
612
+ try { return fs.readFileSync(file, "utf8").trim().split("\n").slice(-n); } catch { return []; }
613
+ };
614
+ const parseLines = (lines) => lines.flatMap((line) => { try { return [JSON.parse(line)]; } catch { return []; } });
615
+
607
616
  http.createServer(async (req, res) => {
608
617
  const json = (d, code = 200) => { res.writeHead(code, { "Content-Type": "application/json; charset=utf-8" }); res.end(JSON.stringify(d)); };
609
618
  try {
@@ -638,9 +647,10 @@ http.createServer(async (req, res) => {
638
647
  }
639
648
  }
640
649
  if (!["127.0.0.1", "localhost", "[::1]", ""].includes(hostName)) { res.writeHead(403, { "Content-Type": "text/plain" }); return res.end("forbidden host"); }
641
- if (req.method === "GET" && u.pathname === "/") {
642
- res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
643
- return res.end(PAGE);
650
+ if (req.method === "GET" && (u.pathname === "/" || u.pathname === "/classic")) {
651
+ const body = u.pathname === "/classic" || !CONSOLE_PAGE ? PAGE : CONSOLE_PAGE;
652
+ res.writeHead(200, { "Content-Type": "text/html; charset=utf-8", "Cache-Control": "no-store" });
653
+ return res.end(body);
644
654
  }
645
655
  // v0.10 版本端点:供插件探测旧版 viewer 并自动升级重启(detached 进程 app 重启杀不掉)
646
656
  if (req.method === "GET" && u.pathname === "/api/version") {
@@ -713,11 +723,67 @@ http.createServer(async (req, res) => {
713
723
  const r = await call("memory_pin", { id: body.id, action: body.action, reason: "viewer" });
714
724
  return json({ ok: String(r).includes("OK"), msg: String(r).slice(0, 80) });
715
725
  }
726
+ if (u.pathname === "/api/conflict/resolve") {
727
+ const oldId = Number(body.old), newId = Number(body.new);
728
+ const oldRow = ro().prepare("SELECT id, kind, content, updated_at FROM memories WHERE id = ? AND status = 'active'").get(oldId);
729
+ const newRow = ro().prepare("SELECT id, kind, content, updated_at FROM memories WHERE id = ? AND status = 'active'").get(newId);
730
+ if (!oldRow || !newRow) return json({ ok: false, msg: "候选对已变化,请刷新后重试" });
731
+ if (body.verdict === "adopt") {
732
+ // 旧说法被接棒:逻辑删除旧条(保留审计),并把这对记为 superseded
733
+ const r = await call("memory_forget", { id: oldId, reason: "conflict-adopt" });
734
+ saveConflictReview(oldRow, newRow, "superseded");
735
+ return json({ ok: String(r).includes("OK"), msg: "已采纳新说法,旧条转为历史(可审计)" });
736
+ }
737
+ // 保留旧说法 / 两条无关:只记录判定,不动任何内容
738
+ const verdict = body.verdict === "keep" ? "related" : "unrelated";
739
+ saveConflictReview(oldRow, newRow, verdict);
740
+ return json({ ok: true, msg: verdict === "related" ? "已记录:两条都保留" : "已记录:判为无关,不再提示" });
741
+ }
716
742
  if (u.pathname === "/api/sleep") {
717
743
  const r = await call("memory_sleep", {});
718
744
  return json({ ok: true, msg: String(r).slice(0, 200) });
719
745
  }
720
746
  }
747
+ if (req.method === "GET" && u.pathname === "/api/events") {
748
+ const changes = parseLines(tailLines(path.join(MEM_DIR, "changes.jsonl"), 400));
749
+ const log = parseLines(tailLines(path.join(MEM_DIR, "log.jsonl"), 400));
750
+ const nowMs = Date.now();
751
+ const hours = [];
752
+ for (let i = 23; i >= 0; i--) {
753
+ const start = new Date(nowMs - i * 3600e3);
754
+ hours.push({
755
+ hour: start.toLocaleTimeString("zh-CN", { hour: "2-digit", hour12: false }),
756
+ n: changes.filter((c) => { const t = new Date(c.ts); return t >= start && t < new Date(start.getTime() + 3600e3); }).length,
757
+ });
758
+ }
759
+ const dayAgo = new Date(nowMs - 864e5);
760
+ const last24 = changes.filter((c) => new Date(c.ts) >= dayAgo);
761
+ const failures = log.filter((l) => /failed|error/i.test(String(l.op ?? "")));
762
+ return json({
763
+ version: PKG_VERSION, pid: process.pid, db: path.join(MEM_DIR, "memory.db"),
764
+ hours,
765
+ events: changes.slice(-40).reverse().map((c) => ({
766
+ ts: c.ts, op: c.op, id: c.new_id ?? c.id ?? null, kind: c.kind ?? null,
767
+ clip: String(c.content ?? c.reason ?? (c.total ? c.total + " 组冲突" : "") ?? "").slice(0, 90),
768
+ })),
769
+ failures: failures.slice(-6).reverse().map((l) => ({ ts: l.ts, op: l.op, session: String(l.sessionId ?? "").slice(8, 16), attempts: l.attempts ?? null })),
770
+ last24: last24.length,
771
+ writes24: last24.filter((c) => ["remember", "episode", "ingest"].includes(c.op)).length,
772
+ corrections24: last24.filter((c) => c.op === "correct").length,
773
+ failures24: failures.filter((l) => new Date(l.ts) >= dayAgo).length,
774
+ });
775
+ }
776
+ if (req.method === "GET" && u.pathname === "/api/audit") {
777
+ const log = parseLines(tailLines(path.join(MEM_DIR, "log.jsonl"), 400));
778
+ const rows = log.slice(-300).reverse().map((l) => ({
779
+ ts: l.ts, op: String(l.op ?? ""),
780
+ by: String(l.by ?? "").replace("mcp:lm-memory", "mcp").replace("extract-helper", "helper"),
781
+ detail: String(l.reason ?? l.error ?? l.sessionId ?? l.id ?? l.patch ?? "").slice(0, 70),
782
+ }));
783
+ const opCount = {};
784
+ for (const row of rows) opCount[row.op] = (opCount[row.op] ?? 0) + 1;
785
+ return json({ rows, ops: Object.entries(opCount).sort((a, b) => b[1] - a[1]) });
786
+ }
721
787
  json({ error: "not found" }, 404);
722
788
  } catch (e) { json({ error: String(e.message) }, 500); }
723
789
  }).listen(PORT, "127.0.0.1", () => {