@mastra/cloudflare-d1 0.0.0-scorers-api-v2-20250801171841 → 0.0.0-scorers-ui-refactored-20250916094952

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.cjs CHANGED
@@ -540,7 +540,6 @@ var MemoryStorageD1 = class extends storage.MemoryStorage {
540
540
  keys: { id: threadId }
541
541
  });
542
542
  if (!thread) return null;
543
- console.log("thread", thread);
544
543
  try {
545
544
  return {
546
545
  ...thread,
@@ -809,6 +808,7 @@ var MemoryStorageD1 = class extends storage.MemoryStorage {
809
808
  }
810
809
  }
811
810
  async _getIncludedMessages(threadId, selectBy) {
811
+ if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
812
812
  const include = selectBy?.include;
813
813
  if (!include) return null;
814
814
  const unionQueries = [];
@@ -867,17 +867,19 @@ var MemoryStorageD1 = class extends storage.MemoryStorage {
867
867
  }
868
868
  async getMessages({
869
869
  threadId,
870
+ resourceId,
870
871
  selectBy,
871
872
  format
872
873
  }) {
873
- const fullTableName = this.operations.getTableName(storage.TABLE_MESSAGES);
874
- const limit = storage.resolveMessageLimit({
875
- last: selectBy?.last,
876
- defaultLimit: 40
877
- });
878
- const include = selectBy?.include || [];
879
- const messages = [];
880
874
  try {
875
+ if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
876
+ const fullTableName = this.operations.getTableName(storage.TABLE_MESSAGES);
877
+ const limit = storage.resolveMessageLimit({
878
+ last: selectBy?.last,
879
+ defaultLimit: 40
880
+ });
881
+ const include = selectBy?.include || [];
882
+ const messages = [];
881
883
  if (include.length) {
882
884
  const includeResult = await this._getIncludedMessages(threadId, selectBy);
883
885
  if (Array.isArray(includeResult)) messages.push(...includeResult);
@@ -917,7 +919,48 @@ var MemoryStorageD1 = class extends storage.MemoryStorage {
917
919
  domain: error.ErrorDomain.STORAGE,
918
920
  category: error.ErrorCategory.THIRD_PARTY,
919
921
  text: `Failed to retrieve messages for thread ${threadId}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
920
- details: { threadId }
922
+ details: { threadId, resourceId: resourceId ?? "" }
923
+ },
924
+ error$1
925
+ );
926
+ this.logger?.error(mastraError.toString());
927
+ this.logger?.trackException(mastraError);
928
+ throw mastraError;
929
+ }
930
+ }
931
+ async getMessagesById({
932
+ messageIds,
933
+ format
934
+ }) {
935
+ if (messageIds.length === 0) return [];
936
+ const fullTableName = this.operations.getTableName(storage.TABLE_MESSAGES);
937
+ const messages = [];
938
+ try {
939
+ const query = createSqlBuilder().select(["id", "content", "role", "type", "createdAt", "thread_id AS threadId", "resourceId"]).from(fullTableName).where(`id in (${messageIds.map(() => "?").join(",")})`, ...messageIds);
940
+ query.orderBy("createdAt", "DESC");
941
+ const { sql, params } = query.build();
942
+ const result = await this.operations.executeQuery({ sql, params });
943
+ if (Array.isArray(result)) messages.push(...result);
944
+ const processedMessages = messages.map((message) => {
945
+ const processedMsg = {};
946
+ for (const [key, value] of Object.entries(message)) {
947
+ if (key === `type` && value === `v2`) continue;
948
+ processedMsg[key] = deserializeValue(value);
949
+ }
950
+ return processedMsg;
951
+ });
952
+ this.logger.debug(`Retrieved ${messages.length} messages`);
953
+ const list = new agent.MessageList().add(processedMessages, "memory");
954
+ if (format === `v1`) return list.get.all.v1();
955
+ return list.get.all.v2();
956
+ } catch (error$1) {
957
+ const mastraError = new error.MastraError(
958
+ {
959
+ id: "CLOUDFLARE_D1_STORAGE_GET_MESSAGES_BY_ID_ERROR",
960
+ domain: error.ErrorDomain.STORAGE,
961
+ category: error.ErrorCategory.THIRD_PARTY,
962
+ text: `Failed to retrieve messages by ID: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
963
+ details: { messageIds: JSON.stringify(messageIds) }
921
964
  },
922
965
  error$1
923
966
  );
@@ -928,6 +971,7 @@ var MemoryStorageD1 = class extends storage.MemoryStorage {
928
971
  }
929
972
  async getMessagesPaginated({
930
973
  threadId,
974
+ resourceId,
931
975
  selectBy,
932
976
  format
933
977
  }) {
@@ -937,6 +981,7 @@ var MemoryStorageD1 = class extends storage.MemoryStorage {
937
981
  const fullTableName = this.operations.getTableName(storage.TABLE_MESSAGES);
938
982
  const messages = [];
939
983
  try {
984
+ if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
940
985
  if (selectBy?.include?.length) {
941
986
  const includeResult = await this._getIncludedMessages(threadId, selectBy);
942
987
  if (Array.isArray(includeResult)) messages.push(...includeResult);
@@ -1025,7 +1070,7 @@ var MemoryStorageD1 = class extends storage.MemoryStorage {
1025
1070
  domain: error.ErrorDomain.STORAGE,
1026
1071
  category: error.ErrorCategory.THIRD_PARTY,
1027
1072
  text: `Failed to retrieve messages for thread ${threadId}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
1028
- details: { threadId }
1073
+ details: { threadId, resourceId: resourceId ?? "" }
1029
1074
  },
1030
1075
  error$1
1031
1076
  );
@@ -1443,6 +1488,7 @@ var StoreOperationsD1 = class extends storage.StoreOperations {
1443
1488
  query.andWhere(`${key} = ?`, value);
1444
1489
  }
1445
1490
  }
1491
+ query.orderBy("createdAt", "DESC");
1446
1492
  query.limit(1);
1447
1493
  const { sql, params } = query.build();
1448
1494
  const result = await this.executeQuery({ sql, params, first: true });
@@ -1527,20 +1573,19 @@ var StoreOperationsD1 = class extends storage.StoreOperations {
1527
1573
  }
1528
1574
  };
1529
1575
  function transformScoreRow(row) {
1530
- let input = void 0;
1531
- if (row.input) {
1532
- try {
1533
- input = JSON.parse(row.input);
1534
- } catch {
1535
- input = row.input;
1536
- }
1537
- }
1538
- return {
1539
- ...row,
1540
- input,
1541
- createdAt: row.createdAtZ || row.createdAt,
1542
- updatedAt: row.updatedAtZ || row.updatedAt
1543
- };
1576
+ const deserialized = { ...row };
1577
+ deserialized.input = storage.safelyParseJSON(row.input);
1578
+ deserialized.output = storage.safelyParseJSON(row.output);
1579
+ deserialized.scorer = storage.safelyParseJSON(row.scorer);
1580
+ deserialized.preprocessStepResult = storage.safelyParseJSON(row.preprocessStepResult);
1581
+ deserialized.analyzeStepResult = storage.safelyParseJSON(row.analyzeStepResult);
1582
+ deserialized.metadata = storage.safelyParseJSON(row.metadata);
1583
+ deserialized.additionalContext = storage.safelyParseJSON(row.additionalContext);
1584
+ deserialized.runtimeContext = storage.safelyParseJSON(row.runtimeContext);
1585
+ deserialized.entity = storage.safelyParseJSON(row.entity);
1586
+ deserialized.createdAt = row.createdAtZ || row.createdAt;
1587
+ deserialized.updatedAt = row.updatedAtZ || row.updatedAt;
1588
+ return deserialized;
1544
1589
  }
1545
1590
  var ScoresStorageD1 = class extends storage.ScoresStorage {
1546
1591
  operations;
@@ -1571,6 +1616,7 @@ var ScoresStorageD1 = class extends storage.ScoresStorage {
1571
1616
  }
1572
1617
  async saveScore(score) {
1573
1618
  try {
1619
+ const id = crypto.randomUUID();
1574
1620
  const fullTableName = this.operations.getTableName(storage.TABLE_SCORERS);
1575
1621
  const { input, ...rest } = score;
1576
1622
  const serializedRecord = {};
@@ -1585,6 +1631,7 @@ var ScoresStorageD1 = class extends storage.ScoresStorage {
1585
1631
  serializedRecord[key] = null;
1586
1632
  }
1587
1633
  }
1634
+ serializedRecord.id = id;
1588
1635
  serializedRecord.input = JSON.stringify(input);
1589
1636
  serializedRecord.createdAt = (/* @__PURE__ */ new Date()).toISOString();
1590
1637
  serializedRecord.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
@@ -1593,7 +1640,7 @@ var ScoresStorageD1 = class extends storage.ScoresStorage {
1593
1640
  const query = createSqlBuilder().insert(fullTableName, columns, values);
1594
1641
  const { sql, params } = query.build();
1595
1642
  await this.operations.executeQuery({ sql, params });
1596
- const scoreFromDb = await this.getScoreById({ id: score.id });
1643
+ const scoreFromDb = await this.getScoreById({ id });
1597
1644
  return { score: scoreFromDb };
1598
1645
  } catch (error$1) {
1599
1646
  throw new error.MastraError(
@@ -1608,11 +1655,23 @@ var ScoresStorageD1 = class extends storage.ScoresStorage {
1608
1655
  }
1609
1656
  async getScoresByScorerId({
1610
1657
  scorerId,
1658
+ entityId,
1659
+ entityType,
1660
+ source,
1611
1661
  pagination
1612
1662
  }) {
1613
1663
  try {
1614
1664
  const fullTableName = this.operations.getTableName(storage.TABLE_SCORERS);
1615
1665
  const countQuery = createSqlBuilder().count().from(fullTableName).where("scorerId = ?", scorerId);
1666
+ if (entityId) {
1667
+ countQuery.andWhere("entityId = ?", entityId);
1668
+ }
1669
+ if (entityType) {
1670
+ countQuery.andWhere("entityType = ?", entityType);
1671
+ }
1672
+ if (source) {
1673
+ countQuery.andWhere("source = ?", source);
1674
+ }
1616
1675
  const countResult = await this.operations.executeQuery(countQuery.build());
1617
1676
  const total = Array.isArray(countResult) ? Number(countResult?.[0]?.count ?? 0) : Number(countResult?.count ?? 0);
1618
1677
  if (total === 0) {
@@ -1626,7 +1685,17 @@ var ScoresStorageD1 = class extends storage.ScoresStorage {
1626
1685
  scores: []
1627
1686
  };
1628
1687
  }
1629
- const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("scorerId = ?", scorerId).limit(pagination.perPage).offset(pagination.page * pagination.perPage);
1688
+ const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("scorerId = ?", scorerId);
1689
+ if (entityId) {
1690
+ selectQuery.andWhere("entityId = ?", entityId);
1691
+ }
1692
+ if (entityType) {
1693
+ selectQuery.andWhere("entityType = ?", entityType);
1694
+ }
1695
+ if (source) {
1696
+ selectQuery.andWhere("source = ?", source);
1697
+ }
1698
+ selectQuery.limit(pagination.perPage).offset(pagination.page * pagination.perPage);
1630
1699
  const { sql, params } = selectQuery.build();
1631
1700
  const results = await this.operations.executeQuery({ sql, params });
1632
1701
  const scores = Array.isArray(results) ? results.map(transformScoreRow) : [];
@@ -1868,9 +1937,26 @@ var WorkflowsStorageD1 = class extends storage.WorkflowsStorage {
1868
1937
  super();
1869
1938
  this.operations = operations;
1870
1939
  }
1940
+ updateWorkflowResults({
1941
+ // workflowName,
1942
+ // runId,
1943
+ // stepId,
1944
+ // result,
1945
+ // runtimeContext,
1946
+ }) {
1947
+ throw new Error("Method not implemented.");
1948
+ }
1949
+ updateWorkflowState({
1950
+ // workflowName,
1951
+ // runId,
1952
+ // opts,
1953
+ }) {
1954
+ throw new Error("Method not implemented.");
1955
+ }
1871
1956
  async persistWorkflowSnapshot({
1872
1957
  workflowName,
1873
1958
  runId,
1959
+ resourceId,
1874
1960
  snapshot
1875
1961
  }) {
1876
1962
  const fullTableName = this.operations.getTableName(storage.TABLE_WORKFLOW_SNAPSHOT);
@@ -1881,11 +1967,13 @@ var WorkflowsStorageD1 = class extends storage.WorkflowsStorage {
1881
1967
  });
1882
1968
  const persisting = currentSnapshot ? {
1883
1969
  ...currentSnapshot,
1970
+ resourceId,
1884
1971
  snapshot: JSON.stringify(snapshot),
1885
1972
  updatedAt: now
1886
1973
  } : {
1887
1974
  workflow_name: workflowName,
1888
1975
  run_id: runId,
1976
+ resourceId,
1889
1977
  snapshot,
1890
1978
  createdAt: now,
1891
1979
  updatedAt: now
@@ -2223,6 +2311,12 @@ var D1Store = class extends storage.MastraStorage {
2223
2311
  }) {
2224
2312
  return this.stores.memory.getMessages({ threadId, selectBy, format });
2225
2313
  }
2314
+ async getMessagesById({
2315
+ messageIds,
2316
+ format
2317
+ }) {
2318
+ return this.stores.memory.getMessagesById({ messageIds, format });
2319
+ }
2226
2320
  async getMessagesPaginated({
2227
2321
  threadId,
2228
2322
  selectBy,
@@ -2230,12 +2324,29 @@ var D1Store = class extends storage.MastraStorage {
2230
2324
  }) {
2231
2325
  return this.stores.memory.getMessagesPaginated({ threadId, selectBy, format });
2232
2326
  }
2327
+ async updateWorkflowResults({
2328
+ workflowName,
2329
+ runId,
2330
+ stepId,
2331
+ result,
2332
+ runtimeContext
2333
+ }) {
2334
+ return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result, runtimeContext });
2335
+ }
2336
+ async updateWorkflowState({
2337
+ workflowName,
2338
+ runId,
2339
+ opts
2340
+ }) {
2341
+ return this.stores.workflows.updateWorkflowState({ workflowName, runId, opts });
2342
+ }
2233
2343
  async persistWorkflowSnapshot({
2234
2344
  workflowName,
2235
2345
  runId,
2346
+ resourceId,
2236
2347
  snapshot
2237
2348
  }) {
2238
- return this.stores.workflows.persistWorkflowSnapshot({ workflowName, runId, snapshot });
2349
+ return this.stores.workflows.persistWorkflowSnapshot({ workflowName, runId, resourceId, snapshot });
2239
2350
  }
2240
2351
  async loadWorkflowSnapshot(params) {
2241
2352
  return this.stores.workflows.loadWorkflowSnapshot(params);
@@ -2322,10 +2433,13 @@ var D1Store = class extends storage.MastraStorage {
2322
2433
  });
2323
2434
  }
2324
2435
  async getScoresByScorerId({
2325
- scorerId: _scorerId,
2326
- pagination: _pagination
2436
+ scorerId,
2437
+ pagination,
2438
+ entityId,
2439
+ entityType,
2440
+ source
2327
2441
  }) {
2328
- return this.stores.scores.getScoresByScorerId({ scorerId: _scorerId, pagination: _pagination });
2442
+ return this.stores.scores.getScoresByScorerId({ scorerId, pagination, entityId, entityType, source });
2329
2443
  }
2330
2444
  /**
2331
2445
  * Close the database connection