@mastra/rag 2.0.0-beta.3 → 2.0.0-beta.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/CHANGELOG.md +45 -0
- package/dist/docs/README.md +32 -0
- package/dist/docs/SKILL.md +33 -0
- package/dist/docs/SOURCE_MAP.json +6 -0
- package/dist/docs/rag/01-overview.md +74 -0
- package/dist/docs/rag/02-chunking-and-embedding.md +190 -0
- package/dist/docs/rag/03-retrieval.md +549 -0
- package/dist/docs/rag/04-graph-rag.md +217 -0
- package/dist/docs/rag/05-reference.md +854 -0
- package/dist/docs/tools/01-reference.md +684 -0
- package/dist/document/extractors/keywords.d.ts +2 -2
- package/dist/document/extractors/keywords.d.ts.map +1 -1
- package/dist/document/extractors/questions.d.ts +2 -2
- package/dist/document/extractors/questions.d.ts.map +1 -1
- package/dist/document/extractors/summary.d.ts.map +1 -1
- package/dist/document/extractors/title.d.ts +2 -2
- package/dist/document/extractors/title.d.ts.map +1 -1
- package/dist/document/extractors/types.d.ts +6 -6
- package/dist/document/extractors/types.d.ts.map +1 -1
- package/dist/index.cjs +130 -59
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +132 -61
- package/dist/index.js.map +1 -1
- package/dist/rerank/relevance/mastra-agent/index.d.ts +2 -2
- package/dist/rerank/relevance/mastra-agent/index.d.ts.map +1 -1
- package/dist/tools/graph-rag.d.ts.map +1 -1
- package/dist/tools/index.d.ts +1 -0
- package/dist/tools/index.d.ts.map +1 -1
- package/dist/tools/types.d.ts +171 -14
- package/dist/tools/types.d.ts.map +1 -1
- package/dist/tools/vector-query.d.ts.map +1 -1
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/tool-helpers.d.ts +54 -0
- package/dist/utils/tool-helpers.d.ts.map +1 -0
- package/dist/utils/vector-search.d.ts.map +1 -1
- package/package.json +9 -9
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { MastraLanguageModel } from '@mastra/core/agent';
|
|
1
|
+
import type { MastraLanguageModel, MastraLegacyLanguageModel } from '@mastra/core/agent';
|
|
2
2
|
import type { QuestionExtractPrompt } from '../prompts/index.js';
|
|
3
3
|
import type { BaseNode } from '../schema/index.js';
|
|
4
4
|
import { BaseExtractor } from './base.js';
|
|
@@ -13,7 +13,7 @@ type ExtractQuestion = {
|
|
|
13
13
|
* Extract questions from a list of nodes.
|
|
14
14
|
*/
|
|
15
15
|
export declare class QuestionsAnsweredExtractor extends BaseExtractor {
|
|
16
|
-
llm: MastraLanguageModel;
|
|
16
|
+
llm: MastraLanguageModel | MastraLegacyLanguageModel;
|
|
17
17
|
questions: number;
|
|
18
18
|
promptTemplate: QuestionExtractPrompt;
|
|
19
19
|
embeddingOnly: boolean;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"questions.d.ts","sourceRoot":"","sources":["../../../src/document/extractors/questions.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"questions.d.ts","sourceRoot":"","sources":["../../../src/document/extractors/questions.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,mBAAmB,EAAE,yBAAyB,EAAE,MAAM,oBAAoB,CAAC;AAEzF,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAC;AACxD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAE1C,OAAO,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAC;AAEvC,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,SAAS,CAAC;AAEzD,KAAK,eAAe,GAAG;IACrB;;OAEG;IACH,6BAA6B,EAAE,MAAM,CAAC;CACvC,CAAC;AAEF;;GAEG;AACH,qBAAa,0BAA2B,SAAQ,aAAa;IAC3D,GAAG,EAAE,mBAAmB,GAAG,yBAAyB,CAAC;IACrD,SAAS,EAAE,MAAM,CAAK;IACtB,cAAc,EAAE,qBAAqB,CAAC;IACtC,aAAa,EAAE,OAAO,CAAS;IAE/B;;;;;;OAMG;gBACS,OAAO,CAAC,EAAE,yBAAyB;IAkB/C;;;;OAIG;IACG,wBAAwB,CAAC,IAAI,EAAE,QAAQ,GAAG,OAAO,CAAC,eAAe,CAAC;IA6CxE;;;;OAIG;IACG,OAAO,CAAC,KAAK,EAAE,QAAQ,EAAE,GAAG,OAAO,CAAC,KAAK,CAAC,eAAe,CAAC,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC;CAKlF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"summary.d.ts","sourceRoot":"","sources":["../../../src/document/extractors/summary.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAChD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAE1C,OAAO,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAC;AAEvC,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAElD,KAAK,cAAc,GAAG;IACpB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B,kBAAkB,CAAC,EAAE,MAAM,CAAC;CAC7B,CAAC;AAEF;;;;;;GAMG;AACH,qBAAa,gBAAiB,SAAQ,aAAa;IACjD,OAAO,CAAC,GAAG,
|
|
1
|
+
{"version":3,"file":"summary.d.ts","sourceRoot":"","sources":["../../../src/document/extractors/summary.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAChD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAE1C,OAAO,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAC;AAEvC,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAElD,KAAK,cAAc,GAAG;IACpB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B,kBAAkB,CAAC,EAAE,MAAM,CAAC;CAC7B,CAAC;AAEF;;;;;;GAMG;AACH,qBAAa,gBAAiB,SAAQ,aAAa;IACjD,OAAO,CAAC,GAAG,CAAkD;IAC7D,SAAS,EAAE,MAAM,EAAE,CAAC;IACpB,cAAc,EAAE,aAAa,CAAC;IAC9B,OAAO,CAAC,WAAW,CAAU;IAC7B,OAAO,CAAC,WAAW,CAAU;IAC7B,OAAO,CAAC,WAAW,CAAU;gBACjB,OAAO,CAAC,EAAE,kBAAkB;IAsBxC;;;;OAIG;IACG,mBAAmB,CAAC,IAAI,EAAE,QAAQ,GAAG,OAAO,CAAC,MAAM,CAAC;IAuC1D;;;;OAIG;IACG,OAAO,CAAC,KAAK,EAAE,QAAQ,EAAE,GAAG,OAAO,CAAC,cAAc,EAAE,CAAC;CAqB5D"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { MastraLanguageModel } from '@mastra/core/agent';
|
|
1
|
+
import type { MastraLanguageModel, MastraLegacyLanguageModel } from '@mastra/core/agent';
|
|
2
2
|
import type { TitleCombinePrompt, TitleExtractorPrompt } from '../prompts/index.js';
|
|
3
3
|
import type { BaseNode } from '../schema/index.js';
|
|
4
4
|
import { BaseExtractor } from './base.js';
|
|
@@ -10,7 +10,7 @@ type ExtractTitle = {
|
|
|
10
10
|
* Extract title from a list of nodes.
|
|
11
11
|
*/
|
|
12
12
|
export declare class TitleExtractor extends BaseExtractor {
|
|
13
|
-
llm: MastraLanguageModel;
|
|
13
|
+
llm: MastraLegacyLanguageModel | MastraLanguageModel;
|
|
14
14
|
isTextNodeOnly: boolean;
|
|
15
15
|
nodes: number;
|
|
16
16
|
nodeTemplate: TitleExtractorPrompt;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"title.d.ts","sourceRoot":"","sources":["../../../src/document/extractors/title.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"title.d.ts","sourceRoot":"","sources":["../../../src/document/extractors/title.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,mBAAmB,EAAE,yBAAyB,EAAE,MAAM,oBAAoB,CAAC;AAEzF,OAAO,KAAK,EAAE,kBAAkB,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAE3E,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAC1C,OAAO,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAC;AAEvC,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAEnD,KAAK,YAAY,GAAG;IAClB,aAAa,EAAE,MAAM,CAAC;CACvB,CAAC;AAEF;;GAEG;AACH,qBAAa,cAAe,SAAQ,aAAa;IAC/C,GAAG,EAAE,yBAAyB,GAAG,mBAAmB,CAAC;IACrD,cAAc,EAAE,OAAO,CAAS;IAChC,KAAK,EAAE,MAAM,CAAK;IAClB,YAAY,EAAE,oBAAoB,CAAC;IACnC,eAAe,EAAE,kBAAkB,CAAC;gBAExB,OAAO,CAAC,EAAE,mBAAmB;IAqBzC;;;;OAIG;IACG,OAAO,CAAC,KAAK,EAAE,QAAQ,EAAE,GAAG,OAAO,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC;IAoC9D,OAAO,CAAC,WAAW;IASnB,OAAO,CAAC,uBAAuB;YAYjB,aAAa;YA6Cb,mBAAmB;CAiClC"}
|
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
import type { MastraLanguageModel } from '@mastra/core/agent';
|
|
1
|
+
import type { MastraLanguageModel, MastraLegacyLanguageModel } from '@mastra/core/agent';
|
|
2
2
|
import type { KeywordExtractPrompt, QuestionExtractPrompt, SummaryPrompt, TitleExtractorPrompt, TitleCombinePrompt } from '../prompts/index.js';
|
|
3
3
|
export type KeywordExtractArgs = {
|
|
4
|
-
llm?: MastraLanguageModel;
|
|
4
|
+
llm?: MastraLegacyLanguageModel | MastraLanguageModel;
|
|
5
5
|
keywords?: number;
|
|
6
6
|
promptTemplate?: KeywordExtractPrompt['template'];
|
|
7
7
|
};
|
|
8
8
|
export type QuestionAnswerExtractArgs = {
|
|
9
|
-
llm?: MastraLanguageModel;
|
|
9
|
+
llm?: MastraLegacyLanguageModel | MastraLanguageModel;
|
|
10
10
|
questions?: number;
|
|
11
11
|
promptTemplate?: QuestionExtractPrompt['template'];
|
|
12
12
|
embeddingOnly?: boolean;
|
|
13
13
|
};
|
|
14
14
|
export type SummaryExtractArgs = {
|
|
15
|
-
llm?: MastraLanguageModel;
|
|
15
|
+
llm?: MastraLegacyLanguageModel | MastraLanguageModel;
|
|
16
16
|
summaries?: string[];
|
|
17
17
|
promptTemplate?: SummaryPrompt['template'];
|
|
18
18
|
};
|
|
19
19
|
export type TitleExtractorsArgs = {
|
|
20
|
-
llm?: MastraLanguageModel;
|
|
20
|
+
llm?: MastraLegacyLanguageModel | MastraLanguageModel;
|
|
21
21
|
nodes?: number;
|
|
22
22
|
nodeTemplate?: TitleExtractorPrompt['template'];
|
|
23
23
|
combineTemplate?: TitleCombinePrompt['template'];
|
|
24
24
|
};
|
|
25
25
|
export declare const STRIP_REGEX: RegExp;
|
|
26
|
-
export declare const baseLLM: MastraLanguageModel;
|
|
26
|
+
export declare const baseLLM: MastraLegacyLanguageModel | MastraLanguageModel;
|
|
27
27
|
//# sourceMappingURL=types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/document/extractors/types.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/document/extractors/types.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,mBAAmB,EAAE,yBAAyB,EAAE,MAAM,oBAAoB,CAAC;AACzF,OAAO,KAAK,EACV,oBAAoB,EACpB,qBAAqB,EACrB,aAAa,EACb,oBAAoB,EACpB,kBAAkB,EACnB,MAAM,YAAY,CAAC;AAEpB,MAAM,MAAM,kBAAkB,GAAG;IAC/B,GAAG,CAAC,EAAE,yBAAyB,GAAG,mBAAmB,CAAC;IACtD,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,cAAc,CAAC,EAAE,oBAAoB,CAAC,UAAU,CAAC,CAAC;CACnD,CAAC;AAEF,MAAM,MAAM,yBAAyB,GAAG;IACtC,GAAG,CAAC,EAAE,yBAAyB,GAAG,mBAAmB,CAAC;IACtD,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,cAAc,CAAC,EAAE,qBAAqB,CAAC,UAAU,CAAC,CAAC;IACnD,aAAa,CAAC,EAAE,OAAO,CAAC;CACzB,CAAC;AAEF,MAAM,MAAM,kBAAkB,GAAG;IAC/B,GAAG,CAAC,EAAE,yBAAyB,GAAG,mBAAmB,CAAC;IACtD,SAAS,CAAC,EAAE,MAAM,EAAE,CAAC;IACrB,cAAc,CAAC,EAAE,aAAa,CAAC,UAAU,CAAC,CAAC;CAC5C,CAAC;AAEF,MAAM,MAAM,mBAAmB,GAAG;IAChC,GAAG,CAAC,EAAE,yBAAyB,GAAG,mBAAmB,CAAC;IACtD,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,YAAY,CAAC,EAAE,oBAAoB,CAAC,UAAU,CAAC,CAAC;IAChD,eAAe,CAAC,EAAE,kBAAkB,CAAC,UAAU,CAAC,CAAC;CAClD,CAAC;AAEF,eAAO,MAAM,WAAW,QAAmB,CAAC;AAG5C,eAAO,MAAM,OAAO,EAAE,yBAAyB,GAAG,mBAAsC,CAAC"}
|
package/dist/index.cjs
CHANGED
|
@@ -21,7 +21,7 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
|
21
21
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
22
22
|
var __getProtoOf = Object.getPrototypeOf;
|
|
23
23
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
24
|
-
var __knownSymbol = (name14, symbol15) => (symbol15 = Symbol[name14]) ? symbol15 : Symbol.for("Symbol." + name14);
|
|
24
|
+
var __knownSymbol = (name14, symbol15) => (symbol15 = Symbol[name14]) ? symbol15 : /* @__PURE__ */ Symbol.for("Symbol." + name14);
|
|
25
25
|
var __typeError = (msg) => {
|
|
26
26
|
throw TypeError(msg);
|
|
27
27
|
};
|
|
@@ -902,7 +902,7 @@ function loadApiKey({
|
|
|
902
902
|
}
|
|
903
903
|
return apiKey;
|
|
904
904
|
}
|
|
905
|
-
var validatorSymbol = Symbol.for("vercel.ai.validator");
|
|
905
|
+
var validatorSymbol = /* @__PURE__ */ Symbol.for("vercel.ai.validator");
|
|
906
906
|
function validator(validate) {
|
|
907
907
|
return { [validatorSymbol]: true, validate };
|
|
908
908
|
}
|
|
@@ -4141,7 +4141,7 @@ var TitleExtractor = class extends BaseExtractor {
|
|
|
4141
4141
|
const titleCandidates = await this.getTitlesCandidates(nodes);
|
|
4142
4142
|
const combinedTitles = titleCandidates.join(", ");
|
|
4143
4143
|
let title = "";
|
|
4144
|
-
if (this.llm
|
|
4144
|
+
if (agent.isSupportedLanguageModel(this.llm)) {
|
|
4145
4145
|
const miniAgent = new agent.Agent({
|
|
4146
4146
|
id: "title-extractor",
|
|
4147
4147
|
model: this.llm,
|
|
@@ -4180,7 +4180,7 @@ var TitleExtractor = class extends BaseExtractor {
|
|
|
4180
4180
|
});
|
|
4181
4181
|
const titleJobs = nodes.map(async (node) => {
|
|
4182
4182
|
let completion;
|
|
4183
|
-
if (this.llm
|
|
4183
|
+
if (agent.isSupportedLanguageModel(this.llm)) {
|
|
4184
4184
|
const result = await miniAgent.generate([
|
|
4185
4185
|
{ role: "user", content: this.nodeTemplate.format({ context: node.getContent() }) }
|
|
4186
4186
|
]);
|
|
@@ -4246,7 +4246,7 @@ var SummaryExtractor = class extends BaseExtractor {
|
|
|
4246
4246
|
instructions: "You are a summary extractor. You are given a node and you need to extract the summary from the node."
|
|
4247
4247
|
});
|
|
4248
4248
|
let summary = "";
|
|
4249
|
-
if (this.llm
|
|
4249
|
+
if (agent.isSupportedLanguageModel(this.llm)) {
|
|
4250
4250
|
const result = await miniAgent.generate([{ role: "user", content: prompt }]);
|
|
4251
4251
|
summary = result.text;
|
|
4252
4252
|
} else {
|
|
@@ -4332,7 +4332,7 @@ var QuestionsAnsweredExtractor = class extends BaseExtractor {
|
|
|
4332
4332
|
instructions: "You are a question extractor. You are given a node and you need to extract the questions from the node."
|
|
4333
4333
|
});
|
|
4334
4334
|
let questionsText = "";
|
|
4335
|
-
if (this.llm
|
|
4335
|
+
if (agent.isSupportedLanguageModel(this.llm)) {
|
|
4336
4336
|
const result2 = await miniAgent.generate([{ role: "user", content: prompt }]);
|
|
4337
4337
|
questionsText = result2.text;
|
|
4338
4338
|
} else {
|
|
@@ -4404,7 +4404,7 @@ var KeywordExtractor = class extends BaseExtractor {
|
|
|
4404
4404
|
name: "keyword-extractor",
|
|
4405
4405
|
instructions: "You are a keyword extractor. You are given a node and you need to extract the keywords from the node."
|
|
4406
4406
|
});
|
|
4407
|
-
if (this.llm
|
|
4407
|
+
if (agent.isSupportedLanguageModel(this.llm)) {
|
|
4408
4408
|
const result = await miniAgent.generate([
|
|
4409
4409
|
{
|
|
4410
4410
|
role: "user",
|
|
@@ -6594,7 +6594,7 @@ Always return just the number, no explanation.`,
|
|
|
6594
6594
|
const prompt = relevance.createSimilarityPrompt(query, text);
|
|
6595
6595
|
const model = await this.agent.getModel();
|
|
6596
6596
|
let response;
|
|
6597
|
-
if (model
|
|
6597
|
+
if (agent.isSupportedLanguageModel(model)) {
|
|
6598
6598
|
response = await this.agent.generate(prompt);
|
|
6599
6599
|
} else {
|
|
6600
6600
|
response = await this.agent.generateLegacy(prompt);
|
|
@@ -6980,11 +6980,20 @@ var vectorQuerySearch = async ({
|
|
|
6980
6980
|
providerOptions
|
|
6981
6981
|
}) => {
|
|
6982
6982
|
let embeddingResult;
|
|
6983
|
-
if (model.specificationVersion === "
|
|
6983
|
+
if (model.specificationVersion === "v3") {
|
|
6984
|
+
embeddingResult = await vector.embedV3({
|
|
6985
|
+
model,
|
|
6986
|
+
value: queryText,
|
|
6987
|
+
maxRetries,
|
|
6988
|
+
// Type assertion needed: providerOptions type is a union, but embedV3 expects specific version
|
|
6989
|
+
...providerOptions && { providerOptions }
|
|
6990
|
+
});
|
|
6991
|
+
} else if (model.specificationVersion === "v2") {
|
|
6984
6992
|
embeddingResult = await vector.embedV2({
|
|
6985
6993
|
model,
|
|
6986
6994
|
value: queryText,
|
|
6987
6995
|
maxRetries,
|
|
6996
|
+
// Type assertion needed: providerOptions type is a union, but embedV2 expects specific version
|
|
6988
6997
|
...providerOptions && { providerOptions }
|
|
6989
6998
|
});
|
|
6990
6999
|
} else {
|
|
@@ -7108,6 +7117,106 @@ var filterSchema = zod.z.object({
|
|
|
7108
7117
|
filter: zod.z.coerce.string().describe(filterDescription)
|
|
7109
7118
|
});
|
|
7110
7119
|
|
|
7120
|
+
// src/utils/tool-helpers.ts
|
|
7121
|
+
function isValidMastraVector(value) {
|
|
7122
|
+
return value !== null && value !== void 0 && typeof value === "object";
|
|
7123
|
+
}
|
|
7124
|
+
function buildContextString(context) {
|
|
7125
|
+
const parts = [];
|
|
7126
|
+
if (context.vectorStoreName) {
|
|
7127
|
+
parts.push(`vectorStoreName="${context.vectorStoreName}"`);
|
|
7128
|
+
}
|
|
7129
|
+
if (context.requestContext) {
|
|
7130
|
+
const schemaId = context.requestContext.get?.("schemaId");
|
|
7131
|
+
const tenantId = context.requestContext.get?.("tenantId");
|
|
7132
|
+
if (schemaId) parts.push(`schemaId="${schemaId}"`);
|
|
7133
|
+
if (tenantId) parts.push(`tenantId="${tenantId}"`);
|
|
7134
|
+
}
|
|
7135
|
+
if (context.mastra) {
|
|
7136
|
+
parts.push("mastra=provided");
|
|
7137
|
+
}
|
|
7138
|
+
return parts.length > 0 ? ` (context: ${parts.join(", ")})` : "";
|
|
7139
|
+
}
|
|
7140
|
+
async function resolveVectorStore(options, context, logger) {
|
|
7141
|
+
const { requestContext, mastra, vectorStoreName, fallbackOnInvalid = false } = context;
|
|
7142
|
+
if ("vectorStore" in options && options.vectorStore !== void 0) {
|
|
7143
|
+
const vectorStoreOption = options.vectorStore;
|
|
7144
|
+
if (typeof vectorStoreOption === "function") {
|
|
7145
|
+
const resolved = await vectorStoreOption({ requestContext, mastra });
|
|
7146
|
+
if (!isValidMastraVector(resolved)) {
|
|
7147
|
+
const contextStr = buildContextString(context);
|
|
7148
|
+
const receivedType = resolved === null ? "null" : resolved === void 0 ? "undefined" : typeof resolved;
|
|
7149
|
+
if (fallbackOnInvalid) {
|
|
7150
|
+
if (mastra && vectorStoreName) {
|
|
7151
|
+
return mastra.getVector(vectorStoreName);
|
|
7152
|
+
}
|
|
7153
|
+
return void 0;
|
|
7154
|
+
}
|
|
7155
|
+
throw new Error(
|
|
7156
|
+
`VectorStoreResolver returned invalid value: expected MastraVector instance, got ${receivedType}${contextStr}`
|
|
7157
|
+
);
|
|
7158
|
+
}
|
|
7159
|
+
return resolved;
|
|
7160
|
+
}
|
|
7161
|
+
if (!isValidMastraVector(vectorStoreOption)) {
|
|
7162
|
+
const contextStr = buildContextString(context);
|
|
7163
|
+
const receivedType = vectorStoreOption === null ? "null" : vectorStoreOption === void 0 ? "undefined" : typeof vectorStoreOption;
|
|
7164
|
+
if (fallbackOnInvalid) {
|
|
7165
|
+
if (mastra && vectorStoreName) {
|
|
7166
|
+
return mastra.getVector(vectorStoreName);
|
|
7167
|
+
}
|
|
7168
|
+
return void 0;
|
|
7169
|
+
}
|
|
7170
|
+
throw new Error(`vectorStore option is not a valid MastraVector instance: got ${receivedType}${contextStr}`);
|
|
7171
|
+
}
|
|
7172
|
+
return vectorStoreOption;
|
|
7173
|
+
}
|
|
7174
|
+
if (mastra) {
|
|
7175
|
+
return mastra.getVector(vectorStoreName);
|
|
7176
|
+
}
|
|
7177
|
+
return void 0;
|
|
7178
|
+
}
|
|
7179
|
+
function coerceTopK(topK, defaultValue = 10) {
|
|
7180
|
+
if (typeof topK === "number") {
|
|
7181
|
+
if (Number.isFinite(topK) && topK > 0) {
|
|
7182
|
+
return topK;
|
|
7183
|
+
}
|
|
7184
|
+
return defaultValue;
|
|
7185
|
+
}
|
|
7186
|
+
if (typeof topK === "string") {
|
|
7187
|
+
const parsed = Number(topK);
|
|
7188
|
+
if (Number.isFinite(parsed) && parsed > 0) {
|
|
7189
|
+
return parsed;
|
|
7190
|
+
}
|
|
7191
|
+
return defaultValue;
|
|
7192
|
+
}
|
|
7193
|
+
return defaultValue;
|
|
7194
|
+
}
|
|
7195
|
+
function parseFilterValue(filter, logger) {
|
|
7196
|
+
if (!filter) {
|
|
7197
|
+
return {};
|
|
7198
|
+
}
|
|
7199
|
+
if (typeof filter === "string") {
|
|
7200
|
+
try {
|
|
7201
|
+
return JSON.parse(filter);
|
|
7202
|
+
} catch (error) {
|
|
7203
|
+
if (logger) {
|
|
7204
|
+
logger.error("Invalid filter", { filter, error });
|
|
7205
|
+
}
|
|
7206
|
+
throw new Error(`Invalid filter format: ${error instanceof Error ? error.message : String(error)}`);
|
|
7207
|
+
}
|
|
7208
|
+
}
|
|
7209
|
+
if (typeof filter !== "object" || filter === null || Array.isArray(filter)) {
|
|
7210
|
+
if (logger) {
|
|
7211
|
+
logger.error("Invalid filter", { filter, error: "Filter must be a plain object" });
|
|
7212
|
+
}
|
|
7213
|
+
throw new Error(
|
|
7214
|
+
`Invalid filter format: expected a plain object, got ${Array.isArray(filter) ? "array" : typeof filter}`
|
|
7215
|
+
);
|
|
7216
|
+
}
|
|
7217
|
+
return filter;
|
|
7218
|
+
}
|
|
7219
|
+
|
|
7111
7220
|
// src/utils/convert-sources.ts
|
|
7112
7221
|
var convertToSources = (results) => {
|
|
7113
7222
|
return results.map((result) => {
|
|
@@ -7150,7 +7259,8 @@ var defaultGraphOptions = {
|
|
|
7150
7259
|
// src/tools/graph-rag.ts
|
|
7151
7260
|
var createGraphRAGTool = (options) => {
|
|
7152
7261
|
const { model, id, description } = options;
|
|
7153
|
-
const
|
|
7262
|
+
const storeName = options["vectorStoreName"] ? options.vectorStoreName : "DirectVectorStore";
|
|
7263
|
+
const toolId = id || `GraphRAG ${storeName} ${options.indexName} Tool`;
|
|
7154
7264
|
const toolDescription = description || defaultGraphRagDescription();
|
|
7155
7265
|
const graphOptions = {
|
|
7156
7266
|
...defaultGraphOptions,
|
|
@@ -7167,7 +7277,7 @@ var createGraphRAGTool = (options) => {
|
|
|
7167
7277
|
execute: async (inputData, context) => {
|
|
7168
7278
|
const { requestContext, mastra } = context || {};
|
|
7169
7279
|
const indexName = requestContext?.get("indexName") ?? options.indexName;
|
|
7170
|
-
const vectorStoreName = requestContext?.get("vectorStoreName") ??
|
|
7280
|
+
const vectorStoreName = "vectorStore" in options ? storeName : requestContext?.get("vectorStoreName") ?? storeName;
|
|
7171
7281
|
if (!indexName) throw new Error(`indexName is required, got: ${indexName}`);
|
|
7172
7282
|
if (!vectorStoreName) throw new Error(`vectorStoreName is required, got: ${vectorStoreName}`);
|
|
7173
7283
|
const includeSources = requestContext?.get("includeSources") ?? options.includeSources ?? true;
|
|
@@ -7179,36 +7289,19 @@ var createGraphRAGTool = (options) => {
|
|
|
7179
7289
|
const providerOptions = requestContext?.get("providerOptions") ?? options.providerOptions;
|
|
7180
7290
|
const enableFilter = !!requestContext?.get("filter") || (options.enableFilter ?? false);
|
|
7181
7291
|
const logger = mastra?.getLogger();
|
|
7182
|
-
if (!logger) {
|
|
7183
|
-
console.warn(
|
|
7184
|
-
"[GraphRAGTool] Logger not initialized: no debug or error logs will be recorded for this tool execution."
|
|
7185
|
-
);
|
|
7186
|
-
}
|
|
7187
7292
|
if (logger) {
|
|
7188
7293
|
logger.debug("[GraphRAGTool] execute called with:", { queryText, topK, filter });
|
|
7189
7294
|
}
|
|
7190
7295
|
try {
|
|
7191
|
-
const topKValue =
|
|
7192
|
-
const vectorStore = mastra
|
|
7296
|
+
const topKValue = coerceTopK(topK);
|
|
7297
|
+
const vectorStore = await resolveVectorStore(options, { requestContext, mastra, vectorStoreName });
|
|
7193
7298
|
if (!vectorStore) {
|
|
7194
7299
|
if (logger) {
|
|
7195
|
-
logger.error(
|
|
7300
|
+
logger.error(`Vector store '${vectorStoreName}' not found`);
|
|
7196
7301
|
}
|
|
7197
7302
|
return { relevantContext: [], sources: [] };
|
|
7198
7303
|
}
|
|
7199
|
-
|
|
7200
|
-
if (enableFilter) {
|
|
7201
|
-
queryFilter = (() => {
|
|
7202
|
-
try {
|
|
7203
|
-
return typeof filter === "string" ? JSON.parse(filter) : filter;
|
|
7204
|
-
} catch (error) {
|
|
7205
|
-
if (logger) {
|
|
7206
|
-
logger.error("Invalid filter", { filter, error });
|
|
7207
|
-
}
|
|
7208
|
-
throw new Error(`Invalid filter format: ${error instanceof Error ? error.message : String(error)}`);
|
|
7209
|
-
}
|
|
7210
|
-
})();
|
|
7211
|
-
}
|
|
7304
|
+
const queryFilter = enableFilter && filter ? parseFilterValue(filter, logger) : {};
|
|
7212
7305
|
if (logger) {
|
|
7213
7306
|
logger.debug("Prepared vector query parameters:", { queryFilter, topK: topKValue });
|
|
7214
7307
|
}
|
|
@@ -7261,7 +7354,7 @@ var createGraphRAGTool = (options) => {
|
|
|
7261
7354
|
};
|
|
7262
7355
|
} catch (err) {
|
|
7263
7356
|
if (logger) {
|
|
7264
|
-
logger.error("Unexpected error in
|
|
7357
|
+
logger.error("Unexpected error in GraphRAGTool execute", {
|
|
7265
7358
|
error: err,
|
|
7266
7359
|
errorMessage: err instanceof Error ? err.message : String(err),
|
|
7267
7360
|
errorStack: err instanceof Error ? err.stack : void 0
|
|
@@ -7301,41 +7394,19 @@ var createVectorQueryTool = (options) => {
|
|
|
7301
7394
|
const queryText = inputData.queryText;
|
|
7302
7395
|
const enableFilter = !!requestContext?.get("filter") || (options.enableFilter ?? false);
|
|
7303
7396
|
const logger = mastra?.getLogger();
|
|
7304
|
-
if (!logger) {
|
|
7305
|
-
console.warn(
|
|
7306
|
-
"[VectorQueryTool] Logger not initialized: no debug or error logs will be recorded for this tool execution."
|
|
7307
|
-
);
|
|
7308
|
-
}
|
|
7309
7397
|
if (logger) {
|
|
7310
7398
|
logger.debug("[VectorQueryTool] execute called with:", { queryText, topK, filter, databaseConfig });
|
|
7311
7399
|
}
|
|
7312
7400
|
try {
|
|
7313
|
-
const topKValue =
|
|
7314
|
-
|
|
7315
|
-
if ("vectorStore" in options) {
|
|
7316
|
-
vectorStore = options.vectorStore;
|
|
7317
|
-
} else if (mastra) {
|
|
7318
|
-
vectorStore = mastra.getVector(vectorStoreName);
|
|
7319
|
-
}
|
|
7401
|
+
const topKValue = coerceTopK(topK);
|
|
7402
|
+
const vectorStore = await resolveVectorStore(options, { requestContext, mastra, vectorStoreName });
|
|
7320
7403
|
if (!vectorStore) {
|
|
7321
7404
|
if (logger) {
|
|
7322
|
-
logger.error(
|
|
7405
|
+
logger.error(`Vector store '${vectorStoreName}' not found`);
|
|
7323
7406
|
}
|
|
7324
7407
|
return { relevantContext: [], sources: [] };
|
|
7325
7408
|
}
|
|
7326
|
-
|
|
7327
|
-
if (enableFilter && filter) {
|
|
7328
|
-
queryFilter = (() => {
|
|
7329
|
-
try {
|
|
7330
|
-
return typeof filter === "string" ? JSON.parse(filter) : filter;
|
|
7331
|
-
} catch (error) {
|
|
7332
|
-
if (logger) {
|
|
7333
|
-
logger.error("Invalid filter", { filter, error });
|
|
7334
|
-
}
|
|
7335
|
-
throw new Error(`Invalid filter format: ${error instanceof Error ? error.message : String(error)}`);
|
|
7336
|
-
}
|
|
7337
|
-
})();
|
|
7338
|
-
}
|
|
7409
|
+
const queryFilter = enableFilter && filter ? parseFilterValue(filter, logger) : {};
|
|
7339
7410
|
if (logger) {
|
|
7340
7411
|
logger.debug("Prepared vector query parameters", { queryText, topK: topKValue, queryFilter, databaseConfig });
|
|
7341
7412
|
}
|