@mastra/pg 1.21.1-alpha.0 → 1.21.1

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
@@ -6314,7 +6314,7 @@ var DatasetsPG = class DatasetsPG extends DatasetsStorage {
6314
6314
  schemaName: getSchemaName$5(this.#schema)
6315
6315
  });
6316
6316
  let result;
6317
- if (args.datasetVersion !== void 0) result = await this.#db.client.oneOrNone(`SELECT * FROM ${tableName} WHERE "id" = $1 AND "datasetVersion" = $2 AND "isDeleted" = false`, [args.id, args.datasetVersion]);
6317
+ if (args.datasetVersion !== void 0) result = await this.#db.client.oneOrNone(`SELECT * FROM ${tableName} WHERE "id" = $1 AND "datasetVersion" <= $2 AND ("validTo" IS NULL OR "validTo" > $2) AND "isDeleted" = false ORDER BY "datasetVersion" DESC LIMIT 1`, [args.id, args.datasetVersion]);
6318
6318
  else result = await this.#db.client.oneOrNone(`SELECT * FROM ${tableName} WHERE "id" = $1 AND "validTo" IS NULL AND "isDeleted" = false`, [args.id]);
6319
6319
  return result ? this.transformItemRow(result) : null;
6320
6320
  } catch (error) {
@@ -6572,7 +6572,8 @@ var ExperimentsPG = class ExperimentsPG extends ExperimentsStorage {
6572
6572
  "experimentSetId",
6573
6573
  "comparisonId",
6574
6574
  "variantId",
6575
- "trialIndex"
6575
+ "trialIndex",
6576
+ "scorerIds"
6576
6577
  ]
6577
6578
  });
6578
6579
  await this.#db.alterTable({
@@ -6584,7 +6585,8 @@ var ExperimentsPG = class ExperimentsPG extends ExperimentsStorage {
6584
6585
  "comment",
6585
6586
  "toolMockReport",
6586
6587
  "organizationId",
6587
- "projectId"
6588
+ "projectId",
6589
+ "attempt"
6588
6590
  ]
6589
6591
  });
6590
6592
  await this.createDefaultIndexes();
@@ -6694,9 +6696,13 @@ var ExperimentsPG = class ExperimentsPG extends ExperimentsStorage {
6694
6696
  columns: ["experimentId"]
6695
6697
  },
6696
6698
  {
6697
- name: "idx_experiment_results_exp_item",
6699
+ name: "idx_experiment_results_exp_item_attempt",
6698
6700
  table: TABLE_EXPERIMENT_RESULTS,
6699
- columns: ["experimentId", "itemId"],
6701
+ columns: [
6702
+ "experimentId",
6703
+ "itemId",
6704
+ "attempt"
6705
+ ],
6700
6706
  unique: true
6701
6707
  },
