@mastra/mongodb 0.13.5 → 0.13.6-alpha.0

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.
@@ -1,440 +0,0 @@
1
- import type { MastraMessageContentV2 } from '@mastra/core/agent';
2
- import { ErrorCategory, ErrorDomain, MastraError } from '@mastra/core/error';
3
- import type { MastraMessageV1, MastraMessageV2, StorageThreadType } from '@mastra/core/memory';
4
- import type { ScoreRowData, ScoringSource } from '@mastra/core/scores';
5
- import type {
6
- EvalRow,
7
- PaginationArgs,
8
- PaginationInfo,
9
- StorageColumn,
10
- StorageDomains,
11
- StorageGetMessagesArg,
12
- StorageGetTracesArg,
13
- StorageGetTracesPaginatedArg,
14
- StoragePagination,
15
- StorageResourceType,
16
- TABLE_NAMES,
17
- WorkflowRun,
18
- WorkflowRuns,
19
- } from '@mastra/core/storage';
20
- import { MastraStorage } from '@mastra/core/storage';
21
- import type { Trace } from '@mastra/core/telemetry';
22
- import type { StepResult, WorkflowRunState } from '@mastra/core/workflows';
23
- import { MongoDBConnector } from './connectors/MongoDBConnector';
24
- import { LegacyEvalsMongoDB } from './domains/legacy-evals';
25
- import { MemoryStorageMongoDB } from './domains/memory';
26
- import { StoreOperationsMongoDB } from './domains/operations';
27
- import { ScoresStorageMongoDB } from './domains/scores';
28
- import { TracesStorageMongoDB } from './domains/traces';
29
- import { WorkflowsStorageMongoDB } from './domains/workflows';
30
- import type { MongoDBConfig } from './types';
31
-
32
- const loadConnector = (config: MongoDBConfig): MongoDBConnector => {
33
- try {
34
- if ('connectorHandler' in config) {
35
- return MongoDBConnector.fromConnectionHandler(config.connectorHandler);
36
- }
37
- } catch (error) {
38
- throw new MastraError(
39
- {
40
- id: 'STORAGE_MONGODB_STORE_CONSTRUCTOR_FAILED',
41
- domain: ErrorDomain.STORAGE,
42
- category: ErrorCategory.USER,
43
- details: { connectionHandler: true },
44
- },
45
- error,
46
- );
47
- }
48
-
49
- try {
50
- return MongoDBConnector.fromDatabaseConfig({
51
- options: config.options,
52
- url: config.url,
53
- dbName: config.dbName,
54
- });
55
- } catch (error) {
56
- throw new MastraError(
57
- {
58
- id: 'STORAGE_MONGODB_STORE_CONSTRUCTOR_FAILED',
59
- domain: ErrorDomain.STORAGE,
60
- category: ErrorCategory.USER,
61
- details: { url: config?.url, dbName: config?.dbName },
62
- },
63
- error,
64
- );
65
- }
66
- };
67
-
68
- export class MongoDBStore extends MastraStorage {
69
- #connector: MongoDBConnector;
70
-
71
- stores: StorageDomains;
72
-
73
- public get supports(): {
74
- selectByIncludeResourceScope: boolean;
75
- resourceWorkingMemory: boolean;
76
- hasColumn: boolean;
77
- createTable: boolean;
78
- deleteMessages: boolean;
79
- } {
80
- return {
81
- selectByIncludeResourceScope: true,
82
- resourceWorkingMemory: true,
83
- hasColumn: false,
84
- createTable: false,
85
- deleteMessages: false,
86
- };
87
- }
88
-
89
- constructor(config: MongoDBConfig) {
90
- super({ name: 'MongoDBStore' });
91
-
92
- this.stores = {} as StorageDomains;
93
-
94
- this.#connector = loadConnector(config);
95
-
96
- const operations = new StoreOperationsMongoDB({
97
- connector: this.#connector,
98
- });
99
-
100
- const memory = new MemoryStorageMongoDB({
101
- operations,
102
- });
103
-
104
- const traces = new TracesStorageMongoDB({
105
- operations,
106
- });
107
-
108
- const legacyEvals = new LegacyEvalsMongoDB({
109
- operations,
110
- });
111
-
112
- const scores = new ScoresStorageMongoDB({
113
- operations,
114
- });
115
-
116
- const workflows = new WorkflowsStorageMongoDB({
117
- operations,
118
- });
119
-
120
- this.stores = {
121
- operations,
122
- memory,
123
- traces,
124
- legacyEvals,
125
- scores,
126
- workflows,
127
- };
128
- }
129
-
130
- async createTable({
131
- tableName,
132
- schema,
133
- }: {
134
- tableName: TABLE_NAMES;
135
- schema: Record<string, StorageColumn>;
136
- }): Promise<void> {
137
- return this.stores.operations.createTable({ tableName, schema });
138
- }
139
-
140
- async alterTable(_args: {
141
- tableName: TABLE_NAMES;
142
- schema: Record<string, StorageColumn>;
143
- ifNotExists: string[];
144
- }): Promise<void> {
145
- return this.stores.operations.alterTable(_args);
146
- }
147
-
148
- async dropTable({ tableName }: { tableName: TABLE_NAMES }): Promise<void> {
149
- return this.stores.operations.dropTable({ tableName });
150
- }
151
-
152
- async clearTable({ tableName }: { tableName: TABLE_NAMES }): Promise<void> {
153
- return this.stores.operations.clearTable({ tableName });
154
- }
155
-
156
- async insert({ tableName, record }: { tableName: TABLE_NAMES; record: Record<string, any> }): Promise<void> {
157
- return this.stores.operations.insert({ tableName, record });
158
- }
159
-
160
- async batchInsert({ tableName, records }: { tableName: TABLE_NAMES; records: Record<string, any>[] }): Promise<void> {
161
- return this.stores.operations.batchInsert({ tableName, records });
162
- }
163
-
164
- async load<R>({ tableName, keys }: { tableName: TABLE_NAMES; keys: Record<string, string> }): Promise<R | null> {
165
- return this.stores.operations.load({ tableName, keys });
166
- }
167
-
168
- async getThreadById({ threadId }: { threadId: string }): Promise<StorageThreadType | null> {
169
- return this.stores.memory.getThreadById({ threadId });
170
- }
171
-
172
- async getThreadsByResourceId({ resourceId }: { resourceId: string }): Promise<StorageThreadType[]> {
173
- return this.stores.memory.getThreadsByResourceId({ resourceId });
174
- }
175
-
176
- async saveThread({ thread }: { thread: StorageThreadType }): Promise<StorageThreadType> {
177
- return this.stores.memory.saveThread({ thread });
178
- }
179
-
180
- async updateThread({
181
- id,
182
- title,
183
- metadata,
184
- }: {
185
- id: string;
186
- title: string;
187
- metadata: Record<string, unknown>;
188
- }): Promise<StorageThreadType> {
189
- return this.stores.memory.updateThread({ id, title, metadata });
190
- }
191
-
192
- async deleteThread({ threadId }: { threadId: string }): Promise<void> {
193
- return this.stores.memory.deleteThread({ threadId });
194
- }
195
-
196
- public async getMessages(args: StorageGetMessagesArg & { format?: 'v1' }): Promise<MastraMessageV1[]>;
197
- public async getMessages(args: StorageGetMessagesArg & { format: 'v2' }): Promise<MastraMessageV2[]>;
198
- public async getMessages({
199
- threadId,
200
- selectBy,
201
- format,
202
- }: StorageGetMessagesArg & {
203
- format?: 'v1' | 'v2';
204
- }): Promise<MastraMessageV1[] | MastraMessageV2[]> {
205
- return this.stores.memory.getMessages({ threadId, selectBy, format });
206
- }
207
-
208
- async getMessagesById({ messageIds, format }: { messageIds: string[]; format: 'v1' }): Promise<MastraMessageV1[]>;
209
- async getMessagesById({ messageIds, format }: { messageIds: string[]; format?: 'v2' }): Promise<MastraMessageV2[]>;
210
- async getMessagesById({
211
- messageIds,
212
- format,
213
- }: {
214
- messageIds: string[];
215
- format?: 'v1' | 'v2';
216
- }): Promise<MastraMessageV1[] | MastraMessageV2[]> {
217
- return this.stores.memory.getMessagesById({ messageIds, format });
218
- }
219
-
220
- async saveMessages(args: { messages: MastraMessageV1[]; format?: undefined | 'v1' }): Promise<MastraMessageV1[]>;
221
- async saveMessages(args: { messages: MastraMessageV2[]; format: 'v2' }): Promise<MastraMessageV2[]>;
222
- async saveMessages(
223
- args: { messages: MastraMessageV1[]; format?: undefined | 'v1' } | { messages: MastraMessageV2[]; format: 'v2' },
224
- ): Promise<MastraMessageV2[] | MastraMessageV1[]> {
225
- return this.stores.memory.saveMessages(args);
226
- }
227
-
228
- async getThreadsByResourceIdPaginated(_args: {
229
- resourceId: string;
230
- page: number;
231
- perPage: number;
232
- }): Promise<PaginationInfo & { threads: StorageThreadType[] }> {
233
- return this.stores.memory.getThreadsByResourceIdPaginated(_args);
234
- }
235
-
236
- async getMessagesPaginated(
237
- _args: StorageGetMessagesArg,
238
- ): Promise<PaginationInfo & { messages: MastraMessageV1[] | MastraMessageV2[] }> {
239
- return this.stores.memory.getMessagesPaginated(_args);
240
- }
241
-
242
- async updateMessages(_args: {
243
- messages: Partial<Omit<MastraMessageV2, 'createdAt'>> &
244
- {
245
- id: string;
246
- content?: { metadata?: MastraMessageContentV2['metadata']; content?: MastraMessageContentV2['content'] };
247
- }[];
248
- }): Promise<MastraMessageV2[]> {
249
- return this.stores.memory.updateMessages(_args);
250
- }
251
-
252
- async getTraces(args: StorageGetTracesArg): Promise<Trace[]> {
253
- return this.stores.traces.getTraces(args);
254
- }
255
-
256
- async getTracesPaginated(args: StorageGetTracesPaginatedArg): Promise<PaginationInfo & { traces: Trace[] }> {
257
- return this.stores.traces.getTracesPaginated(args);
258
- }
259
-
260
- async getWorkflowRuns(args?: {
261
- workflowName?: string;
262
- fromDate?: Date;
263
- toDate?: Date;
264
- limit?: number;
265
- offset?: number;
266
- resourceId?: string;
267
- }): Promise<WorkflowRuns> {
268
- return this.stores.workflows.getWorkflowRuns(args);
269
- }
270
-
271
- async getEvals(
272
- options: {
273
- agentName?: string;
274
- type?: 'test' | 'live';
275
- } & PaginationArgs = {},
276
- ): Promise<PaginationInfo & { evals: EvalRow[] }> {
277
- return this.stores.legacyEvals.getEvals(options);
278
- }
279
-
280
- async getEvalsByAgentName(agentName: string, type?: 'test' | 'live'): Promise<EvalRow[]> {
281
- return this.stores.legacyEvals.getEvalsByAgentName(agentName, type);
282
- }
283
-
284
- async updateWorkflowResults({
285
- workflowName,
286
- runId,
287
- stepId,
288
- result,
289
- runtimeContext,
290
- }: {
291
- workflowName: string;
292
- runId: string;
293
- stepId: string;
294
- result: StepResult<any, any, any, any>;
295
- runtimeContext: Record<string, any>;
296
- }): Promise<Record<string, StepResult<any, any, any, any>>> {
297
- return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result, runtimeContext });
298
- }
299
-
300
- async updateWorkflowState({
301
- workflowName,
302
- runId,
303
- opts,
304
- }: {
305
- workflowName: string;
306
- runId: string;
307
- opts: {
308
- status: string;
309
- result?: StepResult<any, any, any, any>;
310
- error?: string;
311
- suspendedPaths?: Record<string, number[]>;
312
- waitingPaths?: Record<string, number[]>;
313
- };
314
- }): Promise<WorkflowRunState | undefined> {
315
- return this.stores.workflows.updateWorkflowState({ workflowName, runId, opts });
316
- }
317
-
318
- async persistWorkflowSnapshot({
319
- workflowName,
320
- runId,
321
- snapshot,
322
- }: {
323
- workflowName: string;
324
- runId: string;
325
- snapshot: WorkflowRunState;
326
- }): Promise<void> {
327
- return this.stores.workflows.persistWorkflowSnapshot({ workflowName, runId, snapshot });
328
- }
329
-
330
- async loadWorkflowSnapshot({
331
- workflowName,
332
- runId,
333
- }: {
334
- workflowName: string;
335
- runId: string;
336
- }): Promise<WorkflowRunState | null> {
337
- return this.stores.workflows.loadWorkflowSnapshot({ workflowName, runId });
338
- }
339
-
340
- async getWorkflowRunById({
341
- runId,
342
- workflowName,
343
- }: {
344
- runId: string;
345
- workflowName?: string;
346
- }): Promise<WorkflowRun | null> {
347
- return this.stores.workflows.getWorkflowRunById({ runId, workflowName });
348
- }
349
-
350
- async close(): Promise<void> {
351
- try {
352
- await this.#connector.close();
353
- } catch (error) {
354
- throw new MastraError(
355
- {
356
- id: 'STORAGE_MONGODB_STORE_CLOSE_FAILED',
357
- domain: ErrorDomain.STORAGE,
358
- category: ErrorCategory.USER,
359
- },
360
- error,
361
- );
362
- }
363
- }
364
-
365
- /**
366
- * SCORERS
367
- */
368
- async getScoreById({ id }: { id: string }): Promise<ScoreRowData | null> {
369
- return this.stores.scores.getScoreById({ id });
370
- }
371
-
372
- async saveScore(score: Omit<ScoreRowData, 'id' | 'createdAt' | 'updatedAt'>): Promise<{ score: ScoreRowData }> {
373
- return this.stores.scores.saveScore(score);
374
- }
375
-
376
- async getScoresByRunId({
377
- runId,
378
- pagination,
379
- }: {
380
- runId: string;
381
- pagination: StoragePagination;
382
- }): Promise<{ pagination: PaginationInfo; scores: ScoreRowData[] }> {
383
- return this.stores.scores.getScoresByRunId({ runId, pagination });
384
- }
385
-
386
- async getScoresByEntityId({
387
- entityId,
388
- entityType,
389
- pagination,
390
- }: {
391
- pagination: StoragePagination;
392
- entityId: string;
393
- entityType: string;
394
- }): Promise<{ pagination: PaginationInfo; scores: ScoreRowData[] }> {
395
- return this.stores.scores.getScoresByEntityId({ entityId, entityType, pagination });
396
- }
397
-
398
- async getScoresByScorerId({
399
- scorerId,
400
- pagination,
401
- entityId,
402
- entityType,
403
- source,
404
- }: {
405
- scorerId: string;
406
- pagination: StoragePagination;
407
- entityId?: string;
408
- entityType?: string;
409
- source?: ScoringSource;
410
- }): Promise<{ pagination: PaginationInfo; scores: ScoreRowData[] }> {
411
- return this.stores.scores.getScoresByScorerId({ scorerId, pagination, entityId, entityType, source });
412
- }
413
-
414
- /**
415
- * RESOURCES
416
- */
417
- async getResourceById({ resourceId }: { resourceId: string }): Promise<StorageResourceType | null> {
418
- return this.stores.memory.getResourceById({ resourceId });
419
- }
420
-
421
- async saveResource({ resource }: { resource: StorageResourceType }): Promise<StorageResourceType> {
422
- return this.stores.memory.saveResource({ resource });
423
- }
424
-
425
- async updateResource({
426
- resourceId,
427
- workingMemory,
428
- metadata,
429
- }: {
430
- resourceId: string;
431
- workingMemory?: string;
432
- metadata?: Record<string, unknown>;
433
- }): Promise<StorageResourceType> {
434
- return this.stores.memory.updateResource({
435
- resourceId,
436
- workingMemory,
437
- metadata,
438
- });
439
- }
440
- }
@@ -1,14 +0,0 @@
1
- import type { MongoClientOptions } from 'mongodb';
2
- import type { ConnectorHandler } from './connectors/base';
3
-
4
- export type MongoDBConfig =
5
- | DatabaseConfig
6
- | {
7
- connectorHandler: ConnectorHandler;
8
- };
9
-
10
- export type DatabaseConfig = {
11
- url: string;
12
- dbName: string;
13
- options?: MongoClientOptions;
14
- };