@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.
@@ -3,7 +3,7 @@ name: mastra-rag
3
3
  description: Documentation for @mastra/rag. Use when working with @mastra/rag APIs, configuration, or implementation.
4
4
  metadata:
5
5
  package: "@mastra/rag"
6
- version: "2.6.5-alpha.0"
6
+ version: "2.6.5-alpha.1"
7
7
  ---
8
8
 
9
9
  ## When to use
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "2.6.5-alpha.0",
2
+ "version": "2.6.5-alpha.1",
3
3
  "package": "@mastra/rag",
4
4
  "exports": {},
5
5
  "modules": {}
@@ -1 +1 @@
1
- {"version":3,"file":"sentence.d.ts","sourceRoot":"","sources":["../../../src/document/transformers/sentence.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,UAAU,CAAC;AACrD,OAAO,EAAE,eAAe,EAAE,MAAM,QAAQ,CAAC;AAEzC,qBAAa,mBAAoB,SAAQ,eAAe;IACtD,SAAS,CAAC,OAAO,EAAE,MAAM,CAAC;IAC1B,SAAS,CAAC,OAAO,EAAE,MAAM,CAAC;IAC1B,SAAS,CAAC,UAAU,EAAE,MAAM,CAAC;IAC7B,SAAS,CAAC,cAAc,EAAE,MAAM,EAAE,CAAC;IACnC,SAAS,CAAC,eAAe,EAAE,OAAO,CAAC;IACnC,SAAS,CAAC,oBAAoB,EAAE,OAAO,CAAC;IAExC,YAAY,OAAO,EAAE,oBAAoB,EAoBxC;IAED,OAAO,CAAC,wBAAwB;IA+BhC,OAAO,CAAC,sBAAsB;IAqB9B,OAAO,CAAC,oBAAoB;IA8B5B;;OAEG;IACH,OAAO,CAAC,wBAAwB;IAsDhC;;OAEG;IACH,OAAO,CAAC,uBAAuB;IAqB/B,OAAO,CAAC,sBAAsB;IAmC9B,OAAO,CAAC,2BAA2B;IAsBnC,OAAO,CAAC,wBAAwB;IA4BhC,OAAO,CAAC,kBAAkB;IAqB1B,SAAS,CAAC,EAAE,IAAI,EAAE,EAAE;QAAE,IAAI,EAAE,MAAM,CAAA;KAAE,GAAG,MAAM,EAAE,CAQ9C;CACF"}
1
+ {"version":3,"file":"sentence.d.ts","sourceRoot":"","sources":["../../../src/document/transformers/sentence.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,UAAU,CAAC;AACrD,OAAO,EAAE,eAAe,EAAE,MAAM,QAAQ,CAAC;AAEzC,qBAAa,mBAAoB,SAAQ,eAAe;IACtD,SAAS,CAAC,OAAO,EAAE,MAAM,CAAC;IAC1B,SAAS,CAAC,OAAO,EAAE,MAAM,CAAC;IAC1B,SAAS,CAAC,UAAU,EAAE,MAAM,CAAC;IAC7B,SAAS,CAAC,cAAc,EAAE,MAAM,EAAE,CAAC;IACnC,SAAS,CAAC,eAAe,EAAE,OAAO,CAAC;IACnC,SAAS,CAAC,oBAAoB,EAAE,OAAO,CAAC;IAExC,YAAY,OAAO,EAAE,oBAAoB,EAoBxC;IAED,OAAO,CAAC,wBAAwB;IA+BhC,OAAO,CAAC,sBAAsB;IAqB9B,OAAO,CAAC,oBAAoB;IA8B5B;;OAEG;IACH,OAAO,CAAC,wBAAwB;IAmEhC;;OAEG;IACH,OAAO,CAAC,uBAAuB;IAqB/B,OAAO,CAAC,sBAAsB;IAmC9B,OAAO,CAAC,2BAA2B;IAsBnC,OAAO,CAAC,wBAAwB;IA4BhC,OAAO,CAAC,kBAAkB;IAqB1B,SAAS,CAAC,EAAE,IAAI,EAAE,EAAE;QAAE,IAAI,EAAE,MAAM,CAAA;KAAE,GAAG,MAAM,EAAE,CAQ9C;CACF"}
package/dist/index.cjs CHANGED
@@ -5544,35 +5544,44 @@ var SentenceTransformer = class extends TextTransformer {
5544
5544
  const chunks = [];
5545
5545
  let currentChunk = [];
5546
5546
  let currentSize = 0;
5547
+ let carriedCount = 0;
5547
5548
  const separator = " ";
5549
+ const sepLength = this.lengthFunction(separator);
5548
5550
  for (const sentence of sentences) {
5549
5551
  const sentenceLength = this.lengthFunction(sentence);
5550
- const separatorLength = currentChunk.length > 0 ? this.lengthFunction(separator) : 0;
5551
- const totalLength = currentSize + sentenceLength + separatorLength;
5552
5552
  if (sentenceLength > this.maxSize) {
5553
- if (currentChunk.length > 0) {
5554
- chunks.push(currentChunk.join(separator));
5555
- currentChunk = [];
5556
- currentSize = 0;
5557
- }
5553
+ if (currentChunk.length > carriedCount) chunks.push(currentChunk.join(separator));
5554
+ currentChunk = [];
5555
+ currentSize = 0;
5556
+ carriedCount = 0;
5558
5557
  const fallbackChunks = this.handleOversizedSentence(sentence);
5559
5558
  chunks.push(...fallbackChunks);
5560
5559
  continue;
5561
5560
  }
5562
- if (currentChunk.length > 0 && totalLength > this.maxSize) {
5563
- chunks.push(currentChunk.join(separator));
5564
- currentChunk = this.calculateSentenceOverlap(currentChunk);
5565
- currentSize = this.calculateChunkSize(currentChunk);
5561
+ if (currentChunk.length > 0 && currentSize + sepLength + sentenceLength > this.maxSize) {
5562
+ if (currentChunk.length > carriedCount) chunks.push(currentChunk.join(separator));
5563
+ const overlapSentences = this.calculateSentenceOverlap(currentChunk);
5564
+ let overlapSize = this.calculateChunkSize(overlapSentences);
5565
+ let start = 0;
5566
+ while (start < overlapSentences.length && overlapSize + sepLength + sentenceLength > this.maxSize) {
5567
+ overlapSize -= this.lengthFunction(overlapSentences[start]);
5568
+ if (start < overlapSentences.length - 1) overlapSize -= sepLength;
5569
+ start++;
5570
+ }
5571
+ currentChunk = overlapSentences.slice(start);
5572
+ carriedCount = currentChunk.length;
5573
+ currentSize = currentChunk.length > 0 ? overlapSize : 0;
5566
5574
  }
5575
+ currentSize += sentenceLength + (currentChunk.length > 0 ? sepLength : 0);
5567
5576
  currentChunk.push(sentence);
5568
- currentSize += sentenceLength + separatorLength;
5569
5577
  if (currentSize >= this.targetSize) {
5570
5578
  chunks.push(currentChunk.join(separator));
5571
5579
  currentChunk = this.calculateSentenceOverlap(currentChunk);
5580
+ carriedCount = currentChunk.length;
5572
5581
  currentSize = this.calculateChunkSize(currentChunk);
5573
5582
  }
5574
5583
  }
5575
- if (currentChunk.length > 0) chunks.push(currentChunk.join(separator));
5584
+ if (currentChunk.length > carriedCount) chunks.push(currentChunk.join(separator));
5576
5585
  return chunks;
5577
5586
  }
5578
5587
  /**