@mastra/convex 1.7.2-alpha.0 → 1.7.3-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.
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { a as mastraCache, i as mastraStorage, n as mastraNativeVectorMutation, r as mastraNativeVectorQuery, t as mastraNativeVectorAction } from "./server-D3SG5LQf.js";
1
+ import { a as mastraCache, i as mastraStorage, n as mastraNativeVectorMutation, r as mastraNativeVectorQuery, t as mastraNativeVectorAction } from "./server-DozOFGfO.js";
2
2
  import { TABLE_BACKGROUND_TASKS, TABLE_CHANNEL_CONFIG, TABLE_CHANNEL_INSTALLATIONS, TABLE_MESSAGES, TABLE_OBSERVATIONAL_MEMORY, TABLE_RESOURCES, TABLE_SCHEDULES, TABLE_SCHEDULE_TRIGGERS, TABLE_SCORERS, TABLE_THREADS, TABLE_WORKFLOW_SNAPSHOT, defineMastraNativeVectorTable, mastraBackgroundTasksTable, mastraCacheListItemsTable, mastraCacheTable, mastraChannelConfigTable, mastraChannelInstallationsTable, mastraDocumentsTable, mastraMessagesTable, mastraObservationalMemoryTable, mastraResourcesTable, mastraScheduleTriggersTable, mastraSchedulesTable, mastraScoresTable, mastraThreadsTable, mastraVectorIndexesTable, mastraVectorsTable, mastraWorkflowSnapshotsTable } from "./schema.js";
3
3
  import { MastraServerCache } from "@mastra/core/cache";
4
4
  import { BackgroundTasksStorage, ChannelsStorage, MastraCompositeStore, MemoryStorage, SchedulesStorage, ScoresStorage, TABLE_BACKGROUND_TASKS as TABLE_BACKGROUND_TASKS$1, TABLE_CHANNEL_CONFIG as TABLE_CHANNEL_CONFIG$1, TABLE_CHANNEL_INSTALLATIONS as TABLE_CHANNEL_INSTALLATIONS$1, TABLE_MESSAGES as TABLE_MESSAGES$1, TABLE_RESOURCES as TABLE_RESOURCES$1, TABLE_SCHEDULES as TABLE_SCHEDULES$1, TABLE_SCHEDULE_TRIGGERS as TABLE_SCHEDULE_TRIGGERS$1, TABLE_SCORERS as TABLE_SCORERS$1, TABLE_THREADS as TABLE_THREADS$1, TABLE_WORKFLOW_SNAPSHOT as TABLE_WORKFLOW_SNAPSHOT$1, WorkflowsStorage, calculatePagination, createStorageErrorId, createVectorErrorId, filterByDateRange, normalizePerPage, normalizeScheduleTarget, safelyParseJSON, storageMessageMatchesMetadataFilter, validateStorageMetadataFilter } from "@mastra/core/storage";
@@ -487,15 +487,43 @@ var ConvexDB = class extends MastraBase {
487
487
  ...args
488
488
  });
489
489
  }
