@mastra/rag 2.6.4 → 2.6.5-alpha.1

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
@@ -4547,6 +4547,61 @@ var RecursiveCharacterTransformer = class RecursiveCharacterTransformer extends
4547
4547
  };
4548
4548
  //#endregion
4549
4549
  //#region src/document/transformers/html.ts
4550
+ const BLOCK_TAGS = /* @__PURE__ */ new Set([
4551
+ "address",
4552
+ "article",
4553
+ "aside",
4554
+ "blockquote",
4555
+ "br",
4556
+ "caption",
4557
+ "center",
4558
+ "dd",
4559
+ "details",
4560
+ "dialog",
4561
+ "div",
4562
+ "dl",
4563
+ "dt",
4564
+ "fieldset",
4565
+ "figcaption",
4566
+ "figure",
4567
+ "footer",
4568
+ "form",
4569
+ "h1",
4570
+ "h2",
4571
+ "h3",
4572
+ "h4",
4573
+ "h5",
4574
+ "h6",
4575
+ "header",
4576
+ "hgroup",
4577
+ "hr",
4578
+ "legend",
4579
+ "li",
4580
+ "main",
4581
+ "menu",
4582
+ "nav",
4583
+ "ol",
4584
+ "p",
4585
+ "pre",
4586
+ "search",
4587
+ "section",
4588
+ "summary",
4589
+ "table",
4590
+ "tbody",
4591
+ "td",
4592
+ "tfoot",
4593
+ "th",
4594
+ "thead",
4595
+ "tr",
4596
+ "ul"
4597
+ ]);
4598
+ function extractText(node) {
4599
+ if (!node) return "";
4600
+ if (!node.tagName) return node.text || "";
4601
+ let content = "";
4602
+ for (const child of node.childNodes ?? []) content += extractText(child);
4603
+ return BLOCK_TAGS.has(node.tagName.toLowerCase()) ? " " + content + " " : content;
4604
+ }
4550
4605
  var HTMLHeaderTransformer = class {
4551
4606
  headersToSplitOn;
4552
4607
  returnEachElement;
@@ -4605,14 +4660,7 @@ var HTMLHeaderTransformer = class {
4605
4660
  return "/" + parts.join("/");
4606
4661
  }
4607
4662
  getTextContent(element) {
4608
- if (!element) return "";
4609
- if (!element.tagName) return element.text || "";
4610
- let content = element.text || "";
4611
- if (element.childNodes) for (const child of element.childNodes) {
4612
- const childText = this.getTextContent(child);
4613
- if (childText) content += " " + childText;
4614
- }
4615
- return content.trim();
4663
+ return extractText(element).replace(/\s+/g, " ").trim();
4616
4664
  }
4617
4665
  aggregateElementsToChunks(elements) {
4618
4666
  const aggregatedChunks = [];
@@ -4690,14 +4738,7 @@ var HTMLSectionTransformer = class {
4690
4738
  return "/" + parts.join("/");
4691
4739
  }
4692
4740
  getTextContent(element) {
4693
- if (!element) return "";
4694
- if (!element.tagName) return element.text || "";
4695
- let content = element.text || "";
4696
- if (element.childNodes) for (const child of element.childNodes) {
4697
- const childText = this.getTextContent(child);
4698
- if (childText) content += " " + childText;
4699
- }
4700
- return content.trim();
4741
+ return extractText(element).replace(/\s+/g, " ").trim();
4701
4742
  }
4702
4743
  splitHtmlByHeaders(htmlDoc) {
4703
4744
  const sections = [];
@@ -5501,35 +5542,44 @@ var SentenceTransformer = class extends TextTransformer {
5501
5542
  const chunks = [];
5502
5543
  let currentChunk = [];
5503
5544
  let currentSize = 0;
5545
+ let carriedCount = 0;
5504
5546
  const separator = " ";
5547
+ const sepLength = this.lengthFunction(separator);
5505
5548
  for (const sentence of sentences) {
5506
5549
  const sentenceLength = this.lengthFunction(sentence);
5507
- const separatorLength = currentChunk.length > 0 ? this.lengthFunction(separator) : 0;
5508
- const totalLength = currentSize + sentenceLength + separatorLength;
5509
5550
  if (sentenceLength > this.maxSize) {
5510
- if (currentChunk.length > 0) {
5511
- chunks.push(currentChunk.join(separator));
5512
- currentChunk = [];
5513
- currentSize = 0;
5514
- }
5551
+ if (currentChunk.length > carriedCount) chunks.push(currentChunk.join(separator));
5552
+ currentChunk = [];
5553
+ currentSize = 0;
5554
+ carriedCount = 0;
5515
5555
  const fallbackChunks = this.handleOversizedSentence(sentence);
5516
5556
  chunks.push(...fallbackChunks);
5517
5557
  continue;
5518
5558
  }
5519
- if (currentChunk.length > 0 && totalLength > this.maxSize) {
5520
- chunks.push(currentChunk.join(separator));
5521
- currentChunk = this.calculateSentenceOverlap(currentChunk);
5522
- currentSize = this.calculateChunkSize(currentChunk);
5559
+ if (currentChunk.length > 0 && currentSize + sepLength + sentenceLength > this.maxSize) {
5560
+ if (currentChunk.length > carriedCount) chunks.push(currentChunk.join(separator));
5561
+ const overlapSentences = this.calculateSentenceOverlap(currentChunk);
5562
+ let overlapSize = this.calculateChunkSize(overlapSentences);
5563
+ let start = 0;
5564
+ while (start < overlapSentences.length && overlapSize + sepLength + sentenceLength > this.maxSize) {
5565
+ overlapSize -= this.lengthFunction(overlapSentences[start]);
5566
+ if (start < overlapSentences.length - 1) overlapSize -= sepLength;
5567
+ start++;
5568
+ }
5569
+ currentChunk = overlapSentences.slice(start);
5570
+ carriedCount = currentChunk.length;
5571
+ currentSize = currentChunk.length > 0 ? overlapSize : 0;
5523
5572
  }
5573
+ currentSize += sentenceLength + (currentChunk.length > 0 ? sepLength : 0);
5524
5574
  currentChunk.push(sentence);
5525
- currentSize += sentenceLength + separatorLength;
5526
5575
  if (currentSize >= this.targetSize) {
5527
5576
  chunks.push(currentChunk.join(separator));
5528
5577
  currentChunk = this.calculateSentenceOverlap(currentChunk);
5578
+ carriedCount = currentChunk.length;
5529
5579
  currentSize = this.calculateChunkSize(currentChunk);
5530
5580
  }
5531
5581
  }
5532
- if (currentChunk.length > 0) chunks.push(currentChunk.join(separator));
5582
+ if (currentChunk.length > carriedCount) chunks.push(currentChunk.join(separator));
5533
5583
  return chunks;
5534
5584
  }
5535
5585
  /**