@mastra/spanner 1.6.7-alpha.0 → 1.7.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
@@ -6958,6 +6958,88 @@ var MemorySpanner = class MemorySpanner extends MemoryStorage {
6958
6958
  }, error);
6959
6959
  }
6960
6960
  }
6961
+ /**
6962
+ * Atomically reassign a thread and all of its messages to a different resource.
6963
+ *
6964
+ * Runs inside a single read-write transaction: the thread row is read inside the
6965
+ * transaction (acquiring a lock), then the thread and every message are moved together.
6966
+ * Overlapping transfers of the same thread serialize on the transaction, so they can never
6967
+ * interleave the thread update with the message update — either both move or neither does,
6968
+ * with no split-ownership window. The thread's `createdAt` is preserved. Callers are
6969
+ * responsible for authorizing the reassignment.
6970
+ */
6971
+ async updateThreadResourceId({ threadId, resourceId }) {
6972
+ const tableThreads = quoteIdent(TABLE_THREADS, "table name");
6973
+ const messagesTable = quoteIdent(TABLE_MESSAGES, "table name");
6974
+ const now = /* @__PURE__ */ new Date();
6975
+ let existingThread = null;
6976
+ let noop = false;
6977
+ try {
6978
+ await this.db.runWithAbortRetry(() => this.database.runTransactionAsync(async (tx) => {
6979
+ try {
6980
+ const [rows] = await tx.run({
6981
+ sql: `SELECT * FROM ${tableThreads} WHERE id = @threadId LIMIT 1`,
6982
+ params: { threadId },
6983
+ json: true
6984
+ });
6985
+ const row = rows[0];
6986
+ if (!row) throw new MastraError({
6987
+ id: createStorageErrorId("SPANNER", "UPDATE_THREAD_RESOURCE_ID", "NOT_FOUND"),
6988
+ domain: ErrorDomain.STORAGE,
6989
+ category: ErrorCategory.USER,
6990
+ text: `Thread ${threadId} not found`,
6991
+ details: { threadId }
6992
+ });
6993
+ existingThread = this.formatThreadRow(row);
6994
+ if (existingThread.resourceId === resourceId) {
6995
+ noop = true;
6996
+ await tx.commit();
6997
+ return;
6998
+ }
6999
+ await this.db.update({
7000
+ tableName: TABLE_THREADS,
7001
+ keys: { id: threadId },
7002
+ data: {
7003
+ resourceId,
7004
+ updatedAt: now
7005
+ },
7006
+ transaction: tx
7007
+ });
7008
+ await tx.runUpdate({
7009
+ sql: `UPDATE ${messagesTable} SET ${quoteIdent("resourceId", "column name")} = @resourceId WHERE ${quoteIdent("thread_id", "column name")} = @threadId`,
7010
+ params: {
7011
+ resourceId,
7012
+ threadId
7013
+ }
7014
+ });
7015
+ await tx.commit();
7016
+ } catch (err) {
7017
+ await tx.rollback().catch((rollbackErr) => {
7018
+ throw new AggregateError([err, rollbackErr], "Transaction and rollback both failed");
7019
+ });
7020
+ throw err;
7021
+ }
7022
+ }));
7023
+ const thread = existingThread;
7024
+ if (noop) return thread;
7025
+ return {
7026
+ ...thread,
7027
+ resourceId,
7028
+ updatedAt: now
7029
+ };
7030
+ } catch (error) {
7031
+ if (error instanceof MastraError) throw error;
7032
+ throw new MastraError({
7033
+ id: createStorageErrorId("SPANNER", "UPDATE_THREAD_RESOURCE_ID", "FAILED"),
7034
+ domain: ErrorDomain.STORAGE,
7035
+ category: ErrorCategory.THIRD_PARTY,
7036
+ details: {
7037
+ threadId,
7038
+ resourceId
7039
+ }
7040
+ }, error);
7041
+ }
7042
+ }
6961
7043
  /** Deletes a thread and all its messages atomically. */
6962
7044
  async deleteThread({ threadId }) {
6963
7045
  const messagesTable = quoteIdent(TABLE_MESSAGES, "table name");