@mastra/dynamodb 1.0.0-beta.10 → 1.0.0-beta.12

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,7 +1,7 @@
1
1
  import { DynamoDBClient, DescribeTableCommand } from '@aws-sdk/client-dynamodb';
2
2
  import { DynamoDBDocumentClient } from '@aws-sdk/lib-dynamodb';
3
3
  import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
4
- import { MemoryStorage, createStorageErrorId, normalizePerPage, calculatePagination, filterByDateRange, ScoresStorage, SCORERS_SCHEMA, WorkflowsStorage, MastraStorage, TABLE_SCORERS, TABLE_WORKFLOW_SNAPSHOT, TABLE_RESOURCES, TABLE_MESSAGES, TABLE_THREADS } from '@mastra/core/storage';
4
+ import { MemoryStorage, createStorageErrorId, normalizePerPage, calculatePagination, filterByDateRange, ScoresStorage, SCORERS_SCHEMA, WorkflowsStorage, MastraCompositeStore, TABLE_SCORERS, TABLE_WORKFLOW_SNAPSHOT, TABLE_RESOURCES, TABLE_MESSAGES, TABLE_THREADS } from '@mastra/core/storage';
5
5
  import { Entity, Service } from 'electrodb';
6
6
  import { MessageList } from '@mastra/core/agent';
7
7
  import { saveScorePayloadSchema } from '@mastra/core/evals';
@@ -1504,33 +1504,57 @@ var MemoryStorageDynamoDB = class extends MemoryStorage {
1504
1504
  );
1505
1505
  }
1506
1506
  }
1507
- async listThreadsByResourceId(args) {
1508
- const { resourceId, page = 0, perPage: perPageInput, orderBy } = args;
1509
- const perPage = normalizePerPage(perPageInput, 100);
1510
- if (page < 0) {
1507
+ async listThreads(args) {
1508
+ const { page = 0, perPage: perPageInput, orderBy, filter } = args;
1509
+ try {
1510
+ this.validatePaginationInput(page, perPageInput ?? 100);
1511
+ } catch (error) {
1511
1512
  throw new MastraError(
1512
1513
  {
1513
- id: createStorageErrorId("DYNAMODB", "LIST_THREADS_BY_RESOURCE_ID", "INVALID_PAGE"),
1514
+ id: createStorageErrorId("DYNAMODB", "LIST_THREADS", "INVALID_PAGE"),
1514
1515
  domain: ErrorDomain.STORAGE,
1515
1516
  category: ErrorCategory.USER,
1516
- details: { page }
1517
+ details: {
1518
+ page,
1519
+ ...perPageInput !== void 0 && { perPage: perPageInput }
1520
+ }
1517
1521
  },
1518
- new Error("page must be >= 0")
1522
+ error instanceof Error ? error : new Error("Invalid pagination parameters")
1519
1523
  );
1520
1524
  }
1525
+ const perPage = normalizePerPage(perPageInput, 100);
1521
1526
  const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
1522
1527
  const { field, direction } = this.parseOrderBy(orderBy);
1523
- this.logger.debug("Getting threads by resource ID with pagination", {
1524
- resourceId,
1528
+ this.logger.debug("Listing threads with filters", {
1529
+ resourceId: filter?.resourceId,
1530
+ metadataKeys: filter?.metadata ? Object.keys(filter.metadata) : [],
1525
1531
  page,
1526
1532
  perPage,
1527
1533
  field,
1528
1534
  direction
1529
1535
  });
1530
1536
  try {
1531
- const query = this.service.entities.thread.query.byResource({ entity: "thread", resourceId });
1532
- const results = await query.go();
1533
- const allThreads = this.transformAndSortThreads(results.data, field, direction);
1537
+ const rawThreads = filter?.resourceId ? (await this.service.entities.thread.query.byResource({
1538
+ entity: "thread",
1539
+ resourceId: filter.resourceId
1540
+ }).go({ pages: "all" })).data : (await this.service.entities.thread.scan.go({ pages: "all" })).data;
1541
+ let allThreads = this.transformAndSortThreads(rawThreads, field, direction);
1542
+ if (filter?.metadata && Object.keys(filter.metadata).length > 0) {
1543
+ allThreads = allThreads.filter((thread) => {
1544
+ let threadMeta = null;
1545
+ if (typeof thread.metadata === "string") {
1546
+ try {
1547
+ threadMeta = JSON.parse(thread.metadata);
1548
+ } catch {
1549
+ return false;
1550
+ }
1551
+ } else if (thread.metadata && typeof thread.metadata === "object") {
1552
+ threadMeta = thread.metadata;
1553
+ }
1554
+ if (!threadMeta) return false;
1555
+ return Object.entries(filter.metadata).every(([key, value]) => threadMeta[key] === value);
1556
+ });
1557
+ }
1534
1558
  const endIndex = offset + perPage;
1535
1559
  const paginatedThreads = allThreads.slice(offset, endIndex);
1536
1560
  const total = allThreads.length;
@@ -1545,10 +1569,15 @@ var MemoryStorageDynamoDB = class extends MemoryStorage {
1545
1569
  } catch (error) {
1546
1570
  throw new MastraError(
1547
1571
  {
1548
- id: createStorageErrorId("DYNAMODB", "LIST_THREADS_BY_RESOURCE_ID", "FAILED"),
1572
+ id: createStorageErrorId("DYNAMODB", "LIST_THREADS", "FAILED"),
1549
1573
  domain: ErrorDomain.STORAGE,
1550
1574
  category: ErrorCategory.THIRD_PARTY,
1551
- details: { resourceId, page, perPage }
1575
+ details: {
1576
+ ...filter?.resourceId && { resourceId: filter.resourceId },
1577
+ hasMetadataFilter: !!(filter?.metadata && Object.keys(filter.metadata).length),
1578
+ page,
1579
+ perPage: perPageForResponse
1580
+ }
1552
1581
  },
1553
1582
  error
1554
1583
  );
@@ -2426,7 +2455,7 @@ var WorkflowStorageDynamoDB = class extends WorkflowsStorage {
2426
2455
  var isClientConfig = (config) => {
2427
2456
  return "client" in config;
2428
2457
  };
2429
- var DynamoDBStore = class extends MastraStorage {
2458
+ var DynamoDBStore = class extends MastraCompositeStore {
2430
2459
  tableName;
2431
2460
  client;
2432
2461
  service;