@mastra/mssql 0.0.0-remove-unused-model-providers-api-20251030210744 → 0.0.0-safe-stringify-telemetry-20251205024938

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,8 +1,8 @@
1
1
  import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
2
- import { MastraStorage, StoreOperations, TABLE_WORKFLOW_SNAPSHOT, TABLE_SCHEMAS, TABLE_THREADS, TABLE_MESSAGES, TABLE_TRACES, TABLE_SCORERS, TABLE_AI_SPANS, ScoresStorage, WorkflowsStorage, MemoryStorage, resolveMessageLimit, TABLE_RESOURCES, ObservabilityStorage, safelyParseJSON } from '@mastra/core/storage';
3
- import sql2 from 'mssql';
2
+ import { MastraStorage, LegacyEvalsStorage, StoreOperations, TABLE_WORKFLOW_SNAPSHOT, TABLE_SCHEMAS, TABLE_THREADS, TABLE_MESSAGES, TABLE_TRACES, TABLE_EVALS, TABLE_SCORERS, TABLE_AI_SPANS, ScoresStorage, TracesStorage, WorkflowsStorage, MemoryStorage, resolveMessageLimit, TABLE_RESOURCES, ObservabilityStorage, safelyParseJSON } from '@mastra/core/storage';
3
+ import sql3 from 'mssql';
4
+ import { parseSqlIdentifier, parseFieldKey } from '@mastra/core/utils';
4
5
  import { MessageList } from '@mastra/core/agent';
5
- import { parseSqlIdentifier } from '@mastra/core/utils';
6
6
  import { randomUUID } from 'crypto';
7
7
  import { saveScorePayloadSchema } from '@mastra/core/scores';
8
8
 
