@mastra/rag 2.7.1 → 2.7.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
@@ -4007,24 +4007,22 @@ var TextTransformer = class {
4007
4007
  // src/document/transformers/character.ts
4008
4008
  function splitTextWithRegex(text, separator, separatorPosition) {
4009
4009
  if (!separator) return text.split("");
4010
- if (!separatorPosition) return text.split(new RegExp(separator)).filter((s) => s !== "");
4011
4010
  if (!text) return [];
4012
- const splits = text.split(new RegExp(`(${separator})`));
4013
4011
  const result = [];
4014
- if (separatorPosition === "end") {
4015
- for (let i = 0; i < splits.length - 1; i += 2) if (i + 1 < splits.length) {
4016
- const chunk = splits[i] + (splits[i + 1] || "");
4017
- if (chunk) result.push(chunk);
4018
- }
4019
- if (splits.length % 2 === 1 && splits[splits.length - 1]) result.push(splits?.[splits.length - 1]);
4020
- } else {
4021
- if (splits[0]) result.push(splits[0]);
4022
- for (let i = 1; i < splits.length - 1; i += 2) {
4023
- const separator2 = splits[i];
4024
- const text2 = splits[i + 1] ?? "";
4025
- if (separator2) result.push(separator2 + text2);
4026
- }
4027
- }
4012
+ let lastIndex = 0;
4013
+ let pendingSeparator = "";
4014
+ for (const match of text.matchAll(new RegExp(separator, "g"))) {
4015
+ const matched = match[0];
4016
+ const piece = text.slice(lastIndex, match.index);
4017
+ if (separatorPosition === "end") result.push(piece + matched);
4018
+ else if (separatorPosition === "start") {
4019
+ result.push(pendingSeparator + piece);
4020
+ pendingSeparator = matched;
4021
+ } else result.push(piece);
4022
+ lastIndex = match.index + matched.length;
4023
+ }
4024
+ const tail = text.slice(lastIndex);
4025
+ result.push(separatorPosition === "start" ? pendingSeparator + tail : tail);
4028
4026
  return result.filter((s) => s !== "");
4029
4027
  }
4030
4028
  function splitTextWithMatchedSeparators(text, separator) {
@@ -4846,12 +4844,10 @@ var HTMLSectionTransformer = class {
4846
4844
  // src/document/transformers/json.ts
4847
4845
  var RecursiveJsonTransformer = class RecursiveJsonTransformer {
4848
4846
  maxSize;
4849
- minSize;
4850
4847
  ensureAscii;
4851
4848
  convertLists;
4852
- constructor({ maxSize = 2e3, minSize, ensureAscii = false, convertLists = true }) {
4849
+ constructor({ maxSize = 2e3, ensureAscii = false, convertLists = true }) {
4853
4850
  this.maxSize = maxSize;
4854
- this.minSize = minSize ?? Math.max(maxSize - 200, 50);
4855
4851
  this.ensureAscii = ensureAscii;
4856
4852
  this.convertLists = convertLists;
4857
4853
  }
@@ -4978,14 +4974,11 @@ var RecursiveJsonTransformer = class RecursiveJsonTransformer {
4978
4974
  let currentGroup = [];
4979
4975
  const saveCurrentGroup = () => {
4980
4976
  if (currentGroup.length > 0) {
4981
- const groupChunk = this.createChunk(currentGroup, path);
4982
- if (RecursiveJsonTransformer.jsonSize(groupChunk) >= this.minSize) {
4983
- chunks.push(groupChunk);
4984
- currentGroup = [];
4985
- }
4977
+ chunks.push(this.createChunk(currentGroup, path));
4978
+ currentGroup = [];
4986
4979
  }
4987
4980
  };
4988
- for (const item of value) {
4981
+ for (const [itemIndex, item] of value.entries()) {
4989
4982
  const testGroup = [...currentGroup, item];
4990
4983
  const testChunk = this.createChunk(testGroup, path);
4991
4984
  if (this.isWithinSizeLimit(testChunk)) {
@@ -4998,7 +4991,7 @@ var RecursiveJsonTransformer = class RecursiveJsonTransformer {
4998
4991
  const singleItemChunk = this.createChunk(singleItemArray, path);
4999
4992
  if (this.isWithinSizeLimit(singleItemChunk)) currentGroup = singleItemArray;
5000
4993
  else {
5001
- const itemPath = [...path, String(chunks.length)];
4994
+ const itemPath = [...path, String(itemIndex)];
5002
4995
  const nestedChunks = this.handleNestedObject(item, itemPath, depth + 1, maxDepth);
5003
4996
  chunks.push(...nestedChunks);
5004
4997
  }
@@ -5023,11 +5016,8 @@ var RecursiveJsonTransformer = class RecursiveJsonTransformer {
5023
5016
  let currentChunk = {};
5024
5017
  const saveCurrentChunk = () => {
5025
5018
  if (Object.keys(currentChunk).length > 0) {
5026
- const objChunk = this.createChunk(currentChunk, path);
5027
- if (RecursiveJsonTransformer.jsonSize(objChunk) >= this.minSize) {
5028
- chunks.push(objChunk);
5029
- currentChunk = {};
5030
- }
5019
+ chunks.push(this.createChunk(currentChunk, path));
5020
+ currentChunk = {};
5031
5021
  }
5032
5022
  };
5033
5023
  for (const [key, val] of Object.entries(value)) {
@@ -5115,30 +5105,15 @@ var RecursiveJsonTransformer = class RecursiveJsonTransformer {
5115
5105
  return chunks;
5116
5106
  }
5117
5107
  /**
5118
- * Converts Unicode characters to their escaped ASCII representation
5119
- * e.g., 'café' becomes 'caf\u00e9'
5120
- */
5121
- escapeNonAscii(obj) {
5122
- if (typeof obj === "string") return obj.replace(/[\u0080-\uffff]/g, (char) => {
5123
- return `\\u${char.charCodeAt(0).toString(16).padStart(4, "0")}`;
5124
- });
5125
- if (Array.isArray(obj)) return obj.map((item) => this.escapeNonAscii(item));
5126
- if (typeof obj === "object" && obj !== null) return Object.fromEntries(Object.entries(obj).map(([key, value]) => [key, this.escapeNonAscii(value)]));
5127
- return obj;
5128
- }
5129
- /**
5130
5108
  * Splits JSON into a list of JSON formatted strings
5131
5109
  */
5132
5110
  splitText({ jsonData, convertLists = false, ensureAscii = true }) {
5133
- const chunks = this.splitJson({
5111
+ const serialized = this.splitJson({
5134
5112
  jsonData,
5135
5113
  convertLists
5136
- });
5137
- if (ensureAscii) return chunks.map((chunk) => this.escapeNonAscii(chunk)).map((chunk) => JSON.stringify(chunk));
5138
- return chunks.map((chunk) => JSON.stringify(chunk, (key, value) => {
5139
- if (typeof value === "string") return value.replace(/\\u[\da-f]{4}/gi, (match) => String.fromCharCode(parseInt(match.slice(2), 16)));
5140
- return value;
5141
- }));
5114
+ }).map((chunk) => JSON.stringify(chunk));
5115
+ if (!ensureAscii) return serialized;
5116
+ return serialized.map((text) => text.replace(/[\u0080-\uffff]/g, (char) => `\\u${char.charCodeAt(0).toString(16).padStart(4, "0")}`));
5142
5117
  }
5143
5118
  /**
5144
5119
  * Create documents from a list of json objects
@@ -5250,11 +5225,12 @@ var MarkdownHeaderTransformer = class {
5250
5225
  const line = lines[i];
5251
5226
  const strippedLine = line.trim();
5252
5227
  if (!inCodeBlock) {
5253
- if (strippedLine.startsWith("```") && strippedLine.split("```").length === 2 || strippedLine.startsWith("~~~")) {
5228
+ const fence = strippedLine.match(/^(`{3,}|~{3,})/);
5229
+ if (fence && (fence[1][0] !== "`" || !strippedLine.slice(fence[1].length).includes("`"))) {
5254
5230
  inCodeBlock = true;
5255
- openingFence = strippedLine.startsWith("```") ? "```" : "~~~";
5231
+ openingFence = fence[1];
5256
5232
  }
5257
- } else if (strippedLine.startsWith(openingFence)) {
5233
+ } else if (strippedLine.startsWith(openingFence) && new RegExp(`^${openingFence[0]}{${openingFence.length},}\\s*$`).test(strippedLine)) {
5258
5234
  inCodeBlock = false;
5259
5235
  openingFence = "";
5260
5236
  }
@@ -5262,13 +5238,9 @@ var MarkdownHeaderTransformer = class {
5262
5238
  currentContent.push(line);
5263
5239
  continue;
5264
5240
  }
5265
- if (strippedLine.includes("|") && strippedLine.length > 0) {
5266
- if (!inTable) inTable = true;
5267
- currentContent.push(line);
5268
- continue;
5269
- } else if (inTable) inTable = false;
5241
+ const isIndentedCode = strippedLine.includes("|") && /^(?: {4}|\t)/.test(line);
5270
5242
  let headerMatched = false;
5271
- for (const [sep, name] of this.headersToSplitOn) if (strippedLine.startsWith(sep) && (strippedLine.length === sep.length || strippedLine[sep.length] === " ")) {
5243
+ for (const [sep, name] of this.headersToSplitOn) if (!isIndentedCode && strippedLine.startsWith(sep) && (strippedLine.length === sep.length || strippedLine[sep.length] === " ")) {
5272
5244
  headerMatched = true;
5273
5245
  if (currentContent.length > 0) {
5274
5246
  linesWithMetadata.push({
@@ -5300,7 +5272,14 @@ var MarkdownHeaderTransformer = class {
5300
5272
  });
5301
5273
  break;
5302
5274
  }
5303
- if (!headerMatched) {
5275
+ if (headerMatched) inTable = false;
5276
+ else {
5277
+ if (strippedLine.includes("|")) {
5278
+ inTable = true;
5279
+ currentContent.push(line);
5280
+ continue;
5281
+ }
5282
+ if (inTable) inTable = false;
5304
5283
  if (strippedLine || this.returnEachLine) {
5305
5284
  currentContent.push(line);
5306
5285
  if (this.returnEachLine) {