@mastra/mongodb 0.12.0 → 0.12.1

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,35 +1,166 @@
1
1
  import { BaseFilterTranslator } from '@mastra/core/vector/filter';
2
2
  import type { BlacklistedRootOperators } from '@mastra/core/vector/filter';
3
+ import { Collection } from 'mongodb';
3
4
  import type { CreateIndexParams } from '@mastra/core/vector';
4
5
  import type { DeleteIndexParams } from '@mastra/core/vector';
5
6
  import type { DeleteVectorParams } from '@mastra/core/vector';
6
7
  import type { DescribeIndexParams } from '@mastra/core/vector';
8
+ import { Document } from 'mongodb';
7
9
  import type { EvalRow } from '@mastra/core/storage';
10
+ import type { IMastraLogger } from '@mastra/core/logger';
8
11
  import type { IndexStats } from '@mastra/core/vector';
12
+ import { LegacyEvalsStorage } from '@mastra/core/storage';
9
13
  import type { LogicalOperatorValueMap } from '@mastra/core/vector/filter';
10
14
  import type { MastraMessageContentV2 } from '@mastra/core/agent';
11
15
  import type { MastraMessageV1 } from '@mastra/core/memory';
12
16
  import type { MastraMessageV2 } from '@mastra/core/memory';
13
17
  import { MastraStorage } from '@mastra/core/storage';
14
18
  import { MastraVector } from '@mastra/core/vector';
19
+ import { MemoryStorage } from '@mastra/core/storage';
20
+ import { MongoClient } from 'mongodb';
15
21
  import type { MongoClientOptions } from 'mongodb';
16
22
  import type { OperatorSupport } from '@mastra/core/vector/filter';
17
23
  import type { OperatorValueMap } from '@mastra/core/vector/filter';
24
+ import type { PaginationArgs } from '@mastra/core/storage';
18
25
  import type { PaginationInfo } from '@mastra/core/storage';
19
26
  import type { QueryResult } from '@mastra/core/vector';
20
27
  import type { QueryVectorParams } from '@mastra/core/vector';
28
+ import type { ScoreRowData } from '@mastra/core/scores';
29
+ import { ScoresStorage } from '@mastra/core/storage';
21
30
  import type { StorageColumn } from '@mastra/core/storage';
31
+ import type { StorageDomains } from '@mastra/core/storage';
22
32
  import type { StorageGetMessagesArg } from '@mastra/core/storage';
23
33
  import type { StorageGetTracesArg } from '@mastra/core/storage';
34
+ import type { StorageGetTracesPaginatedArg } from '@mastra/core/storage';
35
+ import type { StoragePagination } from '@mastra/core/storage';
36
+ import type { StorageResourceType } from '@mastra/core/storage';
24
37
  import type { StorageThreadType } from '@mastra/core/memory';
38
+ import { StoreOperations } from '@mastra/core/storage';
25
39
  import type { TABLE_NAMES } from '@mastra/core/storage';
26
40
  import type { Trace } from '@mastra/core/telemetry';
41
+ import { TracesStorage } from '@mastra/core/storage';
27
42
  import type { UpdateVectorParams } from '@mastra/core/vector';
28
43
  import type { UpsertVectorParams } from '@mastra/core/vector';
29
44
  import type { VectorFieldValue } from '@mastra/core/vector/filter';
30
45
  import type { VectorFilter } from '@mastra/core/vector/filter';
31
46
  import type { WorkflowRun } from '@mastra/core/storage';
47
+ import type { WorkflowRuns } from '@mastra/core/storage';
32
48
  import type { WorkflowRunState } from '@mastra/core/workflows';
