@mastra/mongodb 1.0.0-beta.2 → 1.0.0-beta.3

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
@@ -3,7 +3,7 @@ import { MastraVector } from '@mastra/core/vector';
3
3
  import { MongoClient } from 'mongodb';
4
4
  import { v4 } from 'uuid';
5
5
  import { BaseFilterTranslator } from '@mastra/core/vector/filter';
6
- import { MastraStorage, StoreOperations, TABLE_SCHEMAS, safelyParseJSON, MemoryStorage, TABLE_MESSAGES, normalizePerPage, calculatePagination, TABLE_THREADS, TABLE_RESOURCES, ScoresStorage, TABLE_SCORERS, WorkflowsStorage, TABLE_WORKFLOW_SNAPSHOT, ObservabilityStorage, TABLE_SPANS } from '@mastra/core/storage';
6
+ import { MastraStorage, StoreOperations, TABLE_SCHEMAS, safelyParseJSON, MemoryStorage, TABLE_MESSAGES, normalizePerPage, calculatePagination, TABLE_THREADS, TABLE_RESOURCES, ScoresStorage, TABLE_SCORERS, WorkflowsStorage, TABLE_WORKFLOW_SNAPSHOT, ObservabilityStorage, TABLE_SPANS, transformScoreRow as transformScoreRow$1 } from '@mastra/core/storage';
7
7
  import { MessageList } from '@mastra/core/agent';
8
8
  import { saveScorePayloadSchema } from '@mastra/core/evals';
9
9
 
@@ -11,7 +11,7 @@ import { saveScorePayloadSchema } from '@mastra/core/evals';
11
11
 
12
12
  // package.json
