@mastra/cloudflare 0.13.2-alpha.0 → 0.13.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,25 @@
1
1
  # @mastra/cloudflare
2
2
 
3
+ ## 0.13.3-alpha.0
4
+
5
+ ### Patch Changes
6
+
7
+ - fix: ensure score responses match saved payloads for Mastra Stores. ([#10570](https://github.com/mastra-ai/mastra/pull/10570))
8
+
9
+ - Fix message sorting in listMessages when using semantic recall (include parameter). Messages are now always sorted by createdAt instead of storage order, ensuring correct chronological ordering of conversation history. ([#10545](https://github.com/mastra-ai/mastra/pull/10545))
10
+
11
+ - Updated dependencies [[`5657314`](https://github.com/mastra-ai/mastra/commit/5657314a1f9d49019bb53f357fa48f75a69247ca), [`e5aca78`](https://github.com/mastra-ai/mastra/commit/e5aca78bb7f263bb8b470bedae81efe9805d7544), [`33a607a`](https://github.com/mastra-ai/mastra/commit/33a607a1f716c2029d4a1ff1603dd756129a33b3), [`cc10fc1`](https://github.com/mastra-ai/mastra/commit/cc10fc192d9f527c71a23cc9def10d8718935ee1), [`1f7ee84`](https://github.com/mastra-ai/mastra/commit/1f7ee841a643ef12d90392125881f06fdf877293), [`e7d5149`](https://github.com/mastra-ai/mastra/commit/e7d514995260b63b2108308e85c64de37dcd0f71), [`f195082`](https://github.com/mastra-ai/mastra/commit/f1950822a2425d5ccae78c5d010e02ddb027a869), [`d9986dd`](https://github.com/mastra-ai/mastra/commit/d9986dd3513f7ca3244a8e599a440ccf4d8bc28b), [`a45b0f0`](https://github.com/mastra-ai/mastra/commit/a45b0f0cd19eab1fe4deceae3abf029442c22f74), [`f6e8eb3`](https://github.com/mastra-ai/mastra/commit/f6e8eb3dac53b70b06e906b2818b1d2a5b0486d7), [`ce57a2b`](https://github.com/mastra-ai/mastra/commit/ce57a2b62fd0d5f6532e4ecd1ba9ba93ac9b95fc), [`3236f35`](https://github.com/mastra-ai/mastra/commit/3236f352ae13cc8552c2965164e97bd125dae48d), [`ce57a2b`](https://github.com/mastra-ai/mastra/commit/ce57a2b62fd0d5f6532e4ecd1ba9ba93ac9b95fc), [`0230321`](https://github.com/mastra-ai/mastra/commit/02303217870bedea0ef009bea9a952f24ed38aaf), [`7b541f4`](https://github.com/mastra-ai/mastra/commit/7b541f49eda6f5a87b738198edbd136927599475), [`0eea842`](https://github.com/mastra-ai/mastra/commit/0eea8423cbdd37f2111593c6f7d2efcde4b7e4ce), [`63ae8a2`](https://github.com/mastra-ai/mastra/commit/63ae8a22c0c09bbb8b9779f5f38934cd75f616af), [`bf810c5`](https://github.com/mastra-ai/mastra/commit/bf810c5c561bd8ef221c0f6bd84e69770b9a38cc), [`ac7ef07`](https://github.com/mastra-ai/mastra/commit/ac7ef07633caee89707142171d2873c888ffef85), [`522f0b4`](https://github.com/mastra-ai/mastra/commit/522f0b45330719858794eabffffde4f343f55549), [`bf810c5`](https://github.com/mastra-ai/mastra/commit/bf810c5c561bd8ef221c0f6bd84e69770b9a38cc), [`8b51d55`](https://github.com/mastra-ai/mastra/commit/8b51d55bae531edf7e383958d7ecee04df31f5d5), [`2131ac5`](https://github.com/mastra-ai/mastra/commit/2131ac571d5065f0a656c57494bca98691bb7609)]:
12
+ - @mastra/core@0.24.6-alpha.0
13
+
14
+ ## 0.13.2
15
+
16
+ ### Patch Changes
17
+
18
+ - update peerdeps ([`5ca1cca`](https://github.com/mastra-ai/mastra/commit/5ca1ccac61ffa7141e6d9fa8f22d3ad4d03bf5dc))
19
+
20
+ - Updated dependencies [[`5ca1cca`](https://github.com/mastra-ai/mastra/commit/5ca1ccac61ffa7141e6d9fa8f22d3ad4d03bf5dc), [`6d7e90d`](https://github.com/mastra-ai/mastra/commit/6d7e90db09713e6250f4d6c3d3cff1b4740e50f9), [`f78b908`](https://github.com/mastra-ai/mastra/commit/f78b9080e11af765969b36b4a619761056030840), [`23c2614`](https://github.com/mastra-ai/mastra/commit/23c26140fdbf04b8c59e8d7d52106d67dad962ec), [`e365eda`](https://github.com/mastra-ai/mastra/commit/e365eda45795b43707310531cac1e2ce4e5a0712)]:
21
+ - @mastra/core@0.24.0
22
+
3
23
  ## 0.13.2-alpha.0
4
24
 
5
25
  ### Patch Changes
package/dist/index.cjs CHANGED
@@ -121,6 +121,17 @@ var MemoryStorageCloudflare = class extends storage.MemoryStorage {
121
121
  if (!metadata) return void 0;
122
122
  return typeof metadata === "string" ? JSON.parse(metadata) : metadata;
123
123
  }
124
+ /**
125
+ * Summarizes message content without exposing raw data (for logging).
126
+ * Returns type, length, and keys only to prevent PII leakage.
127
+ */
128
+ summarizeMessageContent(content) {
129
+ if (!content) return { type: "undefined" };
130
+ if (typeof content === "string") return { type: "string", length: content.length };
131
+ if (Array.isArray(content)) return { type: "array", length: content.length };
132
+ if (typeof content === "object") return { type: "object", keys: Object.keys(content) };
133
+ return { type: typeof content };
134
+ }
124
135
  async getThreadById({ threadId }) {
125
136
  const thread = await this.operations.load({ tableName: storage.TABLE_THREADS, keys: { id: threadId } });
126
137
  if (!thread) return null;
@@ -298,7 +309,7 @@ var MemoryStorageCloudflare = class extends storage.MemoryStorage {
298
309
  return this.operations.getKey(storage.TABLE_MESSAGES, { threadId, id: messageId });
299
310
  } catch (error) {
300
311
  const message = error instanceof Error ? error.message : String(error);
301
- this.logger.error(`Error getting message key for thread ${threadId} and message ${messageId}:`, { message });
312
+ this.logger?.error(`Error getting message key for thread ${threadId} and message ${messageId}:`, { message });
302
313
  throw error;
303
314
  }
304
315
  }
@@ -307,7 +318,7 @@ var MemoryStorageCloudflare = class extends storage.MemoryStorage {
307
318
  return this.operations.getKey(storage.TABLE_MESSAGES, { threadId, id: "messages" });
308
319
  } catch (error) {
309
320
  const message = error instanceof Error ? error.message : String(error);
310
- this.logger.error(`Error getting thread messages key for thread ${threadId}:`, { message });
321
+ this.logger?.error(`Error getting thread messages key for thread ${threadId}:`, { message });
311
322
  throw error;
312
323
  }
313
324
  }
@@ -397,7 +408,7 @@ var MemoryStorageCloudflare = class extends storage.MemoryStorage {
397
408
  });
398
409
  } catch (error) {
399
410
  const message = error instanceof Error ? error.message : String(error);
400
- this.logger.error(`Error updating sorted order for key ${orderKey}:`, { message });
411
+ this.logger?.error(`Error updating sorted order for key ${orderKey}:`, { message });
401
412
  throw error;
402
413
  } finally {
403
414
  if (this.updateQueue.get(orderKey) === nextPromise) {
@@ -415,7 +426,7 @@ var MemoryStorageCloudflare = class extends storage.MemoryStorage {
415
426
  const arr = JSON.parse(typeof raw === "string" ? raw : JSON.stringify(raw));
416
427
  return Array.isArray(arr) ? arr : [];
417
428
  } catch (e) {
418
- this.logger.error(`Error parsing order data for key ${orderKey}:`, { e });
429
+ this.logger?.error(`Error parsing order data for key ${orderKey}:`, { e });
419
430
  return [];
420
431
  }
421
432
  }
@@ -470,9 +481,11 @@ var MemoryStorageCloudflare = class extends storage.MemoryStorage {
470
481
  const messageMigrationTasks = [];
471
482
  for (const message of validatedMessages) {
472
483
  const existingMessage = await this.findMessageInAnyThread(message.id);
473
- console.info(`Checking message ${message.id}: existing=${existingMessage?.threadId}, new=${message.threadId}`);
484
+ this.logger?.debug(
485
+ `Checking message ${message.id}: existing=${existingMessage?.threadId}, new=${message.threadId}`
486
+ );
474
487
  if (existingMessage && existingMessage.threadId && existingMessage.threadId !== message.threadId) {
475
- console.info(`Migrating message ${message.id} from ${existingMessage.threadId} to ${message.threadId}`);
488
+ this.logger?.debug(`Migrating message ${message.id} from ${existingMessage.threadId} to ${message.threadId}`);
476
489
  messageMigrationTasks.push(this.migrateMessage(message.id, existingMessage.threadId, message.threadId));
477
490
  }
478
491
  }
@@ -501,10 +514,8 @@ var MemoryStorageCloudflare = class extends storage.MemoryStorage {
501
514
  ...cleanMessage,
502
515
  createdAt: storage.serializeDate(cleanMessage.createdAt)
503
516
  };
504
- console.info(`Saving message ${message.id} with content:`, {
505
- content: serializedMessage.content,
506
- contentType: typeof serializedMessage.content,
507
- isArray: Array.isArray(serializedMessage.content)
517
+ this.logger?.debug(`Saving message ${message.id}`, {
518
+ contentSummary: this.summarizeMessageContent(serializedMessage.content)
508
519
  });
509
520
  await this.operations.putKV({ tableName: storage.TABLE_MESSAGES, key, value: serializedMessage });
510
521
  })
@@ -568,9 +579,6 @@ var MemoryStorageCloudflare = class extends storage.MemoryStorage {
568
579
  async getLastN(orderKey, n) {
569
580
  return this.getRange(orderKey, -n, -1);
570
581
  }
571
- async getFullOrder(orderKey) {
572
- return this.getRange(orderKey, 0, -1);
573
- }
574
582
  async getIncludedMessagesWithContext(threadId, include, messageIds) {
575
583
  await Promise.all(
576
584
  include.map(async (item) => {
@@ -604,7 +612,7 @@ var MemoryStorageCloudflare = class extends storage.MemoryStorage {
604
612
  const latestIds = await this.getLastN(threadMessagesKey, limit);
605
613
  latestIds.forEach((id) => messageIds.add(id));
606
614
  } catch {
607
- console.info(`No message order found for thread ${threadId}, skipping latest messages`);
615
+ this.logger?.debug(`No message order found for thread ${threadId}, skipping latest messages`);
608
616
  }
609
617
  }
610
618
  async fetchAndParseMessagesFromMultipleThreads(messageIds, include, targetThreadId) {
@@ -635,15 +643,13 @@ var MemoryStorageCloudflare = class extends storage.MemoryStorage {
635
643
  const data = await this.operations.getKV(storage.TABLE_MESSAGES, key);
636
644
  if (!data) return null;
637
645
  const parsed = typeof data === "string" ? JSON.parse(data) : data;
638
- console.info(`Retrieved message ${id} from thread ${threadId} with content:`, {
639
- content: parsed.content,
640
- contentType: typeof parsed.content,
641
- isArray: Array.isArray(parsed.content)
646
+ this.logger?.debug(`Retrieved message ${id} from thread ${threadId}`, {
647
+ contentSummary: this.summarizeMessageContent(parsed.content)
642
648
  });
643
649
  return parsed;
644
650
  } catch (error) {
645
651
  const message = error instanceof Error ? error.message : String(error);
646
- this.logger.error(`Error retrieving message ${id}:`, { message });
652
+ this.logger?.error(`Error retrieving message ${id}:`, { message });
647
653
  return null;
648
654
  }
649
655
  })
@@ -675,33 +681,7 @@ var MemoryStorageCloudflare = class extends storage.MemoryStorage {
675
681
  targetThreadId
676
682
  );
677
683
  if (!messages.length) return [];
678
- try {
679
- const threadMessagesKey = this.getThreadMessagesKey(threadId);
680
- const messageOrder = await this.getFullOrder(threadMessagesKey);
681
- const orderMap = new Map(messageOrder.map((id, index) => [id, index]));
682
- messages.sort((a, b) => {
683
- const indexA = orderMap.get(a.id);
684
- const indexB = orderMap.get(b.id);
685
- if (indexA !== void 0 && indexB !== void 0) return orderMap.get(a.id) - orderMap.get(b.id);
686
- return new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime();
687
- });
688
- } catch (error$1) {
689
- const mastraError = new error.MastraError(
690
- {
691
- id: "CLOUDFLARE_STORAGE_SORT_MESSAGES_FAILED",
692
- domain: error.ErrorDomain.STORAGE,
693
- category: error.ErrorCategory.THIRD_PARTY,
694
- text: `Error sorting messages for thread ${threadId} falling back to creation time`,
695
- details: {
696
- threadId
697
- }
698
- },
699
- error$1
700
- );
701
- this.logger?.trackException(mastraError);
702
- this.logger?.error(mastraError.toString());
703
- messages.sort((a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime());
704
- }
684
+ messages.sort((a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime());
705
685
  const prepared = messages.map(({ _index, ...message }) => ({
706
686
  ...message,
707
687
  type: message.type === `v2` ? void 0 : message.type,
@@ -775,26 +755,49 @@ var MemoryStorageCloudflare = class extends storage.MemoryStorage {
775
755
  const { page = 0, perPage = 100 } = selectBy?.pagination || {};
776
756
  try {
777
757
  if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
778
- const messages = format === "v2" ? await this.getMessages({ threadId, selectBy, format: "v2" }) : await this.getMessages({ threadId, selectBy, format: "v1" });
779
- let filteredMessages = messages;
758
+ const includedMessages = [];
759
+ if (selectBy?.include?.length) {
760
+ const includeOnlySelectBy = { ...selectBy, last: 0 };
761
+ const included = format === "v2" ? await this.getMessages({ threadId, selectBy: includeOnlySelectBy, format: "v2" }) : await this.getMessages({ threadId, selectBy: includeOnlySelectBy, format: "v1" });
762
+ includedMessages.push(...included);
763
+ }
764
+ const threadOnlySelectBy = selectBy ? { ...selectBy, include: void 0 } : void 0;
765
+ const threadMessages = format === "v2" ? await this.getMessages({ threadId, selectBy: threadOnlySelectBy, format: "v2" }) : await this.getMessages({ threadId, selectBy: threadOnlySelectBy, format: "v1" });
766
+ let filteredMessages = threadMessages;
780
767
  if (selectBy?.pagination?.dateRange) {
781
768
  const { start: dateStart, end: dateEnd } = selectBy.pagination.dateRange;
782
- filteredMessages = messages.filter((message) => {
769
+ filteredMessages = threadMessages.filter((message) => {
783
770
  const messageDate = new Date(message.createdAt);
784
771
  if (dateStart && messageDate < dateStart) return false;
785
772
  if (dateEnd && messageDate > dateEnd) return false;
786
773
  return true;
787
774
  });
788
775
  }
776
+ const total = filteredMessages.length;
789
777
  const start = page * perPage;
790
778
  const end = start + perPage;
791
779
  const paginatedMessages = filteredMessages.slice(start, end);
780
+ const seenIds = /* @__PURE__ */ new Set();
781
+ const combinedMessages = [];
782
+ for (const msg of paginatedMessages) {
783
+ if (!seenIds.has(msg.id)) {
784
+ combinedMessages.push(msg);
785
+ seenIds.add(msg.id);
786
+ }
787
+ }
788
+ for (const msg of includedMessages) {
789
+ if (!seenIds.has(msg.id)) {
790
+ combinedMessages.push(msg);
791
+ seenIds.add(msg.id);
792
+ }
793
+ }
794
+ combinedMessages.sort((a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime());
792
795
  return {
793
796
  page,
794
797
  perPage,
795
- total: filteredMessages.length,
796
- hasMore: start + perPage < filteredMessages.length,
797
- messages: paginatedMessages
798
+ total,
799
+ hasMore: start + perPage < total,
800
+ messages: combinedMessages
798
801
  };
799
802
  } catch (error$1) {
800
803
  const mastraError = new error.MastraError(
@@ -1580,17 +1583,21 @@ var StoreOperationsCloudflare = class extends storage.StoreOperations {
1580
1583
  }
1581
1584
  };
1582
1585
  function transformScoreRow(row) {
1583
- const deserialized = { ...row };
1584
- deserialized.input = storage.safelyParseJSON(row.input);
1585
- deserialized.output = storage.safelyParseJSON(row.output);
1586
- deserialized.scorer = storage.safelyParseJSON(row.scorer);
1587
- deserialized.preprocessStepResult = storage.safelyParseJSON(row.preprocessStepResult);
1588
- deserialized.analyzeStepResult = storage.safelyParseJSON(row.analyzeStepResult);
1589
- deserialized.metadata = storage.safelyParseJSON(row.metadata);
1590
- deserialized.additionalContext = storage.safelyParseJSON(row.additionalContext);
1591
- deserialized.runtimeContext = storage.safelyParseJSON(row.runtimeContext);
1592
- deserialized.entity = storage.safelyParseJSON(row.entity);
1593
- return deserialized;
1586
+ const result = {};
1587
+ for (const [key, columnSchema] of Object.entries(storage.SCORERS_SCHEMA)) {
1588
+ const value = row[key];
1589
+ if (value == null) {
1590
+ continue;
1591
+ }
1592
+ if (columnSchema.type === "jsonb") {
1593
+ result[key] = storage.safelyParseJSON(value);
1594
+ } else if (columnSchema.type === "timestamp") {
1595
+ result[key] = new Date(value);
1596
+ } else {
1597
+ result[key] = value;
1598
+ }
1599
+ }
1600
+ return result;
1594
1601
  }
1595
1602
  var ScoresStorageCloudflare = class extends storage.ScoresStorage {
1596
1603
  operations;