@mastra/cloudflare-d1 1.2.0 → 1.3.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
@@ -1205,13 +1205,7 @@ var MemoryStorageD1 = class extends MemoryStorage {
1205
1205
  }, error);
1206
1206
  this.logger?.error(mastraError.toString());
1207
1207
  this.logger?.trackException(mastraError);
1208
- return {
1209
- threads: [],
1210
- total: 0,
1211
- page,
1212
- perPage: perPageForResponse,
1213
- hasMore: false
1214
- };
1208
+ throw mastraError;
1215
1209
  }
1216
1210
  }
1217
1211
  async saveThread({ thread }) {
@@ -1372,15 +1366,23 @@ var MemoryStorageD1 = class extends MemoryStorage {
1372
1366
  return direction === "ASC" ? String(aValue).localeCompare(String(bValue)) : String(bValue).localeCompare(String(aValue));
1373
1367
  });
1374
1368
  }
1375
- async _getIncludedMessages(include) {
1369
+ /**
1370
+ * Fetches the messages named by `include` together with their surrounding context.
1371
+ *
1372
+ * @param include - Message ids to pin, each with an optional before/after window.
1373
+ * @param resourceId - When set, restricts both the pinned messages and their context
1374
+ * to that resource so an id from another resource returns nothing.
1375
+ */
1376
+ async _getIncludedMessages(include, resourceId) {
1376
1377
  if (!include || include.length === 0) return null;
1377
1378
  const tableName = this.#db.getTableName(TABLE_MESSAGES);
1379
+ const resourceCondition = resourceId ? ` AND resourceId = ?` : "";
1378
1380
  const targetIds = include.map((inc) => inc.id).filter(Boolean);
1379
1381
  if (targetIds.length === 0) return null;
1380
1382
  const idPlaceholders = targetIds.map(() => "?").join(", ");
1381
1383
  const targetResult = await this.#db.executeQuery({
1382
- sql: `SELECT id, thread_id, createdAt FROM ${tableName} WHERE id IN (${idPlaceholders})`,
1383
- params: targetIds
1384
+ sql: `SELECT id, thread_id, createdAt FROM ${tableName} WHERE id IN (${idPlaceholders})${resourceCondition}`,
1385
+ params: resourceId ? [...targetIds, resourceId] : targetIds
1384
1386
  });
1385
1387
  if (!Array.isArray(targetResult) || targetResult.length === 0) return null;
1386
1388
  const targetMap = new Map(targetResult.map((r) => [r.id, {
@@ -1412,21 +1414,25 @@ var MemoryStorageD1 = class extends MemoryStorage {
1412
1414
  SELECT id, content, role, type, createdAt, thread_id AS threadId, resourceId
1413
1415
  FROM ${tableName}
1414
1416
  WHERE thread_id = ?
1415
- AND createdAt <= ?
1417
+ AND createdAt <= ?${resourceCondition}
1416
1418
  ORDER BY createdAt DESC, id DESC
1417
1419
  LIMIT ?
1418
1420
  )`);
1419
- unionParams.push(target.threadId, target.createdAt, withPreviousMessages + 1);
1421
+ unionParams.push(target.threadId, target.createdAt);
1422
+ if (resourceId) unionParams.push(resourceId);
1423
+ unionParams.push(withPreviousMessages + 1);
1420
1424
  if (withNextMessages > 0) {
1421
1425
  unionQueries.push(`SELECT * FROM (
1422
1426
  SELECT id, content, role, type, createdAt, thread_id AS threadId, resourceId
1423
1427
  FROM ${tableName}
1424
1428
  WHERE thread_id = ?
1425
- AND createdAt > ?
1429
+ AND createdAt > ?${resourceCondition}
1426
1430
  ORDER BY createdAt ASC, id ASC
1427
1431
  LIMIT ?
1428
1432
  )`);
1429
- unionParams.push(target.threadId, target.createdAt, withNextMessages);
1433
+ unionParams.push(target.threadId, target.createdAt);
1434
+ if (resourceId) unionParams.push(resourceId);
1435
+ unionParams.push(withNextMessages);
1430
1436
  }
1431
1437
  }
1432
1438
  await flushBatch();
@@ -1545,7 +1551,7 @@ var MemoryStorageD1 = class extends MemoryStorage {
1545
1551
  hasMore: false
1546
1552
  };
1547
1553
  if (perPage === 0 && include && include.length > 0) {
1548
- const includeResult = await this._getIncludedMessages(include);
1554
+ const includeResult = await this._getIncludedMessages(include, resourceId);
1549
1555
  if (!Array.isArray(includeResult) || includeResult.length === 0) return {
1550
1556
  messages: [],
1551
1557
  total: 0,
@@ -1616,7 +1622,7 @@ var MemoryStorageD1 = class extends MemoryStorage {
1616
1622
  const messageIds = new Set(paginatedMessages.map((m) => m.id));
1617
1623
  let includeMessages = [];
1618
1624
  if (include && include.length > 0) {
1619
- const includeResult = await this._getIncludedMessages(include);
1625
+ const includeResult = await this._getIncludedMessages(include, resourceId);
1620
1626
  if (Array.isArray(includeResult)) {
1621
1627
  includeMessages = includeResult;
1622
1628
  for (const includeMsg of includeMessages) if (!messageIds.has(includeMsg.id)) {
@@ -1637,6 +1643,7 @@ var MemoryStorageD1 = class extends MemoryStorage {
1637
1643
  hasMore: metadataFilter ? perPageInput !== false && offset + paginatedCount < total : perPageInput !== false && !allThreadMessagesReturned && offset + perPage < total
1638
1644
  };
1639
1645
  } catch (error) {
1646
+ if (error instanceof MastraError && error.category === ErrorCategory.USER) throw error;
1640
1647
  const mastraError = new MastraError({
1641
1648
  id: createStorageErrorId("CLOUDFLARE_D1", "LIST_MESSAGES", "FAILED"),
1642
1649
  domain: ErrorDomain.STORAGE,
@@ -1649,13 +1656,7 @@ var MemoryStorageD1 = class extends MemoryStorage {
1649
1656
  }, error);
1650
1657
  this.logger?.error?.(mastraError.toString());
1651
1658
  this.logger?.trackException?.(mastraError);
1652
- return {
1653
- messages: [],
1654
- total: 0,
1655
- page,
1656
- perPage: perPageForResponse,
1657
- hasMore: false
1658
- };
1659
+ throw mastraError;
1659
1660
  }
1660
1661
  }
1661
1662
  async updateMessages(args) {