@mastra/lance 1.0.0-beta.8 → 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 +946 -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 +196 -87
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +195 -89
- 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 +8 -4
- 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
|
/**
|
|
@@ -1951,19 +1989,6 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
|
|
|
1951
1989
|
super({ id, name, disableInit });
|
|
1952
1990
|
this.stores = {};
|
|
1953
1991
|
}
|
|
1954
|
-
get supports() {
|
|
1955
|
-
return {
|
|
1956
|
-
selectByIncludeResourceScope: true,
|
|
1957
|
-
resourceWorkingMemory: true,
|
|
1958
|
-
hasColumn: true,
|
|
1959
|
-
createTable: true,
|
|
1960
|
-
deleteMessages: true,
|
|
1961
|
-
observability: false,
|
|
1962
|
-
indexManagement: false,
|
|
1963
|
-
listScoresBySpan: true,
|
|
1964
|
-
agents: false
|
|
1965
|
-
};
|
|
1966
|
-
}
|
|
1967
1992
|
};
|
|
1968
1993
|
var LanceFilterTranslator = class extends filter.BaseFilterTranslator {
|
|
1969
1994
|
translate(filter) {
|
|
@@ -2341,6 +2366,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2341
2366
|
}
|
|
2342
2367
|
async query({
|
|
2343
2368
|
tableName,
|
|
2369
|
+
indexName,
|
|
2344
2370
|
queryVector,
|
|
2345
2371
|
filter,
|
|
2346
2372
|
includeVector = false,
|
|
@@ -2348,12 +2374,13 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2348
2374
|
columns = [],
|
|
2349
2375
|
includeAllColumns = false
|
|
2350
2376
|
}) {
|
|
2377
|
+
const resolvedTableName = tableName ?? indexName;
|
|
2351
2378
|
try {
|
|
2352
2379
|
if (!this.lanceClient) {
|
|
2353
2380
|
throw new Error("LanceDB client not initialized. Use LanceVectorStore.create() to create an instance");
|
|
2354
2381
|
}
|
|
2355
|
-
if (!
|
|
2356
|
-
throw new Error("tableName is required");
|
|
2382
|
+
if (!resolvedTableName) {
|
|
2383
|
+
throw new Error("tableName or indexName is required");
|
|
2357
2384
|
}
|
|
2358
2385
|
if (!queryVector) {
|
|
2359
2386
|
throw new Error("queryVector is required");
|
|
@@ -2364,25 +2391,30 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2364
2391
|
id: storage.createVectorErrorId("LANCE", "QUERY", "INVALID_ARGS"),
|
|
2365
2392
|
domain: error.ErrorDomain.STORAGE,
|
|
2366
2393
|
category: error.ErrorCategory.USER,
|
|
2367
|
-
text:
|
|
2368
|
-
details: { tableName }
|
|
2394
|
+
text: error$1 instanceof Error ? error$1.message : "Invalid query arguments",
|
|
2395
|
+
details: { tableName: resolvedTableName }
|
|
2369
2396
|
},
|
|
2370
2397
|
error$1
|
|
2371
2398
|
);
|
|
2372
2399
|
}
|
|
2373
2400
|
try {
|
|
2374
|
-
const
|
|
2375
|
-
|
|
2376
|
-
|
|
2377
|
-
|
|
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 [];
|
|
2378
2405
|
}
|
|
2406
|
+
const table = await this.lanceClient.openTable(resolvedTableName);
|
|
2379
2407
|
let query = table.search(queryVector);
|
|
2380
2408
|
if (filter && Object.keys(filter).length > 0) {
|
|
2381
2409
|
const whereClause = this.filterTranslator(filter);
|
|
2382
2410
|
this.logger.debug(`Where clause generated: ${whereClause}`);
|
|
2383
2411
|
query = query.where(whereClause);
|
|
2384
2412
|
}
|
|
2385
|
-
if (!includeAllColumns &&
|
|
2413
|
+
if (!includeAllColumns && columns.length > 0) {
|
|
2414
|
+
const selectColumns = [...columns];
|
|
2415
|
+
if (!selectColumns.includes("id")) {
|
|
2416
|
+
selectColumns.push("id");
|
|
2417
|
+
}
|
|
2386
2418
|
query = query.select(selectColumns);
|
|
2387
2419
|
}
|
|
2388
2420
|
query = query.limit(topK);
|
|
@@ -2412,7 +2444,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2412
2444
|
id: storage.createVectorErrorId("LANCE", "QUERY", "FAILED"),
|
|
2413
2445
|
domain: error.ErrorDomain.STORAGE,
|
|
2414
2446
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
2415
|
-
details: { tableName, includeVector, columnsCount: columns?.length, includeAllColumns }
|
|
2447
|
+
details: { tableName: resolvedTableName, includeVector, columnsCount: columns?.length, includeAllColumns }
|
|
2416
2448
|
},
|
|
2417
2449
|
error$1
|
|
2418
2450
|
);
|
|
@@ -2447,13 +2479,14 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2447
2479
|
const translator = new LanceFilterTranslator();
|
|
2448
2480
|
return translator.translate(prefixedFilter);
|
|
2449
2481
|
}
|
|
2450
|
-
async upsert({ tableName, vectors, metadata = [], ids = [] }) {
|
|
2482
|
+
async upsert({ tableName, indexName, vectors, metadata = [], ids = [] }) {
|
|
2483
|
+
const resolvedTableName = tableName ?? indexName;
|
|
2451
2484
|
try {
|
|
2452
2485
|
if (!this.lanceClient) {
|
|
2453
2486
|
throw new Error("LanceDB client not initialized. Use LanceVectorStore.create() to create an instance");
|
|
2454
2487
|
}
|
|
2455
|
-
if (!
|
|
2456
|
-
throw new Error("tableName is required");
|
|
2488
|
+
if (!resolvedTableName) {
|
|
2489
|
+
throw new Error("tableName or indexName is required");
|
|
2457
2490
|
}
|
|
2458
2491
|
if (!vectors || !Array.isArray(vectors) || vectors.length === 0) {
|
|
2459
2492
|
throw new Error("vectors array is required and must not be empty");
|
|
@@ -2464,18 +2497,21 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2464
2497
|
id: storage.createVectorErrorId("LANCE", "UPSERT", "INVALID_ARGS"),
|
|
2465
2498
|
domain: error.ErrorDomain.STORAGE,
|
|
2466
2499
|
category: error.ErrorCategory.USER,
|
|
2467
|
-
text:
|
|
2468
|
-
details: { tableName }
|
|
2500
|
+
text: error$1 instanceof Error ? error$1.message : "Invalid upsert arguments",
|
|
2501
|
+
details: { tableName: resolvedTableName }
|
|
2469
2502
|
},
|
|
2470
2503
|
error$1
|
|
2471
2504
|
);
|
|
2472
2505
|
}
|
|
2473
2506
|
try {
|
|
2474
2507
|
const tables = await this.lanceClient.tableNames();
|
|
2475
|
-
|
|
2476
|
-
|
|
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);
|
|
2477
2514
|
}
|
|
2478
|
-
const table = await this.lanceClient.openTable(tableName);
|
|
2479
2515
|
const vectorIds = ids.length === vectors.length ? ids : vectors.map((_, i) => ids[i] || crypto.randomUUID());
|
|
2480
2516
|
const data = vectors.map((vector, i) => {
|
|
2481
2517
|
const id = String(vectorIds[i]);
|
|
@@ -2492,7 +2528,42 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2492
2528
|
}
|
|
2493
2529
|
return rowData;
|
|
2494
2530
|
});
|
|
2495
|
-
|
|
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
|
+
}
|
|
2496
2567
|
return vectorIds;
|
|
2497
2568
|
} catch (error$1) {
|
|
2498
2569
|
throw new error.MastraError(
|
|
@@ -2500,7 +2571,12 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2500
2571
|
id: storage.createVectorErrorId("LANCE", "UPSERT", "FAILED"),
|
|
2501
2572
|
domain: error.ErrorDomain.STORAGE,
|
|
2502
2573
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
2503
|
-
details: {
|
|
2574
|
+
details: {
|
|
2575
|
+
tableName: resolvedTableName,
|
|
2576
|
+
vectorCount: vectors.length,
|
|
2577
|
+
metadataCount: metadata.length,
|
|
2578
|
+
idsCount: ids.length
|
|
2579
|
+
}
|
|
2504
2580
|
},
|
|
2505
2581
|
error$1
|
|
2506
2582
|
);
|
|
@@ -2597,7 +2673,17 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2597
2673
|
}
|
|
2598
2674
|
}
|
|
2599
2675
|
/**
|
|
2600
|
-
*
|
|
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.
|
|
2601
2687
|
*/
|
|
2602
2688
|
async createIndex({
|
|
2603
2689
|
tableName,
|
|
@@ -2606,13 +2692,12 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2606
2692
|
metric = "cosine",
|
|
2607
2693
|
indexConfig = {}
|
|
2608
2694
|
}) {
|
|
2695
|
+
const resolvedTableName = tableName ?? indexName;
|
|
2696
|
+
const columnToIndex = tableName ? indexName : "vector";
|
|
2609
2697
|
try {
|
|
2610
2698
|
if (!this.lanceClient) {
|
|
2611
2699
|
throw new Error("LanceDB client not initialized. Use LanceVectorStore.create() to create an instance");
|
|
2612
2700
|
}
|
|
2613
|
-
if (!tableName) {
|
|
2614
|
-
throw new Error("tableName is required");
|
|
2615
|
-
}
|
|
2616
2701
|
if (!indexName) {
|
|
2617
2702
|
throw new Error("indexName is required");
|
|
2618
2703
|
}
|
|
@@ -2625,19 +2710,33 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2625
2710
|
id: storage.createVectorErrorId("LANCE", "CREATE_INDEX", "INVALID_ARGS"),
|
|
2626
2711
|
domain: error.ErrorDomain.STORAGE,
|
|
2627
2712
|
category: error.ErrorCategory.USER,
|
|
2628
|
-
details: { tableName:
|
|
2713
|
+
details: { tableName: resolvedTableName, indexName, dimension, metric }
|
|
2629
2714
|
},
|
|
2630
2715
|
err
|
|
2631
2716
|
);
|
|
2632
2717
|
}
|
|
2633
2718
|
try {
|
|
2634
2719
|
const tables = await this.lanceClient.tableNames();
|
|
2635
|
-
|
|
2636
|
-
|
|
2637
|
-
|
|
2720
|
+
let table;
|
|
2721
|
+
if (!tables.includes(resolvedTableName)) {
|
|
2722
|
+
this.logger.debug(
|
|
2723
|
+
`Table ${resolvedTableName} does not exist. Creating empty table with dimension ${dimension}.`
|
|
2638
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);
|
|
2639
2739
|
}
|
|
2640
|
-
const table = await this.lanceClient.openTable(tableName);
|
|
2641
2740
|
let metricType;
|
|
2642
2741
|
if (metric === "euclidean") {
|
|
2643
2742
|
metricType = "l2";
|
|
@@ -2646,8 +2745,15 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2646
2745
|
} else if (metric === "cosine") {
|
|
2647
2746
|
metricType = "cosine";
|
|
2648
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
|
+
}
|
|
2649
2755
|
if (indexConfig.type === "ivfflat") {
|
|
2650
|
-
await table.createIndex(
|
|
2756
|
+
await table.createIndex(columnToIndex, {
|
|
2651
2757
|
config: lancedb.Index.ivfPq({
|
|
2652
2758
|
numPartitions: indexConfig.numPartitions || 128,
|
|
2653
2759
|
numSubVectors: indexConfig.numSubVectors || 16,
|
|
@@ -2656,7 +2762,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2656
2762
|
});
|
|
2657
2763
|
} else {
|
|
2658
2764
|
this.logger.debug("Creating HNSW PQ index with config:", indexConfig);
|
|
2659
|
-
await table.createIndex(
|
|
2765
|
+
await table.createIndex(columnToIndex, {
|
|
2660
2766
|
config: lancedb.Index.hnswPq({
|
|
2661
2767
|
m: indexConfig?.hnsw?.m || 16,
|
|
2662
2768
|
efConstruction: indexConfig?.hnsw?.efConstruction || 100,
|
|
@@ -2670,7 +2776,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2670
2776
|
id: storage.createVectorErrorId("LANCE", "CREATE_INDEX", "FAILED"),
|
|
2671
2777
|
domain: error.ErrorDomain.STORAGE,
|
|
2672
2778
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
2673
|
-
details: { tableName:
|
|
2779
|
+
details: { tableName: resolvedTableName, indexName, dimension }
|
|
2674
2780
|
},
|
|
2675
2781
|
error$1
|
|
2676
2782
|
);
|
|
@@ -2968,7 +3074,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2968
3074
|
}
|
|
2969
3075
|
return rowData;
|
|
2970
3076
|
});
|
|
2971
|
-
await table.
|
|
3077
|
+
await table.mergeInsert("id").whenMatchedUpdateAll().whenNotMatchedInsertAll().execute(updatedRecords);
|
|
2972
3078
|
return;
|
|
2973
3079
|
}
|
|
2974
3080
|
} catch (err) {
|
|
@@ -3187,5 +3293,8 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
3187
3293
|
|
|
3188
3294
|
exports.LanceStorage = LanceStorage;
|
|
3189
3295
|
exports.LanceVectorStore = LanceVectorStore;
|
|
3296
|
+
exports.StoreMemoryLance = StoreMemoryLance;
|
|
3297
|
+
exports.StoreScoresLance = StoreScoresLance;
|
|
3298
|
+
exports.StoreWorkflowsLance = StoreWorkflowsLance;
|
|
3190
3299
|
//# sourceMappingURL=index.cjs.map
|
|
3191
3300
|
//# sourceMappingURL=index.cjs.map
|