@mastra/pg 1.9.4-alpha.0 → 1.9.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.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
2
- import { createVectorErrorId, AgentsStorage, TABLE_AGENTS, TABLE_AGENT_VERSIONS, TABLE_SCHEMAS, createStorageErrorId, normalizePerPage, calculatePagination, BackgroundTasksStorage, TABLE_BACKGROUND_TASKS, BlobStore, TABLE_SKILL_BLOBS, DatasetsStorage, TABLE_DATASETS, TABLE_DATASET_ITEMS, TABLE_DATASET_VERSIONS, DATASETS_SCHEMA, TABLE_CONFIGS, DATASET_ITEMS_SCHEMA, DATASET_VERSIONS_SCHEMA, ensureDate, safelyParseJSON, ExperimentsStorage, TABLE_EXPERIMENTS, TABLE_EXPERIMENT_RESULTS, EXPERIMENTS_SCHEMA, EXPERIMENT_RESULTS_SCHEMA, MCPClientsStorage, TABLE_MCP_CLIENTS, TABLE_MCP_CLIENT_VERSIONS, MCPServersStorage, TABLE_MCP_SERVERS, TABLE_MCP_SERVER_VERSIONS, MemoryStorage, TABLE_THREADS, TABLE_MESSAGES, TABLE_RESOURCES, ObservabilityStorage, TABLE_SPANS, listTracesArgsSchema, toTraceSpans, PromptBlocksStorage, TABLE_PROMPT_BLOCKS, TABLE_PROMPT_BLOCK_VERSIONS, ScorerDefinitionsStorage, TABLE_SCORER_DEFINITIONS, TABLE_SCORER_DEFINITION_VERSIONS, ScoresStorage, TABLE_SCORERS, SkillsStorage, TABLE_SKILLS, TABLE_SKILL_VERSIONS, WorkflowsStorage, TABLE_WORKFLOW_SNAPSHOT, WorkspacesStorage, TABLE_WORKSPACES, TABLE_WORKSPACE_VERSIONS, MastraCompositeStore, TraceStatus, getDefaultValue, transformScoreRow as transformScoreRow$1, getSqlType } from '@mastra/core/storage';
2
+ import { createVectorErrorId, AgentsStorage, TABLE_AGENTS, TABLE_AGENT_VERSIONS, TABLE_SCHEMAS, createStorageErrorId, normalizePerPage, calculatePagination, BackgroundTasksStorage, TABLE_BACKGROUND_TASKS, BlobStore, TABLE_SKILL_BLOBS, ChannelsStorage, TABLE_CHANNEL_INSTALLATIONS, TABLE_CHANNEL_CONFIG, DatasetsStorage, TABLE_DATASETS, TABLE_DATASET_ITEMS, TABLE_DATASET_VERSIONS, DATASETS_SCHEMA, TABLE_CONFIGS, DATASET_ITEMS_SCHEMA, DATASET_VERSIONS_SCHEMA, ensureDate, safelyParseJSON, ExperimentsStorage, TABLE_EXPERIMENTS, TABLE_EXPERIMENT_RESULTS, EXPERIMENTS_SCHEMA, EXPERIMENT_RESULTS_SCHEMA, MCPClientsStorage, TABLE_MCP_CLIENTS, TABLE_MCP_CLIENT_VERSIONS, MCPServersStorage, TABLE_MCP_SERVERS, TABLE_MCP_SERVER_VERSIONS, MemoryStorage, TABLE_THREADS, TABLE_MESSAGES, TABLE_RESOURCES, ObservabilityStorage, TABLE_SPANS, listTracesArgsSchema, toTraceSpans, PromptBlocksStorage, TABLE_PROMPT_BLOCKS, TABLE_PROMPT_BLOCK_VERSIONS, ScorerDefinitionsStorage, TABLE_SCORER_DEFINITIONS, TABLE_SCORER_DEFINITION_VERSIONS, ScoresStorage, TABLE_SCORERS, SkillsStorage, TABLE_SKILLS, TABLE_SKILL_VERSIONS, WorkflowsStorage, TABLE_WORKFLOW_SNAPSHOT, WorkspacesStorage, TABLE_WORKSPACES, TABLE_WORKSPACE_VERSIONS, MastraCompositeStore, TraceStatus, getDefaultValue, transformScoreRow as transformScoreRow$1, getSqlType } from '@mastra/core/storage';
3
3
  import { parseSqlIdentifier, parseFieldKey } from '@mastra/core/utils';
4
4
  import { MastraVector, validateTopK, validateUpsertInput } from '@mastra/core/vector';
