@mastra/rag 2.7.0 → 2.7.1

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
@@ -1,5 +1,5 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- //#region \0rolldown/runtime.js
2
+ // \0rolldown/runtime.js
3
3
  var __create$1 = Object.create;
4
4
  var __defProp$1 = Object.defineProperty;
5
5
  var __getOwnPropDesc$1 = Object.getOwnPropertyDescriptor;
@@ -21,7 +21,6 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create$1(__
21
21
  value: mod,
22
22
  enumerable: true
23
23
  }) : target, mod));
24
- //#endregion
25
24
  let _mastra_core_observability = require("@mastra/core/observability");
26
25
  let _mastra_core_agent = require("@mastra/core/agent");
27
26
  let node_crypto = require("node:crypto");
@@ -35,12 +34,11 @@ zeroentropy = __toESM(zeroentropy, 1);
35
34
  let _mastra_core_tools = require("@mastra/core/tools");
36
35
  let _mastra_core_vector = require("@mastra/core/vector");
37
36
  let _aws_sdk_client_bedrock_agent_runtime = require("@aws-sdk/client-bedrock-agent-runtime");
38
- //#region src/document/prompts/format.ts
37
+ // src/document/prompts/format.ts
39
38
  function format(str, params) {
40
39
  return str.replace(/{(\w+)}/g, (_, k) => params[k] ?? "");
41
40
  }
42
- //#endregion
43
- //#region src/document/prompts/base.ts
41
+ // src/document/prompts/base.ts
44
42
  var BasePromptTemplate = class {
45
43
  templateVars = /* @__PURE__ */ new Set();
46
44
  options = {};
@@ -86,8 +84,7 @@ var PromptTemplate = class PromptTemplate extends BasePromptTemplate {
86
84
  return this.#template;
87
85
  }
88
86
  };
89
- //#endregion
90
- //#region src/document/prompts/prompt.ts
87
+ // src/document/prompts/prompt.ts
91
88
  const defaultSummaryPrompt = new PromptTemplate({
92
89
  templateVars: ["context"],
93
90
  template: `Write a summary of the following. Try to use only the information provided. Try to include as many key details as possible.
@@ -132,8 +129,7 @@ const defaultTitleCombinePromptTemplate = new PromptTemplate({
132
129
  Based on the above candidate titles and contents, what is the comprehensive title for this document?
133
130
  Title: `
134
131
  });
135
- //#endregion
136
- //#region src/document/schema/types.ts
132
+ // src/document/schema/types.ts
137
133
  var NodeRelationship = /* @__PURE__ */ ((NodeRelationship2) => {
138
134
  NodeRelationship2["SOURCE"] = "SOURCE";
139
135
  NodeRelationship2["PREVIOUS"] = "PREVIOUS";
@@ -150,8 +146,7 @@ var ObjectType = /* @__PURE__ */ ((ObjectType2) => {
150
146
  ObjectType2["IMAGE_DOCUMENT"] = "IMAGE_DOCUMENT";
151
147
  return ObjectType2;
152
148
  })(ObjectType || {});
153
- //#endregion
154
- //#region src/document/schema/node.ts
149
+ // src/document/schema/node.ts
155
150
  var __create = Object.create;
156
151
  var __defProp = Object.defineProperty;
157
152
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
@@ -362,8 +357,7 @@ function createSHA256() {
362
357
  }
363
358
  };
364
359
  }
