@mastra/dynamodb 0.0.0-toolOptionTypes-20250917085558 → 0.0.0-top-level-fix-20251211103030

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,9 +1,10 @@
1
1
  import { DynamoDBClient, DescribeTableCommand } from '@aws-sdk/client-dynamodb';
2
2
  import { DynamoDBDocumentClient } from '@aws-sdk/lib-dynamodb';
3
3
  import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
4
- import { MastraStorage, StoreOperations, TracesStorage, TABLE_TRACES, WorkflowsStorage, MemoryStorage, resolveMessageLimit, ScoresStorage, LegacyEvalsStorage, TABLE_AI_SPANS, TABLE_RESOURCES, TABLE_SCORERS, TABLE_EVALS, TABLE_WORKFLOW_SNAPSHOT, TABLE_MESSAGES, TABLE_THREADS } from '@mastra/core/storage';
4
+ import { MastraStorage, createStorageErrorId, StoreOperations, WorkflowsStorage, normalizePerPage, MemoryStorage, calculatePagination, ScoresStorage, SCORERS_SCHEMA, TABLE_SPANS, TABLE_RESOURCES, TABLE_TRACES, TABLE_SCORERS, TABLE_WORKFLOW_SNAPSHOT, TABLE_MESSAGES, TABLE_THREADS } from '@mastra/core/storage';
5
5
  import { Entity, Service } from 'electrodb';
6
6
  import { MessageList } from '@mastra/core/agent';
7
+ import { saveScorePayloadSchema } from '@mastra/core/evals';
7
8
 
8
9
  // src/storage/index.ts
9
10
 
