@mastra/pg 1.0.0 → 1.1.0-alpha.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.cjs CHANGED
@@ -3117,7 +3117,7 @@ var AgentsPG = class _AgentsPG extends storage.AgentsStorage {
3117
3117
  #skipDefaultIndexes;
3118
3118
  #indexes;
3119
3119
  /** Tables managed by this domain */
3120
- static MANAGED_TABLES = [storage.TABLE_AGENTS];
3120
+ static MANAGED_TABLES = [storage.TABLE_AGENTS, storage.TABLE_AGENT_VERSIONS];
3121
3121
  constructor(config) {
3122
3122
  super();
3123
3123
  const { client, schemaName, skipDefaultIndexes, indexes } = resolvePgConfig(config);
@@ -3144,6 +3144,7 @@ var AgentsPG = class _AgentsPG extends storage.AgentsStorage {
3144
3144
  }
3145
3145
  async init() {
3146
3146
  await this.#db.createTable({ tableName: storage.TABLE_AGENTS, schema: storage.TABLE_SCHEMAS[storage.TABLE_AGENTS] });
3147
+ await this.#db.createTable({ tableName: storage.TABLE_AGENT_VERSIONS, schema: storage.TABLE_SCHEMAS[storage.TABLE_AGENT_VERSIONS] });
3147
3148
  await this.createDefaultIndexes();
3148
3149
  await this.createCustomIndexes();
3149
3150
  }
@@ -3163,6 +3164,7 @@ var AgentsPG = class _AgentsPG extends storage.AgentsStorage {
3163
3164
  }
3164
3165
  }
3165
3166
  async dangerouslyClearAll() {
3167
+ await this.#db.clearTable({ tableName: storage.TABLE_AGENT_VERSIONS });
3166
3168
  await this.#db.clearTable({ tableName: storage.TABLE_AGENTS });
3167
3169
  }
3168
3170
  parseJson(value, fieldName) {
@@ -3192,18 +3194,9 @@ var AgentsPG = class _AgentsPG extends storage.AgentsStorage {
3192
3194
  parseRow(row) {
3193
3195
  return {
3194
3196
  id: row.id,
3195
- name: row.name,
3196
- description: row.description,
3197
- instructions: row.instructions,
3198
- model: this.parseJson(row.model, "model"),
3199
- tools: this.parseJson(row.tools, "tools"),
3200
- defaultOptions: this.parseJson(row.defaultOptions, "defaultOptions"),
3201
- workflows: this.parseJson(row.workflows, "workflows"),
3202
- agents: this.parseJson(row.agents, "agents"),
3203
- inputProcessors: this.parseJson(row.inputProcessors, "inputProcessors"),
3204
- outputProcessors: this.parseJson(row.outputProcessors, "outputProcessors"),
3205
- memory: this.parseJson(row.memory, "memory"),
3206
- scorers: this.parseJson(row.scorers, "scorers"),
3197
+ status: row.status,
3198
+ activeVersionId: row.activeVersionId,
3199
+ authorId: row.authorId,
3207
3200
  metadata: this.parseJson(row.metadata, "metadata"),
3208
3201
  createdAt: row.createdAtZ || row.createdAt,
3209
3202
  updatedAt: row.updatedAtZ || row.updatedAt
@@ -3231,38 +3224,48 @@ var AgentsPG = class _AgentsPG extends storage.AgentsStorage {
3231
3224
  }
3232
3225
  async createAgent({ agent }) {
3233
3226
  try {
3234
- const tableName = getTableName2({ indexName: storage.TABLE_AGENTS, schemaName: getSchemaName2(this.#schema) });
3227
+ const agentsTable = getTableName2({ indexName: storage.TABLE_AGENTS, schemaName: getSchemaName2(this.#schema) });
3235
3228
  const now = /* @__PURE__ */ new Date();
3236
3229
  const nowIso = now.toISOString();
3237
3230
  await this.#db.client.none(
3238
- `INSERT INTO ${tableName} (
3239
- id, name, description, instructions, model, tools,
3240
- "defaultOptions", workflows, agents, "inputProcessors", "outputProcessors", memory, scorers, metadata,
3231
+ `INSERT INTO ${agentsTable} (
3232
+ id, status, "authorId", metadata,
3233
+ "activeVersionId",
3241
3234
  "createdAt", "createdAtZ", "updatedAt", "updatedAtZ"
3242
- ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18)`,
3235
+ ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)`,
3243
3236
  [
3244
3237
  agent.id,
3245
- agent.name,
3246
- agent.description ?? null,
3247
- agent.instructions,
3248
- JSON.stringify(agent.model),
3249
- agent.tools ? JSON.stringify(agent.tools) : null,
3250
- agent.defaultOptions ? JSON.stringify(agent.defaultOptions) : null,
3251
- agent.workflows ? JSON.stringify(agent.workflows) : null,
3252
- agent.agents ? JSON.stringify(agent.agents) : null,
3253
- agent.inputProcessors ? JSON.stringify(agent.inputProcessors) : null,
3254
- agent.outputProcessors ? JSON.stringify(agent.outputProcessors) : null,
3255
- agent.memory ? JSON.stringify(agent.memory) : null,
3256
- agent.scorers ? JSON.stringify(agent.scorers) : null,
3238
+ "draft",
3239
+ agent.authorId ?? null,
3257
3240
  agent.metadata ? JSON.stringify(agent.metadata) : null,
3241
+ null,
3242
+ // activeVersionId starts as null
3258
3243
  nowIso,
3259
3244
  nowIso,
3260
3245
  nowIso,
3261
3246
  nowIso
3262
3247
  ]
3263
3248
  );
3249
+ const { id: _id, authorId: _authorId, metadata: _metadata, ...snapshotConfig } = agent;
3250
+ const versionId = crypto.randomUUID();
3251
+ await this.createVersion({
3252
+ id: versionId,
3253
+ agentId: agent.id,
3254
+ versionNumber: 1,
3255
+ ...snapshotConfig,
3256
+ changedFields: Object.keys(snapshotConfig),
3257
+ changeMessage: "Initial version"
3258
+ });
3259
+ await this.#db.client.none(
3260
+ `UPDATE ${agentsTable} SET "activeVersionId" = $1, status = $2, "updatedAt" = $3, "updatedAtZ" = $4 WHERE id = $5`,
3261
+ [versionId, "published", nowIso, nowIso, agent.id]
3262
+ );
3264
3263
  return {
3265
- ...agent,
3264
+ id: agent.id,
3265
+ status: "published",
3266
+ activeVersionId: versionId,
3267
+ authorId: agent.authorId,
3268
+ metadata: agent.metadata,
3266
3269
  createdAt: now,
3267
3270
  updatedAt: now
3268
3271
  };
@@ -3294,53 +3297,15 @@ var AgentsPG = class _AgentsPG extends storage.AgentsStorage {
3294
3297
  const setClauses = [];
3295
3298
  const values = [];
3296
3299
  let paramIndex = 1;
3297
- if (updates.name !== void 0) {
3298
- setClauses.push(`name = $${paramIndex++}`);
3299
- values.push(updates.name);
3300
- }
3301
- if (updates.description !== void 0) {
3302
- setClauses.push(`description = $${paramIndex++}`);
3303
- values.push(updates.description);
3304
- }
3305
- if (updates.instructions !== void 0) {
3306
- setClauses.push(`instructions = $${paramIndex++}`);
3307
- values.push(updates.instructions);
3308
- }
3309
- if (updates.model !== void 0) {
3310
- setClauses.push(`model = $${paramIndex++}`);
3311
- values.push(JSON.stringify(updates.model));
3312
- }
3313
- if (updates.tools !== void 0) {
3314
- setClauses.push(`tools = $${paramIndex++}`);
3315
- values.push(JSON.stringify(updates.tools));
3316
- }
3317
- if (updates.defaultOptions !== void 0) {
3318
- setClauses.push(`"defaultOptions" = $${paramIndex++}`);
3319
- values.push(JSON.stringify(updates.defaultOptions));
3320
- }
3321
- if (updates.workflows !== void 0) {
3322
- setClauses.push(`workflows = $${paramIndex++}`);
3323
- values.push(JSON.stringify(updates.workflows));
3324
- }
3325
- if (updates.agents !== void 0) {
3326
- setClauses.push(`agents = $${paramIndex++}`);
3327
- values.push(JSON.stringify(updates.agents));
3328
- }
3329
- if (updates.inputProcessors !== void 0) {
3330
- setClauses.push(`"inputProcessors" = $${paramIndex++}`);
3331
- values.push(JSON.stringify(updates.inputProcessors));
3300
+ if (updates.authorId !== void 0) {
3301
+ setClauses.push(`"authorId" = $${paramIndex++}`);
3302
+ values.push(updates.authorId);
3332
3303
  }
3333
- if (updates.outputProcessors !== void 0) {
3334
- setClauses.push(`"outputProcessors" = $${paramIndex++}`);
3335
- values.push(JSON.stringify(updates.outputProcessors));
3336
- }
3337
- if (updates.memory !== void 0) {
3338
- setClauses.push(`memory = $${paramIndex++}`);
3339
- values.push(JSON.stringify(updates.memory));
3340
- }
3341
- if (updates.scorers !== void 0) {
3342
- setClauses.push(`scorers = $${paramIndex++}`);
3343
- values.push(JSON.stringify(updates.scorers));
3304
+ if (updates.activeVersionId !== void 0) {
3305
+ setClauses.push(`"activeVersionId" = $${paramIndex++}`);
3306
+ values.push(updates.activeVersionId);
3307
+ setClauses.push(`status = $${paramIndex++}`);
3308
+ values.push("published");
3344
3309
  }
3345
3310
  if (updates.metadata !== void 0) {
3346
3311
  const mergedMetadata = { ...existingAgent.metadata, ...updates.metadata };
@@ -3388,6 +3353,7 @@ var AgentsPG = class _AgentsPG extends storage.AgentsStorage {
3388
3353
  async deleteAgent({ id }) {
3389
3354
  try {
3390
3355
  const tableName = getTableName2({ indexName: storage.TABLE_AGENTS, schemaName: getSchemaName2(this.#schema) });
3356
+ await this.deleteVersionsByAgentId(id);
3391
3357
  await this.#db.client.none(`DELETE FROM ${tableName} WHERE id = $1`, [id]);
3392
3358
  } catch (error$1) {
3393
3359
  throw new error.MastraError(
@@ -3454,6 +3420,261 @@ var AgentsPG = class _AgentsPG extends storage.AgentsStorage {
3454
3420
  );
3455
3421
  }
3456
3422
  }
3423
+ // ==========================================================================
3424
+ // Agent Version Methods
3425
+ // ==========================================================================
3426
+ async createVersion(input) {
3427
+ try {
3428
+ const tableName = getTableName2({ indexName: storage.TABLE_AGENT_VERSIONS, schemaName: getSchemaName2(this.#schema) });
3429
+ const now = /* @__PURE__ */ new Date();
3430
+ const nowIso = now.toISOString();
3431
+ await this.#db.client.none(
3432
+ `INSERT INTO ${tableName} (
3433
+ id, "agentId", "versionNumber",
3434
+ name, description, instructions, model, tools,
3435
+ "defaultOptions", workflows, agents, "integrationTools",
3436
+ "inputProcessors", "outputProcessors", memory, scorers,
3437
+ "changedFields", "changeMessage",
3438
+ "createdAt", "createdAtZ"
3439
+ ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20)`,
3440
+ [
3441
+ input.id,
3442
+ input.agentId,
3443
+ input.versionNumber,
3444
+ input.name,
3445
+ input.description ?? null,
3446
+ input.instructions,
3447
+ JSON.stringify(input.model),
3448
+ input.tools ? JSON.stringify(input.tools) : null,
3449
+ input.defaultOptions ? JSON.stringify(input.defaultOptions) : null,
3450
+ input.workflows ? JSON.stringify(input.workflows) : null,
3451
+ input.agents ? JSON.stringify(input.agents) : null,
3452
+ input.integrationTools ? JSON.stringify(input.integrationTools) : null,
3453
+ input.inputProcessors ? JSON.stringify(input.inputProcessors) : null,
3454
+ input.outputProcessors ? JSON.stringify(input.outputProcessors) : null,
3455
+ input.memory ? JSON.stringify(input.memory) : null,
3456
+ input.scorers ? JSON.stringify(input.scorers) : null,
3457
+ input.changedFields ? JSON.stringify(input.changedFields) : null,
3458
+ input.changeMessage ?? null,
3459
+ nowIso,
3460
+ nowIso
3461
+ ]
3462
+ );
3463
+ return {
3464
+ ...input,
3465
+ createdAt: now
3466
+ };
3467
+ } catch (error$1) {
3468
+ throw new error.MastraError(
3469
+ {
3470
+ id: storage.createStorageErrorId("PG", "CREATE_VERSION", "FAILED"),
3471
+ domain: error.ErrorDomain.STORAGE,
3472
+ category: error.ErrorCategory.THIRD_PARTY,
3473
+ details: { versionId: input.id, agentId: input.agentId }
3474
+ },
3475
+ error$1
3476
+ );
3477
+ }
3478
+ }
3479
+ async getVersion(id) {
3480
+ try {
3481
+ const tableName = getTableName2({ indexName: storage.TABLE_AGENT_VERSIONS, schemaName: getSchemaName2(this.#schema) });
3482
+ const result = await this.#db.client.oneOrNone(`SELECT * FROM ${tableName} WHERE id = $1`, [id]);
3483
+ if (!result) {
3484
+ return null;
3485
+ }
3486
+ return this.parseVersionRow(result);
3487
+ } catch (error$1) {
3488
+ throw new error.MastraError(
3489
+ {
3490
+ id: storage.createStorageErrorId("PG", "GET_VERSION", "FAILED"),
3491
+ domain: error.ErrorDomain.STORAGE,
3492
+ category: error.ErrorCategory.THIRD_PARTY,
3493
+ details: { versionId: id }
3494
+ },
3495
+ error$1
3496
+ );
3497
+ }
3498
+ }
3499
+ async getVersionByNumber(agentId, versionNumber) {
3500
+ try {
3501
+ const tableName = getTableName2({ indexName: storage.TABLE_AGENT_VERSIONS, schemaName: getSchemaName2(this.#schema) });
3502
+ const result = await this.#db.client.oneOrNone(
3503
+ `SELECT * FROM ${tableName} WHERE "agentId" = $1 AND "versionNumber" = $2`,
3504
+ [agentId, versionNumber]
3505
+ );
3506
+ if (!result) {
3507
+ return null;
3508
+ }
3509
+ return this.parseVersionRow(result);
3510
+ } catch (error$1) {
3511
+ throw new error.MastraError(
3512
+ {
3513
+ id: storage.createStorageErrorId("PG", "GET_VERSION_BY_NUMBER", "FAILED"),
3514
+ domain: error.ErrorDomain.STORAGE,
3515
+ category: error.ErrorCategory.THIRD_PARTY,
3516
+ details: { agentId, versionNumber }
3517
+ },
3518
+ error$1
3519
+ );
3520
+ }
3521
+ }
3522
+ async getLatestVersion(agentId) {
3523
+ try {
3524
+ const tableName = getTableName2({ indexName: storage.TABLE_AGENT_VERSIONS, schemaName: getSchemaName2(this.#schema) });
3525
+ const result = await this.#db.client.oneOrNone(
3526
+ `SELECT * FROM ${tableName} WHERE "agentId" = $1 ORDER BY "versionNumber" DESC LIMIT 1`,
3527
+ [agentId]
3528
+ );
3529
+ if (!result) {
3530
+ return null;
3531
+ }
3532
+ return this.parseVersionRow(result);
3533
+ } catch (error$1) {
3534
+ throw new error.MastraError(
3535
+ {
3536
+ id: storage.createStorageErrorId("PG", "GET_LATEST_VERSION", "FAILED"),
3537
+ domain: error.ErrorDomain.STORAGE,
3538
+ category: error.ErrorCategory.THIRD_PARTY,
3539
+ details: { agentId }
3540
+ },
3541
+ error$1
3542
+ );
3543
+ }
3544
+ }
3545
+ async listVersions(input) {
3546
+ const { agentId, page = 0, perPage: perPageInput, orderBy } = input;
3547
+ if (page < 0) {
3548
+ throw new error.MastraError(
3549
+ {
3550
+ id: storage.createStorageErrorId("PG", "LIST_VERSIONS", "INVALID_PAGE"),
3551
+ domain: error.ErrorDomain.STORAGE,
3552
+ category: error.ErrorCategory.USER,
3553
+ details: { page }
3554
+ },
3555
+ new Error("page must be >= 0")
3556
+ );
3557
+ }
3558
+ const perPage = storage.normalizePerPage(perPageInput, 20);
3559
+ const { offset, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
3560
+ try {
3561
+ const { field, direction } = this.parseVersionOrderBy(orderBy);
3562
+ const tableName = getTableName2({ indexName: storage.TABLE_AGENT_VERSIONS, schemaName: getSchemaName2(this.#schema) });
3563
+ const countResult = await this.#db.client.one(`SELECT COUNT(*) as count FROM ${tableName} WHERE "agentId" = $1`, [
3564
+ agentId
3565
+ ]);
3566
+ const total = parseInt(countResult.count, 10);
3567
+ if (total === 0) {
3568
+ return {
3569
+ versions: [],
3570
+ total: 0,
3571
+ page,
3572
+ perPage: perPageForResponse,
3573
+ hasMore: false
3574
+ };
3575
+ }
3576
+ const limitValue = perPageInput === false ? total : perPage;
3577
+ const dataResult = await this.#db.client.manyOrNone(
3578
+ `SELECT * FROM ${tableName} WHERE "agentId" = $1 ORDER BY "${field}" ${direction} LIMIT $2 OFFSET $3`,
3579
+ [agentId, limitValue, offset]
3580
+ );
3581
+ const versions = (dataResult || []).map((row) => this.parseVersionRow(row));
3582
+ return {
3583
+ versions,
3584
+ total,
3585
+ page,
3586
+ perPage: perPageForResponse,
3587
+ hasMore: perPageInput === false ? false : offset + perPage < total
3588
+ };
3589
+ } catch (error$1) {
3590
+ throw new error.MastraError(
3591
+ {
3592
+ id: storage.createStorageErrorId("PG", "LIST_VERSIONS", "FAILED"),
3593
+ domain: error.ErrorDomain.STORAGE,
3594
+ category: error.ErrorCategory.THIRD_PARTY,
3595
+ details: { agentId }
3596
+ },
3597
+ error$1
3598
+ );
3599
+ }
3600
+ }
3601
+ async deleteVersion(id) {
3602
+ try {
3603
+ const tableName = getTableName2({ indexName: storage.TABLE_AGENT_VERSIONS, schemaName: getSchemaName2(this.#schema) });
3604
+ await this.#db.client.none(`DELETE FROM ${tableName} WHERE id = $1`, [id]);
3605
+ } catch (error$1) {
3606
+ throw new error.MastraError(
3607
+ {
3608
+ id: storage.createStorageErrorId("PG", "DELETE_VERSION", "FAILED"),
3609
+ domain: error.ErrorDomain.STORAGE,
3610
+ category: error.ErrorCategory.THIRD_PARTY,
3611
+ details: { versionId: id }
3612
+ },
3613
+ error$1
3614
+ );
3615
+ }
3616
+ }
3617
+ async deleteVersionsByAgentId(agentId) {
3618
+ try {
3619
+ const tableName = getTableName2({ indexName: storage.TABLE_AGENT_VERSIONS, schemaName: getSchemaName2(this.#schema) });
3620
+ await this.#db.client.none(`DELETE FROM ${tableName} WHERE "agentId" = $1`, [agentId]);
3621
+ } catch (error$1) {
3622
+ throw new error.MastraError(
3623
+ {
3624
+ id: storage.createStorageErrorId("PG", "DELETE_VERSIONS_BY_AGENT_ID", "FAILED"),
3625
+ domain: error.ErrorDomain.STORAGE,
3626
+ category: error.ErrorCategory.THIRD_PARTY,
3627
+ details: { agentId }
3628
+ },
3629
+ error$1
3630
+ );
3631
+ }
3632
+ }
3633
+ async countVersions(agentId) {
3634
+ try {
3635
+ const tableName = getTableName2({ indexName: storage.TABLE_AGENT_VERSIONS, schemaName: getSchemaName2(this.#schema) });
3636
+ const result = await this.#db.client.one(`SELECT COUNT(*) as count FROM ${tableName} WHERE "agentId" = $1`, [
3637
+ agentId
3638
+ ]);
3639
+ return parseInt(result.count, 10);
3640
+ } catch (error$1) {
3641
+ throw new error.MastraError(
3642
+ {
3643
+ id: storage.createStorageErrorId("PG", "COUNT_VERSIONS", "FAILED"),
3644
+ domain: error.ErrorDomain.STORAGE,
3645
+ category: error.ErrorCategory.THIRD_PARTY,
3646
+ details: { agentId }
3647
+ },
3648
+ error$1
3649
+ );
3650
+ }
3651
+ }
3652
+ // ==========================================================================
3653
+ // Private Helper Methods
3654
+ // ==========================================================================
3655
+ parseVersionRow(row) {
3656
+ return {
3657
+ id: row.id,
3658
+ agentId: row.agentId,
3659
+ versionNumber: row.versionNumber,
3660
+ name: row.name,
3661
+ description: row.description,
3662
+ instructions: row.instructions,
3663
+ model: this.parseJson(row.model, "model"),
3664
+ tools: this.parseJson(row.tools, "tools"),
3665
+ defaultOptions: this.parseJson(row.defaultOptions, "defaultOptions"),
3666
+ workflows: this.parseJson(row.workflows, "workflows"),
3667
+ agents: this.parseJson(row.agents, "agents"),
3668
+ integrationTools: this.parseJson(row.integrationTools, "integrationTools"),
3669
+ inputProcessors: this.parseJson(row.inputProcessors, "inputProcessors"),
3670
+ outputProcessors: this.parseJson(row.outputProcessors, "outputProcessors"),
3671
+ memory: this.parseJson(row.memory, "memory"),
3672
+ scorers: this.parseJson(row.scorers, "scorers"),
3673
+ changedFields: this.parseJson(row.changedFields, "changedFields"),
3674
+ changeMessage: row.changeMessage,
3675
+ createdAt: row.createdAtZ || row.createdAt
3676
+ };
3677
+ }
3457
3678
  };
3458
3679
  function getSchemaName3(schema) {
3459
3680
  return schema ? `"${schema}"` : '"public"';
@@ -5040,11 +5261,13 @@ var ObservabilityPG = class _ObservabilityPG extends storage.ObservabilityStorag
5040
5261
  perPage,
5041
5262
  hasMore: (page + 1) * perPage < count
5042
5263
  },
5043
- spans: spans.map(
5044
- (span) => transformFromSqlRow({
5045
- tableName: storage.TABLE_SPANS,
5046
- sqlRow: span
5047
- })
5264
+ spans: storage.toTraceSpans(
5265
+ spans.map(
5266
+ (span) => transformFromSqlRow({
5267
+ tableName: storage.TABLE_SPANS,
5268
+ sqlRow: span
5269
+ })
5270
+ )
5048
5271
  )
5049
5272
  };
5050
5273
  } catch (error$1) {