365
- //#endregion
366
- //#region src/document/extractors/base.ts
360
+ // src/document/extractors/base.ts
367
361
  var BaseExtractor = class {
368
362
  isTextNodeOnly = true;
369
363
  /**
@@ -381,8 +375,7 @@ var BaseExtractor = class {
381
375
  return newNodes;
382
376
  }
383
377
  };
384
- //#endregion
385
- //#region ../../../../../setup-pnpm/node_modules/.bin/store/v11/links/@ai-sdk/provider/1.1.3/7d8eaddb7973e5820d04ac18a887c85cc63f173732e3c66432ceefbff819b4ac/node_modules/@ai-sdk/provider/dist/index.mjs
378
+ // ../../../../../setup-pnpm/node_modules/.bin/store/v11/links/@ai-sdk/provider/1.1.3/7d8eaddb7973e5820d04ac18a887c85cc63f173732e3c66432ceefbff819b4ac/node_modules/@ai-sdk/provider/dist/index.mjs
386
379
  var marker = "vercel.ai.error";
387
380
  var symbol = Symbol.for(marker);
388
381
  var _a;
@@ -633,8 +626,7 @@ var UnsupportedFunctionalityError = class extends AISDKError {
633
626
  }
634
627
  };
635
628
  _a14 = symbol14;
636
- //#endregion
637
- //#region ../../../../../setup-pnpm/node_modules/.bin/store/v11/links/@/nanoid/5.1.16/f16424b532a5e69aacddcb2977ac17378b6f79695bbd27ead3d90c703848f22f/node_modules/nanoid/non-secure/index.js
629
+ // ../../../../../setup-pnpm/node_modules/.bin/store/v11/links/@/nanoid/5.1.16/f16424b532a5e69aacddcb2977ac17378b6f79695bbd27ead3d90c703848f22f/node_modules/nanoid/non-secure/index.js
638
630
  let customAlphabet = (alphabet, defaultSize = 21) => {
639
631
  return (size = defaultSize) => {
640
632
  let id = "";
@@ -643,8 +635,7 @@ let customAlphabet = (alphabet, defaultSize = 21) => {
643
635
  return id;
644
636
  };
645
637
  };
646
- //#endregion
647
- //#region ../../../../../setup-pnpm/node_modules/.bin/store/v11/links/@ai-sdk/provider-utils/2.2.8/b5ebb650c869d075671e49b164b6e4defe8f9652b8a95fc120ecfeb885936b4f/node_modules/@ai-sdk/provider-utils/dist/index.mjs
638
+ // ../../../../../setup-pnpm/node_modules/.bin/store/v11/links/@ai-sdk/provider-utils/2.2.8/b5ebb650c869d075671e49b164b6e4defe8f9652b8a95fc120ecfeb885936b4f/node_modules/@ai-sdk/provider-utils/dist/index.mjs
648
639
  var import_secure_json_parse = /* @__PURE__ */ __toESM((/* @__PURE__ */ __commonJSMin(((exports, module) => {
649
640
  const hasBuffer = typeof Buffer !== "undefined";
650
641
  const suspectProtoRx = /"(?:_|\\u005[Ff])(?:_|\\u005[Ff])(?:p|\\u0070)(?:r|\\u0072)(?:o|\\u006[Ff])(?:t|\\u0074)(?:o|\\u006[Ff])(?:_|\\u005[Ff])(?:_|\\u005[Ff])"\s*:/;
@@ -1185,8 +1176,7 @@ function convertUint8ArrayToBase64(array) {
1185
1176
  function withoutTrailingSlash(url) {
1186
1177
  return url == null ? void 0 : url.replace(/\/$/, "");
1187
1178
  }
1188
- //#endregion
1189
- //#region ../../../../../setup-pnpm/node_modules/.bin/store/v11/links/@ai-sdk/openai/1.3.24/9ea9437756f1191484449f4e611ce2befb4ff9a7be92f5dfa8b1970d80a87966/node_modules/@ai-sdk/openai/dist/index.mjs
1179
+ // ../../../../../setup-pnpm/node_modules/.bin/store/v11/links/@ai-sdk/openai/1.3.24/9ea9437756f1191484449f4e611ce2befb4ff9a7be92f5dfa8b1970d80a87966/node_modules/@ai-sdk/openai/dist/index.mjs
1190
1180
  function convertToOpenAIChatMessages({ prompt, useLegacyFunctionCalling = false, systemMessageMode = "system" }) {
1191
1181
  const messages = [];
1192
1182
  const warnings = [];
@@ -3467,12 +3457,10 @@ function createOpenAI(options = {}) {
3467
3457
  return provider;
3468
3458
  }
3469
3459
  createOpenAI({ compatibility: "strict" });
3470
- //#endregion
3471
- //#region src/document/extractors/types.ts
3460
+ // src/document/extractors/types.ts
3472
3461
  const STRIP_REGEX = /(\r\n|\n|\r)/gm;
3473
3462
  const baseLLM = createOpenAI({ apiKey: process.env.OPENAI_API_KEY })("gpt-4o");
3474
- //#endregion
3475
- //#region src/document/extractors/title.ts
3463
+ // src/document/extractors/title.ts
3476
3464
  var TitleExtractor = class extends BaseExtractor {
3477
3465
  llm;
3478
3466
  isTextNodeOnly = false;
@@ -3592,8 +3580,7 @@ var TitleExtractor = class extends BaseExtractor {
3592
3580
  return await Promise.all(titleJobs);
3593
3581
  }
3594
3582
  };
3595
- //#endregion
3596
- //#region src/document/extractors/summary.ts
3583
+ // src/document/extractors/summary.ts
3597
3584
  var SummaryExtractor = class extends BaseExtractor {
3598
3585
  llm;
3599
3586
  summaries;
@@ -3668,8 +3655,7 @@ var SummaryExtractor = class extends BaseExtractor {
3668
3655
  return metadataList;
3669
3656
  }
3670
3657
  };
3671
- //#endregion
3672
- //#region src/document/extractors/questions.ts
3658
+ // src/document/extractors/questions.ts
3673
3659
  var QuestionsAnsweredExtractor = class extends BaseExtractor {
3674
3660
  llm;
3675
3661
  questions = 5;
@@ -3737,8 +3723,7 @@ var QuestionsAnsweredExtractor = class extends BaseExtractor {
3737
3723
  return await Promise.all(nodes.map((node) => this.extractQuestionsFromNode(node)));
3738
3724
  }
3739
3725
  };
3740
- //#endregion
3741
- //#region src/document/extractors/keywords.ts
3726
+ // src/document/extractors/keywords.ts
3742
3727
  var KeywordExtractor = class extends BaseExtractor {
3743
3728
  llm;
3744
3729
  keywords = 5;
@@ -3820,8 +3805,7 @@ var KeywordExtractor = class extends BaseExtractor {
3820
3805
  return await Promise.all(nodes.map((node) => this.extractKeywordsFromNodes(node)));
3821
3806
  }
3822
3807
  };
3823
- //#endregion
3824
- //#region src/document/extractors/schema.ts
3808
+ // src/document/extractors/schema.ts
3825
3809
  var SchemaExtractor = class extends BaseExtractor {
3826
3810
  schema;
3827
3811
  llm;
@@ -3856,8 +3840,7 @@ var SchemaExtractor = class extends BaseExtractor {
3856
3840
  }));
3857
3841
  }
3858
3842
  };
3859
- //#endregion
3860
- //#region src/document/types.ts
3843
+ // src/document/types.ts
3861
3844
  var Language = /* @__PURE__ */ ((Language2) => {
3862
3845
  Language2["CPP"] = "cpp";
3863
3846
  Language2["GO"] = "go";
@@ -3887,8 +3870,7 @@ var Language = /* @__PURE__ */ ((Language2) => {
3887
3870
  Language2["POWERSHELL"] = "powershell";
3888
3871
  return Language2;
3889
3872
  })(Language || {});
3890
- //#endregion
3891
- //#region src/document/transformers/text.ts
3873
+ // src/document/transformers/text.ts
3892
3874
  var TextTransformer = class {
3893
3875
  maxSize;
3894
3876
  overlap;
@@ -4024,8 +4006,7 @@ var TextTransformer = class {
4024
4006
  return docs;
4025
4007
  }
4026
4008
  };
4027
- //#endregion
4028
- //#region src/document/transformers/character.ts
4009
+ // src/document/transformers/character.ts
4029
4010
  function splitTextWithRegex(text, separator, separatorPosition) {
4030
4011
  if (!separator) return text.split("");
4031
4012
  if (!separatorPosition) return text.split(new RegExp(separator)).filter((s) => s !== "");
@@ -4597,8 +4578,7 @@ var RecursiveCharacterTransformer = class RecursiveCharacterTransformer extends
4597
4578
  }
4598
4579
  }
4599
4580
  };
4600
- //#endregion
4601
- //#region src/document/transformers/html.ts
4581
+ // src/document/transformers/html.ts
4602
4582
  const BLOCK_TAGS = /* @__PURE__ */ new Set([
4603
4583
  "address",
4604
4584
  "article",
@@ -4865,8 +4845,7 @@ var HTMLSectionTransformer = class {
4865
4845
  return this.createDocuments(texts, metadatas);
4866
4846
  }
4867
4847
  };
4868
- //#endregion
4869
- //#region src/document/transformers/json.ts
4848
+ // src/document/transformers/json.ts
4870
4849
  var RecursiveJsonTransformer = class RecursiveJsonTransformer {
4871
4850
  maxSize;
4872
4851
  minSize;
@@ -4954,7 +4933,7 @@ var RecursiveJsonTransformer = class RecursiveJsonTransformer {
4954
4933
  },
4955
4934
  chunks
4956
4935
  };
4957
- else return {
4936
+ return {
4958
4937
  currentChunk: testValue,
4959
4938
  chunks: [...chunks, currentChunk]
4960
4939
  };
@@ -5199,8 +5178,7 @@ var RecursiveJsonTransformer = class RecursiveJsonTransformer {
5199
5178
  });
5200
5179
  }
5201
5180
  };
5202
- //#endregion
5203
- //#region src/document/transformers/latex.ts
5181
+ // src/document/transformers/latex.ts
5204
5182
  var LatexTransformer = class extends RecursiveCharacterTransformer {
5205
5183
  constructor(options = {}) {
5206
5184
  const separators = RecursiveCharacterTransformer.getSeparatorsForLanguage(Language.LATEX);
@@ -5211,8 +5189,7 @@ var LatexTransformer = class extends RecursiveCharacterTransformer {
5211
5189
  });
5212
5190
  }
5213
5191
  };
5214
- //#endregion
5215
- //#region src/document/transformers/markdown.ts
5192
+ // src/document/transformers/markdown.ts
5216
5193
  var MarkdownTransformer = class extends RecursiveCharacterTransformer {
5217
5194
  constructor(options = {}) {
5218
5195
  const separators = RecursiveCharacterTransformer.getSeparatorsForLanguage(Language.MARKDOWN);
@@ -5378,8 +5355,7 @@ var MarkdownHeaderTransformer = class {
5378
5355
  return this.createDocuments(texts, metadatas);
5379
5356
  }
5380
5357
  };
5381
- //#endregion
5382
- //#region src/document/transformers/semantic-markdown.ts
5358
+ // src/document/transformers/semantic-markdown.ts
5383
5359
  var SemanticMarkdownTransformer = class SemanticMarkdownTransformer extends TextTransformer {
5384
5360
  tokenizer;
5385
5361
  joinThreshold;
@@ -5515,8 +5491,7 @@ ${section.content}`;
5515
5491
  });
5516
5492
  }
5517
5493
  };
5518
- //#endregion
5519
- //#region src/document/transformers/sentence.ts
5494
+ // src/document/transformers/sentence.ts
5520
5495
  var SentenceTransformer = class extends TextTransformer {
5521
5496
  minSize;
5522
5497
  maxSize;
@@ -5710,8 +5685,7 @@ var SentenceTransformer = class extends TextTransformer {
5710
5685
  return this.groupSentencesIntoChunks(sentences).filter((chunk) => chunk.trim().length > 0);
5711
5686
  }
5712
5687
  };
5713
- //#endregion
5714
- //#region src/document/transformers/token.ts
5688
+ // src/document/transformers/token.ts
5715
5689
  function splitTextOnTokens({ text, tokenizer }) {
5716
5690
  const splits = [];
5717
5691
  const inputIds = tokenizer.encode(text);
@@ -5787,8 +5761,7 @@ var TokenTransformer = class TokenTransformer extends TextTransformer {
5787
5761
  });
5788
5762
  }
5789
5763
  };
5790
- //#endregion
5791
- //#region src/document/validation.ts
5764
+ // src/document/validation.ts
5792
5765
  const baseChunkOptionsSchema = zod.z.object({
5793
5766
  maxSize: zod.z.number().positive().optional(),
5794
5767
  overlap: zod.z.number().min(0).optional(),
@@ -5867,8 +5840,7 @@ function validateChunkParams(strategy, params) {
5867
5840
  throw new Error(`Invalid parameters for ${strategy} strategy: ${errorMessage}`);
5868
5841
  }
5869
5842
  }
5870
- //#endregion
5871
- //#region src/document/document.ts
5843
+ // src/document/document.ts
5872
5844
  var MDocument = class MDocument {
5873
5845
  chunks;
5874
5846
  type;
@@ -6135,8 +6107,7 @@ var MDocument = class MDocument {
6135
6107
  return this.chunks.map((doc) => doc.metadata);
6136
6108
  }
6137
6109
  };
6138
- //#endregion
6139
- //#region src/rerank/relevance/cohere/index.ts
6110
+ // src/rerank/relevance/cohere/index.ts
6140
6111
  var CohereRelevanceScorer = class {
6141
6112
  model;
6142
6113
  apiKey;
@@ -6165,8 +6136,7 @@ var CohereRelevanceScorer = class {
6165
6136
  return relevanceScore;
6166
6137
  }
6167
6138
  };
6168
- //#endregion
6169
- //#region src/rerank/relevance/mastra-agent/index.ts
6139
+ // src/rerank/relevance/mastra-agent/index.ts
6170
6140
  function parseRelevanceScore(responseText) {
6171
6141
  const trimmed = responseText.trim();
6172
6142
  const score = Number(trimmed);
@@ -6201,8 +6171,7 @@ Always return just the number, no explanation.`,
6201
6171
  return parseRelevanceScore(response.text);
6202
6172
  }
6203
6173
  };
6204
- //#endregion
6205
- //#region src/rerank/relevance/zeroentropy/index.ts
6174
+ // src/rerank/relevance/zeroentropy/index.ts
6206
6175
  var ZeroEntropyRelevanceScorer = class {
6207
6176
  client;
6208
6177
  model;
@@ -6219,8 +6188,7 @@ var ZeroEntropyRelevanceScorer = class {
6219
6188
  })).results[0]?.relevance_score ?? 0;
6220
6189
  }
6221
6190
  };
