create-ai-memory 0.14.1 → 0.14.2
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/bin/ai-mem-serve.js +83 -15
- package/package.json +1 -1
package/bin/ai-mem-serve.js
CHANGED
|
@@ -82,10 +82,45 @@ function frontmatter(text) {
|
|
|
82
82
|
return out;
|
|
83
83
|
}
|
|
84
84
|
|
|
85
|
-
/**
|
|
86
|
-
function
|
|
87
|
-
const
|
|
88
|
-
return
|
|
85
|
+
/** The note's first H1, or null. */
|
|
86
|
+
function h1Of(text) {
|
|
87
|
+
const m = text.match(/^#\s+(.+)$/m);
|
|
88
|
+
return m ? m[1].trim() : null;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* What to call a note on the graph.
|
|
93
|
+
*
|
|
94
|
+
* The obvious answer -- its first H1 -- is wrong here, because notes are
|
|
95
|
+
* created from templates and inherit the template's heading. On a real vault
|
|
96
|
+
* that made 354 notes read "Session Outcome" and 34 read "Project Snapshot":
|
|
97
|
+
* every project and every log sharing two labels between them, which is worse
|
|
98
|
+
* than no labels at all.
|
|
99
|
+
*
|
|
100
|
+
* So an H1 is only a title if it is *this note's* H1. One shared by several
|
|
101
|
+
* notes is boilerplate by definition, and the rule needs no list of known
|
|
102
|
+
* template headings to maintain -- it corrects itself for any template,
|
|
103
|
+
* including ones a user writes.
|
|
104
|
+
*
|
|
105
|
+
* @param {string} text the note
|
|
106
|
+
* @param {string} path its path
|
|
107
|
+
* @param {object} fm parsed frontmatter
|
|
108
|
+
* @param {Map} h1Counts how many notes share each H1
|
|
109
|
+
*/
|
|
110
|
+
function titleOf(text, path, fm, h1Counts) {
|
|
111
|
+
if (fm.title) return fm.title;
|
|
112
|
+
if (fm.project_name) return fm.project_name;
|
|
113
|
+
|
|
114
|
+
const h1 = h1Of(text);
|
|
115
|
+
if (h1 && h1Counts.get(h1) === 1) return h1;
|
|
116
|
+
|
|
117
|
+
// Fall back to the filename, which is always specific. A session log's name
|
|
118
|
+
// is <project>-<date>_<time>, and the project is already obvious from the
|
|
119
|
+
// node it is attached to -- so show the moment, which is the part that
|
|
120
|
+
// distinguishes it from its forty siblings.
|
|
121
|
+
const name = basename(path, '.md');
|
|
122
|
+
const stamp = name.match(/(\d{4}-\d{2}-\d{2})_(\d{2})-(\d{2})-\d{2}$/);
|
|
123
|
+
return stamp ? stamp[1] + ' ' + stamp[2] + ':' + stamp[3] : name;
|
|
89
124
|
}
|
|
90
125
|
|
|
91
126
|
const LINK_RE = /\[\[([^\]|#]+)/g;
|
|
@@ -103,6 +138,11 @@ function buildGraph() {
|
|
|
103
138
|
const nodes = [];
|
|
104
139
|
const byBasename = new Map();
|
|
105
140
|
|
|
141
|
+
// Read once, keep the text: the vault is a few megabytes of markdown, and
|
|
142
|
+
// reading every file twice to count headings first would double the work
|
|
143
|
+
// for no reason.
|
|
144
|
+
const read = [];
|
|
145
|
+
const h1Counts = new Map();
|
|
106
146
|
for (const f of files) {
|
|
107
147
|
let text;
|
|
108
148
|
try {
|
|
@@ -110,10 +150,16 @@ function buildGraph() {
|
|
|
110
150
|
} catch {
|
|
111
151
|
continue;
|
|
112
152
|
}
|
|
153
|
+
read.push([f, text]);
|
|
154
|
+
const h = h1Of(text);
|
|
155
|
+
if (h) h1Counts.set(h, (h1Counts.get(h) || 0) + 1);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
for (const [f, text] of read) {
|
|
113
159
|
const fm = frontmatter(text);
|
|
114
160
|
const node = {
|
|
115
161
|
id: relative(VAULT, f),
|
|
116
|
-
title:
|
|
162
|
+
title: titleOf(text, f, fm, h1Counts),
|
|
117
163
|
type: fm.type || 'untyped',
|
|
118
164
|
tags: Array.isArray(fm.tags) ? fm.tags : fm.tags ? [fm.tags] : [],
|
|
119
165
|
description: fm.description || '',
|
|
@@ -225,16 +271,38 @@ server.on('error', (err) => {
|
|
|
225
271
|
}
|
|
226
272
|
const url = 'http://127.0.0.1:' + PORT;
|
|
227
273
|
http.get(url + '/api/graph', (res) => {
|
|
228
|
-
if (res.statusCode !== 200) return bail();
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
274
|
+
if (res.statusCode !== 200) return bail('something else');
|
|
275
|
+
let body = '';
|
|
276
|
+
res.setEncoding('utf8');
|
|
277
|
+
res.on('data', (c) => { body += c; });
|
|
278
|
+
res.on('end', () => {
|
|
279
|
+
// Being an ai-mem-serve is not enough -- it has to be serving THIS
|
|
280
|
+
// vault. A leftover server from a test run or another vault answers
|
|
281
|
+
// /api/graph perfectly well, and attaching to it silently shows the
|
|
282
|
+
// wrong notes while reporting success. That is the failure this whole
|
|
283
|
+
// tool exists to avoid, so compare the vault it names.
|
|
284
|
+
let serving;
|
|
285
|
+
try {
|
|
286
|
+
serving = JSON.parse(body).vault;
|
|
287
|
+
} catch {
|
|
288
|
+
return bail('something else');
|
|
289
|
+
}
|
|
290
|
+
if (serving !== VAULT) {
|
|
291
|
+
console.error('ai-mem-serve: port ' + PORT + ' is already serving a different vault:');
|
|
292
|
+
console.error(' running: ' + serving);
|
|
293
|
+
console.error(' wanted: ' + VAULT);
|
|
294
|
+
console.error('Stop that server, or pass another port: ai-mem-serve ' + (PORT + 1));
|
|
295
|
+
process.exit(1);
|
|
296
|
+
}
|
|
297
|
+
console.log('ai-mem-serve: already running');
|
|
298
|
+
console.log(' ' + url);
|
|
299
|
+
openBrowser(url);
|
|
300
|
+
process.exit(0);
|
|
301
|
+
});
|
|
302
|
+
}).on('error', () => bail('something else'));
|
|
303
|
+
|
|
304
|
+
function bail(what) {
|
|
305
|
+
console.error('ai-mem-serve: port ' + PORT + ' is in use by ' + what + '. Pass a different port.');
|
|
238
306
|
process.exit(1);
|
|
239
307
|
}
|
|
240
308
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "create-ai-memory",
|
|
3
|
-
"version": "0.14.
|
|
3
|
+
"version": "0.14.2",
|
|
4
4
|
"description": "Persistent, agent-agnostic session memory for AI coding CLIs (Claude Code, Codex, Gemini, Cursor, opencode). One Markdown vault, any agent.",
|
|
5
5
|
"bin": {
|
|
6
6
|
"create-ai-memory": "bin/create-ai-memory.js",
|