@mastra/pg 1.0.0-beta.5 → 1.0.0-beta.6

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
@@ -1,5 +1,5 @@
1
1
  import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
2
- import { createVectorErrorId, MastraStorage, createStorageErrorId, StoreOperations, TABLE_SCHEMAS, TABLE_WORKFLOW_SNAPSHOT, TABLE_SPANS, TABLE_THREADS, TABLE_MESSAGES, TABLE_TRACES, TABLE_SCORERS, ScoresStorage, normalizePerPage, calculatePagination, WorkflowsStorage, MemoryStorage, TABLE_RESOURCES, ObservabilityStorage, transformScoreRow as transformScoreRow$1 } from '@mastra/core/storage';
2
+ import { createVectorErrorId, MastraStorage, createStorageErrorId, StoreOperations, TABLE_SCHEMAS, TABLE_WORKFLOW_SNAPSHOT, TABLE_SPANS, TABLE_THREADS, TABLE_MESSAGES, TABLE_TRACES, TABLE_SCORERS, ScoresStorage, normalizePerPage, calculatePagination, WorkflowsStorage, MemoryStorage, TABLE_RESOURCES, ObservabilityStorage, AgentsStorage, transformScoreRow as transformScoreRow$1, TABLE_AGENTS } from '@mastra/core/storage';
3
3
  import { parseSqlIdentifier, parseFieldKey } from '@mastra/core/utils';
4
4
  import { MastraVector } from '@mastra/core/vector';
5
5
  import { Mutex } from 'async-mutex';
