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