@mastra/libsql 1.22.3-alpha.1 → 1.22.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/docs/SKILL.md +1 -1
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/index.cjs +125 -24
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +125 -24
- package/dist/index.js.map +1 -1
- package/dist/storage/domains/agents/index.d.ts +1 -0
- package/dist/storage/domains/agents/index.d.ts.map +1 -1
- package/dist/storage/domains/skills/index.d.ts +1 -0
- package/dist/storage/domains/skills/index.d.ts.map +1 -1
- package/dist/vector/index.d.ts +6 -0
- package/dist/vector/index.d.ts.map +1 -1
- package/dist/vector/write-lock.d.ts +7 -0
- package/dist/vector/write-lock.d.ts.map +1 -0
- package/package.json +3 -3
package/dist/docs/SKILL.md
CHANGED
package/dist/index.cjs
CHANGED
|
@@ -5,6 +5,8 @@ let _mastra_core_storage = require("@mastra/core/storage");
|
|
|
5
5
|
let _mastra_core_utils = require("@mastra/core/utils");
|
|
6
6
|
let _mastra_core_vector = require("@mastra/core/vector");
|
|
7
7
|
let _mastra_core_vector_filter = require("@mastra/core/vector/filter");
|
|
8
|
+
let fs_promises = require("fs/promises");
|
|
9
|
+
let path = require("path");
|
|
8
10
|
let _mastra_core_base = require("@mastra/core/base");
|
|
9
11
|
let crypto$1 = require("crypto");
|
|
10
12
|
let _mastra_core_agent = require("@mastra/core/agent");
|
|
@@ -385,6 +387,31 @@ const processOperator = (key, operator, operatorValue) => {
|
|
|
385
387
|
};
|
|
386
388
|
};
|
|
387
389
|
//#endregion
|
|
390
|
+
//#region src/vector/write-lock.ts
|
|
391
|
+
const databaseWriteChains = /* @__PURE__ */ new Map();
|
|
392
|
+
async function getLocalFileDatabaseKey({ url, syncUrl, cwd }) {
|
|
393
|
+
if (!url.startsWith("file:") || url.includes(":memory:") || syncUrl) return;
|
|
394
|
+
const uriPath = url.slice(5).split(/[?#]/, 1)[0];
|
|
395
|
+
const decodedPath = decodeURIComponent(uriPath);
|
|
396
|
+
const absolutePath = (0, path.isAbsolute)(decodedPath) ? decodedPath : (0, path.resolve)(cwd, decodedPath);
|
|
397
|
+
try {
|
|
398
|
+
return await (0, fs_promises.realpath)(absolutePath);
|
|
399
|
+
} catch (error) {
|
|
400
|
+
if (!(error instanceof Error) || !("code" in error) || error.code !== "ENOENT") throw error;
|
|
401
|
+
}
|
|
402
|
+
return (0, path.join)(await (0, fs_promises.realpath)((0, path.dirname)(absolutePath)), (0, path.basename)(absolutePath));
|
|
403
|
+
}
|
|
404
|
+
function withLocalFileDatabaseWriteLock(key, fn) {
|
|
405
|
+
if (!key) return fn();
|
|
406
|
+
const result = (databaseWriteChains.get(key) ?? Promise.resolve()).then(fn, fn);
|
|
407
|
+
const tail = result.then(() => void 0, () => void 0);
|
|
408
|
+
databaseWriteChains.set(key, tail);
|
|
409
|
+
tail.then(() => {
|
|
410
|
+
if (databaseWriteChains.get(key) === tail) databaseWriteChains.delete(key);
|
|
411
|
+
});
|
|
412
|
+
return result;
|
|
413
|
+
}
|
|
414
|
+
//#endregion
|
|
388
415
|
//#region src/vector/index.ts
|
|
389
416
|
var LibSQLVector = class extends _mastra_core_vector.MastraVector {
|
|
390
417
|
turso;
|
|
@@ -392,25 +419,57 @@ var LibSQLVector = class extends _mastra_core_vector.MastraVector {
|
|
|
392
419
|
initialBackoffMs;
|
|
393
420
|
overFetchMultiplier;
|
|
394
421
|
isMemoryDb;
|
|
395
|
-
|
|
422
|
+
initialization;
|
|
423
|
+
databaseKey;
|
|
424
|
+
vectorIndexes = /* @__PURE__ */ new Set();
|
|
396
425
|
constructor({ url, authToken, syncUrl, syncInterval, maxRetries = 5, initialBackoffMs = 100, vectorTopKOverFetchMultiplier = 10, id }) {
|
|
397
426
|
super({ id });
|
|
427
|
+
this.isMemoryDb = url.includes(":memory:");
|
|
428
|
+
const isLocalDb = (url.startsWith("file:") || this.isMemoryDb) && !syncUrl;
|
|
429
|
+
const cwd = process.cwd();
|
|
398
430
|
this.turso = (0, _libsql_client.createClient)({
|
|
399
431
|
url,
|
|
400
432
|
syncUrl,
|
|
401
433
|
authToken,
|
|
402
|
-
syncInterval
|
|
434
|
+
syncInterval,
|
|
435
|
+
...isLocalDb ? { timeout: 5e3 } : {}
|
|
403
436
|
});
|
|
404
437
|
this.maxRetries = maxRetries;
|
|
405
438
|
this.initialBackoffMs = initialBackoffMs;
|
|
406
439
|
if (!Number.isInteger(vectorTopKOverFetchMultiplier) || vectorTopKOverFetchMultiplier < 1) throw new Error("vectorTopKOverFetchMultiplier must be a positive integer");
|
|
407
440
|
this.overFetchMultiplier = vectorTopKOverFetchMultiplier;
|
|
408
|
-
this.
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
441
|
+
this.initialization = this.initialize({
|
|
442
|
+
url,
|
|
443
|
+
syncUrl,
|
|
444
|
+
cwd,
|
|
445
|
+
isLocalDb
|
|
446
|
+
});
|
|
447
|
+
}
|
|
448
|
+
async initialize({ url, syncUrl, cwd, isLocalDb }) {
|
|
449
|
+
if (isLocalDb) {
|
|
450
|
+
await this.applyLocalPragmas();
|
|
451
|
+
this.databaseKey = await getLocalFileDatabaseKey({
|
|
452
|
+
url,
|
|
453
|
+
syncUrl,
|
|
454
|
+
cwd
|
|
455
|
+
});
|
|
412
456
|
}
|
|
413
|
-
|
|
457
|
+
if (!this.isMemoryDb) this.vectorIndexes = await this.discoverVectorIndexes();
|
|
458
|
+
}
|
|
459
|
+
async applyLocalPragmas() {
|
|
460
|
+
for (const [label, sql] of [["journal_mode=WAL", "PRAGMA journal_mode=WAL;"], ["busy_timeout=5000", "PRAGMA busy_timeout = 5000;"]]) try {
|
|
461
|
+
await this.turso.execute(sql);
|
|
462
|
+
this.logger.debug(`LibSQLStore: PRAGMA ${label} set.`);
|
|
463
|
+
} catch (err) {
|
|
464
|
+
this.logger.warn(`LibSQLStore: Failed to set PRAGMA ${label}.`, err);
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
async ensureInitialized() {
|
|
468
|
+
await this.initialization;
|
|
469
|
+
}
|
|
470
|
+
async executeMutation(operation, isTransaction = false) {
|
|
471
|
+
await this.ensureInitialized();
|
|
472
|
+
return withLocalFileDatabaseWriteLock(this.databaseKey, () => this.executeWriteOperationWithRetry(operation, isTransaction));
|
|
414
473
|
}
|
|
415
474
|
/**
|
|
416
475
|
* Closes the underlying libsql client, releasing this vector store's OS file handles.
|
|
@@ -418,6 +477,7 @@ var LibSQLVector = class extends _mastra_core_vector.MastraVector {
|
|
|
418
477
|
* Safe to call more than once; subsequent calls are no-ops.
|
|
419
478
|
*/
|
|
420
479
|
async close() {
|
|
480
|
+
await this.ensureInitialized();
|
|
421
481
|
if (!this.turso.closed) this.turso.close();
|
|
422
482
|
}
|
|
423
483
|
async discoverVectorIndexes() {
|
|
@@ -453,8 +513,8 @@ var LibSQLVector = class extends _mastra_core_vector.MastraVector {
|
|
|
453
513
|
transformFilter(filter) {
|
|
454
514
|
return new LibSQLFilterTranslator().translate(filter);
|
|
455
515
|
}
|
|
456
|
-
|
|
457
|
-
return
|
|
516
|
+
hasVectorIndex(parsedIndexName) {
|
|
517
|
+
return this.vectorIndexes.has(`${parsedIndexName}_vector_idx`);
|
|
458
518
|
}
|
|
459
519
|
async queryWithIndex(parsedIndexName, vectorStr, topK, filter, includeVector, minScore) {
|
|
460
520
|
const { sql: filterQuery, values: filterValues } = buildFilterQuery(this.transformFilter(filter));
|
|
@@ -509,9 +569,10 @@ var LibSQLVector = class extends _mastra_core_vector.MastraVector {
|
|
|
509
569
|
details: { message: "queryVector must be an array of finite numbers" }
|
|
510
570
|
});
|
|
511
571
|
try {
|
|
572
|
+
await this.ensureInitialized();
|
|
512
573
|
const parsedIndexName = (0, _mastra_core_utils.parseSqlIdentifier)(indexName, "index name");
|
|
513
574
|
const vectorStr = `[${queryVector.join(",")}]`;
|
|
514
|
-
if (!this.isMemoryDb &&
|
|
575
|
+
if (!this.isMemoryDb && this.hasVectorIndex(parsedIndexName)) try {
|
|
515
576
|
const indexedResults = await this.queryWithIndex(parsedIndexName, vectorStr, topK, filter, includeVector, minScore);
|
|
516
577
|
if (!filter || indexedResults.length >= topK) return indexedResults;
|
|
517
578
|
} catch (err) {
|
|
@@ -552,9 +613,9 @@ var LibSQLVector = class extends _mastra_core_vector.MastraVector {
|
|
|
552
613
|
}, error);
|
|
553
614
|
}
|
|
554
615
|
}
|
|
555
|
-
upsert(args) {
|
|
616
|
+
async upsert(args) {
|
|
556
617
|
try {
|
|
557
|
-
return this.
|
|
618
|
+
return await this.executeMutation(() => this.doUpsert(args), true);
|
|
558
619
|
} catch (error) {
|
|
559
620
|
throw new _mastra_core_error.MastraError({
|
|
560
621
|
id: (0, _mastra_core_storage.createVectorErrorId)("LIBSQL", "UPSERT", "FAILED"),
|
|
@@ -602,9 +663,9 @@ var LibSQLVector = class extends _mastra_core_vector.MastraVector {
|
|
|
602
663
|
throw error;
|
|
603
664
|
}
|
|
604
665
|
}
|
|
605
|
-
createIndex(args) {
|
|
666
|
+
async createIndex(args) {
|
|
606
667
|
try {
|
|
607
|
-
return this.
|
|
668
|
+
return await this.executeMutation(() => this.doCreateIndex(args));
|
|
608
669
|
} catch (error) {
|
|
609
670
|
throw new _mastra_core_error.MastraError({
|
|
610
671
|
id: (0, _mastra_core_storage.createVectorErrorId)("LIBSQL", "CREATE_INDEX", "FAILED"),
|
|
@@ -638,11 +699,11 @@ var LibSQLVector = class extends _mastra_core_vector.MastraVector {
|
|
|
638
699
|
`,
|
|
639
700
|
args: []
|
|
640
701
|
});
|
|
641
|
-
this.vectorIndexes.
|
|
702
|
+
this.vectorIndexes.add(`${parsedIndexName}_vector_idx`);
|
|
642
703
|
}
|
|
643
|
-
deleteIndex(args) {
|
|
704
|
+
async deleteIndex(args) {
|
|
644
705
|
try {
|
|
645
|
-
return this.
|
|
706
|
+
return await this.executeMutation(() => this.doDeleteIndex(args));
|
|
646
707
|
} catch (error) {
|
|
647
708
|
throw new _mastra_core_error.MastraError({
|
|
648
709
|
id: (0, _mastra_core_storage.createVectorErrorId)("LIBSQL", "DELETE_INDEX", "FAILED"),
|
|
@@ -658,10 +719,11 @@ var LibSQLVector = class extends _mastra_core_vector.MastraVector {
|
|
|
658
719
|
sql: `DROP TABLE IF EXISTS ${parsedIndexName}`,
|
|
659
720
|
args: []
|
|
660
721
|
});
|
|
661
|
-
this.vectorIndexes.
|
|
722
|
+
this.vectorIndexes.delete(`${parsedIndexName}_vector_idx`);
|
|
662
723
|
}
|
|
663
724
|
async listIndexes() {
|
|
664
725
|
try {
|
|
726
|
+
await this.ensureInitialized();
|
|
665
727
|
return (await this.turso.execute({
|
|
666
728
|
sql: `
|
|
667
729
|
SELECT name FROM sqlite_master
|
|
@@ -686,6 +748,7 @@ var LibSQLVector = class extends _mastra_core_vector.MastraVector {
|
|
|
686
748
|
*/
|
|
687
749
|
async describeIndex({ indexName }) {
|
|
688
750
|
try {
|
|
751
|
+
await this.ensureInitialized();
|
|
689
752
|
const parsedIndexName = (0, _mastra_core_utils.parseSqlIdentifier)(indexName, "index name");
|
|
690
753
|
const tableInfo = await this.turso.execute({
|
|
691
754
|
sql: `
|
|
@@ -731,7 +794,7 @@ var LibSQLVector = class extends _mastra_core_vector.MastraVector {
|
|
|
731
794
|
* @throws Will throw an error if no updates are provided or if the update operation fails.
|
|
732
795
|
*/
|
|
733
796
|
updateVector(args) {
|
|
734
|
-
return this.
|
|
797
|
+
return this.executeMutation(() => this.doUpdateVector(args));
|
|
735
798
|
}
|
|
736
799
|
async doUpdateVector(params) {
|
|
737
800
|
const { indexName, update } = params;
|
|
@@ -836,9 +899,9 @@ var LibSQLVector = class extends _mastra_core_vector.MastraVector {
|
|
|
836
899
|
* @returns A promise that resolves when the deletion is complete.
|
|
837
900
|
* @throws Will throw an error if the deletion operation fails.
|
|
838
901
|
*/
|
|
839
|
-
deleteVector(args) {
|
|
902
|
+
async deleteVector(args) {
|
|
840
903
|
try {
|
|
841
|
-
return this.
|
|
904
|
+
return await this.executeMutation(() => this.doDeleteVector(args));
|
|
842
905
|
} catch (error) {
|
|
843
906
|
throw new _mastra_core_error.MastraError({
|
|
844
907
|
id: (0, _mastra_core_storage.createVectorErrorId)("LIBSQL", "DELETE_VECTOR", "FAILED"),
|
|
@@ -859,7 +922,7 @@ var LibSQLVector = class extends _mastra_core_vector.MastraVector {
|
|
|
859
922
|
});
|
|
860
923
|
}
|
|
861
924
|
deleteVectors(args) {
|
|
862
|
-
return this.
|
|
925
|
+
return this.executeMutation(() => this.doDeleteVectors(args));
|
|
863
926
|
}
|
|
864
927
|
async doDeleteVectors({ indexName, filter, ids }) {
|
|
865
928
|
const parsedIndexName = (0, _mastra_core_utils.parseSqlIdentifier)(indexName, "index name");
|
|
@@ -941,9 +1004,9 @@ var LibSQLVector = class extends _mastra_core_vector.MastraVector {
|
|
|
941
1004
|
}, error);
|
|
942
1005
|
}
|
|
943
1006
|
}
|
|
944
|
-
truncateIndex(args) {
|
|
1007
|
+
async truncateIndex(args) {
|
|
945
1008
|
try {
|
|
946
|
-
return this.
|
|
1009
|
+
return await this.executeMutation(() => this._doTruncateIndex(args));
|
|
947
1010
|
} catch (error) {
|
|
948
1011
|
throw new _mastra_core_error.MastraError({
|
|
949
1012
|
id: (0, _mastra_core_storage.createVectorErrorId)("LIBSQL", "TRUNCATE_INDEX", "FAILED"),
|
|
@@ -2499,6 +2562,26 @@ var AgentsLibSQL = class extends _mastra_core_storage.AgentsStorage {
|
|
|
2499
2562
|
}, error);
|
|
2500
2563
|
}
|
|
2501
2564
|
}
|
|
2565
|
+
async getVersions(ids) {
|
|
2566
|
+
if (ids.length === 0) return [];
|
|
2567
|
+
try {
|
|
2568
|
+
return (await this.#db.selectMany({
|
|
2569
|
+
tableName: _mastra_core_storage.TABLE_AGENT_VERSIONS,
|
|
2570
|
+
whereClause: {
|
|
2571
|
+
sql: `WHERE id IN (${ids.map(() => "?").join(", ")})`,
|
|
2572
|
+
args: ids
|
|
2573
|
+
}
|
|
2574
|
+
}) ?? []).map((row) => this.parseVersionRow(row));
|
|
2575
|
+
} catch (error) {
|
|
2576
|
+
if (error instanceof _mastra_core_error.MastraError) throw error;
|
|
2577
|
+
throw new _mastra_core_error.MastraError({
|
|
2578
|
+
id: (0, _mastra_core_storage.createStorageErrorId)("LIBSQL", "GET_VERSIONS", "FAILED"),
|
|
2579
|
+
domain: _mastra_core_error.ErrorDomain.STORAGE,
|
|
2580
|
+
category: _mastra_core_error.ErrorCategory.THIRD_PARTY,
|
|
2581
|
+
details: { count: ids.length }
|
|
2582
|
+
}, error);
|
|
2583
|
+
}
|
|
2584
|
+
}
|
|
2502
2585
|
async getVersionByNumber(agentId, versionNumber) {
|
|
2503
2586
|
try {
|
|
2504
2587
|
const rows = await this.#db.selectMany({
|
|
@@ -12242,6 +12325,24 @@ var SkillsLibSQL = class extends _mastra_core_storage.SkillsStorage {
|
|
|
12242
12325
|
}, error);
|
|
12243
12326
|
}
|
|
12244
12327
|
}
|
|
12328
|
+
async getVersions(ids) {
|
|
12329
|
+
if (ids.length === 0) return [];
|
|
12330
|
+
try {
|
|
12331
|
+
const placeholders = ids.map(() => "?").join(", ");
|
|
12332
|
+
return ((await this.#client.execute({
|
|
12333
|
+
sql: `SELECT ${buildSelectColumns(_mastra_core_storage.TABLE_SKILL_VERSIONS)} FROM "${_mastra_core_storage.TABLE_SKILL_VERSIONS}" WHERE id IN (${placeholders})`,
|
|
12334
|
+
args: ids
|
|
12335
|
+
})).rows ?? []).map((row) => this.#parseVersionRow(row));
|
|
12336
|
+
} catch (error) {
|
|
12337
|
+
if (error instanceof _mastra_core_error.MastraError) throw error;
|
|
12338
|
+
throw new _mastra_core_error.MastraError({
|
|
12339
|
+
id: (0, _mastra_core_storage.createStorageErrorId)("LIBSQL", "GET_SKILL_VERSIONS", "FAILED"),
|
|
12340
|
+
domain: _mastra_core_error.ErrorDomain.STORAGE,
|
|
12341
|
+
category: _mastra_core_error.ErrorCategory.THIRD_PARTY,
|
|
12342
|
+
details: { count: ids.length }
|
|
12343
|
+
}, error);
|
|
12344
|
+
}
|
|
12345
|
+
}
|
|
12245
12346
|
async getVersionByNumber(skillId, versionNumber) {
|
|
12246
12347
|
try {
|
|
12247
12348
|
const row = (await this.#client.execute({
|