@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.
Files changed (37) hide show
  1. package/CHANGELOG.md +45 -0
  2. package/dist/docs/README.md +32 -0
  3. package/dist/docs/SKILL.md +33 -0
  4. package/dist/docs/SOURCE_MAP.json +6 -0
  5. package/dist/docs/rag/01-overview.md +74 -0
  6. package/dist/docs/rag/02-chunking-and-embedding.md +190 -0
  7. package/dist/docs/rag/03-retrieval.md +549 -0
  8. package/dist/docs/rag/04-graph-rag.md +217 -0
  9. package/dist/docs/rag/05-reference.md +854 -0
  10. package/dist/docs/tools/01-reference.md +684 -0
  11. package/dist/document/extractors/keywords.d.ts +2 -2
  12. package/dist/document/extractors/keywords.d.ts.map +1 -1
  13. package/dist/document/extractors/questions.d.ts +2 -2
  14. package/dist/document/extractors/questions.d.ts.map +1 -1
  15. package/dist/document/extractors/summary.d.ts.map +1 -1
  16. package/dist/document/extractors/title.d.ts +2 -2
  17. package/dist/document/extractors/title.d.ts.map +1 -1
  18. package/dist/document/extractors/types.d.ts +6 -6
  19. package/dist/document/extractors/types.d.ts.map +1 -1
  20. package/dist/index.cjs +130 -59
  21. package/dist/index.cjs.map +1 -1
  22. package/dist/index.js +132 -61
  23. package/dist/index.js.map +1 -1
  24. package/dist/rerank/relevance/mastra-agent/index.d.ts +2 -2
  25. package/dist/rerank/relevance/mastra-agent/index.d.ts.map +1 -1
  26. package/dist/tools/graph-rag.d.ts.map +1 -1
  27. package/dist/tools/index.d.ts +1 -0
  28. package/dist/tools/index.d.ts.map +1 -1
  29. package/dist/tools/types.d.ts +171 -14
  30. package/dist/tools/types.d.ts.map +1 -1
  31. package/dist/tools/vector-query.d.ts.map +1 -1
  32. package/dist/utils/index.d.ts +1 -0
  33. package/dist/utils/index.d.ts.map +1 -1
  34. package/dist/utils/tool-helpers.d.ts +54 -0
  35. package/dist/utils/tool-helpers.d.ts.map +1 -0
  36. package/dist/utils/vector-search.d.ts.map +1 -1
  37. package/package.json +9 -9
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { Agent } from '@mastra/core/agent';
1
+ import { Agent, isSupportedLanguageModel } from '@mastra/core/agent';
2
2
  import { randomUUID, createHash } from 'crypto';
3
3
  import { z } from 'zod';
4
4
  import { parse } from 'node-html-better-parser';
@@ -7,7 +7,7 @@ import { Big } from 'big.js';
7
7
  import { createSimilarityPrompt } from '@mastra/core/relevance';
8
8
  import ZeroEntropy from 'zeroentropy';
9
9
  import { createTool } from '@mastra/core/tools';
10
- import { embedV2, embedV1 } from '@mastra/core/vector';
10
+ import { embedV3, embedV2, embedV1 } from '@mastra/core/vector';
11
11
 
12
12
  var __create = Object.create;
13
13
  var __defProp = Object.defineProperty;
@@ -15,7 +15,7 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
15
15
  var __getOwnPropNames = Object.getOwnPropertyNames;
16
16
  var __getProtoOf = Object.getPrototypeOf;
17
17
  var __hasOwnProp = Object.prototype.hasOwnProperty;
18
- var __knownSymbol = (name14, symbol15) => (symbol15 = Symbol[name14]) ? symbol15 : Symbol.for("Symbol." + name14);
18
+ var __knownSymbol = (name14, symbol15) => (symbol15 = Symbol[name14]) ? symbol15 : /* @__PURE__ */ Symbol.for("Symbol." + name14);
19
19
  var __typeError = (msg) => {
20
20
  throw TypeError(msg);
21
21
  };