6702
6708
  {
@@ -6713,6 +6719,11 @@ var ExperimentsPG = class ExperimentsPG extends ExperimentsStorage {
6713
6719
  }
6714
6720
  async createDefaultIndexes() {
6715
6721
  if (this.#skipDefaultIndexes) return;
6722
+ try {
6723
+ await this.#db.dropIndex("idx_experiment_results_exp_item");
6724
+ } catch (error) {
6725
+ this.logger?.warn?.("Failed to drop legacy index idx_experiment_results_exp_item:", error);
6726
+ }
6716
6727
  for (const indexDef of this.getDefaultIndexDefinitions()) try {
6717
6728
  await this.#db.createIndex(indexDef);
6718
6729
  } catch (error) {
@@ -6744,8 +6755,9 @@ var ExperimentsPG = class ExperimentsPG extends ExperimentsStorage {
6744
6755
  agentVersion: row.agentVersion ?? null,
6745
6756
  organizationId: row.organizationId ?? null,
6746
6757
  projectId: row.projectId ?? null,
6747
- targetType: row.targetType,
6748
- targetId: row.targetId,
6758
+ targetType: row.targetType ?? null,
6759
+ targetId: row.targetId ?? null,
6760
+ scorerIds: row.scorerIds ? safelyParseJSON(row.scorerIds) : null,
6749
6761
  status: row.status,
6750
6762
  totalItems: row.totalItems,
6751
6763
  succeededCount: row.succeededCount,
@@ -6772,6 +6784,7 @@ var ExperimentsPG = class ExperimentsPG extends ExperimentsStorage {
6772
6784
  startedAt: ensureDate(row.startedAtZ || row.startedAt),
6773
6785
  completedAt: ensureDate(row.completedAtZ || row.completedAt),
6774
6786
  retryCount: row.retryCount,
6787
+ attempt: row.attempt != null ? row.attempt : 0,
6775
6788
  traceId: row.traceId ?? null,
6776
6789
  status: row.status ?? null,
6777
6790
  tags: row.tags ? safelyParseJSON(row.tags) : null,
@@ -6803,8 +6816,9 @@ var ExperimentsPG = class ExperimentsPG extends ExperimentsStorage {
6803
6816
  agentVersion: input.agentVersion ?? null,
6804
6817
  organizationId: input.organizationId ?? null,
6805
6818
  projectId: input.projectId ?? null,
6806
- targetType: input.targetType,
6807
- targetId: input.targetId,
6819
+ targetType: input.targetType ?? null,
6820
+ targetId: input.targetId ?? null,
6821
+ scorerIds: input.scorerIds ?? null,
6808
6822
  status: "pending",
6809
6823
  totalItems: input.totalItems,
6810
6824
  succeededCount: 0,
@@ -6832,8 +6846,9 @@ var ExperimentsPG = class ExperimentsPG extends ExperimentsStorage {
6832
6846
  agentVersion: input.agentVersion ?? null,
6833
6847
  organizationId: input.organizationId ?? null,
6834
6848
  projectId: input.projectId ?? null,
6835
- targetType: input.targetType,
6836
- targetId: input.targetId,
6849
+ targetType: input.targetType ?? null,
6850
+ targetId: input.targetId ?? null,
6851
+ scorerIds: input.scorerIds ?? null,
6837
6852
  status: "pending",
6838
6853
  totalItems: input.totalItems,
6839
6854
  succeededCount: 0,
@@ -7077,6 +7092,7 @@ var ExperimentsPG = class ExperimentsPG extends ExperimentsStorage {
7077
7092
  startedAt: input.startedAt.toISOString(),
7078
7093
  completedAt: input.completedAt.toISOString(),
7079
7094
  retryCount: input.retryCount,
7095
+ attempt: input.attempt ?? 0,
7080
7096
  traceId: input.traceId ?? null,
7081
7097
  status: input.status ?? null,
7082
7098
  tags: input.tags ?? null,
@@ -7098,6 +7114,7 @@ var ExperimentsPG = class ExperimentsPG extends ExperimentsStorage {
7098
7114
  startedAt: input.startedAt,
7099
7115
  completedAt: input.completedAt,
7100
7116
  retryCount: input.retryCount,
7117
+ attempt: input.attempt ?? 0,
7101
7118
  traceId: input.traceId ?? null,
7102
7119
  status: input.status ?? null,
7103
7120
  tags: input.tags ?? null,
@@ -7112,6 +7129,81 @@ var ExperimentsPG = class ExperimentsPG extends ExperimentsStorage {
7112
7129
  }, error);
7113
7130
  }
7114
7131
  }
7132
+ async upsertExperimentResult(input) {
7133
+ try {
7134
+ const tableName = getTableName$5({
7135
+ indexName: TABLE_EXPERIMENT_RESULTS,
7136
+ schemaName: getSchemaName$5(this.#schema)
7137
+ });
7138
+ const attempt = input.attempt ?? 0;
7139
+ const row = await this.#db.client.tx(async (t) => {
7140
+ const existing = await t.oneOrNone(`SELECT "id" FROM ${tableName} WHERE "experimentId" = $1 AND "itemId" = $2 AND COALESCE("attempt", 0) = $3 FOR UPDATE`, [
7141
+ input.experimentId,
7142
+ input.itemId,
7143
+ attempt
7144
+ ]);
7145
+ if (!existing) {
7146
+ const id = crypto.randomUUID();
7147
+ return t.one(`INSERT INTO ${tableName} (
7148
+ "id", "experimentId", "itemId", "itemDatasetVersion", "organizationId", "projectId",
7149
+ "input", "output", "groundTruth", "error", "startedAt", "completedAt",
7150
+ "retryCount", "attempt", "traceId", "status", "tags", "toolMockReport", "createdAt"
7151
+ ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19)
7152
+ RETURNING *`, [
7153
+ id,
7154
+ input.experimentId,
7155
+ input.itemId,
7156
+ input.itemDatasetVersion ?? null,
7157
+ input.organizationId ?? null,
7158
+ input.projectId ?? null,
7159
+ JSON.stringify(input.input),
7160
+ input.output != null ? JSON.stringify(input.output) : null,
7161
+ input.groundTruth != null ? JSON.stringify(input.groundTruth) : null,
7162
+ input.error != null ? JSON.stringify(input.error) : null,
7163
+ input.startedAt.toISOString(),
7164
+ input.completedAt.toISOString(),
7165
+ input.retryCount,
7166
+ attempt,
7167
+ input.traceId ?? null,
7168
+ input.status ?? null,
7169
+ input.tags != null ? JSON.stringify(input.tags) : null,
7170
+ input.toolMockReport != null ? JSON.stringify(input.toolMockReport) : null,
7171
+ (/* @__PURE__ */ new Date()).toISOString()
7172
+ ]);
7173
+ }
7174
+ return t.one(`UPDATE ${tableName} SET
7175
+ "itemDatasetVersion" = $2, "organizationId" = $3, "projectId" = $4,
7176
+ "input" = $5, "output" = $6, "groundTruth" = $7, "error" = $8,
7177
+ "startedAt" = $9, "completedAt" = $10, "retryCount" = $11, "attempt" = $12,
7178
+ "traceId" = $13, "status" = $14, "tags" = $15, "toolMockReport" = $16
7179
+ WHERE "id" = $1 RETURNING *`, [
7180
+ existing.id,
7181
+ input.itemDatasetVersion ?? null,
7182
+ input.organizationId ?? null,
7183
+ input.projectId ?? null,
7184
+ JSON.stringify(input.input),
7185
+ input.output != null ? JSON.stringify(input.output) : null,
7186
+ input.groundTruth != null ? JSON.stringify(input.groundTruth) : null,
7187
+ input.error != null ? JSON.stringify(input.error) : null,
7188
+ input.startedAt.toISOString(),
7189
+ input.completedAt.toISOString(),
7190
+ input.retryCount,
7191
+ attempt,
7192
+ input.traceId ?? null,
7193
+ input.status ?? null,
7194
+ input.tags != null ? JSON.stringify(input.tags) : null,
7195
+ input.toolMockReport != null ? JSON.stringify(input.toolMockReport) : null
7196
+ ]);
7197
+ });
7198
+ return this.transformExperimentResultRow(row);
7199
+ } catch (error) {
7200
+ throw new MastraError({
7201
+ id: createStorageErrorId("PG", "UPSERT_EXPERIMENT_RESULT", "FAILED"),
7202
+ domain: ErrorDomain.STORAGE,
7203
+ category: ErrorCategory.THIRD_PARTY
7204
+ }, error);
7205
+ }
7206
+ }
7115
7207
  async updateExperimentResult(input) {
7116
7208
  try {
7117
7209
  const tableName = getTableName$5({
@@ -18916,9 +19008,14 @@ var ScoresPG = class ScoresPG extends ScoresStorage {
18916
19008
  }, error);
18917
19009
  }
18918
19010
  try {
18919
- const id = crypto.randomUUID();
19011
+ const suppliedId = parsedScore.id;
19012
+ const id = suppliedId ?? crypto.randomUUID();
18920
19013
  const now = /* @__PURE__ */ new Date();
18921
- const { scorer, preprocessStepResult, analyzeStepResult, metadata, input, output, additionalContext, requestContext, entity, ...rest } = parsedScore;
19014
+ const { id: _suppliedId, scorer, preprocessStepResult, analyzeStepResult, metadata, input, output, additionalContext, requestContext, entity, ...rest } = parsedScore;
19015
+ if (suppliedId) await this.#db.client.none(`DELETE FROM ${getTableName$1({
19016
+ indexName: TABLE_SCORERS,
19017
+ schemaName: getSchemaName$1(this.#schema)
19018
+ })} WHERE id = $1`, [suppliedId]);
18922
19019
  await this.#db.insert({
18923
19020
  tableName: TABLE_SCORERS,
18924
19021
  record: {