@mastra/dsql 1.3.5 → 1.4.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.
package/dist/index.cjs CHANGED
@@ -2186,6 +2186,57 @@ var MemoryDSQL = class MemoryDSQL extends _mastra_core_storage.MemoryStorage {
2186
2186
  }, error);
2187
2187
  });
2188
2188
  }
2189
+ async updateThreadResourceId({ threadId, resourceId }) {
2190
+ const threadTableName = getTableName({
2191
+ indexName: _mastra_core_storage.TABLE_THREADS,
2192
+ schemaName: getSchemaName(this.#schema)
2193
+ });
2194
+ const messagesTableName = getTableName({
2195
+ indexName: _mastra_core_storage.TABLE_MESSAGES,
2196
+ schemaName: getSchemaName(this.#schema)
2197
+ });
2198
+ const { result } = await withRetry(async () => {
2199
+ return await this.#db.client.tx(async (t) => {
2200
+ const thread = await t.oneOrNone(`SELECT * FROM ${threadTableName} WHERE id = $1`, [threadId]);
2201
+ if (!thread) throw new Error(`Thread "${threadId}" not found`);
2202
+ const normalized = {
2203
+ id: thread.id,
2204
+ resourceId: thread.resourceId,
2205
+ title: thread.title,
2206
+ metadata: typeof thread.metadata === "string" ? JSON.parse(thread.metadata) : thread.metadata,
2207
+ createdAt: thread.createdAtZ || thread.createdAt,
2208
+ updatedAt: thread.updatedAtZ || thread.updatedAt
2209
+ };
2210
+ if (thread.resourceId === resourceId) return normalized;
2211
+ const now = (/* @__PURE__ */ new Date()).toISOString();
2212
+ await t.none(`UPDATE ${threadTableName} SET "resourceId" = $1, "updatedAt" = $2::timestamp, "updatedAtZ" = $3::timestamptz WHERE id = $4`, [
2213
+ resourceId,
2214
+ now,
2215
+ now,
2216
+ threadId
2217
+ ]);
2218
+ await t.none(`UPDATE ${messagesTableName} SET "resourceId" = $1 WHERE thread_id = $2`, [resourceId, threadId]);
2219
+ return {
2220
+ ...normalized,
2221
+ resourceId,
2222
+ updatedAt: new Date(now)
2223
+ };
2224
+ });
2225
+ }, { onRetry: (error, attempt, delay) => {
2226
+ this.logger?.warn?.(`updateThreadResourceId retry ${attempt} for ${threadId} after ${delay}ms: ${error.message}`);
2227
+ } }).catch((error) => {
2228
+ throw new _mastra_core_error.MastraError({
2229
+ id: (0, _mastra_core_storage.createStorageErrorId)("DSQL", "UPDATE_THREAD_RESOURCE_ID", "FAILED"),
2230
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
2231
+ category: _mastra_core_error.ErrorCategory.THIRD_PARTY,
2232
+ details: {
2233
+ threadId,
2234
+ resourceId
2235
+ }
2236
+ }, error);
2237
+ });
2238
+ return result;
2239
+ }
2189
2240
  /**
2190
2241
  * Fetches the messages named by `include` together with their surrounding context.
2191
2242
  *