@mastra/libsql 1.8.2-alpha.0 → 1.9.0-alpha.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/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # @mastra/libsql
2
2
 
3
+ ## 1.9.0-alpha.1
4
+
5
+ ### Minor Changes
6
+
7
+ - Use DiskANN vector_top_k() index for faster vector queries when available ([#14913](https://github.com/mastra-ai/mastra/pull/14913))
8
+
9
+ LibSQLVector.query() now automatically uses the existing DiskANN index for approximate nearest neighbor search instead of brute-force full table scans, providing 10-25x query speedups on larger datasets. Falls back to brute-force when no index exists.
10
+
11
+ ### Patch Changes
12
+
13
+ - Updated dependencies [[`16e34ca`](https://github.com/mastra-ai/mastra/commit/16e34caa98b9a114b17a6125e4e3fd87f169d0d0)]:
14
+ - @mastra/core@1.26.0-alpha.9
15
+
3
16
  ## 1.8.2-alpha.0
4
17
 
5
18
  ### Patch Changes
@@ -3,7 +3,7 @@ name: mastra-libsql
3
3
  description: Documentation for @mastra/libsql. Use when working with @mastra/libsql APIs, configuration, or implementation.
4
4
  metadata:
5
5
  package: "@mastra/libsql"
6
- version: "1.8.2-alpha.0"
6
+ version: "1.9.0-alpha.1"
7
7
  ---
8
8
 
9
9
  ## When to use
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.8.2-alpha.0",
2
+ "version": "1.9.0-alpha.1",
3
3
  "package": "@mastra/libsql",
4
4
  "exports": {},
5
5
  "modules": {}
package/dist/index.cjs CHANGED
@@ -511,6 +511,9 @@ var LibSQLVector = class extends vector.MastraVector {
511
511
  turso;
512
512
  maxRetries;
513
513
  initialBackoffMs;
514
+ overFetchMultiplier;
515
+ isMemoryDb;
516
+ vectorIndexes;
514
517
  constructor({
515
518
  url,
516
519
  authToken,
@@ -518,6 +521,7 @@ var LibSQLVector = class extends vector.MastraVector {
518
521
  syncInterval,
519
522
  maxRetries = 5,
520
523
  initialBackoffMs = 100,
524
+ vectorTopKOverFetchMultiplier = 10,
521
525
  id
522
526
  }) {
523
527
  super({ id });
@@ -529,10 +533,27 @@ var LibSQLVector = class extends vector.MastraVector {
529
533
  });
530
534
  this.maxRetries = maxRetries;
531
535
  this.initialBackoffMs = initialBackoffMs;
532
- if (url.includes(`file:`) || url.includes(`:memory:`)) {
536
+ if (!Number.isInteger(vectorTopKOverFetchMultiplier) || vectorTopKOverFetchMultiplier < 1) {
537
+ throw new Error("vectorTopKOverFetchMultiplier must be a positive integer");
538
+ }
539
+ this.overFetchMultiplier = vectorTopKOverFetchMultiplier;
540
+ this.isMemoryDb = url.includes(":memory:");
541
+ if (url.includes(`file:`) || this.isMemoryDb) {
533
542
  this.turso.execute("PRAGMA journal_mode=WAL;").then(() => this.logger.debug("LibSQLStore: PRAGMA journal_mode=WAL set.")).catch((err) => this.logger.warn("LibSQLStore: Failed to set PRAGMA journal_mode=WAL.", err));
534
543
  this.turso.execute("PRAGMA busy_timeout = 5000;").then(() => this.logger.debug("LibSQLStore: PRAGMA busy_timeout=5000 set.")).catch((err) => this.logger.warn("LibSQLStore: Failed to set PRAGMA busy_timeout=5000.", err));
535
544
  }
545
+ this.vectorIndexes = this.isMemoryDb ? Promise.resolve(/* @__PURE__ */ new Set()) : this.discoverVectorIndexes();
546
+ }
547
+ async discoverVectorIndexes() {
548
+ try {
549
+ const result = await this.turso.execute({
550
+ sql: `SELECT name FROM sqlite_master WHERE type='index' AND name LIKE '%_vector_idx'`,
551
+ args: []
552
+ });
553
+ return new Set(result.rows.map((row) => row.name));
554
+ } catch {
555
+ return /* @__PURE__ */ new Set();
556
+ }
536
557
  }
537
558
  async executeWriteOperationWithRetry(operation, isTransaction = false) {
538
559
  let attempts = 0;
@@ -566,6 +587,40 @@ var LibSQLVector = class extends vector.MastraVector {
566
587
  const translator = new LibSQLFilterTranslator();
567
588
  return translator.translate(filter);
568
589
  }
590
+ async hasVectorIndex(parsedIndexName) {
591
+ const indexes = await this.vectorIndexes;
592
+ return indexes.has(`${parsedIndexName}_vector_idx`);
593
+ }
594
+ async queryWithIndex(parsedIndexName, vectorStr, topK, filter, includeVector, minScore) {
595
+ const translatedFilter = this.transformFilter(filter);
596
+ const { sql: filterQuery, values: filterValues } = buildFilterQuery(translatedFilter);
597
+ const hasFilter = filterQuery.length > 0;
598
+ const fetchCount = hasFilter ? topK * this.overFetchMultiplier : topK * 2;
599
+ const embeddingSelect = includeVector ? ", vector_extract(t.embedding) as embedding" : "";
600
+ const filterCondition = hasFilter ? filterQuery.replace(/^\s*WHERE\s+/i, "") : "";
601
+ const whereClause = hasFilter ? `WHERE ${filterCondition} AND score > ?` : "WHERE score > ?";
602
+ const query = `
603
+ WITH candidates AS (
604
+ SELECT t.vector_id AS id,
605
+ (1 - vector_distance_cos(t.embedding, vector32(?))) AS score,
606
+ t.metadata
607
+ ${embeddingSelect}
608
+ FROM vector_top_k('${parsedIndexName}_vector_idx', vector32(?), ?) AS v
609
+ JOIN "${parsedIndexName}" AS t ON t.rowid = v.id
610
+ )
611
+ SELECT * FROM candidates
612
+ ${whereClause}
613
+ ORDER BY score DESC
614
+ LIMIT ?`;
615
+ const args = [vectorStr, vectorStr, fetchCount, ...filterValues, minScore, topK];
616
+ const result = await this.turso.execute({ sql: query, args });
617
+ return result.rows.map(({ id, score, metadata, embedding }) => ({
618
+ id,
619
+ score,
620
+ metadata: JSON.parse(metadata ?? "{}"),
621
+ ...includeVector && embedding && { vector: JSON.parse(embedding) }
622
+ }));
623
+ }
569
624
  async query({
570
625
  indexName,
571
626
  queryVector,
@@ -596,6 +651,23 @@ var LibSQLVector = class extends vector.MastraVector {
596
651
  try {
597
652
  const parsedIndexName = utils.parseSqlIdentifier(indexName, "index name");
598
653
  const vectorStr = `[${queryVector.join(",")}]`;
654
+ if (!this.isMemoryDb && await this.hasVectorIndex(parsedIndexName)) {
655
+ try {
656
+ const indexedResults = await this.queryWithIndex(
657
+ parsedIndexName,
658
+ vectorStr,
659
+ topK,
660
+ filter,
661
+ includeVector,
662
+ minScore
663
+ );
664
+ if (!filter || indexedResults.length >= topK) {
665
+ return indexedResults;
666
+ }
667
+ } catch (err) {
668
+ this.logger.warn("LibSQLVector: indexed query failed, falling back to brute-force", err);
669
+ }
670
+ }
599
671
  const translatedFilter = this.transformFilter(filter);
600
672
  const { sql: filterQuery, values: filterValues } = buildFilterQuery(translatedFilter);
601
673
  filterValues.push(minScore);
@@ -729,6 +801,7 @@ var LibSQLVector = class extends vector.MastraVector {
729
801
  `,
730
802
  args: []
731
803
  });
804
+ void this.vectorIndexes.then((indexes) => indexes.add(`${parsedIndexName}_vector_idx`));
732
805
  }
733
806
  deleteIndex(args) {
734
807
  try {
@@ -751,6 +824,7 @@ var LibSQLVector = class extends vector.MastraVector {
751
824
  sql: `DROP TABLE IF EXISTS ${parsedIndexName}`,
752
825
  args: []
753
826
  });
827
+ void this.vectorIndexes.then((indexes) => indexes.delete(`${parsedIndexName}_vector_idx`));
754
828
  }
755
829
  async listIndexes() {
756
830
  try {