@mastra/pg 1.10.1-alpha.1 → 1.10.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
@@ -2013,7 +2013,6 @@ var PoolAdapter = class {
2013
2013
  constructor($pool) {
2014
2014
  this.$pool = $pool;
2015
2015
  }
2016
- $pool;
2017
2016
  connect() {
2018
2017
  return this.$pool.connect();
2019
2018
  }
@@ -2082,7 +2081,6 @@ var TransactionClient = class {
2082
2081
  constructor(client) {
2083
2082
  this.client = client;
2084
2083
  }
2085
- client;
2086
2084
  async none(query, values) {
2087
2085
  await this.client.query(query, values);
2088
2086
  return null;
@@ -3426,6 +3424,15 @@ function getTableName2({ indexName, schemaName }) {
3426
3424
  const quotedSchemaName = schemaName;
3427
3425
  return quotedSchemaName ? `${quotedSchemaName}.${quotedIndexName}` : quotedIndexName;
3428
3426
  }
3427
+ function parseJsonResilient(value, _fieldName) {
3428
+ if (value == null) return void 0;
3429
+ if (typeof value !== "string") return value;
3430
+ try {
3431
+ return JSON.parse(value);
3432
+ } catch {
3433
+ return value;
3434
+ }
3435
+ }
3429
3436
  function transformFromSqlRow({
3430
3437
  tableName,
3431
3438
  sqlRow
@@ -3687,38 +3694,13 @@ var AgentsPG = class _AgentsPG extends AgentsStorage {
3687
3694
  await this.#db.clearTable({ tableName: TABLE_AGENT_VERSIONS });
3688
3695
  await this.#db.clearTable({ tableName: TABLE_AGENTS });
3689
3696
  }
3690
- parseJson(value, fieldName) {
3691
- if (!value) return void 0;
3692
- if (typeof value !== "string") return value;
3693
- try {
3694
- return JSON.parse(value);
3695
- } catch (error) {
3696
- if (error instanceof MastraError) throw error;
3697
- const details = {
3698
- value: value.length > 100 ? value.substring(0, 100) + "..." : value
3699
- };
3700
- if (fieldName) {
3701
- details.field = fieldName;
3702
- }
3703
- throw new MastraError(
3704
- {
3705
- id: createStorageErrorId("PG", "PARSE_JSON", "INVALID_JSON"),
3706
- domain: ErrorDomain.STORAGE,
3707
- category: ErrorCategory.SYSTEM,
3708
- text: `Failed to parse JSON${fieldName ? ` for field "${fieldName}"` : ""}: ${error instanceof Error ? error.message : "Unknown error"}`,
3709
- details
3710
- },
3711
- error
3712
- );
3713
- }
3714
- }
3715
3697
  parseRow(row) {
3716
3698
  return {
3717
3699
  id: row.id,
3718
3700
  status: row.status,
3719
3701
  activeVersionId: row.activeVersionId,
3720
3702
  authorId: row.authorId,
3721
- metadata: this.parseJson(row.metadata, "metadata"),
3703
+ metadata: parseJsonResilient(row.metadata),
3722
3704
  createdAt: row.createdAtZ || row.createdAt,
3723
3705
  updatedAt: row.updatedAtZ || row.updatedAt
3724
3706
  };
@@ -3947,7 +3929,14 @@ var AgentsPG = class _AgentsPG extends AgentsStorage {
3947
3929
  `SELECT * FROM ${tableName} ${whereClause} ORDER BY "${field}" ${direction} LIMIT $${paramIdx++} OFFSET $${paramIdx++}`,
3948
3930
  [...queryParams, limitValue, offset]
3949
3931
  );
3950
- const agents = (dataResult || []).map((row) => this.parseRow(row));
3932
+ const agents = (dataResult || []).flatMap((row) => {
3933
+ try {
3934
+ return [this.parseRow(row)];
3935
+ } catch (err) {
3936
+ this.logger?.warn?.("[PG] Failed to map agent row, skipping", { id: row?.id, error: err });
3937
+ return [];
3938
+ }
3939
+ });
3951
3940
  return {
3952
3941
  agents,
3953
3942
  total,
@@ -4135,7 +4124,14 @@ var AgentsPG = class _AgentsPG extends AgentsStorage {
4135
4124
  `SELECT * FROM ${tableName} WHERE "agentId" = $1 ORDER BY "${field}" ${direction} LIMIT $2 OFFSET $3`,
4136
4125
  [agentId, limitValue, offset]
4137
4126
  );
4138
- const versions = (dataResult || []).map((row) => this.parseVersionRow(row));
4127
+ const versions = (dataResult || []).flatMap((row) => {
4128
+ try {
4129
+ return [this.parseVersionRow(row)];
4130
+ } catch (err) {
4131
+ this.logger?.warn?.("[PG] Failed to map agent version row, skipping", { id: row?.id, error: err });
4132
+ return [];
4133
+ }
4134
+ });
4139
4135
  return {
4140
4136
  versions,
4141
4137
  total,
@@ -4234,22 +4230,22 @@ var AgentsPG = class _AgentsPG extends AgentsStorage {
4234
4230
  name: row.name,
4235
4231
  description: row.description,
4236
4232
  instructions: this.deserializeInstructions(row.instructions),
4237
- model: this.parseJson(row.model, "model"),
4238
- tools: this.parseJson(row.tools, "tools"),
4239
- defaultOptions: this.parseJson(row.defaultOptions, "defaultOptions"),
4240
- workflows: this.parseJson(row.workflows, "workflows"),
4241
- agents: this.parseJson(row.agents, "agents"),
4242
- integrationTools: this.parseJson(row.integrationTools, "integrationTools"),
4243
- inputProcessors: this.parseJson(row.inputProcessors, "inputProcessors"),
4244
- outputProcessors: this.parseJson(row.outputProcessors, "outputProcessors"),
4245
- memory: this.parseJson(row.memory, "memory"),
4246
- scorers: this.parseJson(row.scorers, "scorers"),
4247
- mcpClients: this.parseJson(row.mcpClients, "mcpClients"),
4248
- requestContextSchema: this.parseJson(row.requestContextSchema, "requestContextSchema"),
4249
- workspace: this.parseJson(row.workspace, "workspace"),
4250
- skills: this.parseJson(row.skills, "skills"),
4233
+ model: parseJsonResilient(row.model),
4234
+ tools: parseJsonResilient(row.tools),
4235
+ defaultOptions: parseJsonResilient(row.defaultOptions),
4236
+ workflows: parseJsonResilient(row.workflows),
4237
+ agents: parseJsonResilient(row.agents),
4238
+ integrationTools: parseJsonResilient(row.integrationTools),
4239
+ inputProcessors: parseJsonResilient(row.inputProcessors),
4240
+ outputProcessors: parseJsonResilient(row.outputProcessors),
4241
+ memory: parseJsonResilient(row.memory),
4242
+ scorers: parseJsonResilient(row.scorers),
4243
+ mcpClients: parseJsonResilient(row.mcpClients),
4244
+ requestContextSchema: parseJsonResilient(row.requestContextSchema),
4245
+ workspace: parseJsonResilient(row.workspace),
4246
+ skills: parseJsonResilient(row.skills),
4251
4247
  skillsFormat: row.skillsFormat,
4252
- changedFields: this.parseJson(row.changedFields, "changedFields"),
4248
+ changedFields: parseJsonResilient(row.changedFields),
4253
4249
  changeMessage: row.changeMessage,
4254
4250
  createdAt: row.createdAtZ || row.createdAt
4255
4251
  };
@@ -6689,7 +6685,14 @@ var MCPClientsPG = class _MCPClientsPG extends MCPClientsStorage {
6689
6685
  `SELECT * FROM ${tableName} ${whereClause} ORDER BY "${field}" ${direction} LIMIT $${paramIdx++} OFFSET $${paramIdx++}`,
6690
6686
  [...queryParams, limitValue, offset]
6691
6687
  );
6692
- const mcpClients = (dataResult || []).map((row) => this.parseMCPClientRow(row));
6688
+ const mcpClients = (dataResult || []).flatMap((row) => {
6689
+ try {
6690
+ return [this.parseMCPClientRow(row)];
6691
+ } catch (err) {
6692
+ this.logger?.warn?.("[PG] Failed to map mcp client row, skipping", { id: row?.id, error: err });
6693
+ return [];
6694
+ }
6695
+ });
6693
6696
  return {
6694
6697
  mcpClients,
6695
6698
  total,
@@ -6875,7 +6878,14 @@ var MCPClientsPG = class _MCPClientsPG extends MCPClientsStorage {
6875
6878
  `SELECT * FROM ${tableName} WHERE "mcpClientId" = $1 ORDER BY "${field}" ${direction} LIMIT $2 OFFSET $3`,
6876
6879
  [mcpClientId, limitValue, offset]
6877
6880
  );
6878
- const versions = (dataResult || []).map((row) => this.parseVersionRow(row));
6881
+ const versions = (dataResult || []).flatMap((row) => {
6882
+ try {
6883
+ return [this.parseVersionRow(row)];
6884
+ } catch (err) {
6885
+ this.logger?.warn?.("[PG] Failed to map mcp client version row, skipping", { id: row?.id, error: err });
6886
+ return [];
6887
+ }
6888
+ });
6879
6889
  return {
6880
6890
  versions,
6881
6891
  total,
@@ -6962,38 +6972,13 @@ var MCPClientsPG = class _MCPClientsPG extends MCPClientsStorage {
6962
6972
  // ==========================================================================
6963
6973
  // Private Helper Methods
6964
6974
  // ==========================================================================
6965
- parseJson(value, fieldName) {
6966
- if (!value) return void 0;
6967
- if (typeof value !== "string") return value;
6968
- try {
6969
- return JSON.parse(value);
6970
- } catch (error) {
6971
- if (error instanceof MastraError) throw error;
6972
- const details = {
6973
- value: value.length > 100 ? value.substring(0, 100) + "..." : value
6974
- };
6975
- if (fieldName) {
6976
- details.field = fieldName;
6977
- }
6978
- throw new MastraError(
6979
- {
6980
- id: createStorageErrorId("PG", "PARSE_JSON", "INVALID_JSON"),
6981
- domain: ErrorDomain.STORAGE,
6982
- category: ErrorCategory.SYSTEM,
6983
- text: `Failed to parse JSON${fieldName ? ` for field "${fieldName}"` : ""}: ${error instanceof Error ? error.message : "Unknown error"}`,
6984
- details
6985
- },
6986
- error
6987
- );
6988
- }
6989
- }
6990
6975
  parseMCPClientRow(row) {
6991
6976
  return {
6992
6977
  id: row.id,
6993
6978
  status: row.status,
6994
6979
  activeVersionId: row.activeVersionId,
6995
6980
  authorId: row.authorId,
6996
- metadata: this.parseJson(row.metadata, "metadata"),
6981
+ metadata: parseJsonResilient(row.metadata),
6997
6982
  createdAt: new Date(row.createdAtZ || row.createdAt),
6998
6983
  updatedAt: new Date(row.updatedAtZ || row.updatedAt)
6999
6984
  };
@@ -7005,8 +6990,8 @@ var MCPClientsPG = class _MCPClientsPG extends MCPClientsStorage {
7005
6990
  versionNumber: row.versionNumber,
7006
6991
  name: row.name,
7007
6992
  description: row.description,
7008
- servers: this.parseJson(row.servers, "servers"),
7009
- changedFields: this.parseJson(row.changedFields, "changedFields"),
6993
+ servers: parseJsonResilient(row.servers),
6994
+ changedFields: parseJsonResilient(row.changedFields),
7010
6995
  changeMessage: row.changeMessage,
7011
6996
  createdAt: new Date(row.createdAtZ || row.createdAt)
7012
6997
  };
@@ -7313,7 +7298,14 @@ var MCPServersPG = class _MCPServersPG extends MCPServersStorage {
7313
7298
  `SELECT * FROM ${tableName} ${whereClause} ORDER BY "${field}" ${direction} LIMIT $${paramIdx++} OFFSET $${paramIdx++}`,
7314
7299
  [...queryParams, limitValue, offset]
7315
7300
  );
7316
- const mcpServers = (dataResult || []).map((row) => this.parseMCPServerRow(row));
7301
+ const mcpServers = (dataResult || []).flatMap((row) => {
7302
+ try {
7303
+ return [this.parseMCPServerRow(row)];
7304
+ } catch (err) {
7305
+ this.logger?.warn?.("[PG] Failed to map mcp server row, skipping", { id: row?.id, error: err });
7306
+ return [];
7307
+ }
7308
+ });
7317
7309
  return {
7318
7310
  mcpServers,
7319
7311
  total,
@@ -7509,7 +7501,14 @@ var MCPServersPG = class _MCPServersPG extends MCPServersStorage {
7509
7501
  `SELECT * FROM ${tableName} WHERE "mcpServerId" = $1 ORDER BY "${field}" ${direction} LIMIT $2 OFFSET $3`,
7510
7502
  [mcpServerId, limitValue, offset]
7511
7503
  );
7512
- const versions = (dataResult || []).map((row) => this.parseVersionRow(row));
7504
+ const versions = (dataResult || []).flatMap((row) => {
7505
+ try {
7506
+ return [this.parseVersionRow(row)];
7507
+ } catch (err) {
7508
+ this.logger?.warn?.("[PG] Failed to map mcp server version row, skipping", { id: row?.id, error: err });
7509
+ return [];
7510
+ }
7511
+ });
7513
7512
  return {
7514
7513
  versions,
7515
7514
  total,
@@ -7596,38 +7595,13 @@ var MCPServersPG = class _MCPServersPG extends MCPServersStorage {
7596
7595
  // ==========================================================================
7597
7596
  // Private Helper Methods
7598
7597
  // ==========================================================================
7599
- parseJson(value, fieldName) {
7600
- if (!value) return void 0;
7601
- if (typeof value !== "string") return value;
7602
- try {
7603
- return JSON.parse(value);
7604
- } catch (error) {
7605
- if (error instanceof MastraError) throw error;
7606
- const details = {
7607
- valueLength: String(value.length)
7608
- };
7609
- if (fieldName) {
7610
- details.field = fieldName;
7611
- }
7612
- throw new MastraError(
7613
- {
7614
- id: createStorageErrorId("PG", "PARSE_JSON", "INVALID_JSON"),
7615
- domain: ErrorDomain.STORAGE,
7616
- category: ErrorCategory.SYSTEM,
7617
- text: `Failed to parse JSON${fieldName ? ` for field "${fieldName}"` : ""}: ${error instanceof Error ? error.message : "Unknown error"}`,
7618
- details
7619
- },
7620
- error
7621
- );
7622
- }
7623
- }
7624
7598
  parseMCPServerRow(row) {
7625
7599
  return {
7626
7600
  id: row.id,
7627
7601
  status: row.status,
7628
7602
  activeVersionId: row.activeVersionId,
7629
7603
  authorId: row.authorId,
7630
- metadata: this.parseJson(row.metadata, "metadata"),
7604
+ metadata: parseJsonResilient(row.metadata),
7631
7605
  createdAt: new Date(row.createdAtZ || row.createdAt),
7632
7606
  updatedAt: new Date(row.updatedAtZ || row.updatedAt)
7633
7607
  };
@@ -7641,14 +7615,14 @@ var MCPServersPG = class _MCPServersPG extends MCPServersStorage {
7641
7615
  version: row.version,
7642
7616
  description: row.description,
7643
7617
  instructions: row.instructions,
7644
- repository: this.parseJson(row.repository, "repository"),
7618
+ repository: parseJsonResilient(row.repository),
7645
7619
  releaseDate: row.releaseDate,
7646
7620
  isLatest: row.isLatest,
7647
7621
  packageCanonical: row.packageCanonical,
7648
- tools: this.parseJson(row.tools, "tools"),
7649
- agents: this.parseJson(row.agents, "agents"),
7650
- workflows: this.parseJson(row.workflows, "workflows"),
7651
- changedFields: this.parseJson(row.changedFields, "changedFields"),
7622
+ tools: parseJsonResilient(row.tools),
7623
+ agents: parseJsonResilient(row.agents),
7624
+ workflows: parseJsonResilient(row.workflows),
7625
+ changedFields: parseJsonResilient(row.changedFields),
7652
7626
  changeMessage: row.changeMessage,
7653
7627
  createdAt: new Date(row.createdAtZ || row.createdAt)
7654
7628
  };
@@ -11057,7 +11031,14 @@ var PromptBlocksPG = class _PromptBlocksPG extends PromptBlocksStorage {
11057
11031
  `SELECT * FROM ${tableName} ${whereClause} ORDER BY "${field}" ${direction} LIMIT $${paramIdx++} OFFSET $${paramIdx++}`,
11058
11032
  [...queryParams, limitValue, offset]
11059
11033
  );
11060
- const promptBlocks = (dataResult || []).map((row) => this.parseBlockRow(row));
11034
+ const promptBlocks = (dataResult || []).flatMap((row) => {
11035
+ try {
11036
+ return [this.parseBlockRow(row)];
11037
+ } catch (err) {
11038
+ this.logger?.warn?.("[PG] Failed to map prompt block row, skipping", { id: row?.id, error: err });
11039
+ return [];
11040
+ }
11041
+ });
11061
11042
  return {
11062
11043
  promptBlocks,
11063
11044
  total,
@@ -11244,7 +11225,14 @@ var PromptBlocksPG = class _PromptBlocksPG extends PromptBlocksStorage {
11244
11225
  `SELECT * FROM ${tableName} WHERE "blockId" = $1 ORDER BY "${field}" ${direction} LIMIT $2 OFFSET $3`,
11245
11226
  [blockId, limitValue, offset]
11246
11227
  );
11247
- const versions = (dataResult || []).map((row) => this.parseVersionRow(row));
11228
+ const versions = (dataResult || []).flatMap((row) => {
11229
+ try {
11230
+ return [this.parseVersionRow(row)];
11231
+ } catch (err) {
11232
+ this.logger?.warn?.("[PG] Failed to map prompt block version row, skipping", { id: row?.id, error: err });
11233
+ return [];
11234
+ }
11235
+ });
11248
11236
  return {
11249
11237
  versions,
11250
11238
  total,
@@ -11331,37 +11319,13 @@ var PromptBlocksPG = class _PromptBlocksPG extends PromptBlocksStorage {
11331
11319
  // ==========================================================================
11332
11320
  // Private Helper Methods
11333
11321
  // ==========================================================================
11334
- parseJson(value, fieldName) {
11335
- if (!value) return void 0;
11336
- if (typeof value !== "string") return value;
11337
- try {
11338
- return JSON.parse(value);
11339
- } catch (error) {
11340
- const details = {
11341
- value: value.length > 100 ? value.substring(0, 100) + "..." : value
11342
- };
11343
- if (fieldName) {
11344
- details.field = fieldName;
11345
- }
11346
- throw new MastraError(
11347
- {
11348
- id: createStorageErrorId("PG", "PARSE_JSON", "INVALID_JSON"),
11349
- domain: ErrorDomain.STORAGE,
11350
- category: ErrorCategory.SYSTEM,
11351
- text: `Failed to parse JSON${fieldName ? ` for field "${fieldName}"` : ""}: ${error instanceof Error ? error.message : "Unknown error"}`,
11352
- details
11353
- },
11354
- error
11355
- );
11356
- }
11357
- }
11358
11322
  parseBlockRow(row) {
11359
11323
  return {
11360
11324
  id: row.id,
11361
11325
  status: row.status,
11362
11326
  activeVersionId: row.activeVersionId,
11363
11327
  authorId: row.authorId,
11364
- metadata: this.parseJson(row.metadata, "metadata"),
11328
+ metadata: parseJsonResilient(row.metadata),
11365
11329
  createdAt: new Date(row.createdAtZ || row.createdAt),
11366
11330
  updatedAt: new Date(row.updatedAtZ || row.updatedAt)
11367
11331
  };
@@ -11374,9 +11338,9 @@ var PromptBlocksPG = class _PromptBlocksPG extends PromptBlocksStorage {
11374
11338
  name: row.name,
11375
11339
  description: row.description,
11376
11340
  content: row.content,
11377
- rules: this.parseJson(row.rules, "rules"),
11378
- requestContextSchema: this.parseJson(row.requestContextSchema, "requestContextSchema"),
11379
- changedFields: this.parseJson(row.changedFields, "changedFields"),
11341
+ rules: parseJsonResilient(row.rules),
11342
+ requestContextSchema: parseJsonResilient(row.requestContextSchema),
11343
+ changedFields: parseJsonResilient(row.changedFields),
11380
11344
  changeMessage: row.changeMessage,
11381
11345
  createdAt: new Date(row.createdAtZ || row.createdAt)
11382
11346
  };
@@ -11993,7 +11957,14 @@ var ScorerDefinitionsPG = class _ScorerDefinitionsPG extends ScorerDefinitionsSt
11993
11957
  `SELECT * FROM ${tableName} ${whereClause} ORDER BY "${field}" ${direction} LIMIT $${paramIdx++} OFFSET $${paramIdx++}`,
11994
11958
  [...queryParams, limitValue, offset]
11995
11959
  );
11996
- const scorerDefinitions = (dataResult || []).map((row) => this.parseScorerRow(row));
11960
+ const scorerDefinitions = (dataResult || []).flatMap((row) => {
11961
+ try {
11962
+ return [this.parseScorerRow(row)];
11963
+ } catch (err) {
11964
+ this.logger?.warn?.("[PG] Failed to map scorer definition row, skipping", { id: row?.id, error: err });
11965
+ return [];
11966
+ }
11967
+ });
11997
11968
  return {
11998
11969
  scorerDefinitions,
11999
11970
  total,
@@ -12184,7 +12155,17 @@ var ScorerDefinitionsPG = class _ScorerDefinitionsPG extends ScorerDefinitionsSt
12184
12155
  `SELECT * FROM ${tableName} WHERE "scorerDefinitionId" = $1 ORDER BY "${field}" ${direction} LIMIT $2 OFFSET $3`,
12185
12156
  [scorerDefinitionId, limitValue, offset]
12186
12157
  );
12187
- const versions = (dataResult || []).map((row) => this.parseVersionRow(row));
12158
+ const versions = (dataResult || []).flatMap((row) => {
12159
+ try {
12160
+ return [this.parseVersionRow(row)];
12161
+ } catch (err) {
12162
+ this.logger?.warn?.("[PG] Failed to map scorer definition version row, skipping", {
12163
+ id: row?.id,
12164
+ error: err
12165
+ });
12166
+ return [];
12167
+ }
12168
+ });
12188
12169
  return {
12189
12170
  versions,
12190
12171
  total,
@@ -12272,38 +12253,13 @@ var ScorerDefinitionsPG = class _ScorerDefinitionsPG extends ScorerDefinitionsSt
12272
12253
  // ==========================================================================
12273
12254
  // Private Helper Methods
12274
12255
  // ==========================================================================
12275
- parseJson(value, fieldName) {
12276
- if (!value) return void 0;
12277
- if (typeof value !== "string") return value;
12278
- try {
12279
- return JSON.parse(value);
12280
- } catch (error) {
12281
- if (error instanceof MastraError) throw error;
12282
- const details = {
12283
- value: value.length > 100 ? value.substring(0, 100) + "..." : value
12284
- };
12285
- if (fieldName) {
12286
- details.field = fieldName;
12287
- }
12288
- throw new MastraError(
12289
- {
12290
- id: createStorageErrorId("PG", "PARSE_JSON", "INVALID_JSON"),
12291
- domain: ErrorDomain.STORAGE,
12292
- category: ErrorCategory.SYSTEM,
12293
- text: `Failed to parse JSON${fieldName ? ` for field "${fieldName}"` : ""}: ${error instanceof Error ? error.message : "Unknown error"}`,
12294
- details
12295
- },
12296
- error
12297
- );
12298
- }
12299
- }
12300
12256
  parseScorerRow(row) {
12301
12257
  return {
12302
12258
  id: row.id,
12303
12259
  status: row.status,
12304
12260
  activeVersionId: row.activeVersionId,
12305
12261
  authorId: row.authorId,
12306
- metadata: this.parseJson(row.metadata, "metadata"),
12262
+ metadata: parseJsonResilient(row.metadata),
12307
12263
  createdAt: new Date(row.createdAtZ || row.createdAt),
12308
12264
  updatedAt: new Date(row.updatedAtZ || row.updatedAt)
12309
12265
  };
@@ -12316,12 +12272,12 @@ var ScorerDefinitionsPG = class _ScorerDefinitionsPG extends ScorerDefinitionsSt
12316
12272
  name: row.name,
12317
12273
  description: row.description,
12318
12274
  type: row.type,
12319
- model: this.parseJson(row.model, "model"),
12275
+ model: parseJsonResilient(row.model),
12320
12276
  instructions: row.instructions,
12321
- scoreRange: this.parseJson(row.scoreRange, "scoreRange"),
12322
- presetConfig: this.parseJson(row.presetConfig, "presetConfig"),
12323
- defaultSampling: this.parseJson(row.defaultSampling, "defaultSampling"),
12324
- changedFields: this.parseJson(row.changedFields, "changedFields"),
12277
+ scoreRange: parseJsonResilient(row.scoreRange),
12278
+ presetConfig: parseJsonResilient(row.presetConfig),
12279
+ defaultSampling: parseJsonResilient(row.defaultSampling),
12280
+ changedFields: parseJsonResilient(row.changedFields),
12325
12281
  changeMessage: row.changeMessage,
12326
12282
  createdAt: new Date(row.createdAtZ || row.createdAt)
12327
12283
  };
@@ -13063,7 +13019,14 @@ var SkillsPG = class _SkillsPG extends SkillsStorage {
13063
13019
  `SELECT * FROM ${tableName} ${whereClause} ORDER BY "${field}" ${direction} LIMIT $${paramIdx++} OFFSET $${paramIdx++}`,
13064
13020
  [...queryParams, limitValue, offset]
13065
13021
  );
13066
- const skills = (dataResult || []).map((row) => this.parseSkillRow(row));
13022
+ const skills = (dataResult || []).flatMap((row) => {
13023
+ try {
13024
+ return [this.parseSkillRow(row)];
13025
+ } catch (err) {
13026
+ this.logger?.warn?.("[PG] Failed to map skill row, skipping", { id: row?.id, error: err });
13027
+ return [];
13028
+ }
13029
+ });
13067
13030
  return {
13068
13031
  skills,
13069
13032
  total,
@@ -13257,7 +13220,14 @@ var SkillsPG = class _SkillsPG extends SkillsStorage {
13257
13220
  `SELECT * FROM ${tableName} WHERE "skillId" = $1 ORDER BY "${field}" ${direction} LIMIT $2 OFFSET $3`,
13258
13221
  [skillId, limitValue, offset]
13259
13222
  );
13260
- const versions = (dataResult || []).map((row) => this.parseVersionRow(row));
13223
+ const versions = (dataResult || []).flatMap((row) => {
13224
+ try {
13225
+ return [this.parseVersionRow(row)];
13226
+ } catch (err) {
13227
+ this.logger?.warn?.("[PG] Failed to map skill version row, skipping", { id: row?.id, error: err });
13228
+ return [];
13229
+ }
13230
+ });
13261
13231
  return {
13262
13232
  versions,
13263
13233
  total,
@@ -13344,31 +13314,6 @@ var SkillsPG = class _SkillsPG extends SkillsStorage {
13344
13314
  // ==========================================================================
13345
13315
  // Private Helper Methods
13346
13316
  // ==========================================================================
13347
- parseJson(value, fieldName) {
13348
- if (!value) return void 0;
13349
- if (typeof value !== "string") return value;
13350
- try {
13351
- return JSON.parse(value);
13352
- } catch (error) {
13353
- if (error instanceof MastraError) throw error;
13354
- const details = {
13355
- value: value.length > 100 ? value.substring(0, 100) + "..." : value
13356
- };
13357
- if (fieldName) {
13358
- details.field = fieldName;
13359
- }
13360
- throw new MastraError(
13361
- {
13362
- id: createStorageErrorId("PG", "PARSE_JSON", "INVALID_JSON"),
13363
- domain: ErrorDomain.STORAGE,
13364
- category: ErrorCategory.SYSTEM,
13365
- text: `Failed to parse JSON${fieldName ? ` for field "${fieldName}"` : ""}: ${error instanceof Error ? error.message : "Unknown error"}`,
13366
- details
13367
- },
13368
- error
13369
- );
13370
- }
13371
- }
13372
13317
  parseSkillRow(row) {
13373
13318
  return {
13374
13319
  id: row.id,
@@ -13388,14 +13333,14 @@ var SkillsPG = class _SkillsPG extends SkillsStorage {
13388
13333
  description: row.description,
13389
13334
  instructions: row.instructions,
13390
13335
  license: row.license,
13391
- compatibility: this.parseJson(row.compatibility, "compatibility"),
13392
- source: this.parseJson(row.source, "source"),
13393
- references: this.parseJson(row.references, "references"),
13394
- scripts: this.parseJson(row.scripts, "scripts"),
13395
- assets: this.parseJson(row.assets, "assets"),
13396
- metadata: this.parseJson(row.metadata, "metadata"),
13397
- tree: this.parseJson(row.tree, "tree"),
13398
- changedFields: this.parseJson(row.changedFields, "changedFields"),
13336
+ compatibility: parseJsonResilient(row.compatibility),
13337
+ source: parseJsonResilient(row.source),
13338
+ references: parseJsonResilient(row.references),
13339
+ scripts: parseJsonResilient(row.scripts),
13340
+ assets: parseJsonResilient(row.assets),
13341
+ metadata: parseJsonResilient(row.metadata),
13342
+ tree: parseJsonResilient(row.tree),
13343
+ changedFields: parseJsonResilient(row.changedFields),
13399
13344
  changeMessage: row.changeMessage,
13400
13345
  createdAt: new Date(row.createdAtZ || row.createdAt)
13401
13346
  };
@@ -14161,7 +14106,14 @@ var WorkspacesPG = class _WorkspacesPG extends WorkspacesStorage {
14161
14106
  `SELECT * FROM ${tableName} ${whereClause} ORDER BY "${field}" ${direction} LIMIT $${paramIdx++} OFFSET $${paramIdx++}`,
14162
14107
  [...queryParams, limitValue, offset]
14163
14108
  );
14164
- const workspaces = (dataResult || []).map((row) => this.parseWorkspaceRow(row));
14109
+ const workspaces = (dataResult || []).flatMap((row) => {
14110
+ try {
14111
+ return [this.parseWorkspaceRow(row)];
14112
+ } catch (err) {
14113
+ this.logger?.warn?.("[PG] Failed to map workspace row, skipping", { id: row?.id, error: err });
14114
+ return [];
14115
+ }
14116
+ });
14165
14117
  return {
14166
14118
  workspaces,
14167
14119
  total,
@@ -14355,7 +14307,14 @@ var WorkspacesPG = class _WorkspacesPG extends WorkspacesStorage {
14355
14307
  `SELECT * FROM ${tableName} WHERE "workspaceId" = $1 ORDER BY "${field}" ${direction} LIMIT $2 OFFSET $3`,
14356
14308
  [workspaceId, limitValue, offset]
14357
14309
  );
14358
- const versions = (dataResult || []).map((row) => this.parseVersionRow(row));
14310
+ const versions = (dataResult || []).flatMap((row) => {
14311
+ try {
14312
+ return [this.parseVersionRow(row)];
14313
+ } catch (err) {
14314
+ this.logger?.warn?.("[PG] Failed to map workspace version row, skipping", { id: row?.id, error: err });
14315
+ return [];
14316
+ }
14317
+ });
14359
14318
  return {
14360
14319
  versions,
14361
14320
  total,
@@ -14442,38 +14401,13 @@ var WorkspacesPG = class _WorkspacesPG extends WorkspacesStorage {
14442
14401
  // ==========================================================================
14443
14402
  // Private Helper Methods
14444
14403
  // ==========================================================================
14445
- parseJson(value, fieldName) {
14446
- if (!value) return void 0;
14447
- if (typeof value !== "string") return value;
14448
- try {
14449
- return JSON.parse(value);
14450
- } catch (error) {
14451
- if (error instanceof MastraError) throw error;
14452
- const details = {
14453
- value: value.length > 100 ? value.substring(0, 100) + "..." : value
14454
- };
14455
- if (fieldName) {
14456
- details.field = fieldName;
14457
- }
14458
- throw new MastraError(
14459
- {
14460
- id: createStorageErrorId("PG", "PARSE_JSON", "INVALID_JSON"),
14461
- domain: ErrorDomain.STORAGE,
14462
- category: ErrorCategory.SYSTEM,
14463
- text: `Failed to parse JSON${fieldName ? ` for field "${fieldName}"` : ""}: ${error instanceof Error ? error.message : "Unknown error"}`,
14464
- details
14465
- },
14466
- error
14467
- );
14468
- }
14469
- }
14470
14404
  parseWorkspaceRow(row) {
14471
14405
  return {
14472
14406
  id: row.id,
14473
14407
  status: row.status,
14474
14408
  activeVersionId: row.activeVersionId,
14475
14409
  authorId: row.authorId,
14476
- metadata: this.parseJson(row.metadata, "metadata"),
14410
+ metadata: parseJsonResilient(row.metadata),
14477
14411
  createdAt: new Date(row.createdAtZ || row.createdAt),
14478
14412
  updatedAt: new Date(row.updatedAtZ || row.updatedAt)
14479
14413
  };
@@ -14485,15 +14419,15 @@ var WorkspacesPG = class _WorkspacesPG extends WorkspacesStorage {
14485
14419
  versionNumber: row.versionNumber,
14486
14420
  name: row.name,
14487
14421
  description: row.description,
14488
- filesystem: this.parseJson(row.filesystem, "filesystem"),
14489
- sandbox: this.parseJson(row.sandbox, "sandbox"),
14490
- mounts: this.parseJson(row.mounts, "mounts"),
14491
- search: this.parseJson(row.search, "search"),
14492
- skills: this.parseJson(row.skills, "skills"),
14493
- tools: this.parseJson(row.tools, "tools"),
14422
+ filesystem: parseJsonResilient(row.filesystem),
14423
+ sandbox: parseJsonResilient(row.sandbox),
14424
+ mounts: parseJsonResilient(row.mounts),
14425
+ search: parseJsonResilient(row.search),
14426
+ skills: parseJsonResilient(row.skills),
14427
+ tools: parseJsonResilient(row.tools),
14494
14428
  autoSync: Boolean(row.autoSync),
14495
14429
  operationTimeout: row.operationTimeout != null ? Number(row.operationTimeout) : void 0,
14496
- changedFields: this.parseJson(row.changedFields, "changedFields"),
14430
+ changedFields: parseJsonResilient(row.changedFields),
14497
14431
  changeMessage: row.changeMessage,
14498
14432
  createdAt: new Date(row.createdAtZ || row.createdAt)
14499
14433
  };