@mastra/convex 0.1.0-beta.5 → 0.1.0-beta.6
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/CHANGELOG.md +218 -0
- package/dist/chunk-H5QJE733.cjs +104 -0
- package/dist/chunk-H5QJE733.cjs.map +1 -0
- package/dist/chunk-HXB4DWFE.js +73 -0
- package/dist/chunk-HXB4DWFE.js.map +1 -0
- package/dist/index.cjs +180 -281
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +168 -269
- package/dist/index.js.map +1 -1
- package/dist/schema.cjs +17 -17
- package/dist/schema.d.ts +52 -75
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +1 -1
- package/dist/server/index.cjs +14 -14
- package/dist/server/index.js +1 -1
- package/dist/storage/db/index.d.ts +57 -0
- package/dist/storage/db/index.d.ts.map +1 -0
- package/dist/storage/domains/{memory.d.ts → memory/index.d.ts} +6 -4
- package/dist/storage/domains/memory/index.d.ts.map +1 -0
- package/dist/storage/domains/{scores.d.ts → scores/index.d.ts} +11 -18
- package/dist/storage/domains/scores/index.d.ts.map +1 -0
- package/dist/storage/domains/{workflows.d.ts → workflows/index.d.ts} +6 -4
- package/dist/storage/domains/workflows/index.d.ts.map +1 -0
- package/dist/storage/index.d.ts +50 -151
- package/dist/storage/index.d.ts.map +1 -1
- package/package.json +2 -2
- package/dist/chunk-PKUUSREO.js +0 -76
- package/dist/chunk-PKUUSREO.js.map +0 -1
- package/dist/chunk-ZBUP3DS6.cjs +0 -93
- package/dist/chunk-ZBUP3DS6.cjs.map +0 -1
- package/dist/server/schema.d.ts +0 -115
- package/dist/server/schema.d.ts.map +0 -1
- package/dist/storage/domains/memory.d.ts.map +0 -1
- package/dist/storage/domains/scores.d.ts.map +0 -1
- package/dist/storage/domains/workflows.d.ts.map +0 -1
- package/dist/storage/operations.d.ts +0 -40
- package/dist/storage/operations.d.ts.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
export { mastraStorage } from './chunk-KSAPIIEJ.js';
|
|
2
|
-
export { TABLE_MESSAGES, TABLE_RESOURCES, TABLE_SCORERS, TABLE_THREADS, TABLE_WORKFLOW_SNAPSHOT, mastraDocumentsTable, mastraMessagesTable, mastraResourcesTable, mastraScoresTable, mastraThreadsTable, mastraVectorIndexesTable, mastraVectorsTable, mastraWorkflowSnapshotsTable } from './chunk-
|
|
3
|
-
import { MastraStorage,
|
|
2
|
+
export { TABLE_MESSAGES, TABLE_RESOURCES, TABLE_SCORERS, TABLE_THREADS, TABLE_WORKFLOW_SNAPSHOT, mastraDocumentsTable, mastraMessagesTable, mastraResourcesTable, mastraScoresTable, mastraThreadsTable, mastraVectorIndexesTable, mastraVectorsTable, mastraWorkflowSnapshotsTable } from './chunk-HXB4DWFE.js';
|
|
3
|
+
import { MastraStorage, MemoryStorage, TABLE_THREADS, TABLE_MESSAGES, TABLE_RESOURCES, createStorageErrorId, normalizePerPage, calculatePagination, safelyParseJSON, WorkflowsStorage, TABLE_WORKFLOW_SNAPSHOT, ScoresStorage, TABLE_SCORERS } from '@mastra/core/storage';
|
|
4
4
|
import { MessageList } from '@mastra/core/agent';
|
|
5
5
|
import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
|
|
6
6
|
import crypto from 'crypto';
|
|
7
|
+
import { MastraBase } from '@mastra/core/base';
|
|
7
8
|
import { MastraVector } from '@mastra/core/vector';
|
|
8
9
|
|
|
9
10
|
// src/storage/client.ts
|
|
@@ -69,13 +70,114 @@ var ConvexAdminClient = class {
|
|
|
69
70
|
return result;
|
|
70
71
|
}
|
|
71
72
|
};
|
|
73
|
+
function resolveConvexConfig(config) {
|
|
74
|
+
if ("client" in config) {
|
|
75
|
+
return config.client;
|
|
76
|
+
}
|
|
77
|
+
return new ConvexAdminClient(config);
|
|
78
|
+
}
|
|
79
|
+
var ConvexDB = class extends MastraBase {
|
|
80
|
+
constructor(client) {
|
|
81
|
+
super({ name: "convex-db" });
|
|
82
|
+
this.client = client;
|
|
83
|
+
}
|
|
84
|
+
async hasColumn(_table, _column) {
|
|
85
|
+
return true;
|
|
86
|
+
}
|
|
87
|
+
async clearTable({ tableName }) {
|
|
88
|
+
let hasMore = true;
|
|
89
|
+
while (hasMore) {
|
|
90
|
+
const response = await this.client.callStorageRaw({
|
|
91
|
+
op: "clearTable",
|
|
92
|
+
tableName
|
|
93
|
+
});
|
|
94
|
+
hasMore = response.hasMore ?? false;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
async dropTable({ tableName }) {
|
|
98
|
+
let hasMore = true;
|
|
99
|
+
while (hasMore) {
|
|
100
|
+
const response = await this.client.callStorageRaw({
|
|
101
|
+
op: "dropTable",
|
|
102
|
+
tableName
|
|
103
|
+
});
|
|
104
|
+
hasMore = response.hasMore ?? false;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
async insert({ tableName, record }) {
|
|
108
|
+
await this.client.callStorage({
|
|
109
|
+
op: "insert",
|
|
110
|
+
tableName,
|
|
111
|
+
record: this.normalizeRecord(tableName, record)
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
async batchInsert({ tableName, records }) {
|
|
115
|
+
if (records.length === 0) return;
|
|
116
|
+
await this.client.callStorage({
|
|
117
|
+
op: "batchInsert",
|
|
118
|
+
tableName,
|
|
119
|
+
records: records.map((record) => this.normalizeRecord(tableName, record))
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
async load({ tableName, keys }) {
|
|
123
|
+
const result = await this.client.callStorage({
|
|
124
|
+
op: "load",
|
|
125
|
+
tableName,
|
|
126
|
+
keys
|
|
127
|
+
});
|
|
128
|
+
return result;
|
|
129
|
+
}
|
|
130
|
+
async queryTable(tableName, filters) {
|
|
131
|
+
return this.client.callStorage({
|
|
132
|
+
op: "queryTable",
|
|
133
|
+
tableName,
|
|
134
|
+
filters
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
async deleteMany(tableName, ids) {
|
|
138
|
+
if (ids.length === 0) return;
|
|
139
|
+
await this.client.callStorage({
|
|
140
|
+
op: "deleteMany",
|
|
141
|
+
tableName,
|
|
142
|
+
ids
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
normalizeRecord(tableName, record) {
|
|
146
|
+
const normalized = { ...record };
|
|
147
|
+
if (tableName === TABLE_WORKFLOW_SNAPSHOT && !normalized.id) {
|
|
148
|
+
const runId = normalized.run_id || normalized.runId;
|
|
149
|
+
const workflowName = normalized.workflow_name || normalized.workflowName;
|
|
150
|
+
normalized.id = workflowName ? `${workflowName}-${runId}` : runId;
|
|
151
|
+
}
|
|
152
|
+
if (!normalized.id) {
|
|
153
|
+
normalized.id = crypto.randomUUID();
|
|
154
|
+
}
|
|
155
|
+
for (const [key, value] of Object.entries(normalized)) {
|
|
156
|
+
if (value instanceof Date) {
|
|
157
|
+
normalized[key] = value.toISOString();
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return normalized;
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
// src/storage/domains/memory/index.ts
|
|
72
165
|
var MemoryConvex = class extends MemoryStorage {
|
|
73
|
-
|
|
166
|
+
#db;
|
|
167
|
+
constructor(config) {
|
|
74
168
|
super();
|
|
75
|
-
|
|
169
|
+
const client = resolveConvexConfig(config);
|
|
170
|
+
this.#db = new ConvexDB(client);
|
|
171
|
+
}
|
|
172
|
+
async init() {
|
|
173
|
+
}
|
|
174
|
+
async dangerouslyClearAll() {
|
|
175
|
+
await this.#db.clearTable({ tableName: TABLE_THREADS });
|
|
176
|
+
await this.#db.clearTable({ tableName: TABLE_MESSAGES });
|
|
177
|
+
await this.#db.clearTable({ tableName: TABLE_RESOURCES });
|
|
76
178
|
}
|
|
77
179
|
async getThreadById({ threadId }) {
|
|
78
|
-
const row = await this.
|
|
180
|
+
const row = await this.#db.load({
|
|
79
181
|
tableName: TABLE_THREADS,
|
|
80
182
|
keys: { id: threadId }
|
|
81
183
|
});
|
|
@@ -88,7 +190,7 @@ var MemoryConvex = class extends MemoryStorage {
|
|
|
88
190
|
};
|
|
89
191
|
}
|
|
90
192
|
async saveThread({ thread }) {
|
|
91
|
-
await this.
|
|
193
|
+
await this.#db.insert({
|
|
92
194
|
tableName: TABLE_THREADS,
|
|
93
195
|
record: {
|
|
94
196
|
...thread,
|
|
@@ -124,21 +226,21 @@ var MemoryConvex = class extends MemoryStorage {
|
|
|
124
226
|
return updated;
|
|
125
227
|
}
|
|
126
228
|
async deleteThread({ threadId }) {
|
|
127
|
-
const messages = await this.
|
|
229
|
+
const messages = await this.#db.queryTable(TABLE_MESSAGES, [
|
|
128
230
|
{ field: "thread_id", value: threadId }
|
|
129
231
|
]);
|
|
130
|
-
await this.
|
|
232
|
+
await this.#db.deleteMany(
|
|
131
233
|
TABLE_MESSAGES,
|
|
132
234
|
messages.map((msg) => msg.id)
|
|
133
235
|
);
|
|
134
|
-
await this.
|
|
236
|
+
await this.#db.deleteMany(TABLE_THREADS, [threadId]);
|
|
135
237
|
}
|
|
136
238
|
async listThreadsByResourceId(args) {
|
|
137
239
|
const { resourceId, page = 0, perPage: perPageInput, orderBy } = args;
|
|
138
240
|
const perPage = normalizePerPage(perPageInput, 100);
|
|
139
241
|
const { field, direction } = this.parseOrderBy(orderBy);
|
|
140
242
|
const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
141
|
-
const rows = await this.
|
|
243
|
+
const rows = await this.#db.queryTable(TABLE_THREADS, [{ field: "resourceId", value: resourceId }]);
|
|
142
244
|
const threads = rows.map((row) => ({
|
|
143
245
|
...row,
|
|
144
246
|
metadata: typeof row.metadata === "string" ? JSON.parse(row.metadata) : row.metadata,
|
|
@@ -181,9 +283,7 @@ var MemoryConvex = class extends MemoryStorage {
|
|
|
181
283
|
const { field, direction } = this.parseOrderBy(orderBy, "ASC");
|
|
182
284
|
let rows = [];
|
|
183
285
|
for (const tid of threadIds) {
|
|
184
|
-
const threadRows = await this.
|
|
185
|
-
{ field: "thread_id", value: tid }
|
|
186
|
-
]);
|
|
286
|
+
const threadRows = await this.#db.queryTable(TABLE_MESSAGES, [{ field: "thread_id", value: tid }]);
|
|
187
287
|
rows.push(...threadRows);
|
|
188
288
|
}
|
|
189
289
|
if (resourceId) {
|
|
@@ -227,14 +327,14 @@ var MemoryConvex = class extends MemoryStorage {
|
|
|
227
327
|
}
|
|
228
328
|
}
|
|
229
329
|
if (!target) {
|
|
230
|
-
const messageRows = await this.
|
|
330
|
+
const messageRows = await this.#db.queryTable(TABLE_MESSAGES, [
|
|
231
331
|
{ field: "id", value: includeItem.id }
|
|
232
332
|
]);
|
|
233
333
|
if (messageRows.length > 0) {
|
|
234
334
|
target = messageRows[0];
|
|
235
335
|
targetThreadId = target.thread_id;
|
|
236
336
|
if (targetThreadId && !threadMessagesCache.has(targetThreadId)) {
|
|
237
|
-
const otherThreadRows = await this.
|
|
337
|
+
const otherThreadRows = await this.#db.queryTable(TABLE_MESSAGES, [
|
|
238
338
|
{ field: "thread_id", value: targetThreadId }
|
|
239
339
|
]);
|
|
240
340
|
threadMessagesCache.set(targetThreadId, otherThreadRows);
|
|
@@ -277,7 +377,7 @@ var MemoryConvex = class extends MemoryStorage {
|
|
|
277
377
|
if (messageIds.length === 0) {
|
|
278
378
|
return { messages: [] };
|
|
279
379
|
}
|
|
280
|
-
const rows = await this.
|
|
380
|
+
const rows = await this.#db.queryTable(TABLE_MESSAGES, void 0);
|
|
281
381
|
const filtered = rows.filter((row) => messageIds.includes(row.id)).map((row) => this.parseStoredMessage(row));
|
|
282
382
|
const list = new MessageList().add(filtered, "memory");
|
|
283
383
|
return { messages: list.get.all.db() };
|
|
@@ -302,7 +402,7 @@ var MemoryConvex = class extends MemoryStorage {
|
|
|
302
402
|
resourceId: message.resourceId
|
|
303
403
|
};
|
|
304
404
|
});
|
|
305
|
-
await this.
|
|
405
|
+
await this.#db.batchInsert({
|
|
306
406
|
tableName: TABLE_MESSAGES,
|
|
307
407
|
records: normalized
|
|
308
408
|
});
|
|
@@ -311,7 +411,7 @@ var MemoryConvex = class extends MemoryStorage {
|
|
|
311
411
|
for (const threadId of threadIds) {
|
|
312
412
|
const thread = await this.getThreadById({ threadId });
|
|
313
413
|
if (thread) {
|
|
314
|
-
await this.
|
|
414
|
+
await this.#db.insert({
|
|
315
415
|
tableName: TABLE_THREADS,
|
|
316
416
|
record: {
|
|
317
417
|
...thread,
|
|
@@ -330,7 +430,7 @@ var MemoryConvex = class extends MemoryStorage {
|
|
|
330
430
|
messages
|
|
331
431
|
}) {
|
|
332
432
|
if (messages.length === 0) return [];
|
|
333
|
-
const existing = await this.
|
|
433
|
+
const existing = await this.#db.queryTable(TABLE_MESSAGES, void 0);
|
|
334
434
|
const updated = [];
|
|
335
435
|
const affectedThreadIds = /* @__PURE__ */ new Set();
|
|
336
436
|
for (const update of messages) {
|
|
@@ -359,7 +459,7 @@ var MemoryConvex = class extends MemoryStorage {
|
|
|
359
459
|
};
|
|
360
460
|
current.content = JSON.stringify(mergedContent);
|
|
361
461
|
}
|
|
362
|
-
await this.
|
|
462
|
+
await this.#db.insert({
|
|
363
463
|
tableName: TABLE_MESSAGES,
|
|
364
464
|
record: current
|
|
365
465
|
});
|
|
@@ -369,7 +469,7 @@ var MemoryConvex = class extends MemoryStorage {
|
|
|
369
469
|
for (const threadId of affectedThreadIds) {
|
|
370
470
|
const thread = await this.getThreadById({ threadId });
|
|
371
471
|
if (thread) {
|
|
372
|
-
await this.
|
|
472
|
+
await this.#db.insert({
|
|
373
473
|
tableName: TABLE_THREADS,
|
|
374
474
|
record: {
|
|
375
475
|
...thread,
|
|
@@ -384,7 +484,7 @@ var MemoryConvex = class extends MemoryStorage {
|
|
|
384
484
|
return updated;
|
|
385
485
|
}
|
|
386
486
|
async deleteMessages(messageIds) {
|
|
387
|
-
await this.
|
|
487
|
+
await this.#db.deleteMany(TABLE_MESSAGES, messageIds);
|
|
388
488
|
}
|
|
389
489
|
async saveResource({ resource }) {
|
|
390
490
|
const record = {
|
|
@@ -395,14 +495,14 @@ var MemoryConvex = class extends MemoryStorage {
|
|
|
395
495
|
if (resource.metadata !== void 0) {
|
|
396
496
|
record.metadata = resource.metadata;
|
|
397
497
|
}
|
|
398
|
-
await this.
|
|
498
|
+
await this.#db.insert({
|
|
399
499
|
tableName: TABLE_RESOURCES,
|
|
400
500
|
record
|
|
401
501
|
});
|
|
402
502
|
return resource;
|
|
403
503
|
}
|
|
404
504
|
async getResourceById({ resourceId }) {
|
|
405
|
-
const record = await this.
|
|
505
|
+
const record = await this.#db.load({
|
|
406
506
|
tableName: TABLE_RESOURCES,
|
|
407
507
|
keys: { id: resourceId }
|
|
408
508
|
});
|
|
@@ -488,12 +588,19 @@ var MemoryConvex = class extends MemoryStorage {
|
|
|
488
588
|
}
|
|
489
589
|
};
|
|
490
590
|
var ScoresConvex = class extends ScoresStorage {
|
|
491
|
-
|
|
591
|
+
#db;
|
|
592
|
+
constructor(config) {
|
|
492
593
|
super();
|
|
493
|
-
|
|
594
|
+
const client = resolveConvexConfig(config);
|
|
595
|
+
this.#db = new ConvexDB(client);
|
|
596
|
+
}
|
|
597
|
+
async init() {
|
|
598
|
+
}
|
|
599
|
+
async dangerouslyClearAll() {
|
|
600
|
+
await this.#db.clearTable({ tableName: TABLE_SCORERS });
|
|
494
601
|
}
|
|
495
602
|
async getScoreById({ id }) {
|
|
496
|
-
const row = await this.
|
|
603
|
+
const row = await this.#db.load({
|
|
497
604
|
tableName: TABLE_SCORERS,
|
|
498
605
|
keys: { id }
|
|
499
606
|
});
|
|
@@ -507,7 +614,7 @@ var ScoresConvex = class extends ScoresStorage {
|
|
|
507
614
|
createdAt: now.toISOString(),
|
|
508
615
|
updatedAt: now.toISOString()
|
|
509
616
|
};
|
|
510
|
-
await this.
|
|
617
|
+
await this.#db.insert({
|
|
511
618
|
tableName: TABLE_SCORERS,
|
|
512
619
|
record
|
|
513
620
|
});
|
|
@@ -558,7 +665,7 @@ var ScoresConvex = class extends ScoresStorage {
|
|
|
558
665
|
new Error("page must be >= 0")
|
|
559
666
|
);
|
|
560
667
|
}
|
|
561
|
-
const rows = await this.
|
|
668
|
+
const rows = await this.#db.queryTable(TABLE_SCORERS, void 0);
|
|
562
669
|
const filtered = rows.filter((row) => filters.scorerId ? row.scorerId === filters.scorerId : true).filter((row) => filters.entityId ? row.entityId === filters.entityId : true).filter((row) => filters.entityType ? row.entityType === filters.entityType : true).filter((row) => filters.runId ? row.runId === filters.runId : true).filter((row) => filters.source ? row.source === filters.source : true).sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime());
|
|
563
670
|
const { perPage, page } = pagination;
|
|
564
671
|
const perPageValue = perPage === false ? filtered.length : perPage;
|
|
@@ -584,9 +691,16 @@ var ScoresConvex = class extends ScoresStorage {
|
|
|
584
691
|
}
|
|
585
692
|
};
|
|
586
693
|
var WorkflowsConvex = class extends WorkflowsStorage {
|
|
587
|
-
|
|
694
|
+
#db;
|
|
695
|
+
constructor(config) {
|
|
588
696
|
super();
|
|
589
|
-
|
|
697
|
+
const client = resolveConvexConfig(config);
|
|
698
|
+
this.#db = new ConvexDB(client);
|
|
699
|
+
}
|
|
700
|
+
async init() {
|
|
701
|
+
}
|
|
702
|
+
async dangerouslyClearAll() {
|
|
703
|
+
await this.#db.clearTable({ tableName: TABLE_WORKFLOW_SNAPSHOT });
|
|
590
704
|
}
|
|
591
705
|
async updateWorkflowResults({
|
|
592
706
|
workflowName,
|
|
@@ -633,11 +747,11 @@ var WorkflowsConvex = class extends WorkflowsStorage {
|
|
|
633
747
|
snapshot
|
|
634
748
|
}) {
|
|
635
749
|
const now = /* @__PURE__ */ new Date();
|
|
636
|
-
const existing = await this.
|
|
750
|
+
const existing = await this.#db.load({
|
|
637
751
|
tableName: TABLE_WORKFLOW_SNAPSHOT,
|
|
638
752
|
keys: { workflow_name: workflowName, run_id: runId }
|
|
639
753
|
});
|
|
640
|
-
await this.
|
|
754
|
+
await this.#db.insert({
|
|
641
755
|
tableName: TABLE_WORKFLOW_SNAPSHOT,
|
|
642
756
|
record: {
|
|
643
757
|
workflow_name: workflowName,
|
|
@@ -653,7 +767,7 @@ var WorkflowsConvex = class extends WorkflowsStorage {
|
|
|
653
767
|
workflowName,
|
|
654
768
|
runId
|
|
655
769
|
}) {
|
|
656
|
-
const row = await this.
|
|
770
|
+
const row = await this.#db.load({
|
|
657
771
|
tableName: TABLE_WORKFLOW_SNAPSHOT,
|
|
658
772
|
keys: { workflow_name: workflowName, run_id: runId }
|
|
659
773
|
});
|
|
@@ -662,7 +776,7 @@ var WorkflowsConvex = class extends WorkflowsStorage {
|
|
|
662
776
|
}
|
|
663
777
|
async listWorkflowRuns(args = {}) {
|
|
664
778
|
const { workflowName, fromDate, toDate, perPage, page, resourceId, status } = args;
|
|
665
|
-
let rows = await this.
|
|
779
|
+
let rows = await this.#db.queryTable(TABLE_WORKFLOW_SNAPSHOT, void 0);
|
|
666
780
|
if (workflowName) rows = rows.filter((run) => run.workflow_name === workflowName);
|
|
667
781
|
if (resourceId) rows = rows.filter((run) => run.resourceId === resourceId);
|
|
668
782
|
if (fromDate) rows = rows.filter((run) => new Date(run.createdAt).getTime() >= fromDate.getTime());
|
|
@@ -694,7 +808,7 @@ var WorkflowsConvex = class extends WorkflowsStorage {
|
|
|
694
808
|
runId,
|
|
695
809
|
workflowName
|
|
696
810
|
}) {
|
|
697
|
-
const runs = await this.
|
|
811
|
+
const runs = await this.#db.queryTable(TABLE_WORKFLOW_SNAPSHOT, void 0);
|
|
698
812
|
const match = runs.find((run) => run.run_id === runId && (!workflowName || run.workflow_name === workflowName));
|
|
699
813
|
if (!match) return null;
|
|
700
814
|
return {
|
|
@@ -707,10 +821,10 @@ var WorkflowsConvex = class extends WorkflowsStorage {
|
|
|
707
821
|
};
|
|
708
822
|
}
|
|
709
823
|
async deleteWorkflowRunById({ runId, workflowName }) {
|
|
710
|
-
await this.
|
|
824
|
+
await this.#db.deleteMany(TABLE_WORKFLOW_SNAPSHOT, [`${workflowName}-${runId}`]);
|
|
711
825
|
}
|
|
712
826
|
async getRun(workflowName, runId) {
|
|
713
|
-
const runs = await this.
|
|
827
|
+
const runs = await this.#db.queryTable(TABLE_WORKFLOW_SNAPSHOT, [
|
|
714
828
|
{ field: "workflow_name", value: workflowName }
|
|
715
829
|
]);
|
|
716
830
|
return runs.find((run) => run.run_id === runId) ?? null;
|
|
@@ -737,113 +851,24 @@ var WorkflowsConvex = class extends WorkflowsStorage {
|
|
|
737
851
|
return JSON.parse(JSON.stringify(run.snapshot));
|
|
738
852
|
}
|
|
739
853
|
};
|
|
740
|
-
var StoreOperationsConvex = class extends StoreOperations {
|
|
741
|
-
constructor(client) {
|
|
742
|
-
super();
|
|
743
|
-
this.client = client;
|
|
744
|
-
}
|
|
745
|
-
async hasColumn(_table, _column) {
|
|
746
|
-
return true;
|
|
747
|
-
}
|
|
748
|
-
async createTable(_args) {
|
|
749
|
-
}
|
|
750
|
-
async clearTable({ tableName }) {
|
|
751
|
-
let hasMore = true;
|
|
752
|
-
while (hasMore) {
|
|
753
|
-
const response = await this.client.callStorageRaw({
|
|
754
|
-
op: "clearTable",
|
|
755
|
-
tableName
|
|
756
|
-
});
|
|
757
|
-
hasMore = response.hasMore ?? false;
|
|
758
|
-
}
|
|
759
|
-
}
|
|
760
|
-
async dropTable({ tableName }) {
|
|
761
|
-
let hasMore = true;
|
|
762
|
-
while (hasMore) {
|
|
763
|
-
const response = await this.client.callStorageRaw({
|
|
764
|
-
op: "dropTable",
|
|
765
|
-
tableName
|
|
766
|
-
});
|
|
767
|
-
hasMore = response.hasMore ?? false;
|
|
768
|
-
}
|
|
769
|
-
}
|
|
770
|
-
async alterTable(_args) {
|
|
771
|
-
}
|
|
772
|
-
async insert({ tableName, record }) {
|
|
773
|
-
await this.client.callStorage({
|
|
774
|
-
op: "insert",
|
|
775
|
-
tableName,
|
|
776
|
-
record: this.normalizeRecord(tableName, record)
|
|
777
|
-
});
|
|
778
|
-
}
|
|
779
|
-
async batchInsert({ tableName, records }) {
|
|
780
|
-
if (records.length === 0) return;
|
|
781
|
-
await this.client.callStorage({
|
|
782
|
-
op: "batchInsert",
|
|
783
|
-
tableName,
|
|
784
|
-
records: records.map((record) => this.normalizeRecord(tableName, record))
|
|
785
|
-
});
|
|
786
|
-
}
|
|
787
|
-
async load({ tableName, keys }) {
|
|
788
|
-
const result = await this.client.callStorage({
|
|
789
|
-
op: "load",
|
|
790
|
-
tableName,
|
|
791
|
-
keys
|
|
792
|
-
});
|
|
793
|
-
return result;
|
|
794
|
-
}
|
|
795
|
-
async queryTable(tableName, filters) {
|
|
796
|
-
return this.client.callStorage({
|
|
797
|
-
op: "queryTable",
|
|
798
|
-
tableName,
|
|
799
|
-
filters
|
|
800
|
-
});
|
|
801
|
-
}
|
|
802
|
-
async deleteMany(tableName, ids) {
|
|
803
|
-
if (ids.length === 0) return;
|
|
804
|
-
await this.client.callStorage({
|
|
805
|
-
op: "deleteMany",
|
|
806
|
-
tableName,
|
|
807
|
-
ids
|
|
808
|
-
});
|
|
809
|
-
}
|
|
810
|
-
normalizeRecord(tableName, record) {
|
|
811
|
-
const normalized = { ...record };
|
|
812
|
-
if (tableName === TABLE_WORKFLOW_SNAPSHOT && !normalized.id) {
|
|
813
|
-
const runId = normalized.run_id || normalized.runId;
|
|
814
|
-
const workflowName = normalized.workflow_name || normalized.workflowName;
|
|
815
|
-
normalized.id = workflowName ? `${workflowName}-${runId}` : runId;
|
|
816
|
-
}
|
|
817
|
-
if (!normalized.id) {
|
|
818
|
-
normalized.id = crypto.randomUUID();
|
|
819
|
-
}
|
|
820
|
-
for (const [key, value] of Object.entries(normalized)) {
|
|
821
|
-
if (value instanceof Date) {
|
|
822
|
-
normalized[key] = value.toISOString();
|
|
823
|
-
}
|
|
824
|
-
}
|
|
825
|
-
return normalized;
|
|
826
|
-
}
|
|
827
|
-
};
|
|
828
854
|
|
|
829
855
|
// src/storage/index.ts
|
|
856
|
+
var isClientConfig = (config) => {
|
|
857
|
+
return "client" in config;
|
|
858
|
+
};
|
|
830
859
|
var ConvexStore = class extends MastraStorage {
|
|
831
|
-
|
|
832
|
-
memory;
|
|
833
|
-
workflows;
|
|
834
|
-
scores;
|
|
860
|
+
stores = {};
|
|
835
861
|
constructor(config) {
|
|
836
862
|
super({ id: config.id, name: config.name ?? "ConvexStore", disableInit: config.disableInit });
|
|
837
|
-
const client = new ConvexAdminClient(config);
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
863
|
+
const client = isClientConfig(config) ? config.client : new ConvexAdminClient(config);
|
|
864
|
+
const domainConfig = { client };
|
|
865
|
+
const memory = new MemoryConvex(domainConfig);
|
|
866
|
+
const workflows = new WorkflowsConvex(domainConfig);
|
|
867
|
+
const scores = new ScoresConvex(domainConfig);
|
|
842
868
|
this.stores = {
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
scores: this.scores
|
|
869
|
+
memory,
|
|
870
|
+
workflows,
|
|
871
|
+
scores
|
|
847
872
|
};
|
|
848
873
|
}
|
|
849
874
|
get supports() {
|
|
@@ -853,138 +878,12 @@ var ConvexStore = class extends MastraStorage {
|
|
|
853
878
|
hasColumn: false,
|
|
854
879
|
createTable: false,
|
|
855
880
|
deleteMessages: true,
|
|
856
|
-
|
|
857
|
-
|
|
881
|
+
observability: false,
|
|
882
|
+
indexManagement: false,
|
|
883
|
+
listScoresBySpan: false,
|
|
884
|
+
agents: false
|
|
858
885
|
};
|
|
859
886
|
}
|
|
860
|
-
async createTable(_args) {
|
|
861
|
-
}
|
|
862
|
-
async clearTable({ tableName }) {
|
|
863
|
-
await this.operations.clearTable({ tableName });
|
|
864
|
-
}
|
|
865
|
-
async dropTable({ tableName }) {
|
|
866
|
-
await this.operations.dropTable({ tableName });
|
|
867
|
-
}
|
|
868
|
-
async alterTable(_args) {
|
|
869
|
-
}
|
|
870
|
-
async insert({ tableName, record }) {
|
|
871
|
-
await this.operations.insert({ tableName, record });
|
|
872
|
-
}
|
|
873
|
-
async batchInsert({ tableName, records }) {
|
|
874
|
-
await this.operations.batchInsert({ tableName, records });
|
|
875
|
-
}
|
|
876
|
-
async load({ tableName, keys }) {
|
|
877
|
-
return this.operations.load({ tableName, keys });
|
|
878
|
-
}
|
|
879
|
-
async getThreadById({ threadId }) {
|
|
880
|
-
return this.memory.getThreadById({ threadId });
|
|
881
|
-
}
|
|
882
|
-
async saveThread({ thread }) {
|
|
883
|
-
return this.memory.saveThread({ thread });
|
|
884
|
-
}
|
|
885
|
-
async updateThread({
|
|
886
|
-
id,
|
|
887
|
-
title,
|
|
888
|
-
metadata
|
|
889
|
-
}) {
|
|
890
|
-
return this.memory.updateThread({ id, title, metadata });
|
|
891
|
-
}
|
|
892
|
-
async deleteThread({ threadId }) {
|
|
893
|
-
await this.memory.deleteThread({ threadId });
|
|
894
|
-
}
|
|
895
|
-
async listMessages(args) {
|
|
896
|
-
return this.memory.listMessages(args);
|
|
897
|
-
}
|
|
898
|
-
async listMessagesById({ messageIds }) {
|
|
899
|
-
return this.memory.listMessagesById({ messageIds });
|
|
900
|
-
}
|
|
901
|
-
async saveMessages(args) {
|
|
902
|
-
return this.memory.saveMessages(args);
|
|
903
|
-
}
|
|
904
|
-
async updateMessages({
|
|
905
|
-
messages
|
|
906
|
-
}) {
|
|
907
|
-
return this.memory.updateMessages({ messages });
|
|
908
|
-
}
|
|
909
|
-
async deleteMessages(messageIds) {
|
|
910
|
-
await this.memory.deleteMessages(messageIds);
|
|
911
|
-
}
|
|
912
|
-
async listThreadsByResourceId(args) {
|
|
913
|
-
return this.memory.listThreadsByResourceId(args);
|
|
914
|
-
}
|
|
915
|
-
async getResourceById({ resourceId }) {
|
|
916
|
-
return this.memory.getResourceById({ resourceId });
|
|
917
|
-
}
|
|
918
|
-
async saveResource({ resource }) {
|
|
919
|
-
return this.memory.saveResource({ resource });
|
|
920
|
-
}
|
|
921
|
-
async updateResource({
|
|
922
|
-
resourceId,
|
|
923
|
-
workingMemory,
|
|
924
|
-
metadata
|
|
925
|
-
}) {
|
|
926
|
-
return this.memory.updateResource({ resourceId, workingMemory, metadata });
|
|
927
|
-
}
|
|
928
|
-
async updateWorkflowResults(params) {
|
|
929
|
-
return this.workflows.updateWorkflowResults(params);
|
|
930
|
-
}
|
|
931
|
-
async updateWorkflowState(params) {
|
|
932
|
-
return this.workflows.updateWorkflowState(params);
|
|
933
|
-
}
|
|
934
|
-
async persistWorkflowSnapshot({
|
|
935
|
-
workflowName,
|
|
936
|
-
runId,
|
|
937
|
-
resourceId,
|
|
938
|
-
snapshot
|
|
939
|
-
}) {
|
|
940
|
-
await this.workflows.persistWorkflowSnapshot({ workflowName, runId, resourceId, snapshot });
|
|
941
|
-
}
|
|
942
|
-
async loadWorkflowSnapshot({
|
|
943
|
-
workflowName,
|
|
944
|
-
runId
|
|
945
|
-
}) {
|
|
946
|
-
return this.workflows.loadWorkflowSnapshot({ workflowName, runId });
|
|
947
|
-
}
|
|
948
|
-
async listWorkflowRuns(args) {
|
|
949
|
-
return this.workflows.listWorkflowRuns(args);
|
|
950
|
-
}
|
|
951
|
-
async getWorkflowRunById({
|
|
952
|
-
runId,
|
|
953
|
-
workflowName
|
|
954
|
-
}) {
|
|
955
|
-
return this.workflows.getWorkflowRunById({ runId, workflowName });
|
|
956
|
-
}
|
|
957
|
-
async deleteWorkflowRunById({ runId, workflowName }) {
|
|
958
|
-
return this.workflows.deleteWorkflowRunById({ runId, workflowName });
|
|
959
|
-
}
|
|
960
|
-
async getScoreById({ id }) {
|
|
961
|
-
return this.scores.getScoreById({ id });
|
|
962
|
-
}
|
|
963
|
-
async saveScore(score) {
|
|
964
|
-
return this.scores.saveScore(score);
|
|
965
|
-
}
|
|
966
|
-
async listScoresByScorerId({
|
|
967
|
-
scorerId,
|
|
968
|
-
pagination,
|
|
969
|
-
entityId,
|
|
970
|
-
entityType,
|
|
971
|
-
source
|
|
972
|
-
}) {
|
|
973
|
-
return this.scores.listScoresByScorerId({ scorerId, pagination, entityId, entityType, source });
|
|
974
|
-
}
|
|
975
|
-
async listScoresByRunId({
|
|
976
|
-
runId,
|
|
977
|
-
pagination
|
|
978
|
-
}) {
|
|
979
|
-
return this.scores.listScoresByRunId({ runId, pagination });
|
|
980
|
-
}
|
|
981
|
-
async listScoresByEntityId({
|
|
982
|
-
entityId,
|
|
983
|
-
entityType,
|
|
984
|
-
pagination
|
|
985
|
-
}) {
|
|
986
|
-
return this.scores.listScoresByEntityId({ entityId, entityType, pagination });
|
|
987
|
-
}
|
|
988
887
|
};
|
|
989
888
|
var INDEX_METADATA_TABLE = "mastra_vector_indexes";
|
|
990
889
|
var ConvexVector = class extends MastraVector {
|