@mastra/lance 1.0.0-beta.9 → 1.0.0
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 +906 -0
- package/dist/docs/README.md +33 -0
- package/dist/docs/SKILL.md +34 -0
- package/dist/docs/SOURCE_MAP.json +6 -0
- package/dist/docs/rag/01-vector-databases.md +643 -0
- package/dist/docs/storage/01-reference.md +113 -0
- package/dist/docs/vectors/01-reference.md +149 -0
- package/dist/index.cjs +193 -74
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +194 -75
- package/dist/index.js.map +1 -1
- package/dist/storage/domains/memory/index.d.ts +2 -2
- package/dist/storage/domains/memory/index.d.ts.map +1 -1
- package/dist/storage/domains/workflows/index.d.ts +1 -0
- package/dist/storage/domains/workflows/index.d.ts.map +1 -1
- package/dist/storage/index.d.ts +2 -2
- package/dist/storage/index.d.ts.map +1 -1
- package/dist/vector/index.d.ts +15 -5
- package/dist/vector/index.d.ts.map +1 -1
- package/package.json +9 -8
package/dist/index.cjs
CHANGED
|
@@ -799,11 +799,13 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
|
|
|
799
799
|
}
|
|
800
800
|
if (filter?.dateRange?.start) {
|
|
801
801
|
const startTime = filter.dateRange.start instanceof Date ? filter.dateRange.start.getTime() : new Date(filter.dateRange.start).getTime();
|
|
802
|
-
|
|
802
|
+
const startOp = filter.dateRange.startExclusive ? ">" : ">=";
|
|
803
|
+
conditions.push(`\`createdAt\` ${startOp} ${startTime}`);
|
|
803
804
|
}
|
|
804
805
|
if (filter?.dateRange?.end) {
|
|
805
806
|
const endTime = filter.dateRange.end instanceof Date ? filter.dateRange.end.getTime() : new Date(filter.dateRange.end).getTime();
|
|
806
|
-
|
|
807
|
+
const endOp = filter.dateRange.endExclusive ? "<" : "<=";
|
|
808
|
+
conditions.push(`\`createdAt\` ${endOp} ${endTime}`);
|
|
807
809
|
}
|
|
808
810
|
const whereClause = conditions.join(" AND ");
|
|
809
811
|
const total = await table.countRows(whereClause);
|
|
@@ -950,27 +952,63 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
|
|
|
950
952
|
);
|
|
951
953
|
}
|
|
952
954
|
}
|
|
953
|
-
async
|
|
955
|
+
async listThreads(args) {
|
|
956
|
+
const { page = 0, perPage: perPageInput, orderBy, filter } = args;
|
|
957
|
+
try {
|
|
958
|
+
this.validatePaginationInput(page, perPageInput ?? 100);
|
|
959
|
+
} catch (error$1) {
|
|
960
|
+
throw new error.MastraError(
|
|
961
|
+
{
|
|
962
|
+
id: storage.createStorageErrorId("LANCE", "LIST_THREADS", "INVALID_PAGE"),
|
|
963
|
+
domain: error.ErrorDomain.STORAGE,
|
|
964
|
+
category: error.ErrorCategory.USER,
|
|
965
|
+
details: { page, ...perPageInput !== void 0 && { perPage: perPageInput } }
|
|
966
|
+
},
|
|
967
|
+
error$1 instanceof Error ? error$1 : new Error("Invalid pagination parameters")
|
|
968
|
+
);
|
|
969
|
+
}
|
|
970
|
+
const perPage = storage.normalizePerPage(perPageInput, 100);
|
|
971
|
+
try {
|
|
972
|
+
this.validateMetadataKeys(filter?.metadata);
|
|
973
|
+
} catch (error$1) {
|
|
974
|
+
throw new error.MastraError(
|
|
975
|
+
{
|
|
976
|
+
id: storage.createStorageErrorId("LANCE", "LIST_THREADS", "INVALID_METADATA_KEY"),
|
|
977
|
+
domain: error.ErrorDomain.STORAGE,
|
|
978
|
+
category: error.ErrorCategory.USER,
|
|
979
|
+
details: { metadataKeys: filter?.metadata ? Object.keys(filter.metadata).join(", ") : "" }
|
|
980
|
+
},
|
|
981
|
+
error$1 instanceof Error ? error$1 : new Error("Invalid metadata key")
|
|
982
|
+
);
|
|
983
|
+
}
|
|
954
984
|
try {
|
|
955
|
-
const { resourceId, page = 0, perPage: perPageInput, orderBy } = args;
|
|
956
|
-
const perPage = storage.normalizePerPage(perPageInput, 100);
|
|
957
|
-
if (page < 0) {
|
|
958
|
-
throw new error.MastraError(
|
|
959
|
-
{
|
|
960
|
-
id: storage.createStorageErrorId("LANCE", "LIST_THREADS_BY_RESOURCE_ID", "INVALID_PAGE"),
|
|
961
|
-
domain: error.ErrorDomain.STORAGE,
|
|
962
|
-
category: error.ErrorCategory.USER,
|
|
963
|
-
details: { page }
|
|
964
|
-
},
|
|
965
|
-
new Error("page must be >= 0")
|
|
966
|
-
);
|
|
967
|
-
}
|
|
968
985
|
const { offset, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
969
986
|
const { field, direction } = this.parseOrderBy(orderBy);
|
|
970
987
|
const table = await this.client.openTable(storage.TABLE_THREADS);
|
|
971
|
-
const
|
|
972
|
-
|
|
973
|
-
|
|
988
|
+
const whereClauses = [];
|
|
989
|
+
if (filter?.resourceId) {
|
|
990
|
+
whereClauses.push(`\`resourceId\` = '${this.escapeSql(filter.resourceId)}'`);
|
|
991
|
+
}
|
|
992
|
+
const whereClause = whereClauses.length > 0 ? whereClauses.join(" AND ") : "";
|
|
993
|
+
const query = whereClause ? table.query().where(whereClause) : table.query();
|
|
994
|
+
let records = await query.toArray();
|
|
995
|
+
if (filter?.metadata && Object.keys(filter.metadata).length > 0) {
|
|
996
|
+
records = records.filter((record) => {
|
|
997
|
+
if (!record.metadata) return false;
|
|
998
|
+
let recordMeta;
|
|
999
|
+
if (typeof record.metadata === "string") {
|
|
1000
|
+
try {
|
|
1001
|
+
recordMeta = JSON.parse(record.metadata);
|
|
1002
|
+
} catch {
|
|
1003
|
+
return false;
|
|
1004
|
+
}
|
|
1005
|
+
} else {
|
|
1006
|
+
recordMeta = record.metadata;
|
|
1007
|
+
}
|
|
1008
|
+
return Object.entries(filter.metadata).every(([key, value]) => recordMeta[key] === value);
|
|
1009
|
+
});
|
|
1010
|
+
}
|
|
1011
|
+
const total = records.length;
|
|
974
1012
|
records.sort((a, b) => {
|
|
975
1013
|
const aValue = ["createdAt", "updatedAt"].includes(field) ? new Date(a[field]).getTime() : a[field];
|
|
976
1014
|
const bValue = ["createdAt", "updatedAt"].includes(field) ? new Date(b[field]).getTime() : b[field];
|
|
@@ -997,7 +1035,7 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
|
|
|
997
1035
|
} catch (error$1) {
|
|
998
1036
|
throw new error.MastraError(
|
|
999
1037
|
{
|
|
1000
|
-
id: storage.createStorageErrorId("LANCE", "
|
|
1038
|
+
id: storage.createStorageErrorId("LANCE", "LIST_THREADS", "FAILED"),
|
|
1001
1039
|
domain: error.ErrorDomain.STORAGE,
|
|
1002
1040
|
category: error.ErrorCategory.THIRD_PARTY
|
|
1003
1041
|
},
|
|
@@ -1603,24 +1641,6 @@ var StoreScoresLance = class extends storage.ScoresStorage {
|
|
|
1603
1641
|
function escapeSql(str) {
|
|
1604
1642
|
return str.replace(/'/g, "''");
|
|
1605
1643
|
}
|
|
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: storage.ensureDate(row.createdAt),
|
|
1620
|
-
updatedAt: storage.ensureDate(row.updatedAt),
|
|
1621
|
-
resourceId: row.resourceId
|
|
1622
|
-
};
|
|
1623
|
-
}
|
|
1624
1644
|
var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
|
|
1625
1645
|
client;
|
|
1626
1646
|
#db;
|
|
@@ -1630,6 +1650,24 @@ var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
|
|
|
1630
1650
|
this.client = client;
|
|
1631
1651
|
this.#db = new LanceDB({ client });
|
|
1632
1652
|
}
|
|
1653
|
+
parseWorkflowRun(row) {
|
|
1654
|
+
let parsedSnapshot = row.snapshot;
|
|
1655
|
+
if (typeof parsedSnapshot === "string") {
|
|
1656
|
+
try {
|
|
1657
|
+
parsedSnapshot = JSON.parse(row.snapshot);
|
|
1658
|
+
} catch (e) {
|
|
1659
|
+
this.logger.warn(`Failed to parse snapshot for workflow ${row.workflow_name}: ${e}`);
|
|
1660
|
+
}
|
|
1661
|
+
}
|
|
1662
|
+
return {
|
|
1663
|
+
workflowName: row.workflow_name,
|
|
1664
|
+
runId: row.run_id,
|
|
1665
|
+
snapshot: parsedSnapshot,
|
|
1666
|
+
createdAt: storage.ensureDate(row.createdAt),
|
|
1667
|
+
updatedAt: storage.ensureDate(row.updatedAt),
|
|
1668
|
+
resourceId: row.resourceId
|
|
1669
|
+
};
|
|
1670
|
+
}
|
|
1633
1671
|
async init() {
|
|
1634
1672
|
const schema = storage.TABLE_SCHEMAS[storage.TABLE_WORKFLOW_SNAPSHOT];
|
|
1635
1673
|
await this.#db.createTable({ tableName: storage.TABLE_WORKFLOW_SNAPSHOT, schema });
|
|
@@ -1761,7 +1799,7 @@ var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
|
|
|
1761
1799
|
const records = await query.toArray();
|
|
1762
1800
|
if (records.length === 0) return null;
|
|
1763
1801
|
const record = records[0];
|
|
1764
|
-
return parseWorkflowRun(record);
|
|
1802
|
+
return this.parseWorkflowRun(record);
|
|
1765
1803
|
} catch (error$1) {
|
|
1766
1804
|
throw new error.MastraError(
|
|
1767
1805
|
{
|
|
@@ -1838,7 +1876,7 @@ var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
|
|
|
1838
1876
|
}
|
|
1839
1877
|
const records = await query.toArray();
|
|
1840
1878
|
return {
|
|
1841
|
-
runs: records.map((record) => parseWorkflowRun(record)),
|
|
1879
|
+
runs: records.map((record) => this.parseWorkflowRun(record)),
|
|
1842
1880
|
total: total || records.length
|
|
1843
1881
|
};
|
|
1844
1882
|
} catch (error$1) {
|
|
@@ -1856,7 +1894,7 @@ var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
|
|
|
1856
1894
|
};
|
|
1857
1895
|
|
|
1858
1896
|
// src/storage/index.ts
|
|
1859
|
-
var LanceStorage = class _LanceStorage extends storage.
|
|
1897
|
+
var LanceStorage = class _LanceStorage extends storage.MastraCompositeStore {
|
|
1860
1898
|
stores;
|
|
1861
1899
|
lanceClient;
|
|
1862
1900
|
/**
|
|
@@ -2328,6 +2366,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2328
2366
|
}
|
|
2329
2367
|
async query({
|
|
2330
2368
|
tableName,
|
|
2369
|
+
indexName,
|
|
2331
2370
|
queryVector,
|
|
2332
2371
|
filter,
|
|
2333
2372
|
includeVector = false,
|
|
@@ -2335,12 +2374,13 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2335
2374
|
columns = [],
|
|
2336
2375
|
includeAllColumns = false
|
|
2337
2376
|
}) {
|
|
2377
|
+
const resolvedTableName = tableName ?? indexName;
|
|
2338
2378
|
try {
|
|
2339
2379
|
if (!this.lanceClient) {
|
|
2340
2380
|
throw new Error("LanceDB client not initialized. Use LanceVectorStore.create() to create an instance");
|
|
2341
2381
|
}
|
|
2342
|
-
if (!
|
|
2343
|
-
throw new Error("tableName is required");
|
|
2382
|
+
if (!resolvedTableName) {
|
|
2383
|
+
throw new Error("tableName or indexName is required");
|
|
2344
2384
|
}
|
|
2345
2385
|
if (!queryVector) {
|
|
2346
2386
|
throw new Error("queryVector is required");
|
|
@@ -2351,25 +2391,30 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2351
2391
|
id: storage.createVectorErrorId("LANCE", "QUERY", "INVALID_ARGS"),
|
|
2352
2392
|
domain: error.ErrorDomain.STORAGE,
|
|
2353
2393
|
category: error.ErrorCategory.USER,
|
|
2354
|
-
text:
|
|
2355
|
-
details: { tableName }
|
|
2394
|
+
text: error$1 instanceof Error ? error$1.message : "Invalid query arguments",
|
|
2395
|
+
details: { tableName: resolvedTableName }
|
|
2356
2396
|
},
|
|
2357
2397
|
error$1
|
|
2358
2398
|
);
|
|
2359
2399
|
}
|
|
2360
2400
|
try {
|
|
2361
|
-
const
|
|
2362
|
-
|
|
2363
|
-
|
|
2364
|
-
|
|
2401
|
+
const tables = await this.lanceClient.tableNames();
|
|
2402
|
+
if (!tables.includes(resolvedTableName)) {
|
|
2403
|
+
this.logger.debug(`Table ${resolvedTableName} does not exist. Returning empty results.`);
|
|
2404
|
+
return [];
|
|
2365
2405
|
}
|
|
2406
|
+
const table = await this.lanceClient.openTable(resolvedTableName);
|
|
2366
2407
|
let query = table.search(queryVector);
|
|
2367
2408
|
if (filter && Object.keys(filter).length > 0) {
|
|
2368
2409
|
const whereClause = this.filterTranslator(filter);
|
|
2369
2410
|
this.logger.debug(`Where clause generated: ${whereClause}`);
|
|
2370
2411
|
query = query.where(whereClause);
|
|
2371
2412
|
}
|
|
2372
|
-
if (!includeAllColumns &&
|
|
2413
|
+
if (!includeAllColumns && columns.length > 0) {
|
|
2414
|
+
const selectColumns = [...columns];
|
|
2415
|
+
if (!selectColumns.includes("id")) {
|
|
2416
|
+
selectColumns.push("id");
|
|
2417
|
+
}
|
|
2373
2418
|
query = query.select(selectColumns);
|
|
2374
2419
|
}
|
|
2375
2420
|
query = query.limit(topK);
|
|
@@ -2399,7 +2444,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2399
2444
|
id: storage.createVectorErrorId("LANCE", "QUERY", "FAILED"),
|
|
2400
2445
|
domain: error.ErrorDomain.STORAGE,
|
|
2401
2446
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
2402
|
-
details: { tableName, includeVector, columnsCount: columns?.length, includeAllColumns }
|
|
2447
|
+
details: { tableName: resolvedTableName, includeVector, columnsCount: columns?.length, includeAllColumns }
|
|
2403
2448
|
},
|
|
2404
2449
|
error$1
|
|
2405
2450
|
);
|
|
@@ -2434,13 +2479,14 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2434
2479
|
const translator = new LanceFilterTranslator();
|
|
2435
2480
|
return translator.translate(prefixedFilter);
|
|
2436
2481
|
}
|
|
2437
|
-
async upsert({ tableName, vectors, metadata = [], ids = [] }) {
|
|
2482
|
+
async upsert({ tableName, indexName, vectors, metadata = [], ids = [] }) {
|
|
2483
|
+
const resolvedTableName = tableName ?? indexName;
|
|
2438
2484
|
try {
|
|
2439
2485
|
if (!this.lanceClient) {
|
|
2440
2486
|
throw new Error("LanceDB client not initialized. Use LanceVectorStore.create() to create an instance");
|
|
2441
2487
|
}
|
|
2442
|
-
if (!
|
|
2443
|
-
throw new Error("tableName is required");
|
|
2488
|
+
if (!resolvedTableName) {
|
|
2489
|
+
throw new Error("tableName or indexName is required");
|
|
2444
2490
|
}
|
|
2445
2491
|
if (!vectors || !Array.isArray(vectors) || vectors.length === 0) {
|
|
2446
2492
|
throw new Error("vectors array is required and must not be empty");
|
|
@@ -2451,18 +2497,21 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2451
2497
|
id: storage.createVectorErrorId("LANCE", "UPSERT", "INVALID_ARGS"),
|
|
2452
2498
|
domain: error.ErrorDomain.STORAGE,
|
|
2453
2499
|
category: error.ErrorCategory.USER,
|
|
2454
|
-
text:
|
|
2455
|
-
details: { tableName }
|
|
2500
|
+
text: error$1 instanceof Error ? error$1.message : "Invalid upsert arguments",
|
|
2501
|
+
details: { tableName: resolvedTableName }
|
|
2456
2502
|
},
|
|
2457
2503
|
error$1
|
|
2458
2504
|
);
|
|
2459
2505
|
}
|
|
2460
2506
|
try {
|
|
2461
2507
|
const tables = await this.lanceClient.tableNames();
|
|
2462
|
-
|
|
2463
|
-
|
|
2508
|
+
const tableExists = tables.includes(resolvedTableName);
|
|
2509
|
+
let table = null;
|
|
2510
|
+
if (!tableExists) {
|
|
2511
|
+
this.logger.debug(`Table ${resolvedTableName} does not exist. Creating it with the first upsert data.`);
|
|
2512
|
+
} else {
|
|
2513
|
+
table = await this.lanceClient.openTable(resolvedTableName);
|
|
2464
2514
|
}
|
|
2465
|
-
const table = await this.lanceClient.openTable(tableName);
|
|
2466
2515
|
const vectorIds = ids.length === vectors.length ? ids : vectors.map((_, i) => ids[i] || crypto.randomUUID());
|
|
2467
2516
|
const data = vectors.map((vector, i) => {
|
|
2468
2517
|
const id = String(vectorIds[i]);
|
|
@@ -2479,7 +2528,42 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2479
2528
|
}
|
|
2480
2529
|
return rowData;
|
|
2481
2530
|
});
|
|
2482
|
-
|
|
2531
|
+
if (table !== null) {
|
|
2532
|
+
const rowCount = await table.countRows();
|
|
2533
|
+
const schema = await table.schema();
|
|
2534
|
+
const existingColumns = new Set(schema.fields.map((f) => f.name));
|
|
2535
|
+
const dataColumns = new Set(Object.keys(data[0] || {}));
|
|
2536
|
+
const extraColumns = [...dataColumns].filter((col) => !existingColumns.has(col));
|
|
2537
|
+
const missingSchemaColumns = [...existingColumns].filter((col) => !dataColumns.has(col));
|
|
2538
|
+
const hasSchemaMismatch = extraColumns.length > 0 || missingSchemaColumns.length > 0;
|
|
2539
|
+
if (rowCount === 0 && extraColumns.length > 0) {
|
|
2540
|
+
this.logger.warn(
|
|
2541
|
+
`Table ${resolvedTableName} is empty and data has extra columns ${extraColumns.join(", ")}. Recreating with new schema.`
|
|
2542
|
+
);
|
|
2543
|
+
await this.lanceClient.dropTable(resolvedTableName);
|
|
2544
|
+
await this.lanceClient.createTable(resolvedTableName, data);
|
|
2545
|
+
} else if (hasSchemaMismatch) {
|
|
2546
|
+
if (extraColumns.length > 0) {
|
|
2547
|
+
this.logger.warn(
|
|
2548
|
+
`Table ${resolvedTableName} has ${rowCount} rows. Columns ${extraColumns.join(", ")} will be dropped from upsert.`
|
|
2549
|
+
);
|
|
2550
|
+
}
|
|
2551
|
+
const schemaFieldNames = schema.fields.map((f) => f.name);
|
|
2552
|
+
const normalizedData = data.map((row) => {
|
|
2553
|
+
const normalized = {};
|
|
2554
|
+
for (const col of schemaFieldNames) {
|
|
2555
|
+
normalized[col] = col in row ? row[col] : null;
|
|
2556
|
+
}
|
|
2557
|
+
return normalized;
|
|
2558
|
+
});
|
|
2559
|
+
await table.mergeInsert("id").whenMatchedUpdateAll().whenNotMatchedInsertAll().execute(normalizedData);
|
|
2560
|
+
} else {
|
|
2561
|
+
await table.mergeInsert("id").whenMatchedUpdateAll().whenNotMatchedInsertAll().execute(data);
|
|
2562
|
+
}
|
|
2563
|
+
} else {
|
|
2564
|
+
this.logger.debug(`Creating table ${resolvedTableName} with initial data`);
|
|
2565
|
+
await this.lanceClient.createTable(resolvedTableName, data);
|
|
2566
|
+
}
|
|
2483
2567
|
return vectorIds;
|
|
2484
2568
|
} catch (error$1) {
|
|
2485
2569
|
throw new error.MastraError(
|
|
@@ -2487,7 +2571,12 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2487
2571
|
id: storage.createVectorErrorId("LANCE", "UPSERT", "FAILED"),
|
|
2488
2572
|
domain: error.ErrorDomain.STORAGE,
|
|
2489
2573
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
2490
|
-
details: {
|
|
2574
|
+
details: {
|
|
2575
|
+
tableName: resolvedTableName,
|
|
2576
|
+
vectorCount: vectors.length,
|
|
2577
|
+
metadataCount: metadata.length,
|
|
2578
|
+
idsCount: ids.length
|
|
2579
|
+
}
|
|
2491
2580
|
},
|
|
2492
2581
|
error$1
|
|
2493
2582
|
);
|
|
@@ -2584,7 +2673,17 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2584
2673
|
}
|
|
2585
2674
|
}
|
|
2586
2675
|
/**
|
|
2587
|
-
*
|
|
2676
|
+
* Creates a vector index on a table.
|
|
2677
|
+
*
|
|
2678
|
+
* The behavior of `indexName` depends on whether `tableName` is provided:
|
|
2679
|
+
* - With `tableName`: `indexName` is the column to index (advanced use case)
|
|
2680
|
+
* - Without `tableName`: `indexName` becomes the table name, and 'vector' is used as the column (Memory compatibility)
|
|
2681
|
+
*
|
|
2682
|
+
* @param tableName - Optional table name. If not provided, defaults to indexName.
|
|
2683
|
+
* @param indexName - The index/column name, or table name if tableName is not provided.
|
|
2684
|
+
* @param dimension - Vector dimension size.
|
|
2685
|
+
* @param metric - Distance metric: 'cosine', 'euclidean', or 'dotproduct'.
|
|
2686
|
+
* @param indexConfig - Optional index configuration.
|
|
2588
2687
|
*/
|
|
2589
2688
|
async createIndex({
|
|
2590
2689
|
tableName,
|
|
@@ -2593,13 +2692,12 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2593
2692
|
metric = "cosine",
|
|
2594
2693
|
indexConfig = {}
|
|
2595
2694
|
}) {
|
|
2695
|
+
const resolvedTableName = tableName ?? indexName;
|
|
2696
|
+
const columnToIndex = tableName ? indexName : "vector";
|
|
2596
2697
|
try {
|
|
2597
2698
|
if (!this.lanceClient) {
|
|
2598
2699
|
throw new Error("LanceDB client not initialized. Use LanceVectorStore.create() to create an instance");
|
|
2599
2700
|
}
|
|
2600
|
-
if (!tableName) {
|
|
2601
|
-
throw new Error("tableName is required");
|
|
2602
|
-
}
|
|
2603
2701
|
if (!indexName) {
|
|
2604
2702
|
throw new Error("indexName is required");
|
|
2605
2703
|
}
|
|
@@ -2612,19 +2710,33 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2612
2710
|
id: storage.createVectorErrorId("LANCE", "CREATE_INDEX", "INVALID_ARGS"),
|
|
2613
2711
|
domain: error.ErrorDomain.STORAGE,
|
|
2614
2712
|
category: error.ErrorCategory.USER,
|
|
2615
|
-
details: { tableName:
|
|
2713
|
+
details: { tableName: resolvedTableName, indexName, dimension, metric }
|
|
2616
2714
|
},
|
|
2617
2715
|
err
|
|
2618
2716
|
);
|
|
2619
2717
|
}
|
|
2620
2718
|
try {
|
|
2621
2719
|
const tables = await this.lanceClient.tableNames();
|
|
2622
|
-
|
|
2623
|
-
|
|
2624
|
-
|
|
2720
|
+
let table;
|
|
2721
|
+
if (!tables.includes(resolvedTableName)) {
|
|
2722
|
+
this.logger.debug(
|
|
2723
|
+
`Table ${resolvedTableName} does not exist. Creating empty table with dimension ${dimension}.`
|
|
2625
2724
|
);
|
|
2725
|
+
const initVector = new Array(dimension).fill(0);
|
|
2726
|
+
table = await this.lanceClient.createTable(resolvedTableName, [{ id: "__init__", vector: initVector }]);
|
|
2727
|
+
try {
|
|
2728
|
+
await table.delete("id = '__init__'");
|
|
2729
|
+
} catch (deleteError) {
|
|
2730
|
+
this.logger.warn(
|
|
2731
|
+
`Failed to delete initialization row from ${resolvedTableName}. Subsequent queries may include '__init__' row.`,
|
|
2732
|
+
deleteError
|
|
2733
|
+
);
|
|
2734
|
+
}
|
|
2735
|
+
this.logger.debug(`Table ${resolvedTableName} created. Index creation deferred until data is available.`);
|
|
2736
|
+
return;
|
|
2737
|
+
} else {
|
|
2738
|
+
table = await this.lanceClient.openTable(resolvedTableName);
|
|
2626
2739
|
}
|
|
2627
|
-
const table = await this.lanceClient.openTable(tableName);
|
|
2628
2740
|
let metricType;
|
|
2629
2741
|
if (metric === "euclidean") {
|
|
2630
2742
|
metricType = "l2";
|
|
@@ -2633,8 +2745,15 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2633
2745
|
} else if (metric === "cosine") {
|
|
2634
2746
|
metricType = "cosine";
|
|
2635
2747
|
}
|
|
2748
|
+
const rowCount = await table.countRows();
|
|
2749
|
+
if (rowCount < 256) {
|
|
2750
|
+
this.logger.warn(
|
|
2751
|
+
`Table ${resolvedTableName} has ${rowCount} rows, which is below the 256 row minimum for index creation. Skipping index creation.`
|
|
2752
|
+
);
|
|
2753
|
+
return;
|
|
2754
|
+
}
|
|
2636
2755
|
if (indexConfig.type === "ivfflat") {
|
|
2637
|
-
await table.createIndex(
|
|
2756
|
+
await table.createIndex(columnToIndex, {
|
|
2638
2757
|
config: lancedb.Index.ivfPq({
|
|
2639
2758
|
numPartitions: indexConfig.numPartitions || 128,
|
|
2640
2759
|
numSubVectors: indexConfig.numSubVectors || 16,
|
|
@@ -2643,7 +2762,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2643
2762
|
});
|
|
2644
2763
|
} else {
|
|
2645
2764
|
this.logger.debug("Creating HNSW PQ index with config:", indexConfig);
|
|
2646
|
-
await table.createIndex(
|
|
2765
|
+
await table.createIndex(columnToIndex, {
|
|
2647
2766
|
config: lancedb.Index.hnswPq({
|
|
2648
2767
|
m: indexConfig?.hnsw?.m || 16,
|
|
2649
2768
|
efConstruction: indexConfig?.hnsw?.efConstruction || 100,
|
|
@@ -2657,7 +2776,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2657
2776
|
id: storage.createVectorErrorId("LANCE", "CREATE_INDEX", "FAILED"),
|
|
2658
2777
|
domain: error.ErrorDomain.STORAGE,
|
|
2659
2778
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
2660
|
-
details: { tableName:
|
|
2779
|
+
details: { tableName: resolvedTableName, indexName, dimension }
|
|
2661
2780
|
},
|
|
2662
2781
|
error$1
|
|
2663
2782
|
);
|
|
@@ -2955,7 +3074,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2955
3074
|
}
|
|
2956
3075
|
return rowData;
|
|
2957
3076
|
});
|
|
2958
|
-
await table.
|
|
3077
|
+
await table.mergeInsert("id").whenMatchedUpdateAll().whenNotMatchedInsertAll().execute(updatedRecords);
|
|
2959
3078
|
return;
|
|
2960
3079
|
}
|
|
2961
3080
|
} catch (err) {
|