490
- async omUpdateActive(args) {
490
+ async omInitialize(record) {
491
+ return this.client.callStorage({
492
+ op: "omInitialize",
493
+ tableName: TABLE_OBSERVATIONAL_MEMORY$1,
494
+ record
495
+ });
496
+ }
497
+ async omCreateReflectionGeneration(args) {
498
+ return this.client.callStorage({
499
+ op: "omCreateReflectionGeneration",
500
+ tableName: TABLE_OBSERVATIONAL_MEMORY$1,
501
+ ...args
502
+ });
503
+ }
504
+ async omSetPendingMessageTokens(args) {
505
+ await this.client.callStorage({
506
+ op: "omSetPendingMessageTokens",
507
+ tableName: TABLE_OBSERVATIONAL_MEMORY$1,
508
+ ...args
509
+ });
510
+ }
511
+ async omSetBufferingObservationFlag(args) {
491
512
  await this.client.callStorage({
513
+ op: "omSetBufferingObservationFlag",
514
+ tableName: TABLE_OBSERVATIONAL_MEMORY$1,
515
+ ...args
516
+ });
517
+ }
518
+ async omUpdateActive(args) {
519
+ return this.client.callStorage({
492
520
  op: "omUpdateActive",
493
521
  tableName: TABLE_OBSERVATIONAL_MEMORY$1,
494
522
  ...args
495
523
  });
496
524
  }
497
525
  async omAppendBufferedChunk(args) {
498
- await this.client.callStorage({
526
+ return this.client.callStorage({
499
527
  op: "omAppendBufferedChunk",
500
528
  tableName: TABLE_OBSERVATIONAL_MEMORY$1,
501
529
  ...args
@@ -900,6 +928,17 @@ var ChannelsConvex = class extends ChannelsStorage {
900
928
  }
901
929
  };
902
930
  //#endregion
931
+ //#region ../../packages/_internal-core/dist/memory/index.js
932
+ /**
933
+ * Backs `updateActiveObservations` in adapters that implement `commitActiveObservations`.
934
+ * Callers of `updateActiveObservations` (which resolves `void`) can't see a commit result, so a
935
+ * write that was not applied throws instead of resolving as if it had committed.
936
+ */
937
+ function assertActiveObservationsApplied(result, id) {
938
+ if (result.applied) return;
939
+ throw new Error(result.reason === "retired" ? `Observational memory record ${id} was superseded by a newer generation; observations were not written` : `Observational memory record ${id} changed since the observations were composed; observations were not written`);
940
+ }
941
+ //#endregion
903
942
  //#region src/storage/domains/memory/index.ts
904
943
  function parseStoredThread(row) {
905
944
  return {
@@ -975,7 +1014,8 @@ function parseStoredOMRecord(doc) {
975
1014
  config: config ?? {},
976
1015
  metadata: metadata ?? void 0,
977
1016
  observedMessageIds: doc.observedMessageIds || void 0,
978
- observedTimezone: doc.observedTimezone || void 0
1017
+ observedTimezone: doc.observedTimezone || void 0,
1018
+ supersededBy: doc.supersededBy ?? null
979
1019
  };
980
1020
  }
981
1021
  var MemoryConvex = class extends MemoryStorage {
@@ -1463,9 +1503,9 @@ var MemoryConvex = class extends MemoryStorage {
1463
1503
  })).map((doc) => parseStoredOMRecord(doc));
1464
1504
  }
1465
1505
  async initializeObservationalMemory(input) {
1466
- const id = crypto.randomUUID();
1467
1506
  const now = /* @__PURE__ */ new Date();
1468
1507
  const lookupKey = this.getOMKey(input.threadId, input.resourceId);
1508
+ const id = crypto.randomUUID();
1469
1509
  const record = {
1470
1510
  id,
1471
1511
  scope: input.scope,
@@ -1487,38 +1527,36 @@ var MemoryConvex = class extends MemoryStorage {
1487
1527
  lastBufferedAtTokens: 0,
1488
1528
  lastBufferedAtTime: null,
1489
1529
  config: input.config,
1490
- observedTimezone: input.observedTimezone
1530
+ observedTimezone: input.observedTimezone,
1531
+ supersededBy: null
1491
1532
  };
1492
- await this.#db.insert({
1493
- tableName: TABLE_OBSERVATIONAL_MEMORY$1,
1494
- record: {
1495
- id,
1496
- lookupKey,
1497
- scope: input.scope,
1498
- resourceId: input.resourceId,
1499
- threadId: input.threadId || null,
1500
- activeObservations: "",
1501
- activeObservationsPendingUpdate: null,
1502
- originType: "initial",
1503
- config: JSON.stringify(input.config ?? {}),
1504
- generationCount: 0,
1505
- lastObservedAt: null,
1506
- lastReflectionAt: null,
1507
- pendingMessageTokens: 0,
1508
- totalTokensObserved: 0,
1509
- observationTokenCount: 0,
1510
- isObserving: false,
1511
- isReflecting: false,
1512
- isBufferingObservation: false,
1513
- isBufferingReflection: false,
1514
- lastBufferedAtTokens: 0,
1515
- lastBufferedAtTime: null,
1516
- observedTimezone: input.observedTimezone || null,
1517
- createdAt: now.toISOString(),
1518
- updatedAt: now.toISOString()
1519
- }
1533
+ const doc = await this.#db.omInitialize({
1534
+ id,
1535
+ lookupKey,
1536
+ scope: input.scope,
1537
+ resourceId: input.resourceId,
1538
+ threadId: input.threadId || null,
1539
+ activeObservations: "",
1540
+ activeObservationsPendingUpdate: null,
1541
+ originType: "initial",
1542
+ config: JSON.stringify(input.config ?? {}),
1543
+ generationCount: 0,
1544
+ lastObservedAt: null,
1545
+ lastReflectionAt: null,
1546
+ pendingMessageTokens: 0,
1547
+ totalTokensObserved: 0,
1548
+ observationTokenCount: 0,
1549
+ isObserving: false,
1550
+ isReflecting: false,
1551
+ isBufferingObservation: false,
1552
+ isBufferingReflection: false,
1553
+ lastBufferedAtTokens: 0,
1554
+ lastBufferedAtTime: null,
1555
+ observedTimezone: input.observedTimezone || null,
1556
+ createdAt: now.toISOString(),
1557
+ updatedAt: now.toISOString()
1520
1558
  });
1521
- return record;
1559
+ return doc ? parseStoredOMRecord(doc) : record;
1522
1560
  }
1523
1561
  async insertObservationalMemoryRecord(record) {
1524
1562
  const lookupKey = this.getOMKey(record.threadId, record.resourceId);
@@ -1554,22 +1592,30 @@ var MemoryConvex = class extends MemoryStorage {
1554
1592
  lastBufferedAtTime: record.lastBufferedAtTime ? toISO(record.lastBufferedAtTime) : null,
1555
1593
  observedTimezone: record.observedTimezone || null,
1556
1594
  metadata: record.metadata ? JSON.stringify(record.metadata) : null,
1595
+ supersededBy: record.supersededBy ?? null,
1557
1596
  createdAt: toISO(record.createdAt),
1558
1597
  updatedAt: toISO(record.updatedAt)
1559
1598
  }
1560
1599
  });
1561
1600
  }
1562
1601
  async updateActiveObservations(input) {
1563
- await this.#db.omUpdateActive({
1602
+ assertActiveObservationsApplied(await this.commitActiveObservations(input), input.id);
1603
+ }
1604
+ async commitActiveObservations(input) {
1605
+ return await this.#db.omUpdateActive({
1564
1606
  id: input.id,
1565
1607
  observations: input.observations,
1566
1608
  tokenCount: input.tokenCount,
1567
1609
  lastObservedAt: toISO(input.lastObservedAt),
1568
1610
  observedMessageIds: input.observedMessageIds ?? null,
1569
- updatedAt: (/* @__PURE__ */ new Date()).toISOString()
1570
- });
1611
+ updatedAt: (/* @__PURE__ */ new Date()).toISOString(),
1612
+ ...input.expectedActiveObservations !== void 0 ? { expectedActiveObservations: input.expectedActiveObservations } : {}
1613
+ }) ?? { applied: true };
1571
1614
  }
