@mastra/pg 1.7.0 → 1.7.1

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
@@ -1439,11 +1439,9 @@ var PgVector = class extends MastraVector {
1439
1439
  WHERE c.relname = $1
1440
1440
  AND n.nspname = $2;
1441
1441
  `;
1442
- const [dimResult, countResult, indexResult] = await Promise.all([
1443
- client.query(dimensionQuery, [tableName]),
1444
- client.query(countQuery),
1445
- client.query(indexQuery, [`${indexName}_vector_idx`, this.schema || "public"])
1446
- ]);
1442
+ const dimResult = await client.query(dimensionQuery, [tableName]);
1443
+ const countResult = await client.query(countQuery);
1444
+ const indexResult = await client.query(indexQuery, [`${indexName}_vector_idx`, this.schema || "public"]);
1447
1445
  const { index_method, index_def, operator_class } = indexResult.rows[0] || {
1448
1446
  index_method: "flat",
1449
1447
  index_def: "",
@@ -6723,7 +6721,8 @@ var MemoryPG = class _MemoryPG extends MemoryStorage {
6723
6721
  "isBufferingObservation",
6724
6722
  "isBufferingReflection",
6725
6723
  "lastBufferedAtTokens",
6726
- "lastBufferedAtTime"
6724
+ "lastBufferedAtTime",
6725
+ "metadata"
6727
6726
  ]
6728
6727
  });
6729
6728
  }
@@ -7897,9 +7896,11 @@ var MemoryPG = class _MemoryPG extends MemoryStorage {
7897
7896
  ]
7898
7897
  );
7899
7898
  const clonedMessages = [];
7899
+ const messageIdMap = {};
7900
7900
  const targetResourceId = resourceId || sourceThread.resourceId;
7901
7901
  for (const sourceMsg of sourceMessages) {
7902
7902
  const newMessageId = crypto.randomUUID();
7903
+ messageIdMap[sourceMsg.id] = newMessageId;
7903
7904
  const normalizedMsg = this.normalizeMessageRow(sourceMsg);
7904
7905
  let parsedContent = normalizedMsg.content;
7905
7906
  try {
@@ -7932,7 +7933,8 @@ var MemoryPG = class _MemoryPG extends MemoryStorage {
7932
7933
  }
7933
7934
  return {
7934
7935
  thread: newThread,
7935
- clonedMessages
7936
+ clonedMessages,
7937
+ messageIdMap
7936
7938
  };
7937
7939
  });
7938
7940
  } catch (error) {
@@ -8142,6 +8144,80 @@ var MemoryPG = class _MemoryPG extends MemoryStorage {
8142
8144
  );
8143
8145
  }
8144
8146
  }
8147
+ async insertObservationalMemoryRecord(record) {
8148
+ try {
8149
+ const lookupKey = this.getOMKey(record.threadId, record.resourceId);
8150
+ const tableName = getTableName3({
8151
+ indexName: OM_TABLE,
8152
+ schemaName: getSchemaName3(this.#schema)
8153
+ });
8154
+ const lastObservedAtStr = record.lastObservedAt ? record.lastObservedAt.toISOString() : null;
8155
+ const lastBufferedAtTimeStr = record.lastBufferedAtTime ? record.lastBufferedAtTime.toISOString() : null;
8156
+ await this.#db.client.none(
8157
+ `INSERT INTO ${tableName} (
8158
+ id, "lookupKey", scope, "resourceId", "threadId",
8159
+ "activeObservations", "activeObservationsPendingUpdate",
8160
+ "originType", config, "generationCount", "lastObservedAt", "lastObservedAtZ", "lastReflectionAt", "lastReflectionAtZ",
8161
+ "pendingMessageTokens", "totalTokensObserved", "observationTokenCount",
8162
+ "observedMessageIds", "bufferedObservationChunks",
8163
+ "bufferedReflection", "bufferedReflectionTokens", "bufferedReflectionInputTokens",
8164
+ "reflectedObservationLineCount",
8165
+ "isObserving", "isReflecting", "isBufferingObservation", "isBufferingReflection",
8166
+ "lastBufferedAtTokens", "lastBufferedAtTime",
8167
+ "observedTimezone", metadata, "createdAt", "createdAtZ", "updatedAt", "updatedAtZ"
8168
+ ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26, $27, $28, $29, $30, $31, $32, $33, $34, $35)`,
8169
+ [
8170
+ record.id,
8171
+ lookupKey,
8172
+ record.scope,
8173
+ record.resourceId,
8174
+ record.threadId || null,
8175
+ record.activeObservations || "",
8176
+ null,
8177
+ record.originType || "initial",
8178
+ record.config ? JSON.stringify(record.config) : null,
8179
+ record.generationCount || 0,
8180
+ lastObservedAtStr,
8181
+ lastObservedAtStr,
8182
+ null,
8183
+ // lastReflectionAt
8184
+ null,
8185
+ // lastReflectionAtZ
8186
+ record.pendingMessageTokens || 0,
8187
+ record.totalTokensObserved || 0,
8188
+ record.observationTokenCount || 0,
8189
+ record.observedMessageIds ? JSON.stringify(record.observedMessageIds) : null,
8190
+ record.bufferedObservationChunks ? JSON.stringify(record.bufferedObservationChunks) : null,
8191
+ record.bufferedReflection || null,
8192
+ record.bufferedReflectionTokens ?? null,
8193
+ record.bufferedReflectionInputTokens ?? null,
8194
+ record.reflectedObservationLineCount ?? null,
8195
+ record.isObserving || false,
8196
+ record.isReflecting || false,
8197
+ record.isBufferingObservation || false,
8198
+ record.isBufferingReflection || false,
8199
+ record.lastBufferedAtTokens || 0,
8200
+ lastBufferedAtTimeStr,
8201
+ record.observedTimezone || null,
8202
+ record.metadata ? JSON.stringify(record.metadata) : null,
8203
+ record.createdAt.toISOString(),
8204
+ record.createdAt.toISOString(),
8205
+ record.updatedAt.toISOString(),
8206
+ record.updatedAt.toISOString()
8207
+ ]
8208
+ );
8209
+ } catch (error) {
8210
+ throw new MastraError(
8211
+ {
8212
+ id: createStorageErrorId("PG", "INSERT_OBSERVATIONAL_MEMORY_RECORD", "FAILED"),
8213
+ domain: ErrorDomain.STORAGE,
8214
+ category: ErrorCategory.THIRD_PARTY,
8215
+ details: { id: record.id, threadId: record.threadId, resourceId: record.resourceId }
8216
+ },
8217
+ error
8218
+ );
8219
+ }
8220
+ }
8145
8221
  async updateActiveObservations(input) {
8146
8222
  try {
8147
8223
  const now = /* @__PURE__ */ new Date();
@@ -8242,8 +8318,8 @@ var MemoryPG = class _MemoryPG extends MemoryStorage {
8242
8318
  "originType", config, "generationCount", "lastObservedAt", "lastObservedAtZ", "lastReflectionAt", "lastReflectionAtZ",
8243
8319
  "pendingMessageTokens", "totalTokensObserved", "observationTokenCount",
8244
8320
  "isObserving", "isReflecting", "isBufferingObservation", "isBufferingReflection", "lastBufferedAtTokens", "lastBufferedAtTime",
8245
- "observedTimezone", "createdAt", "createdAtZ", "updatedAt", "updatedAtZ"
8246
- ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26, $27, $28)`,
8321
+ "observedTimezone", metadata, "createdAt", "createdAtZ", "updatedAt", "updatedAtZ"
8322
+ ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26, $27, $28, $29)`,
8247
8323
  [
8248
8324
  id,
8249
8325
  lookupKey,
@@ -8279,6 +8355,7 @@ var MemoryPG = class _MemoryPG extends MemoryStorage {
8279
8355
  null,
8280
8356
  // lastBufferedAtTime
8281
8357
  record.observedTimezone || null,
8358
+ record.metadata ? JSON.stringify(record.metadata) : null,
8282
8359
  nowStr,
8283
8360
  // createdAt
8284
8361
  nowStr,
@@ -11889,6 +11966,9 @@ var WorkflowsPG = class _WorkflowsPG extends WorkflowsStorage {
11889
11966
  this.#skipDefaultIndexes = skipDefaultIndexes;
11890
11967
  this.#indexes = indexes?.filter((idx) => _WorkflowsPG.MANAGED_TABLES.includes(idx.table));
11891
11968
  }
11969
+ supportsConcurrentUpdates() {
11970
+ return true;
11971
+ }
11892
11972
  parseWorkflowRun(row) {
11893
11973
  let parsedSnapshot = row.snapshot;
11894
11974
  if (typeof parsedSnapshot === "string") {
@@ -11967,21 +12047,111 @@ var WorkflowsPG = class _WorkflowsPG extends WorkflowsStorage {
11967
12047
  async dangerouslyClearAll() {
11968
12048
  await this.#db.clearTable({ tableName: TABLE_WORKFLOW_SNAPSHOT });
11969
12049
  }
11970
- updateWorkflowResults({
11971
- // workflowName,
11972
- // runId,
11973
- // stepId,
11974
- // result,
11975
- // requestContext,
12050
+ async updateWorkflowResults({
12051
+ workflowName,
12052
+ runId,
12053
+ stepId,
12054
+ result,
12055
+ requestContext
11976
12056
  }) {
11977
- throw new Error("Method not implemented.");
12057
+ try {
12058
+ return await this.#db.client.tx(async (t) => {
12059
+ const tableName = getTableName5({ indexName: TABLE_WORKFLOW_SNAPSHOT, schemaName: getSchemaName5(this.#schema) });
12060
+ const existingSnapshotResult = await t.oneOrNone(
12061
+ `SELECT snapshot FROM ${tableName} WHERE workflow_name = $1 AND run_id = $2 FOR UPDATE`,
12062
+ [workflowName, runId]
12063
+ );
12064
+ let snapshot;
12065
+ if (!existingSnapshotResult) {
12066
+ snapshot = {
12067
+ context: {},
12068
+ activePaths: [],
12069
+ timestamp: Date.now(),
12070
+ suspendedPaths: {},
12071
+ activeStepsPath: {},
12072
+ resumeLabels: {},
12073
+ serializedStepGraph: [],
12074
+ status: "pending",
12075
+ value: {},
12076
+ waitingPaths: {},
12077
+ runId,
12078
+ requestContext: {}
12079
+ };
12080
+ } else {
12081
+ const existingSnapshot = existingSnapshotResult.snapshot;
12082
+ snapshot = typeof existingSnapshot === "string" ? JSON.parse(existingSnapshot) : existingSnapshot;
12083
+ }
12084
+ snapshot.context[stepId] = result;
12085
+ snapshot.requestContext = { ...snapshot.requestContext, ...requestContext };
12086
+ const now = /* @__PURE__ */ new Date();
12087
+ const sanitizedSnapshot = sanitizeJsonForPg(JSON.stringify(snapshot));
12088
+ await t.none(
12089
+ `INSERT INTO ${tableName} (workflow_name, run_id, snapshot, "createdAt", "updatedAt")
12090
+ VALUES ($1, $2, $3, $4, $5)
12091
+ ON CONFLICT (workflow_name, run_id) DO UPDATE
12092
+ SET snapshot = $3, "updatedAt" = $5`,
12093
+ [workflowName, runId, sanitizedSnapshot, now, now]
12094
+ );
12095
+ return snapshot.context;
12096
+ });
12097
+ } catch (error) {
12098
+ throw new MastraError(
12099
+ {
12100
+ id: createStorageErrorId("PG", "UPDATE_WORKFLOW_RESULTS", "FAILED"),
12101
+ domain: ErrorDomain.STORAGE,
12102
+ category: ErrorCategory.THIRD_PARTY,
12103
+ details: {
12104
+ workflowName,
12105
+ runId,
12106
+ stepId
12107
+ }
12108
+ },
12109
+ error
12110
+ );
12111
+ }
11978
12112
  }
11979
- updateWorkflowState({
11980
- // workflowName,
11981
- // runId,
11982
- // opts,
12113
+ async updateWorkflowState({
12114
+ workflowName,
12115
+ runId,
12116
+ opts
11983
12117
  }) {
11984
- throw new Error("Method not implemented.");
12118
+ try {
12119
+ return await this.#db.client.tx(async (t) => {
12120
+ const tableName = getTableName5({ indexName: TABLE_WORKFLOW_SNAPSHOT, schemaName: getSchemaName5(this.#schema) });
12121
+ const existingSnapshotResult = await t.oneOrNone(
12122
+ `SELECT snapshot FROM ${tableName} WHERE workflow_name = $1 AND run_id = $2 FOR UPDATE`,
12123
+ [workflowName, runId]
12124
+ );
12125
+ if (!existingSnapshotResult) {
12126
+ return void 0;
12127
+ }
12128
+ const existingSnapshot = existingSnapshotResult.snapshot;
12129
+ const snapshot = typeof existingSnapshot === "string" ? JSON.parse(existingSnapshot) : existingSnapshot;
12130
+ if (!snapshot || !snapshot?.context) {
12131
+ throw new Error(`Snapshot not found for runId ${runId}`);
12132
+ }
12133
+ const updatedSnapshot = { ...snapshot, ...opts };
12134
+ const sanitizedSnapshot = sanitizeJsonForPg(JSON.stringify(updatedSnapshot));
12135
+ await t.none(
12136
+ `UPDATE ${tableName} SET snapshot = $1, "updatedAt" = $2 WHERE workflow_name = $3 AND run_id = $4`,
12137
+ [sanitizedSnapshot, /* @__PURE__ */ new Date(), workflowName, runId]
12138
+ );
12139
+ return updatedSnapshot;
12140
+ });
12141
+ } catch (error) {
12142
+ throw new MastraError(
12143
+ {
12144
+ id: createStorageErrorId("PG", "UPDATE_WORKFLOW_STATE", "FAILED"),
12145
+ domain: ErrorDomain.STORAGE,
12146
+ category: ErrorCategory.THIRD_PARTY,
12147
+ details: {
12148
+ workflowName,
12149
+ runId
12150
+ }
12151
+ },
12152
+ error
12153
+ );
12154
+ }
11985
12155
  }
11986
12156
  async persistWorkflowSnapshot({
11987
12157
  workflowName,