49
+ import { WorkflowsStorage } from '@mastra/core/storage';
50
+
51
+ export declare interface ConnectorHandler {
52
+ getCollection(collectionName: string): Promise<Collection>;
53
+ close(): Promise<void>;
54
+ }
55
+
56
+ export declare interface ConnectorHandler_alias_1 {
57
+ getCollection(collectionName: string): Promise<Collection>;
58
+ close(): Promise<void>;
59
+ }
60
+
61
+ export declare function createExecuteOperationWithRetry({ logger, maxRetries, initialBackoffMs, }: {
62
+ logger: IMastraLogger;
63
+ maxRetries?: number;
64
+ initialBackoffMs?: number;
65
+ }): <T>(operationFn: () => Promise<T>, operationDescription: string) => Promise<T>;
66
+
67
+ export declare type DatabaseConfig = {
68
+ url: string;
69
+ dbName: string;
70
+ options?: MongoClientOptions;
71
+ };
72
+
73
+ export declare function formatDateForMongoDB(date: Date | string): Date;
74
+
75
+ export declare class LegacyEvalsMongoDB extends LegacyEvalsStorage {
76
+ private operations;
77
+ constructor({ operations }: {
78
+ operations: StoreOperationsMongoDB;
79
+ });
80
+ /** @deprecated use getEvals instead */
81
+ getEvalsByAgentName(agentName: string, type?: 'test' | 'live'): Promise<EvalRow[]>;
82
+ getEvals(options?: {
83
+ agentName?: string;
84
+ type?: 'test' | 'live';
85
+ } & PaginationArgs): Promise<PaginationInfo & {
86
+ evals: EvalRow[];
87
+ }>;
88
+ }
89
+
90
+ export declare class MemoryStorageMongoDB extends MemoryStorage {
91
+ private operations;
92
+ constructor({ operations }: {
93
+ operations: StoreOperationsMongoDB;
94
+ });
95
+ private parseRow;
96
+ private _getIncludedMessages;
97
+ /**
98
+ * @deprecated use getMessagesPaginated instead for paginated results.
99
+ */
100
+ getMessages(args: StorageGetMessagesArg & {
101
+ format?: 'v1';
102
+ }): Promise<MastraMessageV1[]>;
103
+ getMessages(args: StorageGetMessagesArg & {
104
+ format: 'v2';
105
+ }): Promise<MastraMessageV2[]>;
106
+ getMessagesPaginated(args: StorageGetMessagesArg & {
107
+ format?: 'v1' | 'v2';
108
+ }): Promise<PaginationInfo & {
109
+ messages: MastraMessageV1[] | MastraMessageV2[];
110
+ }>;
111
+ saveMessages(args: {
112
+ messages: MastraMessageV1[];
113
+ format?: undefined | 'v1';
114
+ }): Promise<MastraMessageV1[]>;
115
+ saveMessages(args: {
116
+ messages: MastraMessageV2[];
117
+ format: 'v2';
118
+ }): Promise<MastraMessageV2[]>;
119
+ updateMessages({ messages, }: {
120
+ messages: (Partial<Omit<MastraMessageV2, 'createdAt'>> & {
121
+ id: string;
122
+ content?: {
123
+ metadata?: MastraMessageContentV2['metadata'];
124
+ content?: MastraMessageContentV2['content'];
125
+ };
126
+ })[];
127
+ }): Promise<MastraMessageV2[]>;
128
+ getResourceById({ resourceId }: {
129
+ resourceId: string;
130
+ }): Promise<StorageResourceType | null>;
131
+ saveResource({ resource }: {
132
+ resource: StorageResourceType;
133
+ }): Promise<StorageResourceType>;
134
+ updateResource({ resourceId, workingMemory, metadata, }: {
135
+ resourceId: string;
136
+ workingMemory?: string;
137
+ metadata?: Record<string, unknown>;
138
+ }): Promise<StorageResourceType>;
139
+ getThreadById({ threadId }: {
140
+ threadId: string;
141
+ }): Promise<StorageThreadType | null>;
142
+ getThreadsByResourceId({ resourceId }: {
143
+ resourceId: string;
144
+ }): Promise<StorageThreadType[]>;
145
+ getThreadsByResourceIdPaginated(args: {
146
+ resourceId: string;
147
+ page: number;
148
+ perPage: number;
149
+ }): Promise<PaginationInfo & {
150
+ threads: StorageThreadType[];
151
+ }>;
152
+ saveThread({ thread }: {
153
+ thread: StorageThreadType;
154
+ }): Promise<StorageThreadType>;
155
+ updateThread({ id, title, metadata, }: {
156
+ id: string;
157
+ title: string;
158
+ metadata: Record<string, unknown>;
159
+ }): Promise<StorageThreadType>;
160
+ deleteThread({ threadId }: {
161
+ threadId: string;
162
+ }): Promise<void>;
163
+ }
33
164
 
