rag-memory-epf-mcp 3.3.1 → 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 +42 -1
- package/package.json +1 -1
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) {
|
|
@@ -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
|
|
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: '',
|