@mastra/libsql 0.10.4-alpha.0 → 0.10.4-alpha.1

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
@@ -1,4 +1,5 @@
1
1
  import { createClient } from '@libsql/client';
2
+ import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
2
3
  import { parseSqlIdentifier, parseFieldKey } from '@mastra/core/utils';
3
4
  import { MastraVector } from '@mastra/core/vector';
4
5
  import { BaseFilterTranslator } from '@mastra/core/vector/filter';
@@ -554,6 +555,17 @@ var LibSQLVector = class extends MastraVector {
554
555
  if (!Array.isArray(queryVector) || !queryVector.every((x) => typeof x === "number" && Number.isFinite(x))) {
555
556
  throw new Error("queryVector must be an array of finite numbers");
556
557
  }
558
+ } catch (error) {
559
+ throw new MastraError(
560
+ {
561
+ id: "LIBSQL_VECTOR_QUERY_INVALID_ARGS",
562
+ domain: ErrorDomain.STORAGE,
563
+ category: ErrorCategory.USER
564
+ },
565
+ error
566
+ );
567
+ }
568
+ try {
557
569
  const parsedIndexName = parseSqlIdentifier(indexName, "index name");
558
570
  const vectorStr = `[${queryVector.join(",")}]`;
559
571
  const translatedFilter = this.transformFilter(filter);
@@ -585,11 +597,30 @@ var LibSQLVector = class extends MastraVector {
585
597
  metadata: JSON.parse(metadata ?? "{}"),
586
598
  ...includeVector && embedding && { vector: JSON.parse(embedding) }
587
599
  }));
588
- } finally {
600
+ } catch (error) {
601
+ throw new MastraError(
602
+ {
603
+ id: "LIBSQL_VECTOR_QUERY_FAILED",
604
+ domain: ErrorDomain.STORAGE,
605
+ category: ErrorCategory.THIRD_PARTY
606
+ },
607
+ error
608
+ );
589
609
  }
590
610
  }
591
611
  upsert(args) {
592
- return this.executeWriteOperationWithRetry(() => this.doUpsert(args), true);
612
+ try {
613
+ return this.executeWriteOperationWithRetry(() => this.doUpsert(args), true);
614
+ } catch (error) {
615
+ throw new MastraError(
616
+ {
617
+ id: "LIBSQL_VECTOR_UPSERT_FAILED",
618
+ domain: ErrorDomain.STORAGE,
619
+ category: ErrorCategory.THIRD_PARTY
620
+ },
621
+ error
622
+ );
623
+ }
593
624
  }
594
625
  async doUpsert({ indexName, vectors, metadata, ids }) {
595
626
  const tx = await this.turso.transaction("write");
@@ -632,7 +663,19 @@ var LibSQLVector = class extends MastraVector {
632
663
  }
633
664
  }
634
665
  createIndex(args) {
635
- return this.executeWriteOperationWithRetry(() => this.doCreateIndex(args));
666
+ try {
667
+ return this.executeWriteOperationWithRetry(() => this.doCreateIndex(args));
668
+ } catch (error) {
669
+ throw new MastraError(
670
+ {
671
+ id: "LIBSQL_VECTOR_CREATE_INDEX_FAILED",
672
+ domain: ErrorDomain.STORAGE,
673
+ category: ErrorCategory.THIRD_PARTY,
674
+ details: { indexName: args.indexName, dimension: args.dimension }
675
+ },
676
+ error
677
+ );
678
+ }
636
679
  }
637
680
  async doCreateIndex({ indexName, dimension }) {
638
681
  if (!Number.isInteger(dimension) || dimension <= 0) {
@@ -659,7 +702,19 @@ var LibSQLVector = class extends MastraVector {
659
702
  });
660
703
  }
661
704
  deleteIndex(args) {
662
- return this.executeWriteOperationWithRetry(() => this.doDeleteIndex(args));
705
+ try {
706
+ return this.executeWriteOperationWithRetry(() => this.doDeleteIndex(args));
707
+ } catch (error) {
708
+ throw new MastraError(
709
+ {
710
+ id: "LIBSQL_VECTOR_DELETE_INDEX_FAILED",
711
+ domain: ErrorDomain.STORAGE,
712
+ category: ErrorCategory.THIRD_PARTY,
713
+ details: { indexName: args.indexName }
714
+ },
715
+ error
716
+ );
717
+ }
663
718
  }
