@mastra/convex 0.0.0-bundle-studio-cloud-20251222034739

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