@mastra/rag 2.6.5-alpha.0 → 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
@@ -5542,35 +5542,44 @@ var SentenceTransformer = class extends TextTransformer {
5542
5542
  const chunks = [];
5543
5543
  let currentChunk = [];
5544
5544
  let currentSize = 0;
5545
+ let carriedCount = 0;
5545
5546
  const separator = " ";
5547
+ const sepLength = this.lengthFunction(separator);
5546
5548
  for (const sentence of sentences) {
5547
5549
  const sentenceLength = this.lengthFunction(sentence);
5548
- const separatorLength = currentChunk.length > 0 ? this.lengthFunction(separator) : 0;
5549
- const totalLength = currentSize + sentenceLength + separatorLength;
5550
5550
  if (sentenceLength > this.maxSize) {
5551
- if (currentChunk.length > 0) {
5552
- chunks.push(currentChunk.join(separator));
5553
- currentChunk = [];
5554
- currentSize = 0;
5555
- }
5551
+ if (currentChunk.length > carriedCount) chunks.push(currentChunk.join(separator));
5552
+ currentChunk = [];
5553
+ currentSize = 0;
5554
+ carriedCount = 0;
5556
5555
  const fallbackChunks = this.handleOversizedSentence(sentence);
5557
5556
  chunks.push(...fallbackChunks);
5558
5557
  continue;
5559
5558
  }
5560
- if (currentChunk.length > 0 && totalLength > this.maxSize) {
5561
- chunks.push(currentChunk.join(separator));
5562
- currentChunk = this.calculateSentenceOverlap(currentChunk);
5563
- 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;
5564
5572
  }
5573
+ currentSize += sentenceLength + (currentChunk.length > 0 ? sepLength : 0);
5565
5574
  currentChunk.push(sentence);
5566
- currentSize += sentenceLength + separatorLength;
5567
5575
  if (currentSize >= this.targetSize) {
5568
5576
  chunks.push(currentChunk.join(separator));
5569
5577
  currentChunk = this.calculateSentenceOverlap(currentChunk);
5578
+ carriedCount = currentChunk.length;
5570
5579
  currentSize = this.calculateChunkSize(currentChunk);
5571
5580
  }
5572
5581
  }
5573
- if (currentChunk.length > 0) chunks.push(currentChunk.join(separator));
5582
+ if (currentChunk.length > carriedCount) chunks.push(currentChunk.join(separator));
5574
5583
  return chunks;
5575
5584
  }
5576
5585
  /**