@mastra/pg 1.0.0-beta.2 → 1.0.0-beta.4

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
@@ -1,13 +1,13 @@
1
1
  'use strict';
2
2
 
3
3
  var error = require('@mastra/core/error');
4
+ var storage = require('@mastra/core/storage');
4
5
  var utils = require('@mastra/core/utils');
5
6
  var vector = require('@mastra/core/vector');
6
7
  var asyncMutex = require('async-mutex');
7
8
  var pg = require('pg');
8
9
  var xxhash = require('xxhash-wasm');
9
10
  var filter = require('@mastra/core/vector/filter');
10
- var storage = require('@mastra/core/storage');
11
11
  var pgPromise = require('pg-promise');
12
12
  var agent = require('@mastra/core/agent');
13
13
  var evals = require('@mastra/core/evals');
@@ -576,7 +576,7 @@ var PgVector = class extends vector.MastraVector {
576
576
  } catch (error$1) {
577
577
  throw new error.MastraError(
578
578
  {
579
- id: "MASTRA_STORAGE_PG_VECTOR_INITIALIZATION_FAILED",
579
+ id: storage.createVectorErrorId("PG", "INITIALIZATION", "FAILED"),
580
580
  domain: error.ErrorDomain.MASTRA_VECTOR,
581
581
  category: error.ErrorCategory.THIRD_PARTY,
582
582
  details: {
@@ -622,9 +622,6 @@ var PgVector = class extends vector.MastraVector {
622
622
  if (this.vectorExtensionSchema === "pg_catalog") {
623
623
  return "vector";
624
624
  }
625
- if (this.vectorExtensionSchema === (this.schema || "public")) {
626
- return "vector";
627
- }
628
625
  const validatedSchema = utils.parseSqlIdentifier(this.vectorExtensionSchema, "vector extension schema");
629
626
  return `${validatedSchema}.vector`;
630
627
  }
@@ -673,7 +670,7 @@ var PgVector = class extends vector.MastraVector {
673
670
  } catch (error$1) {
674
671
  const mastraError = new error.MastraError(
675
672
  {
676
- id: "MASTRA_STORAGE_PG_VECTOR_QUERY_INVALID_INPUT",
673
+ id: storage.createVectorErrorId("PG", "QUERY", "INVALID_INPUT"),
677
674
  domain: error.ErrorDomain.MASTRA_VECTOR,
678
675
  category: error.ErrorCategory.USER,
679
676
  details: {
@@ -729,7 +726,7 @@ var PgVector = class extends vector.MastraVector {
729
726
  await client.query("ROLLBACK");
730
727
  const mastraError = new error.MastraError(
731
728
  {
732
- id: "MASTRA_STORAGE_PG_VECTOR_QUERY_FAILED",
729
+ id: storage.createVectorErrorId("PG", "QUERY", "FAILED"),
733
730
  domain: error.ErrorDomain.MASTRA_VECTOR,
734
731
  category: error.ErrorCategory.THIRD_PARTY,
735
732
  details: {
@@ -798,7 +795,7 @@ var PgVector = class extends vector.MastraVector {
798
795
  const [, expected, actual] = match;
799
796
  const mastraError2 = new error.MastraError(
800
797
  {
801
- id: "MASTRA_STORAGE_PG_VECTOR_UPSERT_INVALID_INPUT",
798
+ id: storage.createVectorErrorId("PG", "UPSERT", "INVALID_INPUT"),
802
799
  domain: error.ErrorDomain.MASTRA_VECTOR,
803
800
  category: error.ErrorCategory.USER,
804
801
  text: `Vector dimension mismatch: Index "${indexName}" expects ${expected} dimensions but got ${actual} dimensions. Either use a matching embedding model or delete and recreate the index with the new dimension.`,
@@ -816,7 +813,7 @@ var PgVector = class extends vector.MastraVector {
816
813
  }
817
814
  const mastraError = new error.MastraError(
818
815
  {
819
- id: "MASTRA_STORAGE_PG_VECTOR_UPSERT_FAILED",
816
+ id: storage.createVectorErrorId("PG", "UPSERT", "FAILED"),
820
817
  domain: error.ErrorDomain.MASTRA_VECTOR,
821
818
  category: error.ErrorCategory.THIRD_PARTY,
822
819
  details: {
@@ -904,7 +901,7 @@ var PgVector = class extends vector.MastraVector {
904
901
  } catch (error$1) {
905
902
  const mastraError = new error.MastraError(
906
903
  {
907
- id: "MASTRA_STORAGE_PG_VECTOR_CREATE_INDEX_INVALID_INPUT",
904
+ id: storage.createVectorErrorId("PG", "CREATE_INDEX", "INVALID_INPUT"),
908
905
  domain: error.ErrorDomain.MASTRA_VECTOR,
909
906
  category: error.ErrorCategory.USER,
910
907
  details: {
@@ -954,7 +951,7 @@ var PgVector = class extends vector.MastraVector {
954
951
  }).catch((error$1) => {
955
952
  const mastraError = new error.MastraError(
956
953
  {
957
- id: "MASTRA_STORAGE_PG_VECTOR_CREATE_INDEX_FAILED",
954
+ id: storage.createVectorErrorId("PG", "CREATE_INDEX", "FAILED"),
958
955
  domain: error.ErrorDomain.MASTRA_VECTOR,
959
956
  category: error.ErrorCategory.THIRD_PARTY,
960
957
  details: {
@@ -974,7 +971,7 @@ var PgVector = class extends vector.MastraVector {
974
971
  } catch (error$1) {
975
972
  const mastraError = new error.MastraError(
976
973
  {
977
- id: "MASTRA_STORAGE_PG_VECTOR_BUILD_INDEX_FAILED",
974
+ id: storage.createVectorErrorId("PG", "BUILD_INDEX", "FAILED"),
978
975
  domain: error.ErrorDomain.MASTRA_VECTOR,
979
976
  category: error.ErrorCategory.THIRD_PARTY,
980
977
  details: {
@@ -1174,7 +1171,7 @@ var PgVector = class extends vector.MastraVector {
1174
1171
  } catch (e) {
1175
1172
  const mastraError = new error.MastraError(
1176
1173
  {
1177
- id: "MASTRA_STORAGE_PG_VECTOR_LIST_INDEXES_FAILED",
1174
+ id: storage.createVectorErrorId("PG", "LIST_INDEXES", "FAILED"),
1178
1175
  domain: error.ErrorDomain.MASTRA_VECTOR,
1179
1176
  category: error.ErrorCategory.THIRD_PARTY
1180
1177
  },
@@ -1263,7 +1260,7 @@ var PgVector = class extends vector.MastraVector {
1263
1260
  await client.query("ROLLBACK");
1264
1261
  const mastraError = new error.MastraError(
1265
1262
  {
1266
- id: "MASTRA_STORAGE_PG_VECTOR_DESCRIBE_INDEX_FAILED",
1263
+ id: storage.createVectorErrorId("PG", "DESCRIBE_INDEX", "FAILED"),
1267
1264
  domain: error.ErrorDomain.MASTRA_VECTOR,
1268
1265
  category: error.ErrorCategory.THIRD_PARTY,
1269
1266
  details: {
@@ -1288,7 +1285,7 @@ var PgVector = class extends vector.MastraVector {
1288
1285
  await client.query("ROLLBACK");
1289
1286
  const mastraError = new error.MastraError(
1290
1287
  {
1291
- id: "MASTRA_STORAGE_PG_VECTOR_DELETE_INDEX_FAILED",
1288
+ id: storage.createVectorErrorId("PG", "DELETE_INDEX", "FAILED"),
1292
1289
  domain: error.ErrorDomain.MASTRA_VECTOR,
1293
1290
  category: error.ErrorCategory.THIRD_PARTY,
1294
1291
  details: {
@@ -1312,7 +1309,7 @@ var PgVector = class extends vector.MastraVector {
1312
1309
  await client.query("ROLLBACK");
1313
1310
  const mastraError = new error.MastraError(
1314
1311
  {
1315
- id: "MASTRA_STORAGE_PG_VECTOR_TRUNCATE_INDEX_FAILED",
1312
+ id: storage.createVectorErrorId("PG", "TRUNCATE_INDEX", "FAILED"),
1316
1313
  domain: error.ErrorDomain.MASTRA_VECTOR,
1317
1314
  category: error.ErrorCategory.THIRD_PARTY,
1318
1315
  details: {
@@ -1354,7 +1351,7 @@ var PgVector = class extends vector.MastraVector {
1354
1351
  }
1355
1352
  if (!id && !filter) {
1356
1353
  throw new error.MastraError({
1357
- id: "MASTRA_STORAGE_PG_VECTOR_UPDATE_MISSING_PARAMS",
1354
+ id: storage.createVectorErrorId("PG", "UPDATE_VECTOR", "NO_TARGET"),
1358
1355
  text: "Either id or filter must be provided",
1359
1356
  domain: error.ErrorDomain.MASTRA_VECTOR,
1360
1357
  category: error.ErrorCategory.USER,
@@ -1363,7 +1360,7 @@ var PgVector = class extends vector.MastraVector {
1363
1360
  }
1364
1361
  if (id && filter) {
1365
1362
  throw new error.MastraError({
1366
- id: "MASTRA_STORAGE_PG_VECTOR_UPDATE_CONFLICTING_PARAMS",
1363
+ id: storage.createVectorErrorId("PG", "UPDATE_VECTOR", "MUTUALLY_EXCLUSIVE"),
1367
1364
  text: "Cannot provide both id and filter - they are mutually exclusive",
1368
1365
  domain: error.ErrorDomain.MASTRA_VECTOR,
1369
1366
  category: error.ErrorCategory.USER,
@@ -1397,7 +1394,7 @@ var PgVector = class extends vector.MastraVector {
1397
1394
  } else {
1398
1395
  if (!filter || Object.keys(filter).length === 0) {
1399
1396
  throw new error.MastraError({
1400
- id: "MASTRA_STORAGE_PG_VECTOR_UPDATE_EMPTY_FILTER",
1397
+ id: storage.createVectorErrorId("PG", "UPDATE_VECTOR", "EMPTY_FILTER"),
1401
1398
  text: "Cannot update with empty filter",
1402
1399
  domain: error.ErrorDomain.MASTRA_VECTOR,
1403
1400
  category: error.ErrorCategory.USER,
@@ -1409,7 +1406,7 @@ var PgVector = class extends vector.MastraVector {
1409
1406
  whereClause = filterQuery.trim().replace(/^WHERE\s+/i, "");
1410
1407
  if (!whereClause) {
1411
1408
  throw new error.MastraError({
1412
- id: "MASTRA_STORAGE_PG_VECTOR_UPDATE_INVALID_FILTER",
1409
+ id: storage.createVectorErrorId("PG", "UPDATE_VECTOR", "INVALID_FILTER"),
1413
1410
  text: "Filter produced empty WHERE clause",
1414
1411
  domain: error.ErrorDomain.MASTRA_VECTOR,
1415
1412
  category: error.ErrorCategory.USER,
@@ -1440,7 +1437,7 @@ var PgVector = class extends vector.MastraVector {
1440
1437
  }
1441
1438
  const mastraError = new error.MastraError(
1442
1439
  {
1443
- id: "MASTRA_STORAGE_PG_VECTOR_UPDATE_VECTOR_FAILED",
1440
+ id: storage.createVectorErrorId("PG", "UPDATE_VECTOR", "FAILED"),
1444
1441
  domain: error.ErrorDomain.MASTRA_VECTOR,
1445
1442
  category: error.ErrorCategory.THIRD_PARTY,
1446
1443
  details: {
@@ -1477,7 +1474,7 @@ var PgVector = class extends vector.MastraVector {
1477
1474
  } catch (error$1) {
1478
1475
  const mastraError = new error.MastraError(
1479
1476
  {
1480
- id: "MASTRA_STORAGE_PG_VECTOR_DELETE_VECTOR_FAILED",
1477
+ id: storage.createVectorErrorId("PG", "DELETE_VECTOR", "FAILED"),
1481
1478
  domain: error.ErrorDomain.MASTRA_VECTOR,
1482
1479
  category: error.ErrorCategory.THIRD_PARTY,
1483
1480
  details: {
@@ -1507,7 +1504,7 @@ var PgVector = class extends vector.MastraVector {
1507
1504
  const { tableName } = this.getTableName(indexName);
1508
1505
  if (!filter && !ids) {
1509
1506
  throw new error.MastraError({
1510
- id: "MASTRA_STORAGE_PG_VECTOR_DELETE_MISSING_PARAMS",
1507
+ id: storage.createVectorErrorId("PG", "DELETE_VECTORS", "NO_TARGET"),
1511
1508
  text: "Either filter or ids must be provided",
1512
1509
  domain: error.ErrorDomain.MASTRA_VECTOR,
1513
1510
  category: error.ErrorCategory.USER,
@@ -1516,7 +1513,7 @@ var PgVector = class extends vector.MastraVector {
1516
1513
  }
1517
1514
  if (filter && ids) {
1518
1515
  throw new error.MastraError({
1519
- id: "MASTRA_STORAGE_PG_VECTOR_DELETE_CONFLICTING_PARAMS",
1516
+ id: storage.createVectorErrorId("PG", "DELETE_VECTORS", "MUTUALLY_EXCLUSIVE"),
1520
1517
  text: "Cannot provide both filter and ids - they are mutually exclusive",
1521
1518
  domain: error.ErrorDomain.MASTRA_VECTOR,
1522
1519
  category: error.ErrorCategory.USER,
@@ -1528,7 +1525,7 @@ var PgVector = class extends vector.MastraVector {
1528
1525
  if (ids) {
1529
1526
  if (ids.length === 0) {
1530
1527
  throw new error.MastraError({
1531
- id: "MASTRA_STORAGE_PG_VECTOR_DELETE_EMPTY_IDS",
1528
+ id: storage.createVectorErrorId("PG", "DELETE_VECTORS", "EMPTY_IDS"),
1532
1529
  text: "Cannot delete with empty ids array",
1533
1530
  domain: error.ErrorDomain.MASTRA_VECTOR,
1534
1531
  category: error.ErrorCategory.USER,
@@ -1541,7 +1538,7 @@ var PgVector = class extends vector.MastraVector {
1541
1538
  } else {
1542
1539
  if (!filter || Object.keys(filter).length === 0) {
1543
1540
  throw new error.MastraError({
1544
- id: "MASTRA_STORAGE_PG_VECTOR_DELETE_EMPTY_FILTER",
1541
+ id: storage.createVectorErrorId("PG", "DELETE_VECTORS", "EMPTY_FILTER"),
1545
1542
  text: "Cannot delete with empty filter. Use deleteIndex to delete all vectors.",
1546
1543
  domain: error.ErrorDomain.MASTRA_VECTOR,
1547
1544
  category: error.ErrorCategory.USER,
@@ -1553,7 +1550,7 @@ var PgVector = class extends vector.MastraVector {
1553
1550
  const whereClause = filterQuery.trim().replace(/^WHERE\s+/i, "");
1554
1551
  if (!whereClause) {
1555
1552
  throw new error.MastraError({
1556
- id: "MASTRA_STORAGE_PG_VECTOR_DELETE_INVALID_FILTER",
1553
+ id: storage.createVectorErrorId("PG", "DELETE_VECTORS", "INVALID_FILTER"),
1557
1554
  text: "Filter produced empty WHERE clause",
1558
1555
  domain: error.ErrorDomain.MASTRA_VECTOR,
1559
1556
  category: error.ErrorCategory.USER,
@@ -1576,7 +1573,7 @@ var PgVector = class extends vector.MastraVector {
1576
1573
  }
1577
1574
  const mastraError = new error.MastraError(
1578
1575
  {
1579
- id: "MASTRA_STORAGE_PG_VECTOR_DELETE_VECTORS_FAILED",
1576
+ id: storage.createVectorErrorId("PG", "DELETE_VECTORS", "FAILED"),
1580
1577
  domain: error.ErrorDomain.MASTRA_VECTOR,
1581
1578
  category: error.ErrorCategory.THIRD_PARTY,
1582
1579
  details: {
@@ -1716,7 +1713,7 @@ var MemoryPG = class extends storage.MemoryStorage {
1716
1713
  } catch (error$1) {
1717
1714
  throw new error.MastraError(
1718
1715
  {
1719
- id: "MASTRA_STORAGE_PG_STORE_GET_THREAD_BY_ID_FAILED",
1716
+ id: storage.createStorageErrorId("PG", "GET_THREAD_BY_ID", "FAILED"),
1720
1717
  domain: error.ErrorDomain.STORAGE,
1721
1718
  category: error.ErrorCategory.THIRD_PARTY,
1722
1719
  details: {
@@ -1731,7 +1728,7 @@ var MemoryPG = class extends storage.MemoryStorage {
1731
1728
  const { resourceId, page = 0, perPage: perPageInput, orderBy } = args;
1732
1729
  if (page < 0) {
1733
1730
  throw new error.MastraError({
1734
- id: "MASTRA_STORAGE_PG_STORE_INVALID_PAGE",
1731
+ id: storage.createStorageErrorId("PG", "LIST_THREADS_BY_RESOURCE_ID", "INVALID_PAGE"),
1735
1732
  domain: error.ErrorDomain.STORAGE,
1736
1733
  category: error.ErrorCategory.USER,
1737
1734
  text: "Page number must be non-negative",
@@ -1780,7 +1777,7 @@ var MemoryPG = class extends storage.MemoryStorage {
1780
1777
  } catch (error$1) {
1781
1778
  const mastraError = new error.MastraError(
1782
1779
  {
1783
- id: "MASTRA_STORAGE_PG_STORE_LIST_THREADS_BY_RESOURCE_ID_FAILED",
1780
+ id: storage.createStorageErrorId("PG", "LIST_THREADS_BY_RESOURCE_ID", "FAILED"),
1784
1781
  domain: error.ErrorDomain.STORAGE,
1785
1782
  category: error.ErrorCategory.THIRD_PARTY,
1786
1783
  details: {
@@ -1838,7 +1835,7 @@ var MemoryPG = class extends storage.MemoryStorage {
1838
1835
  } catch (error$1) {
1839
1836
  throw new error.MastraError(
1840
1837
  {
1841
- id: "MASTRA_STORAGE_PG_STORE_SAVE_THREAD_FAILED",
1838
+ id: storage.createStorageErrorId("PG", "SAVE_THREAD", "FAILED"),
1842
1839
  domain: error.ErrorDomain.STORAGE,
1843
1840
  category: error.ErrorCategory.THIRD_PARTY,
1844
1841
  details: {
@@ -1858,7 +1855,7 @@ var MemoryPG = class extends storage.MemoryStorage {
1858
1855
  const existingThread = await this.getThreadById({ threadId: id });
1859
1856
  if (!existingThread) {
1860
1857
  throw new error.MastraError({
1861
- id: "MASTRA_STORAGE_PG_STORE_UPDATE_THREAD_FAILED",
1858
+ id: storage.createStorageErrorId("PG", "UPDATE_THREAD", "FAILED"),
1862
1859
  domain: error.ErrorDomain.STORAGE,
1863
1860
  category: error.ErrorCategory.USER,
1864
1861
  text: `Thread ${id} not found`,
@@ -1896,7 +1893,7 @@ var MemoryPG = class extends storage.MemoryStorage {
1896
1893
  } catch (error$1) {
1897
1894
  throw new error.MastraError(
1898
1895
  {
1899
- id: "MASTRA_STORAGE_PG_STORE_UPDATE_THREAD_FAILED",
1896
+ id: storage.createStorageErrorId("PG", "UPDATE_THREAD", "FAILED"),
1900
1897
  domain: error.ErrorDomain.STORAGE,
1901
1898
  category: error.ErrorCategory.THIRD_PARTY,
1902
1899
  details: {
@@ -1914,12 +1911,26 @@ var MemoryPG = class extends storage.MemoryStorage {
1914
1911
  const threadTableName = getTableName({ indexName: storage.TABLE_THREADS, schemaName: getSchemaName(this.schema) });
1915
1912
  await this.client.tx(async (t) => {
1916
1913
  await t.none(`DELETE FROM ${tableName} WHERE thread_id = $1`, [threadId]);
1914
+ const schemaName = this.schema || "public";
1915
+ const vectorTables = await t.manyOrNone(
1916
+ `
1917
+ SELECT tablename
1918
+ FROM pg_tables
1919
+ WHERE schemaname = $1
1920
+ AND (tablename = 'memory_messages' OR tablename LIKE 'memory_messages_%')
1921
+ `,
1922
+ [schemaName]
1923
+ );
1924
+ for (const { tablename } of vectorTables) {
1925
+ const vectorTableName = getTableName({ indexName: tablename, schemaName: getSchemaName(this.schema) });
1926
+ await t.none(`DELETE FROM ${vectorTableName} WHERE metadata->>'thread_id' = $1`, [threadId]);
1927
+ }
1917
1928
  await t.none(`DELETE FROM ${threadTableName} WHERE id = $1`, [threadId]);
1918
1929
  });
1919
1930
  } catch (error$1) {
1920
1931
  throw new error.MastraError(
1921
1932
  {
1922
- id: "MASTRA_STORAGE_PG_STORE_DELETE_THREAD_FAILED",
1933
+ id: storage.createStorageErrorId("PG", "DELETE_THREAD", "FAILED"),
1923
1934
  domain: error.ErrorDomain.STORAGE,
1924
1935
  category: error.ErrorCategory.THIRD_PARTY,
1925
1936
  details: {
@@ -1930,28 +1941,26 @@ var MemoryPG = class extends storage.MemoryStorage {
1930
1941
  );
1931
1942
  }
1932
1943
  }
1933
- async _getIncludedMessages({
1934
- threadId,
1935
- include
1936
- }) {
1937
- if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
1938
- if (!include) return null;
1944
+ async _getIncludedMessages({ include }) {
1945
+ if (!include || include.length === 0) return null;
1939
1946
  const unionQueries = [];
1940
1947
  const params = [];
1941
1948
  let paramIdx = 1;
1942
1949
  const tableName = getTableName({ indexName: storage.TABLE_MESSAGES, schemaName: getSchemaName(this.schema) });
1943
1950
  for (const inc of include) {
1944
1951
  const { id, withPreviousMessages = 0, withNextMessages = 0 } = inc;
1945
- const searchId = inc.threadId || threadId;
1946
1952
  unionQueries.push(
1947
1953
  `
1948
1954
  SELECT * FROM (
1949
- WITH ordered_messages AS (
1955
+ WITH target_thread AS (
1956
+ SELECT thread_id FROM ${tableName} WHERE id = $${paramIdx}
1957
+ ),
1958
+ ordered_messages AS (
1950
1959
  SELECT
1951
1960
  *,
1952
1961
  ROW_NUMBER() OVER (ORDER BY "createdAt" ASC) as row_num
1953
1962
  FROM ${tableName}
1954
- WHERE thread_id = $${paramIdx}
1963
+ WHERE thread_id = (SELECT thread_id FROM target_thread)
1955
1964
  )
1956
1965
  SELECT
1957
1966
  m.id,
@@ -1963,24 +1972,24 @@ var MemoryPG = class extends storage.MemoryStorage {
1963
1972
  m.thread_id AS "threadId",
1964
1973
  m."resourceId"
1965
1974
  FROM ordered_messages m
1966
- WHERE m.id = $${paramIdx + 1}
1975
+ WHERE m.id = $${paramIdx}
1967
1976
  OR EXISTS (
1968
1977
  SELECT 1 FROM ordered_messages target
1969
- WHERE target.id = $${paramIdx + 1}
1978
+ WHERE target.id = $${paramIdx}
1970
1979
  AND (
1971
1980
  -- Get previous messages (messages that come BEFORE the target)
1972
- (m.row_num < target.row_num AND m.row_num >= target.row_num - $${paramIdx + 2})
1981
+ (m.row_num < target.row_num AND m.row_num >= target.row_num - $${paramIdx + 1})
1973
1982
  OR
1974
1983
  -- Get next messages (messages that come AFTER the target)
1975
- (m.row_num > target.row_num AND m.row_num <= target.row_num + $${paramIdx + 3})
1984
+ (m.row_num > target.row_num AND m.row_num <= target.row_num + $${paramIdx + 2})
1976
1985
  )
1977
1986
  )
1978
1987
  ) AS query_${paramIdx}
1979
1988
  `
1980
1989
  // Keep ASC for final sorting after fetching context
1981
1990
  );
1982
- params.push(searchId, id, withPreviousMessages, withNextMessages);
1983
- paramIdx += 4;
1991
+ params.push(id, withPreviousMessages, withNextMessages);
1992
+ paramIdx += 3;
1984
1993
  }
1985
1994
  const finalQuery = unionQueries.join(" UNION ALL ") + ' ORDER BY "createdAt" ASC';
1986
1995
  const includedRows = await this.client.manyOrNone(finalQuery, params);
@@ -2028,7 +2037,7 @@ var MemoryPG = class extends storage.MemoryStorage {
2028
2037
  } catch (error$1) {
2029
2038
  const mastraError = new error.MastraError(
2030
2039
  {
2031
- id: "MASTRA_STORAGE_PG_STORE_LIST_MESSAGES_BY_ID_FAILED",
2040
+ id: storage.createStorageErrorId("PG", "LIST_MESSAGES_BY_ID", "FAILED"),
2032
2041
  domain: error.ErrorDomain.STORAGE,
2033
2042
  category: error.ErrorCategory.THIRD_PARTY,
2034
2043
  details: {
@@ -2044,25 +2053,28 @@ var MemoryPG = class extends storage.MemoryStorage {
2044
2053
  }
2045
2054
  async listMessages(args) {
2046
2055
  const { threadId, resourceId, include, filter, perPage: perPageInput, page = 0, orderBy } = args;
2047
- if (!threadId.trim()) {
2056
+ const threadIds = (Array.isArray(threadId) ? threadId : [threadId]).filter(
2057
+ (id) => typeof id === "string"
2058
+ );
2059
+ if (threadIds.length === 0 || threadIds.some((id) => !id.trim())) {
2048
2060
  throw new error.MastraError(
2049
2061
  {
2050
- id: "STORAGE_PG_LIST_MESSAGES_INVALID_THREAD_ID",
2062
+ id: storage.createStorageErrorId("PG", "LIST_MESSAGES", "INVALID_THREAD_ID"),
2051
2063
  domain: error.ErrorDomain.STORAGE,
2052
2064
  category: error.ErrorCategory.THIRD_PARTY,
2053
- details: { threadId }
2065
+ details: { threadId: Array.isArray(threadId) ? String(threadId) : String(threadId) }
2054
2066
  },
2055
- new Error("threadId must be a non-empty string")
2067
+ new Error("threadId must be a non-empty string or array of non-empty strings")
2056
2068
  );
2057
2069
  }
2058
2070
  if (page < 0) {
2059
2071
  throw new error.MastraError({
2060
- id: "MASTRA_STORAGE_PG_STORE_INVALID_PAGE",
2072
+ id: storage.createStorageErrorId("PG", "LIST_MESSAGES", "INVALID_PAGE"),
2061
2073
  domain: error.ErrorDomain.STORAGE,
2062
2074
  category: error.ErrorCategory.USER,
2063
2075
  text: "Page number must be non-negative",
2064
2076
  details: {
2065
- threadId,
2077
+ threadId: Array.isArray(threadId) ? threadId.join(",") : threadId,
2066
2078
  page
2067
2079
  }
2068
2080
  });
@@ -2074,9 +2086,10 @@ var MemoryPG = class extends storage.MemoryStorage {
2074
2086
  const orderByStatement = `ORDER BY "${field}" ${direction}`;
2075
2087
  const selectStatement = `SELECT id, content, role, type, "createdAt", "createdAtZ", thread_id AS "threadId", "resourceId"`;
2076
2088
  const tableName = getTableName({ indexName: storage.TABLE_MESSAGES, schemaName: getSchemaName(this.schema) });
2077
- const conditions = [`thread_id = $1`];
2078
- const queryParams = [threadId];
2079
- let paramIndex = 2;
2089
+ const threadPlaceholders = threadIds.map((_, i) => `$${i + 1}`).join(", ");
2090
+ const conditions = [`thread_id IN (${threadPlaceholders})`];
2091
+ const queryParams = [...threadIds];
2092
+ let paramIndex = threadIds.length + 1;
2080
2093
  if (resourceId) {
2081
2094
  conditions.push(`"resourceId" = $${paramIndex++}`);
2082
2095
  queryParams.push(resourceId);
@@ -2108,7 +2121,7 @@ var MemoryPG = class extends storage.MemoryStorage {
2108
2121
  }
2109
2122
  const messageIds = new Set(messages.map((m) => m.id));
2110
2123
  if (include && include.length > 0) {
2111
- const includeMessages = await this._getIncludedMessages({ threadId, include });
2124
+ const includeMessages = await this._getIncludedMessages({ include });
2112
2125
  if (includeMessages) {
2113
2126
  for (const includeMsg of includeMessages) {
2114
2127
  if (!messageIds.has(includeMsg.id)) {
@@ -2135,7 +2148,10 @@ var MemoryPG = class extends storage.MemoryStorage {
2135
2148
  }
2136
2149
  return direction === "ASC" ? String(aValue).localeCompare(String(bValue)) : String(bValue).localeCompare(String(aValue));
2137
2150
  });
2138
- const returnedThreadMessageIds = new Set(finalMessages.filter((m) => m.threadId === threadId).map((m) => m.id));
2151
+ const threadIdSet = new Set(threadIds);
2152
+ const returnedThreadMessageIds = new Set(
2153
+ finalMessages.filter((m) => m.threadId && threadIdSet.has(m.threadId)).map((m) => m.id)
2154
+ );
2139
2155
  const allThreadMessagesReturned = returnedThreadMessageIds.size >= total;
2140
2156
  const hasMore = perPageInput !== false && !allThreadMessagesReturned && offset + perPage < total;
2141
2157
  return {
@@ -2148,11 +2164,11 @@ var MemoryPG = class extends storage.MemoryStorage {
2148
2164
  } catch (error$1) {
2149
2165
  const mastraError = new error.MastraError(
2150
2166
  {
2151
- id: "MASTRA_STORAGE_PG_STORE_LIST_MESSAGES_FAILED",
2167
+ id: storage.createStorageErrorId("PG", "LIST_MESSAGES", "FAILED"),
2152
2168
  domain: error.ErrorDomain.STORAGE,
2153
2169
  category: error.ErrorCategory.THIRD_PARTY,
2154
2170
  details: {
2155
- threadId,
2171
+ threadId: Array.isArray(threadId) ? threadId.join(",") : threadId,
2156
2172
  resourceId: resourceId ?? ""
2157
2173
  }
2158
2174
  },
@@ -2174,7 +2190,7 @@ var MemoryPG = class extends storage.MemoryStorage {
2174
2190
  const threadId = messages[0]?.threadId;
2175
2191
  if (!threadId) {
2176
2192
  throw new error.MastraError({
2177
- id: "MASTRA_STORAGE_PG_STORE_SAVE_MESSAGES_FAILED",
2193
+ id: storage.createStorageErrorId("PG", "SAVE_MESSAGES", "FAILED"),
2178
2194
  domain: error.ErrorDomain.STORAGE,
2179
2195
  category: error.ErrorCategory.THIRD_PARTY,
2180
2196
  text: `Thread ID is required`
@@ -2183,7 +2199,7 @@ var MemoryPG = class extends storage.MemoryStorage {
2183
2199
  const thread = await this.getThreadById({ threadId });
2184
2200
  if (!thread) {
2185
2201
  throw new error.MastraError({
2186
- id: "MASTRA_STORAGE_PG_STORE_SAVE_MESSAGES_FAILED",
2202
+ id: storage.createStorageErrorId("PG", "SAVE_MESSAGES", "FAILED"),
2187
2203
  domain: error.ErrorDomain.STORAGE,
2188
2204
  category: error.ErrorCategory.THIRD_PARTY,
2189
2205
  text: `Thread ${threadId} not found`,
@@ -2254,7 +2270,7 @@ var MemoryPG = class extends storage.MemoryStorage {
2254
2270
  } catch (error$1) {
2255
2271
  throw new error.MastraError(
2256
2272
  {
2257
- id: "MASTRA_STORAGE_PG_STORE_SAVE_MESSAGES_FAILED",
2273
+ id: storage.createStorageErrorId("PG", "SAVE_MESSAGES", "FAILED"),
2258
2274
  domain: error.ErrorDomain.STORAGE,
2259
2275
  category: error.ErrorCategory.THIRD_PARTY,
2260
2276
  details: {
@@ -2383,7 +2399,7 @@ var MemoryPG = class extends storage.MemoryStorage {
2383
2399
  } catch (error$1) {
2384
2400
  throw new error.MastraError(
2385
2401
  {
2386
- id: "PG_STORE_DELETE_MESSAGES_FAILED",
2402
+ id: storage.createStorageErrorId("PG", "DELETE_MESSAGES", "FAILED"),
2387
2403
  domain: error.ErrorDomain.STORAGE,
2388
2404
  category: error.ErrorCategory.THIRD_PARTY,
2389
2405
  details: { messageIds: messageIds.join(", ") }
@@ -2504,7 +2520,7 @@ var ObservabilityPG = class extends storage.ObservabilityStorage {
2504
2520
  } catch (error$1) {
2505
2521
  throw new error.MastraError(
2506
2522
  {
2507
- id: "PG_STORE_CREATE_SPAN_FAILED",
2523
+ id: storage.createStorageErrorId("PG", "CREATE_SPAN", "FAILED"),
2508
2524
  domain: error.ErrorDomain.STORAGE,
2509
2525
  category: error.ErrorCategory.USER,
2510
2526
  details: {
@@ -2550,7 +2566,7 @@ var ObservabilityPG = class extends storage.ObservabilityStorage {
2550
2566
  } catch (error$1) {
2551
2567
  throw new error.MastraError(
2552
2568
  {
2553
- id: "PG_STORE_GET_TRACE_FAILED",
2569
+ id: storage.createStorageErrorId("PG", "GET_TRACE", "FAILED"),
2554
2570
  domain: error.ErrorDomain.STORAGE,
2555
2571
  category: error.ErrorCategory.USER,
2556
2572
  details: {
@@ -2582,7 +2598,7 @@ var ObservabilityPG = class extends storage.ObservabilityStorage {
2582
2598
  } catch (error$1) {
2583
2599
  throw new error.MastraError(
2584
2600
  {
2585
- id: "PG_STORE_UPDATE_SPAN_FAILED",
2601
+ id: storage.createStorageErrorId("PG", "UPDATE_SPAN", "FAILED"),
2586
2602
  domain: error.ErrorDomain.STORAGE,
2587
2603
  category: error.ErrorCategory.USER,
2588
2604
  details: {
@@ -2618,7 +2634,7 @@ var ObservabilityPG = class extends storage.ObservabilityStorage {
2618
2634
  name = `agent run: '${entityId}'`;
2619
2635
  } else {
2620
2636
  const error$1 = new error.MastraError({
2621
- id: "PG_STORE_GET_TRACES_PAGINATED_FAILED",
2637
+ id: storage.createStorageErrorId("PG", "GET_TRACES_PAGINATED", "INVALID_ENTITY_TYPE"),
2622
2638
  domain: error.ErrorDomain.STORAGE,
2623
2639
  category: error.ErrorCategory.USER,
2624
2640
  details: {
@@ -2686,7 +2702,7 @@ var ObservabilityPG = class extends storage.ObservabilityStorage {
2686
2702
  } catch (error$1) {
2687
2703
  throw new error.MastraError(
2688
2704
  {
2689
- id: "PG_STORE_GET_TRACES_PAGINATED_FAILED",
2705
+ id: storage.createStorageErrorId("PG", "GET_TRACES_PAGINATED", "FAILED"),
2690
2706
  domain: error.ErrorDomain.STORAGE,
2691
2707
  category: error.ErrorCategory.USER
2692
2708
  },
@@ -2715,7 +2731,7 @@ var ObservabilityPG = class extends storage.ObservabilityStorage {
2715
2731
  } catch (error$1) {
2716
2732
  throw new error.MastraError(
2717
2733
  {
2718
- id: "PG_STORE_BATCH_CREATE_SPANS_FAILED",
2734
+ id: storage.createStorageErrorId("PG", "BATCH_CREATE_SPANS", "FAILED"),
2719
2735
  domain: error.ErrorDomain.STORAGE,
2720
2736
  category: error.ErrorCategory.USER
2721
2737
  },
@@ -2750,7 +2766,7 @@ var ObservabilityPG = class extends storage.ObservabilityStorage {
2750
2766
  } catch (error$1) {
2751
2767
  throw new error.MastraError(
2752
2768
  {
2753
- id: "PG_STORE_BATCH_UPDATE_SPANS_FAILED",
2769
+ id: storage.createStorageErrorId("PG", "BATCH_UPDATE_SPANS", "FAILED"),
2754
2770
  domain: error.ErrorDomain.STORAGE,
2755
2771
  category: error.ErrorCategory.USER
2756
2772
  },
@@ -2769,7 +2785,7 @@ var ObservabilityPG = class extends storage.ObservabilityStorage {
2769
2785
  } catch (error$1) {
2770
2786
  throw new error.MastraError(
2771
2787
  {
2772
- id: "PG_STORE_BATCH_DELETE_TRACES_FAILED",
2788
+ id: storage.createStorageErrorId("PG", "BATCH_DELETE_TRACES", "FAILED"),
2773
2789
  domain: error.ErrorDomain.STORAGE,
2774
2790
  category: error.ErrorCategory.USER
2775
2791
  },
@@ -2899,7 +2915,7 @@ var StoreOperationsPG = class extends storage.StoreOperations {
2899
2915
  } catch (error$1) {
2900
2916
  throw new error.MastraError(
2901
2917
  {
2902
- id: "MASTRA_STORAGE_PG_STORE_INSERT_FAILED",
2918
+ id: storage.createStorageErrorId("PG", "INSERT", "FAILED"),
2903
2919
  domain: error.ErrorDomain.STORAGE,
2904
2920
  category: error.ErrorCategory.THIRD_PARTY,
2905
2921
  details: {
@@ -2918,7 +2934,7 @@ var StoreOperationsPG = class extends storage.StoreOperations {
2918
2934
  } catch (error$1) {
2919
2935
  throw new error.MastraError(
2920
2936
  {
2921
- id: "MASTRA_STORAGE_PG_STORE_CLEAR_TABLE_FAILED",
2937
+ id: storage.createStorageErrorId("PG", "CLEAR_TABLE", "FAILED"),
2922
2938
  domain: error.ErrorDomain.STORAGE,
2923
2939
  category: error.ErrorCategory.THIRD_PARTY,
2924
2940
  details: {
@@ -2991,7 +3007,7 @@ var StoreOperationsPG = class extends storage.StoreOperations {
2991
3007
  } catch (error$1) {
2992
3008
  throw new error.MastraError(
2993
3009
  {
2994
- id: "MASTRA_STORAGE_PG_STORE_CREATE_TABLE_FAILED",
3010
+ id: storage.createStorageErrorId("PG", "CREATE_TABLE", "FAILED"),
2995
3011
  domain: error.ErrorDomain.STORAGE,
2996
3012
  category: error.ErrorCategory.THIRD_PARTY,
2997
3013
  details: {
@@ -3078,7 +3094,7 @@ var StoreOperationsPG = class extends storage.StoreOperations {
3078
3094
  } catch (error$1) {
3079
3095
  throw new error.MastraError(
3080
3096
  {
3081
- id: "MASTRA_STORAGE_PG_STORE_ALTER_TABLE_FAILED",
3097
+ id: storage.createStorageErrorId("PG", "ALTER_TABLE", "FAILED"),
3082
3098
  domain: error.ErrorDomain.STORAGE,
3083
3099
  category: error.ErrorCategory.THIRD_PARTY,
3084
3100
  details: {
@@ -3112,7 +3128,7 @@ var StoreOperationsPG = class extends storage.StoreOperations {
3112
3128
  } catch (error$1) {
3113
3129
  throw new error.MastraError(
3114
3130
  {
3115
- id: "MASTRA_STORAGE_PG_STORE_LOAD_FAILED",
3131
+ id: storage.createStorageErrorId("PG", "LOAD", "FAILED"),
3116
3132
  domain: error.ErrorDomain.STORAGE,
3117
3133
  category: error.ErrorCategory.THIRD_PARTY,
3118
3134
  details: {
@@ -3134,7 +3150,7 @@ var StoreOperationsPG = class extends storage.StoreOperations {
3134
3150
  await this.client.query("ROLLBACK");
3135
3151
  throw new error.MastraError(
3136
3152
  {
3137
- id: "MASTRA_STORAGE_PG_STORE_BATCH_INSERT_FAILED",
3153
+ id: storage.createStorageErrorId("PG", "BATCH_INSERT", "FAILED"),
3138
3154
  domain: error.ErrorDomain.STORAGE,
3139
3155
  category: error.ErrorCategory.THIRD_PARTY,
3140
3156
  details: {
@@ -3154,7 +3170,7 @@ var StoreOperationsPG = class extends storage.StoreOperations {
3154
3170
  } catch (error$1) {
3155
3171
  throw new error.MastraError(
3156
3172
  {
3157
- id: "MASTRA_STORAGE_PG_STORE_DROP_TABLE_FAILED",
3173
+ id: storage.createStorageErrorId("PG", "DROP_TABLE", "FAILED"),
3158
3174
  domain: error.ErrorDomain.STORAGE,
3159
3175
  category: error.ErrorCategory.THIRD_PARTY,
3160
3176
  details: {
@@ -3227,7 +3243,7 @@ var StoreOperationsPG = class extends storage.StoreOperations {
3227
3243
  }
3228
3244
  throw new error.MastraError(
3229
3245
  {
3230
- id: "MASTRA_STORAGE_PG_INDEX_CREATE_FAILED",
3246
+ id: storage.createStorageErrorId("PG", "INDEX_CREATE", "FAILED"),
3231
3247
  domain: error.ErrorDomain.STORAGE,
3232
3248
  category: error.ErrorCategory.THIRD_PARTY,
3233
3249
  details: {
@@ -3259,7 +3275,7 @@ var StoreOperationsPG = class extends storage.StoreOperations {
3259
3275
  } catch (error$1) {
3260
3276
  throw new error.MastraError(
3261
3277
  {
3262
- id: "MASTRA_STORAGE_PG_INDEX_DROP_FAILED",
3278
+ id: storage.createStorageErrorId("PG", "INDEX_DROP", "FAILED"),
3263
3279
  domain: error.ErrorDomain.STORAGE,
3264
3280
  category: error.ErrorCategory.THIRD_PARTY,
3265
3281
  details: {
@@ -3335,7 +3351,7 @@ var StoreOperationsPG = class extends storage.StoreOperations {
3335
3351
  } catch (error$1) {
3336
3352
  throw new error.MastraError(
3337
3353
  {
3338
- id: "MASTRA_STORAGE_PG_INDEX_LIST_FAILED",
3354
+ id: storage.createStorageErrorId("PG", "INDEX_LIST", "FAILED"),
3339
3355
  domain: error.ErrorDomain.STORAGE,
3340
3356
  category: error.ErrorCategory.THIRD_PARTY,
3341
3357
  details: tableName ? {
@@ -3417,7 +3433,7 @@ var StoreOperationsPG = class extends storage.StoreOperations {
3417
3433
  } catch (error$1) {
3418
3434
  throw new error.MastraError(
3419
3435
  {
3420
- id: "MASTRA_STORAGE_PG_STORE_CREATE_PERFORMANCE_INDEXES_FAILED",
3436
+ id: storage.createStorageErrorId("PG", "CREATE_AUTOMATIC_INDEXES", "FAILED"),
3421
3437
  domain: error.ErrorDomain.STORAGE,
3422
3438
  category: error.ErrorCategory.THIRD_PARTY
3423
3439
  },
@@ -3479,7 +3495,7 @@ var StoreOperationsPG = class extends storage.StoreOperations {
3479
3495
  } catch (error$1) {
3480
3496
  throw new error.MastraError(
3481
3497
  {
3482
- id: "MASTRA_STORAGE_PG_INDEX_DESCRIBE_FAILED",
3498
+ id: storage.createStorageErrorId("PG", "INDEX_DESCRIBE", "FAILED"),
3483
3499
  domain: error.ErrorDomain.STORAGE,
3484
3500
  category: error.ErrorCategory.THIRD_PARTY,
3485
3501
  details: {
@@ -3524,7 +3540,7 @@ var StoreOperationsPG = class extends storage.StoreOperations {
3524
3540
  } catch (error$1) {
3525
3541
  throw new error.MastraError(
3526
3542
  {
3527
- id: "MASTRA_STORAGE_PG_STORE_UPDATE_FAILED",
3543
+ id: storage.createStorageErrorId("PG", "UPDATE", "FAILED"),
3528
3544
  domain: error.ErrorDomain.STORAGE,
3529
3545
  category: error.ErrorCategory.THIRD_PARTY,
3530
3546
  details: {
@@ -3552,7 +3568,7 @@ var StoreOperationsPG = class extends storage.StoreOperations {
3552
3568
  await this.client.query("ROLLBACK");
3553
3569
  throw new error.MastraError(
3554
3570
  {
3555
- id: "MASTRA_STORAGE_PG_STORE_BATCH_UPDATE_FAILED",
3571
+ id: storage.createStorageErrorId("PG", "BATCH_UPDATE", "FAILED"),
3556
3572
  domain: error.ErrorDomain.STORAGE,
3557
3573
  category: error.ErrorCategory.THIRD_PARTY,
3558
3574
  details: {
@@ -3593,7 +3609,7 @@ var StoreOperationsPG = class extends storage.StoreOperations {
3593
3609
  } catch (error$1) {
3594
3610
  throw new error.MastraError(
3595
3611
  {
3596
- id: "MASTRA_STORAGE_PG_STORE_BATCH_DELETE_FAILED",
3612
+ id: storage.createStorageErrorId("PG", "BATCH_DELETE", "FAILED"),
3597
3613
  domain: error.ErrorDomain.STORAGE,
3598
3614
  category: error.ErrorCategory.THIRD_PARTY,
3599
3615
  details: {
@@ -3607,20 +3623,12 @@ var StoreOperationsPG = class extends storage.StoreOperations {
3607
3623
  }
3608
3624
  };
3609
3625
  function transformScoreRow(row) {
3610
- return {
3611
- ...row,
3612
- input: storage.safelyParseJSON(row.input),
3613
- scorer: storage.safelyParseJSON(row.scorer),
3614
- preprocessStepResult: storage.safelyParseJSON(row.preprocessStepResult),
3615
- analyzeStepResult: storage.safelyParseJSON(row.analyzeStepResult),
3616
- metadata: storage.safelyParseJSON(row.metadata),
3617
- output: storage.safelyParseJSON(row.output),
3618
- additionalContext: storage.safelyParseJSON(row.additionalContext),
3619
- requestContext: storage.safelyParseJSON(row.requestContext),
3620
- entity: storage.safelyParseJSON(row.entity),
3621
- createdAt: row.createdAtZ || row.createdAt,
3622
- updatedAt: row.updatedAtZ || row.updatedAt
3623
- };
3626
+ return storage.transformScoreRow(row, {
3627
+ preferredTimestampFields: {
3628
+ createdAt: "createdAtZ",
3629
+ updatedAt: "updatedAtZ"
3630
+ }
3631
+ });
3624
3632
  }
3625
3633
  var ScoresPG = class extends storage.ScoresStorage {
3626
3634
  client;
@@ -3646,7 +3654,7 @@ var ScoresPG = class extends storage.ScoresStorage {
3646
3654
  } catch (error$1) {
3647
3655
  throw new error.MastraError(
3648
3656
  {
3649
- id: "MASTRA_STORAGE_PG_STORE_GET_SCORE_BY_ID_FAILED",
3657
+ id: storage.createStorageErrorId("PG", "GET_SCORE_BY_ID", "FAILED"),
3650
3658
  domain: error.ErrorDomain.STORAGE,
3651
3659
  category: error.ErrorCategory.THIRD_PARTY
3652
3660
  },
@@ -3714,7 +3722,7 @@ var ScoresPG = class extends storage.ScoresStorage {
3714
3722
  } catch (error$1) {
3715
3723
  throw new error.MastraError(
3716
3724
  {
3717
- id: "MASTRA_STORAGE_PG_STORE_GET_SCORES_BY_SCORER_ID_FAILED",
3725
+ id: storage.createStorageErrorId("PG", "GET_SCORES_BY_SCORER_ID", "FAILED"),
3718
3726
  domain: error.ErrorDomain.STORAGE,
3719
3727
  category: error.ErrorCategory.THIRD_PARTY
3720
3728
  },
@@ -3729,7 +3737,7 @@ var ScoresPG = class extends storage.ScoresStorage {
3729
3737
  } catch (error$1) {
3730
3738
  throw new error.MastraError(
3731
3739
  {
3732
- id: "MASTRA_STORAGE_PG_STORE_SAVE_SCORE_FAILED_INVALID_SCORE_PAYLOAD",
3740
+ id: storage.createStorageErrorId("PG", "SAVE_SCORE", "INVALID_PAYLOAD"),
3733
3741
  domain: error.ErrorDomain.STORAGE,
3734
3742
  category: error.ErrorCategory.USER,
3735
3743
  details: {
@@ -3767,8 +3775,6 @@ var ScoresPG = class extends storage.ScoresStorage {
3767
3775
  scorer: scorer ? JSON.stringify(scorer) : null,
3768
3776
  preprocessStepResult: preprocessStepResult ? JSON.stringify(preprocessStepResult) : null,
3769
3777
  analyzeStepResult: analyzeStepResult ? JSON.stringify(analyzeStepResult) : null,
3770
- metadata: metadata ? JSON.stringify(metadata) : null,
3771
- additionalContext: additionalContext ? JSON.stringify(additionalContext) : null,
3772
3778
  requestContext: requestContext ? JSON.stringify(requestContext) : null,
3773
3779
  entity: entity ? JSON.stringify(entity) : null,
3774
3780
  createdAt: (/* @__PURE__ */ new Date()).toISOString(),
@@ -3780,7 +3786,7 @@ var ScoresPG = class extends storage.ScoresStorage {
3780
3786
  } catch (error$1) {
3781
3787
  throw new error.MastraError(
3782
3788
  {
3783
- id: "MASTRA_STORAGE_PG_STORE_SAVE_SCORE_FAILED",
3789
+ id: storage.createStorageErrorId("PG", "SAVE_SCORE", "FAILED"),
3784
3790
  domain: error.ErrorDomain.STORAGE,
3785
3791
  category: error.ErrorCategory.THIRD_PARTY
3786
3792
  },
@@ -3829,7 +3835,7 @@ var ScoresPG = class extends storage.ScoresStorage {
3829
3835
  } catch (error$1) {
3830
3836
  throw new error.MastraError(
3831
3837
  {
3832
- id: "MASTRA_STORAGE_PG_STORE_GET_SCORES_BY_RUN_ID_FAILED",
3838
+ id: storage.createStorageErrorId("PG", "GET_SCORES_BY_RUN_ID", "FAILED"),
3833
3839
  domain: error.ErrorDomain.STORAGE,
3834
3840
  category: error.ErrorCategory.THIRD_PARTY
3835
3841
  },
@@ -3879,7 +3885,7 @@ var ScoresPG = class extends storage.ScoresStorage {
3879
3885
  } catch (error$1) {
3880
3886
  throw new error.MastraError(
3881
3887
  {
3882
- id: "MASTRA_STORAGE_PG_STORE_GET_SCORES_BY_ENTITY_ID_FAILED",
3888
+ id: storage.createStorageErrorId("PG", "GET_SCORES_BY_ENTITY_ID", "FAILED"),
3883
3889
  domain: error.ErrorDomain.STORAGE,
3884
3890
  category: error.ErrorCategory.THIRD_PARTY
3885
3891
  },
@@ -3922,7 +3928,7 @@ var ScoresPG = class extends storage.ScoresStorage {
3922
3928
  } catch (error$1) {
3923
3929
  throw new error.MastraError(
3924
3930
  {
3925
- id: "MASTRA_STORAGE_PG_STORE_GET_SCORES_BY_SPAN_FAILED",
3931
+ id: storage.createStorageErrorId("PG", "GET_SCORES_BY_SPAN", "FAILED"),
3926
3932
  domain: error.ErrorDomain.STORAGE,
3927
3933
  category: error.ErrorCategory.THIRD_PARTY
3928
3934
  },
@@ -3997,7 +4003,7 @@ var WorkflowsPG = class extends storage.WorkflowsStorage {
3997
4003
  } catch (error$1) {
3998
4004
  throw new error.MastraError(
3999
4005
  {
4000
- id: "MASTRA_STORAGE_PG_STORE_PERSIST_WORKFLOW_SNAPSHOT_FAILED",
4006
+ id: storage.createStorageErrorId("PG", "PERSIST_WORKFLOW_SNAPSHOT", "FAILED"),
4001
4007
  domain: error.ErrorDomain.STORAGE,
4002
4008
  category: error.ErrorCategory.THIRD_PARTY
4003
4009
  },
@@ -4018,7 +4024,7 @@ var WorkflowsPG = class extends storage.WorkflowsStorage {
4018
4024
  } catch (error$1) {
4019
4025
  throw new error.MastraError(
4020
4026
  {
4021
- id: "MASTRA_STORAGE_PG_STORE_LOAD_WORKFLOW_SNAPSHOT_FAILED",
4027
+ id: storage.createStorageErrorId("PG", "LOAD_WORKFLOW_SNAPSHOT", "FAILED"),
4022
4028
  domain: error.ErrorDomain.STORAGE,
4023
4029
  category: error.ErrorCategory.THIRD_PARTY
4024
4030
  },
@@ -4059,7 +4065,7 @@ var WorkflowsPG = class extends storage.WorkflowsStorage {
4059
4065
  } catch (error$1) {
4060
4066
  throw new error.MastraError(
4061
4067
  {
4062
- id: "MASTRA_STORAGE_PG_STORE_GET_WORKFLOW_RUN_BY_ID_FAILED",
4068
+ id: storage.createStorageErrorId("PG", "GET_WORKFLOW_RUN_BY_ID", "FAILED"),
4063
4069
  domain: error.ErrorDomain.STORAGE,
4064
4070
  category: error.ErrorCategory.THIRD_PARTY,
4065
4071
  details: {
@@ -4141,7 +4147,7 @@ var WorkflowsPG = class extends storage.WorkflowsStorage {
4141
4147
  } catch (error$1) {
4142
4148
  throw new error.MastraError(
4143
4149
  {
4144
- id: "MASTRA_STORAGE_PG_STORE_LIST_WORKFLOW_RUNS_FAILED",
4150
+ id: storage.createStorageErrorId("PG", "LIST_WORKFLOW_RUNS", "FAILED"),
4145
4151
  domain: error.ErrorDomain.STORAGE,
4146
4152
  category: error.ErrorCategory.THIRD_PARTY,
4147
4153
  details: {
@@ -4165,7 +4171,7 @@ var PostgresStore = class extends storage.MastraStorage {
4165
4171
  constructor(config) {
4166
4172
  try {
4167
4173
  validateConfig("PostgresStore", config);
4168
- super({ id: config.id, name: "PostgresStore" });
4174
+ super({ id: config.id, name: "PostgresStore", disableInit: config.disableInit });
4169
4175
  this.schema = config.schemaName || "public";
4170
4176
  if (isConnectionStringConfig(config)) {
4171
4177
  this.#config = {
@@ -4203,7 +4209,7 @@ var PostgresStore = class extends storage.MastraStorage {
4203
4209
  } catch (e) {
4204
4210
  throw new error.MastraError(
4205
4211
  {
4206
- id: "MASTRA_STORAGE_PG_STORE_INITIALIZATION_FAILED",
4212
+ id: storage.createStorageErrorId("PG", "INITIALIZATION", "FAILED"),
4207
4213
  domain: error.ErrorDomain.STORAGE,
4208
4214
  category: error.ErrorCategory.USER
4209
4215
  },
@@ -4241,7 +4247,7 @@ var PostgresStore = class extends storage.MastraStorage {
4241
4247
  this.isConnected = false;
4242
4248
  throw new error.MastraError(
4243
4249
  {
4244
- id: "MASTRA_STORAGE_POSTGRES_STORE_INIT_FAILED",
4250
+ id: storage.createStorageErrorId("PG", "INIT", "FAILED"),
4245
4251
  domain: error.ErrorDomain.STORAGE,
4246
4252
  category: error.ErrorCategory.THIRD_PARTY
4247
4253
  },
@@ -4398,7 +4404,7 @@ var PostgresStore = class extends storage.MastraStorage {
4398
4404
  async createSpan(span) {
4399
4405
  if (!this.stores.observability) {
4400
4406
  throw new error.MastraError({
4401
- id: "PG_STORE_OBSERVABILITY_NOT_INITIALIZED",
4407
+ id: storage.createStorageErrorId("PG", "OBSERVABILITY", "NOT_INITIALIZED"),
4402
4408
  domain: error.ErrorDomain.STORAGE,
4403
4409
  category: error.ErrorCategory.SYSTEM,
4404
4410
  text: "Observability storage is not initialized"
@@ -4413,7 +4419,7 @@ var PostgresStore = class extends storage.MastraStorage {
4413
4419
  }) {
4414
4420
  if (!this.stores.observability) {
4415
4421
  throw new error.MastraError({
4416
- id: "PG_STORE_OBSERVABILITY_NOT_INITIALIZED",
4422
+ id: storage.createStorageErrorId("PG", "OBSERVABILITY", "NOT_INITIALIZED"),
4417
4423
  domain: error.ErrorDomain.STORAGE,
4418
4424
  category: error.ErrorCategory.SYSTEM,
4419
4425
  text: "Observability storage is not initialized"
@@ -4424,7 +4430,7 @@ var PostgresStore = class extends storage.MastraStorage {
4424
4430
  async getTrace(traceId) {
4425
4431
  if (!this.stores.observability) {
4426
4432
  throw new error.MastraError({
4427
- id: "PG_STORE_OBSERVABILITY_NOT_INITIALIZED",
4433
+ id: storage.createStorageErrorId("PG", "OBSERVABILITY", "NOT_INITIALIZED"),
4428
4434
  domain: error.ErrorDomain.STORAGE,
4429
4435
  category: error.ErrorCategory.SYSTEM,
4430
4436
  text: "Observability storage is not initialized"
@@ -4435,7 +4441,7 @@ var PostgresStore = class extends storage.MastraStorage {
4435
4441
  async getTracesPaginated(args) {
4436
4442
  if (!this.stores.observability) {
4437
4443
  throw new error.MastraError({
4438
- id: "PG_STORE_OBSERVABILITY_NOT_INITIALIZED",
4444
+ id: storage.createStorageErrorId("PG", "OBSERVABILITY", "NOT_INITIALIZED"),
4439
4445
  domain: error.ErrorDomain.STORAGE,
4440
4446
  category: error.ErrorCategory.SYSTEM,
4441
4447
  text: "Observability storage is not initialized"
@@ -4446,7 +4452,7 @@ var PostgresStore = class extends storage.MastraStorage {
4446
4452
  async batchCreateSpans(args) {
4447
4453
  if (!this.stores.observability) {
4448
4454
  throw new error.MastraError({
4449
- id: "PG_STORE_OBSERVABILITY_NOT_INITIALIZED",
4455
+ id: storage.createStorageErrorId("PG", "OBSERVABILITY", "NOT_INITIALIZED"),
4450
4456
  domain: error.ErrorDomain.STORAGE,
4451
4457
  category: error.ErrorCategory.SYSTEM,
4452
4458
  text: "Observability storage is not initialized"
@@ -4457,7 +4463,7 @@ var PostgresStore = class extends storage.MastraStorage {
4457
4463
  async batchUpdateSpans(args) {
4458
4464
  if (!this.stores.observability) {
4459
4465
  throw new error.MastraError({
4460
- id: "PG_STORE_OBSERVABILITY_NOT_INITIALIZED",
4466
+ id: storage.createStorageErrorId("PG", "OBSERVABILITY", "NOT_INITIALIZED"),
4461
4467
  domain: error.ErrorDomain.STORAGE,
4462
4468
  category: error.ErrorCategory.SYSTEM,
4463
4469
  text: "Observability storage is not initialized"
@@ -4468,7 +4474,7 @@ var PostgresStore = class extends storage.MastraStorage {
4468
4474
  async batchDeleteTraces(args) {
4469
4475
  if (!this.stores.observability) {
4470
4476
  throw new error.MastraError({
4471
- id: "PG_STORE_OBSERVABILITY_NOT_INITIALIZED",
4477
+ id: storage.createStorageErrorId("PG", "OBSERVABILITY", "NOT_INITIALIZED"),
4472
4478
  domain: error.ErrorDomain.STORAGE,
4473
4479
  category: error.ErrorCategory.SYSTEM,
4474
4480
  text: "Observability storage is not initialized"