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