@mastra/convex 0.0.0-main-test-2-20251127211532
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 +23 -0
- package/LICENSE.md +15 -0
- package/README.md +123 -0
- package/dist/chunk-NZCHEPNU.js +362 -0
- package/dist/chunk-NZCHEPNU.js.map +1 -0
- package/dist/chunk-QKN2PWR2.cjs +391 -0
- package/dist/chunk-QKN2PWR2.cjs.map +1 -0
- package/dist/index.cjs +1325 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1262 -0
- package/dist/index.js.map +1 -0
- package/dist/server/index.cjs +64 -0
- package/dist/server/index.cjs.map +1 -0
- package/dist/server/index.d.ts +3 -0
- package/dist/server/index.d.ts.map +1 -0
- package/dist/server/index.js +3 -0
- package/dist/server/index.js.map +1 -0
- package/dist/server/schema.d.ts +115 -0
- package/dist/server/schema.d.ts.map +1 -0
- package/dist/server/storage.d.ts +7 -0
- package/dist/server/storage.d.ts.map +1 -0
- package/dist/storage/client.d.ts +24 -0
- package/dist/storage/client.d.ts.map +1 -0
- package/dist/storage/domains/memory.d.ts +59 -0
- package/dist/storage/domains/memory.d.ts.map +1 -0
- package/dist/storage/domains/scores.d.ts +42 -0
- package/dist/storage/domains/scores.d.ts.map +1 -0
- package/dist/storage/domains/workflows.d.ts +44 -0
- package/dist/storage/domains/workflows.d.ts.map +1 -0
- package/dist/storage/index.d.ts +161 -0
- package/dist/storage/index.d.ts.map +1 -0
- package/dist/storage/operations.d.ts +40 -0
- package/dist/storage/operations.d.ts.map +1 -0
- package/dist/storage/types.d.ts +42 -0
- package/dist/storage/types.d.ts.map +1 -0
- package/dist/vector/index.d.ts +35 -0
- package/dist/vector/index.d.ts.map +1 -0
- package/package.json +75 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,1262 @@
|
|
|
1
|
+
export { TABLE_MESSAGES, TABLE_RESOURCES, TABLE_SCORERS, TABLE_THREADS, TABLE_WORKFLOW_SNAPSHOT, mastraDocumentsTable, mastraMessagesTable, mastraResourcesTable, mastraScoresTable, mastraStorage, mastraThreadsTable, mastraVectorIndexesTable, mastraVectorsTable, mastraWorkflowSnapshotsTable } from './chunk-NZCHEPNU.js';
|
|
2
|
+
import { MastraStorage, StoreOperations, TABLE_WORKFLOW_SNAPSHOT, MemoryStorage, TABLE_THREADS, TABLE_MESSAGES, normalizePerPage, calculatePagination, safelyParseJSON, TABLE_RESOURCES, WorkflowsStorage, ScoresStorage, TABLE_SCORERS } from '@mastra/core/storage';
|
|
3
|
+
import { MessageList } from '@mastra/core/agent';
|
|
4
|
+
import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
|
|
5
|
+
import crypto from 'crypto';
|
|
6
|
+
import { MastraVector } from '@mastra/core/vector';
|
|
7
|
+
|
|
8
|
+
// src/storage/client.ts
|
|
9
|
+
var DEFAULT_STORAGE_FUNCTION = "mastra/storage:handle";
|
|
10
|
+
var ConvexAdminClient = class {
|
|
11
|
+
deploymentUrl;
|
|
12
|
+
adminAuthToken;
|
|
13
|
+
storageFunction;
|
|
14
|
+
constructor({ deploymentUrl, adminAuthToken, storageFunction }) {
|
|
15
|
+
if (!deploymentUrl) {
|
|
16
|
+
throw new Error("ConvexAdminClient: deploymentUrl is required.");
|
|
17
|
+
}
|
|
18
|
+
if (!adminAuthToken) {
|
|
19
|
+
throw new Error("ConvexAdminClient: adminAuthToken is required.");
|
|
20
|
+
}
|
|
21
|
+
this.deploymentUrl = deploymentUrl.replace(/\/$/, "");
|
|
22
|
+
this.adminAuthToken = adminAuthToken;
|
|
23
|
+
this.storageFunction = storageFunction ?? DEFAULT_STORAGE_FUNCTION;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Call storage and return the full response including hasMore flag.
|
|
27
|
+
* Use this for operations that may need multiple calls (e.g., clearTable).
|
|
28
|
+
*/
|
|
29
|
+
async callStorageRaw(request) {
|
|
30
|
+
const url = `${this.deploymentUrl}/api/mutation`;
|
|
31
|
+
const response = await fetch(url, {
|
|
32
|
+
method: "POST",
|
|
33
|
+
headers: {
|
|
34
|
+
"Content-Type": "application/json",
|
|
35
|
+
Authorization: `Convex ${this.adminAuthToken}`
|
|
36
|
+
},
|
|
37
|
+
body: JSON.stringify({
|
|
38
|
+
path: this.storageFunction,
|
|
39
|
+
args: request,
|
|
40
|
+
format: "json"
|
|
41
|
+
})
|
|
42
|
+
});
|
|
43
|
+
if (!response.ok) {
|
|
44
|
+
const text = await response.text();
|
|
45
|
+
throw new Error(`Convex API error: ${response.status} ${text}`);
|
|
46
|
+
}
|
|
47
|
+
const result = await response.json();
|
|
48
|
+
if (result.status === "error") {
|
|
49
|
+
const error = new Error(result.errorMessage || "Unknown Convex error");
|
|
50
|
+
error.code = result.errorCode;
|
|
51
|
+
throw error;
|
|
52
|
+
}
|
|
53
|
+
const storageResponse = result.value;
|
|
54
|
+
if (!storageResponse?.ok) {
|
|
55
|
+
const errResponse = storageResponse;
|
|
56
|
+
const error = new Error(errResponse?.error || "Unknown Convex storage error");
|
|
57
|
+
error.code = errResponse?.code;
|
|
58
|
+
error.details = errResponse?.details;
|
|
59
|
+
throw error;
|
|
60
|
+
}
|
|
61
|
+
return {
|
|
62
|
+
result: storageResponse.result,
|
|
63
|
+
hasMore: storageResponse.hasMore
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
async callStorage(request) {
|
|
67
|
+
const { result } = await this.callStorageRaw(request);
|
|
68
|
+
return result;
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
var MemoryConvex = class extends MemoryStorage {
|
|
72
|
+
constructor(operations) {
|
|
73
|
+
super();
|
|
74
|
+
this.operations = operations;
|
|
75
|
+
}
|
|
76
|
+
async getThreadById({ threadId }) {
|
|
77
|
+
const row = await this.operations.load({
|
|
78
|
+
tableName: TABLE_THREADS,
|
|
79
|
+
keys: { id: threadId }
|
|
80
|
+
});
|
|
81
|
+
if (!row) return null;
|
|
82
|
+
return {
|
|
83
|
+
...row,
|
|
84
|
+
metadata: typeof row.metadata === "string" ? JSON.parse(row.metadata) : row.metadata,
|
|
85
|
+
createdAt: new Date(row.createdAt),
|
|
86
|
+
updatedAt: new Date(row.updatedAt)
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
async saveThread({ thread }) {
|
|
90
|
+
await this.operations.insert({
|
|
91
|
+
tableName: TABLE_THREADS,
|
|
92
|
+
record: {
|
|
93
|
+
...thread,
|
|
94
|
+
metadata: thread.metadata ?? {}
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
return thread;
|
|
98
|
+
}
|
|
99
|
+
async updateThread({
|
|
100
|
+
id,
|
|
101
|
+
title,
|
|
102
|
+
metadata
|
|
103
|
+
}) {
|
|
104
|
+
const existing = await this.getThreadById({ threadId: id });
|
|
105
|
+
if (!existing) {
|
|
106
|
+
throw new MastraError({
|
|
107
|
+
id: "CONVEX_STORAGE_THREAD_NOT_FOUND",
|
|
108
|
+
domain: ErrorDomain.STORAGE,
|
|
109
|
+
category: ErrorCategory.USER,
|
|
110
|
+
text: `Thread ${id} not found`
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
const updated = {
|
|
114
|
+
...existing,
|
|
115
|
+
title,
|
|
116
|
+
metadata: {
|
|
117
|
+
...existing.metadata,
|
|
118
|
+
...metadata
|
|
119
|
+
},
|
|
120
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
121
|
+
};
|
|
122
|
+
await this.saveThread({ thread: updated });
|
|
123
|
+
return updated;
|
|
124
|
+
}
|
|
125
|
+
async deleteThread({ threadId }) {
|
|
126
|
+
const messages = await this.operations.queryTable(TABLE_MESSAGES, [
|
|
127
|
+
{ field: "thread_id", value: threadId }
|
|
128
|
+
]);
|
|
129
|
+
await this.operations.deleteMany(
|
|
130
|
+
TABLE_MESSAGES,
|
|
131
|
+
messages.map((msg) => msg.id)
|
|
132
|
+
);
|
|
133
|
+
await this.operations.deleteMany(TABLE_THREADS, [threadId]);
|
|
134
|
+
}
|
|
135
|
+
async listThreadsByResourceId(args) {
|
|
136
|
+
const { resourceId, page = 0, perPage: perPageInput, orderBy } = args;
|
|
137
|
+
const perPage = normalizePerPage(perPageInput, 100);
|
|
138
|
+
const { field, direction } = this.parseOrderBy(orderBy);
|
|
139
|
+
const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
140
|
+
const rows = await this.operations.queryTable(TABLE_THREADS, [{ field: "resourceId", value: resourceId }]);
|
|
141
|
+
const threads = rows.map((row) => ({
|
|
142
|
+
...row,
|
|
143
|
+
metadata: typeof row.metadata === "string" ? JSON.parse(row.metadata) : row.metadata,
|
|
144
|
+
createdAt: new Date(row.createdAt),
|
|
145
|
+
updatedAt: new Date(row.updatedAt)
|
|
146
|
+
}));
|
|
147
|
+
threads.sort((a, b) => {
|
|
148
|
+
const aValue = a[field];
|
|
149
|
+
const bValue = b[field];
|
|
150
|
+
const aTime = aValue instanceof Date ? aValue.getTime() : new Date(aValue).getTime();
|
|
151
|
+
const bTime = bValue instanceof Date ? bValue.getTime() : new Date(bValue).getTime();
|
|
152
|
+
return direction === "ASC" ? aTime - bTime : bTime - aTime;
|
|
153
|
+
});
|
|
154
|
+
const total = threads.length;
|
|
155
|
+
const paginated = perPageInput === false ? threads : threads.slice(offset, offset + perPage);
|
|
156
|
+
return {
|
|
157
|
+
threads: paginated,
|
|
158
|
+
total,
|
|
159
|
+
page,
|
|
160
|
+
perPage: perPageForResponse,
|
|
161
|
+
hasMore: perPageInput === false ? false : offset + perPage < total
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
async listMessages(args) {
|
|
165
|
+
const { threadId, resourceId, include, filter, perPage: perPageInput, page = 0, orderBy } = args;
|
|
166
|
+
if (!threadId.trim()) {
|
|
167
|
+
throw new MastraError(
|
|
168
|
+
{
|
|
169
|
+
id: "CONVEX_STORAGE_LIST_MESSAGES_INVALID_THREAD_ID",
|
|
170
|
+
domain: ErrorDomain.STORAGE,
|
|
171
|
+
category: ErrorCategory.USER,
|
|
172
|
+
details: { threadId }
|
|
173
|
+
},
|
|
174
|
+
new Error("threadId must be a non-empty string")
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
const perPage = normalizePerPage(perPageInput, 40);
|
|
178
|
+
const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
179
|
+
const { field, direction } = this.parseOrderBy(orderBy, "ASC");
|
|
180
|
+
let rows = await this.operations.queryTable(TABLE_MESSAGES, [
|
|
181
|
+
{ field: "thread_id", value: threadId }
|
|
182
|
+
]);
|
|
183
|
+
if (resourceId) {
|
|
184
|
+
rows = rows.filter((row) => row.resourceId === resourceId);
|
|
185
|
+
}
|
|
186
|
+
if (filter?.dateRange) {
|
|
187
|
+
const { start, end } = filter.dateRange;
|
|
188
|
+
rows = rows.filter((row) => {
|
|
189
|
+
const created = new Date(row.createdAt).getTime();
|
|
190
|
+
if (start && created < start.getTime()) return false;
|
|
191
|
+
if (end && created > end.getTime()) return false;
|
|
192
|
+
return true;
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
rows.sort((a, b) => {
|
|
196
|
+
const aValue = field === "createdAt" || field === "updatedAt" ? new Date(a[field]).getTime() : a[field];
|
|
197
|
+
const bValue = field === "createdAt" || field === "updatedAt" ? new Date(b[field]).getTime() : b[field];
|
|
198
|
+
if (typeof aValue === "number" && typeof bValue === "number") {
|
|
199
|
+
return direction === "ASC" ? aValue - bValue : bValue - aValue;
|
|
200
|
+
}
|
|
201
|
+
return direction === "ASC" ? String(aValue).localeCompare(String(bValue)) : String(bValue).localeCompare(String(aValue));
|
|
202
|
+
});
|
|
203
|
+
const totalThreadMessages = rows.length;
|
|
204
|
+
const paginatedRows = perPageInput === false ? rows : rows.slice(offset, offset + perPage);
|
|
205
|
+
const messages = paginatedRows.map((row) => this.parseStoredMessage(row));
|
|
206
|
+
const messageIds = new Set(messages.map((msg) => msg.id));
|
|
207
|
+
if (include && include.length > 0) {
|
|
208
|
+
const threadMessagesCache = /* @__PURE__ */ new Map();
|
|
209
|
+
threadMessagesCache.set(threadId, rows);
|
|
210
|
+
for (const includeItem of include) {
|
|
211
|
+
const targetThreadId = includeItem.threadId || threadId;
|
|
212
|
+
if (!threadMessagesCache.has(targetThreadId)) {
|
|
213
|
+
const otherThreadRows = await this.operations.queryTable(TABLE_MESSAGES, [
|
|
214
|
+
{ field: "thread_id", value: targetThreadId }
|
|
215
|
+
]);
|
|
216
|
+
threadMessagesCache.set(targetThreadId, otherThreadRows);
|
|
217
|
+
}
|
|
218
|
+
const targetThreadRows = threadMessagesCache.get(targetThreadId) || [];
|
|
219
|
+
const target = targetThreadRows.find((row) => row.id === includeItem.id);
|
|
220
|
+
if (target && !messageIds.has(target.id)) {
|
|
221
|
+
messages.push(this.parseStoredMessage(target));
|
|
222
|
+
messageIds.add(target.id);
|
|
223
|
+
}
|
|
224
|
+
await this.addContextMessages({
|
|
225
|
+
includeItem,
|
|
226
|
+
allMessages: targetThreadRows,
|
|
227
|
+
targetThreadId,
|
|
228
|
+
messageIds,
|
|
229
|
+
messages
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
messages.sort((a, b) => {
|
|
234
|
+
const aValue = field === "createdAt" || field === "updatedAt" ? new Date(a[field]).getTime() : a[field];
|
|
235
|
+
const bValue = field === "createdAt" || field === "updatedAt" ? new Date(b[field]).getTime() : b[field];
|
|
236
|
+
if (typeof aValue === "number" && typeof bValue === "number") {
|
|
237
|
+
return direction === "ASC" ? aValue - bValue : bValue - aValue;
|
|
238
|
+
}
|
|
239
|
+
return direction === "ASC" ? String(aValue).localeCompare(String(bValue)) : String(bValue).localeCompare(String(aValue));
|
|
240
|
+
});
|
|
241
|
+
const hasMore = include && include.length > 0 ? new Set(messages.filter((m) => m.threadId === threadId).map((m) => m.id)).size < totalThreadMessages : perPageInput === false ? false : offset + perPage < totalThreadMessages;
|
|
242
|
+
return {
|
|
243
|
+
messages,
|
|
244
|
+
total: totalThreadMessages,
|
|
245
|
+
page,
|
|
246
|
+
perPage: perPageForResponse,
|
|
247
|
+
hasMore
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
async listMessagesById({ messageIds }) {
|
|
251
|
+
if (messageIds.length === 0) {
|
|
252
|
+
return { messages: [] };
|
|
253
|
+
}
|
|
254
|
+
const rows = await this.operations.queryTable(TABLE_MESSAGES, void 0);
|
|
255
|
+
const filtered = rows.filter((row) => messageIds.includes(row.id)).map((row) => this.parseStoredMessage(row));
|
|
256
|
+
const list = new MessageList().add(filtered, "memory");
|
|
257
|
+
return { messages: list.get.all.db() };
|
|
258
|
+
}
|
|
259
|
+
async saveMessages({ messages }) {
|
|
260
|
+
if (messages.length === 0) return { messages: [] };
|
|
261
|
+
const normalized = messages.map((message) => {
|
|
262
|
+
if (!message.threadId) {
|
|
263
|
+
throw new Error("Thread ID is required");
|
|
264
|
+
}
|
|
265
|
+
if (!message.resourceId) {
|
|
266
|
+
throw new Error("Resource ID is required");
|
|
267
|
+
}
|
|
268
|
+
const createdAt = message.createdAt instanceof Date ? message.createdAt.toISOString() : message.createdAt;
|
|
269
|
+
return {
|
|
270
|
+
id: message.id,
|
|
271
|
+
thread_id: message.threadId,
|
|
272
|
+
content: JSON.stringify(message.content),
|
|
273
|
+
role: message.role,
|
|
274
|
+
type: message.type || "v2",
|
|
275
|
+
createdAt,
|
|
276
|
+
resourceId: message.resourceId
|
|
277
|
+
};
|
|
278
|
+
});
|
|
279
|
+
await this.operations.batchInsert({
|
|
280
|
+
tableName: TABLE_MESSAGES,
|
|
281
|
+
records: normalized
|
|
282
|
+
});
|
|
283
|
+
const threadIds = [...new Set(messages.map((m) => m.threadId).filter(Boolean))];
|
|
284
|
+
const now = /* @__PURE__ */ new Date();
|
|
285
|
+
for (const threadId of threadIds) {
|
|
286
|
+
const thread = await this.getThreadById({ threadId });
|
|
287
|
+
if (thread) {
|
|
288
|
+
await this.operations.insert({
|
|
289
|
+
tableName: TABLE_THREADS,
|
|
290
|
+
record: {
|
|
291
|
+
...thread,
|
|
292
|
+
id: thread.id,
|
|
293
|
+
updatedAt: now.toISOString(),
|
|
294
|
+
createdAt: thread.createdAt instanceof Date ? thread.createdAt.toISOString() : thread.createdAt,
|
|
295
|
+
metadata: thread.metadata ?? {}
|
|
296
|
+
}
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
const list = new MessageList().add(messages, "memory");
|
|
301
|
+
return { messages: list.get.all.db() };
|
|
302
|
+
}
|
|
303
|
+
async updateMessages({
|
|
304
|
+
messages
|
|
305
|
+
}) {
|
|
306
|
+
if (messages.length === 0) return [];
|
|
307
|
+
const existing = await this.operations.queryTable(TABLE_MESSAGES, void 0);
|
|
308
|
+
const updated = [];
|
|
309
|
+
const affectedThreadIds = /* @__PURE__ */ new Set();
|
|
310
|
+
for (const update of messages) {
|
|
311
|
+
const current = existing.find((row) => row.id === update.id);
|
|
312
|
+
if (!current) continue;
|
|
313
|
+
affectedThreadIds.add(current.thread_id);
|
|
314
|
+
if (update.threadId) {
|
|
315
|
+
affectedThreadIds.add(update.threadId);
|
|
316
|
+
current.thread_id = update.threadId;
|
|
317
|
+
}
|
|
318
|
+
if (update.resourceId !== void 0) {
|
|
319
|
+
current.resourceId = update.resourceId ?? null;
|
|
320
|
+
}
|
|
321
|
+
if (update.role) {
|
|
322
|
+
current.role = update.role;
|
|
323
|
+
}
|
|
324
|
+
if (update.type) {
|
|
325
|
+
current.type = update.type;
|
|
326
|
+
}
|
|
327
|
+
if (update.content) {
|
|
328
|
+
const existingContent = safelyParseJSON(current.content) || {};
|
|
329
|
+
const mergedContent = {
|
|
330
|
+
...existingContent,
|
|
331
|
+
...update.content,
|
|
332
|
+
...existingContent.metadata && update.content.metadata ? { metadata: { ...existingContent.metadata, ...update.content.metadata } } : {}
|
|
333
|
+
};
|
|
334
|
+
current.content = JSON.stringify(mergedContent);
|
|
335
|
+
}
|
|
336
|
+
await this.operations.insert({
|
|
337
|
+
tableName: TABLE_MESSAGES,
|
|
338
|
+
record: current
|
|
339
|
+
});
|
|
340
|
+
updated.push(this.parseStoredMessage(current));
|
|
341
|
+
}
|
|
342
|
+
const now = /* @__PURE__ */ new Date();
|
|
343
|
+
for (const threadId of affectedThreadIds) {
|
|
344
|
+
const thread = await this.getThreadById({ threadId });
|
|
345
|
+
if (thread) {
|
|
346
|
+
await this.operations.insert({
|
|
347
|
+
tableName: TABLE_THREADS,
|
|
348
|
+
record: {
|
|
349
|
+
...thread,
|
|
350
|
+
id: thread.id,
|
|
351
|
+
updatedAt: now.toISOString(),
|
|
352
|
+
createdAt: thread.createdAt instanceof Date ? thread.createdAt.toISOString() : thread.createdAt,
|
|
353
|
+
metadata: thread.metadata ?? {}
|
|
354
|
+
}
|
|
355
|
+
});
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
return updated;
|
|
359
|
+
}
|
|
360
|
+
async deleteMessages(messageIds) {
|
|
361
|
+
await this.operations.deleteMany(TABLE_MESSAGES, messageIds);
|
|
362
|
+
}
|
|
363
|
+
async saveResource({ resource }) {
|
|
364
|
+
const record = {
|
|
365
|
+
...resource,
|
|
366
|
+
createdAt: resource.createdAt instanceof Date ? resource.createdAt.toISOString() : resource.createdAt,
|
|
367
|
+
updatedAt: resource.updatedAt instanceof Date ? resource.updatedAt.toISOString() : resource.updatedAt
|
|
368
|
+
};
|
|
369
|
+
if (resource.metadata !== void 0) {
|
|
370
|
+
record.metadata = resource.metadata;
|
|
371
|
+
}
|
|
372
|
+
await this.operations.insert({
|
|
373
|
+
tableName: TABLE_RESOURCES,
|
|
374
|
+
record
|
|
375
|
+
});
|
|
376
|
+
return resource;
|
|
377
|
+
}
|
|
378
|
+
async getResourceById({ resourceId }) {
|
|
379
|
+
const record = await this.operations.load({
|
|
380
|
+
tableName: TABLE_RESOURCES,
|
|
381
|
+
keys: { id: resourceId }
|
|
382
|
+
});
|
|
383
|
+
if (!record) return null;
|
|
384
|
+
return {
|
|
385
|
+
...record,
|
|
386
|
+
metadata: typeof record.metadata === "string" ? safelyParseJSON(record.metadata) : record.metadata,
|
|
387
|
+
createdAt: new Date(record.createdAt),
|
|
388
|
+
updatedAt: new Date(record.updatedAt)
|
|
389
|
+
};
|
|
390
|
+
}
|
|
391
|
+
async updateResource({
|
|
392
|
+
resourceId,
|
|
393
|
+
workingMemory,
|
|
394
|
+
metadata
|
|
395
|
+
}) {
|
|
396
|
+
const existing = await this.getResourceById({ resourceId });
|
|
397
|
+
const now = /* @__PURE__ */ new Date();
|
|
398
|
+
if (!existing) {
|
|
399
|
+
const created = {
|
|
400
|
+
id: resourceId,
|
|
401
|
+
workingMemory,
|
|
402
|
+
metadata: metadata ?? {},
|
|
403
|
+
createdAt: now,
|
|
404
|
+
updatedAt: now
|
|
405
|
+
};
|
|
406
|
+
return this.saveResource({ resource: created });
|
|
407
|
+
}
|
|
408
|
+
const updated = {
|
|
409
|
+
...existing,
|
|
410
|
+
workingMemory: workingMemory ?? existing.workingMemory,
|
|
411
|
+
metadata: {
|
|
412
|
+
...existing.metadata,
|
|
413
|
+
...metadata
|
|
414
|
+
},
|
|
415
|
+
updatedAt: now
|
|
416
|
+
};
|
|
417
|
+
await this.saveResource({ resource: updated });
|
|
418
|
+
return updated;
|
|
419
|
+
}
|
|
420
|
+
parseStoredMessage(message) {
|
|
421
|
+
const content = safelyParseJSON(message.content);
|
|
422
|
+
return {
|
|
423
|
+
id: message.id,
|
|
424
|
+
threadId: message.thread_id,
|
|
425
|
+
content,
|
|
426
|
+
role: message.role,
|
|
427
|
+
type: message.type,
|
|
428
|
+
createdAt: new Date(message.createdAt),
|
|
429
|
+
resourceId: message.resourceId ?? void 0
|
|
430
|
+
};
|
|
431
|
+
}
|
|
432
|
+
async addContextMessages({
|
|
433
|
+
includeItem,
|
|
434
|
+
allMessages,
|
|
435
|
+
targetThreadId,
|
|
436
|
+
messageIds,
|
|
437
|
+
messages
|
|
438
|
+
}) {
|
|
439
|
+
const ordered = allMessages.filter((row) => row.thread_id === targetThreadId).sort((a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime());
|
|
440
|
+
const targetIndex = ordered.findIndex((row) => row.id === includeItem.id);
|
|
441
|
+
if (targetIndex === -1) return;
|
|
442
|
+
if (includeItem.withPreviousMessages) {
|
|
443
|
+
const start = Math.max(0, targetIndex - includeItem.withPreviousMessages);
|
|
444
|
+
for (let i = start; i < targetIndex; i++) {
|
|
445
|
+
const row = ordered[i];
|
|
446
|
+
if (row && !messageIds.has(row.id)) {
|
|
447
|
+
messages.push(this.parseStoredMessage(row));
|
|
448
|
+
messageIds.add(row.id);
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
if (includeItem.withNextMessages) {
|
|
453
|
+
const end = Math.min(ordered.length, targetIndex + includeItem.withNextMessages + 1);
|
|
454
|
+
for (let i = targetIndex + 1; i < end; i++) {
|
|
455
|
+
const row = ordered[i];
|
|
456
|
+
if (row && !messageIds.has(row.id)) {
|
|
457
|
+
messages.push(this.parseStoredMessage(row));
|
|
458
|
+
messageIds.add(row.id);
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
};
|
|
464
|
+
var ScoresConvex = class extends ScoresStorage {
|
|
465
|
+
constructor(operations) {
|
|
466
|
+
super();
|
|
467
|
+
this.operations = operations;
|
|
468
|
+
}
|
|
469
|
+
async getScoreById({ id }) {
|
|
470
|
+
const row = await this.operations.load({
|
|
471
|
+
tableName: TABLE_SCORERS,
|
|
472
|
+
keys: { id }
|
|
473
|
+
});
|
|
474
|
+
return row ? this.deserialize(row) : null;
|
|
475
|
+
}
|
|
476
|
+
async saveScore(score) {
|
|
477
|
+
const now = /* @__PURE__ */ new Date();
|
|
478
|
+
const record = {
|
|
479
|
+
...score,
|
|
480
|
+
id: crypto.randomUUID(),
|
|
481
|
+
createdAt: now.toISOString(),
|
|
482
|
+
updatedAt: now.toISOString()
|
|
483
|
+
};
|
|
484
|
+
await this.operations.insert({
|
|
485
|
+
tableName: TABLE_SCORERS,
|
|
486
|
+
record
|
|
487
|
+
});
|
|
488
|
+
return { score: this.deserialize(record) };
|
|
489
|
+
}
|
|
490
|
+
async listScoresByScorerId({
|
|
491
|
+
scorerId,
|
|
492
|
+
pagination,
|
|
493
|
+
entityId,
|
|
494
|
+
entityType,
|
|
495
|
+
source
|
|
496
|
+
}) {
|
|
497
|
+
return this.listScores({
|
|
498
|
+
filters: { scorerId, entityId, entityType, source },
|
|
499
|
+
pagination
|
|
500
|
+
});
|
|
501
|
+
}
|
|
502
|
+
async listScoresByRunId({
|
|
503
|
+
runId,
|
|
504
|
+
pagination
|
|
505
|
+
}) {
|
|
506
|
+
return this.listScores({
|
|
507
|
+
filters: { runId },
|
|
508
|
+
pagination
|
|
509
|
+
});
|
|
510
|
+
}
|
|
511
|
+
async listScoresByEntityId({
|
|
512
|
+
entityId,
|
|
513
|
+
entityType,
|
|
514
|
+
pagination
|
|
515
|
+
}) {
|
|
516
|
+
return this.listScores({
|
|
517
|
+
filters: { entityId, entityType },
|
|
518
|
+
pagination
|
|
519
|
+
});
|
|
520
|
+
}
|
|
521
|
+
async listScores({
|
|
522
|
+
filters,
|
|
523
|
+
pagination
|
|
524
|
+
}) {
|
|
525
|
+
if (pagination.page < 0) {
|
|
526
|
+
throw new MastraError(
|
|
527
|
+
{
|
|
528
|
+
id: "CONVEX_STORAGE_INVALID_PAGINATION",
|
|
529
|
+
domain: ErrorDomain.STORAGE,
|
|
530
|
+
category: ErrorCategory.USER
|
|
531
|
+
},
|
|
532
|
+
new Error("page must be >= 0")
|
|
533
|
+
);
|
|
534
|
+
}
|
|
535
|
+
const rows = await this.operations.queryTable(TABLE_SCORERS, void 0);
|
|
536
|
+
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());
|
|
537
|
+
const { perPage, page } = pagination;
|
|
538
|
+
const perPageValue = perPage === false ? filtered.length : perPage;
|
|
539
|
+
const start = perPage === false ? 0 : page * perPageValue;
|
|
540
|
+
const end = perPage === false ? filtered.length : start + perPageValue;
|
|
541
|
+
const slice = filtered.slice(start, end).map((row) => this.deserialize(row));
|
|
542
|
+
return {
|
|
543
|
+
pagination: {
|
|
544
|
+
total: filtered.length,
|
|
545
|
+
page,
|
|
546
|
+
perPage,
|
|
547
|
+
hasMore: perPage === false ? false : end < filtered.length
|
|
548
|
+
},
|
|
549
|
+
scores: slice
|
|
550
|
+
};
|
|
551
|
+
}
|
|
552
|
+
deserialize(row) {
|
|
553
|
+
return {
|
|
554
|
+
...row,
|
|
555
|
+
createdAt: new Date(row.createdAt),
|
|
556
|
+
updatedAt: new Date(row.updatedAt)
|
|
557
|
+
};
|
|
558
|
+
}
|
|
559
|
+
};
|
|
560
|
+
var WorkflowsConvex = class extends WorkflowsStorage {
|
|
561
|
+
constructor(operations) {
|
|
562
|
+
super();
|
|
563
|
+
this.operations = operations;
|
|
564
|
+
}
|
|
565
|
+
async updateWorkflowResults({
|
|
566
|
+
workflowName,
|
|
567
|
+
runId,
|
|
568
|
+
stepId,
|
|
569
|
+
result,
|
|
570
|
+
requestContext
|
|
571
|
+
}) {
|
|
572
|
+
const run = await this.getRun(workflowName, runId);
|
|
573
|
+
if (!run) return {};
|
|
574
|
+
const snapshot = this.ensureSnapshot(run);
|
|
575
|
+
snapshot.context = snapshot.context || {};
|
|
576
|
+
snapshot.context[stepId] = result;
|
|
577
|
+
snapshot.requestContext = { ...snapshot.requestContext || {}, ...requestContext };
|
|
578
|
+
await this.persistWorkflowSnapshot({
|
|
579
|
+
workflowName,
|
|
580
|
+
runId,
|
|
581
|
+
resourceId: run.resourceId,
|
|
582
|
+
snapshot
|
|
583
|
+
});
|
|
584
|
+
return JSON.parse(JSON.stringify(snapshot.context));
|
|
585
|
+
}
|
|
586
|
+
async updateWorkflowState({
|
|
587
|
+
workflowName,
|
|
588
|
+
runId,
|
|
589
|
+
opts
|
|
590
|
+
}) {
|
|
591
|
+
const run = await this.getRun(workflowName, runId);
|
|
592
|
+
if (!run) return void 0;
|
|
593
|
+
const snapshot = this.ensureSnapshot(run);
|
|
594
|
+
const updated = { ...snapshot, ...opts };
|
|
595
|
+
await this.persistWorkflowSnapshot({
|
|
596
|
+
workflowName,
|
|
597
|
+
runId,
|
|
598
|
+
resourceId: run.resourceId,
|
|
599
|
+
snapshot: updated
|
|
600
|
+
});
|
|
601
|
+
return updated;
|
|
602
|
+
}
|
|
603
|
+
async persistWorkflowSnapshot({
|
|
604
|
+
workflowName,
|
|
605
|
+
runId,
|
|
606
|
+
resourceId,
|
|
607
|
+
snapshot
|
|
608
|
+
}) {
|
|
609
|
+
const now = /* @__PURE__ */ new Date();
|
|
610
|
+
const existing = await this.operations.load({
|
|
611
|
+
tableName: TABLE_WORKFLOW_SNAPSHOT,
|
|
612
|
+
keys: { workflow_name: workflowName, run_id: runId }
|
|
613
|
+
});
|
|
614
|
+
await this.operations.insert({
|
|
615
|
+
tableName: TABLE_WORKFLOW_SNAPSHOT,
|
|
616
|
+
record: {
|
|
617
|
+
workflow_name: workflowName,
|
|
618
|
+
run_id: runId,
|
|
619
|
+
resourceId,
|
|
620
|
+
snapshot,
|
|
621
|
+
createdAt: existing?.createdAt ?? now.toISOString(),
|
|
622
|
+
updatedAt: now.toISOString()
|
|
623
|
+
}
|
|
624
|
+
});
|
|
625
|
+
}
|
|
626
|
+
async loadWorkflowSnapshot({
|
|
627
|
+
workflowName,
|
|
628
|
+
runId
|
|
629
|
+
}) {
|
|
630
|
+
const row = await this.operations.load({
|
|
631
|
+
tableName: TABLE_WORKFLOW_SNAPSHOT,
|
|
632
|
+
keys: { workflow_name: workflowName, run_id: runId }
|
|
633
|
+
});
|
|
634
|
+
if (!row) return null;
|
|
635
|
+
return typeof row.snapshot === "string" ? JSON.parse(row.snapshot) : JSON.parse(JSON.stringify(row.snapshot));
|
|
636
|
+
}
|
|
637
|
+
async listWorkflowRuns(args = {}) {
|
|
638
|
+
const { workflowName, fromDate, toDate, perPage, page, resourceId, status } = args;
|
|
639
|
+
let rows = await this.operations.queryTable(TABLE_WORKFLOW_SNAPSHOT, void 0);
|
|
640
|
+
if (workflowName) rows = rows.filter((run) => run.workflow_name === workflowName);
|
|
641
|
+
if (resourceId) rows = rows.filter((run) => run.resourceId === resourceId);
|
|
642
|
+
if (fromDate) rows = rows.filter((run) => new Date(run.createdAt).getTime() >= fromDate.getTime());
|
|
643
|
+
if (toDate) rows = rows.filter((run) => new Date(run.createdAt).getTime() <= toDate.getTime());
|
|
644
|
+
if (status) {
|
|
645
|
+
rows = rows.filter((run) => {
|
|
646
|
+
const snapshot = this.ensureSnapshot(run);
|
|
647
|
+
return snapshot.status === status;
|
|
648
|
+
});
|
|
649
|
+
}
|
|
650
|
+
const total = rows.length;
|
|
651
|
+
rows.sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime());
|
|
652
|
+
if (perPage !== void 0 && page !== void 0) {
|
|
653
|
+
const normalized = normalizePerPage(perPage, Number.MAX_SAFE_INTEGER);
|
|
654
|
+
const offset = page * normalized;
|
|
655
|
+
rows = rows.slice(offset, offset + normalized);
|
|
656
|
+
}
|
|
657
|
+
const runs = rows.map((run) => ({
|
|
658
|
+
workflowName: run.workflow_name,
|
|
659
|
+
runId: run.run_id,
|
|
660
|
+
snapshot: this.ensureSnapshot(run),
|
|
661
|
+
createdAt: new Date(run.createdAt),
|
|
662
|
+
updatedAt: new Date(run.updatedAt),
|
|
663
|
+
resourceId: run.resourceId
|
|
664
|
+
}));
|
|
665
|
+
return { runs, total };
|
|
666
|
+
}
|
|
667
|
+
async getWorkflowRunById({
|
|
668
|
+
runId,
|
|
669
|
+
workflowName
|
|
670
|
+
}) {
|
|
671
|
+
const runs = await this.operations.queryTable(TABLE_WORKFLOW_SNAPSHOT, void 0);
|
|
672
|
+
const match = runs.find((run) => run.run_id === runId && (!workflowName || run.workflow_name === workflowName));
|
|
673
|
+
if (!match) return null;
|
|
674
|
+
return {
|
|
675
|
+
workflowName: match.workflow_name,
|
|
676
|
+
runId: match.run_id,
|
|
677
|
+
snapshot: this.ensureSnapshot(match),
|
|
678
|
+
createdAt: new Date(match.createdAt),
|
|
679
|
+
updatedAt: new Date(match.updatedAt),
|
|
680
|
+
resourceId: match.resourceId
|
|
681
|
+
};
|
|
682
|
+
}
|
|
683
|
+
async getRun(workflowName, runId) {
|
|
684
|
+
const runs = await this.operations.queryTable(TABLE_WORKFLOW_SNAPSHOT, [
|
|
685
|
+
{ field: "workflow_name", value: workflowName }
|
|
686
|
+
]);
|
|
687
|
+
return runs.find((run) => run.run_id === runId) ?? null;
|
|
688
|
+
}
|
|
689
|
+
ensureSnapshot(run) {
|
|
690
|
+
if (!run.snapshot) {
|
|
691
|
+
return {
|
|
692
|
+
context: {},
|
|
693
|
+
activePaths: [],
|
|
694
|
+
activeStepsPath: {},
|
|
695
|
+
timestamp: Date.now(),
|
|
696
|
+
suspendedPaths: {},
|
|
697
|
+
resumeLabels: {},
|
|
698
|
+
serializedStepGraph: [],
|
|
699
|
+
value: {},
|
|
700
|
+
waitingPaths: {},
|
|
701
|
+
status: "pending",
|
|
702
|
+
runId: ""
|
|
703
|
+
};
|
|
704
|
+
}
|
|
705
|
+
if (typeof run.snapshot === "string") {
|
|
706
|
+
return JSON.parse(run.snapshot);
|
|
707
|
+
}
|
|
708
|
+
return JSON.parse(JSON.stringify(run.snapshot));
|
|
709
|
+
}
|
|
710
|
+
};
|
|
711
|
+
var StoreOperationsConvex = class extends StoreOperations {
|
|
712
|
+
constructor(client) {
|
|
713
|
+
super();
|
|
714
|
+
this.client = client;
|
|
715
|
+
}
|
|
716
|
+
async hasColumn(_table, _column) {
|
|
717
|
+
return true;
|
|
718
|
+
}
|
|
719
|
+
async createTable(_args) {
|
|
720
|
+
}
|
|
721
|
+
async clearTable({ tableName }) {
|
|
722
|
+
let hasMore = true;
|
|
723
|
+
while (hasMore) {
|
|
724
|
+
const response = await this.client.callStorageRaw({
|
|
725
|
+
op: "clearTable",
|
|
726
|
+
tableName
|
|
727
|
+
});
|
|
728
|
+
hasMore = response.hasMore ?? false;
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
async dropTable({ tableName }) {
|
|
732
|
+
let hasMore = true;
|
|
733
|
+
while (hasMore) {
|
|
734
|
+
const response = await this.client.callStorageRaw({
|
|
735
|
+
op: "dropTable",
|
|
736
|
+
tableName
|
|
737
|
+
});
|
|
738
|
+
hasMore = response.hasMore ?? false;
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
async alterTable(_args) {
|
|
742
|
+
}
|
|
743
|
+
async insert({ tableName, record }) {
|
|
744
|
+
await this.client.callStorage({
|
|
745
|
+
op: "insert",
|
|
746
|
+
tableName,
|
|
747
|
+
record: this.normalizeRecord(tableName, record)
|
|
748
|
+
});
|
|
749
|
+
}
|
|
750
|
+
async batchInsert({ tableName, records }) {
|
|
751
|
+
if (records.length === 0) return;
|
|
752
|
+
await this.client.callStorage({
|
|
753
|
+
op: "batchInsert",
|
|
754
|
+
tableName,
|
|
755
|
+
records: records.map((record) => this.normalizeRecord(tableName, record))
|
|
756
|
+
});
|
|
757
|
+
}
|
|
758
|
+
async load({ tableName, keys }) {
|
|
759
|
+
const result = await this.client.callStorage({
|
|
760
|
+
op: "load",
|
|
761
|
+
tableName,
|
|
762
|
+
keys
|
|
763
|
+
});
|
|
764
|
+
return result;
|
|
765
|
+
}
|
|
766
|
+
async queryTable(tableName, filters) {
|
|
767
|
+
return this.client.callStorage({
|
|
768
|
+
op: "queryTable",
|
|
769
|
+
tableName,
|
|
770
|
+
filters
|
|
771
|
+
});
|
|
772
|
+
}
|
|
773
|
+
async deleteMany(tableName, ids) {
|
|
774
|
+
if (ids.length === 0) return;
|
|
775
|
+
await this.client.callStorage({
|
|
776
|
+
op: "deleteMany",
|
|
777
|
+
tableName,
|
|
778
|
+
ids
|
|
779
|
+
});
|
|
780
|
+
}
|
|
781
|
+
normalizeRecord(tableName, record) {
|
|
782
|
+
const normalized = { ...record };
|
|
783
|
+
if (tableName === TABLE_WORKFLOW_SNAPSHOT && !normalized.id) {
|
|
784
|
+
const runId = normalized.run_id || normalized.runId;
|
|
785
|
+
const workflowName = normalized.workflow_name || normalized.workflowName;
|
|
786
|
+
normalized.id = workflowName ? `${workflowName}-${runId}` : runId;
|
|
787
|
+
}
|
|
788
|
+
if (!normalized.id) {
|
|
789
|
+
normalized.id = crypto.randomUUID();
|
|
790
|
+
}
|
|
791
|
+
for (const [key, value] of Object.entries(normalized)) {
|
|
792
|
+
if (value instanceof Date) {
|
|
793
|
+
normalized[key] = value.toISOString();
|
|
794
|
+
}
|
|
795
|
+
}
|
|
796
|
+
return normalized;
|
|
797
|
+
}
|
|
798
|
+
};
|
|
799
|
+
|
|
800
|
+
// src/storage/index.ts
|
|
801
|
+
var ConvexStore = class extends MastraStorage {
|
|
802
|
+
operations;
|
|
803
|
+
memory;
|
|
804
|
+
workflows;
|
|
805
|
+
scores;
|
|
806
|
+
constructor(config) {
|
|
807
|
+
super({ id: config.id, name: config.name ?? "ConvexStore" });
|
|
808
|
+
const client = new ConvexAdminClient(config);
|
|
809
|
+
this.operations = new StoreOperationsConvex(client);
|
|
810
|
+
this.memory = new MemoryConvex(this.operations);
|
|
811
|
+
this.workflows = new WorkflowsConvex(this.operations);
|
|
812
|
+
this.scores = new ScoresConvex(this.operations);
|
|
813
|
+
this.stores = {
|
|
814
|
+
operations: this.operations,
|
|
815
|
+
memory: this.memory,
|
|
816
|
+
workflows: this.workflows,
|
|
817
|
+
scores: this.scores
|
|
818
|
+
};
|
|
819
|
+
}
|
|
820
|
+
get supports() {
|
|
821
|
+
return {
|
|
822
|
+
selectByIncludeResourceScope: true,
|
|
823
|
+
resourceWorkingMemory: true,
|
|
824
|
+
hasColumn: false,
|
|
825
|
+
createTable: false,
|
|
826
|
+
deleteMessages: true,
|
|
827
|
+
observabilityInstance: false,
|
|
828
|
+
listScoresBySpan: false
|
|
829
|
+
};
|
|
830
|
+
}
|
|
831
|
+
async createTable(_args) {
|
|
832
|
+
}
|
|
833
|
+
async clearTable({ tableName }) {
|
|
834
|
+
await this.operations.clearTable({ tableName });
|
|
835
|
+
}
|
|
836
|
+
async dropTable({ tableName }) {
|
|
837
|
+
await this.operations.dropTable({ tableName });
|
|
838
|
+
}
|
|
839
|
+
async alterTable(_args) {
|
|
840
|
+
}
|
|
841
|
+
async insert({ tableName, record }) {
|
|
842
|
+
await this.operations.insert({ tableName, record });
|
|
843
|
+
}
|
|
844
|
+
async batchInsert({ tableName, records }) {
|
|
845
|
+
await this.operations.batchInsert({ tableName, records });
|
|
846
|
+
}
|
|
847
|
+
async load({ tableName, keys }) {
|
|
848
|
+
return this.operations.load({ tableName, keys });
|
|
849
|
+
}
|
|
850
|
+
async getThreadById({ threadId }) {
|
|
851
|
+
return this.memory.getThreadById({ threadId });
|
|
852
|
+
}
|
|
853
|
+
async saveThread({ thread }) {
|
|
854
|
+
return this.memory.saveThread({ thread });
|
|
855
|
+
}
|
|
856
|
+
async updateThread({
|
|
857
|
+
id,
|
|
858
|
+
title,
|
|
859
|
+
metadata
|
|
860
|
+
}) {
|
|
861
|
+
return this.memory.updateThread({ id, title, metadata });
|
|
862
|
+
}
|
|
863
|
+
async deleteThread({ threadId }) {
|
|
864
|
+
await this.memory.deleteThread({ threadId });
|
|
865
|
+
}
|
|
866
|
+
async listMessages(args) {
|
|
867
|
+
return this.memory.listMessages(args);
|
|
868
|
+
}
|
|
869
|
+
async listMessagesById({ messageIds }) {
|
|
870
|
+
return this.memory.listMessagesById({ messageIds });
|
|
871
|
+
}
|
|
872
|
+
async saveMessages(args) {
|
|
873
|
+
return this.memory.saveMessages(args);
|
|
874
|
+
}
|
|
875
|
+
async updateMessages({
|
|
876
|
+
messages
|
|
877
|
+
}) {
|
|
878
|
+
return this.memory.updateMessages({ messages });
|
|
879
|
+
}
|
|
880
|
+
async deleteMessages(messageIds) {
|
|
881
|
+
await this.memory.deleteMessages(messageIds);
|
|
882
|
+
}
|
|
883
|
+
async listThreadsByResourceId(args) {
|
|
884
|
+
return this.memory.listThreadsByResourceId(args);
|
|
885
|
+
}
|
|
886
|
+
async getResourceById({ resourceId }) {
|
|
887
|
+
return this.memory.getResourceById({ resourceId });
|
|
888
|
+
}
|
|
889
|
+
async saveResource({ resource }) {
|
|
890
|
+
return this.memory.saveResource({ resource });
|
|
891
|
+
}
|
|
892
|
+
async updateResource({
|
|
893
|
+
resourceId,
|
|
894
|
+
workingMemory,
|
|
895
|
+
metadata
|
|
896
|
+
}) {
|
|
897
|
+
return this.memory.updateResource({ resourceId, workingMemory, metadata });
|
|
898
|
+
}
|
|
899
|
+
async updateWorkflowResults(params) {
|
|
900
|
+
return this.workflows.updateWorkflowResults(params);
|
|
901
|
+
}
|
|
902
|
+
async updateWorkflowState(params) {
|
|
903
|
+
return this.workflows.updateWorkflowState(params);
|
|
904
|
+
}
|
|
905
|
+
async persistWorkflowSnapshot({
|
|
906
|
+
workflowName,
|
|
907
|
+
runId,
|
|
908
|
+
resourceId,
|
|
909
|
+
snapshot
|
|
910
|
+
}) {
|
|
911
|
+
await this.workflows.persistWorkflowSnapshot({ workflowName, runId, resourceId, snapshot });
|
|
912
|
+
}
|
|
913
|
+
async loadWorkflowSnapshot({
|
|
914
|
+
workflowName,
|
|
915
|
+
runId
|
|
916
|
+
}) {
|
|
917
|
+
return this.workflows.loadWorkflowSnapshot({ workflowName, runId });
|
|
918
|
+
}
|
|
919
|
+
async listWorkflowRuns(args) {
|
|
920
|
+
return this.workflows.listWorkflowRuns(args);
|
|
921
|
+
}
|
|
922
|
+
async getWorkflowRunById({
|
|
923
|
+
runId,
|
|
924
|
+
workflowName
|
|
925
|
+
}) {
|
|
926
|
+
return this.workflows.getWorkflowRunById({ runId, workflowName });
|
|
927
|
+
}
|
|
928
|
+
async getScoreById({ id }) {
|
|
929
|
+
return this.scores.getScoreById({ id });
|
|
930
|
+
}
|
|
931
|
+
async saveScore(score) {
|
|
932
|
+
return this.scores.saveScore(score);
|
|
933
|
+
}
|
|
934
|
+
async listScoresByScorerId({
|
|
935
|
+
scorerId,
|
|
936
|
+
pagination,
|
|
937
|
+
entityId,
|
|
938
|
+
entityType,
|
|
939
|
+
source
|
|
940
|
+
}) {
|
|
941
|
+
return this.scores.listScoresByScorerId({ scorerId, pagination, entityId, entityType, source });
|
|
942
|
+
}
|
|
943
|
+
async listScoresByRunId({
|
|
944
|
+
runId,
|
|
945
|
+
pagination
|
|
946
|
+
}) {
|
|
947
|
+
return this.scores.listScoresByRunId({ runId, pagination });
|
|
948
|
+
}
|
|
949
|
+
async listScoresByEntityId({
|
|
950
|
+
entityId,
|
|
951
|
+
entityType,
|
|
952
|
+
pagination
|
|
953
|
+
}) {
|
|
954
|
+
return this.scores.listScoresByEntityId({ entityId, entityType, pagination });
|
|
955
|
+
}
|
|
956
|
+
};
|
|
957
|
+
var INDEX_METADATA_TABLE = "mastra_vector_indexes";
|
|
958
|
+
var ConvexVector = class extends MastraVector {
|
|
959
|
+
client;
|
|
960
|
+
constructor(config) {
|
|
961
|
+
super({ id: config.id });
|
|
962
|
+
this.client = new ConvexAdminClient(config);
|
|
963
|
+
}
|
|
964
|
+
async createIndex({ indexName, dimension }) {
|
|
965
|
+
await this.callStorage({
|
|
966
|
+
op: "insert",
|
|
967
|
+
tableName: INDEX_METADATA_TABLE,
|
|
968
|
+
record: {
|
|
969
|
+
id: indexName,
|
|
970
|
+
indexName,
|
|
971
|
+
dimension,
|
|
972
|
+
metric: "cosine",
|
|
973
|
+
createdAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
974
|
+
}
|
|
975
|
+
});
|
|
976
|
+
}
|
|
977
|
+
async deleteIndex({ indexName }) {
|
|
978
|
+
await this.callStorage({
|
|
979
|
+
op: "deleteMany",
|
|
980
|
+
tableName: INDEX_METADATA_TABLE,
|
|
981
|
+
ids: [indexName]
|
|
982
|
+
});
|
|
983
|
+
await this.callStorageUntilComplete({
|
|
984
|
+
op: "clearTable",
|
|
985
|
+
tableName: this.vectorTable(indexName)
|
|
986
|
+
});
|
|
987
|
+
}
|
|
988
|
+
async truncateIndex({ indexName }) {
|
|
989
|
+
await this.callStorageUntilComplete({
|
|
990
|
+
op: "clearTable",
|
|
991
|
+
tableName: this.vectorTable(indexName)
|
|
992
|
+
});
|
|
993
|
+
}
|
|
994
|
+
async listIndexes() {
|
|
995
|
+
const indexes = await this.callStorage({
|
|
996
|
+
op: "queryTable",
|
|
997
|
+
tableName: INDEX_METADATA_TABLE
|
|
998
|
+
});
|
|
999
|
+
return indexes.map((index) => index.id);
|
|
1000
|
+
}
|
|
1001
|
+
async describeIndex({ indexName }) {
|
|
1002
|
+
const index = await this.callStorage({
|
|
1003
|
+
op: "load",
|
|
1004
|
+
tableName: INDEX_METADATA_TABLE,
|
|
1005
|
+
keys: { id: indexName }
|
|
1006
|
+
});
|
|
1007
|
+
if (!index) {
|
|
1008
|
+
throw new Error(`Index ${indexName} not found`);
|
|
1009
|
+
}
|
|
1010
|
+
const vectors = await this.callStorage({
|
|
1011
|
+
op: "queryTable",
|
|
1012
|
+
tableName: this.vectorTable(indexName)
|
|
1013
|
+
});
|
|
1014
|
+
return {
|
|
1015
|
+
dimension: index.dimension,
|
|
1016
|
+
count: vectors.length,
|
|
1017
|
+
metric: "cosine"
|
|
1018
|
+
};
|
|
1019
|
+
}
|
|
1020
|
+
async upsert({ indexName, vectors, ids, metadata }) {
|
|
1021
|
+
const vectorIds = ids ?? vectors.map(() => crypto.randomUUID());
|
|
1022
|
+
const records = vectors.map((vector, i) => ({
|
|
1023
|
+
id: vectorIds[i],
|
|
1024
|
+
embedding: vector,
|
|
1025
|
+
metadata: metadata?.[i]
|
|
1026
|
+
}));
|
|
1027
|
+
await this.callStorage({
|
|
1028
|
+
op: "batchInsert",
|
|
1029
|
+
tableName: this.vectorTable(indexName),
|
|
1030
|
+
records
|
|
1031
|
+
});
|
|
1032
|
+
return vectorIds;
|
|
1033
|
+
}
|
|
1034
|
+
async query({
|
|
1035
|
+
indexName,
|
|
1036
|
+
queryVector,
|
|
1037
|
+
topK = 10,
|
|
1038
|
+
includeVector = false,
|
|
1039
|
+
filter
|
|
1040
|
+
}) {
|
|
1041
|
+
const vectors = await this.callStorage({
|
|
1042
|
+
op: "queryTable",
|
|
1043
|
+
tableName: this.vectorTable(indexName)
|
|
1044
|
+
});
|
|
1045
|
+
const filtered = filter && !this.isEmptyFilter(filter) ? vectors.filter((record) => this.matchesFilter(record.metadata, filter)) : vectors;
|
|
1046
|
+
const scored = filtered.map((record) => ({
|
|
1047
|
+
id: record.id,
|
|
1048
|
+
score: cosineSimilarity(queryVector, record.embedding),
|
|
1049
|
+
metadata: record.metadata,
|
|
1050
|
+
...includeVector ? { vector: record.embedding } : {}
|
|
1051
|
+
})).filter((result) => Number.isFinite(result.score)).sort((a, b) => b.score - a.score).slice(0, topK);
|
|
1052
|
+
return scored;
|
|
1053
|
+
}
|
|
1054
|
+
async updateVector(params) {
|
|
1055
|
+
const hasId = "id" in params && params.id;
|
|
1056
|
+
const hasFilter = "filter" in params && params.filter !== void 0;
|
|
1057
|
+
if (hasId && hasFilter) {
|
|
1058
|
+
throw new Error("ConvexVector.updateVector: id and filter are mutually exclusive");
|
|
1059
|
+
}
|
|
1060
|
+
if (hasFilter) {
|
|
1061
|
+
const filter = params.filter;
|
|
1062
|
+
if (this.isEmptyFilter(filter)) {
|
|
1063
|
+
throw new Error("ConvexVector.updateVector: cannot update with empty filter");
|
|
1064
|
+
}
|
|
1065
|
+
const vectors = await this.callStorage({
|
|
1066
|
+
op: "queryTable",
|
|
1067
|
+
tableName: this.vectorTable(params.indexName)
|
|
1068
|
+
});
|
|
1069
|
+
const matching = vectors.filter((record) => this.matchesFilter(record.metadata, filter));
|
|
1070
|
+
for (const existing2 of matching) {
|
|
1071
|
+
const updated2 = {
|
|
1072
|
+
...existing2,
|
|
1073
|
+
...params.update.vector ? { embedding: params.update.vector } : {},
|
|
1074
|
+
...params.update.metadata ? { metadata: { ...existing2.metadata, ...params.update.metadata } } : {}
|
|
1075
|
+
};
|
|
1076
|
+
await this.callStorage({
|
|
1077
|
+
op: "insert",
|
|
1078
|
+
tableName: this.vectorTable(params.indexName),
|
|
1079
|
+
record: updated2
|
|
1080
|
+
});
|
|
1081
|
+
}
|
|
1082
|
+
return;
|
|
1083
|
+
}
|
|
1084
|
+
if (!hasId) {
|
|
1085
|
+
throw new Error("ConvexVector.updateVector: Either id or filter must be provided");
|
|
1086
|
+
}
|
|
1087
|
+
const existing = await this.callStorage({
|
|
1088
|
+
op: "load",
|
|
1089
|
+
tableName: this.vectorTable(params.indexName),
|
|
1090
|
+
keys: { id: params.id }
|
|
1091
|
+
});
|
|
1092
|
+
if (!existing) return;
|
|
1093
|
+
const updated = {
|
|
1094
|
+
...existing,
|
|
1095
|
+
...params.update.vector ? { embedding: params.update.vector } : {},
|
|
1096
|
+
...params.update.metadata ? { metadata: { ...existing.metadata, ...params.update.metadata } } : {}
|
|
1097
|
+
};
|
|
1098
|
+
await this.callStorage({
|
|
1099
|
+
op: "insert",
|
|
1100
|
+
tableName: this.vectorTable(params.indexName),
|
|
1101
|
+
record: updated
|
|
1102
|
+
});
|
|
1103
|
+
}
|
|
1104
|
+
async deleteVector({ indexName, id }) {
|
|
1105
|
+
await this.callStorage({
|
|
1106
|
+
op: "deleteMany",
|
|
1107
|
+
tableName: this.vectorTable(indexName),
|
|
1108
|
+
ids: [id]
|
|
1109
|
+
});
|
|
1110
|
+
}
|
|
1111
|
+
async deleteVectors(params) {
|
|
1112
|
+
const { indexName } = params;
|
|
1113
|
+
const hasIds = "ids" in params && params.ids !== void 0;
|
|
1114
|
+
const hasFilter = "filter" in params && params.filter !== void 0;
|
|
1115
|
+
if (hasIds && hasFilter) {
|
|
1116
|
+
throw new Error("ConvexVector.deleteVectors: ids and filter are mutually exclusive");
|
|
1117
|
+
}
|
|
1118
|
+
if (!hasIds && !hasFilter) {
|
|
1119
|
+
throw new Error("ConvexVector.deleteVectors: Either filter or ids must be provided");
|
|
1120
|
+
}
|
|
1121
|
+
if (hasIds) {
|
|
1122
|
+
const ids = params.ids;
|
|
1123
|
+
if (ids.length === 0) {
|
|
1124
|
+
throw new Error("ConvexVector.deleteVectors: cannot delete with empty ids array");
|
|
1125
|
+
}
|
|
1126
|
+
await this.callStorage({
|
|
1127
|
+
op: "deleteMany",
|
|
1128
|
+
tableName: this.vectorTable(indexName),
|
|
1129
|
+
ids
|
|
1130
|
+
});
|
|
1131
|
+
return;
|
|
1132
|
+
}
|
|
1133
|
+
const filter = params.filter;
|
|
1134
|
+
if (this.isEmptyFilter(filter)) {
|
|
1135
|
+
throw new Error("ConvexVector.deleteVectors: cannot delete with empty filter");
|
|
1136
|
+
}
|
|
1137
|
+
const vectors = await this.callStorage({
|
|
1138
|
+
op: "queryTable",
|
|
1139
|
+
tableName: this.vectorTable(indexName)
|
|
1140
|
+
});
|
|
1141
|
+
const matchingIds = vectors.filter((record) => this.matchesFilter(record.metadata, filter)).map((record) => record.id);
|
|
1142
|
+
if (matchingIds.length > 0) {
|
|
1143
|
+
await this.callStorage({
|
|
1144
|
+
op: "deleteMany",
|
|
1145
|
+
tableName: this.vectorTable(indexName),
|
|
1146
|
+
ids: matchingIds
|
|
1147
|
+
});
|
|
1148
|
+
}
|
|
1149
|
+
}
|
|
1150
|
+
vectorTable(indexName) {
|
|
1151
|
+
return `mastra_vector_${indexName}`;
|
|
1152
|
+
}
|
|
1153
|
+
isEmptyFilter(filter) {
|
|
1154
|
+
if (!filter) return true;
|
|
1155
|
+
return Object.keys(filter).length === 0;
|
|
1156
|
+
}
|
|
1157
|
+
matchesFilter(recordMetadata, filter) {
|
|
1158
|
+
if (!recordMetadata) return false;
|
|
1159
|
+
if (!filter || Object.keys(filter).length === 0) return true;
|
|
1160
|
+
if ("metadata" in filter && filter.metadata) {
|
|
1161
|
+
return this.matchesFilterConditions(recordMetadata, filter.metadata);
|
|
1162
|
+
}
|
|
1163
|
+
return this.matchesFilterConditions(recordMetadata, filter);
|
|
1164
|
+
}
|
|
1165
|
+
matchesFilterConditions(recordMetadata, conditions) {
|
|
1166
|
+
for (const [key, value] of Object.entries(conditions)) {
|
|
1167
|
+
if (key === "$and" && Array.isArray(value)) {
|
|
1168
|
+
const allMatch = value.every((cond) => this.matchesFilterConditions(recordMetadata, cond));
|
|
1169
|
+
if (!allMatch) return false;
|
|
1170
|
+
continue;
|
|
1171
|
+
}
|
|
1172
|
+
if (key === "$or" && Array.isArray(value)) {
|
|
1173
|
+
const anyMatch = value.some((cond) => this.matchesFilterConditions(recordMetadata, cond));
|
|
1174
|
+
if (!anyMatch) return false;
|
|
1175
|
+
continue;
|
|
1176
|
+
}
|
|
1177
|
+
if (typeof value === "object" && value !== null && "$in" in value) {
|
|
1178
|
+
if (!Array.isArray(value.$in) || !value.$in.includes(recordMetadata[key])) {
|
|
1179
|
+
return false;
|
|
1180
|
+
}
|
|
1181
|
+
continue;
|
|
1182
|
+
}
|
|
1183
|
+
if (typeof value === "object" && value !== null && "$nin" in value) {
|
|
1184
|
+
if (Array.isArray(value.$nin) && value.$nin.includes(recordMetadata[key])) {
|
|
1185
|
+
return false;
|
|
1186
|
+
}
|
|
1187
|
+
continue;
|
|
1188
|
+
}
|
|
1189
|
+
if (typeof value === "object" && value !== null && "$gt" in value) {
|
|
1190
|
+
if (!(recordMetadata[key] > value.$gt)) {
|
|
1191
|
+
return false;
|
|
1192
|
+
}
|
|
1193
|
+
continue;
|
|
1194
|
+
}
|
|
1195
|
+
if (typeof value === "object" && value !== null && "$gte" in value) {
|
|
1196
|
+
if (!(recordMetadata[key] >= value.$gte)) {
|
|
1197
|
+
return false;
|
|
1198
|
+
}
|
|
1199
|
+
continue;
|
|
1200
|
+
}
|
|
1201
|
+
if (typeof value === "object" && value !== null && "$lt" in value) {
|
|
1202
|
+
if (!(recordMetadata[key] < value.$lt)) {
|
|
1203
|
+
return false;
|
|
1204
|
+
}
|
|
1205
|
+
continue;
|
|
1206
|
+
}
|
|
1207
|
+
if (typeof value === "object" && value !== null && "$lte" in value) {
|
|
1208
|
+
if (!(recordMetadata[key] <= value.$lte)) {
|
|
1209
|
+
return false;
|
|
1210
|
+
}
|
|
1211
|
+
continue;
|
|
1212
|
+
}
|
|
1213
|
+
if (typeof value === "object" && value !== null && "$ne" in value) {
|
|
1214
|
+
if (recordMetadata[key] === value.$ne) {
|
|
1215
|
+
return false;
|
|
1216
|
+
}
|
|
1217
|
+
continue;
|
|
1218
|
+
}
|
|
1219
|
+
if (recordMetadata[key] !== value) {
|
|
1220
|
+
return false;
|
|
1221
|
+
}
|
|
1222
|
+
}
|
|
1223
|
+
return true;
|
|
1224
|
+
}
|
|
1225
|
+
async callStorage(request) {
|
|
1226
|
+
return this.client.callStorage(request);
|
|
1227
|
+
}
|
|
1228
|
+
/**
|
|
1229
|
+
* Call storage repeatedly until hasMore is false.
|
|
1230
|
+
* Use for bulk operations like clearTable that may need multiple batches.
|
|
1231
|
+
*/
|
|
1232
|
+
async callStorageUntilComplete(request) {
|
|
1233
|
+
let hasMore = true;
|
|
1234
|
+
while (hasMore) {
|
|
1235
|
+
const response = await this.client.callStorageRaw(request);
|
|
1236
|
+
hasMore = response.hasMore ?? false;
|
|
1237
|
+
}
|
|
1238
|
+
}
|
|
1239
|
+
};
|
|
1240
|
+
function cosineSimilarity(a, b) {
|
|
1241
|
+
if (a.length !== b.length) {
|
|
1242
|
+
return -1;
|
|
1243
|
+
}
|
|
1244
|
+
let dot = 0;
|
|
1245
|
+
let magA = 0;
|
|
1246
|
+
let magB = 0;
|
|
1247
|
+
for (let i = 0; i < a.length; i++) {
|
|
1248
|
+
const aVal = a[i] ?? 0;
|
|
1249
|
+
const bVal = b[i] ?? 0;
|
|
1250
|
+
dot += aVal * bVal;
|
|
1251
|
+
magA += aVal * aVal;
|
|
1252
|
+
magB += bVal * bVal;
|
|
1253
|
+
}
|
|
1254
|
+
if (magA === 0 || magB === 0) {
|
|
1255
|
+
return -1;
|
|
1256
|
+
}
|
|
1257
|
+
return dot / (Math.sqrt(magA) * Math.sqrt(magB));
|
|
1258
|
+
}
|
|
1259
|
+
|
|
1260
|
+
export { ConvexStore, ConvexVector };
|
|
1261
|
+
//# sourceMappingURL=index.js.map
|
|
1262
|
+
//# sourceMappingURL=index.js.map
|