@mastra/lance 1.0.0-beta.10 → 1.0.0-beta.11

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
@@ -1,6 +1,6 @@
1
1
  import { connect, Index } from '@lancedb/lancedb';
2
2
  import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
3
- import { MemoryStorage, TABLE_SCHEMAS, TABLE_THREADS, TABLE_MESSAGES, TABLE_RESOURCES, createStorageErrorId, normalizePerPage, calculatePagination, ScoresStorage, SCORERS_SCHEMA, TABLE_SCORERS, WorkflowsStorage, TABLE_WORKFLOW_SNAPSHOT, MastraStorage, createVectorErrorId, getDefaultValue, ensureDate } from '@mastra/core/storage';
3
+ import { MemoryStorage, TABLE_SCHEMAS, TABLE_THREADS, TABLE_MESSAGES, TABLE_RESOURCES, createStorageErrorId, normalizePerPage, calculatePagination, ScoresStorage, SCORERS_SCHEMA, TABLE_SCORERS, WorkflowsStorage, ensureDate, TABLE_WORKFLOW_SNAPSHOT, MastraStorage, createVectorErrorId, getDefaultValue } from '@mastra/core/storage';
4
4
  import { MessageList } from '@mastra/core/agent';
5
5
  import { MastraBase } from '@mastra/core/base';
6
6
  import { Utf8, Float64, Binary, Float32, Int32, Field, Schema } from 'apache-arrow';