@@ -370,6 +371,10 @@ var scoreEntity = new Entity({
370
371
  type: "string",
371
372
  required: false
372
373
  },
374
+ spanId: {
375
+ type: "string",
376
+ required: false
377
+ },
373
378
  runId: {
374
379
  type: "string",
375
380
  required: true
@@ -418,6 +423,10 @@ var scoreEntity = new Entity({
418
423
  return value;
419
424
  }
420
425
  },
426
+ preprocessPrompt: {
427
+ type: "string",
428
+ required: false
429
+ },
421
430
  preprocessStepResult: {
422
431
  type: "string",
423
432
  required: false,
@@ -557,7 +566,7 @@ var scoreEntity = new Entity({
557
566
  return value;
558
567
  }
559
568
  },
560
- runtimeContext: {
569
+ requestContext: {
561
570
  type: "string",
562
571
  required: false,
563
572
  set: (value) => {
@@ -656,6 +665,11 @@ var scoreEntity = new Entity({
656
665
  index: "gsi6",
657
666
  pk: { field: "gsi6pk", composite: ["entity", "threadId"] },
658
667
  sk: { field: "gsi6sk", composite: ["createdAt"] }
668
+ },
669
+ bySpan: {
670
+ index: "gsi7",
671
+ pk: { field: "gsi7pk", composite: ["entity", "traceId", "spanId"] },
672
+ sk: { field: "gsi7sk", composite: ["createdAt"] }
659
673
  }
660
674
  }
661
675
  });
@@ -923,187 +937,6 @@ function getElectroDbService(client, tableName) {
923
937
  }
924
938
  );
925
939
  }
926
- var LegacyEvalsDynamoDB = class extends LegacyEvalsStorage {
927
- service;
928
- tableName;
929
- constructor({ service, tableName }) {
930
- super();
931
- this.service = service;
932
- this.tableName = tableName;
933
- }
934
- // Eval operations
935
- async getEvalsByAgentName(agentName, type) {
936
- this.logger.debug("Getting evals for agent", { agentName, type });
937
- try {
938
- const query = this.service.entities.eval.query.byAgent({ entity: "eval", agent_name: agentName });
939
- const results = await query.go({ order: "desc", limit: 100 });
940
- if (!results.data.length) {
941
- return [];
942
- }
943
- let filteredData = results.data;
944
- if (type) {
945
- filteredData = filteredData.filter((evalRecord) => {
946
- try {
947
- const testInfo = evalRecord.test_info && typeof evalRecord.test_info === "string" ? JSON.parse(evalRecord.test_info) : void 0;
948
- if (type === "test" && !testInfo) {
949
- return false;
950
- }
951
- if (type === "live" && testInfo) {
952
- return false;
953
- }
954
- } catch (e) {
955
- this.logger.warn("Failed to parse test_info during filtering", { record: evalRecord, error: e });
956
- }
957
- return true;
958
- });
959
- }
960
- return filteredData.map((evalRecord) => {
961
- try {
962
- return {
963
- input: evalRecord.input,
964
- output: evalRecord.output,
965
- // Safely parse result and test_info
966
- result: evalRecord.result && typeof evalRecord.result === "string" ? JSON.parse(evalRecord.result) : void 0,
967
- agentName: evalRecord.agent_name,
968
- createdAt: evalRecord.created_at,
969
- // Keep as string from DDB?
970
- metricName: evalRecord.metric_name,
971
- instructions: evalRecord.instructions,
972
- runId: evalRecord.run_id,
973
- globalRunId: evalRecord.global_run_id,
974
- testInfo: evalRecord.test_info && typeof evalRecord.test_info === "string" ? JSON.parse(evalRecord.test_info) : void 0
975
- };
976
- } catch (parseError) {
977
- this.logger.error("Failed to parse eval record", { record: evalRecord, error: parseError });
978
- return {
979
- agentName: evalRecord.agent_name,
980
- createdAt: evalRecord.created_at,
981
- runId: evalRecord.run_id,
982
- globalRunId: evalRecord.global_run_id
983
- };
984
- }
985
- });
986
- } catch (error) {
987
- throw new MastraError(
988
- {
989
- id: "STORAGE_DYNAMODB_STORE_GET_EVALS_BY_AGENT_NAME_FAILED",
990
- domain: ErrorDomain.STORAGE,
991
- category: ErrorCategory.THIRD_PARTY,
992
- details: { agentName }
993
- },
994
- error
995
- );
996
- }
997
- }
998
- async getEvals(options = {}) {
999
- const { agentName, type, page = 0, perPage = 100, dateRange } = options;
1000
- this.logger.debug("Getting evals with pagination", { agentName, type, page, perPage, dateRange });
1001
- try {
1002
- let query;
1003
- if (agentName) {
1004
- query = this.service.entities.eval.query.byAgent({ entity: "eval", agent_name: agentName });
1005
- } else {
1006
- query = this.service.entities.eval.query.byEntity({ entity: "eval" });
1007
- }
1008
- const results = await query.go({
1009
- order: "desc",
1010
- pages: "all"
1011
- // Get all pages to apply filtering and pagination
1012
- });
1013
- if (!results.data.length) {
1014
- return {
1015
- evals: [],
1016
- total: 0,
1017
- page,
1018
- perPage,
1019
- hasMore: false
1020
- };
1021
- }
1022
- let filteredData = results.data;
1023
- if (type) {
1024
- filteredData = filteredData.filter((evalRecord) => {
1025
- try {
1026
- const testInfo = evalRecord.test_info && typeof evalRecord.test_info === "string" ? JSON.parse(evalRecord.test_info) : void 0;
1027
- if (type === "test" && !testInfo) {
1028
- return false;
1029
- }
1030
- if (type === "live" && testInfo) {
1031
- return false;
1032
- }
1033
- } catch (e) {
1034
- this.logger.warn("Failed to parse test_info during filtering", { record: evalRecord, error: e });
1035
- }
1036
- return true;
1037
- });
1038
- }
1039
- if (dateRange) {
1040
- const fromDate = dateRange.start;
1041
- const toDate = dateRange.end;
1042
- filteredData = filteredData.filter((evalRecord) => {
1043
- const recordDate = new Date(evalRecord.created_at);
1044
- if (fromDate && recordDate < fromDate) {
1045
- return false;
1046
- }
1047
- if (toDate && recordDate > toDate) {
1048
- return false;
1049
- }
1050
- return true;
1051
- });
1052
- }
1053
- const total = filteredData.length;
1054
- const start = page * perPage;
1055
- const end = start + perPage;
1056
- const paginatedData = filteredData.slice(start, end);
1057
- const evals = paginatedData.map((evalRecord) => {
1058
- try {
1059
- return {
1060
- input: evalRecord.input,
1061
- output: evalRecord.output,
1062
- result: evalRecord.result && typeof evalRecord.result === "string" ? JSON.parse(evalRecord.result) : void 0,
1063
- agentName: evalRecord.agent_name,
1064
- createdAt: evalRecord.created_at,
1065
- metricName: evalRecord.metric_name,
1066
- instructions: evalRecord.instructions,
1067
- runId: evalRecord.run_id,
1068
- globalRunId: evalRecord.global_run_id,
1069
- testInfo: evalRecord.test_info && typeof evalRecord.test_info === "string" ? JSON.parse(evalRecord.test_info) : void 0
1070
- };
1071
- } catch (parseError) {
1072
- this.logger.error("Failed to parse eval record", { record: evalRecord, error: parseError });
1073
- return {
1074
- agentName: evalRecord.agent_name,
1075
- createdAt: evalRecord.created_at,
1076
- runId: evalRecord.run_id,
1077
- globalRunId: evalRecord.global_run_id
1078
- };
1079
- }
1080
- });
1081
- const hasMore = end < total;
1082
- return {
1083
- evals,
1084
- total,
1085
- page,
1086
- perPage,
1087
- hasMore
1088
- };
1089
- } catch (error) {
1090
- throw new MastraError(
1091
- {
1092
- id: "STORAGE_DYNAMODB_STORE_GET_EVALS_FAILED",
1093
- domain: ErrorDomain.STORAGE,
1094
- category: ErrorCategory.THIRD_PARTY,
1095
- details: {
1096
- agentName: agentName || "all",
1097
- type: type || "all",
1098
- page,
1099
- perPage
1100
- }
1101
- },
1102
- error
1103
- );
1104
- }
1105
- }
1106
- };
1107
940
  var MemoryStorageDynamoDB = class extends MemoryStorage {
1108
941
  service;
1109
942
  constructor({ service }) {
@@ -1122,17 +955,17 @@ var MemoryStorageDynamoDB = class extends MemoryStorage {
1122
955
  };
1123
956
  }
1124
957
  // Helper function to transform and sort threads
1125
- transformAndSortThreads(rawThreads, orderBy, sortDirection) {
958
+ transformAndSortThreads(rawThreads, field, direction) {
1126
959
  return rawThreads.map((data) => ({
1127
960
  ...data,
1128
961
  // Convert date strings back to Date objects for consistency
1129
962
  createdAt: typeof data.createdAt === "string" ? new Date(data.createdAt) : data.createdAt,
1130
963
  updatedAt: typeof data.updatedAt === "string" ? new Date(data.updatedAt) : data.updatedAt
1131
964
  })).sort((a, b) => {
1132
- const fieldA = orderBy === "createdAt" ? a.createdAt : a.updatedAt;
1133
- const fieldB = orderBy === "createdAt" ? b.createdAt : b.updatedAt;
965
+ const fieldA = field === "createdAt" ? a.createdAt : a.updatedAt;
966
+ const fieldB = field === "createdAt" ? b.createdAt : b.updatedAt;
1134
967
  const comparison = fieldA.getTime() - fieldB.getTime();
1135
- return sortDirection === "DESC" ? -comparison : comparison;
968
+ return direction === "DESC" ? -comparison : comparison;
1136
969
  });
1137
970
  }
1138
971
  async getThreadById({ threadId }) {
@@ -1154,7 +987,7 @@ var MemoryStorageDynamoDB = class extends MemoryStorage {
1154
987
  } catch (error) {
1155
988
  throw new MastraError(
1156
989
  {
1157
- id: "STORAGE_DYNAMODB_STORE_GET_THREAD_BY_ID_FAILED",
990
+ id: createStorageErrorId("DYNAMODB", "GET_THREAD_BY_ID", "FAILED"),
1158
991
  domain: ErrorDomain.STORAGE,
1159
992
  category: ErrorCategory.THIRD_PARTY,
1160
993
  details: { threadId }
@@ -1163,32 +996,6 @@ var MemoryStorageDynamoDB = class extends MemoryStorage {
1163
996
  );
1164
997
  }
1165
998
  }
1166
- /**
1167
- * @deprecated use getThreadsByResourceIdPaginated instead for paginated results.
1168
- */
1169
- async getThreadsByResourceId(args) {
1170
- const resourceId = args.resourceId;
1171
- const orderBy = this.castThreadOrderBy(args.orderBy);
1172
- const sortDirection = this.castThreadSortDirection(args.sortDirection);
1173
- this.logger.debug("Getting threads by resource ID", { resourceId, orderBy, sortDirection });
1174
- try {
1175
- const result = await this.service.entities.thread.query.byResource({ entity: "thread", resourceId }).go();
1176
- if (!result.data.length) {
1177
- return [];
1178
- }
1179
- return this.transformAndSortThreads(result.data, orderBy, sortDirection);
1180
- } catch (error) {
1181
- throw new MastraError(
1182
- {
1183
- id: "STORAGE_DYNAMODB_STORE_GET_THREADS_BY_RESOURCE_ID_FAILED",
1184
- domain: ErrorDomain.STORAGE,
1185
- category: ErrorCategory.THIRD_PARTY,
1186
- details: { resourceId }
1187
- },
1188
- error
1189
- );
1190
- }
1191
- }
1192
999
  async saveThread({ thread }) {
1193
1000
  this.logger.debug("Saving thread", { threadId: thread.id });
1194
1001
  const now = /* @__PURE__ */ new Date();
@@ -1208,13 +1015,13 @@ var MemoryStorageDynamoDB = class extends MemoryStorage {
1208
1015
  resourceId: thread.resourceId,
1209
1016
  title: threadData.title,
1210
1017
  createdAt: thread.createdAt || now,
1211
- updatedAt: now,
1018
+ updatedAt: thread.updatedAt || now,
1212
1019
  metadata: thread.metadata
1213
1020
  };
1214
1021
  } catch (error) {
1215
1022
  throw new MastraError(
1216
1023
  {
1217
- id: "STORAGE_DYNAMODB_STORE_SAVE_THREAD_FAILED",
1024
+ id: createStorageErrorId("DYNAMODB", "SAVE_THREAD", "FAILED"),
1218
1025
  domain: ErrorDomain.STORAGE,
1219
1026
  category: ErrorCategory.THIRD_PARTY,
1220
1027
  details: { threadId: thread.id }
@@ -1256,7 +1063,7 @@ var MemoryStorageDynamoDB = class extends MemoryStorage {
1256
1063
  } catch (error) {
1257
1064
  throw new MastraError(
1258
1065
  {
1259
- id: "STORAGE_DYNAMODB_STORE_UPDATE_THREAD_FAILED",
1066
+ id: createStorageErrorId("DYNAMODB", "UPDATE_THREAD", "FAILED"),
1260
1067
  domain: ErrorDomain.STORAGE,
1261
1068
  category: ErrorCategory.THIRD_PARTY,
1262
1069
  details: { threadId: id }
@@ -1268,7 +1075,7 @@ var MemoryStorageDynamoDB = class extends MemoryStorage {
1268
1075
  async deleteThread({ threadId }) {
1269
1076
  this.logger.debug("Deleting thread", { threadId });
1270
1077
  try {
1271
- const messages = await this.getMessages({ threadId });
1078
+ const { messages } = await this.listMessages({ threadId, perPage: false });
1272
1079
  if (messages.length > 0) {
1273
1080
  const batchSize = 25;
1274
1081
  for (let i = 0; i < messages.length; i += batchSize) {
@@ -1288,7 +1095,7 @@ var MemoryStorageDynamoDB = class extends MemoryStorage {
1288
1095
  } catch (error) {
1289
1096
  throw new MastraError(
1290
1097
  {
1291
- id: "STORAGE_DYNAMODB_STORE_DELETE_THREAD_FAILED",
1098
+ id: createStorageErrorId("DYNAMODB", "DELETE_THREAD", "FAILED"),
1292
1099
  domain: ErrorDomain.STORAGE,
1293
1100
  category: ErrorCategory.THIRD_PARTY,
1294
1101
  details: { threadId }
@@ -1297,104 +1104,176 @@ var MemoryStorageDynamoDB = class extends MemoryStorage {
1297
1104
  );
1298
1105
  }
1299
1106
  }
1300
- async getMessages({
1301
- threadId,
1302
- resourceId,
1303
- selectBy,
1304
- format
1305
- }) {
1306
- this.logger.debug("Getting messages", { threadId, selectBy });
1107
+ async listMessagesById({ messageIds }) {
1108
+ this.logger.debug("Getting messages by ID", { messageIds });
1109
+ if (messageIds.length === 0) return { messages: [] };
1307
1110
  try {
1308
- if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
1309
- const messages = [];
1310
- const limit = resolveMessageLimit({ last: selectBy?.last, defaultLimit: Number.MAX_SAFE_INTEGER });
1311
- if (selectBy?.include?.length) {
1312
- const includeMessages = await this._getIncludedMessages(threadId, selectBy);
1313
- if (includeMessages) {
1314
- messages.push(...includeMessages);
1315
- }
1316
- }
1317
- if (limit !== 0) {
1318
- const query = this.service.entities.message.query.byThread({ entity: "message", threadId });
1319
- let results;
1320
- if (limit !== Number.MAX_SAFE_INTEGER && limit > 0) {
1321
- results = await query.go({ limit, order: "desc" });
1322
- results.data = results.data.reverse();
1323
- } else {
1324
- results = await query.go();
1325
- }
1326
- let allThreadMessages = results.data.map((data) => this.parseMessageData(data)).filter((msg) => "content" in msg);
1327
- allThreadMessages.sort((a, b) => {
1328
- const timeA = a.createdAt.getTime();
1329
- const timeB = b.createdAt.getTime();
1330
- if (timeA === timeB) {
1331
- return a.id.localeCompare(b.id);
1332
- }
1333
- return timeA - timeB;
1334
- });
1335
- messages.push(...allThreadMessages);
1336
- }
1337
- messages.sort((a, b) => {
1338
- const timeA = a.createdAt.getTime();
1339
- const timeB = b.createdAt.getTime();
1340
- if (timeA === timeB) {
1341
- return a.id.localeCompare(b.id);
1342
- }
1343
- return timeA - timeB;
1344
- });
1345
- const uniqueMessages = messages.filter(
1111
+ const results = await Promise.all(
1112
+ messageIds.map((id) => this.service.entities.message.query.primary({ entity: "message", id }).go())
1113
+ );
1114
+ const data = results.map((result) => result.data).flat(1);
1115
+ let parsedMessages = data.map((data2) => this.parseMessageData(data2)).filter((msg) => "content" in msg);
1116
+ const uniqueMessages = parsedMessages.filter(
1346
1117
  (message, index, self) => index === self.findIndex((m) => m.id === message.id)
1347
1118
  );
1348
- const list = new MessageList({ threadId, resourceId }).add(uniqueMessages, "memory");
1349
- if (format === `v2`) return list.get.all.v2();
1350
- return list.get.all.v1();
1119
+ const list = new MessageList().add(uniqueMessages, "memory");
1120
+ return { messages: list.get.all.db() };
1351
1121
  } catch (error) {
1352
1122
  throw new MastraError(
1353
1123
  {
1354
- id: "STORAGE_DYNAMODB_STORE_GET_MESSAGES_FAILED",
1124
+ id: createStorageErrorId("DYNAMODB", "LIST_MESSAGES_BY_ID", "FAILED"),
1355
1125
  domain: ErrorDomain.STORAGE,
1356
1126
  category: ErrorCategory.THIRD_PARTY,
1357
- details: { threadId, resourceId: resourceId ?? "" }
1127
+ details: { messageIds: JSON.stringify(messageIds) }
1358
1128
  },
1359
1129
  error
1360
1130
  );
1361
1131
  }
1362
1132
  }
1363
- async getMessagesById({
1364
- messageIds,
1365
- format
1366
- }) {
1367
- this.logger.debug("Getting messages by ID", { messageIds });
1368
- if (messageIds.length === 0) return [];
1369
- try {
1370
- const results = await Promise.all(
1371
- messageIds.map((id) => this.service.entities.message.query.primary({ entity: "message", id }).go())
1372
- );
1373
- const data = results.map((result) => result.data).flat(1);
1374
- let parsedMessages = data.map((data2) => this.parseMessageData(data2)).filter((msg) => "content" in msg);
1375
- const uniqueMessages = parsedMessages.filter(
1376
- (message, index, self) => index === self.findIndex((m) => m.id === message.id)
1133
+ async listMessages(args) {
1134
+ const { threadId, resourceId, include, filter, perPage: perPageInput, page = 0, orderBy } = args;
1135
+ const threadIds = Array.isArray(threadId) ? threadId : [threadId];
1136
+ if (threadIds.length === 0 || threadIds.some((id) => !id.trim())) {
1137
+ throw new MastraError(
1138
+ {
1139
+ id: createStorageErrorId("DYNAMODB", "LIST_MESSAGES", "INVALID_THREAD_ID"),
1140
+ domain: ErrorDomain.STORAGE,
1141
+ category: ErrorCategory.THIRD_PARTY,
1142
+ details: { threadId: Array.isArray(threadId) ? threadId.join(",") : threadId }
1143
+ },
1144
+ new Error("threadId must be a non-empty string or array of non-empty strings")
1377
1145
  );
1378
- const list = new MessageList().add(uniqueMessages, "memory");
1379
- if (format === `v1`) return list.get.all.v1();
1380
- return list.get.all.v2();
1146
+ }
1147
+ const perPage = normalizePerPage(perPageInput, 40);
1148
+ const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
1149
+ try {
1150
+ if (page < 0) {
1151
+ throw new MastraError(
1152
+ {
1153
+ id: createStorageErrorId("DYNAMODB", "LIST_MESSAGES", "INVALID_PAGE"),
1154
+ domain: ErrorDomain.STORAGE,
1155
+ category: ErrorCategory.USER,
1156
+ details: { page }
1157
+ },
1158
+ new Error("page must be >= 0")
1159
+ );
1160
+ }
1161
+ const { field, direction } = this.parseOrderBy(orderBy, "ASC");
1162
+ this.logger.debug("Getting messages with listMessages", {
1163
+ threadId,
1164
+ resourceId,
1165
+ perPageInput,
1166
+ offset,
1167
+ perPage,
1168
+ page,
1169
+ field,
1170
+ direction
1171
+ });
1172
+ const query = this.service.entities.message.query.byThread({ entity: "message", threadId });
1173
+ const results = await query.go();
1174
+ let allThreadMessages = results.data.map((data) => this.parseMessageData(data)).filter((msg) => "content" in msg && typeof msg.content === "object");
1175
+ if (resourceId) {
1176
+ allThreadMessages = allThreadMessages.filter((msg) => msg.resourceId === resourceId);
1177
+ }
1178
+ if (filter?.dateRange) {
1179
+ const dateRange = filter.dateRange;
1180
+ allThreadMessages = allThreadMessages.filter((msg) => {
1181
+ const createdAt = new Date(msg.createdAt).getTime();
1182
+ if (dateRange.start) {
1183
+ const startTime = dateRange.start instanceof Date ? dateRange.start.getTime() : new Date(dateRange.start).getTime();
1184
+ if (createdAt < startTime) return false;
1185
+ }
1186
+ if (dateRange.end) {
1187
+ const endTime = dateRange.end instanceof Date ? dateRange.end.getTime() : new Date(dateRange.end).getTime();
1188
+ if (createdAt > endTime) return false;
1189
+ }
1190
+ return true;
1191
+ });
1192
+ }
1193
+ allThreadMessages.sort((a, b) => {
1194
+ const aValue = field === "createdAt" ? new Date(a.createdAt).getTime() : a[field];
1195
+ const bValue = field === "createdAt" ? new Date(b.createdAt).getTime() : b[field];
1196
+ if (aValue === bValue) {
1197
+ return a.id.localeCompare(b.id);
1198
+ }
1199
+ return direction === "ASC" ? aValue - bValue : bValue - aValue;
1200
+ });
1201
+ const total = allThreadMessages.length;
1202
+ const paginatedMessages = allThreadMessages.slice(offset, offset + perPage);
1203
+ const paginatedCount = paginatedMessages.length;
1204
+ if (total === 0 && paginatedCount === 0 && (!include || include.length === 0)) {
1205
+ return {
1206
+ messages: [],
1207
+ total: 0,
1208
+ page,
1209
+ perPage: perPageForResponse,
1210
+ hasMore: false
1211
+ };
1212
+ }
1213
+ const messageIds = new Set(paginatedMessages.map((m) => m.id));
1214
+ let includeMessages = [];
1215
+ if (include && include.length > 0) {
1216
+ const selectBy = { include };
1217
+ includeMessages = await this._getIncludedMessages(selectBy);
1218
+ for (const includeMsg of includeMessages) {
1219
+ if (!messageIds.has(includeMsg.id)) {
1220
+ paginatedMessages.push(includeMsg);
1221
+ messageIds.add(includeMsg.id);
1222
+ }
1223
+ }
1224
+ }
1225
+ const list = new MessageList().add(paginatedMessages, "memory");
1226
+ let finalMessages = list.get.all.db();
1227
+ finalMessages = finalMessages.sort((a, b) => {
1228
+ const aValue = field === "createdAt" ? new Date(a.createdAt).getTime() : a[field];
1229
+ const bValue = field === "createdAt" ? new Date(b.createdAt).getTime() : b[field];
1230
+ if (aValue === bValue) {
1231
+ return a.id.localeCompare(b.id);
1232
+ }
1233
+ return direction === "ASC" ? aValue - bValue : bValue - aValue;
1234
+ });
1235
+ const returnedThreadMessageIds = new Set(finalMessages.filter((m) => m.threadId === threadId).map((m) => m.id));
1236
+ const allThreadMessagesReturned = returnedThreadMessageIds.size >= total;
1237
+ let hasMore = false;
1238
+ if (perPageInput !== false && !allThreadMessagesReturned) {
1239
+ hasMore = offset + paginatedCount < total;
1240
+ }
1241
+ return {
1242
+ messages: finalMessages,
1243
+ total,
1244
+ page,
1245
+ perPage: perPageForResponse,
1246
+ hasMore
1247
+ };
1381
1248
  } catch (error) {
1382
- throw new MastraError(
1249
+ const mastraError = new MastraError(
1383
1250
  {
1384
- id: "STORAGE_DYNAMODB_STORE_GET_MESSAGES_BY_ID_FAILED",
1251
+ id: createStorageErrorId("DYNAMODB", "LIST_MESSAGES", "FAILED"),
1385
1252
  domain: ErrorDomain.STORAGE,
1386
1253
  category: ErrorCategory.THIRD_PARTY,
1387
- details: { messageIds: JSON.stringify(messageIds) }
1254
+ details: {
1255
+ threadId: Array.isArray(threadId) ? threadId.join(",") : threadId,
1256
+ resourceId: resourceId ?? ""
1257
+ }
1388
1258
  },
1389
1259
  error
1390
1260
  );
1261
+ this.logger?.error?.(mastraError.toString());
1262
+ this.logger?.trackException?.(mastraError);
1263
+ return {
1264
+ messages: [],
1265
+ total: 0,
1266
+ page,
1267
+ perPage: perPageForResponse,
1268
+ hasMore: false
1269
+ };
1391
1270
  }
1392
1271
  }
1393
1272
  async saveMessages(args) {
1394
- const { messages, format = "v1" } = args;
1273
+ const { messages } = args;
1395
1274
  this.logger.debug("Saving messages", { count: messages.length });
1396
1275
  if (!messages.length) {
1397
- return [];
1276
+ return { messages: [] };
1398
1277
  }
1399
1278
  const threadId = messages[0]?.threadId;
1400
1279
  if (!threadId) {
@@ -1448,12 +1327,11 @@ var MemoryStorageDynamoDB = class extends MemoryStorage {
1448
1327
  updatedAt: (/* @__PURE__ */ new Date()).toISOString()
1449
1328
  }).go();
1450
1329
  const list = new MessageList().add(messages, "memory");
1451
- if (format === `v1`) return list.get.all.v1();
1452
- return list.get.all.v2();
1330
+ return { messages: list.get.all.db() };
1453
1331
  } catch (error) {
1454
1332
  throw new MastraError(
1455
1333
  {
1456
- id: "STORAGE_DYNAMODB_STORE_SAVE_MESSAGES_FAILED",
1334
+ id: createStorageErrorId("DYNAMODB", "SAVE_MESSAGES", "FAILED"),
1457
1335
  domain: ErrorDomain.STORAGE,
1458
1336
  category: ErrorCategory.THIRD_PARTY,
1459
1337
  details: { count: messages.length }
@@ -1462,37 +1340,48 @@ var MemoryStorageDynamoDB = class extends MemoryStorage {
1462
1340
  );
1463
1341
  }
1464
1342
  }
1465
- async getThreadsByResourceIdPaginated(args) {
1466
- const { resourceId, page = 0, perPage = 100 } = args;
1467
- const orderBy = this.castThreadOrderBy(args.orderBy);
1468
- const sortDirection = this.castThreadSortDirection(args.sortDirection);
1343
+ async listThreadsByResourceId(args) {
1344
+ const { resourceId, page = 0, perPage: perPageInput, orderBy } = args;
1345
+ const perPage = normalizePerPage(perPageInput, 100);
1346
+ if (page < 0) {
1347
+ throw new MastraError(
1348
+ {
1349
+ id: createStorageErrorId("DYNAMODB", "LIST_THREADS_BY_RESOURCE_ID", "INVALID_PAGE"),
1350
+ domain: ErrorDomain.STORAGE,
1351
+ category: ErrorCategory.USER,
1352
+ details: { page }
1353
+ },
1354
+ new Error("page must be >= 0")
1355
+ );
1356
+ }
1357
+ const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
1358
+ const { field, direction } = this.parseOrderBy(orderBy);
1469
1359
  this.logger.debug("Getting threads by resource ID with pagination", {
1470
1360
  resourceId,
1471
1361
  page,
1472
1362
  perPage,
1473
- orderBy,
1474
- sortDirection
1363
+ field,
1364
+ direction
1475
1365
  });
1476
1366
  try {
1477
1367
  const query = this.service.entities.thread.query.byResource({ entity: "thread", resourceId });
1478
1368
  const results = await query.go();
1479
- const allThreads = this.transformAndSortThreads(results.data, orderBy, sortDirection);
1480
- const startIndex = page * perPage;
1481
- const endIndex = startIndex + perPage;
1482
- const paginatedThreads = allThreads.slice(startIndex, endIndex);
1369
+ const allThreads = this.transformAndSortThreads(results.data, field, direction);
1370
+ const endIndex = offset + perPage;
1371
+ const paginatedThreads = allThreads.slice(offset, endIndex);
1483
1372
  const total = allThreads.length;
1484
- const hasMore = endIndex < total;
1373
+ const hasMore = offset + perPage < total;
1485
1374
  return {
1486
1375
  threads: paginatedThreads,
1487
1376
  total,
1488
1377
  page,
1489
- perPage,
1378
+ perPage: perPageForResponse,
1490
1379
  hasMore
1491
1380
  };
1492
1381
  } catch (error) {
1493
1382
  throw new MastraError(
1494
1383
  {
1495
- id: "STORAGE_DYNAMODB_STORE_GET_THREADS_BY_RESOURCE_ID_PAGINATED_FAILED",
1384
+ id: createStorageErrorId("DYNAMODB", "LIST_THREADS_BY_RESOURCE_ID", "FAILED"),
1496
1385
  domain: ErrorDomain.STORAGE,
1497
1386
  category: ErrorCategory.THIRD_PARTY,
1498
1387
  details: { resourceId, page, perPage }
@@ -1501,98 +1390,24 @@ var MemoryStorageDynamoDB = class extends MemoryStorage {
1501
1390
  );
1502
1391
  }
1503
1392
  }
1504
- async getMessagesPaginated(args) {
1505
- const { threadId, resourceId, selectBy, format = "v1" } = args;
1506
- const { page = 0, perPage = 40, dateRange } = selectBy?.pagination || {};
1507
- const fromDate = dateRange?.start;
1508
- const toDate = dateRange?.end;
1509
- const limit = resolveMessageLimit({ last: selectBy?.last, defaultLimit: Number.MAX_SAFE_INTEGER });
1510
- this.logger.debug("Getting messages with pagination", { threadId, page, perPage, fromDate, toDate, limit });
1511
- try {
1512
- if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
1513
- let messages = [];
1514
- if (selectBy?.include?.length) {
1515
- const includeMessages = await this._getIncludedMessages(threadId, selectBy);
1516
- if (includeMessages) {
1517
- messages.push(...includeMessages);
1518
- }
1519
- }
1520
- if (limit !== 0) {
1521
- const query = this.service.entities.message.query.byThread({ entity: "message", threadId });
1522
- let results;
1523
- if (limit !== Number.MAX_SAFE_INTEGER && limit > 0) {
1524
- results = await query.go({ limit, order: "desc" });
1525
- results.data = results.data.reverse();
1526
- } else {
1527
- results = await query.go();
1528
- }
1529
- let allThreadMessages = results.data.map((data) => this.parseMessageData(data)).filter((msg) => "content" in msg);
1530
- allThreadMessages.sort((a, b) => {
1531
- const timeA = a.createdAt.getTime();
1532
- const timeB = b.createdAt.getTime();
1533
- if (timeA === timeB) {
1534
- return a.id.localeCompare(b.id);
1535
- }
1536
- return timeA - timeB;
1537
- });
1538
- const excludeIds = messages.map((m) => m.id);
1539
- if (excludeIds.length > 0) {
1540
- allThreadMessages = allThreadMessages.filter((msg) => !excludeIds.includes(msg.id));
1541
- }
1542
- messages.push(...allThreadMessages);
1543
- }
1544
- messages.sort((a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime());
1545
- if (fromDate || toDate) {
1546
- messages = messages.filter((msg) => {
1547
- const createdAt = new Date(msg.createdAt).getTime();
1548
- if (fromDate && createdAt < new Date(fromDate).getTime()) return false;
1549
- if (toDate && createdAt > new Date(toDate).getTime()) return false;
1550
- return true;
1551
- });
1552
- }
1553
- const total = messages.length;
1554
- const start = page * perPage;
1555
- const end = start + perPage;
1556
- const paginatedMessages = messages.slice(start, end);
1557
- const hasMore = end < total;
1558
- const list = new MessageList({ threadId, resourceId }).add(paginatedMessages, "memory");
1559
- const finalMessages = format === "v2" ? list.get.all.v2() : list.get.all.v1();
1560
- return {
1561
- messages: finalMessages,
1562
- total,
1563
- page,
1564
- perPage,
1565
- hasMore
1566
- };
1567
- } catch (error) {
1568
- const mastraError = new MastraError(
1569
- {
1570
- id: "STORAGE_DYNAMODB_STORE_GET_MESSAGES_PAGINATED_FAILED",
1571
- domain: ErrorDomain.STORAGE,
1572
- category: ErrorCategory.THIRD_PARTY,
1573
- details: { threadId, resourceId: resourceId ?? "" }
1574
- },
1575
- error
1576
- );
1577
- this.logger?.trackException?.(mastraError);
1578
- this.logger?.error?.(mastraError.toString());
1579
- return { messages: [], total: 0, page, perPage, hasMore: false };
1580
- }
1581
- }
1582
1393
  // Helper method to get included messages with context
1583
- async _getIncludedMessages(threadId, selectBy) {
1584
- if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
1394
+ async _getIncludedMessages(selectBy) {
1585
1395
  if (!selectBy?.include?.length) {
1586
1396
  return [];
1587
1397
  }
1588
1398
  const includeMessages = [];
1589
1399
  for (const includeItem of selectBy.include) {
1590
1400
  try {
1591
- const { id, threadId: targetThreadId, withPreviousMessages = 0, withNextMessages = 0 } = includeItem;
1592
- const searchThreadId = targetThreadId || threadId;
1401
+ const { id, withPreviousMessages = 0, withNextMessages = 0 } = includeItem;
1402
+ const targetResult = await this.service.entities.message.get({ entity: "message", id }).go();
1403
+ if (!targetResult.data) {
1404
+ this.logger.warn("Target message not found", { id });
1405
+ continue;
1406
+ }
1407
+ const targetMessageData = targetResult.data;
1408
+ const searchThreadId = targetMessageData.threadId;
1593
1409
  this.logger.debug("Getting included messages for", {
1594
1410
  id,
1595
- targetThreadId,
1596
1411
  searchThreadId,
1597
1412
  withPreviousMessages,
1598
1413
  withNextMessages
@@ -1615,7 +1430,7 @@ var MemoryStorageDynamoDB = class extends MemoryStorage {
1615
1430
  });
1616
1431
  const targetIndex = allMessages.findIndex((msg) => msg.id === id);
1617
1432
  if (targetIndex === -1) {
1618
- this.logger.warn("Target message not found", { id, threadId: searchThreadId });
1433
+ this.logger.warn("Target message not found in thread", { id, threadId: searchThreadId });
1619
1434
  continue;
1620
1435
  }
1621
1436
  this.logger.debug("Found target message at index", { id, targetIndex, totalMessages: allMessages.length });
@@ -1700,7 +1515,7 @@ var MemoryStorageDynamoDB = class extends MemoryStorage {
1700
1515
  } catch (error) {
1701
1516
  throw new MastraError(
1702
1517
  {
1703
- id: "STORAGE_DYNAMODB_STORE_UPDATE_MESSAGES_FAILED",
1518
+ id: createStorageErrorId("DYNAMODB", "UPDATE_MESSAGES", "FAILED"),
1704
1519
  domain: ErrorDomain.STORAGE,
1705
1520
  category: ErrorCategory.THIRD_PARTY,
1706
1521
  details: { count: messages.length }
@@ -1729,7 +1544,7 @@ var MemoryStorageDynamoDB = class extends MemoryStorage {
1729
1544
  } catch (error) {
1730
1545
  throw new MastraError(
1731
1546
  {
1732
- id: "STORAGE_DYNAMODB_STORE_GET_RESOURCE_BY_ID_FAILED",
1547
+ id: createStorageErrorId("DYNAMODB", "GET_RESOURCE_BY_ID", "FAILED"),
1733
1548
  domain: ErrorDomain.STORAGE,
1734
1549
  category: ErrorCategory.THIRD_PARTY,
1735
1550
  details: { resourceId }
@@ -1761,7 +1576,7 @@ var MemoryStorageDynamoDB = class extends MemoryStorage {
1761
1576
  } catch (error) {
1762
1577
  throw new MastraError(
1763
1578
  {
1764
- id: "STORAGE_DYNAMODB_STORE_SAVE_RESOURCE_FAILED",
1579
+ id: createStorageErrorId("DYNAMODB", "SAVE_RESOURCE", "FAILED"),
1765
1580
  domain: ErrorDomain.STORAGE,
1766
1581
  category: ErrorCategory.THIRD_PARTY,
1767
1582
  details: { resourceId: resource.id }
@@ -1810,7 +1625,7 @@ var MemoryStorageDynamoDB = class extends MemoryStorage {
1810
1625
  } catch (error) {
1811
1626
  throw new MastraError(
1812
1627
  {
1813
- id: "STORAGE_DYNAMODB_STORE_UPDATE_RESOURCE_FAILED",
1628
+ id: createStorageErrorId("DYNAMODB", "UPDATE_RESOURCE", "FAILED"),
1814
1629
  domain: ErrorDomain.STORAGE,
1815
1630
  category: ErrorCategory.THIRD_PARTY,
1816
1631
  details: { resourceId }
@@ -1845,11 +1660,11 @@ var StoreOperationsDynamoDB = class extends StoreOperations {
1845
1660
  [TABLE_THREADS]: "thread",
1846
1661
  [TABLE_MESSAGES]: "message",
1847
1662
  [TABLE_WORKFLOW_SNAPSHOT]: "workflow_snapshot",
1848
- [TABLE_EVALS]: "eval",
1849
1663
  [TABLE_SCORERS]: "score",
1850
1664
  [TABLE_TRACES]: "trace",
1851
1665
  [TABLE_RESOURCES]: "resource",
1852
- [TABLE_AI_SPANS]: "ai_span"
1666
+ [TABLE_SPANS]: "ai_span",
1667
+ mastra_agents: "agent"
1853
1668
  };
1854
1669
  return mapping[tableName] || null;
1855
1670
  }
@@ -1911,7 +1726,7 @@ var StoreOperationsDynamoDB = class extends StoreOperations {
1911
1726
  }
1912
1727
  throw new MastraError(
1913
1728
  {
1914
- id: "STORAGE_DYNAMODB_STORE_VALIDATE_TABLE_EXISTS_FAILED",
1729
+ id: createStorageErrorId("DYNAMODB", "VALIDATE_TABLE_EXISTS", "FAILED"),
1915
1730
  domain: ErrorDomain.STORAGE,
1916
1731
  category: ErrorCategory.THIRD_PARTY,
1917
1732
  details: { tableName: this.tableName }
@@ -1944,7 +1759,7 @@ var StoreOperationsDynamoDB = class extends StoreOperations {
1944
1759
  this.logger.error("Error validating table access", { tableName: this.tableName, error });
1945
1760
  throw new MastraError(
1946
1761
  {
1947
- id: "STORAGE_DYNAMODB_STORE_VALIDATE_TABLE_ACCESS_FAILED",
1762
+ id: createStorageErrorId("DYNAMODB", "VALIDATE_TABLE_ACCESS", "FAILED"),
1948
1763
  domain: ErrorDomain.STORAGE,
1949
1764
  category: ErrorCategory.THIRD_PARTY,
1950
1765
  details: { tableName: this.tableName }
@@ -1958,7 +1773,7 @@ var StoreOperationsDynamoDB = class extends StoreOperations {
1958
1773
  const entityName = this.getEntityNameForTable(tableName);
1959
1774
  if (!entityName || !this.service.entities[entityName]) {
1960
1775
  throw new MastraError({
1961
- id: "STORAGE_DYNAMODB_STORE_INSERT_INVALID_ARGS",
1776
+ id: createStorageErrorId("DYNAMODB", "INSERT", "INVALID_ARGS"),
1962
1777
  domain: ErrorDomain.STORAGE,
1963
1778
  category: ErrorCategory.USER,
1964
1779
  text: "No entity defined for tableName",
@@ -1971,7 +1786,7 @@ var StoreOperationsDynamoDB = class extends StoreOperations {
1971
1786
  } catch (error) {
1972
1787
  throw new MastraError(
1973
1788
  {
1974
- id: "STORAGE_DYNAMODB_STORE_INSERT_FAILED",
1789
+ id: createStorageErrorId("DYNAMODB", "INSERT", "FAILED"),
1975
1790
  domain: ErrorDomain.STORAGE,
1976
1791
  category: ErrorCategory.THIRD_PARTY,
1977
1792
  details: { tableName }
@@ -1990,7 +1805,7 @@ var StoreOperationsDynamoDB = class extends StoreOperations {
1990
1805
  const entityName = this.getEntityNameForTable(tableName);
1991
1806
  if (!entityName || !this.service.entities[entityName]) {
1992
1807
  throw new MastraError({
1993
- id: "STORAGE_DYNAMODB_STORE_CLEAR_TABLE_INVALID_ARGS",
1808
+ id: createStorageErrorId("DYNAMODB", "CLEAR_TABLE", "INVALID_ARGS"),
1994
1809
  domain: ErrorDomain.STORAGE,
1995
1810
  category: ErrorCategory.USER,
1996
1811
  text: "No entity defined for tableName",
@@ -2034,6 +1849,10 @@ var StoreOperationsDynamoDB = class extends StoreOperations {
2034
1849
  if (!item.id) throw new Error(`Missing required key 'id' for entity 'score'`);
2035
1850
  key.id = item.id;
2036
1851
  break;
1852
+ case "resource":
1853
+ if (!item.id) throw new Error(`Missing required key 'id' for entity 'resource'`);
1854
+ key.id = item.id;
1855
+ break;
2037
1856
  default:
2038
1857
  this.logger.warn(`Unknown entity type encountered during clearTable: ${entityName}`);
2039
1858
  throw new Error(`Cannot construct delete key for unknown entity type: ${entityName}`);
@@ -2049,7 +1868,7 @@ var StoreOperationsDynamoDB = class extends StoreOperations {
2049
1868
  } catch (error) {
2050
1869
  throw new MastraError(
2051
1870
  {
2052
- id: "STORAGE_DYNAMODB_STORE_CLEAR_TABLE_FAILED",
1871
+ id: createStorageErrorId("DYNAMODB", "CLEAR_TABLE", "FAILED"),
2053
1872
  domain: ErrorDomain.STORAGE,
2054
1873
  category: ErrorCategory.THIRD_PARTY,
2055
1874
  details: { tableName }
@@ -2066,7 +1885,7 @@ var StoreOperationsDynamoDB = class extends StoreOperations {
2066
1885
  const entityName = this.getEntityNameForTable(tableName);
2067
1886
  if (!entityName || !this.service.entities[entityName]) {
2068
1887
  throw new MastraError({
2069
- id: "STORAGE_DYNAMODB_STORE_BATCH_INSERT_INVALID_ARGS",
1888
+ id: createStorageErrorId("DYNAMODB", "BATCH_INSERT", "INVALID_ARGS"),
2070
1889
  domain: ErrorDomain.STORAGE,
2071
1890
  category: ErrorCategory.USER,
2072
1891
  text: "No entity defined for tableName",
@@ -2094,7 +1913,7 @@ var StoreOperationsDynamoDB = class extends StoreOperations {
2094
1913
  } catch (error) {
2095
1914
  throw new MastraError(
2096
1915
  {
2097
- id: "STORAGE_DYNAMODB_STORE_BATCH_INSERT_FAILED",
1916
+ id: createStorageErrorId("DYNAMODB", "BATCH_INSERT", "FAILED"),
2098
1917
  domain: ErrorDomain.STORAGE,
2099
1918
  category: ErrorCategory.THIRD_PARTY,
2100
1919
  details: { tableName }
@@ -2111,7 +1930,7 @@ var StoreOperationsDynamoDB = class extends StoreOperations {
2111
1930
  const entityName = this.getEntityNameForTable(tableName);
2112
1931
  if (!entityName || !this.service.entities[entityName]) {
2113
1932
  throw new MastraError({
2114
- id: "STORAGE_DYNAMODB_STORE_LOAD_INVALID_ARGS",
1933
+ id: createStorageErrorId("DYNAMODB", "LOAD", "INVALID_ARGS"),
2115
1934
  domain: ErrorDomain.STORAGE,
2116
1935
  category: ErrorCategory.USER,
2117
1936
  text: "No entity defined for tableName",
@@ -2129,7 +1948,7 @@ var StoreOperationsDynamoDB = class extends StoreOperations {
2129
1948
  } catch (error) {
2130
1949
  throw new MastraError(
2131
1950
  {
2132
- id: "STORAGE_DYNAMODB_STORE_LOAD_FAILED",
1951
+ id: createStorageErrorId("DYNAMODB", "LOAD", "FAILED"),
2133
1952
  domain: ErrorDomain.STORAGE,
2134
1953
  category: ErrorCategory.THIRD_PARTY,
2135
1954
  details: { tableName }
@@ -2145,14 +1964,28 @@ var ScoresStorageDynamoDB = class extends ScoresStorage {
2145
1964
  super();
2146
1965
  this.service = service;
2147
1966
  }
2148
- // Helper function to parse score data (handle JSON fields)
1967
+ /**
1968
+ * DynamoDB-specific score row transformation.
1969
+ *
1970
+ * Note: This implementation does NOT use coreTransformScoreRow because:
1971
+ * 1. ElectroDB already parses JSON fields via its entity getters
1972
+ * 2. DynamoDB stores empty strings for null values (which need special handling)
1973
+ * 3. 'entity' is a reserved ElectroDB key, so we use 'entityData' column
1974
+ */
2149
1975
  parseScoreData(data) {
1976
+ const result = {};
1977
+ for (const key of Object.keys(SCORERS_SCHEMA)) {
1978
+ if (["traceId", "resourceId", "threadId", "spanId"].includes(key)) {
1979
+ result[key] = data[key] === "" ? null : data[key];
1980
+ continue;
1981
+ }
1982
+ result[key] = data[key];
1983
+ }
1984
+ result.entity = data.entityData ?? null;
2150
1985
  return {
2151
- ...data,
2152
- // Convert date strings back to Date objects for consistency
1986
+ ...result,
2153
1987
  createdAt: data.createdAt ? new Date(data.createdAt) : /* @__PURE__ */ new Date(),
2154
1988
  updatedAt: data.updatedAt ? new Date(data.updatedAt) : /* @__PURE__ */ new Date()
2155
- // JSON fields are already transformed by the entity's getters
2156
1989
  };
2157
1990
  }
2158
1991
  async getScoreById({ id }) {
@@ -2166,7 +1999,7 @@ var ScoresStorageDynamoDB = class extends ScoresStorage {
2166
1999
  } catch (error) {
2167
2000
  throw new MastraError(
2168
2001
  {
2169
- id: "STORAGE_DYNAMODB_STORE_GET_SCORE_BY_ID_FAILED",
2002
+ id: createStorageErrorId("DYNAMODB", "GET_SCORE_BY_ID", "FAILED"),
2170
2003
  domain: ErrorDomain.STORAGE,
2171
2004
  category: ErrorCategory.THIRD_PARTY,
2172
2005
  details: { id }
@@ -2176,50 +2009,69 @@ var ScoresStorageDynamoDB = class extends ScoresStorage {
2176
2009
  }
2177
2010
  }
2178
2011
  async saveScore(score) {
2179
- this.logger.debug("Saving score", { scorerId: score.scorerId, runId: score.runId });
2012
+ let validatedScore;
2013
+ try {
2014
+ validatedScore = saveScorePayloadSchema.parse(score);
2015
+ } catch (error) {
2016
+ throw new MastraError(
2017
+ {
2018
+ id: createStorageErrorId("DYNAMODB", "SAVE_SCORE", "VALIDATION_FAILED"),
2019
+ domain: ErrorDomain.STORAGE,
2020
+ category: ErrorCategory.USER,
2021
+ details: {
2022
+ scorer: score.scorer?.id ?? "unknown",
2023
+ entityId: score.entityId ?? "unknown",
2024
+ entityType: score.entityType ?? "unknown",
2025
+ traceId: score.traceId ?? "",
2026
+ spanId: score.spanId ?? ""
2027
+ }
2028
+ },
2029
+ error
2030
+ );
2031
+ }
2180
2032
  const now = /* @__PURE__ */ new Date();
2181
- const scoreId = `score-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
2182
- const scoreData = {
2183
- entity: "score",
2184
- id: scoreId,
2185
- scorerId: score.scorerId,
2186
- traceId: score.traceId || "",
2187
- runId: score.runId,
2188
- scorer: typeof score.scorer === "string" ? score.scorer : JSON.stringify(score.scorer),
2189
- preprocessStepResult: typeof score.preprocessStepResult === "string" ? score.preprocessStepResult : JSON.stringify(score.preprocessStepResult),
2190
- analyzeStepResult: typeof score.analyzeStepResult === "string" ? score.analyzeStepResult : JSON.stringify(score.analyzeStepResult),
2191
- score: score.score,
2192
- reason: score.reason,
2193
- preprocessPrompt: score.preprocessPrompt,
2194
- generateScorePrompt: score.generateScorePrompt,
2195
- analyzePrompt: score.analyzePrompt,
2196
- reasonPrompt: score.reasonPrompt,
2197
- input: typeof score.input === "string" ? score.input : JSON.stringify(score.input),
2198
- output: typeof score.output === "string" ? score.output : JSON.stringify(score.output),
2199
- additionalContext: typeof score.additionalContext === "string" ? score.additionalContext : JSON.stringify(score.additionalContext),
2200
- runtimeContext: typeof score.runtimeContext === "string" ? score.runtimeContext : JSON.stringify(score.runtimeContext),
2201
- entityType: score.entityType,
2202
- entityData: typeof score.entity === "string" ? score.entity : JSON.stringify(score.entity),
2203
- entityId: score.entityId,
2204
- source: score.source,
2205
- resourceId: score.resourceId || "",
2206
- threadId: score.threadId || "",
2207
- createdAt: now.toISOString(),
2208
- updatedAt: now.toISOString()
2209
- };
2033
+ const scoreId = crypto.randomUUID();
2034
+ const scorer = typeof validatedScore.scorer === "string" ? validatedScore.scorer : JSON.stringify(validatedScore.scorer);
2035
+ const preprocessStepResult = typeof validatedScore.preprocessStepResult === "string" ? validatedScore.preprocessStepResult : JSON.stringify(validatedScore.preprocessStepResult);
2036
+ const analyzeStepResult = typeof validatedScore.analyzeStepResult === "string" ? validatedScore.analyzeStepResult : JSON.stringify(validatedScore.analyzeStepResult);
2037
+ const input = typeof validatedScore.input === "string" ? validatedScore.input : JSON.stringify(validatedScore.input);
2038
+ const output = typeof validatedScore.output === "string" ? validatedScore.output : JSON.stringify(validatedScore.output);
2039
+ const requestContext = typeof validatedScore.requestContext === "string" ? validatedScore.requestContext : JSON.stringify(validatedScore.requestContext);
2040
+ const entity = typeof validatedScore.entity === "string" ? validatedScore.entity : JSON.stringify(validatedScore.entity);
2041
+ const scoreData = Object.fromEntries(
2042
+ Object.entries({
2043
+ ...validatedScore,
2044
+ entity: "score",
2045
+ id: scoreId,
2046
+ scorer,
2047
+ preprocessStepResult,
2048
+ analyzeStepResult,
2049
+ input,
2050
+ output,
2051
+ requestContext,
2052
+ entityData: entity,
2053
+ traceId: validatedScore.traceId || "",
2054
+ resourceId: validatedScore.resourceId || "",
2055
+ threadId: validatedScore.threadId || "",
2056
+ spanId: validatedScore.spanId || "",
2057
+ createdAt: now.toISOString(),
2058
+ updatedAt: now.toISOString()
2059
+ }).filter(([_, value]) => value !== void 0 && value !== null)
2060
+ );
2210
2061
  try {
2211
2062
  await this.service.entities.score.upsert(scoreData).go();
2212
- const savedScore = {
2213
- ...score,
2214
- id: scoreId,
2215
- createdAt: now,
2216
- updatedAt: now
2063
+ return {
2064
+ score: {
2065
+ ...validatedScore,
2066
+ id: scoreId,
2067
+ createdAt: now,
2068
+ updatedAt: now
2069
+ }
2217
2070
  };
2218
- return { score: savedScore };
2219
2071
  } catch (error) {
2220
2072
  throw new MastraError(
2221
2073
  {
2222
- id: "STORAGE_DYNAMODB_STORE_SAVE_SCORE_FAILED",
2074
+ id: createStorageErrorId("DYNAMODB", "SAVE_SCORE", "FAILED"),
2223
2075
  domain: ErrorDomain.STORAGE,
2224
2076
  category: ErrorCategory.THIRD_PARTY,
2225
2077
  details: { scorerId: score.scorerId, runId: score.runId }
@@ -2228,7 +2080,7 @@ var ScoresStorageDynamoDB = class extends ScoresStorage {
2228
2080
  );
2229
2081
  }
2230
2082
  }
2231
- async getScoresByScorerId({
2083
+ async listScoresByScorerId({
2232
2084
  scorerId,
2233
2085
  pagination,
2234
2086
  entityId,
@@ -2249,24 +2101,25 @@ var ScoresStorageDynamoDB = class extends ScoresStorage {
2249
2101
  allScores = allScores.filter((score) => score.source === source);
2250
2102
  }
2251
2103
  allScores.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
2252
- const startIndex = pagination.page * pagination.perPage;
2253
- const endIndex = startIndex + pagination.perPage;
2254
- const paginatedScores = allScores.slice(startIndex, endIndex);
2104
+ const { page, perPage: perPageInput } = pagination;
2105
+ const perPage = normalizePerPage(perPageInput, Number.MAX_SAFE_INTEGER);
2106
+ const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
2255
2107
  const total = allScores.length;
2256
- const hasMore = endIndex < total;
2108
+ const end = perPageInput === false ? allScores.length : start + perPage;
2109
+ const paginatedScores = allScores.slice(start, end);
2257
2110
  return {
2258
2111
  scores: paginatedScores,
2259
2112
  pagination: {
2260
2113
  total,
2261
- page: pagination.page,
2262
- perPage: pagination.perPage,
2263
- hasMore
2114
+ page,
2115
+ perPage: perPageForResponse,
2116
+ hasMore: end < total
2264
2117
  }
2265
2118
  };
2266
2119
  } catch (error) {
2267
2120
  throw new MastraError(
2268
2121
  {
2269
- id: "STORAGE_DYNAMODB_STORE_GET_SCORES_BY_SCORER_ID_FAILED",
2122
+ id: createStorageErrorId("DYNAMODB", "LIST_SCORES_BY_SCORER_ID", "FAILED"),
2270
2123
  domain: ErrorDomain.STORAGE,
2271
2124
  category: ErrorCategory.THIRD_PARTY,
2272
2125
  details: {
@@ -2282,7 +2135,7 @@ var ScoresStorageDynamoDB = class extends ScoresStorage {
2282
2135
  );
2283
2136
  }
2284
2137
  }
2285
- async getScoresByRunId({
2138
+ async listScoresByRunId({
2286
2139
  runId,
2287
2140
  pagination
2288
2141
  }) {
@@ -2292,24 +2145,25 @@ var ScoresStorageDynamoDB = class extends ScoresStorage {
2292
2145
  const results = await query.go();
2293
2146
  const allScores = results.data.map((data) => this.parseScoreData(data));
2294
2147
  allScores.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
2295
- const startIndex = pagination.page * pagination.perPage;
2296
- const endIndex = startIndex + pagination.perPage;
2297
- const paginatedScores = allScores.slice(startIndex, endIndex);
2148
+ const { page, perPage: perPageInput } = pagination;
2149
+ const perPage = normalizePerPage(perPageInput, Number.MAX_SAFE_INTEGER);
2150
+ const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
2298
2151
  const total = allScores.length;
2299
- const hasMore = endIndex < total;
2152
+ const end = perPageInput === false ? allScores.length : start + perPage;
2153
+ const paginatedScores = allScores.slice(start, end);
2300
2154
  return {
2301
2155
  scores: paginatedScores,
2302
2156
  pagination: {
2303
2157
  total,
2304
- page: pagination.page,
2305
- perPage: pagination.perPage,
2306
- hasMore
2158
+ page,
2159
+ perPage: perPageForResponse,
2160
+ hasMore: end < total
2307
2161
  }
2308
2162
  };
2309
2163
  } catch (error) {
2310
2164
  throw new MastraError(
2311
2165
  {
2312
- id: "STORAGE_DYNAMODB_STORE_GET_SCORES_BY_RUN_ID_FAILED",
2166
+ id: createStorageErrorId("DYNAMODB", "LIST_SCORES_BY_RUN_ID", "FAILED"),
2313
2167
  domain: ErrorDomain.STORAGE,
2314
2168
  category: ErrorCategory.THIRD_PARTY,
2315
2169
  details: { runId, page: pagination.page, perPage: pagination.perPage }
@@ -2318,7 +2172,7 @@ var ScoresStorageDynamoDB = class extends ScoresStorage {
2318
2172
  );
2319
2173
  }
2320
2174
  }
2321
- async getScoresByEntityId({
2175
+ async listScoresByEntityId({
2322
2176
  entityId,
2323
2177
  entityType,
2324
2178
  pagination
@@ -2330,24 +2184,25 @@ var ScoresStorageDynamoDB = class extends ScoresStorage {
2330
2184
  let allScores = results.data.map((data) => this.parseScoreData(data));
2331
2185
  allScores = allScores.filter((score) => score.entityType === entityType);
2332
2186
  allScores.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
2333
- const startIndex = pagination.page * pagination.perPage;
2334
- const endIndex = startIndex + pagination.perPage;
2335
- const paginatedScores = allScores.slice(startIndex, endIndex);
2187
+ const { page, perPage: perPageInput } = pagination;
2188
+ const perPage = normalizePerPage(perPageInput, Number.MAX_SAFE_INTEGER);
2189
+ const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
2336
2190
  const total = allScores.length;
2337
- const hasMore = endIndex < total;
2191
+ const end = perPageInput === false ? allScores.length : start + perPage;
2192
+ const paginatedScores = allScores.slice(start, end);
2338
2193
  return {
2339
2194
  scores: paginatedScores,
2340
2195
  pagination: {
2341
2196
  total,
2342
- page: pagination.page,
2343
- perPage: pagination.perPage,
2344
- hasMore
2197
+ page,
2198
+ perPage: perPageForResponse,
2199
+ hasMore: end < total
2345
2200
  }
2346
2201
  };
2347
2202
  } catch (error) {
2348
2203
  throw new MastraError(
2349
2204
  {
2350
- id: "STORAGE_DYNAMODB_STORE_GET_SCORES_BY_ENTITY_ID_FAILED",
2205
+ id: createStorageErrorId("DYNAMODB", "LIST_SCORES_BY_ENTITY_ID", "FAILED"),
2351
2206
  domain: ErrorDomain.STORAGE,
2352
2207
  category: ErrorCategory.THIRD_PARTY,
2353
2208
  details: { entityId, entityType, page: pagination.page, perPage: pagination.perPage }
@@ -2356,234 +2211,39 @@ var ScoresStorageDynamoDB = class extends ScoresStorage {
2356
2211
  );
2357
2212
  }
2358
2213
  }
2359
- };
2360
- var TracesStorageDynamoDB = class extends TracesStorage {
2361
- service;
2362
- operations;
2363
- constructor({ service, operations }) {
2364
- super();
2365
- this.service = service;
2366
- this.operations = operations;
2367
- }
2368
- // Trace operations
2369
- async getTraces(args) {
2370
- const { name, scope, page, perPage } = args;
2371
- this.logger.debug("Getting traces", { name, scope, page, perPage });
2372
- try {
2373
- let query;
2374
- if (name) {
2375
- query = this.service.entities.trace.query.byName({ entity: "trace", name });
2376
- } else if (scope) {
2377
- query = this.service.entities.trace.query.byScope({ entity: "trace", scope });
2378
- } else {
2379
- this.logger.warn("Performing a scan operation on traces - consider using a more specific query");
2380
- query = this.service.entities.trace.scan;
2381
- }
2382
- let items = [];
2383
- let cursor = null;
2384
- let pagesFetched = 0;
2385
- const startPage = page > 0 ? page : 1;
2386
- do {
2387
- const results = await query.go({ cursor, limit: perPage });
2388
- pagesFetched++;
2389
- if (pagesFetched === startPage) {
2390
- items = results.data;
2391
- break;
2392
- }
2393
- cursor = results.cursor;
2394
- if (!cursor && results.data.length > 0 && pagesFetched < startPage) {
2395
- break;
2396
- }
2397
- } while (cursor && pagesFetched < startPage);
2398
- return items;
2399
- } catch (error) {
2400
- throw new MastraError(
2401
- {
2402
- id: "STORAGE_DYNAMODB_STORE_GET_TRACES_FAILED",
2403
- domain: ErrorDomain.STORAGE,
2404
- category: ErrorCategory.THIRD_PARTY
2405
- },
2406
- error
2407
- );
2408
- }
2409
- }
2410
- async batchTraceInsert({ records }) {
2411
- this.logger.debug("Batch inserting traces", { count: records.length });
2412
- if (!records.length) {
2413
- return;
2414
- }
2415
- try {
2416
- const recordsToSave = records.map((rec) => ({ entity: "trace", ...rec }));
2417
- await this.operations.batchInsert({
2418
- tableName: TABLE_TRACES,
2419
- records: recordsToSave
2420
- // Pass records with 'entity' included
2421
- });
2422
- } catch (error) {
2423
- throw new MastraError(
2424
- {
2425
- id: "STORAGE_DYNAMODB_STORE_BATCH_TRACE_INSERT_FAILED",
2426
- domain: ErrorDomain.STORAGE,
2427
- category: ErrorCategory.THIRD_PARTY,
2428
- details: { count: records.length }
2429
- },
2430
- error
2431
- );
2432
- }
2433
- }
2434
- async getTracesPaginated(args) {
2435
- const { name, scope, page = 0, perPage = 100, attributes, filters, dateRange } = args;
2436
- this.logger.debug("Getting traces with pagination", { name, scope, page, perPage, attributes, filters, dateRange });
2214
+ async listScoresBySpan({
2215
+ traceId,
2216
+ spanId,
2217
+ pagination
2218
+ }) {
2219
+ this.logger.debug("Getting scores by span", { traceId, spanId, pagination });
2437
2220
  try {
2438
- let query;
2439
- if (name) {
2440
- query = this.service.entities.trace.query.byName({ entity: "trace", name });
2441
- } else if (scope) {
2442
- query = this.service.entities.trace.query.byScope({ entity: "trace", scope });
2443
- } else {
2444
- this.logger.warn("Performing a scan operation on traces - consider using a more specific query");
2445
- query = this.service.entities.trace.scan;
2446
- }
2447
- const results = await query.go({
2448
- order: "desc",
2449
- pages: "all"
2450
- // Get all pages to apply filtering and pagination
2451
- });
2452
- if (!results.data.length) {
2453
- return {
2454
- traces: [],
2455
- total: 0,
2221
+ const query = this.service.entities.score.query.bySpan({ entity: "score", traceId, spanId });
2222
+ const results = await query.go();
2223
+ const allScores = results.data.map((data) => this.parseScoreData(data));
2224
+ allScores.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
2225
+ const { page, perPage: perPageInput } = pagination;
2226
+ const perPage = normalizePerPage(perPageInput, Number.MAX_SAFE_INTEGER);
2227
+ const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
2228
+ const total = allScores.length;
2229
+ const end = perPageInput === false ? allScores.length : start + perPage;
2230
+ const paginatedScores = allScores.slice(start, end);
2231
+ return {
2232
+ scores: paginatedScores,
2233
+ pagination: {
2234
+ total,
2456
2235
  page,
2457
- perPage,
2458
- hasMore: false
2459
- };
2460
- }
2461
- let filteredData = results.data;
2462
- if (attributes) {
2463
- filteredData = filteredData.filter((item) => {
2464
- try {
2465
- let itemAttributes = {};
2466
- if (item.attributes) {
2467
- if (typeof item.attributes === "string") {
2468
- if (item.attributes === "[object Object]") {
2469
- itemAttributes = {};
2470
- } else {
2471
- try {
2472
- itemAttributes = JSON.parse(item.attributes);
2473
- } catch {
2474
- itemAttributes = {};
2475
- }
2476
- }
2477
- } else if (typeof item.attributes === "object") {
2478
- itemAttributes = item.attributes;
2479
- }
2480
- }
2481
- return Object.entries(attributes).every(([key, value]) => itemAttributes[key] === value);
2482
- } catch (e) {
2483
- this.logger.warn("Failed to parse attributes during filtering", { item, error: e });
2484
- return false;
2485
- }
2486
- });
2487
- }
2488
- if (dateRange?.start) {
2489
- filteredData = filteredData.filter((item) => {
2490
- const itemDate = new Date(item.createdAt);
2491
- return itemDate >= dateRange.start;
2492
- });
2493
- }
2494
- if (dateRange?.end) {
2495
- filteredData = filteredData.filter((item) => {
2496
- const itemDate = new Date(item.createdAt);
2497
- return itemDate <= dateRange.end;
2498
- });
2499
- }
2500
- const total = filteredData.length;
2501
- const start = page * perPage;
2502
- const end = start + perPage;
2503
- const paginatedData = filteredData.slice(start, end);
2504
- const traces = paginatedData.map((item) => {
2505
- let attributes2;
2506
- if (item.attributes) {
2507
- if (typeof item.attributes === "string") {
2508
- if (item.attributes === "[object Object]") {
2509
- attributes2 = void 0;
2510
- } else {
2511
- try {
2512
- attributes2 = JSON.parse(item.attributes);
2513
- } catch {
2514
- attributes2 = void 0;
2515
- }
2516
- }
2517
- } else if (typeof item.attributes === "object") {
2518
- attributes2 = item.attributes;
2519
- }
2520
- }
2521
- let status;
2522
- if (item.status) {
2523
- if (typeof item.status === "string") {
2524
- try {
2525
- status = JSON.parse(item.status);
2526
- } catch {
2527
- status = void 0;
2528
- }
2529
- } else if (typeof item.status === "object") {
2530
- status = item.status;
2531
- }
2236
+ perPage: perPageForResponse,
2237
+ hasMore: end < total
2532
2238
  }
2533
- let events;
2534
- if (item.events) {
2535
- if (typeof item.events === "string") {
2536
- try {
2537
- events = JSON.parse(item.events);
2538
- } catch {
2539
- events = void 0;
2540
- }
2541
- } else if (Array.isArray(item.events)) {
2542
- events = item.events;
2543
- }
2544
- }
2545
- let links;
2546
- if (item.links) {
2547
- if (typeof item.links === "string") {
2548
- try {
2549
- links = JSON.parse(item.links);
2550
- } catch {
2551
- links = void 0;
2552
- }
2553
- } else if (Array.isArray(item.links)) {
2554
- links = item.links;
2555
- }
2556
- }
2557
- return {
2558
- id: item.id,
2559
- parentSpanId: item.parentSpanId,
2560
- name: item.name,
2561
- traceId: item.traceId,
2562
- scope: item.scope,
2563
- kind: item.kind,
2564
- attributes: attributes2,
2565
- status,
2566
- events,
2567
- links,
2568
- other: item.other,
2569
- startTime: item.startTime,
2570
- endTime: item.endTime,
2571
- createdAt: item.createdAt
2572
- };
2573
- });
2574
- return {
2575
- traces,
2576
- total,
2577
- page,
2578
- perPage,
2579
- hasMore: end < total
2580
2239
  };
2581
2240
  } catch (error) {
2582
2241
  throw new MastraError(
2583
2242
  {
2584
- id: "STORAGE_DYNAMODB_STORE_GET_TRACES_PAGINATED_FAILED",
2243
+ id: createStorageErrorId("DYNAMODB", "LIST_SCORES_BY_SPAN", "FAILED"),
2585
2244
  domain: ErrorDomain.STORAGE,
2586
- category: ErrorCategory.THIRD_PARTY
2245
+ category: ErrorCategory.THIRD_PARTY,
2246
+ details: { traceId, spanId, page: pagination.page, perPage: pagination.perPage }
2587
2247
  },
2588
2248
  error
2589
2249
  );
@@ -2611,7 +2271,7 @@ var WorkflowStorageDynamoDB = class extends WorkflowsStorage {
2611
2271
  // runId,
2612
2272
  // stepId,
2613
2273
  // result,
2614
- // runtimeContext,
2274
+ // requestContext,
2615
2275
  }) {
2616
2276
  throw new Error("Method not implemented.");
2617
2277
  }
@@ -2638,7 +2298,6 @@ var WorkflowStorageDynamoDB = class extends WorkflowsStorage {
2638
2298
  workflow_name: workflowName,
2639
2299
  run_id: runId,
2640
2300
  snapshot: JSON.stringify(snapshot),
2641
- // Stringify the snapshot object
2642
2301
  createdAt: now,
2643
2302
  updatedAt: now,
2644
2303
  resourceId
@@ -2647,7 +2306,7 @@ var WorkflowStorageDynamoDB = class extends WorkflowsStorage {
2647
2306
  } catch (error) {
2648
2307
  throw new MastraError(
2649
2308
  {
2650
- id: "STORAGE_DYNAMODB_STORE_PERSIST_WORKFLOW_SNAPSHOT_FAILED",
2309
+ id: createStorageErrorId("DYNAMODB", "PERSIST_WORKFLOW_SNAPSHOT", "FAILED"),
2651
2310
  domain: ErrorDomain.STORAGE,
2652
2311
  category: ErrorCategory.THIRD_PARTY,
2653
2312
  details: { workflowName, runId }
@@ -2675,7 +2334,7 @@ var WorkflowStorageDynamoDB = class extends WorkflowsStorage {
2675
2334
  } catch (error) {
2676
2335
  throw new MastraError(
2677
2336
  {
2678
- id: "STORAGE_DYNAMODB_STORE_LOAD_WORKFLOW_SNAPSHOT_FAILED",
2337
+ id: createStorageErrorId("DYNAMODB", "LOAD_WORKFLOW_SNAPSHOT", "FAILED"),
2679
2338
  domain: ErrorDomain.STORAGE,
2680
2339
  category: ErrorCategory.THIRD_PARTY,
2681
2340
  details: { workflowName, runId }
@@ -2684,11 +2343,24 @@ var WorkflowStorageDynamoDB = class extends WorkflowsStorage {
2684
2343
  );
2685
2344
  }
2686
2345
  }
2687
- async getWorkflowRuns(args) {
2346
+ async listWorkflowRuns(args) {
2688
2347
  this.logger.debug("Getting workflow runs", { args });
2689
2348
  try {
2690
- const limit = args?.limit || 10;
2691
- const offset = args?.offset || 0;
2349
+ const perPage = args?.perPage !== void 0 ? args.perPage : 10;
2350
+ const page = args?.page !== void 0 ? args.page : 0;
2351
+ if (page < 0) {
2352
+ throw new MastraError(
2353
+ {
2354
+ id: createStorageErrorId("DYNAMODB", "LIST_WORKFLOW_RUNS", "INVALID_PAGE"),
2355
+ domain: ErrorDomain.STORAGE,
2356
+ category: ErrorCategory.USER,
2357
+ details: { page }
2358
+ },
2359
+ new Error("page must be >= 0")
2360
+ );
2361
+ }
2362
+ const normalizedPerPage = normalizePerPage(perPage, 10);
2363
+ const offset = page * normalizedPerPage;
2692
2364
  let query;
2693
2365
  if (args?.workflowName) {
2694
2366
  query = this.service.entities.workflow_snapshot.query.primary({
@@ -2710,6 +2382,11 @@ var WorkflowStorageDynamoDB = class extends WorkflowsStorage {
2710
2382
  });
2711
2383
  if (pageResults.data && pageResults.data.length > 0) {
2712
2384
  let pageFilteredData = pageResults.data;
2385
+ if (args?.status) {
2386
+ pageFilteredData = pageFilteredData.filter((snapshot) => {
2387
+ return snapshot.snapshot.status === args.status;
2388
+ });
2389
+ }
2713
2390
  if (args?.fromDate || args?.toDate) {
2714
2391
  pageFilteredData = pageFilteredData.filter((snapshot) => {
2715
2392
  const createdAt = new Date(snapshot.createdAt);
@@ -2735,7 +2412,7 @@ var WorkflowStorageDynamoDB = class extends WorkflowsStorage {
2735
2412
  return { runs: [], total: 0 };
2736
2413
  }
2737
2414
  const total = allMatchingSnapshots.length;
2738
- const paginatedData = allMatchingSnapshots.slice(offset, offset + limit);
2415
+ const paginatedData = allMatchingSnapshots.slice(offset, offset + normalizedPerPage);
2739
2416
  const runs = paginatedData.map((snapshot) => formatWorkflowRun(snapshot));
2740
2417
  return {
2741
2418
  runs,
@@ -2744,7 +2421,7 @@ var WorkflowStorageDynamoDB = class extends WorkflowsStorage {
2744
2421
  } catch (error) {
2745
2422
  throw new MastraError(
2746
2423
  {
2747
- id: "STORAGE_DYNAMODB_STORE_GET_WORKFLOW_RUNS_FAILED",
2424
+ id: createStorageErrorId("DYNAMODB", "LIST_WORKFLOW_RUNS", "FAILED"),
2748
2425
  domain: ErrorDomain.STORAGE,
2749
2426
  category: ErrorCategory.THIRD_PARTY,
2750
2427
  details: { workflowName: args?.workflowName || "", resourceId: args?.resourceId || "" }
@@ -2756,8 +2433,6 @@ var WorkflowStorageDynamoDB = class extends WorkflowsStorage {
2756
2433
  async getWorkflowRunById(args) {
2757
2434
  const { runId, workflowName } = args;
2758
2435
  this.logger.debug("Getting workflow run by ID", { runId, workflowName });
2759
- console.log("workflowName", workflowName);
2760
- console.log("runId", runId);
2761
2436
  try {
2762
2437
  if (workflowName) {
2763
2438
  this.logger.debug("WorkflowName provided, using direct GET operation.");
@@ -2767,7 +2442,6 @@ var WorkflowStorageDynamoDB = class extends WorkflowsStorage {
2767
2442
  workflow_name: workflowName,
2768
2443
  run_id: runId
2769
2444
  }).go();
2770
- console.log("result", result2);
2771
2445
  if (!result2.data) {
2772
2446
  return null;
2773
2447
  }
@@ -2801,7 +2475,7 @@ var WorkflowStorageDynamoDB = class extends WorkflowsStorage {
2801
2475
  } catch (error) {
2802
2476
  throw new MastraError(
2803
2477
  {
2804
- id: "STORAGE_DYNAMODB_STORE_GET_WORKFLOW_RUN_BY_ID_FAILED",
2478
+ id: createStorageErrorId("DYNAMODB", "GET_WORKFLOW_RUN_BY_ID", "FAILED"),
2805
2479
  domain: ErrorDomain.STORAGE,
2806
2480
  category: ErrorCategory.THIRD_PARTY,
2807
2481
  details: { runId, workflowName: args?.workflowName || "" }
@@ -2810,6 +2484,26 @@ var WorkflowStorageDynamoDB = class extends WorkflowsStorage {
2810
2484
  );
2811
2485
  }
2812
2486
  }
2487
+ async deleteWorkflowRunById({ runId, workflowName }) {
2488
+ this.logger.debug("Deleting workflow run by ID", { runId, workflowName });
2489
+ try {
2490
+ await this.service.entities.workflow_snapshot.delete({
2491
+ entity: "workflow_snapshot",
2492
+ workflow_name: workflowName,
2493
+ run_id: runId
2494
+ }).go();
2495
+ } catch (error) {
2496
+ throw new MastraError(
2497
+ {
2498
+ id: createStorageErrorId("DYNAMODB", "DELETE_WORKFLOW_RUN_BY_ID", "FAILED"),
2499
+ domain: ErrorDomain.STORAGE,
2500
+ category: ErrorCategory.THIRD_PARTY,
2501
+ details: { runId, workflowName }
2502
+ },
2503
+ error
2504
+ );
2505
+ }
2506
+ }
2813
2507
  };
2814
2508
 
2815
2509
  // src/storage/index.ts
@@ -2820,7 +2514,7 @@ var DynamoDBStore = class extends MastraStorage {
2820
2514
  hasInitialized = null;
2821
2515
  stores;
2822
2516
  constructor({ name, config }) {
2823
- super({ name });
2517
+ super({ id: config.id, name, disableInit: config.disableInit });
2824
2518
  try {
2825
2519
  if (!config.tableName || typeof config.tableName !== "string" || config.tableName.trim() === "") {
2826
2520
  throw new Error("DynamoDBStore: config.tableName must be provided and cannot be empty.");
@@ -2843,14 +2537,11 @@ var DynamoDBStore = class extends MastraStorage {
2843
2537
  tableName: this.tableName,
2844
2538
  client: this.client
2845
2539
  });
2846
- const traces = new TracesStorageDynamoDB({ service: this.service, operations });
2847
2540
  const workflows = new WorkflowStorageDynamoDB({ service: this.service });
2848
2541
  const memory = new MemoryStorageDynamoDB({ service: this.service });
2849
2542
  const scores = new ScoresStorageDynamoDB({ service: this.service });
2850
2543
  this.stores = {
2851
2544
  operations,
2852
- legacyEvals: new LegacyEvalsDynamoDB({ service: this.service, tableName: this.tableName }),
2853
- traces,
2854
2545
  workflows,
2855
2546
  memory,
2856
2547
  scores
@@ -2858,7 +2549,7 @@ var DynamoDBStore = class extends MastraStorage {
2858
2549
  } catch (error) {
2859
2550
  throw new MastraError(
2860
2551
  {
2861
- id: "STORAGE_DYNAMODB_STORE_CONSTRUCTOR_FAILED",
2552
+ id: createStorageErrorId("DYNAMODB", "CONSTRUCTOR", "FAILED"),
2862
2553
  domain: ErrorDomain.STORAGE,
2863
2554
  category: ErrorCategory.USER
2864
2555
  },
@@ -2872,7 +2563,8 @@ var DynamoDBStore = class extends MastraStorage {
2872
2563
  resourceWorkingMemory: true,
2873
2564
  hasColumn: false,
2874
2565
  createTable: false,
2875
- deleteMessages: false
2566
+ deleteMessages: false,
2567
+ listScoresBySpan: true
2876
2568
  };
2877
2569
  }
2878
2570
  /**
@@ -2893,7 +2585,7 @@ var DynamoDBStore = class extends MastraStorage {
2893
2585
  }
2894
2586
  throw new MastraError(
2895
2587
  {
2896
- id: "STORAGE_DYNAMODB_STORE_VALIDATE_TABLE_EXISTS_FAILED",
2588
+ id: createStorageErrorId("DYNAMODB", "VALIDATE_TABLE_EXISTS", "FAILED"),
2897
2589
  domain: ErrorDomain.STORAGE,
2898
2590
  category: ErrorCategory.THIRD_PARTY,
2899
2591
  details: { tableName: this.tableName }
@@ -2916,7 +2608,7 @@ var DynamoDBStore = class extends MastraStorage {
2916
2608
  } catch (error) {
2917
2609
  throw new MastraError(
2918
2610
  {
2919
- id: "STORAGE_DYNAMODB_STORE_INIT_FAILED",
2611
+ id: createStorageErrorId("DYNAMODB", "INIT", "FAILED"),
2920
2612
  domain: ErrorDomain.STORAGE,
2921
2613
  category: ErrorCategory.THIRD_PARTY,
2922
2614
  details: { tableName: this.tableName }
@@ -2967,9 +2659,6 @@ var DynamoDBStore = class extends MastraStorage {
2967
2659
  async getThreadById({ threadId }) {
2968
2660
  return this.stores.memory.getThreadById({ threadId });
2969
2661
  }
2970
- async getThreadsByResourceId(args) {
2971
- return this.stores.memory.getThreadsByResourceId(args);
2972
- }
2973
2662
  async saveThread({ thread }) {
2974
2663
  return this.stores.memory.saveThread({ thread });
2975
2664
  }
@@ -2983,51 +2672,24 @@ var DynamoDBStore = class extends MastraStorage {
2983
2672
  async deleteThread({ threadId }) {
2984
2673
  return this.stores.memory.deleteThread({ threadId });
2985
2674
  }
2986
- async getMessages({
2987
- threadId,
2988
- resourceId,
2989
- selectBy,
2990
- format
2991
- }) {
2992
- return this.stores.memory.getMessages({ threadId, resourceId, selectBy, format });
2993
- }
2994
- async getMessagesById({
2995
- messageIds,
2996
- format
2997
- }) {
2998
- return this.stores.memory.getMessagesById({ messageIds, format });
2675
+ async listMessagesById(args) {
2676
+ return this.stores.memory.listMessagesById(args);
2999
2677
  }
3000
2678
  async saveMessages(args) {
3001
2679
  return this.stores.memory.saveMessages(args);
3002
2680
  }
3003
- async getThreadsByResourceIdPaginated(args) {
3004
- return this.stores.memory.getThreadsByResourceIdPaginated(args);
3005
- }
3006
- async getMessagesPaginated(args) {
3007
- return this.stores.memory.getMessagesPaginated(args);
3008
- }
3009
2681
  async updateMessages(_args) {
3010
2682
  return this.stores.memory.updateMessages(_args);
3011
2683
  }
3012
- // Trace operations
3013
- async getTraces(args) {
3014
- return this.stores.traces.getTraces(args);
3015
- }
3016
- async batchTraceInsert({ records }) {
3017
- return this.stores.traces.batchTraceInsert({ records });
3018
- }
3019
- async getTracesPaginated(_args) {
3020
- return this.stores.traces.getTracesPaginated(_args);
3021
- }
3022
2684
  // Workflow operations
3023
2685
  async updateWorkflowResults({
3024
2686
  workflowName,
3025
2687
  runId,
3026
2688
  stepId,
3027
2689
  result,
3028
- runtimeContext
2690
+ requestContext
3029
2691
  }) {
3030
- return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result, runtimeContext });
2692
+ return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result, requestContext });
3031
2693
  }
3032
2694
  async updateWorkflowState({
3033
2695
  workflowName,
@@ -3050,12 +2712,15 @@ var DynamoDBStore = class extends MastraStorage {
3050
2712
  }) {
3051
2713
  return this.stores.workflows.loadWorkflowSnapshot({ workflowName, runId });
3052
2714
  }
3053
- async getWorkflowRuns(args) {
3054
- return this.stores.workflows.getWorkflowRuns(args);
2715
+ async listWorkflowRuns(args) {
2716
+ return this.stores.workflows.listWorkflowRuns(args);
3055
2717
  }
3056
2718
  async getWorkflowRunById(args) {
3057
2719
  return this.stores.workflows.getWorkflowRunById(args);
3058
2720
  }
2721
+ async deleteWorkflowRunById({ runId, workflowName }) {
2722
+ return this.stores.workflows.deleteWorkflowRunById({ runId, workflowName });
2723
+ }
3059
2724
  async getResourceById({ resourceId }) {
3060
2725
  return this.stores.memory.getResourceById({ resourceId });
3061
2726
  }
@@ -3069,13 +2734,6 @@ var DynamoDBStore = class extends MastraStorage {
3069
2734
  }) {
3070
2735
  return this.stores.memory.updateResource({ resourceId, workingMemory, metadata });
3071
2736
  }
3072
- // Eval operations
3073
- async getEvalsByAgentName(agentName, type) {
3074
- return this.stores.legacyEvals.getEvalsByAgentName(agentName, type);
3075
- }
3076
- async getEvals(options) {
3077
- return this.stores.legacyEvals.getEvals(options);
3078
- }
3079
2737
  /**
3080
2738
  * Closes the DynamoDB client connection and cleans up resources.
3081
2739
  * Should be called when the store is no longer needed, e.g., at the end of tests or application shutdown.
@@ -3088,7 +2746,7 @@ var DynamoDBStore = class extends MastraStorage {
3088
2746
  } catch (error) {
3089
2747
  throw new MastraError(
3090
2748
  {
3091
- id: "STORAGE_DYNAMODB_STORE_CLOSE_FAILED",
2749
+ id: createStorageErrorId("DYNAMODB", "CLOSE", "FAILED"),
3092
2750
  domain: ErrorDomain.STORAGE,
3093
2751
  category: ErrorCategory.THIRD_PARTY
3094
2752
  },
@@ -3102,34 +2760,41 @@ var DynamoDBStore = class extends MastraStorage {
3102
2760
  async getScoreById({ id: _id }) {
3103
2761
  return this.stores.scores.getScoreById({ id: _id });
3104
2762
  }
3105
- async saveScore(_score) {
3106
- return this.stores.scores.saveScore(_score);
2763
+ async saveScore(score) {
2764
+ return this.stores.scores.saveScore(score);
3107
2765
  }
3108
- async getScoresByRunId({
2766
+ async listScoresByRunId({
3109
2767
  runId: _runId,
3110
2768
  pagination: _pagination
3111
2769
  }) {
3112
- return this.stores.scores.getScoresByRunId({ runId: _runId, pagination: _pagination });
2770
+ return this.stores.scores.listScoresByRunId({ runId: _runId, pagination: _pagination });
3113
2771
  }
3114
- async getScoresByEntityId({
2772
+ async listScoresByEntityId({
3115
2773
  entityId: _entityId,
3116
2774
  entityType: _entityType,
3117
2775
  pagination: _pagination
3118
2776
  }) {
3119
- return this.stores.scores.getScoresByEntityId({
2777
+ return this.stores.scores.listScoresByEntityId({
3120
2778
  entityId: _entityId,
3121
2779
  entityType: _entityType,
3122
2780
  pagination: _pagination
3123
2781
  });
3124
2782
  }
3125
- async getScoresByScorerId({
2783
+ async listScoresByScorerId({
3126
2784
  scorerId,
3127
2785
  source,
3128
2786
  entityId,
3129
2787
  entityType,
3130
2788
  pagination
3131
2789
  }) {
3132
- return this.stores.scores.getScoresByScorerId({ scorerId, source, entityId, entityType, pagination });
2790
+ return this.stores.scores.listScoresByScorerId({ scorerId, source, entityId, entityType, pagination });
2791
+ }
2792
+ async listScoresBySpan({
2793
+ traceId,
2794
+ spanId,
2795
+ pagination
2796
+ }) {
2797
+ return this.stores.scores.listScoresBySpan({ traceId, spanId, pagination });
3133
2798
  }
3134
2799
  };
3135
2800