@mastra/convex 1.1.0-alpha.0 → 1.2.0-alpha.0

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 (42) hide show
  1. package/CHANGELOG.md +129 -0
  2. package/README.md +31 -13
  3. package/dist/{chunk-FZDLZ4S3.js → chunk-AJFME2ZF.js} +323 -16
  4. package/dist/chunk-AJFME2ZF.js.map +1 -0
  5. package/dist/{chunk-JPWDG4L3.js → chunk-MC75WADX.js} +79 -4
  6. package/dist/chunk-MC75WADX.js.map +1 -0
  7. package/dist/{chunk-EEELVBWO.cjs → chunk-ORSDZTO4.cjs} +322 -15
  8. package/dist/chunk-ORSDZTO4.cjs.map +1 -0
  9. package/dist/{chunk-CV23JOCS.cjs → chunk-SFRHJGSM.cjs} +102 -2
  10. package/dist/chunk-SFRHJGSM.cjs.map +1 -0
  11. package/dist/docs/SKILL.md +1 -1
  12. package/dist/docs/assets/SOURCE_MAP.json +63 -23
  13. package/dist/index.cjs +634 -69
  14. package/dist/index.cjs.map +1 -1
  15. package/dist/index.js +574 -49
  16. package/dist/index.js.map +1 -1
  17. package/dist/schema.cjs +60 -20
  18. package/dist/schema.d.ts +192 -2
  19. package/dist/schema.d.ts.map +1 -1
  20. package/dist/schema.js +1 -1
  21. package/dist/server/index-map.d.ts.map +1 -1
  22. package/dist/server/index.cjs +63 -23
  23. package/dist/server/index.d.ts +1 -1
  24. package/dist/server/index.d.ts.map +1 -1
  25. package/dist/server/index.js +2 -2
  26. package/dist/server/storage.d.ts.map +1 -1
  27. package/dist/storage/db/index.d.ts +28 -1
  28. package/dist/storage/db/index.d.ts.map +1 -1
  29. package/dist/storage/domains/background-tasks/index.d.ts.map +1 -1
  30. package/dist/storage/domains/channels/index.d.ts +19 -0
  31. package/dist/storage/domains/channels/index.d.ts.map +1 -0
  32. package/dist/storage/domains/schedules/index.d.ts +19 -0
  33. package/dist/storage/domains/schedules/index.d.ts.map +1 -0
  34. package/dist/storage/index.d.ts +3 -1
  35. package/dist/storage/index.d.ts.map +1 -1
  36. package/dist/storage/types.d.ts +42 -0
  37. package/dist/storage/types.d.ts.map +1 -1
  38. package/package.json +5 -5
  39. package/dist/chunk-CV23JOCS.cjs.map +0 -1
  40. package/dist/chunk-EEELVBWO.cjs.map +0 -1
  41. package/dist/chunk-FZDLZ4S3.js.map +0 -1
  42. package/dist/chunk-JPWDG4L3.js.map +0 -1
package/dist/index.cjs CHANGED
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
- var chunkEEELVBWO_cjs = require('./chunk-EEELVBWO.cjs');
4
- var chunkCV23JOCS_cjs = require('./chunk-CV23JOCS.cjs');
3
+ var chunkORSDZTO4_cjs = require('./chunk-ORSDZTO4.cjs');
4
+ var chunkSFRHJGSM_cjs = require('./chunk-SFRHJGSM.cjs');
5
5
  var cache = require('@mastra/core/cache');
6
6
  var storage = require('@mastra/core/storage');
7
7
  var crypto = require('crypto');
@@ -343,6 +343,18 @@ var ConvexDB = class extends base.MastraBase {
343
343
  records: records.map((record) => this.normalizeRecord(tableName, record))
344
344
  });
345
345
  }
