@kolivri/memoreal-mcp 1.0.2 → 1.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/.claude-plugin/plugin.json +9 -0
- package/.cursor-plugin/plugin.json +2 -2
- package/CLAUDE.md +29 -29
- package/README.md +188 -11
- package/agents/memoreal.md +25 -9
- package/agents/memoria.md +25 -9
- package/bin/memoreal-hook.mjs +483 -0
- package/bin/memoreal-ns +54 -4
- package/bin/memoreal-ns.cmd +2 -0
- package/bin/memoreal-ns.mjs +945 -0
- package/bin/memoria-ns +54 -5
- package/hooks/hooks.json +11 -11
- package/install.sh +565 -119
- package/manifest.json +1 -1
- package/opencode/memoreal-hooks.js +266 -381
- package/opencode/memoria-hooks.js +45 -10
- package/package.json +26 -1
- package/rules/memoreal.mdc +20 -4
- package/rules/memoria.mdc +6 -3
- package/server.json +2 -2
- package/skills/memoreal-connect/SKILL.md +178 -2
- package/skills/memoria-connect/SKILL.md +178 -2
- package/src/bootstrap.mjs +2850 -0
- package/src/config-store.mjs +1156 -93
- package/src/schemas.mjs +429 -1
- package/src/server.mjs +238 -23
- package/src/wizard.mjs +100 -6
- package/.actor/Dockerfile +0 -6
- package/.actor/actor.json +0 -15
- package/.actor/input_schema.json +0 -92
- package/Dockerfile +0 -5
- package/prd/001-rebrand-memoreal-mcp.md +0 -132
- package/prd/002-connect-oauth-wizard.md +0 -259
- package/test/apify-actor.test.mjs +0 -429
- package/test/config-store.test.mjs +0 -680
- package/test/connect-wizard.test.mjs +0 -936
- package/test/hooks-onboarding.test.mjs +0 -1039
- package/test/memoria-mcp.test.mjs +0 -376
- package/test/wizard-unit.test.mjs +0 -1272
|
@@ -0,0 +1,483 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import fs from "node:fs"
|
|
3
|
+
import path from "node:path"
|
|
4
|
+
import os from "node:os"
|
|
5
|
+
import cp from "node:child_process"
|
|
6
|
+
import { deriveNamespaceCrossOs, resolveProjectNamespaceInfo, MEMOREAL_CLIENT_VERSION } from "./memoreal-ns.mjs"
|
|
7
|
+
|
|
8
|
+
export function isPlaceholderApiKey(raw) {
|
|
9
|
+
if (typeof raw !== "string") return true
|
|
10
|
+
let str = raw.trim()
|
|
11
|
+
while ((str.startsWith('"') && str.endsWith('"')) || (str.startsWith("'") && str.endsWith("'"))) {
|
|
12
|
+
str = str.slice(1, -1).trim()
|
|
13
|
+
}
|
|
14
|
+
if (!str) return true
|
|
15
|
+
if (str.includes("...")) return true
|
|
16
|
+
if (str.startsWith("<") && str.endsWith(">")) return true
|
|
17
|
+
if (str.startsWith("${") && str.endsWith("}")) return true
|
|
18
|
+
const lower = str.toLowerCase()
|
|
19
|
+
if (
|
|
20
|
+
lower === "mem_dev_your_key_here" ||
|
|
21
|
+
lower === "your_api_key" ||
|
|
22
|
+
lower === "your-api-key" ||
|
|
23
|
+
lower === "your-memoreal-api-key" ||
|
|
24
|
+
lower === "your_memoreal_api_key" ||
|
|
25
|
+
lower === "placeholder" ||
|
|
26
|
+
lower.includes("your_key_here") ||
|
|
27
|
+
lower.includes("your_api_key") ||
|
|
28
|
+
lower.includes("your-api-key") ||
|
|
29
|
+
lower.includes("your-memoreal-api-key")
|
|
30
|
+
) {
|
|
31
|
+
return true
|
|
32
|
+
}
|
|
33
|
+
return false
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function resolveHomeDir() {
|
|
37
|
+
return (
|
|
38
|
+
process.env.MEMOREAL_HOME ||
|
|
39
|
+
(process.platform === "win32"
|
|
40
|
+
? process.env.USERPROFILE || process.env.HOME
|
|
41
|
+
: process.env.HOME || process.env.USERPROFILE) ||
|
|
42
|
+
os.homedir()
|
|
43
|
+
)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function resolveCloudConfig(customHome) {
|
|
47
|
+
const isExplicitHome = Boolean(customHome)
|
|
48
|
+
const baseHome = customHome || resolveHomeDir()
|
|
49
|
+
|
|
50
|
+
const isExplicitEmptyAmbientKey =
|
|
51
|
+
!isExplicitHome &&
|
|
52
|
+
!process.env.MEMOREAL_HOME &&
|
|
53
|
+
"MEMOREAL_API_KEY" in process.env &&
|
|
54
|
+
process.env.MEMOREAL_API_KEY === ""
|
|
55
|
+
|
|
56
|
+
const fileVars = {}
|
|
57
|
+
if (!isExplicitEmptyAmbientKey) {
|
|
58
|
+
for (const dirName of [".memoreal", ".memoria"]) {
|
|
59
|
+
try {
|
|
60
|
+
const cfgPath = path.join(baseHome, dirName, "config.env")
|
|
61
|
+
if (fs.existsSync(cfgPath)) {
|
|
62
|
+
const raw = fs.readFileSync(cfgPath, "utf8")
|
|
63
|
+
for (const line of raw.split(/\r?\n/)) {
|
|
64
|
+
const m = line.match(/^\s*(?:export\s+)?([A-Za-z0-9_]+)\s*=\s*(.*)\s*$/)
|
|
65
|
+
if (!m) continue
|
|
66
|
+
let val = m[2].trim()
|
|
67
|
+
while ((val.startsWith('"') && val.endsWith('"')) || (val.startsWith("'") && val.endsWith("'"))) {
|
|
68
|
+
val = val.slice(1, -1).trim()
|
|
69
|
+
}
|
|
70
|
+
if (!(m[1] in fileVars)) {
|
|
71
|
+
fileVars[m[1]] = val
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
} catch {}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const rawUrl =
|
|
80
|
+
process.env.MEMOREAL_API_URL ||
|
|
81
|
+
process.env.MEMORIA_API_URL ||
|
|
82
|
+
(!isExplicitEmptyAmbientKey ? (fileVars.MEMOREAL_API_URL || fileVars.MEMORIA_API_URL) : "") ||
|
|
83
|
+
process.env.MEMOREAL_URL ||
|
|
84
|
+
process.env.MEMORIA_URL ||
|
|
85
|
+
(!isExplicitEmptyAmbientKey ? (fileVars.MEMOREAL_URL || fileVars.MEMORIA_URL) : "") ||
|
|
86
|
+
"https://memoreal.kolivri.com"
|
|
87
|
+
|
|
88
|
+
const candidateUrl = rawUrl.replace(/\/v1\/mcp\/?$/, "").replace(/\/+$/, "")
|
|
89
|
+
const isSafeUrl =
|
|
90
|
+
/^https:\/\/[A-Za-z0-9._:-]+(\/[A-Za-z0-9._/-]*)?$/.test(candidateUrl) ||
|
|
91
|
+
/^http:\/\/(localhost|127\.0\.0\.1)(:[0-9]+)?(\/[A-Za-z0-9._/-]*)?$/.test(candidateUrl)
|
|
92
|
+
const apiUrl = isSafeUrl ? candidateUrl : "https://memoreal.kolivri.com"
|
|
93
|
+
|
|
94
|
+
if (process.env.MEMOREAL_FORCE_UNREGISTERED === "1" || process.env.MEMORIA_FORCE_UNREGISTERED === "1") {
|
|
95
|
+
return { apiUrl, apiKey: "", baseHome }
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const envCandidates = [
|
|
99
|
+
process.env.MEMOREAL_API_KEY,
|
|
100
|
+
process.env.MEMORIA_API_KEY,
|
|
101
|
+
process.env.MEMOREAL_KEY,
|
|
102
|
+
process.env.MEMORIA_KEY,
|
|
103
|
+
]
|
|
104
|
+
|
|
105
|
+
let rawKey = ""
|
|
106
|
+
for (const candidate of envCandidates) {
|
|
107
|
+
if (candidate && !isPlaceholderApiKey(candidate)) {
|
|
108
|
+
rawKey = candidate
|
|
109
|
+
break
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (!rawKey && !isExplicitEmptyAmbientKey) {
|
|
114
|
+
const fileCandidates = [
|
|
115
|
+
fileVars.MEMOREAL_API_KEY,
|
|
116
|
+
fileVars.MEMORIA_API_KEY,
|
|
117
|
+
fileVars.MEMOREAL_KEY,
|
|
118
|
+
fileVars.MEMORIA_KEY,
|
|
119
|
+
]
|
|
120
|
+
for (const candidate of fileCandidates) {
|
|
121
|
+
if (candidate && !isPlaceholderApiKey(candidate)) {
|
|
122
|
+
rawKey = candidate
|
|
123
|
+
break
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
let apiKey = (rawKey || "").trim()
|
|
129
|
+
while ((apiKey.startsWith('"') && apiKey.endsWith('"')) || (apiKey.startsWith("'") && apiKey.endsWith("'"))) {
|
|
130
|
+
apiKey = apiKey.slice(1, -1).trim()
|
|
131
|
+
}
|
|
132
|
+
if (isPlaceholderApiKey(apiKey) || !/^[A-Za-z0-9_.-]+$/.test(apiKey)) {
|
|
133
|
+
apiKey = ""
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return { apiUrl, apiKey, baseHome }
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const WIZARD_PROMPT =
|
|
140
|
+
'[memoreal-session-start] Memoreal Cloud is not yet connected.\n' +
|
|
141
|
+
'[memoreal-setup-wizard] Drive the 3-Click Interactive In-Chat Wizard using AskUserQuestion (Claude Code) or question (OpenCode & Cursor):\n' +
|
|
142
|
+
'- Step 1: Present choice card "Connect in 1 Click via Google / Browser (Recommended)" vs "Skip for this session".\n' +
|
|
143
|
+
'- Step 2: Call connect_account({ action: "start", tool: "claude-code", client_name: "Claude Code" }) and present clickable https://memoreal.kolivri.com/connect?tool=claude-code (https://memoreal.kolivri.com/connect?code=XXXX-YYYY) link with "I clicked Authorize — Verify" card (calling connect_account({ action: "status" })).\n' +
|
|
144
|
+
'- Step 3: Once connected, present choice card "Yes — Index repo now (Recommended)" vs "Later".\n' +
|
|
145
|
+
'Never ask the user to paste a raw API key into chat.'
|
|
146
|
+
|
|
147
|
+
function readStdinSync() {
|
|
148
|
+
try {
|
|
149
|
+
return fs.readFileSync(0, "utf8")
|
|
150
|
+
} catch {
|
|
151
|
+
return ""
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function getGitContext(cwd) {
|
|
156
|
+
let branch = ""
|
|
157
|
+
let recent = ""
|
|
158
|
+
let changed = ""
|
|
159
|
+
try {
|
|
160
|
+
branch = cp
|
|
161
|
+
.execFileSync("git", ["branch", "--show-current"], {
|
|
162
|
+
cwd,
|
|
163
|
+
encoding: "utf8",
|
|
164
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
165
|
+
})
|
|
166
|
+
.trim()
|
|
167
|
+
} catch {}
|
|
168
|
+
try {
|
|
169
|
+
recent = cp
|
|
170
|
+
.execFileSync("git", ["log", "--oneline", "-3"], {
|
|
171
|
+
cwd,
|
|
172
|
+
encoding: "utf8",
|
|
173
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
174
|
+
})
|
|
175
|
+
.replace(/\r?\n+/g, "; ")
|
|
176
|
+
.trim()
|
|
177
|
+
} catch {}
|
|
178
|
+
try {
|
|
179
|
+
changed = cp
|
|
180
|
+
.execFileSync("git", ["diff", "--name-only", "HEAD"], {
|
|
181
|
+
cwd,
|
|
182
|
+
encoding: "utf8",
|
|
183
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
184
|
+
})
|
|
185
|
+
.replace(/\r?\n+/g, ", ")
|
|
186
|
+
.trim()
|
|
187
|
+
} catch {}
|
|
188
|
+
|
|
189
|
+
const parts = []
|
|
190
|
+
if (branch) parts.push(`branch: ${branch}`)
|
|
191
|
+
if (recent) parts.push(`recent commits: ${recent}`)
|
|
192
|
+
if (changed) parts.push(`changed files: ${changed}`)
|
|
193
|
+
return parts.join(", ")
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
async function runHook() {
|
|
197
|
+
const arg1 = process.argv[2] || ""
|
|
198
|
+
const arg2 = process.argv[3] || ""
|
|
199
|
+
|
|
200
|
+
let eventName = arg1
|
|
201
|
+
let matcher = arg2
|
|
202
|
+
|
|
203
|
+
if (["TaskCreate", "Write", "Edit", "Read"].includes(arg1)) {
|
|
204
|
+
eventName = "PostToolUse"
|
|
205
|
+
matcher = arg1
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const { apiUrl, apiKey, baseHome } = resolveCloudConfig()
|
|
209
|
+
|
|
210
|
+
if (!apiKey) {
|
|
211
|
+
if (eventName === "SessionStart") {
|
|
212
|
+
console.log(WIZARD_PROMPT)
|
|
213
|
+
}
|
|
214
|
+
process.exit(0)
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const nsInfo = resolveProjectNamespaceInfo(process.cwd())
|
|
218
|
+
const ns = nsInfo.namespace
|
|
219
|
+
const legacyNamespace = nsInfo.legacyNamespace
|
|
220
|
+
const isCodeProject = nsInfo.isCodeProject
|
|
221
|
+
|
|
222
|
+
if (eventName === "SessionStart") {
|
|
223
|
+
let query = "previous tasks, plans, pending work, key decisions, progress, corrections, lessons learned"
|
|
224
|
+
const gitCtx = getGitContext(process.cwd())
|
|
225
|
+
if (gitCtx) {
|
|
226
|
+
query += `, context for ${gitCtx}`
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const authHeaders = {
|
|
230
|
+
Authorization: `Bearer ${apiKey}`,
|
|
231
|
+
"X-Memoreal-Client-Version": MEMOREAL_CLIENT_VERSION,
|
|
232
|
+
"User-Agent": `@kolivri/memoreal-mcp/${MEMOREAL_CLIENT_VERSION}`,
|
|
233
|
+
}
|
|
234
|
+
const extraHeaders = {}
|
|
235
|
+
const contextBody = { namespace: ns, query, token_budget: 4096 }
|
|
236
|
+
if (isCodeProject && legacyNamespace && legacyNamespace !== ns) {
|
|
237
|
+
extraHeaders["X-Memoreal-Legacy-Namespace"] = legacyNamespace
|
|
238
|
+
extraHeaders["X-Memoreal-Code-Project"] = "1"
|
|
239
|
+
contextBody.legacy_namespace = legacyNamespace
|
|
240
|
+
}
|
|
241
|
+
try {
|
|
242
|
+
const healthRes = await fetch(`${apiUrl}/v1/health`, {
|
|
243
|
+
headers: authHeaders,
|
|
244
|
+
signal: AbortSignal.timeout(2500),
|
|
245
|
+
})
|
|
246
|
+
if (healthRes.status === 401) {
|
|
247
|
+
console.log(WIZARD_PROMPT)
|
|
248
|
+
process.exit(0)
|
|
249
|
+
}
|
|
250
|
+
if (!healthRes.ok) {
|
|
251
|
+
throw new Error(`health status ${healthRes.status}`)
|
|
252
|
+
}
|
|
253
|
+
const healthText = await healthRes.text()
|
|
254
|
+
|
|
255
|
+
try {
|
|
256
|
+
const ctxRes = await fetch(`${apiUrl}/v1/memories/context`, {
|
|
257
|
+
method: "POST",
|
|
258
|
+
headers: { "Content-Type": "application/json", ...authHeaders, ...extraHeaders },
|
|
259
|
+
body: JSON.stringify(contextBody),
|
|
260
|
+
signal: AbortSignal.timeout(2500),
|
|
261
|
+
})
|
|
262
|
+
if (ctxRes.status === 401) {
|
|
263
|
+
console.log(WIZARD_PROMPT)
|
|
264
|
+
process.exit(0)
|
|
265
|
+
}
|
|
266
|
+
if (!ctxRes.ok) {
|
|
267
|
+
console.log(
|
|
268
|
+
`[memoreal-session-start] namespace=${ns}. Health: ${healthText}. Call build_context MCP tool with namespace "${ns}" and query "${query}" to load prior context.`
|
|
269
|
+
)
|
|
270
|
+
process.exit(0)
|
|
271
|
+
}
|
|
272
|
+
const ctxText = await ctxRes.text()
|
|
273
|
+
console.log(healthText)
|
|
274
|
+
console.log("---")
|
|
275
|
+
console.log(ctxText)
|
|
276
|
+
} catch {
|
|
277
|
+
console.log(
|
|
278
|
+
`[memoreal-session-start] namespace=${ns}. Health: ${healthText}. Call build_context MCP tool with namespace "${ns}" and query "${query}" to load prior context.`
|
|
279
|
+
)
|
|
280
|
+
}
|
|
281
|
+
} catch {
|
|
282
|
+
console.log(
|
|
283
|
+
`[memoreal-session-start] namespace=${ns}. Call build_context MCP tool with namespace "${ns}" and query "${query}" and token_budget 4096 to load prior session context.`
|
|
284
|
+
)
|
|
285
|
+
}
|
|
286
|
+
process.exit(0)
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
if (eventName === "PreCompact") {
|
|
290
|
+
console.log(
|
|
291
|
+
`[memoreal-pre-compact] Context is about to be compressed. BEFORE compression happens, encode the following into Memoreal (namespace: ${ns}) so nothing is lost:\n` +
|
|
292
|
+
`1. (type: episodic) Current task state: what you were working on, what is done, what remains\n` +
|
|
293
|
+
`2. (type: episodic) Key findings so far: bugs found, root causes identified, decisions made\n` +
|
|
294
|
+
`3. (type: episodic) Open questions and blockers\n` +
|
|
295
|
+
`4. (type: semantic) Any code patterns, architecture insights, or facts learned this session\n` +
|
|
296
|
+
`This is critical — after compaction, detailed context is lost. Encode it NOW before responding.`
|
|
297
|
+
)
|
|
298
|
+
return
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
if (eventName === "PostCompact") {
|
|
302
|
+
console.log(
|
|
303
|
+
`[memoreal-post-compact] Context was just compressed. Call build_context with namespace "${ns}" and query "current task, recent progress, findings, decisions, open questions, what was I working on" and token_budget 2048 to restore critical context that may have been lost during compression.`
|
|
304
|
+
)
|
|
305
|
+
return
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
if (eventName === "UserPromptSubmit") {
|
|
309
|
+
let prompt = ""
|
|
310
|
+
const rawStdin = readStdinSync()
|
|
311
|
+
if (rawStdin) {
|
|
312
|
+
try {
|
|
313
|
+
const json = JSON.parse(rawStdin)
|
|
314
|
+
prompt = typeof json.prompt === "string" ? json.prompt : ""
|
|
315
|
+
} catch {}
|
|
316
|
+
}
|
|
317
|
+
const lower = prompt.toLowerCase()
|
|
318
|
+
const isCorrection = [
|
|
319
|
+
"don't",
|
|
320
|
+
"do not",
|
|
321
|
+
"stop",
|
|
322
|
+
"never",
|
|
323
|
+
"wrong",
|
|
324
|
+
"no,",
|
|
325
|
+
"no ",
|
|
326
|
+
"actually",
|
|
327
|
+
"instead",
|
|
328
|
+
"not like that",
|
|
329
|
+
"that's wrong",
|
|
330
|
+
"incorrect",
|
|
331
|
+
].some((pat) => lower.includes(pat))
|
|
332
|
+
|
|
333
|
+
if (isCorrection) {
|
|
334
|
+
console.log(
|
|
335
|
+
`[memoreal-correction] The user appears to be correcting your approach. After addressing the correction, encode the lesson into Memoreal (namespace: ${ns}, type: semantic, salience: 0.9) as a rule: what was wrong, what the correct approach is, and why. This ensures the mistake is never repeated in future sessions.`
|
|
336
|
+
)
|
|
337
|
+
}
|
|
338
|
+
return
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
if (eventName === "PostToolUse") {
|
|
342
|
+
if (matcher === "TaskCreate") {
|
|
343
|
+
console.log(
|
|
344
|
+
`[memoreal-encode-task] A task was just created. Encode it into Memoreal via encode_memory with type "episodic" and namespace "${ns}". Include the task description, status, and which plan file it relates to (if any).`
|
|
345
|
+
)
|
|
346
|
+
return
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
let file = ""
|
|
350
|
+
const rawStdin = readStdinSync()
|
|
351
|
+
if (rawStdin) {
|
|
352
|
+
try {
|
|
353
|
+
const json = JSON.parse(rawStdin)
|
|
354
|
+
file =
|
|
355
|
+
json?.tool_input?.file_path ||
|
|
356
|
+
json?.tool_input?.path ||
|
|
357
|
+
json?.file_path ||
|
|
358
|
+
json?.path ||
|
|
359
|
+
""
|
|
360
|
+
} catch {}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
const norm = file.replace(/\\/g, "/")
|
|
364
|
+
const isPlan =
|
|
365
|
+
/(?:^|\/)(?:\.claude\/plans\/|\.opencode\/plans\/|design-log\/|docs\/.*plan|docs\/.*roadmap|prd\/)/i.test(norm)
|
|
366
|
+
const isMarkdown = /\.(md|markdown)$/i.test(norm)
|
|
367
|
+
const isSkip =
|
|
368
|
+
/\.(lock|sum|toml|cfg|ini)$/i.test(norm) ||
|
|
369
|
+
/(?:^|\/)\.env(?:[.-].*)?$/i.test(norm)
|
|
370
|
+
const isCode =
|
|
371
|
+
/\.(rs|py|ts|tsx|js|jsx|mjs|cjs|go|java|kt|kts|c|h|cc|cpp|hpp|cxx|cs|rb|php|swift|scala|sh|bash|zsh|sql|proto)$/i.test(norm)
|
|
372
|
+
|
|
373
|
+
if (matcher === "Write") {
|
|
374
|
+
if (isSkip) return
|
|
375
|
+
if (isPlan) {
|
|
376
|
+
console.log(
|
|
377
|
+
`[memoreal-encode-plan] Plan file written: ${file}\n` +
|
|
378
|
+
`Encode this plan into Memoreal (namespace: ${ns}). Create MULTIPLE memories:\n` +
|
|
379
|
+
`1. (type: semantic) Plan overview: title, goals, phase structure, and current status\n` +
|
|
380
|
+
`2. (type: semantic) Key decisions and their rationale from this plan\n` +
|
|
381
|
+
`3. (type: episodic) Progress snapshot: what phases are done, what is next, what is blocked\n` +
|
|
382
|
+
`4. (type: semantic) Relationships: other files, systems, or plans referenced in this document\n` +
|
|
383
|
+
`5. (type: semantic) Knowledge: patterns, techniques, or lessons relevant to executing this plan\n` +
|
|
384
|
+
`6. (type: episodic) Ideas and open questions found in the plan\n` +
|
|
385
|
+
`7. (type: semantic) Reflection: what is working well, what is not, what should change in approach\n` +
|
|
386
|
+
`8. (type: semantic) Dream: replay and consolidate — what patterns emerge across related memories, what connections between ideas were missed, what should be strengthened or let go\n` +
|
|
387
|
+
`9. (type: semantic) Workflow: how we are working on this plan — tools, processes, patterns that help or hinder\n` +
|
|
388
|
+
`Include the file path in context metadata for every memory.`
|
|
389
|
+
)
|
|
390
|
+
} else if (isMarkdown) {
|
|
391
|
+
console.log(
|
|
392
|
+
`[memoreal-encode-doc] Markdown document written: ${file}\n` +
|
|
393
|
+
`Encode into Memoreal (namespace: ${ns}). Create memories for:\n` +
|
|
394
|
+
`1. (type: semantic) Document purpose, structure, and key content\n` +
|
|
395
|
+
`2. (type: semantic) Decisions, patterns, or knowledge worth remembering\n` +
|
|
396
|
+
`3. (type: episodic) Relationships to other project work and plans\n` +
|
|
397
|
+
`Include the file path in context metadata.`
|
|
398
|
+
)
|
|
399
|
+
} else if (isCode) {
|
|
400
|
+
console.log(
|
|
401
|
+
`[memoreal-encode-code] Code file written: ${file}. Re-encode this code into Memoreal (namespace: ${ns}) so search_code, explain_code, and find_references stay current: encode the key definitions (functions, types, classes) with their signatures, purpose, and notable logic as type semantic memories, salience 0.6, with the file path in context. Search before encoding so dedup can merge near-duplicates. For a full structural re-index of the project, run the memoreal-sync-code skill.`
|
|
402
|
+
)
|
|
403
|
+
} else {
|
|
404
|
+
console.log(
|
|
405
|
+
`[memoreal-encode-file] File written: ${file}. Apply write gate: only encode if this file contains a key definition, decision, or pattern worth remembering across sessions. Skip boilerplate. If encoding, use namespace ${ns}, type semantic, salience 0.5. Include file path in context.`
|
|
406
|
+
)
|
|
407
|
+
}
|
|
408
|
+
return
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
if (matcher === "Edit") {
|
|
412
|
+
if (isSkip) return
|
|
413
|
+
if (isPlan) {
|
|
414
|
+
console.log(
|
|
415
|
+
`[memoreal-encode-plan] Plan file updated: ${file}\n` +
|
|
416
|
+
`Encode this change into Memoreal (namespace: ${ns}). Create memories for:\n` +
|
|
417
|
+
`1. (type: episodic) What specifically changed in the plan and why\n` +
|
|
418
|
+
`2. (type: episodic) Progress update: mark completed items, note new items or phase transitions\n` +
|
|
419
|
+
`3. (type: semantic) Any new decisions, ideas, or insights from this edit\n` +
|
|
420
|
+
`4. (type: semantic) New relationships or dependencies added\n` +
|
|
421
|
+
`5. (type: semantic) Reflection: does this change reveal something about the approach — what is working, what needs rethinking\n` +
|
|
422
|
+
`6. (type: semantic) Workflow ideas: better ways to work on this plan based on what just happened\n` +
|
|
423
|
+
`Include the file path in context metadata.`
|
|
424
|
+
)
|
|
425
|
+
} else if (isMarkdown) {
|
|
426
|
+
console.log(
|
|
427
|
+
`[memoreal-encode-doc] Markdown document edited: ${file}\n` +
|
|
428
|
+
`Encode the change into Memoreal (namespace: ${ns}):\n` +
|
|
429
|
+
`1. (type: episodic) What changed and why\n` +
|
|
430
|
+
`2. (type: semantic) Any new knowledge, decisions, or patterns\n` +
|
|
431
|
+
`Include the file path in context metadata.`
|
|
432
|
+
)
|
|
433
|
+
} else if (isCode) {
|
|
434
|
+
console.log(
|
|
435
|
+
`[memoreal-encode-code] Code file edited: ${file}. Re-encode the changed code into Memoreal (namespace: ${ns}): encode the updated definitions and logic (what changed and the new behavior) as type semantic, salience 0.6, with the file path in context, so search_code, explain_code, and find_references reflect the current code. Search before encoding to avoid duplicates. For a full structural re-index, run the memoreal-sync-code skill.`
|
|
436
|
+
)
|
|
437
|
+
} else {
|
|
438
|
+
console.log(
|
|
439
|
+
`[memoreal-encode-file] File edited: ${file}. Apply write gate: only encode if this edit represents a meaningful decision, pattern, or lesson. Skip routine changes. If encoding, use namespace ${ns}, type semantic, salience 0.5. Include file path and what changed.`
|
|
440
|
+
)
|
|
441
|
+
}
|
|
442
|
+
return
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
if (matcher === "Read") {
|
|
446
|
+
if (isPlan || isMarkdown) {
|
|
447
|
+
console.log(
|
|
448
|
+
`[memoreal-recall-doc] Reading: ${file}\n` +
|
|
449
|
+
`Recall related memories from Memoreal (namespace: ${ns}) about this file: prior edits, progress updates, decisions, related plans, ideas, reflections, workflow patterns, consolidated patterns, and context from previous sessions. Show the user what Memoreal knows about this file before proceeding.`
|
|
450
|
+
)
|
|
451
|
+
}
|
|
452
|
+
return
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
return
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
if (eventName === "TaskCompleted") {
|
|
459
|
+
console.log(
|
|
460
|
+
`[memoreal-update-task] A task was just completed. Encode into Memoreal (namespace: ${ns}). Create memories for:\n` +
|
|
461
|
+
`1. (type: episodic) What was accomplished and which plan file it relates to\n` +
|
|
462
|
+
`2. (type: semantic) Lessons learned and knowledge gained from this task\n` +
|
|
463
|
+
`3. (type: semantic) Reflection: what went well, what was harder than expected, what to do differently next time\n` +
|
|
464
|
+
`4. (type: semantic) Workflow ideas: any process improvements or tool usage patterns discovered while doing this task`
|
|
465
|
+
)
|
|
466
|
+
return
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
if (eventName === "Stop") {
|
|
470
|
+
console.log(
|
|
471
|
+
`[memoreal-session-end] Session is ending. Encode a session summary into Memoreal (namespace: ${ns}) with the following write gate — ONLY encode if the item would change future behavior, represents a commitment, or is a decision worth preserving. Skip transient details.\n` +
|
|
472
|
+
`1. (type: episodic, salience: 0.7) Session summary: what was the goal, what was accomplished, what remains unfinished\n` +
|
|
473
|
+
`2. (type: semantic, salience: 0.8) Key decisions made and their rationale — only decisions that affect future work\n` +
|
|
474
|
+
`3. (type: semantic, salience: 0.8) Lessons learned: mistakes made, corrections received, patterns discovered\n` +
|
|
475
|
+
`4. (type: semantic, salience: 0.6) Code patterns or architecture insights worth remembering\n` +
|
|
476
|
+
`5. (type: episodic, salience: 0.5) Open questions and next steps for the next session\n` +
|
|
477
|
+
`Include [["source", "session-end"]] in context metadata for each. Then run consolidate on namespace "${ns}".`
|
|
478
|
+
)
|
|
479
|
+
return
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
runHook().catch(() => process.exit(0))
|
package/bin/memoreal-ns
CHANGED
|
@@ -1,11 +1,61 @@
|
|
|
1
1
|
#!/bin/sh
|
|
2
2
|
# Derive Memoreal namespace from the current working directory.
|
|
3
|
-
# Strips
|
|
4
|
-
# nested under them) share the parent project's namespace.
|
|
3
|
+
# Strips worktree suffixes (.worktrees, .claude/worktrees, .opencode/worktrees, .codex/worktrees)
|
|
4
|
+
# so worktrees (and submodule worktrees nested under them) share the parent project's namespace.
|
|
5
5
|
# Usage: NS=$(memoreal-ns)
|
|
6
|
-
|
|
6
|
+
DIR="$(dirname "$0")"
|
|
7
|
+
RAW_TARGET="${1:-$PWD}"
|
|
8
|
+
if [ "$RAW_TARGET" = "--json" ]; then
|
|
9
|
+
RAW_TARGET="${2:-$PWD}"
|
|
10
|
+
fi
|
|
11
|
+
CHECK_P=$(printf '%s' "$RAW_TARGET" | tr '\\' '/')
|
|
12
|
+
case "$CHECK_P" in
|
|
13
|
+
*/.worktrees/*) CHECK_P="${CHECK_P%%/.worktrees/*}" ;;
|
|
14
|
+
*/.worktrees) CHECK_P="${CHECK_P%%/.worktrees}" ;;
|
|
15
|
+
*/.claude/worktrees/*) CHECK_P="${CHECK_P%%/.claude/worktrees/*}" ;;
|
|
16
|
+
*/.claude/worktrees) CHECK_P="${CHECK_P%%/.claude/worktrees}" ;;
|
|
17
|
+
*/.opencode/worktrees/*) CHECK_P="${CHECK_P%%/.opencode/worktrees/*}" ;;
|
|
18
|
+
*/.opencode/worktrees) CHECK_P="${CHECK_P%%/.opencode/worktrees}" ;;
|
|
19
|
+
*/.codex/worktrees/*) CHECK_P="${CHECK_P%%/.codex/worktrees/*}" ;;
|
|
20
|
+
*/.codex/worktrees) CHECK_P="${CHECK_P%%/.codex/worktrees}" ;;
|
|
21
|
+
*/.cursor/worktrees/*) CHECK_P="${CHECK_P%%/.cursor/worktrees/*}" ;;
|
|
22
|
+
*/.cursor/worktrees) CHECK_P="${CHECK_P%%/.cursor/worktrees}" ;;
|
|
23
|
+
*/.local/share/opencode/worktree/*/*) CHECK_P="${CHECK_P%%/.local/share/opencode/worktree/*}" ;;
|
|
24
|
+
esac
|
|
25
|
+
if { [ -z "$1" ] || [ -d "$1" ] || [ -d "$CHECK_P" ] || [ "$1" = "--json" ]; } && command -v node >/dev/null 2>&1 && [ -f "$DIR/memoreal-ns.mjs" ]; then
|
|
26
|
+
exec node "$DIR/memoreal-ns.mjs" "$@"
|
|
27
|
+
fi
|
|
28
|
+
P=$(printf '%s' "${1:-$PWD}" | tr '\\' '/')
|
|
7
29
|
case "$P" in
|
|
30
|
+
*/.worktrees/*) P="${P%%/.worktrees/*}" ;;
|
|
31
|
+
*/.worktrees) P="${P%%/.worktrees}" ;;
|
|
8
32
|
*/.claude/worktrees/*) P="${P%%/.claude/worktrees/*}" ;;
|
|
33
|
+
*/.claude/worktrees) P="${P%%/.claude/worktrees}" ;;
|
|
9
34
|
*/.opencode/worktrees/*) P="${P%%/.opencode/worktrees/*}" ;;
|
|
35
|
+
*/.opencode/worktrees) P="${P%%/.opencode/worktrees}" ;;
|
|
36
|
+
*/.codex/worktrees/*) P="${P%%/.codex/worktrees/*}" ;;
|
|
37
|
+
*/.codex/worktrees) P="${P%%/.codex/worktrees}" ;;
|
|
38
|
+
*/.cursor/worktrees/*) P="${P%%/.cursor/worktrees/*}" ;;
|
|
39
|
+
*/.cursor/worktrees) P="${P%%/.cursor/worktrees}" ;;
|
|
40
|
+
*/.local/share/opencode/worktree/*/*) P="${P%%/.local/share/opencode/worktree/*}" ;;
|
|
41
|
+
esac
|
|
42
|
+
case "$P" in
|
|
43
|
+
?*/) P=$(printf '%s' "$P" | sed 's|/*$||') ;;
|
|
10
44
|
esac
|
|
11
|
-
|
|
45
|
+
if [ -f "$P/.memoreal-namespace" ]; then
|
|
46
|
+
FILE_NS=$(head -n 1 "$P/.memoreal-namespace" 2>/dev/null | tr -d '\r\n')
|
|
47
|
+
FILE_NS=$(printf '%s' "$FILE_NS" | sed -e 's|^[[:space:]]*||' -e 's|[[:space:]]*$||' -e 's|^org:||i')
|
|
48
|
+
if [ -n "$FILE_NS" ]; then
|
|
49
|
+
printf '%s' "$FILE_NS"
|
|
50
|
+
exit 0
|
|
51
|
+
fi
|
|
52
|
+
fi
|
|
53
|
+
if [ -f "$P/.memoria-namespace" ]; then
|
|
54
|
+
FILE_NS=$(head -n 1 "$P/.memoria-namespace" 2>/dev/null | tr -d '\r\n')
|
|
55
|
+
FILE_NS=$(printf '%s' "$FILE_NS" | sed -e 's|^[[:space:]]*||' -e 's|[[:space:]]*$||' -e 's|^org:||i')
|
|
56
|
+
if [ -n "$FILE_NS" ]; then
|
|
57
|
+
printf '%s' "$FILE_NS"
|
|
58
|
+
exit 0
|
|
59
|
+
fi
|
|
60
|
+
fi
|
|
61
|
+
printf '%s' "$P" | sed -e 's|^\([A-Za-z]\):/*|\1-|' -e 's|[:/]|-|g'
|