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/README.de.md +3 -5
- package/README.es.md +5 -7
- package/README.fr.md +3 -5
- package/README.ja.md +3 -5
- package/README.ko.md +3 -5
- package/README.md +39 -11
- package/README.pt-br.md +3 -5
- package/README.zh.md +3 -5
- package/dist/cli/capture.js +49 -1
- package/dist/cli/session-start.js +49 -1
- package/dist/cli/setup.js +147 -8
- package/mcp/server.js +968 -420
- package/package.json +5 -3
- package/skills/toon-memory.md +2 -4
- package/src/cli/capture.ts +2 -1
- package/src/cli/commands.ts +4 -3
- package/src/cli/memory.ts +3 -2
- package/src/cli/session-start.ts +2 -1
- package/src/lib/graph.ts +213 -17
- package/src/lib/quality.ts +49 -20
- package/src/lib/utils.ts +81 -5
- package/src/mcp/archive.ts +3 -2
- package/src/mcp/consolidation.ts +5 -4
- package/src/mcp/observations.ts +2 -1
- package/src/mcp/resources.ts +2 -1
- package/src/mcp/scoring.ts +5 -5
- package/src/mcp/session-store.ts +10 -9
- package/src/mcp/tools.ts +767 -393
package/src/lib/utils.ts
CHANGED
|
@@ -6,17 +6,18 @@
|
|
|
6
6
|
export const normalize = (s: string): string =>
|
|
7
7
|
s.toLowerCase().replace(/[-_]/g, " ").replace(/\s+/g, " ").trim()
|
|
8
8
|
|
|
9
|
-
export const isExpiredLocal = (ttl: string): boolean => {
|
|
9
|
+
export const isExpiredLocal = (ttl: string, today = new Date().toISOString().split("T")[0]): boolean => {
|
|
10
10
|
if (!ttl) return false
|
|
11
|
-
const today = new Date().toISOString().split("T")[0]
|
|
12
11
|
return ttl <= today
|
|
13
12
|
}
|
|
14
13
|
|
|
15
14
|
/** Lightweight importance score: recency (60%) + frequency (40%). */
|
|
16
|
-
export function importance(
|
|
17
|
-
|
|
15
|
+
export function importance(
|
|
16
|
+
e: { date: string; accessed: number },
|
|
17
|
+
today = new Date().toISOString().split("T")[0]
|
|
18
|
+
): number {
|
|
18
19
|
const days =
|
|
19
|
-
(Date.
|
|
20
|
+
(Date.parse(`${today}T00:00:00`) - Date.parse(`${e.date || today}T00:00:00`)) / 86400000
|
|
20
21
|
const recency = Math.min(1, Math.max(0, 30 - days) / 30)
|
|
21
22
|
const freq = Math.min(1, e.accessed / 5)
|
|
22
23
|
return recency * 0.6 + freq * 0.4
|
|
@@ -30,6 +31,81 @@ export const tokenize = (s: string): string[] =>
|
|
|
30
31
|
.split(/\s+/)
|
|
31
32
|
.filter(Boolean)
|
|
32
33
|
|
|
34
|
+
/**
|
|
35
|
+
* Escape a field for the pipe-delimited TOON format. Literal newlines, pipes and
|
|
36
|
+
* backslashes would otherwise break line-based parsing (a multi-line content turns
|
|
37
|
+
* each continuation line into a bogus entry), so they are stored as `\n`, `\|` and
|
|
38
|
+
* `\\` to keep the encoding reversible.
|
|
39
|
+
*/
|
|
40
|
+
export const escField = (s: string): string => {
|
|
41
|
+
let out = ""
|
|
42
|
+
for (let i = 0; i < s.length; i++) {
|
|
43
|
+
const c = s[i]
|
|
44
|
+
if (c === "\\") out += "\\\\"
|
|
45
|
+
else if (c === "\n") out += "\\n"
|
|
46
|
+
else if (c === "\r") {
|
|
47
|
+
out += "\\n"
|
|
48
|
+
if (s[i + 1] === "\n") i++
|
|
49
|
+
} else if (c === "|") out += "\\|"
|
|
50
|
+
else out += c
|
|
51
|
+
}
|
|
52
|
+
return out
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Inverse of `escField`: `\\n` -> newline, `\\|` -> pipe, `\\\\` -> backslash. */
|
|
56
|
+
export const unescField = (s: string): string => {
|
|
57
|
+
let out = ""
|
|
58
|
+
for (let i = 0; i < s.length; i++) {
|
|
59
|
+
const c = s[i]
|
|
60
|
+
if (c === "\\" && i + 1 < s.length) {
|
|
61
|
+
const n = s[i + 1]
|
|
62
|
+
if (n === "n") {
|
|
63
|
+
out += "\n"
|
|
64
|
+
i++
|
|
65
|
+
} else if (n === "|") {
|
|
66
|
+
out += "|"
|
|
67
|
+
i++
|
|
68
|
+
} else if (n === "\\") {
|
|
69
|
+
out += "\\"
|
|
70
|
+
i++
|
|
71
|
+
} else out += c
|
|
72
|
+
} else out += c
|
|
73
|
+
}
|
|
74
|
+
return out
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Split a stored TOON line into unescaped fields (`\|` is not a delimiter). */
|
|
78
|
+
export const parseToonLine = (line: string): string[] => {
|
|
79
|
+
const parts: string[] = []
|
|
80
|
+
let cur = ""
|
|
81
|
+
let escaped = false
|
|
82
|
+
for (const c of line.trim()) {
|
|
83
|
+
if (escaped) {
|
|
84
|
+
cur += c
|
|
85
|
+
escaped = false
|
|
86
|
+
continue
|
|
87
|
+
}
|
|
88
|
+
if (c === "\\") {
|
|
89
|
+
cur += c
|
|
90
|
+
escaped = true
|
|
91
|
+
continue
|
|
92
|
+
}
|
|
93
|
+
if (c === "|") {
|
|
94
|
+
parts.push(unescField(cur))
|
|
95
|
+
cur = ""
|
|
96
|
+
continue
|
|
97
|
+
}
|
|
98
|
+
cur += c
|
|
99
|
+
}
|
|
100
|
+
if (escaped) cur += "\\"
|
|
101
|
+
parts.push(unescField(cur))
|
|
102
|
+
return parts
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Serialize raw fields into a stored TOON line (escapes newlines, pipes, backslashes). */
|
|
106
|
+
export const toToonLine = (parts: string[]): string =>
|
|
107
|
+
` ${parts.map(escField).join("|")}`
|
|
108
|
+
|
|
33
109
|
/** Check if an entry is marked as private (excluded from context injection). */
|
|
34
110
|
export const isPrivate = (e: { tags: string[] }): boolean =>
|
|
35
111
|
e.tags.some((t) => t.toLowerCase() === "private")
|
package/src/mcp/archive.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { readMemory, writeMemory, safeWrite } from "./memory-io"
|
|
|
4
4
|
import { ensureMemoryFile, MEMORY_FILE, MEMORY_DIR, getMaxEntries, ARCHIVE_DAYS, ARCHIVE_FILE } from "./config"
|
|
5
5
|
import { isExpired } from "./entries"
|
|
6
6
|
import { entryScoreForLine } from "./scoring"
|
|
7
|
+
import { parseToonLine } from "../lib/utils"
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
10
|
* Archive entries older than ARCHIVE_DAYS or with expired TTL.
|
|
@@ -26,7 +27,7 @@ export function archiveOldEntries(opts: { trimToMax?: boolean } = {}): { archive
|
|
|
26
27
|
const toArchive = new Set<number>()
|
|
27
28
|
|
|
28
29
|
entryLines.forEach((line, idx) => {
|
|
29
|
-
const parts = line
|
|
30
|
+
const parts = parseToonLine(line)
|
|
30
31
|
if (parts.length < 7) return
|
|
31
32
|
const date = parts[6]
|
|
32
33
|
const ttl = parts[7] || ""
|
|
@@ -108,7 +109,7 @@ export function pruneExpiredEntries(): number {
|
|
|
108
109
|
|
|
109
110
|
const entryLines = lines.slice(headerIdx + 1).filter((l) => l.trim().length > 0 && !l.startsWith(" summaries:"))
|
|
110
111
|
const kept = entryLines.filter((l) => {
|
|
111
|
-
const parts = l
|
|
112
|
+
const parts = parseToonLine(l)
|
|
112
113
|
if (parts.length < 8) return true
|
|
113
114
|
const ttl = parts[7] || ""
|
|
114
115
|
if (ttl && isExpired(ttl)) {
|
package/src/mcp/consolidation.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { readMemory, writeMemory } from "./memory-io"
|
|
2
2
|
import { mergeEntries } from "../lib/quality"
|
|
3
|
+
import { parseToonLine } from "../lib/utils"
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* Jaccard similarity between two content strings (0..1).
|
|
@@ -41,11 +42,11 @@ export function consolidateEntries(): { removed: number; kept: number; duplicate
|
|
|
41
42
|
|
|
42
43
|
for (let i = 0; i < entryLines.length; i++) {
|
|
43
44
|
const line = entryLines[i].trim()
|
|
44
|
-
const parts = line
|
|
45
|
+
const parts = parseToonLine(line)
|
|
45
46
|
if (parts.length < 3) continue
|
|
46
47
|
const key = parts[2]
|
|
47
48
|
const existingIdx = keyOrder.findIndex((idx) => {
|
|
48
|
-
const ep = entryLines[idx]
|
|
49
|
+
const ep = parseToonLine(entryLines[idx])
|
|
49
50
|
return ep[2] === key
|
|
50
51
|
})
|
|
51
52
|
if (existingIdx !== -1) {
|
|
@@ -65,7 +66,7 @@ export function consolidateEntries(): { removed: number; kept: number; duplicate
|
|
|
65
66
|
|
|
66
67
|
for (const idx of keyOrder) {
|
|
67
68
|
const line = byKey.get(idx)!
|
|
68
|
-
const parts = line
|
|
69
|
+
const parts = parseToonLine(line)
|
|
69
70
|
if (parts.length < 3) {
|
|
70
71
|
order.push(line)
|
|
71
72
|
continue
|
|
@@ -82,7 +83,7 @@ export function consolidateEntries(): { removed: number; kept: number; duplicate
|
|
|
82
83
|
// Check for near-duplicate (same category, similar content)
|
|
83
84
|
let merged = false
|
|
84
85
|
for (const [seenNorm, existing] of contentSeen) {
|
|
85
|
-
const existingParts = existing.line
|
|
86
|
+
const existingParts = parseToonLine(existing.line)
|
|
86
87
|
if (existingParts[1] !== parts[1]) continue // different category
|
|
87
88
|
const sim = contentSimilarity(content, existingParts[3] || "")
|
|
88
89
|
if (sim >= NEAR_DUP_THRESHOLD) {
|
package/src/mcp/observations.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { readFileSync, existsSync, writeFileSync } from "fs"
|
|
2
2
|
import { OBSERVATIONS_FILE } from "./config"
|
|
3
|
+
import { parseToonLine } from "../lib/utils"
|
|
3
4
|
|
|
4
5
|
/** Maximum observations to keep in the log file */
|
|
5
6
|
const MAX_OBSERVATIONS = 500
|
|
@@ -13,7 +14,7 @@ export function readObservations(): Array<{ ts: string; session: string; agent:
|
|
|
13
14
|
.split("\n")
|
|
14
15
|
.filter((l) => l.startsWith(" ") && l.includes("|"))
|
|
15
16
|
.map((l) => {
|
|
16
|
-
const p = l
|
|
17
|
+
const p = parseToonLine(l)
|
|
17
18
|
return { ts: p[0] || "", session: p[1] || "", agent: p[2] || "", branch: p[3] || "", tool: p[4] || "", file: p[6] || "", summary: p[7] || "" }
|
|
18
19
|
})
|
|
19
20
|
}
|
package/src/mcp/resources.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { registerAppResource } from "@modelcontextprotocol/ext-apps/server"
|
|
|
3
3
|
import { createUIResource } from "@mcp-ui/server"
|
|
4
4
|
import { readMemory } from "./memory-io"
|
|
5
5
|
import { isExpired } from "./entries"
|
|
6
|
+
import { parseToonLine } from "../lib/utils"
|
|
6
7
|
import { generateSystemPrimer } from "../lib/quality"
|
|
7
8
|
import { buildViewerData } from "../viewer/data"
|
|
8
9
|
import { generateHtml } from "../viewer/html"
|
|
@@ -29,7 +30,7 @@ export function registerResources(server: McpServer): void {
|
|
|
29
30
|
const data = readMemory()
|
|
30
31
|
const lines = data.split("\n").filter((l) => l.startsWith(" ") && l.includes("|") && !l.startsWith(" summaries:"))
|
|
31
32
|
const entries = lines.map((l) => {
|
|
32
|
-
const parts = l
|
|
33
|
+
const parts = parseToonLine(l)
|
|
33
34
|
return { category: parts[1] || "unknown", ttl: parts[7] || "" }
|
|
34
35
|
})
|
|
35
36
|
const byCategory: Record<string, number> = {}
|
package/src/mcp/scoring.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { readMemory, writeMemory } from "./memory-io"
|
|
2
|
-
import { normalize, importance } from "../lib/utils"
|
|
2
|
+
import { normalize, importance, parseToonLine, toToonLine } from "../lib/utils"
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Importance score for an entry: blends recency and access frequency.
|
|
@@ -10,7 +10,7 @@ export function entryScore(dateStr: string, accessed: number): number {
|
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
export function entryScoreForLine(line: string): number {
|
|
13
|
-
const parts = line
|
|
13
|
+
const parts = parseToonLine(line)
|
|
14
14
|
const date = parts[6] || new Date().toISOString().split("T")[0]
|
|
15
15
|
const accessed = parts.length > 8 ? parseInt(parts[8]) || 0 : 0
|
|
16
16
|
return entryScore(date, accessed)
|
|
@@ -28,7 +28,7 @@ export function findRelatedEntries(text: string, excludeKey: string = "", limit:
|
|
|
28
28
|
const scored = lines
|
|
29
29
|
.map((line) => {
|
|
30
30
|
const trimmed = line.trim()
|
|
31
|
-
const parts = trimmed
|
|
31
|
+
const parts = parseToonLine(trimmed)
|
|
32
32
|
if (parts.length < 7) return null
|
|
33
33
|
const [id, cat, key, content, file, tags, date] = parts
|
|
34
34
|
if (key === excludeKey) return null
|
|
@@ -65,14 +65,14 @@ export function bumpAccessed(ids: string[]): void {
|
|
|
65
65
|
const line = lines[i]
|
|
66
66
|
if (!line.startsWith(" ") || !line.includes("|")) continue
|
|
67
67
|
if (line.startsWith(" summaries:")) break
|
|
68
|
-
const parts = line
|
|
68
|
+
const parts = parseToonLine(line)
|
|
69
69
|
if (idSet.has(parts[0])) {
|
|
70
70
|
const accessed = parts.length > 8 ? (parseInt(parts[8]) || 0) + 1 : 1
|
|
71
71
|
parts[8] = String(accessed)
|
|
72
72
|
// Ensure we have enough fields for lastAccessed (field 12)
|
|
73
73
|
while (parts.length < 13) parts.push("")
|
|
74
74
|
parts[12] = now
|
|
75
|
-
lines[i] =
|
|
75
|
+
lines[i] = toToonLine(parts)
|
|
76
76
|
}
|
|
77
77
|
}
|
|
78
78
|
writeMemory(lines.join("\n"))
|
package/src/mcp/session-store.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { readFileSync, existsSync, writeFileSync, renameSync } from "fs"
|
|
|
2
2
|
import { join } from "path"
|
|
3
3
|
import { OBSERVATIONS_FILE, MEMORY_FILE, MEMORY_DIR } from "./config"
|
|
4
4
|
import { withLockSync, atomicWrite, readUnderLock } from "../lib/lock"
|
|
5
|
+
import { parseToonLine, toToonLine } from "../lib/utils"
|
|
5
6
|
import { loadConfig, getKey } from "./config"
|
|
6
7
|
import { encrypt, decrypt } from "./crypto"
|
|
7
8
|
|
|
@@ -41,7 +42,7 @@ export function readSessionEntries(sessionId?: string): SessionEntry[] {
|
|
|
41
42
|
.split("\n")
|
|
42
43
|
.filter((l) => l.startsWith(" ") && l.includes("|"))
|
|
43
44
|
.map((l) => {
|
|
44
|
-
const p = l
|
|
45
|
+
const p = parseToonLine(l)
|
|
45
46
|
return {
|
|
46
47
|
ts: p[0] || "",
|
|
47
48
|
session: p[1] || "",
|
|
@@ -76,11 +77,11 @@ export function writeSessionEntry(entry: Omit<SessionEntry, "accessed" | "lastAc
|
|
|
76
77
|
headerIdx = lines.length - 1
|
|
77
78
|
}
|
|
78
79
|
|
|
79
|
-
const entryLine =
|
|
80
|
+
const entryLine = toToonLine([entry.ts, entry.session, entry.agent, entry.branch, entry.tool, "", entry.file, entry.summary, "0", ""])
|
|
80
81
|
|
|
81
82
|
const match = lines[headerIdx].match(/\[(\d+)\|/)
|
|
82
83
|
const count = match ? parseInt(match[1]) : 0
|
|
83
|
-
lines.splice(headerIdx + 1, 0,
|
|
84
|
+
lines.splice(headerIdx + 1, 0, entryLine)
|
|
84
85
|
lines[headerIdx] = lines[headerIdx].replace(/\[\d+\|/, `[${count + 1}|`)
|
|
85
86
|
|
|
86
87
|
atomicWrite(filePath, lines.join("\n"))
|
|
@@ -113,11 +114,11 @@ export function promoteSessionEntries(
|
|
|
113
114
|
const date = entry.ts.split("T")[0] || new Date().toISOString().split("T")[0]
|
|
114
115
|
const key = entry.summary.slice(0, 50).replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "") || `session-${id}`
|
|
115
116
|
|
|
116
|
-
const memoryEntry =
|
|
117
|
+
const memoryEntry = toToonLine([id, category, key, entry.summary, entry.file, "session-promoted", date, "", "0", "", "0.5", "1.0", ""])
|
|
117
118
|
|
|
118
119
|
const match = memoryLines[memoryHeaderIdx].match(/\[(\d+)\|/)
|
|
119
120
|
const count = match ? parseInt(match[1]) : 0
|
|
120
|
-
memoryLines.splice(memoryHeaderIdx + 1, 0,
|
|
121
|
+
memoryLines.splice(memoryHeaderIdx + 1, 0, memoryEntry)
|
|
121
122
|
memoryLines[memoryHeaderIdx] = memoryLines[memoryHeaderIdx].replace(/\[\d+\|/, `[${count + 1}|`)
|
|
122
123
|
promoted++
|
|
123
124
|
}
|
|
@@ -176,14 +177,14 @@ export function bumpSessionAccess(summaries: string[]): void {
|
|
|
176
177
|
for (let i = headerIdx + 1; i < lines.length; i++) {
|
|
177
178
|
const line = lines[i]
|
|
178
179
|
if (!line.startsWith(" ") || !line.includes("|")) continue
|
|
179
|
-
const parts = line
|
|
180
|
+
const parts = parseToonLine(line)
|
|
180
181
|
const summary = parts[7] || ""
|
|
181
182
|
if (summarySet.has(summary)) {
|
|
182
183
|
const accessed = parts.length > 8 ? (parseInt(parts[8]) || 0) + 1 : 1
|
|
183
184
|
parts[8] = String(accessed)
|
|
184
185
|
while (parts.length < 10) parts.push("")
|
|
185
186
|
parts[9] = now
|
|
186
|
-
lines[i] =
|
|
187
|
+
lines[i] = toToonLine(parts)
|
|
187
188
|
}
|
|
188
189
|
}
|
|
189
190
|
|
|
@@ -219,12 +220,12 @@ function migrateObservationsToSession(): void {
|
|
|
219
220
|
const newLines = [`session[0|]{ts|session|agent|branch|tool||file|summary|accessed|lastAccessed}:`]
|
|
220
221
|
for (const line of lines.slice(headerIdx + 1)) {
|
|
221
222
|
if (!line.startsWith(" ") || !line.includes("|")) continue
|
|
222
|
-
const parts = line
|
|
223
|
+
const parts = parseToonLine(line)
|
|
223
224
|
// Old format: ts|session|agent|branch|tool||file|summary
|
|
224
225
|
// New format: ts|session|agent|branch|tool||file|summary|accessed|lastAccessed
|
|
225
226
|
while (parts.length < 8) parts.push("")
|
|
226
227
|
parts.push("0", "") // Add accessed=0 and lastAccessed=""
|
|
227
|
-
newLines.push(
|
|
228
|
+
newLines.push(toToonLine(parts))
|
|
228
229
|
}
|
|
229
230
|
|
|
230
231
|
const sessionFile = join(MEMORY_DIR, "session.toon")
|