@mastra/cloudflare-d1 1.3.0-alpha.0 → 1.3.0-alpha.2

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
@@ -1260,7 +1260,7 @@ var MemoryStorageD1 = class extends MemoryStorage {
1260
1260
  "updatedAt"
1261
1261
  ];
1262
1262
  const values = [
1263
- title,
1263
+ title ?? thread.title,
1264
1264
  JSON.stringify(mergedMetadata),
1265
1265
  updatedAt.toISOString()
1266
1266
  ];
@@ -1271,7 +1271,7 @@ var MemoryStorageD1 = class extends MemoryStorage {
1271
1271
  });
1272
1272
  return {
1273
1273
  ...thread,
1274
- title,
1274
+ title: title ?? thread.title,
1275
1275
  metadata: {
1276
1276
  ...typeof thread.metadata === "string" ? JSON.parse(thread.metadata) : thread.metadata,
1277
1277
  ...metadata
@@ -1366,15 +1366,23 @@ var MemoryStorageD1 = class extends MemoryStorage {
1366
1366
  return direction === "ASC" ? String(aValue).localeCompare(String(bValue)) : String(bValue).localeCompare(String(aValue));
1367
1367
  });
1368
1368
  }
1369
- 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) {
1370
1377
  if (!include || include.length === 0) return null;
1371
1378
  const tableName = this.#db.getTableName(TABLE_MESSAGES);
1379
+ const resourceCondition = resourceId ? ` AND resourceId = ?` : "";
1372
1380
  const targetIds = include.map((inc) => inc.id).filter(Boolean);
1373
1381
  if (targetIds.length === 0) return null;
1374
1382
  const idPlaceholders = targetIds.map(() => "?").join(", ");
1375
1383
  const targetResult = await this.#db.executeQuery({
1376
- sql: `SELECT id, thread_id, createdAt FROM ${tableName} WHERE id IN (${idPlaceholders})`,
1377
- params: targetIds
1384
+ sql: `SELECT id, thread_id, createdAt FROM ${tableName} WHERE id IN (${idPlaceholders})${resourceCondition}`,
1385
+ params: resourceId ? [...targetIds, resourceId] : targetIds
1378
1386
  });
1379
1387
  if (!Array.isArray(targetResult) || targetResult.length === 0) return null;
1380
1388
  const targetMap = new Map(targetResult.map((r) => [r.id, {
@@ -1406,21 +1414,25 @@ var MemoryStorageD1 = class extends MemoryStorage {
1406
1414
  SELECT id, content, role, type, createdAt, thread_id AS threadId, resourceId
1407
1415
  FROM ${tableName}
1408
1416
  WHERE thread_id = ?
1409
- AND createdAt <= ?
1417
+ AND createdAt <= ?${resourceCondition}
1410
1418
  ORDER BY createdAt DESC, id DESC
1411
1419
  LIMIT ?
1412
1420
  )`);
1413
- 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);
1414
1424
  if (withNextMessages > 0) {
1415
1425
  unionQueries.push(`SELECT * FROM (
1416
1426
  SELECT id, content, role, type, createdAt, thread_id AS threadId, resourceId
1417
1427
  FROM ${tableName}
1418
1428
  WHERE thread_id = ?
1419
- AND createdAt > ?
1429
+ AND createdAt > ?${resourceCondition}
1420
1430
  ORDER BY createdAt ASC, id ASC
1421
1431
  LIMIT ?
1422
1432
  )`);
1423
- unionParams.push(target.threadId, target.createdAt, withNextMessages);
1433
+ unionParams.push(target.threadId, target.createdAt);
1434
+ if (resourceId) unionParams.push(resourceId);
1435
+ unionParams.push(withNextMessages);
1424
1436
  }
1425
1437
  }
1426
1438
  await flushBatch();
@@ -1539,7 +1551,7 @@ var MemoryStorageD1 = class extends MemoryStorage {
1539
1551
  hasMore: false
1540
1552
  };
1541
1553
  if (perPage === 0 && include && include.length > 0) {
1542
- const includeResult = await this._getIncludedMessages(include);
1554
+ const includeResult = await this._getIncludedMessages(include, resourceId);
1543
1555
  if (!Array.isArray(includeResult) || includeResult.length === 0) return {
1544
1556
  messages: [],
1545
1557
  total: 0,
@@ -1610,7 +1622,7 @@ var MemoryStorageD1 = class extends MemoryStorage {
1610
1622
  const messageIds = new Set(paginatedMessages.map((m) => m.id));
1611
1623
  let includeMessages = [];
1612
1624
  if (include && include.length > 0) {
1613
- const includeResult = await this._getIncludedMessages(include);
1625
+ const includeResult = await this._getIncludedMessages(include, resourceId);
1614
1626
  if (Array.isArray(includeResult)) {
1615
1627
  includeMessages = includeResult;
1616
1628
  for (const includeMsg of includeMessages) if (!messageIds.has(includeMsg.id)) {