@mastra/mongodb 1.5.4 → 1.5.5

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
@@ -12,7 +12,7 @@ import { saveScorePayloadSchema } from '@mastra/core/evals';
12
12
 
13
13
  // package.json
14
14
  var package_default = {
15
- version: "1.5.4"};
15
+ version: "1.5.5"};
16
16
  var MongoDBFilterTranslator = class extends BaseFilterTranslator {
17
17
  getSupportedOperators() {
18
18
  return {
@@ -2905,22 +2905,37 @@ var MemoryStorageMongoDB = class _MemoryStorageMongoDB extends MemoryStorage {
2905
2905
  if (row.type && row.type !== "v2") result.type = row.type;
2906
2906
  return result;
2907
2907
  }
2908
+ _sortMessages(messages, field, direction) {
2909
+ return messages.sort((a, b) => {
2910
+ const isDateField = field === "createdAt" || field === "updatedAt";
2911
+ const aValue = isDateField ? new Date(a[field]).getTime() : a[field];
2912
+ const bValue = isDateField ? new Date(b[field]).getTime() : b[field];
2913
+ if (typeof aValue === "number" && typeof bValue === "number") {
2914
+ return direction === "ASC" ? aValue - bValue : bValue - aValue;
2915
+ }
2916
+ return direction === "ASC" ? String(aValue).localeCompare(String(bValue)) : String(bValue).localeCompare(String(aValue));
2917
+ });
2918
+ }
2908
2919
  async _getIncludedMessages({ include }) {
2909
2920
  if (!include || include.length === 0) return null;
2910
2921
  const collection = await this.getCollection(TABLE_MESSAGES);
2922
+ const targetIds = include.map((inc) => inc.id).filter(Boolean);
2923
+ if (targetIds.length === 0) return null;
2924
+ const targetDocs = await collection.find({ id: { $in: targetIds } }, { projection: { id: 1, thread_id: 1, createdAt: 1 } }).toArray();
2925
+ if (targetDocs.length === 0) return null;
2926
+ const targetMap = new Map(
2927
+ targetDocs.map((doc) => [doc.id, { threadId: doc.thread_id, createdAt: doc.createdAt }])
2928
+ );
2911
2929
  const includedMessages = [];
2912
2930
  for (const inc of include) {
2913
2931
  const { id, withPreviousMessages = 0, withNextMessages = 0 } = inc;
2914
- const targetMessage = await collection.findOne({ id });
2915
- if (!targetMessage) continue;
2916
- const messageThreadId = targetMessage.thread_id;
2917
- const allMessages = await collection.find({ thread_id: messageThreadId }).sort({ createdAt: 1 }).toArray();
2918
- const targetIndex = allMessages.findIndex((msg) => msg.id === id);
2919
- if (targetIndex === -1) continue;
2920
- const startIndex = Math.max(0, targetIndex - withPreviousMessages);
2921
- const endIndex = Math.min(allMessages.length - 1, targetIndex + withNextMessages);
2922
- for (let i = startIndex; i <= endIndex; i++) {
2923
- includedMessages.push(allMessages[i]);
2932
+ const target = targetMap.get(id);
2933
+ if (!target) continue;
2934
+ const prevMessages = await collection.find({ thread_id: target.threadId, createdAt: { $lte: target.createdAt } }).sort({ createdAt: -1, id: -1 }).limit(withPreviousMessages + 1).toArray();
2935
+ includedMessages.push(...prevMessages);
2936
+ if (withNextMessages > 0) {
2937
+ const nextMessages = await collection.find({ thread_id: target.threadId, createdAt: { $gt: target.createdAt } }).sort({ createdAt: 1, id: 1 }).limit(withNextMessages).toArray();
2938
+ includedMessages.push(...nextMessages);
2924
2939
  }
2925
2940
  }
2926
2941
  const seen = /* @__PURE__ */ new Set();
@@ -2996,6 +3011,20 @@ var MemoryStorageMongoDB = class _MemoryStorageMongoDB extends MemoryStorage {
2996
3011
  const endOp = filter.dateRange.endExclusive ? "$lt" : "$lte";
2997
3012
  query.createdAt = { ...query.createdAt, [endOp]: formatDateForMongoDB(filter.dateRange.end) };
2998
3013
  }
3014
+ if (perPage === 0 && (!include || include.length === 0)) {
3015
+ return { messages: [], total: 0, page, perPage: perPageForResponse, hasMore: false };
3016
+ }
3017
+ if (perPage === 0 && include && include.length > 0) {
3018
+ const includeMessages = await this._getIncludedMessages({ include });
3019
+ const list2 = new MessageList().add(includeMessages ?? [], "memory");
3020
+ return {
3021
+ messages: this._sortMessages(list2.get.all.db(), field, direction),
3022
+ total: 0,
3023
+ page,
3024
+ perPage: perPageForResponse,
3025
+ hasMore: false
3026
+ };
3027
+ }
2999
3028
  const total = await collection.countDocuments(query);
3000
3029
  const messages = [];
3001
3030
  if (perPage !== 0) {
@@ -3029,16 +3058,7 @@ var MemoryStorageMongoDB = class _MemoryStorageMongoDB extends MemoryStorage {
3029
3058
  }
3030
3059
  }
3031
3060
  const list = new MessageList().add(messages, "memory");
3032
- let finalMessages = list.get.all.db();
3033
- finalMessages = finalMessages.sort((a, b) => {
3034
- const isDateField = field === "createdAt" || field === "updatedAt";
3035
- const aValue = isDateField ? new Date(a[field]).getTime() : a[field];
3036
- const bValue = isDateField ? new Date(b[field]).getTime() : b[field];
3037
- if (typeof aValue === "number" && typeof bValue === "number") {
3038
- return direction === "ASC" ? aValue - bValue : bValue - aValue;
3039
- }
3040
- return direction === "ASC" ? String(aValue).localeCompare(String(bValue)) : String(bValue).localeCompare(String(aValue));
3041
- });
3061
+ const finalMessages = this._sortMessages(list.get.all.db(), field, direction);
3042
3062
  const threadIdSet = new Set(threadIds);
3043
3063
  const returnedThreadMessageIds = new Set(
3044
3064
  finalMessages.filter((m) => m.threadId && threadIdSet.has(m.threadId)).map((m) => m.id)
@@ -3116,6 +3136,23 @@ var MemoryStorageMongoDB = class _MemoryStorageMongoDB extends MemoryStorage {
3116
3136
  const endOp = filter.dateRange.endExclusive ? "$lt" : "$lte";
3117
3137
  query.createdAt = { ...query.createdAt, [endOp]: formatDateForMongoDB(filter.dateRange.end) };
3118
3138
  }
3139
+ if (perPage === 0 && (!include || include.length === 0)) {
3140
+ return { messages: [], total: 0, page, perPage: perPageForResponse, hasMore: false };
3141
+ }
3142
+ if (perPage === 0 && include && include.length > 0) {
3143
+ const includeMessages = await this._getIncludedMessages({ include });
3144
+ if (!includeMessages || includeMessages.length === 0) {
3145
+ return { messages: [], total: 0, page, perPage: perPageForResponse, hasMore: false };
3146
+ }
3147
+ const list2 = new MessageList().add(includeMessages, "memory");
3148
+ return {
3149
+ messages: this._sortMessages(list2.get.all.db(), field, direction),
3150
+ total: 0,
3151
+ page,
3152
+ perPage: perPageForResponse,
3153
+ hasMore: false
3154
+ };
3155
+ }
3119
3156
  const total = await collection.countDocuments(query);
3120
3157
  const messages = [];
3121
3158
  if (perPage !== 0) {
@@ -3149,16 +3186,7 @@ var MemoryStorageMongoDB = class _MemoryStorageMongoDB extends MemoryStorage {
3149
3186
  }
3150
3187
  }
3151
3188
  const list = new MessageList().add(messages, "memory");
3152
- let finalMessages = list.get.all.db();
3153
- finalMessages = finalMessages.sort((a, b) => {
3154
- const isDateField = field === "createdAt" || field === "updatedAt";
3155
- const aValue = isDateField ? new Date(a[field]).getTime() : a[field];
3156
- const bValue = isDateField ? new Date(b[field]).getTime() : b[field];
3157
- if (typeof aValue === "number" && typeof bValue === "number") {
3158
- return direction === "ASC" ? aValue - bValue : bValue - aValue;
3159
- }
3160
- return direction === "ASC" ? String(aValue).localeCompare(String(bValue)) : String(bValue).localeCompare(String(aValue));
3161
- });
3189
+ const finalMessages = this._sortMessages(list.get.all.db(), field, direction);
3162
3190
  const hasMore = perPageInput !== false && offset + perPage < total;
3163
3191
  return {
3164
3192
  messages: finalMessages,
@@ -4993,7 +5021,8 @@ Note: This migration may take some time for large collections.
4993
5021
  }
4994
5022
  async listTraces(args) {
4995
5023
  const { filters, pagination, orderBy } = listTracesArgsSchema.parse(args);
4996
- const { page, perPage } = pagination;
5024
+ const page = pagination?.page ?? 0;
5025
+ const perPage = pagination?.perPage ?? 10;
4997
5026
  try {
4998
5027
  const collection = await this.getCollection(TABLE_SPANS);
4999
5028
  const mongoFilter = {
@@ -5098,8 +5127,8 @@ Note: This migration may take some time for large collections.
5098
5127
  if (andConditions.length) {
5099
5128
  mongoFilter.$and = andConditions;
5100
5129
  }
5101
- const sortField = orderBy.field;
5102
- const sortDirection = orderBy.direction === "ASC" ? 1 : -1;
5130
+ const sortField = orderBy?.field ?? "startedAt";
5131
+ const sortDirection = (orderBy?.direction ?? "DESC") === "ASC" ? 1 : -1;
5103
5132
  if (filters?.hasChildError !== void 0) {
5104
5133
  const pipeline = [
5105
5134
  { $match: mongoFilter },