13
13
  var package_default = {
14
- version: "1.0.0-beta.2"};
14
+ version: "1.0.0-beta.3"};
15
15
  var MongoDBFilterTranslator = class extends BaseFilterTranslator {
16
16
  getSupportedOperators() {
17
17
  return {
@@ -836,8 +836,6 @@ var MongoDBConnector = class _MongoDBConnector {
836
836
  }
837
837
  }
838
838
  };
839
-
840
- // src/storage/domains/utils.ts
841
839
  function formatDateForMongoDB(date) {
842
840
  return typeof date === "string" ? new Date(date) : date;
843
841
  }
@@ -868,18 +866,16 @@ var MemoryStorageMongoDB = class extends MemoryStorage {
868
866
  if (row.type && row.type !== "v2") result.type = row.type;
869
867
  return result;
870
868
  }
871
- async _getIncludedMessages({
872
- threadId,
873
- include
874
- }) {
875
- if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
876
- if (!include) return null;
869
+ async _getIncludedMessages({ include }) {
870
+ if (!include || include.length === 0) return null;
877
871
  const collection = await this.operations.getCollection(TABLE_MESSAGES);
878
872
  const includedMessages = [];
879
873
  for (const inc of include) {
880
874
  const { id, withPreviousMessages = 0, withNextMessages = 0 } = inc;
881
- const searchThreadId = inc.threadId || threadId;
882
- const allMessages = await collection.find({ thread_id: searchThreadId }).sort({ createdAt: 1 }).toArray();
875
+ const targetMessage = await collection.findOne({ id });
876
+ if (!targetMessage) continue;
877
+ const messageThreadId = targetMessage.thread_id;
878
+ const allMessages = await collection.find({ thread_id: messageThreadId }).sort({ createdAt: 1 }).toArray();
883
879
  const targetIndex = allMessages.findIndex((msg) => msg.id === id);
884
880
  if (targetIndex === -1) continue;
885
881
  const startIndex = Math.max(0, targetIndex - withPreviousMessages);
@@ -920,15 +916,16 @@ var MemoryStorageMongoDB = class extends MemoryStorage {
920
916
  }
921
917
  async listMessages(args) {
922
918
  const { threadId, resourceId, include, filter, perPage: perPageInput, page = 0, orderBy } = args;
923
- if (!threadId.trim()) {
919
+ const threadIds = Array.isArray(threadId) ? threadId : [threadId];
920
+ if (threadIds.length === 0 || threadIds.some((id) => !id.trim())) {
924
921
  throw new MastraError(
925
922
  {
926
923
  id: "STORAGE_MONGODB_LIST_MESSAGES_INVALID_THREAD_ID",
927
924
  domain: ErrorDomain.STORAGE,
928
925
  category: ErrorCategory.THIRD_PARTY,
929
- details: { threadId }
926
+ details: { threadId: Array.isArray(threadId) ? threadId.join(",") : threadId }
930
927
  },
931
- new Error("threadId must be a non-empty string")
928
+ new Error("threadId must be a non-empty string or array of non-empty strings")
932
929
  );
933
930
  }
934
931
  if (page < 0) {
@@ -948,7 +945,7 @@ var MemoryStorageMongoDB = class extends MemoryStorage {
948
945
  const { field, direction } = this.parseOrderBy(orderBy, "ASC");
949
946
  const sortOrder = direction === "ASC" ? 1 : -1;
950
947
  const collection = await this.operations.getCollection(TABLE_MESSAGES);
951
- const query = { thread_id: threadId };
948
+ const query = { thread_id: threadIds.length === 1 ? threadIds[0] : { $in: threadIds } };
952
949
  if (resourceId) {
953
950
  query.resourceId = resourceId;
954
951
  }
@@ -980,7 +977,7 @@ var MemoryStorageMongoDB = class extends MemoryStorage {
980
977
  }
981
978
  const messageIds = new Set(messages.map((m) => m.id));
982
979
  if (include && include.length > 0) {
983
- const includeMessages = await this._getIncludedMessages({ threadId, include });
980
+ const includeMessages = await this._getIncludedMessages({ include });
984
981
  if (includeMessages) {
985
982
  for (const includeMsg of includeMessages) {
986
983
  if (!messageIds.has(includeMsg.id)) {
@@ -1001,7 +998,10 @@ var MemoryStorageMongoDB = class extends MemoryStorage {
1001
998
  }
1002
999
  return direction === "ASC" ? String(aValue).localeCompare(String(bValue)) : String(bValue).localeCompare(String(aValue));
1003
1000
  });
1004
- const returnedThreadMessageIds = new Set(finalMessages.filter((m) => m.threadId === threadId).map((m) => m.id));
1001
+ const threadIdSet = new Set(threadIds);
1002
+ const returnedThreadMessageIds = new Set(
1003
+ finalMessages.filter((m) => m.threadId && threadIdSet.has(m.threadId)).map((m) => m.id)
1004
+ );
1005
1005
  const allThreadMessagesReturned = returnedThreadMessageIds.size >= total;
1006
1006
  const hasMore = perPageInput !== false && !allThreadMessagesReturned && offset + perPage < total;
1007
1007
  return {
@@ -1018,7 +1018,7 @@ var MemoryStorageMongoDB = class extends MemoryStorage {
1018
1018
  domain: ErrorDomain.STORAGE,
1019
1019
  category: ErrorCategory.THIRD_PARTY,
1020
1020
  details: {
1021
- threadId,
1021
+ threadId: Array.isArray(threadId) ? threadId.join(",") : threadId,
1022
1022
  resourceId: resourceId ?? ""
1023
1023
  }
1024
1024
  },
@@ -1898,98 +1898,9 @@ var StoreOperationsMongoDB = class extends StoreOperations {
1898
1898
  }
1899
1899
  };
1900
1900
  function transformScoreRow(row) {
1901
- let scorerValue = null;
1902
- if (row.scorer) {
1903
- try {
1904
- scorerValue = typeof row.scorer === "string" ? safelyParseJSON(row.scorer) : row.scorer;
1905
- } catch (e) {
1906
- console.warn("Failed to parse scorer:", e);
1907
- }
1908
- }
1909
- let preprocessStepResultValue = null;
1910
- if (row.preprocessStepResult) {
1911
- try {
1912
- preprocessStepResultValue = typeof row.preprocessStepResult === "string" ? safelyParseJSON(row.preprocessStepResult) : row.preprocessStepResult;
1913
- } catch (e) {
1914
- console.warn("Failed to parse preprocessStepResult:", e);
1915
- }
1916
- }
1917
- let analyzeStepResultValue = null;
1918
- if (row.analyzeStepResult) {
1919
- try {
1920
- analyzeStepResultValue = typeof row.analyzeStepResult === "string" ? safelyParseJSON(row.analyzeStepResult) : row.analyzeStepResult;
1921
- } catch (e) {
1922
- console.warn("Failed to parse analyzeStepResult:", e);
1923
- }
1924
- }
1925
- let inputValue = null;
1926
- if (row.input) {
1927
- try {
1928
- inputValue = typeof row.input === "string" ? safelyParseJSON(row.input) : row.input;
1929
- } catch (e) {
1930
- console.warn("Failed to parse input:", e);
1931
- }
1932
- }
1933
- let outputValue = null;
1934
- if (row.output) {
1935
- try {
1936
- outputValue = typeof row.output === "string" ? safelyParseJSON(row.output) : row.output;
1937
- } catch (e) {
1938
- console.warn("Failed to parse output:", e);
1939
- }
1940
- }
1941
- let entityValue = null;
1942
- if (row.entity) {
1943
- try {
1944
- entityValue = typeof row.entity === "string" ? safelyParseJSON(row.entity) : row.entity;
1945
- } catch (e) {
1946
- console.warn("Failed to parse entity:", e);
1947
- }
1948
- }
1949
- let requestContextValue = null;
1950
- if (row.requestContext) {
1951
- try {
1952
- requestContextValue = typeof row.requestContext === "string" ? safelyParseJSON(row.requestContext) : row.requestContext;
1953
- } catch (e) {
1954
- console.warn("Failed to parse requestContext:", e);
1955
- }
1956
- }
1957
- let metadataValue = null;
1958
- if (row.metadata) {
1959
- try {
1960
- metadataValue = typeof row.metadata === "string" ? safelyParseJSON(row.metadata) : row.metadata;
1961
- } catch (e) {
1962
- console.warn("Failed to parse metadata:", e);
1963
- }
1964
- }
1965
- return {
1966
- id: row.id,
1967
- entityId: row.entityId,
1968
- entityType: row.entityType,
1969
- scorerId: row.scorerId,
1970
- traceId: row.traceId,
1971
- spanId: row.spanId,
1972
- runId: row.runId,
1973
- scorer: scorerValue,
1974
- preprocessStepResult: preprocessStepResultValue,
1975
- preprocessPrompt: row.preprocessPrompt,
1976
- analyzeStepResult: analyzeStepResultValue,
1977
- generateScorePrompt: row.generateScorePrompt,
1978
- score: row.score,
1979
- analyzePrompt: row.analyzePrompt,
1980
- reasonPrompt: row.reasonPrompt,
1981
- metadata: metadataValue,
1982
- input: inputValue,
1983
- output: outputValue,
1984
- additionalContext: row.additionalContext,
1985
- requestContext: requestContextValue,
1986
- entity: entityValue,
1987
- source: row.source,
1988
- resourceId: row.resourceId,
1989
- threadId: row.threadId,
1990
- createdAt: new Date(row.createdAt),
1991
- updatedAt: new Date(row.updatedAt)
1992
- };
1901
+ return transformScoreRow$1(row, {
1902
+ convertTimestamps: true
1903
+ });
1993
1904
  }
1994
1905
  var ScoresStorageMongoDB = class extends ScoresStorage {
1995
1906
  operations;
@@ -2034,36 +1945,29 @@ var ScoresStorageMongoDB = class extends ScoresStorage {
2034
1945
  try {
2035
1946
  const now = /* @__PURE__ */ new Date();
2036
1947
  const scoreId = `score-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
2037
- const scoreData = {
2038
- id: scoreId,
2039
- entityId: validatedScore.entityId,
2040
- entityType: validatedScore.entityType,
2041
- scorerId: validatedScore.scorerId,
2042
- traceId: validatedScore.traceId || "",
2043
- spanId: validatedScore.spanId || "",
2044
- runId: validatedScore.runId,
2045
- scorer: typeof validatedScore.scorer === "string" ? safelyParseJSON(validatedScore.scorer) : validatedScore.scorer,
2046
- preprocessStepResult: typeof validatedScore.preprocessStepResult === "string" ? safelyParseJSON(validatedScore.preprocessStepResult) : validatedScore.preprocessStepResult,
2047
- analyzeStepResult: typeof validatedScore.analyzeStepResult === "string" ? safelyParseJSON(validatedScore.analyzeStepResult) : validatedScore.analyzeStepResult,
2048
- score: validatedScore.score,
2049
- reason: validatedScore.reason,
2050
- preprocessPrompt: validatedScore.preprocessPrompt,
2051
- generateScorePrompt: validatedScore.generateScorePrompt,
2052
- generateReasonPrompt: validatedScore.generateReasonPrompt,
2053
- analyzePrompt: validatedScore.analyzePrompt,
2054
- input: typeof validatedScore.input === "string" ? safelyParseJSON(validatedScore.input) : validatedScore.input,
2055
- output: typeof validatedScore.output === "string" ? safelyParseJSON(validatedScore.output) : validatedScore.output,
2056
- additionalContext: validatedScore.additionalContext,
2057
- requestContext: typeof validatedScore.requestContext === "string" ? safelyParseJSON(validatedScore.requestContext) : validatedScore.requestContext,
2058
- entity: typeof validatedScore.entity === "string" ? safelyParseJSON(validatedScore.entity) : validatedScore.entity,
2059
- source: validatedScore.source,
2060
- resourceId: validatedScore.resourceId || "",
2061
- threadId: validatedScore.threadId || "",
2062
- createdAt: now,
2063
- updatedAt: now
1948
+ const scorer = typeof validatedScore.scorer === "string" ? safelyParseJSON(validatedScore.scorer) : validatedScore.scorer;
1949
+ const preprocessStepResult = typeof validatedScore.preprocessStepResult === "string" ? safelyParseJSON(validatedScore.preprocessStepResult) : validatedScore.preprocessStepResult;
1950
+ const analyzeStepResult = typeof validatedScore.analyzeStepResult === "string" ? safelyParseJSON(validatedScore.analyzeStepResult) : validatedScore.analyzeStepResult;
1951
+ const input = typeof validatedScore.input === "string" ? safelyParseJSON(validatedScore.input) : validatedScore.input;
1952
+ const output = typeof validatedScore.output === "string" ? safelyParseJSON(validatedScore.output) : validatedScore.output;
1953
+ const requestContext = typeof validatedScore.requestContext === "string" ? safelyParseJSON(validatedScore.requestContext) : validatedScore.requestContext;
1954
+ const entity = typeof validatedScore.entity === "string" ? safelyParseJSON(validatedScore.entity) : validatedScore.entity;
1955
+ const createdAt = now;
1956
+ const updatedAt = now;
1957
+ const dataToSave = {
1958
+ ...validatedScore,
1959
+ scorer,
1960
+ preprocessStepResult,
1961
+ analyzeStepResult,
1962
+ input,
1963
+ output,
1964
+ requestContext,
1965
+ entity,
1966
+ createdAt,
1967
+ updatedAt
2064
1968
  };
2065
1969
  const collection = await this.operations.getCollection(TABLE_SCORERS);
2066
- await collection.insertOne(scoreData);
1970
+ await collection.insertOne(dataToSave);
2067
1971
  const savedScore = {
2068
1972
  ...score,
2069
1973
  id: scoreId,