@@ -896,7 +896,7 @@ function loadApiKey({
896
896
  }
897
897
  return apiKey;
898
898
  }
899
- var validatorSymbol = Symbol.for("vercel.ai.validator");
899
+ var validatorSymbol = /* @__PURE__ */ Symbol.for("vercel.ai.validator");
900
900
  function validator(validate) {
901
901
  return { [validatorSymbol]: true, validate };
902
902
  }
@@ -4135,7 +4135,7 @@ var TitleExtractor = class extends BaseExtractor {
4135
4135
  const titleCandidates = await this.getTitlesCandidates(nodes);
4136
4136
  const combinedTitles = titleCandidates.join(", ");
4137
4137
  let title = "";
4138
- if (this.llm.specificationVersion === "v2") {
4138
+ if (isSupportedLanguageModel(this.llm)) {
4139
4139
  const miniAgent = new Agent({
4140
4140
  id: "title-extractor",
4141
4141
  model: this.llm,
@@ -4174,7 +4174,7 @@ var TitleExtractor = class extends BaseExtractor {
4174
4174
  });
4175
4175
  const titleJobs = nodes.map(async (node) => {
4176
4176
  let completion;
4177
- if (this.llm.specificationVersion === "v2") {
4177
+ if (isSupportedLanguageModel(this.llm)) {
4178
4178
  const result = await miniAgent.generate([
4179
4179
  { role: "user", content: this.nodeTemplate.format({ context: node.getContent() }) }
4180
4180
  ]);
@@ -4240,7 +4240,7 @@ var SummaryExtractor = class extends BaseExtractor {
4240
4240
  instructions: "You are a summary extractor. You are given a node and you need to extract the summary from the node."
4241
4241
  });
4242
4242
  let summary = "";
4243
- if (this.llm.specificationVersion === "v2") {
4243
+ if (isSupportedLanguageModel(this.llm)) {
4244
4244
  const result = await miniAgent.generate([{ role: "user", content: prompt }]);
4245
4245
  summary = result.text;
4246
4246
  } else {
@@ -4326,7 +4326,7 @@ var QuestionsAnsweredExtractor = class extends BaseExtractor {
4326
4326
  instructions: "You are a question extractor. You are given a node and you need to extract the questions from the node."
4327
4327
  });
4328
4328
  let questionsText = "";
4329
- if (this.llm.specificationVersion === "v2") {
4329
+ if (isSupportedLanguageModel(this.llm)) {
4330
4330
  const result2 = await miniAgent.generate([{ role: "user", content: prompt }]);
4331
4331
  questionsText = result2.text;
4332
4332
  } else {
@@ -4398,7 +4398,7 @@ var KeywordExtractor = class extends BaseExtractor {
4398
4398
  name: "keyword-extractor",
4399
4399
  instructions: "You are a keyword extractor. You are given a node and you need to extract the keywords from the node."
4400
4400
  });
4401
- if (this.llm.specificationVersion === "v2") {
4401
+ if (isSupportedLanguageModel(this.llm)) {
4402
4402
  const result = await miniAgent.generate([
4403
4403
  {
4404
4404
  role: "user",
@@ -6588,7 +6588,7 @@ Always return just the number, no explanation.`,
6588
6588
  const prompt = createSimilarityPrompt(query, text);
6589
6589
  const model = await this.agent.getModel();
6590
6590
  let response;
6591
- if (model.specificationVersion === "v2") {
6591
+ if (isSupportedLanguageModel(model)) {
6592
6592
  response = await this.agent.generate(prompt);
6593
6593
  } else {
6594
6594
  response = await this.agent.generateLegacy(prompt);
@@ -6974,11 +6974,20 @@ var vectorQuerySearch = async ({
6974
6974
  providerOptions
6975
6975
  }) => {
6976
6976
  let embeddingResult;
6977
- if (model.specificationVersion === "v2") {
6977
+ if (model.specificationVersion === "v3") {
6978
+ embeddingResult = await embedV3({
6979
+ model,
6980
+ value: queryText,
6981
+ maxRetries,
6982
+ // Type assertion needed: providerOptions type is a union, but embedV3 expects specific version
6983
+ ...providerOptions && { providerOptions }
6984
+ });
6985
+ } else if (model.specificationVersion === "v2") {
6978
6986
  embeddingResult = await embedV2({
6979
6987
  model,
6980
6988
  value: queryText,
6981
6989
  maxRetries,
6990
+ // Type assertion needed: providerOptions type is a union, but embedV2 expects specific version
6982
6991
  ...providerOptions && { providerOptions }
6983
6992
  });
6984
6993
  } else {
@@ -7102,6 +7111,106 @@ var filterSchema = z.object({
7102
7111
  filter: z.coerce.string().describe(filterDescription)
7103
7112
  });
7104
7113
 
7114
+ // src/utils/tool-helpers.ts
7115
+ function isValidMastraVector(value) {
7116
+ return value !== null && value !== void 0 && typeof value === "object";
7117
+ }
7118
+ function buildContextString(context) {
7119
+ const parts = [];
7120
+ if (context.vectorStoreName) {
7121
+ parts.push(`vectorStoreName="${context.vectorStoreName}"`);
7122
+ }
7123
+ if (context.requestContext) {
7124
+ const schemaId = context.requestContext.get?.("schemaId");
7125
+ const tenantId = context.requestContext.get?.("tenantId");
7126
+ if (schemaId) parts.push(`schemaId="${schemaId}"`);
7127
+ if (tenantId) parts.push(`tenantId="${tenantId}"`);
7128
+ }
7129
+ if (context.mastra) {
7130
+ parts.push("mastra=provided");
7131
+ }
7132
+ return parts.length > 0 ? ` (context: ${parts.join(", ")})` : "";
7133
+ }
7134
+ async function resolveVectorStore(options, context, logger) {
7135
+ const { requestContext, mastra, vectorStoreName, fallbackOnInvalid = false } = context;
7136
+ if ("vectorStore" in options && options.vectorStore !== void 0) {
7137
+ const vectorStoreOption = options.vectorStore;
7138
+ if (typeof vectorStoreOption === "function") {
7139
+ const resolved = await vectorStoreOption({ requestContext, mastra });
7140
+ if (!isValidMastraVector(resolved)) {
7141
+ const contextStr = buildContextString(context);
7142
+ const receivedType = resolved === null ? "null" : resolved === void 0 ? "undefined" : typeof resolved;
7143
+ if (fallbackOnInvalid) {
7144
+ if (mastra && vectorStoreName) {
7145
+ return mastra.getVector(vectorStoreName);
7146
+ }
7147
+ return void 0;
7148
+ }
7149
+ throw new Error(
7150
+ `VectorStoreResolver returned invalid value: expected MastraVector instance, got ${receivedType}${contextStr}`
7151
+ );
7152
+ }
7153
+ return resolved;
7154
+ }
7155
+ if (!isValidMastraVector(vectorStoreOption)) {
7156
+ const contextStr = buildContextString(context);
7157
+ const receivedType = vectorStoreOption === null ? "null" : vectorStoreOption === void 0 ? "undefined" : typeof vectorStoreOption;
7158
+ if (fallbackOnInvalid) {
7159
+ if (mastra && vectorStoreName) {
7160
+ return mastra.getVector(vectorStoreName);
7161
+ }
7162
+ return void 0;
7163
+ }
7164
+ throw new Error(`vectorStore option is not a valid MastraVector instance: got ${receivedType}${contextStr}`);
7165
+ }
7166
+ return vectorStoreOption;
7167
+ }
7168
+ if (mastra) {
7169
+ return mastra.getVector(vectorStoreName);
7170
+ }
7171
+ return void 0;
7172
+ }
7173
+ function coerceTopK(topK, defaultValue = 10) {
7174
+ if (typeof topK === "number") {
7175
+ if (Number.isFinite(topK) && topK > 0) {
7176
+ return topK;
7177
+ }
7178
+ return defaultValue;
7179
+ }
7180
+ if (typeof topK === "string") {
7181
+ const parsed = Number(topK);
7182
+ if (Number.isFinite(parsed) && parsed > 0) {
7183
+ return parsed;
7184
+ }
7185
+ return defaultValue;
7186
+ }
7187
+ return defaultValue;
7188
+ }
7189
+ function parseFilterValue(filter, logger) {
7190
+ if (!filter) {
7191
+ return {};
7192
+ }
7193
+ if (typeof filter === "string") {
7194
+ try {
7195
+ return JSON.parse(filter);
7196
+ } catch (error) {
7197
+ if (logger) {
7198
+ logger.error("Invalid filter", { filter, error });
7199
+ }
7200
+ throw new Error(`Invalid filter format: ${error instanceof Error ? error.message : String(error)}`);
7201
+ }
7202
+ }
7203
+ if (typeof filter !== "object" || filter === null || Array.isArray(filter)) {
7204
+ if (logger) {
7205
+ logger.error("Invalid filter", { filter, error: "Filter must be a plain object" });
7206
+ }
7207
+ throw new Error(
7208
+ `Invalid filter format: expected a plain object, got ${Array.isArray(filter) ? "array" : typeof filter}`
7209
+ );
7210
+ }
7211
+ return filter;
7212
+ }
7213
+
7105
7214
  // src/utils/convert-sources.ts
7106
7215
  var convertToSources = (results) => {
7107
7216
  return results.map((result) => {
@@ -7144,7 +7253,8 @@ var defaultGraphOptions = {
7144
7253
  // src/tools/graph-rag.ts
7145
7254
  var createGraphRAGTool = (options) => {
7146
7255
  const { model, id, description } = options;
7147
- const toolId = id || `GraphRAG ${options.vectorStoreName} ${options.indexName} Tool`;
7256
+ const storeName = options["vectorStoreName"] ? options.vectorStoreName : "DirectVectorStore";
7257
+ const toolId = id || `GraphRAG ${storeName} ${options.indexName} Tool`;
7148
7258
  const toolDescription = description || defaultGraphRagDescription();
7149
7259
  const graphOptions = {
7150
7260
  ...defaultGraphOptions,
@@ -7161,7 +7271,7 @@ var createGraphRAGTool = (options) => {
7161
7271
  execute: async (inputData, context) => {
7162
7272
  const { requestContext, mastra } = context || {};
7163
7273
  const indexName = requestContext?.get("indexName") ?? options.indexName;
7164
- const vectorStoreName = requestContext?.get("vectorStoreName") ?? options.vectorStoreName;
7274
+ const vectorStoreName = "vectorStore" in options ? storeName : requestContext?.get("vectorStoreName") ?? storeName;
7165
7275
  if (!indexName) throw new Error(`indexName is required, got: ${indexName}`);
7166
7276
  if (!vectorStoreName) throw new Error(`vectorStoreName is required, got: ${vectorStoreName}`);
7167
7277
  const includeSources = requestContext?.get("includeSources") ?? options.includeSources ?? true;
@@ -7173,36 +7283,19 @@ var createGraphRAGTool = (options) => {
7173
7283
  const providerOptions = requestContext?.get("providerOptions") ?? options.providerOptions;
7174
7284
  const enableFilter = !!requestContext?.get("filter") || (options.enableFilter ?? false);
7175
7285
  const logger = mastra?.getLogger();
7176
- if (!logger) {
7177
- console.warn(
7178
- "[GraphRAGTool] Logger not initialized: no debug or error logs will be recorded for this tool execution."
7179
- );
7180
- }
7181
7286
  if (logger) {
7182
7287
  logger.debug("[GraphRAGTool] execute called with:", { queryText, topK, filter });
7183
7288
  }
7184
7289
  try {
7185
- const topKValue = typeof topK === "number" && !isNaN(topK) ? topK : typeof topK === "string" && !isNaN(Number(topK)) ? Number(topK) : 10;
7186
- const vectorStore = mastra?.getVector(vectorStoreName);
7290
+ const topKValue = coerceTopK(topK);
7291
+ const vectorStore = await resolveVectorStore(options, { requestContext, mastra, vectorStoreName });
7187
7292
  if (!vectorStore) {
7188
7293
  if (logger) {
7189
- logger.error("Vector store not found", { vectorStoreName });
7294
+ logger.error(`Vector store '${vectorStoreName}' not found`);
7190
7295
  }
7191
7296
  return { relevantContext: [], sources: [] };
7192
7297
  }
7193
- let queryFilter = {};
7194
- if (enableFilter) {
7195
- queryFilter = (() => {
7196
- try {
7197
- return typeof filter === "string" ? JSON.parse(filter) : filter;
7198
- } catch (error) {
7199
- if (logger) {
7200
- logger.error("Invalid filter", { filter, error });
7201
- }
7202
- throw new Error(`Invalid filter format: ${error instanceof Error ? error.message : String(error)}`);
7203
- }
7204
- })();
7205
- }
7298
+ const queryFilter = enableFilter && filter ? parseFilterValue(filter, logger) : {};
7206
7299
  if (logger) {
7207
7300
  logger.debug("Prepared vector query parameters:", { queryFilter, topK: topKValue });
7208
7301
  }
@@ -7255,7 +7348,7 @@ var createGraphRAGTool = (options) => {
7255
7348
  };
7256
7349
  } catch (err) {
7257
7350
  if (logger) {
7258
- logger.error("Unexpected error in VectorQueryTool execute", {
7351
+ logger.error("Unexpected error in GraphRAGTool execute", {
7259
7352
  error: err,
7260
7353
  errorMessage: err instanceof Error ? err.message : String(err),
7261
7354
  errorStack: err instanceof Error ? err.stack : void 0
@@ -7295,41 +7388,19 @@ var createVectorQueryTool = (options) => {
7295
7388
  const queryText = inputData.queryText;
7296
7389
  const enableFilter = !!requestContext?.get("filter") || (options.enableFilter ?? false);
7297
7390
  const logger = mastra?.getLogger();
7298
- if (!logger) {
7299
- console.warn(
7300
- "[VectorQueryTool] Logger not initialized: no debug or error logs will be recorded for this tool execution."
7301
- );
7302
- }
7303
7391
  if (logger) {
7304
7392
  logger.debug("[VectorQueryTool] execute called with:", { queryText, topK, filter, databaseConfig });
7305
7393
  }
7306
7394
  try {
7307
- const topKValue = typeof topK === "number" && !isNaN(topK) ? topK : typeof topK === "string" && !isNaN(Number(topK)) ? Number(topK) : 10;
7308
- let vectorStore = void 0;
7309
- if ("vectorStore" in options) {
7310
- vectorStore = options.vectorStore;
7311
- } else if (mastra) {
7312
- vectorStore = mastra.getVector(vectorStoreName);
7313
- }
7395
+ const topKValue = coerceTopK(topK);
7396
+ const vectorStore = await resolveVectorStore(options, { requestContext, mastra, vectorStoreName });
7314
7397
  if (!vectorStore) {
7315
7398
  if (logger) {
7316
- logger.error("Vector store not found", { vectorStoreName });
7399
+ logger.error(`Vector store '${vectorStoreName}' not found`);
7317
7400
  }
7318
7401
  return { relevantContext: [], sources: [] };
7319
7402
  }
7320
- let queryFilter = {};
7321
- if (enableFilter && filter) {
7322
- queryFilter = (() => {
7323
- try {
7324
- return typeof filter === "string" ? JSON.parse(filter) : filter;
7325
- } catch (error) {
7326
- if (logger) {
7327
- logger.error("Invalid filter", { filter, error });
7328
- }
7329
- throw new Error(`Invalid filter format: ${error instanceof Error ? error.message : String(error)}`);
7330
- }
7331
- })();
7332
- }
7403
+ const queryFilter = enableFilter && filter ? parseFilterValue(filter, logger) : {};
7333
7404
  if (logger) {
7334
7405
  logger.debug("Prepared vector query parameters", { queryText, topK: topKValue, queryFilter, databaseConfig });
7335
7406
  }