@mastra/mssql 1.6.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.
package/dist/index.js CHANGED
@@ -2340,13 +2340,7 @@ var MemoryMSSQL = class MemoryMSSQL extends MemoryStorage {
2340
2340
  }, error);
2341
2341
  this.logger?.error?.(mastraError.toString());
2342
2342
  this.logger?.trackException?.(mastraError);
2343
- return {
2344
- threads: [],
2345
- total: 0,
2346
- page,
2347
- perPage: perPageForResponse,
2348
- hasMore: false
2349
- };
2343
+ throw mastraError;
2350
2344
  }
2351
2345
  }
2352
2346
  async saveThread({ thread }) {
@@ -2493,7 +2487,14 @@ var MemoryMSSQL = class MemoryMSSQL extends MemoryStorage {
2493
2487
  return diff !== 0 ? diff : a.id.localeCompare(b.id);
2494
2488
  });
2495
2489
  }
2496
- async _getIncludedMessages({ include }) {
2490
+ /**
2491
+ * Fetches the messages named by `include` together with their surrounding context.
2492
+ *
2493
+ * @param include - Message ids to pin, each with an optional before/after window.
2494
+ * @param resourceId - When set, restricts both the pinned messages and their context
2495
+ * to that resource so an id from another resource returns nothing.
2496
+ */
2497
+ async _getIncludedMessages({ include, resourceId }) {
2497
2498
  if (!include || include.length === 0) return null;
2498
2499
  const unionQueries = [];
2499
2500
  const paramValues = [];
@@ -2503,6 +2504,7 @@ var MemoryMSSQL = class MemoryMSSQL extends MemoryStorage {
2503
2504
  indexName: TABLE_MESSAGES,
2504
2505
  schemaName: getSchemaName(this.schema)
2505
2506
  });
2507
+ const resourceCondition = resourceId ? ` AND [resourceId] = @presource` : "";
2506
2508
  for (const inc of include) {
2507
2509
  const { id, withPreviousMessages = 0, withNextMessages = 0 } = inc;
2508
2510
  const pId = `@p${paramIdx}`;
@@ -2521,7 +2523,7 @@ var MemoryMSSQL = class MemoryMSSQL extends MemoryStorage {
2521
2523
  FROM (
2522
2524
  SELECT *, ROW_NUMBER() OVER (ORDER BY [createdAt] ASC) as row_num
2523
2525
  FROM ${tableName}
2524
- WHERE [thread_id] = (SELECT thread_id FROM ${tableName} WHERE id = ${pId})
2526
+ WHERE [thread_id] = (SELECT thread_id FROM ${tableName} WHERE id = ${pId}${resourceCondition})${resourceCondition}
2525
2527
  ) AS m
2526
2528
  WHERE m.id = ${pId}
2527
2529
  OR EXISTS (
@@ -2529,7 +2531,7 @@ var MemoryMSSQL = class MemoryMSSQL extends MemoryStorage {
2529
2531
  FROM (
2530
2532
  SELECT *, ROW_NUMBER() OVER (ORDER BY [createdAt] ASC) as row_num
2531
2533
  FROM ${tableName}
2532
- WHERE [thread_id] = (SELECT thread_id FROM ${tableName} WHERE id = ${pId})
2534
+ WHERE [thread_id] = (SELECT thread_id FROM ${tableName} WHERE id = ${pId}${resourceCondition})${resourceCondition}
2533
2535
  ) AS target
2534
2536
  WHERE target.id = ${pId}
2535
2537
  AND (
@@ -2553,6 +2555,7 @@ var MemoryMSSQL = class MemoryMSSQL extends MemoryStorage {
2553
2555
  `;
2554
2556
  const req = this.pool.request();
2555
2557
  for (let i = 0; i < paramValues.length; ++i) req.input(paramNames[i], paramValues[i]);
2558
+ if (resourceId) req.input("presource", resourceId);
2556
2559
  const includedRows = (await req.query(finalQuery)).recordset || [];
2557
2560
  const seen = /* @__PURE__ */ new Set();
2558
2561
  return includedRows.filter((row) => {
@@ -2600,7 +2603,7 @@ var MemoryMSSQL = class MemoryMSSQL extends MemoryStorage {
2600
2603
  }, error);
2601
2604
  this.logger?.error?.(mastraError.toString());
2602
2605
  this.logger?.trackException?.(mastraError);
2603
- return { messages: [] };
2606
+ throw mastraError;
2604
2607
  }
2605
2608
  }
2606
2609
  async listMessages(args) {
@@ -2652,7 +2655,10 @@ var MemoryMSSQL = class MemoryMSSQL extends MemoryStorage {
2652
2655
  hasMore: false
2653
2656
  };
2654
2657
  if (perPage === 0 && include && include.length > 0) {
2655
- const includeMessages = await this._getIncludedMessages({ include });
2658
+ const includeMessages = await this._getIncludedMessages({
2659
+ include,
2660
+ resourceId
2661
+ });
2656
2662
  const messages = this._parseAndFormatMessages(includeMessages ?? [], "v2");
2657
2663
  return {
2658
2664
  messages: this._sortMessages(messages, field, direction),
@@ -2689,7 +2695,10 @@ var MemoryMSSQL = class MemoryMSSQL extends MemoryStorage {
2689
2695
  };
2690
2696
  if (include?.length) {
2691
2697
  const messageIds = new Set(messages.map((m) => m.id));
2692
- (await this._getIncludedMessages({ include }))?.forEach((msg) => {
2698
+ (await this._getIncludedMessages({
2699
+ include,
2700
+ resourceId
2701
+ }))?.forEach((msg) => {
2693
2702
  if (!messageIds.has(msg.id)) {
2694
2703
  messages.push(msg);
2695
2704
  messageIds.add(msg.id);
@@ -2719,6 +2728,7 @@ var MemoryMSSQL = class MemoryMSSQL extends MemoryStorage {
2719
2728
  hasMore: metadataFilter ? perPageInput !== false && offset + primaryPageCount < total : perPageInput !== false && returnedThreadMessageCount < total && offset + perPage < total
2720
2729
  };
2721
2730
  } catch (error) {
2731
+ if (error instanceof MastraError && error.category === ErrorCategory.USER) throw error;
2722
2732
  const mastraError = new MastraError({
2723
2733
  id: createStorageErrorId("MSSQL", "LIST_MESSAGES", "FAILED"),
2724
2734
  domain: ErrorDomain.STORAGE,
@@ -2730,13 +2740,7 @@ var MemoryMSSQL = class MemoryMSSQL extends MemoryStorage {
2730
2740
  }, error);
2731
2741
  this.logger?.error?.(mastraError.toString());
2732
2742
  this.logger?.trackException?.(mastraError);
2733
- return {
2734
- messages: [],
2735
- total: 0,
2736
- page,
2737
- perPage: perPageForResponse,
2738
- hasMore: false
2739
- };
2743
+ throw mastraError;
2740
2744
  }
2741
2745
  }
2742
2746
  async saveMessages({ messages }) {