toon-memory 3.6.1 → 4.1.0

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/dist/cli/setup.js CHANGED
@@ -288,6 +288,57 @@ function detectAgents() {
288
288
  // src/cli/memory.ts
289
289
  import { existsSync as existsSync4, mkdirSync as mkdirSync2, readFileSync as readFileSync4, writeFileSync as writeFileSync2 } from "fs";
290
290
  import { join as join4 } from "path";
291
+
292
+ // src/lib/utils.ts
293
+ var tokenize = (s) => s.replace(/([a-z])([A-Z])/g, "$1 $2").replace(/[-_]/g, " ").toLowerCase().split(/\s+/).filter(Boolean);
294
+ var unescField = (s) => {
295
+ let out = "";
296
+ for (let i = 0; i < s.length; i++) {
297
+ const c = s[i];
298
+ if (c === "\\" && i + 1 < s.length) {
299
+ const n = s[i + 1];
300
+ if (n === "n") {
301
+ out += "\n";
302
+ i++;
303
+ } else if (n === "|") {
304
+ out += "|";
305
+ i++;
306
+ } else if (n === "\\") {
307
+ out += "\\";
308
+ i++;
309
+ } else out += c;
310
+ } else out += c;
311
+ }
312
+ return out;
313
+ };
314
+ var parseToonLine = (line) => {
315
+ const parts = [];
316
+ let cur = "";
317
+ let escaped = false;
318
+ for (const c of line.trim()) {
319
+ if (escaped) {
320
+ cur += c;
321
+ escaped = false;
322
+ continue;
323
+ }
324
+ if (c === "\\") {
325
+ cur += c;
326
+ escaped = true;
327
+ continue;
328
+ }
329
+ if (c === "|") {
330
+ parts.push(unescField(cur));
331
+ cur = "";
332
+ continue;
333
+ }
334
+ cur += c;
335
+ }
336
+ if (escaped) cur += "\\";
337
+ parts.push(unescField(cur));
338
+ return parts;
339
+ };
340
+
341
+ // src/cli/memory.ts
291
342
  function installMemoryDir() {
292
343
  const memoryFile = join4(MEMORY_DIR, "data.toon");
293
344
  if (!existsSync4(MEMORY_DIR)) mkdirSync2(MEMORY_DIR, { recursive: true });
@@ -322,7 +373,7 @@ function dumpMemoryMarkdown() {
322
373
  const data = readFileSync4(memoryFile, "utf-8");
323
374
  const lines = data.split("\n").filter((l) => l.startsWith(" ") && l.includes("|"));
324
375
  const entries = lines.map((line) => {
325
- const parts = line.trim().split("|");
376
+ const parts = parseToonLine(line);
326
377
  return {
327
378
  id: parts[0] ?? "",
328
379
  category: parts[1] ?? "",
@@ -341,7 +392,7 @@ function dumpMemoryMarkdown() {
341
392
  if (!sl.includes(":") || !sl.startsWith(" ")) continue;
342
393
  const idx = sl.indexOf(":");
343
394
  const file = sl.slice(0, idx).trim();
344
- const summary = sl.slice(idx + 1).trim();
395
+ const summary = unescField(sl.slice(idx + 1).trim());
345
396
  if (file && summary) summaries.push({ file, summary });
346
397
  }
347
398
  }
@@ -1128,7 +1179,7 @@ function stats() {
1128
1179
  const categories = {};
1129
1180
  let latestDate = "";
1130
1181
  for (const line of lines) {
1131
- const parts = line.trim().split("|");
1182
+ const parts = parseToonLine(line);
1132
1183
  if (parts.length >= 7) {
1133
1184
  const category = parts[1];
1134
1185
  const date = parts[6];
@@ -1157,7 +1208,7 @@ function exportMemory() {
1157
1208
  const data = readFileSync9(memoryFile, "utf-8");
1158
1209
  const lines = data.split("\n").filter((l) => l.startsWith(" ") && l.includes("|"));
1159
1210
  const entries = lines.map((line) => {
1160
- const parts = line.trim().split("|");
1211
+ const parts = parseToonLine(line);
1161
1212
  return {
1162
1213
  id: parts[0],
1163
1214
  category: parts[1],
@@ -1209,7 +1260,7 @@ function importMemory() {
1209
1260
  let existingKeys = [];
1210
1261
  if (existsSync9(memoryFile)) {
1211
1262
  const existing = readFileSync9(memoryFile, "utf-8");
1212
- existingKeys = existing.split("\n").filter((l) => l.startsWith(" ") && l.includes("|")).map((l) => l.trim().split("|")[2]);
1263
+ existingKeys = existing.split("\n").filter((l) => l.startsWith(" ") && l.includes("|")).map((l) => parseToonLine(l)[2]);
1213
1264
  }
1214
1265
  const newEntries = importData.entries.filter((e) => !existingKeys.includes(e.key));
1215
1266
  if (newEntries.length === 0) {
@@ -1582,13 +1633,24 @@ function readMemory() {
1582
1633
  }
1583
1634
 
1584
1635
  // src/lib/graph.ts
1636
+ var DEFAULT_EDGE_TYPE = "related";
1637
+ function parseLinkToken(token) {
1638
+ const idx = token.lastIndexOf(":");
1639
+ if (idx === -1) return { type: DEFAULT_EDGE_TYPE, key: token };
1640
+ const type = token.slice(0, idx);
1641
+ const key = token.slice(idx + 1);
1642
+ return { type: type || DEFAULT_EDGE_TYPE, key: key || token };
1643
+ }
1644
+ function linkKey(token) {
1645
+ return parseLinkToken(token).key;
1646
+ }
1585
1647
  var entryLines = (data) => data.split("\n").filter(
1586
1648
  (l) => l.startsWith(" ") && l.includes("|") && !l.startsWith(" summaries:")
1587
1649
  );
1588
1650
  function parseEntries(data) {
1589
1651
  const out = [];
1590
1652
  for (const line of entryLines(data)) {
1591
- const parts = line.trim().split("|");
1653
+ const parts = parseToonLine(line);
1592
1654
  if (parts.length < 7) continue;
1593
1655
  const [id, category, key, content, file, tags, date, ttl, accessedRaw, linksRaw] = parts;
1594
1656
  out.push({
@@ -1606,11 +1668,25 @@ function parseEntries(data) {
1606
1668
  priority: parts.length > 13 ? parseInt(parts[13]) || 0 : 0,
1607
1669
  path_scope: parts.length > 14 ? parts[14] || "" : "",
1608
1670
  origin: parts.length > 15 && parts[15] ? parts[15] : "agent",
1609
- status: parts.length > 16 && parts[16] ? parts[16] : "active"
1671
+ status: parts.length > 16 && parts[16] ? parts[16] : "active",
1672
+ supersededOn: parts.length > 17 ? parts[17] || "" : ""
1610
1673
  });
1611
1674
  }
1612
1675
  return out;
1613
1676
  }
1677
+ function orphanScore(a, b) {
1678
+ const aTags = new Set(a.tags);
1679
+ let sharedTags = 0;
1680
+ for (const t of b.tags) if (aTags.has(t)) sharedTags++;
1681
+ const aKey = new Set(tokenize(a.key));
1682
+ const bKey = new Set(tokenize(b.key));
1683
+ let sharedKey = 0;
1684
+ for (const t of bKey) if (aKey.has(t)) sharedKey++;
1685
+ const aTok = new Set(tokenize(a.content));
1686
+ let sharedContent = 0;
1687
+ for (const t of tokenize(b.content)) if (aTok.has(t)) sharedContent++;
1688
+ return sharedTags * 4 + sharedKey * 2 + sharedContent;
1689
+ }
1614
1690
  function buildGraph(entries) {
1615
1691
  const byKey = /* @__PURE__ */ new Map();
1616
1692
  for (const e of entries) {
@@ -1626,7 +1702,7 @@ function buildGraph(entries) {
1626
1702
  adj.get(b).add(a);
1627
1703
  };
1628
1704
  for (const e of entries) {
1629
- for (const l of e.links) link(e.key, l);
1705
+ for (const l of e.links) link(e.key, linkKey(l));
1630
1706
  const refs = e.content.match(/\[\[([\w-]+)\]\]/g) || [];
1631
1707
  for (const r of refs) link(e.key, r.slice(2, -2));
1632
1708
  }
@@ -1646,8 +1722,71 @@ function buildGraph(entries) {
1646
1722
  }
1647
1723
  const adjacency = /* @__PURE__ */ new Map();
1648
1724
  for (const [k, v] of adj) adjacency.set(k, [...v]);
1725
+ const allKeys = [...byKey.keys()];
1726
+ for (const key of allKeys) {
1727
+ if (adj.has(key)) continue;
1728
+ let best = "";
1729
+ let bestScore = -1;
1730
+ for (const other of allKeys) {
1731
+ if (other === key) continue;
1732
+ const score = orphanScore(byKey.get(key), byKey.get(other));
1733
+ if (best === "" || score > bestScore || score === bestScore && other < best) {
1734
+ bestScore = score;
1735
+ best = other;
1736
+ }
1737
+ }
1738
+ if (best) link(key, best);
1739
+ }
1740
+ for (const [k, v] of adj) adjacency.set(k, [...v]);
1741
+ const comps = connectedComponents(adjacency);
1742
+ if (comps.length > 1) {
1743
+ let largest = comps[0];
1744
+ for (const c of comps) if (c.length > largest.length) largest = c;
1745
+ const largestSet = new Set(largest);
1746
+ for (const comp of comps) {
1747
+ if (comp.length === largest.length && comp[0] === largest[0]) continue;
1748
+ let bestA = "";
1749
+ let bestB = "";
1750
+ let bestScore = -1;
1751
+ for (const a of comp) {
1752
+ const ea = byKey.get(a);
1753
+ for (const b of largest) {
1754
+ const score = orphanScore(ea, byKey.get(b));
1755
+ if (bestA === "" || score > bestScore || score === bestScore && a + b < bestA + bestB) {
1756
+ bestScore = score;
1757
+ bestA = a;
1758
+ bestB = b;
1759
+ }
1760
+ }
1761
+ }
1762
+ if (bestA) link(bestA, bestB);
1763
+ }
1764
+ for (const [k, v] of adj) adjacency.set(k, [...v]);
1765
+ }
1649
1766
  return { adjacency, byKey };
1650
1767
  }
1768
+ function connectedComponents(adjacency) {
1769
+ const comps = [];
1770
+ const seen = /* @__PURE__ */ new Set();
1771
+ for (const [start] of adjacency) {
1772
+ if (seen.has(start)) continue;
1773
+ const comp = [];
1774
+ const stack = [start];
1775
+ seen.add(start);
1776
+ while (stack.length) {
1777
+ const cur = stack.pop();
1778
+ comp.push(cur);
1779
+ for (const next of adjacency.get(cur) || []) {
1780
+ if (!seen.has(next)) {
1781
+ seen.add(next);
1782
+ stack.push(next);
1783
+ }
1784
+ }
1785
+ }
1786
+ comps.push(comp);
1787
+ }
1788
+ return comps;
1789
+ }
1651
1790
 
1652
1791
  // src/viewer/data.ts
1653
1792
  function buildViewerData() {