@@ -1603,24 +1603,6 @@ var StoreScoresLance = class extends ScoresStorage {
1603
1603
  function escapeSql(str) {
1604
1604
  return str.replace(/'/g, "''");
1605
1605
  }
1606
- function parseWorkflowRun(row) {
1607
- let parsedSnapshot = row.snapshot;
1608
- if (typeof parsedSnapshot === "string") {
1609
- try {
1610
- parsedSnapshot = JSON.parse(row.snapshot);
1611
- } catch (e) {
1612
- console.warn(`Failed to parse snapshot for workflow ${row.workflow_name}: ${e}`);
1613
- }
1614
- }
1615
- return {
1616
- workflowName: row.workflow_name,
1617
- runId: row.run_id,
1618
- snapshot: parsedSnapshot,
1619
- createdAt: ensureDate(row.createdAt),
1620
- updatedAt: ensureDate(row.updatedAt),
1621
- resourceId: row.resourceId
1622
- };
1623
- }
1624
1606
  var StoreWorkflowsLance = class extends WorkflowsStorage {
1625
1607
  client;
1626
1608
  #db;
@@ -1630,6 +1612,24 @@ var StoreWorkflowsLance = class extends WorkflowsStorage {
1630
1612
  this.client = client;
1631
1613
  this.#db = new LanceDB({ client });
1632
1614
  }
1615
+ parseWorkflowRun(row) {
1616
+ let parsedSnapshot = row.snapshot;
1617
+ if (typeof parsedSnapshot === "string") {
1618
+ try {
1619
+ parsedSnapshot = JSON.parse(row.snapshot);
1620
+ } catch (e) {
1621
+ this.logger.warn(`Failed to parse snapshot for workflow ${row.workflow_name}: ${e}`);
1622
+ }
1623
+ }
1624
+ return {
1625
+ workflowName: row.workflow_name,
1626
+ runId: row.run_id,
1627
+ snapshot: parsedSnapshot,
1628
+ createdAt: ensureDate(row.createdAt),
1629
+ updatedAt: ensureDate(row.updatedAt),
1630
+ resourceId: row.resourceId
1631
+ };
1632
+ }
1633
1633
  async init() {
1634
1634
  const schema = TABLE_SCHEMAS[TABLE_WORKFLOW_SNAPSHOT];
1635
1635
  await this.#db.createTable({ tableName: TABLE_WORKFLOW_SNAPSHOT, schema });
@@ -1761,7 +1761,7 @@ var StoreWorkflowsLance = class extends WorkflowsStorage {
1761
1761
  const records = await query.toArray();
1762
1762
  if (records.length === 0) return null;
1763
1763
  const record = records[0];
1764
- return parseWorkflowRun(record);
1764
+ return this.parseWorkflowRun(record);
1765
1765
  } catch (error) {
1766
1766
  throw new MastraError(
1767
1767
  {
@@ -1838,7 +1838,7 @@ var StoreWorkflowsLance = class extends WorkflowsStorage {
1838
1838
  }
1839
1839
  const records = await query.toArray();
1840
1840
  return {
1841
- runs: records.map((record) => parseWorkflowRun(record)),
1841
+ runs: records.map((record) => this.parseWorkflowRun(record)),
1842
1842
  total: total || records.length
1843
1843
  };
1844
1844
  } catch (error) {
@@ -2328,6 +2328,7 @@ var LanceVectorStore = class _LanceVectorStore extends MastraVector {
2328
2328
  }
2329
2329
  async query({
2330
2330
  tableName,
2331
+ indexName,
2331
2332
  queryVector,
2332
2333
  filter,
2333
2334
  includeVector = false,
@@ -2335,12 +2336,13 @@ var LanceVectorStore = class _LanceVectorStore extends MastraVector {
2335
2336
  columns = [],
2336
2337
  includeAllColumns = false
2337
2338
  }) {
2339
+ const resolvedTableName = tableName ?? indexName;
2338
2340
  try {
2339
2341
  if (!this.lanceClient) {
2340
2342
  throw new Error("LanceDB client not initialized. Use LanceVectorStore.create() to create an instance");
2341
2343
  }
2342
- if (!tableName) {
2343
- throw new Error("tableName is required");
2344
+ if (!resolvedTableName) {
2345
+ throw new Error("tableName or indexName is required");
2344
2346
  }
2345
2347
  if (!queryVector) {
2346
2348
  throw new Error("queryVector is required");
@@ -2351,25 +2353,30 @@ var LanceVectorStore = class _LanceVectorStore extends MastraVector {
2351
2353
  id: createVectorErrorId("LANCE", "QUERY", "INVALID_ARGS"),
2352
2354
  domain: ErrorDomain.STORAGE,
2353
2355
  category: ErrorCategory.USER,
2354
- text: "LanceDB client not initialized. Use LanceVectorStore.create() to create an instance",
2355
- details: { tableName }
2356
+ text: error instanceof Error ? error.message : "Invalid query arguments",
2357
+ details: { tableName: resolvedTableName }
2356
2358
  },
2357
2359
  error
2358
2360
  );
2359
2361
  }
2360
2362
  try {
2361
- const table = await this.lanceClient.openTable(tableName);
2362
- const selectColumns = [...columns];
2363
- if (!selectColumns.includes("id")) {
2364
- selectColumns.push("id");
2363
+ const tables = await this.lanceClient.tableNames();
2364
+ if (!tables.includes(resolvedTableName)) {
2365
+ this.logger.debug(`Table ${resolvedTableName} does not exist. Returning empty results.`);
2366
+ return [];
2365
2367
  }
2368
+ const table = await this.lanceClient.openTable(resolvedTableName);
2366
2369
  let query = table.search(queryVector);
2367
2370
  if (filter && Object.keys(filter).length > 0) {
2368
2371
  const whereClause = this.filterTranslator(filter);
2369
2372
  this.logger.debug(`Where clause generated: ${whereClause}`);
2370
2373
  query = query.where(whereClause);
2371
2374
  }
2372
- if (!includeAllColumns && selectColumns.length > 0) {
2375
+ if (!includeAllColumns && columns.length > 0) {
2376
+ const selectColumns = [...columns];
2377
+ if (!selectColumns.includes("id")) {
2378
+ selectColumns.push("id");
2379
+ }
2373
2380
  query = query.select(selectColumns);
2374
2381
  }
2375
2382
  query = query.limit(topK);
@@ -2399,7 +2406,7 @@ var LanceVectorStore = class _LanceVectorStore extends MastraVector {
2399
2406
  id: createVectorErrorId("LANCE", "QUERY", "FAILED"),
2400
2407
  domain: ErrorDomain.STORAGE,
2401
2408
  category: ErrorCategory.THIRD_PARTY,
2402
- details: { tableName, includeVector, columnsCount: columns?.length, includeAllColumns }
2409
+ details: { tableName: resolvedTableName, includeVector, columnsCount: columns?.length, includeAllColumns }
2403
2410
  },
2404
2411
  error
2405
2412
  );
@@ -2434,13 +2441,14 @@ var LanceVectorStore = class _LanceVectorStore extends MastraVector {
2434
2441
  const translator = new LanceFilterTranslator();
2435
2442
  return translator.translate(prefixedFilter);
2436
2443
  }
2437
- async upsert({ tableName, vectors, metadata = [], ids = [] }) {
2444
+ async upsert({ tableName, indexName, vectors, metadata = [], ids = [] }) {
2445
+ const resolvedTableName = tableName ?? indexName;
2438
2446
  try {
2439
2447
  if (!this.lanceClient) {
2440
2448
  throw new Error("LanceDB client not initialized. Use LanceVectorStore.create() to create an instance");
2441
2449
  }
2442
- if (!tableName) {
2443
- throw new Error("tableName is required");
2450
+ if (!resolvedTableName) {
2451
+ throw new Error("tableName or indexName is required");
2444
2452
  }
2445
2453
  if (!vectors || !Array.isArray(vectors) || vectors.length === 0) {
2446
2454
  throw new Error("vectors array is required and must not be empty");
@@ -2451,18 +2459,21 @@ var LanceVectorStore = class _LanceVectorStore extends MastraVector {
2451
2459
  id: createVectorErrorId("LANCE", "UPSERT", "INVALID_ARGS"),
2452
2460
  domain: ErrorDomain.STORAGE,
2453
2461
  category: ErrorCategory.USER,
2454
- text: "LanceDB client not initialized. Use LanceVectorStore.create() to create an instance",
2455
- details: { tableName }
2462
+ text: error instanceof Error ? error.message : "Invalid upsert arguments",
2463
+ details: { tableName: resolvedTableName }
2456
2464
  },
2457
2465
  error
2458
2466
  );
2459
2467
  }
2460
2468
  try {
2461
2469
  const tables = await this.lanceClient.tableNames();
2462
- if (!tables.includes(tableName)) {
2463
- throw new Error(`Table ${tableName} does not exist`);
2470
+ const tableExists = tables.includes(resolvedTableName);
2471
+ let table = null;
2472
+ if (!tableExists) {
2473
+ this.logger.debug(`Table ${resolvedTableName} does not exist. Creating it with the first upsert data.`);
2474
+ } else {
2475
+ table = await this.lanceClient.openTable(resolvedTableName);
2464
2476
  }
2465
- const table = await this.lanceClient.openTable(tableName);
2466
2477
  const vectorIds = ids.length === vectors.length ? ids : vectors.map((_, i) => ids[i] || crypto.randomUUID());
2467
2478
  const data = vectors.map((vector, i) => {
2468
2479
  const id = String(vectorIds[i]);
@@ -2479,7 +2490,42 @@ var LanceVectorStore = class _LanceVectorStore extends MastraVector {
2479
2490
  }
2480
2491
  return rowData;
2481
2492
  });
2482
- await table.add(data, { mode: "overwrite" });
2493
+ if (table !== null) {
2494
+ const rowCount = await table.countRows();
2495
+ const schema = await table.schema();
2496
+ const existingColumns = new Set(schema.fields.map((f) => f.name));
2497
+ const dataColumns = new Set(Object.keys(data[0] || {}));
2498
+ const extraColumns = [...dataColumns].filter((col) => !existingColumns.has(col));
2499
+ const missingSchemaColumns = [...existingColumns].filter((col) => !dataColumns.has(col));
2500
+ const hasSchemaMismatch = extraColumns.length > 0 || missingSchemaColumns.length > 0;
2501
+ if (rowCount === 0 && extraColumns.length > 0) {
2502
+ this.logger.warn(
2503
+ `Table ${resolvedTableName} is empty and data has extra columns ${extraColumns.join(", ")}. Recreating with new schema.`
2504
+ );
2505
+ await this.lanceClient.dropTable(resolvedTableName);
2506
+ await this.lanceClient.createTable(resolvedTableName, data);
2507
+ } else if (hasSchemaMismatch) {
2508
+ if (extraColumns.length > 0) {
2509
+ this.logger.warn(
2510
+ `Table ${resolvedTableName} has ${rowCount} rows. Columns ${extraColumns.join(", ")} will be dropped from upsert.`
2511
+ );
2512
+ }
2513
+ const schemaFieldNames = schema.fields.map((f) => f.name);
2514
+ const normalizedData = data.map((row) => {
2515
+ const normalized = {};
2516
+ for (const col of schemaFieldNames) {
2517
+ normalized[col] = col in row ? row[col] : null;
2518
+ }
2519
+ return normalized;
2520
+ });
2521
+ await table.mergeInsert("id").whenMatchedUpdateAll().whenNotMatchedInsertAll().execute(normalizedData);
2522
+ } else {
2523
+ await table.mergeInsert("id").whenMatchedUpdateAll().whenNotMatchedInsertAll().execute(data);
2524
+ }
2525
+ } else {
2526
+ this.logger.debug(`Creating table ${resolvedTableName} with initial data`);
2527
+ await this.lanceClient.createTable(resolvedTableName, data);
2528
+ }
2483
2529
  return vectorIds;
2484
2530
  } catch (error) {
2485
2531
  throw new MastraError(
@@ -2487,7 +2533,12 @@ var LanceVectorStore = class _LanceVectorStore extends MastraVector {
2487
2533
  id: createVectorErrorId("LANCE", "UPSERT", "FAILED"),
2488
2534
  domain: ErrorDomain.STORAGE,
2489
2535
  category: ErrorCategory.THIRD_PARTY,
2490
- details: { tableName, vectorCount: vectors.length, metadataCount: metadata.length, idsCount: ids.length }
2536
+ details: {
2537
+ tableName: resolvedTableName,
2538
+ vectorCount: vectors.length,
2539
+ metadataCount: metadata.length,
2540
+ idsCount: ids.length
2541
+ }
2491
2542
  },
2492
2543
  error
2493
2544
  );
@@ -2584,7 +2635,17 @@ var LanceVectorStore = class _LanceVectorStore extends MastraVector {
2584
2635
  }
2585
2636
  }
2586
2637
  /**
2587
- * indexName is actually a column name in a table in lanceDB
2638
+ * Creates a vector index on a table.
2639
+ *
2640
+ * The behavior of `indexName` depends on whether `tableName` is provided:
2641
+ * - With `tableName`: `indexName` is the column to index (advanced use case)
2642
+ * - Without `tableName`: `indexName` becomes the table name, and 'vector' is used as the column (Memory compatibility)
2643
+ *
2644
+ * @param tableName - Optional table name. If not provided, defaults to indexName.
2645
+ * @param indexName - The index/column name, or table name if tableName is not provided.
2646
+ * @param dimension - Vector dimension size.
2647
+ * @param metric - Distance metric: 'cosine', 'euclidean', or 'dotproduct'.
2648
+ * @param indexConfig - Optional index configuration.
2588
2649
  */
2589
2650
  async createIndex({
2590
2651
  tableName,
@@ -2593,13 +2654,12 @@ var LanceVectorStore = class _LanceVectorStore extends MastraVector {
2593
2654
  metric = "cosine",
2594
2655
  indexConfig = {}
2595
2656
  }) {
2657
+ const resolvedTableName = tableName ?? indexName;
2658
+ const columnToIndex = tableName ? indexName : "vector";
2596
2659
  try {
2597
2660
  if (!this.lanceClient) {
2598
2661
  throw new Error("LanceDB client not initialized. Use LanceVectorStore.create() to create an instance");
2599
2662
  }
2600
- if (!tableName) {
2601
- throw new Error("tableName is required");
2602
- }
2603
2663
  if (!indexName) {
2604
2664
  throw new Error("indexName is required");
2605
2665
  }
@@ -2612,19 +2672,33 @@ var LanceVectorStore = class _LanceVectorStore extends MastraVector {
2612
2672
  id: createVectorErrorId("LANCE", "CREATE_INDEX", "INVALID_ARGS"),
2613
2673
  domain: ErrorDomain.STORAGE,
2614
2674
  category: ErrorCategory.USER,
2615
- details: { tableName: tableName || "", indexName, dimension, metric }
2675
+ details: { tableName: resolvedTableName, indexName, dimension, metric }
2616
2676
  },
2617
2677
  err
2618
2678
  );
2619
2679
  }
2620
2680
  try {
2621
2681
  const tables = await this.lanceClient.tableNames();
2622
- if (!tables.includes(tableName)) {
2623
- throw new Error(
2624
- `Table ${tableName} does not exist. Please create the table first by calling createTable() method.`
2682
+ let table;
2683
+ if (!tables.includes(resolvedTableName)) {
2684
+ this.logger.debug(
2685
+ `Table ${resolvedTableName} does not exist. Creating empty table with dimension ${dimension}.`
2625
2686
  );
2687
+ const initVector = new Array(dimension).fill(0);
2688
+ table = await this.lanceClient.createTable(resolvedTableName, [{ id: "__init__", vector: initVector }]);
2689
+ try {
2690
+ await table.delete("id = '__init__'");
2691
+ } catch (deleteError) {
2692
+ this.logger.warn(
2693
+ `Failed to delete initialization row from ${resolvedTableName}. Subsequent queries may include '__init__' row.`,
2694
+ deleteError
2695
+ );
2696
+ }
2697
+ this.logger.debug(`Table ${resolvedTableName} created. Index creation deferred until data is available.`);
2698
+ return;
2699
+ } else {
2700
+ table = await this.lanceClient.openTable(resolvedTableName);
2626
2701
  }
2627
- const table = await this.lanceClient.openTable(tableName);
2628
2702
  let metricType;
2629
2703
  if (metric === "euclidean") {
2630
2704
  metricType = "l2";
@@ -2633,8 +2707,15 @@ var LanceVectorStore = class _LanceVectorStore extends MastraVector {
2633
2707
  } else if (metric === "cosine") {
2634
2708
  metricType = "cosine";
2635
2709
  }
2710
+ const rowCount = await table.countRows();
2711
+ if (rowCount < 256) {
2712
+ this.logger.warn(
2713
+ `Table ${resolvedTableName} has ${rowCount} rows, which is below the 256 row minimum for index creation. Skipping index creation.`
2714
+ );
2715
+ return;
2716
+ }
2636
2717
  if (indexConfig.type === "ivfflat") {
2637
- await table.createIndex(indexName, {
2718
+ await table.createIndex(columnToIndex, {
2638
2719
  config: Index.ivfPq({
2639
2720
  numPartitions: indexConfig.numPartitions || 128,
2640
2721
  numSubVectors: indexConfig.numSubVectors || 16,
@@ -2643,7 +2724,7 @@ var LanceVectorStore = class _LanceVectorStore extends MastraVector {
2643
2724
  });
2644
2725
  } else {
2645
2726
  this.logger.debug("Creating HNSW PQ index with config:", indexConfig);
2646
- await table.createIndex(indexName, {
2727
+ await table.createIndex(columnToIndex, {
2647
2728
  config: Index.hnswPq({
2648
2729
  m: indexConfig?.hnsw?.m || 16,
2649
2730
  efConstruction: indexConfig?.hnsw?.efConstruction || 100,
@@ -2657,7 +2738,7 @@ var LanceVectorStore = class _LanceVectorStore extends MastraVector {
2657
2738
  id: createVectorErrorId("LANCE", "CREATE_INDEX", "FAILED"),
2658
2739
  domain: ErrorDomain.STORAGE,
2659
2740
  category: ErrorCategory.THIRD_PARTY,
2660
- details: { tableName: tableName || "", indexName, dimension }
2741
+ details: { tableName: resolvedTableName, indexName, dimension }
2661
2742
  },
2662
2743
  error
2663
2744
  );
@@ -2955,7 +3036,7 @@ var LanceVectorStore = class _LanceVectorStore extends MastraVector {
2955
3036
  }
2956
3037
  return rowData;
2957
3038
  });
2958
- await table.add(updatedRecords, { mode: "overwrite" });
3039
+ await table.mergeInsert("id").whenMatchedUpdateAll().whenNotMatchedInsertAll().execute(updatedRecords);
2959
3040
  return;
2960
3041
  }
2961
3042
  } catch (err) {