@mastra/mssql 1.0.0-beta.2 → 1.0.0-beta.4

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.cjs CHANGED
@@ -32,24 +32,62 @@ function buildDateRangeFilter(dateRange, fieldName) {
32
32
  }
33
33
  return filters;
34
34
  }
35
+ function isInOperator(value) {
36
+ return typeof value === "object" && value !== null && "$in" in value && Array.isArray(value.$in);
37
+ }
35
38
  function prepareWhereClause(filters, _schema) {
36
39
  const conditions = [];
37
40
  const params = {};
38
41
  let paramIndex = 1;
39
42
  Object.entries(filters).forEach(([key, value]) => {
40
43
  if (value === void 0) return;
41
- const paramName = `p${paramIndex++}`;
42
44
  if (key.endsWith("_gte")) {
45
+ const paramName = `p${paramIndex++}`;
43
46
  const fieldName = key.slice(0, -4);
44
47
  conditions.push(`[${utils.parseSqlIdentifier(fieldName, "field name")}] >= @${paramName}`);
45
48
  params[paramName] = value instanceof Date ? value.toISOString() : value;
46
49
  } else if (key.endsWith("_lte")) {
50
+ const paramName = `p${paramIndex++}`;
47
51
  const fieldName = key.slice(0, -4);
48
52
  conditions.push(`[${utils.parseSqlIdentifier(fieldName, "field name")}] <= @${paramName}`);
49
53
  params[paramName] = value instanceof Date ? value.toISOString() : value;
50
54
  } else if (value === null) {
51
55
  conditions.push(`[${utils.parseSqlIdentifier(key, "field name")}] IS NULL`);
56
+ } else if (isInOperator(value)) {
57
+ const inValues = value.$in;
58
+ if (inValues.length === 0) {
59
+ conditions.push("1 = 0");
60
+ } else if (inValues.length === 1) {
61
+ const paramName = `p${paramIndex++}`;
62
+ conditions.push(`[${utils.parseSqlIdentifier(key, "field name")}] = @${paramName}`);
63
+ params[paramName] = inValues[0] instanceof Date ? inValues[0].toISOString() : inValues[0];
64
+ } else {
65
+ const inParamNames = [];
66
+ for (const item of inValues) {
67
+ const paramName = `p${paramIndex++}`;
68
+ inParamNames.push(`@${paramName}`);
69
+ params[paramName] = item instanceof Date ? item.toISOString() : item;
70
+ }
71
+ conditions.push(`[${utils.parseSqlIdentifier(key, "field name")}] IN (${inParamNames.join(", ")})`);
72
+ }
73
+ } else if (Array.isArray(value)) {
74
+ if (value.length === 0) {
75
+ conditions.push("1 = 0");
76
+ } else if (value.length === 1) {
77
+ const paramName = `p${paramIndex++}`;
78
+ conditions.push(`[${utils.parseSqlIdentifier(key, "field name")}] = @${paramName}`);
79
+ params[paramName] = value[0] instanceof Date ? value[0].toISOString() : value[0];
80
+ } else {
81
+ const inParamNames = [];
82
+ for (const item of value) {
83
+ const paramName = `p${paramIndex++}`;
84
+ inParamNames.push(`@${paramName}`);
85
+ params[paramName] = item instanceof Date ? item.toISOString() : item;
86
+ }
87
+ conditions.push(`[${utils.parseSqlIdentifier(key, "field name")}] IN (${inParamNames.join(", ")})`);
88
+ }
52
89
  } else {
90
+ const paramName = `p${paramIndex++}`;
53
91
  conditions.push(`[${utils.parseSqlIdentifier(key, "field name")}] = @${paramName}`);
54
92
  params[paramName] = value instanceof Date ? value.toISOString() : value;
55
93
  }
@@ -143,7 +181,7 @@ var MemoryMSSQL = class extends storage.MemoryStorage {
143
181
  } catch (error$1) {
144
182
  throw new error.MastraError(
145
183
  {
146
- id: "MASTRA_STORAGE_MSSQL_STORE_GET_THREAD_BY_ID_FAILED",
184
+ id: storage.createStorageErrorId("MSSQL", "GET_THREAD_BY_ID", "FAILED"),
147
185
  domain: error.ErrorDomain.STORAGE,
148
186
  category: error.ErrorCategory.THIRD_PARTY,
149
187
  details: {
@@ -158,7 +196,7 @@ var MemoryMSSQL = class extends storage.MemoryStorage {
158
196
  const { resourceId, page = 0, perPage: perPageInput, orderBy } = args;
159
197
  if (page < 0) {
160
198
  throw new error.MastraError({
161
- id: "MASTRA_STORAGE_MSSQL_STORE_INVALID_PAGE",
199
+ id: storage.createStorageErrorId("MSSQL", "LIST_THREADS_BY_RESOURCE_ID", "INVALID_PAGE"),
162
200
  domain: error.ErrorDomain.STORAGE,
163
201
  category: error.ErrorCategory.USER,
164
202
  text: "Page number must be non-negative",
@@ -217,7 +255,7 @@ var MemoryMSSQL = class extends storage.MemoryStorage {
217
255
  } catch (error$1) {
218
256
  const mastraError = new error.MastraError(
219
257
  {
220
- id: "MASTRA_STORAGE_MSSQL_STORE_LIST_THREADS_BY_RESOURCE_ID_FAILED",
258
+ id: storage.createStorageErrorId("MSSQL", "LIST_THREADS_BY_RESOURCE_ID", "FAILED"),
221
259
  domain: error.ErrorDomain.STORAGE,
222
260
  category: error.ErrorCategory.THIRD_PARTY,
223
261
  details: {
@@ -270,7 +308,7 @@ var MemoryMSSQL = class extends storage.MemoryStorage {
270
308
  } catch (error$1) {
271
309
  throw new error.MastraError(
272
310
  {
273
- id: "MASTRA_STORAGE_MSSQL_STORE_SAVE_THREAD_FAILED",
311
+ id: storage.createStorageErrorId("MSSQL", "SAVE_THREAD", "FAILED"),
274
312
  domain: error.ErrorDomain.STORAGE,
275
313
  category: error.ErrorCategory.THIRD_PARTY,
276
314
  details: {
@@ -292,7 +330,7 @@ var MemoryMSSQL = class extends storage.MemoryStorage {
292
330
  const existingThread = await this.getThreadById({ threadId: id });
293
331
  if (!existingThread) {
294
332
  throw new error.MastraError({
295
- id: "MASTRA_STORAGE_MSSQL_STORE_UPDATE_THREAD_FAILED",
333
+ id: storage.createStorageErrorId("MSSQL", "UPDATE_THREAD", "NOT_FOUND"),
296
334
  domain: error.ErrorDomain.STORAGE,
297
335
  category: error.ErrorCategory.USER,
298
336
  text: `Thread ${id} not found`,
@@ -327,7 +365,7 @@ var MemoryMSSQL = class extends storage.MemoryStorage {
327
365
  }
328
366
  if (!thread) {
329
367
  throw new error.MastraError({
330
- id: "MASTRA_STORAGE_MSSQL_STORE_UPDATE_THREAD_FAILED",
368
+ id: storage.createStorageErrorId("MSSQL", "UPDATE_THREAD", "NOT_FOUND"),
331
369
  domain: error.ErrorDomain.STORAGE,
332
370
  category: error.ErrorCategory.USER,
333
371
  text: `Thread ${id} not found after update`,
@@ -346,7 +384,7 @@ var MemoryMSSQL = class extends storage.MemoryStorage {
346
384
  } catch (error$1) {
347
385
  throw new error.MastraError(
348
386
  {
349
- id: "MASTRA_STORAGE_MSSQL_STORE_UPDATE_THREAD_FAILED",
387
+ id: storage.createStorageErrorId("MSSQL", "UPDATE_THREAD", "FAILED"),
350
388
  domain: error.ErrorDomain.STORAGE,
351
389
  category: error.ErrorCategory.THIRD_PARTY,
352
390
  details: {
@@ -376,7 +414,7 @@ var MemoryMSSQL = class extends storage.MemoryStorage {
376
414
  });
377
415
  throw new error.MastraError(
378
416
  {
379
- id: "MASTRA_STORAGE_MSSQL_STORE_DELETE_THREAD_FAILED",
417
+ id: storage.createStorageErrorId("MSSQL", "DELETE_THREAD", "FAILED"),
380
418
  domain: error.ErrorDomain.STORAGE,
381
419
  category: error.ErrorCategory.THIRD_PARTY,
382
420
  details: {
@@ -387,23 +425,18 @@ var MemoryMSSQL = class extends storage.MemoryStorage {
387
425
  );
388
426
  }
389
427
  }
390
- async _getIncludedMessages({
391
- threadId,
392
- include
393
- }) {
394
- if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
395
- if (!include) return null;
428
+ async _getIncludedMessages({ include }) {
429
+ if (!include || include.length === 0) return null;
396
430
  const unionQueries = [];
397
431
  const paramValues = [];
398
432
  let paramIdx = 1;
399
433
  const paramNames = [];
434
+ const tableName = getTableName({ indexName: storage.TABLE_MESSAGES, schemaName: getSchemaName(this.schema) });
400
435
  for (const inc of include) {
401
436
  const { id, withPreviousMessages = 0, withNextMessages = 0 } = inc;
402
- const searchId = inc.threadId || threadId;
403
- const pThreadId = `@p${paramIdx}`;
404
- const pId = `@p${paramIdx + 1}`;
405
- const pPrev = `@p${paramIdx + 2}`;
406
- const pNext = `@p${paramIdx + 3}`;
437
+ const pId = `@p${paramIdx}`;
438
+ const pPrev = `@p${paramIdx + 1}`;
439
+ const pNext = `@p${paramIdx + 2}`;
407
440
  unionQueries.push(
408
441
  `
409
442
  SELECT
@@ -417,16 +450,16 @@ var MemoryMSSQL = class extends storage.MemoryStorage {
417
450
  m.seq_id
418
451
  FROM (
419
452
  SELECT *, ROW_NUMBER() OVER (ORDER BY [createdAt] ASC) as row_num
420
- FROM ${getTableName({ indexName: storage.TABLE_MESSAGES, schemaName: getSchemaName(this.schema) })}
421
- WHERE [thread_id] = ${pThreadId}
453
+ FROM ${tableName}
454
+ WHERE [thread_id] = (SELECT thread_id FROM ${tableName} WHERE id = ${pId})
422
455
  ) AS m
423
456
  WHERE m.id = ${pId}
424
457
  OR EXISTS (
425
458
  SELECT 1
426
459
  FROM (
427
460
  SELECT *, ROW_NUMBER() OVER (ORDER BY [createdAt] ASC) as row_num
428
- FROM ${getTableName({ indexName: storage.TABLE_MESSAGES, schemaName: getSchemaName(this.schema) })}
429
- WHERE [thread_id] = ${pThreadId}
461
+ FROM ${tableName}
462
+ WHERE [thread_id] = (SELECT thread_id FROM ${tableName} WHERE id = ${pId})
430
463
  ) AS target
431
464
  WHERE target.id = ${pId}
432
465
  AND (
@@ -439,9 +472,9 @@ var MemoryMSSQL = class extends storage.MemoryStorage {
439
472
  )
440
473
  `
441
474
  );
442
- paramValues.push(searchId, id, withPreviousMessages, withNextMessages);
443
- paramNames.push(`p${paramIdx}`, `p${paramIdx + 1}`, `p${paramIdx + 2}`, `p${paramIdx + 3}`);
444
- paramIdx += 4;
475
+ paramValues.push(id, withPreviousMessages, withNextMessages);
476
+ paramNames.push(`p${paramIdx}`, `p${paramIdx + 1}`, `p${paramIdx + 2}`);
477
+ paramIdx += 3;
445
478
  }
446
479
  const finalQuery = `
447
480
  SELECT * FROM (
@@ -496,7 +529,7 @@ var MemoryMSSQL = class extends storage.MemoryStorage {
496
529
  } catch (error$1) {
497
530
  const mastraError = new error.MastraError(
498
531
  {
499
- id: "MASTRA_STORAGE_MSSQL_STORE_LIST_MESSAGES_BY_ID_FAILED",
532
+ id: storage.createStorageErrorId("MSSQL", "LIST_MESSAGES_BY_ID", "FAILED"),
500
533
  domain: error.ErrorDomain.STORAGE,
501
534
  category: error.ErrorCategory.THIRD_PARTY,
502
535
  details: {
@@ -512,25 +545,26 @@ var MemoryMSSQL = class extends storage.MemoryStorage {
512
545
  }
513
546
  async listMessages(args) {
514
547
  const { threadId, resourceId, include, filter, perPage: perPageInput, page = 0, orderBy } = args;
515
- if (!threadId.trim()) {
548
+ const threadIds = Array.isArray(threadId) ? threadId : [threadId];
549
+ if (threadIds.length === 0 || threadIds.some((id) => !id.trim())) {
516
550
  throw new error.MastraError(
517
551
  {
518
- id: "STORAGE_MSSQL_LIST_MESSAGES_INVALID_THREAD_ID",
552
+ id: storage.createStorageErrorId("MSSQL", "LIST_MESSAGES", "INVALID_THREAD_ID"),
519
553
  domain: error.ErrorDomain.STORAGE,
520
554
  category: error.ErrorCategory.THIRD_PARTY,
521
- details: { threadId }
555
+ details: { threadId: Array.isArray(threadId) ? threadId.join(",") : threadId }
522
556
  },
523
- new Error("threadId must be a non-empty string")
557
+ new Error("threadId must be a non-empty string or array of non-empty strings")
524
558
  );
525
559
  }
526
560
  if (page < 0) {
527
561
  throw new error.MastraError({
528
- id: "MASTRA_STORAGE_MSSQL_STORE_INVALID_PAGE",
562
+ id: storage.createStorageErrorId("MSSQL", "LIST_MESSAGES", "INVALID_PAGE"),
529
563
  domain: error.ErrorDomain.STORAGE,
530
564
  category: error.ErrorCategory.USER,
531
565
  text: "Page number must be non-negative",
532
566
  details: {
533
- threadId,
567
+ threadId: Array.isArray(threadId) ? threadId.join(",") : threadId,
534
568
  page
535
569
  }
536
570
  });
@@ -543,7 +577,7 @@ var MemoryMSSQL = class extends storage.MemoryStorage {
543
577
  const tableName = getTableName({ indexName: storage.TABLE_MESSAGES, schemaName: getSchemaName(this.schema) });
544
578
  const baseQuery = `SELECT seq_id, id, content, role, type, [createdAt], thread_id AS threadId, resourceId FROM ${tableName}`;
545
579
  const filters = {
546
- thread_id: threadId,
580
+ thread_id: threadIds.length === 1 ? threadIds[0] : { $in: threadIds },
547
581
  ...resourceId ? { resourceId } : {},
548
582
  ...buildDateRangeFilter(filter?.dateRange, "createdAt")
549
583
  };
@@ -587,7 +621,7 @@ var MemoryMSSQL = class extends storage.MemoryStorage {
587
621
  }
588
622
  if (include?.length) {
589
623
  const messageIds = new Set(messages.map((m) => m.id));
590
- const includeMessages = await this._getIncludedMessages({ threadId, include });
624
+ const includeMessages = await this._getIncludedMessages({ include });
591
625
  includeMessages?.forEach((msg) => {
592
626
  if (!messageIds.has(msg.id)) {
593
627
  messages.push(msg);
@@ -610,7 +644,8 @@ var MemoryMSSQL = class extends storage.MemoryStorage {
610
644
  const seqB = seqById.get(b.id);
611
645
  return seqA != null && seqB != null ? (seqA - seqB) * mult : a.id.localeCompare(b.id);
612
646
  });
613
- const returnedThreadMessageCount = finalMessages.filter((m) => m.threadId === threadId).length;
647
+ const threadIdSet = new Set(threadIds);
648
+ const returnedThreadMessageCount = finalMessages.filter((m) => m.threadId && threadIdSet.has(m.threadId)).length;
614
649
  const hasMore = perPageInput !== false && returnedThreadMessageCount < total && offset + perPage < total;
615
650
  return {
616
651
  messages: finalMessages,
@@ -622,11 +657,11 @@ var MemoryMSSQL = class extends storage.MemoryStorage {
622
657
  } catch (error$1) {
623
658
  const mastraError = new error.MastraError(
624
659
  {
625
- id: "MASTRA_STORAGE_MSSQL_STORE_LIST_MESSAGES_FAILED",
660
+ id: storage.createStorageErrorId("MSSQL", "LIST_MESSAGES", "FAILED"),
626
661
  domain: error.ErrorDomain.STORAGE,
627
662
  category: error.ErrorCategory.THIRD_PARTY,
628
663
  details: {
629
- threadId,
664
+ threadId: Array.isArray(threadId) ? threadId.join(",") : threadId,
630
665
  resourceId: resourceId ?? ""
631
666
  }
632
667
  },
@@ -648,7 +683,7 @@ var MemoryMSSQL = class extends storage.MemoryStorage {
648
683
  const threadId = messages[0]?.threadId;
649
684
  if (!threadId) {
650
685
  throw new error.MastraError({
651
- id: "MASTRA_STORAGE_MSSQL_STORE_SAVE_MESSAGES_FAILED",
686
+ id: storage.createStorageErrorId("MSSQL", "SAVE_MESSAGES", "INVALID_THREAD_ID"),
652
687
  domain: error.ErrorDomain.STORAGE,
653
688
  category: error.ErrorCategory.THIRD_PARTY,
654
689
  text: `Thread ID is required`
@@ -657,7 +692,7 @@ var MemoryMSSQL = class extends storage.MemoryStorage {
657
692
  const thread = await this.getThreadById({ threadId });
658
693
  if (!thread) {
659
694
  throw new error.MastraError({
660
- id: "MASTRA_STORAGE_MSSQL_STORE_SAVE_MESSAGES_FAILED",
695
+ id: storage.createStorageErrorId("MSSQL", "SAVE_MESSAGES", "THREAD_NOT_FOUND"),
661
696
  domain: error.ErrorDomain.STORAGE,
662
697
  category: error.ErrorCategory.THIRD_PARTY,
663
698
  text: `Thread ${threadId} not found`,
@@ -730,7 +765,7 @@ var MemoryMSSQL = class extends storage.MemoryStorage {
730
765
  } catch (error$1) {
731
766
  throw new error.MastraError(
732
767
  {
733
- id: "MASTRA_STORAGE_MSSQL_STORE_SAVE_MESSAGES_FAILED",
768
+ id: storage.createStorageErrorId("MSSQL", "SAVE_MESSAGES", "FAILED"),
734
769
  domain: error.ErrorDomain.STORAGE,
735
770
  category: error.ErrorCategory.THIRD_PARTY,
736
771
  details: { threadId }
@@ -821,7 +856,7 @@ var MemoryMSSQL = class extends storage.MemoryStorage {
821
856
  await transaction.rollback();
822
857
  throw new error.MastraError(
823
858
  {
824
- id: "MASTRA_STORAGE_MSSQL_UPDATE_MESSAGES_FAILED",
859
+ id: storage.createStorageErrorId("MSSQL", "UPDATE_MESSAGES", "FAILED"),
825
860
  domain: error.ErrorDomain.STORAGE,
826
861
  category: error.ErrorCategory.THIRD_PARTY
827
862
  },
@@ -883,7 +918,7 @@ var MemoryMSSQL = class extends storage.MemoryStorage {
883
918
  } catch (error$1) {
884
919
  throw new error.MastraError(
885
920
  {
886
- id: "MASTRA_STORAGE_MSSQL_STORE_DELETE_MESSAGES_FAILED",
921
+ id: storage.createStorageErrorId("MSSQL", "DELETE_MESSAGES", "FAILED"),
887
922
  domain: error.ErrorDomain.STORAGE,
888
923
  category: error.ErrorCategory.THIRD_PARTY,
889
924
  details: { messageIds: messageIds.join(", ") }
@@ -911,7 +946,7 @@ var MemoryMSSQL = class extends storage.MemoryStorage {
911
946
  } catch (error$1) {
912
947
  const mastraError = new error.MastraError(
913
948
  {
914
- id: "MASTRA_STORAGE_MSSQL_GET_RESOURCE_BY_ID_FAILED",
949
+ id: storage.createStorageErrorId("MSSQL", "GET_RESOURCE_BY_ID", "FAILED"),
915
950
  domain: error.ErrorDomain.STORAGE,
916
951
  category: error.ErrorCategory.THIRD_PARTY,
917
952
  details: { resourceId }
@@ -978,7 +1013,7 @@ var MemoryMSSQL = class extends storage.MemoryStorage {
978
1013
  } catch (error$1) {
979
1014
  const mastraError = new error.MastraError(
980
1015
  {
981
- id: "MASTRA_STORAGE_MSSQL_UPDATE_RESOURCE_FAILED",
1016
+ id: storage.createStorageErrorId("MSSQL", "UPDATE_RESOURCE", "FAILED"),
982
1017
  domain: error.ErrorDomain.STORAGE,
983
1018
  category: error.ErrorCategory.THIRD_PARTY,
984
1019
  details: { resourceId }
@@ -1025,7 +1060,7 @@ var ObservabilityMSSQL = class extends storage.ObservabilityStorage {
1025
1060
  } catch (error$1) {
1026
1061
  throw new error.MastraError(
1027
1062
  {
1028
- id: "MSSQL_STORE_CREATE_SPAN_FAILED",
1063
+ id: storage.createStorageErrorId("MSSQL", "CREATE_SPAN", "FAILED"),
1029
1064
  domain: error.ErrorDomain.STORAGE,
1030
1065
  category: error.ErrorCategory.USER,
1031
1066
  details: {
@@ -1071,7 +1106,7 @@ var ObservabilityMSSQL = class extends storage.ObservabilityStorage {
1071
1106
  } catch (error$1) {
1072
1107
  throw new error.MastraError(
1073
1108
  {
1074
- id: "MSSQL_STORE_GET_TRACE_FAILED",
1109
+ id: storage.createStorageErrorId("MSSQL", "GET_TRACE", "FAILED"),
1075
1110
  domain: error.ErrorDomain.STORAGE,
1076
1111
  category: error.ErrorCategory.USER,
1077
1112
  details: {
@@ -1103,7 +1138,7 @@ var ObservabilityMSSQL = class extends storage.ObservabilityStorage {
1103
1138
  } catch (error$1) {
1104
1139
  throw new error.MastraError(
1105
1140
  {
1106
- id: "MSSQL_STORE_UPDATE_SPAN_FAILED",
1141
+ id: storage.createStorageErrorId("MSSQL", "UPDATE_SPAN", "FAILED"),
1107
1142
  domain: error.ErrorDomain.STORAGE,
1108
1143
  category: error.ErrorCategory.USER,
1109
1144
  details: {
@@ -1140,7 +1175,7 @@ var ObservabilityMSSQL = class extends storage.ObservabilityStorage {
1140
1175
  name = `agent run: '${entityId}'`;
1141
1176
  } else {
1142
1177
  const error$1 = new error.MastraError({
1143
- id: "MSSQL_STORE_GET_TRACES_PAGINATED_FAILED",
1178
+ id: storage.createStorageErrorId("MSSQL", "GET_TRACES_PAGINATED", "INVALID_ENTITY_TYPE"),
1144
1179
  domain: error.ErrorDomain.STORAGE,
1145
1180
  category: error.ErrorCategory.USER,
1146
1181
  details: {
@@ -1209,7 +1244,7 @@ var ObservabilityMSSQL = class extends storage.ObservabilityStorage {
1209
1244
  } catch (error$1) {
1210
1245
  throw new error.MastraError(
1211
1246
  {
1212
- id: "MSSQL_STORE_GET_TRACES_PAGINATED_FAILED",
1247
+ id: storage.createStorageErrorId("MSSQL", "GET_TRACES_PAGINATED", "FAILED"),
1213
1248
  domain: error.ErrorDomain.STORAGE,
1214
1249
  category: error.ErrorCategory.USER
1215
1250
  },
@@ -1233,7 +1268,7 @@ var ObservabilityMSSQL = class extends storage.ObservabilityStorage {
1233
1268
  } catch (error$1) {
1234
1269
  throw new error.MastraError(
1235
1270
  {
1236
- id: "MSSQL_STORE_BATCH_CREATE_SPANS_FAILED",
1271
+ id: storage.createStorageErrorId("MSSQL", "BATCH_CREATE_SPANS", "FAILED"),
1237
1272
  domain: error.ErrorDomain.STORAGE,
1238
1273
  category: error.ErrorCategory.USER,
1239
1274
  details: {
@@ -1269,7 +1304,7 @@ var ObservabilityMSSQL = class extends storage.ObservabilityStorage {
1269
1304
  } catch (error$1) {
1270
1305
  throw new error.MastraError(
1271
1306
  {
1272
- id: "MSSQL_STORE_BATCH_UPDATE_SPANS_FAILED",
1307
+ id: storage.createStorageErrorId("MSSQL", "BATCH_UPDATE_SPANS", "FAILED"),
1273
1308
  domain: error.ErrorDomain.STORAGE,
1274
1309
  category: error.ErrorCategory.USER,
1275
1310
  details: {
@@ -1293,7 +1328,7 @@ var ObservabilityMSSQL = class extends storage.ObservabilityStorage {
1293
1328
  } catch (error$1) {
1294
1329
  throw new error.MastraError(
1295
1330
  {
1296
- id: "MSSQL_STORE_BATCH_DELETE_TRACES_FAILED",
1331
+ id: storage.createStorageErrorId("MSSQL", "BATCH_DELETE_TRACES", "FAILED"),
1297
1332
  domain: error.ErrorDomain.STORAGE,
1298
1333
  category: error.ErrorCategory.USER,
1299
1334
  details: {
@@ -1333,7 +1368,7 @@ var StoreOperationsMSSQL = class extends storage.StoreOperations {
1333
1368
  return "BIT";
1334
1369
  default:
1335
1370
  throw new error.MastraError({
1336
- id: "MASTRA_STORAGE_MSSQL_STORE_TYPE_NOT_SUPPORTED",
1371
+ id: storage.createStorageErrorId("MSSQL", "TYPE", "NOT_SUPPORTED"),
1337
1372
  domain: error.ErrorDomain.STORAGE,
1338
1373
  category: error.ErrorCategory.THIRD_PARTY
1339
1374
  });
@@ -1419,7 +1454,7 @@ var StoreOperationsMSSQL = class extends storage.StoreOperations {
1419
1454
  } catch (error$1) {
1420
1455
  throw new error.MastraError(
1421
1456
  {
1422
- id: "MASTRA_STORAGE_MSSQL_STORE_INSERT_FAILED",
1457
+ id: storage.createStorageErrorId("MSSQL", "INSERT", "FAILED"),
1423
1458
  domain: error.ErrorDomain.STORAGE,
1424
1459
  category: error.ErrorCategory.THIRD_PARTY,
1425
1460
  details: {
@@ -1445,7 +1480,7 @@ var StoreOperationsMSSQL = class extends storage.StoreOperations {
1445
1480
  } catch (error$1) {
1446
1481
  throw new error.MastraError(
1447
1482
  {
1448
- id: "MASTRA_STORAGE_MSSQL_STORE_CLEAR_TABLE_FAILED",
1483
+ id: storage.createStorageErrorId("MSSQL", "CLEAR_TABLE", "FAILED"),
1449
1484
  domain: error.ErrorDomain.STORAGE,
1450
1485
  category: error.ErrorCategory.THIRD_PARTY,
1451
1486
  details: {
@@ -1548,7 +1583,7 @@ ${columns}
1548
1583
  } catch (error$1) {
1549
1584
  throw new error.MastraError(
1550
1585
  {
1551
- id: "MASTRA_STORAGE_MSSQL_STORE_CREATE_TABLE_FAILED",
1586
+ id: storage.createStorageErrorId("MSSQL", "CREATE_TABLE", "FAILED"),
1552
1587
  domain: error.ErrorDomain.STORAGE,
1553
1588
  category: error.ErrorCategory.THIRD_PARTY,
1554
1589
  details: {
@@ -1608,7 +1643,7 @@ ${columns}
1608
1643
  } catch (error$1) {
1609
1644
  throw new error.MastraError(
1610
1645
  {
1611
- id: "MASTRA_STORAGE_MSSQL_STORE_ALTER_TABLE_FAILED",
1646
+ id: storage.createStorageErrorId("MSSQL", "ALTER_TABLE", "FAILED"),
1612
1647
  domain: error.ErrorDomain.STORAGE,
1613
1648
  category: error.ErrorCategory.THIRD_PARTY,
1614
1649
  details: {
@@ -1649,7 +1684,7 @@ ${columns}
1649
1684
  } catch (error$1) {
1650
1685
  throw new error.MastraError(
1651
1686
  {
1652
- id: "MASTRA_STORAGE_MSSQL_STORE_LOAD_FAILED",
1687
+ id: storage.createStorageErrorId("MSSQL", "LOAD", "FAILED"),
1653
1688
  domain: error.ErrorDomain.STORAGE,
1654
1689
  category: error.ErrorCategory.THIRD_PARTY,
1655
1690
  details: {
@@ -1672,7 +1707,7 @@ ${columns}
1672
1707
  await transaction.rollback();
1673
1708
  throw new error.MastraError(
1674
1709
  {
1675
- id: "MASTRA_STORAGE_MSSQL_STORE_BATCH_INSERT_FAILED",
1710
+ id: storage.createStorageErrorId("MSSQL", "BATCH_INSERT", "FAILED"),
1676
1711
  domain: error.ErrorDomain.STORAGE,
1677
1712
  category: error.ErrorCategory.THIRD_PARTY,
1678
1713
  details: {
@@ -1691,7 +1726,7 @@ ${columns}
1691
1726
  } catch (error$1) {
1692
1727
  throw new error.MastraError(
1693
1728
  {
1694
- id: "MASTRA_STORAGE_MSSQL_STORE_DROP_TABLE_FAILED",
1729
+ id: storage.createStorageErrorId("MSSQL", "DROP_TABLE", "FAILED"),
1695
1730
  domain: error.ErrorDomain.STORAGE,
1696
1731
  category: error.ErrorCategory.THIRD_PARTY,
1697
1732
  details: {
@@ -1777,7 +1812,7 @@ ${columns}
1777
1812
  try {
1778
1813
  if (!data || Object.keys(data).length === 0) {
1779
1814
  throw new error.MastraError({
1780
- id: "MASTRA_STORAGE_MSSQL_UPDATE_EMPTY_DATA",
1815
+ id: storage.createStorageErrorId("MSSQL", "UPDATE", "EMPTY_DATA"),
1781
1816
  domain: error.ErrorDomain.STORAGE,
1782
1817
  category: error.ErrorCategory.USER,
1783
1818
  text: "Cannot update with empty data payload"
@@ -1785,7 +1820,7 @@ ${columns}
1785
1820
  }
1786
1821
  if (!keys || Object.keys(keys).length === 0) {
1787
1822
  throw new error.MastraError({
1788
- id: "MASTRA_STORAGE_MSSQL_UPDATE_EMPTY_KEYS",
1823
+ id: storage.createStorageErrorId("MSSQL", "UPDATE", "EMPTY_KEYS"),
1789
1824
  domain: error.ErrorDomain.STORAGE,
1790
1825
  category: error.ErrorCategory.USER,
1791
1826
  text: "Cannot update without keys to identify records"
@@ -1826,7 +1861,7 @@ ${columns}
1826
1861
  } catch (error$1) {
1827
1862
  throw new error.MastraError(
1828
1863
  {
1829
- id: "MASTRA_STORAGE_MSSQL_STORE_UPDATE_FAILED",
1864
+ id: storage.createStorageErrorId("MSSQL", "UPDATE", "FAILED"),
1830
1865
  domain: error.ErrorDomain.STORAGE,
1831
1866
  category: error.ErrorCategory.THIRD_PARTY,
1832
1867
  details: {
@@ -1855,7 +1890,7 @@ ${columns}
1855
1890
  await transaction.rollback();
1856
1891
  throw new error.MastraError(
1857
1892
  {
1858
- id: "MASTRA_STORAGE_MSSQL_STORE_BATCH_UPDATE_FAILED",
1893
+ id: storage.createStorageErrorId("MSSQL", "BATCH_UPDATE", "FAILED"),
1859
1894
  domain: error.ErrorDomain.STORAGE,
1860
1895
  category: error.ErrorCategory.THIRD_PARTY,
1861
1896
  details: {
@@ -1904,7 +1939,7 @@ ${columns}
1904
1939
  await transaction.rollback();
1905
1940
  throw new error.MastraError(
1906
1941
  {
1907
- id: "MASTRA_STORAGE_MSSQL_STORE_BATCH_DELETE_FAILED",
1942
+ id: storage.createStorageErrorId("MSSQL", "BATCH_DELETE", "FAILED"),
1908
1943
  domain: error.ErrorDomain.STORAGE,
1909
1944
  category: error.ErrorCategory.THIRD_PARTY,
1910
1945
  details: {
@@ -1961,7 +1996,7 @@ ${columns}
1961
1996
  } catch (error$1) {
1962
1997
  throw new error.MastraError(
1963
1998
  {
1964
- id: "MASTRA_STORAGE_MSSQL_INDEX_CREATE_FAILED",
1999
+ id: storage.createStorageErrorId("MSSQL", "INDEX_CREATE", "FAILED"),
1965
2000
  domain: error.ErrorDomain.STORAGE,
1966
2001
  category: error.ErrorCategory.THIRD_PARTY,
1967
2002
  details: {
@@ -1997,7 +2032,7 @@ ${columns}
1997
2032
  if (result.recordset.length > 1) {
1998
2033
  const tables = result.recordset.map((r) => r.table_name).join(", ");
1999
2034
  throw new error.MastraError({
2000
- id: "MASTRA_STORAGE_MSSQL_INDEX_AMBIGUOUS",
2035
+ id: storage.createStorageErrorId("MSSQL", "INDEX", "AMBIGUOUS"),
2001
2036
  domain: error.ErrorDomain.STORAGE,
2002
2037
  category: error.ErrorCategory.USER,
2003
2038
  text: `Index "${indexNameSafe}" exists on multiple tables (${tables}) in schema "${schemaName}". Please drop indexes manually or ensure unique index names.`
@@ -2013,7 +2048,7 @@ ${columns}
2013
2048
  } catch (error$1) {
2014
2049
  throw new error.MastraError(
2015
2050
  {
2016
- id: "MASTRA_STORAGE_MSSQL_INDEX_DROP_FAILED",
2051
+ id: storage.createStorageErrorId("MSSQL", "INDEX_DROP", "FAILED"),
2017
2052
  domain: error.ErrorDomain.STORAGE,
2018
2053
  category: error.ErrorCategory.THIRD_PARTY,
2019
2054
  details: {
@@ -2097,7 +2132,7 @@ ${columns}
2097
2132
  } catch (error$1) {
2098
2133
  throw new error.MastraError(
2099
2134
  {
2100
- id: "MASTRA_STORAGE_MSSQL_INDEX_LIST_FAILED",
2135
+ id: storage.createStorageErrorId("MSSQL", "INDEX_LIST", "FAILED"),
2101
2136
  domain: error.ErrorDomain.STORAGE,
2102
2137
  category: error.ErrorCategory.THIRD_PARTY,
2103
2138
  details: tableName ? {
@@ -2170,7 +2205,7 @@ ${columns}
2170
2205
  } catch (error$1) {
2171
2206
  throw new error.MastraError(
2172
2207
  {
2173
- id: "MASTRA_STORAGE_MSSQL_INDEX_DESCRIBE_FAILED",
2208
+ id: storage.createStorageErrorId("MSSQL", "INDEX_DESCRIBE", "FAILED"),
2174
2209
  domain: error.ErrorDomain.STORAGE,
2175
2210
  category: error.ErrorCategory.THIRD_PARTY,
2176
2211
  details: {
@@ -2251,7 +2286,7 @@ ${columns}
2251
2286
  } catch (error$1) {
2252
2287
  throw new error.MastraError(
2253
2288
  {
2254
- id: "MASTRA_STORAGE_MSSQL_STORE_CREATE_PERFORMANCE_INDEXES_FAILED",
2289
+ id: storage.createStorageErrorId("MSSQL", "CREATE_PERFORMANCE_INDEXES", "FAILED"),
2255
2290
  domain: error.ErrorDomain.STORAGE,
2256
2291
  category: error.ErrorCategory.THIRD_PARTY
2257
2292
  },
@@ -2261,20 +2296,9 @@ ${columns}
2261
2296
  }
2262
2297
  };
2263
2298
  function transformScoreRow(row) {
2264
- return {
2265
- ...row,
2266
- input: storage.safelyParseJSON(row.input),
2267
- scorer: storage.safelyParseJSON(row.scorer),
2268
- preprocessStepResult: storage.safelyParseJSON(row.preprocessStepResult),
2269
- analyzeStepResult: storage.safelyParseJSON(row.analyzeStepResult),
2270
- metadata: storage.safelyParseJSON(row.metadata),
2271
- output: storage.safelyParseJSON(row.output),
2272
- additionalContext: storage.safelyParseJSON(row.additionalContext),
2273
- requestContext: storage.safelyParseJSON(row.requestContext),
2274
- entity: storage.safelyParseJSON(row.entity),
2275
- createdAt: row.createdAt,
2276
- updatedAt: row.updatedAt
2277
- };
2299
+ return storage.transformScoreRow(row, {
2300
+ convertTimestamps: true
2301
+ });
2278
2302
  }
2279
2303
  var ScoresMSSQL = class extends storage.ScoresStorage {
2280
2304
  pool;
@@ -2304,7 +2328,7 @@ var ScoresMSSQL = class extends storage.ScoresStorage {
2304
2328
  } catch (error$1) {
2305
2329
  throw new error.MastraError(
2306
2330
  {
2307
- id: "MASTRA_STORAGE_MSSQL_STORE_GET_SCORE_BY_ID_FAILED",
2331
+ id: storage.createStorageErrorId("MSSQL", "GET_SCORE_BY_ID", "FAILED"),
2308
2332
  domain: error.ErrorDomain.STORAGE,
2309
2333
  category: error.ErrorCategory.THIRD_PARTY,
2310
2334
  details: { id }
@@ -2320,7 +2344,7 @@ var ScoresMSSQL = class extends storage.ScoresStorage {
2320
2344
  } catch (error$1) {
2321
2345
  throw new error.MastraError(
2322
2346
  {
2323
- id: "MASTRA_STORAGE_MSSQL_STORE_SAVE_SCORE_VALIDATION_FAILED",
2347
+ id: storage.createStorageErrorId("MSSQL", "SAVE_SCORE", "VALIDATION_FAILED"),
2324
2348
  domain: error.ErrorDomain.STORAGE,
2325
2349
  category: error.ErrorCategory.THIRD_PARTY
2326
2350
  },
@@ -2364,7 +2388,7 @@ var ScoresMSSQL = class extends storage.ScoresStorage {
2364
2388
  } catch (error$1) {
2365
2389
  throw new error.MastraError(
2366
2390
  {
2367
- id: "MASTRA_STORAGE_MSSQL_STORE_SAVE_SCORE_FAILED",
2391
+ id: storage.createStorageErrorId("MSSQL", "SAVE_SCORE", "FAILED"),
2368
2392
  domain: error.ErrorDomain.STORAGE,
2369
2393
  category: error.ErrorCategory.THIRD_PARTY
2370
2394
  },
@@ -2442,7 +2466,7 @@ var ScoresMSSQL = class extends storage.ScoresStorage {
2442
2466
  } catch (error$1) {
2443
2467
  throw new error.MastraError(
2444
2468
  {
2445
- id: "MASTRA_STORAGE_MSSQL_STORE_GET_SCORES_BY_SCORER_ID_FAILED",
2469
+ id: storage.createStorageErrorId("MSSQL", "LIST_SCORES_BY_SCORER_ID", "FAILED"),
2446
2470
  domain: error.ErrorDomain.STORAGE,
2447
2471
  category: error.ErrorCategory.THIRD_PARTY,
2448
2472
  details: { scorerId }
@@ -2497,7 +2521,7 @@ var ScoresMSSQL = class extends storage.ScoresStorage {
2497
2521
  } catch (error$1) {
2498
2522
  throw new error.MastraError(
2499
2523
  {
2500
- id: "MASTRA_STORAGE_MSSQL_STORE_GET_SCORES_BY_RUN_ID_FAILED",
2524
+ id: storage.createStorageErrorId("MSSQL", "LIST_SCORES_BY_RUN_ID", "FAILED"),
2501
2525
  domain: error.ErrorDomain.STORAGE,
2502
2526
  category: error.ErrorCategory.THIRD_PARTY,
2503
2527
  details: { runId }
@@ -2555,7 +2579,7 @@ var ScoresMSSQL = class extends storage.ScoresStorage {
2555
2579
  } catch (error$1) {
2556
2580
  throw new error.MastraError(
2557
2581
  {
2558
- id: "MASTRA_STORAGE_MSSQL_STORE_GET_SCORES_BY_ENTITY_ID_FAILED",
2582
+ id: storage.createStorageErrorId("MSSQL", "LIST_SCORES_BY_ENTITY_ID", "FAILED"),
2559
2583
  domain: error.ErrorDomain.STORAGE,
2560
2584
  category: error.ErrorCategory.THIRD_PARTY,
2561
2585
  details: { entityId, entityType }
@@ -2613,7 +2637,7 @@ var ScoresMSSQL = class extends storage.ScoresStorage {
2613
2637
  } catch (error$1) {
2614
2638
  throw new error.MastraError(
2615
2639
  {
2616
- id: "MASTRA_STORAGE_MSSQL_STORE_GET_SCORES_BY_SPAN_FAILED",
2640
+ id: storage.createStorageErrorId("MSSQL", "LIST_SCORES_BY_SPAN", "FAILED"),
2617
2641
  domain: error.ErrorDomain.STORAGE,
2618
2642
  category: error.ErrorCategory.THIRD_PARTY,
2619
2643
  details: { traceId, spanId }
@@ -2717,7 +2741,7 @@ var WorkflowsMSSQL = class extends storage.WorkflowsStorage {
2717
2741
  }
2718
2742
  throw new error.MastraError(
2719
2743
  {
2720
- id: "MASTRA_STORAGE_MSSQL_STORE_UPDATE_WORKFLOW_RESULTS_FAILED",
2744
+ id: storage.createStorageErrorId("MSSQL", "UPDATE_WORKFLOW_RESULTS", "FAILED"),
2721
2745
  domain: error.ErrorDomain.STORAGE,
2722
2746
  category: error.ErrorCategory.THIRD_PARTY,
2723
2747
  details: {
@@ -2755,7 +2779,7 @@ var WorkflowsMSSQL = class extends storage.WorkflowsStorage {
2755
2779
  await transaction.rollback();
2756
2780
  throw new error.MastraError(
2757
2781
  {
2758
- id: "MASTRA_STORAGE_MSSQL_STORE_UPDATE_WORKFLOW_STATE_SNAPSHOT_NOT_FOUND",
2782
+ id: storage.createStorageErrorId("MSSQL", "UPDATE_WORKFLOW_STATE", "SNAPSHOT_NOT_FOUND"),
2759
2783
  domain: error.ErrorDomain.STORAGE,
2760
2784
  category: error.ErrorCategory.SYSTEM,
2761
2785
  details: {
@@ -2782,9 +2806,10 @@ var WorkflowsMSSQL = class extends storage.WorkflowsStorage {
2782
2806
  await transaction.rollback();
2783
2807
  } catch {
2784
2808
  }
2809
+ if (error$1 instanceof error.MastraError) throw error$1;
2785
2810
  throw new error.MastraError(
2786
2811
  {
2787
- id: "MASTRA_STORAGE_MSSQL_STORE_UPDATE_WORKFLOW_STATE_FAILED",
2812
+ id: storage.createStorageErrorId("MSSQL", "UPDATE_WORKFLOW_STATE", "FAILED"),
2788
2813
  domain: error.ErrorDomain.STORAGE,
2789
2814
  category: error.ErrorCategory.THIRD_PARTY,
2790
2815
  details: {
@@ -2825,7 +2850,7 @@ var WorkflowsMSSQL = class extends storage.WorkflowsStorage {
2825
2850
  } catch (error$1) {
2826
2851
  throw new error.MastraError(
2827
2852
  {
2828
- id: "MASTRA_STORAGE_MSSQL_STORE_PERSIST_WORKFLOW_SNAPSHOT_FAILED",
2853
+ id: storage.createStorageErrorId("MSSQL", "PERSIST_WORKFLOW_SNAPSHOT", "FAILED"),
2829
2854
  domain: error.ErrorDomain.STORAGE,
2830
2855
  category: error.ErrorCategory.THIRD_PARTY,
2831
2856
  details: {
@@ -2856,7 +2881,7 @@ var WorkflowsMSSQL = class extends storage.WorkflowsStorage {
2856
2881
  } catch (error$1) {
2857
2882
  throw new error.MastraError(
2858
2883
  {
2859
- id: "MASTRA_STORAGE_MSSQL_STORE_LOAD_WORKFLOW_SNAPSHOT_FAILED",
2884
+ id: storage.createStorageErrorId("MSSQL", "LOAD_WORKFLOW_SNAPSHOT", "FAILED"),
2860
2885
  domain: error.ErrorDomain.STORAGE,
2861
2886
  category: error.ErrorCategory.THIRD_PARTY,
2862
2887
  details: {
@@ -2896,7 +2921,7 @@ var WorkflowsMSSQL = class extends storage.WorkflowsStorage {
2896
2921
  } catch (error$1) {
2897
2922
  throw new error.MastraError(
2898
2923
  {
2899
- id: "MASTRA_STORAGE_MSSQL_STORE_GET_WORKFLOW_RUN_BY_ID_FAILED",
2924
+ id: storage.createStorageErrorId("MSSQL", "GET_WORKFLOW_RUN_BY_ID", "FAILED"),
2900
2925
  domain: error.ErrorDomain.STORAGE,
2901
2926
  category: error.ErrorCategory.THIRD_PARTY,
2902
2927
  details: {
@@ -2976,7 +3001,7 @@ var WorkflowsMSSQL = class extends storage.WorkflowsStorage {
2976
3001
  } catch (error$1) {
2977
3002
  throw new error.MastraError(
2978
3003
  {
2979
- id: "MASTRA_STORAGE_MSSQL_STORE_LIST_WORKFLOW_RUNS_FAILED",
3004
+ id: storage.createStorageErrorId("MSSQL", "LIST_WORKFLOW_RUNS", "FAILED"),
2980
3005
  domain: error.ErrorDomain.STORAGE,
2981
3006
  category: error.ErrorCategory.THIRD_PARTY,
2982
3007
  details: {
@@ -2999,7 +3024,7 @@ var MSSQLStore = class extends storage.MastraStorage {
2999
3024
  if (!config.id || typeof config.id !== "string" || config.id.trim() === "") {
3000
3025
  throw new Error("MSSQLStore: id must be provided and cannot be empty.");
3001
3026
  }
3002
- super({ id: config.id, name: "MSSQLStore" });
3027
+ super({ id: config.id, name: "MSSQLStore", disableInit: config.disableInit });
3003
3028
  try {
3004
3029
  if ("connectionString" in config) {
3005
3030
  if (!config.connectionString || typeof config.connectionString !== "string" || config.connectionString.trim() === "") {
@@ -3037,7 +3062,7 @@ var MSSQLStore = class extends storage.MastraStorage {
3037
3062
  } catch (e) {
3038
3063
  throw new error.MastraError(
3039
3064
  {
3040
- id: "MASTRA_STORAGE_MSSQL_STORE_INITIALIZATION_FAILED",
3065
+ id: storage.createStorageErrorId("MSSQL", "INITIALIZATION", "FAILED"),
3041
3066
  domain: error.ErrorDomain.STORAGE,
3042
3067
  category: error.ErrorCategory.USER
3043
3068
  },
@@ -3061,7 +3086,7 @@ var MSSQLStore = class extends storage.MastraStorage {
3061
3086
  this.isConnected = null;
3062
3087
  throw new error.MastraError(
3063
3088
  {
3064
- id: "MASTRA_STORAGE_MSSQL_STORE_INIT_FAILED",
3089
+ id: storage.createStorageErrorId("MSSQL", "INIT", "FAILED"),
3065
3090
  domain: error.ErrorDomain.STORAGE,
3066
3091
  category: error.ErrorCategory.THIRD_PARTY
3067
3092
  },
@@ -3229,7 +3254,7 @@ var MSSQLStore = class extends storage.MastraStorage {
3229
3254
  getObservabilityStore() {
3230
3255
  if (!this.stores.observability) {
3231
3256
  throw new error.MastraError({
3232
- id: "MSSQL_STORE_OBSERVABILITY_NOT_INITIALIZED",
3257
+ id: storage.createStorageErrorId("MSSQL", "OBSERVABILITY", "NOT_INITIALIZED"),
3233
3258
  domain: error.ErrorDomain.STORAGE,
3234
3259
  category: error.ErrorCategory.SYSTEM,
3235
3260
  text: "Observability storage is not initialized"