@mastra/rag 2.7.3-alpha.1 → 2.7.3-alpha.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
@@ -6208,9 +6208,17 @@ async function executeRerank({ results, query, scorer, options }) {
6208
6208
  ...DEFAULT_WEIGHTS,
6209
6209
  ...options.weights
6210
6210
  };
6211
+ for (const [name, weight] of Object.entries(weights)) if (!Number.isFinite(weight) || weight < 0 || weight > 1) {
6212
+ const err = /* @__PURE__ */ new Error(`Weights must be finite numbers between 0 and 1. Got ${name}: ${weight}`);
6213
+ rerankSpan?.error({
6214
+ error: err,
6215
+ endSpan: true
6216
+ });
6217
+ throw err;
6218
+ }
6211
6219
  const sum = Object.values(weights).reduce((acc, w) => acc.plus(w.toString()), new Big(0));
6212
6220
  if (!sum.eq(1)) {
6213
- const err = /* @__PURE__ */ new Error(`Weights must add up to 1. Got ${sum} from ${weights}`);
6221
+ const err = /* @__PURE__ */ new Error(`Weights must add up to 1. Got ${sum} from ${JSON.stringify(weights)}`);
6214
6222
  rerankSpan?.error({
6215
6223
  error: err,
6216
6224
  endSpan: true
@@ -6296,12 +6304,17 @@ var GraphRAG = class GraphRAG {
6296
6304
  addNode(node) {
6297
6305
  if (!node.embedding) throw new Error("Node must have an embedding");
6298
6306
  if (node.embedding.length !== this.dimension) throw new Error(`Embedding dimension must be ${this.dimension}`);
6299
- this.nodes.set(node.id, node);
6307
+ for (const component of node.embedding) if (!Number.isFinite(component)) throw new Error("Embedding must contain only finite numbers");
6308
+ this.nodes.set(node.id, {
6309
+ ...node,
6310
+ embedding: [...node.embedding]
6311
+ });
6300
6312
  }
6301
6313
  addEdge(edge) {
6302
6314
  if (!this.nodes.has(edge.source) || !this.nodes.has(edge.target)) throw new Error("Both source and target nodes must exist");
6315
+ if (!Number.isFinite(edge.weight)) throw new Error("Edge weight must be a finite number");
6303
6316
  this.edges.push(edge);
6304
- this.edges.push({
6317
+ if (edge.source !== edge.target) this.edges.push({
6305
6318
  source: edge.target,
6306
6319
  target: edge.source,
6307
6320
  weight: edge.weight,
@@ -6309,7 +6322,10 @@ var GraphRAG = class GraphRAG {
6309
6322
  });
6310
6323
  }
6311
6324
  getNodes() {
6312
- return Array.from(this.nodes.values());
6325
+ return Array.from(this.nodes.values(), (node) => ({
6326
+ ...node,
6327
+ ...node.embedding ? { embedding: [...node.embedding] } : {}
6328
+ }));
6313
6329
  }
6314
6330
  getEdges() {
6315
6331
  return this.edges;
@@ -6342,20 +6358,25 @@ var GraphRAG = class GraphRAG {
6342
6358
  /**
6343
6359
  * Rebuild a GraphRAG instance from a snapshot produced by `serialize()`.
6344
6360
  *
6345
- * @throws if the snapshot version is unsupported, a node embedding does not
6346
- * match the snapshot dimension, or an edge references an unknown node.
6361
+ * @throws if the snapshot version is unsupported, a node ID is duplicated,
6362
+ * a node embedding does not match the snapshot dimension or contains nonfinite values,
6363
+ * or an edge references an unknown node.
6347
6364
  */
6348
6365
  static deserialize(snapshot) {
6349
6366
  if (snapshot?.version !== GRAPH_RAG_SNAPSHOT_VERSION) throw new Error(`Unsupported GraphRAG snapshot version: ${snapshot?.version}`);
6350
6367
  const graph = new GraphRAG(snapshot.dimension, snapshot.threshold);
6351
- for (const node of snapshot.nodes ?? []) graph.addNode({
6352
- ...node,
6353
- ...node.embedding ? { embedding: [...node.embedding] } : {},
6354
- ...node.metadata ? { metadata: structuredClone(node.metadata) } : {}
6355
- });
6368
+ for (const node of snapshot.nodes ?? []) {
6369
+ if (graph.nodes.has(node.id)) throw new Error(`Duplicate node id in snapshot: ${node.id}`);
6370
+ graph.addNode({
6371
+ ...node,
6372
+ ...node.embedding ? { embedding: [...node.embedding] } : {},
6373
+ ...node.metadata ? { metadata: structuredClone(node.metadata) } : {}
6374
+ });
6375
+ }
6356
6376
  for (const edge of snapshot.edges ?? []) {
6357
6377
  if (!graph.nodes.has(edge.source)) throw new Error(`Edge references unknown node: ${edge.source}`);
6358
6378
  if (!graph.nodes.has(edge.target)) throw new Error(`Edge references unknown node: ${edge.target}`);
6379
+ if (!Number.isFinite(edge.weight)) throw new Error("Edge weight must be a finite number");
6359
6380
  }
6360
6381
  graph.edges = (snapshot.edges ?? []).map((edge) => ({ ...edge }));
6361
6382
  return graph;
@@ -6397,22 +6418,21 @@ var GraphRAG = class GraphRAG {
6397
6418
  createGraph(chunks, embeddings) {
6398
6419
  if (!chunks?.length || !embeddings?.length) throw new Error("Chunks and embeddings arrays must not be empty");
6399
6420
  if (chunks.length !== embeddings.length) throw new Error("Chunks and embeddings must have the same length");
6421
+ const graph = new GraphRAG(this.dimension, this.threshold);
6400
6422
  chunks.forEach((chunk, index) => {
6401
- const node = {
6423
+ graph.addNode({
6402
6424
  id: index.toString(),
6403
6425
  content: chunk.text,
6404
6426
  embedding: embeddings[index]?.vector,
6405
6427
  metadata: { ...chunk.metadata }
6406
- };
6407
- this.addNode(node);
6408
- this.nodes.set(node.id, node);
6428
+ });
6409
6429
  });
6410
6430
  for (let i = 0; i < chunks.length; i++) {
6411
6431
  const firstEmbedding = embeddings[i]?.vector;
6412
6432
  for (let j = i + 1; j < chunks.length; j++) {
6413
6433
  const secondEmbedding = embeddings[j]?.vector;
6414
6434
  const similarity = this.cosineSimilarity(firstEmbedding, secondEmbedding);
6415
- if (similarity > this.threshold) this.addEdge({
6435
+ if (similarity > this.threshold) graph.addEdge({
6416
6436
  source: i.toString(),
6417
6437
  target: j.toString(),
6418
6438
  weight: similarity,
@@ -6420,6 +6440,8 @@ var GraphRAG = class GraphRAG {
6420
6440
  });
6421
6441
  }
6422
6442
  }
6443
+ this.nodes = graph.nodes;
6444
+ this.edges = graph.edges;
6423
6445
  }
6424
6446
  selectWeightedNeighbor(neighbors) {
6425
6447
  const totalWeight = neighbors.reduce((sum, n) => sum + n.weight, 0);
@@ -6463,9 +6485,10 @@ var GraphRAG = class GraphRAG {
6463
6485
  */
6464
6486
  query({ query, topK = 10, randomWalkSteps = 100, restartProb = .15, filter }) {
6465
6487
  if (!query || query.length !== this.dimension) throw new Error(`Query embedding must have dimension ${this.dimension}`);
6466
- if (topK < 1) throw new Error("TopK must be greater than 0");
6467
- if (randomWalkSteps < 1) throw new Error("Random walk steps must be greater than 0");
6468
- if (restartProb <= 0 || restartProb >= 1) throw new Error("Restart probability must be between 0 and 1");
6488
+ for (const component of query) if (!Number.isFinite(component)) throw new Error("Query embedding must contain only finite numbers");
6489
+ if (!Number.isInteger(topK) || topK < 1) throw new Error("TopK must be a positive integer");
6490
+ if (!Number.isInteger(randomWalkSteps) || randomWalkSteps < 1) throw new Error("Random walk steps must be a positive integer");
6491
+ if (!Number.isFinite(restartProb) || restartProb <= 0 || restartProb >= 1) throw new Error("Restart probability must be between 0 and 1");
6469
6492
  const filterEntries = Object.entries(filter ?? {});
6470
6493
  const matchesFilter = (node) => filterEntries.length === 0 ? true : filterEntries.every(([key, value]) => node.metadata?.[key] === value);
6471
6494
  const nodesToSearch = Array.from(this.nodes.values()).filter(matchesFilter);