@mastra/cloudflare 1.0.0-beta.9 → 1.0.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/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
2
- import { MemoryStorage, TABLE_MESSAGES, TABLE_THREADS, TABLE_RESOURCES, ensureDate, createStorageErrorId, normalizePerPage, calculatePagination, serializeDate, ScoresStorage, TABLE_SCORERS, WorkflowsStorage, TABLE_WORKFLOW_SNAPSHOT, MastraStorage, TABLE_TRACES, transformScoreRow as transformScoreRow$1 } from '@mastra/core/storage';
2
+ import { MemoryStorage, TABLE_MESSAGES, TABLE_THREADS, TABLE_RESOURCES, ensureDate, createStorageErrorId, normalizePerPage, calculatePagination, serializeDate, filterByDateRange, ScoresStorage, TABLE_SCORERS, WorkflowsStorage, TABLE_WORKFLOW_SNAPSHOT, MastraCompositeStore, TABLE_TRACES, transformScoreRow as transformScoreRow$1 } from '@mastra/core/storage';
3
3
  import Cloudflare from 'cloudflare';
4
4
  import { MessageList } from '@mastra/core/agent';
5
5
  import { MastraBase } from '@mastra/core/base';
@@ -614,21 +614,23 @@ var MemoryStorageCloudflare = class extends MemoryStorage {
614
614
  return null;
615
615
  }
616
616
  }
617
- async listThreadsByResourceId(args) {
617
+ async listThreads(args) {
618
+ const { page = 0, perPage: perPageInput, orderBy, filter } = args;
619
+ try {
620
+ this.validatePaginationInput(page, perPageInput ?? 100);
621
+ } catch (error) {
622
+ throw new MastraError(
623
+ {
624
+ id: createStorageErrorId("CLOUDFLARE", "LIST_THREADS", "INVALID_PAGE"),
625
+ domain: ErrorDomain.STORAGE,
626
+ category: ErrorCategory.USER,
627
+ details: { page, ...perPageInput !== void 0 && { perPage: perPageInput } }
628
+ },
629
+ error instanceof Error ? error : new Error("Invalid pagination parameters")
630
+ );
631
+ }
632
+ const perPage = normalizePerPage(perPageInput, 100);
618
633
  try {
619
- const { resourceId, page = 0, perPage: perPageInput, orderBy } = args;
620
- const perPage = normalizePerPage(perPageInput, 100);
621
- if (page < 0) {
622
- throw new MastraError(
623
- {
624
- id: createStorageErrorId("CLOUDFLARE", "LIST_THREADS_BY_RESOURCE_ID", "INVALID_PAGE"),
625
- domain: ErrorDomain.STORAGE,
626
- category: ErrorCategory.USER,
627
- details: { page }
628
- },
629
- new Error("page must be >= 0")
630
- );
631
- }
632
634
  const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
633
635
  const { field, direction } = this.parseOrderBy(orderBy);
634
636
  const prefix = this.#db.namespacePrefix ? `${this.#db.namespacePrefix}:` : "";
@@ -637,7 +639,15 @@ var MemoryStorageCloudflare = class extends MemoryStorage {
637
639
  for (const { name: key } of keyObjs) {
638
640
  const data = await this.#db.getKV(TABLE_THREADS, key);
639
641
  if (!data) continue;
640
- if (data.resourceId !== resourceId) continue;
642
+ if (filter?.resourceId && data.resourceId !== filter.resourceId) {
643
+ continue;
644
+ }
645
+ if (filter?.metadata && Object.keys(filter.metadata).length > 0) {
646
+ const metadata = this.ensureMetadata(data.metadata);
647
+ if (!metadata) continue;
648
+ const matches = Object.entries(filter.metadata).every(([key2, value]) => metadata[key2] === value);
649
+ if (!matches) continue;
650
+ }
641
651
  threads.push(data);
642
652
  }
643
653
  threads.sort((a, b) => {
@@ -657,10 +667,10 @@ var MemoryStorageCloudflare = class extends MemoryStorage {
657
667
  } catch (error) {
658
668
  throw new MastraError(
659
669
  {
660
- id: createStorageErrorId("CLOUDFLARE", "LIST_THREADS_BY_RESOURCE_ID", "FAILED"),
670
+ id: createStorageErrorId("CLOUDFLARE", "LIST_THREADS", "FAILED"),
661
671
  domain: ErrorDomain.STORAGE,
662
672
  category: ErrorCategory.THIRD_PARTY,
663
- text: "Failed to get threads by resource ID with pagination"
673
+ text: "Failed to list threads with filters"
664
674
  },
665
675
  error
666
676
  );
@@ -1194,15 +1204,11 @@ var MemoryStorageCloudflare = class extends MemoryStorage {
1194
1204
  if (resourceId) {
1195
1205
  filteredThreadMessages = filteredThreadMessages.filter((msg) => msg.resourceId === resourceId);
1196
1206
  }
1197
- const dateRange = filter?.dateRange;
1198
- if (dateRange) {
1199
- filteredThreadMessages = filteredThreadMessages.filter((msg) => {
1200
- const messageDate = new Date(msg.createdAt);
1201
- if (dateRange.start && messageDate < new Date(dateRange.start)) return false;
1202
- if (dateRange.end && messageDate > new Date(dateRange.end)) return false;
1203
- return true;
1204
- });
1205
- }
1207
+ filteredThreadMessages = filterByDateRange(
1208
+ filteredThreadMessages,
1209
+ (msg) => new Date(msg.createdAt),
1210
+ filter?.dateRange
1211
+ );
1206
1212
  const total = filteredThreadMessages.length;
1207
1213
  if (perPage === 0 && (!include || include.length === 0)) {
1208
1214
  return {
@@ -1983,7 +1989,7 @@ var WorkflowsStorageCloudflare = class extends WorkflowsStorage {
1983
1989
  try {
1984
1990
  parsedSnapshot = JSON.parse(row.snapshot);
1985
1991
  } catch (e) {
1986
- console.warn(`Failed to parse snapshot for workflow ${row.workflow_name}: ${e}`);
1992
+ this.logger.warn(`Failed to parse snapshot for workflow ${row.workflow_name}: ${e}`);
1987
1993
  }
1988
1994
  }
1989
1995
  return {
@@ -2157,7 +2163,7 @@ function isWorkersConfig(config) {
2157
2163
  }
2158
2164
 
2159
2165
  // src/storage/index.ts
2160
- var CloudflareStore = class extends MastraStorage {
2166
+ var CloudflareStore = class extends MastraCompositeStore {
2161
2167
  stores;
2162
2168
  client;
2163
2169
  accountId;