@@ -1637,7 +1637,302 @@ function transformFromSqlRow({
1637
1637
  return result;
1638
1638
  }
1639
1639
 
1640
- // src/storage/domains/memory/index.ts
1640
+ // src/storage/domains/agents/index.ts
1641
+ var AgentsPG = class extends AgentsStorage {
1642
+ client;
1643
+ schema;
1644
+ constructor({ client, schema }) {
1645
+ super();
1646
+ this.client = client;
1647
+ this.schema = schema;
1648
+ }
1649
+ parseJson(value, fieldName) {
1650
+ if (!value) return void 0;
1651
+ if (typeof value !== "string") return value;
1652
+ try {
1653
+ return JSON.parse(value);
1654
+ } catch (error) {
1655
+ const details = {
1656
+ value: value.length > 100 ? value.substring(0, 100) + "..." : value
1657
+ };
1658
+ if (fieldName) {
1659
+ details.field = fieldName;
1660
+ }
1661
+ throw new MastraError(
1662
+ {
1663
+ id: createStorageErrorId("PG", "PARSE_JSON", "INVALID_JSON"),
1664
+ domain: ErrorDomain.STORAGE,
1665
+ category: ErrorCategory.SYSTEM,
1666
+ text: `Failed to parse JSON${fieldName ? ` for field "${fieldName}"` : ""}: ${error instanceof Error ? error.message : "Unknown error"}`,
1667
+ details
1668
+ },
1669
+ error
1670
+ );
1671
+ }
1672
+ }
1673
+ parseRow(row) {
1674
+ return {
1675
+ id: row.id,
1676
+ name: row.name,
1677
+ description: row.description,
1678
+ instructions: row.instructions,
1679
+ model: this.parseJson(row.model, "model"),
1680
+ tools: this.parseJson(row.tools, "tools"),
1681
+ defaultOptions: this.parseJson(row.defaultOptions, "defaultOptions"),
1682
+ workflows: this.parseJson(row.workflows, "workflows"),
1683
+ agents: this.parseJson(row.agents, "agents"),
1684
+ inputProcessors: this.parseJson(row.inputProcessors, "inputProcessors"),
1685
+ outputProcessors: this.parseJson(row.outputProcessors, "outputProcessors"),
1686
+ memory: this.parseJson(row.memory, "memory"),
1687
+ scorers: this.parseJson(row.scorers, "scorers"),
1688
+ metadata: this.parseJson(row.metadata, "metadata"),
1689
+ createdAt: row.createdAtZ || row.createdAt,
1690
+ updatedAt: row.updatedAtZ || row.updatedAt
1691
+ };
1692
+ }
1693
+ async getAgentById({ id }) {
1694
+ try {
1695
+ const tableName = getTableName({ indexName: TABLE_AGENTS, schemaName: getSchemaName(this.schema) });
1696
+ const result = await this.client.oneOrNone(`SELECT * FROM ${tableName} WHERE id = $1`, [id]);
1697
+ if (!result) {
1698
+ return null;
1699
+ }
1700
+ return this.parseRow(result);
1701
+ } catch (error) {
1702
+ throw new MastraError(
1703
+ {
1704
+ id: createStorageErrorId("PG", "GET_AGENT_BY_ID", "FAILED"),
1705
+ domain: ErrorDomain.STORAGE,
1706
+ category: ErrorCategory.THIRD_PARTY,
1707
+ details: { agentId: id }
1708
+ },
1709
+ error
1710
+ );
1711
+ }
1712
+ }
1713
+ async createAgent({ agent }) {
1714
+ try {
1715
+ const tableName = getTableName({ indexName: TABLE_AGENTS, schemaName: getSchemaName(this.schema) });
1716
+ const now = /* @__PURE__ */ new Date();
1717
+ const nowIso = now.toISOString();
1718
+ await this.client.none(
1719
+ `INSERT INTO ${tableName} (
1720
+ id, name, description, instructions, model, tools,
1721
+ "defaultOptions", workflows, agents, "inputProcessors", "outputProcessors", memory, scorers, metadata,
1722
+ "createdAt", "createdAtZ", "updatedAt", "updatedAtZ"
1723
+ ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18)`,
1724
+ [
1725
+ agent.id,
1726
+ agent.name,
1727
+ agent.description ?? null,
1728
+ agent.instructions,
1729
+ JSON.stringify(agent.model),
1730
+ agent.tools ? JSON.stringify(agent.tools) : null,
1731
+ agent.defaultOptions ? JSON.stringify(agent.defaultOptions) : null,
1732
+ agent.workflows ? JSON.stringify(agent.workflows) : null,
1733
+ agent.agents ? JSON.stringify(agent.agents) : null,
1734
+ agent.inputProcessors ? JSON.stringify(agent.inputProcessors) : null,
1735
+ agent.outputProcessors ? JSON.stringify(agent.outputProcessors) : null,
1736
+ agent.memory ? JSON.stringify(agent.memory) : null,
1737
+ agent.scorers ? JSON.stringify(agent.scorers) : null,
1738
+ agent.metadata ? JSON.stringify(agent.metadata) : null,
1739
+ nowIso,
1740
+ nowIso,
1741
+ nowIso,
1742
+ nowIso
1743
+ ]
1744
+ );
1745
+ return {
1746
+ ...agent,
1747
+ createdAt: now,
1748
+ updatedAt: now
1749
+ };
1750
+ } catch (error) {
1751
+ throw new MastraError(
1752
+ {
1753
+ id: createStorageErrorId("PG", "CREATE_AGENT", "FAILED"),
1754
+ domain: ErrorDomain.STORAGE,
1755
+ category: ErrorCategory.THIRD_PARTY,
1756
+ details: { agentId: agent.id }
1757
+ },
1758
+ error
1759
+ );
1760
+ }
1761
+ }
1762
+ async updateAgent({ id, ...updates }) {
1763
+ try {
1764
+ const tableName = getTableName({ indexName: TABLE_AGENTS, schemaName: getSchemaName(this.schema) });
1765
+ const existingAgent = await this.getAgentById({ id });
1766
+ if (!existingAgent) {
1767
+ throw new MastraError({
1768
+ id: createStorageErrorId("PG", "UPDATE_AGENT", "NOT_FOUND"),
1769
+ domain: ErrorDomain.STORAGE,
1770
+ category: ErrorCategory.USER,
1771
+ text: `Agent ${id} not found`,
1772
+ details: { agentId: id }
1773
+ });
1774
+ }
1775
+ const setClauses = [];
1776
+ const values = [];
1777
+ let paramIndex = 1;
1778
+ if (updates.name !== void 0) {
1779
+ setClauses.push(`name = $${paramIndex++}`);
1780
+ values.push(updates.name);
1781
+ }
1782
+ if (updates.description !== void 0) {
1783
+ setClauses.push(`description = $${paramIndex++}`);
1784
+ values.push(updates.description);
1785
+ }
1786
+ if (updates.instructions !== void 0) {
1787
+ setClauses.push(`instructions = $${paramIndex++}`);
1788
+ values.push(updates.instructions);
1789
+ }
1790
+ if (updates.model !== void 0) {
1791
+ setClauses.push(`model = $${paramIndex++}`);
1792
+ values.push(JSON.stringify(updates.model));
1793
+ }
1794
+ if (updates.tools !== void 0) {
1795
+ setClauses.push(`tools = $${paramIndex++}`);
1796
+ values.push(JSON.stringify(updates.tools));
1797
+ }
1798
+ if (updates.defaultOptions !== void 0) {
1799
+ setClauses.push(`"defaultOptions" = $${paramIndex++}`);
1800
+ values.push(JSON.stringify(updates.defaultOptions));
1801
+ }
1802
+ if (updates.workflows !== void 0) {
1803
+ setClauses.push(`workflows = $${paramIndex++}`);
1804
+ values.push(JSON.stringify(updates.workflows));
1805
+ }
1806
+ if (updates.agents !== void 0) {
1807
+ setClauses.push(`agents = $${paramIndex++}`);
1808
+ values.push(JSON.stringify(updates.agents));
1809
+ }
1810
+ if (updates.inputProcessors !== void 0) {
1811
+ setClauses.push(`"inputProcessors" = $${paramIndex++}`);
1812
+ values.push(JSON.stringify(updates.inputProcessors));
1813
+ }
1814
+ if (updates.outputProcessors !== void 0) {
1815
+ setClauses.push(`"outputProcessors" = $${paramIndex++}`);
1816
+ values.push(JSON.stringify(updates.outputProcessors));
1817
+ }
1818
+ if (updates.memory !== void 0) {
1819
+ setClauses.push(`memory = $${paramIndex++}`);
1820
+ values.push(JSON.stringify(updates.memory));
1821
+ }
1822
+ if (updates.scorers !== void 0) {
1823
+ setClauses.push(`scorers = $${paramIndex++}`);
1824
+ values.push(JSON.stringify(updates.scorers));
1825
+ }
1826
+ if (updates.metadata !== void 0) {
1827
+ const mergedMetadata = { ...existingAgent.metadata, ...updates.metadata };
1828
+ setClauses.push(`metadata = $${paramIndex++}`);
1829
+ values.push(JSON.stringify(mergedMetadata));
1830
+ }
1831
+ const now = (/* @__PURE__ */ new Date()).toISOString();
1832
+ setClauses.push(`"updatedAt" = $${paramIndex++}`);
1833
+ values.push(now);
1834
+ setClauses.push(`"updatedAtZ" = $${paramIndex++}`);
1835
+ values.push(now);
1836
+ values.push(id);
1837
+ if (setClauses.length > 2) {
1838
+ await this.client.none(`UPDATE ${tableName} SET ${setClauses.join(", ")} WHERE id = $${paramIndex}`, values);
1839
+ }
1840
+ const updatedAgent = await this.getAgentById({ id });
1841
+ if (!updatedAgent) {
1842
+ throw new MastraError({
1843
+ id: createStorageErrorId("PG", "UPDATE_AGENT", "NOT_FOUND_AFTER_UPDATE"),
1844
+ domain: ErrorDomain.STORAGE,
1845
+ category: ErrorCategory.SYSTEM,
1846
+ text: `Agent ${id} not found after update`,
1847
+ details: { agentId: id }
1848
+ });
1849
+ }
1850
+ return updatedAgent;
1851
+ } catch (error) {
1852
+ if (error instanceof MastraError) {
1853
+ throw error;
1854
+ }
1855
+ throw new MastraError(
1856
+ {
1857
+ id: createStorageErrorId("PG", "UPDATE_AGENT", "FAILED"),
1858
+ domain: ErrorDomain.STORAGE,
1859
+ category: ErrorCategory.THIRD_PARTY,
1860
+ details: { agentId: id }
1861
+ },
1862
+ error
1863
+ );
1864
+ }
1865
+ }
1866
+ async deleteAgent({ id }) {
1867
+ try {
1868
+ const tableName = getTableName({ indexName: TABLE_AGENTS, schemaName: getSchemaName(this.schema) });
1869
+ await this.client.none(`DELETE FROM ${tableName} WHERE id = $1`, [id]);
1870
+ } catch (error) {
1871
+ throw new MastraError(
1872
+ {
1873
+ id: createStorageErrorId("PG", "DELETE_AGENT", "FAILED"),
1874
+ domain: ErrorDomain.STORAGE,
1875
+ category: ErrorCategory.THIRD_PARTY,
1876
+ details: { agentId: id }
1877
+ },
1878
+ error
1879
+ );
1880
+ }
1881
+ }
1882
+ async listAgents(args) {
1883
+ const { page = 0, perPage: perPageInput, orderBy } = args || {};
1884
+ const { field, direction } = this.parseOrderBy(orderBy);
1885
+ if (page < 0) {
1886
+ throw new MastraError(
1887
+ {
1888
+ id: createStorageErrorId("PG", "LIST_AGENTS", "INVALID_PAGE"),
1889
+ domain: ErrorDomain.STORAGE,
1890
+ category: ErrorCategory.USER,
1891
+ details: { page }
1892
+ },
1893
+ new Error("page must be >= 0")
1894
+ );
1895
+ }
1896
+ const perPage = normalizePerPage(perPageInput, 100);
1897
+ const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
1898
+ try {
1899
+ const tableName = getTableName({ indexName: TABLE_AGENTS, schemaName: getSchemaName(this.schema) });
1900
+ const countResult = await this.client.one(`SELECT COUNT(*) as count FROM ${tableName}`);
1901
+ const total = parseInt(countResult.count, 10);
1902
+ if (total === 0) {
1903
+ return {
1904
+ agents: [],
1905
+ total: 0,
1906
+ page,
1907
+ perPage: perPageForResponse,
1908
+ hasMore: false
1909
+ };
1910
+ }
1911
+ const limitValue = perPageInput === false ? total : perPage;
1912
+ const dataResult = await this.client.manyOrNone(
1913
+ `SELECT * FROM ${tableName} ORDER BY "${field}" ${direction} LIMIT $1 OFFSET $2`,
1914
+ [limitValue, offset]
1915
+ );
1916
+ const agents = (dataResult || []).map((row) => this.parseRow(row));
1917
+ return {
1918
+ agents,
1919
+ total,
1920
+ page,
1921
+ perPage: perPageForResponse,
1922
+ hasMore: perPageInput === false ? false : offset + perPage < total
1923
+ };
1924
+ } catch (error) {
1925
+ throw new MastraError(
1926
+ {
1927
+ id: createStorageErrorId("PG", "LIST_AGENTS", "FAILED"),
1928
+ domain: ErrorDomain.STORAGE,
1929
+ category: ErrorCategory.THIRD_PARTY
1930
+ },
1931
+ error
1932
+ );
1933
+ }
1934
+ }
1935
+ };
1641
1936
  var MemoryPG = class extends MemoryStorage {
1642
1937
  client;
1643
1938
  schema;
@@ -4188,12 +4483,14 @@ var PostgresStore = class extends MastraStorage {
4188
4483
  const workflows = new WorkflowsPG({ client: this.#db, operations, schema: this.schema });
4189
4484
  const memory = new MemoryPG({ client: this.#db, schema: this.schema, operations });
4190
4485
  const observability = new ObservabilityPG({ client: this.#db, operations, schema: this.schema });
4486
+ const agents = new AgentsPG({ client: this.#db, schema: this.schema });
4191
4487
  this.stores = {
4192
4488
  operations,
4193
4489
  scores,
4194
4490
  workflows,
4195
4491
  memory,
4196
- observability
4492
+ observability,
4493
+ agents
4197
4494
  };
4198
4495
  } catch (e) {
4199
4496
  throw new MastraError(
@@ -4245,7 +4542,8 @@ var PostgresStore = class extends MastraStorage {
4245
4542
  deleteMessages: true,
4246
4543
  observabilityInstance: true,
4247
4544
  indexManagement: true,
4248
- listScoresBySpan: true
4545
+ listScoresBySpan: true,
4546
+ agents: true
4249
4547
  };
4250
4548
  }
4251
4549
  async createTable({