346
+ async patch({
347
+ tableName,
348
+ id,
349
+ record
350
+ }) {
351
+ return this.client.callStorage({
352
+ op: "patch",
353
+ tableName,
354
+ id,
355
+ record: this.normalizePatch(record)
356
+ });
357
+ }
346
358
  async load({ tableName, keys }) {
347
359
  const result = await this.client.callStorage({
348
360
  op: "load",
@@ -351,12 +363,13 @@ var ConvexDB = class extends base.MastraBase {
351
363
  });
352
364
  return result;
353
365
  }
354
- async queryTable(tableName, filters, indexHint) {
366
+ async queryTable(tableName, filters, indexHint, limit) {
355
367
  return this.client.callStorage({
356
368
  op: "queryTable",
357
369
  tableName,
358
370
  filters,
359
- indexHint
371
+ indexHint,
372
+ limit
360
373
  });
361
374
  }
362
375
  async deleteMany(tableName, ids) {
@@ -367,6 +380,85 @@ var ConvexDB = class extends base.MastraBase {
367
380
  ids
368
381
  });
369
382
  }
383
+ async createSchedule(record) {
384
+ if (!record.id) {
385
+ throw new Error(`Schedule is missing an id`);
386
+ }
387
+ await this.client.callStorage({
388
+ op: "createSchedule",
389
+ tableName: storage.TABLE_SCHEDULES,
390
+ record: this.normalizeRecord(storage.TABLE_SCHEDULES, record)
391
+ });
392
+ }
393
+ async recordScheduleTrigger(record) {
394
+ if (!record.id) {
395
+ throw new Error(`Schedule trigger is missing an id`);
396
+ }
397
+ await this.client.callStorage({
398
+ op: "recordScheduleTrigger",
399
+ tableName: storage.TABLE_SCHEDULE_TRIGGERS,
400
+ record: this.normalizeRecord(storage.TABLE_SCHEDULE_TRIGGERS, record)
401
+ });
402
+ }
403
+ async listDueSchedules(now, limit) {
404
+ return this.client.callStorage({
405
+ op: "listDueSchedules",
406
+ tableName: storage.TABLE_SCHEDULES,
407
+ now,
408
+ limit
409
+ });
410
+ }
411
+ async updateScheduleNextFire({
412
+ id,
413
+ expectedNextFireAt,
414
+ newNextFireAt,
415
+ lastFireAt,
416
+ lastRunId
417
+ }) {
418
+ return this.client.callStorage({
419
+ op: "updateScheduleNextFire",
420
+ tableName: storage.TABLE_SCHEDULES,
421
+ id,
422
+ expectedNextFireAt,
423
+ newNextFireAt,
424
+ lastFireAt,
425
+ lastRunId
426
+ });
427
+ }
428
+ async updateSchedule({ id, patch }) {
429
+ return this.client.callStorage({
430
+ op: "updateSchedule",
431
+ tableName: storage.TABLE_SCHEDULES,
432
+ id,
433
+ patch
434
+ });
435
+ }
436
+ async listScheduleTriggers({
437
+ scheduleId,
438
+ fromActualFireAt,
439
+ toActualFireAt,
440
+ limit
441
+ }) {
442
+ return this.client.callStorage({
443
+ op: "listScheduleTriggers",
444
+ tableName: storage.TABLE_SCHEDULE_TRIGGERS,
445
+ scheduleId,
446
+ fromActualFireAt,
447
+ toActualFireAt,
448
+ limit
449
+ });
450
+ }
451
+ async deleteScheduleTriggers(scheduleId) {
452
+ let hasMore = true;
453
+ while (hasMore) {
454
+ const response = await this.client.callStorageRaw({
455
+ op: "deleteScheduleTriggers",
456
+ tableName: storage.TABLE_SCHEDULE_TRIGGERS,
457
+ scheduleId
458
+ });
459
+ hasMore = response.hasMore ?? false;
460
+ }
461
+ }
370
462
  normalizeRecord(tableName, record) {
371
463
  const normalized = { ...record };
372
464
  if (tableName === storage.TABLE_WORKFLOW_SNAPSHOT && !normalized.id) {
@@ -384,29 +476,69 @@ var ConvexDB = class extends base.MastraBase {
384
476
  }
385
477
  return normalized;
386
478
  }
479
+ normalizePatch(record) {
480
+ const normalized = { ...record };
481
+ for (const [key, value] of Object.entries(normalized)) {
482
+ if (value instanceof Date) {
483
+ normalized[key] = value.toISOString();
484
+ }
485
+ }
486
+ return normalized;
487
+ }
387
488
  };
388
489
 
389
490
  // src/storage/domains/background-tasks/index.ts
390
491
  function serializeJson(v) {
391
- if (typeof v === "object" && v != null) return JSON.stringify(v);
392
- return v ?? void 0;
492
+ if (v === void 0 || v === null) return null;
493
+ return JSON.stringify(v);
494
+ }
495
+ function serializeRequiredJson(v) {
496
+ return JSON.stringify(v ?? {});
393
497
  }
394
498
  function toStored(task) {
395
499
  return {
396
- ...task,
397
- args: serializeJson(task.args),
500
+ id: task.id,
501
+ status: task.status,
502
+ tool_call_id: task.toolCallId,
503
+ tool_name: task.toolName,
504
+ agent_id: task.agentId,
505
+ run_id: task.runId,
506
+ thread_id: task.threadId ?? null,
507
+ resource_id: task.resourceId ?? null,
508
+ args: serializeRequiredJson(task.args),
398
509
  result: serializeJson(task.result),
399
510
  error: serializeJson(task.error),
400
- suspendPayload: serializeJson(task.suspendPayload),
511
+ suspend_payload: serializeJson(task.suspendPayload),
512
+ retry_count: task.retryCount,
513
+ max_retries: task.maxRetries,
514
+ timeout_ms: task.timeoutMs,
401
515
  createdAt: task.createdAt.toISOString(),
402
- startedAt: task.startedAt?.toISOString(),
403
- suspendedAt: task.suspendedAt?.toISOString(),
404
- completedAt: task.completedAt?.toISOString()
516
+ startedAt: task.startedAt?.toISOString() ?? null,
517
+ suspendedAt: task.suspendedAt?.toISOString() ?? null,
518
+ completedAt: task.completedAt?.toISOString() ?? null
405
519
  };
406
520
  }
521
+ function toStoredPatch(update) {
522
+ const patch = {};
523
+ if (update.status !== void 0) patch.status = update.status;
524
+ if ("result" in update) patch.result = serializeJson(update.result);
525
+ if ("error" in update) patch.error = serializeJson(update.error);
526
+ if ("suspendPayload" in update) patch.suspend_payload = serializeJson(update.suspendPayload);
527
+ if (update.retryCount !== void 0) patch.retry_count = update.retryCount;
528
+ if (update.maxRetries !== void 0) patch.max_retries = update.maxRetries;
529
+ if (update.timeoutMs !== void 0) patch.timeout_ms = update.timeoutMs;
530
+ if ("startedAt" in update) patch.startedAt = update.startedAt?.toISOString() ?? null;
531
+ if ("suspendedAt" in update) patch.suspendedAt = update.suspendedAt?.toISOString() ?? null;
532
+ if ("completedAt" in update) patch.completedAt = update.completedAt?.toISOString() ?? null;
533
+ return patch;
534
+ }
535
+ function legacyOrCurrent(stored, currentKey, legacyKey) {
536
+ return currentKey in stored ? stored[currentKey] : stored[legacyKey];
537
+ }
407
538
  function fromStored(stored) {
408
- const parseJson = (val) => {
409
- if (!val) return void 0;
539
+ const record = stored;
540
+ const parseJson3 = (val) => {
541
+ if (val == null) return void 0;
410
542
  try {
411
543
  return JSON.parse(val);
412
544
  } catch {
@@ -414,27 +546,32 @@ function fromStored(stored) {
414
546
  }
415
547
  };
416
548
  return {
417
- id: stored.id,
418
- status: stored.status,
419
- toolName: stored.toolName,
420
- toolCallId: stored.toolCallId,
421
- args: parseJson(stored.args) ?? {},
422
- agentId: stored.agentId,
423
- threadId: stored.threadId,
424
- resourceId: stored.resourceId,
425
- runId: stored.runId,
426
- result: parseJson(stored.result),
427
- error: parseJson(stored.error),
428
- suspendPayload: parseJson(stored.suspendPayload),
429
- retryCount: stored.retryCount,
430
- maxRetries: stored.maxRetries,
431
- timeoutMs: stored.timeoutMs,
432
- createdAt: new Date(stored.createdAt),
433
- startedAt: stored.startedAt ? new Date(stored.startedAt) : void 0,
434
- suspendedAt: stored.suspendedAt ? new Date(stored.suspendedAt) : void 0,
435
- completedAt: stored.completedAt ? new Date(stored.completedAt) : void 0
549
+ id: record.id,
550
+ status: record.status,
551
+ toolName: legacyOrCurrent(record, "tool_name", "toolName"),
552
+ toolCallId: legacyOrCurrent(record, "tool_call_id", "toolCallId"),
553
+ args: parseJson3(record.args) ?? {},
554
+ agentId: legacyOrCurrent(record, "agent_id", "agentId"),
555
+ threadId: legacyOrCurrent(record, "thread_id", "threadId") ?? void 0,
556
+ resourceId: legacyOrCurrent(record, "resource_id", "resourceId") ?? void 0,
557
+ runId: legacyOrCurrent(record, "run_id", "runId"),
558
+ result: parseJson3(record.result),
559
+ error: parseJson3(record.error),
560
+ suspendPayload: parseJson3(legacyOrCurrent(record, "suspend_payload", "suspendPayload")),
561
+ retryCount: legacyOrCurrent(record, "retry_count", "retryCount"),
562
+ maxRetries: legacyOrCurrent(record, "max_retries", "maxRetries"),
563
+ timeoutMs: legacyOrCurrent(record, "timeout_ms", "timeoutMs"),
564
+ createdAt: new Date(record.createdAt),
565
+ startedAt: record.startedAt ? new Date(record.startedAt) : void 0,
566
+ suspendedAt: record.suspendedAt ? new Date(record.suspendedAt) : void 0,
567
+ completedAt: record.completedAt ? new Date(record.completedAt) : void 0
436
568
  };
437
569
  }
570
+ function hasDeleteFilter(filter) {
571
+ return Boolean(
572
+ (Array.isArray(filter.status) ? filter.status.length > 0 : filter.status) || filter.agentId || filter.threadId || filter.resourceId || filter.toolName || filter.toolCallId || filter.runId || filter.fromDate || filter.toDate
573
+ );
574
+ }
438
575
  var BackgroundTasksConvex = class extends storage.BackgroundTasksStorage {
439
576
  #db;
440
577
  constructor(config) {
@@ -449,26 +586,34 @@ var BackgroundTasksConvex = class extends storage.BackgroundTasksStorage {
449
586
  await this.#db.insert({ tableName: storage.TABLE_BACKGROUND_TASKS, record: toStored(task) });
450
587
  }
451
588
  async updateTask(taskId, update) {
452
- const existing = await this.getTask(taskId);
453
- if (!existing) return;
454
- const merged = { ...existing };
455
- if ("status" in update) merged.status = update.status;
456
- if ("result" in update) merged.result = update.result;
457
- if ("error" in update) merged.error = update.error;
458
- if ("suspendPayload" in update) merged.suspendPayload = update.suspendPayload;
459
- if ("retryCount" in update) merged.retryCount = update.retryCount;
460
- if ("startedAt" in update) merged.startedAt = update.startedAt;
461
- if ("suspendedAt" in update) merged.suspendedAt = update.suspendedAt;
462
- if ("completedAt" in update) merged.completedAt = update.completedAt;
463
- await this.#db.deleteMany(storage.TABLE_BACKGROUND_TASKS, [taskId]);
464
- await this.#db.insert({ tableName: storage.TABLE_BACKGROUND_TASKS, record: toStored(merged) });
589
+ const patch = toStoredPatch(update);
590
+ if (Object.keys(patch).length === 0) return;
591
+ await this.#db.patch({
592
+ tableName: storage.TABLE_BACKGROUND_TASKS,
593
+ id: taskId,
594
+ record: patch
595
+ });
465
596
  }
466
597
  async getTask(taskId) {
467
598
  const data = await this.#db.load({ tableName: storage.TABLE_BACKGROUND_TASKS, keys: { id: taskId } });
468
599
  return data ? fromStored(data) : null;
469
600
  }
470
601
  async listTasks(filter) {
471
- const all = await this.#db.queryTable(storage.TABLE_BACKGROUND_TASKS);
602
+ const queryFilters = [];
603
+ if (typeof filter.status === "string") queryFilters.push({ field: "status", value: filter.status });
604
+ if (Array.isArray(filter.status) && filter.status.length === 1) {
605
+ queryFilters.push({ field: "status", value: filter.status[0] });
606
+ }
607
+ if (filter.agentId) queryFilters.push({ field: "agent_id", value: filter.agentId });
608
+ if (filter.threadId) queryFilters.push({ field: "thread_id", value: filter.threadId });
609
+ if (filter.resourceId) queryFilters.push({ field: "resource_id", value: filter.resourceId });
610
+ if (filter.toolName) queryFilters.push({ field: "tool_name", value: filter.toolName });
611
+ if (filter.toolCallId) queryFilters.push({ field: "tool_call_id", value: filter.toolCallId });
612
+ if (filter.runId) queryFilters.push({ field: "run_id", value: filter.runId });
613
+ const all = await this.#db.queryTable(
614
+ storage.TABLE_BACKGROUND_TASKS,
615
+ queryFilters.length > 0 ? queryFilters : void 0
616
+ );
472
617
  let tasks = all.map(fromStored);
473
618
  if (filter.status) {
474
619
  const s = Array.isArray(filter.status) ? filter.status : [filter.status];
@@ -476,6 +621,7 @@ var BackgroundTasksConvex = class extends storage.BackgroundTasksStorage {
476
621
  }
477
622
  if (filter.agentId) tasks = tasks.filter((t) => t.agentId === filter.agentId);
478
623
  if (filter.threadId) tasks = tasks.filter((t) => t.threadId === filter.threadId);
624
+ if (filter.resourceId) tasks = tasks.filter((t) => t.resourceId === filter.resourceId);
479
625
  if (filter.toolName) tasks = tasks.filter((t) => t.toolName === filter.toolName);
480
626
  if (filter.toolCallId) tasks = tasks.filter((t) => t.toolCallId === filter.toolCallId);
481
627
  if (filter.runId) tasks = tasks.filter((t) => t.runId === filter.runId);
@@ -508,6 +654,7 @@ var BackgroundTasksConvex = class extends storage.BackgroundTasksStorage {
508
654
  await this.#db.deleteMany(storage.TABLE_BACKGROUND_TASKS, [taskId]);
509
655
  }
510
656
  async deleteTasks(filter) {
657
+ if (!hasDeleteFilter(filter)) return;
511
658
  const { tasks } = await this.listTasks(filter);
512
659
  const taskIds = tasks.map((t) => t.id);
513
660
  await this.#db.deleteMany(storage.TABLE_BACKGROUND_TASKS, taskIds);
@@ -521,6 +668,141 @@ var BackgroundTasksConvex = class extends storage.BackgroundTasksStorage {
521
668
  return total;
522
669
  }
523
670
  };
671
+ var statusPriority = {
672
+ active: 0,
673
+ pending: 1,
674
+ error: 2
675
+ };
676
+ function stringifyJson(value) {
677
+ return JSON.stringify(value);
678
+ }
679
+ function parseJson(value, context) {
680
+ try {
681
+ return JSON.parse(value);
682
+ } catch (error) {
683
+ throw new Error(`Invalid channel data JSON for ${context}`, { cause: error });
684
+ }
685
+ }
686
+ function installationToRecord(installation, existingRecord) {
687
+ const now = (/* @__PURE__ */ new Date()).toISOString();
688
+ return {
689
+ id: installation.id,
690
+ platform: installation.platform,
691
+ agentId: installation.agentId,
692
+ status: installation.status,
693
+ webhookId: installation.webhookId ?? null,
694
+ data: stringifyJson(installation.data),
695
+ configHash: installation.configHash ?? null,
696
+ error: installation.error ?? null,
697
+ createdAt: existingRecord?.createdAt ?? installation.createdAt?.toISOString() ?? now,
698
+ updatedAt: now
699
+ };
700
+ }
701
+ function recordToInstallation(record) {
702
+ return {
703
+ id: record.id,
704
+ platform: record.platform,
705
+ agentId: record.agentId,
706
+ status: record.status,
707
+ webhookId: record.webhookId ?? void 0,
708
+ data: parseJson(record.data, `installation ${record.id}`),
709
+ configHash: record.configHash ?? void 0,
710
+ error: record.error ?? void 0,
711
+ createdAt: new Date(record.createdAt),
712
+ updatedAt: new Date(record.updatedAt)
713
+ };
714
+ }
715
+ function configToRecord(config) {
716
+ return {
717
+ id: config.platform,
718
+ platform: config.platform,
719
+ data: stringifyJson(config.data),
720
+ updatedAt: config.updatedAt.toISOString()
721
+ };
722
+ }
723
+ function recordToConfig(record) {
724
+ return {
725
+ platform: record.platform,
726
+ data: parseJson(record.data, `config ${record.platform}`),
727
+ updatedAt: new Date(record.updatedAt)
728
+ };
729
+ }
730
+ function sortInstallationsForAgent(a, b) {
731
+ const statusDiff = statusPriority[a.status] - statusPriority[b.status];
732
+ if (statusDiff !== 0) return statusDiff;
733
+ return b.updatedAt.getTime() - a.updatedAt.getTime();
734
+ }
735
+ var ChannelsConvex = class extends storage.ChannelsStorage {
736
+ #db;
737
+ constructor(config) {
738
+ super();
739
+ const client = resolveConvexConfig(config);
740
+ this.#db = new ConvexDB(client);
741
+ }
742
+ async init() {
743
+ }
744
+ async dangerouslyClearAll() {
745
+ await this.#db.clearTable({ tableName: storage.TABLE_CHANNEL_INSTALLATIONS });
746
+ await this.#db.clearTable({ tableName: storage.TABLE_CHANNEL_CONFIG });
747
+ }
748
+ async saveInstallation(installation) {
749
+ const existingRecord = await this.#db.load({
750
+ tableName: storage.TABLE_CHANNEL_INSTALLATIONS,
751
+ keys: { id: installation.id }
752
+ });
753
+ await this.#db.insert({
754
+ tableName: storage.TABLE_CHANNEL_INSTALLATIONS,
755
+ record: installationToRecord(installation, existingRecord)
756
+ });
757
+ }
758
+ async getInstallation(id) {
759
+ const record = await this.#db.load({
760
+ tableName: storage.TABLE_CHANNEL_INSTALLATIONS,
761
+ keys: { id }
762
+ });
763
+ return record ? recordToInstallation(record) : null;
764
+ }
765
+ async getInstallationByAgent(platform, agentId) {
766
+ const records = await this.#db.queryTable(storage.TABLE_CHANNEL_INSTALLATIONS, [
767
+ { field: "platform", value: platform },
768
+ { field: "agentId", value: agentId }
769
+ ]);
770
+ const [installation] = records.map(recordToInstallation).sort(sortInstallationsForAgent);
771
+ return installation ?? null;
772
+ }
773
+ async getInstallationByWebhookId(webhookId) {
774
+ const records = await this.#db.queryTable(storage.TABLE_CHANNEL_INSTALLATIONS, [
775
+ { field: "webhookId", value: webhookId }
776
+ ]);
777
+ const [installation] = records.map(recordToInstallation).sort((a, b) => b.updatedAt.getTime() - a.updatedAt.getTime());
778
+ return installation ?? null;
779
+ }
780
+ async listInstallations(platform) {
781
+ const records = await this.#db.queryTable(storage.TABLE_CHANNEL_INSTALLATIONS, [
782
+ { field: "platform", value: platform }
783
+ ]);
784
+ return records.map(recordToInstallation).sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
785
+ }
786
+ async deleteInstallation(id) {
787
+ await this.#db.deleteMany(storage.TABLE_CHANNEL_INSTALLATIONS, [id]);
788
+ }
789
+ async saveConfig(config) {
790
+ await this.#db.insert({
791
+ tableName: storage.TABLE_CHANNEL_CONFIG,
792
+ record: configToRecord(config)
793
+ });
794
+ }
795
+ async getConfig(platform) {
796
+ const record = await this.#db.load({
797
+ tableName: storage.TABLE_CHANNEL_CONFIG,
798
+ keys: { id: platform }
799
+ });
800
+ return record ? recordToConfig(record) : null;
801
+ }
802
+ async deleteConfig(platform) {
803
+ await this.#db.deleteMany(storage.TABLE_CHANNEL_CONFIG, [platform]);
804
+ }
805
+ };
524
806
  var MemoryConvex = class extends storage.MemoryStorage {
525
807
  #db;
526
808
  constructor(config) {
@@ -1006,6 +1288,247 @@ var MemoryConvex = class extends storage.MemoryStorage {
1006
1288
  }
1007
1289
  }
1008
1290
  };
