@axiom-lattice/pg-stores 1.0.84 → 1.0.86

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
@@ -34,10 +34,12 @@ __export(index_exports, {
34
34
  ChannelIdentityMappingStore: () => ChannelIdentityMappingStore,
35
35
  MenuStore: () => MenuStore,
36
36
  MigrationManager: () => MigrationManager,
37
- Pool: () => import_pg24.Pool,
37
+ PGVectorStoreProvider: () => PGVectorStoreProvider,
38
+ Pool: () => import_pg26.Pool,
38
39
  PostgreSQLA2AApiKeyStore: () => PostgreSQLA2AApiKeyStore,
39
40
  PostgreSQLAssistantStore: () => PostgreSQLAssistantStore,
40
41
  PostgreSQLChannelInstallationStore: () => PostgreSQLChannelInstallationStore,
42
+ PostgreSQLCollectionStore: () => PostgreSQLCollectionStore,
41
43
  PostgreSQLDatabaseConfigStore: () => PostgreSQLDatabaseConfigStore,
42
44
  PostgreSQLEvalStore: () => PostgreSQLEvalStore,
43
45
  PostgreSQLMcpServerConfigStore: () => PostgreSQLMcpServerConfigStore,
@@ -69,6 +71,7 @@ __export(index_exports, {
69
71
  createChannelBindingsTable: () => createChannelBindingsTable,
70
72
  createChannelIdentityMappingTables: () => createChannelIdentityMappingTables,
71
73
  createChannelInstallationsTable: () => createChannelInstallationsTable,
74
+ createCollectionsTable: () => createCollectionsTable,
72
75
  createDatabaseConfigsTable: () => createDatabaseConfigsTable,
73
76
  createEvalCasesTable: () => createEvalCasesTable,
74
77
  createEvalProjectsTable: () => createEvalProjectsTable,
@@ -78,6 +81,7 @@ __export(index_exports, {
78
81
  createMcpServerConfigsTable: () => createMcpServerConfigsTable,
79
82
  createMenuItemsTable: () => createMenuItemsTable,
80
83
  createMetricsConfigsTable: () => createMetricsConfigsTable,
84
+ createPGVectorStoreProvider: () => createPGVectorStoreProvider,
81
85
  createPgStoreConfig: () => createPgStoreConfig,
82
86
  createProjectsTable: () => createProjectsTable,
83
87
  createScheduledTasksTable: () => createScheduledTasksTable,
@@ -95,10 +99,10 @@ __export(index_exports, {
95
99
  safeParse: () => safeParse
96
100
  });
97
101
  module.exports = __toCommonJS(index_exports);
98
- var import_pg24 = require("pg");
102
+ var import_pg26 = require("pg");
99
103
 
100
104
  // src/createPgStoreConfig.ts
101
- var import_pg21 = require("pg");
105
+ var import_pg23 = require("pg");
102
106
 
103
107
  // src/migrations/migration.ts
104
108
  var MigrationManager = class {
@@ -153,6 +157,11 @@ var MigrationManager = class {
153
157
  const pkColumn = pkCheck.rows[0]?.column_name;
154
158
  const constraintName = pkCheck.rows[0]?.constraint_name;
155
159
  if (pkColumn === "version" && constraintName) {
160
+ await client.query(`
161
+ DELETE FROM lattice_schema_migrations a
162
+ USING lattice_schema_migrations b
163
+ WHERE a.name = b.name AND a.version < b.version
164
+ `);
156
165
  await client.query(
157
166
  `ALTER TABLE lattice_schema_migrations DROP CONSTRAINT ${constraintName}`
158
167
  );
@@ -1866,7 +1875,7 @@ var PostgreSQLMcpServerConfigStore = class {
1866
1875
  data.name || null,
1867
1876
  data.description || null,
1868
1877
  JSON.stringify(configWithEncryptedEnv),
1869
- data.selectedTools || [],
1878
+ JSON.stringify(data.selectedTools || []),
1870
1879
  isEnvEncrypted,
1871
1880
  "disconnected",
1872
1881
  nowString,
@@ -1912,7 +1921,7 @@ var PostgreSQLMcpServerConfigStore = class {
1912
1921
  updateData.is_env_encrypted = isEnvEncrypted;
1913
1922
  }
1914
1923
  if (updates.selectedTools !== void 0) {
1915
- updateData.selected_tools = updates.selectedTools;
1924
+ updateData.selected_tools = JSON.stringify(updates.selectedTools);
1916
1925
  }
1917
1926
  if (updates.status !== void 0) {
1918
1927
  updateData.status = updates.status;
@@ -6978,9 +6987,218 @@ var PostgresSharedResourceStore = class {
6978
6987
  }
6979
6988
  };
6980
6989
 
6990
+ // src/stores/PostgreSQLCollectionStore.ts
6991
+ var import_pg21 = require("pg");
6992
+ var PostgreSQLCollectionStore = class {
6993
+ constructor(options) {
6994
+ this.initialized = false;
6995
+ this.ownsPool = true;
6996
+ this.initPromise = null;
6997
+ if (options.pool) {
6998
+ this.pool = options.pool;
6999
+ this.ownsPool = false;
7000
+ this.initialized = true;
7001
+ return;
7002
+ }
7003
+ if (typeof options.poolConfig === "string") {
7004
+ this.pool = new import_pg21.Pool({ connectionString: options.poolConfig });
7005
+ } else if (options.poolConfig) {
7006
+ this.pool = new import_pg21.Pool(options.poolConfig);
7007
+ } else {
7008
+ throw new Error("Either pool or poolConfig must be provided");
7009
+ }
7010
+ if (options.autoMigrate !== false) {
7011
+ this.initialize().catch((error) => {
7012
+ console.error("Failed to initialize PostgreSQLCollectionStore:", error);
7013
+ });
7014
+ }
7015
+ }
7016
+ async initialize() {
7017
+ if (this.initialized) return;
7018
+ if (this.initPromise) return this.initPromise;
7019
+ this.initPromise = (async () => {
7020
+ this.initialized = true;
7021
+ })();
7022
+ return this.initPromise;
7023
+ }
7024
+ ensureInit() {
7025
+ if (!this.initialized) {
7026
+ throw new Error("PostgreSQLCollectionStore not initialized");
7027
+ }
7028
+ }
7029
+ async getAllCollections(tenantId) {
7030
+ this.ensureInit();
7031
+ const result = await this.pool.query(
7032
+ `SELECT id, name, tenant_id, label, embedding_key, schema, created_at, updated_at
7033
+ FROM lattice_collections WHERE tenant_id = $1 ORDER BY name`,
7034
+ [tenantId]
7035
+ );
7036
+ return result.rows.map(this.rowToCollection);
7037
+ }
7038
+ async getCollectionByName(tenantId, name) {
7039
+ this.ensureInit();
7040
+ const result = await this.pool.query(
7041
+ `SELECT id, name, tenant_id, label, embedding_key, schema, created_at, updated_at
7042
+ FROM lattice_collections WHERE tenant_id = $1 AND name = $2`,
7043
+ [tenantId, name]
7044
+ );
7045
+ return result.rows.length > 0 ? this.rowToCollection(result.rows[0]) : null;
7046
+ }
7047
+ async createCollection(tenantId, data) {
7048
+ this.ensureInit();
7049
+ const schema = JSON.stringify(data.schema || { fields: [] });
7050
+ const result = await this.pool.query(
7051
+ `INSERT INTO lattice_collections (name, tenant_id, label, embedding_key, schema)
7052
+ VALUES ($1, $2, $3, $4, $5)
7053
+ ON CONFLICT (tenant_id, name) DO NOTHING
7054
+ RETURNING id, name, tenant_id, label, embedding_key, schema, created_at, updated_at`,
7055
+ [data.name, tenantId, data.label, data.embeddingKey, schema]
7056
+ );
7057
+ if (result.rows.length === 0) {
7058
+ throw new Error(`Collection "${data.name}" already exists`);
7059
+ }
7060
+ return this.rowToCollection(result.rows[0]);
7061
+ }
7062
+ async updateCollection(tenantId, name, updates) {
7063
+ this.ensureInit();
7064
+ const setClauses = [];
7065
+ const values = [];
7066
+ let paramIndex = 1;
7067
+ if (updates.label !== void 0) {
7068
+ setClauses.push(`label = $${paramIndex++}`);
7069
+ values.push(updates.label);
7070
+ }
7071
+ if (updates.embeddingKey !== void 0) {
7072
+ setClauses.push(`embedding_key = $${paramIndex++}`);
7073
+ values.push(updates.embeddingKey);
7074
+ }
7075
+ if (updates.schema !== void 0) {
7076
+ setClauses.push(`schema = $${paramIndex++}`);
7077
+ values.push(JSON.stringify(updates.schema));
7078
+ }
7079
+ if (setClauses.length === 0) return this.getCollectionByName(tenantId, name);
7080
+ setClauses.push(`updated_at = NOW()`);
7081
+ values.push(tenantId, name);
7082
+ const result = await this.pool.query(
7083
+ `UPDATE lattice_collections SET ${setClauses.join(", ")}
7084
+ WHERE tenant_id = $${paramIndex++} AND name = $${paramIndex}
7085
+ RETURNING id, name, tenant_id, label, embedding_key, schema, created_at, updated_at`,
7086
+ values
7087
+ );
7088
+ return result.rows.length > 0 ? this.rowToCollection(result.rows[0]) : null;
7089
+ }
7090
+ async deleteCollection(tenantId, name) {
7091
+ this.ensureInit();
7092
+ const result = await this.pool.query(
7093
+ `DELETE FROM lattice_collections WHERE tenant_id = $1 AND name = $2`,
7094
+ [tenantId, name]
7095
+ );
7096
+ return (result.rowCount ?? 0) > 0;
7097
+ }
7098
+ async dispose() {
7099
+ if (this.ownsPool && this.pool) {
7100
+ await this.pool.end();
7101
+ }
7102
+ }
7103
+ rowToCollection(row) {
7104
+ return {
7105
+ id: row.id,
7106
+ name: row.name,
7107
+ tenantId: row.tenant_id,
7108
+ label: row.label,
7109
+ embeddingKey: row.embedding_key,
7110
+ schema: typeof row.schema === "string" ? JSON.parse(row.schema) : row.schema,
7111
+ createdAt: row.created_at,
7112
+ updatedAt: row.updated_at
7113
+ };
7114
+ }
7115
+ };
7116
+
6981
7117
  // src/createPgStoreConfig.ts
6982
7118
  var import_langgraph_checkpoint_postgres = require("@langchain/langgraph-checkpoint-postgres");
6983
7119
 
7120
+ // src/PGVectorStoreProvider.ts
7121
+ var import_pg22 = require("pg");
7122
+ var import_pgvector = require("@langchain/community/vectorstores/pgvector");
7123
+ var import_documents = require("@langchain/core/documents");
7124
+ var import_core6 = require("@axiom-lattice/core");
7125
+ function safeTableName(name) {
7126
+ if (!/^[a-z][a-z0-9_-]*$/.test(name)) {
7127
+ throw new Error(`Invalid table name: ${name}`);
7128
+ }
7129
+ return name;
7130
+ }
7131
+ var PGVectorStoreProvider = class {
7132
+ constructor(pool, connectionString) {
7133
+ this.pool = pool;
7134
+ this.connectionString = connectionString;
7135
+ }
7136
+ async create(params) {
7137
+ const { tableName, embeddingKey } = params;
7138
+ let embeddings;
7139
+ try {
7140
+ embeddings = import_core6.embeddingsLatticeManager.getEmbeddingsClient(embeddingKey);
7141
+ } catch {
7142
+ try {
7143
+ embeddings = import_core6.embeddingsLatticeManager.getEmbeddingsClient("default");
7144
+ } catch {
7145
+ throw new Error(
7146
+ `No embedding model registered. Register one first:
7147
+ import { registerEmbeddingsLattice } from "@axiom-lattice/core";
7148
+ registerEmbeddingsLattice("default", new SomeEmbeddings(...));`
7149
+ );
7150
+ }
7151
+ }
7152
+ return import_pgvector.PGVectorStore.initialize(embeddings, {
7153
+ postgresConnectionOptions: { connectionString: this.connectionString },
7154
+ tableName: safeTableName(tableName),
7155
+ columns: {
7156
+ idColumnName: "id",
7157
+ vectorColumnName: "vector",
7158
+ contentColumnName: "content",
7159
+ metadataColumnName: "metadata"
7160
+ },
7161
+ distanceStrategy: "cosine"
7162
+ });
7163
+ }
7164
+ async getEntryCount(tableName) {
7165
+ const name = safeTableName(tableName);
7166
+ try {
7167
+ const result = await this.pool.query(`SELECT COUNT(*) as count FROM "${name}"`);
7168
+ return parseInt(result.rows[0]?.count || "0", 10);
7169
+ } catch {
7170
+ return 0;
7171
+ }
7172
+ }
7173
+ async listEntries(tableName) {
7174
+ const name = safeTableName(tableName);
7175
+ try {
7176
+ const result = await this.pool.query(
7177
+ `SELECT content, metadata FROM "${name}" ORDER BY metadata->>'_created_at' DESC`
7178
+ );
7179
+ return result.rows.map((row) => new import_documents.Document({
7180
+ pageContent: row.content || "",
7181
+ metadata: typeof row.metadata === "string" ? JSON.parse(row.metadata) : row.metadata || {}
7182
+ }));
7183
+ } catch {
7184
+ return [];
7185
+ }
7186
+ }
7187
+ async dropTable(tableName) {
7188
+ const name = safeTableName(tableName);
7189
+ await this.pool.query(`DROP TABLE IF EXISTS "${name}"`);
7190
+ }
7191
+ async dispose(_name) {
7192
+ }
7193
+ };
7194
+ function createPGVectorStoreProvider(connectionString) {
7195
+ const pool = new import_pg22.Pool({ connectionString });
7196
+ return {
7197
+ provider: new PGVectorStoreProvider(pool, connectionString),
7198
+ pool
7199
+ };
7200
+ }
7201
+
6984
7202
  // src/migrations/skill_migrations.ts
6985
7203
  var createSkillsTable = {
6986
7204
  version: 4,
@@ -7189,9 +7407,43 @@ var createChannelIdentityMappingTables = {
7189
7407
  }
7190
7408
  };
7191
7409
 
7410
+ // src/migrations/collection_migrations.ts
7411
+ var createCollectionsTable = {
7412
+ version: 137,
7413
+ name: "create_collections_table",
7414
+ up: async (client) => {
7415
+ await client.query(`
7416
+ CREATE TABLE IF NOT EXISTS lattice_collections (
7417
+ id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
7418
+ name VARCHAR(255) NOT NULL,
7419
+ tenant_id VARCHAR(255) NOT NULL DEFAULT 'default',
7420
+ label VARCHAR(255) NOT NULL,
7421
+ embedding_key VARCHAR(255) NOT NULL DEFAULT 'default',
7422
+ schema JSONB DEFAULT '{"fields":[]}',
7423
+ created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
7424
+ updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
7425
+ UNIQUE(tenant_id, name)
7426
+ )
7427
+ `);
7428
+ await client.query(`
7429
+ CREATE INDEX IF NOT EXISTS idx_lattice_collections_tenant
7430
+ ON lattice_collections(tenant_id)
7431
+ `);
7432
+ await client.query(`
7433
+ CREATE INDEX IF NOT EXISTS idx_lattice_collections_name
7434
+ ON lattice_collections(name)
7435
+ `);
7436
+ },
7437
+ down: async (client) => {
7438
+ await client.query("DROP INDEX IF EXISTS idx_lattice_collections_name");
7439
+ await client.query("DROP INDEX IF EXISTS idx_lattice_collections_tenant");
7440
+ await client.query("DROP TABLE IF EXISTS lattice_collections");
7441
+ }
7442
+ };
7443
+
7192
7444
  // src/createPgStoreConfig.ts
7193
7445
  async function createPgStoreConfig(connectionString) {
7194
- const pool = new import_pg21.Pool({ connectionString });
7446
+ const pool = new import_pg23.Pool({ connectionString });
7195
7447
  const mm = new MigrationManager(pool);
7196
7448
  mm.register(createThreadsTable);
7197
7449
  mm.register(createScheduledTasksTable);
@@ -7235,6 +7487,7 @@ async function createPgStoreConfig(connectionString) {
7235
7487
  mm.register(createMenuItemsTable);
7236
7488
  mm.register(createSharedResourcesTable);
7237
7489
  mm.register(addFileContentType);
7490
+ mm.register(createCollectionsTable);
7238
7491
  await mm.migrate();
7239
7492
  const checkpoint = import_langgraph_checkpoint_postgres.PostgresSaver.fromConnString(connectionString);
7240
7493
  checkpoint.setup().catch((err) => {
@@ -7262,12 +7515,14 @@ async function createPgStoreConfig(connectionString) {
7262
7515
  schedule: new PostgreSQLScheduleStorage(opts),
7263
7516
  menu: new MenuStore(opts),
7264
7517
  sharedResource: new PostgresSharedResourceStore(opts),
7518
+ collection: new PostgreSQLCollectionStore(opts),
7519
+ vectorStoreProvider: new PGVectorStoreProvider(pool, connectionString),
7265
7520
  checkpoint
7266
7521
  };
7267
7522
  }
7268
7523
 
7269
7524
  // src/stores/PostgreSQLSkillStore.ts
7270
- var import_pg22 = require("pg");
7525
+ var import_pg24 = require("pg");
7271
7526
  var PostgreSQLSkillStore = class {
7272
7527
  constructor(options) {
7273
7528
  this.initialized = false;
@@ -7280,9 +7535,9 @@ var PostgreSQLSkillStore = class {
7280
7535
  return;
7281
7536
  }
7282
7537
  if (typeof options.poolConfig === "string") {
7283
- this.pool = new import_pg22.Pool({ connectionString: options.poolConfig });
7538
+ this.pool = new import_pg24.Pool({ connectionString: options.poolConfig });
7284
7539
  } else if (options.poolConfig) {
7285
- this.pool = new import_pg22.Pool(options.poolConfig);
7540
+ this.pool = new import_pg24.Pool(options.poolConfig);
7286
7541
  } else {
7287
7542
  throw new Error("Either pool or poolConfig must be provided");
7288
7543
  }
@@ -7583,7 +7838,7 @@ var PostgreSQLSkillStore = class {
7583
7838
  };
7584
7839
 
7585
7840
  // src/stores/ChannelIdentityMappingStore.ts
7586
- var import_pg23 = require("pg");
7841
+ var import_pg25 = require("pg");
7587
7842
  var ChannelIdentityMappingStore = class {
7588
7843
  constructor(options) {
7589
7844
  this.initialized = false;
@@ -7595,7 +7850,7 @@ var ChannelIdentityMappingStore = class {
7595
7850
  this.initialized = true;
7596
7851
  return;
7597
7852
  }
7598
- this.pool = typeof options.poolConfig === "string" ? new import_pg23.Pool({ connectionString: options.poolConfig }) : options.poolConfig ? new import_pg23.Pool(options.poolConfig) : (() => {
7853
+ this.pool = typeof options.poolConfig === "string" ? new import_pg25.Pool({ connectionString: options.poolConfig }) : options.poolConfig ? new import_pg25.Pool(options.poolConfig) : (() => {
7599
7854
  throw new Error("Either pool or poolConfig must be provided");
7600
7855
  })();
7601
7856
  this.migrationManager = new MigrationManager(this.pool);
@@ -7819,10 +8074,12 @@ function mapRowToChannelIdentityMapping(row) {
7819
8074
  ChannelIdentityMappingStore,
7820
8075
  MenuStore,
7821
8076
  MigrationManager,
8077
+ PGVectorStoreProvider,
7822
8078
  Pool,
7823
8079
  PostgreSQLA2AApiKeyStore,
7824
8080
  PostgreSQLAssistantStore,
7825
8081
  PostgreSQLChannelInstallationStore,
8082
+ PostgreSQLCollectionStore,
7826
8083
  PostgreSQLDatabaseConfigStore,
7827
8084
  PostgreSQLEvalStore,
7828
8085
  PostgreSQLMcpServerConfigStore,
@@ -7854,6 +8111,7 @@ function mapRowToChannelIdentityMapping(row) {
7854
8111
  createChannelBindingsTable,
7855
8112
  createChannelIdentityMappingTables,
7856
8113
  createChannelInstallationsTable,
8114
+ createCollectionsTable,
7857
8115
  createDatabaseConfigsTable,
7858
8116
  createEvalCasesTable,
7859
8117
  createEvalProjectsTable,
@@ -7863,6 +8121,7 @@ function mapRowToChannelIdentityMapping(row) {
7863
8121
  createMcpServerConfigsTable,
7864
8122
  createMenuItemsTable,
7865
8123
  createMetricsConfigsTable,
8124
+ createPGVectorStoreProvider,
7866
8125
  createPgStoreConfig,
7867
8126
  createProjectsTable,
7868
8127
  createScheduledTasksTable,