@mastra/rag 2.7.3-alpha.2 → 2.8.0-alpha.3

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.
@@ -3,7 +3,7 @@ name: mastra-rag
3
3
  description: Documentation for @mastra/rag. Use when working with @mastra/rag APIs, configuration, or implementation.
4
4
  metadata:
5
5
  package: "@mastra/rag"
6
- version: "2.7.3-alpha.2"
6
+ version: "2.8.0-alpha.3"
7
7
  ---
8
8
 
9
9
  ## When to use
@@ -1,91 +1,91 @@
1
1
  {
2
- "version": "2.7.3-alpha.2",
2
+ "version": "2.8.0-alpha.3",
3
3
  "package": "@mastra/rag",
4
4
  "exports": {
5
5
  "CohereRelevanceScorer": {
6
6
  "types": "dist/rerank/relevance/cohere/index.d.ts",
7
7
  "implementation": "dist/index.js",
8
- "line": 6088
8
+ "line": 6091
9
9
  },
10
10
  "GraphRAG": {
11
11
  "types": "dist/graph-rag/index.d.ts",
12
12
  "implementation": "dist/index.js",
13
- "line": 6293
13
+ "line": 6296
14
14
  },
15
15
  "Language": {
16
16
  "types": "dist/document/types.d.ts",
17
17
  "implementation": "dist/index.js",
18
- "line": 3842
18
+ "line": 3843
19
19
  },
20
20
  "MDocument": {
21
21
  "types": "dist/document/document.d.ts",
22
22
  "implementation": "dist/index.js",
23
- "line": 5821
23
+ "line": 5824
24
24
  },
25
25
  "MastraAgentRelevanceScorer": {
26
26
  "types": "dist/rerank/relevance/mastra-agent/index.d.ts",
27
27
  "implementation": "dist/index.js",
28
- "line": 6123
28
+ "line": 6126
29
29
  },
30
30
  "ZeroEntropyRelevanceScorer": {
31
31
  "types": "dist/rerank/relevance/zeroentropy/index.d.ts",
32
32
  "implementation": "dist/index.js",
33
- "line": 6152
33
+ "line": 6155
34
34
  },
35
35
  "createBedrockKBTool": {
36
36
  "types": "dist/tools/bedrock-knowledge-base.d.ts",
37
37
  "implementation": "dist/index.js",
38
- "line": 7138
38
+ "line": 7142
39
39
  },
40
40
  "createDocumentChunkerTool": {
41
41
  "types": "dist/tools/document-chunker.d.ts",
42
42
  "implementation": "dist/index.js",
43
- "line": 6529
43
+ "line": 6532
44
44
  },
45
45
  "createGraphRAGTool": {
46
46
  "types": "dist/tools/graph-rag.d.ts",
47
47
  "implementation": "dist/index.js",
48
- "line": 6844
48
+ "line": 6848
49
49
  },
50
50
  "createVectorQueryTool": {
51
51
  "types": "dist/tools/vector-query.d.ts",
52
52
  "implementation": "dist/index.js",
53
- "line": 6998
53
+ "line": 7002
54
54
  },
55
55
  "defaultGraphRagDescription": {
56
56
  "types": "dist/utils/default-settings.d.ts",
57
57
  "implementation": "dist/index.js",
58
- "line": 6676
58
+ "line": 6679
59
59
  },
60
60
  "defaultVectorQueryDescription": {
61
61
  "types": "dist/utils/default-settings.d.ts",
62
62
  "implementation": "dist/index.js",
63
- "line": 6675
63
+ "line": 6678
64
64
  },
65
65
  "filterDescription": {
66
66
  "types": "dist/utils/default-settings.d.ts",
67
67
  "implementation": "dist/index.js",
68
- "line": 6695
68
+ "line": 6698
69
69
  },
70
70
  "queryTextDescription": {
71
71
  "types": "dist/utils/default-settings.d.ts",
72
72
  "implementation": "dist/index.js",
73
- "line": 6677
73
+ "line": 6680
74
74
  },
75
75
  "rerank": {
76
76
  "types": "dist/rerank/index.d.ts",
77
77
  "implementation": "dist/index.js",
78
- "line": 6280
78
+ "line": 6283
79
79
  },
80
80
  "rerankWithScorer": {
81
81
  "types": "dist/rerank/index.d.ts",
82
82
  "implementation": "dist/index.js",
83
- "line": 6272
83
+ "line": 6275
84
84
  },
85
85
  "topKDescription": {
86
86
  "types": "dist/utils/default-settings.d.ts",
87
87
  "implementation": "dist/index.js",
88
- "line": 6685
88
+ "line": 6688
89
89
  }
90
90
  },
91
91
  "modules": {}
@@ -200,7 +200,7 @@ When using the `headers` option, the markdown strategy ignores all general optio
200
200
 
201
201
  ### JSON
202
202
 
203
- **maxSize** (`number`): Maximum size of each chunk
203
+ **maxSize** (`number`): Target maximum size of each JSON chunk. Must be at least 21. Long values nested in objects can produce larger chunks.
204
204
 
205
205
  **minSize** (`number`): Accepted for compatibility. Has no effect on JSON chunking: chunks smaller than this are kept, so no data is discarded.
206
206
 
@@ -23,10 +23,12 @@ For straightforward semantic search without relationship traversal, use [standar
23
23
  GraphRAG combines vector similarity with knowledge graph traversal:
24
24
 
25
25
  1. Initial vector search retrieves relevant chunks based on semantic similarity
26
- 2. On the tool's first execution, a knowledge graph is constructed from the retrieved chunks. Later queries reuse this graph
26
+ 2. When retrieval finds chunks, the tool builds a knowledge graph from that request's chunks. The graph isn't reused across requests
27
27
  3. The graph is traversed to find connected information
28
28
  4. Results include both directly relevant chunks and related content
29
29
 
30
+ If retrieval finds no chunks, the tool returns empty context and sources without building a graph.
31
+
30
32
  This process helps surface information that might not be semantically similar to the query but is contextually relevant through connections.
31
33
 
32
34
  ## Creating a graph query tool
@@ -1 +1 @@
1
- {"version":3,"file":"json.d.ts","sourceRoot":"","sources":["../../../src/document/transformers/json.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AACrC,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAEjD,qBAAa,wBAAwB;IACnC,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,WAAW,CAAU;IAC7B,OAAO,CAAC,YAAY,CAAU;IAE9B,YAAY,EAAE,OAAc,EAAE,WAAmB,EAAE,YAAmB,EAAE,EAAE,gBAAgB,EAIzF;IAED,OAAO,CAAC,MAAM,CAAC,QAAQ;IAoCvB;;OAEG;IACI,SAAS,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAiC/D;IAED;;OAEG;IACH,OAAO,CAAC,MAAM,CAAC,aAAa;IAS5B;;OAEG;IACH,OAAO,CAAC,uBAAuB;IAU/B;;;OAGG;IACH,OAAO,CAAC,oBAAoB;IAyC5B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AAE,GAAG,CAAC,CAAC;QAC9B,YAAY,CAAC,EAAE,OAAO,CAAC;QACvB,WAAW,CAAC,EAAE,OAAO,CAAC;KACvB,GAAG,MAAM,EAAE,CAcX;IAED;;OAEG;IACH,eAAe,CAAC,EACd,KAAK,EACL,YAAoB,EACpB,WAAkB,EAClB,SAAS,GACV,EAAE;QACD,KAAK,EAAE,MAAM,EAAE,CAAC;QAChB,YAAY,CAAC,EAAE,OAAO,CAAC;QACvB,WAAW,CAAC,EAAE,OAAO,CAAC;QACtB,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,CAAC;KACnC,GAAG,QAAQ,EAAE,CAkBb;IAED,kBAAkB,CAAC,EACjB,WAAW,EACX,SAAS,EACT,YAAY,GACb,EAAE;QACD,WAAW,CAAC,EAAE,OAAO,CAAC;QACtB,YAAY,CAAC,EAAE,OAAO,CAAC;QACvB,SAAS,EAAE,QAAQ,EAAE,CAAC;KACvB,GAAG,QAAQ,EAAE,CAgBb;CACF"}
@@ -1 +1 @@
1
- {"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../../src/document/validation.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAsH7C,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,aAAa,EAAE,MAAM,EAAE,GAAG,GAAG,IAAI,CAwB9E"}
1
+ {"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../../src/document/validation.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAuH7C,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,aAAa,EAAE,MAAM,EAAE,GAAG,GAAG,IAAI,CAwB9E"}
package/dist/index.cjs CHANGED
@@ -33,6 +33,7 @@ let zeroentropy = require("zeroentropy");
33
33
  zeroentropy = __toESM(zeroentropy, 1);
34
34
  let _mastra_core_tools = require("@mastra/core/tools");
35
35
  let _mastra_core_vector = require("@mastra/core/vector");
36
+ let _mastra_core_utils = require("@mastra/core/utils");
36
37
  let _aws_sdk_client_bedrock_agent_runtime = require("@aws-sdk/client-bedrock-agent-runtime");
37
38
  // src/document/prompts/format.ts
38
39
  function format(str, params) {
@@ -4849,6 +4850,7 @@ var RecursiveJsonTransformer = class RecursiveJsonTransformer {
4849
4850
  ensureAscii;
4850
4851
  convertLists;
4851
4852
  constructor({ maxSize = 2e3, ensureAscii = false, convertLists = true }) {
4853
+ if (!Number.isFinite(maxSize) || maxSize < 21) throw new RangeError("JSON maxSize must be at least 21");
4852
4854
  this.maxSize = maxSize;
4853
4855
  this.ensureAscii = ensureAscii;
4854
4856
  this.convertLists = convertLists;
@@ -5781,6 +5783,7 @@ const validationSchemas = {
5781
5783
  disallowedSpecial: setOrAllSchema
5782
5784
  }).strict(),
5783
5785
  json: baseChunkOptionsSchema.extend({
5786
+ maxSize: zod.z.number().min(21).optional(),
5784
5787
  minSize: zod.z.number().positive().optional(),
5785
5788
  ensureAscii: zod.z.boolean().optional(),
5786
5789
  convertLists: zod.z.boolean().optional()
@@ -6748,7 +6751,8 @@ async function resolveVectorStore(options, context, logger) {
6748
6751
  if (typeof vectorStoreOption === "function") {
6749
6752
  const resolved = await vectorStoreOption({
6750
6753
  requestContext,
6751
- mastra
6754
+ mastra,
6755
+ user: (0, _mastra_core_utils.getRequestUser)(requestContext)
6752
6756
  });
6753
6757
  if (!isValidMastraVector(resolved)) {
6754
6758
  const contextStr = buildContextString(context);
@@ -6852,8 +6856,6 @@ const createGraphRAGTool = (options) => {
6852
6856
  ...defaultGraphOptions,
6853
6857
  ...options.graphOptions || {}
6854
6858
  };
6855
- const graphRag = new GraphRAG(graphOptions.dimension, graphOptions.threshold);
6856
- let isInitialized = false;
6857
6859
  const inputSchema = options.enableFilter ? filterSchema : zod.z.object(baseSchema).passthrough();
6858
6860
  return (0, _mastra_core_tools.createTool)({
6859
6861
  id: toolId,
@@ -6913,38 +6915,40 @@ const createGraphRAGTool = (options) => {
6913
6915
  observabilityContext
6914
6916
  });
6915
6917
  if (logger) logger.debug("vectorQuerySearch returned results", { count: results.length });
6916
- if (!isInitialized) {
6917
- const chunks = results.map((result) => ({
6918
- text: result?.metadata?.text,
6919
- metadata: result.metadata ?? {}
6920
- }));
6921
- const embeddings = results.map((result) => ({ vector: result.vector || [] }));
6922
- if (logger) logger.debug("Initializing graph", {
6923
- chunkCount: chunks.length,
6924
- embeddingCount: embeddings.length
6925
- });
6926
- const buildSpan = parentSpan?.createChildSpan({
6927
- type: _mastra_core_observability.SpanType.GRAPH_ACTION,
6928
- name: "graph build",
6929
- input: { nodeCount: chunks.length },
6930
- attributes: {
6931
- action: "build",
6932
- nodeCount: chunks.length,
6933
- threshold: graphOptions.threshold
6934
- }
6935
- });
6936
- try {
6937
- graphRag.createGraph(chunks, embeddings);
6938
- } catch (err) {
6939
- buildSpan?.error({
6940
- error: err,
6941
- endSpan: true
6942
- });
6943
- throw err;
6918
+ if (results.length === 0) return {
6919
+ relevantContext: [],
6920
+ sources: []
6921
+ };
6922
+ const graphRag = new GraphRAG(graphOptions.dimension, graphOptions.threshold);
6923
+ const chunks = results.map((result) => ({
6924
+ text: result?.metadata?.text,
6925
+ metadata: result.metadata ?? {}
6926
+ }));
6927
+ const embeddings = results.map((result) => ({ vector: result.vector || [] }));
6928
+ if (logger) logger.debug("Initializing graph", {
6929
+ chunkCount: chunks.length,
6930
+ embeddingCount: embeddings.length
6931
+ });
6932
+ const buildSpan = parentSpan?.createChildSpan({
6933
+ type: _mastra_core_observability.SpanType.GRAPH_ACTION,
6934
+ name: "graph build",
6935
+ input: { nodeCount: chunks.length },
6936
+ attributes: {
6937
+ action: "build",
6938
+ nodeCount: chunks.length,
6939
+ threshold: graphOptions.threshold
6944
6940
  }
6945
- buildSpan?.end();
6946
- isInitialized = true;
6947
- } else if (logger) logger.debug("Graph already initialized, skipping graph construction");
6941
+ });
6942
+ try {
6943
+ graphRag.createGraph(chunks, embeddings);
6944
+ } catch (err) {
6945
+ buildSpan?.error({
6946
+ error: err,
6947
+ endSpan: true
6948
+ });
6949
+ throw err;
6950
+ }
6951
+ buildSpan?.end();
6948
6952
  const traverseSpan = parentSpan?.createChildSpan({
6949
6953
  type: _mastra_core_observability.SpanType.GRAPH_ACTION,
6950
6954
  name: "graph traverse",