34
165
  /**
35
166
  * Vector store specific prompt that details supported operators and examples.
@@ -41,13 +172,49 @@ export { MONGODB_PROMPT as MONGODB_PROMPT_alias_1 }
41
172
 
42
173
  declare type MongoDBBlacklisted = BlacklistedRootOperators | '$size';
43
174
 
44
- declare interface MongoDBConfig {
45
- url: string;
46
- dbName: string;
47
- options?: MongoClientOptions;
175
+ export declare type MongoDBConfig = DatabaseConfig | {
176
+ connectorHandler: ConnectorHandler_alias_1;
177
+ };
178
+
179
+ export declare class MongoDBConnector {
180
+ #private;
181
+ constructor(options: MongoDBConnectorOptions);
182
+ static fromDatabaseConfig(config: DatabaseConfig): MongoDBConnector;
183
+ static fromConnectionHandler(handler: ConnectorHandler): MongoDBConnector;
184
+ private getConnection;
185
+ getCollection(collectionName: string): Promise<Collection<Document>>;
186
+ close(): Promise<void>;
48
187
  }
49
- export { MongoDBConfig }
50
- export { MongoDBConfig as MongoDBConfig_alias_1 }
188
+
189
+ export declare class MongoDBConnector_alias_1 {
190
+ #private;
191
+ constructor(options: MongoDBConnectorOptions_2);
192
+ static fromDatabaseConfig(config: DatabaseConfig): MongoDBConnector_alias_1;
193
+ static fromConnectionHandler(handler: ConnectorHandler_alias_1): MongoDBConnector_alias_1;
194
+ private getConnection;
195
+ getCollection(collectionName: string): Promise<Collection<Document>>;
196
+ close(): Promise<void>;
197
+ }
198
+
199
+ declare type MongoDBConnectorOptions = {
200
+ client: MongoClient;
201
+ dbName: string;
202
+ handler: undefined;
203
+ } | {
204
+ client: undefined;
205
+ dbName: undefined;
206
+ handler: ConnectorHandler;
207
+ };
208
+
209
+ declare type MongoDBConnectorOptions_2 = {
210
+ client: MongoClient;
211
+ dbName: string;
212
+ handler: undefined;
213
+ } | {
214
+ client: undefined;
215
+ dbName: undefined;
216
+ handler: ConnectorHandler_alias_1;
217
+ };
51
218
 
52
219
  /**
53
220
  * Translator for MongoDB filter queries.
@@ -70,6 +237,10 @@ declare interface MongoDBIndexReadyParams {
70
237
  export { MongoDBIndexReadyParams }
71
238
  export { MongoDBIndexReadyParams as MongoDBIndexReadyParams_alias_1 }
72
239
 
240
+ export declare interface MongoDBOperationsConfig {
241
+ connector: ConnectorHandler_alias_1;
242
+ }
243
+
73
244
  declare type MongoDBOperatorValueMap = Omit<OperatorValueMap, '$options'> & {
74
245
  $size: number;
75
246
  };
@@ -80,21 +251,26 @@ declare interface MongoDBQueryVectorParams extends QueryVectorParams<MongoDBVect
80
251
 
81
252
  declare class MongoDBStore extends MastraStorage {
82
253
  #private;
254
+ stores: StorageDomains;
255
+ get supports(): {
256
+ selectByIncludeResourceScope: boolean;
257
+ resourceWorkingMemory: boolean;
258
+ hasColumn: boolean;
259
+ createTable: boolean;
260
+ };
83
261
  constructor(config: MongoDBConfig);
84
- private getConnection;
85
- private getCollection;
86
- createTable(): Promise<void>;
87
- /**
88
- * No-op: This backend is schemaless and does not require schema changes.
89
- * @param tableName Name of the table
90
- * @param schema Schema of the table
91
- * @param ifNotExists Array of column names to add if they don't exist
92
- */
262
+ createTable({ tableName, schema, }: {
263
+ tableName: TABLE_NAMES;
264
+ schema: Record<string, StorageColumn>;
265
+ }): Promise<void>;
93
266
  alterTable(_args: {
94
267
  tableName: TABLE_NAMES;
95
268
  schema: Record<string, StorageColumn>;
96
269
  ifNotExists: string[];
97
270
  }): Promise<void>;