664
719
  async doDeleteIndex({ indexName }) {
665
720
  const parsedIndexName = parseSqlIdentifier(indexName, "index name");
@@ -681,7 +736,14 @@ var LibSQLVector = class extends MastraVector {
681
736
  });
682
737
  return result.rows.map((row) => row.name);
683
738
  } catch (error) {
684
- throw new Error(`Failed to list vector tables: ${error.message}`);
739
+ throw new MastraError(
740
+ {
741
+ id: "LIBSQL_VECTOR_LIST_INDEXES_FAILED",
742
+ domain: ErrorDomain.STORAGE,
743
+ category: ErrorCategory.THIRD_PARTY
744
+ },
745
+ error
746
+ );
685
747
  }
686
748
  }
687
749
  /**
@@ -722,7 +784,15 @@ var LibSQLVector = class extends MastraVector {
722
784
  metric
723
785
  };
724
786
  } catch (e) {
725
- throw new Error(`Failed to describe vector table: ${e.message}`);
787
+ throw new MastraError(
788
+ {
789
+ id: "LIBSQL_VECTOR_DESCRIBE_INDEX_FAILED",
790
+ domain: ErrorDomain.STORAGE,
791
+ category: ErrorCategory.THIRD_PARTY,
792
+ details: { indexName }
793
+ },
794
+ e
795
+ );
726
796
  }
727
797
  }
728
798
  /**
@@ -752,7 +822,13 @@ var LibSQLVector = class extends MastraVector {
752
822
  args.push(JSON.stringify(update.metadata));
753
823
  }
754
824
  if (updates.length === 0) {
755
- throw new Error("No updates provided");
825
+ throw new MastraError({
826
+ id: "LIBSQL_VECTOR_UPDATE_VECTOR_INVALID_ARGS",
827
+ domain: ErrorDomain.STORAGE,
828
+ category: ErrorCategory.USER,
829
+ details: { indexName, id },
830
+ text: "No updates provided"
831
+ });
756
832
  }
757
833
  args.push(id);
758
834
  const query = `
@@ -760,10 +836,22 @@ var LibSQLVector = class extends MastraVector {
760
836
  SET ${updates.join(", ")}
761
837
  WHERE vector_id = ?;
762
838
  `;
763
- await this.turso.execute({
764
- sql: query,
765
- args
766
- });
839
+ try {
840
+ await this.turso.execute({
841
+ sql: query,
842
+ args
843
+ });
844
+ } catch (error) {
845
+ throw new MastraError(
846
+ {
847
+ id: "LIBSQL_VECTOR_UPDATE_VECTOR_FAILED",
848
+ domain: ErrorDomain.STORAGE,
849
+ category: ErrorCategory.THIRD_PARTY,
850
+ details: { indexName, id }
851
+ },
852
+ error
853
+ );
854
+ }
767
855
  }
768
856
  /**
769
857
  * Deletes a vector by its ID.
@@ -773,7 +861,19 @@ var LibSQLVector = class extends MastraVector {
773
861
  * @throws Will throw an error if the deletion operation fails.
774
862
  */
775
863
  deleteVector(args) {
776
- return this.executeWriteOperationWithRetry(() => this.doDeleteVector(args));
864
+ try {
865
+ return this.executeWriteOperationWithRetry(() => this.doDeleteVector(args));
866
+ } catch (error) {
867
+ throw new MastraError(
868
+ {
869
+ id: "LIBSQL_VECTOR_DELETE_VECTOR_FAILED",
870
+ domain: ErrorDomain.STORAGE,
871
+ category: ErrorCategory.THIRD_PARTY,
872
+ details: { indexName: args.indexName, id: args.id }
873
+ },
874
+ error
875
+ );
876
+ }
777
877
  }
778
878
  async doDeleteVector({ indexName, id }) {
779
879
  const parsedIndexName = parseSqlIdentifier(indexName, "index name");
@@ -783,7 +883,19 @@ var LibSQLVector = class extends MastraVector {
783
883
  });
784
884
  }
785
885
  truncateIndex(args) {
786
- return this.executeWriteOperationWithRetry(() => this._doTruncateIndex(args));
886
+ try {
887
+ return this.executeWriteOperationWithRetry(() => this._doTruncateIndex(args));
888
+ } catch (error) {
889
+ throw new MastraError(
890
+ {
891
+ id: "LIBSQL_VECTOR_TRUNCATE_INDEX_FAILED",
892
+ domain: ErrorDomain.STORAGE,
893
+ category: ErrorCategory.THIRD_PARTY,
894
+ details: { indexName: args.indexName }
895
+ },
896
+ error
897
+ );
898
+ }
787
899
  }
