@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/docs/SKILL.md +1 -1
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/docs/references/reference-rag-graph-rag.md +1 -1
- package/dist/document/transformers/character.d.ts.map +1 -1
- package/dist/document/transformers/text.d.ts +6 -1
- package/dist/document/transformers/text.d.ts.map +1 -1
- package/dist/index.cjs +78 -27
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +78 -27
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
package/dist/docs/SKILL.md
CHANGED
|
@@ -127,7 +127,7 @@ Throws if the snapshot version is unsupported, if a node embedding doesn't match
|
|
|
127
127
|
|
|
128
128
|
## Persisting a graph
|
|
129
129
|
|
|
130
|
-
Building a graph is O(n²) in the number of chunks, so rebuilding it on every process start is wasteful. Serialize the graph once and store the snapshot wherever you already keep state. A snapshot is plain JSON
|
|
130
|
+
Building a graph is O(n²) in the number of chunks, so rebuilding it on every process start is wasteful. Serialize the graph once and store the snapshot wherever you already keep state. A snapshot is plain JSON. Any store works (a file, a blob column, a key-value cache), and `GraphRAG` doesn't depend on a storage backend.
|
|
131
131
|
|
|
132
132
|
```typescript
|
|
133
133
|
import { readFile, writeFile } from 'node:fs/promises'
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"character.d.ts","sourceRoot":"","sources":["../../../src/document/transformers/character.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AACpC,OAAO,KAAK,EAAE,gBAAgB,EAAE,qBAAqB,EAAE,qBAAqB,EAAE,MAAM,UAAU,CAAC;AAE/F,OAAO,EAAE,eAAe,EAAE,MAAM,QAAQ,CAAC;
|
|
1
|
+
{"version":3,"file":"character.d.ts","sourceRoot":"","sources":["../../../src/document/transformers/character.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AACpC,OAAO,KAAK,EAAE,gBAAgB,EAAE,qBAAqB,EAAE,qBAAqB,EAAE,MAAM,UAAU,CAAC;AAE/F,OAAO,EAAE,eAAe,EAAE,MAAM,QAAQ,CAAC;AA6EzC,qBAAa,oBAAqB,SAAQ,eAAe;IACvD,SAAS,CAAC,SAAS,EAAE,MAAM,CAAC;IAC5B,SAAS,CAAC,gBAAgB,EAAE,OAAO,CAAC;IAEpC,YAAY,EAAE,SAAkB,EAAE,gBAAwB,EAAE,GAAG,WAAW,EAAE,GAAE,qBAA0B,EAIvG;IAED,SAAS,CAAC,EAAE,IAAI,EAAE,EAAE;QAAE,IAAI,EAAE,MAAM,CAAA;KAAE,GAAG,MAAM,EAAE,CAmB9C;IAED,OAAO,CAAC,YAAY;CAyDrB;AAED,qBAAa,6BAA8B,SAAQ,eAAe;IAChE,SAAS,CAAC,UAAU,EAAE,MAAM,EAAE,CAAC;IAC/B,SAAS,CAAC,gBAAgB,EAAE,OAAO,CAAC;IAEpC,YAAY,EAAE,UAAU,EAAE,gBAAwB,EAAE,QAAQ,EAAE,GAAG,WAAW,EAAE,GAAE,qBAA0B,EAIzG;IAED,OAAO,CAAC,UAAU;IA8DlB,SAAS,CAAC,EAAE,IAAI,EAAE,EAAE;QAAE,IAAI,EAAE,MAAM,CAAA;KAAE,GAAG,MAAM,EAAE,CAE9C;IAED,MAAM,CAAC,YAAY,CAAC,QAAQ,EAAE,QAAQ,EAAE,OAAO,GAAE,gBAAqB,GAAG,6BAA6B,CAQrG;IAED,MAAM,CAAC,wBAAwB,CAAC,QAAQ,EAAE,QAAQ,GAAG,MAAM,EAAE,CAua5D;CACF"}
|
|
@@ -17,6 +17,11 @@ export declare abstract class TextTransformer implements Transformer {
|
|
|
17
17
|
splitDocuments(documents: Document[]): Document[];
|
|
18
18
|
transformDocuments(documents: Document[]): Document[];
|
|
19
19
|
protected joinDocs(docs: string[], separator: string): string | null;
|
|
20
|
-
|
|
20
|
+
/**
|
|
21
|
+
* Merges splits into chunks of at most `maxSize`.
|
|
22
|
+
* `joiners[i]`, when given, is the text placed before `splits[i]` when it is
|
|
23
|
+
* joined to a preceding piece; otherwise `separator` is used for every piece.
|
|
24
|
+
*/
|
|
25
|
+
protected mergeSplits(splits: string[], separator: string, joiners?: string[]): string[];
|
|
21
26
|
}
|
|
22
27
|
//# sourceMappingURL=text.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"text.d.ts","sourceRoot":"","sources":["../../../src/document/transformers/text.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAErC,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAEjD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAEjD,8BAAsB,eAAgB,YAAW,WAAW;IAC1D,SAAS,CAAC,OAAO,EAAE,MAAM,CAAC;IAC1B,SAAS,CAAC,OAAO,EAAE,MAAM,CAAC;IAC1B,SAAS,CAAC,cAAc,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,MAAM,CAAC;IACnD,SAAS,CAAC,iBAAiB,CAAC,EAAE,OAAO,GAAG,KAAK,CAAC;IAC9C,SAAS,CAAC,aAAa,EAAE,OAAO,CAAC;IACjC,SAAS,CAAC,eAAe,EAAE,OAAO,CAAC;IAEnC,YAAY,EACV,OAAc,EACd,OAAa,EACb,cAA8C,EAC9C,iBAAiB,EACjB,aAAqB,EACrB,eAAsB,GACvB,EAAE,gBAAgB,EAUlB;IAED,gBAAgB,CAAC,KAAK,EAAE,OAAO,GAAG,IAAI,CAErC;IAED,QAAQ,CAAC,SAAS,CAAC,EAAE,IAAI,EAAE,EAAE;QAAE,IAAI,EAAE,MAAM,CAAA;KAAE,GAAG,MAAM,EAAE,CAAC;IAEzD,eAAe,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,SAAS,CAAC,EAAE,MAAM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I,EAAE,MAAM,CAAA;KAAE,GAAG,MAAM,EAAE,CAAC;IAEzD,eAAe,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,GAAG,QAAQ,EAAE,CA0B9E;IAED,cAAc,CAAC,SAAS,EAAE,QAAQ,EAAE,GAAG,QAAQ,EAAE,CAQhD;IAED,kBAAkB,CAAC,SAAS,EAAE,QAAQ,EAAE,GAAG,QAAQ,EAAE,CAUpD;IAED,SAAS,CAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,SAAS,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAMnE;IAED;;;;OAIG;IACH,SAAS,CAAC,WAAW,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,SAAS,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,EAAE,GAAG,MAAM,EAAE,CAkFvF;CACF"}
|
package/dist/index.cjs
CHANGED
|
@@ -3960,46 +3960,66 @@ var TextTransformer = class {
|
|
|
3960
3960
|
if (this.stripWhitespace) text = text.trim();
|
|
3961
3961
|
return text === "" ? null : text;
|
|
3962
3962
|
}
|
|
3963
|
-
|
|
3963
|
+
/**
|
|
3964
|
+
* Merges splits into chunks of at most `maxSize`.
|
|
3965
|
+
* `joiners[i]`, when given, is the text placed before `splits[i]` when it is
|
|
3966
|
+
* joined to a preceding piece; otherwise `separator` is used for every piece.
|
|
3967
|
+
*/
|
|
3968
|
+
mergeSplits(splits, separator, joiners) {
|
|
3964
3969
|
const docs = [];
|
|
3965
3970
|
let currentDoc = [];
|
|
3971
|
+
let currentJoiners = [];
|
|
3966
3972
|
let total = 0;
|
|
3967
|
-
|
|
3973
|
+
const lengthOf = (s) => s ? this.lengthFunction(s) : 0;
|
|
3974
|
+
const join = (pieces, pieceJoiners) => {
|
|
3975
|
+
if (!joiners) return this.joinDocs(pieces, separator);
|
|
3976
|
+
const text = pieces.map((piece, i) => i === 0 ? piece : pieceJoiners[i] + piece).join("");
|
|
3977
|
+
return this.joinDocs([text], "");
|
|
3978
|
+
};
|
|
3979
|
+
splits.forEach((d, index) => {
|
|
3968
3980
|
const len = this.lengthFunction(d);
|
|
3969
|
-
const
|
|
3981
|
+
const joiner = joiners ? joiners[index] ?? separator : separator;
|
|
3982
|
+
const separatorLen = lengthOf(joiner);
|
|
3970
3983
|
if (total + len + (currentDoc.length > 0 ? separatorLen : 0) > this.maxSize) {
|
|
3971
3984
|
if (total > this.maxSize) console.warn(`Created a chunk of size ${total}, which is longer than the specified ${this.maxSize}`);
|
|
3972
3985
|
if (currentDoc.length > 0) {
|
|
3973
|
-
const doc =
|
|
3986
|
+
const doc = join(currentDoc, currentJoiners);
|
|
3974
3987
|
if (doc !== null) docs.push(doc);
|
|
3975
3988
|
if (this.overlap > 0) {
|
|
3976
|
-
|
|
3989
|
+
const overlapContent = [];
|
|
3990
|
+
const overlapJoiners = [];
|
|
3977
3991
|
let overlapSize = 0;
|
|
3978
3992
|
for (let i = currentDoc.length - 1; i >= 0; i--) {
|
|
3979
3993
|
const piece = currentDoc[i];
|
|
3980
3994
|
const pieceLen = this.lengthFunction(piece);
|
|
3981
|
-
|
|
3995
|
+
const connectorLen = overlapContent.length > 0 ? lengthOf(currentJoiners[i + 1]) : 0;
|
|
3996
|
+
if (overlapSize + pieceLen + connectorLen > this.overlap) break;
|
|
3982
3997
|
overlapContent.unshift(piece);
|
|
3983
|
-
|
|
3998
|
+
overlapJoiners.unshift(currentJoiners[i]);
|
|
3999
|
+
overlapSize += pieceLen + connectorLen;
|
|
3984
4000
|
}
|
|
3985
4001
|
while (overlapContent.length > 0 && overlapSize + len + separatorLen > this.maxSize) {
|
|
3986
4002
|
const removed = overlapContent.shift();
|
|
4003
|
+
overlapJoiners.shift();
|
|
3987
4004
|
const removedLen = this.lengthFunction(removed);
|
|
3988
|
-
overlapSize -= removedLen + (overlapContent.length > 0 ?
|
|
4005
|
+
overlapSize -= removedLen + (overlapContent.length > 0 ? lengthOf(overlapJoiners[0]) : 0);
|
|
3989
4006
|
}
|
|
3990
4007
|
currentDoc = overlapContent;
|
|
4008
|
+
currentJoiners = overlapJoiners;
|
|
3991
4009
|
total = overlapSize;
|
|
3992
4010
|
} else {
|
|
3993
4011
|
currentDoc = [];
|
|
4012
|
+
currentJoiners = [];
|
|
3994
4013
|
total = 0;
|
|
3995
4014
|
}
|
|
3996
4015
|
}
|
|
3997
4016
|
}
|
|
3998
4017
|
currentDoc.push(d);
|
|
4018
|
+
currentJoiners.push(joiner);
|
|
3999
4019
|
total += len + (currentDoc.length > 1 ? separatorLen : 0);
|
|
4000
|
-
}
|
|
4020
|
+
});
|
|
4001
4021
|
if (currentDoc.length > 0) {
|
|
4002
|
-
const doc =
|
|
4022
|
+
const doc = join(currentDoc, currentJoiners);
|
|
4003
4023
|
if (doc !== null) docs.push(doc);
|
|
4004
4024
|
}
|
|
4005
4025
|
return docs;
|
|
@@ -4029,6 +4049,30 @@ function splitTextWithRegex(text, separator, separatorPosition) {
|
|
|
4029
4049
|
}
|
|
4030
4050
|
return result.filter((s) => s !== "");
|
|
4031
4051
|
}
|
|
4052
|
+
function splitTextWithMatchedSeparators(text, separator) {
|
|
4053
|
+
const splits = [];
|
|
4054
|
+
const joiners = [];
|
|
4055
|
+
let pending = "";
|
|
4056
|
+
let lastIndex = 0;
|
|
4057
|
+
const push = (piece) => {
|
|
4058
|
+
if (piece !== "") {
|
|
4059
|
+
splits.push(piece);
|
|
4060
|
+
joiners.push(pending);
|
|
4061
|
+
pending = "";
|
|
4062
|
+
}
|
|
4063
|
+
};
|
|
4064
|
+
for (const match of text.matchAll(new RegExp(separator, "g"))) {
|
|
4065
|
+
if (match[0] === "" && (match.index === 0 || match.index === text.length)) continue;
|
|
4066
|
+
push(text.slice(lastIndex, match.index));
|
|
4067
|
+
pending += match[0];
|
|
4068
|
+
lastIndex = match.index + match[0].length;
|
|
4069
|
+
}
|
|
4070
|
+
push(text.slice(lastIndex));
|
|
4071
|
+
return {
|
|
4072
|
+
splits,
|
|
4073
|
+
joiners
|
|
4074
|
+
};
|
|
4075
|
+
}
|
|
4032
4076
|
var CharacterTransformer = class extends TextTransformer {
|
|
4033
4077
|
separator;
|
|
4034
4078
|
isSeparatorRegex;
|
|
@@ -4098,26 +4142,33 @@ var RecursiveCharacterTransformer = class RecursiveCharacterTransformer extends
|
|
|
4098
4142
|
break;
|
|
4099
4143
|
}
|
|
4100
4144
|
}
|
|
4101
|
-
const
|
|
4145
|
+
const _separator = this.isSeparatorRegex ? separator : separator?.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
4146
|
+
const { splits, joiners } = this.isSeparatorRegex && !this.separatorPosition && !!_separator ? splitTextWithMatchedSeparators(text, _separator) : {
|
|
4147
|
+
splits: splitTextWithRegex(text, _separator, this.separatorPosition),
|
|
4148
|
+
joiners: void 0
|
|
4149
|
+
};
|
|
4102
4150
|
const goodSplits = [];
|
|
4151
|
+
const goodJoiners = [];
|
|
4103
4152
|
const mergeSeparator = this.separatorPosition ? "" : separator;
|
|
4104
|
-
|
|
4105
|
-
|
|
4106
|
-
if (
|
|
4107
|
-
|
|
4108
|
-
|
|
4109
|
-
|
|
4110
|
-
|
|
4111
|
-
|
|
4112
|
-
|
|
4113
|
-
|
|
4114
|
-
|
|
4153
|
+
const merge = () => this.mergeSplits(goodSplits, mergeSeparator, joiners ? goodJoiners : void 0);
|
|
4154
|
+
splits.forEach((s, index) => {
|
|
4155
|
+
if (this.lengthFunction(s) < this.maxSize) {
|
|
4156
|
+
goodSplits.push(s);
|
|
4157
|
+
goodJoiners.push(joiners?.[index] ?? "");
|
|
4158
|
+
} else {
|
|
4159
|
+
if (goodSplits.length > 0) {
|
|
4160
|
+
finalChunks.push(...merge());
|
|
4161
|
+
goodSplits.length = 0;
|
|
4162
|
+
goodJoiners.length = 0;
|
|
4163
|
+
}
|
|
4164
|
+
if (newSeparators.length === 0) finalChunks.push(s);
|
|
4165
|
+
else {
|
|
4166
|
+
const otherInfo = this._splitText(s, newSeparators);
|
|
4167
|
+
finalChunks.push(...otherInfo);
|
|
4168
|
+
}
|
|
4115
4169
|
}
|
|
4116
|
-
}
|
|
4117
|
-
if (goodSplits.length > 0)
|
|
4118
|
-
const mergedText = this.mergeSplits(goodSplits, mergeSeparator);
|
|
4119
|
-
finalChunks.push(...mergedText);
|
|
4120
|
-
}
|
|
4170
|
+
});
|
|
4171
|
+
if (goodSplits.length > 0) finalChunks.push(...merge());
|
|
4121
4172
|
return finalChunks;
|
|
4122
4173
|
}
|
|
4123
4174
|
splitText({ text }) {
|