1291
+ var SCHEDULE_LIST_LIMIT = 8e3;
1292
+ function serializeJson2(value) {
1293
+ return JSON.stringify(value);
1294
+ }
1295
+ function parseJson2(value) {
1296
+ if (value == null) return void 0;
1297
+ if (typeof value === "string") {
1298
+ try {
1299
+ return JSON.parse(value);
1300
+ } catch {
1301
+ return value;
1302
+ }
1303
+ }
1304
+ return value;
1305
+ }
1306
+ function isMissingSchedulesSchemaError(error) {
1307
+ const message = error instanceof Error ? error.message : String(error);
1308
+ return /(?:Table|table)\s+(?:["'`])?(?:mastra_schedules|mastra_schedule_triggers)(?:["'`])?\s+(?:is\s+)?(?:not\s+in\s+the\s+schema|not\s+found\s+in\s+schema|not\s+found|does\s+not\s+exist)/i.test(
1309
+ message
1310
+ );
1311
+ }
1312
+ function scheduleToRecord(schedule) {
1313
+ return {
1314
+ id: schedule.id,
1315
+ target: serializeJson2(schedule.target),
1316
+ cron: schedule.cron,
1317
+ timezone: schedule.timezone ?? null,
1318
+ status: schedule.status,
1319
+ next_fire_at: schedule.nextFireAt,
1320
+ last_fire_at: schedule.lastFireAt ?? null,
1321
+ last_run_id: schedule.lastRunId ?? null,
1322
+ created_at: schedule.createdAt,
1323
+ updated_at: schedule.updatedAt,
1324
+ metadata: schedule.metadata == null ? null : serializeJson2(schedule.metadata),
1325
+ owner_type: schedule.ownerType ?? null,
1326
+ owner_id: schedule.ownerId ?? null,
1327
+ workflow_id: schedule.target.type === "workflow" ? schedule.target.workflowId : null
1328
+ };
1329
+ }
1330
+ function recordToSchedule(record) {
1331
+ const target = parseJson2(record.target);
1332
+ if (!target || typeof target !== "object" || typeof target.type !== "string") {
1333
+ throw new Error(`Schedule ${record.id} has invalid target`);
1334
+ }
1335
+ const schedule = {
1336
+ id: String(record.id),
1337
+ target,
1338
+ cron: String(record.cron),
1339
+ status: String(record.status),
1340
+ nextFireAt: Number(record.next_fire_at),
1341
+ createdAt: Number(record.created_at),
1342
+ updatedAt: Number(record.updated_at)
1343
+ };
1344
+ if (record.timezone != null) schedule.timezone = String(record.timezone);
1345
+ if (record.last_fire_at != null) schedule.lastFireAt = Number(record.last_fire_at);
1346
+ if (record.last_run_id != null) schedule.lastRunId = String(record.last_run_id);
1347
+ const metadata = parseJson2(record.metadata);
1348
+ if (metadata != null) schedule.metadata = metadata;
1349
+ if (record.owner_type != null) schedule.ownerType = String(record.owner_type);
1350
+ if (record.owner_id != null) schedule.ownerId = String(record.owner_id);
1351
+ return schedule;
1352
+ }
1353
+ function triggerToRecord(trigger) {
1354
+ return {
1355
+ id: trigger.id ?? crypto__default.default.randomUUID(),
1356
+ schedule_id: trigger.scheduleId,
1357
+ run_id: trigger.runId,
1358
+ scheduled_fire_at: trigger.scheduledFireAt,
1359
+ actual_fire_at: trigger.actualFireAt,
1360
+ outcome: trigger.outcome,
1361
+ error: trigger.error ?? null,
1362
+ trigger_kind: trigger.triggerKind ?? "schedule-fire",
1363
+ parent_trigger_id: trigger.parentTriggerId ?? null,
1364
+ metadata: trigger.metadata == null ? null : serializeJson2(trigger.metadata)
1365
+ };
1366
+ }
1367
+ function recordToTrigger(record) {
1368
+ const trigger = {
1369
+ id: record.id != null ? String(record.id) : void 0,
1370
+ scheduleId: String(record.schedule_id),
1371
+ runId: record.run_id != null ? String(record.run_id) : null,
1372
+ scheduledFireAt: Number(record.scheduled_fire_at),
1373
+ actualFireAt: Number(record.actual_fire_at),
1374
+ outcome: String(record.outcome),
1375
+ triggerKind: record.trigger_kind != null ? String(record.trigger_kind) : "schedule-fire"
1376
+ };
1377
+ if (record.error != null) trigger.error = String(record.error);
1378
+ if (record.parent_trigger_id != null) trigger.parentTriggerId = String(record.parent_trigger_id);
1379
+ const metadata = parseJson2(record.metadata);
1380
+ if (metadata != null) trigger.metadata = metadata;
1381
+ return trigger;
1382
+ }
1383
+ var SchedulesConvex = class extends storage.SchedulesStorage {
1384
+ #db;
1385
+ constructor(config) {
1386
+ super();
1387
+ const client = resolveConvexConfig(config);
1388
+ this.#db = new ConvexDB(client);
1389
+ }
1390
+ async init() {
1391
+ }
1392
+ async dangerouslyClearAll() {
1393
+ await this.#db.clearTable({ tableName: storage.TABLE_SCHEDULE_TRIGGERS });
1394
+ await this.#db.clearTable({ tableName: storage.TABLE_SCHEDULES });
1395
+ }
1396
+ async createSchedule(schedule) {
1397
+ await this.#db.createSchedule(scheduleToRecord(schedule));
1398
+ return schedule;
1399
+ }
1400
+ async getSchedule(id) {
1401
+ const record = await this.#db.load({ tableName: storage.TABLE_SCHEDULES, keys: { id } });
1402
+ return record ? recordToSchedule(record) : null;
1403
+ }
1404
+ async listSchedules(filter) {
1405
+ const queryFilters = [];
1406
+ if (filter?.status) queryFilters.push({ field: "status", value: filter.status });
1407
+ if (filter?.ownerType !== void 0 && filter.ownerType !== null) {
1408
+ queryFilters.push({ field: "owner_type", value: filter.ownerType });
1409
+ }
1410
+ if (filter?.ownerType === null) {
1411
+ queryFilters.push({ field: "owner_type", value: null });
1412
+ }
1413
+ if (filter?.ownerId !== void 0 && filter.ownerId !== null) {
1414
+ queryFilters.push({ field: "owner_id", value: filter.ownerId });
1415
+ }
1416
+ if (filter?.ownerId === null) {
1417
+ queryFilters.push({ field: "owner_id", value: null });
1418
+ }
1419
+ if (filter?.workflowId) queryFilters.push({ field: "workflow_id", value: filter.workflowId });
1420
+ let records;
1421
+ try {
1422
+ records = await this.#db.queryTable(
1423
+ storage.TABLE_SCHEDULES,
1424
+ queryFilters.length ? queryFilters : void 0,
1425
+ void 0,
1426
+ SCHEDULE_LIST_LIMIT
1427
+ );
1428
+ } catch (error) {
1429
+ if (isMissingSchedulesSchemaError(error)) {
1430
+ this.logger.warn("Convex schedules schema is not available; returning no schedules", { error });
1431
+ return [];
1432
+ }
1433
+ throw error;
1434
+ }
1435
+ if (records.length >= SCHEDULE_LIST_LIMIT) {
1436
+ this.logger.warn("Convex schedules list reached the adapter limit; results may be truncated", {
1437
+ limit: SCHEDULE_LIST_LIMIT
1438
+ });
1439
+ }
1440
+ let schedules = records.map(recordToSchedule);
1441
+ if (filter?.workflowId) {
1442
+ schedules = schedules.filter(
1443
+ (schedule) => schedule.target.type === "workflow" && schedule.target.workflowId === filter.workflowId
1444
+ );
1445
+ }
1446
+ if (filter?.ownerType === null) {
1447
+ schedules = schedules.filter((schedule) => (schedule.ownerType ?? null) === null);
1448
+ }
1449
+ if (filter?.ownerId === null) {
1450
+ schedules = schedules.filter((schedule) => (schedule.ownerId ?? null) === null);
1451
+ }
1452
+ schedules.sort((a, b) => a.createdAt - b.createdAt);
1453
+ return schedules;
1454
+ }
1455
+ async listDueSchedules(now, limit) {
1456
+ let records;
1457
+ try {
1458
+ records = await this.#db.listDueSchedules(now, limit);
1459
+ } catch (error) {
1460
+ if (isMissingSchedulesSchemaError(error)) {
1461
+ this.logger.warn("Convex schedules schema is not available; returning no due schedules", { error });
1462
+ return [];
1463
+ }
1464
+ throw error;
1465
+ }
1466
+ return records.map(recordToSchedule);
1467
+ }
1468
+ async updateSchedule(id, patch) {
1469
+ const updates = {};
1470
+ if ("cron" in patch && patch.cron !== void 0) {
1471
+ updates.cron = patch.cron;
1472
+ }
1473
+ if ("timezone" in patch) {
1474
+ updates.timezone = patch.timezone ?? null;
1475
+ }
1476
+ if ("status" in patch && patch.status !== void 0) {
1477
+ updates.status = patch.status;
1478
+ }
1479
+ if ("nextFireAt" in patch && patch.nextFireAt !== void 0) {
1480
+ updates.next_fire_at = patch.nextFireAt;
1481
+ }
1482
+ if ("target" in patch && patch.target !== void 0) {
1483
+ updates.target = serializeJson2(patch.target);
1484
+ updates.workflow_id = patch.target.type === "workflow" ? patch.target.workflowId : null;
1485
+ }
1486
+ if ("metadata" in patch) {
1487
+ updates.metadata = patch.metadata == null ? null : serializeJson2(patch.metadata);
1488
+ }
1489
+ if ("ownerType" in patch) {
1490
+ updates.owner_type = patch.ownerType ?? null;
1491
+ }
1492
+ if ("ownerId" in patch) {
1493
+ updates.owner_id = patch.ownerId ?? null;
1494
+ }
1495
+ if (Object.keys(updates).length === 0) {
1496
+ const existing = await this.getSchedule(id);
1497
+ if (!existing) {
1498
+ throw new Error(`Schedule ${id} not found`);
1499
+ }
1500
+ return existing;
1501
+ }
1502
+ updates.updated_at = Date.now();
1503
+ const updated = await this.#db.updateSchedule({ id, patch: updates });
1504
+ return recordToSchedule(updated);
1505
+ }
1506
+ async updateScheduleNextFire(id, expectedNextFireAt, newNextFireAt, lastFireAt, lastRunId) {
1507
+ return this.#db.updateScheduleNextFire({
1508
+ id,
1509
+ expectedNextFireAt,
1510
+ newNextFireAt,
1511
+ lastFireAt,
1512
+ lastRunId
1513
+ });
1514
+ }
1515
+ async deleteSchedule(id) {
1516
+ await this.#db.deleteScheduleTriggers(id);
1517
+ await this.#db.deleteMany(storage.TABLE_SCHEDULES, [id]);
1518
+ }
1519
+ async recordTrigger(trigger) {
1520
+ await this.#db.recordScheduleTrigger(triggerToRecord(trigger));
1521
+ }
1522
+ async listTriggers(scheduleId, opts) {
1523
+ const triggers = await this.#db.listScheduleTriggers({
1524
+ scheduleId,
1525
+ fromActualFireAt: opts?.fromActualFireAt,
1526
+ toActualFireAt: opts?.toActualFireAt,
1527
+ limit: opts?.limit
1528
+ });
1529
+ return triggers.map(recordToTrigger);
1530
+ }
1531
+ };
1009
1532
  var ScoresConvex = class extends storage.ScoresStorage {
1010
1533
  #db;
1011
1534
  constructor(config) {
@@ -1286,7 +1809,9 @@ var ConvexStore = class extends storage.MastraCompositeStore {
1286
1809
  memory,
1287
1810
  workflows,
1288
1811
  scores,
1289
- backgroundTasks: new BackgroundTasksConvex(domainConfig)
1812
+ backgroundTasks: new BackgroundTasksConvex(domainConfig),
1813
+ schedules: new SchedulesConvex(domainConfig),
1814
+ channels: new ChannelsConvex(domainConfig)
1290
1815
  };
1291
1816
  }
1292
1817
  };
