@nhatnguyen9317/memory-mcp-server 1.3.3 → 1.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/dist/utils.js CHANGED
@@ -28,11 +28,7 @@ export function parseJsonObject(text) {
28
28
  return null;
29
29
  }
30
30
  }
31
- /** FTS5 MATCH queries break on raw punctuation; wrap terms as a safe prefix query. */
32
- export function toFtsQuery(raw) {
33
- const terms = ftsTerms(raw);
34
- return terms.length ? terms.join(" ") : '""';
35
- }
31
+ /** Splits raw text into quoted-prefix FTS5 terms so punctuation cannot break MATCH. */
36
32
  function ftsTerms(raw) {
37
33
  return raw
38
34
  .trim()
@@ -41,7 +37,7 @@ function ftsTerms(raw) {
41
37
  .map((t) => `"${t.replace(/"/g, '""')}"*`);
42
38
  }
43
39
  /**
44
- * memory_search terms: the same quoted-prefix terms as toFtsQuery, deduped
40
+ * memory_search terms: quoted-prefix FTS terms, deduped
45
41
  * case-insensitively so a repeated word cannot inflate the coverage floor.
46
42
  * Punctuation-only tokens ("-", "–", "/") never match anything, so they are
47
43
  * dropped rather than counted toward the floor. No stopword or length
@@ -123,7 +119,7 @@ export function handleToolError(error) {
123
119
  return `Error: Invalid value provided (${error.message}). Check allowed ranges/enums in the tool description.`;
124
120
  }
125
121
  if (error.message.includes("FOREIGN KEY constraint failed")) {
126
- return "Error: Referenced session_id does not exist. Use reasoning_start_session first, or check reasoning_list_sessions for valid IDs.";
122
+ return "Error: Referenced session_id does not exist. Use reasoning_start_session first, or reasoning_find to look up an existing session.";
127
123
  }
128
124
  if (error.message.includes("UNIQUE constraint failed")) {
129
125
  return "Error: A record with this identifier already exists.";
@@ -132,4 +128,101 @@ export function handleToolError(error) {
132
128
  }
133
129
  return `Error: Unexpected error occurred: ${String(error)}`;
134
130
  }
131
+ /** Whitespace-collapsed text cut to `max` chars (default 160; "..." when cut). */
132
+ export function compactSnippetText(text, max = 160) {
133
+ const compact = text.replace(/\s+/g, " ").trim();
134
+ return compact.length > max ? `${compact.slice(0, max - 3)}...` : compact;
135
+ }
136
+ /** Reverses the FTS quoting of toRecallTerms/toSearchTerms: `"to""k"*` -> `to"k`. */
137
+ export function unquoteFtsTerm(term) {
138
+ return term.replace(/^"/, "").replace(/"\*?$/, "").replace(/""/g, '"');
139
+ }
140
+ function escapeRegExp(value) {
141
+ return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
142
+ }
143
+ /** Case-insensitive pattern matching `word` at the start of a word. */
144
+ export function wordPrefixPattern(word) {
145
+ return new RegExp(`(^|[^\\p{L}\\p{N}])(${escapeRegExp(word)})`, "iu");
146
+ }
147
+ /**
148
+ * Earliest case-insensitive word-prefix match of any word. Literal only:
149
+ * FTS folds diacritics, this does not, so an FTS hit can return null here.
150
+ */
151
+ function findFirstMatch(text, words) {
152
+ let best = null;
153
+ for (const word of words) {
154
+ if (!word)
155
+ continue;
156
+ const match = wordPrefixPattern(word).exec(text);
157
+ if (!match)
158
+ continue;
159
+ const index = match.index + match[1].length;
160
+ if (!best || index < best.index)
161
+ best = { index, length: match[2].length };
162
+ }
163
+ return best;
164
+ }
165
+ /**
166
+ * Slices [start, end) and trims partial words at both edges (within 20
167
+ * chars), never cutting into `keep`.
168
+ */
169
+ function sliceToWords(text, start, end, keep) {
170
+ let s = Math.max(0, start);
171
+ let e = Math.min(text.length, end);
172
+ if (s > 0) {
173
+ const space = text.indexOf(" ", s);
174
+ if (space !== -1 && space < s + 20 && (!keep || space < keep.index))
175
+ s = space + 1;
176
+ }
177
+ if (e < text.length) {
178
+ const space = text.lastIndexOf(" ", e);
179
+ if (space > s && space > e - 20 && (!keep || space >= keep.index + keep.length))
180
+ e = space;
181
+ }
182
+ return text.slice(s, e).trim();
183
+ }
184
+ /** A `size`-bounded excerpt centred on the first match; head cut when none. */
185
+ export function buildMatchExcerpt(text, words, size = 160) {
186
+ const compact = text.replace(/\s+/g, " ").trim();
187
+ if (compact.length <= size)
188
+ return compact;
189
+ const match = findFirstMatch(compact, words);
190
+ if (!match)
191
+ return `${compact.slice(0, size - 3)}...`;
192
+ const room = size - 6; // leading and trailing "..."
193
+ const start = Math.max(0, match.index - Math.floor(room / 3));
194
+ const end = start + room;
195
+ const body = sliceToWords(compact, start, end, match);
196
+ return `${start > 0 ? "..." : ""}${body}${end < compact.length ? "..." : ""}`;
197
+ }
198
+ const RECALL_SNIPPET_MAX = 160;
199
+ const RECALL_SNIPPET_HEAD = 60;
200
+ const RECALL_SNIPPET_SEPARATOR = " … ";
201
+ /**
202
+ * Auto-recall snippet: the head when the first title-term match is already
203
+ * visible in it (identical to compactSnippetText), otherwise ~60 chars of
204
+ * head + " … " + a window around the match. Falls back to the head when no
205
+ * term occurs literally (tag-only or diacritic-folded FTS matches) or on
206
+ * any error.
207
+ */
208
+ export function buildRecallSnippet(content, terms) {
209
+ try {
210
+ const compact = content.replace(/\s+/g, " ").trim();
211
+ const head = compactSnippetText(compact);
212
+ if (compact.length <= RECALL_SNIPPET_MAX)
213
+ return head;
214
+ const match = findFirstMatch(compact, terms.map(unquoteFtsTerm));
215
+ if (!match || match.index + match.length <= RECALL_SNIPPET_MAX - 3)
216
+ return head;
217
+ const headPart = sliceToWords(compact, 0, RECALL_SNIPPET_HEAD);
218
+ const room = RECALL_SNIPPET_MAX - headPart.length - RECALL_SNIPPET_SEPARATOR.length - 3;
219
+ const start = Math.max(headPart.length, match.index - Math.floor(room / 3));
220
+ const end = start + room;
221
+ const windowText = sliceToWords(compact, start, end, match);
222
+ return `${headPart}${RECALL_SNIPPET_SEPARATOR}${windowText}${end < compact.length ? "..." : ""}`;
223
+ }
224
+ catch {
225
+ return compactSnippetText(content);
226
+ }
227
+ }
135
228
  //# sourceMappingURL=utils.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nhatnguyen9317/memory-mcp-server",
3
- "version": "1.3.3",
3
+ "version": "1.4.0",
4
4
  "keywords": [
5
5
  "mcp",
6
6
  "model-context-protocol",