5
5
  import { Mutex } from 'async-mutex';
@@ -4633,6 +4633,202 @@ var BlobsPG = class extends BlobStore {
4633
4633
  };
4634
4634
  }
4635
4635
  };
4636
+ var ChannelsPG = class _ChannelsPG extends ChannelsStorage {
4637
+ #db;
4638
+ #schema;
4639
+ #skipDefaultIndexes;
4640
+ #indexes;
4641
+ static MANAGED_TABLES = [TABLE_CHANNEL_INSTALLATIONS, TABLE_CHANNEL_CONFIG];
4642
+ constructor(config) {
4643
+ super();
4644
+ const { client, schemaName, skipDefaultIndexes, indexes } = resolvePgConfig(config);
4645
+ this.#db = new PgDB({ client, schemaName, skipDefaultIndexes });
4646
+ this.#schema = schemaName || "public";
4647
+ this.#skipDefaultIndexes = skipDefaultIndexes;
4648
+ this.#indexes = indexes?.filter((idx) => _ChannelsPG.MANAGED_TABLES.includes(idx.table));
4649
+ }
4650
+ async init() {
4651
+ await this.#db.createTable({
4652
+ tableName: TABLE_CHANNEL_INSTALLATIONS,
4653
+ schema: TABLE_SCHEMAS[TABLE_CHANNEL_INSTALLATIONS]
4654
+ });
4655
+ await this.#db.createTable({
4656
+ tableName: TABLE_CHANNEL_CONFIG,
4657
+ schema: TABLE_SCHEMAS[TABLE_CHANNEL_CONFIG]
4658
+ });
4659
+ await this.createDefaultIndexes();
4660
+ await this.createCustomIndexes();
4661
+ }
4662
+ static getDefaultIndexDefs(schemaPrefix) {
4663
+ return [
4664
+ {
4665
+ name: `${schemaPrefix}idx_channel_installations_webhook`,
4666
+ table: TABLE_CHANNEL_INSTALLATIONS,
4667
+ columns: ["webhookId"],
4668
+ unique: true
4669
+ },
4670
+ {
4671
+ name: `${schemaPrefix}idx_channel_installations_platform_agent`,
4672
+ table: TABLE_CHANNEL_INSTALLATIONS,
4673
+ columns: ["platform", "agentId"]
4674
+ }
4675
+ ];
4676
+ }
4677
+ static getExportDDL(schemaName) {
4678
+ const statements = [];
4679
+ const parsedSchema = schemaName ? parseSqlIdentifier(schemaName, "schema name") : "";
4680
+ const schemaPrefix = parsedSchema && parsedSchema !== "public" ? `${parsedSchema}_` : "";
4681
+ for (const tableName of _ChannelsPG.MANAGED_TABLES) {
4682
+ statements.push(
4683
+ generateTableSQL({
4684
+ tableName,
4685
+ schema: TABLE_SCHEMAS[tableName],
4686
+ schemaName,
4687
+ includeAllConstraints: true
4688
+ })
4689
+ );
4690
+ }
4691
+ for (const idx of _ChannelsPG.getDefaultIndexDefs(schemaPrefix)) {
4692
+ statements.push(generateIndexSQL(idx, schemaName));
4693
+ }
4694
+ return statements;
4695
+ }
4696
+ getDefaultIndexDefinitions() {
4697
+ const schemaPrefix = this.#schema !== "public" ? `${this.#schema}_` : "";
4698
+ return _ChannelsPG.getDefaultIndexDefs(schemaPrefix);
4699
+ }
4700
+ async createDefaultIndexes() {
4701
+ if (this.#skipDefaultIndexes) return;
4702
+ for (const indexDef of this.getDefaultIndexDefinitions()) {
4703
+ try {
4704
+ await this.#db.createIndex(indexDef);
4705
+ } catch (error) {
4706
+ this.logger?.warn?.(`Failed to create index ${indexDef.name}:`, error);
4707
+ }
4708
+ }
4709
+ }
4710
+ async createCustomIndexes() {
4711
+ if (!this.#indexes || this.#indexes.length === 0) return;
4712
+ for (const indexDef of this.#indexes) {
4713
+ try {
4714
+ await this.#db.createIndex(indexDef);
4715
+ } catch (error) {
4716
+ this.logger?.warn?.(`Failed to create custom index ${indexDef.name}:`, error);
4717
+ }
4718
+ }
4719
+ }
4720
+ async dangerouslyClearAll() {
4721
+ await this.#db.clearTable({ tableName: TABLE_CHANNEL_INSTALLATIONS });
4722
+ await this.#db.clearTable({ tableName: TABLE_CHANNEL_CONFIG });
4723
+ }
4724
+ async saveInstallation(installation) {
4725
+ const schemaName = getSchemaName2(this.#schema);
4726
+ const tableName = getTableName2({ indexName: TABLE_CHANNEL_INSTALLATIONS, schemaName });
4727
+ const now = (/* @__PURE__ */ new Date()).toISOString();
4728
+ await this.#db.client.none(
4729
+ `INSERT INTO ${tableName} ("id", "platform", "agentId", "status", "webhookId", "data", "configHash", "error", "createdAt", "updatedAt")
4730
+ VALUES ($1, $2, $3, $4, $5, $6::jsonb, $7, $8, $9, $10)
4731
+ ON CONFLICT ("id") DO UPDATE SET
4732
+ "platform" = EXCLUDED."platform",
4733
+ "agentId" = EXCLUDED."agentId",
4734
+ "status" = EXCLUDED."status",
4735
+ "webhookId" = EXCLUDED."webhookId",
4736
+ "data" = EXCLUDED."data",
4737
+ "configHash" = EXCLUDED."configHash",
4738
+ "error" = EXCLUDED."error",
4739
+ "updatedAt" = EXCLUDED."updatedAt"`,
4740
+ [
4741
+ installation.id,
4742
+ installation.platform,
4743
+ installation.agentId,
4744
+ installation.status,
4745
+ installation.webhookId ?? null,
4746
+ JSON.stringify(installation.data),
4747
+ installation.configHash ?? null,
4748
+ installation.error ?? null,
4749
+ installation.createdAt?.toISOString() ?? now,
4750
+ now
4751
+ ]
4752
+ );
4753
+ }
4754
+ async getInstallation(id) {
4755
+ const schemaName = getSchemaName2(this.#schema);
4756
+ const tableName = getTableName2({ indexName: TABLE_CHANNEL_INSTALLATIONS, schemaName });
4757
+ const row = await this.#db.client.oneOrNone(`SELECT * FROM ${tableName} WHERE "id" = $1`, [id]);
4758
+ return row ? this.#parseInstallationRow(row) : null;
4759
+ }
4760
+ async getInstallationByAgent(platform, agentId) {
4761
+ const schemaName = getSchemaName2(this.#schema);
4762
+ const tableName = getTableName2({ indexName: TABLE_CHANNEL_INSTALLATIONS, schemaName });
4763
+ const row = await this.#db.client.oneOrNone(
4764
+ `SELECT * FROM ${tableName} WHERE "platform" = $1 AND "agentId" = $2 ORDER BY CASE "status" WHEN 'active' THEN 0 WHEN 'pending' THEN 1 ELSE 2 END, "updatedAt" DESC LIMIT 1`,
4765
+ [platform, agentId]
4766
+ );
4767
+ return row ? this.#parseInstallationRow(row) : null;
4768
+ }
4769
+ async getInstallationByWebhookId(webhookId) {
4770
+ const schemaName = getSchemaName2(this.#schema);
4771
+ const tableName = getTableName2({ indexName: TABLE_CHANNEL_INSTALLATIONS, schemaName });
4772
+ const row = await this.#db.client.oneOrNone(`SELECT * FROM ${tableName} WHERE "webhookId" = $1`, [webhookId]);
4773
+ return row ? this.#parseInstallationRow(row) : null;
4774
+ }
4775
+ async listInstallations(platform) {
4776
+ const schemaName = getSchemaName2(this.#schema);
4777
+ const tableName = getTableName2({ indexName: TABLE_CHANNEL_INSTALLATIONS, schemaName });
4778
+ const rows = await this.#db.client.manyOrNone(
4779
+ `SELECT * FROM ${tableName} WHERE "platform" = $1 ORDER BY "createdAt" DESC`,
4780
+ [platform]
4781
+ );
4782
+ return rows.map((row) => this.#parseInstallationRow(row));
4783
+ }
4784
+ async deleteInstallation(id) {
4785
+ const schemaName = getSchemaName2(this.#schema);
4786
+ const tableName = getTableName2({ indexName: TABLE_CHANNEL_INSTALLATIONS, schemaName });
4787
+ await this.#db.client.none(`DELETE FROM ${tableName} WHERE "id" = $1`, [id]);
4788
+ }
4789
+ async saveConfig(config) {
4790
+ const schemaName = getSchemaName2(this.#schema);
4791
+ const tableName = getTableName2({ indexName: TABLE_CHANNEL_CONFIG, schemaName });
4792
+ await this.#db.client.none(
4793
+ `INSERT INTO ${tableName} ("platform", "data", "updatedAt")
4794
+ VALUES ($1, $2::jsonb, $3)
4795
+ ON CONFLICT ("platform") DO UPDATE SET
4796
+ "data" = EXCLUDED."data",
4797
+ "updatedAt" = EXCLUDED."updatedAt"`,
4798
+ [config.platform, JSON.stringify(config.data), config.updatedAt.toISOString()]
4799
+ );
4800
+ }
4801
+ async getConfig(platform) {
4802
+ const schemaName = getSchemaName2(this.#schema);
4803
+ const tableName = getTableName2({ indexName: TABLE_CHANNEL_CONFIG, schemaName });
4804
+ const row = await this.#db.client.oneOrNone(`SELECT * FROM ${tableName} WHERE "platform" = $1`, [platform]);
4805
+ if (!row) return null;
4806
+ return {
4807
+ platform: row.platform,
4808
+ data: typeof row.data === "string" ? JSON.parse(row.data) : row.data,
4809
+ updatedAt: new Date(row.updatedAt)
4810
+ };
4811
+ }
4812
+ async deleteConfig(platform) {
4813
+ const schemaName = getSchemaName2(this.#schema);
4814
+ const tableName = getTableName2({ indexName: TABLE_CHANNEL_CONFIG, schemaName });
4815
+ await this.#db.client.none(`DELETE FROM ${tableName} WHERE "platform" = $1`, [platform]);
4816
+ }
4817
+ #parseInstallationRow(row) {
4818
+ return {
4819
+ id: row.id,
4820
+ platform: row.platform,
4821
+ agentId: row.agentId,
4822
+ status: row.status,
4823
+ webhookId: row.webhookId || void 0,
4824
+ data: typeof row.data === "string" ? JSON.parse(row.data) : row.data,
4825
+ configHash: row.configHash || void 0,
4826
+ error: row.error || void 0,
4827
+ createdAt: row.createdAt instanceof Date ? row.createdAt : new Date(row.createdAt),
4828
+ updatedAt: row.updatedAt instanceof Date ? row.updatedAt : new Date(row.updatedAt)
4829
+ };
4830
+ }
4831
+ };
4636
4832
  function jsonbArg(value) {
4637
4833
  return value === void 0 || value === null ? null : JSON.stringify(value);
4638
4834
  }
@@ -14009,7 +14205,8 @@ var ALL_DOMAINS = [
14009
14205
  WorkflowsPG,
14010
14206
  DatasetsPG,
14011
14207
  ExperimentsPG,
14012
- BackgroundTasksPG
14208
+ BackgroundTasksPG,
14209
+ ChannelsPG
14013
14210
  ];
14014
14211
  function exportSchemas(schemaName) {
14015
14212
  const statements = [];
@@ -14064,7 +14261,8 @@ var PostgresStore = class extends MastraCompositeStore {
14064
14261
  blobs: new BlobsPG(domainConfig),
14065
14262
  datasets: new DatasetsPG(domainConfig),
14066
14263
  experiments: new ExperimentsPG(domainConfig),
14067
- backgroundTasks: new BackgroundTasksPG(domainConfig)
14264
+ backgroundTasks: new BackgroundTasksPG(domainConfig),
14265
+ channels: new ChannelsPG(domainConfig)
14068
14266
  };
14069
14267
  } catch (e) {
14070
14268
  throw new MastraError(
@@ -14253,6 +14451,6 @@ Example Complex Query:
14253
14451
  ]
14254
14452
  }`;
14255
14453
 
14256
- export { AgentsPG, BackgroundTasksPG, BlobsPG, DatasetsPG, ExperimentsPG, MCPClientsPG, MCPServersPG, MemoryPG, ObservabilityPG, PGVECTOR_PROMPT, PgVector, PoolAdapter, PostgresStore, PromptBlocksPG, ScorerDefinitionsPG, ScoresPG, SkillsPG, WorkflowsPG, WorkspacesPG, exportSchemas };
14454
+ export { AgentsPG, BackgroundTasksPG, BlobsPG, ChannelsPG, DatasetsPG, ExperimentsPG, MCPClientsPG, MCPServersPG, MemoryPG, ObservabilityPG, PGVECTOR_PROMPT, PgVector, PoolAdapter, PostgresStore, PromptBlocksPG, ScorerDefinitionsPG, ScoresPG, SkillsPG, WorkflowsPG, WorkspacesPG, exportSchemas };
14257
14455
  //# sourceMappingURL=index.js.map
14258
14456
  //# sourceMappingURL=index.js.map