toon-memory 2.2.0 → 2.4.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 +88 -16
- package/bin/toon-memory.js +40 -0
- package/dist/cli/setup.js +461 -195
- package/mcp/server.js +168 -41
- package/package.json +3 -2
- package/skills/toon-memory.md +5 -5
- package/src/bin/toon-memory.ts +44 -0
- package/src/cli/setup.ts +318 -63
- package/src/lib/graph.ts +193 -39
- package/src/lib/vocab.ts +100 -0
- package/src/mcp/server.ts +56 -10
- package/src/memory.ts +0 -237
package/src/memory.ts
DELETED
|
@@ -1,237 +0,0 @@
|
|
|
1
|
-
import { tool } from "@opencode-ai/plugin"
|
|
2
|
-
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs"
|
|
3
|
-
import { dirname, join } from "path"
|
|
4
|
-
import { fileURLToPath } from "url"
|
|
5
|
-
import { randomBytes } from "crypto"
|
|
6
|
-
|
|
7
|
-
const __dirname = dirname(fileURLToPath(import.meta.url))
|
|
8
|
-
const MEMORY_DIR = join(__dirname, "..", "memory")
|
|
9
|
-
const MEMORY_FILE = join(MEMORY_DIR, "data.toon")
|
|
10
|
-
|
|
11
|
-
function ensureMemoryFile() {
|
|
12
|
-
if (!existsSync(MEMORY_DIR)) mkdirSync(MEMORY_DIR, { recursive: true })
|
|
13
|
-
if (!existsSync(MEMORY_FILE)) {
|
|
14
|
-
writeFileSync(MEMORY_FILE, "version: 1\nentries[0|]{id|category|key|content|file|tags|date|ttl}:\n")
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
function readMemory() {
|
|
19
|
-
ensureMemoryFile()
|
|
20
|
-
return readFileSync(MEMORY_FILE, "utf-8")
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
function writeMemory(content: string) {
|
|
24
|
-
ensureMemoryFile()
|
|
25
|
-
writeFileSync(MEMORY_FILE, content)
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function generateId(): string {
|
|
29
|
-
return randomBytes(4).toString("hex")
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export const memoryRemember = tool({
|
|
33
|
-
description: "Guarda un hecho en la memoria persistente del proyecto (decisiones, patrones, bugs, conocimiento). Se recuerda entre sesiones.",
|
|
34
|
-
args: {
|
|
35
|
-
category: tool.schema.enum({
|
|
36
|
-
description: "Categoría del hecho",
|
|
37
|
-
options: ["decision", "pattern", "bug", "knowledge"],
|
|
38
|
-
}),
|
|
39
|
-
key: tool.schema.string({
|
|
40
|
-
description: "Título corto en kebab-case (ej: risk-engine-prioridad)",
|
|
41
|
-
}),
|
|
42
|
-
content: tool.schema.string({
|
|
43
|
-
description: "Contenido detallado del hecho",
|
|
44
|
-
}),
|
|
45
|
-
file: tool.schema.string({
|
|
46
|
-
description: "Archivo o línea relacionada (ej: spec.md:145)",
|
|
47
|
-
}),
|
|
48
|
-
tags: tool.schema.string({
|
|
49
|
-
description: "Tags separados por punto y coma (ej: risk;spec)",
|
|
50
|
-
}),
|
|
51
|
-
},
|
|
52
|
-
async execute(args) {
|
|
53
|
-
const data = readMemory()
|
|
54
|
-
const id = generateId()
|
|
55
|
-
const date = new Date().toISOString().split("T")[0]
|
|
56
|
-
const lines = data.split("\n")
|
|
57
|
-
|
|
58
|
-
let headerIdx = lines.findIndex((l) => l.startsWith("entries["))
|
|
59
|
-
if (headerIdx === -1) {
|
|
60
|
-
lines.push(`entries[0|]{id|category|key|content|file|tags|date}:`)
|
|
61
|
-
headerIdx = lines.length - 1
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
const match = lines[headerIdx].match(/entries\[(\d+)\|/)
|
|
65
|
-
const count = match ? parseInt(match[1]) : 0
|
|
66
|
-
const newEntry = `${id}|${args.category}|${args.key}|${args.content}|${args.file || ""}|${args.tags || ""}|${date}`
|
|
67
|
-
|
|
68
|
-
lines.splice(headerIdx + 1, 0, ` ${newEntry}`)
|
|
69
|
-
lines[headerIdx] = lines[headerIdx].replace(/entries\[\d+\|/, `[${count + 1}|`)
|
|
70
|
-
|
|
71
|
-
writeMemory(lines.join("\n"))
|
|
72
|
-
return `🧠 Guardado: ${args.category}/${args.key} (${id})\n${args.content}`
|
|
73
|
-
},
|
|
74
|
-
})
|
|
75
|
-
|
|
76
|
-
export const memoryRecall = tool({
|
|
77
|
-
description: "Busca en la memoria persistente del proyecto. Devuelve entradas relevantes por categoría, tag, o texto libre. Usar ANTES de leer archivos para evitar re-investigar.",
|
|
78
|
-
args: {
|
|
79
|
-
query: tool.schema.string({
|
|
80
|
-
description: "Texto a buscar (busca en key, content, tags)",
|
|
81
|
-
}),
|
|
82
|
-
category: tool.schema.string({
|
|
83
|
-
description: "Filtrar por categoría (decision|pattern|bug|knowledge). Vacío = todos.",
|
|
84
|
-
}),
|
|
85
|
-
},
|
|
86
|
-
async execute(args) {
|
|
87
|
-
const data = readMemory()
|
|
88
|
-
const lines = data.split("\n").filter((l) => l.startsWith(" ") && l.includes("|"))
|
|
89
|
-
const queryLower = args.query.toLowerCase()
|
|
90
|
-
|
|
91
|
-
const results = lines
|
|
92
|
-
.map((line) => {
|
|
93
|
-
const trimmed = line.trim()
|
|
94
|
-
const parts = trimmed.split("|")
|
|
95
|
-
if (parts.length < 7) return null
|
|
96
|
-
const [id, cat, key, content, file, tags, date] = parts
|
|
97
|
-
if (args.category && cat !== args.category) return null
|
|
98
|
-
const searchStr = `${id} ${cat} ${key} ${content} ${file} ${tags}`.toLowerCase()
|
|
99
|
-
if (!searchStr.includes(queryLower)) return null
|
|
100
|
-
return { id, cat, key, content, file, tags, date }
|
|
101
|
-
})
|
|
102
|
-
.filter(Boolean)
|
|
103
|
-
|
|
104
|
-
if (results.length === 0) {
|
|
105
|
-
return `No se encontraron resultados para "${args.query}"`
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
return results
|
|
109
|
-
.map((r) => `[${r!.cat}] ${r!.key} (${r!.id})\n ${r!.content}\n File: ${r!.file} | Tags: ${r!.tags} | Date: ${r!.date}`)
|
|
110
|
-
.join("\n\n")
|
|
111
|
-
},
|
|
112
|
-
})
|
|
113
|
-
|
|
114
|
-
export const memoryForget = tool({
|
|
115
|
-
description: "Elimina una entrada de la memoria por su key o id.",
|
|
116
|
-
args: {
|
|
117
|
-
key: tool.schema.string({
|
|
118
|
-
description: "Key o id de la entrada a eliminar",
|
|
119
|
-
}),
|
|
120
|
-
},
|
|
121
|
-
async execute(args) {
|
|
122
|
-
const data = readMemory()
|
|
123
|
-
const lines = data.split("\n")
|
|
124
|
-
const headerIdx = lines.findIndex((l) => l.startsWith("entries["))
|
|
125
|
-
|
|
126
|
-
if (headerIdx === -1) {
|
|
127
|
-
return "No hay entradas en memoria"
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
const entryLines = lines.slice(headerIdx + 1).filter((l) => l.trim().length > 0)
|
|
131
|
-
const filtered = entryLines.filter((l) => {
|
|
132
|
-
const parts = l.trim().split("|")
|
|
133
|
-
return parts[0] !== args.key && parts[2] !== args.key
|
|
134
|
-
})
|
|
135
|
-
|
|
136
|
-
const removed = entryLines.length - filtered.length
|
|
137
|
-
const match = lines[headerIdx].match(/entries\[(\d+)\|/)
|
|
138
|
-
const count = match ? parseInt(match[1]) : 0
|
|
139
|
-
lines[headerIdx] = lines[headerIdx].replace(/entries\[\d+\|/, `[${count - removed}|`)
|
|
140
|
-
lines.splice(headerIdx + 1, entryLines.length, ...filtered.map((l) => ` ${l.trim()}`))
|
|
141
|
-
|
|
142
|
-
writeMemory(lines.join("\n"))
|
|
143
|
-
return `"${args.key}" eliminado. Quedan ${count - removed} entradas.`
|
|
144
|
-
},
|
|
145
|
-
})
|
|
146
|
-
|
|
147
|
-
export const memoryStats = tool({
|
|
148
|
-
description: "Muestra estadísticas de la memoria: total entradas, por categoría, y resúmenes de archivos.",
|
|
149
|
-
args: {},
|
|
150
|
-
async execute() {
|
|
151
|
-
const data = readMemory()
|
|
152
|
-
const lines = data.split("\n").filter((l) => l.startsWith(" ") && l.includes("|"))
|
|
153
|
-
const entries = lines.map((l) => {
|
|
154
|
-
const parts = l.trim().split("|")
|
|
155
|
-
return { category: parts[1] || "unknown" }
|
|
156
|
-
})
|
|
157
|
-
|
|
158
|
-
const byCategory: Record<string, number> = {}
|
|
159
|
-
for (const e of entries) {
|
|
160
|
-
byCategory[e.category] = (byCategory[e.category] || 0) + 1
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
const summaryLines = data.split("\n").filter((l) => l.includes(":") && !l.startsWith(" ") && !l.startsWith("version") && !l.startsWith("entries"))
|
|
164
|
-
const stats = [
|
|
165
|
-
`Entradas totales: ${entries.length}`,
|
|
166
|
-
`Resúmenes de archivos: ${summaryLines.length}`,
|
|
167
|
-
"",
|
|
168
|
-
"Por categoría:",
|
|
169
|
-
...Object.entries(byCategory).map(([k, v]) => ` ${k}: ${v}`),
|
|
170
|
-
"",
|
|
171
|
-
`Últimas 5 entradas:`,
|
|
172
|
-
...lines.slice(-5).map((l) => {
|
|
173
|
-
const parts = l.trim().split("|")
|
|
174
|
-
return ` [${parts[1]}] ${parts[2]} (${parts[0]})`
|
|
175
|
-
}),
|
|
176
|
-
]
|
|
177
|
-
|
|
178
|
-
return stats.join("\n")
|
|
179
|
-
},
|
|
180
|
-
})
|
|
181
|
-
|
|
182
|
-
export const memorySummary = tool({
|
|
183
|
-
description: "Guarda o recupera un resumen de un archivo grande. El agente puede leer el resumen en vez del archivo completo para ahorrar tokens.",
|
|
184
|
-
args: {
|
|
185
|
-
action: tool.schema.enum({
|
|
186
|
-
description: "get para leer, set para guardar",
|
|
187
|
-
options: ["get", "set"],
|
|
188
|
-
}),
|
|
189
|
-
file: tool.schema.string({
|
|
190
|
-
description: "Ruta del archivo",
|
|
191
|
-
}),
|
|
192
|
-
summary: tool.schema.string({
|
|
193
|
-
description: "Resumen del archivo (solo para set)",
|
|
194
|
-
}),
|
|
195
|
-
},
|
|
196
|
-
async execute(args) {
|
|
197
|
-
const data = readMemory()
|
|
198
|
-
|
|
199
|
-
if (args.action === "get") {
|
|
200
|
-
const lines = data.split("\n")
|
|
201
|
-
const summaryIdx = lines.findIndex((l) => l.trim().startsWith("summaries:"))
|
|
202
|
-
if (summaryIdx === -1) {
|
|
203
|
-
return `No hay resúmenes guardados para "${args.file}"`
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
const summaryLines = lines.slice(summaryIdx + 1).filter((l) => l.includes(":"))
|
|
207
|
-
const match = summaryLines.find((l) => l.startsWith(` ${args.file}:`))
|
|
208
|
-
if (!match) {
|
|
209
|
-
return `No hay resumen para "${args.file}"`
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
const summaryText = match.replace(` ${args.file}: `, "")
|
|
213
|
-
return summaryText
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
const lines = data.split("\n")
|
|
217
|
-
let summaryIdx = lines.findIndex((l) => l.trim().startsWith("summaries:"))
|
|
218
|
-
|
|
219
|
-
if (summaryIdx === -1) {
|
|
220
|
-
lines.push("", "summaries:")
|
|
221
|
-
summaryIdx = lines.length - 1
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
const summaryLines = lines.slice(summaryIdx + 1).filter((l) => l.includes(":"))
|
|
225
|
-
const existingIdx = summaryLines.findIndex((l) => l.startsWith(` ${args.file}:`))
|
|
226
|
-
|
|
227
|
-
if (existingIdx !== -1) {
|
|
228
|
-
summaryLines[existingIdx] = ` ${args.file}: ${args.summary}`
|
|
229
|
-
} else {
|
|
230
|
-
summaryLines.push(` ${args.file}: ${args.summary}`)
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
lines.splice(summaryIdx + 1, lines.length - summaryIdx - 1, ...summaryLines)
|
|
234
|
-
writeMemory(lines.join("\n"))
|
|
235
|
-
return `📝 Resumen guardado para ${args.file}`
|
|
236
|
-
},
|
|
237
|
-
})
|