@mastra/libsql 1.22.3-alpha.0 → 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.
@@ -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.22.3-alpha.0"
6
+ version: "1.22.3-alpha.2"
7
7
  ---
8
8
 
9
9
  ## When to use
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.22.3-alpha.0",
2
+ "version": "1.22.3-alpha.2",
3
3
  "package": "@mastra/libsql",
4
4
  "exports": {},
5
5
  "modules": {}
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
- vectorIndexes;
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.isMemoryDb = url.includes(":memory:");
409
- if (url.includes(`file:`) || this.isMemoryDb) {
410
- 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));
411
- 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));
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
+ });
456
+ }
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);
412
465
  }
413
- this.vectorIndexes = this.isMemoryDb ? Promise.resolve(/* @__PURE__ */ new Set()) : this.discoverVectorIndexes();
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
- async hasVectorIndex(parsedIndexName) {
457
- return (await this.vectorIndexes).has(`${parsedIndexName}_vector_idx`);
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 && await this.hasVectorIndex(parsedIndexName)) try {
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.executeWriteOperationWithRetry(() => this.doUpsert(args), true);
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.executeWriteOperationWithRetry(() => this.doCreateIndex(args));
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.then((indexes) => indexes.add(`${parsedIndexName}_vector_idx`));
702
+ this.vectorIndexes.add(`${parsedIndexName}_vector_idx`);
642
703
  }
643
- deleteIndex(args) {
704
+ async deleteIndex(args) {
644
705
  try {
645
- return this.executeWriteOperationWithRetry(() => this.doDeleteIndex(args));
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.then((indexes) => indexes.delete(`${parsedIndexName}_vector_idx`));
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.executeWriteOperationWithRetry(() => this.doUpdateVector(args));
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.executeWriteOperationWithRetry(() => this.doDeleteVector(args));
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.executeWriteOperationWithRetry(() => this.doDeleteVectors(args));
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.executeWriteOperationWithRetry(() => this._doTruncateIndex(args));
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({
@@ -12762,6 +12863,9 @@ function parseJson(val, column, rowId) {
12762
12863
  }
12763
12864
  return val;
12764
12865
  }
12866
+ function workflowDefinitionSelectColumns() {
12867
+ return buildSelectColumns(_mastra_core_storage.TABLE_WORKFLOW_DEFINITIONS).replace("json(\"schedule\") as \"schedule\"", "\"schedule\"");
12868
+ }
12765
12869
  function rowToDefinition(row) {
12766
12870
  const inputSchema = parseJson(row.inputSchema, "inputSchema", row.id);
12767
12871
  const outputSchema = parseJson(row.outputSchema, "outputSchema", row.id);
@@ -12784,6 +12888,12 @@ function rowToDefinition(row) {
12784
12888
  if (stateSchema !== void 0) def.stateSchema = stateSchema;
12785
12889
  const requestContextSchema = parseJson(row.requestContextSchema, "requestContextSchema", row.id);
12786
12890
  if (requestContextSchema !== void 0) def.requestContextSchema = requestContextSchema;
12891
+ try {
12892
+ const schedule = parseJson(row.schedule, "schedule", row.id);
12893
+ if (schedule != null) def.schedule = schedule;
12894
+ } catch {
12895
+ def.schedule = row.schedule;
12896
+ }
12787
12897
  if (row.authorId != null) def.authorId = String(row.authorId);
12788
12898
  return def;
12789
12899
  }
@@ -12805,6 +12915,11 @@ var WorkflowDefinitionsLibSQL = class extends _mastra_core_storage.WorkflowDefin
12805
12915
  tableName: _mastra_core_storage.TABLE_WORKFLOW_DEFINITIONS,
12806
12916
  schema: _mastra_core_storage.TABLE_SCHEMAS[_mastra_core_storage.TABLE_WORKFLOW_DEFINITIONS]
12807
12917
  });
12918
+ await this.#db.alterTable({
12919
+ tableName: _mastra_core_storage.TABLE_WORKFLOW_DEFINITIONS,
12920
+ schema: _mastra_core_storage.TABLE_SCHEMAS[_mastra_core_storage.TABLE_WORKFLOW_DEFINITIONS],
12921
+ ifNotExists: ["schedule"]
12922
+ });
12808
12923
  await this.#client.execute({
12809
12924
  sql: `CREATE INDEX IF NOT EXISTS idx_workflow_definitions_status ON "${_mastra_core_storage.TABLE_WORKFLOW_DEFINITIONS}" ("status")`,
12810
12925
  args: []
@@ -12828,6 +12943,7 @@ var WorkflowDefinitionsLibSQL = class extends _mastra_core_storage.WorkflowDefin
12828
12943
  stateSchema: input.stateSchema ?? null,
12829
12944
  requestContextSchema: input.requestContextSchema ?? null,
12830
12945
  graph: input.graph,
12946
+ schedule: "schedule" in input ? input.schedule ?? null : null,
12831
12947
  status: "active",
12832
12948
  source: "storage",
12833
12949
  authorId: "authorId" in input ? input.authorId ?? null : null,
@@ -12858,6 +12974,7 @@ var WorkflowDefinitionsLibSQL = class extends _mastra_core_storage.WorkflowDefin
12858
12974
  if ("stateSchema" in input && input.stateSchema !== void 0) data.stateSchema = input.stateSchema;
12859
12975
  if ("requestContextSchema" in input && input.requestContextSchema !== void 0) data.requestContextSchema = input.requestContextSchema;
12860
12976
  if ("graph" in input && input.graph !== void 0) data.graph = input.graph;
12977
+ if ("schedule" in input && input.schedule !== void 0) data.schedule = input.schedule;
12861
12978
  if ("status" in input && input.status !== void 0) data.status = input.status;
12862
12979
  if ("authorId" in input && input.authorId !== void 0) data.authorId = input.authorId;
12863
12980
  await this.#db.update({
@@ -12871,7 +12988,7 @@ var WorkflowDefinitionsLibSQL = class extends _mastra_core_storage.WorkflowDefin
12871
12988
  }
12872
12989
  async get(id) {
12873
12990
  const row = (await this.#client.execute({
12874
- sql: `SELECT ${buildSelectColumns(_mastra_core_storage.TABLE_WORKFLOW_DEFINITIONS)} FROM "${_mastra_core_storage.TABLE_WORKFLOW_DEFINITIONS}" WHERE id = ?`,
12991
+ sql: `SELECT ${workflowDefinitionSelectColumns()} FROM "${_mastra_core_storage.TABLE_WORKFLOW_DEFINITIONS}" WHERE id = ?`,
12875
12992
  args: [id]
12876
12993
  })).rows[0];
12877
12994
  return row ? rowToDefinition(row) : null;
@@ -12889,7 +13006,7 @@ var WorkflowDefinitionsLibSQL = class extends _mastra_core_storage.WorkflowDefin
12889
13006
  }
12890
13007
  const where = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
12891
13008
  const definitions = (await this.#client.execute({
12892
- sql: `SELECT ${buildSelectColumns(_mastra_core_storage.TABLE_WORKFLOW_DEFINITIONS)} FROM "${_mastra_core_storage.TABLE_WORKFLOW_DEFINITIONS}" ${where} ORDER BY updatedAt DESC`,
13009
+ sql: `SELECT ${workflowDefinitionSelectColumns()} FROM "${_mastra_core_storage.TABLE_WORKFLOW_DEFINITIONS}" ${where} ORDER BY updatedAt DESC`,
12893
13010
  args: params
12894
13011
  })).rows.map((row) => rowToDefinition(row));
12895
13012
  return {