@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/.turbo/turbo-build.log +10 -10
- package/CHANGELOG.md +19 -0
- package/dist/index.d.mts +66 -2
- package/dist/index.d.ts +66 -2
- package/dist/index.js +270 -11
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +266 -11
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -3
- package/src/PGVectorStoreProvider.ts +113 -0
- package/src/createPgStoreConfig.ts +6 -0
- package/src/index.ts +4 -0
- package/src/migrations/collection_migrations.ts +41 -0
- package/src/migrations/migration.ts +9 -0
- package/src/stores/PostgreSQLCollectionStore.ts +165 -0
- package/src/stores/PostgreSQLMcpServerConfigStore.ts +2 -2
package/dist/index.mjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
|
-
import { Pool as
|
|
2
|
+
import { Pool as Pool26 } from "pg";
|
|
3
3
|
|
|
4
4
|
// src/createPgStoreConfig.ts
|
|
5
|
-
import { Pool as
|
|
5
|
+
import { Pool as Pool23 } from "pg";
|
|
6
6
|
|
|
7
7
|
// src/migrations/migration.ts
|
|
8
8
|
var MigrationManager = class {
|
|
@@ -57,6 +57,11 @@ var MigrationManager = class {
|
|
|
57
57
|
const pkColumn = pkCheck.rows[0]?.column_name;
|
|
58
58
|
const constraintName = pkCheck.rows[0]?.constraint_name;
|
|
59
59
|
if (pkColumn === "version" && constraintName) {
|
|
60
|
+
await client.query(`
|
|
61
|
+
DELETE FROM lattice_schema_migrations a
|
|
62
|
+
USING lattice_schema_migrations b
|
|
63
|
+
WHERE a.name = b.name AND a.version < b.version
|
|
64
|
+
`);
|
|
60
65
|
await client.query(
|
|
61
66
|
`ALTER TABLE lattice_schema_migrations DROP CONSTRAINT ${constraintName}`
|
|
62
67
|
);
|
|
@@ -1770,7 +1775,7 @@ var PostgreSQLMcpServerConfigStore = class {
|
|
|
1770
1775
|
data.name || null,
|
|
1771
1776
|
data.description || null,
|
|
1772
1777
|
JSON.stringify(configWithEncryptedEnv),
|
|
1773
|
-
data.selectedTools || [],
|
|
1778
|
+
JSON.stringify(data.selectedTools || []),
|
|
1774
1779
|
isEnvEncrypted,
|
|
1775
1780
|
"disconnected",
|
|
1776
1781
|
nowString,
|
|
@@ -1816,7 +1821,7 @@ var PostgreSQLMcpServerConfigStore = class {
|
|
|
1816
1821
|
updateData.is_env_encrypted = isEnvEncrypted;
|
|
1817
1822
|
}
|
|
1818
1823
|
if (updates.selectedTools !== void 0) {
|
|
1819
|
-
updateData.selected_tools = updates.selectedTools;
|
|
1824
|
+
updateData.selected_tools = JSON.stringify(updates.selectedTools);
|
|
1820
1825
|
}
|
|
1821
1826
|
if (updates.status !== void 0) {
|
|
1822
1827
|
updateData.status = updates.status;
|
|
@@ -6884,9 +6889,218 @@ var PostgresSharedResourceStore = class {
|
|
|
6884
6889
|
}
|
|
6885
6890
|
};
|
|
6886
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
|
+
|
|
6887
7019
|
// src/createPgStoreConfig.ts
|
|
6888
7020
|
import { PostgresSaver } from "@langchain/langgraph-checkpoint-postgres";
|
|
6889
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
|
+
|
|
6890
7104
|
// src/migrations/skill_migrations.ts
|
|
6891
7105
|
var createSkillsTable = {
|
|
6892
7106
|
version: 4,
|
|
@@ -7095,9 +7309,43 @@ var createChannelIdentityMappingTables = {
|
|
|
7095
7309
|
}
|
|
7096
7310
|
};
|
|
7097
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
|
+
|
|
7098
7346
|
// src/createPgStoreConfig.ts
|
|
7099
7347
|
async function createPgStoreConfig(connectionString) {
|
|
7100
|
-
const pool = new
|
|
7348
|
+
const pool = new Pool23({ connectionString });
|
|
7101
7349
|
const mm = new MigrationManager(pool);
|
|
7102
7350
|
mm.register(createThreadsTable);
|
|
7103
7351
|
mm.register(createScheduledTasksTable);
|
|
@@ -7141,6 +7389,7 @@ async function createPgStoreConfig(connectionString) {
|
|
|
7141
7389
|
mm.register(createMenuItemsTable);
|
|
7142
7390
|
mm.register(createSharedResourcesTable);
|
|
7143
7391
|
mm.register(addFileContentType);
|
|
7392
|
+
mm.register(createCollectionsTable);
|
|
7144
7393
|
await mm.migrate();
|
|
7145
7394
|
const checkpoint = PostgresSaver.fromConnString(connectionString);
|
|
7146
7395
|
checkpoint.setup().catch((err) => {
|
|
@@ -7168,12 +7417,14 @@ async function createPgStoreConfig(connectionString) {
|
|
|
7168
7417
|
schedule: new PostgreSQLScheduleStorage(opts),
|
|
7169
7418
|
menu: new MenuStore(opts),
|
|
7170
7419
|
sharedResource: new PostgresSharedResourceStore(opts),
|
|
7420
|
+
collection: new PostgreSQLCollectionStore(opts),
|
|
7421
|
+
vectorStoreProvider: new PGVectorStoreProvider(pool, connectionString),
|
|
7171
7422
|
checkpoint
|
|
7172
7423
|
};
|
|
7173
7424
|
}
|
|
7174
7425
|
|
|
7175
7426
|
// src/stores/PostgreSQLSkillStore.ts
|
|
7176
|
-
import { Pool as
|
|
7427
|
+
import { Pool as Pool24 } from "pg";
|
|
7177
7428
|
var PostgreSQLSkillStore = class {
|
|
7178
7429
|
constructor(options) {
|
|
7179
7430
|
this.initialized = false;
|
|
@@ -7186,9 +7437,9 @@ var PostgreSQLSkillStore = class {
|
|
|
7186
7437
|
return;
|
|
7187
7438
|
}
|
|
7188
7439
|
if (typeof options.poolConfig === "string") {
|
|
7189
|
-
this.pool = new
|
|
7440
|
+
this.pool = new Pool24({ connectionString: options.poolConfig });
|
|
7190
7441
|
} else if (options.poolConfig) {
|
|
7191
|
-
this.pool = new
|
|
7442
|
+
this.pool = new Pool24(options.poolConfig);
|
|
7192
7443
|
} else {
|
|
7193
7444
|
throw new Error("Either pool or poolConfig must be provided");
|
|
7194
7445
|
}
|
|
@@ -7489,7 +7740,7 @@ var PostgreSQLSkillStore = class {
|
|
|
7489
7740
|
};
|
|
7490
7741
|
|
|
7491
7742
|
// src/stores/ChannelIdentityMappingStore.ts
|
|
7492
|
-
import { Pool as
|
|
7743
|
+
import { Pool as Pool25 } from "pg";
|
|
7493
7744
|
var ChannelIdentityMappingStore = class {
|
|
7494
7745
|
constructor(options) {
|
|
7495
7746
|
this.initialized = false;
|
|
@@ -7501,7 +7752,7 @@ var ChannelIdentityMappingStore = class {
|
|
|
7501
7752
|
this.initialized = true;
|
|
7502
7753
|
return;
|
|
7503
7754
|
}
|
|
7504
|
-
this.pool = typeof options.poolConfig === "string" ? new
|
|
7755
|
+
this.pool = typeof options.poolConfig === "string" ? new Pool25({ connectionString: options.poolConfig }) : options.poolConfig ? new Pool25(options.poolConfig) : (() => {
|
|
7505
7756
|
throw new Error("Either pool or poolConfig must be provided");
|
|
7506
7757
|
})();
|
|
7507
7758
|
this.migrationManager = new MigrationManager(this.pool);
|
|
@@ -7724,10 +7975,12 @@ export {
|
|
|
7724
7975
|
ChannelIdentityMappingStore,
|
|
7725
7976
|
MenuStore,
|
|
7726
7977
|
MigrationManager,
|
|
7727
|
-
|
|
7978
|
+
PGVectorStoreProvider,
|
|
7979
|
+
Pool26 as Pool,
|
|
7728
7980
|
PostgreSQLA2AApiKeyStore,
|
|
7729
7981
|
PostgreSQLAssistantStore,
|
|
7730
7982
|
PostgreSQLChannelInstallationStore,
|
|
7983
|
+
PostgreSQLCollectionStore,
|
|
7731
7984
|
PostgreSQLDatabaseConfigStore,
|
|
7732
7985
|
PostgreSQLEvalStore,
|
|
7733
7986
|
PostgreSQLMcpServerConfigStore,
|
|
@@ -7759,6 +8012,7 @@ export {
|
|
|
7759
8012
|
createChannelBindingsTable,
|
|
7760
8013
|
createChannelIdentityMappingTables,
|
|
7761
8014
|
createChannelInstallationsTable,
|
|
8015
|
+
createCollectionsTable,
|
|
7762
8016
|
createDatabaseConfigsTable,
|
|
7763
8017
|
createEvalCasesTable,
|
|
7764
8018
|
createEvalProjectsTable,
|
|
@@ -7768,6 +8022,7 @@ export {
|
|
|
7768
8022
|
createMcpServerConfigsTable,
|
|
7769
8023
|
createMenuItemsTable,
|
|
7770
8024
|
createMetricsConfigsTable,
|
|
8025
|
+
createPGVectorStoreProvider,
|
|
7771
8026
|
createPgStoreConfig,
|
|
7772
8027
|
createProjectsTable,
|
|
7773
8028
|
createScheduledTasksTable,
|