@@ -1847,87 +2372,127 @@ var ConvexNativeVector = class extends vector.MastraVector {
1847
2372
 
1848
2373
  Object.defineProperty(exports, "mastraCache", {
1849
2374
  enumerable: true,
1850
- get: function () { return chunkEEELVBWO_cjs.mastraCache; }
2375
+ get: function () { return chunkORSDZTO4_cjs.mastraCache; }
1851
2376
  });
1852
2377
  Object.defineProperty(exports, "mastraNativeVectorAction", {
1853
2378
  enumerable: true,
1854
- get: function () { return chunkEEELVBWO_cjs.mastraNativeVectorAction; }
2379
+ get: function () { return chunkORSDZTO4_cjs.mastraNativeVectorAction; }
1855
2380
  });
1856
2381
  Object.defineProperty(exports, "mastraNativeVectorMutation", {
1857
2382
  enumerable: true,
1858
- get: function () { return chunkEEELVBWO_cjs.mastraNativeVectorMutation; }
2383
+ get: function () { return chunkORSDZTO4_cjs.mastraNativeVectorMutation; }
1859
2384
  });
1860
2385
  Object.defineProperty(exports, "mastraNativeVectorQuery", {
1861
2386
  enumerable: true,
1862
- get: function () { return chunkEEELVBWO_cjs.mastraNativeVectorQuery; }
2387
+ get: function () { return chunkORSDZTO4_cjs.mastraNativeVectorQuery; }
1863
2388
  });
1864
2389
  Object.defineProperty(exports, "mastraStorage", {
1865
2390
  enumerable: true,
1866
- get: function () { return chunkEEELVBWO_cjs.mastraStorage; }
2391
+ get: function () { return chunkORSDZTO4_cjs.mastraStorage; }
2392
+ });
2393
+ Object.defineProperty(exports, "TABLE_BACKGROUND_TASKS", {
2394
+ enumerable: true,
2395
+ get: function () { return chunkSFRHJGSM_cjs.TABLE_BACKGROUND_TASKS; }
2396
+ });
2397
+ Object.defineProperty(exports, "TABLE_CHANNEL_CONFIG", {
2398
+ enumerable: true,
2399
+ get: function () { return chunkSFRHJGSM_cjs.TABLE_CHANNEL_CONFIG; }
2400
+ });
2401
+ Object.defineProperty(exports, "TABLE_CHANNEL_INSTALLATIONS", {
2402
+ enumerable: true,
2403
+ get: function () { return chunkSFRHJGSM_cjs.TABLE_CHANNEL_INSTALLATIONS; }
1867
2404
  });
1868
2405
  Object.defineProperty(exports, "TABLE_MESSAGES", {
1869
2406
  enumerable: true,
1870
- get: function () { return chunkCV23JOCS_cjs.TABLE_MESSAGES; }
2407
+ get: function () { return chunkSFRHJGSM_cjs.TABLE_MESSAGES; }
1871
2408
  });
1872
2409
  Object.defineProperty(exports, "TABLE_RESOURCES", {
1873
2410
  enumerable: true,
1874
- get: function () { return chunkCV23JOCS_cjs.TABLE_RESOURCES; }
2411
+ get: function () { return chunkSFRHJGSM_cjs.TABLE_RESOURCES; }
2412
+ });
2413
+ Object.defineProperty(exports, "TABLE_SCHEDULES", {
2414
+ enumerable: true,
2415
+ get: function () { return chunkSFRHJGSM_cjs.TABLE_SCHEDULES; }
2416
+ });
2417
+ Object.defineProperty(exports, "TABLE_SCHEDULE_TRIGGERS", {
2418
+ enumerable: true,
2419
+ get: function () { return chunkSFRHJGSM_cjs.TABLE_SCHEDULE_TRIGGERS; }
1875
2420
  });
1876
2421
  Object.defineProperty(exports, "TABLE_SCORERS", {
1877
2422
  enumerable: true,
1878
- get: function () { return chunkCV23JOCS_cjs.TABLE_SCORERS; }
2423
+ get: function () { return chunkSFRHJGSM_cjs.TABLE_SCORERS; }
1879
2424
  });
1880
2425
  Object.defineProperty(exports, "TABLE_THREADS", {
1881
2426
  enumerable: true,
1882
- get: function () { return chunkCV23JOCS_cjs.TABLE_THREADS; }
2427
+ get: function () { return chunkSFRHJGSM_cjs.TABLE_THREADS; }
1883
2428
  });
1884
2429
  Object.defineProperty(exports, "TABLE_WORKFLOW_SNAPSHOT", {
1885
2430
  enumerable: true,
1886
- get: function () { return chunkCV23JOCS_cjs.TABLE_WORKFLOW_SNAPSHOT; }
2431
+ get: function () { return chunkSFRHJGSM_cjs.TABLE_WORKFLOW_SNAPSHOT; }
1887
2432
  });
1888
2433
  Object.defineProperty(exports, "defineMastraNativeVectorTable", {
1889
2434
  enumerable: true,
1890
- get: function () { return chunkCV23JOCS_cjs.defineMastraNativeVectorTable; }
2435
+ get: function () { return chunkSFRHJGSM_cjs.defineMastraNativeVectorTable; }
2436
+ });
2437
+ Object.defineProperty(exports, "mastraBackgroundTasksTable", {
2438
+ enumerable: true,
2439
+ get: function () { return chunkSFRHJGSM_cjs.mastraBackgroundTasksTable; }
1891
2440
  });
1892
2441
  Object.defineProperty(exports, "mastraCacheListItemsTable", {
1893
2442
  enumerable: true,
1894
- get: function () { return chunkCV23JOCS_cjs.mastraCacheListItemsTable; }
2443
+ get: function () { return chunkSFRHJGSM_cjs.mastraCacheListItemsTable; }
1895
2444
  });
1896
2445
  Object.defineProperty(exports, "mastraCacheTable", {
1897
2446
  enumerable: true,
1898
- get: function () { return chunkCV23JOCS_cjs.mastraCacheTable; }
2447
+ get: function () { return chunkSFRHJGSM_cjs.mastraCacheTable; }
2448
+ });
2449
+ Object.defineProperty(exports, "mastraChannelConfigTable", {
2450
+ enumerable: true,
2451
+ get: function () { return chunkSFRHJGSM_cjs.mastraChannelConfigTable; }
2452
+ });
2453
+ Object.defineProperty(exports, "mastraChannelInstallationsTable", {
2454
+ enumerable: true,
2455
+ get: function () { return chunkSFRHJGSM_cjs.mastraChannelInstallationsTable; }
1899
2456
  });
1900
2457
  Object.defineProperty(exports, "mastraDocumentsTable", {
1901
2458
  enumerable: true,
1902
- get: function () { return chunkCV23JOCS_cjs.mastraDocumentsTable; }
2459
+ get: function () { return chunkSFRHJGSM_cjs.mastraDocumentsTable; }
1903
2460
  });
1904
2461
  Object.defineProperty(exports, "mastraMessagesTable", {
1905
2462
  enumerable: true,
1906
- get: function () { return chunkCV23JOCS_cjs.mastraMessagesTable; }
2463
+ get: function () { return chunkSFRHJGSM_cjs.mastraMessagesTable; }
1907
2464
  });
1908
2465
  Object.defineProperty(exports, "mastraResourcesTable", {
1909
2466
  enumerable: true,
1910
- get: function () { return chunkCV23JOCS_cjs.mastraResourcesTable; }
2467
+ get: function () { return chunkSFRHJGSM_cjs.mastraResourcesTable; }
2468
+ });
2469
+ Object.defineProperty(exports, "mastraScheduleTriggersTable", {
2470
+ enumerable: true,
2471
+ get: function () { return chunkSFRHJGSM_cjs.mastraScheduleTriggersTable; }
2472
+ });
2473
+ Object.defineProperty(exports, "mastraSchedulesTable", {
2474
+ enumerable: true,
2475
+ get: function () { return chunkSFRHJGSM_cjs.mastraSchedulesTable; }
1911
2476
  });
1912
2477
  Object.defineProperty(exports, "mastraScoresTable", {
1913
2478
  enumerable: true,
1914
- get: function () { return chunkCV23JOCS_cjs.mastraScoresTable; }
2479
+ get: function () { return chunkSFRHJGSM_cjs.mastraScoresTable; }
1915
2480
  });
1916
2481
  Object.defineProperty(exports, "mastraThreadsTable", {
1917
2482
  enumerable: true,
1918
- get: function () { return chunkCV23JOCS_cjs.mastraThreadsTable; }
2483
+ get: function () { return chunkSFRHJGSM_cjs.mastraThreadsTable; }
1919
2484
  });
1920
2485
  Object.defineProperty(exports, "mastraVectorIndexesTable", {
1921
2486
  enumerable: true,
1922
- get: function () { return chunkCV23JOCS_cjs.mastraVectorIndexesTable; }
2487
+ get: function () { return chunkSFRHJGSM_cjs.mastraVectorIndexesTable; }
1923
2488
  });
1924
2489
  Object.defineProperty(exports, "mastraVectorsTable", {
1925
2490
  enumerable: true,
1926
- get: function () { return chunkCV23JOCS_cjs.mastraVectorsTable; }
2491
+ get: function () { return chunkSFRHJGSM_cjs.mastraVectorsTable; }
1927
2492
  });
1928
2493
  Object.defineProperty(exports, "mastraWorkflowSnapshotsTable", {
1929
2494
  enumerable: true,
1930
- get: function () { return chunkCV23JOCS_cjs.mastraWorkflowSnapshotsTable; }
2495
+ get: function () { return chunkSFRHJGSM_cjs.mastraWorkflowSnapshotsTable; }
1931
2496
  });
1932
2497
  exports.ConvexCacheClient = ConvexCacheClient;
1933
2498
  exports.ConvexNativeVector = ConvexNativeVector;