toon-memory 2.5.2 → 2.8.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.md +128 -6
- package/dist/cli/setup.js +463 -425
- package/mcp/server.js +2112 -1156
- package/package.json +4 -6
- package/skills/toon-memory.md +1 -0
- package/src/cli/agents.ts +184 -0
- package/src/cli/capture-toggle.ts +29 -0
- package/src/cli/commands.ts +532 -0
- package/src/cli/config-writer.ts +133 -0
- package/src/cli/constants.ts +56 -0
- package/src/cli/hooks.ts +202 -0
- package/src/cli/instructions.ts +35 -0
- package/src/cli/memory.ts +104 -0
- package/src/cli/opencode-plugin.ts +84 -0
- package/src/cli/setup.ts +5 -1596
- package/src/cli/types.ts +29 -0
- package/src/cli/watch.ts +169 -0
- package/src/lib/code-search.ts +192 -0
- package/src/lib/context.ts +637 -0
- package/src/lib/git.ts +262 -0
- package/src/lib/graph.ts +2 -20
- package/src/lib/project-scan.ts +224 -0
- package/src/lib/quality.ts +1 -20
- package/src/lib/utils.ts +25 -0
- package/src/mcp/archive.ts +129 -0
- package/src/mcp/config.ts +88 -0
- package/src/mcp/consolidation.ts +70 -0
- package/src/mcp/crypto.ts +41 -0
- package/src/mcp/entries.ts +110 -0
- package/src/mcp/memory-io.ts +53 -0
- package/src/mcp/observations.ts +16 -0
- package/src/mcp/resources.ts +57 -0
- package/src/mcp/scoring.ts +77 -0
- package/src/mcp/server.ts +5 -1482
- package/src/mcp/tools.ts +835 -0
package/src/cli/types.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/** Config format: "json" | "toml" | "jsonc" | "none" (instructions only) */
|
|
2
|
+
export type AgentFormat = "json" | "toml" | "jsonc" | "none"
|
|
3
|
+
|
|
4
|
+
/** Supported AI coding agent configuration */
|
|
5
|
+
export interface Agent {
|
|
6
|
+
name: string
|
|
7
|
+
global?: string
|
|
8
|
+
local?: string
|
|
9
|
+
mcpKey: string
|
|
10
|
+
format: AgentFormat
|
|
11
|
+
needsHooks: boolean
|
|
12
|
+
needsInstructions: boolean
|
|
13
|
+
instructionFile?: string
|
|
14
|
+
/** Claude Code-style hook events for activity capture (e.g. PostToolUse, Stop) */
|
|
15
|
+
captureJson?: string[]
|
|
16
|
+
/** Codex TOML hook events for activity capture (e.g. post_tool_use, stop) */
|
|
17
|
+
captureToml?: string[]
|
|
18
|
+
/** OpenCode: install the toon-memory plugin instead of a `hooks` config key */
|
|
19
|
+
needsPlugin?: boolean
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Watch mode options */
|
|
23
|
+
export interface WatchOptions {
|
|
24
|
+
interval: number
|
|
25
|
+
maxBackups: number
|
|
26
|
+
compress: boolean
|
|
27
|
+
logFile: boolean
|
|
28
|
+
logPath: string
|
|
29
|
+
}
|
package/src/cli/watch.ts
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync, readdirSync, statSync, unlinkSync } from "fs"
|
|
2
|
+
import { dirname, join } from "path"
|
|
3
|
+
import { gzipSync } from "zlib"
|
|
4
|
+
import type { WatchOptions } from "./types"
|
|
5
|
+
import { MEMORY_DIR, projectRoot } from "./constants"
|
|
6
|
+
|
|
7
|
+
/** Parse watch CLI arguments into WatchOptions */
|
|
8
|
+
export function parseWatchOptions(args: string[]): WatchOptions {
|
|
9
|
+
const opts: WatchOptions = {
|
|
10
|
+
interval: 5,
|
|
11
|
+
maxBackups: 10,
|
|
12
|
+
compress: false,
|
|
13
|
+
logFile: false,
|
|
14
|
+
logPath: ""
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
for (let i = 1; i < args.length; i++) {
|
|
18
|
+
const arg = args[i]
|
|
19
|
+
if (arg === "--compress" || arg === "-c") {
|
|
20
|
+
opts.compress = true
|
|
21
|
+
} else if (arg === "--log" || arg === "-l") {
|
|
22
|
+
opts.logFile = true
|
|
23
|
+
opts.logPath = args[++i] || join(MEMORY_DIR, "watch.log")
|
|
24
|
+
} else if (arg === "--max-backups" || arg === "-m") {
|
|
25
|
+
opts.maxBackups = parseInt(args[++i]) || 10
|
|
26
|
+
} else if (!arg.startsWith("-")) {
|
|
27
|
+
opts.interval = parseInt(arg) || 5
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return opts
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Write a line to the watch log file */
|
|
35
|
+
export function writeWatchLog(logPath: string, message: string): void {
|
|
36
|
+
if (!logPath) return
|
|
37
|
+
const timestamp = new Date().toISOString()
|
|
38
|
+
const logLine = `[${timestamp}] ${message}\n`
|
|
39
|
+
writeFileSync(logPath, logLine, { flag: "a" })
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Get list of backup files sorted by creation time (oldest first) */
|
|
43
|
+
export function getBackupFiles(backupDir: string): string[] {
|
|
44
|
+
if (!existsSync(backupDir)) return []
|
|
45
|
+
|
|
46
|
+
return readdirSync(backupDir)
|
|
47
|
+
.filter(f => f.startsWith("backup-") && (f.endsWith(".toon") || f.endsWith(".gz")))
|
|
48
|
+
.map(f => join(backupDir, f))
|
|
49
|
+
.sort((a, b) => statSync(a).mtimeMs - statSync(b).mtimeMs)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Remove oldest backups if we exceed maxBackups */
|
|
53
|
+
export function pruneBackups(backupDir: string, maxBackups: number): number {
|
|
54
|
+
if (maxBackups <= 0) return 0
|
|
55
|
+
|
|
56
|
+
const files = getBackupFiles(backupDir)
|
|
57
|
+
const excess = files.length - maxBackups
|
|
58
|
+
|
|
59
|
+
if (excess <= 0) return 0
|
|
60
|
+
|
|
61
|
+
for (let i = 0; i < excess; i++) {
|
|
62
|
+
unlinkSync(files[i])
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return excess
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Compress content with gzip */
|
|
69
|
+
export function compressData(data: string): Buffer {
|
|
70
|
+
return gzipSync(Buffer.from(data, "utf-8"))
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Simple hash for change detection (not cryptographic) */
|
|
74
|
+
function hashContent(content: string): string {
|
|
75
|
+
let hash = 0
|
|
76
|
+
for (let i = 0; i < content.length; i++) {
|
|
77
|
+
const char = content.charCodeAt(i)
|
|
78
|
+
hash = ((hash << 5) - hash) + char
|
|
79
|
+
hash = hash & hash
|
|
80
|
+
}
|
|
81
|
+
return hash.toString(36)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Watch mode - backup memory every N minutes
|
|
86
|
+
*/
|
|
87
|
+
export function watch(args: string[]): void {
|
|
88
|
+
console.log("\n🧠 toon-memory watch\n")
|
|
89
|
+
|
|
90
|
+
const memoryFile = join(MEMORY_DIR, "data.toon")
|
|
91
|
+
const backupDir = join(MEMORY_DIR, "backups")
|
|
92
|
+
|
|
93
|
+
if (!existsSync(memoryFile)) {
|
|
94
|
+
console.log("Memory not initialized. Run 'npx toon-memory init' first.\n")
|
|
95
|
+
return
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (!existsSync(backupDir)) mkdirSync(backupDir, { recursive: true })
|
|
99
|
+
|
|
100
|
+
const opts = parseWatchOptions(args)
|
|
101
|
+
|
|
102
|
+
if (opts.logFile) {
|
|
103
|
+
const logDir = dirname(opts.logPath)
|
|
104
|
+
if (!existsSync(logDir)) mkdirSync(logDir, { recursive: true })
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
console.log(`Watching memory file every ${opts.interval} minutes...`)
|
|
108
|
+
console.log(`Max backups: ${opts.maxBackups === 0 ? "unlimited" : opts.maxBackups}`)
|
|
109
|
+
console.log(`Compression: ${opts.compress ? "enabled" : "disabled"}`)
|
|
110
|
+
console.log(`Logging: ${opts.logFile ? `enabled (${opts.logPath})` : "disabled"}`)
|
|
111
|
+
console.log(`Press Ctrl+C to stop\n`)
|
|
112
|
+
|
|
113
|
+
let lastContent = readFileSync(memoryFile, "utf-8")
|
|
114
|
+
let lastHash = hashContent(lastContent)
|
|
115
|
+
let backupCount = 0
|
|
116
|
+
|
|
117
|
+
if (opts.logFile) writeWatchLog(opts.logPath, "Watch started")
|
|
118
|
+
|
|
119
|
+
const backup = () => {
|
|
120
|
+
try {
|
|
121
|
+
const currentContent = readFileSync(memoryFile, "utf-8")
|
|
122
|
+
const currentHash = hashContent(currentContent)
|
|
123
|
+
|
|
124
|
+
if (currentHash !== lastHash) {
|
|
125
|
+
const timestamp = new Date().toISOString().replace(/[:.]/g, "-")
|
|
126
|
+
const ext = opts.compress ? ".toon.gz" : ".toon"
|
|
127
|
+
const backupFile = join(backupDir, `backup-${timestamp}${ext}`)
|
|
128
|
+
|
|
129
|
+
if (opts.compress) {
|
|
130
|
+
writeFileSync(backupFile, compressData(currentContent))
|
|
131
|
+
} else {
|
|
132
|
+
writeFileSync(backupFile, currentContent)
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
backupCount++
|
|
136
|
+
console.log(`📦 Backup #${backupCount} created: ${timestamp}`)
|
|
137
|
+
if (opts.logFile) writeWatchLog(opts.logPath, `Backup #${backupCount}: ${timestamp}`)
|
|
138
|
+
|
|
139
|
+
lastContent = currentContent
|
|
140
|
+
lastHash = currentHash
|
|
141
|
+
|
|
142
|
+
const pruned = pruneBackups(backupDir, opts.maxBackups)
|
|
143
|
+
if (pruned > 0) {
|
|
144
|
+
console.log(`🗑️ Pruned ${pruned} old backup(s)`)
|
|
145
|
+
if (opts.logFile) writeWatchLog(opts.logPath, `Pruned ${pruned} old backup(s)`)
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
} catch (err) {
|
|
149
|
+
const msg = `Error creating backup: ${(err as Error).message}`
|
|
150
|
+
console.error(`❌ ${msg}`)
|
|
151
|
+
if (opts.logFile) writeWatchLog(opts.logPath, msg)
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
backup()
|
|
156
|
+
const interval = setInterval(backup, opts.interval * 60 * 1000)
|
|
157
|
+
|
|
158
|
+
process.on("SIGINT", () => {
|
|
159
|
+
clearInterval(interval)
|
|
160
|
+
console.log(`\n✅ Watch stopped. ${backupCount} backups created.\n`)
|
|
161
|
+
if (opts.logFile) writeWatchLog(opts.logPath, `Watch stopped. ${backupCount} backups created.`)
|
|
162
|
+
process.exit(0)
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
process.on("SIGTERM", () => {
|
|
166
|
+
clearInterval(interval)
|
|
167
|
+
process.exit(0)
|
|
168
|
+
})
|
|
169
|
+
}
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Simple code search utilities for toon-memory.
|
|
3
|
+
*
|
|
4
|
+
* Grep-like search across source files without external dependencies.
|
|
5
|
+
* Used by context tools to find related files, callers, and references.
|
|
6
|
+
* Zero LLM, no network, no AST parsing — pure string matching.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { existsSync, readFileSync, readdirSync, statSync } from "fs"
|
|
10
|
+
import { join, extname, relative } from "path"
|
|
11
|
+
|
|
12
|
+
const SOURCE_EXTS = new Set([
|
|
13
|
+
".ts", ".tsx", ".js", ".jsx", ".mjs", ".cjs",
|
|
14
|
+
".py", ".rb", ".go", ".rs", ".java", ".kt",
|
|
15
|
+
".swift", ".c", ".cpp", ".h", ".hpp", ".cs",
|
|
16
|
+
".vue", ".svelte", ".astro",
|
|
17
|
+
])
|
|
18
|
+
|
|
19
|
+
const SKIP_DIRS = new Set([
|
|
20
|
+
"node_modules", ".git", ".toon-memory", "dist", "build",
|
|
21
|
+
".next", ".nuxt", "coverage", "__pycache__", ".venv",
|
|
22
|
+
"vendor", ".cache", ".turbo",
|
|
23
|
+
])
|
|
24
|
+
|
|
25
|
+
export interface SearchResult {
|
|
26
|
+
file: string
|
|
27
|
+
line: number
|
|
28
|
+
content: string
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Find files matching a simple pattern (substring match on filename).
|
|
33
|
+
*/
|
|
34
|
+
export function findFilesByPattern(root: string, pattern: string): string[] {
|
|
35
|
+
const results: string[] = []
|
|
36
|
+
const pat = pattern.toLowerCase()
|
|
37
|
+
|
|
38
|
+
function walk(dir: string): void {
|
|
39
|
+
let entries: string[]
|
|
40
|
+
try {
|
|
41
|
+
entries = readdirSync(dir)
|
|
42
|
+
} catch {
|
|
43
|
+
return
|
|
44
|
+
}
|
|
45
|
+
for (const entry of entries) {
|
|
46
|
+
if (SKIP_DIRS.has(entry)) continue
|
|
47
|
+
const fullPath = join(dir, entry)
|
|
48
|
+
let isDir: boolean
|
|
49
|
+
try {
|
|
50
|
+
isDir = statSync(fullPath).isDirectory()
|
|
51
|
+
} catch {
|
|
52
|
+
continue
|
|
53
|
+
}
|
|
54
|
+
if (isDir) {
|
|
55
|
+
walk(fullPath)
|
|
56
|
+
} else if (entry.toLowerCase().includes(pat)) {
|
|
57
|
+
results.push(relative(root, fullPath))
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
walk(root)
|
|
63
|
+
return results.sort()
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Search source file contents for a query string (case-insensitive substring).
|
|
68
|
+
* Returns matching lines with file path and line number.
|
|
69
|
+
*/
|
|
70
|
+
export function searchCode(root: string, query: string, opts: { maxResults?: number; extensions?: string[] } = {}): SearchResult[] {
|
|
71
|
+
const results: SearchResult[] = []
|
|
72
|
+
const maxResults = opts.maxResults ?? 30
|
|
73
|
+
const extensions = opts.extensions
|
|
74
|
+
? new Set(opts.extensions)
|
|
75
|
+
: SOURCE_EXTS
|
|
76
|
+
const q = query.toLowerCase()
|
|
77
|
+
|
|
78
|
+
function walk(dir: string): void {
|
|
79
|
+
if (results.length >= maxResults) return
|
|
80
|
+
let entries: string[]
|
|
81
|
+
try {
|
|
82
|
+
entries = readdirSync(dir)
|
|
83
|
+
} catch {
|
|
84
|
+
return
|
|
85
|
+
}
|
|
86
|
+
for (const entry of entries) {
|
|
87
|
+
if (results.length >= maxResults) return
|
|
88
|
+
if (SKIP_DIRS.has(entry)) continue
|
|
89
|
+
const fullPath = join(dir, entry)
|
|
90
|
+
let isDir: boolean
|
|
91
|
+
try {
|
|
92
|
+
isDir = statSync(fullPath).isDirectory()
|
|
93
|
+
} catch {
|
|
94
|
+
continue
|
|
95
|
+
}
|
|
96
|
+
if (isDir) {
|
|
97
|
+
walk(fullPath)
|
|
98
|
+
} else {
|
|
99
|
+
const ext = extname(entry)
|
|
100
|
+
if (!extensions.has(ext)) continue
|
|
101
|
+
try {
|
|
102
|
+
const content = readFileSync(fullPath, "utf-8")
|
|
103
|
+
const lines = content.split("\n")
|
|
104
|
+
for (let i = 0; i < lines.length; i++) {
|
|
105
|
+
if (results.length >= maxResults) return
|
|
106
|
+
if (lines[i].toLowerCase().includes(q)) {
|
|
107
|
+
results.push({
|
|
108
|
+
file: relative(root, fullPath),
|
|
109
|
+
line: i + 1,
|
|
110
|
+
content: lines[i].trim(),
|
|
111
|
+
})
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
} catch {
|
|
115
|
+
// skip unreadable files
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
walk(root)
|
|
122
|
+
return results
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Find usages of a symbol (function/method/class name) across source files.
|
|
127
|
+
* Searches for the symbol as a word boundary (not substring of another word).
|
|
128
|
+
*/
|
|
129
|
+
export function findCallers(root: string, symbol: string): SearchResult[] {
|
|
130
|
+
const results: SearchResult[] = []
|
|
131
|
+
const maxResults = 30
|
|
132
|
+
|
|
133
|
+
// Build a word-boundary regex: matches the symbol but not when surrounded by word chars
|
|
134
|
+
const escaped = symbol.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")
|
|
135
|
+
const regex = new RegExp(`(?<![\\w$])${escaped}(?![\\w$])`, "g")
|
|
136
|
+
|
|
137
|
+
function walk(dir: string): void {
|
|
138
|
+
if (results.length >= maxResults) return
|
|
139
|
+
let entries: string[]
|
|
140
|
+
try {
|
|
141
|
+
entries = readdirSync(dir)
|
|
142
|
+
} catch {
|
|
143
|
+
return
|
|
144
|
+
}
|
|
145
|
+
for (const entry of entries) {
|
|
146
|
+
if (results.length >= maxResults) return
|
|
147
|
+
if (SKIP_DIRS.has(entry)) continue
|
|
148
|
+
const fullPath = join(dir, entry)
|
|
149
|
+
let isDir: boolean
|
|
150
|
+
try {
|
|
151
|
+
isDir = statSync(fullPath).isDirectory()
|
|
152
|
+
} catch {
|
|
153
|
+
continue
|
|
154
|
+
}
|
|
155
|
+
if (isDir) {
|
|
156
|
+
walk(fullPath)
|
|
157
|
+
} else {
|
|
158
|
+
const ext = extname(entry)
|
|
159
|
+
if (!SOURCE_EXTS.has(ext)) continue
|
|
160
|
+
try {
|
|
161
|
+
const content = readFileSync(fullPath, "utf-8")
|
|
162
|
+
const lines = content.split("\n")
|
|
163
|
+
for (let i = 0; i < lines.length; i++) {
|
|
164
|
+
if (results.length >= maxResults) return
|
|
165
|
+
regex.lastIndex = 0
|
|
166
|
+
if (regex.test(lines[i])) {
|
|
167
|
+
results.push({
|
|
168
|
+
file: relative(root, fullPath),
|
|
169
|
+
line: i + 1,
|
|
170
|
+
content: lines[i].trim(),
|
|
171
|
+
})
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
} catch {
|
|
175
|
+
// skip
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
walk(root)
|
|
182
|
+
return results
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Find test files related to a given pattern (e.g., a module name).
|
|
187
|
+
*/
|
|
188
|
+
export function findTestFiles(root: string, pattern: string): string[] {
|
|
189
|
+
const pat = pattern.toLowerCase()
|
|
190
|
+
return findFilesByPattern(root, ".test.").concat(findFilesByPattern(root, ".spec."))
|
|
191
|
+
.filter((f) => f.toLowerCase().includes(pat) || pat.includes(f.split("/").pop()?.split(".")[0] || ""))
|
|
192
|
+
}
|