@mastra/rag 2.6.5-alpha.1 → 2.6.5

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
@@ -3958,46 +3958,66 @@ var TextTransformer = class {
3958
3958
  if (this.stripWhitespace) text = text.trim();
3959
3959
  return text === "" ? null : text;
3960
3960
  }
3961
- mergeSplits(splits, separator) {
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
- for (const d of splits) {
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 separatorLen = separator ? this.lengthFunction(separator) : 0;
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 = this.joinDocs(currentDoc, separator);
3984
+ const doc = join(currentDoc, currentJoiners);
3972
3985
  if (doc !== null) docs.push(doc);
3973
3986
  if (this.overlap > 0) {
3974
- let overlapContent = [];
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
- if (overlapSize + pieceLen > this.overlap) break;
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
- overlapSize += pieceLen + (overlapContent.length > 1 ? separatorLen : 0);
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 ? separatorLen : 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 = this.joinDocs(currentDoc, separator);
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 splits = splitTextWithRegex(text, this.isSeparatorRegex ? separator : separator?.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), this.separatorPosition);
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
- for (const s of splits) if (this.lengthFunction(s) < this.maxSize) goodSplits.push(s);
4103
- else {
4104
- if (goodSplits.length > 0) {
4105
- const mergedText = this.mergeSplits(goodSplits, mergeSeparator);
4106
- finalChunks.push(...mergedText);
4107
- goodSplits.length = 0;
4108
- }
4109
- if (newSeparators.length === 0) finalChunks.push(s);
4110
- else {
4111
- const otherInfo = this._splitText(s, newSeparators);
4112
- finalChunks.push(...otherInfo);
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 }) {