@mastra/dsql 1.1.1 → 1.1.2-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,42 @@
1
1
  # @mastra/dsql
2
2
 
3
+ ## 1.1.2-alpha.0
4
+
5
+ ### Patch Changes
6
+
7
+ - Add optional `batchId`, `datasetId`, and `datasetItemId` fields to persisted scores so saved baseline scores can be grouped as one scoring pass and joined back to the dataset items they came from. ([#18331](https://github.com/mastra-ai/mastra/pull/18331))
8
+ - `scoreTrace()` accepts top-level `batchId`, `datasetId`, and `datasetItemId` when persisting a score for a stored trace.
9
+ - `ScoreRowData` and score save payloads now include nullable `batchId`, `datasetId`, and `datasetItemId`.
10
+ - Built-in stores with explicit score schema or attribute mappings now persist these provenance fields on saved scores.
11
+ - D1, DSQL, MSSQL, and Upstash score stores now apply additive provenance migrations or deterministic score ordering for persisted score reads.
12
+
13
+ ```ts
14
+ await scoreTrace({
15
+ storage,
16
+ scorer,
17
+ target: { traceId },
18
+ batchId: 'baseline-batch-1',
19
+ datasetId,
20
+ datasetItemId,
21
+ });
22
+ ```
23
+
24
+ - Added optional `organizationId` and `projectId` fields to scores for multi-tenant isolation. Scores can now be saved with tenancy metadata and the `listScoresBy*` methods accept a `filters` option to scope results by organization and project. ([#18331](https://github.com/mastra-ai/mastra/pull/18331))
25
+
26
+ ```ts
27
+ await storage.saveScore({ ...score, organizationId: 'org-a', projectId: 'proj-1' });
28
+
29
+ const result = await storage.listScoresByScorerId({
30
+ scorerId,
31
+ filters: { organizationId: 'org-a', projectId: 'proj-1' },
32
+ });
33
+ ```
34
+
35
+ `projectId` identifies the project scope, separate from `resourceId` which continues to mean the agent memory resource.
36
+
37
+ - Updated dependencies [[`9250acd`](https://github.com/mastra-ai/mastra/commit/9250acd1357f0f1f33d0dcca16f9655084c58eca), [`215f9b0`](https://github.com/mastra-ai/mastra/commit/215f9b0f3f3f6fc165edad360582dd4d3d7ea748), [`c64c2a8`](https://github.com/mastra-ai/mastra/commit/c64c2a8503a50252f9ca6b8e8c54cadee31b92a2), [`06e2680`](https://github.com/mastra-ai/mastra/commit/06e26806b51d2cbd858afdc66daa2b86ff3ba64a), [`1240f05`](https://github.com/mastra-ai/mastra/commit/1240f051c8e5371f1c014448bf37b1a1b9a05e47), [`215f9b0`](https://github.com/mastra-ai/mastra/commit/215f9b0f3f3f6fc165edad360582dd4d3d7ea748), [`24c10d3`](https://github.com/mastra-ai/mastra/commit/24c10d333e6649ac06075903aeeee13a933db3b3), [`24c10d3`](https://github.com/mastra-ai/mastra/commit/24c10d333e6649ac06075903aeeee13a933db3b3), [`24c10d3`](https://github.com/mastra-ai/mastra/commit/24c10d333e6649ac06075903aeeee13a933db3b3), [`24c10d3`](https://github.com/mastra-ai/mastra/commit/24c10d333e6649ac06075903aeeee13a933db3b3), [`215f9b0`](https://github.com/mastra-ai/mastra/commit/215f9b0f3f3f6fc165edad360582dd4d3d7ea748), [`215f9b0`](https://github.com/mastra-ai/mastra/commit/215f9b0f3f3f6fc165edad360582dd4d3d7ea748)]:
38
+ - @mastra/core@1.49.0-alpha.5
39
+
3
40
  ## 1.1.1
4
41
 
5
42
  ### Patch Changes
package/dist/index.cjs CHANGED
@@ -3566,6 +3566,17 @@ function transformScoreRow(row) {
3566
3566
  }
3567
3567
  });
3568
3568
  }
3569
+ function applyTenancyFilters(conditions, queryParams, paramIndex, filters) {
3570
+ if (filters?.organizationId !== void 0) {
3571
+ conditions.push(`"organizationId" = $${paramIndex++}`);
3572
+ queryParams.push(filters.organizationId);
3573
+ }
3574
+ if (filters?.projectId !== void 0) {
3575
+ conditions.push(`"projectId" = $${paramIndex++}`);
3576
+ queryParams.push(filters.projectId);
3577
+ }
3578
+ return paramIndex;
3579
+ }
3569
3580
  var ScoresDSQL = class _ScoresDSQL extends storage.ScoresStorage {
3570
3581
  #db;
3571
3582
  #schema;
@@ -3583,6 +3594,11 @@ var ScoresDSQL = class _ScoresDSQL extends storage.ScoresStorage {
3583
3594
  }
3584
3595
  async init() {
3585
3596
  await this.#db.createTable({ tableName: storage.TABLE_SCORERS, schema: storage.TABLE_SCHEMAS[storage.TABLE_SCORERS] });
3597
+ await this.#db.alterTable({
3598
+ tableName: storage.TABLE_SCORERS,
3599
+ schema: storage.TABLE_SCHEMAS[storage.TABLE_SCORERS],
3600
+ ifNotExists: ["organizationId", "projectId", "batchId", "datasetId", "datasetItemId"]
3601
+ });
3586
3602
  await this.createDefaultIndexes();
3587
3603
  await this.createCustomIndexes();
3588
3604
  }
@@ -3656,7 +3672,8 @@ var ScoresDSQL = class _ScoresDSQL extends storage.ScoresStorage {
3656
3672
  pagination,
3657
3673
  entityId,
3658
3674
  entityType,
3659
- source
3675
+ source,
3676
+ filters
3660
3677
  }) {
3661
3678
  try {
3662
3679
  const conditions = [`"scorerId" = $1`];
@@ -3674,6 +3691,7 @@ var ScoresDSQL = class _ScoresDSQL extends storage.ScoresStorage {
3674
3691
  conditions.push(`"source" = $${paramIndex++}`);
3675
3692
  queryParams.push(source);
3676
3693
  }
3694
+ paramIndex = applyTenancyFilters(conditions, queryParams, paramIndex, filters);
3677
3695
  const whereClause = conditions.join(" AND ");
3678
3696
  const total = await this.#db.client.oneOrNone(
3679
3697
  `SELECT COUNT(*) FROM ${getTableName2({ indexName: storage.TABLE_SCORERS, schemaName: getSchemaName2(this.#schema) })} WHERE ${whereClause}`,
@@ -3787,12 +3805,18 @@ var ScoresDSQL = class _ScoresDSQL extends storage.ScoresStorage {
3787
3805
  }
3788
3806
  async listScoresByRunId({
3789
3807
  runId,
3790
- pagination
3808
+ pagination,
3809
+ filters
3791
3810
  }) {
3792
3811
  try {
3812
+ const conditions = [`"runId" = $1`];
3813
+ const queryParams = [runId];
3814
+ let paramIndex = 2;
3815
+ paramIndex = applyTenancyFilters(conditions, queryParams, paramIndex, filters);
3816
+ const whereClause = conditions.join(" AND ");
3793
3817
  const total = await this.#db.client.oneOrNone(
3794
- `SELECT COUNT(*) FROM ${getTableName2({ indexName: storage.TABLE_SCORERS, schemaName: getSchemaName2(this.#schema) })} WHERE "runId" = $1`,
3795
- [runId]
3818
+ `SELECT COUNT(*) FROM ${getTableName2({ indexName: storage.TABLE_SCORERS, schemaName: getSchemaName2(this.#schema) })} WHERE ${whereClause}`,
3819
+ queryParams
3796
3820
  );
3797
3821
  const { page, perPage: perPageInput } = pagination;
3798
3822
  const perPage = storage.normalizePerPage(perPageInput, 100);
@@ -3811,8 +3835,8 @@ var ScoresDSQL = class _ScoresDSQL extends storage.ScoresStorage {
3811
3835
  const limitValue = perPageInput === false ? Number(total?.count) : perPage;
3812
3836
  const end = perPageInput === false ? Number(total?.count) : start + perPage;
3813
3837
  const result = await this.#db.client.manyOrNone(
3814
- `SELECT * FROM ${getTableName2({ indexName: storage.TABLE_SCORERS, schemaName: getSchemaName2(this.#schema) })} WHERE "runId" = $1 ORDER BY "createdAt" DESC LIMIT $2 OFFSET $3`,
3815
- [runId, limitValue, start]
3838
+ `SELECT * FROM ${getTableName2({ indexName: storage.TABLE_SCORERS, schemaName: getSchemaName2(this.#schema) })} WHERE ${whereClause} ORDER BY "createdAt" DESC LIMIT $${paramIndex++} OFFSET $${paramIndex++}`,
3839
+ [...queryParams, limitValue, start]
3816
3840
  );
3817
3841
  return {
3818
3842
  pagination: {
@@ -3837,12 +3861,18 @@ var ScoresDSQL = class _ScoresDSQL extends storage.ScoresStorage {
3837
3861
  async listScoresByEntityId({
3838
3862
  entityId,
3839
3863
  entityType,
3840
- pagination
3864
+ pagination,
3865
+ filters
3841
3866
  }) {
3842
3867
  try {
3868
+ const conditions = [`"entityId" = $1`, `"entityType" = $2`];
3869
+ const queryParams = [entityId, entityType];
3870
+ let paramIndex = 3;
3871
+ paramIndex = applyTenancyFilters(conditions, queryParams, paramIndex, filters);
3872
+ const whereClause = conditions.join(" AND ");
3843
3873
  const total = await this.#db.client.oneOrNone(
3844
- `SELECT COUNT(*) FROM ${getTableName2({ indexName: storage.TABLE_SCORERS, schemaName: getSchemaName2(this.#schema) })} WHERE "entityId" = $1 AND "entityType" = $2`,
3845
- [entityId, entityType]
3874
+ `SELECT COUNT(*) FROM ${getTableName2({ indexName: storage.TABLE_SCORERS, schemaName: getSchemaName2(this.#schema) })} WHERE ${whereClause}`,
3875
+ queryParams
3846
3876
  );
3847
3877
  const { page, perPage: perPageInput } = pagination;
3848
3878
  const perPage = storage.normalizePerPage(perPageInput, 100);
@@ -3861,8 +3891,8 @@ var ScoresDSQL = class _ScoresDSQL extends storage.ScoresStorage {
3861
3891
  const limitValue = perPageInput === false ? Number(total?.count) : perPage;
3862
3892
  const end = perPageInput === false ? Number(total?.count) : start + perPage;
3863
3893
  const result = await this.#db.client.manyOrNone(
3864
- `SELECT * FROM ${getTableName2({ indexName: storage.TABLE_SCORERS, schemaName: getSchemaName2(this.#schema) })} WHERE "entityId" = $1 AND "entityType" = $2 ORDER BY "createdAt" DESC LIMIT $3 OFFSET $4`,
3865
- [entityId, entityType, limitValue, start]
3894
+ `SELECT * FROM ${getTableName2({ indexName: storage.TABLE_SCORERS, schemaName: getSchemaName2(this.#schema) })} WHERE ${whereClause} ORDER BY "createdAt" DESC LIMIT $${paramIndex++} OFFSET $${paramIndex++}`,
3895
+ [...queryParams, limitValue, start]
3866
3896
  );
3867
3897
  return {
3868
3898
  pagination: {
@@ -3887,13 +3917,19 @@ var ScoresDSQL = class _ScoresDSQL extends storage.ScoresStorage {
3887
3917
  async listScoresBySpan({
3888
3918
  traceId,
3889
3919
  spanId,
3890
- pagination
3920
+ pagination,
3921
+ filters
3891
3922
  }) {
3892
3923
  try {
3893
3924
  const tableName = getTableName2({ indexName: storage.TABLE_SCORERS, schemaName: getSchemaName2(this.#schema) });
3925
+ const conditions = [`"traceId" = $1`, `"spanId" = $2`];
3926
+ const queryParams = [traceId, spanId];
3927
+ let paramIndex = 3;
3928
+ paramIndex = applyTenancyFilters(conditions, queryParams, paramIndex, filters);
3929
+ const whereClause = conditions.join(" AND ");
3894
3930
  const countSQLResult = await this.#db.client.oneOrNone(
3895
- `SELECT COUNT(*) as count FROM ${tableName} WHERE "traceId" = $1 AND "spanId" = $2`,
3896
- [traceId, spanId]
3931
+ `SELECT COUNT(*) as count FROM ${tableName} WHERE ${whereClause}`,
3932
+ queryParams
3897
3933
  );
3898
3934
  const total = Number(countSQLResult?.count ?? 0);
3899
3935
  const { page, perPage: perPageInput } = pagination;
@@ -3902,8 +3938,8 @@ var ScoresDSQL = class _ScoresDSQL extends storage.ScoresStorage {
3902
3938
  const limitValue = perPageInput === false ? total : perPage;
3903
3939
  const end = perPageInput === false ? total : start + perPage;
3904
3940
  const result = await this.#db.client.manyOrNone(
3905
- `SELECT * FROM ${tableName} WHERE "traceId" = $1 AND "spanId" = $2 ORDER BY "createdAt" DESC LIMIT $3 OFFSET $4`,
3906
- [traceId, spanId, limitValue, start]
3941
+ `SELECT * FROM ${tableName} WHERE ${whereClause} ORDER BY "createdAt" DESC LIMIT $${paramIndex++} OFFSET $${paramIndex++}`,
3942
+ [...queryParams, limitValue, start]
3907
3943
  );
3908
3944
  const hasMore = end < total;
3909
3945
  const scores = result.map((row) => transformScoreRow(row)) ?? [];