rag-memory-epf-mcp 3.3.0 → 3.3.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/dist/index.js CHANGED
@@ -39,6 +39,40 @@ const DB_FILE_PATH = process.env.DB_FILE_PATH
39
39
  : defaultDbPath;
40
40
  const EMBEDDING_MODEL = process.env.EMBEDDING_MODEL || 'Xenova/bge-m3';
41
41
  // Safe rowid for vec0 virtual tables (require literal integer, not parameterized)
42
+ // Trim incomplete UTF-8 multi-byte sequences at chunk boundaries.
43
+ // Continuation bytes match 10xxxxxx (0x80-0xBF); lead bytes indicate how many
44
+ // bytes the sequence needs (0xxxxxxx=1, 110xxxxx=2, 1110xxxx=3, 11110xxx=4).
45
+ // When a chunk is not at the document head/tail, any partial sequence at that
46
+ // edge belongs to an adjacent chunk and must be removed so TextDecoder does
47
+ // not emit U+FFFD. Pass trimHead/trimTail=false to preserve head/tail bytes.
48
+ function trimIncompleteUtf8(bytes, trimHead, trimTail) {
49
+ let start = 0;
50
+ let end = bytes.length;
51
+ if (trimHead) {
52
+ while (start < end && (bytes[start] & 0xC0) === 0x80)
53
+ start++;
54
+ }
55
+ if (trimTail) {
56
+ let i = end - 1;
57
+ while (i >= start && (bytes[i] & 0xC0) === 0x80)
58
+ i--;
59
+ if (i >= start) {
60
+ const lead = bytes[i];
61
+ let needed = 1;
62
+ if ((lead & 0x80) === 0)
63
+ needed = 1;
64
+ else if ((lead & 0xE0) === 0xC0)
65
+ needed = 2;
66
+ else if ((lead & 0xF0) === 0xE0)
67
+ needed = 3;
68
+ else if ((lead & 0xF8) === 0xF0)
69
+ needed = 4;
70
+ if (end - i < needed)
71
+ end = i;
72
+ }
73
+ }
74
+ return bytes.subarray(start, end);
75
+ }
42
76
  function safeRowid(value) {
43
77
  const n = Number(value);
44
78
  if (!Number.isInteger(n) || n < 0) {
@@ -154,7 +188,7 @@ class RAGKnowledgeGraphManager {
154
188
  VALUES (?, ?, ?, ?, ?)
155
189
  `);
156
190
  for (const entity of entities) {
157
- const entityId = `entity_${entity.name.toLowerCase().replace(/[^a-z0-9]/g, '_')}`;
191
+ const entityId = `entity_${entity.name.toLowerCase().replace(/[^\p{L}\p{N}]/gu, '_')}`;
158
192
  const timestamped = (entity.observations || []).map(o => this._timestampObservation(o));
159
193
  // Try insert first
160
194
  const insertResult = insertStmt.run(entityId, entity.name, entity.entityType, JSON.stringify(timestamped), '{}');
@@ -199,8 +233,8 @@ class RAGKnowledgeGraphManager {
199
233
  { name: relation.from, entityType: 'CONCEPT', observations: [] },
200
234
  { name: relation.to, entityType: 'CONCEPT', observations: [] }
201
235
  ]);
202
- const sourceId = `entity_${relation.from.toLowerCase().replace(/[^a-z0-9]/g, '_')}`;
203
- const targetId = `entity_${relation.to.toLowerCase().replace(/[^a-z0-9]/g, '_')}`;
236
+ const sourceId = `entity_${relation.from.toLowerCase().replace(/[^\p{L}\p{N}]/gu, '_')}`;
237
+ const targetId = `entity_${relation.to.toLowerCase().replace(/[^\p{L}\p{N}]/gu, '_')}`;
204
238
  const relationId = `rel_${sourceId}_${relation.relationType}_${targetId}`.toLowerCase();
205
239
  const stmt = this.db.prepare(`
206
240
  INSERT OR IGNORE INTO relationships
@@ -219,7 +253,7 @@ class RAGKnowledgeGraphManager {
219
253
  throw new Error('Database not initialized');
220
254
  const results = [];
221
255
  for (const obs of observations) {
222
- const entityId = `entity_${obs.entityName.toLowerCase().replace(/[^a-z0-9]/g, '_')}`;
256
+ const entityId = `entity_${obs.entityName.toLowerCase().replace(/[^\p{L}\p{N}]/gu, '_')}`;
223
257
  // Get current observations
224
258
  const entity = this.db.prepare(`
225
259
  SELECT observations FROM entities WHERE id = ?
@@ -250,7 +284,7 @@ class RAGKnowledgeGraphManager {
250
284
  throw new Error('Database not initialized');
251
285
  console.error(`🗑️ Deleting entities: ${entityNames.join(', ')}`);
252
286
  for (const name of entityNames) {
253
- const entityId = `entity_${name.toLowerCase().replace(/[^a-z0-9]/g, '_')}`;
287
+ const entityId = `entity_${name.toLowerCase().replace(/[^\p{L}\p{N}]/gu, '_')}`;
254
288
  try {
255
289
  // Check if entity exists first
256
290
  const entityExists = this.db.prepare(`
@@ -312,7 +346,7 @@ class RAGKnowledgeGraphManager {
312
346
  if (!this.db)
313
347
  throw new Error('Database not initialized');
314
348
  for (const deletion of deletions) {
315
- const entityId = `entity_${deletion.entityName.toLowerCase().replace(/[^a-z0-9]/g, '_')}`;
349
+ const entityId = `entity_${deletion.entityName.toLowerCase().replace(/[^\p{L}\p{N}]/gu, '_')}`;
316
350
  const entity = this.db.prepare(`
317
351
  SELECT observations FROM entities WHERE id = ?
318
352
  `).get(entityId);
@@ -329,8 +363,8 @@ class RAGKnowledgeGraphManager {
329
363
  if (!this.db)
330
364
  throw new Error('Database not initialized');
331
365
  for (const relation of relations) {
332
- const sourceId = `entity_${relation.from.toLowerCase().replace(/[^a-z0-9]/g, '_')}`;
333
- const targetId = `entity_${relation.to.toLowerCase().replace(/[^a-z0-9]/g, '_')}`;
366
+ const sourceId = `entity_${relation.from.toLowerCase().replace(/[^\p{L}\p{N}]/gu, '_')}`;
367
+ const targetId = `entity_${relation.to.toLowerCase().replace(/[^\p{L}\p{N}]/gu, '_')}`;
334
368
  this.db.prepare(`
335
369
  DELETE FROM relationships
336
370
  WHERE source_entity = ? AND target_entity = ? AND relationType = ?
@@ -343,8 +377,8 @@ class RAGKnowledgeGraphManager {
343
377
  let updated = 0;
344
378
  let notFound = 0;
345
379
  for (const update of updates) {
346
- const sourceId = `entity_${update.from.toLowerCase().replace(/[^a-z0-9]/g, '_')}`;
347
- const targetId = `entity_${update.to.toLowerCase().replace(/[^a-z0-9]/g, '_')}`;
380
+ const sourceId = `entity_${update.from.toLowerCase().replace(/[^\p{L}\p{N}]/gu, '_')}`;
381
+ const targetId = `entity_${update.to.toLowerCase().replace(/[^\p{L}\p{N}]/gu, '_')}`;
348
382
  const relationId = `rel_${sourceId}_${update.relationType}_${targetId}`.toLowerCase();
349
383
  // Check if relation exists
350
384
  const existing = this.db.prepare(`
@@ -407,7 +441,7 @@ class RAGKnowledgeGraphManager {
407
441
  // Cap depth at 5 to prevent runaway queries
408
442
  const effectiveDepth = Math.min(Math.max(depth, 1), 5);
409
443
  // Convert entity names to IDs
410
- const seedIds = entityNames.map(name => `entity_${name.toLowerCase().replace(/[^a-z0-9]/g, '_')}`);
444
+ const seedIds = entityNames.map(name => `entity_${name.toLowerCase().replace(/[^\p{L}\p{N}]/gu, '_')}`);
411
445
  // Build dynamic placeholders for the seed IDs
412
446
  const seedPlaceholders = seedIds.map(() => '?').join(',');
413
447
  // Build the recursive CTE query
@@ -1098,6 +1132,10 @@ class RAGKnowledgeGraphManager {
1098
1132
  return Array.from(terms);
1099
1133
  }
1100
1134
  // Tokenize and chunk text
1135
+ // BPE tokenizers (cl100k_base) split multi-byte UTF-8 sequences across tokens.
1136
+ // Slicing token arrays at arbitrary boundaries can leave incomplete UTF-8
1137
+ // prefix/suffix bytes, which TextDecoder replaces with U+FFFD (�). Trim the
1138
+ // incomplete sequences at chunk boundaries; overlap covers the removed bytes.
1101
1139
  chunkText(text, maxTokens = 800, overlap = 160) {
1102
1140
  if (!this.encoding)
1103
1141
  throw new Error('Tokenizer not initialized');
@@ -1106,7 +1144,10 @@ class RAGKnowledgeGraphManager {
1106
1144
  for (let i = 0; i < tokens.length; i += maxTokens - overlap) {
1107
1145
  const chunkTokens = tokens.slice(i, i + maxTokens);
1108
1146
  const decodedBytes = this.encoding.decode(chunkTokens);
1109
- const chunkText = new TextDecoder().decode(decodedBytes);
1147
+ const isFirst = i === 0;
1148
+ const isLast = i + chunkTokens.length >= tokens.length;
1149
+ const safeBytes = trimIncompleteUtf8(decodedBytes, !isFirst, !isLast);
1150
+ const chunkText = new TextDecoder('utf-8').decode(safeBytes);
1110
1151
  chunks.push({
1111
1152
  id: '',
1112
1153
  document_id: '',
@@ -1364,7 +1405,7 @@ class RAGKnowledgeGraphManager {
1364
1405
  INSERT OR IGNORE INTO chunk_entities (chunk_rowid, entity_id) VALUES (?, ?)
1365
1406
  `);
1366
1407
  for (const entityName of entityNames) {
1367
- const entityId = `entity_${entityName.toLowerCase().replace(/[^a-z0-9]/g, '_')}`;
1408
+ const entityId = `entity_${entityName.toLowerCase().replace(/[^\p{L}\p{N}]/gu, '_')}`;
1368
1409
  // Verify entity exists
1369
1410
  const entity = this.db.prepare(`
1370
1411
  SELECT id FROM entities WHERE id = ?
@@ -2144,7 +2185,7 @@ class RAGKnowledgeGraphManager {
2144
2185
  if (entityNames && entityNames.length > 0) {
2145
2186
  const targetIds = new Set();
2146
2187
  for (const name of entityNames) {
2147
- const id = `entity_${name.toLowerCase().replace(/[^a-z0-9]/g, '_')}`;
2188
+ const id = `entity_${name.toLowerCase().replace(/[^\p{L}\p{N}]/gu, '_')}`;
2148
2189
  if (graph.hasNode(id))
2149
2190
  targetIds.add(id);
2150
2191
  }
Binary file
Binary file
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rag-memory-epf-mcp",
3
- "version": "3.3.0",
3
+ "version": "3.3.2",
4
4
  "description": "MCP server for project-local RAG memory with knowledge graph and multilingual vector search",
5
5
  "license": "MIT",
6
6
  "author": "bripin123",