@mastra/rag 2.6.5-alpha.0 → 2.6.5-alpha.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/docs/SKILL.md +1 -1
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/document/transformers/character.d.ts.map +1 -1
- package/dist/document/transformers/sentence.d.ts.map +1 -1
- package/dist/document/transformers/text.d.ts +6 -1
- package/dist/document/transformers/text.d.ts.map +1 -1
- package/dist/index.cjs +100 -40
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +100 -40
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -3958,46 +3958,66 @@ var TextTransformer = class {
|
|
|
3958
3958
|
if (this.stripWhitespace) text = text.trim();
|
|
3959
3959
|
return text === "" ? null : text;
|
|
3960
3960
|
}
|
|
3961
|
-
|
|
3961
|
+
/**
|
|
3962
|
+
* Merges splits into chunks of at most `maxSize`.
|
|
3963
|
+
* `joiners[i]`, when given, is the text placed before `splits[i]` when it is
|
|
3964
|
+
* joined to a preceding piece; otherwise `separator` is used for every piece.
|
|
3965
|
+
*/
|
|
3966
|
+
mergeSplits(splits, separator, joiners) {
|
|
3962
3967
|
const docs = [];
|
|
3963
3968
|
let currentDoc = [];
|
|
3969
|
+
let currentJoiners = [];
|
|
3964
3970
|
let total = 0;
|
|
3965
|
-
|
|
3971
|
+
const lengthOf = (s) => s ? this.lengthFunction(s) : 0;
|
|
3972
|
+
const join = (pieces, pieceJoiners) => {
|
|
3973
|
+
if (!joiners) return this.joinDocs(pieces, separator);
|
|
3974
|
+
const text = pieces.map((piece, i) => i === 0 ? piece : pieceJoiners[i] + piece).join("");
|
|
3975
|
+
return this.joinDocs([text], "");
|
|
3976
|
+
};
|
|
3977
|
+
splits.forEach((d, index) => {
|
|
3966
3978
|
const len = this.lengthFunction(d);
|
|
3967
|
-
const
|
|
3979
|
+
const joiner = joiners ? joiners[index] ?? separator : separator;
|
|
3980
|
+
const separatorLen = lengthOf(joiner);
|
|
3968
3981
|
if (total + len + (currentDoc.length > 0 ? separatorLen : 0) > this.maxSize) {
|
|
3969
3982
|
if (total > this.maxSize) console.warn(`Created a chunk of size ${total}, which is longer than the specified ${this.maxSize}`);
|
|
3970
3983
|
if (currentDoc.length > 0) {
|
|
3971
|
-
const doc =
|
|
3984
|
+
const doc = join(currentDoc, currentJoiners);
|
|
3972
3985
|
if (doc !== null) docs.push(doc);
|
|
3973
3986
|
if (this.overlap > 0) {
|
|
3974
|
-
|
|
3987
|
+
const overlapContent = [];
|
|
3988
|
+
const overlapJoiners = [];
|
|
3975
3989
|
let overlapSize = 0;
|
|
3976
3990
|
for (let i = currentDoc.length - 1; i >= 0; i--) {
|
|
3977
3991
|
const piece = currentDoc[i];
|
|
3978
3992
|
const pieceLen = this.lengthFunction(piece);
|
|
3979
|
-
|
|
3993
|
+
const connectorLen = overlapContent.length > 0 ? lengthOf(currentJoiners[i + 1]) : 0;
|
|
3994
|
+
if (overlapSize + pieceLen + connectorLen > this.overlap) break;
|
|
3980
3995
|
overlapContent.unshift(piece);
|
|
3981
|
-
|
|
3996
|
+
overlapJoiners.unshift(currentJoiners[i]);
|
|
3997
|
+
overlapSize += pieceLen + connectorLen;
|
|
3982
3998
|
}
|
|
3983
3999
|
while (overlapContent.length > 0 && overlapSize + len + separatorLen > this.maxSize) {
|
|
3984
4000
|
const removed = overlapContent.shift();
|
|
4001
|
+
overlapJoiners.shift();
|
|
3985
4002
|
const removedLen = this.lengthFunction(removed);
|
|
3986
|
-
overlapSize -= removedLen + (overlapContent.length > 0 ?
|
|
4003
|
+
overlapSize -= removedLen + (overlapContent.length > 0 ? lengthOf(overlapJoiners[0]) : 0);
|
|
3987
4004
|
}
|
|
3988
4005
|
currentDoc = overlapContent;
|
|
4006
|
+
currentJoiners = overlapJoiners;
|
|
3989
4007
|
total = overlapSize;
|
|
3990
4008
|
} else {
|
|
3991
4009
|
currentDoc = [];
|
|
4010
|
+
currentJoiners = [];
|
|
3992
4011
|
total = 0;
|
|
3993
4012
|
}
|
|
3994
4013
|
}
|
|
3995
4014
|
}
|
|
3996
4015
|
currentDoc.push(d);
|
|
4016
|
+
currentJoiners.push(joiner);
|
|
3997
4017
|
total += len + (currentDoc.length > 1 ? separatorLen : 0);
|
|
3998
|
-
}
|
|
4018
|
+
});
|
|
3999
4019
|
if (currentDoc.length > 0) {
|
|
4000
|
-
const doc =
|
|
4020
|
+
const doc = join(currentDoc, currentJoiners);
|
|
4001
4021
|
if (doc !== null) docs.push(doc);
|
|
4002
4022
|
}
|
|
4003
4023
|
return docs;
|
|
@@ -4027,6 +4047,30 @@ function splitTextWithRegex(text, separator, separatorPosition) {
|
|
|
4027
4047
|
}
|
|
4028
4048
|
return result.filter((s) => s !== "");
|
|
4029
4049
|
}
|
|
4050
|
+
function splitTextWithMatchedSeparators(text, separator) {
|
|
4051
|
+
const splits = [];
|
|
4052
|
+
const joiners = [];
|
|
4053
|
+
let pending = "";
|
|
4054
|
+
let lastIndex = 0;
|
|
4055
|
+
const push = (piece) => {
|
|
4056
|
+
if (piece !== "") {
|
|
4057
|
+
splits.push(piece);
|
|
4058
|
+
joiners.push(pending);
|
|
4059
|
+
pending = "";
|
|
4060
|
+
}
|
|
4061
|
+
};
|
|
4062
|
+
for (const match of text.matchAll(new RegExp(separator, "g"))) {
|
|
4063
|
+
if (match[0] === "" && (match.index === 0 || match.index === text.length)) continue;
|
|
4064
|
+
push(text.slice(lastIndex, match.index));
|
|
4065
|
+
pending += match[0];
|
|
4066
|
+
lastIndex = match.index + match[0].length;
|
|
4067
|
+
}
|
|
4068
|
+
push(text.slice(lastIndex));
|
|
4069
|
+
return {
|
|
4070
|
+
splits,
|
|
4071
|
+
joiners
|
|
4072
|
+
};
|
|
4073
|
+
}
|
|
4030
4074
|
var CharacterTransformer = class extends TextTransformer {
|
|
4031
4075
|
separator;
|
|
4032
4076
|
isSeparatorRegex;
|
|
@@ -4096,26 +4140,33 @@ var RecursiveCharacterTransformer = class RecursiveCharacterTransformer extends
|
|
|
4096
4140
|
break;
|
|
4097
4141
|
}
|
|
4098
4142
|
}
|
|
4099
|
-
const
|
|
4143
|
+
const _separator = this.isSeparatorRegex ? separator : separator?.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
4144
|
+
const { splits, joiners } = this.isSeparatorRegex && !this.separatorPosition && !!_separator ? splitTextWithMatchedSeparators(text, _separator) : {
|
|
4145
|
+
splits: splitTextWithRegex(text, _separator, this.separatorPosition),
|
|
4146
|
+
joiners: void 0
|
|
4147
|
+
};
|
|
4100
4148
|
const goodSplits = [];
|
|
4149
|
+
const goodJoiners = [];
|
|
4101
4150
|
const mergeSeparator = this.separatorPosition ? "" : separator;
|
|
4102
|
-
|
|
4103
|
-
|
|
4104
|
-
if (
|
|
4105
|
-
|
|
4106
|
-
|
|
4107
|
-
|
|
4108
|
-
|
|
4109
|
-
|
|
4110
|
-
|
|
4111
|
-
|
|
4112
|
-
|
|
4151
|
+
const merge = () => this.mergeSplits(goodSplits, mergeSeparator, joiners ? goodJoiners : void 0);
|
|
4152
|
+
splits.forEach((s, index) => {
|
|
4153
|
+
if (this.lengthFunction(s) < this.maxSize) {
|
|
4154
|
+
goodSplits.push(s);
|
|
4155
|
+
goodJoiners.push(joiners?.[index] ?? "");
|
|
4156
|
+
} else {
|
|
4157
|
+
if (goodSplits.length > 0) {
|
|
4158
|
+
finalChunks.push(...merge());
|
|
4159
|
+
goodSplits.length = 0;
|
|
4160
|
+
goodJoiners.length = 0;
|
|
4161
|
+
}
|
|
4162
|
+
if (newSeparators.length === 0) finalChunks.push(s);
|
|
4163
|
+
else {
|
|
4164
|
+
const otherInfo = this._splitText(s, newSeparators);
|
|
4165
|
+
finalChunks.push(...otherInfo);
|
|
4166
|
+
}
|
|
4113
4167
|
}
|
|
4114
|
-
}
|
|
4115
|
-
if (goodSplits.length > 0)
|
|
4116
|
-
const mergedText = this.mergeSplits(goodSplits, mergeSeparator);
|
|
4117
|
-
finalChunks.push(...mergedText);
|
|
4118
|
-
}
|
|
4168
|
+
});
|
|
4169
|
+
if (goodSplits.length > 0) finalChunks.push(...merge());
|
|
4119
4170
|
return finalChunks;
|
|
4120
4171
|
}
|
|
4121
4172
|
splitText({ text }) {
|
|
@@ -5542,35 +5593,44 @@ var SentenceTransformer = class extends TextTransformer {
|
|
|
5542
5593
|
const chunks = [];
|
|
5543
5594
|
let currentChunk = [];
|
|
5544
5595
|
let currentSize = 0;
|
|
5596
|
+
let carriedCount = 0;
|
|
5545
5597
|
const separator = " ";
|
|
5598
|
+
const sepLength = this.lengthFunction(separator);
|
|
5546
5599
|
for (const sentence of sentences) {
|
|
5547
5600
|
const sentenceLength = this.lengthFunction(sentence);
|
|
5548
|
-
const separatorLength = currentChunk.length > 0 ? this.lengthFunction(separator) : 0;
|
|
5549
|
-
const totalLength = currentSize + sentenceLength + separatorLength;
|
|
5550
5601
|
if (sentenceLength > this.maxSize) {
|
|
5551
|
-
if (currentChunk.length >
|
|
5552
|
-
|
|
5553
|
-
|
|
5554
|
-
|
|
5555
|
-
}
|
|
5602
|
+
if (currentChunk.length > carriedCount) chunks.push(currentChunk.join(separator));
|
|
5603
|
+
currentChunk = [];
|
|
5604
|
+
currentSize = 0;
|
|
5605
|
+
carriedCount = 0;
|
|
5556
5606
|
const fallbackChunks = this.handleOversizedSentence(sentence);
|
|
5557
5607
|
chunks.push(...fallbackChunks);
|
|
5558
5608
|
continue;
|
|
5559
5609
|
}
|
|
5560
|
-
if (currentChunk.length > 0 &&
|
|
5561
|
-
chunks.push(currentChunk.join(separator));
|
|
5562
|
-
|
|
5563
|
-
|
|
5610
|
+
if (currentChunk.length > 0 && currentSize + sepLength + sentenceLength > this.maxSize) {
|
|
5611
|
+
if (currentChunk.length > carriedCount) chunks.push(currentChunk.join(separator));
|
|
5612
|
+
const overlapSentences = this.calculateSentenceOverlap(currentChunk);
|
|
5613
|
+
let overlapSize = this.calculateChunkSize(overlapSentences);
|
|
5614
|
+
let start = 0;
|
|
5615
|
+
while (start < overlapSentences.length && overlapSize + sepLength + sentenceLength > this.maxSize) {
|
|
5616
|
+
overlapSize -= this.lengthFunction(overlapSentences[start]);
|
|
5617
|
+
if (start < overlapSentences.length - 1) overlapSize -= sepLength;
|
|
5618
|
+
start++;
|
|
5619
|
+
}
|
|
5620
|
+
currentChunk = overlapSentences.slice(start);
|
|
5621
|
+
carriedCount = currentChunk.length;
|
|
5622
|
+
currentSize = currentChunk.length > 0 ? overlapSize : 0;
|
|
5564
5623
|
}
|
|
5624
|
+
currentSize += sentenceLength + (currentChunk.length > 0 ? sepLength : 0);
|
|
5565
5625
|
currentChunk.push(sentence);
|
|
5566
|
-
currentSize += sentenceLength + separatorLength;
|
|
5567
5626
|
if (currentSize >= this.targetSize) {
|
|
5568
5627
|
chunks.push(currentChunk.join(separator));
|
|
5569
5628
|
currentChunk = this.calculateSentenceOverlap(currentChunk);
|
|
5629
|
+
carriedCount = currentChunk.length;
|
|
5570
5630
|
currentSize = this.calculateChunkSize(currentChunk);
|
|
5571
5631
|
}
|
|
5572
5632
|
}
|
|
5573
|
-
if (currentChunk.length >
|
|
5633
|
+
if (currentChunk.length > carriedCount) chunks.push(currentChunk.join(separator));
|
|
5574
5634
|
return chunks;
|
|
5575
5635
|
}
|
|
5576
5636
|
/**
|