@mastra/convex 0.0.0-scorers-logs-20251208123838 → 0.0.0-standard-schema-20260123120255
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 +901 -5
- package/README.md +1 -1
- package/dist/{chunk-QKN2PWR2.cjs → chunk-BKVR7SL7.cjs} +2 -89
- package/dist/chunk-BKVR7SL7.cjs.map +1 -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/{chunk-NZCHEPNU.js → chunk-KSAPIIEJ.js} +5 -65
- package/dist/chunk-KSAPIIEJ.js.map +1 -0
- package/dist/docs/README.md +32 -0
- package/dist/docs/SKILL.md +46 -0
- package/dist/docs/SOURCE_MAP.json +63 -0
- package/dist/docs/storage/01-reference.md +140 -0
- package/dist/docs/vectors/01-reference.md +240 -0
- package/dist/index.cjs +223 -303
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +207 -287
- package/dist/index.js.map +1 -1
- package/dist/schema.cjs +72 -0
- package/dist/schema.cjs.map +1 -0
- package/dist/{server/schema.d.ts → schema.d.ts} +14 -1
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +3 -0
- package/dist/schema.js.map +1 -0
- package/dist/server/index.cjs +19 -18
- package/dist/server/index.d.ts +1 -1
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +2 -1
- package/dist/storage/db/index.d.ts +66 -0
- package/dist/storage/db/index.d.ts.map +1 -0
- package/dist/storage/domains/{memory.d.ts → memory/index.d.ts} +8 -6
- 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} +13 -13
- package/dist/storage/domains/workflows/index.d.ts.map +1 -0
- package/dist/storage/index.d.ts +56 -155
- package/dist/storage/index.d.ts.map +1 -1
- package/package.json +23 -13
- package/dist/chunk-NZCHEPNU.js.map +0 -1
- package/dist/chunk-QKN2PWR2.cjs.map +0 -1
- 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,8 +1,10 @@
|
|
|
1
|
-
export {
|
|
2
|
-
|
|
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-HXB4DWFE.js';
|
|
3
|
+
import { MastraCompositeStore, MemoryStorage, TABLE_THREADS, TABLE_MESSAGES, TABLE_RESOURCES, createStorageErrorId, normalizePerPage, calculatePagination, filterByDateRange, safelyParseJSON, WorkflowsStorage, TABLE_WORKFLOW_SNAPSHOT, ScoresStorage, TABLE_SCORERS } from '@mastra/core/storage';
|
|
3
4
|
import { MessageList } from '@mastra/core/agent';
|
|
4
5
|
import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
|
|
5
6
|
import crypto from 'crypto';
|
|
7
|
+
import { MastraBase } from '@mastra/core/base';
|
|
6
8
|
import { MastraVector } from '@mastra/core/vector';
|
|
7
9
|
|
|
8
10
|
// src/storage/client.ts
|
|
@@ -68,13 +70,127 @@ var ConvexAdminClient = class {
|
|
|
68
70
|
return result;
|
|
69
71
|
}
|
|
70
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 createTable({
|
|
88
|
+
tableName,
|
|
89
|
+
schema: _schema
|
|
90
|
+
}) {
|
|
91
|
+
this.logger.debug(`ConvexDB: createTable called for ${tableName} (schema managed server-side)`);
|
|
92
|
+
}
|
|
93
|
+
async alterTable({
|
|
94
|
+
tableName,
|
|
95
|
+
schema: _schema,
|
|
96
|
+
ifNotExists: _ifNotExists
|
|
97
|
+
}) {
|
|
98
|
+
this.logger.debug(`ConvexDB: alterTable called for ${tableName} (schema managed server-side)`);
|
|
99
|
+
}
|
|
100
|
+
async clearTable({ tableName }) {
|
|
101
|
+
let hasMore = true;
|
|
102
|
+
while (hasMore) {
|
|
103
|
+
const response = await this.client.callStorageRaw({
|
|
104
|
+
op: "clearTable",
|
|
105
|
+
tableName
|
|
106
|
+
});
|
|
107
|
+
hasMore = response.hasMore ?? false;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
async dropTable({ tableName }) {
|
|
111
|
+
let hasMore = true;
|
|
112
|
+
while (hasMore) {
|
|
113
|
+
const response = await this.client.callStorageRaw({
|
|
114
|
+
op: "dropTable",
|
|
115
|
+
tableName
|
|
116
|
+
});
|
|
117
|
+
hasMore = response.hasMore ?? false;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
async insert({ tableName, record }) {
|
|
121
|
+
await this.client.callStorage({
|
|
122
|
+
op: "insert",
|
|
123
|
+
tableName,
|
|
124
|
+
record: this.normalizeRecord(tableName, record)
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
async batchInsert({ tableName, records }) {
|
|
128
|
+
if (records.length === 0) return;
|
|
129
|
+
await this.client.callStorage({
|
|
130
|
+
op: "batchInsert",
|
|
131
|
+
tableName,
|
|
132
|
+
records: records.map((record) => this.normalizeRecord(tableName, record))
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
async load({ tableName, keys }) {
|
|
136
|
+
const result = await this.client.callStorage({
|
|
137
|
+
op: "load",
|
|
138
|
+
tableName,
|
|
139
|
+
keys
|
|
140
|
+
});
|
|
141
|
+
return result;
|
|
142
|
+
}
|
|
143
|
+
async queryTable(tableName, filters) {
|
|
144
|
+
return this.client.callStorage({
|
|
145
|
+
op: "queryTable",
|
|
146
|
+
tableName,
|
|
147
|
+
filters
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
async deleteMany(tableName, ids) {
|
|
151
|
+
if (ids.length === 0) return;
|
|
152
|
+
await this.client.callStorage({
|
|
153
|
+
op: "deleteMany",
|
|
154
|
+
tableName,
|
|
155
|
+
ids
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
normalizeRecord(tableName, record) {
|
|
159
|
+
const normalized = { ...record };
|
|
160
|
+
if (tableName === TABLE_WORKFLOW_SNAPSHOT && !normalized.id) {
|
|
161
|
+
const runId = normalized.run_id || normalized.runId;
|
|
162
|
+
const workflowName = normalized.workflow_name || normalized.workflowName;
|
|
163
|
+
normalized.id = workflowName ? `${workflowName}-${runId}` : runId;
|
|
164
|
+
}
|
|
165
|
+
if (!normalized.id) {
|
|
166
|
+
normalized.id = crypto.randomUUID();
|
|
167
|
+
}
|
|
168
|
+
for (const [key, value] of Object.entries(normalized)) {
|
|
169
|
+
if (value instanceof Date) {
|
|
170
|
+
normalized[key] = value.toISOString();
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return normalized;
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
// src/storage/domains/memory/index.ts
|
|
71
178
|
var MemoryConvex = class extends MemoryStorage {
|
|
72
|
-
|
|
179
|
+
#db;
|
|
180
|
+
constructor(config) {
|
|
73
181
|
super();
|
|
74
|
-
|
|
182
|
+
const client = resolveConvexConfig(config);
|
|
183
|
+
this.#db = new ConvexDB(client);
|
|
184
|
+
}
|
|
185
|
+
async init() {
|
|
186
|
+
}
|
|
187
|
+
async dangerouslyClearAll() {
|
|
188
|
+
await this.#db.clearTable({ tableName: TABLE_THREADS });
|
|
189
|
+
await this.#db.clearTable({ tableName: TABLE_MESSAGES });
|
|
190
|
+
await this.#db.clearTable({ tableName: TABLE_RESOURCES });
|
|
75
191
|
}
|
|
76
192
|
async getThreadById({ threadId }) {
|
|
77
|
-
const row = await this.
|
|
193
|
+
const row = await this.#db.load({
|
|
78
194
|
tableName: TABLE_THREADS,
|
|
79
195
|
keys: { id: threadId }
|
|
80
196
|
});
|
|
@@ -87,7 +203,7 @@ var MemoryConvex = class extends MemoryStorage {
|
|
|
87
203
|
};
|
|
88
204
|
}
|
|
89
205
|
async saveThread({ thread }) {
|
|
90
|
-
await this.
|
|
206
|
+
await this.#db.insert({
|
|
91
207
|
tableName: TABLE_THREADS,
|
|
92
208
|
record: {
|
|
93
209
|
...thread,
|
|
@@ -123,27 +239,50 @@ var MemoryConvex = class extends MemoryStorage {
|
|
|
123
239
|
return updated;
|
|
124
240
|
}
|
|
125
241
|
async deleteThread({ threadId }) {
|
|
126
|
-
const messages = await this.
|
|
242
|
+
const messages = await this.#db.queryTable(TABLE_MESSAGES, [
|
|
127
243
|
{ field: "thread_id", value: threadId }
|
|
128
244
|
]);
|
|
129
|
-
await this.
|
|
245
|
+
await this.#db.deleteMany(
|
|
130
246
|
TABLE_MESSAGES,
|
|
131
247
|
messages.map((msg) => msg.id)
|
|
132
248
|
);
|
|
133
|
-
await this.
|
|
249
|
+
await this.#db.deleteMany(TABLE_THREADS, [threadId]);
|
|
134
250
|
}
|
|
135
|
-
async
|
|
136
|
-
const {
|
|
251
|
+
async listThreads(args) {
|
|
252
|
+
const { page = 0, perPage: perPageInput, orderBy, filter } = args;
|
|
253
|
+
try {
|
|
254
|
+
this.validatePaginationInput(page, perPageInput ?? 100);
|
|
255
|
+
} catch (error) {
|
|
256
|
+
throw new MastraError(
|
|
257
|
+
{
|
|
258
|
+
id: createStorageErrorId("CONVEX", "LIST_THREADS", "INVALID_PAGE"),
|
|
259
|
+
domain: ErrorDomain.STORAGE,
|
|
260
|
+
category: ErrorCategory.USER,
|
|
261
|
+
details: { page, ...perPageInput !== void 0 && { perPage: perPageInput } }
|
|
262
|
+
},
|
|
263
|
+
error instanceof Error ? error : new Error("Invalid pagination parameters")
|
|
264
|
+
);
|
|
265
|
+
}
|
|
137
266
|
const perPage = normalizePerPage(perPageInput, 100);
|
|
138
267
|
const { field, direction } = this.parseOrderBy(orderBy);
|
|
139
268
|
const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
140
|
-
const
|
|
141
|
-
|
|
269
|
+
const queryFilters = [];
|
|
270
|
+
if (filter?.resourceId) {
|
|
271
|
+
queryFilters.push({ field: "resourceId", value: filter.resourceId });
|
|
272
|
+
}
|
|
273
|
+
const rows = await this.#db.queryTable(TABLE_THREADS, queryFilters);
|
|
274
|
+
let threads = rows.map((row) => ({
|
|
142
275
|
...row,
|
|
143
276
|
metadata: typeof row.metadata === "string" ? JSON.parse(row.metadata) : row.metadata,
|
|
144
277
|
createdAt: new Date(row.createdAt),
|
|
145
278
|
updatedAt: new Date(row.updatedAt)
|
|
146
279
|
}));
|
|
280
|
+
if (filter?.metadata && Object.keys(filter.metadata).length > 0) {
|
|
281
|
+
threads = threads.filter((thread) => {
|
|
282
|
+
if (!thread.metadata) return false;
|
|
283
|
+
return Object.entries(filter.metadata).every(([key, value]) => thread.metadata[key] === value);
|
|
284
|
+
});
|
|
285
|
+
}
|
|
147
286
|
threads.sort((a, b) => {
|
|
148
287
|
const aValue = a[field];
|
|
149
288
|
const bValue = b[field];
|
|
@@ -180,23 +319,13 @@ var MemoryConvex = class extends MemoryStorage {
|
|
|
180
319
|
const { field, direction } = this.parseOrderBy(orderBy, "ASC");
|
|
181
320
|
let rows = [];
|
|
182
321
|
for (const tid of threadIds) {
|
|
183
|
-
const threadRows = await this.
|
|
184
|
-
{ field: "thread_id", value: tid }
|
|
185
|
-
]);
|
|
322
|
+
const threadRows = await this.#db.queryTable(TABLE_MESSAGES, [{ field: "thread_id", value: tid }]);
|
|
186
323
|
rows.push(...threadRows);
|
|
187
324
|
}
|
|
188
325
|
if (resourceId) {
|
|
189
326
|
rows = rows.filter((row) => row.resourceId === resourceId);
|
|
190
327
|
}
|
|
191
|
-
|
|
192
|
-
const { start, end } = filter.dateRange;
|
|
193
|
-
rows = rows.filter((row) => {
|
|
194
|
-
const created = new Date(row.createdAt).getTime();
|
|
195
|
-
if (start && created < start.getTime()) return false;
|
|
196
|
-
if (end && created > end.getTime()) return false;
|
|
197
|
-
return true;
|
|
198
|
-
});
|
|
199
|
-
}
|
|
328
|
+
rows = filterByDateRange(rows, (row) => new Date(row.createdAt), filter?.dateRange);
|
|
200
329
|
rows.sort((a, b) => {
|
|
201
330
|
const aValue = field === "createdAt" || field === "updatedAt" ? new Date(a[field]).getTime() : a[field];
|
|
202
331
|
const bValue = field === "createdAt" || field === "updatedAt" ? new Date(b[field]).getTime() : b[field];
|
|
@@ -226,14 +355,14 @@ var MemoryConvex = class extends MemoryStorage {
|
|
|
226
355
|
}
|
|
227
356
|
}
|
|
228
357
|
if (!target) {
|
|
229
|
-
const messageRows = await this.
|
|
358
|
+
const messageRows = await this.#db.queryTable(TABLE_MESSAGES, [
|
|
230
359
|
{ field: "id", value: includeItem.id }
|
|
231
360
|
]);
|
|
232
361
|
if (messageRows.length > 0) {
|
|
233
362
|
target = messageRows[0];
|
|
234
363
|
targetThreadId = target.thread_id;
|
|
235
364
|
if (targetThreadId && !threadMessagesCache.has(targetThreadId)) {
|
|
236
|
-
const otherThreadRows = await this.
|
|
365
|
+
const otherThreadRows = await this.#db.queryTable(TABLE_MESSAGES, [
|
|
237
366
|
{ field: "thread_id", value: targetThreadId }
|
|
238
367
|
]);
|
|
239
368
|
threadMessagesCache.set(targetThreadId, otherThreadRows);
|
|
@@ -276,7 +405,7 @@ var MemoryConvex = class extends MemoryStorage {
|
|
|
276
405
|
if (messageIds.length === 0) {
|
|
277
406
|
return { messages: [] };
|
|
278
407
|
}
|
|
279
|
-
const rows = await this.
|
|
408
|
+
const rows = await this.#db.queryTable(TABLE_MESSAGES, void 0);
|
|
280
409
|
const filtered = rows.filter((row) => messageIds.includes(row.id)).map((row) => this.parseStoredMessage(row));
|
|
281
410
|
const list = new MessageList().add(filtered, "memory");
|
|
282
411
|
return { messages: list.get.all.db() };
|
|
@@ -301,7 +430,7 @@ var MemoryConvex = class extends MemoryStorage {
|
|
|
301
430
|
resourceId: message.resourceId
|
|
302
431
|
};
|
|
303
432
|
});
|
|
304
|
-
await this.
|
|
433
|
+
await this.#db.batchInsert({
|
|
305
434
|
tableName: TABLE_MESSAGES,
|
|
306
435
|
records: normalized
|
|
307
436
|
});
|
|
@@ -310,7 +439,7 @@ var MemoryConvex = class extends MemoryStorage {
|
|
|
310
439
|
for (const threadId of threadIds) {
|
|
311
440
|
const thread = await this.getThreadById({ threadId });
|
|
312
441
|
if (thread) {
|
|
313
|
-
await this.
|
|
442
|
+
await this.#db.insert({
|
|
314
443
|
tableName: TABLE_THREADS,
|
|
315
444
|
record: {
|
|
316
445
|
...thread,
|
|
@@ -329,7 +458,7 @@ var MemoryConvex = class extends MemoryStorage {
|
|
|
329
458
|
messages
|
|
330
459
|
}) {
|
|
331
460
|
if (messages.length === 0) return [];
|
|
332
|
-
const existing = await this.
|
|
461
|
+
const existing = await this.#db.queryTable(TABLE_MESSAGES, void 0);
|
|
333
462
|
const updated = [];
|
|
334
463
|
const affectedThreadIds = /* @__PURE__ */ new Set();
|
|
335
464
|
for (const update of messages) {
|
|
@@ -358,7 +487,7 @@ var MemoryConvex = class extends MemoryStorage {
|
|
|
358
487
|
};
|
|
359
488
|
current.content = JSON.stringify(mergedContent);
|
|
360
489
|
}
|
|
361
|
-
await this.
|
|
490
|
+
await this.#db.insert({
|
|
362
491
|
tableName: TABLE_MESSAGES,
|
|
363
492
|
record: current
|
|
364
493
|
});
|
|
@@ -368,7 +497,7 @@ var MemoryConvex = class extends MemoryStorage {
|
|
|
368
497
|
for (const threadId of affectedThreadIds) {
|
|
369
498
|
const thread = await this.getThreadById({ threadId });
|
|
370
499
|
if (thread) {
|
|
371
|
-
await this.
|
|
500
|
+
await this.#db.insert({
|
|
372
501
|
tableName: TABLE_THREADS,
|
|
373
502
|
record: {
|
|
374
503
|
...thread,
|
|
@@ -383,7 +512,7 @@ var MemoryConvex = class extends MemoryStorage {
|
|
|
383
512
|
return updated;
|
|
384
513
|
}
|
|
385
514
|
async deleteMessages(messageIds) {
|
|
386
|
-
await this.
|
|
515
|
+
await this.#db.deleteMany(TABLE_MESSAGES, messageIds);
|
|
387
516
|
}
|
|
388
517
|
async saveResource({ resource }) {
|
|
389
518
|
const record = {
|
|
@@ -394,14 +523,14 @@ var MemoryConvex = class extends MemoryStorage {
|
|
|
394
523
|
if (resource.metadata !== void 0) {
|
|
395
524
|
record.metadata = resource.metadata;
|
|
396
525
|
}
|
|
397
|
-
await this.
|
|
526
|
+
await this.#db.insert({
|
|
398
527
|
tableName: TABLE_RESOURCES,
|
|
399
528
|
record
|
|
400
529
|
});
|
|
401
530
|
return resource;
|
|
402
531
|
}
|
|
403
532
|
async getResourceById({ resourceId }) {
|
|
404
|
-
const record = await this.
|
|
533
|
+
const record = await this.#db.load({
|
|
405
534
|
tableName: TABLE_RESOURCES,
|
|
406
535
|
keys: { id: resourceId }
|
|
407
536
|
});
|
|
@@ -487,12 +616,19 @@ var MemoryConvex = class extends MemoryStorage {
|
|
|
487
616
|
}
|
|
488
617
|
};
|
|
489
618
|
var ScoresConvex = class extends ScoresStorage {
|
|
490
|
-
|
|
619
|
+
#db;
|
|
620
|
+
constructor(config) {
|
|
491
621
|
super();
|
|
492
|
-
|
|
622
|
+
const client = resolveConvexConfig(config);
|
|
623
|
+
this.#db = new ConvexDB(client);
|
|
624
|
+
}
|
|
625
|
+
async init() {
|
|
626
|
+
}
|
|
627
|
+
async dangerouslyClearAll() {
|
|
628
|
+
await this.#db.clearTable({ tableName: TABLE_SCORERS });
|
|
493
629
|
}
|
|
494
630
|
async getScoreById({ id }) {
|
|
495
|
-
const row = await this.
|
|
631
|
+
const row = await this.#db.load({
|
|
496
632
|
tableName: TABLE_SCORERS,
|
|
497
633
|
keys: { id }
|
|
498
634
|
});
|
|
@@ -506,7 +642,7 @@ var ScoresConvex = class extends ScoresStorage {
|
|
|
506
642
|
createdAt: now.toISOString(),
|
|
507
643
|
updatedAt: now.toISOString()
|
|
508
644
|
};
|
|
509
|
-
await this.
|
|
645
|
+
await this.#db.insert({
|
|
510
646
|
tableName: TABLE_SCORERS,
|
|
511
647
|
record
|
|
512
648
|
});
|
|
@@ -557,7 +693,7 @@ var ScoresConvex = class extends ScoresStorage {
|
|
|
557
693
|
new Error("page must be >= 0")
|
|
558
694
|
);
|
|
559
695
|
}
|
|
560
|
-
const rows = await this.
|
|
696
|
+
const rows = await this.#db.queryTable(TABLE_SCORERS, void 0);
|
|
561
697
|
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());
|
|
562
698
|
const { perPage, page } = pagination;
|
|
563
699
|
const perPageValue = perPage === false ? filtered.length : perPage;
|
|
@@ -583,9 +719,16 @@ var ScoresConvex = class extends ScoresStorage {
|
|
|
583
719
|
}
|
|
584
720
|
};
|
|
585
721
|
var WorkflowsConvex = class extends WorkflowsStorage {
|
|
586
|
-
|
|
722
|
+
#db;
|
|
723
|
+
constructor(config) {
|
|
587
724
|
super();
|
|
588
|
-
|
|
725
|
+
const client = resolveConvexConfig(config);
|
|
726
|
+
this.#db = new ConvexDB(client);
|
|
727
|
+
}
|
|
728
|
+
async init() {
|
|
729
|
+
}
|
|
730
|
+
async dangerouslyClearAll() {
|
|
731
|
+
await this.#db.clearTable({ tableName: TABLE_WORKFLOW_SNAPSHOT });
|
|
589
732
|
}
|
|
590
733
|
async updateWorkflowResults({
|
|
591
734
|
workflowName,
|
|
@@ -632,11 +775,11 @@ var WorkflowsConvex = class extends WorkflowsStorage {
|
|
|
632
775
|
snapshot
|
|
633
776
|
}) {
|
|
634
777
|
const now = /* @__PURE__ */ new Date();
|
|
635
|
-
const existing = await this.
|
|
778
|
+
const existing = await this.#db.load({
|
|
636
779
|
tableName: TABLE_WORKFLOW_SNAPSHOT,
|
|
637
780
|
keys: { workflow_name: workflowName, run_id: runId }
|
|
638
781
|
});
|
|
639
|
-
await this.
|
|
782
|
+
await this.#db.insert({
|
|
640
783
|
tableName: TABLE_WORKFLOW_SNAPSHOT,
|
|
641
784
|
record: {
|
|
642
785
|
workflow_name: workflowName,
|
|
@@ -652,7 +795,7 @@ var WorkflowsConvex = class extends WorkflowsStorage {
|
|
|
652
795
|
workflowName,
|
|
653
796
|
runId
|
|
654
797
|
}) {
|
|
655
|
-
const row = await this.
|
|
798
|
+
const row = await this.#db.load({
|
|
656
799
|
tableName: TABLE_WORKFLOW_SNAPSHOT,
|
|
657
800
|
keys: { workflow_name: workflowName, run_id: runId }
|
|
658
801
|
});
|
|
@@ -661,7 +804,7 @@ var WorkflowsConvex = class extends WorkflowsStorage {
|
|
|
661
804
|
}
|
|
662
805
|
async listWorkflowRuns(args = {}) {
|
|
663
806
|
const { workflowName, fromDate, toDate, perPage, page, resourceId, status } = args;
|
|
664
|
-
let rows = await this.
|
|
807
|
+
let rows = await this.#db.queryTable(TABLE_WORKFLOW_SNAPSHOT, void 0);
|
|
665
808
|
if (workflowName) rows = rows.filter((run) => run.workflow_name === workflowName);
|
|
666
809
|
if (resourceId) rows = rows.filter((run) => run.resourceId === resourceId);
|
|
667
810
|
if (fromDate) rows = rows.filter((run) => new Date(run.createdAt).getTime() >= fromDate.getTime());
|
|
@@ -693,7 +836,7 @@ var WorkflowsConvex = class extends WorkflowsStorage {
|
|
|
693
836
|
runId,
|
|
694
837
|
workflowName
|
|
695
838
|
}) {
|
|
696
|
-
const runs = await this.
|
|
839
|
+
const runs = await this.#db.queryTable(TABLE_WORKFLOW_SNAPSHOT, void 0);
|
|
697
840
|
const match = runs.find((run) => run.run_id === runId && (!workflowName || run.workflow_name === workflowName));
|
|
698
841
|
if (!match) return null;
|
|
699
842
|
return {
|
|
@@ -705,8 +848,11 @@ var WorkflowsConvex = class extends WorkflowsStorage {
|
|
|
705
848
|
resourceId: match.resourceId
|
|
706
849
|
};
|
|
707
850
|
}
|
|
851
|
+
async deleteWorkflowRunById({ runId, workflowName }) {
|
|
852
|
+
await this.#db.deleteMany(TABLE_WORKFLOW_SNAPSHOT, [`${workflowName}-${runId}`]);
|
|
853
|
+
}
|
|
708
854
|
async getRun(workflowName, runId) {
|
|
709
|
-
const runs = await this.
|
|
855
|
+
const runs = await this.#db.queryTable(TABLE_WORKFLOW_SNAPSHOT, [
|
|
710
856
|
{ field: "workflow_name", value: workflowName }
|
|
711
857
|
]);
|
|
712
858
|
return runs.find((run) => run.run_id === runId) ?? null;
|
|
@@ -733,251 +879,25 @@ var WorkflowsConvex = class extends WorkflowsStorage {
|
|
|
733
879
|
return JSON.parse(JSON.stringify(run.snapshot));
|
|
734
880
|
}
|
|
735
881
|
};
|
|
736
|
-
var StoreOperationsConvex = class extends StoreOperations {
|
|
737
|
-
constructor(client) {
|
|
738
|
-
super();
|
|
739
|
-
this.client = client;
|
|
740
|
-
}
|
|
741
|
-
async hasColumn(_table, _column) {
|
|
742
|
-
return true;
|
|
743
|
-
}
|
|
744
|
-
async createTable(_args) {
|
|
745
|
-
}
|
|
746
|
-
async clearTable({ tableName }) {
|
|
747
|
-
let hasMore = true;
|
|
748
|
-
while (hasMore) {
|
|
749
|
-
const response = await this.client.callStorageRaw({
|
|
750
|
-
op: "clearTable",
|
|
751
|
-
tableName
|
|
752
|
-
});
|
|
753
|
-
hasMore = response.hasMore ?? false;
|
|
754
|
-
}
|
|
755
|
-
}
|
|
756
|
-
async dropTable({ tableName }) {
|
|
757
|
-
let hasMore = true;
|
|
758
|
-
while (hasMore) {
|
|
759
|
-
const response = await this.client.callStorageRaw({
|
|
760
|
-
op: "dropTable",
|
|
761
|
-
tableName
|
|
762
|
-
});
|
|
763
|
-
hasMore = response.hasMore ?? false;
|
|
764
|
-
}
|
|
765
|
-
}
|
|
766
|
-
async alterTable(_args) {
|
|
767
|
-
}
|
|
768
|
-
async insert({ tableName, record }) {
|
|
769
|
-
await this.client.callStorage({
|
|
770
|
-
op: "insert",
|
|
771
|
-
tableName,
|
|
772
|
-
record: this.normalizeRecord(tableName, record)
|
|
773
|
-
});
|
|
774
|
-
}
|
|
775
|
-
async batchInsert({ tableName, records }) {
|
|
776
|
-
if (records.length === 0) return;
|
|
777
|
-
await this.client.callStorage({
|
|
778
|
-
op: "batchInsert",
|
|
779
|
-
tableName,
|
|
780
|
-
records: records.map((record) => this.normalizeRecord(tableName, record))
|
|
781
|
-
});
|
|
782
|
-
}
|
|
783
|
-
async load({ tableName, keys }) {
|
|
784
|
-
const result = await this.client.callStorage({
|
|
785
|
-
op: "load",
|
|
786
|
-
tableName,
|
|
787
|
-
keys
|
|
788
|
-
});
|
|
789
|
-
return result;
|
|
790
|
-
}
|
|
791
|
-
async queryTable(tableName, filters) {
|
|
792
|
-
return this.client.callStorage({
|
|
793
|
-
op: "queryTable",
|
|
794
|
-
tableName,
|
|
795
|
-
filters
|
|
796
|
-
});
|
|
797
|
-
}
|
|
798
|
-
async deleteMany(tableName, ids) {
|
|
799
|
-
if (ids.length === 0) return;
|
|
800
|
-
await this.client.callStorage({
|
|
801
|
-
op: "deleteMany",
|
|
802
|
-
tableName,
|
|
803
|
-
ids
|
|
804
|
-
});
|
|
805
|
-
}
|
|
806
|
-
normalizeRecord(tableName, record) {
|
|
807
|
-
const normalized = { ...record };
|
|
808
|
-
if (tableName === TABLE_WORKFLOW_SNAPSHOT && !normalized.id) {
|
|
809
|
-
const runId = normalized.run_id || normalized.runId;
|
|
810
|
-
const workflowName = normalized.workflow_name || normalized.workflowName;
|
|
811
|
-
normalized.id = workflowName ? `${workflowName}-${runId}` : runId;
|
|
812
|
-
}
|
|
813
|
-
if (!normalized.id) {
|
|
814
|
-
normalized.id = crypto.randomUUID();
|
|
815
|
-
}
|
|
816
|
-
for (const [key, value] of Object.entries(normalized)) {
|
|
817
|
-
if (value instanceof Date) {
|
|
818
|
-
normalized[key] = value.toISOString();
|
|
819
|
-
}
|
|
820
|
-
}
|
|
821
|
-
return normalized;
|
|
822
|
-
}
|
|
823
|
-
};
|
|
824
882
|
|
|
825
883
|
// src/storage/index.ts
|
|
826
|
-
var
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
scores;
|
|
884
|
+
var isClientConfig = (config) => {
|
|
885
|
+
return "client" in config;
|
|
886
|
+
};
|
|
887
|
+
var ConvexStore = class extends MastraCompositeStore {
|
|
831
888
|
constructor(config) {
|
|
832
889
|
super({ id: config.id, name: config.name ?? "ConvexStore", disableInit: config.disableInit });
|
|
833
|
-
const client = new ConvexAdminClient(config);
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
890
|
+
const client = isClientConfig(config) ? config.client : new ConvexAdminClient(config);
|
|
891
|
+
const domainConfig = { client };
|
|
892
|
+
const memory = new MemoryConvex(domainConfig);
|
|
893
|
+
const workflows = new WorkflowsConvex(domainConfig);
|
|
894
|
+
const scores = new ScoresConvex(domainConfig);
|
|
838
895
|
this.stores = {
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
scores: this.scores
|
|
843
|
-
};
|
|
844
|
-
}
|
|
845
|
-
get supports() {
|
|
846
|
-
return {
|
|
847
|
-
selectByIncludeResourceScope: true,
|
|
848
|
-
resourceWorkingMemory: true,
|
|
849
|
-
hasColumn: false,
|
|
850
|
-
createTable: false,
|
|
851
|
-
deleteMessages: true,
|
|
852
|
-
observabilityInstance: false,
|
|
853
|
-
listScoresBySpan: false
|
|
896
|
+
memory,
|
|
897
|
+
workflows,
|
|
898
|
+
scores
|
|
854
899
|
};
|
|
855
900
|
}
|
|
856
|
-
async createTable(_args) {
|
|
857
|
-
}
|
|
858
|
-
async clearTable({ tableName }) {
|
|
859
|
-
await this.operations.clearTable({ tableName });
|
|
860
|
-
}
|
|
861
|
-
async dropTable({ tableName }) {
|
|
862
|
-
await this.operations.dropTable({ tableName });
|
|
863
|
-
}
|
|
864
|
-
async alterTable(_args) {
|
|
865
|
-
}
|
|
866
|
-
async insert({ tableName, record }) {
|
|
867
|
-
await this.operations.insert({ tableName, record });
|
|
868
|
-
}
|
|
869
|
-
async batchInsert({ tableName, records }) {
|
|
870
|
-
await this.operations.batchInsert({ tableName, records });
|
|
871
|
-
}
|
|
872
|
-
async load({ tableName, keys }) {
|
|
873
|
-
return this.operations.load({ tableName, keys });
|
|
874
|
-
}
|
|
875
|
-
async getThreadById({ threadId }) {
|
|
876
|
-
return this.memory.getThreadById({ threadId });
|
|
877
|
-
}
|
|
878
|
-
async saveThread({ thread }) {
|
|
879
|
-
return this.memory.saveThread({ thread });
|
|
880
|
-
}
|
|
881
|
-
async updateThread({
|
|
882
|
-
id,
|
|
883
|
-
title,
|
|
884
|
-
metadata
|
|
885
|
-
}) {
|
|
886
|
-
return this.memory.updateThread({ id, title, metadata });
|
|
887
|
-
}
|
|
888
|
-
async deleteThread({ threadId }) {
|
|
889
|
-
await this.memory.deleteThread({ threadId });
|
|
890
|
-
}
|
|
891
|
-
async listMessages(args) {
|
|
892
|
-
return this.memory.listMessages(args);
|
|
893
|
-
}
|
|
894
|
-
async listMessagesById({ messageIds }) {
|
|
895
|
-
return this.memory.listMessagesById({ messageIds });
|
|
896
|
-
}
|
|
897
|
-
async saveMessages(args) {
|
|
898
|
-
return this.memory.saveMessages(args);
|
|
899
|
-
}
|
|
900
|
-
async updateMessages({
|
|
901
|
-
messages
|
|
902
|
-
}) {
|
|
903
|
-
return this.memory.updateMessages({ messages });
|
|
904
|
-
}
|
|
905
|
-
async deleteMessages(messageIds) {
|
|
906
|
-
await this.memory.deleteMessages(messageIds);
|
|
907
|
-
}
|
|
908
|
-
async listThreadsByResourceId(args) {
|
|
909
|
-
return this.memory.listThreadsByResourceId(args);
|
|
910
|
-
}
|
|
911
|
-
async getResourceById({ resourceId }) {
|
|
912
|
-
return this.memory.getResourceById({ resourceId });
|
|
913
|
-
}
|
|
914
|
-
async saveResource({ resource }) {
|
|
915
|
-
return this.memory.saveResource({ resource });
|
|
916
|
-
}
|
|
917
|
-
async updateResource({
|
|
918
|
-
resourceId,
|
|
919
|
-
workingMemory,
|
|
920
|
-
metadata
|
|
921
|
-
}) {
|
|
922
|
-
return this.memory.updateResource({ resourceId, workingMemory, metadata });
|
|
923
|
-
}
|
|
924
|
-
async updateWorkflowResults(params) {
|
|
925
|
-
return this.workflows.updateWorkflowResults(params);
|
|
926
|
-
}
|
|
927
|
-
async updateWorkflowState(params) {
|
|
928
|
-
return this.workflows.updateWorkflowState(params);
|
|
929
|
-
}
|
|
930
|
-
async persistWorkflowSnapshot({
|
|
931
|
-
workflowName,
|
|
932
|
-
runId,
|
|
933
|
-
resourceId,
|
|
934
|
-
snapshot
|
|
935
|
-
}) {
|
|
936
|
-
await this.workflows.persistWorkflowSnapshot({ workflowName, runId, resourceId, snapshot });
|
|
937
|
-
}
|
|
938
|
-
async loadWorkflowSnapshot({
|
|
939
|
-
workflowName,
|
|
940
|
-
runId
|
|
941
|
-
}) {
|
|
942
|
-
return this.workflows.loadWorkflowSnapshot({ workflowName, runId });
|
|
943
|
-
}
|
|
944
|
-
async listWorkflowRuns(args) {
|
|
945
|
-
return this.workflows.listWorkflowRuns(args);
|
|
946
|
-
}
|
|
947
|
-
async getWorkflowRunById({
|
|
948
|
-
runId,
|
|
949
|
-
workflowName
|
|
950
|
-
}) {
|
|
951
|
-
return this.workflows.getWorkflowRunById({ runId, workflowName });
|
|
952
|
-
}
|
|
953
|
-
async getScoreById({ id }) {
|
|
954
|
-
return this.scores.getScoreById({ id });
|
|
955
|
-
}
|
|
956
|
-
async saveScore(score) {
|
|
957
|
-
return this.scores.saveScore(score);
|
|
958
|
-
}
|
|
959
|
-
async listScoresByScorerId({
|
|
960
|
-
scorerId,
|
|
961
|
-
pagination,
|
|
962
|
-
entityId,
|
|
963
|
-
entityType,
|
|
964
|
-
source
|
|
965
|
-
}) {
|
|
966
|
-
return this.scores.listScoresByScorerId({ scorerId, pagination, entityId, entityType, source });
|
|
967
|
-
}
|
|
968
|
-
async listScoresByRunId({
|
|
969
|
-
runId,
|
|
970
|
-
pagination
|
|
971
|
-
}) {
|
|
972
|
-
return this.scores.listScoresByRunId({ runId, pagination });
|
|
973
|
-
}
|
|
974
|
-
async listScoresByEntityId({
|
|
975
|
-
entityId,
|
|
976
|
-
entityType,
|
|
977
|
-
pagination
|
|
978
|
-
}) {
|
|
979
|
-
return this.scores.listScoresByEntityId({ entityId, entityType, pagination });
|
|
980
|
-
}
|
|
981
901
|
};
|
|
982
902
|
var INDEX_METADATA_TABLE = "mastra_vector_indexes";
|
|
983
903
|
var ConvexVector = class extends MastraVector {
|