toon-memory 3.7.0 → 4.1.1

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.
@@ -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.trim().split("|")
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.split("|")
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.trim().split("|")
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] = ` ${parts.join("|")}`
75
+ lines[i] = toToonLine(parts)
76
76
  }
77
77
  }
78
78
  writeMemory(lines.join("\n"))
@@ -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.trim().split("|")
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 = `${entry.ts}|${entry.session}|${entry.agent}|${entry.branch}|${entry.tool}||${entry.file}|${entry.summary}|0|`
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, ` ${entryLine}`)
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 = `${id}|${category}|${key}|${entry.summary}|${entry.file}|session-promoted|${date}||0||0.5|1.0|`
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, ` ${memoryEntry}`)
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.trim().split("|")
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] = ` ${parts.join("|")}`
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.trim().split("|")
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(` ${parts.join("|")}`)
228
+ newLines.push(toToonLine(parts))
228
229
  }
229
230
 
230
231
  const sessionFile = join(MEMORY_DIR, "session.toon")