@axiom-lattice/pg-stores 1.0.85 → 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.mjs CHANGED
@@ -1,8 +1,8 @@
1
1
  // src/index.ts
2
- import { Pool as Pool24 } from "pg";
2
+ import { Pool as Pool26 } from "pg";
3
3
 
4
4
  // src/createPgStoreConfig.ts
5
- import { Pool as Pool21 } from "pg";
5
+ import { Pool as Pool23 } from "pg";
6
6
 
7
7
  // src/migrations/migration.ts
8
8
  var MigrationManager = class {
@@ -1775,7 +1775,7 @@ var PostgreSQLMcpServerConfigStore = class {
1775
1775
  data.name || null,
1776
1776
  data.description || null,
1777
1777
  JSON.stringify(configWithEncryptedEnv),
1778
- data.selectedTools || [],
1778
+ JSON.stringify(data.selectedTools || []),
1779
1779
  isEnvEncrypted,
1780
1780
  "disconnected",
1781
1781
  nowString,
@@ -1821,7 +1821,7 @@ var PostgreSQLMcpServerConfigStore = class {
1821
1821
  updateData.is_env_encrypted = isEnvEncrypted;
1822
1822
  }
1823
1823
  if (updates.selectedTools !== void 0) {
1824
- updateData.selected_tools = updates.selectedTools;
1824
+ updateData.selected_tools = JSON.stringify(updates.selectedTools);
1825
1825
  }
1826
1826
  if (updates.status !== void 0) {
1827
1827
  updateData.status = updates.status;
@@ -6889,9 +6889,218 @@ var PostgresSharedResourceStore = class {
6889
6889
  }
6890
6890
  };
6891
6891
 
6892
+ // src/stores/PostgreSQLCollectionStore.ts
6893
+ import { Pool as Pool21 } from "pg";
6894
+ var PostgreSQLCollectionStore = class {
6895
+ constructor(options) {
6896
+ this.initialized = false;
6897
+ this.ownsPool = true;
6898
+ this.initPromise = null;
6899
+ if (options.pool) {
6900
+ this.pool = options.pool;
6901
+ this.ownsPool = false;
6902
+ this.initialized = true;
6903
+ return;
6904
+ }
6905
+ if (typeof options.poolConfig === "string") {
6906
+ this.pool = new Pool21({ connectionString: options.poolConfig });
6907
+ } else if (options.poolConfig) {
6908
+ this.pool = new Pool21(options.poolConfig);
6909
+ } else {
6910
+ throw new Error("Either pool or poolConfig must be provided");
6911
+ }
6912
+ if (options.autoMigrate !== false) {
6913
+ this.initialize().catch((error) => {
6914
+ console.error("Failed to initialize PostgreSQLCollectionStore:", error);
6915
+ });
6916
+ }
6917
+ }
6918
+ async initialize() {
6919
+ if (this.initialized) return;
6920
+ if (this.initPromise) return this.initPromise;
6921
+ this.initPromise = (async () => {
6922
+ this.initialized = true;
6923
+ })();
6924
+ return this.initPromise;
6925
+ }
6926
+ ensureInit() {
6927
+ if (!this.initialized) {
6928
+ throw new Error("PostgreSQLCollectionStore not initialized");
6929
+ }
6930
+ }
6931
+ async getAllCollections(tenantId) {
6932
+ this.ensureInit();
6933
+ const result = await this.pool.query(
6934
+ `SELECT id, name, tenant_id, label, embedding_key, schema, created_at, updated_at
6935
+ FROM lattice_collections WHERE tenant_id = $1 ORDER BY name`,
6936
+ [tenantId]
6937
+ );
6938
+ return result.rows.map(this.rowToCollection);
6939
+ }
6940
+ async getCollectionByName(tenantId, name) {
6941
+ this.ensureInit();
6942
+ const result = await this.pool.query(
6943
+ `SELECT id, name, tenant_id, label, embedding_key, schema, created_at, updated_at
6944
+ FROM lattice_collections WHERE tenant_id = $1 AND name = $2`,
6945
+ [tenantId, name]
6946
+ );
6947
+ return result.rows.length > 0 ? this.rowToCollection(result.rows[0]) : null;
6948
+ }
6949
+ async createCollection(tenantId, data) {
6950
+ this.ensureInit();
6951
+ const schema = JSON.stringify(data.schema || { fields: [] });
6952
+ const result = await this.pool.query(
6953
+ `INSERT INTO lattice_collections (name, tenant_id, label, embedding_key, schema)
6954
+ VALUES ($1, $2, $3, $4, $5)
6955
+ ON CONFLICT (tenant_id, name) DO NOTHING
6956
+ RETURNING id, name, tenant_id, label, embedding_key, schema, created_at, updated_at`,
6957
+ [data.name, tenantId, data.label, data.embeddingKey, schema]
6958
+ );
6959
+ if (result.rows.length === 0) {
6960
+ throw new Error(`Collection "${data.name}" already exists`);
6961
+ }
6962
+ return this.rowToCollection(result.rows[0]);
6963
+ }
6964
+ async updateCollection(tenantId, name, updates) {
6965
+ this.ensureInit();
6966
+ const setClauses = [];
6967
+ const values = [];
6968
+ let paramIndex = 1;
6969
+ if (updates.label !== void 0) {
6970
+ setClauses.push(`label = $${paramIndex++}`);
6971
+ values.push(updates.label);
6972
+ }
6973
+ if (updates.embeddingKey !== void 0) {
6974
+ setClauses.push(`embedding_key = $${paramIndex++}`);
6975
+ values.push(updates.embeddingKey);
6976
+ }
6977
+ if (updates.schema !== void 0) {
6978
+ setClauses.push(`schema = $${paramIndex++}`);
6979
+ values.push(JSON.stringify(updates.schema));
6980
+ }
6981
+ if (setClauses.length === 0) return this.getCollectionByName(tenantId, name);
6982
+ setClauses.push(`updated_at = NOW()`);
6983
+ values.push(tenantId, name);
6984
+ const result = await this.pool.query(
6985
+ `UPDATE lattice_collections SET ${setClauses.join(", ")}
6986
+ WHERE tenant_id = $${paramIndex++} AND name = $${paramIndex}
6987
+ RETURNING id, name, tenant_id, label, embedding_key, schema, created_at, updated_at`,
6988
+ values
6989
+ );
6990
+ return result.rows.length > 0 ? this.rowToCollection(result.rows[0]) : null;
6991
+ }
6992
+ async deleteCollection(tenantId, name) {
6993
+ this.ensureInit();
6994
+ const result = await this.pool.query(
6995
+ `DELETE FROM lattice_collections WHERE tenant_id = $1 AND name = $2`,
6996
+ [tenantId, name]
6997
+ );
6998
+ return (result.rowCount ?? 0) > 0;
6999
+ }
7000
+ async dispose() {
7001
+ if (this.ownsPool && this.pool) {
7002
+ await this.pool.end();
7003
+ }
7004
+ }
7005
+ rowToCollection(row) {
7006
+ return {
7007
+ id: row.id,
7008
+ name: row.name,
7009
+ tenantId: row.tenant_id,
7010
+ label: row.label,
7011
+ embeddingKey: row.embedding_key,
7012
+ schema: typeof row.schema === "string" ? JSON.parse(row.schema) : row.schema,
7013
+ createdAt: row.created_at,
7014
+ updatedAt: row.updated_at
7015
+ };
7016
+ }
7017
+ };
7018
+
6892
7019
  // src/createPgStoreConfig.ts
6893
7020
  import { PostgresSaver } from "@langchain/langgraph-checkpoint-postgres";
6894
7021
 
7022
+ // src/PGVectorStoreProvider.ts
7023
+ import { Pool as Pool22 } from "pg";
7024
+ import { PGVectorStore } from "@langchain/community/vectorstores/pgvector";
7025
+ import { Document } from "@langchain/core/documents";
7026
+ import { embeddingsLatticeManager } from "@axiom-lattice/core";
7027
+ function safeTableName(name) {
7028
+ if (!/^[a-z][a-z0-9_-]*$/.test(name)) {
7029
+ throw new Error(`Invalid table name: ${name}`);
7030
+ }
7031
+ return name;
7032
+ }
7033
+ var PGVectorStoreProvider = class {
7034
+ constructor(pool, connectionString) {
7035
+ this.pool = pool;
7036
+ this.connectionString = connectionString;
7037
+ }
7038
+ async create(params) {
7039
+ const { tableName, embeddingKey } = params;
7040
+ let embeddings;
7041
+ try {
7042
+ embeddings = embeddingsLatticeManager.getEmbeddingsClient(embeddingKey);
7043
+ } catch {
7044
+ try {
7045
+ embeddings = embeddingsLatticeManager.getEmbeddingsClient("default");
7046
+ } catch {
7047
+ throw new Error(
7048
+ `No embedding model registered. Register one first:
7049
+ import { registerEmbeddingsLattice } from "@axiom-lattice/core";
7050
+ registerEmbeddingsLattice("default", new SomeEmbeddings(...));`
7051
+ );
7052
+ }
7053
+ }
7054
+ return PGVectorStore.initialize(embeddings, {
7055
+ postgresConnectionOptions: { connectionString: this.connectionString },
7056
+ tableName: safeTableName(tableName),
7057
+ columns: {
7058
+ idColumnName: "id",
7059
+ vectorColumnName: "vector",
7060
+ contentColumnName: "content",
7061
+ metadataColumnName: "metadata"
7062
+ },
7063
+ distanceStrategy: "cosine"
7064
+ });
7065
+ }
7066
+ async getEntryCount(tableName) {
7067
+ const name = safeTableName(tableName);
7068
+ try {
7069
+ const result = await this.pool.query(`SELECT COUNT(*) as count FROM "${name}"`);
7070
+ return parseInt(result.rows[0]?.count || "0", 10);
7071
+ } catch {
7072
+ return 0;
7073
+ }
7074
+ }
7075
+ async listEntries(tableName) {
7076
+ const name = safeTableName(tableName);
7077
+ try {
7078
+ const result = await this.pool.query(
7079
+ `SELECT content, metadata FROM "${name}" ORDER BY metadata->>'_created_at' DESC`
7080
+ );
7081
+ return result.rows.map((row) => new Document({
7082
+ pageContent: row.content || "",
7083
+ metadata: typeof row.metadata === "string" ? JSON.parse(row.metadata) : row.metadata || {}
7084
+ }));
7085
+ } catch {
7086
+ return [];
7087
+ }
7088
+ }
7089
+ async dropTable(tableName) {
7090
+ const name = safeTableName(tableName);
7091
+ await this.pool.query(`DROP TABLE IF EXISTS "${name}"`);
7092
+ }
7093
+ async dispose(_name) {
7094
+ }
7095
+ };
7096
+ function createPGVectorStoreProvider(connectionString) {
7097
+ const pool = new Pool22({ connectionString });
7098
+ return {
7099
+ provider: new PGVectorStoreProvider(pool, connectionString),
7100
+ pool
7101
+ };
7102
+ }
7103
+
6895
7104
  // src/migrations/skill_migrations.ts
6896
7105
  var createSkillsTable = {
6897
7106
  version: 4,
@@ -7100,9 +7309,43 @@ var createChannelIdentityMappingTables = {
7100
7309
  }
7101
7310
  };
7102
7311
 
7312
+ // src/migrations/collection_migrations.ts
7313
+ var createCollectionsTable = {
7314
+ version: 137,
7315
+ name: "create_collections_table",
7316
+ up: async (client) => {
7317
+ await client.query(`
7318
+ CREATE TABLE IF NOT EXISTS lattice_collections (
7319
+ id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
7320
+ name VARCHAR(255) NOT NULL,
7321
+ tenant_id VARCHAR(255) NOT NULL DEFAULT 'default',
7322
+ label VARCHAR(255) NOT NULL,
7323
+ embedding_key VARCHAR(255) NOT NULL DEFAULT 'default',
7324
+ schema JSONB DEFAULT '{"fields":[]}',
7325
+ created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
7326
+ updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
7327
+ UNIQUE(tenant_id, name)
7328
+ )
7329
+ `);
7330
+ await client.query(`
7331
+ CREATE INDEX IF NOT EXISTS idx_lattice_collections_tenant
7332
+ ON lattice_collections(tenant_id)
7333
+ `);
7334
+ await client.query(`
7335
+ CREATE INDEX IF NOT EXISTS idx_lattice_collections_name
7336
+ ON lattice_collections(name)
7337
+ `);
7338
+ },
7339
+ down: async (client) => {
7340
+ await client.query("DROP INDEX IF EXISTS idx_lattice_collections_name");
7341
+ await client.query("DROP INDEX IF EXISTS idx_lattice_collections_tenant");
7342
+ await client.query("DROP TABLE IF EXISTS lattice_collections");
7343
+ }
7344
+ };
7345
+
7103
7346
  // src/createPgStoreConfig.ts
7104
7347
  async function createPgStoreConfig(connectionString) {
7105
- const pool = new Pool21({ connectionString });
7348
+ const pool = new Pool23({ connectionString });
7106
7349
  const mm = new MigrationManager(pool);
7107
7350
  mm.register(createThreadsTable);
7108
7351
  mm.register(createScheduledTasksTable);
@@ -7146,6 +7389,7 @@ async function createPgStoreConfig(connectionString) {
7146
7389
  mm.register(createMenuItemsTable);
7147
7390
  mm.register(createSharedResourcesTable);
7148
7391
  mm.register(addFileContentType);
7392
+ mm.register(createCollectionsTable);
7149
7393
  await mm.migrate();
7150
7394
  const checkpoint = PostgresSaver.fromConnString(connectionString);
7151
7395
  checkpoint.setup().catch((err) => {
@@ -7173,12 +7417,14 @@ async function createPgStoreConfig(connectionString) {
7173
7417
  schedule: new PostgreSQLScheduleStorage(opts),
7174
7418
  menu: new MenuStore(opts),
7175
7419
  sharedResource: new PostgresSharedResourceStore(opts),
7420
+ collection: new PostgreSQLCollectionStore(opts),
7421
+ vectorStoreProvider: new PGVectorStoreProvider(pool, connectionString),
7176
7422
  checkpoint
7177
7423
  };
7178
7424
  }
7179
7425
 
7180
7426
  // src/stores/PostgreSQLSkillStore.ts
7181
- import { Pool as Pool22 } from "pg";
7427
+ import { Pool as Pool24 } from "pg";
7182
7428
  var PostgreSQLSkillStore = class {
7183
7429
  constructor(options) {
7184
7430
  this.initialized = false;
@@ -7191,9 +7437,9 @@ var PostgreSQLSkillStore = class {
7191
7437
  return;
7192
7438
  }
7193
7439
  if (typeof options.poolConfig === "string") {
7194
- this.pool = new Pool22({ connectionString: options.poolConfig });
7440
+ this.pool = new Pool24({ connectionString: options.poolConfig });
7195
7441
  } else if (options.poolConfig) {
7196
- this.pool = new Pool22(options.poolConfig);
7442
+ this.pool = new Pool24(options.poolConfig);
7197
7443
  } else {
7198
7444
  throw new Error("Either pool or poolConfig must be provided");
7199
7445
  }
@@ -7494,7 +7740,7 @@ var PostgreSQLSkillStore = class {
7494
7740
  };
7495
7741
 
7496
7742
  // src/stores/ChannelIdentityMappingStore.ts
7497
- import { Pool as Pool23 } from "pg";
7743
+ import { Pool as Pool25 } from "pg";
7498
7744
  var ChannelIdentityMappingStore = class {
7499
7745
  constructor(options) {
7500
7746
  this.initialized = false;
@@ -7506,7 +7752,7 @@ var ChannelIdentityMappingStore = class {
7506
7752
  this.initialized = true;
7507
7753
  return;
7508
7754
  }
7509
- this.pool = typeof options.poolConfig === "string" ? new Pool23({ connectionString: options.poolConfig }) : options.poolConfig ? new Pool23(options.poolConfig) : (() => {
7755
+ this.pool = typeof options.poolConfig === "string" ? new Pool25({ connectionString: options.poolConfig }) : options.poolConfig ? new Pool25(options.poolConfig) : (() => {
7510
7756
  throw new Error("Either pool or poolConfig must be provided");
7511
7757
  })();
7512
7758
  this.migrationManager = new MigrationManager(this.pool);
@@ -7729,10 +7975,12 @@ export {
7729
7975
  ChannelIdentityMappingStore,
7730
7976
  MenuStore,
7731
7977
  MigrationManager,
7732
- Pool24 as Pool,
7978
+ PGVectorStoreProvider,
7979
+ Pool26 as Pool,
7733
7980
  PostgreSQLA2AApiKeyStore,
7734
7981
  PostgreSQLAssistantStore,
7735
7982
  PostgreSQLChannelInstallationStore,
7983
+ PostgreSQLCollectionStore,
7736
7984
  PostgreSQLDatabaseConfigStore,
7737
7985
  PostgreSQLEvalStore,
7738
7986
  PostgreSQLMcpServerConfigStore,
@@ -7764,6 +8012,7 @@ export {
7764
8012
  createChannelBindingsTable,
7765
8013
  createChannelIdentityMappingTables,
7766
8014
  createChannelInstallationsTable,
8015
+ createCollectionsTable,
7767
8016
  createDatabaseConfigsTable,
7768
8017
  createEvalCasesTable,
7769
8018
  createEvalProjectsTable,
@@ -7773,6 +8022,7 @@ export {
7773
8022
  createMcpServerConfigsTable,
7774
8023
  createMenuItemsTable,
7775
8024
  createMetricsConfigsTable,
8025
+ createPGVectorStoreProvider,
7776
8026
  createPgStoreConfig,
7777
8027
  createProjectsTable,
7778
8028
  createScheduledTasksTable,