@mastra/rag 2.7.3-alpha.2 → 2.8.0

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
@@ -9,6 +9,7 @@ import { createSimilarityPrompt } from "@mastra/core/relevance";
9
9
  import ZeroEntropy from "zeroentropy";
10
10
  import { createTool } from "@mastra/core/tools";
11
11
  import { embedV1, embedV2, embedV3, embedV4 } from "@mastra/core/vector";
12
+ import { getRequestUser } from "@mastra/core/utils";
12
13
  import { AgenticRetrieveStreamCommand, BedrockAgentRuntimeClient, RetrieveCommand } from "@aws-sdk/client-bedrock-agent-runtime";
13
14
  // \0rolldown/runtime.js
14
15
  var __create$1 = Object.create;
@@ -4847,6 +4848,7 @@ var RecursiveJsonTransformer = class RecursiveJsonTransformer {
4847
4848
  ensureAscii;
4848
4849
  convertLists;
4849
4850
  constructor({ maxSize = 2e3, ensureAscii = false, convertLists = true }) {
4851
+ if (!Number.isFinite(maxSize) || maxSize < 21) throw new RangeError("JSON maxSize must be at least 21");
4850
4852
  this.maxSize = maxSize;
4851
4853
  this.ensureAscii = ensureAscii;
4852
4854
  this.convertLists = convertLists;
@@ -5779,6 +5781,7 @@ const validationSchemas = {
5779
5781
  disallowedSpecial: setOrAllSchema
5780
5782
  }).strict(),
5781
5783
  json: baseChunkOptionsSchema.extend({
5784
+ maxSize: z.number().min(21).optional(),
5782
5785
  minSize: z.number().positive().optional(),
5783
5786
  ensureAscii: z.boolean().optional(),
5784
5787
  convertLists: z.boolean().optional()
@@ -6746,7 +6749,8 @@ async function resolveVectorStore(options, context, logger) {
6746
6749
  if (typeof vectorStoreOption === "function") {
6747
6750
  const resolved = await vectorStoreOption({
6748
6751
  requestContext,
6749
- mastra
6752
+ mastra,
6753
+ user: getRequestUser(requestContext)
6750
6754
  });
6751
6755
  if (!isValidMastraVector(resolved)) {
6752
6756
  const contextStr = buildContextString(context);
@@ -6850,8 +6854,6 @@ const createGraphRAGTool = (options) => {
6850
6854
  ...defaultGraphOptions,
6851
6855
  ...options.graphOptions || {}
6852
6856
  };
6853
- const graphRag = new GraphRAG(graphOptions.dimension, graphOptions.threshold);
6854
- let isInitialized = false;
6855
6857
  const inputSchema = options.enableFilter ? filterSchema : z.object(baseSchema).passthrough();
6856
6858
  return createTool({
6857
6859
  id: toolId,
@@ -6911,38 +6913,40 @@ const createGraphRAGTool = (options) => {
6911
6913
  observabilityContext
6912
6914
  });
6913
6915
  if (logger) logger.debug("vectorQuerySearch returned results", { count: results.length });
6914
- if (!isInitialized) {
6915
- const chunks = results.map((result) => ({
6916
- text: result?.metadata?.text,
6917
- metadata: result.metadata ?? {}
6918
- }));
6919
- const embeddings = results.map((result) => ({ vector: result.vector || [] }));
6920
- if (logger) logger.debug("Initializing graph", {
6921
- chunkCount: chunks.length,
6922
- embeddingCount: embeddings.length
6923
- });
6924
- const buildSpan = parentSpan?.createChildSpan({
6925
- type: SpanType.GRAPH_ACTION,
6926
- name: "graph build",
6927
- input: { nodeCount: chunks.length },
6928
- attributes: {
6929
- action: "build",
6930
- nodeCount: chunks.length,
6931
- threshold: graphOptions.threshold
6932
- }
6933
- });
6934
- try {
6935
- graphRag.createGraph(chunks, embeddings);
6936
- } catch (err) {
6937
- buildSpan?.error({
6938
- error: err,
6939
- endSpan: true
6940
- });
6941
- throw err;
6916
+ if (results.length === 0) return {
6917
+ relevantContext: [],
6918
+ sources: []
6919
+ };
6920
+ const graphRag = new GraphRAG(graphOptions.dimension, graphOptions.threshold);
6921
+ const chunks = results.map((result) => ({
6922
+ text: result?.metadata?.text,
6923
+ metadata: result.metadata ?? {}
6924
+ }));
6925
+ const embeddings = results.map((result) => ({ vector: result.vector || [] }));
6926
+ if (logger) logger.debug("Initializing graph", {
6927
+ chunkCount: chunks.length,
6928
+ embeddingCount: embeddings.length
6929
+ });
6930
+ const buildSpan = parentSpan?.createChildSpan({
6931
+ type: SpanType.GRAPH_ACTION,
6932
+ name: "graph build",
6933
+ input: { nodeCount: chunks.length },
6934
+ attributes: {
6935
+ action: "build",
6936
+ nodeCount: chunks.length,
6937
+ threshold: graphOptions.threshold
6942
6938
  }
6943
- buildSpan?.end();
6944
- isInitialized = true;
6945
- } else if (logger) logger.debug("Graph already initialized, skipping graph construction");
6939
+ });
6940
+ try {
6941
+ graphRag.createGraph(chunks, embeddings);
6942
+ } catch (err) {
6943
+ buildSpan?.error({
6944
+ error: err,
6945
+ endSpan: true
6946
+ });
6947
+ throw err;
6948
+ }
6949
+ buildSpan?.end();
6946
6950
  const traverseSpan = parentSpan?.createChildSpan({
6947
6951
  type: SpanType.GRAPH_ACTION,
6948
6952
  name: "graph traverse",