1572
1615
  async updateBufferedObservations(input) {
1616
+ await this.appendBufferedObservations(input);
1617
+ }
1618
+ async appendBufferedObservations(input) {
1573
1619
  const chunk = {
1574
1620
  id: `ombuf-${crypto.randomUUID()}`,
1575
1621
  cycleId: input.chunk.cycleId,
@@ -1585,12 +1631,15 @@ var MemoryConvex = class extends MemoryStorage {
1585
1631
  extractedValues: input.chunk.extractedValues,
1586
1632
  extractionFailures: input.chunk.extractionFailures
1587
1633
  };
1588
- await this.#db.omAppendBufferedChunk({
1634
+ return await this.#db.omAppendBufferedChunk({
1589
1635
  id: input.id,
1590
1636
  chunk,
1591
1637
  lastBufferedAtTime: input.lastBufferedAtTime ? toISO(input.lastBufferedAtTime) : void 0,
1592
1638
  updatedAt: (/* @__PURE__ */ new Date()).toISOString()
1593
- });
1639
+ }) ?? {
1640
+ persisted: true,
1641
+ recordId: input.id
1642
+ };
1594
1643
  }
1595
1644
  async swapBufferedToActive(input) {
1596
1645
  return this.#db.omSwapBuffered({
@@ -1604,65 +1653,33 @@ var MemoryConvex = class extends MemoryStorage {
1604
1653
  now: (/* @__PURE__ */ new Date()).toISOString()
1605
1654
  });
1606
1655
  }
