@nhatnguyen9317/memory-mcp-server 1.3.2 → 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()
@@ -40,19 +36,61 @@ function ftsTerms(raw) {
40
36
  .filter(Boolean)
41
37
  .map((t) => `"${t.replace(/"/g, '""')}"*`);
42
38
  }
39
+ /**
40
+ * memory_search terms: quoted-prefix FTS terms, deduped
41
+ * case-insensitively so a repeated word cannot inflate the coverage floor.
42
+ * Punctuation-only tokens ("-", "–", "/") never match anything, so they are
43
+ * dropped rather than counted toward the floor. No stopword or length
44
+ * filtering — an explicit query is the caller's intent.
45
+ */
46
+ export function toSearchTerms(raw) {
47
+ const seen = new Set();
48
+ const words = raw
49
+ .trim()
50
+ .split(/\s+/)
51
+ .filter((token) => /[\p{L}\p{N}]/u.test(token))
52
+ .join(" ");
53
+ return ftsTerms(words).filter((term) => {
54
+ const key = term.toLowerCase();
55
+ if (seen.has(key))
56
+ return false;
57
+ seen.add(key);
58
+ return true;
59
+ });
60
+ }
61
+ /**
62
+ * Generic task words and function words. They match nearly every memory,
63
+ * so counting them toward the recall floor lets unrelated memories in.
64
+ * Includes a few common Vietnamese words (titles are often mixed-language).
65
+ */
66
+ const RECALL_STOPWORDS = new Set([
67
+ "fix", "fixes", "fixing", "add", "adds", "adding", "update", "updates",
68
+ "updating", "improve", "improves", "improving", "implement",
69
+ "implementing", "review", "investigate", "investigating", "debug",
70
+ "debugging", "bug", "bugs", "issue", "issues", "task", "tasks",
71
+ "the", "and", "for", "with", "from", "into", "that", "this", "are", "not",
72
+ "không", "của", "cho", "với", "các", "những", "một", "cải", "tiến",
73
+ "sửa", "lỗi", "thêm",
74
+ ]);
75
+ function bareToken(token) {
76
+ return token.toLowerCase().replace(/^[^\p{L}\p{N}]+|[^\p{L}\p{N}]+$/gu, "");
77
+ }
43
78
  /**
44
79
  * Term preparation for auto-recall only. Drops noise tokens (<=2 chars,
45
- * the main source of one-word junk matches) and caps the count so long
46
- * titles stay cheap. memory_search keeps raw ftsTerms behavior — an
47
- * explicit query is the caller's intent.
80
+ * the main source of one-word junk matches) and generic stopwords, and caps
81
+ * the count so long titles stay cheap. If filtering would leave nothing, it
82
+ * falls back to the unfiltered tokens. memory_search uses toSearchTerms (no
83
+ * filtering) — an explicit query is the caller's intent.
48
84
  */
49
85
  export function toRecallTerms(raw) {
50
86
  const tokens = raw.trim().split(/\s+/).filter(Boolean);
51
87
  const significant = tokens.filter((token) => token.length > 2);
88
+ const topical = significant.filter((token) => !RECALL_STOPWORDS.has(bareToken(token)));
89
+ const base = topical.length > 0 ? topical : significant.length > 0 ? significant : tokens;
52
90
  // Dedupe case-insensitively: a repeated word must not raise the
53
91
  // match floor or double-count as two matched terms.
54
92
  const seen = new Set();
55
- const chosen = (significant.length > 0 ? significant : tokens)
93
+ const chosen = base
56
94
  .filter((token) => {
57
95
  const key = token.toLowerCase();
58
96
  if (seen.has(key))
@@ -81,7 +119,7 @@ export function handleToolError(error) {
81
119
  return `Error: Invalid value provided (${error.message}). Check allowed ranges/enums in the tool description.`;
82
120
  }
83
121
  if (error.message.includes("FOREIGN KEY constraint failed")) {
84
- 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.";
85
123
  }
86
124
  if (error.message.includes("UNIQUE constraint failed")) {
87
125
  return "Error: A record with this identifier already exists.";
@@ -90,4 +128,101 @@ export function handleToolError(error) {
90
128
  }
91
129
  return `Error: Unexpected error occurred: ${String(error)}`;
92
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
+ }
93
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.2",
3
+ "version": "1.4.0",
4
4
  "keywords": [
5
5
  "mcp",
6
6
  "model-context-protocol",