@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.
- package/.turbo/turbo-build.log +7 -7
- package/CHANGELOG.md +57 -0
- package/LICENSE.md +11 -42
- package/dist/_tsup-dts-rollup.d.cts +376 -54
- package/dist/_tsup-dts-rollup.d.ts +376 -54
- package/dist/index.cjs +1420 -424
- package/dist/index.d.cts +0 -1
- package/dist/index.d.ts +0 -1
- package/dist/index.js +1414 -418
- package/docker-compose.yaml +1 -1
- package/package.json +6 -6
- package/src/storage/ConnectorHandler.ts +7 -0
- package/src/storage/MongoDBConnector.ts +93 -0
- package/src/storage/connectors/MongoDBConnector.ts +93 -0
- package/src/storage/connectors/base.ts +7 -0
- package/src/storage/domains/legacy-evals/index.ts +193 -0
- package/src/storage/domains/memory/index.ts +741 -0
- package/src/storage/domains/operations/index.ts +152 -0
- package/src/storage/domains/scores/index.ts +379 -0
- package/src/storage/domains/traces/index.ts +142 -0
- package/src/storage/domains/utils.ts +43 -0
- package/src/storage/domains/workflows/index.ts +196 -0
- package/src/storage/index.test.ts +24 -1226
- package/src/storage/index.ts +218 -776
- package/src/storage/types.ts +14 -0
- package/src/vector/index.test.ts +16 -1
- package/src/vector/index.ts +34 -11
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import { ErrorDomain, ErrorCategory, MastraError } from '@mastra/core/error';
|
|
2
|
+
import { WorkflowsStorage, TABLE_WORKFLOW_SNAPSHOT, safelyParseJSON } from '@mastra/core/storage';
|
|
3
|
+
import type { WorkflowRun, WorkflowRuns } from '@mastra/core/storage';
|
|
4
|
+
import type { WorkflowRunState } from '@mastra/core/workflows';
|
|
5
|
+
import type { StoreOperationsMongoDB } from '../operations';
|
|
6
|
+
|
|
7
|
+
export class WorkflowsStorageMongoDB extends WorkflowsStorage {
|
|
8
|
+
private operations: StoreOperationsMongoDB;
|
|
9
|
+
|
|
10
|
+
constructor({ operations }: { operations: StoreOperationsMongoDB }) {
|
|
11
|
+
super();
|
|
12
|
+
this.operations = operations;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
async persistWorkflowSnapshot({
|
|
16
|
+
workflowName,
|
|
17
|
+
runId,
|
|
18
|
+
snapshot,
|
|
19
|
+
}: {
|
|
20
|
+
workflowName: string;
|
|
21
|
+
runId: string;
|
|
22
|
+
snapshot: WorkflowRunState;
|
|
23
|
+
}): Promise<void> {
|
|
24
|
+
try {
|
|
25
|
+
const collection = await this.operations.getCollection(TABLE_WORKFLOW_SNAPSHOT);
|
|
26
|
+
await collection.updateOne(
|
|
27
|
+
{ workflow_name: workflowName, run_id: runId },
|
|
28
|
+
{
|
|
29
|
+
$set: {
|
|
30
|
+
workflow_name: workflowName,
|
|
31
|
+
run_id: runId,
|
|
32
|
+
snapshot,
|
|
33
|
+
createdAt: new Date(),
|
|
34
|
+
updatedAt: new Date(),
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
{ upsert: true },
|
|
38
|
+
);
|
|
39
|
+
} catch (error) {
|
|
40
|
+
throw new MastraError(
|
|
41
|
+
{
|
|
42
|
+
id: 'STORAGE_MONGODB_STORE_PERSIST_WORKFLOW_SNAPSHOT_FAILED',
|
|
43
|
+
domain: ErrorDomain.STORAGE,
|
|
44
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
45
|
+
details: { workflowName, runId },
|
|
46
|
+
},
|
|
47
|
+
error,
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async loadWorkflowSnapshot({
|
|
53
|
+
workflowName,
|
|
54
|
+
runId,
|
|
55
|
+
}: {
|
|
56
|
+
workflowName: string;
|
|
57
|
+
runId: string;
|
|
58
|
+
}): Promise<WorkflowRunState | null> {
|
|
59
|
+
try {
|
|
60
|
+
const result = await this.operations.load<any[]>({
|
|
61
|
+
tableName: TABLE_WORKFLOW_SNAPSHOT,
|
|
62
|
+
keys: {
|
|
63
|
+
workflow_name: workflowName,
|
|
64
|
+
run_id: runId,
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
if (!result?.length) {
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return typeof result[0].snapshot === 'string' ? safelyParseJSON(result[0].snapshot) : result[0].snapshot;
|
|
73
|
+
} catch (error) {
|
|
74
|
+
throw new MastraError(
|
|
75
|
+
{
|
|
76
|
+
id: 'STORAGE_MONGODB_STORE_LOAD_WORKFLOW_SNAPSHOT_FAILED',
|
|
77
|
+
domain: ErrorDomain.STORAGE,
|
|
78
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
79
|
+
details: { workflowName, runId },
|
|
80
|
+
},
|
|
81
|
+
error,
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async getWorkflowRuns(args?: {
|
|
87
|
+
workflowName?: string;
|
|
88
|
+
fromDate?: Date;
|
|
89
|
+
toDate?: Date;
|
|
90
|
+
limit?: number;
|
|
91
|
+
offset?: number;
|
|
92
|
+
resourceId?: string;
|
|
93
|
+
}): Promise<WorkflowRuns> {
|
|
94
|
+
const options = args || {};
|
|
95
|
+
try {
|
|
96
|
+
const query: any = {};
|
|
97
|
+
if (options.workflowName) {
|
|
98
|
+
query['workflow_name'] = options.workflowName;
|
|
99
|
+
}
|
|
100
|
+
if (options.fromDate) {
|
|
101
|
+
query['createdAt'] = { $gte: options.fromDate };
|
|
102
|
+
}
|
|
103
|
+
if (options.toDate) {
|
|
104
|
+
if (query['createdAt']) {
|
|
105
|
+
query['createdAt'].$lte = options.toDate;
|
|
106
|
+
} else {
|
|
107
|
+
query['createdAt'] = { $lte: options.toDate };
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
if (options.resourceId) {
|
|
111
|
+
query['resourceId'] = options.resourceId;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const collection = await this.operations.getCollection(TABLE_WORKFLOW_SNAPSHOT);
|
|
115
|
+
const total = await collection.countDocuments(query);
|
|
116
|
+
|
|
117
|
+
let cursor = collection.find(query).sort({ createdAt: -1 });
|
|
118
|
+
if (options.offset) {
|
|
119
|
+
cursor = cursor.skip(options.offset);
|
|
120
|
+
}
|
|
121
|
+
if (options.limit) {
|
|
122
|
+
cursor = cursor.limit(options.limit);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const results = await cursor.toArray();
|
|
126
|
+
|
|
127
|
+
const runs = results.map(row => this.parseWorkflowRun(row));
|
|
128
|
+
|
|
129
|
+
return {
|
|
130
|
+
runs,
|
|
131
|
+
total,
|
|
132
|
+
};
|
|
133
|
+
} catch (error) {
|
|
134
|
+
throw new MastraError(
|
|
135
|
+
{
|
|
136
|
+
id: 'STORAGE_MONGODB_STORE_GET_WORKFLOW_RUNS_FAILED',
|
|
137
|
+
domain: ErrorDomain.STORAGE,
|
|
138
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
139
|
+
details: { workflowName: options.workflowName || 'unknown' },
|
|
140
|
+
},
|
|
141
|
+
error,
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
async getWorkflowRunById(args: { runId: string; workflowName?: string }): Promise<WorkflowRun | null> {
|
|
147
|
+
try {
|
|
148
|
+
const query: any = {};
|
|
149
|
+
if (args.runId) {
|
|
150
|
+
query['run_id'] = args.runId;
|
|
151
|
+
}
|
|
152
|
+
if (args.workflowName) {
|
|
153
|
+
query['workflow_name'] = args.workflowName;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const collection = await this.operations.getCollection(TABLE_WORKFLOW_SNAPSHOT);
|
|
157
|
+
const result = await collection.findOne(query);
|
|
158
|
+
if (!result) {
|
|
159
|
+
return null;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
return this.parseWorkflowRun(result);
|
|
163
|
+
} catch (error) {
|
|
164
|
+
throw new MastraError(
|
|
165
|
+
{
|
|
166
|
+
id: 'STORAGE_MONGODB_STORE_GET_WORKFLOW_RUN_BY_ID_FAILED',
|
|
167
|
+
domain: ErrorDomain.STORAGE,
|
|
168
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
169
|
+
details: { runId: args.runId },
|
|
170
|
+
},
|
|
171
|
+
error,
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
private parseWorkflowRun(row: any): WorkflowRun {
|
|
177
|
+
let parsedSnapshot: WorkflowRunState | string = row.snapshot as string;
|
|
178
|
+
if (typeof parsedSnapshot === 'string') {
|
|
179
|
+
try {
|
|
180
|
+
parsedSnapshot = typeof row.snapshot === 'string' ? safelyParseJSON(row.snapshot as string) : row.snapshot;
|
|
181
|
+
} catch (e) {
|
|
182
|
+
// If parsing fails, return the raw snapshot string
|
|
183
|
+
console.warn(`Failed to parse snapshot for workflow ${row.workflow_name}: ${e}`);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
return {
|
|
188
|
+
workflowName: row.workflow_name as string,
|
|
189
|
+
runId: row.run_id as string,
|
|
190
|
+
snapshot: parsedSnapshot,
|
|
191
|
+
createdAt: new Date(row.createdAt as string),
|
|
192
|
+
updatedAt: new Date(row.updatedAt as string),
|
|
193
|
+
resourceId: row.resourceId,
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
}
|