1607
- async createReflectionGeneration(input) {
1608
- const id = crypto.randomUUID();
1609
- const now = /* @__PURE__ */ new Date();
1610
- const lookupKey = this.getOMKey(input.currentRecord.threadId, input.currentRecord.resourceId);
1611
- const record = {
1612
- id,
1613
- scope: input.currentRecord.scope,
1614
- threadId: input.currentRecord.threadId,
1615
- resourceId: input.currentRecord.resourceId,
1616
- createdAt: now,
1617
- updatedAt: now,
1618
- lastObservedAt: input.currentRecord.lastObservedAt,
1619
- originType: "reflection",
1620
- generationCount: input.currentRecord.generationCount + 1,
1621
- activeObservations: input.reflection,
1622
- totalTokensObserved: input.currentRecord.totalTokensObserved,
1623
- observationTokenCount: input.tokenCount,
1624
- pendingMessageTokens: 0,
1625
- isReflecting: false,
1626
- isObserving: false,
1627
- isBufferingObservation: false,
1628
- isBufferingReflection: false,
1629
- lastBufferedAtTokens: 0,
1630
- lastBufferedAtTime: null,
1631
- config: input.currentRecord.config,
1632
- metadata: input.currentRecord.metadata,
1633
- observedTimezone: input.currentRecord.observedTimezone
1656
+ /** The caller's snapshot as the server's rollover ops expect it. */
1657
+ #serializeCurrentRecord(currentRecord) {
1658
+ return {
1659
+ id: currentRecord.id,
1660
+ lookupKey: this.getOMKey(currentRecord.threadId, currentRecord.resourceId),
1661
+ scope: currentRecord.scope,
1662
+ threadId: currentRecord.threadId || null,
1663
+ resourceId: currentRecord.resourceId,
1664
+ config: JSON.stringify(currentRecord.config ?? {}),
1665
+ metadata: currentRecord.metadata ? JSON.stringify(currentRecord.metadata) : null,
1666
+ observedTimezone: currentRecord.observedTimezone || null,
1667
+ lastObservedAt: currentRecord.lastObservedAt ? toISO(currentRecord.lastObservedAt) : null,
1668
+ totalTokensObserved: currentRecord.totalTokensObserved,
1669
+ generationCount: currentRecord.generationCount,
1670
+ activeObservations: currentRecord.activeObservations ?? "",
1671
+ observationTokenCount: currentRecord.observationTokenCount ?? 0
1634
1672
  };
1635
- await this.#db.insert({
1636
- tableName: TABLE_OBSERVATIONAL_MEMORY$1,
1637
- record: {
1638
- id,
1639
- lookupKey,
1640
- scope: record.scope,
1641
- resourceId: record.resourceId,
1642
- threadId: record.threadId || null,
1643
- activeObservations: input.reflection,
1644
- activeObservationsPendingUpdate: null,
1645
- originType: "reflection",
1646
- config: JSON.stringify(record.config ?? {}),
1647
- generationCount: record.generationCount,
1648
- lastObservedAt: record.lastObservedAt ? toISO(record.lastObservedAt) : null,
1649
- lastReflectionAt: now.toISOString(),
1650
- pendingMessageTokens: 0,
1651
- totalTokensObserved: record.totalTokensObserved,
1652
- observationTokenCount: record.observationTokenCount,
1653
- isObserving: false,
1654
- isReflecting: false,
1655
- isBufferingObservation: false,
1656
- isBufferingReflection: false,
1657
- lastBufferedAtTokens: 0,
1658
- lastBufferedAtTime: null,
1659
- observedTimezone: record.observedTimezone || null,
1660
- metadata: record.metadata ? JSON.stringify(record.metadata) : null,
1661
- createdAt: now.toISOString(),
1662
- updatedAt: now.toISOString()
1663
- }
1673
+ }
1674
+ async createReflectionGeneration(input) {
1675
+ const doc = await this.#db.omCreateReflectionGeneration({
1676
+ currentRecord: this.#serializeCurrentRecord(input.currentRecord),
1677
+ newId: input.newRecordId ?? crypto.randomUUID(),
1678
+ reflection: input.reflection,
1679
+ tokenCount: input.tokenCount,
1680
+ now: (/* @__PURE__ */ new Date()).toISOString()
1664
1681
  });
