@mastra/rag 2.0.0-beta.4 → 2.0.0-beta.6

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.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
  }
@@ -6985,6 +6985,7 @@ var vectorQuerySearch = async ({
6985
6985
  model,
6986
6986
  value: queryText,
6987
6987
  maxRetries,
6988
+ // Type assertion needed: providerOptions type is a union, but embedV3 expects specific version
6988
6989
  ...providerOptions && { providerOptions }
6989
6990
  });
6990
6991
  } else if (model.specificationVersion === "v2") {
@@ -6992,6 +6993,7 @@ var vectorQuerySearch = async ({
6992
6993
  model,
6993
6994
  value: queryText,
6994
6995
  maxRetries,
6996
+ // Type assertion needed: providerOptions type is a union, but embedV2 expects specific version
6995
6997
  ...providerOptions && { providerOptions }
6996
6998
  });
6997
6999
  } else {
@@ -7115,6 +7117,106 @@ var filterSchema = zod.z.object({
7115
7117
  filter: zod.z.coerce.string().describe(filterDescription)
7116
7118
  });
7117
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
+
7118
7220
  // src/utils/convert-sources.ts
7119
7221
  var convertToSources = (results) => {
7120
7222
  return results.map((result) => {
@@ -7157,7 +7259,8 @@ var defaultGraphOptions = {
7157
7259
  // src/tools/graph-rag.ts
7158
7260
  var createGraphRAGTool = (options) => {
7159
7261
  const { model, id, description } = options;
7160
- const toolId = id || `GraphRAG ${options.vectorStoreName} ${options.indexName} Tool`;
7262
+ const storeName = options["vectorStoreName"] ? options.vectorStoreName : "DirectVectorStore";
7263
+ const toolId = id || `GraphRAG ${storeName} ${options.indexName} Tool`;
7161
7264
  const toolDescription = description || defaultGraphRagDescription();
7162
7265
  const graphOptions = {
7163
7266
  ...defaultGraphOptions,
@@ -7174,7 +7277,7 @@ var createGraphRAGTool = (options) => {
7174
7277
  execute: async (inputData, context) => {
7175
7278
  const { requestContext, mastra } = context || {};
7176
7279
  const indexName = requestContext?.get("indexName") ?? options.indexName;
7177
- const vectorStoreName = requestContext?.get("vectorStoreName") ?? options.vectorStoreName;
7280
+ const vectorStoreName = "vectorStore" in options ? storeName : requestContext?.get("vectorStoreName") ?? storeName;
7178
7281
  if (!indexName) throw new Error(`indexName is required, got: ${indexName}`);
7179
7282
  if (!vectorStoreName) throw new Error(`vectorStoreName is required, got: ${vectorStoreName}`);
7180
7283
  const includeSources = requestContext?.get("includeSources") ?? options.includeSources ?? true;
@@ -7186,36 +7289,19 @@ var createGraphRAGTool = (options) => {
7186
7289
  const providerOptions = requestContext?.get("providerOptions") ?? options.providerOptions;
7187
7290
  const enableFilter = !!requestContext?.get("filter") || (options.enableFilter ?? false);
7188
7291
  const logger = mastra?.getLogger();
7189
- if (!logger) {
7190
- console.warn(
7191
- "[GraphRAGTool] Logger not initialized: no debug or error logs will be recorded for this tool execution."
7192
- );
7193
- }
7194
7292
  if (logger) {
7195
7293
  logger.debug("[GraphRAGTool] execute called with:", { queryText, topK, filter });
7196
7294
  }
7197
7295
  try {
7198
- const topKValue = typeof topK === "number" && !isNaN(topK) ? topK : typeof topK === "string" && !isNaN(Number(topK)) ? Number(topK) : 10;
7199
- const vectorStore = mastra?.getVector(vectorStoreName);
7296
+ const topKValue = coerceTopK(topK);
7297
+ const vectorStore = await resolveVectorStore(options, { requestContext, mastra, vectorStoreName });
7200
7298
  if (!vectorStore) {
7201
7299
  if (logger) {
7202
- logger.error("Vector store not found", { vectorStoreName });
7300
+ logger.error(`Vector store '${vectorStoreName}' not found`);
7203
7301
  }
7204
7302
  return { relevantContext: [], sources: [] };
7205
7303
  }
7206
- let queryFilter = {};
7207
- if (enableFilter) {
7208
- queryFilter = (() => {
7209
- try {
7210
- return typeof filter === "string" ? JSON.parse(filter) : filter;
7211
- } catch (error) {
7212
- if (logger) {
7213
- logger.error("Invalid filter", { filter, error });
7214
- }
7215
- throw new Error(`Invalid filter format: ${error instanceof Error ? error.message : String(error)}`);
7216
- }
7217
- })();
7218
- }
7304
+ const queryFilter = enableFilter && filter ? parseFilterValue(filter, logger) : {};
7219
7305
  if (logger) {
7220
7306
  logger.debug("Prepared vector query parameters:", { queryFilter, topK: topKValue });
7221
7307
  }
@@ -7268,7 +7354,7 @@ var createGraphRAGTool = (options) => {
7268
7354
  };
7269
7355
  } catch (err) {
7270
7356
  if (logger) {
7271
- logger.error("Unexpected error in VectorQueryTool execute", {
7357
+ logger.error("Unexpected error in GraphRAGTool execute", {
7272
7358
  error: err,
7273
7359
  errorMessage: err instanceof Error ? err.message : String(err),
7274
7360
  errorStack: err instanceof Error ? err.stack : void 0
@@ -7308,41 +7394,19 @@ var createVectorQueryTool = (options) => {
7308
7394
  const queryText = inputData.queryText;
7309
7395
  const enableFilter = !!requestContext?.get("filter") || (options.enableFilter ?? false);
7310
7396
  const logger = mastra?.getLogger();
7311
- if (!logger) {
7312
- console.warn(
7313
- "[VectorQueryTool] Logger not initialized: no debug or error logs will be recorded for this tool execution."
7314
- );
7315
- }
7316
7397
  if (logger) {
7317
7398
  logger.debug("[VectorQueryTool] execute called with:", { queryText, topK, filter, databaseConfig });
7318
7399
  }
7319
7400
  try {
7320
- const topKValue = typeof topK === "number" && !isNaN(topK) ? topK : typeof topK === "string" && !isNaN(Number(topK)) ? Number(topK) : 10;
7321
- let vectorStore = void 0;
7322
- if ("vectorStore" in options) {
7323
- vectorStore = options.vectorStore;
7324
- } else if (mastra) {
7325
- vectorStore = mastra.getVector(vectorStoreName);
7326
- }
7401
+ const topKValue = coerceTopK(topK);
7402
+ const vectorStore = await resolveVectorStore(options, { requestContext, mastra, vectorStoreName });
7327
7403
  if (!vectorStore) {
7328
7404
  if (logger) {
7329
- logger.error("Vector store not found", { vectorStoreName });
7405
+ logger.error(`Vector store '${vectorStoreName}' not found`);
7330
7406
  }
7331
7407
  return { relevantContext: [], sources: [] };
7332
7408
  }
7333
- let queryFilter = {};
7334
- if (enableFilter && filter) {
7335
- queryFilter = (() => {
7336
- try {
7337
- return typeof filter === "string" ? JSON.parse(filter) : filter;
7338
- } catch (error) {
7339
- if (logger) {
7340
- logger.error("Invalid filter", { filter, error });
7341
- }
7342
- throw new Error(`Invalid filter format: ${error instanceof Error ? error.message : String(error)}`);
7343
- }
7344
- })();
7345
- }
7409
+ const queryFilter = enableFilter && filter ? parseFilterValue(filter, logger) : {};
7346
7410
  if (logger) {
7347
7411
  logger.debug("Prepared vector query parameters", { queryText, topK: topKValue, queryFilter, databaseConfig });
7348
7412
  }