toon-memory 4.3.4 → 4.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.de.md +2 -2
- package/README.es.md +2 -2
- package/README.fr.md +2 -2
- package/README.ja.md +2 -2
- package/README.ko.md +2 -2
- package/README.md +59 -13
- package/README.pt-br.md +2 -2
- package/README.zh.md +2 -2
- package/bin/toon-memory.js +4 -0
- package/dist/cli/capture.js +658 -26
- package/dist/cli/setup.js +1813 -143
- package/mcp/server.js +1391 -380
- package/package.json +6 -3
- package/src/bin/toon-memory.ts +4 -0
- package/src/cli/ambient.ts +117 -0
- package/src/cli/capture.ts +5 -2
- package/src/cli/commands.ts +3 -1
- package/src/cli/constants.ts +19 -11
- package/src/cli/dense.ts +50 -0
- package/src/cli/instructions.ts +2 -2
- package/src/cli/opencode-plugin.ts +100 -95
- package/src/cli/setup.ts +51 -4
- package/src/cli/sleep.ts +45 -0
- package/src/lib/activation.ts +32 -0
- package/src/lib/compiler.ts +178 -0
- package/src/lib/dense.ts +123 -0
- package/src/lib/graph.ts +123 -36
- package/src/lib/handoff.ts +144 -0
- package/src/lib/ngrams.ts +68 -0
- package/src/lib/promoter.ts +140 -0
- package/src/lib/quality.ts +25 -10
- package/src/lib/utils.ts +17 -7
- package/src/mcp/args.ts +46 -0
- package/src/mcp/config.ts +20 -0
- package/src/mcp/edges.ts +99 -0
- package/src/mcp/entries.ts +34 -9
- package/src/mcp/feedback.ts +130 -0
- package/src/mcp/resources.ts +12 -9
- package/src/mcp/server.ts +13 -1
- package/src/mcp/tools.ts +271 -102
- package/src/viewer/html.ts +23 -25
- package/src/viewer/index.ts +8 -8
package/src/lib/utils.ts
CHANGED
|
@@ -4,7 +4,20 @@
|
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
export const normalize = (s: string): string =>
|
|
7
|
-
s
|
|
7
|
+
s
|
|
8
|
+
.toLowerCase()
|
|
9
|
+
.normalize("NFD")
|
|
10
|
+
.replace(/[\u0300-\u036f]/g, "")
|
|
11
|
+
.replace(/[-_]/g, " ")
|
|
12
|
+
.replace(/\s+/g, " ")
|
|
13
|
+
.trim()
|
|
14
|
+
|
|
15
|
+
const STOPWORDS = new Set([
|
|
16
|
+
"a", "de", "del", "la", "el", "los", "las", "un", "una", "unos", "unas", "y", "o", "en", "al",
|
|
17
|
+
"por", "para", "con", "sin", "sobre", "como", "que", "cual", "cuando", "donde", "es", "son",
|
|
18
|
+
"fue", "era", "se", "su", "sus", "lo", "le", "les", "me", "te", "nos", "os", "mi", "tu",
|
|
19
|
+
"the", "and", "or", "for", "with", "from", "this", "that", "into", "was", "were", "been", "being",
|
|
20
|
+
])
|
|
8
21
|
|
|
9
22
|
export const isExpiredLocal = (ttl: string, today = new Date().toISOString().split("T")[0]): boolean => {
|
|
10
23
|
if (!ttl) return false
|
|
@@ -48,12 +61,9 @@ export const importanceRank = (level: string): number => {
|
|
|
48
61
|
}
|
|
49
62
|
|
|
50
63
|
export const tokenize = (s: string): string[] =>
|
|
51
|
-
s
|
|
52
|
-
.
|
|
53
|
-
.
|
|
54
|
-
.toLowerCase()
|
|
55
|
-
.split(/\s+/)
|
|
56
|
-
.filter(Boolean)
|
|
64
|
+
normalize(s.replace(/([a-z])([A-Z])/g, "$1 $2"))
|
|
65
|
+
.split(" ")
|
|
66
|
+
.filter((t) => t.length >= 2 && !STOPWORDS.has(t))
|
|
57
67
|
|
|
58
68
|
/**
|
|
59
69
|
* Deterministic token estimate for a text: roughly one token per 4 chars,
|
package/src/mcp/args.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/** Tolerant argument helpers: LLMs routinely send "6" instead of 6. */
|
|
2
|
+
|
|
3
|
+
import { z } from "zod"
|
|
4
|
+
|
|
5
|
+
const toNumber = (v: unknown): unknown => {
|
|
6
|
+
if (typeof v === "string" && v.trim() !== "" && Number.isFinite(Number(v))) return Number(v)
|
|
7
|
+
return v
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** Positive integer, optional with a fallback. Rejects 0, negatives and junk. */
|
|
11
|
+
export function positiveInt(fallback: number, max = 500) {
|
|
12
|
+
return z.preprocess(toNumber, z.number().int().min(1).max(max).optional().default(fallback))
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** Positive integer, optional with no default (0 = absent). */
|
|
16
|
+
export function optionalPositiveInt(max = 500) {
|
|
17
|
+
return z.preprocess(toNumber, z.number().int().min(1).max(max).optional())
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** String that must carry content — prevents silent empty-key entries. */
|
|
21
|
+
export function requiredText(label: string, max = 4000) {
|
|
22
|
+
return z.string().trim().min(1, `${label} must not be empty`).max(max)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Normalize id lists regardless of separator, including JSON arrays. */
|
|
26
|
+
export function splitIds(value: string): string[] {
|
|
27
|
+
if (!value) return []
|
|
28
|
+
let raw = value
|
|
29
|
+
if (value.trim().startsWith("[")) {
|
|
30
|
+
try {
|
|
31
|
+
const parsed = JSON.parse(value)
|
|
32
|
+
if (Array.isArray(parsed)) raw = parsed.map(String).join(" ")
|
|
33
|
+
} catch {
|
|
34
|
+
/* keep the raw string */
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return raw.split(/[\s,;]+/).map((t) => t.trim()).filter(Boolean)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Empty results are data, not errors. Actionable text + machine-readable payload. */
|
|
41
|
+
export function noMatches(query: string): { content: Array<{ type: "text"; text: string }>; structuredContent: { found: false; count: 0 } } {
|
|
42
|
+
return {
|
|
43
|
+
content: [{ type: "text", text: `No matching memories for "${query}". Try a shorter query, or save one first with memory_remember. A transient empty result is not an error.` }],
|
|
44
|
+
structuredContent: { found: false, count: 0 },
|
|
45
|
+
}
|
|
46
|
+
}
|
package/src/mcp/config.ts
CHANGED
|
@@ -14,6 +14,12 @@ export const ARCHIVE_FILE = join(MEMORY_DIR, "archive.toon")
|
|
|
14
14
|
/** Observations log written by capture hooks (opt-in, separate from memory) */
|
|
15
15
|
export const OBSERVATIONS_FILE = join(MEMORY_DIR, "observations.toon")
|
|
16
16
|
|
|
17
|
+
export const FEEDBACK_FILE = join(MEMORY_DIR, "feedback.toon")
|
|
18
|
+
|
|
19
|
+
export const EDGES_FILE = join(MEMORY_DIR, "edges.toon")
|
|
20
|
+
|
|
21
|
+
export const VECTORS_FILE = join(MEMORY_DIR, "vectors.bin")
|
|
22
|
+
|
|
17
23
|
/** Encrypted sidecar vault for secrets. Whole-file AES-256-GCM when TOON_MEMORY_KEY is set.
|
|
18
24
|
* Kept separate from data.toon so the main memory stays a readable, open format. */
|
|
19
25
|
export const SECRETS_FILE = join(MEMORY_DIR, "secrets.toon")
|
|
@@ -56,6 +62,20 @@ export interface MemoryConfig {
|
|
|
56
62
|
enabled?: boolean
|
|
57
63
|
interval?: number
|
|
58
64
|
}
|
|
65
|
+
feedback?: {
|
|
66
|
+
implicit?: boolean
|
|
67
|
+
}
|
|
68
|
+
graph?: {
|
|
69
|
+
activation?: boolean
|
|
70
|
+
}
|
|
71
|
+
sleep?: {
|
|
72
|
+
enabled?: boolean
|
|
73
|
+
}
|
|
74
|
+
dense?: {
|
|
75
|
+
enabled?: boolean
|
|
76
|
+
model?: string
|
|
77
|
+
dim?: number
|
|
78
|
+
}
|
|
59
79
|
}
|
|
60
80
|
|
|
61
81
|
/**
|
package/src/mcp/edges.ts
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync } from "fs"
|
|
2
|
+
import { dirname } from "path"
|
|
3
|
+
import { EDGES_FILE } from "./config"
|
|
4
|
+
import { atomicWrite, withLockSync } from "../lib/lock"
|
|
5
|
+
import { parseEntries, parseLinkToken } from "../lib/graph"
|
|
6
|
+
import { parseToonLine, toToonLine } from "../lib/utils"
|
|
7
|
+
import type { FeedbackEvent } from "./feedback"
|
|
8
|
+
|
|
9
|
+
export const EDGE_TYPES = ["related", "supports", "contradicts", "supersedes", "depends_on", "derived_from", "used_in"] as const
|
|
10
|
+
export type EdgeType = (typeof EDGE_TYPES)[number]
|
|
11
|
+
|
|
12
|
+
export interface Edge {
|
|
13
|
+
from: string
|
|
14
|
+
to: string
|
|
15
|
+
type: EdgeType
|
|
16
|
+
weight: number
|
|
17
|
+
ts: string
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const HEADER = "version: 1\nedges[0|]{from|to|type|weight|ts}:"
|
|
21
|
+
const MAX_EDGES = 2000
|
|
22
|
+
|
|
23
|
+
function normalizeType(type: string): EdgeType {
|
|
24
|
+
const t = type.toLowerCase()
|
|
25
|
+
return (EDGE_TYPES as readonly string[]).includes(t) ? (t as EdgeType) : "related"
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function readEdges(): Edge[] {
|
|
29
|
+
if (!existsSync(EDGES_FILE)) return []
|
|
30
|
+
try {
|
|
31
|
+
const lines = readFileSync(EDGES_FILE, "utf-8").split("\n")
|
|
32
|
+
const start = lines.findIndex((l) => l.startsWith("edges[") || /^\[\d+\|]/.test(l))
|
|
33
|
+
if (start === -1) return []
|
|
34
|
+
const out: Edge[] = []
|
|
35
|
+
for (const line of lines.slice(start + 1)) {
|
|
36
|
+
if (!line.startsWith(" ") || !line.includes("|")) continue
|
|
37
|
+
const p = parseToonLine(line)
|
|
38
|
+
if (p.length < 5) continue
|
|
39
|
+
const weight = Number(p[3])
|
|
40
|
+
if (!p[0] || !p[1] || !Number.isFinite(weight)) continue
|
|
41
|
+
out.push({ from: p[0], to: p[1], type: normalizeType(p[2]), weight, ts: p[4] || "" })
|
|
42
|
+
}
|
|
43
|
+
return out
|
|
44
|
+
} catch {
|
|
45
|
+
return []
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function writeEdges(edges: Edge[]): void {
|
|
50
|
+
const dir = dirname(EDGES_FILE)
|
|
51
|
+
if (!existsSync(dir)) mkdirSync(dir, { recursive: true })
|
|
52
|
+
const sorted = [...edges]
|
|
53
|
+
.filter((e) => e.from && e.to && e.from !== e.to)
|
|
54
|
+
.sort((a, b) => a.from.localeCompare(b.from) || a.to.localeCompare(b.to) || a.type.localeCompare(b.type))
|
|
55
|
+
.slice(0, MAX_EDGES)
|
|
56
|
+
const header = `version: 1\nedges[${sorted.length}|]{from|to|type|weight|ts}:`
|
|
57
|
+
const lines = [header, ...sorted.map((e) => toToonLine([e.from, e.to, e.type, String(e.weight), e.ts]))]
|
|
58
|
+
withLockSync(EDGES_FILE, () => atomicWrite(EDGES_FILE, lines.join("\n")))
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function deriveEdges(data: string, feedback: FeedbackEvent[] = []): Edge[] {
|
|
62
|
+
const entries = parseEntries(data)
|
|
63
|
+
const keys = new Set(entries.map((e) => e.key))
|
|
64
|
+
const map = new Map<string, Edge>()
|
|
65
|
+
const add = (from: string, to: string, type: EdgeType, weight: number, ts: string) => {
|
|
66
|
+
if (!keys.has(from) || !keys.has(to) || from === to) return
|
|
67
|
+
const id = `${from}|${to}|${type}`
|
|
68
|
+
const prev = map.get(id)
|
|
69
|
+
if (prev) prev.weight = Math.max(prev.weight, weight)
|
|
70
|
+
else map.set(id, { from, to, type, weight, ts })
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
for (const e of entries) {
|
|
74
|
+
for (const link of e.links) {
|
|
75
|
+
const { type, key } = parseLinkToken(link)
|
|
76
|
+
add(e.key, key, normalizeType(type), 1, e.date)
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
for (const event of feedback) {
|
|
81
|
+
if (!event.task) continue
|
|
82
|
+
const haystack = event.task.toLowerCase()
|
|
83
|
+
const type: EdgeType = event.signal === "wrong" || event.signal === "outdated" ? "contradicts" : "used_in"
|
|
84
|
+
const weight = event.signal === "useful" ? 1 : 0.5
|
|
85
|
+
for (const key of keys) {
|
|
86
|
+
if (key === event.key) continue
|
|
87
|
+
if (!haystack.includes(key.toLowerCase())) continue
|
|
88
|
+
add(event.key, key, type, weight, event.ts.slice(0, 10))
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return [...map.values()]
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function rebuildEdges(data: string, feedback: FeedbackEvent[] = []): number {
|
|
96
|
+
const edges = deriveEdges(data, feedback)
|
|
97
|
+
writeEdges(edges)
|
|
98
|
+
return edges.length
|
|
99
|
+
}
|
package/src/mcp/entries.ts
CHANGED
|
@@ -86,25 +86,50 @@ export function inferTags(content: string, key: string, vocab?: Record<string, s
|
|
|
86
86
|
|
|
87
87
|
/**
|
|
88
88
|
* Parse a relative date string into an absolute YYYY-MM-DD date.
|
|
89
|
-
* Supports
|
|
89
|
+
* Supports compact ("24h", "7d"), exact dates, and natural language in
|
|
90
|
+
* Spanish/English ("ayer", "la semana pasada", "hace 3 dias", "last 7 days").
|
|
90
91
|
*/
|
|
91
92
|
export function parseRelativeDate(since: string): string {
|
|
92
93
|
const trimmed = since.trim()
|
|
94
|
+
const iso = (d: Date) => d.toISOString().split("T")[0]
|
|
95
|
+
const shiftDays = (days: number) => {
|
|
96
|
+
const d = new Date()
|
|
97
|
+
d.setDate(d.getDate() - days)
|
|
98
|
+
return iso(d)
|
|
99
|
+
}
|
|
100
|
+
|
|
93
101
|
const hourMatch = trimmed.match(/^(\d+)h$/)
|
|
94
102
|
if (hourMatch) {
|
|
95
103
|
const d = new Date()
|
|
96
104
|
d.setHours(d.getHours() - parseInt(hourMatch[1]))
|
|
97
|
-
return d
|
|
105
|
+
return iso(d)
|
|
98
106
|
}
|
|
99
107
|
const dayMatch = trimmed.match(/^(\d+)d$/)
|
|
100
|
-
if (dayMatch)
|
|
101
|
-
const d = new Date()
|
|
102
|
-
d.setDate(d.getDate() - parseInt(dayMatch[1]))
|
|
103
|
-
return d.toISOString().split("T")[0]
|
|
104
|
-
}
|
|
108
|
+
if (dayMatch) return shiftDays(parseInt(dayMatch[1]))
|
|
105
109
|
if (/^\d{4}-\d{2}-\d{2}$/.test(trimmed)) return trimmed
|
|
106
|
-
|
|
107
|
-
|
|
110
|
+
|
|
111
|
+
const key = trimmed
|
|
112
|
+
.toLowerCase()
|
|
113
|
+
.normalize("NFD")
|
|
114
|
+
.replace(/[\u0300-\u036f]/g, "")
|
|
115
|
+
.replace(/\s+/g, " ")
|
|
116
|
+
.trim()
|
|
117
|
+
|
|
118
|
+
if (key === "hoy" || key === "today") return iso(new Date())
|
|
119
|
+
if (key === "ayer" || key === "yesterday") return shiftDays(1)
|
|
120
|
+
if (/^(la |el )?semana pasada$/.test(key) || key === "last week") return shiftDays(7)
|
|
121
|
+
if (/^(la |el )?mes pasada?$/.test(key) || key === "last month") return shiftDays(30)
|
|
122
|
+
|
|
123
|
+
const natural = key.match(/^(?:hace|last|ultimos?|past)\s+(\d+)\s*(dia|dias|day|days|semana|semanas|week|weeks|mes|meses|month|months)\b/)
|
|
124
|
+
if (natural) {
|
|
125
|
+
const n = parseInt(natural[1])
|
|
126
|
+
const unit = natural[2]
|
|
127
|
+
if (/^(dia|dias|day|days)$/.test(unit)) return shiftDays(n)
|
|
128
|
+
if (/^(semana|semanas|week|weeks)$/.test(unit)) return shiftDays(n * 7)
|
|
129
|
+
return shiftDays(n * 30)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return iso(new Date())
|
|
108
133
|
}
|
|
109
134
|
|
|
110
135
|
export { normalize } from "../lib/utils"
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync } from "fs"
|
|
2
|
+
import { dirname } from "path"
|
|
3
|
+
import { randomBytes } from "crypto"
|
|
4
|
+
import { withLockSync, atomicWrite } from "../lib/lock"
|
|
5
|
+
import { parseToonLine, toToonLine } from "../lib/utils"
|
|
6
|
+
import { FEEDBACK_FILE } from "./config"
|
|
7
|
+
import { readMemory, writeMemory } from "./memory-io"
|
|
8
|
+
|
|
9
|
+
export type FeedbackSignal = "useful" | "irrelevant" | "outdated" | "wrong"
|
|
10
|
+
export type FeedbackSource = "explicit" | "implicit"
|
|
11
|
+
|
|
12
|
+
export interface FeedbackEvent {
|
|
13
|
+
id: string
|
|
14
|
+
key: string
|
|
15
|
+
signal: FeedbackSignal
|
|
16
|
+
source: FeedbackSource
|
|
17
|
+
ts: string
|
|
18
|
+
session: string
|
|
19
|
+
task: string
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const HEADER = "version: 1\nfeedback[0|]{id|key|signal|source|ts|session|task}:"
|
|
23
|
+
const MAX_EVENTS = 1000
|
|
24
|
+
const SIGNAL_WEIGHT: Record<FeedbackSignal, number> = {
|
|
25
|
+
useful: 1,
|
|
26
|
+
irrelevant: -1,
|
|
27
|
+
outdated: -1.5,
|
|
28
|
+
wrong: -2,
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function readFeedback(): FeedbackEvent[] {
|
|
32
|
+
if (!existsSync(FEEDBACK_FILE)) return []
|
|
33
|
+
let data = ""
|
|
34
|
+
try {
|
|
35
|
+
data = readFileSync(FEEDBACK_FILE, "utf-8")
|
|
36
|
+
} catch {
|
|
37
|
+
return []
|
|
38
|
+
}
|
|
39
|
+
const lines = data.split("\n")
|
|
40
|
+
const headerIdx = lines.findIndex((l) => l.startsWith("feedback[") || /^\[\d+\|]/.test(l))
|
|
41
|
+
if (headerIdx === -1) return []
|
|
42
|
+
const out: FeedbackEvent[] = []
|
|
43
|
+
for (const line of lines.slice(headerIdx + 1)) {
|
|
44
|
+
if (!line.startsWith(" ") || !line.includes("|")) continue
|
|
45
|
+
const p = parseToonLine(line.trim())
|
|
46
|
+
if (p.length < 5) continue
|
|
47
|
+
const signal = p[2] as FeedbackSignal
|
|
48
|
+
if (!(signal in SIGNAL_WEIGHT)) continue
|
|
49
|
+
out.push({
|
|
50
|
+
id: p[0],
|
|
51
|
+
key: p[1],
|
|
52
|
+
signal,
|
|
53
|
+
source: p[3] === "implicit" ? "implicit" : "explicit",
|
|
54
|
+
ts: p[4],
|
|
55
|
+
session: p[5] || "",
|
|
56
|
+
task: p[6] || "",
|
|
57
|
+
})
|
|
58
|
+
}
|
|
59
|
+
return out
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function appendFeedback(
|
|
63
|
+
event: Omit<FeedbackEvent, "id" | "ts" | "session"> & { session?: string; id?: string; ts?: string }
|
|
64
|
+
): FeedbackEvent {
|
|
65
|
+
const full: FeedbackEvent = {
|
|
66
|
+
id: event.id || `fb-${randomBytes(4).toString("hex")}`,
|
|
67
|
+
key: event.key,
|
|
68
|
+
signal: event.signal,
|
|
69
|
+
source: event.source,
|
|
70
|
+
ts: event.ts || new Date().toISOString(),
|
|
71
|
+
session: event.session || "",
|
|
72
|
+
task: event.task || "",
|
|
73
|
+
}
|
|
74
|
+
const dir = dirname(FEEDBACK_FILE)
|
|
75
|
+
if (!existsSync(dir)) mkdirSync(dir, { recursive: true })
|
|
76
|
+
withLockSync(FEEDBACK_FILE, () => {
|
|
77
|
+
const data = existsSync(FEEDBACK_FILE) ? readFileSync(FEEDBACK_FILE, "utf-8") : HEADER
|
|
78
|
+
const lines = data.split("\n")
|
|
79
|
+
let headerIdx = lines.findIndex((l) => l.startsWith("feedback[") || /^\[\d+\|]/.test(l))
|
|
80
|
+
if (headerIdx === -1) {
|
|
81
|
+
lines.unshift(HEADER.split("\n")[1])
|
|
82
|
+
headerIdx = 0
|
|
83
|
+
}
|
|
84
|
+
const rows = lines.slice(headerIdx + 1).filter((l) => l.trim().length > 0 && l.startsWith(" "))
|
|
85
|
+
const row = toToonLine([
|
|
86
|
+
full.id, full.key, full.signal, full.source, full.ts,
|
|
87
|
+
full.session, full.task.slice(0, 2000),
|
|
88
|
+
])
|
|
89
|
+
const kept = [row, ...rows].slice(0, MAX_EVENTS)
|
|
90
|
+
const countMatch = lines[headerIdx].match(/\[(\d+)\|/)
|
|
91
|
+
const count = countMatch ? parseInt(countMatch[1], 10) : 0
|
|
92
|
+
lines[headerIdx] = lines[headerIdx].replace(/\[\d+\|/, `[${count + 1}|`)
|
|
93
|
+
lines.splice(headerIdx + 1, lines.length - headerIdx - 1, ...kept)
|
|
94
|
+
atomicWrite(FEEDBACK_FILE, lines.join("\n"))
|
|
95
|
+
})
|
|
96
|
+
return full
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function utilityMap(today?: string): Map<string, number> {
|
|
100
|
+
const ref = today || new Date().toISOString().split("T")[0]
|
|
101
|
+
const refTime = Date.parse(`${ref}T00:00:00Z`)
|
|
102
|
+
const out = new Map<string, number>()
|
|
103
|
+
for (const e of readFeedback()) {
|
|
104
|
+
const days = Number.isNaN(refTime) ? 0 : Math.max(0, (refTime - Date.parse(e.ts)) / 86400000)
|
|
105
|
+
const decay = Math.pow(0.9, days)
|
|
106
|
+
const delta = SIGNAL_WEIGHT[e.signal] * decay
|
|
107
|
+
out.set(e.key, Math.max(-3, Math.min(3, Number(((out.get(e.key) || 0) + delta).toFixed(3)))))
|
|
108
|
+
}
|
|
109
|
+
return out
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export function markWrong(key: string): boolean {
|
|
113
|
+
const data = readMemory()
|
|
114
|
+
const lines = data.split("\n")
|
|
115
|
+
const headerIdx = lines.findIndex((l) => l.startsWith("entries[") || /^\[\d+\|]/.test(l))
|
|
116
|
+
if (headerIdx === -1) return false
|
|
117
|
+
for (let i = headerIdx + 1; i < lines.length; i++) {
|
|
118
|
+
const line = lines[i]
|
|
119
|
+
if (!line.startsWith(" ") || !line.includes("|")) continue
|
|
120
|
+
if (line.startsWith(" summaries:")) break
|
|
121
|
+
const parts = parseToonLine(line)
|
|
122
|
+
if (parts[0] !== key && parts[2] !== key) continue
|
|
123
|
+
while (parts.length < 20) parts.push("")
|
|
124
|
+
parts[19] = "conflict"
|
|
125
|
+
lines[i] = toToonLine(parts)
|
|
126
|
+
writeMemory(lines.join("\n"))
|
|
127
|
+
return true
|
|
128
|
+
}
|
|
129
|
+
return false
|
|
130
|
+
}
|
package/src/mcp/resources.ts
CHANGED
|
@@ -60,18 +60,21 @@ export function registerResources(server: McpServer): void {
|
|
|
60
60
|
}
|
|
61
61
|
)
|
|
62
62
|
|
|
63
|
-
// MCP Apps: interactive graph viewer rendered inline in MCP hosts
|
|
64
|
-
|
|
65
|
-
uri: "ui://viewer",
|
|
66
|
-
content: { type: "rawHtml", htmlString: generateHtml(buildViewerData()) },
|
|
67
|
-
encoding: "text",
|
|
68
|
-
adapters: { mcpApps: { enabled: true } },
|
|
69
|
-
})
|
|
63
|
+
// MCP Apps: interactive graph viewer rendered inline in MCP hosts.
|
|
64
|
+
// Built lazily: a bad viewer build must never kill MCP startup.
|
|
70
65
|
registerAppResource(
|
|
71
66
|
server as unknown as Parameters<typeof registerAppResource>[0],
|
|
72
67
|
"Memory Graph Viewer",
|
|
73
|
-
|
|
68
|
+
"ui://viewer",
|
|
74
69
|
{ description: "Interactive memory graph viewer (D3.js force-directed graph with stats, timeline, detail panel)" },
|
|
75
|
-
async () =>
|
|
70
|
+
async (uri) => {
|
|
71
|
+
const viewerResource = createUIResource({
|
|
72
|
+
uri: "ui://viewer",
|
|
73
|
+
content: { type: "rawHtml", htmlString: generateHtml(buildViewerData()) },
|
|
74
|
+
encoding: "text",
|
|
75
|
+
adapters: { mcpApps: { enabled: true } },
|
|
76
|
+
})
|
|
77
|
+
return { contents: [{ ...viewerResource.resource, uri: uri.href }] }
|
|
78
|
+
},
|
|
76
79
|
)
|
|
77
80
|
}
|
package/src/mcp/server.ts
CHANGED
|
@@ -3,13 +3,24 @@ import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"
|
|
|
3
3
|
import { InitializeRequestSchema, SUPPORTED_PROTOCOL_VERSIONS, LATEST_PROTOCOL_VERSION } from "@modelcontextprotocol/sdk/types.js"
|
|
4
4
|
import { registerTools } from "./tools"
|
|
5
5
|
import { registerResources } from "./resources"
|
|
6
|
+
import { registerPrompts } from "./prompts"
|
|
6
7
|
import { pruneExpiredEntries } from "./archive"
|
|
7
8
|
|
|
8
9
|
declare const TOON_VERSION: string
|
|
9
10
|
|
|
11
|
+
const INSTRUCTIONS = [
|
|
12
|
+
"toon-memory: at task start call context_compile(task=...) and hydrate only the keys you need.",
|
|
13
|
+
"Before reading files, call memory_recall(query=<path or concept>).",
|
|
14
|
+
"When recalled memory was useful or wrong, call memory_feedback(key=..., signal=...).",
|
|
15
|
+
"Save decisions, directives and resolved bugs with memory_remember.",
|
|
16
|
+
].join(" ")
|
|
17
|
+
|
|
10
18
|
const server = new McpServer(
|
|
11
19
|
{ name: "toon-memory", version: TOON_VERSION },
|
|
12
|
-
{
|
|
20
|
+
{
|
|
21
|
+
instructions: INSTRUCTIONS,
|
|
22
|
+
capabilities: { tools: { listChanged: true }, resources: { listChanged: true }, experimental: { "io.modelcontextprotocol/ui": {} } },
|
|
23
|
+
}
|
|
13
24
|
)
|
|
14
25
|
|
|
15
26
|
// MCP Apps support is advertised via capabilities.experimental.
|
|
@@ -32,6 +43,7 @@ s.setRequestHandler(InitializeRequestSchema, async (request: any) => {
|
|
|
32
43
|
|
|
33
44
|
registerResources(server)
|
|
34
45
|
registerTools(server)
|
|
46
|
+
registerPrompts(server)
|
|
35
47
|
|
|
36
48
|
// Startup TTL prune: drop entries whose TTL has elapsed. Best-effort — never
|
|
37
49
|
// block server startup on a prune failure.
|