@mastra/cloudflare-d1 1.0.0-beta.0 → 1.0.0-beta.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
@@ -1,5 +1,5 @@
1
1
  import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
2
- import { MastraStorage, StoreOperations, ScoresStorage, TABLE_SCORERS, normalizePerPage, calculatePagination, WorkflowsStorage, TABLE_WORKFLOW_SNAPSHOT, ensureDate, MemoryStorage, TABLE_RESOURCES, TABLE_THREADS, TABLE_MESSAGES, serializeDate, safelyParseJSON } from '@mastra/core/storage';
2
+ import { MastraStorage, StoreOperations, ScoresStorage, TABLE_SCORERS, normalizePerPage, calculatePagination, WorkflowsStorage, TABLE_WORKFLOW_SNAPSHOT, ensureDate, MemoryStorage, TABLE_RESOURCES, TABLE_THREADS, TABLE_MESSAGES, serializeDate, 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 { parseSqlIdentifier } from '@mastra/core/utils';
@@ -627,23 +627,25 @@ var MemoryStorageD1 = class extends MemoryStorage {
627
627
  );
628
628
  }
629
629
  }
630
- async _getIncludedMessages(threadId, include) {
631
- if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
632
- if (!include) return null;
630
+ async _getIncludedMessages(include) {
631
+ if (!include || include.length === 0) return null;
633
632
  const unionQueries = [];
634
633
  const params = [];
635
634
  let paramIdx = 1;
635
+ const tableName = this.operations.getTableName(TABLE_MESSAGES);
636
636
  for (const inc of include) {
637
637
  const { id, withPreviousMessages = 0, withNextMessages = 0 } = inc;
638
- const searchId = inc.threadId || threadId;
639
638
  unionQueries.push(`
640
639
  SELECT * FROM (
641
- WITH ordered_messages AS (
640
+ WITH target_thread AS (
641
+ SELECT thread_id FROM ${tableName} WHERE id = ?
642
+ ),
643
+ ordered_messages AS (
642
644
  SELECT
643
645
  *,
644
646
  ROW_NUMBER() OVER (ORDER BY createdAt ASC) AS row_num
645
- FROM ${this.operations.getTableName(TABLE_MESSAGES)}
646
- WHERE thread_id = ?
647
+ FROM ${tableName}
648
+ WHERE thread_id = (SELECT thread_id FROM target_thread)
647
649
  )
648
650
  SELECT
649
651
  m.id,
@@ -666,7 +668,7 @@ var MemoryStorageD1 = class extends MemoryStorage {
666
668
  )
667
669
  ) AS query_${paramIdx}
668
670
  `);
669
- params.push(searchId, id, id, withNextMessages, withPreviousMessages);
671
+ params.push(id, id, id, withNextMessages, withPreviousMessages);
670
672
  paramIdx++;
671
673
  }
672
674
  const finalQuery = unionQueries.join(" UNION ALL ") + " ORDER BY createdAt ASC";
@@ -723,15 +725,16 @@ var MemoryStorageD1 = class extends MemoryStorage {
723
725
  }
724
726
  async listMessages(args) {
725
727
  const { threadId, resourceId, include, filter, perPage: perPageInput, page = 0, orderBy } = args;
726
- if (!threadId.trim()) {
728
+ const threadIds = Array.isArray(threadId) ? threadId : [threadId];
729
+ if (threadIds.length === 0 || threadIds.some((id) => !id.trim())) {
727
730
  throw new MastraError(
728
731
  {
729
732
  id: "STORAGE_CLOUDFLARE_D1_LIST_MESSAGES_INVALID_THREAD_ID",
730
733
  domain: ErrorDomain.STORAGE,
731
734
  category: ErrorCategory.THIRD_PARTY,
732
- details: { threadId }
735
+ details: { threadId: Array.isArray(threadId) ? threadId.join(",") : threadId }
733
736
  },
734
- new Error("threadId must be a non-empty string")
737
+ new Error("threadId must be a non-empty string or array of non-empty strings")
735
738
  );
736
739
  }
737
740
  if (page < 0) {
@@ -816,7 +819,7 @@ var MemoryStorageD1 = class extends MemoryStorage {
816
819
  const messageIds = new Set(paginatedMessages.map((m) => m.id));
817
820
  let includeMessages = [];
818
821
  if (include && include.length > 0) {
819
- const includeResult = await this._getIncludedMessages(threadId, include);
822
+ const includeResult = await this._getIncludedMessages(include);
820
823
  if (Array.isArray(includeResult)) {
821
824
  includeMessages = includeResult;
822
825
  for (const includeMsg of includeMessages) {
@@ -857,9 +860,9 @@ var MemoryStorageD1 = class extends MemoryStorage {
857
860
  id: "CLOUDFLARE_D1_STORAGE_LIST_MESSAGES_ERROR",
858
861
  domain: ErrorDomain.STORAGE,
859
862
  category: ErrorCategory.THIRD_PARTY,
860
- text: `Failed to list messages for thread ${threadId}: ${error instanceof Error ? error.message : String(error)}`,
863
+ text: `Failed to list messages for thread ${Array.isArray(threadId) ? threadId.join(",") : threadId}: ${error instanceof Error ? error.message : String(error)}`,
861
864
  details: {
862
- threadId,
865
+ threadId: Array.isArray(threadId) ? threadId.join(",") : threadId,
863
866
  resourceId: resourceId ?? ""
864
867
  }
865
868
  },
@@ -1364,19 +1367,12 @@ var StoreOperationsD1 = class extends StoreOperations {
1364
1367
  }
1365
1368
  };
1366
1369
  function transformScoreRow(row) {
1367
- const deserialized = { ...row };
1368
- deserialized.input = safelyParseJSON(row.input);
1369
- deserialized.output = safelyParseJSON(row.output);
1370
- deserialized.scorer = safelyParseJSON(row.scorer);
1371
- deserialized.preprocessStepResult = safelyParseJSON(row.preprocessStepResult);
1372
- deserialized.analyzeStepResult = safelyParseJSON(row.analyzeStepResult);
1373
- deserialized.metadata = safelyParseJSON(row.metadata);
1374
- deserialized.additionalContext = safelyParseJSON(row.additionalContext);
1375
- deserialized.requestContext = safelyParseJSON(row.requestContext);
1376
- deserialized.entity = safelyParseJSON(row.entity);
1377
- deserialized.createdAt = row.createdAtZ || row.createdAt;
1378
- deserialized.updatedAt = row.updatedAtZ || row.updatedAt;
1379
- return deserialized;
1370
+ return transformScoreRow$1(row, {
1371
+ preferredTimestampFields: {
1372
+ createdAt: "createdAtZ",
1373
+ updatedAt: "updatedAtZ"
1374
+ }
1375
+ });
1380
1376
  }
1381
1377
  var ScoresStorageD1 = class extends ScoresStorage {
1382
1378
  operations;
@@ -1798,13 +1794,18 @@ var WorkflowsStorageD1 = class extends WorkflowsStorage {
1798
1794
  toDate,
1799
1795
  page,
1800
1796
  perPage,
1801
- resourceId
1797
+ resourceId,
1798
+ status
1802
1799
  } = {}) {
1803
1800
  const fullTableName = this.operations.getTableName(TABLE_WORKFLOW_SNAPSHOT);
1804
1801
  try {
1805
1802
  const builder = createSqlBuilder().select().from(fullTableName);
1806
1803
  const countBuilder = createSqlBuilder().count().from(fullTableName);
1807
1804
  if (workflowName) builder.whereAnd("workflow_name = ?", workflowName);
1805
+ if (status) {
1806
+ builder.whereAnd("json_extract(snapshot, '$.status') = ?", status);
1807
+ countBuilder.whereAnd("json_extract(snapshot, '$.status') = ?", status);
1808
+ }
1808
1809
  if (resourceId) {
1809
1810
  const hasResourceId = await this.operations.hasColumn(fullTableName, "resourceId");
1810
1811
  if (hasResourceId) {
@@ -2064,15 +2065,8 @@ var D1Store = class extends MastraStorage {
2064
2065
  async loadWorkflowSnapshot(params) {
2065
2066
  return this.stores.workflows.loadWorkflowSnapshot(params);
2066
2067
  }
2067
- async listWorkflowRuns({
2068
- workflowName,
2069
- fromDate,
2070
- toDate,
2071
- perPage,
2072
- page,
2073
- resourceId
2074
- } = {}) {
2075
- return this.stores.workflows.listWorkflowRuns({ workflowName, fromDate, toDate, perPage, page, resourceId });
2068
+ async listWorkflowRuns(args = {}) {
2069
+ return this.stores.workflows.listWorkflowRuns(args);
2076
2070
  }
2077
2071
  async getWorkflowRunById({
2078
2072
  runId,