788
900
  async _doTruncateIndex({ indexName }) {
789
901
  await this.turso.execute({
@@ -850,8 +962,17 @@ var LibSQLStore = class extends MastraStorage {
850
962
  const sql = this.getCreateTableSQL(tableName, schema);
851
963
  await this.client.execute(sql);
852
964
  } catch (error) {
853
- this.logger.error(`Error creating table ${tableName}: ${error}`);
854
- throw error;
965
+ throw new MastraError(
966
+ {
967
+ id: "LIBSQL_STORE_CREATE_TABLE_FAILED",
968
+ domain: ErrorDomain.STORAGE,
969
+ category: ErrorCategory.THIRD_PARTY,
970
+ details: {
971
+ tableName
972
+ }
973
+ },
974
+ error
975
+ );
855
976
  }
856
977
  }
857
978
  getSqlType(type) {
@@ -894,10 +1015,17 @@ var LibSQLStore = class extends MastraStorage {
894
1015
  }
895
1016
  }
896
1017
  } catch (error) {
897
- this.logger?.error?.(
898
- `Error altering table ${tableName}: ${error instanceof Error ? error.message : String(error)}`
1018
+ throw new MastraError(
1019
+ {
1020
+ id: "LIBSQL_STORE_ALTER_TABLE_FAILED",
1021
+ domain: ErrorDomain.STORAGE,
1022
+ category: ErrorCategory.THIRD_PARTY,
1023
+ details: {
1024
+ tableName
1025
+ }
1026
+ },
1027
+ error
899
1028
  );
900
- throw new Error(`Failed to alter table ${tableName}: ${error}`);
901
1029
  }
902
1030
  }
903
1031
  async clearTable({ tableName }) {
@@ -905,9 +1033,19 @@ var LibSQLStore = class extends MastraStorage {
905
1033
  try {
906
1034
  await this.client.execute(`DELETE FROM ${parsedTableName}`);
907
1035
  } catch (e) {
908
- if (e instanceof Error) {
909
- this.logger.error(e.message);
910
- }
1036
+ const mastraError = new MastraError(
1037
+ {
1038
+ id: "LIBSQL_STORE_CLEAR_TABLE_FAILED",
1039
+ domain: ErrorDomain.STORAGE,
1040
+ category: ErrorCategory.THIRD_PARTY,
1041
+ details: {
1042
+ tableName
1043
+ }
1044
+ },
1045
+ e
1046
+ );
1047
+ this.logger?.trackException?.(mastraError);
1048
+ this.logger?.error?.(mastraError.toString());
911
1049
  }
912
1050
  }
913
1051
  prepareStatement({ tableName, record }) {
@@ -966,7 +1104,19 @@ var LibSQLStore = class extends MastraStorage {
966
1104
  return this.executeWriteOperationWithRetry(
967
1105
  () => this.doBatchInsert(args),
968
1106
  `batch insert into table ${args.tableName}`
969
- );
1107
+ ).catch((error) => {
1108
+ throw new MastraError(
1109
+ {
1110
+ id: "LIBSQL_STORE_BATCH_INSERT_FAILED",
1111
+ domain: ErrorDomain.STORAGE,
1112
+ category: ErrorCategory.THIRD_PARTY,
1113
+ details: {
1114
+ tableName: args.tableName
1115
+ }
1116
+ },
1117
+ error
1118
+ );
1119
+ });
970
1120
  }
971
1121
  async doBatchInsert({
972
1122
  tableName,
@@ -1001,17 +1151,29 @@ var LibSQLStore = class extends MastraStorage {
1001
1151
  return parsed;
1002
1152
  }
1003
1153
  async getThreadById({ threadId }) {
1004
- const result = await this.load({
1005
- tableName: TABLE_THREADS,
1006
- keys: { id: threadId }
1007
- });
1008
- if (!result) {
1009
- return null;
1154
+ try {
1155
+ const result = await this.load({
1156
+ tableName: TABLE_THREADS,
1157
+ keys: { id: threadId }
1158
+ });
1159
+ if (!result) {
1160
+ return null;
1161
+ }
1162
+ return {
1163
+ ...result,
1164
+ metadata: typeof result.metadata === "string" ? JSON.parse(result.metadata) : result.metadata
1165
+ };
1166
+ } catch (error) {
1167
+ throw new MastraError(
1168
+ {
1169
+ id: "LIBSQL_STORE_GET_THREAD_BY_ID_FAILED",
1170
+ domain: ErrorDomain.STORAGE,
1171
+ category: ErrorCategory.THIRD_PARTY,
1172
+ details: { threadId }
1173
+ },
1174
+ error
1175
+ );
1010
1176
  }
1011
- return {
1012
- ...result,
1013
- metadata: typeof result.metadata === "string" ? JSON.parse(result.metadata) : result.metadata
1014
- };
1015
1177
  }
1016
1178
  /**
1017
1179
  * @deprecated use getThreadsByResourceIdPaginated instead for paginated results.
@@ -1040,7 +1202,17 @@ var LibSQLStore = class extends MastraStorage {
1040
1202
  }
1041
1203
  return result.rows.map(mapRowToStorageThreadType);
1042
1204
  } catch (error) {
1043
- this.logger.error(`Error getting threads for resource ${resourceId}:`, error);
1205
+ const mastraError = new MastraError(
1206
+ {
1207
+ id: "LIBSQL_STORE_GET_THREADS_BY_RESOURCE_ID_FAILED",
1208
+ domain: ErrorDomain.STORAGE,
1209
+ category: ErrorCategory.THIRD_PARTY,
1210
+ details: { resourceId }
1211
+ },
1212
+ error
1213
+ );
1214
+ this.logger?.trackException?.(mastraError);
1215
+ this.logger?.error?.(mastraError.toString());
1044
1216
  return [];
1045
1217
  }
1046
1218
  }
@@ -1087,19 +1259,44 @@ var LibSQLStore = class extends MastraStorage {
1087
1259
  hasMore: currentOffset + threads.length < total
1088
1260
  };
1089
1261
  } catch (error) {
1090
- this.logger.error(`Error getting threads for resource ${resourceId}:`, error);
1262
+ const mastraError = new MastraError(
1263
+ {
1264
+ id: "LIBSQL_STORE_GET_THREADS_BY_RESOURCE_ID_FAILED",
1265
+ domain: ErrorDomain.STORAGE,
1266
+ category: ErrorCategory.THIRD_PARTY,
1267
+ details: { resourceId }
1268
+ },
1269
+ error
1270
+ );
1271
+ this.logger?.trackException?.(mastraError);
1272
+ this.logger?.error?.(mastraError.toString());
1091
1273
  return { threads: [], total: 0, page, perPage, hasMore: false };
1092
1274
  }
1093
1275
  }
1094
1276
  async saveThread({ thread }) {
1095
- await this.insert({
1096
- tableName: TABLE_THREADS,
1097
- record: {
1098
- ...thread,
1099
- metadata: JSON.stringify(thread.metadata)
1100
- }
1101
- });
1102
- return thread;
1277
+ try {
1278
+ await this.insert({
1279
+ tableName: TABLE_THREADS,
1280
+ record: {
1281
+ ...thread,
1282
+ metadata: JSON.stringify(thread.metadata)
1283
+ }
1284
+ });
1285
+ return thread;
1286
+ } catch (error) {
1287
+ const mastraError = new MastraError(
1288
+ {
1289
+ id: "LIBSQL_STORE_SAVE_THREAD_FAILED",
1290
+ domain: ErrorDomain.STORAGE,
1291
+ category: ErrorCategory.THIRD_PARTY,
1292
+ details: { threadId: thread.id }
1293
+ },
1294
+ error
1295
+ );
1296
+ this.logger?.trackException?.(mastraError);
1297
+ this.logger?.error?.(mastraError.toString());
1298
+ throw mastraError;
1299
+ }
1103
1300
  }
1104
1301
  async updateThread({
1105
1302
  id,
@@ -1108,7 +1305,13 @@ var LibSQLStore = class extends MastraStorage {
1108
1305
  }) {
1109
1306
  const thread = await this.getThreadById({ threadId: id });
1110
1307
  if (!thread) {
1111
- throw new Error(`Thread ${id} not found`);
1308
+ throw new MastraError({
1309
+ id: "LIBSQL_STORE_UPDATE_THREAD_FAILED_THREAD_NOT_FOUND",
1310
+ domain: ErrorDomain.STORAGE,
1311
+ category: ErrorCategory.USER,
1312
+ text: `Thread ${id} not found`,
1313
+ details: { threadId: id }
1314
+ });
1112
1315
  }
1113
1316
  const updatedThread = {
1114
1317
  ...thread,
@@ -1118,21 +1321,46 @@ var LibSQLStore = class extends MastraStorage {
1118
1321
  ...metadata
1119
1322
  }
1120
1323
  };
1121
- await this.client.execute({
1122
- sql: `UPDATE ${TABLE_THREADS} SET title = ?, metadata = ? WHERE id = ?`,
1123
- args: [title, JSON.stringify(updatedThread.metadata), id]
1124
- });
1125
- return updatedThread;
1324
+ try {
1325
+ await this.client.execute({
1326
+ sql: `UPDATE ${TABLE_THREADS} SET title = ?, metadata = ? WHERE id = ?`,
1327
+ args: [title, JSON.stringify(updatedThread.metadata), id]
1328
+ });
1329
+ return updatedThread;
1330
+ } catch (error) {
1331
+ throw new MastraError(
1332
+ {
1333
+ id: "LIBSQL_STORE_UPDATE_THREAD_FAILED",
1334
+ domain: ErrorDomain.STORAGE,
1335
+ category: ErrorCategory.THIRD_PARTY,
1336
+ text: `Failed to update thread ${id}`,
1337
+ details: { threadId: id }
1338
+ },
1339
+ error
1340
+ );
1341
+ }
1126
1342
  }
1127
1343
  async deleteThread({ threadId }) {
1128
- await this.client.execute({
1129
- sql: `DELETE FROM ${TABLE_MESSAGES} WHERE thread_id = ?`,
1130
- args: [threadId]
1131
- });
1132
- await this.client.execute({
1133
- sql: `DELETE FROM ${TABLE_THREADS} WHERE id = ?`,
1134
- args: [threadId]
1135
- });
1344
+ try {
1345
+ await this.client.execute({
1346
+ sql: `DELETE FROM ${TABLE_MESSAGES} WHERE thread_id = ?`,
1347
+ args: [threadId]
1348
+ });
1349
+ await this.client.execute({
1350
+ sql: `DELETE FROM ${TABLE_THREADS} WHERE id = ?`,
1351
+ args: [threadId]
1352
+ });
1353
+ } catch (error) {
1354
+ throw new MastraError(
1355
+ {
1356
+ id: "LIBSQL_STORE_DELETE_THREAD_FAILED",
1357
+ domain: ErrorDomain.STORAGE,
1358
+ category: ErrorCategory.THIRD_PARTY,
1359
+ details: { threadId }
1360
+ },
1361
+ error
1362
+ );
1363
+ }
1136
1364
  }
1137
1365
  parseRow(row) {
1138
1366
  let content = row.content;
@@ -1205,7 +1433,7 @@ var LibSQLStore = class extends MastraStorage {
1205
1433
  }) {
1206
1434
  try {
1207
1435
  const messages = [];
1208
- const limit = typeof selectBy?.last === `number` ? selectBy.last : 40;
1436
+ const limit = this.resolveMessageLimit({ last: selectBy?.last, defaultLimit: 40 });
1209
1437
  if (selectBy?.include?.length) {
1210
1438
  const includeMessages = await this._getIncludedMessages({ threadId, selectBy });
1211
1439
  if (includeMessages) {
@@ -1238,8 +1466,15 @@ var LibSQLStore = class extends MastraStorage {
1238
1466
  if (format === `v2`) return list.get.all.v2();
1239
1467
  return list.get.all.v1();
1240
1468
  } catch (error) {
1241
- this.logger.error("Error getting messages:", error);
1242
- throw error;
1469
+ throw new MastraError(
1470
+ {
1471
+ id: "LIBSQL_STORE_GET_MESSAGES_FAILED",
1472
+ domain: ErrorDomain.STORAGE,
1473
+ category: ErrorCategory.THIRD_PARTY,
1474
+ details: { threadId }
1475
+ },
1476
+ error
1477
+ );
1243
1478
  }
1244
1479
  }
1245
1480
  async getMessagesPaginated(args) {
@@ -1249,9 +1484,21 @@ var LibSQLStore = class extends MastraStorage {
1249
1484
  const toDate = dateRange?.end;
1250
1485
  const messages = [];
1251
1486
  if (selectBy?.include?.length) {
1252
- const includeMessages = await this._getIncludedMessages({ threadId, selectBy });
1253
- if (includeMessages) {
1254
- messages.push(...includeMessages);
1487
+ try {
1488
+ const includeMessages = await this._getIncludedMessages({ threadId, selectBy });
1489
+ if (includeMessages) {
1490
+ messages.push(...includeMessages);
1491
+ }
1492
+ } catch (error) {
1493
+ throw new MastraError(
1494
+ {
1495
+ id: "LIBSQL_STORE_GET_MESSAGES_PAGINATED_GET_INCLUDE_MESSAGES_FAILED",
1496
+ domain: ErrorDomain.STORAGE,
1497
+ category: ErrorCategory.THIRD_PARTY,
1498
+ details: { threadId }
1499
+ },
1500
+ error
1501
+ );
1255
1502
  }
1256
1503
  }
1257
1504
  try {
@@ -1295,7 +1542,17 @@ var LibSQLStore = class extends MastraStorage {
1295
1542
  hasMore: currentOffset + messages.length < total
1296
1543
  };
1297
1544
  } catch (error) {
1298
- this.logger.error("Error getting paginated messages:", error);
1545
+ const mastraError = new MastraError(
1546
+ {
1547
+ id: "LIBSQL_STORE_GET_MESSAGES_PAGINATED_FAILED",
1548
+ domain: ErrorDomain.STORAGE,
1549
+ category: ErrorCategory.THIRD_PARTY,
1550
+ details: { threadId }
1551
+ },
1552
+ error
1553
+ );
1554
+ this.logger?.trackException?.(mastraError);
1555
+ this.logger?.error?.(mastraError.toString());
1299
1556
  return { messages: [], total: 0, page, perPage, hasMore: false };
1300
1557
  }
1301
1558
  }
@@ -1345,8 +1602,14 @@ var LibSQLStore = class extends MastraStorage {
1345
1602
  if (format === `v2`) return list.get.all.v2();
1346
1603
  return list.get.all.v1();
1347
1604
  } catch (error) {
1348
- this.logger.error("Failed to save messages in database: " + error?.message);
1349
- throw error;
1605
+ throw new MastraError(
1606
+ {
1607
+ id: "LIBSQL_STORE_SAVE_MESSAGES_FAILED",
1608
+ domain: ErrorDomain.STORAGE,
1609
+ category: ErrorCategory.THIRD_PARTY
1610
+ },
1611
+ error
1612
+ );
1350
1613
  }
1351
1614
  }
1352
1615
  async updateMessages({
@@ -1461,8 +1724,15 @@ var LibSQLStore = class extends MastraStorage {
1461
1724
  if (error instanceof Error && error.message.includes("no such table")) {
1462
1725
  return [];
1463
1726
  }
1464
- this.logger.error("Failed to get evals for the specified agent: " + error?.message);
1465
- throw error;
1727
+ throw new MastraError(
1728
+ {
1729
+ id: "LIBSQL_STORE_GET_EVALS_BY_AGENT_NAME_FAILED",
1730
+ domain: ErrorDomain.STORAGE,
1731
+ category: ErrorCategory.THIRD_PARTY,
1732
+ details: { agentName }
1733
+ },
1734
+ error
1735
+ );
1466
1736
  }
1467
1737
  }
1468
1738
  async getEvals(options = {}) {
@@ -1489,33 +1759,44 @@ var LibSQLStore = class extends MastraStorage {
1489
1759
  queryParams.push(toDate.toISOString());
1490
1760
  }
1491
1761
  const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
1492
- const countResult = await this.client.execute({
1493
- sql: `SELECT COUNT(*) as count FROM ${TABLE_EVALS} ${whereClause}`,
1494
- args: queryParams
1495
- });
1496
- const total = Number(countResult.rows?.[0]?.count ?? 0);
1497
- const currentOffset = page * perPage;
1498
- const hasMore = currentOffset + perPage < total;
1499
- if (total === 0) {
1762
+ try {
1763
+ const countResult = await this.client.execute({
1764
+ sql: `SELECT COUNT(*) as count FROM ${TABLE_EVALS} ${whereClause}`,
1765
+ args: queryParams
1766
+ });
1767
+ const total = Number(countResult.rows?.[0]?.count ?? 0);
1768
+ const currentOffset = page * perPage;
1769
+ const hasMore = currentOffset + perPage < total;
1770
+ if (total === 0) {
1771
+ return {
1772
+ evals: [],
1773
+ total: 0,
1774
+ page,
1775
+ perPage,
1776
+ hasMore: false
1777
+ };
1778
+ }
1779
+ const dataResult = await this.client.execute({
1780
+ sql: `SELECT * FROM ${TABLE_EVALS} ${whereClause} ORDER BY created_at DESC LIMIT ? OFFSET ?`,
1781
+ args: [...queryParams, perPage, currentOffset]
1782
+ });
1500
1783
  return {
1501
- evals: [],
1502
- total: 0,
1784
+ evals: dataResult.rows?.map((row) => this.transformEvalRow(row)) ?? [],
1785
+ total,
1503
1786
  page,
1504
1787
  perPage,
1505
- hasMore: false
1788
+ hasMore
1506
1789
  };
1790
+ } catch (error) {
1791
+ throw new MastraError(
1792
+ {
1793
+ id: "LIBSQL_STORE_GET_EVALS_FAILED",
1794
+ domain: ErrorDomain.STORAGE,
1795
+ category: ErrorCategory.THIRD_PARTY
1796
+ },
1797
+ error
1798
+ );
1507
1799
  }
1508
- const dataResult = await this.client.execute({
1509
- sql: `SELECT * FROM ${TABLE_EVALS} ${whereClause} ORDER BY created_at DESC LIMIT ? OFFSET ?`,
1510
- args: [...queryParams, perPage, currentOffset]
1511
- });
1512
- return {
1513
- evals: dataResult.rows?.map((row) => this.transformEvalRow(row)) ?? [],
1514
- total,
1515
- page,
1516
- perPage,
1517
- hasMore
1518
- };
1519
1800
  }
1520
1801
  /**
1521
1802
  * @deprecated use getTracesPaginated instead.
@@ -1527,8 +1808,19 @@ var LibSQLStore = class extends MastraStorage {
1527
1808
  end: args.toDate
1528
1809
  };
1529
1810
  }
1530
- const result = await this.getTracesPaginated(args);
1531
- return result.traces;
1811
+ try {
1812
+ const result = await this.getTracesPaginated(args);
1813
+ return result.traces;
1814
+ } catch (error) {
1815
+ throw new MastraError(
1816
+ {
1817
+ id: "LIBSQL_STORE_GET_TRACES_FAILED",
1818
+ domain: ErrorDomain.STORAGE,
1819
+ category: ErrorCategory.THIRD_PARTY
1820
+ },
1821
+ error
1822
+ );
1823
+ }
1532
1824
  }
1533
1825
  async getTracesPaginated(args) {
1534
1826
  const { name, scope, page = 0, perPage = 100, attributes, filters, dateRange } = args;
@@ -1566,49 +1858,60 @@ var LibSQLStore = class extends MastraStorage {
1566
1858
  queryArgs.push(toDate.toISOString());
1567
1859
  }
1568
1860
  const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
1569
- const countResult = await this.client.execute({
1570
- sql: `SELECT COUNT(*) as count FROM ${TABLE_TRACES} ${whereClause}`,
1571
- args: queryArgs
1572
- });
1573
- const total = Number(countResult.rows?.[0]?.count ?? 0);
1574
- if (total === 0) {
1861
+ try {
1862
+ const countResult = await this.client.execute({
1863
+ sql: `SELECT COUNT(*) as count FROM ${TABLE_TRACES} ${whereClause}`,
1864
+ args: queryArgs
1865
+ });
1866
+ const total = Number(countResult.rows?.[0]?.count ?? 0);
1867
+ if (total === 0) {
1868
+ return {
1869
+ traces: [],
1870
+ total: 0,
1871
+ page,
1872
+ perPage,
1873
+ hasMore: false
1874
+ };
1875
+ }
1876
+ const dataResult = await this.client.execute({
1877
+ sql: `SELECT * FROM ${TABLE_TRACES} ${whereClause} ORDER BY "startTime" DESC LIMIT ? OFFSET ?`,
1878
+ args: [...queryArgs, perPage, currentOffset]
1879
+ });
1880
+ const traces = dataResult.rows?.map(
1881
+ (row) => ({
1882
+ id: row.id,
1883
+ parentSpanId: row.parentSpanId,
1884
+ traceId: row.traceId,
1885
+ name: row.name,
1886
+ scope: row.scope,
1887
+ kind: row.kind,
1888
+ status: safelyParseJSON(row.status),
1889
+ events: safelyParseJSON(row.events),
1890
+ links: safelyParseJSON(row.links),
1891
+ attributes: safelyParseJSON(row.attributes),
1892
+ startTime: row.startTime,
1893
+ endTime: row.endTime,
1894
+ other: safelyParseJSON(row.other),
1895
+ createdAt: row.createdAt
1896
+ })
1897
+ ) ?? [];
1575
1898
  return {
1576
- traces: [],
1577
- total: 0,
1899
+ traces,
1900
+ total,
1578
1901
  page,
1579
1902
  perPage,
1580
- hasMore: false
1903
+ hasMore: currentOffset + traces.length < total
1581
1904
  };
1905
+ } catch (error) {
1906
+ throw new MastraError(
1907
+ {
1908
+ id: "LIBSQL_STORE_GET_TRACES_PAGINATED_FAILED",
1909
+ domain: ErrorDomain.STORAGE,
1910
+ category: ErrorCategory.THIRD_PARTY
1911
+ },
1912
+ error
1913
+ );
1582
1914
  }
1583
- const dataResult = await this.client.execute({
1584
- sql: `SELECT * FROM ${TABLE_TRACES} ${whereClause} ORDER BY "startTime" DESC LIMIT ? OFFSET ?`,
1585
- args: [...queryArgs, perPage, currentOffset]
1586
- });
1587
- const traces = dataResult.rows?.map(
1588
- (row) => ({
1589
- id: row.id,
1590
- parentSpanId: row.parentSpanId,
1591
- traceId: row.traceId,
1592
- name: row.name,
1593
- scope: row.scope,
1594
- kind: row.kind,
1595
- status: safelyParseJSON(row.status),
1596
- events: safelyParseJSON(row.events),
1597
- links: safelyParseJSON(row.links),
1598
- attributes: safelyParseJSON(row.attributes),
1599
- startTime: row.startTime,
1600
- endTime: row.endTime,
1601
- other: safelyParseJSON(row.other),
1602
- createdAt: row.createdAt
1603
- })
1604
- ) ?? [];
1605
- return {
1606
- traces,
1607
- total,
1608
- page,
1609
- perPage,
1610
- hasMore: currentOffset + traces.length < total
1611
- };
1612
1915
  }
1613
1916
  async getWorkflowRuns({
1614
1917
  workflowName,
@@ -1658,8 +1961,14 @@ var LibSQLStore = class extends MastraStorage {
1658
1961
  const runs = (result.rows || []).map((row) => this.parseWorkflowRun(row));
1659
1962
  return { runs, total: total || runs.length };
1660
1963
  } catch (error) {
1661
- console.error("Error getting workflow runs:", error);
1662
- throw error;
1964
+ throw new MastraError(
1965
+ {
1966
+ id: "LIBSQL_STORE_GET_WORKFLOW_RUNS_FAILED",
1967
+ domain: ErrorDomain.STORAGE,
1968
+ category: ErrorCategory.THIRD_PARTY
1969
+ },
1970
+ error
1971
+ );
1663
1972
  }
1664
1973
  }
1665
1974
  async getWorkflowRunById({
@@ -1677,14 +1986,25 @@ var LibSQLStore = class extends MastraStorage {
1677
1986
  args.push(workflowName);
1678
1987
  }
1679
1988
  const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
1680
- const result = await this.client.execute({
1681
- sql: `SELECT * FROM ${TABLE_WORKFLOW_SNAPSHOT} ${whereClause}`,
1682
- args
1683
- });
1684
- if (!result.rows?.[0]) {
1685
- return null;
1989
+ try {
1990
+ const result = await this.client.execute({
1991
+ sql: `SELECT * FROM ${TABLE_WORKFLOW_SNAPSHOT} ${whereClause}`,
1992
+ args
1993
+ });
1994
+ if (!result.rows?.[0]) {
1995
+ return null;
1996
+ }
1997
+ return this.parseWorkflowRun(result.rows[0]);
1998
+ } catch (error) {
1999
+ throw new MastraError(
2000
+ {
2001
+ id: "LIBSQL_STORE_GET_WORKFLOW_RUN_BY_ID_FAILED",
2002
+ domain: ErrorDomain.STORAGE,
2003
+ category: ErrorCategory.THIRD_PARTY
2004
+ },
2005
+ error
2006
+ );
1686
2007
  }
1687
- return this.parseWorkflowRun(result.rows[0]);
1688
2008
  }
1689
2009
  async hasColumn(table, column) {
1690
2010
  const result = await this.client.execute({