6222
- //#endregion
6223
- //#region src/rerank/index.ts
6191
+ // src/rerank/index.ts
6224
6192
  const DEFAULT_WEIGHTS = {
6225
6193
  semantic: .4,
6226
6194
  vector: .4,
@@ -6327,8 +6295,7 @@ async function rerank(results, query, model, options) {
6327
6295
  options
6328
6296
  });
6329
6297
  }
6330
- //#endregion
6331
- //#region src/graph-rag/index.ts
6298
+ // src/graph-rag/index.ts
6332
6299
  const GRAPH_RAG_SNAPSHOT_VERSION = 1;
6333
6300
  var GraphRAG = class GraphRAG {
6334
6301
  nodes;
@@ -6544,8 +6511,7 @@ var GraphRAG = class GraphRAG {
6544
6511
  }));
6545
6512
  }
6546
6513
  };
6547
- //#endregion
6548
- //#region src/tools/document-chunker.ts
6514
+ // src/tools/document-chunker.ts
6549
6515
  const DEFAULT_CHUNK_PARAMS = {
6550
6516
  strategy: "recursive",
6551
6517
  maxSize: 512,
@@ -6562,8 +6528,7 @@ const createDocumentChunkerTool = ({ doc, params = DEFAULT_CHUNK_PARAMS }) => {
6562
6528
  }
6563
6529
  });
