@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.js CHANGED
@@ -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
  }
@@ -6979,6 +6979,7 @@ var vectorQuerySearch = async ({
6979
6979
  model,
6980
6980
  value: queryText,
6981
6981
  maxRetries,
6982
+ // Type assertion needed: providerOptions type is a union, but embedV3 expects specific version
6982
6983
  ...providerOptions && { providerOptions }
6983
6984
  });
6984
6985
  } else if (model.specificationVersion === "v2") {
@@ -6986,6 +6987,7 @@ var vectorQuerySearch = async ({
6986
6987
  model,
6987
6988
  value: queryText,
6988
6989
  maxRetries,
6990
+ // Type assertion needed: providerOptions type is a union, but embedV2 expects specific version
6989
6991
  ...providerOptions && { providerOptions }
6990
6992
  });
6991
6993
  } else {
@@ -7109,6 +7111,106 @@ var filterSchema = z.object({
7109
7111
  filter: z.coerce.string().describe(filterDescription)
7110
7112
  });
7111
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
+
7112
7214
  // src/utils/convert-sources.ts
7113
7215
  var convertToSources = (results) => {
7114
7216
  return results.map((result) => {
@@ -7151,7 +7253,8 @@ var defaultGraphOptions = {
7151
7253
  // src/tools/graph-rag.ts
7152
7254
  var createGraphRAGTool = (options) => {
7153
7255
  const { model, id, description } = options;
7154
- 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`;
7155
7258
  const toolDescription = description || defaultGraphRagDescription();
7156
7259
  const graphOptions = {
7157
7260
  ...defaultGraphOptions,
@@ -7168,7 +7271,7 @@ var createGraphRAGTool = (options) => {
7168
7271
  execute: async (inputData, context) => {
7169
7272
  const { requestContext, mastra } = context || {};
7170
7273
  const indexName = requestContext?.get("indexName") ?? options.indexName;
7171
- const vectorStoreName = requestContext?.get("vectorStoreName") ?? options.vectorStoreName;
7274
+ const vectorStoreName = "vectorStore" in options ? storeName : requestContext?.get("vectorStoreName") ?? storeName;
7172
7275
  if (!indexName) throw new Error(`indexName is required, got: ${indexName}`);
7173
7276
  if (!vectorStoreName) throw new Error(`vectorStoreName is required, got: ${vectorStoreName}`);
7174
7277
  const includeSources = requestContext?.get("includeSources") ?? options.includeSources ?? true;
@@ -7180,36 +7283,19 @@ var createGraphRAGTool = (options) => {
7180
7283
  const providerOptions = requestContext?.get("providerOptions") ?? options.providerOptions;
7181
7284
  const enableFilter = !!requestContext?.get("filter") || (options.enableFilter ?? false);
7182
7285
  const logger = mastra?.getLogger();
7183
- if (!logger) {
7184
- console.warn(
7185
- "[GraphRAGTool] Logger not initialized: no debug or error logs will be recorded for this tool execution."
7186
- );
7187
- }
7188
7286
  if (logger) {
7189
7287
  logger.debug("[GraphRAGTool] execute called with:", { queryText, topK, filter });
7190
7288
  }
7191
7289
  try {
7192
- const topKValue = typeof topK === "number" && !isNaN(topK) ? topK : typeof topK === "string" && !isNaN(Number(topK)) ? Number(topK) : 10;
7193
- const vectorStore = mastra?.getVector(vectorStoreName);
7290
+ const topKValue = coerceTopK(topK);
7291
+ const vectorStore = await resolveVectorStore(options, { requestContext, mastra, vectorStoreName });
7194
7292
  if (!vectorStore) {
7195
7293
  if (logger) {
7196
- logger.error("Vector store not found", { vectorStoreName });
7294
+ logger.error(`Vector store '${vectorStoreName}' not found`);
7197
7295
  }
7198
7296
  return { relevantContext: [], sources: [] };
7199
7297
  }
7200
- let queryFilter = {};
7201
- if (enableFilter) {
7202
- queryFilter = (() => {
7203
- try {
7204
- return typeof filter === "string" ? JSON.parse(filter) : filter;
7205
- } catch (error) {
7206
- if (logger) {
7207
- logger.error("Invalid filter", { filter, error });
7208
- }
7209
- throw new Error(`Invalid filter format: ${error instanceof Error ? error.message : String(error)}`);
7210
- }
7211
- })();
7212
- }
7298
+ const queryFilter = enableFilter && filter ? parseFilterValue(filter, logger) : {};
7213
7299
  if (logger) {
7214
7300
  logger.debug("Prepared vector query parameters:", { queryFilter, topK: topKValue });
7215
7301
  }
@@ -7262,7 +7348,7 @@ var createGraphRAGTool = (options) => {
7262
7348
  };
7263
7349
  } catch (err) {
7264
7350
  if (logger) {
7265
- logger.error("Unexpected error in VectorQueryTool execute", {
7351
+ logger.error("Unexpected error in GraphRAGTool execute", {
7266
7352
  error: err,
7267
7353
  errorMessage: err instanceof Error ? err.message : String(err),
7268
7354
  errorStack: err instanceof Error ? err.stack : void 0
@@ -7302,41 +7388,19 @@ var createVectorQueryTool = (options) => {
7302
7388
  const queryText = inputData.queryText;
7303
7389
  const enableFilter = !!requestContext?.get("filter") || (options.enableFilter ?? false);
7304
7390
  const logger = mastra?.getLogger();
7305
- if (!logger) {
7306
- console.warn(
7307
- "[VectorQueryTool] Logger not initialized: no debug or error logs will be recorded for this tool execution."
7308
- );
7309
- }
7310
7391
  if (logger) {
7311
7392
  logger.debug("[VectorQueryTool] execute called with:", { queryText, topK, filter, databaseConfig });
7312
7393
  }
7313
7394
  try {
7314
- const topKValue = typeof topK === "number" && !isNaN(topK) ? topK : typeof topK === "string" && !isNaN(Number(topK)) ? Number(topK) : 10;
7315
- let vectorStore = void 0;
7316
- if ("vectorStore" in options) {
7317
- vectorStore = options.vectorStore;
7318
- } else if (mastra) {
7319
- vectorStore = mastra.getVector(vectorStoreName);
7320
- }
7395
+ const topKValue = coerceTopK(topK);
7396
+ const vectorStore = await resolveVectorStore(options, { requestContext, mastra, vectorStoreName });
7321
7397
  if (!vectorStore) {
7322
7398
  if (logger) {
7323
- logger.error("Vector store not found", { vectorStoreName });
7399
+ logger.error(`Vector store '${vectorStoreName}' not found`);
7324
7400
  }
7325
7401
  return { relevantContext: [], sources: [] };
7326
7402
  }
7327
- let queryFilter = {};
7328
- if (enableFilter && filter) {
7329
- queryFilter = (() => {
7330
- try {
7331
- return typeof filter === "string" ? JSON.parse(filter) : filter;
7332
- } catch (error) {
7333
- if (logger) {
7334
- logger.error("Invalid filter", { filter, error });
7335
- }
7336
- throw new Error(`Invalid filter format: ${error instanceof Error ? error.message : String(error)}`);
7337
- }
7338
- })();
7339
- }
7403
+ const queryFilter = enableFilter && filter ? parseFilterValue(filter, logger) : {};
7340
7404
  if (logger) {
7341
7405
  logger.debug("Prepared vector query parameters", { queryText, topK: topKValue, queryFilter, databaseConfig });
7342
7406
  }