@mastra/cloudflare-d1 1.0.2 → 1.0.3-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/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # @mastra/cloudflare-d1
2
2
 
3
+ ## 1.0.3-alpha.0
4
+
5
+ ### Patch Changes
6
+
7
+ - Fixed D1 listMessages returning empty results when using semantic recall with perPage=0 and many include targets, by batching UNION ALL queries to avoid SQLite's compound SELECT limit ([#14117](https://github.com/mastra-ai/mastra/pull/14117))
8
+
9
+ - Updated dependencies [[`cddf895`](https://github.com/mastra-ai/mastra/commit/cddf895532b8ee7f9fa814136ec672f53d37a9ba), [`aede3cc`](https://github.com/mastra-ai/mastra/commit/aede3cc2a83b54bbd9e9a54c8aedcd1708b2ef87), [`c4c7dad`](https://github.com/mastra-ai/mastra/commit/c4c7dadfe2e4584f079f6c24bfabdb8c4981827f), [`45c3112`](https://github.com/mastra-ai/mastra/commit/45c31122666a0cc56b94727099fcb1871ed1b3f6), [`5e7c287`](https://github.com/mastra-ai/mastra/commit/5e7c28701f2bce795dd5c811e4c3060bf2ea2242), [`7e17d3f`](https://github.com/mastra-ai/mastra/commit/7e17d3f656fdda2aad47c4beb8c491636d70820c)]:
10
+ - @mastra/core@1.12.0-alpha.0
11
+
3
12
  ## 1.0.2
4
13
 
5
14
  ### Patch Changes
@@ -3,7 +3,7 @@ name: mastra-cloudflare-d1
3
3
  description: Documentation for @mastra/cloudflare-d1. Use when working with @mastra/cloudflare-d1 APIs, configuration, or implementation.
4
4
  metadata:
5
5
  package: "@mastra/cloudflare-d1"
6
- version: "1.0.2"
6
+ version: "1.0.3-alpha.0"
7
7
  ---
8
8
 
9
9
  ## When to use
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.0.2",
2
+ "version": "1.0.3-alpha.0",
3
3
  "package": "@mastra/cloudflare-d1",
4
4
  "exports": {},
5
5
  "modules": {}
package/dist/index.cjs CHANGED
@@ -1209,12 +1209,28 @@ var MemoryStorageD1 = class extends storage.MemoryStorage {
1209
1209
  const targetMap = new Map(
1210
1210
  targetResult.map((r) => [r.id, { threadId: r.thread_id, createdAt: r.createdAt }])
1211
1211
  );
1212
+ const MAX_UNION_TERMS = 5;
1212
1213
  const unionQueries = [];
1213
- const params = [];
1214
+ const unionParams = [];
1215
+ const allMessages = [];
1216
+ const flushBatch = async () => {
1217
+ if (unionQueries.length === 0) return;
1218
+ const sql = unionQueries.length === 1 ? unionQueries[0] : `${unionQueries.join(" UNION ALL ")} ORDER BY createdAt ASC, id ASC`;
1219
+ const result = await this.#db.executeQuery({ sql, params: unionParams });
1220
+ if (Array.isArray(result)) {
1221
+ allMessages.push(...result);
1222
+ }
1223
+ unionQueries.length = 0;
1224
+ unionParams.length = 0;
1225
+ };
1214
1226
  for (const inc of include) {
1215
1227
  const { id, withPreviousMessages = 0, withNextMessages = 0 } = inc;
1216
1228
  const target = targetMap.get(id);
1217
1229
  if (!target) continue;
1230
+ const termsNeeded = withNextMessages > 0 ? 2 : 1;
1231
+ if (unionQueries.length + termsNeeded > MAX_UNION_TERMS) {
1232
+ await flushBatch();
1233
+ }
1218
1234
  unionQueries.push(`SELECT * FROM (
1219
1235
  SELECT id, content, role, type, createdAt, thread_id AS threadId, resourceId
1220
1236
  FROM ${tableName}
@@ -1223,7 +1239,7 @@ var MemoryStorageD1 = class extends storage.MemoryStorage {
1223
1239
  ORDER BY createdAt DESC, id DESC
1224
1240
  LIMIT ?
1225
1241
  )`);
1226
- params.push(target.threadId, target.createdAt, withPreviousMessages + 1);
1242
+ unionParams.push(target.threadId, target.createdAt, withPreviousMessages + 1);
1227
1243
  if (withNextMessages > 0) {
1228
1244
  unionQueries.push(`SELECT * FROM (
1229
1245
  SELECT id, content, role, type, createdAt, thread_id AS threadId, resourceId
@@ -1233,22 +1249,12 @@ var MemoryStorageD1 = class extends storage.MemoryStorage {
1233
1249
  ORDER BY createdAt ASC, id ASC
1234
1250
  LIMIT ?
1235
1251
  )`);
1236
- params.push(target.threadId, target.createdAt, withNextMessages);
1252
+ unionParams.push(target.threadId, target.createdAt, withNextMessages);
1237
1253
  }
1238
1254
  }
1239
- if (unionQueries.length === 0) return null;
1240
- let finalQuery;
1241
- if (unionQueries.length === 1) {
1242
- finalQuery = unionQueries[0];
1243
- } else {
1244
- finalQuery = `${unionQueries.join(" UNION ALL ")} ORDER BY createdAt ASC, id ASC`;
1245
- }
1246
- const messages = await this.#db.executeQuery({ sql: finalQuery, params });
1247
- if (!Array.isArray(messages)) {
1248
- return [];
1249
- }
1255
+ await flushBatch();
1250
1256
  const seen = /* @__PURE__ */ new Set();
1251
- const processedMessages = messages.filter((message) => {
1257
+ const processedMessages = allMessages.filter((message) => {
1252
1258
  const id = message.id;
1253
1259
  if (seen.has(id)) return false;
1254
1260
  seen.add(id);