1665
- return record;
1682
+ return doc ? parseStoredOMRecord(doc) : input.currentRecord;
1666
1683
  }
1667
1684
  async updateBufferedReflection(input) {
1668
1685
  await this.#db.omUpdateBufferedReflection({
@@ -1675,26 +1692,13 @@ var MemoryConvex = class extends MemoryStorage {
1675
1692
  });
1676
1693
  }
1677
1694
  async swapBufferedReflectionToActive(input) {
1678
- const { currentRecord } = input;
1679
- const serializedCurrentRecord = {
1680
- id: currentRecord.id,
1681
- lookupKey: this.getOMKey(currentRecord.threadId, currentRecord.resourceId),
1682
- scope: currentRecord.scope,
1683
- threadId: currentRecord.threadId || null,
1684
- resourceId: currentRecord.resourceId,
1685
- config: JSON.stringify(currentRecord.config ?? {}),
1686
- metadata: currentRecord.metadata ? JSON.stringify(currentRecord.metadata) : null,
1687
- observedTimezone: currentRecord.observedTimezone || null,
1688
- lastObservedAt: currentRecord.lastObservedAt ? toISO(currentRecord.lastObservedAt) : null,
1689
- totalTokensObserved: currentRecord.totalTokensObserved,
1690
- generationCount: currentRecord.generationCount
1691
- };
1692
- return parseStoredOMRecord(await this.#db.omSwapBufferedReflection({
1693
- currentRecord: serializedCurrentRecord,
1694
- newId: crypto.randomUUID(),
1695
+ const doc = await this.#db.omSwapBufferedReflection({
1696
+ currentRecord: this.#serializeCurrentRecord(input.currentRecord),
1697
+ newId: input.newRecordId ?? crypto.randomUUID(),
1695
1698
  tokenCount: input.tokenCount,
1696
1699
  now: (/* @__PURE__ */ new Date()).toISOString()
1697
- }));
1700
+ });
1701
+ return doc ? parseStoredOMRecord(doc) : input.currentRecord;
1698
1702
  }
1699
1703
  async setReflectingFlag(id, isReflecting) {
1700
1704
  if (!await this.#db.patch({
@@ -1717,15 +1721,12 @@ var MemoryConvex = class extends MemoryStorage {
1717
1721
  })) throw this.omRecordNotFound("SET_OBSERVING_FLAG", id);
1718
1722
  }
1719
1723
  async setBufferingObservationFlag(id, isBuffering, lastBufferedAtTokens) {
1720
- if (!await this.#db.patch({
1721
- tableName: "mastra_observational_memory",
1724
+ await this.#db.omSetBufferingObservationFlag({
1722
1725
  id,
1723
- record: {
1724
- isBufferingObservation: isBuffering,
1725
- ...lastBufferedAtTokens !== void 0 ? { lastBufferedAtTokens } : {},
1726
- updatedAt: /* @__PURE__ */ new Date()
1727
- }
1728
- })) throw this.omRecordNotFound("SET_BUFFERING_OBSERVATION_FLAG", id);
1726
+ isBuffering,
1727
+ ...lastBufferedAtTokens !== void 0 ? { lastBufferedAtTokens } : {},
1728
+ updatedAt: (/* @__PURE__ */ new Date()).toISOString()
1729
+ });
1729
1730
  }
1730
1731
  async setBufferingReflectionFlag(id, isBuffering) {
1731
1732
  if (!await this.#db.patch({
@@ -1748,14 +1749,11 @@ var MemoryConvex = class extends MemoryStorage {
1748
1749
  tokenCount
1749
1750
  }
1750
1751
  });
1751
- if (!await this.#db.patch({
1752
- tableName: "mastra_observational_memory",
1752
+ await this.#db.omSetPendingMessageTokens({
1753
1753
  id,
1754
- record: {
1755
- pendingMessageTokens: tokenCount,
1756
- updatedAt: /* @__PURE__ */ new Date()
1757
- }
1758
- })) throw this.omRecordNotFound("SET_PENDING_MESSAGE_TOKENS", id);
1754
+ tokenCount,
1755
+ updatedAt: (/* @__PURE__ */ new Date()).toISOString()
1756
+ });
1759
1757
  }
1760
1758
  async clearObservationalMemory(threadId, resourceId) {
1761
1759
  const lookupKey = this.getOMKey(threadId, resourceId);