6564
6530
  };
6565
- //#endregion
6566
- //#region src/utils/vector-search.ts
6531
+ // src/utils/vector-search.ts
6567
6532
  var DatabaseType = /* @__PURE__ */ ((DatabaseType2) => {
6568
6533
  DatabaseType2["Pinecone"] = "pinecone";
6569
6534
  DatabaseType2["PgVector"] = "pgvector";
@@ -6698,8 +6663,7 @@ const databaseSpecificParams = (databaseConfig) => {
6698
6663
  }
6699
6664
  return databaseSpecificParams2;
6700
6665
  };
6701
- //#endregion
6702
- //#region src/utils/default-settings.ts
6666
+ // src/utils/default-settings.ts
6703
6667
  const defaultVectorQueryDescription = () => `Access the knowledge base to find information needed to answer user questions.`;
6704
6668
  const defaultGraphRagDescription = () => `Access and analyze relationships between information in the knowledge base to answer complex questions about connections and patterns.`;
6705
6669
  const queryTextDescription = `The text query to search for in the vector database.
@@ -6732,8 +6696,7 @@ const filterDescription = `JSON-formatted criteria to refine search results.
6732
6696
  - Do NOT use single quotes or unquoted properties
6733
6697
  - IMPORTANT: Always ensure JSON is properly closed with matching brackets
6734
6698
  - Multiple filters can be combined`;
6735
- //#endregion
6736
- //#region src/utils/tool-schemas.ts
6699
+ // src/utils/tool-schemas.ts
6737
6700
  const baseSchema = {
6738
6701
  queryText: zod.z.string().describe(queryTextDescription),
6739
6702
  topK: zod.z.coerce.number().describe(topKDescription)
@@ -6752,8 +6715,7 @@ const filterSchema = zod.z.object({
6752
6715
  ...baseSchema,
6753
6716
  filter: zod.z.coerce.string().describe(filterDescription)
6754
6717
  });
6755
- //#endregion
6756
- //#region src/utils/tool-helpers.ts
6718
+ // src/utils/tool-helpers.ts
6757
6719
  function isValidMastraVector(value) {
6758
6720
  return value !== null && value !== void 0 && typeof value === "object";
6759
6721
  }
@@ -6844,8 +6806,7 @@ function parseFilterValue(filter, logger) {
6844
6806
  }
6845
6807
  return parsedFilter;
6846
6808
  }
6847
- //#endregion
6848
- //#region src/utils/convert-sources.ts
6809
+ // src/utils/convert-sources.ts
6849
6810
  const convertToSources = (results) => {
6850
6811
  return results.map((result) => {
6851
6812
  if ("content" in result) return {
@@ -6871,16 +6832,14 @@ const convertToSources = (results) => {
6871
6832
  };
6872
6833
  });
6873
6834
  };
6874
- //#endregion
6875
- //#region src/tools/types.ts
6835
+ // src/tools/types.ts
6876
6836
  const defaultGraphOptions = {
6877
6837
  dimension: 1536,
6878
6838
  randomWalkSteps: 100,
6879
6839
  restartProb: .15,
6880
6840
  threshold: .7
6881
6841
  };
6882
- //#endregion
6883
- //#region src/tools/graph-rag.ts
6842
+ // src/tools/graph-rag.ts
6884
6843
  const createGraphRAGTool = (options) => {
6885
6844
  const { model, id, description } = options;
6886
6845
  const storeName = options["vectorStoreName"] ? options.vectorStoreName : "DirectVectorStore";
@@ -7034,8 +6993,7 @@ const createGraphRAGTool = (options) => {
7034
6993
  }
7035
6994
  });
7036
6995
  };
7037
- //#endregion
7038
- //#region src/tools/vector-query.ts
6996
+ // src/tools/vector-query.ts
7039
6997
  const createVectorQueryTool = (options) => {
7040
6998
  const { id, description } = options;
7041
6999
  const storeName = options["vectorStoreName"] ? options.vectorStoreName : "DirectVectorStore";
@@ -7154,8 +7112,7 @@ const createVectorQueryTool = (options) => {
7154
7112
  }
7155
7113
  });
7156
7114
  };
7157
- //#endregion
7158
- //#region src/tools/bedrock-knowledge-base.ts
7115
+ // src/tools/bedrock-knowledge-base.ts
7159
7116
  function getSourceUri(location) {
7160
7117
  if (location?.s3Location) return location.s3Location.uri;
7161
7118
  if (location?.webLocation) return location.webLocation.url;
@@ -7244,7 +7201,6 @@ function createBedrockKBTool(options) {
7244
7201
  }
7245
7202
  });
7246
7203
  }
7247
- //#endregion
7248
7204
  exports.CohereRelevanceScorer = CohereRelevanceScorer;
7249
7205
  exports.GraphRAG = GraphRAG;
7250
7206
  exports.Language = Language;