@mastra/libsql 1.27.0-alpha.1 → 1.27.0-alpha.3

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
@@ -1644,6 +1644,27 @@ function buildSelectColumnsWithAlias(tableName, alias) {
1644
1644
  function isLockError(error) {
1645
1645
  return error.code === "SQLITE_BUSY" || error.code === "SQLITE_LOCKED" || error.message?.toLowerCase().includes("database is locked") || error.message?.toLowerCase().includes("database table is locked") || error.message?.toLowerCase().includes("table is locked") || error.constructor.name === "SqliteError" && error.message?.toLowerCase().includes("locked");
1646
1646
  }
1647
+ const TRANSIENT_NETWORK_PATTERNS = [
1648
+ "fetch failed",
1649
+ "econnreset",
1650
+ "etimedout",
1651
+ "eai_again",
1652
+ "socket hang up",
1653
+ "und_err_"
1654
+ ];
1655
+ /**
1656
+ * Checks if an error (or any error in its `cause` chain) is a transient network failure
1657
+ * such as `fetch failed` from a remote libsql client.
1658
+ */
1659
+ function isTransientNetworkError(error) {
1660
+ let current = error;
1661
+ for (let depth = 0; current && depth < 5; depth++) {
1662
+ const text = `${current.message ?? ""} ${current.code ?? ""}`.toLowerCase();
1663
+ if (TRANSIENT_NETWORK_PATTERNS.some((pattern) => text.includes(pattern))) return true;
1664
+ current = current.cause;
1665
+ }
1666
+ return false;
1667
+ }
1647
1668
  function createExecuteWriteOperationWithRetry({ logger, maxRetries, initialBackoffMs }) {
1648
1669
  return async function executeWriteOperationWithRetry(operationFn, operationDescription) {
1649
1670
  let attempts = 0;
@@ -14355,7 +14376,7 @@ var SkillsLibSQL = class extends SkillsStorage {
14355
14376
  }
14356
14377
  async list(args) {
14357
14378
  try {
14358
- const { page = 0, perPage: perPageInput, orderBy, authorId, visibility, status, entityIds, pinFavoritedFor, favoritedOnly } = args || {};
14379
+ const { page = 0, perPage: perPageInput, orderBy, authorId, visibility, status, entityIds, pinFavoritedFor, favoritedOnly, metadata } = args || {};
14359
14380
  const { field, direction } = this.parseOrderBy(orderBy);
14360
14381
  if (entityIds && entityIds.length === 0) return {
14361
14382
  skills: [],
@@ -14383,6 +14404,28 @@ var SkillsLibSQL = class extends SkillsStorage {
14383
14404
  conditions.push(`s_e.id IN (${placeholders})`);
14384
14405
  queryParams.push(...entityIds);
14385
14406
  }
14407
+ if (metadata && Object.keys(metadata).length > 0) {
14408
+ const latestVersionId = `(SELECT lv.id FROM "${TABLE_SKILL_VERSIONS}" lv WHERE lv."skillId" = s_e.id ORDER BY lv."versionNumber" DESC LIMIT 1)`;
14409
+ const resolvedVersionId = status === "draft" ? latestVersionId : `COALESCE(s_e."activeVersionId", ${latestVersionId})`;
14410
+ const metadataConditions = [];
14411
+ for (const [key, value] of Object.entries(metadata)) {
14412
+ if (!/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(key)) throw new MastraError({
14413
+ id: createStorageErrorId("LIBSQL", "LIST_SKILLS", "INVALID_METADATA_KEY"),
14414
+ domain: ErrorDomain.STORAGE,
14415
+ category: ErrorCategory.USER,
14416
+ text: `Invalid metadata key: ${key}. Keys must be alphanumeric with underscores.`,
14417
+ details: { key }
14418
+ });
14419
+ if (typeof value === "string") {
14420
+ metadataConditions.push(`json_extract(mv.metadata, '$.${key}') = ?`);
14421
+ queryParams.push(value);
14422
+ } else {
14423
+ metadataConditions.push(`json_extract(mv.metadata, '$.${key}') = json_extract(?, '$')`);
14424
+ queryParams.push(JSON.stringify(value));
14425
+ }
14426
+ }
14427
+ conditions.push(`EXISTS (SELECT 1 FROM "${TABLE_SKILL_VERSIONS}" mv WHERE mv."skillId" = s_e.id AND mv.id = ${resolvedVersionId} AND ${metadataConditions.join(" AND ")})`);
14428
+ }
14386
14429
  const joinUserId = pinFavoritedFor;
14387
14430
  const useJoin = Boolean(joinUserId);
14388
14431
  let joinClause = "";
@@ -16583,6 +16626,8 @@ var LibSQLFactoryStorage = class extends FactoryStorage {
16583
16626
  };
16584
16627
  //#endregion
16585
16628
  //#region src/storage/index.ts
16629
+ const REMOTE_INIT_CONCURRENCY = 5;
16630
+ const REMOTE_INIT_MAX_ATTEMPTS = 3;
16586
16631
  const DEFAULT_LOCAL_CACHE_SIZE = -16e3;
16587
16632
  const DEFAULT_LOCAL_MMAP_SIZE = 134217728;
16588
16633
  /**
@@ -16729,8 +16774,31 @@ var LibSQLStore = class extends MastraCompositeStore {
16729
16774
  for (const store of this.getStoresToInit()) await store.init();
16730
16775
  return true;
16731
16776
  }
16777
+ async initStoreWithRetry(store) {
16778
+ for (let attempt = 1;; attempt++) try {
16779
+ await store.init();
16780
+ return;
16781
+ } catch (error) {
16782
+ if (attempt >= REMOTE_INIT_MAX_ATTEMPTS || !isTransientNetworkError(error)) throw error;
16783
+ const backoff = this.initialBackoffMs * 2 ** (attempt - 1);
16784
+ this.logger.warn(`LibSQLStore: Transient network error during domain init, retrying in ${backoff}ms (attempt ${attempt}/${REMOTE_INIT_MAX_ATTEMPTS}).`, error);
16785
+ await new Promise((resolve) => setTimeout(resolve, backoff));
16786
+ }
16787
+ }
16732
16788
  async initDomainsInParallel() {
16733
- await Promise.all(this.getStoresToInit().map((store) => store.init()));
16789
+ const stores = this.getStoresToInit();
16790
+ let next = 0;
16791
+ let failed = false;
16792
+ const worker = async () => {
16793
+ while (!failed && next < stores.length) try {
16794
+ await this.initStoreWithRetry(stores[next++]);
16795
+ } catch (error) {
16796
+ failed = true;
16797
+ throw error;
16798
+ }
16799
+ };
16800
+ const rejected = (await Promise.allSettled(Array.from({ length: Math.min(REMOTE_INIT_CONCURRENCY, stores.length) }, worker))).find((r) => r.status === "rejected");
16801
+ if (rejected) throw rejected.reason;
16734
16802
  return true;
16735
16803
  }
16736
16804
  async init() {
@@ -16741,7 +16809,10 @@ var LibSQLStore = class extends MastraCompositeStore {
16741
16809
  await this.hasInitialized;
16742
16810
  return;
16743
16811
  }
16744
- this.hasInitialized = this.initDomainsInParallel();
16812
+ this.hasInitialized = this.initDomainsInParallel().catch((error) => {
16813
+ this.hasInitialized = null;
16814
+ throw error;
16815
+ });
16745
16816
  await this.hasInitialized;
16746
16817
  return;
16747
16818
  }
@@ -16753,7 +16824,10 @@ var LibSQLStore = class extends MastraCompositeStore {
16753
16824
  await this.hasInitialized;
16754
16825
  return;
16755
16826
  }
16756
- this.hasInitialized = this.initDomainsSequentially();
16827
+ this.hasInitialized = this.initDomainsSequentially().catch((error) => {
16828
+ this.hasInitialized = null;
16829
+ throw error;
16830
+ });
16757
16831
  await this.hasInitialized;
16758
16832
  return;
16759
16833
  }