@@ -80,7 +80,153 @@ function transformFromSqlRow({
80
80
  return result;
81
81
  }
82
82
 
83
- // src/storage/domains/memory/index.ts
83
+ // src/storage/domains/legacy-evals/index.ts
84
+ function transformEvalRow(row) {
85
+ let testInfoValue = null, resultValue = null;
86
+ if (row.test_info) {
87
+ try {
88
+ testInfoValue = typeof row.test_info === "string" ? JSON.parse(row.test_info) : row.test_info;
89
+ } catch {
90
+ }
91
+ }
92
+ if (row.result) {
93
+ try {
94
+ resultValue = typeof row.result === "string" ? JSON.parse(row.result) : row.result;
95
+ } catch {
96
+ }
97
+ }
98
+ return {
99
+ agentName: row.agent_name,
100
+ input: row.input,
101
+ output: row.output,
102
+ result: resultValue,
103
+ metricName: row.metric_name,
104
+ instructions: row.instructions,
105
+ testInfo: testInfoValue,
106
+ globalRunId: row.global_run_id,
107
+ runId: row.run_id,
108
+ createdAt: row.created_at
109
+ };
110
+ }
111
+ var LegacyEvalsMSSQL = class extends LegacyEvalsStorage {
112
+ pool;
113
+ schema;
114
+ constructor({ pool, schema }) {
115
+ super();
116
+ this.pool = pool;
117
+ this.schema = schema;
118
+ }
119
+ /** @deprecated use getEvals instead */
120
+ async getEvalsByAgentName(agentName, type) {
121
+ try {
122
+ let query = `SELECT * FROM ${getTableName({ indexName: TABLE_EVALS, schemaName: getSchemaName(this.schema) })} WHERE agent_name = @p1`;
123
+ if (type === "test") {
124
+ query += " AND test_info IS NOT NULL AND JSON_VALUE(test_info, '$.testPath') IS NOT NULL";
125
+ } else if (type === "live") {
126
+ query += " AND (test_info IS NULL OR JSON_VALUE(test_info, '$.testPath') IS NULL)";
127
+ }
128
+ query += " ORDER BY created_at DESC";
129
+ const request = this.pool.request();
130
+ request.input("p1", agentName);
131
+ const result = await request.query(query);
132
+ const rows = result.recordset;
133
+ return typeof transformEvalRow === "function" ? rows?.map((row) => transformEvalRow(row)) ?? [] : rows ?? [];
134
+ } catch (error) {
135
+ if (error && error.number === 208 && error.message && error.message.includes("Invalid object name")) {
136
+ return [];
137
+ }
138
+ this.logger?.error?.("Failed to get evals for the specified agent:", error);
139
+ throw error;
140
+ }
141
+ }
142
+ async getEvals(options = {}) {
143
+ const { agentName, type, page = 0, perPage = 100, dateRange } = options;
144
+ const fromDate = dateRange?.start;
145
+ const toDate = dateRange?.end;
146
+ const where = [];
147
+ const params = {};
148
+ if (agentName) {
149
+ where.push("agent_name = @agentName");
150
+ params["agentName"] = agentName;
151
+ }
152
+ if (type === "test") {
153
+ where.push("test_info IS NOT NULL AND JSON_VALUE(test_info, '$.testPath') IS NOT NULL");
154
+ } else if (type === "live") {
155
+ where.push("(test_info IS NULL OR JSON_VALUE(test_info, '$.testPath') IS NULL)");
156
+ }
157
+ if (fromDate instanceof Date && !isNaN(fromDate.getTime())) {
158
+ where.push(`[created_at] >= @fromDate`);
159
+ params[`fromDate`] = fromDate.toISOString();
160
+ }
161
+ if (toDate instanceof Date && !isNaN(toDate.getTime())) {
162
+ where.push(`[created_at] <= @toDate`);
163
+ params[`toDate`] = toDate.toISOString();
164
+ }
165
+ const whereClause = where.length > 0 ? `WHERE ${where.join(" AND ")}` : "";
166
+ const tableName = getTableName({ indexName: TABLE_EVALS, schemaName: getSchemaName(this.schema) });
167
+ const offset = page * perPage;
168
+ const countQuery = `SELECT COUNT(*) as total FROM ${tableName} ${whereClause}`;
169
+ const dataQuery = `SELECT * FROM ${tableName} ${whereClause} ORDER BY seq_id DESC OFFSET @offset ROWS FETCH NEXT @perPage ROWS ONLY`;
170
+ try {
171
+ const countReq = this.pool.request();
172
+ Object.entries(params).forEach(([key, value]) => {
173
+ if (value instanceof Date) {
174
+ countReq.input(key, sql3.DateTime, value);
175
+ } else {
176
+ countReq.input(key, value);
177
+ }
178
+ });
179
+ const countResult = await countReq.query(countQuery);
180
+ const total = countResult.recordset[0]?.total || 0;
181
+ if (total === 0) {
182
+ return {
183
+ evals: [],
184
+ total: 0,
185
+ page,
186
+ perPage,
187
+ hasMore: false
188
+ };
189
+ }
190
+ const req = this.pool.request();
191
+ Object.entries(params).forEach(([key, value]) => {
192
+ if (value instanceof Date) {
193
+ req.input(key, sql3.DateTime, value);
194
+ } else {
195
+ req.input(key, value);
196
+ }
197
+ });
198
+ req.input("offset", offset);
199
+ req.input("perPage", perPage);
200
+ const result = await req.query(dataQuery);
201
+ const rows = result.recordset;
202
+ return {
203
+ evals: rows?.map((row) => transformEvalRow(row)) ?? [],
204
+ total,
205
+ page,
206
+ perPage,
207
+ hasMore: offset + (rows?.length ?? 0) < total
208
+ };
209
+ } catch (error) {
210
+ const mastraError = new MastraError(
211
+ {
212
+ id: "MASTRA_STORAGE_MSSQL_STORE_GET_EVALS_FAILED",
213
+ domain: ErrorDomain.STORAGE,
214
+ category: ErrorCategory.THIRD_PARTY,
215
+ details: {
216
+ agentName: agentName || "all",
217
+ type: type || "all",
218
+ page,
219
+ perPage
220
+ }
221
+ },
222
+ error
223
+ );
224
+ this.logger?.error?.(mastraError.toString());
225
+ this.logger?.trackException?.(mastraError);
226
+ throw mastraError;
227
+ }
228
+ }
229
+ };
84
230
  var MemoryMSSQL = class extends MemoryStorage {
85
231
  pool;
86
232
  schema;
@@ -112,7 +258,7 @@ var MemoryMSSQL = class extends MemoryStorage {
112
258
  }
113
259
  async getThreadById({ threadId }) {
114
260
  try {
115
- const sql5 = `SELECT
261
+ const sql7 = `SELECT
116
262
  id,
117
263
  [resourceId],
118
264
  title,
@@ -123,7 +269,7 @@ var MemoryMSSQL = class extends MemoryStorage {
123
269
  WHERE id = @threadId`;
124
270
  const request = this.pool.request();
125
271
  request.input("threadId", threadId);
126
- const resultSet = await request.query(sql5);
272
+ const resultSet = await request.query(sql7);
127
273
  const thread = resultSet.recordset[0] || null;
128
274
  if (!thread) {
129
275
  return null;
@@ -229,12 +375,12 @@ var MemoryMSSQL = class extends MemoryStorage {
229
375
  req.input("title", thread.title);
230
376
  const metadata = thread.metadata ? JSON.stringify(thread.metadata) : null;
231
377
  if (metadata === null) {
232
- req.input("metadata", sql2.NVarChar, null);
378
+ req.input("metadata", sql3.NVarChar, null);
233
379
  } else {
234
380
  req.input("metadata", metadata);
235
381
  }
236
- req.input("createdAt", sql2.DateTime2, thread.createdAt);
237
- req.input("updatedAt", sql2.DateTime2, thread.updatedAt);
382
+ req.input("createdAt", sql3.DateTime2, thread.createdAt);
383
+ req.input("updatedAt", sql3.DateTime2, thread.updatedAt);
238
384
  await req.query(mergeSql);
239
385
  return thread;
240
386
  } catch (error) {
@@ -303,7 +449,7 @@ var MemoryMSSQL = class extends MemoryStorage {
303
449
  };
304
450
  try {
305
451
  const table = getTableName({ indexName: TABLE_THREADS, schemaName: getSchemaName(this.schema) });
306
- const sql5 = `UPDATE ${table}
452
+ const sql7 = `UPDATE ${table}
307
453
  SET title = @title,
308
454
  metadata = @metadata,
309
455
  [updatedAt] = @updatedAt
@@ -314,7 +460,7 @@ var MemoryMSSQL = class extends MemoryStorage {
314
460
  req.input("title", title);
315
461
  req.input("metadata", JSON.stringify(mergedMetadata));
316
462
  req.input("updatedAt", /* @__PURE__ */ new Date());
317
- const result = await req.query(sql5);
463
+ const result = await req.query(sql7);
318
464
  let thread = result.recordset && result.recordset[0];
319
465
  if (thread && "seq_id" in thread) {
320
466
  const { seq_id, ...rest } = thread;
@@ -384,7 +530,8 @@ var MemoryMSSQL = class extends MemoryStorage {
384
530
  }
385
531
  async _getIncludedMessages({
386
532
  threadId,
387
- selectBy
533
+ selectBy,
534
+ orderByStatement
388
535
  }) {
389
536
  if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
390
537
  const include = selectBy?.include;
@@ -412,7 +559,7 @@ var MemoryMSSQL = class extends MemoryStorage {
412
559
  m.[resourceId],
413
560
  m.seq_id
414
561
  FROM (
415
- SELECT *, ROW_NUMBER() OVER (ORDER BY [createdAt] ASC) as row_num
562
+ SELECT *, ROW_NUMBER() OVER (${orderByStatement}) as row_num
416
563
  FROM ${getTableName({ indexName: TABLE_MESSAGES, schemaName: getSchemaName(this.schema) })}
417
564
  WHERE [thread_id] = ${pThreadId}
418
565
  ) AS m
@@ -420,17 +567,15 @@ var MemoryMSSQL = class extends MemoryStorage {
420
567
  OR EXISTS (
421
568
  SELECT 1
422
569
  FROM (
423
- SELECT *, ROW_NUMBER() OVER (ORDER BY [createdAt] ASC) as row_num
570
+ SELECT *, ROW_NUMBER() OVER (${orderByStatement}) as row_num
424
571
  FROM ${getTableName({ indexName: TABLE_MESSAGES, schemaName: getSchemaName(this.schema) })}
425
572
  WHERE [thread_id] = ${pThreadId}
426
573
  ) AS target
427
574
  WHERE target.id = ${pId}
428
575
  AND (
429
- -- Get previous messages (messages that come BEFORE the target)
430
- (m.row_num < target.row_num AND m.row_num >= target.row_num - ${pPrev})
576
+ (m.row_num <= target.row_num + ${pPrev} AND m.row_num > target.row_num)
431
577
  OR
432
- -- Get next messages (messages that come AFTER the target)
433
- (m.row_num > target.row_num AND m.row_num <= target.row_num + ${pNext})
578
+ (m.row_num >= target.row_num - ${pNext} AND m.row_num < target.row_num)
434
579
  )
435
580
  )
436
581
  `
@@ -469,7 +614,7 @@ var MemoryMSSQL = class extends MemoryStorage {
469
614
  let rows = [];
470
615
  const include = selectBy?.include || [];
471
616
  if (include?.length) {
472
- const includeMessages = await this._getIncludedMessages({ threadId, selectBy });
617
+ const includeMessages = await this._getIncludedMessages({ threadId, selectBy, orderByStatement });
473
618
  if (includeMessages) {
474
619
  rows.push(...includeMessages);
475
620
  }
@@ -514,7 +659,10 @@ var MemoryMSSQL = class extends MemoryStorage {
514
659
  return [];
515
660
  }
516
661
  }
517
- async listMessagesById({ messageIds }) {
662
+ async getMessagesById({
663
+ messageIds,
664
+ format
665
+ }) {
518
666
  if (messageIds.length === 0) return [];
519
667
  const selectStatement = `SELECT seq_id, id, content, role, type, [createdAt], thread_id AS threadId, resourceId`;
520
668
  const orderByStatement = `ORDER BY [seq_id] DESC`;
@@ -532,8 +680,8 @@ var MemoryMSSQL = class extends MemoryStorage {
532
680
  return timeDiff;
533
681
  });
534
682
  rows = rows.map(({ seq_id, ...rest }) => rest);
535
- const messages = this._parseAndFormatMessages(rows, `v2`);
536
- return messages;
683
+ if (format === `v1`) return this._parseAndFormatMessages(rows, format);
684
+ return this._parseAndFormatMessages(rows, `v2`);
537
685
  } catch (error) {
538
686
  const mastraError = new MastraError(
539
687
  {
@@ -551,135 +699,6 @@ var MemoryMSSQL = class extends MemoryStorage {
551
699
  return [];
552
700
  }
553
701
  }
554
- async listMessages(args) {
555
- const { threadId, resourceId, include, filter, limit, offset = 0, orderBy } = args;
556
- if (!threadId.trim()) {
557
- throw new MastraError(
558
- {
559
- id: "STORAGE_MSSQL_LIST_MESSAGES_INVALID_THREAD_ID",
560
- domain: ErrorDomain.STORAGE,
561
- category: ErrorCategory.THIRD_PARTY,
562
- details: { threadId }
563
- },
564
- new Error("threadId must be a non-empty string")
565
- );
566
- }
567
- try {
568
- let perPage = 40;
569
- if (limit !== void 0) {
570
- if (limit === false) {
571
- perPage = Number.MAX_SAFE_INTEGER;
572
- } else if (limit === 0) {
573
- perPage = 0;
574
- } else if (typeof limit === "number" && limit > 0) {
575
- perPage = limit;
576
- }
577
- }
578
- const page = perPage === 0 ? 0 : Math.floor(offset / perPage);
579
- const sortField = orderBy?.field || "createdAt";
580
- const sortDirection = orderBy?.direction || "DESC";
581
- const orderByStatement = `ORDER BY [${sortField}] ${sortDirection}`;
582
- const selectStatement = `SELECT seq_id, id, content, role, type, [createdAt], thread_id AS threadId, resourceId`;
583
- const tableName = getTableName({ indexName: TABLE_MESSAGES, schemaName: getSchemaName(this.schema) });
584
- const conditions = ["[thread_id] = @threadId"];
585
- const request = this.pool.request();
586
- request.input("threadId", threadId);
587
- if (resourceId) {
588
- conditions.push("[resourceId] = @resourceId");
589
- request.input("resourceId", resourceId);
590
- }
591
- if (filter?.dateRange?.start) {
592
- conditions.push("[createdAt] >= @fromDate");
593
- request.input("fromDate", filter.dateRange.start);
594
- }
595
- if (filter?.dateRange?.end) {
596
- conditions.push("[createdAt] <= @toDate");
597
- request.input("toDate", filter.dateRange.end);
598
- }
599
- const whereClause = `WHERE ${conditions.join(" AND ")}`;
600
- const countQuery = `SELECT COUNT(*) as total FROM ${tableName} ${whereClause}`;
601
- const countResult = await request.query(countQuery);
602
- const total = parseInt(countResult.recordset[0]?.total, 10) || 0;
603
- const dataQuery = `${selectStatement} FROM ${tableName} ${whereClause} ${orderByStatement} OFFSET @offset ROWS FETCH NEXT @limit ROWS ONLY`;
604
- request.input("offset", offset);
605
- if (perPage > 2147483647) {
606
- request.input("limit", sql2.BigInt, perPage);
607
- } else {
608
- request.input("limit", perPage);
609
- }
610
- const rowsResult = await request.query(dataQuery);
611
- const rows = rowsResult.recordset || [];
612
- const messages = [...rows];
613
- if (total === 0 && messages.length === 0) {
614
- return {
615
- messages: [],
616
- total: 0,
617
- page,
618
- perPage,
619
- hasMore: false
620
- };
621
- }
622
- const messageIds = new Set(messages.map((m) => m.id));
623
- if (include && include.length > 0) {
624
- const selectBy = { include };
625
- const includeMessages = await this._getIncludedMessages({ threadId, selectBy });
626
- if (includeMessages) {
627
- for (const includeMsg of includeMessages) {
628
- if (!messageIds.has(includeMsg.id)) {
629
- messages.push(includeMsg);
630
- messageIds.add(includeMsg.id);
631
- }
632
- }
633
- }
634
- }
635
- const parsed = this._parseAndFormatMessages(messages, "v2");
636
- let finalMessages = parsed;
637
- finalMessages = finalMessages.sort((a, b) => {
638
- const aValue = sortField === "createdAt" ? new Date(a.createdAt).getTime() : a[sortField];
639
- const bValue = sortField === "createdAt" ? new Date(b.createdAt).getTime() : b[sortField];
640
- return sortDirection === "ASC" ? aValue - bValue : bValue - aValue;
641
- });
642
- const returnedThreadMessageIds = new Set(finalMessages.filter((m) => m.threadId === threadId).map((m) => m.id));
643
- const allThreadMessagesReturned = returnedThreadMessageIds.size >= total;
644
- const hasMore = limit === false ? false : allThreadMessagesReturned ? false : offset + rows.length < total;
645
- return {
646
- messages: finalMessages,
647
- total,
648
- page,
649
- perPage,
650
- hasMore
651
- };
652
- } catch (error) {
653
- const errorPerPage = limit === false ? Number.MAX_SAFE_INTEGER : limit === 0 ? 0 : limit || 40;
654
- const mastraError = new MastraError(
655
- {
656
- id: "MASTRA_STORAGE_MSSQL_STORE_LIST_MESSAGES_FAILED",
657
- domain: ErrorDomain.STORAGE,
658
- category: ErrorCategory.THIRD_PARTY,
659
- details: {
660
- threadId,
661
- resourceId: resourceId ?? ""
662
- }
663
- },
664
- error
665
- );
666
- this.logger?.error?.(mastraError.toString());
667
- this.logger?.trackException?.(mastraError);
668
- return {
669
- messages: [],
670
- total: 0,
671
- page: errorPerPage === 0 ? 0 : Math.floor(offset / errorPerPage),
672
- perPage: errorPerPage,
673
- hasMore: false
674
- };
675
- }
676
- }
677
- async listThreadsByResourceId(args) {
678
- const { resourceId, limit, offset, orderBy, sortDirection } = args;
679
- const page = Math.floor(offset / limit);
680
- const perPage = limit;
681
- return this.getThreadsByResourceIdPaginated({ resourceId, page, perPage, orderBy, sortDirection });
682
- }
683
702
  async getMessagesPaginated(args) {
684
703
  const { threadId, resourceId, format, selectBy } = args;
685
704
  const { page = 0, perPage: perPageInput, dateRange } = selectBy?.pagination || {};
@@ -691,7 +710,7 @@ var MemoryMSSQL = class extends MemoryStorage {
691
710
  const orderByStatement = `ORDER BY [seq_id] DESC`;
692
711
  let messages = [];
693
712
  if (selectBy?.include?.length) {
694
- const includeMessages = await this._getIncludedMessages({ threadId, selectBy });
713
+ const includeMessages = await this._getIncludedMessages({ threadId, selectBy, orderByStatement });
695
714
  if (includeMessages) messages.push(...includeMessages);
696
715
  }
697
716
  const perPage = perPageInput !== void 0 ? perPageInput : resolveMessageLimit({ last: selectBy?.last, defaultLimit: 40 });
@@ -735,7 +754,8 @@ var MemoryMSSQL = class extends MemoryStorage {
735
754
  const rows = rowsResult.recordset || [];
736
755
  rows.sort((a, b) => a.seq_id - b.seq_id);
737
756
  messages.push(...rows);
738
- const parsed = this._parseAndFormatMessages(messages, format);
757
+ let parsed = this._parseAndFormatMessages(messages, format);
758
+ parsed = parsed.sort((a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime());
739
759
  return {
740
760
  messages: parsed,
741
761
  total,
@@ -810,7 +830,7 @@ var MemoryMSSQL = class extends MemoryStorage {
810
830
  "content",
811
831
  typeof message.content === "string" ? message.content : JSON.stringify(message.content)
812
832
  );
813
- request.input("createdAt", sql2.DateTime2, message.createdAt);
833
+ request.input("createdAt", sql3.DateTime2, message.createdAt);
814
834
  request.input("role", message.role);
815
835
  request.input("type", message.type || "v2");
816
836
  request.input("resourceId", message.resourceId);
@@ -829,7 +849,7 @@ var MemoryMSSQL = class extends MemoryStorage {
829
849
  await request.query(mergeSql);
830
850
  }
831
851
  const threadReq = transaction.request();
832
- threadReq.input("updatedAt", sql2.DateTime2, /* @__PURE__ */ new Date());
852
+ threadReq.input("updatedAt", sql3.DateTime2, /* @__PURE__ */ new Date());
833
853
  threadReq.input("id", threadId);
834
854
  await threadReq.query(`UPDATE ${tableThreads} SET [updatedAt] = @updatedAt WHERE id = @id`);
835
855
  await transaction.commit();
@@ -1531,7 +1551,7 @@ var StoreOperationsMSSQL = class extends StoreOperations {
1531
1551
  const value = record[col];
1532
1552
  const preparedValue = this.prepareValue(value, col, tableName);
1533
1553
  if (preparedValue instanceof Date) {
1534
- request.input(`param${i}`, sql2.DateTime2, preparedValue);
1554
+ request.input(`param${i}`, sql3.DateTime2, preparedValue);
1535
1555
  } else if (preparedValue === null || preparedValue === void 0) {
1536
1556
  request.input(`param${i}`, this.getMssqlType(tableName, col), null);
1537
1557
  } else {
@@ -1746,7 +1766,7 @@ ${columns}
1746
1766
  try {
1747
1767
  const keyEntries = Object.entries(keys).map(([key, value]) => [parseSqlIdentifier(key, "column name"), value]);
1748
1768
  const conditions = keyEntries.map(([key], i) => `[${key}] = @param${i}`).join(" AND ");
1749
- const sql5 = `SELECT * FROM ${getTableName({ indexName: tableName, schemaName: getSchemaName(this.schemaName) })} WHERE ${conditions}`;
1769
+ const sql7 = `SELECT * FROM ${getTableName({ indexName: tableName, schemaName: getSchemaName(this.schemaName) })} WHERE ${conditions}`;
1750
1770
  const request = this.pool.request();
1751
1771
  keyEntries.forEach(([key, value], i) => {
1752
1772
  const preparedValue = this.prepareValue(value, key, tableName);
@@ -1756,7 +1776,7 @@ ${columns}
1756
1776
  request.input(`param${i}`, preparedValue);
1757
1777
  }
1758
1778
  });
1759
- const resultSet = await request.query(sql5);
1779
+ const resultSet = await request.query(sql7);
1760
1780
  const result = resultSet.recordset[0] || null;
1761
1781
  if (!result) {
1762
1782
  return null;
@@ -1841,6 +1861,20 @@ ${columns}
1841
1861
  return value ? 1 : 0;
1842
1862
  }
1843
1863
  if (columnSchema?.type === "jsonb") {
1864
+ if (typeof value === "string") {
1865
+ const trimmed = value.trim();
1866
+ if (trimmed.length > 0) {
1867
+ try {
1868
+ JSON.parse(trimmed);
1869
+ return trimmed;
1870
+ } catch {
1871
+ }
1872
+ }
1873
+ return JSON.stringify(value);
1874
+ }
1875
+ if (typeof value === "bigint") {
1876
+ return value.toString();
1877
+ }
1844
1878
  return JSON.stringify(value);
1845
1879
  }
1846
1880
  if (typeof value === "object") {
@@ -1855,23 +1889,23 @@ ${columns}
1855
1889
  const col = TABLE_SCHEMAS[tableName]?.[columnName];
1856
1890
  switch (col?.type) {
1857
1891
  case "text":
1858
- return sql2.NVarChar;
1892
+ return sql3.NVarChar;
1859
1893
  case "timestamp":
1860
- return sql2.DateTime2;
1894
+ return sql3.DateTime2;
1861
1895
  case "uuid":
1862
- return sql2.UniqueIdentifier;
1896
+ return sql3.UniqueIdentifier;
1863
1897
  case "jsonb":
1864
- return sql2.NVarChar;
1898
+ return sql3.NVarChar;
1865
1899
  case "integer":
1866
- return sql2.Int;
1900
+ return sql3.Int;
1867
1901
  case "bigint":
1868
- return sql2.BigInt;
1902
+ return sql3.BigInt;
1869
1903
  case "float":
1870
- return sql2.Float;
1904
+ return sql3.Float;
1871
1905
  case "boolean":
1872
- return sql2.Bit;
1906
+ return sql3.Bit;
1873
1907
  default:
1874
- return sql2.NVarChar;
1908
+ return sql3.NVarChar;
1875
1909
  }
1876
1910
  }
1877
1911
  /**
@@ -2315,6 +2349,11 @@ ${columns}
2315
2349
  table: TABLE_TRACES,
2316
2350
  columns: ["name", "seq_id DESC"]
2317
2351
  },
2352
+ {
2353
+ name: `${schemaPrefix}mastra_evals_agent_name_seqid_idx`,
2354
+ table: TABLE_EVALS,
2355
+ columns: ["agent_name", "seq_id DESC"]
2356
+ },
2318
2357
  {
2319
2358
  name: `${schemaPrefix}mastra_scores_trace_id_span_id_seqid_idx`,
2320
2359
  table: TABLE_SCORERS,
@@ -2379,10 +2418,10 @@ function transformScoreRow(row) {
2379
2418
  metadata: safelyParseJSON(row.metadata),
2380
2419
  output: safelyParseJSON(row.output),
2381
2420
  additionalContext: safelyParseJSON(row.additionalContext),
2382
- requestContext: safelyParseJSON(row.requestContext),
2421
+ runtimeContext: safelyParseJSON(row.runtimeContext),
2383
2422
  entity: safelyParseJSON(row.entity),
2384
- createdAt: row.createdAt,
2385
- updatedAt: row.updatedAt
2423
+ createdAt: new Date(row.createdAt),
2424
+ updatedAt: new Date(row.updatedAt)
2386
2425
  };
2387
2426
  }
2388
2427
  var ScoresMSSQL = class extends ScoresStorage {
@@ -2446,7 +2485,7 @@ var ScoresMSSQL = class extends ScoresStorage {
2446
2485
  input,
2447
2486
  output,
2448
2487
  additionalContext,
2449
- requestContext,
2488
+ runtimeContext,
2450
2489
  entity,
2451
2490
  ...rest
2452
2491
  } = validatedScore;
@@ -2461,7 +2500,7 @@ var ScoresMSSQL = class extends ScoresStorage {
2461
2500
  analyzeStepResult: analyzeStepResult || null,
2462
2501
  metadata: metadata || null,
2463
2502
  additionalContext: additionalContext || null,
2464
- requestContext: requestContext || null,
2503
+ runtimeContext: runtimeContext || null,
2465
2504
  entity: entity || null,
2466
2505
  scorer: scorer || null,
2467
2506
  createdAt: (/* @__PURE__ */ new Date()).toISOString(),
@@ -2713,6 +2752,173 @@ var ScoresMSSQL = class extends ScoresStorage {
2713
2752
  }
2714
2753
  }
2715
2754
  };
2755
+ var TracesMSSQL = class extends TracesStorage {
2756
+ pool;
2757
+ operations;
2758
+ schema;
2759
+ constructor({
2760
+ pool,
2761
+ operations,
2762
+ schema
2763
+ }) {
2764
+ super();
2765
+ this.pool = pool;
2766
+ this.operations = operations;
2767
+ this.schema = schema;
2768
+ }
2769
+ /** @deprecated use getTracesPaginated instead*/
2770
+ async getTraces(args) {
2771
+ if (args.fromDate || args.toDate) {
2772
+ args.dateRange = {
2773
+ start: args.fromDate,
2774
+ end: args.toDate
2775
+ };
2776
+ }
2777
+ const result = await this.getTracesPaginated(args);
2778
+ return result.traces;
2779
+ }
2780
+ async getTracesPaginated(args) {
2781
+ const { name, scope, page = 0, perPage: perPageInput, attributes, filters, dateRange } = args;
2782
+ const fromDate = dateRange?.start;
2783
+ const toDate = dateRange?.end;
2784
+ const perPage = perPageInput !== void 0 ? perPageInput : 100;
2785
+ const currentOffset = page * perPage;
2786
+ const paramMap = {};
2787
+ const conditions = [];
2788
+ let paramIndex = 1;
2789
+ if (name) {
2790
+ const paramName = `p${paramIndex++}`;
2791
+ conditions.push(`[name] LIKE @${paramName}`);
2792
+ paramMap[paramName] = `${name}%`;
2793
+ }
2794
+ if (scope) {
2795
+ const paramName = `p${paramIndex++}`;
2796
+ conditions.push(`[scope] = @${paramName}`);
2797
+ paramMap[paramName] = scope;
2798
+ }
2799
+ if (attributes) {
2800
+ Object.entries(attributes).forEach(([key, value]) => {
2801
+ const parsedKey = parseFieldKey(key);
2802
+ const paramName = `p${paramIndex++}`;
2803
+ conditions.push(`JSON_VALUE([attributes], '$.${parsedKey}') = @${paramName}`);
2804
+ paramMap[paramName] = value;
2805
+ });
2806
+ }
2807
+ if (filters) {
2808
+ Object.entries(filters).forEach(([key, value]) => {
2809
+ const parsedKey = parseFieldKey(key);
2810
+ const paramName = `p${paramIndex++}`;
2811
+ conditions.push(`[${parsedKey}] = @${paramName}`);
2812
+ paramMap[paramName] = value;
2813
+ });
2814
+ }
2815
+ if (fromDate instanceof Date && !isNaN(fromDate.getTime())) {
2816
+ const paramName = `p${paramIndex++}`;
2817
+ conditions.push(`[createdAt] >= @${paramName}`);
2818
+ paramMap[paramName] = fromDate.toISOString();
2819
+ }
2820
+ if (toDate instanceof Date && !isNaN(toDate.getTime())) {
2821
+ const paramName = `p${paramIndex++}`;
2822
+ conditions.push(`[createdAt] <= @${paramName}`);
2823
+ paramMap[paramName] = toDate.toISOString();
2824
+ }
2825
+ const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
2826
+ const countQuery = `SELECT COUNT(*) as total FROM ${getTableName({ indexName: TABLE_TRACES, schemaName: getSchemaName(this.schema) })} ${whereClause}`;
2827
+ let total = 0;
2828
+ try {
2829
+ const countRequest = this.pool.request();
2830
+ Object.entries(paramMap).forEach(([key, value]) => {
2831
+ if (value instanceof Date) {
2832
+ countRequest.input(key, sql3.DateTime, value);
2833
+ } else {
2834
+ countRequest.input(key, value);
2835
+ }
2836
+ });
2837
+ const countResult = await countRequest.query(countQuery);
2838
+ total = parseInt(countResult.recordset[0].total, 10);
2839
+ } catch (error) {
2840
+ throw new MastraError(
2841
+ {
2842
+ id: "MASTRA_STORAGE_MSSQL_STORE_GET_TRACES_PAGINATED_FAILED_TO_RETRIEVE_TOTAL_COUNT",
2843
+ domain: ErrorDomain.STORAGE,
2844
+ category: ErrorCategory.THIRD_PARTY,
2845
+ details: {
2846
+ name: args.name ?? "",
2847
+ scope: args.scope ?? ""
2848
+ }
2849
+ },
2850
+ error
2851
+ );
2852
+ }
2853
+ if (total === 0) {
2854
+ return {
2855
+ traces: [],
2856
+ total: 0,
2857
+ page,
2858
+ perPage,
2859
+ hasMore: false
2860
+ };
2861
+ }
2862
+ const dataQuery = `SELECT * FROM ${getTableName({ indexName: TABLE_TRACES, schemaName: getSchemaName(this.schema) })} ${whereClause} ORDER BY [seq_id] DESC OFFSET @offset ROWS FETCH NEXT @limit ROWS ONLY`;
2863
+ const dataRequest = this.pool.request();
2864
+ Object.entries(paramMap).forEach(([key, value]) => {
2865
+ if (value instanceof Date) {
2866
+ dataRequest.input(key, sql3.DateTime, value);
2867
+ } else {
2868
+ dataRequest.input(key, value);
2869
+ }
2870
+ });
2871
+ dataRequest.input("offset", currentOffset);
2872
+ dataRequest.input("limit", perPage);
2873
+ try {
2874
+ const rowsResult = await dataRequest.query(dataQuery);
2875
+ const rows = rowsResult.recordset;
2876
+ const traces = rows.map((row) => ({
2877
+ id: row.id,
2878
+ parentSpanId: row.parentSpanId,
2879
+ traceId: row.traceId,
2880
+ name: row.name,
2881
+ scope: row.scope,
2882
+ kind: row.kind,
2883
+ status: JSON.parse(row.status),
2884
+ events: JSON.parse(row.events),
2885
+ links: JSON.parse(row.links),
2886
+ attributes: JSON.parse(row.attributes),
2887
+ startTime: row.startTime,
2888
+ endTime: row.endTime,
2889
+ other: row.other,
2890
+ createdAt: row.createdAt
2891
+ }));
2892
+ return {
2893
+ traces,
2894
+ total,
2895
+ page,
2896
+ perPage,
2897
+ hasMore: currentOffset + traces.length < total
2898
+ };
2899
+ } catch (error) {
2900
+ throw new MastraError(
2901
+ {
2902
+ id: "MASTRA_STORAGE_MSSQL_STORE_GET_TRACES_PAGINATED_FAILED_TO_RETRIEVE_TRACES",
2903
+ domain: ErrorDomain.STORAGE,
2904
+ category: ErrorCategory.THIRD_PARTY,
2905
+ details: {
2906
+ name: args.name ?? "",
2907
+ scope: args.scope ?? ""
2908
+ }
2909
+ },
2910
+ error
2911
+ );
2912
+ }
2913
+ }
2914
+ async batchTraceInsert({ records }) {
2915
+ this.logger.debug("Batch inserting traces", { count: records.length });
2916
+ await this.operations.batchInsert({
2917
+ tableName: TABLE_TRACES,
2918
+ records
2919
+ });
2920
+ }
2921
+ };
2716
2922
  var WorkflowsMSSQL = class extends WorkflowsStorage {
2717
2923
  pool;
2718
2924
  operations;
@@ -2750,13 +2956,13 @@ var WorkflowsMSSQL = class extends WorkflowsStorage {
2750
2956
  runId,
2751
2957
  stepId,
2752
2958
  result,
2753
- requestContext
2959
+ runtimeContext
2754
2960
  }) {
2755
2961
  const table = getTableName({ indexName: TABLE_WORKFLOW_SNAPSHOT, schemaName: getSchemaName(this.schema) });
2756
2962
  const transaction = this.pool.transaction();
2757
2963
  try {
2758
2964
  await transaction.begin();
2759
- const selectRequest = new sql2.Request(transaction);
2965
+ const selectRequest = new sql3.Request(transaction);
2760
2966
  selectRequest.input("workflow_name", workflowName);
2761
2967
  selectRequest.input("run_id", runId);
2762
2968
  const existingSnapshotResult = await selectRequest.query(
@@ -2767,28 +2973,29 @@ var WorkflowsMSSQL = class extends WorkflowsStorage {
2767
2973
  snapshot = {
2768
2974
  context: {},
2769
2975
  activePaths: [],
2976
+ activeStepsPath: {},
2770
2977
  timestamp: Date.now(),
2771
2978
  suspendedPaths: {},
2772
2979
  resumeLabels: {},
2773
2980
  serializedStepGraph: [],
2981
+ status: "pending",
2774
2982
  value: {},
2775
2983
  waitingPaths: {},
2776
- status: "pending",
2777
2984
  runId,
2778
- requestContext: {}
2985
+ runtimeContext: {}
2779
2986
  };
2780
2987
  } else {
2781
2988
  const existingSnapshot = existingSnapshotResult.recordset[0].snapshot;
2782
2989
  snapshot = typeof existingSnapshot === "string" ? JSON.parse(existingSnapshot) : existingSnapshot;
2783
2990
  }
2784
2991
  snapshot.context[stepId] = result;
2785
- snapshot.requestContext = { ...snapshot.requestContext, ...requestContext };
2786
- const upsertReq = new sql2.Request(transaction);
2992
+ snapshot.runtimeContext = { ...snapshot.runtimeContext, ...runtimeContext };
2993
+ const upsertReq = new sql3.Request(transaction);
2787
2994
  upsertReq.input("workflow_name", workflowName);
2788
2995
  upsertReq.input("run_id", runId);
2789
2996
  upsertReq.input("snapshot", JSON.stringify(snapshot));
2790
- upsertReq.input("createdAt", sql2.DateTime2, /* @__PURE__ */ new Date());
2791
- upsertReq.input("updatedAt", sql2.DateTime2, /* @__PURE__ */ new Date());
2997
+ upsertReq.input("createdAt", sql3.DateTime2, /* @__PURE__ */ new Date());
2998
+ upsertReq.input("updatedAt", sql3.DateTime2, /* @__PURE__ */ new Date());
2792
2999
  await upsertReq.query(
2793
3000
  `MERGE ${table} AS target
2794
3001
  USING (SELECT @workflow_name AS workflow_name, @run_id AS run_id) AS src
@@ -2828,7 +3035,7 @@ var WorkflowsMSSQL = class extends WorkflowsStorage {
2828
3035
  const transaction = this.pool.transaction();
2829
3036
  try {
2830
3037
  await transaction.begin();
2831
- const selectRequest = new sql2.Request(transaction);
3038
+ const selectRequest = new sql3.Request(transaction);
2832
3039
  selectRequest.input("workflow_name", workflowName);
2833
3040
  selectRequest.input("run_id", runId);
2834
3041
  const existingSnapshotResult = await selectRequest.query(
@@ -2856,11 +3063,11 @@ var WorkflowsMSSQL = class extends WorkflowsStorage {
2856
3063
  );
2857
3064
  }
2858
3065
  const updatedSnapshot = { ...snapshot, ...opts };
2859
- const updateRequest = new sql2.Request(transaction);
3066
+ const updateRequest = new sql3.Request(transaction);
2860
3067
  updateRequest.input("snapshot", JSON.stringify(updatedSnapshot));
2861
3068
  updateRequest.input("workflow_name", workflowName);
2862
3069
  updateRequest.input("run_id", runId);
2863
- updateRequest.input("updatedAt", sql2.DateTime2, /* @__PURE__ */ new Date());
3070
+ updateRequest.input("updatedAt", sql3.DateTime2, /* @__PURE__ */ new Date());
2864
3071
  await updateRequest.query(
2865
3072
  `UPDATE ${table} SET snapshot = @snapshot, [updatedAt] = @updatedAt WHERE workflow_name = @workflow_name AND run_id = @run_id`
2866
3073
  );
@@ -2899,8 +3106,8 @@ var WorkflowsMSSQL = class extends WorkflowsStorage {
2899
3106
  request.input("run_id", runId);
2900
3107
  request.input("resourceId", resourceId);
2901
3108
  request.input("snapshot", JSON.stringify(snapshot));
2902
- request.input("createdAt", sql2.DateTime2, new Date(now));
2903
- request.input("updatedAt", sql2.DateTime2, new Date(now));
3109
+ request.input("createdAt", sql3.DateTime2, new Date(now));
3110
+ request.input("updatedAt", sql3.DateTime2, new Date(now));
2904
3111
  const mergeSql = `MERGE INTO ${table} AS target
2905
3112
  USING (SELECT @workflow_name AS workflow_name, @run_id AS run_id) AS src
2906
3113
  ON target.workflow_name = src.workflow_name AND target.run_id = src.run_id
@@ -2997,13 +3204,14 @@ var WorkflowsMSSQL = class extends WorkflowsStorage {
2997
3204
  );
2998
3205
  }
2999
3206
  }
3000
- async listWorkflowRuns({
3207
+ async getWorkflowRuns({
3001
3208
  workflowName,
3002
3209
  fromDate,
3003
3210
  toDate,
3004
3211
  limit,
3005
3212
  offset,
3006
- resourceId
3213
+ resourceId,
3214
+ status
3007
3215
  } = {}) {
3008
3216
  try {
3009
3217
  const conditions = [];
@@ -3012,6 +3220,10 @@ var WorkflowsMSSQL = class extends WorkflowsStorage {
3012
3220
  conditions.push(`[workflow_name] = @workflowName`);
3013
3221
  paramMap["workflowName"] = workflowName;
3014
3222
  }
3223
+ if (status) {
3224
+ conditions.push(`JSON_VALUE([snapshot], '$.status') = @status`);
3225
+ paramMap["status"] = status;
3226
+ }
3015
3227
  if (resourceId) {
3016
3228
  const hasResourceId = await this.operations.hasColumn(TABLE_WORKFLOW_SNAPSHOT, "resourceId");
3017
3229
  if (hasResourceId) {
@@ -3035,7 +3247,7 @@ var WorkflowsMSSQL = class extends WorkflowsStorage {
3035
3247
  const request = this.pool.request();
3036
3248
  Object.entries(paramMap).forEach(([key, value]) => {
3037
3249
  if (value instanceof Date) {
3038
- request.input(key, sql2.DateTime, value);
3250
+ request.input(key, sql3.DateTime, value);
3039
3251
  } else {
3040
3252
  request.input(key, value);
3041
3253
  }
@@ -3092,7 +3304,7 @@ var MSSQLStore = class extends MastraStorage {
3092
3304
  }
3093
3305
  }
3094
3306
  this.schema = config.schemaName || "dbo";
3095
- this.pool = "connectionString" in config ? new sql2.ConnectionPool(config.connectionString) : new sql2.ConnectionPool({
3307
+ this.pool = "connectionString" in config ? new sql3.ConnectionPool(config.connectionString) : new sql3.ConnectionPool({
3096
3308
  server: config.server,
3097
3309
  database: config.database,
3098
3310
  user: config.user,
@@ -3100,15 +3312,19 @@ var MSSQLStore = class extends MastraStorage {
3100
3312
  port: config.port,
3101
3313
  options: config.options || { encrypt: true, trustServerCertificate: true }
3102
3314
  });
3315
+ const legacyEvals = new LegacyEvalsMSSQL({ pool: this.pool, schema: this.schema });
3103
3316
  const operations = new StoreOperationsMSSQL({ pool: this.pool, schemaName: this.schema });
3104
3317
  const scores = new ScoresMSSQL({ pool: this.pool, operations, schema: this.schema });
3318
+ const traces = new TracesMSSQL({ pool: this.pool, operations, schema: this.schema });
3105
3319
  const workflows = new WorkflowsMSSQL({ pool: this.pool, operations, schema: this.schema });
3106
3320
  const memory = new MemoryMSSQL({ pool: this.pool, schema: this.schema, operations });
3107
3321
  const observability = new ObservabilityMSSQL({ pool: this.pool, operations, schema: this.schema });
3108
3322
  this.stores = {
3109
3323
  operations,
3110
3324
  scores,
3325
+ traces,
3111
3326
  workflows,
3327
+ legacyEvals,
3112
3328
  memory,
3113
3329
  observability
3114
3330
  };
@@ -3167,6 +3383,25 @@ var MSSQLStore = class extends MastraStorage {
3167
3383
  indexManagement: true
3168
3384
  };
3169
3385
  }
3386
+ /** @deprecated use getEvals instead */
3387
+ async getEvalsByAgentName(agentName, type) {
3388
+ return this.stores.legacyEvals.getEvalsByAgentName(agentName, type);
3389
+ }
3390
+ async getEvals(options = {}) {
3391
+ return this.stores.legacyEvals.getEvals(options);
3392
+ }
3393
+ /**
3394
+ * @deprecated use getTracesPaginated instead
3395
+ */
3396
+ async getTraces(args) {
3397
+ return this.stores.traces.getTraces(args);
3398
+ }
3399
+ async getTracesPaginated(args) {
3400
+ return this.stores.traces.getTracesPaginated(args);
3401
+ }
3402
+ async batchTraceInsert({ records }) {
3403
+ return this.stores.traces.batchTraceInsert({ records });
3404
+ }
3170
3405
  async createTable({
3171
3406
  tableName,
3172
3407
  schema
@@ -3226,6 +3461,12 @@ var MSSQLStore = class extends MastraStorage {
3226
3461
  async getMessages(args) {
3227
3462
  return this.stores.memory.getMessages(args);
3228
3463
  }
3464
+ async getMessagesById({
3465
+ messageIds,
3466
+ format
3467
+ }) {
3468
+ return this.stores.memory.getMessagesById({ messageIds, format });
3469
+ }
3229
3470
  async getMessagesPaginated(args) {
3230
3471
  return this.stores.memory.getMessagesPaginated(args);
3231
3472
  }
@@ -3261,9 +3502,9 @@ var MSSQLStore = class extends MastraStorage {
3261
3502
  runId,
3262
3503
  stepId,
3263
3504
  result,
3264
- requestContext
3505
+ runtimeContext
3265
3506
  }) {
3266
- return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result, requestContext });
3507
+ return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result, runtimeContext });
3267
3508
  }
3268
3509
  async updateWorkflowState({
3269
3510
  workflowName,
@@ -3286,15 +3527,8 @@ var MSSQLStore = class extends MastraStorage {
3286
3527
  }) {
3287
3528
  return this.stores.workflows.loadWorkflowSnapshot({ workflowName, runId });
3288
3529
  }
3289
- async listWorkflowRuns({
3290
- workflowName,
3291
- fromDate,
3292
- toDate,
3293
- limit,
3294
- offset,
3295
- resourceId
3296
- } = {}) {
3297
- return this.stores.workflows.listWorkflowRuns({ workflowName, fromDate, toDate, limit, offset, resourceId });
3530
+ async getWorkflowRuns(args = {}) {
3531
+ return this.stores.workflows.getWorkflowRuns(args);
3298
3532
  }
3299
3533
  async getWorkflowRunById({
3300
3534
  runId,