271
+ dropTable({ tableName }: {
272
+ tableName: TABLE_NAMES;
273
+ }): Promise<void>;
98
274
  clearTable({ tableName }: {
99
275
  tableName: TABLE_NAMES;
100
276
  }): Promise<void>;
@@ -141,29 +317,42 @@ declare class MongoDBStore extends MastraStorage {
141
317
  messages: MastraMessageV2[];
142
318
  format: 'v2';
143
319
  }): Promise<MastraMessageV2[]>;
144
- getTraces({ name, scope, page, perPage, attributes, filters, }?: {
145
- name?: string;
146
- scope?: string;
320
+ getThreadsByResourceIdPaginated(_args: {
321
+ resourceId: string;
147
322
  page: number;
148
323
  perPage: number;
149
- attributes?: Record<string, string>;
150
- filters?: Record<string, any>;
151
- }): Promise<any[]>;
152
- getWorkflowRuns({ workflowName, fromDate, toDate, limit, offset, }?: {
324
+ }): Promise<PaginationInfo & {
325
+ threads: StorageThreadType[];
326
+ }>;
327
+ getMessagesPaginated(_args: StorageGetMessagesArg): Promise<PaginationInfo & {
328
+ messages: MastraMessageV1[] | MastraMessageV2[];
329
+ }>;
330
+ updateMessages(_args: {
331
+ messages: Partial<Omit<MastraMessageV2, 'createdAt'>> & {
332
+ id: string;
333
+ content?: {
334
+ metadata?: MastraMessageContentV2['metadata'];
335
+ content?: MastraMessageContentV2['content'];
336
+ };
337
+ }[];
338
+ }): Promise<MastraMessageV2[]>;
339
+ getTraces(args: StorageGetTracesArg): Promise<Trace[]>;
340
+ getTracesPaginated(args: StorageGetTracesPaginatedArg): Promise<PaginationInfo & {
341
+ traces: Trace[];
342
+ }>;
343
+ getWorkflowRuns(args?: {
153
344
  workflowName?: string;
154
345
  fromDate?: Date;
155
346
  toDate?: Date;
156
347
  limit?: number;
157
348
  offset?: number;
158
- }): Promise<{
159
- runs: Array<{
160
- workflowName: string;
161
- runId: string;
162
- snapshot: WorkflowRunState | string;
163
- createdAt: Date;
164
- updatedAt: Date;
165
- }>;
166
- total: number;
349
+ resourceId?: string;
350
+ }): Promise<WorkflowRuns>;
351
+ getEvals(options?: {
352
+ agentName?: string;
353
+ type?: 'test' | 'live';
354
+ } & PaginationArgs): Promise<PaginationInfo & {
355
+ evals: EvalRow[];
167
356
  }>;
168
357
  getEvalsByAgentName(agentName: string, type?: 'test' | 'live'): Promise<EvalRow[]>;
169
358
  persistWorkflowSnapshot({ workflowName, runId, snapshot, }: {
@@ -179,32 +368,54 @@ declare class MongoDBStore extends MastraStorage {
179
368
  runId: string;
180
369
  workflowName?: string;
181
370
  }): Promise<WorkflowRun | null>;
182
- private parseWorkflowRun;
183
- private parseRow;
184
- private transformEvalRow;
185
- getTracesPaginated(_args: StorageGetTracesArg): Promise<PaginationInfo & {
186
- traces: Trace[];
371
+ close(): Promise<void>;
372
+ /**
373
+ * SCORERS
374
+ */
375
+ getScoreById({ id }: {
376
+ id: string;
377
+ }): Promise<ScoreRowData | null>;
378
+ saveScore(score: Omit<ScoreRowData, 'id' | 'createdAt' | 'updatedAt'>): Promise<{
379
+ score: ScoreRowData;
187
380
  }>;
188
- getThreadsByResourceIdPaginated(_args: {
189
- resourceId: string;
190
- page?: number;
191
- perPage?: number;
192
- }): Promise<PaginationInfo & {
193
- threads: StorageThreadType[];
381
+ getScoresByRunId({ runId, pagination, }: {
382
+ runId: string;
383
+ pagination: StoragePagination;
384
+ }): Promise<{
385
+ pagination: PaginationInfo;
386
+ scores: ScoreRowData[];
194
387
  }>;
195
- getMessagesPaginated(_args: StorageGetMessagesArg): Promise<PaginationInfo & {
196
- messages: MastraMessageV1[] | MastraMessageV2[];
388
+ getScoresByEntityId({ entityId, entityType, pagination, }: {
389
+ pagination: StoragePagination;
390
+ entityId: string;
391
+ entityType: string;
392
+ }): Promise<{
393
+ pagination: PaginationInfo;
394
+ scores: ScoreRowData[];
197
395
  }>;
198
- close(): Promise<void>;
199
- updateMessages(_args: {
200
- messages: Partial<Omit<MastraMessageV2, 'createdAt'>> & {
201
- id: string;
202
- content?: {
203
- metadata?: MastraMessageContentV2['metadata'];
204
- content?: MastraMessageContentV2['content'];
205
- };
206
- }[];
207
- }): Promise<MastraMessageV2[]>;
396
+ getScoresByScorerId({ scorerId, pagination, entityId, entityType, }: {
397
+ scorerId: string;
398
+ pagination: StoragePagination;
399
+ entityId?: string;
400
+ entityType?: string;
401
+ }): Promise<{
402
+ pagination: PaginationInfo;
403
+ scores: ScoreRowData[];
404
+ }>;
405
+ /**
406
+ * RESOURCES
407
+ */
408
+ getResourceById({ resourceId }: {
409
+ resourceId: string;
410
+ }): Promise<StorageResourceType | null>;
411
+ saveResource({ resource }: {
412
+ resource: StorageResourceType;
413
+ }): Promise<StorageResourceType>;
414
+ updateResource({ resourceId, workingMemory, metadata, }: {
415
+ resourceId: string;
416
+ workingMemory?: string;
417
+ metadata?: Record<string, unknown>;
418
+ }): Promise<StorageResourceType>;
208
419
  }
209
420
  export { MongoDBStore }
210
421
  export { MongoDBStore as MongoDBStore_alias_1 }
@@ -281,4 +492,115 @@ export { MongoDBVector as MongoDBVector_alias_1 }
281
492
 
282
493
  export declare type MongoDBVectorFilter = VectorFilter<keyof MongoDBOperatorValueMap, MongoDBOperatorValueMap, LogicalOperatorValueMap, MongoDBBlacklisted, VectorFieldValue | RegExp>;
283
494
 
495
+ export declare class ScoresStorageMongoDB extends ScoresStorage {
496
+ private operations;
497
+ constructor({ operations }: {
498
+ operations: StoreOperationsMongoDB;
499
+ });
500
+ getScoreById({ id }: {
501
+ id: string;
502
+ }): Promise<ScoreRowData | null>;
503
+ saveScore(score: Omit<ScoreRowData, 'id' | 'createdAt' | 'updatedAt'>): Promise<{
504
+ score: ScoreRowData;
505
+ }>;
506
+ getScoresByScorerId({ scorerId, pagination, entityId, entityType, }: {
507
+ scorerId: string;
508
+ pagination: StoragePagination;
509
+ entityId?: string;
510
+ entityType?: string;
511
+ }): Promise<{
512
+ pagination: PaginationInfo;
513
+ scores: ScoreRowData[];
514
+ }>;
515
+ getScoresByRunId({ runId, pagination, }: {
516
+ runId: string;
517
+ pagination: StoragePagination;
518
+ }): Promise<{
519
+ pagination: PaginationInfo;
520
+ scores: ScoreRowData[];
521
+ }>;
522
+ getScoresByEntityId({ entityId, entityType, pagination, }: {
523
+ pagination: StoragePagination;
524
+ entityId: string;
525
+ entityType: string;
526
+ }): Promise<{
527
+ pagination: PaginationInfo;
528
+ scores: ScoreRowData[];
529
+ }>;
530
+ }
531
+
532
+ export declare class StoreOperationsMongoDB extends StoreOperations {
533
+ #private;
534
+ constructor(config: MongoDBOperationsConfig);
535
+ getCollection(collectionName: string): Promise<Collection<Document>>;
536
+ hasColumn(_table: string, _column: string): Promise<boolean>;
537
+ createTable(): Promise<void>;
538
+ alterTable(_args: {
539
+ tableName: TABLE_NAMES;
540
+ schema: Record<string, StorageColumn>;
541
+ ifNotExists: string[];
542
+ }): Promise<void>;
543
+ clearTable({ tableName }: {
544
+ tableName: TABLE_NAMES;
545
+ }): Promise<void>;
546
+ dropTable({ tableName }: {
547
+ tableName: TABLE_NAMES;
548
+ }): Promise<void>;
549
+ insert({ tableName, record }: {
550
+ tableName: TABLE_NAMES;
551
+ record: Record<string, any>;
552
+ }): Promise<void>;
553
+ batchInsert({ tableName, records }: {
554
+ tableName: TABLE_NAMES;
555
+ records: Record<string, any>[];
556
+ }): Promise<void>;
557
+ load<R>({ tableName, keys }: {
558
+ tableName: TABLE_NAMES;
559
+ keys: Record<string, string>;
560
+ }): Promise<R | null>;
561
+ }
562
+
563
+ export declare class TracesStorageMongoDB extends TracesStorage {
564
+ private operations;
565
+ constructor({ operations }: {
566
+ operations: StoreOperationsMongoDB;
567
+ });
568
+ getTraces(args: StorageGetTracesArg): Promise<Trace[]>;
569
+ getTracesPaginated(args: StorageGetTracesPaginatedArg): Promise<PaginationInfo & {
570
+ traces: Trace[];
571
+ }>;
572
+ batchTraceInsert({ records }: {
573
+ records: Record<string, any>[];
574
+ }): Promise<void>;
575
+ }
576
+
577
+ export declare class WorkflowsStorageMongoDB extends WorkflowsStorage {
578
+ private operations;
579
+ constructor({ operations }: {
580
+ operations: StoreOperationsMongoDB;
581
+ });
582
+ persistWorkflowSnapshot({ workflowName, runId, snapshot, }: {
583
+ workflowName: string;
584
+ runId: string;
585
+ snapshot: WorkflowRunState;
586
+ }): Promise<void>;
587
+ loadWorkflowSnapshot({ workflowName, runId, }: {
588
+ workflowName: string;
589
+ runId: string;
590
+ }): Promise<WorkflowRunState | null>;
591
+ getWorkflowRuns(args?: {
592
+ workflowName?: string;
593
+ fromDate?: Date;
594
+ toDate?: Date;
595
+ limit?: number;
596
+ offset?: number;
597
+ resourceId?: string;
598
+ }): Promise<WorkflowRuns>;
599
+ getWorkflowRunById(args: {
600
+ runId: string;
601
+ workflowName?: string;
602
+ }): Promise<WorkflowRun | null>;
603
+ private parseWorkflowRun;
604
+ }
605
+
284
606
  export { }