@mastra/rag 2.0.0-beta.1 → 2.0.0-beta.2

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
@@ -5561,6 +5561,11 @@ var MarkdownHeaderTransformer = class {
5561
5561
  currentContent.push(line);
5562
5562
  continue;
5563
5563
  }
5564
+ const isTableLine = strippedLine.includes("|") && strippedLine.length > 0;
5565
+ if (isTableLine) {
5566
+ currentContent.push(line);
5567
+ continue;
5568
+ }
5564
5569
  let headerMatched = false;
5565
5570
  for (const [sep, name14] of this.headersToSplitOn) {
5566
5571
  if (strippedLine.startsWith(sep) && (strippedLine.length === sep.length || strippedLine[sep.length] === " ")) {
@@ -6797,7 +6802,7 @@ var GraphRAG = class {
6797
6802
  return neighbors[neighbors.length - 1]?.id;
6798
6803
  }
6799
6804
  // Perform random walk with restart
6800
- randomWalkWithRestart(startNodeId, steps, restartProb) {
6805
+ randomWalkWithRestart(startNodeId, steps, restartProb, allowedNodeIds) {
6801
6806
  const visits = /* @__PURE__ */ new Map();
6802
6807
  let currentNodeId = startNodeId;
6803
6808
  for (let step = 0; step < steps; step++) {
@@ -6806,7 +6811,10 @@ var GraphRAG = class {
6806
6811
  currentNodeId = startNodeId;
6807
6812
  continue;
6808
6813
  }
6809
- const neighbors = this.getNeighbors(currentNodeId);
6814
+ let neighbors = this.getNeighbors(currentNodeId);
6815
+ if (allowedNodeIds) {
6816
+ neighbors = neighbors.filter((n) => allowedNodeIds.has(n.id));
6817
+ }
6810
6818
  if (neighbors.length === 0) {
6811
6819
  currentNodeId = startNodeId;
6812
6820
  continue;
@@ -6820,12 +6828,22 @@ var GraphRAG = class {
6820
6828
  }
6821
6829
  return normalizedVisits;
6822
6830
  }
6831
+ /**
6832
+ * Query the graph with a dense embedding and optional metadata filter.
6833
+ *
6834
+ * @param query - The embedding vector to query.
6835
+ * @param topK - Number of top results to return.
6836
+ * @param randomWalkSteps - Steps for random walk reranking.
6837
+ * @param restartProb - Restart probability for random walk.
6838
+ * @param filter - Optional strict metadata filter. All key-value pairs must match exactly.
6839
+ */
6823
6840
  // Retrieve relevant nodes using hybrid approach
6824
6841
  query({
6825
6842
  query,
6826
6843
  topK = 10,
6827
6844
  randomWalkSteps = 100,
6828
- restartProb = 0.15
6845
+ restartProb = 0.15,
6846
+ filter
6829
6847
  }) {
6830
6848
  if (!query || query.length !== this.dimension) {
6831
6849
  throw new Error(`Query embedding must have dimension ${this.dimension}`);
@@ -6839,15 +6857,20 @@ var GraphRAG = class {
6839
6857
  if (restartProb <= 0 || restartProb >= 1) {
6840
6858
  throw new Error("Restart probability must be between 0 and 1");
6841
6859
  }
6842
- const similarities = Array.from(this.nodes.values()).map((node) => ({
6860
+ const filterEntries = Object.entries(filter ?? {});
6861
+ const matchesFilter = (node) => filterEntries.length === 0 ? true : filterEntries.every(([key, value]) => node.metadata?.[key] === value);
6862
+ const nodesToSearch = Array.from(this.nodes.values()).filter(matchesFilter);
6863
+ const similarities = nodesToSearch.map((node) => ({
6843
6864
  node,
6844
6865
  similarity: this.cosineSimilarity(query, node.embedding)
6845
6866
  }));
6846
6867
  similarities.sort((a, b) => b.similarity - a.similarity);
6847
6868
  const topNodes = similarities.slice(0, topK);
6869
+ const useFilter = filterEntries.length > 0;
6870
+ const allowedNodeIds = useFilter ? new Set(nodesToSearch.map((n) => n.id)) : void 0;
6848
6871
  const rerankedNodes = /* @__PURE__ */ new Map();
6849
6872
  for (const { node, similarity } of topNodes) {
6850
- const walkScores = this.randomWalkWithRestart(node.id, randomWalkSteps, restartProb);
6873
+ const walkScores = this.randomWalkWithRestart(node.id, randomWalkSteps, restartProb, allowedNodeIds);
6851
6874
  for (const [nodeId, walkScore] of walkScores) {
6852
6875
  const node2 = this.nodes.get(nodeId);
6853
6876
  const existingScore = rerankedNodes.get(nodeId)?.score || 0;
@@ -7130,9 +7153,9 @@ var createGraphRAGTool = (options) => {
7130
7153
  return typeof filter === "string" ? JSON.parse(filter) : filter;
7131
7154
  } catch (error) {
7132
7155
  if (logger) {
7133
- logger.warn("Failed to parse filter as JSON, using empty filter", { filter, error });
7156
+ logger.error("Invalid filter", { filter, error });
7134
7157
  }
7135
- return {};
7158
+ throw new Error(`Invalid filter format: ${error instanceof Error ? error.message : String(error)}`);
7136
7159
  }
7137
7160
  })();
7138
7161
  }
@@ -7257,9 +7280,9 @@ var createVectorQueryTool = (options) => {
7257
7280
  return typeof filter === "string" ? JSON.parse(filter) : filter;
7258
7281
  } catch (error) {
7259
7282
  if (logger) {
7260
- logger.warn("Failed to parse filter as JSON, using empty filter", { filter, error });
7283
+ logger.error("Invalid filter", { filter, error });
7261
7284
  }
7262
- return {};
7285
+ throw new Error(`Invalid filter format: ${error instanceof Error ? error.message : String(error)}`);
7263
7286
  }
7264
7287
  })();
7265
7288
  }